@woosh/meep-engine 3.78.0 → 3.78.2

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 (213) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +1 -1
  3. package/package.json +1 -1
  4. package/src/engine/graphics3/GraphicsEngine.d.ts +19 -1
  5. package/src/engine/graphics3/GraphicsEngine.d.ts.map +1 -1
  6. package/src/engine/graphics3/GraphicsEngine.js +24 -2
  7. package/src/engine/physics/cloth/solver/cloth_step.d.ts.map +1 -1
  8. package/src/engine/physics/cloth/solver/cloth_step.js +4 -1
  9. package/src/engine/physics/cloth/solver/constraint/cloth_contact_row.d.ts +36 -2
  10. package/src/engine/physics/cloth/solver/constraint/cloth_contact_row.d.ts.map +1 -1
  11. package/src/engine/physics/cloth/solver/constraint/cloth_contact_row.js +60 -3
  12. package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.d.ts +119 -0
  13. package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.d.ts.map +1 -0
  14. package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.js +563 -0
  15. package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.d.ts +11 -0
  16. package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.d.ts.map +1 -0
  17. package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.js +10 -0
  18. package/src/generation/wfc/GridWFCCompiledTileSet.d.ts +123 -0
  19. package/src/generation/wfc/GridWFCCompiledTileSet.d.ts.map +1 -0
  20. package/src/generation/wfc/GridWFCCompiledTileSet.js +355 -0
  21. package/src/generation/wfc/GridWFCEdge.d.ts +13 -0
  22. package/src/generation/wfc/GridWFCEdge.d.ts.map +1 -0
  23. package/src/generation/wfc/GridWFCEdge.js +14 -0
  24. package/src/generation/wfc/GridWFCTileSet.d.ts +120 -0
  25. package/src/generation/wfc/GridWFCTileSet.d.ts.map +1 -0
  26. package/src/generation/wfc/GridWFCTileSet.js +275 -0
  27. package/src/shade/device/pipeline/ComputePipelineManager.d.ts +8 -0
  28. package/src/shade/device/pipeline/ComputePipelineManager.d.ts.map +1 -1
  29. package/src/shade/device/pipeline/ComputePipelineManager.js +18 -0
  30. package/src/shade/device/pipeline/RenderPipelineManager.d.ts +8 -0
  31. package/src/shade/device/pipeline/RenderPipelineManager.d.ts.map +1 -1
  32. package/src/shade/device/pipeline/RenderPipelineManager.js +17 -0
  33. package/src/shade/playground/avboit_footprint/index.html +20 -0
  34. package/src/shade/playground/avboit_footprint/main.d.ts +2 -0
  35. package/src/shade/playground/avboit_footprint/main.d.ts.map +1 -0
  36. package/src/shade/playground/avboit_footprint/main.js +428 -0
  37. package/src/shade/playground/probe_clipmap/main.d.ts.map +1 -1
  38. package/src/shade/playground/probe_clipmap/main.js +7 -3
  39. package/src/shade/playground/screen_probes/main.d.ts.map +1 -1
  40. package/src/shade/playground/screen_probes/main.js +7 -3
  41. package/src/shade/renderer/Renderer.d.ts +18 -8
  42. package/src/shade/renderer/Renderer.d.ts.map +1 -1
  43. package/src/shade/renderer/Renderer.js +201 -16
  44. package/src/shade/renderer/RendererConfiguration.d.ts +95 -0
  45. package/src/shade/renderer/RendererConfiguration.d.ts.map +1 -0
  46. package/src/shade/renderer/RendererConfiguration.js +119 -0
  47. package/src/shade/renderer/geometry/sdf/GPUGeometrySDFManager.d.ts +8 -0
  48. package/src/shade/renderer/geometry/sdf/GPUGeometrySDFManager.d.ts.map +1 -1
  49. package/src/shade/renderer/geometry/sdf/GPUGeometrySDFManager.js +34 -0
  50. package/src/shade/renderer/geometry/sdf/surface/GPUMeshSurfaceCaches.d.ts +7 -0
  51. package/src/shade/renderer/geometry/sdf/surface/GPUMeshSurfaceCaches.d.ts.map +1 -1
  52. package/src/shade/renderer/geometry/sdf/surface/GPUMeshSurfaceCaches.js +15 -0
  53. package/src/shade/renderer/global_illumination/probe_clipmap/GPUProbeClipmapGI.d.ts +10 -2
  54. package/src/shade/renderer/global_illumination/probe_clipmap/GPUProbeClipmapGI.d.ts.map +1 -1
  55. package/src/shade/renderer/global_illumination/probe_clipmap/GPUProbeClipmapGI.js +37 -3
  56. package/src/shade/renderer/global_illumination/probe_clipmap/ProbeClipmapSettings.d.ts +6 -0
  57. package/src/shade/renderer/global_illumination/probe_clipmap/ProbeClipmapSettings.d.ts.map +1 -1
  58. package/src/shade/renderer/global_illumination/probe_clipmap/ProbeClipmapSettings.js +31 -0
  59. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUBrickClipmap.d.ts +7 -0
  60. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUBrickClipmap.d.ts.map +1 -1
  61. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUBrickClipmap.js +20 -0
  62. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts +8 -0
  63. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.d.ts.map +1 -1
  64. package/src/shade/renderer/global_illumination/probe_clipmap/brick/GPUSceneBrickClipmap.js +14 -0
  65. package/src/shade/renderer/global_illumination/probe_clipmap/brick/mesh/MeshSDFBrickProvider.d.ts +7 -0
  66. package/src/shade/renderer/global_illumination/probe_clipmap/brick/mesh/MeshSDFBrickProvider.d.ts.map +1 -1
  67. package/src/shade/renderer/global_illumination/probe_clipmap/brick/mesh/MeshSDFBrickProvider.js +13 -0
  68. package/src/shade/renderer/global_illumination/probe_clipmap/material/GPUBrickMaterial.d.ts +6 -0
  69. package/src/shade/renderer/global_illumination/probe_clipmap/material/GPUBrickMaterial.d.ts.map +1 -1
  70. package/src/shade/renderer/global_illumination/probe_clipmap/material/GPUBrickMaterial.js +9 -0
  71. package/src/shade/renderer/global_illumination/probe_clipmap/probe/GPUProbeClipmap.d.ts +11 -0
  72. package/src/shade/renderer/global_illumination/probe_clipmap/probe/GPUProbeClipmap.d.ts.map +1 -1
  73. package/src/shade/renderer/global_illumination/probe_clipmap/probe/GPUProbeClipmap.js +71 -1
  74. package/src/shade/renderer/global_illumination/probe_clipmap/probe/ProbeClipmapHitWants.d.ts +6 -0
  75. package/src/shade/renderer/global_illumination/probe_clipmap/probe/ProbeClipmapHitWants.d.ts.map +1 -1
  76. package/src/shade/renderer/global_illumination/probe_clipmap/probe/ProbeClipmapHitWants.js +9 -0
  77. package/src/shade/renderer/global_illumination/probe_clipmap/probe/ProbeClipmapScreenWants.d.ts +6 -0
  78. package/src/shade/renderer/global_illumination/probe_clipmap/probe/ProbeClipmapScreenWants.d.ts.map +1 -1
  79. package/src/shade/renderer/global_illumination/probe_clipmap/probe/ProbeClipmapScreenWants.js +9 -0
  80. package/src/shade/renderer/global_illumination/probe_clipmap/probe/graph_probe_clipmap_want_meshlets.d.ts +6 -0
  81. package/src/shade/renderer/global_illumination/probe_clipmap/probe/graph_probe_clipmap_want_meshlets.d.ts.map +1 -1
  82. package/src/shade/renderer/global_illumination/probe_clipmap/probe/graph_probe_clipmap_want_meshlets.js +13 -0
  83. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.d.ts +9 -0
  84. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.d.ts.map +1 -1
  85. package/src/shade/renderer/global_illumination/screen_probe/GPUScreenProbeGI.js +47 -0
  86. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.d.ts +6 -0
  87. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.d.ts.map +1 -1
  88. package/src/shade/renderer/global_illumination/screen_probe/ScreenProbeSettings.js +17 -0
  89. package/src/shade/renderer/hair/GPUHairSystem.d.ts.map +1 -1
  90. package/src/shade/renderer/hair/GPUHairSystem.js +28 -9
  91. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts +50 -9
  92. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts.map +1 -1
  93. package/src/shade/renderer/hair/HAIR_SIMULATION.js +71 -11
  94. package/src/shade/renderer/hair/shader_hair_simulate.d.ts +22 -16
  95. package/src/shade/renderer/hair/shader_hair_simulate.d.ts.map +1 -1
  96. package/src/shade/renderer/hair/shader_hair_simulate.js +151 -78
  97. package/src/shade/renderer/particles/GPUParticleSystem.d.ts +8 -0
  98. package/src/shade/renderer/particles/GPUParticleSystem.d.ts.map +1 -1
  99. package/src/shade/renderer/particles/GPUParticleSystem.js +20 -1
  100. package/src/shade/renderer/particles/bounds/graph_particle_bounds.d.ts +6 -0
  101. package/src/shade/renderer/particles/bounds/graph_particle_bounds.d.ts.map +1 -1
  102. package/src/shade/renderer/particles/bounds/graph_particle_bounds.js +11 -1
  103. package/src/shade/renderer/particles/coherence/graph_particle_bucket.d.ts +6 -0
  104. package/src/shade/renderer/particles/coherence/graph_particle_bucket.d.ts.map +1 -1
  105. package/src/shade/renderer/particles/coherence/graph_particle_bucket.js +15 -0
  106. package/src/shade/renderer/particles/graph_particles.d.ts +8 -0
  107. package/src/shade/renderer/particles/graph_particles.d.ts.map +1 -1
  108. package/src/shade/renderer/particles/graph_particles.js +28 -3
  109. package/src/shade/renderer/particles/graph_particles_avboit.d.ts +7 -0
  110. package/src/shade/renderer/particles/graph_particles_avboit.d.ts.map +1 -1
  111. package/src/shade/renderer/particles/graph_particles_avboit.js +14 -0
  112. package/src/shade/renderer/particles/shaders/shader_particle_avboit_draw.d.ts.map +1 -1
  113. package/src/shade/renderer/particles/shaders/shader_particle_avboit_draw.js +5 -7
  114. package/src/shade/renderer/particles/warmup/graph_particle_warmup.d.ts +7 -0
  115. package/src/shade/renderer/particles/warmup/graph_particle_warmup.d.ts.map +1 -1
  116. package/src/shade/renderer/particles/warmup/graph_particle_warmup.js +15 -0
  117. package/src/shade/renderer/path_tracer/accumulating/AccumulatingPathTracer.d.ts +6 -0
  118. package/src/shade/renderer/path_tracer/accumulating/AccumulatingPathTracer.d.ts.map +1 -1
  119. package/src/shade/renderer/path_tracer/accumulating/AccumulatingPathTracer.js +16 -2
  120. package/src/shade/renderer/path_tracer/accumulating/graph_float32rgba_to_float16rgba.d.ts +6 -0
  121. package/src/shade/renderer/path_tracer/accumulating/graph_float32rgba_to_float16rgba.d.ts.map +1 -1
  122. package/src/shade/renderer/path_tracer/accumulating/graph_float32rgba_to_float16rgba.js +9 -0
  123. package/src/shade/renderer/path_tracer/accumulating/graph_flood_fill.d.ts +11 -0
  124. package/src/shade/renderer/path_tracer/accumulating/graph_flood_fill.d.ts.map +1 -1
  125. package/src/shade/renderer/path_tracer/accumulating/graph_flood_fill.js +10 -0
  126. package/src/shade/renderer/postprocess/dof/gather/DepthOfFieldU.d.ts +6 -0
  127. package/src/shade/renderer/postprocess/dof/gather/DepthOfFieldU.d.ts.map +1 -1
  128. package/src/shade/renderer/postprocess/dof/gather/DepthOfFieldU.js +10 -1
  129. package/src/shade/renderer/postprocess/dof/gather/graph_postprocess_dof2.d.ts +7 -0
  130. package/src/shade/renderer/postprocess/dof/gather/graph_postprocess_dof2.d.ts.map +1 -1
  131. package/src/shade/renderer/postprocess/dof/gather/graph_postprocess_dof2.js +21 -0
  132. package/src/shade/renderer/postprocess/dof/raymarch/DepthOfField.d.ts +7 -0
  133. package/src/shade/renderer/postprocess/dof/raymarch/DepthOfField.d.ts.map +1 -1
  134. package/src/shade/renderer/postprocess/dof/raymarch/DepthOfField.js +11 -1
  135. package/src/shade/renderer/postprocess/dof/raymarch/graph_postprocess_dof.d.ts +9 -0
  136. package/src/shade/renderer/postprocess/dof/raymarch/graph_postprocess_dof.d.ts.map +1 -1
  137. package/src/shade/renderer/postprocess/dof/raymarch/graph_postprocess_dof.js +29 -0
  138. package/src/shade/renderer/postprocess/motion_blur/MotionBlur.d.ts +6 -0
  139. package/src/shade/renderer/postprocess/motion_blur/MotionBlur.d.ts.map +1 -1
  140. package/src/shade/renderer/postprocess/motion_blur/MotionBlur.js +13 -1
  141. package/src/shade/renderer/postprocess/motion_blur/graph_postprocess_motion_blur.d.ts +6 -0
  142. package/src/shade/renderer/postprocess/motion_blur/graph_postprocess_motion_blur.d.ts.map +1 -1
  143. package/src/shade/renderer/postprocess/motion_blur/graph_postprocess_motion_blur.js +11 -0
  144. package/src/shade/renderer/postprocess/nss/NSS.d.ts +9 -0
  145. package/src/shade/renderer/postprocess/nss/NSS.d.ts.map +1 -1
  146. package/src/shade/renderer/postprocess/nss/NSS.js +33 -18
  147. package/src/shade/renderer/postprocess/ssr/SSR.d.ts +8 -0
  148. package/src/shade/renderer/postprocess/ssr/SSR.d.ts.map +1 -1
  149. package/src/shade/renderer/postprocess/ssr/SSR.js +42 -2
  150. package/src/shade/renderer/postprocess/ssr/ssr_graph_denoise_tiles.d.ts +6 -0
  151. package/src/shade/renderer/postprocess/ssr/ssr_graph_denoise_tiles.d.ts.map +1 -1
  152. package/src/shade/renderer/postprocess/ssr/ssr_graph_denoise_tiles.js +10 -0
  153. package/src/shade/renderer/postprocess/ssr/ssr_graph_prefilter_color.d.ts +6 -0
  154. package/src/shade/renderer/postprocess/ssr/ssr_graph_prefilter_color.d.ts.map +1 -1
  155. package/src/shade/renderer/postprocess/ssr/ssr_graph_prefilter_color.js +11 -0
  156. package/src/shade/renderer/rasterize/bucket/prepare_meshlet_draw_commands_by_material.d.ts +8 -0
  157. package/src/shade/renderer/rasterize/bucket/prepare_meshlet_draw_commands_by_material.d.ts.map +1 -1
  158. package/src/shade/renderer/rasterize/bucket/prepare_meshlet_draw_commands_by_material.js +27 -3
  159. package/src/shade/renderer/rasterize/native/avboit/AVBOITSettings.d.ts +6 -0
  160. package/src/shade/renderer/rasterize/native/avboit/AVBOITSettings.d.ts.map +1 -1
  161. package/src/shade/renderer/rasterize/native/avboit/AVBOITSettings.js +10 -0
  162. package/src/shade/renderer/rasterize/native/avboit/AVBOITSideChannel.d.ts +5 -4
  163. package/src/shade/renderer/rasterize/native/avboit/AVBOITSideChannel.d.ts.map +1 -1
  164. package/src/shade/renderer/rasterize/native/avboit/AVBOITSideChannel.js +5 -4
  165. package/src/shade/renderer/rasterize/native/avboit/AVBOIT_CONSTANTS.d.ts +3 -3
  166. package/src/shade/renderer/rasterize/native/avboit/AVBOIT_CONSTANTS.js +3 -3
  167. package/src/shade/renderer/rasterize/native/avboit/avboit_draw_fragment_body.d.ts.map +1 -1
  168. package/src/shade/renderer/rasterize/native/avboit/avboit_draw_fragment_body.js +5 -7
  169. package/src/shade/renderer/rasterize/native/avboit/avboit_unlit_draw.d.ts.map +1 -1
  170. package/src/shade/renderer/rasterize/native/avboit/avboit_unlit_draw.js +5 -7
  171. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_footprint_depth.d.ts +34 -0
  172. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_footprint_depth.d.ts.map +1 -0
  173. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_footprint_depth.js +42 -0
  174. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_integral_depth_coordinate.d.ts +5 -4
  175. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_integral_depth_coordinate.d.ts.map +1 -1
  176. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_integral_depth_coordinate.js +8 -7
  177. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_sample_transmittance.d.ts +2 -2
  178. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_sample_transmittance.d.ts.map +1 -1
  179. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_sample_transmittance.js +4 -5
  180. package/src/shade/renderer/rasterize/native/avboit/graph_rasterize_meshes_transparent_avboit.d.ts +9 -0
  181. package/src/shade/renderer/rasterize/native/avboit/graph_rasterize_meshes_transparent_avboit.d.ts.map +1 -1
  182. package/src/shade/renderer/rasterize/native/avboit/graph_rasterize_meshes_transparent_avboit.js +18 -0
  183. package/src/shade/renderer/rasterize/native/oit/graph_rasterize_meshes_transparent_oit.d.ts +9 -0
  184. package/src/shade/renderer/rasterize/native/oit/graph_rasterize_meshes_transparent_oit.d.ts.map +1 -1
  185. package/src/shade/renderer/rasterize/native/oit/graph_rasterize_meshes_transparent_oit.js +15 -0
  186. package/src/shade/renderer/restir/di/ReSTIRDI.d.ts +6 -0
  187. package/src/shade/renderer/restir/di/ReSTIRDI.d.ts.map +1 -1
  188. package/src/shade/renderer/restir/di/ReSTIRDI.js +10 -1
  189. package/src/shade/renderer/restir/di/graph_restir_di.d.ts +9 -0
  190. package/src/shade/renderer/restir/di/graph_restir_di.d.ts.map +1 -1
  191. package/src/shade/renderer/restir/di/graph_restir_di.js +19 -1
  192. package/src/shade/renderer/restir/di/graph_restir_di_denoise.d.ts +6 -0
  193. package/src/shade/renderer/restir/di/graph_restir_di_denoise.d.ts.map +1 -1
  194. package/src/shade/renderer/restir/di/graph_restir_di_denoise.js +9 -0
  195. package/src/shade/renderer/scene/movers/graph_scene_movers.d.ts +7 -0
  196. package/src/shade/renderer/scene/movers/graph_scene_movers.d.ts.map +1 -1
  197. package/src/shade/renderer/scene/movers/graph_scene_movers.js +11 -0
  198. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.d.ts +10 -0
  199. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.d.ts.map +1 -1
  200. package/src/shade/renderer/shadow/map/GPUSceneShadowmapContext.js +25 -1
  201. package/src/shade/renderer/shadow/vsm/graph_vsm_feedback.d.ts +7 -0
  202. package/src/shade/renderer/shadow/vsm/graph_vsm_feedback.d.ts.map +1 -1
  203. package/src/shade/renderer/shadow/vsm/graph_vsm_feedback.js +14 -0
  204. package/src/shade/renderer/text/shaders/shader_text_avboit.d.ts.map +1 -1
  205. package/src/shade/renderer/text/shaders/shader_text_avboit.js +5 -7
  206. package/src/shade/renderer/volumetrics/irradiance_clipmap/GPUIrradianceClipmap.d.ts +9 -1
  207. package/src/shade/renderer/volumetrics/irradiance_clipmap/GPUIrradianceClipmap.d.ts.map +1 -1
  208. package/src/shade/renderer/volumetrics/irradiance_clipmap/GPUIrradianceClipmap.js +16 -1
  209. package/src/shade/renderer/water/shader_water_avboit.d.ts.map +1 -1
  210. package/src/shade/renderer/water/shader_water_avboit.js +16 -13
  211. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_footprint_bias.d.ts +0 -33
  212. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_footprint_bias.d.ts.map +0 -1
  213. package/src/shade/renderer/rasterize/native/avboit/chunk_avboit_footprint_bias.js +0 -45
