-
Notifications
You must be signed in to change notification settings - Fork 3
/
shadow-plane.js
77 lines (65 loc) · 2.63 KB
/
shadow-plane.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
ShadowPlane = function (width, length, shadowColor, upVector) {
THREE.Object3D.call( this );
this.width = width || 200;
this.length = length || 200;
this.shadowColor = shadowColor || new THREE.Color(1,1,1);
this.upVector = upVector || new THREE.Vector3(0,1,0);
this.userData.unselectable = true; // this should never be selectable
this._drawPlane();
}
ShadowPlane.prototype = Object.create( THREE.Object3D.prototype );
ShadowPlane.prototype._drawPlane=function(){
//create plane for shadow projection
var width = this.width;
var length = this.length;
var shadowColor = this.shadowColor;
var planeGeometry = new THREE.PlaneBufferGeometry(-width, length, 1, 1);
planeFragmentShader = [
"uniform vec3 diffuse;",
"uniform float opacity;",
"uniform vec3 shadowColor;",
THREE.ShaderChunk["color_pars_fragment"],
THREE.ShaderChunk["map_pars_fragment"],
THREE.ShaderChunk["lightmap_pars_fragment"],
THREE.ShaderChunk["envmap_pars_fragment"],
THREE.ShaderChunk["fog_pars_fragment"],
THREE.ShaderChunk["shadowmap_pars_fragment"],
THREE.ShaderChunk["specularmap_pars_fragment"],
"void main() {",
"gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );",
THREE.ShaderChunk["map_fragment"],
THREE.ShaderChunk["alphatest_fragment"],
THREE.ShaderChunk["specularmap_fragment"],
THREE.ShaderChunk["lightmap_fragment"],
THREE.ShaderChunk["color_fragment"],
THREE.ShaderChunk["envmap_fragment"],
THREE.ShaderChunk["shadowmap_fragment"],
THREE.ShaderChunk["linear_to_gamma_fragment"],
THREE.ShaderChunk["fog_fragment"],
"gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 - shadowColor.x );",
"}"
].join("\n");
//= vec3(0.0,0.0,0.0)
var uniforms = THREE.ShaderLib['basic'].uniforms;
if( ! ("shadowColor" in uniforms) ) {uniforms["shadowColor"] = {type:'c',value:shadowColor }; }
planeMaterial = new THREE.ShaderMaterial({
uniforms: uniforms,
vertexShader: THREE.ShaderLib['basic'].vertexShader,
fragmentShader: planeFragmentShader,
color: 0x0000FF,
transparent: true
});
//create plane for shadow projection
this.plane = new THREE.Mesh(planeGeometry, planeMaterial);
this.plane.rotation.x = Math.PI;
this.plane.position.z = -0.2;
this.name = "shadowPlane";
this.plane.receiveShadow = true;
this.plane.castShadow = false;
this.add(this.plane);
}
ShadowPlane.prototype.setUp = function(upVector) {
this.upVector = upVector;
this.up = upVector;
this.lookAt(upVector);
};