2
0

ShadowMesh.js 2.0 KB

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