Skip to content

Commit d99e633

Browse files
committed
Add support for depth copying
1 parent b09ff79 commit d99e633

File tree

2 files changed

+184
-11
lines changed

2 files changed

+184
-11
lines changed

src/materials/CopyMaterial.js

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NoBlending, ShaderMaterial, Uniform } from "three";
1+
import { AlwaysDepth, Color, NoBlending, ShaderMaterial, Uniform } from "three";
22

33
import fragmentShader from "./glsl/copy.frag";
44
import vertexShader from "./glsl/common.vert";
@@ -17,8 +17,14 @@ export class CopyMaterial extends ShaderMaterial {
1717

1818
super({
1919
name: "CopyMaterial",
20+
defines: {
21+
DEPTH_PACKING: "0",
22+
COLOR_WRITE: "1"
23+
},
2024
uniforms: {
2125
inputBuffer: new Uniform(null),
26+
depthBuffer: new Uniform(null),
27+
color: new Uniform(new Color()),
2228
opacity: new Uniform(1.0)
2329
},
2430
blending: NoBlending,
@@ -29,6 +35,8 @@ export class CopyMaterial extends ShaderMaterial {
2935
vertexShader
3036
});
3137

38+
this.depthFunc = AlwaysDepth;
39+
3240
}
3341

3442
/**
@@ -37,12 +45,111 @@ export class CopyMaterial extends ShaderMaterial {
3745
* @type {Texture}
3846
*/
3947

48+
get inputBuffer() {
49+
50+
return this.uniforms.inputBuffer.value;
51+
52+
}
53+
4054
set inputBuffer(value) {
4155

56+
const colorWrite = value !== null;
57+
58+
if(this.colorWrite !== colorWrite) {
59+
60+
if(colorWrite) {
61+
62+
this.defines.COLOR_WRITE = true;
63+
64+
} else {
65+
66+
delete this.defines.COLOR_WRITE;
67+
68+
}
69+
70+
this.colorWrite = colorWrite;
71+
this.needsUpdate = true;
72+
73+
}
74+
4275
this.uniforms.inputBuffer.value = value;
4376

4477
}
4578

79+
/**
80+
* A depth buffer.
81+
*
82+
* @type {Texture}
83+
*/
84+
85+
get depthBuffer() {
86+
87+
return this.uniforms.depthBuffer.value;
88+
89+
}
90+
91+
set depthBuffer(value) {
92+
93+
const depthWrite = value !== null;
94+
95+
if(this.depthWrite !== depthWrite) {
96+
97+
if(depthWrite) {
98+
99+
this.defines.DEPTH_WRITE = true;
100+
101+
} else {
102+
103+
delete this.defines.DEPTH_WRITE;
104+
105+
}
106+
107+
this.depthTest = depthWrite;
108+
this.depthWrite = depthWrite;
109+
this.needsUpdate = true;
110+
111+
}
112+
113+
this.uniforms.depthBuffer.value = value;
114+
115+
}
116+
117+
/**
118+
* The depth packing strategy of the depth buffer.
119+
*
120+
* @type {DepthPackingStrategies}
121+
*/
122+
123+
set depthPacking(value) {
124+
125+
this.defines.DEPTH_PACKING = value.toFixed(0);
126+
this.needsUpdate = true;
127+
128+
}
129+
130+
/**
131+
* A color that should be applied to the input buffer.
132+
*
133+
* @type {Color | Number}
134+
*/
135+
136+
set color(value) {
137+
138+
if(value !== null) {
139+
140+
this.defines.USE_COLOR = "1";
141+
this.uniforms.color.value.set(value);
142+
143+
} else {
144+
145+
delete this.defines.USE_COLOR;
146+
147+
}
148+
149+
this.needsUpdate = true;
150+
151+
}
152+
46153
/**
47154
* Sets the input buffer.
48155
*

src/materials/glsl/copy.frag

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,53 @@
1-
#include <common>
2-
#include <dithering_pars_fragment>
1+
#ifdef COLOR_WRITE
32

4-
#ifdef FRAMEBUFFER_PRECISION_HIGH
3+
#include <common>
4+
#include <dithering_pars_fragment>
55

6-
uniform mediump sampler2D inputBuffer;
6+
#ifdef FRAMEBUFFER_PRECISION_HIGH
77

8-
#else
8+
uniform mediump sampler2D inputBuffer;
99

10-
uniform lowp sampler2D inputBuffer;
10+
#else
11+
12+
uniform lowp sampler2D inputBuffer;
13+
14+
#endif
15+
16+
#endif
17+
18+
#ifdef DEPTH_WRITE
19+
20+
#include <packing>
21+
22+
#ifdef GL_FRAGMENT_PRECISION_HIGH
23+
24+
uniform highp sampler2D depthBuffer;
25+
26+
#else
27+
28+
uniform mediump sampler2D depthBuffer;
29+
30+
#endif
31+
32+
float readDepth(const in vec2 uv) {
33+
34+
#if DEPTH_PACKING == 3201
35+
36+
return unpackRGBAToDepth(texture2D(depthBuffer, uv));
37+
38+
#else
39+
40+
return texture2D(depthBuffer, uv).r;
41+
42+
#endif
43+
44+
}
45+
46+
#endif
47+
48+
#ifdef USE_COLOR
49+
50+
uniform vec3 color;
1151

1252
#endif
1353

@@ -17,10 +57,36 @@ varying vec2 vUv;
1757

1858
void main() {
1959

20-
vec4 texel = texture2D(inputBuffer, vUv);
21-
gl_FragColor = opacity * texel;
60+
#ifdef COLOR_WRITE
61+
62+
vec4 texel = texture2D(inputBuffer, vUv);
63+
64+
#ifdef USE_COLOR
65+
66+
texel.rgb *= color;
67+
68+
#endif
69+
70+
gl_FragColor = opacity * texel;
71+
72+
#ifdef COLOR_SPACE_CONVERSION
73+
74+
#include <colorspace_fragment>
75+
76+
#endif
77+
78+
#include <dithering_fragment>
79+
80+
#else
81+
82+
gl_FragColor = vec4(0.0);
83+
84+
#endif
85+
86+
#ifdef DEPTH_WRITE
87+
88+
gl_FragDepth = readDepth(vUv);
2289

23-
#include <colorspace_fragment>
24-
#include <dithering_fragment>
90+
#endif
2591

2692
}

0 commit comments

Comments
 (0)