@woosh/meep-engine 2.34.14 → 2.34.18

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,20 @@
1
+ //
2
+
3
+ import { intersectRay } from "./AABB3Math.js";
4
+
5
+ /**
6
+ *
7
+ * @param {ArrayLike<number>|number[]|Float32Array} aabb bounding box, order: x0,y0,z0,x1,y1,z1
8
+ * @param {number} origin_x
9
+ * @param {number} origin_y
10
+ * @param {number} origin_z
11
+ * @param {number} direction_x
12
+ * @param {number} direction_y
13
+ * @param {number} direction_z
14
+ */
15
+ export function aabb3_array_intersects_ray(aabb, origin_x, origin_y, origin_z, direction_x, direction_y, direction_z) {
16
+ return intersectRay(aabb[0], aabb[1], aabb[2], aabb[3], aabb[4], aabb[5],
17
+ origin_x, origin_y, origin_z,
18
+ direction_x, direction_y, direction_z
19
+ );
20
+ }
@@ -140,7 +140,23 @@ export default class IndexedBinaryBVH {
140
140
  }
141
141
 
142
142
  /**
143
- *
143
+ * Sets bounds of a given leaf
144
+ * NOTE: to build tree, must call {@link #unsortedBuiltIntermediate} after all leaves are set
145
+ * @param {number} leaf_index
146
+ * @param {number} x0
147
+ * @param {number} y0
148
+ * @param {number} z0
149
+ * @param {number} x1
150
+ * @param {number} y1
151
+ * @param {number} z1
152
+ */
153
+ writeLeaf(leaf_index, x0, y0, z0, x1, y1, z1) {
154
+ const offset = this.binaryNodeCount * 6 + leaf_index * 6;
155
+ writeBox(this.data, offset, x0, y0, z0, x1, y1, z1);
156
+ }
157
+
158
+ /**
159
+ * @deprecated use {@link #writeLeaf} instead followed by {@link #unsortedBuiltIntermediate}
144
160
  * @param {function(index:number,offset:number,data:*, writeBox:function):*} visitor
145
161
  */
