-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDepthMapPlane.js
41 lines (38 loc) · 1.1 KB
/
DepthMapPlane.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function DepthMapPlane() {
const { camera } = useThree();
const material = useMemo(
() =>
new THREE.ShaderMaterial({
uniforms: {
uCameraNear: { value: camera.near },
uCameraFar: { value: camera.far },
},
vertexShader: `
varying float vDepth;
void main() {
vec4 worldPosition = modelMatrix * vec4(position, 1.0);
vec4 viewPosition = viewMatrix * worldPosition;
vDepth = -viewPosition.z;
gl_Position = projectionMatrix * viewPosition;
}
`,
fragmentShader: `
uniform float uCameraNear;
uniform float uCameraFar;
varying float vDepth;
void main() {
float depth = (vDepth - uCameraNear) / (uCameraFar - uCameraNear);
depth = 1.0 - depth; // Invert depth to make closer objects white
gl_FragColor = vec4(vec3(depth), 1.0);
}
`,
}),
[camera.near, camera.far]
);
return (
<mesh position={[0, 0, -2]}>
<planeGeometry args={[2, 2]} />
<primitive attach="material" object={material} />
</mesh>
);
}