@woosh/meep-engine 2.34.29 → 2.34.33

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.
@@ -19,3 +19,19 @@ export const DataTypeByteSizes = {
19
19
  [DataType.Float32]: 4,
20
20
  [DataType.Float64]: 8
21
21
  };
22
+
23
+
24
+ /**
25
+ *
26
+ * @param {DataType} type
27
+ * @return {number}
28
+ */
29
+ export function dataTypeByteSize(type) {
30
+ const size = DataTypeByteSizes[type];
31
+
32
+ if (size === undefined) {
33
+ throw new Error(`Unsupported type '${type}'`);
34
+ }
35
+
36
+ return size;
37
+ }
package/engine/Engine.js CHANGED
@@ -131,6 +131,11 @@ class Engine {
131
131
  const v_fps = (1 / stats.mean).toFixed(2);
132
132
 
133
133
 
134
+ // clear stats
135
+ m_render.clear();
136
+ m_sim.clear();
137
+ m_frame.clear();
138
+
134
139
  return `FPS: ${v_fps}, RENDER: ${v_render}ms, SIMULATION: ${v_sim}ms`;
135
140
  });
136
141
 
@@ -21,4 +21,8 @@ export class AbstractMetric {
21
21
  computeStats(result) {
22
22
  throw new Error('Not implemented');
23
23
  }
24
+
25
+ clear(){
26
+ throw new Error('Not implemented');
27
+ }
24
28
  }
@@ -31,4 +31,8 @@ export class RingBufferMetric extends AbstractMetric {
31
31
  result.max = computeArrayMax(array, 0, data_count);
32
32
  result.min = computeArrayMin(array, 0, data_count);
33
33
  }
34
+
35
+ clear() {
36
+ this.__data.clear();
37
+ }
34
38
  }
