-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshFogMaterial.js
50 lines (45 loc) · 1.8 KB
/
MeshFogMaterial.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
import {MeshStandardMaterial, Color, Vector4} from 'three'
export class MeshFoggyMaterial extends MeshStandardMaterial {
constructor( parameters ) {
super()
this.uniforms = {
fPlane: { value: parameters.fogPlane ? parameters.fogPlane : new Vector4() },
fDepth: { value: parameters.fogDepth ? parameters.fogDepth : 200 },
fColor: { value: parameters.fogColor ? parameters.fogColor.isColor ? parameters.fogColor : new Color(parameters.fogColor) : new Color(0xffffff) }
}
for (const property in this) {
if (property in parameters){
if(this[property].isColor){
this[property] = parameters[property].isColor ? parameters[property] : new Color(parameters[property])
}
else{
this[property] = parameters[property];
}
}
}
this.onBeforeCompile = (shader) => {
shader.uniforms = {
...shader.uniforms,
...this.uniforms
}
shader.fragmentShader = `
uniform vec4 fPlane;
uniform float fDepth;
uniform vec3 fColor;
` + shader.fragmentShader;
shader.fragmentShader = shader.fragmentShader.replace(
`#include <clipping_planes_fragment>`,
`#include <clipping_planes_fragment>
float planeFog = 0.0;
planeFog = smoothstep(0.0, -fDepth, dot( vViewPosition, fPlane.xyz) - fPlane.w);
`
);
shader.fragmentShader = shader.fragmentShader.replace(
`#include <fog_fragment>`,
`#include <fog_fragment>
gl_FragColor.rgb = mix( gl_FragColor.rgb, fColor, planeFog );
`
)
}
}
}