ShadowMesh.js 2.0 KB

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