@woosh/meep-engine 2.34.2 → 2.34.3

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.
package/editor/Editor.js CHANGED
@@ -25,7 +25,7 @@ import TopDownCameraController from "../engine/graphics/ecs/camera/topdown/TopDo
25
25
  import { Transform } from "../engine/ecs/transform/Transform.js";
26
26
  import TopDownCameraControllerSystem, { setCameraControllerFromTransform } from "../engine/graphics/ecs/camera/topdown/TopDownCameraControllerSystem.js";
27
27
  import { downloadAsFile } from "../core/binary/ByteArrayTools.js";
28
- import BinaryBufferSerialization from "../engine/ecs/storage/BinaryBufferSerializer.js";
28
+ import BinaryBufferSerializer from "../engine/ecs/storage/BinaryBufferSerializer.js";
29
29
  import { EncodingBinaryBuffer } from "../core/binary/EncodingBinaryBuffer.js";
30
30
  import { EntityComponentDataset } from "../engine/ecs/EntityComponentDataset.js";
31
31
  import InputControllerSystem from "../engine/input/ecs/systems/InputControllerSystem.js";
@@ -383,7 +383,7 @@ function Editor() {
383
383
  // Set music tracks back to time=0
384
384
  resetSoundEmitterTracks(dataset);
385
385
 
386
- const serializer = new BinaryBufferSerialization();
386
+ const serializer = new BinaryBufferSerializer();
387
387
  serializer.registry = engine.binarySerializationRegistry;
388
388
 
389
389
  const state = new EncodingBinaryBuffer();
@@ -328,7 +328,6 @@ export class EngineHarness {
328
328
  * @param {number} [cameraEntity]
329
329
  * @param {Engine} engine
330
330
  * @param {number} [sensitivity]
331
- * @param {Element} domElement
332
331
  * @param {EntityComponentDataset} [ecd]
333
332
  * @returns {EntityBuilder}
334
333
  */
@@ -48,7 +48,7 @@ function isEntityTransient(entity, dataset, smComponentIndex, componentInstance)
48
48
  return isTransient;
49
49
  }
50
50
 
51
- class BinaryBufferSerialization {
51
+ class BinaryBufferSerializer {
52
52
  constructor() {
53
53
  /**
54
54
  *
@@ -171,4 +171,4 @@ class BinaryBufferSerialization {
171
171
  }
172
172
  }
173
173
 
174
- export default BinaryBufferSerialization;
174
+ export default BinaryBufferSerializer;
@@ -267,6 +267,15 @@ export class TerrainLayers {
267
267
  return index;
268
268
  }
269
269
 
270
+ /**
271
+ *
272
+ * @param {TerrainLayer} layer
273
+ * @return {boolean}
274
+ */
275
+ removeLayer(layer) {
276
+ return this.layers.removeOneOf(layer);
277
+ }
278
+
270
279
  /**
271
280
  *
272
281
  * @param {TerrainLayer} layer
@@ -159,6 +159,39 @@ export class SplatMapping {
159
159
 
160
160
  }
161
161
 
162
+ /**
163
+ *
164
+ * @param {Sampler2D} destination
165
+ * @param {number} index
166
+ * @param {number} channel
167
+ */
168
+ readLayerToSampler(destination, index, channel = 0) {
169
+ const weightData = this.weightData;
170
+
171
+ const width = this.size.x;
172
+ const height = this.size.y;
173
+
174
+ assert.equal(destination.width, width, 'destination dimensions mismatch');
175
+ assert.equal(destination.height, height, 'destination dimensions mismatch');
176
+
177
+ const dataOffset = index * width * height;
178
+
179
+ for (let y = 0; y < height; y++) {
180
+
181
+ for (let x = 0; x < width; x++) {
182
+
183
+ const targetIndex = y * width + x;
184
+ const targetAddress = dataOffset + targetIndex;
185
+
186
+ const value = weightData[targetAddress];
187
+
188
+ destination.writeChannel(x, y, channel, value);
189
+
190
+ }
191
+ }
192
+
193
+ }
194
+
162
195
  /**
163
196
  *
164
197
  * @param {Uint8Array} source
@@ -180,6 +180,29 @@ export class MicronGeometry {
180
180
  return hits;
181
181
  }
182
182
 
183
+ /**
184
+ *
185
+ * @param {number} lod
186
+ * @returns {number}
187
+ */
188
+ computeLodFaceCount(lod) {
189
+ let r = 0;
190
+
191
+ const patches = this.patches;
192
+ const patch_count = patches.length;
193
+ for (let i = 0; i < patch_count; i++) {
194
+ const patch = patches[i];
195
+
196
+ if (patch.lod !== lod) {
197
+ continue;
198
+ }
199
+
200
+ r += patch.polygon_count;
201
+ }
202
+
203
+ return r;
204
+ }
205
+
183
206
  get patches() {
184
207
  return this.__patches;
185
208
  }
@@ -209,17 +232,21 @@ export class MicronGeometry {
209
232
 
210
233
  /**
211
234
  * Build geometry proxy from the patches
212
- * @param {number} percent percentage of the original geometry to keep
235
+ * @param {number} fraction percentage of the original geometry to keep
236
+ * @param {number} min minimum number of triangles to keep, this will be respected first before percentage value
213
237
  */
214
- buildProxy(percent) {
238
+ buildProxy(fraction, min) {
215
239
  throw new Error('Not Implemented');
216
240
  }
217
241
 
218
242
  computeByteSize() {
219
243
  let result = 0;
220
244
 
221
- for (let i = 0; i < this.__patches.length; i++) {
222
- result += this.__patches[i].computeByteSize();
245
+ const patches = this.__patches;
246
+ const patch_count = patches.length;
247
+
248
+ for (let i = 0; i < patch_count; i++) {
249
+ result += patches[i].computeByteSize();
223
250
  }
224
251
 
225
252
  return result;
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.2",
8
+ "version": "2.34.3",
9
9
  "dependencies": {
10
10
  "gl-matrix": "3.4.3",
11
11
  "fast-levenshtein": "2.0.6",
@@ -0,0 +1,346 @@
1
+ import { EngineHarness } from "../../engine/EngineHarness.js";
2
+ import EmptyView from "../../view/elements/EmptyView.js";
3
+ import { EngineConfiguration } from "../../engine/EngineConfiguration.js";
4
+ import { buildCubeURLs } from "../../engine/graphics/texture/cubemap/buildCubeURLs.js";
5
+ import { ArrayBufferLoader } from "../../engine/asset/loaders/ArrayBufferLoader.js";
6
+ import { TerrainFlags } from "../../engine/ecs/terrain/ecs/TerrainFlags.js";
7
+ import Signal from "../../core/events/signal/Signal.js";
8
+ import { NativeListController } from "../../view/controller/controls/NativeListController.js";
9
+ import { TerrainLayer } from "../../engine/ecs/terrain/ecs/layers/TerrainLayer.js";
10
+ import CheckersTextureURI from "../../engine/graphics/texture/CheckersTextureURI.js";
11
+ import { obtainTerrain } from "../../engine/ecs/terrain/util/obtainTerrain.js";
12
+ import { Cache } from "../../core/cache/Cache.js";
13
+ import { computeStringHash } from "../../core/primitives/strings/computeStringHash.js";
14
+ import { noop, strictEquals } from "../../core/function/Functions.js";
15
+ import { scaleSampler2D } from "../../engine/graphics/texture/sampler/scaleSampler2D.js";
16
+ import { Sampler2D } from "../../engine/graphics/texture/sampler/Sampler2D.js";
17
+ import { DataType } from "../../core/collection/table/DataType.js";
18
+ import { compute_typed_array_constructor_from_data_type } from "../../core/collection/table/DataType2TypedArrayConstructorMapping.js";
19
+ import { copy_Sampler2D_channel_data } from "../../engine/graphics/texture/sampler/copy_Sampler2D_channel_data.js";
20
+ import { TextureAssetLoader } from "../../engine/asset/loaders/texture/TextureAssetLoader.js";
21
+ import { CanvasView } from "../../view/elements/CanvasView.js";
22
+ import convertSampler2D2Canvas from "../../engine/graphics/texture/sampler/Sampler2D2Canvas.js";
23
+
24
+ const engineHarness = new EngineHarness();
25
+
26
+ /**
27
+ *
28
+ * @param {Engine} engine
29
+ */
30
+ function makeConfig(engine) {
31
+
32
+ const config = new EngineConfiguration();
33
+
34
+ config.addLoader('arraybuffer', new ArrayBufferLoader());
35
+ config.addLoader('texture', new TextureAssetLoader());
36
+
37
+ return config;
38
+ }
39
+
40
+ class EditorTextureSlot {
41
+ constructor() {
42
+ this.onChanged = new Signal();
43
+
44
+ this.sampler = null;
45
+
46
+ this.__read = noop;
47
+ this.__write = noop;
48
+
49
+ /**
50
+ *
51
+ * @type {number}
52
+ */
53
+ this.itemSize = 1;
54
+ }
55
+
56
+ read() {
57
+ this.__read(this.sampler);
58
+
59
+ this.onChanged.send0();
60
+ }
61
+
62
+ write() {
63
+ this.__write(this.sampler);
64
+ }
65
+
66
+ /**
67
+ *
68
+ * @param {(Sampler2D)=>any} read
69
+ * @param {(Sampler2D)=>any} write
70
+ * @param {number} [itemSize]
71
+ * @param {DataType} [dataType]
72
+ * @return {EditorTextureSlot}
73
+ */
74
+ static from({ read, write, itemSize = 1, dataType = DataType.Uint8 }) {
75
+ const r = new EditorTextureSlot();
76
+
77
+ r.__read = read;
78
+ r.__write = write;
79
+
80
+ const TA = compute_typed_array_constructor_from_data_type(dataType);
81
+
82
+ r.sampler = new Sampler2D(new TA(itemSize), itemSize, 1, 1);
83
+
84
+ return r;
85
+ }
86
+ }
87
+
88
+ class TextureLayerEditor {
89
+ constructor() {
90
+ this.index = 0;
91
+
92
+ /**
93
+ *
94
+ * @type {Terrain|null}
95
+ * @private
96
+ */
97
+ this.__terrain = null;
98
+
99
+ this.__splat = EditorTextureSlot.from({
100
+ read: (target) => {
101
+ const terrain = this.__terrain;
102
+
103
+ target.resize(terrain.splat.size.x, terrain.splat.size.y)
104
+
105
+ terrain.splat.readLayerToSampler(target, this.index, 0);
106
+ },
107
+ write: (source) => {
108
+ this.__terrain.splat.writeLayerFromSampler(source, this.index, 0);
109
+ },
110
+ itemSize: 1
111
+ });
112
+ this.__diffuse = EditorTextureSlot.from({
113
+ read: (target) => {
114
+
115
+ const layers = this.__terrain.layers;
116
+
117
+ const layer = layers.get(this.index);
118
+
119
+ target.resize(layer.diffuse.width, layer.diffuse.height);
120
+ copy_Sampler2D_channel_data(layer.diffuse, target);
121
+ },
122
+ write: (source) => {
123
+ const layer = this.__terrain.layers.get(this.index);
124
+
125
+ copy_Sampler2D_channel_data(source, layer.diffuse);
126
+
127
+ this.__terrain.layers.writeLayerDataIntoTexture(this.index);
128
+ },
129
+ itemSize: 3
130
+ });
131
+ }
132
+
133
+ /**
134
+ * @param {Terrain} t
135
+ */
136
+ set terrain(t) {
137
+ this.__terrain = t;
138
+ }
139
+
140
+ init() {
141
+ this.__splat.read();
142
+ this.__diffuse.read();
143
+ }
144
+ }
145
+
146
+ /**
147
+ *
148
+ * @param {EditorTextureSlot} slot
149
+ */
150
+ function makeTextureSlotView({ slot }) {
151
+
152
+ const preview = new CanvasView();
153
+ preview.size.set(64, 64);
154
+
155
+
156
+ function update() {
157
+ convertSampler2D2Canvas(slot.sampler, 1, 0, preview.el);
158
+ }
159
+
160
+ preview.on.linked.add(update);
161
+ preview.bindSignal(slot.onChanged, update);
162
+
163
+
164
+ return preview;
165
+ }
166
+
167
+ /**
168
+ *
169
+ * @param {View} root
170
+ * @param {Terrain} terrain
171
+ * @param {Engine} engine
172
+ */
173
+ function makeTerrainEditor(root, terrain, engine) {
174
+ /**
175
+ *
176
+ * @type {Cache<string, Sampler2D>}
177
+ */
178
+ const PREVIEW_CACHE = new Cache({
179
+ keyHashFunction: computeStringHash,
180
+ keyEqualityFunction: strictEquals
181
+ });
182
+
183
+ const PREVIEW_SIZE = 64;
184
+
185
+ /**
186
+ *
187
+ * @param {string} url
188
+ * @return {Promise<Sampler2D>}
189
+ */
190
+ async function getPreviewFromURL(url) {
191
+ if (PREVIEW_CACHE.contains(url)) {
192
+ return PREVIEW_CACHE.get(url);
193
+ }
194
+
195
+ const asset = await engine.assetManager.promise(url, 'image');
196
+
197
+ // check cache again, might have been loaded in the meanwhile
198
+ if (PREVIEW_CACHE.contains(url)) {
199
+ return PREVIEW_CACHE.get(url);
200
+ }
201
+
202
+ const source = asset.create();
203
+
204
+ const preview = Sampler2D.uint8(4, PREVIEW_SIZE, PREVIEW_SIZE);
205
+
206
+ scaleSampler2D(source, preview);
207
+
208
+ PREVIEW_CACHE.put(url, preview);
209
+
210
+ return preview;
211
+ }
212
+
213
+ /**
214
+ *
215
+ * @type {EditorTextureSlot|null}
216
+ */
217
+ let active_slot = null;
218
+
219
+ function rebuild() {
220
+ // rebuild terrain
221
+ terrain.clearFlag(TerrainFlags.Built);
222
+ terrain.build(engine.assetManager);
223
+ }
224
+
225
+ function build_splat_editor() {
226
+ /**
227
+ *
228
+ * @type {Map<TerrainLayer, TextureLayerEditor>}
229
+ */
230
+ const editors = new Map();
231
+
232
+ return new NativeListController({
233
+ model: terrain.layers.layers,
234
+ elementFactory() {
235
+ return TerrainLayer.from(CheckersTextureURI, 1, 1);
236
+ },
237
+ /**
238
+ *
239
+ * @param {TerrainLayer} layer
240
+ */
241
+ elementViewFactory(layer) {
242
+ const e = new TextureLayerEditor();
243
+
244
+ editors.set(layer, e);
245
+
246
+ const layer_index = terrain.layers.layers.indexOf(layer);
247
+
248
+ e.terrain = terrain;
249
+ e.index = layer_index;
250
+
251
+
252
+ e.init();
253
+
254
+ const view = new EmptyView();
255
+
256
+
257
+ view.addChild(makeTextureSlotView({
258
+ slot: e.__diffuse
259
+ }));
260
+ view.addChild(makeTextureSlotView({
261
+ slot: e.__splat
262
+ }));
263
+
264
+ view.bindSignal(layer.onChanged, e.__diffuse.read, e.__diffuse);
265
+
266
+ return view;
267
+ },
268
+ operationAdd(list, el) {
269
+ terrain.layers.addLayer(el);
270
+ terrain.splat.addWeightLayer();
271
+ },
272
+ operationRemove(list, el) {
273
+ const index = terrain.layers.layers.indexOf(el);
274
+ terrain.layers.removeLayer(el);
275
+ terrain.splat.removeWeightLayer(index);
276
+
277
+ editors.delete(el);
278
+
279
+ // re-shuffle remaining editors
280
+
281
+ for (const [layer, editor] of editors) {
282
+ if (editor.index > index) {
283
+ editor.index--;
284
+ editor.init();
285
+ }
286
+ }
287
+ }
288
+ });
289
+ }
290
+
291
+ root.addChild(build_splat_editor());
292
+ }
293
+
294
+ /**
295
+ *
296
+ * @param {Engine} engine
297
+ * @return {string}
298
+ */
299
+ async function main(engine) {
300
+
301
+ //disable drag and drop on game canvas
302
+ engine.graphics.domElement.addEventListener('dragover', (e) => e.preventDefault());
303
+ engine.graphics.domElement.addEventListener('drop', (e) => e.preventDefault());
304
+
305
+
306
+ EngineHarness.buildLights({ engine });
307
+ const camera = EngineHarness.buildCamera({ engine });
308
+ EngineHarness.buildOrbitalCameraController({ engine, cameraEntity: camera.entity })
309
+
310
+ await EngineHarness.buildTerrain({ engine });
311
+
312
+ const vMain = new EmptyView({ classList: ['ui-texture-testing-tool-view'] });
313
+
314
+ vMain.css({
315
+ position: "absolute",
316
+ left: "0",
317
+ top: "0",
318
+ pointerEvents: "initial"
319
+ });
320
+
321
+ makeTerrainEditor(vMain, obtainTerrain(engine.entityManager.dataset), engine);
322
+
323
+
324
+ document.body.appendChild(vMain.el);
325
+ vMain.link();
326
+ }
327
+
328
+
329
+ /**
330
+ *
331
+ * @param {EngineHarness} harness
332
+ */
333
+ async function init(harness) {
334
+
335
+ const engine = harness.engine;
336
+
337
+ await makeConfig(engine).apply(engine);
338
+
339
+ await engine.graphics.loadEnvironmentMap(buildCubeURLs('data/textures/cubemaps/hip_miramar/32/', '.png'));
340
+
341
+ await harness.initialize();
342
+
343
+ main(engine);
344
+ }
345
+
346
+ init(engineHarness);
@@ -27,8 +27,8 @@ export class NativeListController extends View {
27
27
  /**
28
28
  * @template T
29
29
  * @param {List<T>} model
30
- * @param {function} elementViewFactory
31
- * @param {function} elementFactory
30
+ * @param {(T)=>View} elementViewFactory
31
+ * @param {()=>T} elementFactory
32
32
  * @param {string[]} [classList]
33
33
  * @param {function(List<T>, T)} [operationAdd] Allows user to supply a special operation for adding an element to the list
34
34
  * @param {function(List<T>, T)} [operationRemove] Allows user to supply a special operation for removing an element from the list
@@ -43,7 +43,7 @@ export class NativeListController extends View {
43
43
  }) {
44
44
  super();
45
45
 
46
- assert.isFunction(elementViewFactory,'elementViewFactory');
46
+ assert.isFunction(elementViewFactory, 'elementViewFactory');
47
47
 
48
48
  this.el = document.createElement('div');
49
49
 
@@ -55,6 +55,9 @@ export class NativeListController extends View {
55
55
  elementFactory(el) {
56
56
  const elementView = elementViewFactory(el);
57
57
 
58
+ assert.defined(elementView, 'elementView');
59
+ assert.equal(elementView.isView, true, 'elementView.isView !== true');
60
+
58
61
  const vWrapper = new EmptyView({ classList: ['element-wrapper'] });
59
62
 
60
63
  elementView.addClass('wrapped-element');
@@ -1,32 +0,0 @@
1
- import { Sampler2D } from "../Sampler2D.js";
2
- import { scaleSampler2D } from "../scaleSampler2D.js";
3
- import convertSampler2D2Canvas from "../Sampler2D2Canvas.js";
4
- import { typedArrayConstructorByInstance } from "../typedArrayConstructorByInstance.js";
5
-
6
- /**
7
- *
8
- * @param {Sampler2D} s
9
- * @param {number} x
10
- * @param {number} y
11
- * @param scale
12
- * @param offset
13
- * @param {number} resolution
14
- */
15
- export function paintSamplerOnHTML(s, x, y, scale, offset, resolution=256) {
16
-
17
- const d = resolution;
18
-
19
- const TA = typedArrayConstructorByInstance(s.data);
20
-
21
- const target = new Sampler2D(new TA(d * d * s.itemSize), s.itemSize, d, d);
22
-
23
- scaleSampler2D(s, target);
24
-
25
- const c = convertSampler2D2Canvas(target, scale, offset);
26
- c.style.zIndex = 1000;
27
- c.style.position = "absolute";
28
- c.style.left = `${x}px`;
29
- c.style.top = `${y}px`;
30
-
31
- document.body.appendChild(c);
32
- }