Browse Source

Create ShadowMesh.js

erichlof 10 years ago
parent
commit
76bd6d1379
1 changed files with 73 additions and 0 deletions
  1. 73 0
      src/objects/ShadowMesh.js

+ 73 - 0
src/objects/ShadowMesh.js

@@ -0,0 +1,73 @@
+// File:src/objects/ShadowMesh.js
+
+/**
+ * @author erichlof /  http://github.com/erichlof
+ */
+
+// a shadow Mesh that follows its parent Mesh in the scene, but is confined to a single plane.
+
+THREE.ShadowMesh = function ( parentMesh ) {
+	
+	var shadowMaterial = new THREE.MeshBasicMaterial( {
+		
+		color: 0x000000,
+		transparent: true,
+		opacity: 0.6,
+		depthWrite: false
+		
+	} );
+	
+	THREE.Mesh.call( this, parentMesh.geometry, shadowMaterial );
+	
+	this.parentMeshMatrix = parentMesh.matrix;
+	
+	this.frustumCulled = false;
+
+};
+
+THREE.ShadowMesh.prototype = Object.create( THREE.Mesh.prototype );
+THREE.ShadowMesh.prototype.constructor = THREE.ShadowMesh;
+
+THREE.ShadowMesh.prototype.update = function () {
+
+	// based on https://www.opengl.org/archives/resources/features/StencilTalk/tsld021.htm
+	var shadowMatrix = new THREE.Matrix4();
+
+	return function ( plane, lightPosition4D ) {
+		
+		var dot = plane.normal.x * lightPosition4D.x +
+			  plane.normal.y * lightPosition4D.y +
+			  plane.normal.z * lightPosition4D.z +
+			  -plane.constant * lightPosition4D.w;
+
+		var sme = shadowMatrix.elements;
+
+		sme[ 0 ]  = dot - lightPosition4D.x * plane.normal.x;
+		sme[ 4 ]  = - lightPosition4D.x * plane.normal.y;
+		sme[ 8 ]  = - lightPosition4D.x * plane.normal.z;
+		sme[ 12 ] = - lightPosition4D.x * -plane.constant;
+
+		sme[ 1 ]  = - lightPosition4D.y * plane.normal.x;
+		sme[ 5 ]  = dot - lightPosition4D.y * plane.normal.y;
+		sme[ 9 ]  = - lightPosition4D.y * plane.normal.z;
+		sme[ 13 ] = - lightPosition4D.y * -plane.constant;
+
+		sme[ 2 ]  = - lightPosition4D.z * plane.normal.x;
+		sme[ 6 ]  = - lightPosition4D.z * plane.normal.y;
+		sme[ 10 ] = dot - lightPosition4D.z * plane.normal.z;
+		sme[ 14 ] = - lightPosition4D.z * -plane.constant;
+
+		sme[ 3 ]  = - lightPosition4D.w * plane.normal.x;
+		sme[ 7 ]  = - lightPosition4D.w * plane.normal.y;
+		sme[ 11 ] = - lightPosition4D.w * plane.normal.z;
+		sme[ 15 ] = dot - lightPosition4D.w * -plane.constant;
+	
+		this.matrix.identity();
+		this.matrix.multiply( shadowMatrix );
+		this.matrix.multiply( this.parentMeshMatrix );
+		
+		this.matrixAutoUpdate = false;
+
+	};
+
+}();