1 line
8.2 KiB
Plaintext
1 line
8.2 KiB
Plaintext
|
|
{"version":3,"file":"RaycasterHelper.cjs","sources":["../../src/helpers/RaycasterHelper.ts"],"sourcesContent":["/**\n * from https://github.com/gsimone/things/tree/main/packages/three-raycaster-helper\n */\n\nimport {\n BufferAttribute,\n BufferGeometry,\n Float32BufferAttribute,\n InstancedMesh,\n Intersection,\n Line,\n LineBasicMaterial,\n Mesh,\n MeshBasicMaterial,\n Object3D,\n Raycaster,\n SphereGeometry,\n Vector3,\n} from 'three'\n\nconst _o = /* @__PURE__ */ new Object3D()\nconst _v = /* @__PURE__ */ new Vector3()\n\nclass RaycasterHelper extends Object3D {\n raycaster: Raycaster\n hits: Intersection[]\n\n origin: Mesh<SphereGeometry, MeshBasicMaterial>\n near: Line<BufferGeometry, LineBasicMaterial>\n far: Line<BufferGeometry, LineBasicMaterial>\n\n nearToFar: Line<BufferGeometry, LineBasicMaterial>\n originToNear: Line<BufferGeometry, LineBasicMaterial>\n\n hitPoints: InstancedMesh\n\n colors = {\n near: 0xffffff,\n far: 0xffffff,\n originToNear: 0x333333,\n nearToFar: 0xffffff,\n origin: [0x0eec82, 0xff005b],\n }\n\n constructor(raycaster: Raycaster, public numberOfHitsToVisualize = 20) {\n super()\n this.raycaster = raycaster\n\n this.hits = []\n\n this.origin = new Mesh(new SphereGeometry(0.04, 32), new MeshBasicMaterial())\n this.origin.name = 'RaycasterHelper_origin'\n this.origin.raycast = () => null\n\n const size = 0.1\n let geometry = new BufferGeometry()\n // prettier-ignore\n geometry.setAttribute( 'position', new Float32BufferAttribute( [\n - size, size, 0,\n size, size, 0,\n size, - size, 0,\n - size, - size, 0,\n - size, size, 0\n ], 3 ) );\n\n this.near = new Line(geometry, new LineBasicMaterial())\n this.near.name = 'RaycasterHelper_near'\n this.near.raycast = () => null\n\n this.far = new Line(geometry, new LineBasicMaterial())\n this.far.name = 'RaycasterHelper_far'\n this.far.raycast = () => null\n\n this.nearToFar = new Line(new BufferGeometry(), new LineBasicMaterial())\n this.nearToFar.name = 'RaycasterHelper_nearToFar'\n this.nearToFar.raycast = () => null\n\n this.nearToFar.geometry.setFromPoints([_v, _v])\n\n this.originToNear = new Line(this.nearToFar.geometry.clone(), new LineBasicMaterial())\n this.originToNear.name = 'RaycasterHelper_originToNear'\n this.originToNear.raycast = () => null\n\n this.hitPoints = new InstancedMesh(new SphereGeometry(0.04), new MeshBasicMaterial(), this.numberOfHitsToVisualize)\n this.hitPoints.name = 'RaycasterHelper_hits'\n this.hitPoints.raycast = () => null\n\n this.add(this.nearToFar)\n this.add(this.originToNear)\n\n this.add(this.near)\n this.add(this.far)\n\n this.add(this.origin)\n this.add(this.hitPoints)\n\n this.setColors()\n }\n\n setColors = (colors?: Partial<typeof this.colors>) => {\n const _colors = {\n ...this.colors,\n ...colors,\n }\n\n this.near.material.color.set(_colors.near)\n this.far.material.color.set(_colors.far)\n this.nearToFar.material.color.set(_colors.nearToFar)\n this.originToNear.material.color.set(_colors.originToNear)\n }\n\n update = () => {\n const origin = this.raycaster.ray.origin\n const direction = this.raycaster.ray.direction\n\n this.origin.position.copy(origin)\n\n this.near.position.copy(origin).add(direction.clone().multiplyScalar(this.raycaster.near))\n\n this.far.position.copy(origin).add(direction.clone().multiplyScalar(this.raycaster.far))\n\n this.far.lookAt(origin)\n this.near.lookAt(origin)\n\n let pos = this.nearToFar.geometry.getAttribute('position') as BufferAttribute\n pos.set([...this.near.position.toArray(), ...this.far.position.toArray()])\n pos.needsUpdate = true\n\n pos = this.originToNear.geometry.getAttribute('position') as BufferAttribute\n pos.set([...origin.toArray(), ...this.near.position.toArray()])\n pos.needsUpdate = true\n\n /**\n * Update hit points visualization\n */\n for (let i = 0; i
|