Переглянути джерело

Add Viewer.raycast()

customisations
alemart 10 місяці тому
джерело
коміт
785cfe7a8c
3 змінених файлів з 56 додано та 2 видалено
  1. 1
    1
      docs/api/trackable-pointer.md
  2. 17
    1
      docs/api/viewer.md
  3. 38
    0
      src/geometry/viewer.ts

+ 1
- 1
docs/api/trackable-pointer.md Переглянути файл

@@ -32,7 +32,7 @@ The phase of the pointer. It's one of the following strings:
32 32
 
33 33
 `pointer.position: Vector2, read-only`
34 34
 
35
-The current position of the pointer, given in normalized units. See also: [Viewport.convertToPixels](viewport.md#converttopixels).
35
+The current position of the pointer, given in normalized units. See also: [Viewer.raycast](viewer.md#raycast), [Viewport.convertToPixels](viewport.md#converttopixels).
36 36
 
37 37
 ### initialPosition
38 38
 

+ 17
- 1
docs/api/viewer.md Переглянути файл

@@ -36,4 +36,20 @@ The input `pose` converted to viewer space.
36 36
 
37 37
 ```js
38 38
 const modelViewMatrix = viewer.convertToViewerSpace(pose).transform.matrix;
39
-```
39
+```
40
+
41
+### raycast
42
+
43
+`viewer.raycast(position: Vector2): Ray`
44
+
45
+Cast a [ray](ray.md) from a point in the image space associated with this viewer.
46
+
47
+*Since:* 0.4.0
48
+
49
+**Arguments**
50
+
51
+* `position: Vector2`. A point in image space, given in [normalized units](trackable-pointer.md).
52
+
53
+**Returns**
54
+
55
+A ray in world space that corresponds to the given point.

+ 38
- 0
src/geometry/viewer.ts Переглянути файл

@@ -26,6 +26,9 @@ import { Pose } from './pose';
26 26
 import { ViewerPose } from './viewer-pose';
27 27
 import { View, PerspectiveView } from './view';
28 28
 import { Transform } from './transform';
29
+import { Vector2 } from './vector2';
30
+import { Vector3 } from './vector3';
31
+import { Ray } from './ray';
29 32
 
30 33
 
31 34
 
@@ -98,4 +101,39 @@ export class Viewer
98 101
         const transform = new Transform(modelViewMatrix);
99 102
         return new Pose(transform);
100 103
     }
104
+
105
+    /**
106
+     * Cast a ray from a point in the image space associated with this Viewer
107
+     * @param position a point in image space, given in normalized units [-1,1]x[-1,1]
108
+     * @returns a ray in world space that corresponds to the given point
109
+     */
110
+    raycast(position: Vector2): Ray
111
+    {
112
+        const projectionMatrixInverse = this.view._projectionMatrixInverse;
113
+        const viewMatrixInverse = this._pose.transform.matrix;
114
+        const pointInClipSpace = Speedy.Matrix(4, 1, [
115
+            // Normalized Device Coordinates (NDC)
116
+            position.x,
117
+            position.y,
118
+            0, // (*)
119
+            1  // homogeneous coordinates
120
+        ]);
121
+
122
+        const pointInViewSpace = projectionMatrixInverse.times(pointInClipSpace);
123
+        const pointInWorldSpace = viewMatrixInverse.times(pointInViewSpace);
124
+        const p = Speedy.Matrix(pointInWorldSpace).read();
125
+
126
+        /*
127
+
128
+        (*) since we're just interested in the direction, any z coordinate in
129
+            clip space [-1,1] will give us a suitable point p in world space.
130
+
131
+        */
132
+
133
+        const origin = this._pose.transform.position;
134
+        const direction = new Vector3(p[0] / p[3], p[1] / p[3], p[2] / p[3])
135
+                          ._subtract(origin)._normalize();
136
+
137
+        return new Ray(origin, direction);
138
+    }
101 139
 }

Завантаження…
Відмінити
Зберегти