forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDistanceBasedFog.coffee
More file actions
49 lines (44 loc) · 1.75 KB
/
DistanceBasedFog.coffee
File metadata and controls
49 lines (44 loc) · 1.75 KB
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
import * as THREE from "three"
class DistanceBasedFog
constructor:(options)->
@view=new THREE.Vector3
@farnear=new THREE.Vector2
@color=new THREE.Vector4
return
addShaderToMaterial:(material)->
_this=@
material.onBeforeCompile=(shader)->
shader.uniforms.u_viewPos={value:_this.view}
shader.uniforms.u_fogColor={value:_this.color}
shader.uniforms.u_farnear={value:_this.farnear}
shader.fragmentShader=[
"uniform vec3 u_viewPos;"
"uniform vec4 u_fogColor;"
"uniform vec2 u_farnear;"
shader.fragmentShader
].join("\n")
shader.fragmentShader=shader.fragmentShader.replace(
"gl_FragColor = vec4( outgoingLight, diffuseColor.a );"
[
"float dist=length(u_viewPos-vViewPosition);"
"float fogAmount = smoothstep(u_farnear.x, u_farnear.y, dist);"
"gl_FragColor = vec4( outgoingLight, diffuseColor.a );"
"gl_FragColor = mix(gl_FragColor,u_fogColor,max(0.1,fogAmount));"
].join("\n")
)
shader.vertexShader=[
"uniform float time;"
"uniform mat4 u_worldView;"
"attribute vec4 a_position;"
shader.vertexShader
].join("\n")
shader.vertexShader=shader.vertexShader.replace(
"#include <fog_vertex>"
[
"vec4 vViewPosition4 = modelViewMatrix * vec4(position, 1);"
"vViewPosition = vViewPosition4.xyz;"
].join("\n")
)
return
return
export {DistanceBasedFog}