@@ -0,0 +1,563 @@
1
+ import { assert } from "../../../../core/assert.js";
2
+ import { array_shuffle } from "../../../../core/collection/array/array_shuffle.js";
3
+ import { uf_find, uf_init, uf_union } from "../../../../core/collection/union-find/union_find.js";
4
+ import { PI_HALF } from "../../../../core/math/PI_HALF.js";
5
+ import { seededRandom } from "../../../../core/math/random/seededRandom.js";
6
+ import Task from "../../../../core/process/task/Task.js";
7
+ import TaskGroup from "../../../../core/process/task/TaskGroup.js";
8
+ import { TaskSignal } from "../../../../core/process/task/TaskSignal.js";
9
+ import { actionTask } from "../../../../core/process/task/util/actionTask.js";
10
+ import { countTask } from "../../../../core/process/task/util/countTask.js";
11
+ import { WFCCellChoice } from "../../../../core/wfc/WFCCellChoice.js";
12
+ import { WFCLattice } from "../../../../core/wfc/WFCLattice.js";
13
+ import { WFCState } from "../../../../core/wfc/WFCState.js";
14
+ import { WFCStatus } from "../../../../core/wfc/WFCStatus.js";
15
+ import { wfc_solve } from "../../../../core/wfc/wfc_solve.js";
16
+ import { wfc_state_apply_boundary } from "../../../../core/wfc/wfc_state_apply_boundary.js";
17
+ import { wfc_state_read } from "../../../../core/wfc/wfc_state_read.js";
18
+ import { wfc_state_restrict } from "../../../../core/wfc/wfc_state_restrict.js";
19
+ import { compute_writes_union } from "../../../compute_writes_union.js";
20
+ import { GridTaskGenerator } from "../../GridTaskGenerator.js";
21
+
22
+ /**
23
+ * Solver operations per task cycle. A cycle is the unit the executor time-slices on, so this is
24
+ * how long the solve can hold a slice: a few thousand edge relaxations is well under a
25
+ * millisecond.
26
+ *
27
+ * @type {number}
28
+ */
29
+ const OPERATIONS_PER_CYCLE = 4096;
30
+
31
+ /**
32
+ * Lays tiles over the whole grid by wave function collapse: every cell gets one tile of a
33
+ * {@link GridWFCTileSet}, and every pair of neighbours shows the same socket on the edge they
34
+ * share. Each tile's {@link GridCellAction} is then run on its cell, turned the way the tile is.
35
+ *
36
+ * What adjacency alone cannot say is said around the solve:
37
+ * - {@link restrictions} narrow what a cell may hold before it starts — keep these cells open,
38
+ * let paving stand only where that field is high;
39
+ * - {@link boundary} says what may stand against the edge of the grid;
40
+ * - {@link connected} makes the tiles of one kind hang together — by opening what keeps them
41
+ * apart where the set has a tile for it, and by drawing again, up to {@link attempts} times,
42
+ * where it has not;
43
+ * - {@link fallback} covers the grid when every attempt failed, so a generator that must always
44
+ * produce a map does.
45
+ *
46
+ * The same grid, tile set and seed always give the same result.
47
+ */
48
+ export class GridTaskWaveFunctionCollapse extends GridTaskGenerator {
49
+
50
+ /**
51
+ * @type {GridWFCTileSet}
52
+ */
53
+ tiles = null;
54
+
55
+ /**
56
+ * A cell that `matcher` matches may only hold tiles carrying `tag`. A cell matched by several
57
+ * may only hold tiles carrying all their tags.
58
+ *
59
+ * @type {{matcher: CellMatcher, tag: string}[]}
60
+ */
61
+ restrictions = [];
62
+
63
+ /**
64
+ * Tag of the tiles allowed in the cells along the edge of the grid; `null` allows any.
65
+ *
66
+ * A tile set whose walls must close on themselves wants its open tile here: a wall could
67
+ * otherwise run off the edge and never have to turn back.
68
+ *
69
+ * @type {string|null}
70
+ */
71
+ boundary = null;
72
+
73
+ /**
74
+ * Tag of the tiles that must form one region, joined edge to edge; `null` skips the check.
75
+ *
76
+ * Adjacency rules cannot express this — a ring of wall with no gate in it is locally perfect —
77
+ * so it is seen to on the finished result. Where a tile keeping two regions apart has a
78
+ * **twin** carrying the tag (a tile with the same four sockets: the gap in a wall, for a
79
+ * stretch of that wall), the twin is swapped in and nothing around it has to change. Give the
80
+ * set such twins and one draw nearly always does; a result no swap can join (a pocket walled
81
+ * by tiles with no twin, or by two walls back to back) is thrown away and drawn again — which
82
+ * is all a set without twins can do, and a large grid can rarely afford.
83
+ *
84
+ * @type {string|null}
85
+ */
86
+ connected = null;
87
+
88
+ /**
89
+ * @type {WFCCellChoice}
90
+ */
91
+ cell_choice = WFCCellChoice.SCANLINE;
92
+
93
+ /**
94
+ * How many draws are tried before giving up. A draw is abandoned when the search runs out of
95
+ * backtracks, or when its result cannot be made {@link connected}.
96
+ *
97
+ * @type {number}
98
+ */
99
+ attempts = 8;
100
+
101
+ /**
102
+ * Name of the tile laid over the whole grid when every attempt failed; `null` fails the task
103
+ * instead. It has to be a tile that may neighbour itself, which {@link build} checks, and that
104
+ * may stand in every restricted cell and against the edge of the grid, which nothing checks:
105
+ * the fallback is laid over restrictions and boundary alike — the set's open tile, usually.
106
+ *
107
+ * @type {string|null}
108
+ */
109
+ fallback = null;
110
+
111
+ /**
112
+ * How the most recent build went, for tuning a tile set. Not an input. `backtracks` and
113
+ * `openings` are summed over every draw of the build, the ones thrown away included.
114
+ *
115
+ * @type {{attempts: number, backtracks: number, openings: number, solved: boolean, fallback: boolean}}
116
+ */
117
+ statistics = { attempts: 0, backtracks: 0, openings: 0, solved: false, fallback: false };
118
+
119
+ /**
120
+ * @param {Object} options
121
+ * @param {string} [options.name]
122
+ * @param {GridWFCTileSet} options.tiles
123
+ * @param {{matcher: CellMatcher, tag: string}[]} [options.restrictions]
124
+ * @param {string|null} [options.boundary]
125
+ * @param {string|null} [options.connected]
126
+ * @param {WFCCellChoice} [options.cell_choice]
127
+ * @param {number} [options.attempts]
128
+ * @param {string|null} [options.fallback]
129
+ * @returns {GridTaskWaveFunctionCollapse}
130
+ */
131
+ static from({
132
+ name = 'wave function collapse',
133
+ tiles,
134
+ restrictions = [],
135
+ boundary = null,
136
+ connected = null,
137
+ cell_choice = WFCCellChoice.SCANLINE,
138
+ attempts = 8,
139
+ fallback = null
140
+ }) {
141
+ assert.defined(tiles, 'tiles');
142
+ assert.isArray(restrictions, 'restrictions');
143
+ assert.isPositiveInteger(attempts, 'attempts');
144
+
145
+ for (let i = 0; i < restrictions.length; i++) {
146
+ assert.equal(restrictions[i].matcher.isCellMatcher, true, `restrictions[${i}].matcher.isCellMatcher !== true`);
147
+ assert.isString(restrictions[i].tag, `restrictions[${i}].tag`);
148
+ }
149
+
150
+ const r = new GridTaskWaveFunctionCollapse();
151
+
152
+ r.name = name;
153
+ r.tiles = tiles;
154
+ r.restrictions = restrictions;
155
+ r.boundary = boundary;
156
+ r.connected = connected;
157
+ r.cell_choice = cell_choice;
158
+ r.attempts = attempts;
159
+ r.fallback = fallback;
160
+
161
+ return r;
162
+ }
163
+
164
+ computeWrites() {
165
+ return compute_writes_union(this.tiles.prototypes.map(p => p.action).filter(a => a !== null));
166
+ }
167
+
168
+ build(grid, ecd, seed) {
169
+ const compiled = this.tiles.compile();
170
+ const prototypes = compiled.prototypes;
171
+
172
+ const width = grid.width;
173
+ const height = grid.height;
174
+ const cell_count = width * height;
175
+
176
+ // y on axis 0, so a cell's index in the lattice is the grid's own `y * width + x`
177
+ const lattice = cell_count > 0 ? WFCLattice.from_shape([height, width]) : null;
178
+
179
+ const restrictions = this.restrictions;
180
+ const boundary = this.boundary;
181
+ const connected = this.connected;
182
+ const cell_choice = this.cell_choice;
183
+ const attempts = this.attempts;
184
+
185
+ let fallback_tile = -1;
186
+
187
+ if (this.fallback !== null) {
188
+ const prototype = compiled.prototype_named(this.fallback);
189
+
190
+ // a real throw: a misspelt fallback would otherwise only be found the day it is needed
191
+ if (prototype === -1) {
192
+ throw new Error(`fallback tile '${this.fallback}' is not in the tile set`);
193
+ }
194
+
195
+ fallback_tile = compiled.tile_prototype.indexOf(prototype);
196
+
197
+ // the same reasoning: a fallback that cannot stand beside itself lays a grid of broken
198
+ // seams, and only on the day every draw failed
199
+ for (let direction = 0; direction < 4; direction++) {
200
+ if (!compiled.rules.allows(direction, fallback_tile, fallback_tile)) {
201
+ throw new Error(`fallback tile '${this.fallback}' cannot stand beside itself, so it cannot cover the grid`);
202
+ }
203
+ }
204
+ }
205
+
206
+ const statistics = { attempts: 0, backtracks: 0, openings: 0, solved: false, fallback: false };
207
+ this.statistics = statistics;
208
+
209
+ // what may stand against the grid's edge: the same for every attempt, and asked again
210
+ // of any tile swapped in after a solve
211
+ const boundary_masks = compiled.boundary_masks(boundary);
212
+
213
+ /**
214
+ * The tile each cell ended up with
215
+ * @type {Uint32Array}
216
+ */
217
+ const result = new Uint32Array(cell_count);
218
+
219
+ /**
220
+ * One draw from this for the whole build: an attempt continues the stream the last one
221
+ * left, so the build as a whole is a function of the seed
222
+ */
223
+ const random = seededRandom(seed);
224
+
225
+ /**
226
+ * Which restrictions hold in which cell is a fact about the grid, the same for every
227
+ * attempt: `cell_restriction[cell]` indexes `restriction_masks`, 0 meaning unrestricted
228
+ * @type {Uint32Array}
229
+ */
230
+ const cell_restriction = new Uint32Array(cell_count);
231
+ /** @type {(Uint32Array|null)[]} */
232
+ const restriction_masks = [null];
233
+
234
+ const tInitialize = actionTask(() => {
235
+ for (let i = 0; i < restrictions.length; i++) {
236
+ restrictions[i].matcher.initialize(grid, seed);
237
+ }
238
+
239
+ for (let i = 0; i < prototypes.length; i++) {
240
+ const action = prototypes[i].action;
241
+
242
+ if (action !== null) {
243
+ action.initialize(grid, seed);
244
+ }
245
+ }
246
+
247
+ if (restrictions.length === 0) {
248
+ return;
249
+ }
250
+
251
+ const words = compiled.rules.words_per_cell;
252
+ // cells matched by the same restrictions share a mask
253
+ const mask_of_set = new Map();
254
+
255
+ for (let cell = 0; cell < cell_count; cell++) {
256
+ const x = cell % width;
257
+ const y = (cell / width) | 0;
258
+
259
+ let key = '';
260
+
261
+ for (let i = 0; i < restrictions.length; i++) {
262
+ if (restrictions[i].matcher.match(grid, x, y, 0)) {
263
+ key += i + ',';
264
+ }
265
+ }
266
+
267
+ if (key === '') {
268
+ continue;
269
+ }
270
+
271
+ let index = mask_of_set.get(key);
272
+
273
+ if (index === undefined) {
274
+ const mask = new Uint32Array(words).fill(0xFFFFFFFF);
275
+
276
+ for (const i of key.split(',')) {
277
+ if (i === '') {
278
+ continue;
279
+ }
280
+
281
+ const tagged = compiled.mask_of_tag(restrictions[Number(i)].tag);
282
+
283
+ for (let w = 0; w < words; w++) {
284
+ mask[w] &= tagged[w];
285
+ }
286
+ }
287
+
288
+ index = restriction_masks.length;
289
+ restriction_masks.push(mask);
290
+ mask_of_set.set(key, index);
291
+ }
292
+
293
+ cell_restriction[cell] = index;
294
+ }
295
+ });
296
+
297
+ /**
298
+ * A fresh solver state with the boundary and the restrictions applied, or `null` when they
299
+ * leave some cell with nothing to hold — which no other draw would fix.
300
+ *
301
+ * @returns {WFCState|null}
302
+ */
303
+ const start = () => {
304
+ const state = new WFCState(compiled.rules, lattice, compiled.weights, random, cell_choice);
305
+
306
+ // always: even with no tag asked for, the edge of the grid must not cut a piece in two
307
+ if (!wfc_state_apply_boundary(state, boundary_masks)) {
308
+ return null;
309
+ }
310
+
311
+ for (let cell = 0; cell < cell_count; cell++) {
312
+ const index = cell_restriction[cell];
313
+
314
+ if (index !== 0 && !wfc_state_restrict(state, cell, restriction_masks[index])) {
315
+ return null;
316
+ }
317
+ }
318
+
319
+ return state;
320
+ };
321
+
322
+ /**
323
+ * May `tile` stand in `cell`, by the restrictions and the boundary? The solve has already
324
+ * answered this for the tile it chose; it is asked again for a tile swapped in afterwards.
325
+ *
326
+ * @param {number} cell
327
+ * @param {number} tile
328
+ * @returns {boolean}
329
+ */
330
+ const may_stand = (cell, tile) => {
331
+ const word = tile >> 5, bit = (1 << (tile & 31)) >>> 0;
332
+ const index = cell_restriction[cell];
333
+
334
+ if (index !== 0 && (restriction_masks[index][word] & bit) === 0) {
335
+ return false;
336
+ }
337
+
338
+ const x = cell % width, y = (cell / width) | 0;
339
+ const words = compiled.rules.words_per_cell;
340
+
341
+ // solver directions: axis 0 is y, axis 1 is x, the low side first
342
+ return (y !== 0 || (boundary_masks[word] & bit) !== 0)
343
+ && (y !== height - 1 || (boundary_masks[words + word] & bit) !== 0)
344
+ && (x !== 0 || (boundary_masks[2 * words + word] & bit) !== 0)
345
+ && (x !== width - 1 || (boundary_masks[3 * words + word] & bit) !== 0);
346
+ };
347
+
348
+ /**
349
+ * Make the cells of `result` whose tile carries the `connected` tag one region, if a few
350
+ * swaps can.
351
+ *
352
+ * Where two regions are kept apart by a tile that has a twin carrying the tag — the same
353
+ * sockets, so nothing around it has to change — the twin takes its place and the regions
354
+ * are one. One such opening per pair of regions, at a cell chosen by the build's draw.
355
+ *
356
+ * @returns {boolean} whether it ended as one region
357
+ */
358
+ const connect = () => {
359
+ const tagged = compiled.mask_of_tag(connected);
360
+ const member = (cell) => {
361
+ const tile = result[cell];
362
+
363
+ return (tagged[tile >> 5] >>> (tile & 31)) & 1;
364
+ };
365
+
366
+ const parent = new Uint32Array(cell_count);
367
+
368
+ uf_init(parent, cell_count);
369
+
370
+ for (let cell = 0; cell < cell_count; cell++) {
371
+ if (member(cell) === 0) {
372
+ continue;
373
+ }
374
+
375
+ // the cell on the +x side, unless this one ends its row, and the cell on the +y side
376
+ if ((cell + 1) % width !== 0 && member(cell + 1) === 1) {
377
+ uf_union(parent, cell, cell + 1);
378
+ }
379
+
380
+ if (cell + width < cell_count && member(cell + width) === 1) {
381
+ uf_union(parent, cell, cell + width);
382
+ }
383
+ }
384
+
385
+ const is_one_region = () => {
386
+ let root = -1;
387
+
388
+ for (let cell = 0; cell < cell_count; cell++) {
389
+ if (member(cell) === 0) {
390
+ continue;
391
+ }
392
+
393
+ const r = uf_find(parent, cell);
394
+
395
+ if (root === -1) {
396
+ root = r;
397
+ } else if (r !== root) {
398
+ return false;
399
+ }
400
+ }
401
+
402
+ return true;
403
+ };
404
+
405
+ if (is_one_region()) {
406
+ return true;
407
+ }
408
+
409
+ // every cell that could be opened, in an order the draw decides: taken in cell order,
410
+ // every pocket would be opened at its top-left
411
+ const candidates = [];
412
+
413
+ for (let cell = 0; cell < cell_count; cell++) {
414
+ if (member(cell) === 0 && compiled.twins_of(result[cell], connected).length > 0) {
415
+ candidates.push(cell);
416
+ }
417
+ }
418
+
419
+ array_shuffle(random, candidates);
420
+
421
+ const neighbours = [0, 0, 0, 0];
422
+
423
+ for (let i = 0; i < candidates.length; i++) {
424
+ const cell = candidates[i];
425
+ const x = cell % width;
426
+
427
+ let count = 0;
428
+
429
+ if (x > 0 && member(cell - 1) === 1) {
430
+ neighbours[count++] = cell - 1;
431
+ }
432
+
433
+ if (x < width - 1 && member(cell + 1) === 1) {
434
+ neighbours[count++] = cell + 1;
435
+ }
436
+
437
+ if (cell >= width && member(cell - width) === 1) {
438
+ neighbours[count++] = cell - width;
439
+ }
440
+
441
+ if (cell + width < cell_count && member(cell + width) === 1) {
442
+ neighbours[count++] = cell + width;
443
+ }
444
+
445
+ // worth opening only if it stands between two regions that are still apart
446
+ let apart = false;
447
+
448
+ for (let n = 1; n < count && !apart; n++) {
449
+ apart = uf_find(parent, neighbours[n]) !== uf_find(parent, neighbours[0]);
450
+ }
451
+
452
+ if (!apart) {
453
+ continue;
454
+ }
455
+
456
+ const twins = compiled.twins_of(result[cell], connected);
457
+ const first = Math.floor(random() * twins.length);
458
+
459
+ for (let t = 0; t < twins.length; t++) {
460
+ const twin = twins[(first + t) % twins.length];
461
+
462
+ if (!may_stand(cell, twin)) {
463
+ continue;
464
+ }
465
+
466
+ result[cell] = twin;
467
+ statistics.openings++;
468
+
469
+ for (let n = 0; n < count; n++) {
470
+ uf_union(parent, cell, neighbours[n]);
471
+ }
472
+
473
+ break;
474
+ }
475
+ }
476
+
477
+ return is_one_region();
478
+ };
479
+
480
+ /** @type {WFCState|null} */
481
+ let state = null;
482
+ let attempt = 0;
483
+
484
+ const give_up = () => {
485
+ if (fallback_tile === -1) {
486
+ return TaskSignal.EndFailure;
487
+ }
488
+
489
+ result.fill(fallback_tile);
490
+ statistics.fallback = true;
491
+
492
+ return TaskSignal.EndSuccess;
493
+ };
494
+
495
+ const tSolve = new Task({
496
+ name: `${this.name}: solve`,
497
+ cycleFunction() {
498
+ if (cell_count === 0) {
499
+ return TaskSignal.EndSuccess;
500
+ }
501
+
502
+ if (state === null) {
503
+ if (attempt >= attempts) {
504
+ return give_up();
505
+ }
506
+
507
+ attempt++;
508
+ statistics.attempts = attempt;
509
+
510
+ state = start();
511
+
512
+ if (state === null) {
513
+ // the restrictions contradict each other before a single tile is drawn
514
+ return give_up();
515
+ }
516
+ }
517
+
518
+ const status = wfc_solve(state, OPERATIONS_PER_CYCLE);
519
+
520
+ if (status === WFCStatus.RUNNING) {
521
+ return TaskSignal.Continue;
522
+ }
523
+
524
+ statistics.backtracks += state.backtrack_count;
525
+
526
+ if (status === WFCStatus.UNSATISFIABLE) {
527
+ // the search was exhaustive: the instance has no solution, whatever the draw
528
+ return give_up();
529
+ }
530
+
531
+ if (status === WFCStatus.SOLVED) {
532
+ wfc_state_read(result, state);
533
+
534
+ if (connected === null || connect()) {
535
+ statistics.solved = true;
536
+
537
+ return TaskSignal.EndSuccess;
538
+ }
539
+ }
540
+
541
+ // out of backtracks, or solved into pieces no swap could join: draw again
542
+ state = null;
543
+
544
+ return TaskSignal.Continue;
545
+ }
546
+ });
547
+
548
+ tSolve.addDependency(tInitialize);
549
+
550
+ const tApply = countTask(0, cell_count, cell => {
551
+ const tile = result[cell];
552
+ const action = prototypes[compiled.tile_prototype[tile]].action;
553
+
554
+ if (action !== null) {
555
+ action.execute(grid, cell % width, (cell / width) | 0, compiled.tile_turns[tile] * PI_HALF);
556
+ }
557
+ });
558
+
559
+ tApply.addDependency(tSolve);
560
+
561
+ return new TaskGroup([tInitialize, tSolve, tApply], this.name);
562
+ }
563
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Marks a socket as belonging to the inside of a multi-cell piece. Such a socket is suffixed with
3
+ * the variant's rotation when the set is compiled, so the cells of one turned copy of a piece can
4
+ * only ever meet each other — never the cells of the same piece turned another way.
5
+ *
6
+ * Not a character a caller's socket name can start with by accident.
7
+ *
8
+ * @type {string}
9
+ */
10
+ export const GRID_WFC_PIECE_SOCKET_PREFIX: string;
11
+ //# sourceMappingURL=GRID_WFC_PIECE_SOCKET_PREFIX.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GRID_WFC_PIECE_SOCKET_PREFIX.d.ts","sourceRoot":"","sources":["../../../../src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.js"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,2CAFU,MAAM,CAEqC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Marks a socket as belonging to the inside of a multi-cell piece. Such a socket is suffixed with
3
+ * the variant's rotation when the set is compiled, so the cells of one turned copy of a piece can
4
+ * only ever meet each other — never the cells of the same piece turned another way.
5
+ *
6
+ * Not a character a caller's socket name can start with by accident.
7
+ *
8
+ * @type {string}
9
+ */
10
+ export const GRID_WFC_PIECE_SOCKET_PREFIX = '\u0000';