Skip to content

Commit 0a935c3

Browse files
✨ debug mode can render shadow map
This appears black if alphaTest for mesh is changed to != 0.5
1 parent a3322b9 commit 0a935c3

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

ipyvolume/pylab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def figure(
231231
if controls_light:
232232
globals()['controls_light']()
233233
if debug:
234-
show = ipywidgets.ToggleButtons(options=["Volume", "Back", "Front", "Coordinate"])
234+
show = ipywidgets.ToggleButtons(options=["Volume", "Back", "Front", "Coordinate", "Shadow"])
235235
current.container.children += (show,)
236236
# ipywidgets.jslink((current.figure, 'show'), (show, 'value'))
237237
traitlets.link((current.figure, 'show'), (show, 'value'))

js/src/figure.ts

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,22 +2197,92 @@ class FigureView extends widgets.DOMWidgetView {
21972197
});
21982198
}
21992199

2200-
// render to screen
2201-
this.screen_texture = {
2202-
Volume: this.color_pass_target,
2203-
Back: this.volume_back_target,
2204-
Geometry_back: this.geometry_depth_target,
2205-
Coordinate: this.coordinate_target,
2206-
}[this.model.get("show")];
2207-
// TODO: remove any
2208-
this.screen_material.uniforms.tex.value = (this.screen_texture as any).texture;
2209-
2210-
this.renderer.setRenderTarget(null);
2211-
this.renderer.clear(true, true, true);
2212-
this.renderer.render(this.screen_scene, this.screen_camera);
2200+
if(this.model.get("show") == "Shadow") {
2201+
this._render_shadow();
2202+
} else {
2203+
// render to screen
2204+
this.screen_texture = {
2205+
Volume: this.color_pass_target,
2206+
Back: this.volume_back_target,
2207+
Geometry_back: this.geometry_depth_target,
2208+
Coordinate: this.coordinate_target,
2209+
}[this.model.get("show")];
2210+
// TODO: remove any
2211+
this.screen_material.uniforms.tex.value = (this.screen_texture as any).texture;
2212+
2213+
this.renderer.setRenderTarget(null);
2214+
this.renderer.clear(true, true, true);
2215+
this.renderer.render(this.screen_scene, this.screen_camera);
2216+
}
22132217
restoreVisible();
22142218
}
22152219

2220+
_render_shadow() {
2221+
const lights = this.model.get('lights').map((model) => model.obj).filter((light) => {
2222+
return Boolean(light.shadow) && Boolean(light.shadow.map);
2223+
});
2224+
if(lights.length == 0) {
2225+
throw "No light with a shadow map found."
2226+
}
2227+
const light = lights[0];
2228+
const target = light.shadow.map;
2229+
const textureWidth = target.width;
2230+
const textureHeight = target.height;
2231+
const quadCamera = new THREE.OrthographicCamera (textureWidth / - 2, textureHeight / 2, textureWidth / 2, textureHeight / - 2, -1000, 1000);
2232+
quadCamera.position.z = 100;
2233+
var quadScene = new THREE.Scene();
2234+
2235+
const quadMaterial = new THREE.ShaderMaterial ({
2236+
2237+
uniforms:
2238+
{
2239+
map: { type: "t", value: null },
2240+
},
2241+
2242+
vertexShader:
2243+
[
2244+
"varying vec2 vUv;",
2245+
2246+
"void main ()",
2247+
"{",
2248+
"vUv = vec2 (uv.x, 1.0 - uv.y);",
2249+
"gl_Position = projectionMatrix * modelViewMatrix * vec4 (position, 1.0);",
2250+
"}"
2251+
].join("\n"),
2252+
2253+
fragmentShader:
2254+
[
2255+
"uniform sampler2D map;",
2256+
"varying vec2 vUv;",
2257+
"#include <packing>",
2258+
2259+
"float unpack_depth (const in vec4 rgba_depth)",
2260+
"{",
2261+
"const vec4 bit_shift = vec4 (1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);",
2262+
"float depth = dot (rgba_depth, bit_shift);",
2263+
2264+
"return depth;",
2265+
"}",
2266+
2267+
"void main ()",
2268+
"{",
2269+
"vec4 rgbaDepth = texture2D (map, vUv);",
2270+
"float fDepth = unpackRGBAToDepth(rgbaDepth);",
2271+
2272+
"gl_FragColor = vec4 (vec3 (fDepth), 1.0);",
2273+
"}"
2274+
].join("\n"),
2275+
2276+
blending: THREE.NoBlending,
2277+
depthTest: false,
2278+
depthWrite: false,
2279+
});
2280+
2281+
quadScene.add (new THREE.Mesh (new THREE.PlaneGeometry (textureWidth, textureHeight), quadMaterial));
2282+
quadMaterial.uniforms.map.value = target;
2283+
this.renderer.render(quadScene, quadCamera);
2284+
}
2285+
22162286
rebuild_multivolume_rendering_material() {
22172287
const volumes = this.model.get("volumes") as VolumeModel[]; // This is always a list?
22182288
if (volumes.length === 0) {

0 commit comments

Comments
 (0)