@woosh/meep-engine 2.42.8 → 2.43.0

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.
Files changed (136) hide show
  1. package/core/binary/BitSet.js +4 -4
  2. package/core/binary/ctz32.js +40 -0
  3. package/core/collection/ObservedMap.js +61 -57
  4. package/core/collection/heap/FastBinaryHeap.js +7 -1
  5. package/core/collection/heap/Uint32Heap.js +19 -0
  6. package/core/collection/map/AsyncLoadingCache.js +3 -1
  7. package/core/geom/2d/compute_polygon_area_2d.js +32 -0
  8. package/core/geom/2d/compute_polygon_area_2d.spec.js +10 -0
  9. package/core/geom/2d/compute_triangle_area_2d.js +15 -0
  10. package/core/geom/2d/compute_triangle_area_2d.spec.js +9 -0
  11. package/core/geom/2d/convex-hull/convex_hull_jarvis_2d.js +64 -0
  12. package/core/geom/2d/convex-hull/convex_hull_jarvis_2d.spec.js +33 -0
  13. package/core/geom/2d/convex-hull/convex_hull_monotone_2d.js +82 -0
  14. package/core/geom/2d/convex-hull/fixed_convex_hull_humus.js +135 -0
  15. package/core/geom/2d/convex-hull/fixed_convex_hull_relaxation.js +282 -0
  16. package/core/geom/2d/convex-hull/orientation3.js +444 -0
  17. package/core/geom/2d/convex-hull/orientation3_array.js +22 -0
  18. package/core/geom/2d/convex-hull/orientation3_v2.js +12 -0
  19. package/core/geom/2d/intersect_ray_2d.js +56 -0
  20. package/core/geom/2d/quad-tree/QuadTreeNode.js +0 -81
  21. package/core/geom/2d/quad-tree/qt_match_data_by_circle.js +70 -0
  22. package/core/geom/3d/matrix/m4_multiply_alphatensor.js +131 -0
  23. package/core/geom/3d/plane/orient3d_fast.js +2 -6
  24. package/core/geom/3d/tetrahedra/README.md +7 -0
  25. package/core/geom/3d/tetrahedra/compute_bounding_simplex_3d.js +3 -1
  26. package/core/geom/3d/tetrahedra/delaunay/Cavity.js +48 -0
  27. package/core/geom/3d/tetrahedra/{compute_delaunay_tetrahedral_mesh.js → delaunay/compute_delaunay_tetrahedral_mesh.js} +15 -7
  28. package/core/geom/3d/tetrahedra/{compute_delaunay_tetrahedral_mesh.spec.js → delaunay/compute_delaunay_tetrahedral_mesh.spec.js} +0 -0
  29. package/core/geom/3d/tetrahedra/delaunay/tetrahedral_mesh_compute_cavity.js +73 -0
  30. package/core/geom/3d/tetrahedra/delaunay/tetrahedral_mesh_walk_toward_cavity.js +48 -0
  31. package/core/geom/3d/tetrahedra/hxt/a.js +524 -0
  32. package/core/geom/3d/tetrahedra/hxt/hxt.js +140 -0
  33. package/core/geom/3d/tetrahedra/hxt/hxt.wasm +0 -0
  34. package/core/geom/3d/tetrahedra/point_in_tetrahedron_circumsphere.js +35 -20
  35. package/core/geom/3d/tetrahedra/prototypeTetrahedraBuilder.js +98 -0
  36. package/core/geom/3d/tetrahedra/tetrahedra_collection.js +60 -131
  37. package/core/geom/packing/{MaxRectangles.js → max-rect/MaxRectangles.js} +28 -124
  38. package/core/geom/packing/max-rect/removeRedundantBoxes.js +69 -0
  39. package/core/geom/packing/max-rect/removeRedundantBoxesArray.js +40 -0
  40. package/core/geom/v3_distance_above_plane.js +1 -1
  41. package/core/graph/layout/BoxLayouter.js +2 -88
  42. package/core/graph/layout/CircleLayout.js +2 -1
  43. package/core/graph/layout/box/forceIntoBox.js +45 -0
  44. package/core/graph/layout/box/pullBoxTowardsPoint.js +20 -0
  45. package/core/graph/layout/box/resolveAABB2Overlap.js +22 -0
  46. package/core/math/bessel_3.js +11 -0
  47. package/core/math/bessel_i0.js +26 -0
  48. package/core/process/executor/ConcurrentExecutor.spec.js +2 -1
  49. package/core/process/task/util/actionTask.js +19 -0
  50. package/core/process/task/util/countTask.js +62 -0
  51. package/core/process/task/util/delayTask.js +45 -0
  52. package/core/process/task/util/emptyTask.js +19 -0
  53. package/core/process/task/util/failingTask.js +17 -0
  54. package/core/process/task/util/futureTask.js +48 -0
  55. package/core/process/task/util/promiseTask.js +42 -0
  56. package/core/process/task/util/randomCountTask.js +64 -0
  57. package/core/process/task/util/wrapTaskIgnoreFailure.js +47 -0
  58. package/engine/Engine.js +8 -8
  59. package/engine/EngineBootstrapper.js +1 -1
  60. package/engine/asset/AssetManager.js +197 -53
  61. package/engine/asset/AssetRequest.js +32 -0
  62. package/engine/asset/loaders/ArrayBufferLoader.js +62 -50
  63. package/engine/asset/loaders/image/png/PNG.js +15 -1
  64. package/engine/asset/loaders/image/png/PNGReader.js +3 -2
  65. package/engine/ecs/foliage/ecs/InstancedMeshUtils.js +2 -1
  66. package/engine/ecs/storage/BinaryBufferDeSerializer.js +1 -1
  67. package/engine/ecs/storage/JSONDeSerializer.js +2 -1
  68. package/engine/ecs/terrain/ecs/splat/SplatMapOptimizer.js +2 -1
  69. package/engine/ecs/terrain/ecs/splat/SplatMapping.js +1 -1
  70. package/engine/graphics/camera/makeScreenScissorFrustum.js +1 -1
  71. package/engine/graphics/camera/testClippingPlaneComputation.js +4 -45
  72. package/engine/graphics/ecs/camera/FrustumProjector.js +6 -0
  73. package/engine/graphics/ecs/decal/v2/FPDecalSystem.js +5 -0
  74. package/engine/graphics/ecs/decal/v2/prototypeDecalSystem.js +23 -4
  75. package/engine/graphics/ecs/highlight/plugin/OutlineRenderPlugin.js +1 -1
  76. package/engine/graphics/ecs/mesh-v2/ShadedGeometry.js +11 -0
  77. package/engine/graphics/geometry/FULL_SCREEN_TRIANGLE_GEOMETRY.js +1 -2
  78. package/engine/graphics/impostors/octahedral/ImpostorBaker.js +5 -2
  79. package/engine/graphics/impostors/octahedral/ImpostorDescription.js +18 -0
  80. package/engine/graphics/impostors/octahedral/bake/prepare_bake_material.js +15 -0
  81. package/engine/graphics/impostors/octahedral/prototypeBaker.js +66 -79
  82. package/engine/graphics/impostors/octahedral/shader/ImpostorShaderWireframeV0.js +134 -0
  83. package/engine/graphics/impostors/octahedral/util/build_cutout_from_atlas_by_alpha.js +128 -0
  84. package/engine/graphics/impostors/octahedral/util/build_geometry_from_cutout_shape.js +32 -0
  85. package/engine/graphics/impostors/octahedral/util/load_mesh_for_bake.js +31 -0
  86. package/engine/graphics/impostors/octahedral/util/makeImpostorAtlasPreview.js +107 -0
  87. package/engine/graphics/material/manager/ManagedMaterial.js +4 -0
  88. package/engine/graphics/material/manager/MaterialManager.js +1 -0
  89. package/engine/graphics/material/optimization/MaterialOptimizationContext.js +7 -3
  90. package/engine/graphics/particles/particular/engine/renderers/billboard/ParticleBillboardMaterial.js +2 -2
  91. package/engine/graphics/render/forward_plus/prototype/prototypeLightManager.js +2 -2
  92. package/engine/graphics/render/visibility/hiz/buildCanvasViewFromTexture.js +83 -27
  93. package/engine/graphics/shadows/ShadowMapRenderer.js +11 -4
  94. package/engine/graphics/texture/atlas/AbstractTextureAtlas.js +2 -1
  95. package/engine/graphics/texture/atlas/CachingTextureAtlas.js +208 -38
  96. package/engine/graphics/texture/atlas/TextureAtlas.js +31 -24
  97. package/engine/graphics/texture/atlas/gpu/WebGLTextureAtlas.js +1 -1
  98. package/engine/graphics/texture/sampler/filter/box.js +16 -0
  99. package/engine/graphics/texture/sampler/filter/cubic2.js +32 -0
  100. package/engine/graphics/texture/sampler/filter/gaussian.js +16 -0
  101. package/engine/graphics/texture/sampler/filter/kaiser_1.js +19 -0
  102. package/engine/graphics/texture/sampler/filter/kaiser_bessel_window.js +19 -0
  103. package/engine/graphics/texture/sampler/filter/mitchell.js +55 -0
  104. package/engine/graphics/texture/sampler/filter/sampler2d_scale_down_generic.js +109 -0
  105. package/engine/graphics/texture/sampler/filter/triangle.js +19 -0
  106. package/engine/graphics/texture/sampler/prototypeSamplerFiltering.js +187 -86
  107. package/engine/graphics/texture/sampler/sampler2_d_scale_down_lanczos.js +77 -25
  108. package/engine/graphics/texture/sampler/search/make_edge_condition_channel_threshold.js +34 -0
  109. package/engine/graphics/texture/sampler/search/sampler2d_find_pixels.js +24 -0
  110. package/engine/graphics/texture/sprite/prototypeSpriteCutoutGeometry.js +212 -0
  111. package/engine/knowledge/database/StaticKnowledgeDataTable.js +1 -1
  112. package/engine/navigation/grid/AStar.js +1 -1
  113. package/engine/scene/Scene.js +1 -1
  114. package/engine/scene/SerializedScene.js +1 -1
  115. package/engine/scene/transitionToScene.js +3 -1
  116. package/generation/example/main.js +1 -1
  117. package/generation/grid/generation/GridTaskApplyActionToCells.js +1 -1
  118. package/generation/grid/generation/GridTaskDensityMarkerDistribution.js +1 -1
  119. package/generation/grid/generation/GridTaskExecuteRuleTimes.js +1 -1
  120. package/generation/grid/generation/NoopGridTaskGenerator.js +1 -1
  121. package/generation/grid/generation/discrete/GridTaskCellularAutomata.js +2 -1
  122. package/generation/grid/generation/discrete/GridTaskConnectRooms.js +1 -1
  123. package/generation/grid/generation/discrete/layer/GridTaskBuildSourceDistanceMap.js +3 -2
  124. package/generation/grid/generation/discrete/layer/GridTaskDistanceToMarkers.js +1 -1
  125. package/generation/grid/generation/grid/GridTaskAddNodesFixed.js +1 -1
  126. package/generation/grid/generation/road/GridTaskGenerateRoads.js +3 -2
  127. package/generation/grid/generation/util/buildDistanceMapToObjective.js +1 -1
  128. package/generation/markers/GridActionRuleSet.js +2 -1
  129. package/generation/placement/GridCellActionTransformNearbyMarkers.js +2 -4
  130. package/generation/theme/ThemeEngine.js +4 -1
  131. package/package.json +1 -1
  132. package/view/asset/AssetLoaderStatusView.js +5 -5
  133. package/view/minimap/gl/MinimapTerrainGL.js +1 -2
  134. package/view/renderModel.js +1 -1
  135. package/view/tooltip/TooltipView.js +5 -5
  136. package/core/process/task/TaskUtils.js +0 -352
