|
| 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 | + |
| 7 | +import { |
| 8 | + Matrix4, |
| 9 | + Mesh, |
| 10 | + MeshBasicMaterial |
| 11 | +} from "../../../build/three.module.js"; |
| 12 | + |
| 13 | +var ShadowMesh = function ( mesh ) { |
| 14 | + |
| 15 | + var shadowMaterial = new MeshBasicMaterial( { |
| 16 | + |
| 17 | + color: 0x000000, |
| 18 | + transparent: true, |
| 19 | + opacity: 0.6, |
| 20 | + depthWrite: false |
| 21 | + |
| 22 | + } ); |
| 23 | + |
| 24 | + Mesh.call( this, mesh.geometry, shadowMaterial ); |
| 25 | + |
| 26 | + this.meshMatrix = mesh.matrixWorld; |
| 27 | + |
| 28 | + this.frustumCulled = false; |
| 29 | + this.matrixAutoUpdate = false; |
| 30 | + |
| 31 | +}; |
| 32 | + |
| 33 | +ShadowMesh.prototype = Object.create( Mesh.prototype ); |
| 34 | +ShadowMesh.prototype.constructor = ShadowMesh; |
| 35 | + |
| 36 | +ShadowMesh.prototype.update = function () { |
| 37 | + |
| 38 | + var shadowMatrix = new Matrix4(); |
| 39 | + |
| 40 | + return function ( plane, lightPosition4D ) { |
| 41 | + |
| 42 | + // based on https://www.opengl.org/archives/resources/features/StencilTalk/tsld021.htm |
| 43 | + |
| 44 | + var dot = plane.normal.x * lightPosition4D.x + |
| 45 | + plane.normal.y * lightPosition4D.y + |
| 46 | + plane.normal.z * lightPosition4D.z + |
| 47 | + - plane.constant * lightPosition4D.w; |
| 48 | + |
| 49 | + var sme = shadowMatrix.elements; |
| 50 | + |
| 51 | + sme[ 0 ] = dot - lightPosition4D.x * plane.normal.x; |
| 52 | + sme[ 4 ] = - lightPosition4D.x * plane.normal.y; |
| 53 | + sme[ 8 ] = - lightPosition4D.x * plane.normal.z; |
| 54 | + sme[ 12 ] = - lightPosition4D.x * - plane.constant; |
| 55 | + |
| 56 | + sme[ 1 ] = - lightPosition4D.y * plane.normal.x; |
| 57 | + sme[ 5 ] = dot - lightPosition4D.y * plane.normal.y; |
| 58 | + sme[ 9 ] = - lightPosition4D.y * plane.normal.z; |
| 59 | + sme[ 13 ] = - lightPosition4D.y * - plane.constant; |
| 60 | + |
| 61 | + sme[ 2 ] = - lightPosition4D.z * plane.normal.x; |
| 62 | + sme[ 6 ] = - lightPosition4D.z * plane.normal.y; |
| 63 | + sme[ 10 ] = dot - lightPosition4D.z * plane.normal.z; |
| 64 | + sme[ 14 ] = - lightPosition4D.z * - plane.constant; |
| 65 | + |
| 66 | + sme[ 3 ] = - lightPosition4D.w * plane.normal.x; |
| 67 | + sme[ 7 ] = - lightPosition4D.w * plane.normal.y; |
| 68 | + sme[ 11 ] = - lightPosition4D.w * plane.normal.z; |
| 69 | + sme[ 15 ] = dot - lightPosition4D.w * - plane.constant; |
| 70 | + |
| 71 | + this.matrix.multiplyMatrices( shadowMatrix, this.meshMatrix ); |
| 72 | + |
| 73 | + }; |
| 74 | + |
| 75 | +}(); |
| 76 | + |
| 77 | +export { ShadowMesh }; |
0 commit comments