@woosh/meep-engine 2.47.32 → 2.47.34

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.
@@ -3,43 +3,34 @@ import { HashMap } from "../../../../../../core/collection/HashMap.js";
3
3
  import { InstancedMeshGroup } from "../../../../geometry/instancing/InstancedMeshGroup.js";
4
4
  import { ShadedGeometryFlags } from "../../ShadedGeometryFlags.js";
5
5
  import { StreamDrawUsage } from "three";
6
+ import { SGCacheKey } from "./SGCacheKey.js";
6
7
 
7
8
  const INSTANCED_EQUALITY_FLAGS = ShadedGeometryFlags.CastShadow
8
9
  | ShadedGeometryFlags.ReceiveShadow
9
10
  ;
10
11
 
12
+ /**
13
+ * @readonly
14
+ * @type {SGCacheKey}
15
+ */
16
+ const scratch_key = new SGCacheKey();
17
+
11
18
  export class InstancedRendererAdapter extends AbstractRenderAdapter {
12
- constructor() {
13
- super();
14
-
15
- /**
16
- *
17
- * @type {HashMap<ShadedGeometry, InstancedMeshGroup>}
18
- * @private
19
- */
20
- this.__instanced_meshes = new HashMap({
21
- keyHashFunction(sg) {
22
- return sg.geometry.id
23
- ^ sg.material.id
24
- ^ sg.mode
25
- ;
26
- },
27
- keyEqualityFunction(a, b) {
28
- return a.material.id === b.material.id
29
- && a.geometry.id === b.geometry.id
30
- && a.mode === b.mode
31
- && (a.flags & INSTANCED_EQUALITY_FLAGS) === (b.flags & INSTANCED_EQUALITY_FLAGS)
32
- ;
33
- }
34
- });
35
19
 
36
- /**
37
- *
38
- * @type {Set<InstancedMeshGroup>}
39
- * @private
40
- */
41
- this.__active_meshes = new Set();
42
- }
20
+ /**
21
+ *
22
+ * @type {HashMap<SGCacheKey, InstancedMeshGroup>}
23
+ * @private
24
+ */
25
+ __instanced_meshes = new HashMap();
26
+
27
+ /**
28
+ *
29
+ * @type {Set<InstancedMeshGroup>}
30
+ * @private
31
+ */
32
+ __active_meshes = new Set();
33
+
43
34
 
44
35
  score(geometry, material, instance_count) {
45
36
  if (instance_count > 16) {
@@ -58,7 +49,12 @@ export class InstancedRendererAdapter extends AbstractRenderAdapter {
58
49
  * @private
59
50
  */
60
51
  __get_instanced_mesh(sg) {
61
- let im = this.__instanced_meshes.get(sg);
52
+ scratch_key.fromSG(sg);
53
+
54
+ // mask out some flags
55
+ scratch_key.flags &= INSTANCED_EQUALITY_FLAGS
56
+
57
+ let im = this.__instanced_meshes.get(scratch_key);
62
58
 
63
59
 
64
60
  if (im !== undefined) {
@@ -79,7 +75,7 @@ export class InstancedRendererAdapter extends AbstractRenderAdapter {
79
75
  im.mesh.castShadow = sg.getFlag(ShadedGeometryFlags.CastShadow);
80
76
  im.mesh.receiveShadow = sg.getFlag(ShadedGeometryFlags.ReceiveShadow);
81
77
 
82
- this.__instanced_meshes.set(sg, im);
78
+ this.__instanced_meshes.set(scratch_key.clone(), im);
83
79
 
84
80
  return im;
85
81
 
@@ -131,8 +127,8 @@ export class InstancedRendererAdapter extends AbstractRenderAdapter {
131
127
 
132
128
  const meshes = this.__active_meshes;
133
129
 
134
- for (const activeMesh of meshes) {
135
- activeMesh.setCount(0);
130
+ for (const mesh of meshes) {
131
+ mesh.setCount(0);
136
132
  }
137
133
 
138
134
  meshes.clear();
@@ -0,0 +1,59 @@
1
+ import { DrawMode } from "../../DrawMode.js";
2
+
3
+ export class SGCacheKey {
4
+ geometry = -1;
5
+ material = -1;
6
+ /**
7
+ *
8
+ * @type {DrawMode}
9
+ */
10
+ mode = DrawMode.Triangles;
11
+
12
+ flags = 0;
13
+
14
+ hash(){
15
+ return this.geometry ^ this.material;
16
+ }
17
+
18
+ /**
19
+ *
20
+ * @param {SGCacheKey} other
21
+ * @returns {boolean}
22
+ */
23
+ equals(other){
24
+ return this.geometry === other.geometry
25
+ && this.material === other.material
26
+ && this.mode === other.mode
27
+ && this.flags === other.flags;
28
+ }
29
+
30
+ /**
31
+ *
32
+ * @param {SGCacheKey} other
33
+ */
34
+ copy(other){
35
+ this.geometry = other.geometry;
36
+ this.material = other.material;
37
+ this.mode = other.mode;
38
+ this.flags = other.flags;
39
+ }
40
+
41
+ clone(){
42
+ const r = new SGCacheKey();
43
+
44
+ r.copy(this);
45
+
46
+ return r;
47
+ }
48
+
49
+ /**
50
+ *
51
+ * @param {ShadedGeometry} sg
52
+ */
53
+ fromSG(sg){
54
+ this.geometry = sg.geometry.id;
55
+ this.material = sg.material.id;
56
+ this.mode = sg.mode;
57
+ this.flags = sg.flags;
58
+ }
59
+ }