@woosh/meep-engine 2.36.24 → 2.36.25

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.
@@ -1216,6 +1216,20 @@ export class EntityComponentDataset {
1216
1216
  }
1217
1217
  }
1218
1218
 
1219
+ /**
1220
+ *
1221
+ * @returns {Generator<number>}
1222
+ */
1223
+ * createEntityIterator() {
1224
+
1225
+ const eo = this.entityOccupancy;
1226
+
1227
+ for (let i = eo.nextSetBit(0); i !== -1; i = eo.nextSetBit(i + 1)) {
1228
+ yield i;
1229
+ }
1230
+
1231
+ }
1232
+
1219
1233
  /**
1220
1234
  * @template T
1221
1235
  * @param {Class<T>} klass
@@ -15,6 +15,10 @@ import {
15
15
  } from "../../../../core/bvh2/bvh3/ExplicitBinaryBoundingVolumeHierarchy.js";
16
16
  import { bvh_query_leaves_generic } from "../../../../core/bvh2/bvh3/bvh_query_leaves_generic.js";
17
17
  import { BVHQueryIntersectsRay } from "../../../../core/bvh2/bvh3/query/BVHQueryIntersectsRay.js";
18
+ import Task from "../../../../core/process/task/Task.js";
19
+ import { RuntimeDrawMethodOptimizer } from "./render/optimization/RuntimeDrawMethodOptimizer.js";
20
+ import TaskSignal from "../../../../core/process/task/TaskSignal.js";
21
+ import TaskState from "../../../../core/process/task/TaskState.js";
18
22
 
19
23
 
20
24
  /**
@@ -81,6 +85,31 @@ export class ShadedGeometrySystem extends System {
81
85
  * @private
82
86
  */
83
87
  this.__bvh_binary = new ExplicitBinaryBoundingVolumeHierarchy();
88
+
89
+ const optimizer = new RuntimeDrawMethodOptimizer();
90
+
91
+ this.__optimization_task = new Task({
92
+ name: 'ShadedGeometry Draw Method optimizer',
93
+ initializer: () => {
94
+ optimizer.__system = this;
95
+ optimizer.__ecd = this.entityManager.dataset
96
+ },
97
+ cycleFunction() {
98
+ if (optimizer.step()) {
99
+ return TaskSignal.Continue;
100
+ } else {
101
+ return TaskSignal.Yield;
102
+ }
103
+ }
104
+ });
105
+ }
106
+
107
+ /**
108
+ * NOTE: DO NOT MODIFY RESULTS
109
+ * @returns {Map<number,number>}
110
+ */
111
+ getGeometryUsageCounters() {
112
+ return this.__geometry_usage_counters;
84
113
  }
85
114
 
86
115
  /**
@@ -184,6 +213,9 @@ export class ShadedGeometrySystem extends System {
184
213
  return ctx.collect(destination, destination_offset, graphics.renderer, view, this.__bvh_binary, ecd);
185
214
  });
186
215
 
216
+ this.__optimization_task.state.set(TaskState.INITIAL);
217
+ engine.executor.run(this.__optimization_task);
218
+
187
219
  readyCallback();
188
220
  }
189
221
 
@@ -194,6 +226,8 @@ export class ShadedGeometrySystem extends System {
194
226
 
195
227
  const view_list = graphics.views.elements;
196
228
 
229
+ engine.executor.removeTask(this.__optimization_task);
230
+
197
231
  view_list.forEach(this.__handle_view_removed, this);
198
232
  view_list.on.added.remove(this.__handle_view_added, this);
199
233
  view_list.on.removed.remove(this.__handle_view_removed, this);
@@ -20,9 +20,10 @@ export class AbstractRenderAdapter {
20
20
  *
21
21
  * @param {THREE.BufferGeometry} geometry
22
22
  * @param {THREE.Material} material
23
+ * @param {number} instance_count
23
24
  * @returns {number}
24
25
  */
