|
1 | 1 | import * as THREE from 'three'; |
2 | 2 |
|
| 3 | +let planes: any[] = []; |
| 4 | + |
3 | 5 | export class IFCRaycaster { |
4 | 6 | private camera: THREE.Camera; |
5 | 7 | private ifcModels: THREE.Object3D[]; |
6 | 8 | private raycaster: THREE.Raycaster; |
7 | 9 | private canvas: HTMLCanvasElement; |
| 10 | + private renderer: THREE.WebGLRenderer; |
8 | 11 |
|
9 | 12 | constructor(ifcModels: THREE.Object3D[], camera: THREE.Camera, renderer: THREE.WebGLRenderer) { |
10 | 13 | this.camera = camera; |
11 | 14 | this.canvas = renderer.domElement; |
12 | 15 | this.ifcModels = ifcModels; |
13 | 16 | this.raycaster = new THREE.Raycaster(); |
| 17 | + this.renderer = renderer; |
14 | 18 | // @ts-ignore |
15 | | - this.raycaster.firstHitOnly = true; |
| 19 | + // this.raycaster.firstHitOnly = true; |
16 | 20 | } |
17 | 21 |
|
18 | | - castRay(event: any, onHit: (event: any, item: THREE.Intersection) => any ) { |
19 | | - const mouse = new THREE.Vector2(); |
20 | | - const canvasBounds = this.canvas.getBoundingClientRect(); |
21 | | - mouse.x = ((event.clientX - canvasBounds.left) / (canvasBounds.right - canvasBounds.left)) * 2 - 1; |
22 | | - mouse.y = -((event.clientY - canvasBounds.top) / (canvasBounds.bottom - canvasBounds.top)) * 2 + 1; |
| 22 | + castRay(event: any, onHit: (_event: any, _item: THREE.Intersection) => any) { |
| 23 | + const mouse = this.getMouseProjection(event); |
23 | 24 | this.raycaster.setFromCamera(mouse, this.camera); |
24 | | - const result = this.raycaster.intersectObjects(this.ifcModels); |
| 25 | + const items = this.raycaster.intersectObjects(this.ifcModels); |
| 26 | + const result = this.filterClippingPlanes(items); |
25 | 27 | if (result.length > 0) return onHit(event, result[0]); |
| 28 | + return null; |
| 29 | + } |
| 30 | + |
| 31 | + private filterClippingPlanes(objs: THREE.Intersection[]) { |
| 32 | + if (objs.length <= 0) return objs; |
| 33 | + // @ts-ignore |
| 34 | + planes = this.camera.planes as any[]; |
| 35 | + if (!planes) return objs; |
| 36 | + if (planes.length <= 0) return objs; |
| 37 | + // return objs.filter((elem) => planes.every((elem2) => elem2.distanceToPoint(elem.point) > 0)); |
| 38 | + return objs.filter(this.filter); |
| 39 | + } |
| 40 | + |
| 41 | + private filter(elem: any) { |
| 42 | + return planes.every((elem2) => elem2.distanceToPoint(elem.point) > 0); |
| 43 | + } |
| 44 | + |
| 45 | + private getMouseProjection(event: any) { |
| 46 | + const mouse = new THREE.Vector2(); |
| 47 | + const bounds = this.canvas.getBoundingClientRect(); |
| 48 | + mouse.x = ((event.clientX - bounds.left) / (bounds.right - bounds.left)) * 2 - 1; |
| 49 | + mouse.y = -((event.clientY - bounds.top) / (bounds.bottom - bounds.top)) * 2 + 1; |
| 50 | + return mouse; |
26 | 51 | } |
27 | 52 | } |
0 commit comments