1 line
8.9 KiB
Plaintext
1 line
8.9 KiB
Plaintext
|
|
{"version":3,"file":"CSMHelper.cjs","sources":["../../src/csm/CSMHelper.js"],"sourcesContent":["import {\n Group,\n Mesh,\n LineSegments,\n BufferGeometry,\n LineBasicMaterial,\n Box3Helper,\n Box3,\n PlaneGeometry,\n MeshBasicMaterial,\n BufferAttribute,\n DoubleSide,\n} from 'three'\n\nclass CSMHelper extends Group {\n constructor(csm) {\n super()\n this.csm = csm\n this.displayFrustum = true\n this.displayPlanes = true\n this.displayShadowBounds = true\n\n const indices = new Uint16Array([0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7])\n const positions = new Float32Array(24)\n const frustumGeometry = new BufferGeometry()\n frustumGeometry.setIndex(new BufferAttribute(indices, 1))\n frustumGeometry.setAttribute('position', new BufferAttribute(positions, 3, false))\n const frustumLines = new LineSegments(frustumGeometry, new LineBasicMaterial())\n this.add(frustumLines)\n\n this.frustumLines = frustumLines\n this.cascadeLines = []\n this.cascadePlanes = []\n this.shadowLines = []\n }\n\n updateVisibility() {\n const displayFrustum = this.displayFrustum\n const displayPlanes = this.displayPlanes\n const displayShadowBounds = this.displayShadowBounds\n\n const frustumLines = this.frustumLines\n const cascadeLines = this.cascadeLines\n const cascadePlanes = this.cascadePlanes\n const shadowLines = this.shadowLines\n for (let i = 0, l = cascadeLines.length; i < l; i++) {\n const cascadeLine = cascadeLines[i]\n const cascadePlane = cascadePlanes[i]\n const shadowLineGroup = shadowLines[i]\n\n cascadeLine.visible = displayFrustum\n cascadePlane.visible = displayFrustum && displayPlanes\n shadowLineGroup.visible = displayShadowBounds\n }\n\n frustumLines.visible = displayFrustum\n }\n\n update() {\n const csm = this.csm\n const camera = csm.camera\n const cascades = csm.cascades\n const mainFrustum = csm.mainFrustum\n const frustums = csm.frustums\n const lights = csm.lights\n\n const frustumLines = this.frustumLines\n const frustumLinePositions = frustumLines.geometry.getAttribute('position')\n const cascadeLines = this.cascadeLines\n const cascadePlanes = this.cascadePlanes\n const shadowLines = this.shadowLines\n\n this.position.copy(camera.position)\n this.quaternion.copy(camera.quaternion)\n this.scale.copy(camera.scale)\n this.updateMatrixWorld(true)\n\n while (cascadeLines.length > cascades) {\n this.remove(cascadeLines.pop())\n this.remove(cascadePlanes.pop())\n this.remove(shadowLines.pop())\n }\n\n while (cascadeLines.length < cascades) {\n const cascadeLine = new Box3Helper(new Box3(), 0xffffff)\n const planeMat = new MeshBasicMaterial({ transparent: true, opacity: 0.1, depthWrite: false, side: DoubleSide })\n const cascadePlane = new Mesh(new PlaneGeometry(), planeMat)\n const shadowLineGroup = new Group()\n const shadowLine = new Box3Helper(new Box3(), 0xffff00)\n shadowLineGroup.add(shadowLine)\n\n this.add(cascadeLine)\n this.add(cascadePlane)\n this.add(shadowLineGroup)\n\n cascadeLines.push(cascadeLine)\n cascadePlanes.push(cascadePlane)\n shadowLines.push(shadowLineGroup)\n }\n\n for (let i = 0; i < cascades; i++) {\n const frustum = frustums[i]\n const light = lights[i]\n const shadowCam = light.shadow.camera\n const farVerts = frustum.vertices.far\n\n const cascadeLine = cascadeLines[i]\n const cascadePlane = cascadePlanes[i]\n const shadowLineGroup = shadowLines[i]\n const shadowLine = shadowLineGroup.children[0]\n\n cascadeLine.box.min.copy(farVerts[2])\n cascadeLine.box.max.copy(farVerts[0])\n cascadeLine.box.max.z += 1e-4\n\n cascadePlane.position.addVectors(farVerts[0], farVerts[2])\n cascadePlane.position.multiplyScalar(0.5)\n cascadePlane.scale.subVectors(farVerts[0], farVerts[2])\n cascadePlane.scale.z = 1e-4\n\n this.remove(shadowLineGroup
|