25
- score(geometry, material) {
26
+ score(geometry, material, instance_count) {
26
27
  return 0;
27
28
  }
28
29
 
@@ -14,11 +14,6 @@ export class GenericRendererAdapter extends AbstractRenderAdapter {
14
14
  this.__cache = SGThreeObjectCache.INSTANCE;
15
15
  }
16
16
 
17
-
18
- score(geometry, material) {
19
- return 0;
20
- }
21
-
22
17
  /**
23
18
  *
24
19
  * @param {THREE.WebGLRenderer} renderer
@@ -69,6 +69,16 @@ export class InstancedRendererAdapter extends AbstractRenderAdapter {
69
69
  this.__active_meshes = new Set();
70
70
  }
71
71
 
72
+ score(geometry, material, instance_count) {
73
+ if (instance_count > 64) {
74
+ return 1;
75
+ } else if (instance_count < 4) {
76
+ return -1;
77
+ } else {
78
+ return 0;
79
+ }
80
+ }
81
+
72
82
  /**
73
83
  *
74
84
  * @param {ShadedGeometry} sg
@@ -0,0 +1,109 @@
1
+ import { ShadedGeometry } from "../../ShadedGeometry.js";
2
+
3
+ export class RuntimeDrawMethodOptimizer {
4
+ constructor() {
5
+ /**
6
+ *
7
+ * @type {EntityComponentDataset|null}
8
+ * @private
9
+ */
10
+ this.__ecd = null;
11
+
12
+ /**
13
+ *
14
+ * @type {ShadedGeometrySystem|null}
15
+ * @private
16
+ */
17
+ this.__system = null;
18
+
19
+ /**
20
+ *
21
+ * @type {Generator<number>}
22
+ * @private
23
+ */
24
+ this.__entity_iterator = null;
25
+ }
26
+
27
+ /**
28
+ *
29
+ * @returns {boolean} true if a change was made, false otherwise
30
+ */
31
+ step() {
32
+ const ecd = this.__ecd;
33
+
34
+ if (ecd === null) {
35
+ // no dataset attached
36
+ return false;
37
+ }
38
+
39
+ if (!ecd.isComponentTypeRegistered(ShadedGeometry)) {
40
+ // component not registered
41
+ return false;
42
+ }
43
+
44
+ if (this.__entity_iterator === null) {
45
+ this.__entity_iterator = ecd.createEntityIterator();
46
+ }
47
+
48
+ const next = this.__entity_iterator.next();
49
+
50
+ if (next.done) {
51
+ this.__entity_iterator = ecd.createEntityIterator();
52
+ return false;
53
+ }
54
+
55
+ const entity = next.value;
56
+
57
+ /**
58
+ *
59
+ * @type {ShadedGeometry}
60
+ */
61
+ const sg = ecd.getComponent(entity, ShadedGeometry);
62
+
63
+ if (sg === undefined) {
64
+ return false;
65
+ }
66
+
67
+ const use_count = this.__system.getGeometryUsageCounters().get(sg.geometry.id);
68
+
69
+ /**
70
+ *
71
+ * @type {ShadedGeometryRendererContext|undefined}
72
+ */
73
+ const first_context = this.__system.__view_contexts.values().next().value;
74
+
75
+ if (first_context === undefined) {
76
+ return false;
77
+ }
78
+
79
+ const adapters = first_context.adapters;
80
+ const adapter_count = adapters.length;
81
+
82
+ let best_score = Number.NEGATIVE_INFINITY;
83
+ let best_adapter = -1;
84
+
85
+ for (let i = 0; i < adapter_count; i++) {
86
+ const adapter = adapters[i];
87
+
88
+ const score = adapter.score(sg.geometry, sg.material, use_count);
89
+
90
+ if (score > best_score) {
91
+ best_score = score;
92
+ best_adapter = i;
93
+ }
94
+ }
95
+
96
+ if (best_adapter === -1) {
97
+ // not found
98
+ return false;
99
+ }
100
+
101
+ if (sg.draw_method === best_adapter) {
102
+ return false;
103
+ }
104
+
105
+ sg.draw_method = best_adapter;
106
+
107
+ return true;
108
+ }
109
+ }
@@ -3,6 +3,8 @@ import Vector2 from "../../../../../core/geom/Vector2.js";
3
3
  import { buildMicronGeometryFromBufferGeometry } from "../../build/buildMicronGeometryFromBufferGeometry.js";
4
4
  import { ShadedGeometryFlags } from "../../../ecs/mesh-v2/ShadedGeometryFlags.js";
5
5
  import { MaterialVertexSpec, MaterialVertexSpecFlags } from "../../render/v1/MaterialVertexSpec.js";
6
+ import { compute_geometry_polycount } from "../../../geometry/compute_geometry_polycount.js";
7
+ import { inverseLerp } from "../../../../../core/math/inverseLerp.js";
6
8
 
7
9
  /**
8
10
  *
@@ -10,6 +12,18 @@ import { MaterialVertexSpec, MaterialVertexSpecFlags } from "../../render/v1/Mat
10
12
  */
11
13
  const scratch_mvc = new MaterialVertexSpec();
12
14
 
15
+ /**
16
+ * @readonly
17
+ * @type {number}
18
+ */
19
+ const MICRON_POLYCOUNT_THRESHOLD_MIN = 4096;
20
+
21
+ /**
22
+ * @readonly
23
+ * @type {number}
24
+ */
25
+ const MICRON_POLYCOUNT_THRESHOLD_MAX = 10000;
26
+
13
27
  export class MicronShadedGeometryRenderAdapter extends AbstractRenderAdapter {
14
28
 
15
29
  constructor() {
@@ -36,6 +50,12 @@ export class MicronShadedGeometryRenderAdapter extends AbstractRenderAdapter {
36
50
  this.__geometry_building_promises = new Map();
37
51
  }
38
52
 
53
+ score(geometry, material, instance_count) {
54
+ const polycount = compute_geometry_polycount(geometry);
55
+
56
+ return inverseLerp(MICRON_POLYCOUNT_THRESHOLD_MIN, MICRON_POLYCOUNT_THRESHOLD_MAX, polycount);
57
+ }
58
+
39
59
  add(sg) {
40
60
 
41
61
  const geometry_id = sg.geometry.id;
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.36.24",
8
+ "version": "2.36.25",
9
9
  "dependencies": {
10
10
  "gl-matrix": "3.4.3",
11
11
  "fast-levenshtein": "2.0.6",