@geastack/gea3d 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ export { InstancedMesh, Line, LineSegments, Mesh, Points, Sprite } from './core'
@@ -0,0 +1,101 @@
1
+ // SPDX-License-Identifier: GPL-3.0-only
2
+ // Picking against uploaded mesh geometry, three.js Raycaster style. The ray
3
+ // tests run natively against each mesh's triangles in world space; points,
4
+ // lines and sprites are not hit.
5
+
6
+ import { Camera } from './camera'
7
+ import { Intersection, Object3D } from './core'
8
+ import { Matrix4, Vector2, Vector3 } from './math'
9
+
10
+ export class Ray {
11
+ readonly origin: Vector3
12
+ // Normalized.
13
+ readonly direction: Vector3
14
+
15
+ constructor() {
16
+ this.origin = new Vector3()
17
+ this.direction = new Vector3(0, 0, -1)
18
+ }
19
+
20
+ set(origin: Vector3, direction: Vector3): Ray {
21
+ this.origin.copy(origin)
22
+ this.direction.copy(direction).normalize()
23
+ return this
24
+ }
25
+ }
26
+
27
+ export class Raycaster {
28
+ readonly ray: Ray
29
+ near: number
30
+ far: number
31
+ private readonly unproject: Matrix4
32
+
33
+ constructor(near: number = 0, far: number = Infinity) {
34
+ this.ray = new Ray()
35
+ this.near = near
36
+ this.far = far
37
+ this.unproject = new Matrix4()
38
+ }
39
+
40
+ set(origin: Vector3, direction: Vector3): void {
41
+ this.ray.set(origin, direction)
42
+ }
43
+
44
+ // Ray from the camera through `coords` in normalized device coordinates
45
+ // (x and y in -1..1, y up).
46
+ setFromCamera(coords: Vector2, camera: Camera): void {
47
+ camera.updateMatrixWorld(false)
48
+ const origin = this.ray.origin
49
+ const direction = this.ray.direction
50
+ this.unproject.multiplyMatrices(camera.matrixWorld, camera.projectionMatrixInverse)
51
+ if (camera.isOrthographicCamera) {
52
+ origin.set(coords.x, coords.y, -1).applyMatrix4(this.unproject)
53
+ direction.set(0, 0, -1).transformDirection(camera.matrixWorld)
54
+ return
55
+ }
56
+ origin.setFromMatrixPosition(camera.matrixWorld)
57
+ direction.set(coords.x, coords.y, 0.5).applyMatrix4(this.unproject).sub(origin).normalize()
58
+ }
59
+
60
+ // Hits on `object` (and its descendants when recursive), nearest first.
61
+ intersectObject(object: Object3D, recursive: boolean = true): Intersection[] {
62
+ const intersects: Intersection[] = []
63
+ object.root().syncNative(-1)
64
+ this.collect(object, recursive, intersects)
65
+ sortByDistance(intersects)
66
+ return intersects
67
+ }
68
+
69
+ intersectObjects(objects: Object3D[], recursive: boolean = true): Intersection[] {
70
+ const intersects: Intersection[] = []
71
+ for (let i = 0; i < objects.length; i++) {
72
+ objects[i].root().syncNative(-1)
73
+ this.collect(objects[i], recursive, intersects)
74
+ }
75
+ sortByDistance(intersects)
76
+ return intersects
77
+ }
78
+
79
+ private collect(object: Object3D, recursive: boolean, intersects: Intersection[]): void {
80
+ if (!object.visible) return
81
+
82
+ object.raycastSelf(this.ray.origin, this.ray.direction, this.near, this.far, intersects)
83
+ if (!recursive) return
84
+
85
+ const children = object.children
86
+ for (let i = 0; i < children.length; i++) this.collect(children[i], true, intersects)
87
+ }
88
+ }
89
+
90
+ // Insertion sort: hit lists are short.
91
+ function sortByDistance(intersects: Intersection[]): void {
92
+ for (let i = 1; i < intersects.length; i++) {
93
+ const hit = intersects[i]
94
+ let j = i - 1
95
+ while (j >= 0 && intersects[j].distance > hit.distance) {
96
+ intersects[j + 1] = intersects[j]
97
+ j--
98
+ }
99
+ intersects[j + 1] = hit
100
+ }
101
+ }