@bitefoundry/retrieviewer 0.0.0-stage → 0.0.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 (297) hide show
  1. package/ASSET-PROVENANCE.md +43 -0
  2. package/ASSETS-LICENSE.md +58 -0
  3. package/ATTRIBUTION.md +54 -0
  4. package/EMBEDDING.md +96 -0
  5. package/INSTALLING.md +126 -0
  6. package/LICENSE +16 -0
  7. package/LICENSE-CODE +21 -0
  8. package/PLUGIN.md +110 -0
  9. package/PUPPY-MEMORY.md +57 -0
  10. package/PUPPY-Q5-COMPARISON.md +73 -0
  11. package/README.md +86 -2
  12. package/RELEASING.md +70 -0
  13. package/THIRD-PARTY-NOTICES.md +17 -0
  14. package/TRADEMARKS.md +21 -0
  15. package/WINDOWS-COMPANION.md +113 -0
  16. package/braille.mjs +45 -0
  17. package/examples/bridge.py +62 -0
  18. package/examples/embed.mjs +15 -0
  19. package/examples/live.mjs +40 -0
  20. package/examples/plugin.mjs +21 -0
  21. package/integrations/opencode/runtime.mjs +195 -0
  22. package/integrations/opencode/tui.tsx +59 -0
  23. package/integrations/windows-overlay/Overlay.cs +259 -0
  24. package/integrations/windows-overlay/desktop.mjs +87 -0
  25. package/integrations/windows-overlay/protocol.mjs +39 -0
  26. package/integrations/windows-overlay/worker.mjs +60 -0
  27. package/locomotion-core.mjs +21 -0
  28. package/locomotion-puppy.animal +0 -0
  29. package/package.json +131 -4
  30. package/pet-q5-manifest.json +118 -0
  31. package/puppy-clean.mjs +51 -0
  32. package/puppy-companion-interactions-v3-q5.bin.gz +0 -0
  33. package/puppy-companion-interactions-v3.json +1 -0
  34. package/puppy-companion-life.mjs +182 -0
  35. package/puppy-companion-objects.mjs +30 -0
  36. package/puppy-companion-sprites.mjs +14 -0
  37. package/puppy-continuous.mjs +57 -0
  38. package/puppy-face.mjs +46 -0
  39. package/puppy-frame-predictive.mjs +147 -0
  40. package/puppy-frame-store.mjs +108 -0
  41. package/puppy-mouth-anchor.json +1 -0
  42. package/puppy-mouth-anchor.mjs +33 -0
  43. package/puppy-paper-actions-v9-q5.bin.gz +0 -0
  44. package/puppy-paper-actions-v9.json +1 -0
  45. package/puppy-paper-actions.mjs +48 -0
  46. package/puppy-paper-approach-q5.bin.gz +0 -0
  47. package/puppy-paper-approach.json +1 -0
  48. package/puppy-paper-area.mjs +15 -0
  49. package/puppy-paper-arrival-seat-q5.bin.gz +0 -0
  50. package/puppy-paper-arrival-seat.json +1 -0
  51. package/puppy-paper-behaviors-q5.bin.gz +0 -0
  52. package/puppy-paper-behaviors.json +1 -0
  53. package/puppy-paper-behaviors.mjs +8 -0
  54. package/puppy-paper-contact.mjs +103 -0
  55. package/puppy-paper-director.mjs +21 -0
  56. package/puppy-paper-paw-transport.mjs +76 -0
  57. package/puppy-paper-rear-gallop-q5.bin.gz +0 -0
  58. package/puppy-paper-rear-gallop.json +1 -0
  59. package/puppy-paper-seat-q5.bin.gz +0 -0
  60. package/puppy-paper-seat.json +14 -0
  61. package/puppy-paper-seat.mjs +13 -0
  62. package/puppy-paper-trick-timing.mjs +58 -0
  63. package/puppy-paper-world.mjs +108 -0
  64. package/puppy-paper.mjs +271 -0
  65. package/puppy-stable-meta.json +1606 -0
  66. package/puppy-stable.mjs +72 -0
  67. package/puppy-stride-meta.json +343 -0
  68. package/puppy-stride-tweens-q5.bin.gz +0 -0
  69. package/puppy-zoomies.mjs +27 -0
  70. package/puppy-zoomies30.mjs +12 -0
  71. package/retrieviewer-cli.mjs +94 -0
  72. package/retrieviewer-config.mjs +49 -0
  73. package/retrieviewer-install.mjs +157 -0
  74. package/retrieviewer-plugin.d.mts +44 -0
  75. package/retrieviewer-plugin.mjs +97 -0
  76. package/retrieviewer-stdio.mjs +38 -0
  77. package/retrieviewer.d.mts +85 -0
  78. package/retrieviewer.mjs +142 -0
  79. package/vendor/procedural-animals/LICENSE +21 -0
  80. package/vendor/procedural-animals/package.json +40 -0
  81. package/vendor/procedural-animals/src/animal.js +146 -0
  82. package/vendor/procedural-animals/src/bake.js +51 -0
  83. package/vendor/procedural-animals/src/build.js +81 -0
  84. package/vendor/procedural-animals/src/core/build/coatKit.js +121 -0
  85. package/vendor/procedural-animals/src/core/build/featherCards.js +189 -0
  86. package/vendor/procedural-animals/src/core/build/finEdges.js +85 -0
  87. package/vendor/procedural-animals/src/core/build/furSlope.js +115 -0
  88. package/vendor/procedural-animals/src/core/build/mesh.js +67 -0
  89. package/vendor/procedural-animals/src/core/build/pipeline.js +272 -0
  90. package/vendor/procedural-animals/src/core/build/seams.js +183 -0
  91. package/vendor/procedural-animals/src/core/build/warp.js +112 -0
  92. package/vendor/procedural-animals/src/core/build/weights.js +393 -0
  93. package/vendor/procedural-animals/src/core/math/vec.js +81 -0
  94. package/vendor/procedural-animals/src/core/motion/actions.js +800 -0
  95. package/vendor/procedural-animals/src/core/motion/bird.js +3063 -0
  96. package/vendor/procedural-animals/src/core/motion/birdActions.js +1059 -0
  97. package/vendor/procedural-animals/src/core/motion/common.js +158 -0
  98. package/vendor/procedural-animals/src/core/motion/index.js +22 -0
  99. package/vendor/procedural-animals/src/core/motion/quadruped.js +2875 -0
  100. package/vendor/procedural-animals/src/core/motion/snake.js +1594 -0
  101. package/vendor/procedural-animals/src/core/motion/spider.js +1810 -0
  102. package/vendor/procedural-animals/src/core/motion/swimmer.js +1047 -0
  103. package/vendor/procedural-animals/src/core/motion/swimmerActions.js +613 -0
  104. package/vendor/procedural-animals/src/core/render/animalObject.js +262 -0
  105. package/vendor/procedural-animals/src/core/render/coatMaterial.js +1301 -0
  106. package/vendor/procedural-animals/src/core/render/dqs.js +159 -0
  107. package/vendor/procedural-animals/src/core/render/eyes.js +676 -0
  108. package/vendor/procedural-animals/src/core/render/membranes.js +77 -0
  109. package/vendor/procedural-animals/src/core/render/skeleton.js +41 -0
  110. package/vendor/procedural-animals/src/core/rig/bird.js +327 -0
  111. package/vendor/procedural-animals/src/core/rig/quadruped.js +76 -0
  112. package/vendor/procedural-animals/src/core/rig/rig.js +79 -0
  113. package/vendor/procedural-animals/src/core/rig/snake.js +76 -0
  114. package/vendor/procedural-animals/src/core/rig/spider.js +110 -0
  115. package/vendor/procedural-animals/src/core/rig/swimmer.js +181 -0
  116. package/vendor/procedural-animals/src/core/sdf/eyeSocket.js +53 -0
  117. package/vendor/procedural-animals/src/core/sdf/mesher.js +238 -0
  118. package/vendor/procedural-animals/src/core/sdf/sdf.js +263 -0
  119. package/vendor/procedural-animals/src/index.d.ts +98 -0
  120. package/vendor/procedural-animals/src/index.js +29 -0
  121. package/vendor/procedural-animals/src/registry.js +48 -0
  122. package/vendor/procedural-animals/src/species/bear/behaviour.js +463 -0
  123. package/vendor/procedural-animals/src/species/bear/claws.js +147 -0
  124. package/vendor/procedural-animals/src/species/bear/coat.js +726 -0
  125. package/vendor/procedural-animals/src/species/bear/index.js +139 -0
  126. package/vendor/procedural-animals/src/species/bear/motion.js +112 -0
  127. package/vendor/procedural-animals/src/species/bear/regions.js +78 -0
  128. package/vendor/procedural-animals/src/species/bear/rig.js +112 -0
  129. package/vendor/procedural-animals/src/species/bear/sculpt.js +272 -0
  130. package/vendor/procedural-animals/src/species/boar/behaviour.js +335 -0
  131. package/vendor/procedural-animals/src/species/boar/coat.js +372 -0
  132. package/vendor/procedural-animals/src/species/boar/index.js +110 -0
  133. package/vendor/procedural-animals/src/species/boar/motion.js +85 -0
  134. package/vendor/procedural-animals/src/species/boar/regions.js +88 -0
  135. package/vendor/procedural-animals/src/species/boar/rig.js +111 -0
  136. package/vendor/procedural-animals/src/species/boar/sculpt.js +285 -0
  137. package/vendor/procedural-animals/src/species/cat/behaviour.js +559 -0
  138. package/vendor/procedural-animals/src/species/cat/coat.js +841 -0
  139. package/vendor/procedural-animals/src/species/cat/index.js +211 -0
  140. package/vendor/procedural-animals/src/species/cat/motion.js +101 -0
  141. package/vendor/procedural-animals/src/species/cat/regions.js +82 -0
  142. package/vendor/procedural-animals/src/species/cat/rig.js +100 -0
  143. package/vendor/procedural-animals/src/species/cat/sculpt.js +266 -0
  144. package/vendor/procedural-animals/src/species/cat/whiskers.js +110 -0
  145. package/vendor/procedural-animals/src/species/cheetah/coat.js +427 -0
  146. package/vendor/procedural-animals/src/species/cheetah/index.js +51 -0
  147. package/vendor/procedural-animals/src/species/cheetah/joints.js +3 -0
  148. package/vendor/procedural-animals/src/species/cheetah/motion.js +77 -0
  149. package/vendor/procedural-animals/src/species/cheetah/regions.js +95 -0
  150. package/vendor/procedural-animals/src/species/cheetah/rig.js +66 -0
  151. package/vendor/procedural-animals/src/species/cheetah/sculpt.js +158 -0
  152. package/vendor/procedural-animals/src/species/chicken/coat.js +313 -0
  153. package/vendor/procedural-animals/src/species/chicken/coverts.js +320 -0
  154. package/vendor/procedural-animals/src/species/chicken/index.js +110 -0
  155. package/vendor/procedural-animals/src/species/chicken/motion.js +177 -0
  156. package/vendor/procedural-animals/src/species/chicken/regions.js +66 -0
  157. package/vendor/procedural-animals/src/species/chicken/rig.js +144 -0
  158. package/vendor/procedural-animals/src/species/chicken/sculpt.js +389 -0
  159. package/vendor/procedural-animals/src/species/chicken/wingLook.js +66 -0
  160. package/vendor/procedural-animals/src/species/chicken/wingStack.js +124 -0
  161. package/vendor/procedural-animals/src/species/cow/behaviour.js +521 -0
  162. package/vendor/procedural-animals/src/species/cow/coat.js +465 -0
  163. package/vendor/procedural-animals/src/species/cow/index.js +118 -0
  164. package/vendor/procedural-animals/src/species/cow/motion.js +81 -0
  165. package/vendor/procedural-animals/src/species/cow/regions.js +98 -0
  166. package/vendor/procedural-animals/src/species/cow/rig.js +116 -0
  167. package/vendor/procedural-animals/src/species/cow/sculpt.js +339 -0
  168. package/vendor/procedural-animals/src/species/crow/coat.js +145 -0
  169. package/vendor/procedural-animals/src/species/crow/index.js +70 -0
  170. package/vendor/procedural-animals/src/species/crow/joints.js +3 -0
  171. package/vendor/procedural-animals/src/species/crow/motion.js +101 -0
  172. package/vendor/procedural-animals/src/species/crow/regions.js +50 -0
  173. package/vendor/procedural-animals/src/species/crow/rig.js +83 -0
  174. package/vendor/procedural-animals/src/species/crow/sculpt.js +118 -0
  175. package/vendor/procedural-animals/src/species/deer/behaviour.js +513 -0
  176. package/vendor/procedural-animals/src/species/deer/coat.js +391 -0
  177. package/vendor/procedural-animals/src/species/deer/index.js +106 -0
  178. package/vendor/procedural-animals/src/species/deer/motion.js +97 -0
  179. package/vendor/procedural-animals/src/species/deer/regions.js +60 -0
  180. package/vendor/procedural-animals/src/species/deer/rig.js +124 -0
  181. package/vendor/procedural-animals/src/species/deer/sculpt.js +297 -0
  182. package/vendor/procedural-animals/src/species/dog/behaviour.js +521 -0
  183. package/vendor/procedural-animals/src/species/dog/coat.js +495 -0
  184. package/vendor/procedural-animals/src/species/dog/index.js +144 -0
  185. package/vendor/procedural-animals/src/species/dog/motion.js +118 -0
  186. package/vendor/procedural-animals/src/species/dog/regions.js +61 -0
  187. package/vendor/procedural-animals/src/species/dog/rig.js +129 -0
  188. package/vendor/procedural-animals/src/species/dog/sculpt.js +246 -0
  189. package/vendor/procedural-animals/src/species/eagle/coat.js +250 -0
  190. package/vendor/procedural-animals/src/species/eagle/index.js +76 -0
  191. package/vendor/procedural-animals/src/species/eagle/joints.js +3 -0
  192. package/vendor/procedural-animals/src/species/eagle/motion.js +137 -0
  193. package/vendor/procedural-animals/src/species/eagle/regions.js +57 -0
  194. package/vendor/procedural-animals/src/species/eagle/rig.js +107 -0
  195. package/vendor/procedural-animals/src/species/eagle/sculpt.js +137 -0
  196. package/vendor/procedural-animals/src/species/fish/coat.js +271 -0
  197. package/vendor/procedural-animals/src/species/fish/index.js +88 -0
  198. package/vendor/procedural-animals/src/species/fish/motion.js +92 -0
  199. package/vendor/procedural-animals/src/species/fish/regions.js +27 -0
  200. package/vendor/procedural-animals/src/species/fish/rig.js +143 -0
  201. package/vendor/procedural-animals/src/species/fish/sculpt.js +214 -0
  202. package/vendor/procedural-animals/src/species/fish/variants.js +126 -0
  203. package/vendor/procedural-animals/src/species/fox/behaviour.js +314 -0
  204. package/vendor/procedural-animals/src/species/fox/coat.js +444 -0
  205. package/vendor/procedural-animals/src/species/fox/index.js +120 -0
  206. package/vendor/procedural-animals/src/species/fox/motion.js +81 -0
  207. package/vendor/procedural-animals/src/species/fox/regions.js +59 -0
  208. package/vendor/procedural-animals/src/species/fox/rig.js +91 -0
  209. package/vendor/procedural-animals/src/species/fox/sculpt.js +202 -0
  210. package/vendor/procedural-animals/src/species/frog/behaviour.js +590 -0
  211. package/vendor/procedural-animals/src/species/frog/coat.js +305 -0
  212. package/vendor/procedural-animals/src/species/frog/index.js +97 -0
  213. package/vendor/procedural-animals/src/species/frog/motion.js +69 -0
  214. package/vendor/procedural-animals/src/species/frog/regions.js +46 -0
  215. package/vendor/procedural-animals/src/species/frog/rig.js +156 -0
  216. package/vendor/procedural-animals/src/species/frog/sculpt.js +240 -0
  217. package/vendor/procedural-animals/src/species/goat/behaviour.js +509 -0
  218. package/vendor/procedural-animals/src/species/goat/coat.js +590 -0
  219. package/vendor/procedural-animals/src/species/goat/index.js +149 -0
  220. package/vendor/procedural-animals/src/species/goat/motion.js +95 -0
  221. package/vendor/procedural-animals/src/species/goat/regions.js +98 -0
  222. package/vendor/procedural-animals/src/species/goat/rig.js +181 -0
  223. package/vendor/procedural-animals/src/species/goat/sculpt.js +547 -0
  224. package/vendor/procedural-animals/src/species/horse/coat.js +361 -0
  225. package/vendor/procedural-animals/src/species/horse/coatSeam.js +122 -0
  226. package/vendor/procedural-animals/src/species/horse/index.js +80 -0
  227. package/vendor/procedural-animals/src/species/horse/motion.js +245 -0
  228. package/vendor/procedural-animals/src/species/horse/regions.js +60 -0
  229. package/vendor/procedural-animals/src/species/horse/rig.js +117 -0
  230. package/vendor/procedural-animals/src/species/horse/sculpt.js +316 -0
  231. package/vendor/procedural-animals/src/species/horse/seamWeights.js +272 -0
  232. package/vendor/procedural-animals/src/species/lion/behaviour.js +509 -0
  233. package/vendor/procedural-animals/src/species/lion/coat.js +1016 -0
  234. package/vendor/procedural-animals/src/species/lion/index.js +111 -0
  235. package/vendor/procedural-animals/src/species/lion/motion.js +87 -0
  236. package/vendor/procedural-animals/src/species/lion/regions.js +106 -0
  237. package/vendor/procedural-animals/src/species/lion/rig.js +110 -0
  238. package/vendor/procedural-animals/src/species/lion/sculpt.js +384 -0
  239. package/vendor/procedural-animals/src/species/lion/whiskers.js +132 -0
  240. package/vendor/procedural-animals/src/species/pig/behaviour.js +575 -0
  241. package/vendor/procedural-animals/src/species/pig/coat.js +730 -0
  242. package/vendor/procedural-animals/src/species/pig/ears.js +242 -0
  243. package/vendor/procedural-animals/src/species/pig/index.js +161 -0
  244. package/vendor/procedural-animals/src/species/pig/motion.js +105 -0
  245. package/vendor/procedural-animals/src/species/pig/regions.js +100 -0
  246. package/vendor/procedural-animals/src/species/pig/rig.js +152 -0
  247. package/vendor/procedural-animals/src/species/pig/sculpt.js +382 -0
  248. package/vendor/procedural-animals/src/species/rabbit/behaviour.js +605 -0
  249. package/vendor/procedural-animals/src/species/rabbit/coat.js +285 -0
  250. package/vendor/procedural-animals/src/species/rabbit/index.js +85 -0
  251. package/vendor/procedural-animals/src/species/rabbit/motion.js +85 -0
  252. package/vendor/procedural-animals/src/species/rabbit/regions.js +48 -0
  253. package/vendor/procedural-animals/src/species/rabbit/rig.js +99 -0
  254. package/vendor/procedural-animals/src/species/rabbit/sculpt.js +148 -0
  255. package/vendor/procedural-animals/src/species/rat/behaviour.js +539 -0
  256. package/vendor/procedural-animals/src/species/rat/coat.js +341 -0
  257. package/vendor/procedural-animals/src/species/rat/index.js +111 -0
  258. package/vendor/procedural-animals/src/species/rat/motion.js +90 -0
  259. package/vendor/procedural-animals/src/species/rat/regions.js +92 -0
  260. package/vendor/procedural-animals/src/species/rat/rig.js +112 -0
  261. package/vendor/procedural-animals/src/species/rat/sculpt.js +163 -0
  262. package/vendor/procedural-animals/src/species/rat/whiskers.js +119 -0
  263. package/vendor/procedural-animals/src/species/shark/coat.js +383 -0
  264. package/vendor/procedural-animals/src/species/shark/index.js +104 -0
  265. package/vendor/procedural-animals/src/species/shark/motion.js +69 -0
  266. package/vendor/procedural-animals/src/species/shark/regions.js +34 -0
  267. package/vendor/procedural-animals/src/species/shark/rig.js +194 -0
  268. package/vendor/procedural-animals/src/species/shark/sculpt.js +343 -0
  269. package/vendor/procedural-animals/src/species/shark/variants.js +105 -0
  270. package/vendor/procedural-animals/src/species/sheep/behaviour.js +522 -0
  271. package/vendor/procedural-animals/src/species/sheep/coat.js +529 -0
  272. package/vendor/procedural-animals/src/species/sheep/index.js +152 -0
  273. package/vendor/procedural-animals/src/species/sheep/motion.js +86 -0
  274. package/vendor/procedural-animals/src/species/sheep/neck.js +41 -0
  275. package/vendor/procedural-animals/src/species/sheep/regions.js +62 -0
  276. package/vendor/procedural-animals/src/species/sheep/rig.js +163 -0
  277. package/vendor/procedural-animals/src/species/sheep/sculpt.js +542 -0
  278. package/vendor/procedural-animals/src/species/snake/coat.js +315 -0
  279. package/vendor/procedural-animals/src/species/snake/index.js +73 -0
  280. package/vendor/procedural-animals/src/species/snake/motion.js +51 -0
  281. package/vendor/procedural-animals/src/species/snake/regions.js +51 -0
  282. package/vendor/procedural-animals/src/species/snake/rig.js +108 -0
  283. package/vendor/procedural-animals/src/species/snake/sculpt.js +122 -0
  284. package/vendor/procedural-animals/src/species/spider/coat.js +279 -0
  285. package/vendor/procedural-animals/src/species/spider/index.js +78 -0
  286. package/vendor/procedural-animals/src/species/spider/motion.js +72 -0
  287. package/vendor/procedural-animals/src/species/spider/regions.js +87 -0
  288. package/vendor/procedural-animals/src/species/spider/rig.js +118 -0
  289. package/vendor/procedural-animals/src/species/spider/sculpt.js +182 -0
  290. package/vendor/procedural-animals/src/species/wolf/behaviour.js +160 -0
  291. package/vendor/procedural-animals/src/species/wolf/coat.js +749 -0
  292. package/vendor/procedural-animals/src/species/wolf/index.js +107 -0
  293. package/vendor/procedural-animals/src/species/wolf/motion.js +80 -0
  294. package/vendor/procedural-animals/src/species/wolf/regions.js +57 -0
  295. package/vendor/procedural-animals/src/species/wolf/rig.js +87 -0
  296. package/vendor/procedural-animals/src/species/wolf/sculpt.js +339 -0
  297. package/vendor/procedural-animals/src/worker.js +29 -0
