ShadowMesh.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. console.warn( "THREE.ShadowMesh: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author erichlof / http://github.com/erichlof
  4. *
  5. * A shadow Mesh that follows a shadow-casting Mesh in the scene, but is confined to a single plane.
  6. */
  7. THREE.ShadowMesh = function ( mesh ) {
  8. var shadowMaterial = new THREE.MeshBasicMaterial( {
  9. color: 0x000000,
  10. transparent: true,
  11. opacity: 0.6,
  12. depthWrite: false
  13. } );
  14. THREE.Mesh.call( this, mesh.geometry, shadowMaterial );
  15. this.meshMatrix = mesh.matrixWorld;
  16. this.frustumCulled = false;
  17. this.matrixAutoUpdate = false;
  18. };
  19. THREE.ShadowMesh.prototype = Object.create( THREE.Mesh.prototype );
  20. THREE.ShadowMesh.prototype.constructor = THREE.ShadowMesh;
  21. THREE.ShadowMesh.prototype.update = function () {
  22. var shadowMatrix = new THREE.Matrix4();
  23. return function ( plane, lightPosition4D ) {
  24. // based on https://www.opengl.org/archives/resources/features/StencilTalk/tsld021.htm
  25. var dot = plane.normal.x * lightPosition4D.x +
  26. plane.normal.y * lightPosition4D.y +
  27. plane.normal.z * lightPosition4D.z +
  28. - plane.constant * lightPosition4D.w;
  29. var sme = shadowMatrix.elements;
  30. sme[ 0 ] = dot - lightPosition4D.x * plane.normal.x;
  31. sme[ 4 ] = - lightPosition4D.x * plane.normal.y;
  32. sme[ 8 ] = - lightPosition4D.x * plane.normal.z;
  33. sme[ 12 ] = - lightPosition4D.x * - plane.constant;
  34. sme[ 1 ] = - lightPosition4D.y * plane.normal.x;
  35. sme[ 5 ] = dot - lightPosition4D.y * plane.normal.y;
  36. sme[ 9 ] = - lightPosition4D.y * plane.normal.z;
  37. sme[ 13 ] = - lightPosition4D.y * - plane.constant;
  38. sme[ 2 ] = - lightPosition4D.z * plane.normal.x;
  39. sme[ 6 ] = - lightPosition4D.z * plane.normal.y;
  40. sme[ 10 ] = dot - lightPosition4D.z * plane.normal.z;
  41. sme[ 14 ] = - lightPosition4D.z * - plane.constant;
  42. sme[ 3 ] = - lightPosition4D.w * plane.normal.x;
  43. sme[ 7 ] = - lightPosition4D.w * plane.normal.y;
  44. sme[ 11 ] = - lightPosition4D.w * plane.normal.z;
  45. sme[ 15 ] = dot - lightPosition4D.w * - plane.constant;
  46. this.matrix.multiplyMatrices( shadowMatrix, this.meshMatrix );
  47. };
  48. }();