@@ -5,10 +5,10 @@ import { min2 } from "../../../../../core/math/min2.js";
5
5
  import { Sampler2D } from "../../../../graphics/texture/sampler/Sampler2D.js";
6
6
  import { assert } from "../../../../../core/assert.js";
7
7
  import { scaleSampler2D } from "../../../../graphics/texture/sampler/scaleSampler2D.js";
8
- import { countTask } from "../../../../../core/process/task/TaskUtils.js";
9
8
  import { array_copy } from "../../../../../core/collection/array/copyArray.js";
10
9
  import { typed_array_copy } from "../../../../../core/collection/array/typed/typed_array_copy.js";
11
10
  import { Base64 } from "../../../../../core/binary/Base64.js";
11
+ import { countTask } from "../../../../../core/process/task/util/countTask.js";
12
12
 
13
13
 
14
14
  /**
@@ -1,4 +1,4 @@
1
- import { Vector3 as ThreeVector3 } from "three/src/math/Vector3.js";
1
+ import { Vector3 as ThreeVector3 } from "three";
2
2
 
3
3
 
4
4
  const farTL = new ThreeVector3();
@@ -59,6 +59,7 @@ import { computeStatisticalMean } from "../../../core/math/statistics/computeSta
59
59
  import { Transform } from "../../ecs/transform/Transform.js";
60
60
  import Mesh from "../ecs/mesh/Mesh.js";
61
61
  import { frustum_from_camera } from "../ecs/camera/frustum_from_camera.js";
62
+ import { convex_hull_jarvis_2d } from "../../../core/geom/2d/convex-hull/convex_hull_jarvis_2d.js";
62
63
 
63
64
  const engineHarness = new EngineHarness();
64
65
 
@@ -190,59 +191,17 @@ function makeCameraClippingDebug(engine) {
190
191
 
191
192
  function drawConvexHull(vertices, fillColor, strokeColor) {
192
193
 
193
- const jarvis_vertices = [];
194
-
195
- /**
196
- *
197
- * @param {Vector3} p
198
- * @param {Vector3} q
199
- * @param {Vector3} r
200
- */
201
- function orientation(p, q, r) {
202
- const val = (q.z - p.z) * (r.x - q.x) - (q.x - p.x) * (r.z - q.z);
203
-
204
- if (val === 0) {
205
- return 0;
206
- }
207
-
208
- return val > 0 ? 1 : 2;
209
- }
210
-
211
- let l = 0;
212
- const n = vertices.length;
213
- for (let i = 0; i < n; i++) {
214
- if (vertices[i].x < vertices[l].x) {
215
- l = i;
216
- }
217
- }
218
-
219
- let p = l, q;
220
-
221
- do {
222
- jarvis_vertices.push(vertices[p]);
223
-
224
- q = (p + 1) % n;
225
-
226
- for (let i = 0; i < n; i++) {
227
- if (orientation(vertices[p], vertices[i], vertices[q]) === 2) {
228
- q = i;
229
- }
230
- }
231
-
232
- p = q;
233
-
234
- } while (p !== l);
235
-
194
+ const jarvis_vertices = convex_hull_jarvis_2d(vertices.map(v => [v.x, v.z]).flat(), vertices.length);
236
195
 
