@woosh/meep-engine 2.56.1 → 2.57.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 (227) hide show
  1. package/editor/ecs/component/editors/ColorEditor.js +1 -1
  2. package/editor/ecs/component/editors/ImagePathEditor.js +2 -2
  3. package/editor/ecs/component/editors/{LargeStrongEditor.js → LargeStringEditor.js} +1 -1
  4. package/editor/tools/GridPaintTool.js +3 -3
  5. package/editor/tools/TopDownCameraControlTool.js +1 -1
  6. package/editor/tools/TransformTool.js +1 -1
  7. package/editor/tools/engine/Tool.js +9 -23
  8. package/editor/tools/engine/ToolEngine.js +5 -7
  9. package/editor/tools/paint/TerrainHeightPaintTool.js +2 -2
  10. package/editor/tools/paint/TerrainPaintTool.js +2 -2
  11. package/editor/tools/paint/TerrainTexturePaintTool.js +2 -2
  12. package/package.json +1 -1
  13. package/src/core/bvh2/bvh3/query/compute_tight_near_far_clipping_planes.js +1 -1
  14. package/src/core/bvh2/traversal/ThreeClippingPlaneComputingBVHVisitor.js +1 -1
  15. package/src/core/bvh2/traversal/__process_point_if_within_planes.js +1 -1
  16. package/src/core/cache/LoadingCache.js +3 -2
  17. package/src/core/collection/array/arraySetSortingDiff.spec.js +12 -12
  18. package/src/core/collection/array/weightedRandomFromArray.js +2 -2
  19. package/src/core/color/Color.js +6 -6
  20. package/src/core/color/YCbCr/YCbCr.spec.js +16 -0
  21. package/src/core/color/{YCbCr_to_rgb_uint24.js → YCbCr/YCbCr_to_rgb_uint24.js} +6 -6
  22. package/src/core/color/YCbCr/rgb_to_YCbCr_uint24.js +16 -0
  23. package/src/core/color/{hex2rgb.js → hex/hex2rgb.js} +1 -1
  24. package/src/core/color/{rgb2hex.js → hex/rgb2hex.js} +1 -1
  25. package/src/core/color/{hsv2rgb.js → hsv/hsv2rgb.js} +3 -3
  26. package/src/core/color/hsv/rgb2hsv.js +49 -0
  27. package/src/core/color/{kelvin_to_rgb.js → kelvin/kelvin_to_rgb.js} +1 -1
  28. package/src/core/color/{kelvin_to_rgb.spec.js → kelvin/kelvin_to_rgb.spec.js} +2 -2
  29. package/src/core/color/{prototype_kelvin_to_rgb.js → kelvin/prototype_kelvin_to_rgb.js} +5 -5
  30. package/src/core/color/{rgb_to_kelvin.spec.js → kelvin/rgb_to_kelvin.spec.js} +1 -1
  31. package/src/core/color/{parseColor.js → parse_color.js} +4 -4
  32. package/src/core/color/parse_color.spec.js +18 -0
  33. package/src/core/color/rgb2uint24.js +3 -3
  34. package/src/core/color/sRGB/sRGB.spec.js +16 -0
  35. package/src/core/color/xyz/XYZ.spec.js +16 -0
  36. package/src/core/color/xyz/rgb_to_xyz.js +15 -0
  37. package/src/core/color/xyz/xyz_to_rgb.js +14 -0
  38. package/src/core/events/signal/Signal.js +5 -0
  39. package/src/core/events/signal/{signalAggregateByTimeWindow.js → signal_aggregate_by_time_window.js} +1 -1
  40. package/src/core/events/signal/{SignalUtils.js → signal_filter.js} +1 -2
  41. package/src/core/geom/2d/spline_bezier2_2d.js +26 -0
  42. package/src/core/geom/2d/spline_bezier2_2d.spec.js +16 -0
  43. package/src/core/geom/3d/SurfacePoint3.js +1 -1
  44. package/src/core/geom/3d/aabb/aabb3_compute_plane_side.js +1 -1
  45. package/src/core/geom/3d/aabb/aabb3_detailed_volume_intersection.js +1 -1
  46. package/src/core/geom/3d/aabb/aabb3_transformed_compute_plane_side.js +1 -1
  47. package/src/core/geom/3d/compute_circle_bounding_box.js +1 -1
  48. package/src/core/geom/3d/cone/computeConeBoundingBox.js +1 -1
  49. package/src/core/geom/3d/frustum/frustum3_computeNearestPointToPoint.js +1 -1
  50. package/src/core/geom/3d/line/line3_computeSegmentPointDistance_sqr.js +1 -1
  51. package/src/core/geom/3d/line/line3_compute_nearest_point_to_point.js +1 -1
  52. package/src/core/geom/3d/normal/hemioct/unit_hemioct.spec.js +1 -1
  53. package/src/core/geom/3d/normal/spherical/sphere_map_transform.js +1 -1
  54. package/src/core/geom/3d/plane/is_point_within_planes.js +1 -1
  55. package/src/core/geom/3d/plane/lerp_planes_to_array.js +1 -1
  56. package/src/core/geom/3d/plane/plane3_compute_convex_3_plane_intersection.js +1 -1
  57. package/src/core/geom/3d/plane/plane3_compute_line_segment_intersection.js +1 -1
  58. package/src/core/geom/3d/plane/plane3_compute_ray_intersection.js +1 -1
  59. package/src/core/geom/3d/plane/plane3_lerp.js +2 -2
  60. package/src/core/geom/3d/plane/plane3_projectPoint.js +1 -1
  61. package/src/core/geom/3d/plane/plane3_slerp.js +3 -3
  62. package/src/core/geom/3d/ray/ray_computeNearestPointToPoint.js +1 -1
  63. package/src/core/geom/3d/ray/ray_distance_to_point.js +1 -1
  64. package/src/core/geom/3d/shape/UnionShape3D.js +3 -3
  65. package/src/core/geom/3d/shape/UnitCubeShape3D.js +1 -1
  66. package/src/core/geom/3d/shape/UnitSphereShape3D.js +1 -1
  67. package/src/core/geom/3d/sphere/sphere_intersects_ray.js +1 -1
  68. package/src/core/geom/3d/tetrahedra/TetrahedralMesh.js +6 -0
  69. package/src/core/geom/3d/tetrahedra/compute_bounding_simplex_3d.js +1 -1
  70. package/src/core/geom/3d/tetrahedra/compute_bounding_simplex_3d.spec.js +1 -1
  71. package/src/core/geom/3d/tetrahedra/compute_circumsphere.js +1 -1
  72. package/src/core/geom/3d/tetrahedra/delaunay/compute_delaunay_tetrahedral_mesh.js +1 -0
  73. package/src/core/geom/3d/tetrahedra/delaunay/compute_delaunay_tetrahedral_mesh.spec.js +10 -2
  74. package/src/core/geom/3d/topology/bounds/computeTriangleClusterNormalBoundingCone.js +1 -1
  75. package/src/core/geom/3d/topology/samples/sampleFloodFill.js +1 -1
  76. package/src/core/geom/3d/topology/simplify/compute_face_normal_change_dot_product.js +1 -1
  77. package/src/core/geom/3d/topology/simplify/quadratic/Quadratic3.js +1 -1
  78. package/src/core/geom/3d/topology/simplify/tm_edge_collapse_is_degenerate_flip.js +2 -2
  79. package/src/core/geom/3d/topology/struct/TopoVertex.js +1 -1
  80. package/src/core/geom/3d/topology/util/compute_face_connection_weight.js +2 -2
  81. package/src/core/geom/3d/triangle/computeTrianglePlaneSide.js +1 -1
  82. package/src/core/geom/3d/triangle/computeTriangleRayIntersection.js +1 -1
  83. package/src/core/geom/3d/triangle/computeTriangleRayIntersectionBarycentricEdge.js +1 -1
  84. package/src/core/geom/3d/v3_compute_triangle_normal.js +1 -1
  85. package/src/core/geom/Quaternion.js +1 -1
  86. package/src/core/geom/Vector3.js +7 -7
  87. package/src/core/geom/Vector3.spec.js +68 -1
  88. package/src/core/geom/mat2/m2_add.js +17 -0
  89. package/src/core/geom/mat2/m2_determinant.js +8 -0
  90. package/src/core/geom/mat2/m2_multiply.js +17 -0
  91. package/src/core/geom/mat2/m2_multiply_scalar.js +18 -0
  92. package/src/core/geom/mat2/m2_multiply_transposed.js +18 -0
  93. package/src/core/geom/mat2/m2_sub_transposed.js +17 -0
  94. package/src/{view → core/geom/mat3}/m3_cm_compose_transform.js +0 -29
  95. package/src/core/geom/mat3/m3_cm_extract_rotation.js +8 -0
  96. package/src/core/geom/mat3/m3_cm_from_translation.js +19 -0
  97. package/src/{view/multiplyMatrices3.js → core/geom/mat3/m3_multiply.js} +2 -2
  98. package/src/{view → core/geom/mat3}/m3_rm_compose_transform.js +0 -9
  99. package/src/core/geom/mat3/m3_rm_extract_scale.js +7 -0
  100. package/src/core/geom/{3d/vector/allocate_v3.js → vec3/v3_allocate.js} +1 -1
  101. package/src/core/geom/{v3_angle_between.js → vec3/v3_angle_between.js} +1 -1
  102. package/src/core/geom/vec3/v3_dot_array_array.js +20 -0
  103. package/src/core/geom/{v3_lerp.js → vec3/v3_lerp.js} +1 -1
  104. package/src/core/graph/SquareMatrix.js +26 -3
  105. package/src/core/graph/SquareMatrix.spec.js +52 -2
  106. package/src/core/math/clamp01.js +1 -0
  107. package/src/core/math/fract.js +3 -1
  108. package/src/core/math/inverseLerp.js +7 -5
  109. package/src/core/math/lerp.js +5 -5
  110. package/src/core/math/physics/spring/computeHookeForce.js +9 -0
  111. package/src/core/math/remap.js +19 -0
  112. package/src/core/math/remap.spec.js +15 -0
  113. package/src/core/math/smoothStep.spec.js +16 -0
  114. package/src/core/math/spline/spline_bezier2.js +13 -0
  115. package/src/core/math/spline/spline_bezier2.spec.js +13 -0
  116. package/src/core/model/object/compareValues.js +2 -2
  117. package/src/core/model/object/read_property.js +10 -2
  118. package/src/core/model/object/write_property.js +3 -23
  119. package/src/core/model/object/write_property.spec.js +26 -0
  120. package/src/core/primitives/numbers/number_pretty_print.spec.js +19 -0
  121. package/src/engine/ecs/ik/OneBoneSurfaceAlignmentSolver.js +2 -2
  122. package/src/engine/ecs/ik/TwoBoneInverseKinematicsSolver.js +2 -2
  123. package/src/engine/ecs/systems/SteeringSystem.js +1 -1
  124. package/src/engine/ecs/terrain/ecs/splat/SplatMapOptimizerDebugger.js +1 -1
  125. package/src/engine/graphics/ecs/camera/Camera.js +1 -1
  126. package/src/engine/graphics/ecs/camera/CameraClippingPlaneComputer.js +1 -1
  127. package/src/engine/graphics/ecs/decal/v2/FPDecalSystem.js +3 -11
  128. package/src/engine/graphics/ecs/highlight/Highlight.js +20 -102
  129. package/src/engine/graphics/ecs/mesh/Mesh.js +12 -18
  130. package/src/engine/graphics/ecs/mesh/MeshSystem.js +41 -26
  131. package/src/engine/graphics/ecs/mesh-v2/ShadedGeometrySystem.js +1 -1
  132. package/src/engine/graphics/ecs/path/tube/build/TubePathBuilder.js +1 -1
  133. package/src/engine/graphics/ecs/path/tube/build/makeTubeGeometry.js +1 -1
  134. package/src/engine/graphics/ecs/path/tube/build/make_cap.js +3 -3
  135. package/src/engine/graphics/ecs/path/tube/build/make_ring_vertices.js +1 -1
  136. package/src/engine/graphics/geometry/MikkT/InitTriInfo.js +5 -6
  137. package/src/engine/graphics/geometry/MikkT/MikkTSpace.js +3 -3
  138. package/src/engine/graphics/geometry/MikkT/NormalizeSafe.js +1 -2
  139. package/src/engine/graphics/geometry/MikkT/STSpace.js +3 -3
  140. package/src/engine/graphics/geometry/MikkT/v3_scale_dot_sub_normalize.js +2 -2
  141. package/src/engine/graphics/geometry/bvh/buffered/BVHGeometryRaycaster.js +1 -1
  142. package/src/engine/graphics/geometry/clipping/ClippedGeometry.js +1 -1
  143. package/src/engine/graphics/particles/particular/engine/emitter/ParticleEmitter.js +102 -95
  144. package/src/engine/graphics/particles/particular/engine/emitter/ParticleLayer.spec.js +25 -0
  145. package/src/engine/graphics/particles/particular/engine/emitter/ParticlePool.js +0 -35
  146. package/src/engine/graphics/particles/particular/engine/emitter/ParticlePool.spec.js +39 -2
  147. package/src/engine/graphics/particles/particular/engine/shader/ShaderManager.js +22 -6
  148. package/src/engine/graphics/render/forward_plus/LightManager.js +1 -1
  149. package/src/engine/graphics/render/forward_plus/plugin/ptototypeFPPlugin.js +2 -2
  150. package/src/engine/graphics/render/forward_plus/prototype/prototypeLightManager.js +3 -3
  151. package/src/engine/graphics/render/forward_plus/query/detailed_sphere_frustum_intersection_test.js +1 -1
  152. package/src/engine/graphics/render/layers/RenderLayerUtils.js +1 -1
  153. package/src/engine/graphics/render/visibility/hiz/prototypeHiZ.js +3 -2
  154. package/src/engine/graphics/sh3/LightProbeVolume.js +1 -1
  155. package/src/engine/graphics/sh3/path_tracer/PathTracer.js +2 -2
  156. package/src/engine/graphics/sh3/path_tracer/prototypePathTracer.js +2 -2
  157. package/src/engine/graphics/sh3/path_tracer/random_in_hemisphere.js +1 -1
  158. package/src/engine/graphics/sh3/path_tracer/ray_hit_apply_transform.js +1 -1
  159. package/src/engine/graphics/sh3/path_tracer/ray_reflect.js +1 -1
  160. package/src/engine/graphics/sh3/sky/hosek/data.js +3744 -0
  161. package/src/engine/graphics/sh3/sky/hosek/sky_hosek_compute_irradiance_by_direction.js +10 -3786
  162. package/src/engine/graphics/texture/atlas/AbstractTextureAtlas.js +1 -1
  163. package/src/engine/graphics/texture/atlas/AtlasPatch.js +0 -1
  164. package/src/engine/graphics/texture/atlas/AtlasPatchFlag.js +8 -1
  165. package/src/engine/graphics/texture/atlas/CachingTextureAtlas.js +25 -0
  166. package/src/engine/graphics/texture/atlas/CachingTextureAtlas.spec.js +54 -0
  167. package/src/engine/graphics/texture/atlas/ManagedTextureAtlas.js +101 -160
  168. package/src/engine/graphics/texture/atlas/TextureAtlas.js +18 -1
  169. package/src/engine/graphics/texture/sampler/sampler2d_ensure_uint8_RGBA.js +22 -0
  170. package/src/engine/graphics/util/projectSphere.js +1 -1
  171. package/src/engine/navigation/ecs/components/Path.js +1 -1
  172. package/src/engine/physics/mls-mpm/MLS_MPM.js +11 -106
  173. package/src/engine/reference/v2/Reference.js +6 -2
  174. package/src/engine/sound/ecs/emitter/SoundEmitter.js +7 -7
  175. package/src/engine/sound/ecs/emitter/SoundTrack.js +6 -6
  176. package/src/generation/filtering/numeric/complex/CellFilterAngleToNormal.js +1 -1
  177. package/src/generation/grid/generation/road/GridTaskGenerateRoads.js +2 -4
  178. package/src/generation/placement/action/random/weighted/CellActionSelectWeightedRandom.js +2 -2
  179. package/src/generation/theme/ThemeEngine.js +2 -2
  180. package/src/generation/theme/ThemeEngine.spec.js +7 -0
  181. package/src/view/View.js +21 -13
  182. package/src/view/elements/ColorPickerView.js +2 -2
  183. package/src/view/minimap/gl/MarkerGL.js +20 -18
  184. package/src/view/minimap/gl/MinimapMarkersGL.js +2 -1
  185. package/editor/tools/FoliagePaintTool.js +0 -168
  186. package/editor/tools/engine/ToolStateMachine.js +0 -53
  187. package/src/core/NumberFormat.js +0 -4
  188. package/src/core/color/rgb2hsv.js +0 -47
  189. package/src/core/color/rgb_to_YCbCr_uint24.js +0 -16
  190. package/src/core/fsm/StateMachine.js +0 -440
  191. package/src/core/fsm/Transition.js +0 -65
  192. package/src/core/geom/Bezier.js +0 -24
  193. package/src/core/geom/Bezier.spec.js +0 -16
  194. package/src/core/math/spline/quadraticCurve.js +0 -11
  195. package/src/core/math/spline/quadraticCurve.spec.js +0 -13
  196. package/src/core/primitives/numbers/compareNumbers.js +0 -9
  197. package/src/engine/graphics/ecs/decal/Decal.js +0 -10
  198. package/src/engine/graphics/ecs/decal/DecalSystem.js +0 -99
  199. package/src/engine/graphics/ecs/decal/threejs/DecalGeometry.js +0 -298
  200. package/src/engine/graphics/ecs/trail/Trail.js +0 -83
  201. package/src/engine/graphics/ecs/trail/TrailMaterial.js +0 -67
  202. package/src/engine/graphics/ecs/trail/TrailMaterial2.js +0 -73
  203. package/src/engine/graphics/ecs/trail/TrailSystem.js +0 -162
  204. package/src/engine/graphics/ecs/water2/NodeWaterShader1.js +0 -213
  205. package/src/engine/graphics/ecs/water2/shader/JBWaterShader.js +0 -315
  206. package/src/engine/graphics/geometry/MikkT/Length.js +0 -10
  207. package/src/engine/graphics/texture/ImageLoader.js +0 -33
  208. package/src/engine/graphics/three/DDSLoader.js +0 -286
  209. package/src/engine/graphics/three/Water2.js +0 -343
  210. package/src/engine/physics/spring/computeHookeForce.js +0 -9
  211. package/src/engine/reference/v1/ReferenceManager.js +0 -124
  212. /package/src/core/color/{hex2rgb.spec.js → hex/hex2rgb.spec.js} +0 -0
  213. /package/src/core/color/{rgb2hex.spec.js → hex/rgb2hex.spec.js} +0 -0
  214. /package/src/core/color/{hsv2rgb.spec.js → hsv/hsv2rgb.spec.js} +0 -0
  215. /package/src/core/color/{rgb_to_kelvin.js → kelvin/rgb_to_kelvin.js} +0 -0
  216. /package/src/core/color/{linear_to_sRGB.js → sRGB/linear_to_sRGB.js} +0 -0
  217. /package/src/core/color/{sRGB_to_linear.js → sRGB/sRGB_to_linear.js} +0 -0
  218. /package/src/core/geom/{m3_determinant.js → mat3/m3_determinant.js} +0 -0
  219. /package/src/core/geom/{v3_distance.js → vec3/v3_distance.js} +0 -0
  220. /package/src/core/geom/{v3_distance_above_plane.js → vec3/v3_distance_above_plane.js} +0 -0
  221. /package/src/core/geom/{v3_distance_above_plane.spec.js → vec3/v3_distance_above_plane.spec.js} +0 -0
  222. /package/src/core/geom/{v3_distance_sqr.d.ts → vec3/v3_distance_sqr.d.ts} +0 -0
  223. /package/src/core/geom/{v3_distance_sqr.js → vec3/v3_distance_sqr.js} +0 -0
  224. /package/src/core/geom/{v3_dot.js → vec3/v3_dot.js} +0 -0
  225. /package/src/core/geom/{v3_length.js → vec3/v3_length.js} +0 -0
  226. /package/src/core/geom/{v3_length_sqr.js → vec3/v3_length_sqr.js} +0 -0
  227. /package/src/core/geom/{v3_slerp.js → vec3/v3_slerp.js} +0 -0