146
162
  setLeafs(visitor) {
@@ -0,0 +1,7 @@
1
+ /**
2
+ *
3
+ * @param {THREE.Color} c
4
+ */
5
+ export function computeHashColor(c) {
6
+ return c.getHex();
7
+ }
@@ -1,9 +1,9 @@
1
1
  import { planesEqual } from "./planesEqual.js";
2
- import { computeTextureEquality } from "./computeTextureEquality.js";
3
2
  import { isArraysEqualWithComparator } from "../../../../core/collection/array/isArraysEqualWithComparator.js";
3
+ import { computeTextureEqualityById } from "./computeTextureEqualityById.js";
4
4
 
5
5
  /**
6
- *
6
+ * NOTE: does not do deep comparison on textures, only checks the IDs
7
7
  * @param {Material|MeshStandardMaterial|MeshDepthMaterial|MeshMatcapMaterial} a
8
8
  * @param {Material|MeshStandardMaterial|MeshDepthMaterial|MeshMatcapMaterial} b
9
9
  * @returns {boolean}
@@ -76,28 +76,28 @@ export function computeMaterialEquality(a, b) {
76
76
  || a.wireframeLinewidth !== b.wireframeLinewidth
77
77
  || a.morphTargets !== b.morphTargets
78
78
  || a.morphNormals !== b.morphNormals
79
- || !computeTextureEquality(a.roughnessMap, b.roughnessMap)
80
- || !computeTextureEquality(a.metalnessMap, b.metalnessMap)
81
- || !computeTextureEquality(a.alphaMap, b.alphaMap)
82
- || !computeTextureEquality(a.envMap, b.envMap)
83
- || !computeTextureEquality(a.displacementMap, b.displacementMap)
84
- || !computeTextureEquality(a.normalMap, b.normalMap)
85
- || !computeTextureEquality(a.emissiveMap, b.emissiveMap)
86
- || !computeTextureEquality(a.bumpMap, b.bumpMap)
87
- || !computeTextureEquality(a.aoMap, b.aoMap)
88
- || !computeTextureEquality(a.map, b.map)
89
- || !computeTextureEquality(a.lightMap, b.lightMap)
79
+ || !computeTextureEqualityById(a.roughnessMap, b.roughnessMap)
80
+ || !computeTextureEqualityById(a.metalnessMap, b.metalnessMap)
81
+ || !computeTextureEqualityById(a.alphaMap, b.alphaMap)
82
+ || !computeTextureEqualityById(a.envMap, b.envMap)
83
+ || !computeTextureEqualityById(a.displacementMap, b.displacementMap)
84
+ || !computeTextureEqualityById(a.normalMap, b.normalMap)
85
+ || !computeTextureEqualityById(a.emissiveMap, b.emissiveMap)
86
+ || !computeTextureEqualityById(a.bumpMap, b.bumpMap)
87
+ || !computeTextureEqualityById(a.aoMap, b.aoMap)
88
+ || !computeTextureEqualityById(a.map, b.map)
89
+ || !computeTextureEqualityById(a.lightMap, b.lightMap)
90
90
  ) {
91
91
  return false;
92
92
  }
93
93
  } else if (a.isMeshDepthMaterial) {
94
94
  if (
95
- !computeTextureEquality(a.alphaMap, b.alphaMap)
95
+ !computeTextureEqualityById(a.alphaMap, b.alphaMap)
96
96
  || a.depthPacking !== b.depthPacking
97
- || !computeTextureEquality(a.displacementMap, b.displacementMap)
97
+ || !computeTextureEqualityById(a.displacementMap, b.displacementMap)
98
98
  || a.displacementScale !== b.displacementScale
99
99
  || a.displacementBias !== b.displacementBias
100
- || !computeTextureEquality(a.map, b.map)
100
+ || !computeTextureEqualityById(a.map, b.map)
101
101
  || a.morphTargets !== b.morphTargets
102
102
  || a.wireframe !== b.wireframe
103
103
  || a.wireframeLinewidth !== b.wireframeLinewidth
@@ -107,15 +107,15 @@ export function computeMaterialEquality(a, b) {
107
107
  } else if (a.isMeshMatcapMaterial) {
108
108
  if (
109
109
  !a.color.equals(b.color)
110
- || !computeTextureEquality(a.matcap, b.matcap)
111
- || !computeTextureEquality(a.map, b.map)
112
- || !computeTextureEquality(a.bumpMap, b.bumpMap)
113
- || !computeTextureEquality(a.normalMap, b.normalMap)
110
+ || !computeTextureEqualityById(a.matcap, b.matcap)
111
+ || !computeTextureEqualityById(a.map, b.map)
112
+ || !computeTextureEqualityById(a.bumpMap, b.bumpMap)
113
+ || !computeTextureEqualityById(a.normalMap, b.normalMap)
114
114
  || !a.normalScale.equals(b.normalScale)
115
- || !computeTextureEquality(a.displacementMap, b.displacementMap)
115
+ || !computeTextureEqualityById(a.displacementMap, b.displacementMap)
116
116
  || a.displacementScale !== b.displacementScale
117
117
  || a.displacementBias !== b.displacementBias
118
- || !computeTextureEquality(a.alphaMap, b.alphaMap)
118
+ || !computeTextureEqualityById(a.alphaMap, b.alphaMap)
119
119
  || a.morphTargets !== b.morphTargets
120
120
  || a.morphNormals !== b.morphNormals
121
121
  || a.flatShading !== b.flatShading
@@ -1,9 +1,10 @@
1
1
  import { planeHash } from "./planeHash.js";
2
- import { computeTextureHash } from "./computeTextureHash.js";
3
2
  import { computeStringHash } from "../../../../core/primitives/strings/computeStringHash.js";
4
3
  import { computeHashArray } from "../../../../core/collection/array/computeHashArray.js";
5
4
  import { computeHashIntegerArray } from "../../../../core/collection/array/computeHashIntegerArray.js";
6
5
  import { computeHashFloat } from "../../../../core/math/hash/computeHashFloat.js";
6
+ import { textureHashById } from "./textureHashById.js";
7
+ import { computeHashColor } from "./computeHashColor.js";
7
8
 
8
9
  /**
9
10
  *
@@ -46,21 +47,22 @@ export function computeMaterialHash(material) {
46
47
 
47
48
 
48
49
  if (material.isMeshStandardMaterial) {
49
- //TODO extend hash
50
50
  hash = computeHashIntegerArray(
51
51
  hash,
52
- computeTextureHash(material.map),
53
- computeTextureHash(material.lightMap),
54
- computeTextureHash(material.aoMap),
55
- computeTextureHash(material.emissiveMap),
56
- computeTextureHash(material.bumpMap),
57
- computeTextureHash(material.normalMap),
58
- computeTextureHash(material.displacementMap),
59
- computeTextureHash(material.roughnessMap),
60
- computeTextureHash(material.metalnessMap),
61
- computeTextureHash(material.alphaMap),
52
+ computeHashColor(material.color),
53
+ computeHashFloat(material.roughness),
54
+ computeHashFloat(material.metalness),
55
+ textureHashById(material.map),
56
+ textureHashById(material.lightMap),
57
+ computeHashFloat(material.lightMapIntensity),
58
+ textureHashById(material.aoMap),
59
+ computeHashFloat(material.aoMapIntensity),
60
+ computeHashColor(material.emissive),
61
+ computeHashFloat(material.emissiveIntensity),
62
+ textureHashById(material.emissiveMap),
62
63
  computeHashFloat(material.envMapIntensity),
63
- computeTextureHash(material.envMap)
64
+ textureHashById(material.bumpMap),
65
+ textureHashById(material.normalMap),
64
66
  );
65
67
  } else if (material.isShaderMaterial) {
66
68
  hash ^= material.lights ? 0 : 1;
@@ -0,0 +1,22 @@
1
+ /**
2
+ *
3
+ * @param {Texture} a
4
+ * @param {Texture} b
5
+ * @returns {boolean}
6
+ */
7
+ export function computeTextureEqualityById(a, b) {
8
+ if (a === b) {
9
+ return true;
10
+ }
11
+
12
+ if (
13
+ a === null
14
+ || b === null
15
+ || a === undefined
16
+ || b === undefined
17
+ ) {
18
+ return false;
19
+ }
20
+
21
+ return a.id === b.id;
22
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ *
3
+ * @param {THREE.Texture} texture
4
+ * @return {number}
5
+ */
6
+ export function textureHashById(texture) {
7
+ if (texture === undefined || texture === null) {
8
+ return 0;
9
+ }
10
+
11
+ return texture.id;
12
+ }
@@ -8,4 +8,11 @@ export class GeometrySpatialQueryAccelerator {
8
8
  queryContainmentViaClippingPlanes(geometry: BufferGeometry, planes: Plane[]): number
9
9
 
10
10
  queryRaycastNearest(destination: SurfacePoint3, geometry: BufferGeometry, ray_origin: Vector3, ray_direction: Vector3): boolean
11
+
12
+ queryRaycastNearest_primitive(
13
+ destination: SurfacePoint3,
14
+ geometry: BufferGeometry,
15
+ origin_x: number, origin_y: number, origin_z: number,
16
+ direction_x: number, direction_y: number, direction_z: number
17
+ ): boolean
11
18
  }
@@ -3,6 +3,7 @@ import { buildUnsorted } from "../../bvh/buffered/BinaryBVHFromBufferGeometry.js
3
3
  import { ClippingPlaneContainmentComputingVisitor } from "./ClippingPlaneContainmentComputingVisitor.js";
4
4
  import { RaycastNearestHitComputingVisitor } from "./RaycastNearestHitComputingVisitor.js";
5
5
  import { assert } from "../../../../../core/assert.js";
6
+ import { deinterleaveBufferAttribute } from "../deinterleaveBufferAttribute.js";
6
7
 
7
8
  export class GeometrySpatialQueryAccelerator {
8
9
  constructor(cache_size = 100 * 1024 * 1024) {
@@ -153,8 +154,14 @@ export class GeometrySpatialQueryAccelerator {
153
154
  * @returns {IndexedBinaryBVH}
154
155
  */
155
156
  __buildBVH(geometry) {
156
- const vertices = geometry.getAttribute('position').array;
157
- const indices = geometry.getIndex().array;
157
+ const position_attribute = geometry.getAttribute('position');
158
+ const index_attribute = geometry.getIndex();
159
+
160
+ const de_interleaved_position_attribute = deinterleaveBufferAttribute(position_attribute);
161
+ const de_interleaved_index_attribute = deinterleaveBufferAttribute(index_attribute);
162
+
163
+ const vertices = de_interleaved_position_attribute.array;
164
+ const indices = de_interleaved_index_attribute.array;
158
165
 
159
166
  return buildUnsorted(vertices, indices);
160
167
  }
@@ -1,10 +1,9 @@
1
1
  /**
2
2
  * Created by Alex on 29/05/2016.
3
3
  */
4
- import IndexedTriangleBoundsComputer from './IndexedTraingleBoundsComputer.js';
5
4
  import IndexedBinaryBVH from '../../../../../core/bvh2/binary/IndexedBinaryBVH.js';
6
-
7
- const makeTriangle = IndexedTriangleBoundsComputer.compute;
5
+ import { min3 } from "../../../../../core/math/min3.js";
6
+ import { max3 } from "../../../../../core/math/max3.js";
8
7
 
9
8
  /**
10
9
  *
@@ -19,20 +18,46 @@ function buildUnsorted(vertices, indices) {
19
18
  const tree = new IndexedBinaryBVH();
20
19
  tree.initialize(numNodes);
21
20
 
22
- tree.setLeafs(function (i, offset, data, writeBox) {
21
+ for (let i = 0; i < numNodes; i++) {
23
22
 
24
23
  const index3 = i * 3;
25
24
 
25
+ // read triangle vertex indices
26
26
  const iA = indices[index3];
27
27
  const iB = indices[index3 + 1];
28
28
  const iC = indices[index3 + 2];
29
29
 
30
- makeTriangle(iA * 3, iB * 3, iC * 3, vertices, function (x0, y0, z0, x1, y1, z1) {
31
- writeBox(data, offset, x0, y0, z0, x1, y1, z1);
32
- });
30
+ const a = iA * 3;
31
+ const b = iB * 3;
32
+ const c = iC * 3;
33
+
34
+ // read actual positions of each vertex
35
+ const aX = vertices[a];
36
+ const aY = vertices[a + 1];
37
+ const aZ = vertices[a + 2];
38
+
39
+ const bX = vertices[b];
40
+ const bY = vertices[b + 1];
41
+ const bZ = vertices[b + 2];
42
+
43
+ const cX = vertices[c];
44
+ const cY = vertices[c + 1];
45
+ const cZ = vertices[c + 2];
46
+
47
+ // compute bounds of the triangle
48
+ const x0 = min3(aX, bX, cX);
49
+ const y0 = min3(aY, bY, cY);
50
+ const z0 = min3(aZ, bZ, cZ);
51
+
52
+ const x1 = max3(aX, bX, cX);
53
+ const y1 = max3(aY, bY, cY);
54
+ const z1 = max3(aZ, bZ, cZ);
55
+
56
+ tree.writeLeaf(i, x0, y0, z0, x1, y1, z1);
57
+ }
33
58
 
34
- return i;
35
- });
59
+ // finalize build
60
+ tree.unsortedBuiltIntermediate();
36
61
 
37
62
  return tree;
38
63
  }
@@ -12,4 +12,6 @@ export class MicronGeometry {
12
12
  origin_x: number, origin_y: number, origin_z: number,
13
13
  direction_x: number, direction_y: number, direction_z: number
14
14
  ): SurfacePoint3[]
15
+
16
+ readonly isMicronGeometry: boolean
15
17
  }
@@ -1,11 +1,17 @@
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
3
  import { computeTriangleRayIntersection } from "../../../../core/geom/3d/triangle/computeTriangleRayIntersection.js";
5
4
  import { SurfacePoint3 } from "../../../../core/geom/3d/SurfacePoint3.js";
5
+ import { aabb3_array_intersects_ray } from "../../../../core/bvh2/aabb3/aabb3_array_intersects_ray.js";
6
6
 
7
7
  let id_counter = 0;
8
8
 
9
+ /**
10
+ * @readonly
11
+ * @type {MicronGeometryPatch[]}
12
+ */
13
+ const scratch_traversal_stack = [];
14
+
9
15
  export class MicronGeometry {
10
16
  constructor() {
11
17
  /**
@@ -79,8 +85,10 @@ export class MicronGeometry {
79
85
  */
80
86
  const hits = [];
81
87
 
88
+ // initialize stack
82
89
  let stack_cursor = 1;
83
- const stack = [this.root];
90
+ const stack = scratch_traversal_stack;
91
+ stack[0] = this.root;
84
92
 
85
93
  const spec = this.vertex_spec;
86
94
 
@@ -99,8 +107,8 @@ export class MicronGeometry {
99
107
  // using group bounding box because it's conservative, as we want to be able to hit LOD_0
100
108
  const bb = patch.group_bounding_box;
101
109
 
102
- const bb_intersection_exists = intersectRay(
103
- bb[0], bb[1], bb[2], bb[3], bb[4], bb[5],
110
+ const bb_intersection_exists = aabb3_array_intersects_ray(
111
+ bb,
104
112
  origin_x, origin_y, origin_z,
105
113
  direction_x, direction_y, direction_z
106
114
  );
@@ -113,6 +121,15 @@ export class MicronGeometry {
113
121
  if (patch.lod === 0) {
114
122
  // LOD 0 - source geometry
115
123
 
124
+ // cast against bounds
125
+ if (!aabb3_array_intersects_ray(
126
+ patch.bounding_box,
127
+ origin_x, origin_y, origin_z,
128
+ direction_x, direction_y, direction_z
129
+ )) {
130
+ continue;
131
+ }
132
+
116
133
  // raycast against geometry
117
134
  const indices = patch.face_indices;
118
135
  const index_count = indices.length;
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.14",
8
+ "version": "2.34.18",
9
9
  "dependencies": {
10
10
  "gl-matrix": "3.4.3",
11
11
  "fast-levenshtein": "2.0.6",