237
196
  ctx.fillStyle = fillColor;
238
197
  ctx.strokeStyle = strokeColor;
239
198
 
240
199
  ctx.beginPath();
241
- moveTo(jarvis_vertices[0].x, jarvis_vertices[0].z);
200
+ moveTo(vertices[jarvis_vertices[0]].x, vertices[jarvis_vertices[0]].z);
242
201
  // drawPoint(jarvis_vertices[0].x, jarvis_vertices[0].y, jarvis_vertices[0].z, 'red');
243
202
 
244
203
  for (let i = 0; i < jarvis_vertices.length; i++) {
245
- const last = jarvis_vertices[i];
204
+ const last = vertices[jarvis_vertices[i]];
246
205
 
247
206
  lineTo(last.x, last.z);
248
207
 
@@ -230,7 +230,13 @@ function unproject(_x0, _y0, _z0, _x1, _y1, _z1, camera, callback) {
230
230
  }
231
231
 
232
232
  export default {
233
+ /**
234
+ * @deprecated use non-threejs specific code
235
+ */
233
236
  project,
234
237
  unproject,
238
+ /**
239
+ * @deprecated use non-threejs specific code
240
+ */
235
241
  projectInWorldSpace
236
242
  };
@@ -146,6 +146,11 @@ class Context extends SystemEntityContext {
146
146
  }
147
147
  }