@@ -2,7 +2,7 @@ export class AbstractTextureAtlas {
2
2
  /**
3
3
  *
4
4
  * @param {Sampler2D} sampler
5
- * @param {number} padding
5
+ * @param {number} [padding]
6
6
  * @returns {AtlasPatch}
7
7
  */
8
8
  add(sampler, padding) {
@@ -47,7 +47,6 @@ export class AtlasPatch {
47
47
  * Used for packing inside TextureAtlas.
48
48
  * Do not modify manually
49
49
  * @readonly
50
- * @protected
51
50
  * @type {AABB2}
52
51
  */
53
52
  this.packing = new AABB2();
@@ -3,5 +3,12 @@
3
3
  */
4
4
  export const AtlasPatchFlag = {
5
5
  Packed: 1,
6
- Painted: 2
6
+ /**
7
+ * Set when patch is drawn into the atlas, cleared when it's not
8
+ */
9
+ Painted: 2,
10
+ /**
11
+ * Set when patch is added to an atlas and clear when it is removed
12
+ */
13
+ Attached: 4
7
14
  };
@@ -249,6 +249,24 @@ export class CachingTextureAtlas extends AbstractTextureAtlas {
249
249
  return -1;
250
250
  }
251
251
 
252
+ /**
253
+ *
254
+ * @param {AtlasPatch} patch
255
+ * @returns {number}
256
+ * @private
257
+ */
258
+ __find_cache_record_index_by_patch(patch) {
259
+ const records = this.__cached_patches;
260
+ const n = records.length;
261
+ for (let i = 0; i < n; i++) {
262
+ if (records[i].patch === patch) {
263
+ return i;
264
+ }
265
+ }
266
+
267
+ return -1;
268
+ }
269
+
252
270
  add(sampler, padding = 4) {
253
271
  const existing_patch_index = this.__find_cache_record_index(sampler);
254
272
 
@@ -289,6 +307,13 @@ export class CachingTextureAtlas extends AbstractTextureAtlas {
289
307
  return false;
290
308
  }
291
309
 
310
+ const index = this.__find_cache_record_index_by_patch(patch);
311
+
312
+ if (index !== -1) {
313
+ // already in the cache
314
+ return false;
315
+ }
316
+
292
317
  // push to cache, let eviction logic handle the rest
293
318
  const record = new PatchRecord();
294
319
 
@@ -22,3 +22,57 @@ test("add one", () => {
22
22
  expect(atlas.contains(patch)).toBe(true);
23
23
 
24
24
  });
25
+
26
+ test("remove one", () => {
27
+
28
+ const atlas = new TextureAtlas();
29
+ const cached = new CachingTextureAtlas({ atlas });
30
+
31
+ const s = Sampler2D.uint8(4, 1, 1);
32
+
33
+ const patch = cached.add(s, 0);
34
+
35
+ expect(cached.remove(patch)).toBe(true);
36
+
37
+ // should stay cached
38
+ expect(atlas.contains(patch)).toBe(true);
39
+
40
+ // cache should tell us that patch is already removed
41
+ expect(cached.remove(patch)).toBe(false);
42
+
43
+ });
44
+
45
+ test("add patch that was already in the cache", () => {
46
+
47
+ const atlas = new TextureAtlas();
48
+ const cached = new CachingTextureAtlas({ atlas });
49
+
50
+ const s = Sampler2D.uint8(4, 1, 1);
51
+
52
+ const patch_a = cached.add(s, 0);
53
+
54
+ cached.remove(patch_a);
55
+
56
+ const patch_b = cached.add(s, 0);
57
+
58
+ expect(patch_a).toBe(patch_b);
59
+ });
60
+
61
+ test("eviction", () => {
62
+
63
+ const atlas = new TextureAtlas(1);
64
+ const cached = new CachingTextureAtlas({ atlas });
65
+
66
+ const sampler_a = Sampler2D.uint8(4, 1, 1);
67
+
68
+ const patch_a = cached.add(sampler_a, 0);
69
+
70
+ cached.remove(patch_a);
71
+ cached.update();
72
+
73
+ const sampler_b = Sampler2D.uint8(4, 1, 1);
74
+
75
+ const patch_b = cached.add(sampler_b, 0);
76
+
77
+ expect(atlas.contains(patch_a)).toBe(false);
78
+ });
@@ -1,20 +1,56 @@
1
1
  import { TextureAtlas } from "./TextureAtlas.js";
2
- import { ReferenceManager } from "../../../reference/v1/ReferenceManager.js";
3
- import { Sampler2D } from "../sampler/Sampler2D.js";
4
2
  import { GameAssetType } from "../../../asset/GameAssetType.js";
5
- import { HashMap } from "../../../../core/collection/map/HashMap.js";
6
3
  import { strictEquals } from "../../../../core/function/Functions.js";
7
4
  import { computeStringHash } from "../../../../core/primitives/strings/computeStringHash.js";
8
- import { copy_Sampler2D_channel_data } from "../sampler/copy_Sampler2D_channel_data.js";
9
5
  import { CachingTextureAtlas } from "./CachingTextureAtlas.js";
6
+ import { Reference } from "../../../reference/v2/Reference.js";
7
+ import { LoadingCache } from "../../../../core/cache/LoadingCache.js";
8
+ import { Sampler2D } from "../sampler/Sampler2D.js";
9
+ import { sampler2d_ensure_uint8_RGBA } from "../sampler/sampler2d_ensure_uint8_RGBA.js";
10
10
 
11
11
  export class ManagedAtlas {
12
+
13
+ /**
14
+ *
15
+ * @type {AssetManager|null}
16
+ */
17
+ #assets = null;
18
+
19
+ /**
20
+ *
21
+ * @type {Map<string, Reference>}
22
+ */
23
+ #references = new Map();
24
+
25
+ /**
26
+ * Pixel padding
27
+ * @type {number}
28
+ */
29
+ #padding = 4;
30
+
31
+ /**
32
+ * @type {LoadingCache<string, AtlasPatch>}
33
+ */
34
+ #patch_loader;
35
+
36
+ /**
37
+ *
38
+ * @type {Map<string, AtlasPatch>}
39
+ */
40
+ #url_patch_lookup = new Map();
41
+ /**
42
+ *
43
+ * @type {Map<AtlasPatch,string[]>}
44
+ */
45
+ #patch_url_lookup = new Map();
46
+
12
47
  /**
13
48
  *
14
49
  * @param {AssetManager} assetManager
15
50
  * @constructor
16
51
  */
17
52
  constructor(assetManager) {
53
+ this.#assets = assetManager;
18
54
 
19
55
  /**
20
56
  * pre-allocate space on the atlas to avoid some initial re-sizing
@@ -22,16 +58,8 @@ export class ManagedAtlas {
22
58
  */
23
59
  const atlas = new TextureAtlas(64);
24
60
 
25
- /**
26
- *
27
- * @type {WeakMap<string, Sampler2D>}
28
- * @private
29
- */
30
- this.__cached_samplers = new Map();
31
- const cached_samplers = this.__cached_samplers;
32
61
 
33
- const cached_atlas = new CachingTextureAtlas({ atlas });
34
- this.__caching_atlas = cached_atlas;
62
+ this.__caching_atlas = new CachingTextureAtlas({ atlas });
35
63
 
36
64
  /**
37
65
  *
@@ -39,177 +67,90 @@ export class ManagedAtlas {
39
67
  */
40
68
  this.atlas = atlas;
41
69
 
42
- /**
43
- *
44
- * @type {HashMap<String, AtlasPatch>}
45
- */
46
- const activePatches = new HashMap({
47
- keyHashFunction: computeStringHash,
48
- keyEqualityFunction: strictEquals
49
- });
50
-
51
- /**
52
- * @private
53
- * @type {HashMap<String, AtlasPatch>}
54
- */
55
- this.patchesActive = activePatches;
56
-
57
- /**
58
- * Automatically update TextureAtlas
59
- * @type {boolean}
60
- */
61
- this.autoUpdate = true;
62
-
63
- this.rebuildDelay = 200; //in milliseconds
70
+ atlas.on.removed.add(this.#remove_from_lookup, this);
64
71
 
65
- let counterLoadingAssets = 0;
66
- let lastRebuildTime = 0;
67
-
68
- const self = this;
72
+ this.#patch_loader = new LoadingCache({
73
+ keyHashFunction: computeStringHash,
74
+ keyEqualityFunction: strictEquals,
75
+ load: async (url) => {
76
+ const sampler = await this.#resolve_texture(url);
69
77
 
70
- function tryRebuild() {
71
- const timeNow = Date.now();
72
- const timeElapsed = timeNow - lastRebuildTime;
78
+ const patch = this.__caching_atlas.add(sampler, this.#padding);
73
79
 
74
- if (counterLoadingAssets === 0 || timeElapsed > self.rebuildDelay) {
75
- atlas.update();
80
+ this.#add_to_lookup(url, patch);
76
81
 
77
- lastRebuildTime = timeNow;
82
+ return patch;
78
83
  }
79
- }
80
-
81
- /**
82
- *
83
- * @param {string} url
84
- * @returns {Promise<AtlasPatch>}
85
- */
86
- function creator(url) {
87
- return new Promise(function (resolve, reject) {
88
-
89
- let patch = activePatches.get(url);
90
-
91
- if (patch !== undefined) {
92
- resolve(patch);
93
- return;
94
- }
95
-
96
- const cached_sampler = cached_samplers.get(url);
97
-
98
- if (cached_sampler !== undefined) {
99
- patch = cached_atlas.add(cached_sampler);
100
- resolve(patch);
101
- return;
102
- }
103
-
104
- /**
105
- *
106
- * @param {Asset} asset
107
- */
108
- function onLoad(asset) {
109
-
110
- //since load happens asynchronously, we need to make sure that the patch is still required
111
- const refCount = referenceManager.getCount(url);
112
-
113
- if (refCount === 0) {
114
- //no longer part of active reference set, ignore
115
- //this happens when a patch is requested and removed before it is loaded
116
- reject(`'${url}' is no longer part of active reference set`);
117
- return;
118
- }
119
-
120
- const image = asset.create();
121
-
122
- const source = new Sampler2D(image.data, image.itemSize, image.width, image.height);
123
- //assert.equal(atlas.patches.filter(p => p.sampler.data === image.data).length, 0, "Atlas already contains this asset data");
124
-
125
- //build sampler
126
- let sampler;
127
-
128
- if (source.itemSize !== 4) {
129
- // source image is not in RGBA layout, resample
130
- sampler = Sampler2D.uint8(4, image.width, image.height);
131
- copy_Sampler2D_channel_data(source, sampler)
132
- } else {
133
- sampler = source;
134
- }
135
-
136
- cached_samplers.set(url, sampler);
137
-
138
-
139
- const patch = cached_atlas.add(sampler);
140
-
141
- //record active patch
142
- activePatches.set(url, patch);
143
-
144
- counterLoadingAssets--;
145
-
146
- if (self.autoUpdate) {
147
- tryRebuild();
148
- }
149
-
150
- // console.warn(`ManagedAtlas: Loaded patch. size: ${image.data.length}, width: ${image.width}, height: ${image.height}, url: ${url}`, atlas);
151
-
152
- resolve(patch);
153
- }
154
-
155
- function onFailure(error) {
156
- counterLoadingAssets--;
157
-
158
- reject(error);
159
- }
160
-
84
+ });
85
+ }
161
86
 
162
- counterLoadingAssets++;
87
+ /**
88
+ *
89
+ * @param {AtlasPatch} patch
90
+ */
91
+ #remove_from_lookup(patch) {
92
+ const urls = this.#patch_url_lookup.get(patch);
163
93
 
164
- assetManager.get({ path: url, type: GameAssetType.Image, callback: onLoad, failure: onFailure });
94
+ for (let i = 0; i < urls.length; i++) {
95
+ const url = urls[i];
165
96
 
166
- });
97
+ this.#url_patch_lookup.delete(url);
167
98
  }
99
+ }
168
100
 
169
- /**
170
- *
171
- * @param {String} url
172
- * @param {Promise<AtlasPatch>} promise
173
- */
174
- function destroyer(url, promise) {
175
- promise.then(function (patch) {
176
- //it is possible that promise is resolved after patch has already been removed from the Atlas, so we first check
101
+ /**
102
+ *
103
+ * @param {string} url
104
+ * @param {AtlasPatch} patch
105
+ */
106
+ #add_to_lookup(url, patch) {
177
107
 
178
- if (activePatches.has(url)) {
179
- //remove from active
180
- activePatches.delete(url);
108
+ this.#url_patch_lookup.set(url, patch);
181
109
 
182
- self.__caching_atlas.remove(patch);
183
- }
184
- });
110
+ let urls = this.#patch_url_lookup.get(patch);
111
+ if (urls === undefined) {
112
+ urls = [];
113
+ this.#patch_url_lookup.set(patch, urls);
185
114
  }
115
+ urls.push(url);
116
+ }
186
117
 
187
- /**
188
- *
189
- * @type {ReferenceManager.<String,Promise.<AtlasPatch>>}
190
- */
191
- const referenceManager = new ReferenceManager(creator, destroyer);
118
+ /**
119
+ *
120
+ * @param {string} url
121
+ * @returns {Promise<Sampler2D>}
122
+ */
123
+ async #resolve_texture(url) {
124
+ const asset = await this.#assets.promise(url, GameAssetType.Image);
125
+ const source = asset.create();
192
126
 
193
- /**
194
- *
195
- * @type {ReferenceManager<String, Promise<AtlasPatch>>}
196
- */
197
- this.references = referenceManager;
127
+ return sampler2d_ensure_uint8_RGBA(source);
198
128
  }
199
129
 
200
130
  reset() {
201
- this.references.reset();
131
+ this.#references.clear();
202
132
  this.__caching_atlas.reset();
203
- this.patchesActive.clear();
133
+ this.#url_patch_lookup.clear();
204
134
  }
205
135
 
206
136
  /**
207
137
  *
208
138
  * @param {string} key
209
- * @returns {Promise<AtlasPatch>}
139
+ * @returns {Promise<Reference<AtlasPatch>>}
210
140
  */
211
- acquire(key) {
212
- return this.references.acquire(key);
141
+ async acquire(url) {
142
+ const reference = new Reference();
143
+
144
+ let patch = this.#url_patch_lookup.get(url);
145
+
146
+ if (patch === undefined) {
147
+ // patch is not active, need to load it
148
+ patch = await this.#patch_loader.get(url);
149
+ }
150
+
151
+ reference.bind(patch);
152
+
153
+ return reference;
213
154
  }
214
155
 
215
156
  /**
@@ -217,6 +158,6 @@ export class ManagedAtlas {
217
158
  * @param {string} key
218
159
  */
219
160
  release(key) {
220
- this.references.release(key);
161
+ throw new Error('Method is deprecated, use "Reference.release" instead');
221
162
  }
222
163
  }
@@ -66,7 +66,11 @@ export class TextureAtlas extends AbstractTextureAtlas {
66
66
  this.packer = new MaxRectanglesPacker(size, size);
67
67
 
68
68
  this.on = {
69
- painted: new Signal()
69
+ painted: new Signal(),
70
+ /**
71
+ * @type {Signal<AtlasPatch, Sampler2D>}
72
+ */
73
+ removed: new Signal()
70
74
  };
71
75
 
72
76
  /**
@@ -264,6 +268,7 @@ export class TextureAtlas extends AbstractTextureAtlas {
264
268
 
265
269
  assert.notNull(sampler, 'sampler');
266
270
  assert.defined(sampler, 'sampler');
271
+ assert.equal(sampler.isSampler2D, true, 'sampler.isSampler2D !== true');
267
272
 
268
273
  assert.isNonNegativeInteger(padding, 'padding');
269
274
 
@@ -276,6 +281,9 @@ export class TextureAtlas extends AbstractTextureAtlas {
276
281
  patch.size.set(sampler.width, sampler.height);
277
282
  patch.padding = padding;
278
283
 
284
+ // mark patch as belonging to an atlas
285
+ patch.setFlag(AtlasPatchFlag.Attached);
286
+
279
287
  const padding2 = padding * 2;
280
288
 
281
289
  patch.packing.set(0, 0, sampler.width + padding2, sampler.height + padding2);
@@ -324,6 +332,15 @@ export class TextureAtlas extends AbstractTextureAtlas {
324
332
  this.packer.remove(patch.packing);
325
333
  }
326
334
 
335
+ // clear flags
336
+ patch.clearFlag(
337
+ AtlasPatchFlag.Attached
338
+ | AtlasPatchFlag.Packed
339
+ | AtlasPatchFlag.Painted
340
+ );
341
+
342
+ this.on.removed.send2(patch, patch.sampler);
343
+
327
344
  return true;
328
345
  }
329
346
 
@@ -0,0 +1,22 @@
1
+ import { Sampler2D } from "./Sampler2D.js";
2
+ import { sampler2d_to_uint8_RGBA } from "./sampler2d_to_uint8_RGBA.js";
3
+
4
+ /**
5
+ *
6
+ * @param {Sampler2D} input
7
+ * @returns {Sampler2D}
8
+ */
9
+ export function sampler2d_ensure_uint8_RGBA(input) {
10
+ if (
11
+ input.itemSize === 4
12
+ && (input.data.constructor === Uint8Array || input.data.constructor === Uint8ClampedArray)
13
+ ) {
14
+ return input;
15
+ }
16
+
17
+ const resampled = Sampler2D.uint8(4, input.width, input.height);
18
+
19
+ sampler2d_to_uint8_RGBA(resampled, input);
20
+
21
+ return resampled;
22
+ }
@@ -1,6 +1,6 @@
1
1
  import { assert } from "../../../core/assert.js";
2
2
  import { v4_applyMatrix4 } from "../../../core/geom/Vector4.js";
3
- import { v3_dot } from "../../../core/geom/v3_dot.js";
3
+ import { v3_dot } from "../../../core/geom/vec3/v3_dot.js";
4
4
 
5
5
  const v4 = [];
6
6
 
@@ -14,7 +14,7 @@ import { RowFirstTable } from "../../../../core/collection/table/RowFirstTable.j
14
14
  import { min2 } from "../../../../core/math/min2.js";
15
15
  import { computeNonuniformCatmullRomSplineSample } from "./computeNonuniformCatmullRomSplineSample.js";
16
16
  import { InterpolationType } from "./InterpolationType.js";
17
- import { v3_distance } from "../../../../core/geom/v3_distance.js";
17
+ import { v3_distance } from "../../../../core/geom/vec3/v3_distance.js";
18
18
  import {
19
19
  line3_computeSegmentPointDistance_sqr
20
20
  } from "../../../../core/geom/3d/line/line3_computeSegmentPointDistance_sqr.js";
@@ -1,105 +1,10 @@
1
1
  import { clamp } from "../../../core/math/clamp.js";
2
-
3
- /**
4
- *
5
- * @param {number[]} a 2d matrix
6
- * @returns {number}
7
- */
8
- function determinant(a) {
9
- return a[0] * a[3] - a[1] * a[2]
10
- }
11
-
12
- /**
13
- *
14
- * @param {number[]} r
15
- * @param {number[]} a
16
- * @param {number[]} b
17
- */
18
- function m2_multiply(r, a, b) {
19
- const v0 = a[0] * b[0] + a[1] * b[2];
20
- const v1 = a[0] * b[1] + a[1] * b[3];
21
- const v2 = a[2] * b[0] + a[3] * b[2];
22
- const v3 = a[2] * b[1] + a[3] * b[3];
23
-
24
- r[0] = v0;
25
- r[1] = v1;
26
- r[2] = v2;
27
- r[3] = v3;
28
- }
29
-
30
- /**
31
- * Second argument will be transposed before multiplication
32
- * @param {number[]} r
33
- * @param {number[]} a
34
- * @param {number[]} b
35
- */
36
- function m2_multiply_t(r, a, b) {
37
- const v0 = a[0] * b[0] + a[1] * b[1];
38
- const v1 = a[0] * b[2] + a[1] * b[3];
39
- const v2 = a[2] * b[0] + a[3] * b[1];
40
- const v3 = a[2] * b[2] + a[3] * b[3];
41
-
42
- r[0] = v0;
43
- r[1] = v1;
44
- r[2] = v2;
45
- r[3] = v3;
46
- }
47
-
48
- /**
49
- *
50
- * @param {number[]} r
51
- * @param {number[]} a
52
- * @param {number} s
53
- */
54
- function m2_multiply_scalar(r, a, s) {
55
-
56
- const v0 = a[0] * s;
57
- const v1 = a[1] * s;
58
- const v2 = a[2] * s;
59
- const v3 = a[3] * s;
60
-
61
- r[0] = v0;
62
- r[1] = v1;
63
- r[2] = v2;
64
- r[3] = v3;
65
- }
66
-
67
- /**
68
- *
69
- * @param {number[]} r
70
- * @param {number[]} a
71
- * @param {number[]} b
72
- */
73
- function m2_add(r, a, b) {
74
- const v0 = a[0] + b[0];
75
- const v1 = a[1] + b[1];
76
- const v2 = a[2] + b[2];
77
- const v3 = a[3] + b[3];
78
-
79
- r[0] = v0;
80
- r[1] = v1;
81
- r[2] = v2;
82
- r[3] = v3;
83
- }
84
-
85
-
86
- /**
87
- * Trasposes second matrix before subtraction
88
- * @param {number[]} r
89
- * @param {number[]} a
90
- * @param {number[]} b
91
- */
92
- function m2_sub_ta(r, a, b) {
93
- const v0 = a[0] - b[0];
94
- const v1 = a[2] - b[1];
95
- const v2 = a[1] - b[2];
96
- const v3 = a[3] - b[3];
97
-
98
- r[0] = v0;
99
- r[1] = v1;
100
- r[2] = v2;
101
- r[3] = v3;
102
- }
2
+ import { m2_multiply } from "../../../core/geom/mat2/m2_multiply.js";
3
+ import { m2_multiply_transposed } from "../../../core/geom/mat2/m2_multiply_transposed.js";
4
+ import { m2_determinant } from "../../../core/geom/mat2/m2_determinant.js";
5
+ import { m2_multiply_scalar } from "../../../core/geom/mat2/m2_multiply_scalar.js";
6
+ import { m2_add } from "../../../core/geom/mat2/m2_add.js";
7
+ import { m2_sub_transposed } from "../../../core/geom/mat2/m2_sub_transposed.js";
103
8
 
104
9
 
105
10
  /**
@@ -354,14 +259,14 @@ export class MLS_MPMSolver {
354
259
  // Cauchy stress times dt and inv_dx
355
260
  // original taichi: stress = -4*inv_dx*inv_dx*dt*vol*( 2*mu*(p.F-r)*transposed(p.F) + lambda*(J-1)*J )
356
261
  // (in taichi matrices are coded transposed)
357
- const J = determinant(p.F); // Current volume
262
+ const J = m2_determinant(p.F); // Current volume
358
263
  polar_decomp_noS(r, p.F); // Polar decomp. for fixed corotated model
359
264
  const k1 = -4 * inv_dx * inv_dx * dt * vol;
360
265
  const k2 = lambda * (J - 1) * J;
361
266
 
362
267
 
363
268
  //compute stress
364
- m2_sub_ta(m2_temp0, p.F, r);
269
+ m2_sub_transposed(m2_temp0, p.F, r);
365
270
 
366
271
  m2_multiply(m2_temp0, m2_temp0, p.F);
367
272
 
@@ -571,11 +476,11 @@ export class MLS_MPMSolver {
571
476
  sig[i + 2 * i] = clamp(sig[i + 2 * i], 1.0 - 2.5e-2, 1.0 + 7.5e-3);
572
477
  }
573
478
 
574
- const oldJ = determinant(F);
479
+ const oldJ = m2_determinant(F);
575
480
  // original taichi: F = svd_u * sig * transposed(svd_v)
576
481
  m2_multiply(m2_temp0, svd_u, sig);
577
- m2_multiply_t(p.F, m2_temp0, svd_v);
578
- const Jp_new = clamp(p.Jp * oldJ / determinant(p.F), 0.6, 20.0);
482
+ m2_multiply_transposed(p.F, m2_temp0, svd_v);
483
+ const Jp_new = clamp(p.Jp * oldJ / m2_determinant(p.F), 0.6, 20.0);
579
484
  p.Jp = Jp_new;
580
485
  }
581
486
  }