ShadowMesh.js 2.0 KB

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