2
0

ShadowMesh.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * A shadow Mesh that follows a shadow-casting Mesh in the scene, but is confined to a single plane.
  3. */
  4. THREE.ShadowMesh = function ( mesh ) {
  5. var shadowMaterial = new THREE.MeshBasicMaterial( {
  6. color: 0x000000,
  7. transparent: true,
  8. opacity: 0.6,
  9. depthWrite: false
  10. } );
  11. THREE.Mesh.call( this, mesh.geometry, shadowMaterial );
  12. this.meshMatrix = mesh.matrixWorld;
  13. this.frustumCulled = false;
  14. this.matrixAutoUpdate = false;
  15. };
  16. THREE.ShadowMesh.prototype = Object.create( THREE.Mesh.prototype );
  17. THREE.ShadowMesh.prototype.constructor = THREE.ShadowMesh;
  18. THREE.ShadowMesh.prototype.update = function () {
  19. var shadowMatrix = new THREE.Matrix4();
  20. return function ( plane, lightPosition4D ) {
  21. // based on https://www.opengl.org/archives/resources/features/StencilTalk/tsld021.htm
  22. var dot = plane.normal.x * lightPosition4D.x +
  23. plane.normal.y * lightPosition4D.y +
  24. plane.normal.z * lightPosition4D.z +
  25. - plane.constant * lightPosition4D.w;
  26. var sme = shadowMatrix.elements;
  27. sme[ 0 ] = dot - lightPosition4D.x * plane.normal.x;
  28. sme[ 4 ] = - lightPosition4D.x * plane.normal.y;
  29. sme[ 8 ] = - lightPosition4D.x * plane.normal.z;
  30. sme[ 12 ] = - lightPosition4D.x * - plane.constant;
  31. sme[ 1 ] = - lightPosition4D.y * plane.normal.x;
  32. sme[ 5 ] = dot - lightPosition4D.y * plane.normal.y;
  33. sme[ 9 ] = - lightPosition4D.y * plane.normal.z;
  34. sme[ 13 ] = - lightPosition4D.y * - plane.constant;
  35. sme[ 2 ] = - lightPosition4D.z * plane.normal.x;
  36. sme[ 6 ] = - lightPosition4D.z * plane.normal.y;
  37. sme[ 10 ] = dot - lightPosition4D.z * plane.normal.z;
  38. sme[ 14 ] = - lightPosition4D.z * - plane.constant;
  39. sme[ 3 ] = - lightPosition4D.w * plane.normal.x;
  40. sme[ 7 ] = - lightPosition4D.w * plane.normal.y;
  41. sme[ 11 ] = - lightPosition4D.w * plane.normal.z;
  42. sme[ 15 ] = dot - lightPosition4D.w * - plane.constant;
  43. this.matrix.multiplyMatrices( shadowMatrix, this.meshMatrix );
  44. };
  45. }();