148
148
 
149
+ // TODO we can reduce memory usage and speed things up considerably by using a queue to wait for assets instead of using promises everywhere
150
+
151
+ /**
152
+ *
153
+ */
149
154
  export class FPDecalSystem extends AbstractContextSystem {
150
155
  /**
151
156
  *
@@ -13,6 +13,9 @@ import { TransformAttachmentSystem } from "../../../../ecs/transform-attachment/
13
13
  import { SGMesh } from "../../mesh-v2/aggregate/SGMesh.js";
14
14
  import { GameAssetType } from "../../../../asset/GameAssetType.js";
15
15
  import { GLTFAssetLoader } from "../../../../asset/loaders/GLTFAssetLoader.js";
16
+ import { TaskLoadingScreen } from "../../../../../view/task/TaskLoadingScreen.js";
17
+ import '../../../../../../../../css/game.scss';
18
+ import { countTask } from "../../../../../core/process/task/util/countTask.js";
16
19
 
17
20
  const decal_urls = `data/textures/icons/FantasyIconsMegaPack/MagicItems/MagicItems_png/transparent/x64/staff_13_t.png
18
21
  data/textures/icons/FantasyIconsMegaPack/MagicItems/MagicItems_png/transparent/x64/artifact_01_t.png
@@ -227,9 +230,9 @@ async function main(engine) {
227
230
 
228
231
  const random = seededRandom();
229
232
 
230
- const ENTITY_COUNT = 10000;
233
+ const ENTITY_COUNT = 100000;
231
234
 
232
- for (let i = 0; i < ENTITY_COUNT; i++) {
235
+ function makeOne() {
233
236
  const decal = new Decal();
234
237
 
235
238
  decal.uri = randomFromArray(random, decal_urls);
@@ -253,10 +256,26 @@ async function main(engine) {
253
256
  // .add(BehaviorComponent.fromOne(RotationBehavior.fromJSON({ speed: 1 })))
254
257
 
255
258
  entity.build(engine.entityManager.dataset);
256
-
257
259
  }
258
260
 
259
- await promise_time_out(10);
261
+ // for (let i = 0; i < ENTITY_COUNT; i++) {
262
+ // makeOne();
263
+ //
264
+ // }
265
+
266
+ const task = countTask(0, ENTITY_COUNT, makeOne);
267
+
268
+ TaskLoadingScreen.instance.load(engine, task);
269
+
270
+ // console.profile('spawn');
271
+
272
+ task.promise().then(() => {
273
+ // console.profileEnd('spawn');
274
+ });
275
+
276
+ engine.executor.run(task);
277
+
278
+ // await promise_time_out(10);
260
279
 
261
280
 
262
281
  }
@@ -1,7 +1,7 @@
1
1
  import { max2 } from "../../../../../core/math/max2.js";
2
2
  import { EnginePlugin } from "../../../../plugin/EnginePlugin.js";
3
3
  import { OutlineRenderer } from "../renderer/OutlineRenderer.js";
4
- import { Scene as ThreeScene } from "three/src/scenes/Scene.js";
4
+ import { Scene as ThreeScene } from "three";
5
5
  import { BlendingType } from "../../../texture/sampler/BlendingType.js";
6
6
  import { HighlightRenderGroup } from "../renderer/HighlightRenderGroup.js";
7
7
  import { ObjectPoolFactory } from "../../../../../core/model/object/ObjectPoolFactory.js";
@@ -5,6 +5,7 @@ import { aabb3_matrix4_project } from "../../../../core/geom/3d/aabb/aabb3_matri
5
5
  import { mat4 } from "gl-matrix";
6
6
  import { ray3_array_apply_matrix4 } from "../../../../core/geom/3d/ray/ray3_array_apply_matrix4.js";
7
7
  import { GeometrySpatialQueryAccelerator } from "../../geometry/buffered/query/GeometrySpatialQueryAccelerator.js";
8
+ import { assert } from "../../../../core/assert.js";
8
9
 
9
10
  /**
10
11
  * @readonly
@@ -298,6 +299,16 @@ export class ShadedGeometry {
298
299
  const y1 = max.y;
299
300
  const z1 = max.z;
300
301
 
302
+ // validate bounds
303
+ assert.notNaN(x0, 'x0');
304
+ assert.notNaN(y0, 'y0');
305
+ assert.notNaN(z0, 'z0');
306
+
307
+ assert.notNaN(x1, 'x1');
308
+ assert.notNaN(y1, 'y1');
309
+ assert.notNaN(z1, 'z1');
310
+
311
+ // marshal into transform-accepted format
301
312
  scratch_aabb3_array[0] = x0;
302
313
  scratch_aabb3_array[1] = y0;
303
314
  scratch_aabb3_array[2] = z0;
@@ -1,5 +1,4 @@
1
- import { BufferGeometry } from "three/src/core/BufferGeometry.js";
2
- import { BufferAttribute } from "three/src/core/BufferAttribute.js";
1
+ import { BufferAttribute, BufferGeometry } from "three";
3
2
 
4
3
  export const FULL_SCREEN_TRIANGLE_GEOMETRY = new BufferGeometry();
5
4
  FULL_SCREEN_TRIANGLE_GEOMETRY.setAttribute(
@@ -16,7 +16,7 @@ import { HemiOctahedralUvEncoder } from "./grid/HemiOctahedralUvEncoder.js";
16
16
  import { prepare_bake_material } from "./bake/prepare_bake_material.js";
17
17
  import { collectIteratorValueToArray } from "../../../../core/collection/IteratorUtils.js";
18
18
  import { compute_bounding_sphere } from "./bake/compute_bounding_sphere.js";
19
- import { buffer_geometry_ensure_tangents } from "../../geometry/MikkT/buffer_geometry_ensure_tangents.js";
19
+ import { build_cutout_from_atlas_by_alpha } from "./util/build_cutout_from_atlas_by_alpha.js";
20
20
 
21
21
  export class ImpostorBaker {
22
22
 
@@ -134,7 +134,7 @@ export class ImpostorBaker {
134
134
  const source_mesh = object.mesh;
135
135
 
136
136
  // ensure tangents are generated
137
- buffer_geometry_ensure_tangents(source_mesh.geometry);
137
+ //buffer_geometry_ensure_tangents(source_mesh.geometry);
138
138
 
139
139
  const source_material = source_mesh.material;
140
140
 
@@ -264,6 +264,9 @@ export class ImpostorBaker {
264
264
  // id.atlas = sampler;
265
265
  id.rt = rt;
266
266
 
267
+ // build cutout
268
+ id.cutout = build_cutout_from_atlas_by_alpha(id, renderer);
269
+
267
270
  return id;
268
271
  }
269
272
 
@@ -27,6 +27,12 @@ export class ImpostorDescription {
27
27
  */
28
28
  sphere_radius = 0;
29
29
 
30
+ /**
31
+ * How much space to leave between frames to ensure that there is no bleeding across frames
32
+ * @type {number}
33
+ */
34
+ padding = 0;
35
+
30
36
  /**
31
37
  * Baking offset
32
38
  * @type {number[]}
@@ -59,6 +65,18 @@ export class ImpostorDescription {
59
65
  */
60
66
  source_instance_count = 0;
61
67
 
68
+ /**
69
+ * Frame-relative sequence of points defining a hole-free shape
70
+ * Normalized between 0,0 and 1,1; Might extend further in some cases, should be clipped in shader in those cases
71
+ * @type {number[]}
72
+ */
73
+ cutout = [
74
+ 0, 0,
75
+ 1, 0,
76
+ 1, 1,
77
+ 0, 1
78
+ ];
79
+
62
80
 
63
81
  /**
64
82
  * TODO: replace with something non-three.js specific
@@ -1,4 +1,6 @@
1
1
  import { DataTexture, UnsignedByteType } from "three";
2
+ import { float2uint8 } from "../../../../../core/binary/float2uint8.js";
3
+ import { clamp01 } from "../../../../../core/math/clamp01.js";
2
4
 
3
5
  /**
4
6
  *
@@ -51,6 +53,19 @@ export function prepare_bake_material({
51
53
  texture_metalness = source_material.metalnessMap;
52
54
  texture_occlusion = source_material.aoMap;
53
55
  texture_emissive = source_material.emissiveMap;
56
+
57
+
58
+ if (texture_metalness === null) {
59
+ const m = float2uint8(clamp01(source_material.metalness));
60
+ texture_metalness = onePixelTexture([m, m, m, 255], cleanup_signal);
61
+ }
62
+
63
+ if (texture_roughness === null) {
64
+ const r = float2uint8(clamp01(source_material.roughness));
65
+
66
+ texture_roughness = onePixelTexture([r, r, r, 255], cleanup_signal);
67
+ }
68
+
54
69
  } else {
55
70
  if (source_material.map !== undefined && source_material.map !== null) {
56
71
  texture_diffuse = source_material.map;
@@ -1,26 +1,19 @@
1
1
  import { EngineHarness } from "../../../EngineHarness.js";
2
2
  import { ImpostorBaker } from "./ImpostorBaker.js";
3
3
  import { ShadedGeometry } from "../../ecs/mesh-v2/ShadedGeometry.js";
4
- import sampler2D2Canvas from "../../texture/sampler/Sampler2D2Canvas.js";
5
- import { CanvasView } from "../../../../view/elements/CanvasView.js";
6
4
  import EntityBuilder from "../../../ecs/EntityBuilder.js";
7
5
  import ViewportPosition from "../../../ecs/gui/position/ViewportPosition.js";
8
6
  import GUIElement from "../../../ecs/gui/GUIElement.js";
9
7
  import GUIElementSystem from "../../../ecs/gui/GUIElementSystem.js";
10
8
  import ViewportPositionSystem from "../../../ecs/gui/position/ViewportPositionSystem.js";
11
- import { WebGLRendererPool } from "../../render/RendererPool.js";
12
- import { buildCanvasViewFromTexture } from "../../render/visibility/hiz/buildCanvasViewFromTexture.js";
13
9
  import Vector2 from "../../../../core/geom/Vector2.js";
14
10
  import { GameAssetType } from "../../../asset/GameAssetType.js";
15
- import { three_object_to_entity_composition } from "../../ecs/mesh-v2/three_object_to_entity_composition.js";
16
11
  import { Transform } from "../../../ecs/transform/Transform.js";
17
12
  import { GLTFAssetLoader } from "../../../asset/loaders/GLTFAssetLoader.js";
18
13
  import { ImpostorCaptureType } from "./ImpostorCaptureType.js";
19
- import { PlaneBufferGeometry } from "three";
20
14
  import { ShadedGeometrySystem } from "../../ecs/mesh-v2/ShadedGeometrySystem.js";
21
15
  import { ImpostorShaderV0 } from "./shader/ImpostorShaderV0.js";
22
16
  import Quaternion from "../../../../core/geom/Quaternion.js";
23
- import { DEG2RAD } from "three/src/math/MathUtils.js";
24
17
  import { SGMesh } from "../../ecs/mesh-v2/aggregate/SGMesh.js";
25
18
  import { SGMeshSystem } from "../../ecs/mesh-v2/aggregate/SGMeshSystem.js";
26
19
  import { ShadedGeometryFlags } from "../../ecs/mesh-v2/ShadedGeometryFlags.js";
@@ -29,6 +22,11 @@ import { BehaviorComponent } from "../../../intelligence/behavior/ecs/BehaviorCo
29
22
  import Vector3 from "../../../../core/geom/Vector3.js";
30
23
  import { BehaviorSystem } from "../../../intelligence/behavior/ecs/BehaviorSystem.js";
31
24
  import { RotationBehavior } from "../../../intelligence/behavior/util/RotationBehavior.js";
25
+ import { DEG_TO_RAD } from "../../../../core/math/DEG_TO_RAD.js";
26
+ import { ImpostorShaderWireframeV0 } from "./shader/ImpostorShaderWireframeV0.js";
27
+ import { makeImpostorAtlasPreview } from "./util/makeImpostorAtlasPreview.js";
28
+ import { load_mesh_for_bake } from "./util/load_mesh_for_bake.js";
29
+ import { build_geometry_from_cutout_shape } from "./util/build_geometry_from_cutout_shape.js";
32
30
 
33
31
  /**
34
32
  *
@@ -42,15 +40,18 @@ async function main(engine) {
42
40
  cameraFieldOfView: 45
43
41
  });
44
42
 
43
+ const ecd = engine.entityManager.dataset;
44
+
45
45
  const baker = new ImpostorBaker();
46
46
  const renderer = engine.graphics.renderer;
47
47
  baker.renderer = renderer;
48
48
 
49
49
 
50
50
  // const path = 'data/models/LowPolyTownshipSet/Small_house/Small_house.gltf';
51
+ // const path = 'data/models/road_bike/road_bike.gltf'; //large CAD-type model
51
52
  // const path = 'data/models/LowPolyTownshipSet/Barrel/model.gltf';
52
- const path = 'data/models/LowPolyTownshipSet/Town_Hall/model.gltf';
53
- // const path = 'data/models/RTS_Buildings_Humans/18/Building_R_18_out/Building_R_18.gltf';
53
+ // const path = 'data/models/LowPolyTownshipSet/Town_Hall/model.gltf';
54
+ const path = 'data/models/RTS_Buildings_Humans/18/Building_R_18_out/Building_R_18.gltf';
54
55
  // const path = 'data/models/MOBA and Tower Defense/Tree_01.gltf';
55
56
  // const path = 'data/models/samples/transform-hierarchy.glb';
56
57
  // const path = 'data/models/sponza-pbr/gltf/sponza.glb';
@@ -58,86 +59,37 @@ async function main(engine) {
58
59
  // const path = 'moicon/isiflow_Oct_15_21/1/model.gltf';
59
60
  // const path = 'moicon/Kople/EVCharger1.gltf';
60
61
 
61
- const gltf_asset = await engine.assetManager.promise(path, GameAssetType.ModelGLTF_JSON);
62
-
63
- const node_hierarchy = three_object_to_entity_composition(gltf_asset.create());
62
+ const objects = await load_mesh_for_bake(engine, path);
64
63
 
65
-
66
- const objects = [];
67
-
68
- node_hierarchy.traverse(n => {
69
- const sg = n.entity.getComponent(ShadedGeometry);
70
- if (sg !== null) {
71
- objects.push({
72
- mesh: sg,
73
- transform: n.entity.getComponent(Transform).matrix
74
- })
75
- // objects.push({
76
- // mesh: ShadedGeometry.from(new BoxBufferGeometry(), sg.material),
77
- // transform: mat4.create()
78
- // })
79
- }
80
- });
81
-
82
- // objects.push(
83
- // {
84
- // mesh: ShadedGeometry.from(new BoxBufferGeometry(), new MeshStandardMaterial({
85
- // color: 0xFF0000
86
- // })),
87
- // transform: mat4.create()
88
- // }
89
- // );
90
-
91
- console.profile('bake');
64
+ // console.profile('bake');
92
65
  const id = baker.bake({
93
66
  objects,
94
- frames: 16,
95
- resolution: 2048,
96
- type: ImpostorCaptureType.FullSphere
67
+ frames: 8,
68
+ resolution: 1024,
69
+ type: ImpostorCaptureType.Hemisphere
97
70
  });
98
- console.profileEnd('bake');
71
+ // console.profileEnd('bake');
99
72
 
100
73
  console.log(id);
101
74
 
102
- const ctrl = buildCanvasViewFromTexture({
103
- texture: id.rt.texture[0],
104
- renderer: renderer,
105
- // swizzle: ['a', 'a', 'a', 1]
106
- // swizzle: ['r', 'g', 'b', 1]
107
- swizzle: ['r', 'g', 'b', 'a']
108
- });
109
- ctrl.view.css({
110
- opacity: 1
111
- });
112
- ctrl.render();
113
-
114
- const cv = new CanvasView();
115
- cv.size.set(id.atlas.width, id.atlas.height);
116
- cv.css({
117
- background: "rgba(0,0,0,0.1)"
75
+ const ctrl = makeImpostorAtlasPreview({
76
+ impostor: id,
77
+ renderer,
78
+ mode: 'alpha'
118
79
  });
119
80
 
120
- sampler2D2Canvas(id.atlas, 255, 0, cv.el);
121
-
122
- WebGLRendererPool.global.release(renderer);
123
-
124
- new EntityBuilder()
125
- .add(new ViewportPosition())
126
- .add(GUIElement.fromView(cv))
127
- // .build(engine.entityManager.dataset);
128
-
129
- const ecd = engine.entityManager.dataset;
130
81
  new EntityBuilder()
131
82
  .add(new ViewportPosition({
132
83
  offset: new Vector2(0, 0)
133
84
  }))
134
- .add(GUIElement.fromView(ctrl.view))
85
+ .add(GUIElement.fromView(ctrl))
135
86
  // .build(ecd);
136
87
 
88
+ // build out preview scene with impostor and the original
137
89
  const t0 = Transform.fromJSON({
138
90
  position: { x: 10, y: 0.5, z: 10 },
139
91
  scale: 3,
140
- rotation: Quaternion.fromEulerAngles(0, 90 * DEG2RAD, 0)
92
+ rotation: Quaternion.fromEulerAngles(0, 90 * DEG_TO_RAD, 0)
141
93
  });
142
94
 
143
95
  t0.scale.setScalar(3 / (id.sphere_radius * 2));
@@ -145,10 +97,12 @@ async function main(engine) {
145
97
  const entity_impostor = make_impostor_entity(id, t0);
146
98
  entity_impostor.build(ecd);
147
99
 
100
+ const entity_impostor_wireframe = make_impostor_wireframe(id, t0);
101
+ entity_impostor_wireframe.build(ecd);
102
+
148
103
  const t1 = new Transform();
149
104
  t1.copy(t0);
150
105
  t1.position._add(3, 0, 0);
151
- // t1.scale.setScalar(1.9);
152
106
 
153
107
  const sg_mesh = SGMesh.fromURL(path);
154
108
 
@@ -187,6 +141,43 @@ function make_spin(entity, speed = 1, axis = Vector3.up) {
187
141
  })));
188
142
  }
189
143
 
144
+ /**
145
+ *
146
+ * @param {{tBase,tGeometry,uFrames,uOffset,uRadius,uIsFullSphere}} uniforms
147
+ * @param {ImpostorDescription} id
148
+ */
149
+ function write_impostor_definition_to_material_uniforms(uniforms, id) {
150
+
151
+ uniforms.tBase.value = id.rt.texture[0];
152
+ uniforms.tGeometry.value = id.rt.texture[1];
153
+ uniforms.uFrames.value = id.frame_count;
154
+ uniforms.uOffset.value.set(id.offset[0], id.offset[1], id.offset[2]);
155
+ uniforms.uRadius.value = id.sphere_radius;
156
+ uniforms.uIsFullSphere.value = id.capture_type === ImpostorCaptureType.FullSphere;
157
+ }
158
+
159
+ function make_impostor_wireframe(id, t = new Transform()) {
160
+ const mat = new ImpostorShaderWireframeV0();
161
+
162
+ mat.wireframe = true;
163
+ write_impostor_definition_to_material_uniforms(mat.uniforms, id);
164
+
165
+ const transform = new Transform();
166
+
167
+ transform.copy(t);
168
+
169
+ const sg = ShadedGeometry.from(build_geometry_from_cutout_shape(id.cutout), mat);
170
+
171
+ sg.depth_material = mat;
172
+ sg.clearFlag(ShadedGeometryFlags.CastShadow);
173
+ sg.clearFlag(ShadedGeometryFlags.ReceiveShadow);
174
+
175
+ return new EntityBuilder()
176
+ .add(transform)
177
+ .add(sg)
178
+ ;
179
+ }
180
+
190
181
  /**
191
182
  *
192
183
  * @param {ImpostorDescription} id
@@ -197,18 +188,13 @@ function make_impostor_entity(id, t = new Transform()) {
197
188
 
198
189
  const mat = new ImpostorShaderV0();
199
190
 
200
- mat.uniforms.tBase.value = id.rt.texture[0];
201
- mat.uniforms.tGeometry.value = id.rt.texture[1];
202
- mat.uniforms.uFrames.value = id.frame_count;
203
- mat.uniforms.uOffset.value.set(id.offset[0], id.offset[1], id.offset[2]);
204
- mat.uniforms.uRadius.value = id.sphere_radius;
205
- mat.uniforms.uIsFullSphere.value = id.capture_type === ImpostorCaptureType.FullSphere;
191
+ write_impostor_definition_to_material_uniforms(mat.uniforms, id);
206
192
 
207
193
  const transform = new Transform();
208
194
 
209
195
  transform.copy(t);
210
196
 
211
- const sg = ShadedGeometry.from(new PlaneBufferGeometry(1, 1, 1), mat);
197
+ const sg = ShadedGeometry.from(build_geometry_from_cutout_shape(id.cutout), mat);
212
198
 
213
199
  sg.depth_material = mat;
214
200
  sg.clearFlag(ShadedGeometryFlags.CastShadow);
@@ -218,6 +204,7 @@ function make_impostor_entity(id, t = new Transform()) {
218
204
  .add(transform)
219
205
  .add(sg)
220
206
  ;
207
+
221
208
  }
222
209
 
223
210
  new EngineHarness().initialize({
@@ -0,0 +1,134 @@
1
+ import {
2
+ AddEquation,
3
+ CustomBlending,
4
+ GLSL3,
5
+ OneFactor,
6
+ OneMinusSrcAlphaFactor,
7
+ RawShaderMaterial,
8
+ Vector3
9
+ } from "three";
10
+
11
+ /*
12
+ *
13
+ * For ray projection using projection matrix : https://encreative.blogspot.com/2019/05/computing-ray-origin-and-direction-from.html
14
+ */
15
+ const shader_vx = `
16
+
17
+ in vec3 position;
18
+
19
+ uniform mat4 modelViewMatrix;
20
+ uniform mat4 projectionMatrix;
21
+ uniform mat3 normalMatrix;
22
+
23
+ uniform vec3 uOffset;
24
+ uniform float uRadius;
25
+ uniform float uFrames;
26
+
27
+ void main() {
28
+
29
+ vec2 framesMinusOne = uFrames - vec2(1.0);
30
+
31
+ mat4 m4 = modelViewMatrix;
32
+
33
+ m4[0][0] = 1.0;
34
+ m4[0][1] = 0.0;
35
+ m4[0][2] = 0.0;
36
+
37
+ m4[1][0] = 0.0;
38
+ m4[1][1] = 1.0;
39
+ m4[1][2] = 0.0;
40
+
41
+ m4[2][0] = 0.0;
42
+ m4[2][1] = 0.0;
43
+ m4[2][2] = 1.0;
44
+
45
+ vec3 object_scale = vec3(
46
+ length(modelViewMatrix[0].xyz),
47
+ length(modelViewMatrix[1].xyz),
48
+ length(modelViewMatrix[2].xyz)
49
+ );
50
+
51
+ // scale by object's baking bounding sphere's radius
52
+ float card_diameter = uRadius*2.0;
53
+ object_scale *= card_diameter;
54
+
55
+ vec4 mvPosition = m4 * vec4( object_scale*(position+uOffset/card_diameter), 1.0 );
56
+
57
+ gl_Position = projectionMatrix * mvPosition;
58
+
59
+ }
60
+ `;
61
+ const shader_fg = `
62
+ precision highp float;
63
+ precision highp int;
64
+
65
+ out vec4 color_out;
66
+
67
+ void main(){
68
+
69
+ color_out = vec4(1.0, .0, .0, 1.0);
70
+ }
71
+ `;
72
+
73
+ export class ImpostorShaderWireframeV0 extends RawShaderMaterial {
74
+ constructor() {
75
+ super({
76
+ fragmentShader: shader_fg,
77
+ vertexShader: shader_vx,
78
+ uniforms: {
79
+ /**
80
+ * RGB + Alpha
81
+ */
82
+ tBase: {
83
+ value: null
84
+ },
85
+ /**
86
+ * Normal+Depth
87
+ */
88
+ tGeometry: {
89
+ value: null
90
+ },
91
+ /**
92
+ * Material properties: Occlusion, Roughness, Metalness
93
+ * Alpha unused
94
+ */
95
+ tMaterial: {
96
+ value: null
97
+ },
98
+ /**
99
+ * Number of frames
100
+ */
101
+ uFrames: {
102
+ value: 0
103
+ },
104
+ /**
105
+ * Radius of bounding sphere of the impostor
106
+ */
107
+ uRadius: {
108
+ value: 0
109
+ },
110
+ /**
111
+ * Impostor offset
112
+ */
113
+ uOffset: {
114
+ value: new Vector3(0, 0, 0)
115
+ },
116
+ uIsFullSphere: {
117
+ value: false
118
+ },
119
+ uDepthScale: {
120
+ // value should be in range between 0 and 1
121
+ value: 1
122
+ }
123
+ },
124
+ glslVersion: GLSL3
125
+ });
126
+
127
+ // Save some effort by disabling blending
128
+ this.blending = CustomBlending;
129
+ this.blendEquation = AddEquation;
130
+ this.blendSrc = OneFactor;
131
+ this.blendDst = OneMinusSrcAlphaFactor;
132
+
133
+ }
134
+ }