-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeshFoggyMaterialNew.js
41 lines (36 loc) · 1.34 KB
/
MeshFoggyMaterialNew.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
import {MeshStandardMaterial, Color} from 'three'
export class MeshFoggyMaterial extends MeshStandardMaterial {
constructor( parameters ) {
super()
console.log(parameters);
this.uniforms = {
fPlane: { value: parameters.fogPlane },
fDepth: { value: parameters.fogDepth },
fColor: { value: parameters.fogColor.isColor ? parameters.fogColor : new Color(parameters.fogColor) },
}
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 );
`
)
}
}
}