@@ -0,0 +1,115 @@
1
+ // Fur-length slope limit (shorten only), applied by the pipeline to every coat.
2
+ //
3
+ // The coat shells are offset copies of the skin at hair height x (0..1) x furLen, so where the hair
4
+ // length jumps from one vertex to the next the shells stand up as a wall: a round pad with a hard
5
+ // rim (a cheek switched from 5 to 11 mm across a plane), an eye sunk in a ring of long fur, a strand
6
+ // pattern smeared into a smooth plate over the steep triangles. Real hair changes length gradually
7
+ // and lies over the shorter hair around it, so the coat surface has no cliffs.
8
+ //
9
+ // The limit: hair may grow no faster than `slope` metres of hair per metre of skin above the hair
10
+ // around it, U(v) = min(L(v), min_u U(u) + s(v) |p_u - p_v|) over the furred, hairy vertices of the
11
+ // same surface (a Dijkstra sweep from the shortest hair outward: exact, one pass). Long hair may rise
12
+ // proportionally faster, s(v) = slope x max(1, L(v) / free): every boundary then bevels over at most
13
+ // ~free / slope of skin whatever the hair length, so manes, ruffs, forelocks and fleeces keep
14
+ // standing off (their edge is beveled, not flattened) while a face loses its walls.
15
+ //
16
+ // Species control (optional):
17
+ // species.render.furSlope slope for hair up to `furSlopeFree` long (default 1); false / 0: off
18
+ // species.render.furSlopeFree hair length (m) above which the allowed slope grows (default 0.025)
19
+ // coat().furSlope per-vertex slope (Float32Array) replacing the default at that vertex;
20
+ // Infinity (or <= 0) leaves that vertex's hair as painted (a crest meant
21
+ // to stand up as a wall)
22
+ // Bare vertices (no hair, or a material without shells) neither limit nor get limited: fur may meet a
23
+ // lid margin, the nose leather or a hoof at full length.
24
+
25
+ export const FUR_SLOPE_DEFAULTS = { slope: 1.0, free: 0.025 };
26
+
27
+ const HAIRY = (m) => m === 0 || m === 4 || m === 8 || m === 9; // materials that grow shells (coatMaterial.js hairy())
28
+
29
+ /**
30
+ * Shortens furLen in place so that no hair rises faster than the slope above its neighbours.
31
+ * @param {Float32Array} furLen per-vertex hair length (m), modified in place
32
+ * @param {Float32Array} pos per-vertex positions (reference space)
33
+ * @param {Uint32Array} index triangle indices
34
+ * @param {number} nV vertex count (vertices >= nV are ignored)
35
+ * @param {Float32Array} tint per-vertex rgba, a = material id
36
+ * @param {object} o { slope, free, perVertex }
37
+ * @returns {{ shortened: number, maxCut: number }} vertices shortened by more than 0.1 mm, the largest cut (m)
38
+ */
39
+ export function limitFurSlope(furLen, pos, index, nV, tint, { slope = FUR_SLOPE_DEFAULTS.slope, free = FUR_SLOPE_DEFAULTS.free, perVertex = null } = {}) {
40
+ const res = { shortened: 0, maxCut: 0 };
41
+ if (!furLen || !(slope > 0) || !(free > 0)) return res;
42
+ const L0 = Float32Array.from(furLen.subarray(0, nV));
43
+ const active = new Uint8Array(nV);
44
+ let any = false;
45
+ for (let v = 0; v < nV; v++) {
46
+ if (L0[v] > 0 && HAIRY(Math.round(tint[v * 4 + 3]))) { active[v] = 1; any = true; }
47
+ }
48
+ if (!any) return res;
49
+ // the allowed slope of each vertex's hair (0 = unlimited)
50
+ const sOf = new Float32Array(nV);
51
+ for (let v = 0; v < nV; v++) {
52
+ if (!active[v]) continue;
53
+ let s = perVertex ? perVertex[v] : slope;
54
+ if (perVertex && !(s > 0)) s = Infinity; // a species' explicit "leave as painted"
55
+ sOf[v] = Number.isFinite(s) ? s * Math.max(1, L0[v] / free) : 0;
56
+ }
57
+ // adjacency (CSR) over the active vertices
58
+ const deg = new Uint32Array(nV + 1);
59
+ for (let i = 0; i < index.length; i += 3) {
60
+ for (let e = 0; e < 3; e++) {
61
+ const u = index[i + e], v = index[i + ((e + 1) % 3)];
62
+ if (u < nV && v < nV && active[u] && active[v]) { deg[u + 1]++; deg[v + 1]++; }
63
+ }
64
+ }
65
+ for (let v = 0; v < nV; v++) deg[v + 1] += deg[v];
66
+ const adj = new Uint32Array(deg[nV]), fill = deg.slice(0, nV);
67
+ for (let i = 0; i < index.length; i += 3) {
68
+ for (let e = 0; e < 3; e++) {
69
+ const u = index[i + e], v = index[i + ((e + 1) % 3)];
70
+ if (u < nV && v < nV && active[u] && active[v]) { adj[fill[u]++] = v; adj[fill[v]++] = u; }
71
+ }
72
+ }
73
+ // Dijkstra: settle vertices in increasing (limited) length; a settled vertex bounds its neighbours
74
+ const U = furLen;
75
+ let hk = new Float64Array(4096), hv = new Int32Array(4096), n = 0;
76
+ const push = (k, v) => {
77
+ if (n === hk.length) { const k2 = new Float64Array(n * 2); k2.set(hk); hk = k2; const v2 = new Int32Array(n * 2); v2.set(hv); hv = v2; }
78
+ let i = n++;
79
+ while (i > 0) { const p = (i - 1) >> 1; if (hk[p] <= k) break; hk[i] = hk[p]; hv[i] = hv[p]; i = p; }
80
+ hk[i] = k; hv[i] = v;
81
+ };
82
+ const pop = () => {
83
+ const top = hv[0], k = hk[--n], v = hv[n];
84
+ let i = 0;
85
+ for (;;) {
86
+ const l = 2 * i + 1;
87
+ if (l >= n) break;
88
+ const r = l + 1, m = r < n && hk[r] < hk[l] ? r : l;
89
+ if (hk[m] >= k) break;
90
+ hk[i] = hk[m]; hv[i] = hv[m]; i = m;
91
+ }
92
+ hk[i] = k; hv[i] = v;
93
+ return top;
94
+ };
95
+ const done = new Uint8Array(nV);
96
+ for (let v = 0; v < nV; v++) if (active[v]) push(U[v], v);
97
+ while (n) {
98
+ const v = pop();
99
+ if (done[v]) continue;
100
+ done[v] = 1;
101
+ const Uv = U[v], px = pos[v * 3], py = pos[v * 3 + 1], pz = pos[v * 3 + 2];
102
+ for (let k = deg[v]; k < deg[v + 1]; k++) {
103
+ const u = adj[k];
104
+ if (done[u] || sOf[u] === 0) continue;
105
+ const cand = Uv + sOf[u] * Math.hypot(pos[u * 3] - px, pos[u * 3 + 1] - py, pos[u * 3 + 2] - pz);
106
+ if (cand < U[u]) { U[u] = cand; push(cand, u); }
107
+ }
108
+ }
109
+ for (let v = 0; v < nV; v++) {
110
+ const cut = L0[v] - U[v];
111
+ if (cut > 0.0001) res.shortened++;
112
+ if (cut > res.maxCut) res.maxCut = cut;
113
+ }
114
+ return res;
115
+ }
@@ -0,0 +1,67 @@
1
+ // Meshing stage: turns a species' SDF sculpt into surface pieces ("regions").
2
+ //
3
+ // A species describes its regions as jobs (each job can run in its own worker):
4
+ // { name, part, bmin, bmax, h, F?, clip?(x,y,z), patches?: [{ c, R, B, h }], rigidBone? }
5
+ // part: which primitives to mesh (primitives carry part: 'body' | 'jaw' | ...)
6
+ // h: cell size in metres (quality tiers multiply it)
7
+ // clip: keep only cells whose centre passes (used for overlapping regions that cross-fade)
8
+ // patches: balls re-meshed at a finer cell size (eyelid margins); the region itself is clipped
9
+ // to exclude the inner ball and the two cross-fade in a thin shell
10
+ // rigidBone: the whole region follows one bone (a separately meshed jaw)
11
+ import { meshSDF } from '../sdf/mesher.js';
12
+
13
+ export function meshRegion(model, R) {
14
+ const prims = model.forPart(R.part || 'body');
15
+ if (!prims.length) throw new Error(`region ${R.name}: no primitives in part ${R.part}`);
16
+ const d2 = (x, y, z, c) => (x - c[0]) * (x - c[0]) + (y - c[1]) * (y - c[1]) + (z - c[2]) * (z - c[2]);
17
+ const patches = R.patches || [];
18
+ const baseClip = R.clip || null;
19
+ // The region stops one of its own cells inside the patch's inner radius: its cut is as ragged as its
20
+ // cells, and a cut that pokes past the middle of the cross-fade band (a cell wider than the band B) left
21
+ // a slit there that neither surface covered; one cell further in, the cut lies where the patch shows
22
+ // alone and the region's fade is ~0 (drawn nowhere).
23
+ const clip = patches.length
24
+ ? (x, y, z) => (!baseClip || baseClip(x, y, z)) && patches.every((p) => d2(x, y, z, p.c) > Math.max(0, p.R - p.B - R.h) ** 2)
25
+ : baseClip;
26
+ const parts = [meshSDF(prims, R.bmin, R.bmax, R.h, R.F || 6, null, clip)];
27
+ for (const p of patches) {
28
+ const rOut = p.R + p.B, pad = rOut + 3 * p.h;
29
+ parts.push(meshSDF(prims, [p.c[0] - pad, p.c[1] - pad, p.c[2] - pad], [p.c[0] + pad, p.c[1] + pad, p.c[2] + pad], p.h, 4, null, (x, y, z) => d2(x, y, z, p.c) < rOut * rOut && (!baseClip || baseClip(x, y, z))));
30
+ }
31
+ const m = parts.length > 1 ? mergeMeshes(parts) : { ...parts[0], patch: null };
32
+ return serialiseLists({ name: R.name, ...m });
33
+ }
34
+
35
+ export function mergeMeshes(meshes) {
36
+ let n = 0, ni = 0;
37
+ for (const m of meshes) { n += m.count; ni += m.indices.length; }
38
+ const positions = new Float32Array(n * 3), normals = new Float32Array(n * 3), indices = new Uint32Array(ni), patch = new Uint8Array(n);
39
+ const lists = new Array(n);
40
+ let v0 = 0, i0 = 0;
41
+ meshes.forEach((m, k) => {
42
+ positions.set(m.positions, v0 * 3);
43
+ normals.set(m.normals, v0 * 3);
44
+ for (let i = 0; i < m.indices.length; i++) indices[i0 + i] = m.indices[i] + v0;
45
+ for (let i = 0; i < m.count; i++) { lists[v0 + i] = m.lists[i]; patch[v0 + i] = k > 0 ? 1 : 0; }
46
+ v0 += m.count; i0 += m.indices.length;
47
+ });
48
+ return { positions, normals, indices, lists, count: n, patch };
49
+ }
50
+
51
+ // replace per-vertex primitive lists by ids so a region can cross a worker boundary
52
+ function serialiseLists(p) {
53
+ const uniq = new Map();
54
+ const listIndex = new Uint32Array(p.count);
55
+ const listIds = [];
56
+ for (let v = 0; v < p.count; v++) {
57
+ const l = p.lists[v];
58
+ let k = uniq.get(l);
59
+ if (k === undefined) { k = listIds.length; uniq.set(l, k); listIds.push(Int16Array.from(l.map((q) => q.id))); }
60
+ listIndex[v] = k;
61
+ }
62
+ return { name: p.name, positions: p.positions, normals: p.normals, indices: p.indices, count: p.count, listIndex, listIds, patch: p.patch || null };
63
+ }
64
+
65
+ export function regionTransferables(parts) {
66
+ return parts.flatMap((p) => [p.positions.buffer, p.normals.buffer, p.indices.buffer, p.listIndex.buffer]);
67
+ }
@@ -0,0 +1,272 @@
1
+ // The build pipeline: species data -> seeded individual -> SDF sculpt -> meshed regions -> skin
2
+ // weights -> coat -> proportion warp and size -> packed, transferable arrays.
3
+ //
4
+ // Every stage is deterministic in (species, seed, options), so a worker can redo `prepare` from the
5
+ // species id and seed instead of receiving functions.
6
+ import { SDFModel } from '../sdf/sdf.js';
7
+ import { meshRegion } from './mesh.js';
8
+ import { computeWeights } from './weights.js';
9
+ import { composeWarps, warpMesh, warpScaleAt } from './warp.js';
10
+ import { transformRig } from '../rig/rig.js';
11
+ import { rng } from '../math/vec.js';
12
+ import { eyeFrameOf } from '../sdf/eyeSocket.js';
13
+ import { limitFurSlope, FUR_SLOPE_DEFAULTS } from './furSlope.js';
14
+ import { measureSeams } from './seams.js';
15
+ import { sharpenFinEdges } from './finEdges.js';
16
+
17
+ export const FORMAT_VERSION = 1;
18
+
19
+ // Mesh resolution multiplier and render settings per quality tier.
20
+ export const QUALITY = {
21
+ hero: { res: 1.0, shells: 40, fins: true, eyePatches: true },
22
+ high: { res: 1.3, shells: 28, fins: true, eyePatches: true },
23
+ medium: { res: 2.0, shells: 16, fins: false, eyePatches: false },
24
+ low: { res: 3.0, shells: 8, fins: false, eyePatches: false },
25
+ crowd: { res: 4.2, shells: 0, fins: false, eyePatches: false },
26
+ };
27
+
28
+ export function normaliseOptions(opts = {}) {
29
+ const quality = QUALITY[opts.quality] ? opts.quality : 'high';
30
+ return { seed: (opts.seed ?? 1) >>> 0, quality, sex: opts.sex, age: opts.age, variant: opts.variant, overrides: opts.overrides || {} };
31
+ }
32
+
33
+ export function prepare(species, opts) {
34
+ const o = normaliseOptions(opts);
35
+ const R = rng(o.seed * 2654435761 + 12345);
36
+ const params = { ...species.variation(R, o), ...o.overrides };
37
+ params.seed = o.seed;
38
+ const rig = species.rig(params);
39
+ const model = new SDFModel();
40
+ species.sculpt(model, rig, params);
41
+ if (params.minThick) inflateThin(model, QUALITY[o.quality].res, params.minThick);
42
+ const Q = QUALITY[o.quality];
43
+ const reg = species.regions(rig, params, Q);
44
+ return { o, params, rig, model, reg, Q };
45
+ }
46
+
47
+ // primitives tagged thin (ears, tail tips) are inflated at coarse tiers so they do not vanish
48
+ function inflateThin(model, res, minThick) {
49
+ if (res < 1.9) return;
50
+ const t = minThick * res;
51
+ for (const p of model.prims) {
52
+ if (!p.thin || p.carve) continue;
53
+ if (p.type === 0) for (let i = 12; i < 15; i++) p.P[i] = Math.max(p.P[i], t);
54
+ if (p.type === 3) { p.P[12] = Math.max(p.P[12], t); p.P[13] = Math.max(p.P[13], Math.min(t, p.P[12])); }
55
+ }
56
+ }
57
+
58
+ export function meshJob(species, opts, jobIndex, prep = prepare(species, opts)) {
59
+ return prep.reg.jobs[jobIndex].map((R) => meshRegion(prep.model, R));
60
+ }
61
+
62
+ export function jobCount(species, opts) {
63
+ return prepare(species, opts).reg.jobs.length;
64
+ }
65
+
66
+ export function finish(species, opts, meshedJobs, prep = prepare(species, opts), onProgress = () => {}) {
67
+ const t0 = now();
68
+ const { rig, model, reg, params, o, Q } = prep;
69
+ const order = reg.jobs.flat().map((r) => r.name);
70
+ const regDef = Object.fromEntries(reg.jobs.flat().map((r) => [r.name, r]));
71
+ const parts = meshedJobs.flat().sort((a, b) => order.indexOf(a.name) - order.indexOf(b.name));
72
+ let nV = 0, nI = 0;
73
+ for (const p of parts) { p.v0 = nV; nV += p.count; nI += p.indices.length; }
74
+ let pos = new Float32Array(nV * 3), nrm = new Float32Array(nV * 3);
75
+ let index = new Uint32Array(nI);
76
+ const lists = new Array(nV);
77
+ const regionOf = new Uint8Array(nV);
78
+ const patchOf = new Uint8Array(nV);
79
+ const rigidBone = new Int16Array(nV).fill(-1);
80
+ let io = 0;
81
+ parts.forEach((p, pi) => {
82
+ pos.set(p.positions, p.v0 * 3);
83
+ nrm.set(p.normals, p.v0 * 3);
84
+ if (p.patch) patchOf.set(p.patch, p.v0);
85
+ for (let i = 0; i < p.indices.length; i++) index[io++] = p.indices[i] + p.v0;
86
+ const L = p.listIds.map((ids) => Array.from(ids, (id) => model.prims[id]));
87
+ const rb = regDef[p.name].rigidBone ? rig.BONE[regDef[p.name].rigidBone].index : -1;
88
+ for (let i = 0; i < p.count; i++) { lists[p.v0 + i] = L[p.listIndex[i]]; regionOf[p.v0 + i] = pi; rigidBone[p.v0 + i] = rb; }
89
+ });
90
+ const regionNames = parts.map((p) => p.name);
91
+ // fin slabs tagged `sharpen` get knife edges below the cell size (finEdges.js; opt-in)
92
+ if (model.prims.some((pr) => pr.sharpen)) sharpenFinEdges({ pos, nrm, index, lists, nV, regionOf }, parts.map((p) => regDef[p.name].h));
93
+ onProgress('weights');
94
+ const weights = computeWeights({ pos, nrm, index, lists, rigidBone, nV }, rig);
95
+ const t1 = now();
96
+ // cross-fade of overlapping regions
97
+ let fade = new Float32Array(nV).fill(1);
98
+ if (reg.fade) for (let v = 0; v < nV; v++) fade[v] = reg.fade(pos[v * 3], pos[v * 3 + 1], pos[v * 3 + 2], regionNames[regionOf[v]], patchOf[v]);
99
+ // how far the overlapping surfaces lie apart at their hand-over: the depth of the renderer's seam
100
+ // skirt (seams.js)
101
+ const seams = measureSeams({ pos, nrm, index, nV, fade, regionOf, patchOf, hOf: parts.map((p) => regDef[p.name].h) });
102
+ onProgress('coat');
103
+ const coat = species.coat({ model, rig, params, pos, nrm, index, lists, regionOf, regionNames, patchOf, rigidBone, weights, nV, fade, rng: rng(o.seed * 7919 + 17), quality: o.quality, Q });
104
+ // (species.render may be a function of the individual: a mark colour that follows the morph)
105
+ const hints = (typeof species.render === 'function' ? species.render(params) : species.render) || {};
106
+ // no fur walls: hair may not rise faster than the render hint's slope above the hair around it
107
+ // (furSlope.js; the crowd tier draws no shells)
108
+ let furSlope = null;
109
+ if (Q.shells > 0 && coat.furLen && coat.tint && hints.furSlope !== false) {
110
+ furSlope = limitFurSlope(coat.furLen, pos, index, nV, coat.tint, {
111
+ slope: hints.furSlope ?? FUR_SLOPE_DEFAULTS.slope, free: hints.furSlopeFree ?? FUR_SLOPE_DEFAULTS.free, perVertex: coat.furSlope || null,
112
+ });
113
+ }
114
+ if (Q.shells <= 0) prefilterPattern(coat, index, nV, pos, hints.markColor || [0.016, 0.0105, 0.0075]);
115
+ const t2 = now();
116
+
117
+ // ---- extra (non-SDF) surfaces: feather cards etc., rigidly skinned, with their own attributes
118
+ const extra = species.surfaces ? species.surfaces({ rig, params, Q, quality: o.quality, model }) : null;
119
+ let skinIndex = weights.skinIndex, skinWeight = weights.skinWeight;
120
+ if (extra && extra.nV) {
121
+ const n0 = nV, n1 = nV + extra.nV;
122
+ const cat = (a, b, k, T = Float32Array) => { const r = new T(n1 * k); r.set(a.subarray(0, n0 * k)); r.set(b, n0 * k); return r; };
123
+ const idx = new Uint32Array(index.length + extra.index.length);
124
+ idx.set(index); for (let i = 0; i < extra.index.length; i++) idx[index.length + i] = extra.index[i] + n0;
125
+ pos = cat(pos, extra.pos, 3); nrm = cat(nrm, extra.nrm, 3); index = idx;
126
+ skinIndex = cat(skinIndex, extra.skinIndex, 4, Uint16Array); skinWeight = cat(skinWeight, extra.skinWeight, 4);
127
+ coat.comb = cat(coat.comb, extra.comb, 3); coat.tint = cat(coat.tint, extra.tint, 4);
128
+ coat.pattern = cat(coat.pattern || new Float32Array(n0).fill(1), extra.pattern, 1);
129
+ coat.mark = cat(coat.mark || new Float32Array(n0).fill(1), extra.mark, 1);
130
+ coat.furLen = cat(coat.furLen || new Float32Array(n0), extra.furLen, 1);
131
+ coat.patternColor = cat(coat.patternColor || constant4(n0, [0.016, 0.0105, 0.0075, 1]), extra.patternColor, 4);
132
+ coat.surf = cat(coat.surf || new Float32Array(n0 * 4), extra.surf, 4);
133
+ fade = cat(fade, new Float32Array(extra.nV).fill(1), 1);
134
+ nV = n1; nI = index.length;
135
+ }
136
+
137
+ // ---- proportion warps and size, applied to the finished surface and the skeleton
138
+ const size = params.size || 1;
139
+ const warps = [...(params.warps || [])];
140
+ if (size !== 1) warps.push({ type: 'scale', k: size });
141
+ const W = composeWarps(warps);
142
+ let rigOut = rig;
143
+ let eyes = (species.eyeSpecs ? species.eyeSpecs(params, rig) : []).map((e) => {
144
+ const f = eyeFrameOf(e.spec, e.headOrigin, e.side);
145
+ return { ...e, frame: f, scale: 1 };
146
+ });
147
+ const scaleLen = (arr, k) => { if (arr && k !== 1) for (let i = 0; i < arr.length; i++) arr[i] *= k; };
148
+ if (W) {
149
+ warpMesh(pos, nrm, W);
150
+ rigOut = transformRig(rig, W);
151
+ eyes = eyes.map((e) => ({ ...e, frame: { ...e.frame, c: W(e.frame.c) }, scale: warpScaleAt(W, e.frame.c) }));
152
+ scaleLen(coat.pattern, size);
153
+ scaleLen(coat.mark, size);
154
+ scaleLen(coat.furLen, Math.pow(size, 0.35));
155
+ }
156
+
157
+ const out = {
158
+ version: FORMAT_VERSION,
159
+ species: species.id,
160
+ seed: o.seed,
161
+ quality: o.quality,
162
+ params: jsonSafe(params),
163
+ nV,
164
+ pos, nrm, index,
165
+ skinIndex,
166
+ skinWeight,
167
+ comb: coat.comb,
168
+ tint: coat.tint,
169
+ coat: interleave(nV, coat.pattern, coat.mark, coat.furLen, fade),
170
+ pat: coat.patternColor || constant4(nV, [0.016, 0.0105, 0.0075, 1]),
171
+ surf: coat.surf || new Float32Array(nV * 4),
172
+ // seam skirt depth (m, final size; the warps are within ~15 %): coatMaterial sinks the handed-over
173
+ // half of every cross-fade band by up to this much under the surface taking over
174
+ seamSink: +(seams.sink * size * 1.15).toFixed(6),
175
+ joints: rigOut.J,
176
+ bones: rigOut.BONES.map((b) => ({ name: b.name, parent: b.parent, headJ: b.headJ, tailJ: b.tailJ, region: b.region, group: b.group })),
177
+ refJoints: rig.J,
178
+ eyes: eyes.map((e) => ({ side: e.side, bone: e.bone || 'head', c: e.frame.c, x: e.frame.x, y: e.frame.y, z: e.frame.z, scale: e.scale * (size !== 1 && !W ? size : 1), spec: e.spec, look: e.look || {} })),
179
+ prims: model.prims.map((p) => ({ type: p.type, P: Array.from(p.P), bone: p.bone, group: p.group, part: p.part, carve: p.carve, tag: p.tag, k: p.k })),
180
+ stats: {
181
+ nV, tris: nI / 3, prims: model.prims.length, bones: rig.BONES.length, weightsMs: Math.round(t1 - t0), coatMs: Math.round(t2 - t1),
182
+ ...(furSlope ? { furSlopeShortened: furSlope.shortened, furSlopeMaxCutMm: +(furSlope.maxCut * 1000).toFixed(1) } : {}),
183
+ ...(seams.samples ? { seamMaxMm: +(seams.max * 1000).toFixed(2), seamSinkMm: +(seams.sink * 1000).toFixed(2) } : {}),
184
+ ...(coat.stats || {}),
185
+ },
186
+ };
187
+ return out;
188
+ }
189
+
190
+ export function buildSync(species, opts, onProgress = () => {}) {
191
+ const t0 = now();
192
+ const prep = prepare(species, opts);
193
+ const jobs = prep.reg.jobs.map((_, i) => { onProgress('mesh ' + i); return meshJob(species, opts, i, prep); });
194
+ const d = finish(species, opts, jobs, prep, onProgress);
195
+ d.stats.buildMs = Math.round(now() - t0);
196
+ return d;
197
+ }
198
+
199
+ const now = () => (typeof performance !== 'undefined' ? performance.now() : Date.now());
200
+
201
+ // Crowd tier (no shells, ~2 cm cells): the pattern and mark fields are signed distances sampled per
202
+ // vertex, and features are about the size of a cell (spot centres even sit on vertices), so the
203
+ // crisp zero level aliases: dense small spots (paws, lower legs) cover every vertex and turn whole
204
+ // paws black, spots become polygons, a tail tip ring swallows the white tip. Prefilter like a mip
205
+ // level: per vertex, the fraction of its surrounding triangles the (linearly interpolated) field
206
+ // covers. Spots are then drawn as that coverage (pattern inside everywhere, the pattern colour's
207
+ // intensity scaled by the coverage: soft blobs of the right size and tone); crisp marks (tear lines,
208
+ // tail rings) are mixed into the base colour by their coverage.
209
+ function prefilterPattern(coat, index, nV, pos, markColor) {
210
+ // per vertex: area-weighted fraction of the incident triangles the linear field covers, capped by
211
+ // the size of the features around it (a vertex on a 4 mm spot centre, with neighbours on other
212
+ // small spots, reads fully covered although the spots cover ~15 % of its area): the deepest
213
+ // inside value in its 1-ring is taken as the local feature radius r, coverage <= (r / (h / 2))^2
214
+ const coverage = (f) => {
215
+ const acc = new Float64Array(nV), wsum = new Float64Array(nV), deep = new Float64Array(nV), hsum = new Float64Array(nV), hn = new Float64Array(nV);
216
+ for (let i = 0; i < index.length; i += 3) {
217
+ const a = index[i], b = index[i + 1], c = index[i + 2];
218
+ const ux = pos[b * 3] - pos[a * 3], uy = pos[b * 3 + 1] - pos[a * 3 + 1], uz = pos[b * 3 + 2] - pos[a * 3 + 2];
219
+ const vx = pos[c * 3] - pos[a * 3], vy = pos[c * 3 + 1] - pos[a * 3 + 1], vz = pos[c * 3 + 2] - pos[a * 3 + 2];
220
+ const area = 0.5 * Math.hypot(uy * vz - uz * vy, uz * vx - ux * vz, ux * vy - uy * vx);
221
+ const fr = negFraction(f[a], f[b], f[c]);
222
+ const d = Math.max(0, -f[a], -f[b], -f[c]), h = Math.hypot(ux, uy, uz) + Math.hypot(vx, vy, vz);
223
+ for (const v of [a, b, c]) { acc[v] += area * fr; wsum[v] += area; deep[v] = Math.max(deep[v], d); hsum[v] += h; hn[v] += 2; }
224
+ }
225
+ return Float32Array.from(acc, (x, v) => {
226
+ if (!(wsum[v] > 0)) return 0;
227
+ const h = hsum[v] / hn[v];
228
+ return Math.min(x / wsum[v], Math.min(1, (deep[v] / (0.5 * h)) ** 2));
229
+ });
230
+ };
231
+ if (coat.pattern) {
232
+ const cov = coverage(coat.pattern);
233
+ const pc = coat.patternColor || (coat.patternColor = new Float32Array(nV * 4).map((_, i) => [0.016, 0.0105, 0.0075, 1][i % 4]));
234
+ // (a contrast curve keeps the blobs reading as spots rather than a stain)
235
+ for (let v = 0; v < nV; v++) { const k = Math.min(1, Math.max(0, (cov[v] - 0.12) / 0.43)); pc[v * 4 + 3] *= k * k * (3 - 2 * k); coat.pattern[v] = -1; }
236
+ }
237
+ if (coat.mark && coat.tint) {
238
+ // crisp marks have no intensity channel: their coverage goes into the base colour instead
239
+ const cov = coverage(coat.mark), t = coat.tint;
240
+ for (let v = 0; v < nV; v++) {
241
+ const k = cov[v];
242
+ for (let i = 0; i < 3; i++) t[v * 4 + i] += (markColor[i] - t[v * 4 + i]) * k;
243
+ coat.mark[v] = 1;
244
+ }
245
+ }
246
+ }
247
+ // area fraction of a triangle where the linear interpolation of the vertex values is negative
248
+ function negFraction(a, b, c) {
249
+ const neg = (a < 0) + (b < 0) + (c < 0);
250
+ if (neg === 0) return 0;
251
+ if (neg === 3) return 1;
252
+ let n, p1, p2;
253
+ if (neg === 1) { [n, p1, p2] = a < 0 ? [a, b, c] : b < 0 ? [b, a, c] : [c, a, b]; return (n * n) / ((n - p1) * (n - p2)); }
254
+ [n, p1, p2] = a >= 0 ? [a, b, c] : b >= 0 ? [b, a, c] : [c, a, b]; // n: the single non-negative one
255
+ return 1 - (n * n) / ((n - p1) * (n - p2));
256
+ }
257
+
258
+ function interleave(n, a, b, c, d) {
259
+ const out = new Float32Array(n * 4);
260
+ for (let v = 0; v < n; v++) { out[v * 4] = a ? a[v] : 1; out[v * 4 + 1] = b ? b[v] : 1; out[v * 4 + 2] = c ? c[v] : 0; out[v * 4 + 3] = d ? d[v] : 1; }
261
+ return out;
262
+ }
263
+ function constant4(n, c) {
264
+ const out = new Float32Array(n * 4);
265
+ for (let v = 0; v < n; v++) out.set(c, v * 4);
266
+ return out;
267
+ }
268
+ function jsonSafe(o) { return JSON.parse(JSON.stringify(o, (k, v) => (typeof v === 'function' ? undefined : v))); }
269
+
270
+ // Arrays that can be transferred from a worker (or written to a bake file).
271
+ export const ARRAY_KEYS = ['pos', 'nrm', 'index', 'skinIndex', 'skinWeight', 'comb', 'tint', 'coat', 'pat', 'surf'];
272
+ export function transferables(d) { return ARRAY_KEYS.map((k) => d[k].buffer); }
@@ -0,0 +1,183 @@
1
+ // Cross-fade seams: how far two overlapping surfaces lie apart where they hand over.
2
+ //
3
+ // Regions overlap and cross-fade (a fine head over the coarse neck, eyelid patches over the face):
4
+ // the base of each surface is drawn where its fade >= 0.5, the two halves meeting at the midline of
5
+ // the band. The surfaces approximate the same SDF at different cell sizes, so at the midline they do
6
+ // not coincide: the coarse one bridges creases and cuts chords inside convex skin, by up to ~0.4 of
7
+ // its cell in a crease. Seen at a grazing angle the step between them is a thin slit into the body
8
+ // (a red line in the 'holes' debug view).
9
+ //
10
+ // The renderer closes it with a skirt (render/coatMaterial.js): past the midline, down to fade 0.1,
11
+ // the base sinks along its normal (quadratically, by up to `sink` metres) instead of stopping, so the
12
+ // surface that is handing over dives under the one taking over (which wins the depth test) and every
13
+ // ray through the step lands on skin. `measureSeams` sizes the sink from the real mismatch of this individual's
14
+ // surfaces (bind pose, reference space): rays along the normal of every band vertex against the
15
+ // other surfaces' band triangles. Shells and fins keep cross-fading on the true surfaces.
16
+ import { smoothstep } from '../math/vec.js';
17
+
18
+ // groups: a region's main surface and its eyelid patches are separate surfaces
19
+ const groupOf = (regionOf, patchOf, v) => regionOf[v] * 2 + (patchOf && patchOf[v] ? 1 : 0);
20
+
21
+ /**
22
+ * @param {object} m { pos, nrm, index, nV, fade, regionOf, patchOf, hOf: region index -> cell size (m) }
23
+ * @returns {{ sink: number, max: number, p99: number, samples: number, hBand: number }} metres (reference space)
24
+ */
25
+ export function measureSeams({ pos, nrm, index, nV, fade, regionOf, patchOf, hOf }) {
26
+ const res = { sink: 0, max: 0, p99: 0, samples: 0, hBand: 0 };
27
+ // band triangles (some vertex fades, some vertex shows) and the coarsest cell of a banded region
28
+ const tri = [];
29
+ let hBand = 0;
30
+ for (let i = 0; i < index.length; i += 3) {
31
+ const a = index[i], b = index[i + 1], c = index[i + 2];
32
+ if (a >= nV || b >= nV || c >= nV) continue;
33
+ const fm = Math.min(fade[a], fade[b], fade[c]), fM = Math.max(fade[a], fade[b], fade[c]);
34
+ if (fM <= 0.02 || fm >= 0.98) continue;
35
+ tri.push(i);
36
+ hBand = Math.max(hBand, hOf[regionOf[a]] || 0);
37
+ }
38
+ if (!tri.length || !(hBand > 0)) return res;
39
+ res.hBand = hBand;
40
+ const range = 0.75 * hBand; // search depth along the normal
41
+ // hash grid of band triangles
42
+ const G = Math.max(range, 0.004), grid = new Map();
43
+ const key = (x, y, z) => (Math.floor(x / G) + 4096) * 67108864 + (Math.floor(y / G) + 4096) * 8192 + (Math.floor(z / G) + 4096);
44
+ for (let t = 0; t < tri.length; t++) {
45
+ const i = tri[t], a = index[i], b = index[i + 1], c = index[i + 2];
46
+ const x0 = Math.floor(Math.min(pos[a * 3], pos[b * 3], pos[c * 3]) / G), x1 = Math.floor(Math.max(pos[a * 3], pos[b * 3], pos[c * 3]) / G);
47
+ const y0 = Math.floor(Math.min(pos[a * 3 + 1], pos[b * 3 + 1], pos[c * 3 + 1]) / G), y1 = Math.floor(Math.max(pos[a * 3 + 1], pos[b * 3 + 1], pos[c * 3 + 1]) / G);
48
+ const z0 = Math.floor(Math.min(pos[a * 3 + 2], pos[b * 3 + 2], pos[c * 3 + 2]) / G), z1 = Math.floor(Math.max(pos[a * 3 + 2], pos[b * 3 + 2], pos[c * 3 + 2]) / G);
49
+ for (let x = x0; x <= x1; x++) for (let y = y0; y <= y1; y++) for (let z = z0; z <= z1; z++) {
50
+ const k = (x + 4096) * 67108864 + (y + 4096) * 8192 + (z + 4096);
51
+ let l = grid.get(k);
52
+ if (!l) grid.set(k, (l = []));
53
+ l.push(t);
54
+ }
55
+ }
56
+ const offs = [];
57
+ const seen = new Set();
58
+ for (let v = 0; v < nV; v++) {
59
+ const f = fade[v];
60
+ if (!(f > 0.3 && f < 0.7)) continue;
61
+ const g = groupOf(regionOf, patchOf, v);
62
+ const ox = pos[v * 3], oy = pos[v * 3 + 1], oz = pos[v * 3 + 2], nx = nrm[v * 3], ny = nrm[v * 3 + 1], nz = nrm[v * 3 + 2];
63
+ seen.clear();
64
+ let best = Infinity;
65
+ for (let s = -1; s <= 1.001; s += 0.25) {
66
+ const k = key(ox + nx * s * range, oy + ny * s * range, oz + nz * s * range);
67
+ const l = grid.get(k);
68
+ if (!l) continue;
69
+ for (const t of l) {
70
+ if (seen.has(t)) continue;
71
+ seen.add(t);
72
+ const i = tri[t], a = index[i], b = index[i + 1], c = index[i + 2];
73
+ if (groupOf(regionOf, patchOf, a) === g) continue;
74
+ // Moller-Trumbore, both directions along the normal
75
+ const e1x = pos[b * 3] - pos[a * 3], e1y = pos[b * 3 + 1] - pos[a * 3 + 1], e1z = pos[b * 3 + 2] - pos[a * 3 + 2];
76
+ const e2x = pos[c * 3] - pos[a * 3], e2y = pos[c * 3 + 1] - pos[a * 3 + 1], e2z = pos[c * 3 + 2] - pos[a * 3 + 2];
77
+ const px = ny * e2z - nz * e2y, py = nz * e2x - nx * e2z, pz = nx * e2y - ny * e2x;
78
+ const det = e1x * px + e1y * py + e1z * pz;
79
+ if (Math.abs(det) < 1e-14) continue;
80
+ const tx = ox - pos[a * 3], ty = oy - pos[a * 3 + 1], tz = oz - pos[a * 3 + 2];
81
+ const u = (tx * px + ty * py + tz * pz) / det;
82
+ if (u < 0 || u > 1) continue;
83
+ const qx = ty * e1z - tz * e1y, qy = tz * e1x - tx * e1z, qz = tx * e1y - ty * e1x;
84
+ const w = (nx * qx + ny * qy + nz * qz) / det;
85
+ if (w < 0 || u + w > 1) continue;
86
+ const d = (e2x * qx + e2y * qy + e2z * qz) / det;
87
+ if (Math.abs(d) > range || Math.abs(d) >= Math.abs(best)) continue;
88
+ // the surface taking over here: its fade complements this one (not the far side of a thin part)
89
+ const fo = fade[a] * (1 - u - w) + fade[b] * u + fade[c] * w;
90
+ if (Math.abs(f + fo - 1) > 0.3) continue;
91
+ best = d;
92
+ }
93
+ }
94
+ if (Number.isFinite(best)) offs.push(Math.abs(best));
95
+ }
96
+ res.samples = offs.length;
97
+ if (!offs.length) { res.sink = 0.1 * hBand; return res; }
98
+ offs.sort((x, y) => x - y);
99
+ res.max = offs[offs.length - 1];
100
+ res.p99 = offs[Math.min(offs.length - 1, Math.floor(0.99 * offs.length))];
101
+ // cover the creases (99th percentile: a few hits land on another fold of the skin) with room for
102
+ // skinning differences in motion; never deeper than 0.4 of the coarse cell
103
+ res.sink = Math.min(0.4 * hBand, Math.max(0.1 * hBand, 1.5 * res.p99));
104
+ return res;
105
+ }
106
+
107
+ // The head and the body surfaces of a region plan overlap across a cut and cross-fade there, and the core smooths each
108
+ // surface's skin weights over its own mesh, so where the weights curve the two surfaces at one place take bone shares a
109
+ // few per cent apart. Posed (the head bent against the neck), they part by millimetres, and the fur rooted on the
110
+ // surface lying under the other's skin loses its roots: a crisp edge across the coat (a cat's cheek, a dog's throat).
111
+ // The body surface near the cut takes the head surface's weights at the same place (the closest point on the finer head
112
+ // mesh, barycentric), in full across the cross-fade band and fading back to its own over the next 8 mm on the body side.
113
+ // sideOf(x, y, z): signed distance from the cut (> 0 on the head side); band: the cross-fade half-width. Called from a
114
+ // species' coat (the core passes it the weights and uses them after it).
115
+ export function harmonizeSeamWeights({ pos, index, regionOf, regionNames, weights, nV }, sideOf, band) {
116
+ const { skinIndex, skinWeight } = weights;
117
+ const bodyR = regionNames.indexOf('body'), headR = regionNames.indexOf('head');
118
+ if (bodyR < 0 || headR < 0) return 0;
119
+ const B = band, R = B + 0.008, C = 0.004;
120
+ const sOf = (v) => sideOf(pos[v * 3], pos[v * 3 + 1], pos[v * 3 + 2]);
121
+ // head triangles near the cut, in a hash grid by centroid
122
+ const grid = new Map(), key = (i, j, k) => `${i},${j},${k}`;
123
+ for (let t = 0; t < index.length; t += 3) {
124
+ const a = index[t], b = index[t + 1], c = index[t + 2];
125
+ if (a >= nV || regionOf[a] !== headR || regionOf[b] !== headR || regionOf[c] !== headR) continue;
126
+ if (Math.abs(sOf(a)) > R + 0.006) continue;
127
+ const g = [0, 1, 2].map((k) => Math.floor((pos[a * 3 + k] + pos[b * 3 + k] + pos[c * 3 + k]) / 3 / C));
128
+ const kk = key(...g); let l = grid.get(kk); if (!l) grid.set(kk, (l = [])); l.push(t);
129
+ }
130
+ const W0 = Float32Array.from(skinWeight), I0 = Uint16Array.from(skinIndex);
131
+ const P3 = (v) => [pos[v * 3], pos[v * 3 + 1], pos[v * 3 + 2]];
132
+ let changed = 0;
133
+ for (let v = 0; v < nV; v++) {
134
+ if (regionOf[v] !== bodyR) continue;
135
+ const s = sOf(v);
136
+ if (s < -R || s > R) continue;
137
+ const kMix = s > -B ? 1 : smoothstep(-R, -B, s);
138
+ if (kMix <= 0) continue;
139
+ const p = P3(v), g = p.map((x) => Math.floor(x / C));
140
+ let best = null, bd = 0.004;
141
+ for (let a = -1; a <= 1; a++) for (let b = -1; b <= 1; b++) for (let c = -1; c <= 1; c++) {
142
+ const l = grid.get(key(g[0] + a, g[1] + b, g[2] + c)); if (!l) continue;
143
+ for (const t of l) {
144
+ const q = closestOnTri(p, P3(index[t]), P3(index[t + 1]), P3(index[t + 2]));
145
+ if (q.d < bd) { bd = q.d; best = [t, q.u, q.v, q.w]; }
146
+ }
147
+ }
148
+ if (!best) continue;
149
+ const acc = new Map();
150
+ const add = (u, f) => { for (let k = 0; k < 4; k++) { const w = W0[u * 4 + k]; if (w > 0) acc.set(I0[u * 4 + k], (acc.get(I0[u * 4 + k]) || 0) + w * f); } };
151
+ add(v, 1 - kMix);
152
+ add(index[best[0]], kMix * best[1]); add(index[best[0] + 1], kMix * best[2]); add(index[best[0] + 2], kMix * best[3]);
153
+ const top = [...acc.entries()].sort((x, y) => y[1] - x[1]).slice(0, 4);
154
+ let sum = 0; for (const [, w] of top) sum += w;
155
+ for (let k = 0; k < 4; k++) {
156
+ if (k < top.length) { skinIndex[v * 4 + k] = top[k][0]; skinWeight[v * 4 + k] = top[k][1] / sum; } else { skinIndex[v * 4 + k] = 0; skinWeight[v * 4 + k] = 0; }
157
+ }
158
+ changed++;
159
+ }
160
+ return changed;
161
+ }
162
+
163
+ // closest point on triangle abc to p: distance and barycentric weights (Ericson, Real-Time Collision Detection 5.1.5)
164
+ function closestOnTri(p, a, b, c) {
165
+ const sub3 = (x, y) => [x[0] - y[0], x[1] - y[1], x[2] - y[2]], dot3 = (x, y) => x[0] * y[0] + x[1] * y[1] + x[2] * y[2];
166
+ const ab = sub3(b, a), ac = sub3(c, a), ap = sub3(p, a);
167
+ const d1 = dot3(ab, ap), d2 = dot3(ac, ap);
168
+ let u, v, w;
169
+ if (d1 <= 0 && d2 <= 0) { u = 1; v = 0; w = 0; } else {
170
+ const bp = sub3(p, b), d3 = dot3(ab, bp), d4 = dot3(ac, bp);
171
+ const cp = sub3(p, c), d5 = dot3(ab, cp), d6 = dot3(ac, cp);
172
+ const vc = d1 * d4 - d3 * d2, vb = d5 * d2 - d1 * d6, va = d3 * d6 - d5 * d4;
173
+ if (d3 >= 0 && d4 <= d3) { u = 0; v = 1; w = 0; }
174
+ else if (d6 >= 0 && d5 <= d6) { u = 0; v = 0; w = 1; }
175
+ else if (vc <= 0 && d1 >= 0 && d3 <= 0) { const t = d1 / (d1 - d3); u = 1 - t; v = t; w = 0; }
176
+ else if (vb <= 0 && d2 >= 0 && d6 <= 0) { const t = d2 / (d2 - d6); u = 1 - t; v = 0; w = t; }
177
+ else if (va <= 0 && d4 - d3 >= 0 && d5 - d6 >= 0) { const t = (d4 - d3) / (d4 - d3 + (d5 - d6)); u = 0; v = 1 - t; w = t; }
178
+ else { const den = 1 / (va + vb + vc); v = vb * den; w = vc * den; u = 1 - v - w; }
179
+ }
180
+ const q = [0, 1, 2].map((k) => a[k] * u + b[k] * v + c[k] * w);
181
+ return { d: Math.hypot(p[0] - q[0], p[1] - q[1], p[2] - q[2]), u, v, w };
182
+ }
183
+