@@ -162,7 +162,7 @@ export class Transform {
162
162
 
163
163
  /**
164
164
  *
165
- * @param {Matrix4} matrix
165
+ * @param {THREE.Matrix4} matrix
166
166
  */
167
167
  fromThreeMatrix4(matrix) {
168
168
  this.fromMatrix4(matrix.elements);
@@ -125,16 +125,29 @@ export class ShadedGeometry {
125
125
  }
126
126
 
127
127
  __build_object() {
128
+ /**
129
+ * @type {THREE.Object3D}
130
+ */
131
+ let object;
132
+
128
133
  const mode = this.mode;
134
+ const shader = this.material;
135
+ const geo = this.geometry;
136
+
129
137
  if (mode === DrawMode.Triangles) {
130
- this.__cached_object = new Mesh(this.geometry, this.material);
138
+ object = new Mesh(geo, shader);
131
139
  } else if (mode === DrawMode.Lines) {
132
- this.__cached_object = new Line(this.geometry, this.material);
140
+ object = new Line(geo, shader);
133
141
  } else if (mode === DrawMode.LineSegments) {
134
- this.__cached_object = new LineSegments(this.geometry, this.material);
142
+ object = new LineSegments(geo, shader);
135
143
  } else {
136
144
  throw new Error(`Unsupported draw mode '${mode}'`);
137
145
  }
146
+
147
+ object.matrixAutoUpdate = false;
148
+ object.frustumCulled = false;
149
+
150
+ this.__cached_object = object;
138
151
  }
139
152
 
140
153
  get bvh() {
@@ -81,6 +81,9 @@ export class ShadedGeometrySystem extends System {
81
81
 
82
82
  const leaf_node = sg.bvh;
83
83
 
84
+ // record entity on the BVH leaf
85
+ leaf_node.entity = entity;
86
+
84
87
  // remember entity for lookups
85
88
  sg.__entity = entity;
86
89
 
@@ -0,0 +1,52 @@
1
+ import { EngineHarness } from "../../../../EngineHarness.js";
2
+ import { ShadedGeometrySystem } from "../ShadedGeometrySystem.js";
3
+ import { GameAssetType } from "../../../../asset/GameAssetType.js";
4
+ import { EngineConfiguration } from "../../../../EngineConfiguration.js";
5
+ import { GLTFAssetLoader } from "../../../../asset/loaders/GLTFAssetLoader.js";
6
+ import { three_object_to_entity_composition } from "../three_object_to_entity_composition.js";
7
+
8
+ const eh = new EngineHarness();
9
+
10
+ /**
11
+ *
12
+ * @param {EngineHarness} harness
13
+ * @return {Promise<void>}
14
+ */
15
+ async function init(harness) {
16
+ const engine = harness.engine;
17
+
18
+ const config = new EngineConfiguration();
19
+
20
+ config.addSystem(new ShadedGeometrySystem(engine));
21
+ config.addLoader(GameAssetType.ModelGLTF_JSON, new GLTFAssetLoader());
22
+
23
+ await config.apply(engine);
24
+
25
+ eh.initialize().then(main);
26
+ }
27
+
28
+ /**
29
+ *
30
+ * @param {Engine} engine
31
+ */
32
+ async function main(engine) {
33
+
34
+ const em = engine.entityManager;
35
+
36
+ EngineHarness.buildBasics({
37
+ engine,
38
+ enableWater: false
39
+ })
40
+
41
+ const ecd = em.dataset;
42
+
43
+ const asset = await engine.assetManager.promise('moicon/06_04_hapro/08-clickable-assets-new/model.gltf', GameAssetType.ModelGLTF_JSON);
44
+
45
+ const three_object = asset.create();
46
+
47
+ const composition = three_object_to_entity_composition(three_object);
48
+
49
+ composition.build(ecd);
50
+ }
51
+
52
+ init(eh);
@@ -1,8 +1,8 @@
1
- import { EngineHarness } from "../../../EngineHarness.js";
2
- import { ShadedGeometrySystem } from "./ShadedGeometrySystem.js";
3
- import EntityBuilder from "../../../ecs/EntityBuilder.js";
4
- import { Transform } from "../../../ecs/transform/Transform.js";
5
- import { ShadedGeometry } from "./ShadedGeometry.js";
1
+ import { EngineHarness } from "../../../../EngineHarness.js";
2
+ import { ShadedGeometrySystem } from "../ShadedGeometrySystem.d.ts";
3
+ import EntityBuilder from "../../../../ecs/EntityBuilder.d.ts";
4
+ import { Transform } from "../../../../ecs/transform/Transform.d.ts";
5
+ import { ShadedGeometry } from "../ShadedGeometry.d.ts";
6
6
  import { BoxBufferGeometry, MeshBasicMaterial } from "three";
7
7
 
8
8
  const eh = new EngineHarness();
@@ -0,0 +1,37 @@
1
+ import { EntityNode } from "../../../ecs/parent/EntityNode.js";
2
+ import { Transform } from "../../../ecs/transform/Transform.js";
3
+ import { ShadedGeometry } from "./ShadedGeometry.js";
4
+
5
+ /**
6
+ *
7
+ * @param {THREE.Object3D} root
8
+ * @returns {EntityNode}
9
+ */
10
+ export function three_object_to_entity_composition(root) {
11
+ const node = new EntityNode();
12
+
13
+ const transform = new Transform();
14
+ transform.fromMatrix4(root.matrix.elements);
15
+
16
+ node.entity.add(transform);
17
+
18
+ if (root.isMesh) {
19
+ if (root.isSkinnedMesh) {
20
+ throw new Error(`Skinned meshes are not supported`);
21
+ }
22
+
23
+ node.entity.add(ShadedGeometry.from(root.geometry, root.material));
24
+ }
25
+
26
+ const children = root.children;
27
+ const child_count = children.length;
28
+ for (let i = 0; i < child_count; i++) {
29
+ const child = children[i];
30
+
31
+ const child_node = three_object_to_entity_composition(child);
32
+
33
+ node.addChild(child_node);
34
+ }
35
+
36
+ return node;
37
+ }
@@ -51,6 +51,17 @@ function custom_attribute_format_uv(spec, source) {
51
51
  spec.type = DataType.Float16;
52
52
  }
53
53
 
54
+ /**
55
+ *
56
+ * @param {AttributeSpec} spec
57
+ * @param {THREE.BufferAttribute} source
58
+ */
59
+ function custom_attribute_format_color(spec, source) {
60
+ // crush color data to 8 bit
61
+ spec.type = DataType.Uint8;
62
+ spec.normalized = true;
63
+ }
64
+
54
65
  /**
55
66
  *
56
67
  * @param {AttributeSpec} spec
@@ -70,7 +81,11 @@ function format_attribute(spec, source, name) {
70
81
  case 'normal':
71
82
  custom_attribute_format_normal(spec, source);
72
83
  break;
84
+ case 'color':
85
+ custom_attribute_format_color(spec, source);
86
+ break;
73
87
  default:
88
+ spec.normalized = source.normalized;
74
89
  spec.type = typedArrayToDataType(source.array)
75
90
  }
76
91
 
@@ -14,6 +14,8 @@ export function compute_micron_buffer_array_constructor(type) {
14
14
  return Uint16Array;
15
15
  case DataType.Uint16:
16
16
  return Uint16Array;
17
+ case DataType.Uint8:
18
+ return Uint8Array;
17
19
  case DataType.Int8:
18
20
  return Int8Array;
19
21
  default:
@@ -9,6 +9,9 @@ import { DataType } from "../../../../core/collection/table/DataType.js";
9
9
  import { passThrough } from "../../../../core/function/Functions.js";
10
10
  import { to_half_float_uint16 } from "../../../../core/binary/to_half_float_uint16.js";
11
11
  import { compute_micron_buffer_array_constructor } from "./compute_micron_buffer_array_constructor.js";
12
+ import { typedArrayToDataType } from "../../../../core/collection/array/typedArrayToDataType.js";
13
+ import { dataTypeByteSize } from "../../../../core/collection/table/DataTypeByteSizes.js";
14
+ import { clamp } from "../../../../core/math/clamp.js";
12
15
 
13
16
  function UintArrayForCount(count) {
14
17
 
@@ -90,6 +93,17 @@ function copy_attribute_data(vertex_count, index_map_backward, itemSize, attribu
90
93
  }
91
94
  }
92
95
 
96
+ /**
97
+ *
98
+ * @param {number} value
99
+ * @returns {number}
100
+ */
101
+ function resample_normalized_uint16_to_uint8(value) {
102
+ const normalized = clamp(value / 65535, 0, 1);
103
+
104
+ return Math.round(normalized * 255);
105
+ }
106
+
93
107
  /**
94
108
  *
95
109
  * @param {number} value
@@ -105,7 +119,8 @@ function normalize_to_int8(value) {
105
119
  const DataConverterType = {
106
120
  None: passThrough,
107
121
  Number2HalfFloat: to_half_float_uint16,
108
- Normalize2Int8: normalize_to_int8
122
+ Normalize2Int8: normalize_to_int8,
123
+ ResampleNormalized_Uint16_to_Uint8: resample_normalized_uint16_to_uint8
109
124
  };
110
125
 
111
126
  /**
@@ -115,17 +130,36 @@ const DataConverterType = {
115
130
  * @returns {DataConverterType}
116
131
  */
117
132
  function compute_attribute_data_converter(source, spec) {
118
- if (spec.type === DataType.Float16) {
133
+ const target_type = spec.type;
134
+ if (target_type === DataType.Float16) {
119
135
  return DataConverterType.Number2HalfFloat;
120
136
  } else if (spec.normalized && !source.normalized) {
121
137
  // source data can be normalized but isn't
122
- if (spec.type === DataType.Int8) {
138
+ if (target_type === DataType.Int8) {
123
139
  return DataConverterType.Normalize2Int8;
124
140
  } else {
125
- throw new Error(`Unsupported normalized type '${spec.type}'`);
141
+ throw new Error(`Unsupported normalized type '${target_type}'`);
126
142
  }
127
143
  } else {
128
- return DataConverterType.None;
144
+ const source_array = source.array;
145
+
146
+ const source_type = typedArrayToDataType(source_array);
147
+
148
+ const target_byte_size = dataTypeByteSize(target_type);
149
+ const source_byte_size = dataTypeByteSize(source_type)
150
+
151
+ if (target_byte_size !== source_byte_size) {
152
+ // wrong data size
153
+
154
+ if (target_type === DataType.Uint8 && source_type === DataType.Uint16) {
155
+ return DataConverterType.ResampleNormalized_Uint16_to_Uint8;
156
+ } else {
157
+ throw new Error(`Unsupported conversion ${source_type} -> ${target_type}`);
158
+ }
159
+
160
+ } else {
161
+ return DataConverterType.None;
162
+ }
129
163
  }
130
164
  }
131
165
 
@@ -3,6 +3,7 @@ import { assert } from "../../../../core/assert.js";
3
3
  import { computeTriangleRayIntersection } from "../../../../core/geom/3d/triangle/computeTriangleRayIntersection.js";
4
4
  import { SurfacePoint3 } from "../../../../core/geom/3d/SurfacePoint3.js";
5
5
  import { aabb3_array_intersects_ray } from "../../../../core/bvh2/aabb3/aabb3_array_intersects_ray.js";
6
+ import { sphere_array_intersects_ray } from "../../../../core/geom/3d/sphere/sphere_array_intersects_ray.js";
6
7
 
7
8
  let id_counter = 0;
8
9
 
@@ -105,43 +106,18 @@ export class MicronGeometry {
105
106
 
106
107
  const patch = stack[stack_cursor];
107
108
 
108
- // using group bounds because it's conservative, as we want to be able to hit LOD_0
109
-
110
- // sphere test
111
-
112
- // TODO check what's wrong with sphere tests and re-enable them
113
- // if (!sphere_array_intersects_ray(patch.group_bounding_sphere,
114
- // origin_x, origin_y, origin_z,
115
- // direction_x, direction_y, direction_z
116
- // )) {
117
- // continue;
118
- // }
119
-
120
-
121
- // box test (more expensive, different volume)
122
- if (!aabb3_array_intersects_ray(
123
- patch.group_bounding_box,
124
- origin_x, origin_y, origin_z,
125
- direction_x, direction_y, direction_z
126
- )) {
127
- // no intersection
128
- continue;
129
- }
130
-
131
109
  if (patch.lod === 0) {
132
110
  // LOD 0 - source geometry
133
111
 
134
112
  // cast against bounds
135
113
 
136
114
  // sphere test
137
-
138
- // TODO check what's wrong with sphere tests and re-enable them
139
- // if (!sphere_array_intersects_ray(patch.bounding_sphere,
140
- // origin_x, origin_y, origin_z,
141
- // direction_x, direction_y, direction_z
142
- // )) {
143
- // continue;
144
- // }
115
+ if (!sphere_array_intersects_ray(patch.bounding_sphere,
116
+ origin_x, origin_y, origin_z,
117
+ direction_x, direction_y, direction_z
118
+ )) {
119
+ continue;
120
+ }
145
121
 
146
122
  // box test
147
123
  if (!aabb3_array_intersects_ray(
@@ -200,6 +176,28 @@ export class MicronGeometry {
200
176
  }
201
177
 
202
178
  } else {
179
+
180
+ // using group bounds because it's conservative, as we want to be able to hit LOD_0
181
+
182
+ // sphere test
183
+ if (!sphere_array_intersects_ray(patch.group_bounding_sphere,
184
+ origin_x, origin_y, origin_z,
185
+ direction_x, direction_y, direction_z
186
+ )) {
187
+ continue;
188
+ }
189
+
190
+
191
+ // box test (more expensive, different volume)
192
+ if (!aabb3_array_intersects_ray(
193
+ patch.group_bounding_box,
194
+ origin_x, origin_y, origin_z,
195
+ direction_x, direction_y, direction_z
196
+ )) {
197
+ // no intersection
198
+ continue;
199
+ }
200
+
203
201
  // refine more
204
202
  const children = patch.children;
205
203
 
@@ -455,7 +455,7 @@ function init() {
455
455
  }
456
456
 
457
457
  // sample_gltf_sponza();
458
- gltf_sample('data/models/RTS_Buildings_Humans/18/Building_R_18_out/Building_R_18.gltf');
458
+ // gltf_sample('data/models/RTS_Buildings_Humans/18/Building_R_18_out/Building_R_18.gltf');
459
459
  // sample_lucy();
460
460
  // sample_gltf_hapro_2();
461
461
  // sample_gltf_hapro_1();
@@ -468,6 +468,12 @@ function init() {
468
468
  // gltf_sample('moicon/29_Dec_2021_Nammo/1/model.gltf');
469
469
  // gltf_sample('moicon/29_Dec_2021_Nammo/1-onematerial/model.gltf');
470
470
  // gltf_sample('moicon/29_Dec_2021_Nammo/2/model.gltf');
471
+ // gltf_sample('moicon/03_Jan_22_Demo/1-ground_floor/model.gltf');
472
+ // gltf_sample('moicon/03_Jan_22_Demo/2-1st_floor/model.gltf');
473
+ // gltf_sample('moicon/03_Jan_22_Demo/3-area_ground_floor/model.gltf');
474
+ // gltf_sample('moicon/03_Jan_22_Demo/4-area_1st_floor/model.gltf');
475
+ // gltf_sample('moicon/03_Jan_22_Demo/8-assets/model.gltf');
476
+ gltf_sample('moicon/03_Jan_22_Demo/worker_test/model.gltf');
471
477
 
472
478
  document.addEventListener("mousemove", onDocumentMouseMove);
473
479
 
@@ -24,6 +24,9 @@ function normalized_internal_format(dt, channel_count) {
24
24
  case DataType.Int8:
25
25
  suffix = '8_SNORM';
26
26
  break;
27
+ case DataType.Uint16:
28
+ suffix = '16UI';
29
+ break;
27
30
  default:
28
31
  throw new Error(`Unsupported data type '${dt}'`);
29
32
  }
@@ -160,6 +160,29 @@ export function shader_rewrite_standard(shader, uniforms, spec, column_count) {
160
160
 
161
161
  if (attribute_spec_color !== undefined) {
162
162
  // using color
163
+ const vx_color_value = `texelFetch(${gen_micron_vertex_attribute_texture_name('color')}, micron_vertex_uv, 0)`
164
+
165
+ rewrittenVertexShader = rewrittenVertexShader.replace(
166
+ '#include <color_vertex>',
167
+ `
168
+ #if defined( USE_COLOR_ALPHA )
169
+ vColor = vec4( 1.0 );
170
+ #elif defined( USE_COLOR ) || defined( USE_INSTANCING_COLOR )
171
+ vColor = vec3( 1.0 );
172
+ #endif
173
+
174
+ #ifdef USE_COLOR
175
+
176
+ #ifdef USE_COLOR_ALPHA
177
+ vColor *= ${vx_color_value}.rgba;
178
+ #else
179
+ vColor *= ${vx_color_value}.rgb;
180
+ #endif
181
+
182
+ #endif
183
+
184
+ `
185
+ );
163
186
 
164
187
  }
165
188
 
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.29",
8
+ "version": "2.34.33",
9
9
  "dependencies": {
10
10
  "gl-matrix": "3.4.3",
11
11
  "fast-levenshtein": "2.0.6",