@woosh/meep-engine 2.34.1 → 2.34.2

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.
@@ -41,6 +41,26 @@ export class VertexDataSpec {
41
41
  return undefined;
42
42
  }
43
43
 
44
+ /**
45
+ * @returns {number} -1 if not found
46
+ * @param {string} name
47
+ */
48
+ getAttributeIndexByName(name) {
49
+
50
+ const attributes = this.attributes;
51
+ const n = attributes.length;
52
+
53
+ for (let i = 0; i < n; i++) {
54
+ const attribute = attributes[i];
55
+
56
+ if (attribute.name === name) {
57
+ return i;
58
+ }
59
+ }
60
+
61
+ return undefined;
62
+ }
63
+
44
64
  /**
45
65
  *
46
66
  * @param {AttributeSpec} attribute
@@ -1,9 +1,15 @@
1
1
  import {BufferGeometry} from "three";
2
2
  import {VertexDataSpec} from "../../geometry/VertexDataSpec";
3
+ import {SurfacePoint3} from "../../../../core/geom/3d/SurfacePoint3";
3
4
 
4
5
  export class MicronGeometry {
5
6
  vertex_spec: VertexDataSpec
6
7
  public proxy: BufferGeometry
7
8
 
8
9
  initialize(): void
10
+
11
+ raycast(
12
+ origin_x: number, origin_y: number, origin_z: number,
13
+ direction_x: number, direction_y: number, direction_z: number
14
+ ): SurfacePoint3[]
9
15
  }
@@ -1,5 +1,8 @@
1
1
  import { VertexDataSpec } from "../../geometry/VertexDataSpec.js";
2
2
  import { assert } from "../../../../core/assert.js";
3
+ import { intersectRay } from "../../../../core/bvh2/aabb3/AABB3Math.js";
4
+ import { computeTriangleRayIntersection } from "../../../../core/geom/3d/triangle/computeTriangleRayIntersection.js";
5
+ import { SurfacePoint3 } from "../../../../core/geom/3d/SurfacePoint3.js";
3
6
 
4
7
  let id_counter = 0;
5
8
 
@@ -55,6 +58,128 @@ export class MicronGeometry {
55
58
  this.__proxy = null;
56
59
  }
57
60
 
61
+ /**
62
+ *
63
+ * @param {number} origin_x
64
+ * @param {number} origin_y
65
+ * @param {number} origin_z
66
+ * @param {number} direction_x
67
+ * @param {number} direction_y
68
+ * @param {number} direction_z
69
+ * @returns {SurfacePoint3[]}
70
+ */
71
+ raycast(
72
+ origin_x, origin_y, origin_z,
73
+ direction_x, direction_y, direction_z
74
+ ) {
75
+
76
+ /**
77
+ *
78
+ * @type {SurfacePoint3[]}
79
+ */
80
+ const hits = [];
81
+
82
+ let stack_cursor = 1;
83
+ const stack = [this.root];
84
+
85
+ const spec = this.vertex_spec;
86
+
87
+ const position_attribute_index = spec.getAttributeIndexByName('position');
88
+
89
+ // check that position attribute actually exists
90
+ assert.isNonNegativeInteger(position_attribute_index, 'position_attribute_index');
91
+
92
+ const temp_hit = new SurfacePoint3();
93
+
94
+ while (stack_cursor > 0) {
95
+ stack_cursor--;
96
+
97
+ const patch = stack[stack_cursor];
98
+
99
+ // using group bounding box because it's conservative, as we want to be able to hit LOD_0
100
+ const bb = patch.group_bounding_box;
101
+
102
+ const bb_intersection_exists = intersectRay(
103
+ bb[0], bb[1], bb[2], bb[3], bb[4], bb[5],
104
+ origin_x, origin_y, origin_z,
105
+ direction_x, direction_y, direction_z
106
+ );
107
+
108
+ if (!bb_intersection_exists) {
109
+ // no intersection
110
+ continue;
111
+ }
112
+
113
+ if (patch.lod === 0) {
114
+ // LOD 0 - source geometry
115
+
116
+ // raycast against geometry
117
+ const indices = patch.face_indices;
118
+ const index_count = indices.length;
119
+
120
+ const vertices = patch.vertex_attribute_arrays[position_attribute_index];
121
+
122
+ for (let i = 0; i < index_count;) {
123
+ const a = indices[i++];
124
+ const b = indices[i++];
125
+ const c = indices[i++];
126
+
127
+
128
+ const a_address = a * 3;
129
+ const b_address = b * 3;
130
+ const c_address = c * 3;
131
+
132
+
133
+ const ax = vertices[a_address];
134
+ const ay = vertices[a_address + 1];
135
+ const az = vertices[a_address + 2];
136
+
137
+ const bx = vertices[b_address];
138
+ const by = vertices[b_address + 1];
139
+ const bz = vertices[b_address + 2];
140
+
141
+ const cx = vertices[c_address];
142
+ const cy = vertices[c_address + 1];
143
+ const cz = vertices[c_address + 2];
144
+
145
+ if (computeTriangleRayIntersection(
146
+ temp_hit,
147
+ origin_x, origin_y, origin_z,
148
+ direction_x, direction_y, direction_z,
149
+ ax, ay, az,
150
+ bx, by, bz,
151
+ cx, cy, cz
152
+ )) {
153
+
154
+ const contact = new SurfacePoint3();
155
+
156
+ contact.copy(temp_hit);
157
+
158
+ hits.push(contact);
159
+
160
+ }
161
+ }
162
+
163
+ } else {
164
+ // refine more
165
+ const children = patch.children;
166
+
167
+ const child_count = children.length;
168
+
169
+ for (let i = 0; i < child_count; i++) {
170
+ const child = children[i];
171
+
172
+ // push to stack
173
+ stack[stack_cursor] = child;
174
+
175
+ stack_cursor++;
176
+ }
177
+ }
178
+ }
179
+
180
+ return hits;
181
+ }
182
+
58
183
  get patches() {
59
184
  return this.__patches;
60
185
  }
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "productName": "Meep",
6
6
  "description": "production-ready JavaScript game engine based on Entity Component System Architecture",
7
7
  "author": "Alexander Goldring",
8
- "version": "2.34.1",
8
+ "version": "2.34.2",
9
9
  "dependencies": {
10
10
  "gl-matrix": "3.4.3",
11
11
  "fast-levenshtein": "2.0.6",