-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
GBufferMaterial.js
134 lines (103 loc) · 4.51 KB
/
GBufferMaterial.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* eslint-disable camelcase */
import { MeshPhysicalMaterial, Vector2 } from "three"
import gbuffer_packing from "../shader/gbuffer_packing.glsl"
import { setupBlueNoise } from "../../utils/BlueNoiseUtils"
class GBufferMaterial extends MeshPhysicalMaterial {
onBeforeCompile(shader) {
this.uniforms = shader.uniforms
shader.uniforms.resolution = { value: new Vector2(1, 1) }
shader.uniforms.cameraNotMovedFrames = { value: 0 }
// delete all includes that have the pattern "#include <lights_something>"
shader.vertexShader = shader.vertexShader.replace(/#include <lights_.*>/g, "")
shader.fragmentShader = shader.fragmentShader.replace(/#include <lights_.*>/g, "")
// delete all includes that have the pattern "#include <alpha...>"
shader.vertexShader = shader.vertexShader.replace(/#include <alpha.*>/g, "")
shader.fragmentShader = shader.fragmentShader.replace(/#include <alpha.*>/g, "")
// delete all includes that have the pattern "#include <aomap...>"
shader.vertexShader = shader.vertexShader.replace(/#include <aomap.*>/g, "")
shader.fragmentShader = shader.fragmentShader.replace(/#include <aomap.*>/g, "")
// delete all includes that have the pattern "#include <lightmap...>"
shader.vertexShader = shader.vertexShader.replace(/#include <lightmap.*>/g, "")
shader.fragmentShader = shader.fragmentShader.replace(/#include <lightmap.*>/g, "")
// delete all includes that have the pattern "#include <alphahash...>"
shader.vertexShader = shader.vertexShader.replace(/#include <alphahash.*>/g, "")
shader.fragmentShader = shader.fragmentShader.replace(/#include <alphahash.*>/g, "")
// delete all includes that have the pattern "#include <alphatest...>"
shader.vertexShader = shader.vertexShader.replace(/#include <alphatest.*>/g, "")
shader.fragmentShader = shader.fragmentShader.replace(/#include <alphatest.*>/g, "")
// remove opaque_fragment include
shader.fragmentShader = shader.fragmentShader.replace("#include <opaque_fragment>", "")
// remove colorspace_fragment include
shader.fragmentShader = shader.fragmentShader.replace("#include <colorspace_fragment>", "")
// delete the fog_fragment include
shader.fragmentShader = shader.fragmentShader.replace("#include <fog_fragment>", "")
shader.fragmentShader = shader.fragmentShader
.replace(
"void main() {",
/* glsl */ `
#define vUv gl_FragCoord.xy
uniform vec2 resolution;
uniform float cameraNotMovedFrames;
${gbuffer_packing}
void main() {
float a = opacity;
#ifdef USE_ALPHAMAP
a *= texture2D( alphaMap, vAlphaMapUv ).g;
#endif
if (cameraNotMovedFrames == 0.) {
if(a < 0.5) {
discard;
return;
}
a = 1.;
} else if (a != 1.) {
float aStep = a > 0.5 ? 1. : 0.;
a = mix(a, aStep, (1. / (cameraNotMovedFrames * 0.1 + 1.)));
vec4 noise = blueNoise();
if (noise.x > a) {
discard;
return;
}
}
`
)
.replace(
"#include <dithering_fragment>",
/* glsl */ `
#include <dithering_fragment>
vec3 worldNormal = normalize((vec4(normal, 1.) * viewMatrix).xyz);
vec4 gBuffer = packGBuffer(diffuseColor, worldNormal, roughnessFactor, metalnessFactor, totalEmissiveRadiance);
gl_FragColor = gBuffer;`
)
const { uniforms, fragmentShader } = setupBlueNoise(shader.fragmentShader)
shader.uniforms = { ...shader.uniforms, ...uniforms }
shader.fragmentShader = fragmentShader
}
}
const gBufferMaterial = new GBufferMaterial()
export function createGBufferMaterial(originalMaterial) {
const material = gBufferMaterial.clone()
copyAllPropsToGBufferMaterial(originalMaterial, material)
return material
}
let props = Object.keys(gBufferMaterial)
// delete the ones that start with "_"
props = props.filter(
key => !key.startsWith("_") && !key.startsWith("is") && key !== "uuid" && key !== "type" && key !== "transparent"
)
// this function attempts to copy all the props from the original material to the new GBufferMaterial
function copyAllPropsToGBufferMaterial(originalMaterial, gBufferMaterial) {
for (const key of props) {
if (originalMaterial[key] !== undefined) {
gBufferMaterial[key] = originalMaterial[key]
}
}
}
const propsPrimitive = props.filter(
key => typeof gBufferMaterial[key] === "string" || typeof gBufferMaterial[key] === "number"
)
export function copyPropsToGBufferMaterial(originalMaterial, gBufferMaterial) {
for (const prop of propsPrimitive) {
gBufferMaterial[prop] = originalMaterial[prop]
}
}