ShadowMesh.js 2.0 KB

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