@bitefoundry/retrieviewer 0.0.2 → 0.0.4

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 (261) hide show
  1. package/ASSET-PROVENANCE.md +6 -0
  2. package/ASSETS-LICENSE.md +2 -1
  3. package/ATTRIBUTION.md +6 -1
  4. package/COMFORT-ART.md +65 -0
  5. package/DESKTOP-PLATFORMS.md +75 -0
  6. package/INSTALLING.md +8 -5
  7. package/PLUGIN.md +13 -10
  8. package/PREREQUISITES.md +95 -0
  9. package/PUPPY-MEMORY.md +102 -1
  10. package/README.ko.md +110 -0
  11. package/README.md +119 -58
  12. package/RELEASING.md +27 -14
  13. package/THIRD-PARTY-NOTICES.md +5 -3
  14. package/braille.mjs +35 -9
  15. package/examples/live.mjs +2 -2
  16. package/integrations/desktop/platform.mjs +27 -0
  17. package/integrations/linux-overlay/README.md +83 -0
  18. package/integrations/linux-overlay/overlay.py +1094 -0
  19. package/integrations/macos-overlay/README.md +83 -0
  20. package/integrations/macos-overlay/overlay.jxa +639 -0
  21. package/integrations/opencode/runtime.mjs +4 -4
  22. package/integrations/opencode/tui.tsx +1 -1
  23. package/integrations/windows-overlay/Overlay.cs +4 -4
  24. package/integrations/windows-overlay/desktop.mjs +40 -8
  25. package/integrations/windows-overlay/protocol.mjs +11 -7
  26. package/integrations/windows-overlay/worker.mjs +13 -7
  27. package/package.json +41 -14
  28. package/puppy-comfort-sprites.mjs +25 -0
  29. package/puppy-companion-comfort-q5.bin.gz +0 -0
  30. package/puppy-companion-comfort.json +77 -0
  31. package/puppy-companion-life.mjs +49 -16
  32. package/puppy-companion-objects.mjs +12 -0
  33. package/puppy-frame-predictive.mjs +15 -9
  34. package/puppy-frame-store.mjs +2 -2
  35. package/puppy-paper-world.mjs +14 -0
  36. package/puppy-paper.mjs +65 -14
  37. package/retrieviewer-cli.mjs +10 -3
  38. package/retrieviewer-plugin.d.mts +1 -1
  39. package/retrieviewer-plugin.mjs +1 -1
  40. package/retrieviewer.d.mts +10 -4
  41. package/retrieviewer.mjs +20 -14
  42. package/locomotion-core.mjs +0 -21
  43. package/locomotion-puppy.animal +0 -0
  44. package/vendor/procedural-animals/package.json +0 -40
  45. package/vendor/procedural-animals/src/animal.js +0 -146
  46. package/vendor/procedural-animals/src/bake.js +0 -51
  47. package/vendor/procedural-animals/src/build.js +0 -81
  48. package/vendor/procedural-animals/src/core/build/coatKit.js +0 -121
  49. package/vendor/procedural-animals/src/core/build/featherCards.js +0 -189
  50. package/vendor/procedural-animals/src/core/build/finEdges.js +0 -85
  51. package/vendor/procedural-animals/src/core/build/furSlope.js +0 -115
  52. package/vendor/procedural-animals/src/core/build/mesh.js +0 -67
  53. package/vendor/procedural-animals/src/core/build/pipeline.js +0 -272
  54. package/vendor/procedural-animals/src/core/build/seams.js +0 -183
  55. package/vendor/procedural-animals/src/core/build/warp.js +0 -112
  56. package/vendor/procedural-animals/src/core/build/weights.js +0 -393
  57. package/vendor/procedural-animals/src/core/math/vec.js +0 -81
  58. package/vendor/procedural-animals/src/core/motion/actions.js +0 -800
  59. package/vendor/procedural-animals/src/core/motion/bird.js +0 -3063
  60. package/vendor/procedural-animals/src/core/motion/birdActions.js +0 -1059
  61. package/vendor/procedural-animals/src/core/motion/common.js +0 -158
  62. package/vendor/procedural-animals/src/core/motion/index.js +0 -22
  63. package/vendor/procedural-animals/src/core/motion/quadruped.js +0 -2875
  64. package/vendor/procedural-animals/src/core/motion/snake.js +0 -1594
  65. package/vendor/procedural-animals/src/core/motion/spider.js +0 -1810
  66. package/vendor/procedural-animals/src/core/motion/swimmer.js +0 -1047
  67. package/vendor/procedural-animals/src/core/motion/swimmerActions.js +0 -613
  68. package/vendor/procedural-animals/src/core/render/animalObject.js +0 -262
  69. package/vendor/procedural-animals/src/core/render/coatMaterial.js +0 -1301
  70. package/vendor/procedural-animals/src/core/render/dqs.js +0 -159
  71. package/vendor/procedural-animals/src/core/render/eyes.js +0 -676
  72. package/vendor/procedural-animals/src/core/render/membranes.js +0 -77
  73. package/vendor/procedural-animals/src/core/render/skeleton.js +0 -41
  74. package/vendor/procedural-animals/src/core/rig/bird.js +0 -327
  75. package/vendor/procedural-animals/src/core/rig/quadruped.js +0 -76
  76. package/vendor/procedural-animals/src/core/rig/rig.js +0 -79
  77. package/vendor/procedural-animals/src/core/rig/snake.js +0 -76
  78. package/vendor/procedural-animals/src/core/rig/spider.js +0 -110
  79. package/vendor/procedural-animals/src/core/rig/swimmer.js +0 -181
  80. package/vendor/procedural-animals/src/core/sdf/eyeSocket.js +0 -53
  81. package/vendor/procedural-animals/src/core/sdf/mesher.js +0 -238
  82. package/vendor/procedural-animals/src/core/sdf/sdf.js +0 -263
  83. package/vendor/procedural-animals/src/index.d.ts +0 -98
  84. package/vendor/procedural-animals/src/index.js +0 -29
  85. package/vendor/procedural-animals/src/registry.js +0 -48
  86. package/vendor/procedural-animals/src/species/bear/behaviour.js +0 -463
  87. package/vendor/procedural-animals/src/species/bear/claws.js +0 -147
  88. package/vendor/procedural-animals/src/species/bear/coat.js +0 -726
  89. package/vendor/procedural-animals/src/species/bear/index.js +0 -139
  90. package/vendor/procedural-animals/src/species/bear/motion.js +0 -112
  91. package/vendor/procedural-animals/src/species/bear/regions.js +0 -78
  92. package/vendor/procedural-animals/src/species/bear/rig.js +0 -112
  93. package/vendor/procedural-animals/src/species/bear/sculpt.js +0 -272
  94. package/vendor/procedural-animals/src/species/boar/behaviour.js +0 -335
  95. package/vendor/procedural-animals/src/species/boar/coat.js +0 -372
  96. package/vendor/procedural-animals/src/species/boar/index.js +0 -110
  97. package/vendor/procedural-animals/src/species/boar/motion.js +0 -85
  98. package/vendor/procedural-animals/src/species/boar/regions.js +0 -88
  99. package/vendor/procedural-animals/src/species/boar/rig.js +0 -111
  100. package/vendor/procedural-animals/src/species/boar/sculpt.js +0 -285
  101. package/vendor/procedural-animals/src/species/cat/behaviour.js +0 -559
  102. package/vendor/procedural-animals/src/species/cat/coat.js +0 -841
  103. package/vendor/procedural-animals/src/species/cat/index.js +0 -211
  104. package/vendor/procedural-animals/src/species/cat/motion.js +0 -101
  105. package/vendor/procedural-animals/src/species/cat/regions.js +0 -82
  106. package/vendor/procedural-animals/src/species/cat/rig.js +0 -100
  107. package/vendor/procedural-animals/src/species/cat/sculpt.js +0 -266
  108. package/vendor/procedural-animals/src/species/cat/whiskers.js +0 -110
  109. package/vendor/procedural-animals/src/species/cheetah/coat.js +0 -427
  110. package/vendor/procedural-animals/src/species/cheetah/index.js +0 -51
  111. package/vendor/procedural-animals/src/species/cheetah/joints.js +0 -3
  112. package/vendor/procedural-animals/src/species/cheetah/motion.js +0 -77
  113. package/vendor/procedural-animals/src/species/cheetah/regions.js +0 -95
  114. package/vendor/procedural-animals/src/species/cheetah/rig.js +0 -66
  115. package/vendor/procedural-animals/src/species/cheetah/sculpt.js +0 -158
  116. package/vendor/procedural-animals/src/species/chicken/coat.js +0 -313
  117. package/vendor/procedural-animals/src/species/chicken/coverts.js +0 -320
  118. package/vendor/procedural-animals/src/species/chicken/index.js +0 -110
  119. package/vendor/procedural-animals/src/species/chicken/motion.js +0 -177
  120. package/vendor/procedural-animals/src/species/chicken/regions.js +0 -66
  121. package/vendor/procedural-animals/src/species/chicken/rig.js +0 -144
  122. package/vendor/procedural-animals/src/species/chicken/sculpt.js +0 -389
  123. package/vendor/procedural-animals/src/species/chicken/wingLook.js +0 -66
  124. package/vendor/procedural-animals/src/species/chicken/wingStack.js +0 -124
  125. package/vendor/procedural-animals/src/species/cow/behaviour.js +0 -521
  126. package/vendor/procedural-animals/src/species/cow/coat.js +0 -465
  127. package/vendor/procedural-animals/src/species/cow/index.js +0 -118
  128. package/vendor/procedural-animals/src/species/cow/motion.js +0 -81
  129. package/vendor/procedural-animals/src/species/cow/regions.js +0 -98
  130. package/vendor/procedural-animals/src/species/cow/rig.js +0 -116
  131. package/vendor/procedural-animals/src/species/cow/sculpt.js +0 -339
  132. package/vendor/procedural-animals/src/species/crow/coat.js +0 -145
  133. package/vendor/procedural-animals/src/species/crow/index.js +0 -70
  134. package/vendor/procedural-animals/src/species/crow/joints.js +0 -3
  135. package/vendor/procedural-animals/src/species/crow/motion.js +0 -101
  136. package/vendor/procedural-animals/src/species/crow/regions.js +0 -50
  137. package/vendor/procedural-animals/src/species/crow/rig.js +0 -83
  138. package/vendor/procedural-animals/src/species/crow/sculpt.js +0 -118
  139. package/vendor/procedural-animals/src/species/deer/behaviour.js +0 -513
  140. package/vendor/procedural-animals/src/species/deer/coat.js +0 -391
  141. package/vendor/procedural-animals/src/species/deer/index.js +0 -106
  142. package/vendor/procedural-animals/src/species/deer/motion.js +0 -97
  143. package/vendor/procedural-animals/src/species/deer/regions.js +0 -60
  144. package/vendor/procedural-animals/src/species/deer/rig.js +0 -124
  145. package/vendor/procedural-animals/src/species/deer/sculpt.js +0 -297
  146. package/vendor/procedural-animals/src/species/dog/behaviour.js +0 -521
  147. package/vendor/procedural-animals/src/species/dog/coat.js +0 -495
  148. package/vendor/procedural-animals/src/species/dog/index.js +0 -144
  149. package/vendor/procedural-animals/src/species/dog/motion.js +0 -118
  150. package/vendor/procedural-animals/src/species/dog/regions.js +0 -61
  151. package/vendor/procedural-animals/src/species/dog/rig.js +0 -129
  152. package/vendor/procedural-animals/src/species/dog/sculpt.js +0 -246
  153. package/vendor/procedural-animals/src/species/eagle/coat.js +0 -250
  154. package/vendor/procedural-animals/src/species/eagle/index.js +0 -76
  155. package/vendor/procedural-animals/src/species/eagle/joints.js +0 -3
  156. package/vendor/procedural-animals/src/species/eagle/motion.js +0 -137
  157. package/vendor/procedural-animals/src/species/eagle/regions.js +0 -57
  158. package/vendor/procedural-animals/src/species/eagle/rig.js +0 -107
  159. package/vendor/procedural-animals/src/species/eagle/sculpt.js +0 -137
  160. package/vendor/procedural-animals/src/species/fish/coat.js +0 -271
  161. package/vendor/procedural-animals/src/species/fish/index.js +0 -88
  162. package/vendor/procedural-animals/src/species/fish/motion.js +0 -92
  163. package/vendor/procedural-animals/src/species/fish/regions.js +0 -27
  164. package/vendor/procedural-animals/src/species/fish/rig.js +0 -143
  165. package/vendor/procedural-animals/src/species/fish/sculpt.js +0 -214
  166. package/vendor/procedural-animals/src/species/fish/variants.js +0 -126
  167. package/vendor/procedural-animals/src/species/fox/behaviour.js +0 -314
  168. package/vendor/procedural-animals/src/species/fox/coat.js +0 -444
  169. package/vendor/procedural-animals/src/species/fox/index.js +0 -120
  170. package/vendor/procedural-animals/src/species/fox/motion.js +0 -81
  171. package/vendor/procedural-animals/src/species/fox/regions.js +0 -59
  172. package/vendor/procedural-animals/src/species/fox/rig.js +0 -91
  173. package/vendor/procedural-animals/src/species/fox/sculpt.js +0 -202
  174. package/vendor/procedural-animals/src/species/frog/behaviour.js +0 -590
  175. package/vendor/procedural-animals/src/species/frog/coat.js +0 -305
  176. package/vendor/procedural-animals/src/species/frog/index.js +0 -97
  177. package/vendor/procedural-animals/src/species/frog/motion.js +0 -69
  178. package/vendor/procedural-animals/src/species/frog/regions.js +0 -46
  179. package/vendor/procedural-animals/src/species/frog/rig.js +0 -156
  180. package/vendor/procedural-animals/src/species/frog/sculpt.js +0 -240
  181. package/vendor/procedural-animals/src/species/goat/behaviour.js +0 -509
  182. package/vendor/procedural-animals/src/species/goat/coat.js +0 -590
  183. package/vendor/procedural-animals/src/species/goat/index.js +0 -149
  184. package/vendor/procedural-animals/src/species/goat/motion.js +0 -95
  185. package/vendor/procedural-animals/src/species/goat/regions.js +0 -98
  186. package/vendor/procedural-animals/src/species/goat/rig.js +0 -181
  187. package/vendor/procedural-animals/src/species/goat/sculpt.js +0 -547
  188. package/vendor/procedural-animals/src/species/horse/coat.js +0 -361
  189. package/vendor/procedural-animals/src/species/horse/coatSeam.js +0 -122
  190. package/vendor/procedural-animals/src/species/horse/index.js +0 -80
  191. package/vendor/procedural-animals/src/species/horse/motion.js +0 -245
  192. package/vendor/procedural-animals/src/species/horse/regions.js +0 -60
  193. package/vendor/procedural-animals/src/species/horse/rig.js +0 -117
  194. package/vendor/procedural-animals/src/species/horse/sculpt.js +0 -316
  195. package/vendor/procedural-animals/src/species/horse/seamWeights.js +0 -272
  196. package/vendor/procedural-animals/src/species/lion/behaviour.js +0 -509
  197. package/vendor/procedural-animals/src/species/lion/coat.js +0 -1016
  198. package/vendor/procedural-animals/src/species/lion/index.js +0 -111
  199. package/vendor/procedural-animals/src/species/lion/motion.js +0 -87
  200. package/vendor/procedural-animals/src/species/lion/regions.js +0 -106
  201. package/vendor/procedural-animals/src/species/lion/rig.js +0 -110
  202. package/vendor/procedural-animals/src/species/lion/sculpt.js +0 -384
  203. package/vendor/procedural-animals/src/species/lion/whiskers.js +0 -132
  204. package/vendor/procedural-animals/src/species/pig/behaviour.js +0 -575
  205. package/vendor/procedural-animals/src/species/pig/coat.js +0 -730
  206. package/vendor/procedural-animals/src/species/pig/ears.js +0 -242
  207. package/vendor/procedural-animals/src/species/pig/index.js +0 -161
  208. package/vendor/procedural-animals/src/species/pig/motion.js +0 -105
  209. package/vendor/procedural-animals/src/species/pig/regions.js +0 -100
  210. package/vendor/procedural-animals/src/species/pig/rig.js +0 -152
  211. package/vendor/procedural-animals/src/species/pig/sculpt.js +0 -382
  212. package/vendor/procedural-animals/src/species/rabbit/behaviour.js +0 -605
  213. package/vendor/procedural-animals/src/species/rabbit/coat.js +0 -285
  214. package/vendor/procedural-animals/src/species/rabbit/index.js +0 -85
  215. package/vendor/procedural-animals/src/species/rabbit/motion.js +0 -85
  216. package/vendor/procedural-animals/src/species/rabbit/regions.js +0 -48
  217. package/vendor/procedural-animals/src/species/rabbit/rig.js +0 -99
  218. package/vendor/procedural-animals/src/species/rabbit/sculpt.js +0 -148
  219. package/vendor/procedural-animals/src/species/rat/behaviour.js +0 -539
  220. package/vendor/procedural-animals/src/species/rat/coat.js +0 -341
  221. package/vendor/procedural-animals/src/species/rat/index.js +0 -111
  222. package/vendor/procedural-animals/src/species/rat/motion.js +0 -90
  223. package/vendor/procedural-animals/src/species/rat/regions.js +0 -92
  224. package/vendor/procedural-animals/src/species/rat/rig.js +0 -112
  225. package/vendor/procedural-animals/src/species/rat/sculpt.js +0 -163
  226. package/vendor/procedural-animals/src/species/rat/whiskers.js +0 -119
  227. package/vendor/procedural-animals/src/species/shark/coat.js +0 -383
  228. package/vendor/procedural-animals/src/species/shark/index.js +0 -104
  229. package/vendor/procedural-animals/src/species/shark/motion.js +0 -69
  230. package/vendor/procedural-animals/src/species/shark/regions.js +0 -34
  231. package/vendor/procedural-animals/src/species/shark/rig.js +0 -194
  232. package/vendor/procedural-animals/src/species/shark/sculpt.js +0 -343
  233. package/vendor/procedural-animals/src/species/shark/variants.js +0 -105
  234. package/vendor/procedural-animals/src/species/sheep/behaviour.js +0 -522
  235. package/vendor/procedural-animals/src/species/sheep/coat.js +0 -529
  236. package/vendor/procedural-animals/src/species/sheep/index.js +0 -152
  237. package/vendor/procedural-animals/src/species/sheep/motion.js +0 -86
  238. package/vendor/procedural-animals/src/species/sheep/neck.js +0 -41
  239. package/vendor/procedural-animals/src/species/sheep/regions.js +0 -62
  240. package/vendor/procedural-animals/src/species/sheep/rig.js +0 -163
  241. package/vendor/procedural-animals/src/species/sheep/sculpt.js +0 -542
  242. package/vendor/procedural-animals/src/species/snake/coat.js +0 -315
  243. package/vendor/procedural-animals/src/species/snake/index.js +0 -73
  244. package/vendor/procedural-animals/src/species/snake/motion.js +0 -51
  245. package/vendor/procedural-animals/src/species/snake/regions.js +0 -51
  246. package/vendor/procedural-animals/src/species/snake/rig.js +0 -108
  247. package/vendor/procedural-animals/src/species/snake/sculpt.js +0 -122
  248. package/vendor/procedural-animals/src/species/spider/coat.js +0 -279
  249. package/vendor/procedural-animals/src/species/spider/index.js +0 -78
  250. package/vendor/procedural-animals/src/species/spider/motion.js +0 -72
  251. package/vendor/procedural-animals/src/species/spider/regions.js +0 -87
  252. package/vendor/procedural-animals/src/species/spider/rig.js +0 -118
  253. package/vendor/procedural-animals/src/species/spider/sculpt.js +0 -182
  254. package/vendor/procedural-animals/src/species/wolf/behaviour.js +0 -160
  255. package/vendor/procedural-animals/src/species/wolf/coat.js +0 -749
  256. package/vendor/procedural-animals/src/species/wolf/index.js +0 -107
  257. package/vendor/procedural-animals/src/species/wolf/motion.js +0 -80
  258. package/vendor/procedural-animals/src/species/wolf/regions.js +0 -57
  259. package/vendor/procedural-animals/src/species/wolf/rig.js +0 -87
  260. package/vendor/procedural-animals/src/species/wolf/sculpt.js +0 -339
  261. package/vendor/procedural-animals/src/worker.js +0 -29
@@ -1,522 +0,0 @@
1
- // Sheep behaviour layer for the quadruped engine (species.motion.hooks, see core/motion/actions.js):
2
- //
3
- // pose(engine, dt) after the engine's solve: the ruminant's sideways grinding of the lower
4
- // jaw (cud chewing, and the constant chewing between bites while grazing),
5
- // quick ear flicks (one ear at a time), the udder / scrotum drawn up when
6
- // the body rests on the ground
7
- // update(P, dt, layer) every frame over the action layer: the fleece added to the torso sections
8
- // (ground contact), the walk's head nod, cud chewing bouts (standing or
9
- // lying), grazing (bite and tear jerks, chewing in between, a step now and
10
- // then), tail flicks (a docked stump wiggles, a long tail swishes), ear flicks,
11
- // automatic idle variants of a flock animal (heads up in alarm, bleating,
12
- // grazing, a head shake, the ewe's warning stamp, the lamb's pronk)
13
- // actions alert (head up, ears forward, freeze and stare; a stamp), bleat, headshake,
14
- // stamp (foreleg stamp), pronk (lambs: stiff-legged bounce), attack (ram:
15
- // walks off, turns, charges and clashes head-down; ewe: stamps and butts), lie
16
- // (kneels first, rises hind end first), sleep (sternal, head back on the
17
- // flank)
18
- //
19
- // Posture lengths (headRaise, neckReach, fwd, side) are engine units (x cfg.k): species metres /
20
- // cfg.unit. World points are metres.
21
- import * as THREE from 'three';
22
- import { clamp, lerp, smooth, TAU, tv, tqa, angDiff } from '../../core/motion/common.js';
23
- import { mx, mxLeg, POSTURES, ONESHOTS } from '../../core/motion/actions.js';
24
-
25
- const _q = new THREE.Quaternion(), _s = new THREE.Vector3(1, 1, 1), _m = new THREE.Matrix4();
26
- const PI = Math.PI;
27
- const sm = (w) => w * w * (3 - 2 * w);
28
-
29
- function B(e) {
30
- if (!e._sheep) {
31
- // the fleece of this individual widens and deepens the torso sections the engine rests on the
32
- // ground (lying, sleeping, dead); motion.body holds the shorn body
33
- const F = (e.params?.wool || 0) * e.cfg.s;
34
- if (F > 0) {
35
- for (const k of ['chest', 'mid', 'pelvis', 'rump']) { const S = e.cfg[k]; if (S) { S.hh += 0.62 * F; S.hw += 1.2 * F + (F > 0.02 * e.cfg.s ? 0.02 * e.cfg.s : 0); S.hw *= 1 + 0.06 * (e.params?.heavy || 0); } }
36
- e.cfg.back += 0.9 * F;
37
- }
38
- e._sheep = {
39
- cud: 0, cudOn: e.rand() < 0.5, cudT: 6 + e.rand() * 10, chewPh: 0, grind: 0,
40
- ear: [{ a: 0, age: 9, next: 2 + e.rand() * 4 }, { a: 0, age: 9, next: 3 + e.rand() * 5 }],
41
- tailT: 1 + e.rand() * 3, tailAge: 9, tailAmp: 0.6,
42
- idleT: 3 + e.rand() * 5,
43
- steer: null,
44
- long: (e.params?.tailLen ?? 0.3) > 0.15,
45
- ram: e.params?.sex === 'male' && e.params?.age !== 'juvenile',
46
- lamb: e.params?.age === 'juvenile',
47
- };
48
- }
49
- return e._sheep;
50
- }
51
- const busy = (L) => L.postures.length > 0 || L.oneshots.some((a) => !a.stopping);
52
- const find = (L, n) => L.oneshots.find((a) => a.name === n && a.w > 0);
53
-
54
- // The engine turns only the head bone to look aside (up to 0.85 rad, plus a lead into turns), so all of
55
- // that yaw fell into the head / neck skin blends: the thick wool collar sheared and its fur shells (15-20
56
- // mm staples) tore open in a crack down the side and front of the neck. A sheep looks aside with its
57
- // neck: here the neck swings toward the look (neck1 about the neck base, neck2 about its own base, 30 %
58
- // of the head's yaw each), carrying the head along, and the head turns back by the same amount about the
59
- // occiput, so it keeps the orientation the engine gave it and only 40 % of the yaw is left between the
60
- // head and the neck.
61
- const _qa = new THREE.Quaternion(), _qb = new THREE.Quaternion(), _qr = new THREE.Quaternion();
62
- const _p = new THREE.Vector3(), _sc = new THREE.Vector3(), _c = new THREE.Vector3(), _up = new THREE.Vector3(0, 1, 0);
63
- const SWING = [0.22, 0.42];
64
- function rotAbout(bone, c, Q) {
65
- bone.matrixWorld.decompose(_p, _qa, _sc);
66
- _p.sub(c).applyQuaternion(Q).add(c);
67
- _qa.premultiply(Q);
68
- bone.matrixWorld.compose(_p, _qa, _sc);
69
- }
70
- function neckSwing(e) {
71
- const H = e.bone.head, N2 = e.bone.neck2, N1 = e.bone.neck1;
72
- if (!H || !N2 || !N1 || !e.bindQ.head) return;
73
- const b = e._sheepSwing || (e._sheepSwing = {
74
- a2: new THREE.Vector3().subVectors(e.J.occiput, e.J.neckMid).normalize(),
75
- iH: e.bindQ.head.clone().invert(), i2: e.bindQ.neck2.clone().invert(),
76
- kids: ['jaw', 'earL', 'earR'].map((k) => e.bone[k]).filter(Boolean),
77
- });
78
- // the head's yaw relative to neck2: the twist of their relative rotation (posed from bind) about the
79
- // neck2 axis (near vertical in the standing pose)
80
- H.matrixWorld.decompose(_p, _qa, _sc); _qa.multiply(b.iH);
81
- N2.matrixWorld.decompose(_p, _qb, _sc); _qb.multiply(b.i2);
82
- _qb.invert().multiply(_qa);
83
- const a = b.a2, d = _qb.x * a.x + _qb.y * a.y + _qb.z * a.z;
84
- let th = 2 * Math.atan2(d, _qb.w);
85
- if (th > Math.PI) th -= 2 * Math.PI; else if (th < -Math.PI) th += 2 * Math.PI;
86
- // (the twist about the neck axis maps onto a swing about the vertical only while the neck stands up)
87
- _c.setFromMatrixPosition(H.matrixWorld).sub(_p.setFromMatrixPosition(N2.matrixWorld));
88
- th *= sm(clamp((_c.y / Math.max(_c.length(), 1e-6) - 0.25) / 0.45, 0, 1));
89
- if (!(Math.abs(th) > 1e-4)) return;
90
- const head = [H, ...b.kids];
91
- _c.setFromMatrixPosition(N1.matrixWorld);
92
- _qr.setFromAxisAngle(_up, SWING[0] * th);
93
- for (const B of [N1, N2, ...head]) rotAbout(B, _c, _qr);
94
- _c.setFromMatrixPosition(N2.matrixWorld);
95
- _qr.setFromAxisAngle(_up, SWING[1] * th);
96
- for (const B of [N2, ...head]) rotAbout(B, _c, _qr);
97
- _c.setFromMatrixPosition(H.matrixWorld);
98
- _qr.setFromAxisAngle(_up, -(SWING[0] + SWING[1]) * th);
99
- for (const B of head) rotAbout(B, _c, _qr);
100
- }
101
-
102
- // ------------------------------------------------------------------------------------------------
103
- // pose: jaw grinding, ear flicks, udder lift
104
- function pose(e, dt) {
105
- const b = B(e);
106
- const ud = e.bone.udder;
107
- if (ud) {
108
- const want = e.P.groundW * (0.05 + 0.05 * (e.params?.udder || 0)) * e.cfg.s;
109
- b.udLift = (b.udLift || 0) + (want - (b.udLift || 0)) * (1 - Math.exp(-dt * 12));
110
- if (b.udLift > 1e-5) {
111
- _m.copy(ud.matrixWorld);
112
- _m.elements[13] += b.udLift;
113
- ud.matrixWorld.copy(_m);
114
- }
115
- }
116
- if (e.lod >= 2) return;
117
- neckSwing(e);
118
- const jaw = e.bone.jaw;
119
- if (jaw && b.grind > 1e-3) {
120
- const base = tv().setFromMatrixPosition(jaw.matrixWorld);
121
- _q.setFromRotationMatrix(jaw.matrixWorld);
122
- const ax = tv(0, 0, 1).applyQuaternion(_q);
123
- // (~4 mm sideways at the incisors: the lower lip stays under the upper; 7 mm with the jaw opened 6 mm slid the
124
- // lower lip out from under the upper at the corner, a dark gap)
125
- const a = 0.026 * b.grind * Math.sin(b.chewPh + (b.chewSide || 0));
126
- jaw.matrixWorld.compose(base, tqa(ax, a).multiply(_q), _s);
127
- }
128
- for (let k = 0; k < 2; k++) {
129
- const E = b.ear[k];
130
- if (E.age > 0.5) continue;
131
- const bone = k === 0 ? e.bone.earL : e.bone.earR;
132
- if (!bone) continue;
133
- // a flick: fast twist about the ear's long axis and a flap, decaying in ~0.4 s
134
- const f = Math.sin(Math.min(1, E.age / 0.4) * PI * 2) * Math.exp(-E.age * 5.5) * (k === 0 ? 1 : -1);
135
- const base = tv().setFromMatrixPosition(bone.matrixWorld);
136
- _q.setFromRotationMatrix(bone.matrixWorld);
137
- const along = tv(0, 1, 0).applyQuaternion(_q), flap = tv(0, 0, 1).applyQuaternion(_q);
138
- _q.premultiply(tqa(along, 0.45 * f * E.a)).premultiply(tqa(flap, 0.4 * f * E.a));
139
- bone.matrixWorld.compose(base, _q, _s);
140
- }
141
- }
142
-
143
- // ------------------------------------------------------------------------------------------------
144
- // update
145
- function update(P, dt, L) {
146
- const e = L.engine, b = B(e), cfg = e.cfg, u = 1 / cfg.unit;
147
- const vn = e.speed / cfg.sq;
148
- const still = 1 - smooth(0.1, 0.6, vn);
149
- const g = e.gait;
150
- // --- walk: a gentle head nod with each forefoot (+-2-3 cm)
151
- const walkW = smooth(0.15, 0.6, vn) * (1 - smooth(1.4, 1.9, vn)) * P.gaitW;
152
- if (walkW > 0.001 && g) {
153
- const c = 0.5 + 0.5 * Math.cos(4 * PI * (e.phase - g.off[0] - 0.1));
154
- P.headRaise -= 0.025 * u * walkW * c;
155
- P.headPitch += 0.04 * walkW * (c - 0.5);
156
- }
157
- const lying = L.postures.some((p) => p.name === 'lie') ? 1 : 0;
158
- const asleep = L.postures.some((p) => p.name === 'sleep' || p.name === 'death') ? 1 : 0;
159
-
160
- // --- grazing: bite and tear (a short jerk of the head up and to the side) about once a second,
161
- // chewing all the time in between, the jaw grinding sideways; now and then a step forward
162
- const eat = find(L, 'eat');
163
- let chewW = 0, chewRate = 1.25;
164
- if (eat) {
165
- const w = sm(clamp(eat.w, 0, 1)) * smooth(0.5, 1.2, eat.t);
166
- // one cycle per ~1.05 s: the mouth opens and bites (0 - 0.3), a jerk up and to the side tears the
167
- // grass (0.3 - 0.55), then chewing; every part starts and ends at rest (continuous)
168
- const c = (eat.t * 0.95) % 1;
169
- const bite = c < 0.3 ? Math.sin(PI * c / 0.3) : 0;
170
- const tear = c > 0.3 && c < 0.55 ? Math.sin(PI * (c - 0.3) / 0.25) ** 2 : 0;
171
- const side = Math.floor(eat.t * 0.95) % 2 ? 1 : -1;
172
- P.headPos.y += 0.022 * cfg.s * tear * w;
173
- P.headPitch -= 0.12 * tear * w;
174
- P.headYaw += side * 0.08 * tear * w;
175
- mx(P, 'jaw', 0.05 + 0.12 * bite, w);
176
- chewW = Math.max(chewW, w * (c < 0.55 ? 0 : Math.sin(PI * (c - 0.55) / 0.45))); chewRate = 1.6;
177
- mx(P, 'earFlat', -0.15, w); mx(P, 'earTwitch', 0.6, w);
178
- }
179
- // --- rumination: bouts of 20-40 s (standing idle, above all lying)
180
- b.cudT -= dt;
181
- if (b.cudT <= 0) { b.cudOn = !b.cudOn; b.cudT = b.cudOn ? 18 + e.rand() * 22 : 8 + e.rand() * 14; }
182
- const quiet = L.oneshots.some((a) => !a.stopping && a.name !== 'hit' && a.name !== 'headshake' && a.name !== 'eat') ? 0 : 1;
183
- const want = b.cudOn && quiet && !asleep ? Math.max(still * L.idle.w, lying) : 0;
184
- b.cud += (Math.max(want, chewW) - b.cud) * (1 - Math.exp(-dt * 4));
185
- // chew rate ~1.2 Hz; every ~50 chews a bolus is swallowed (a short pause, then the other side)
186
- const rate = chewRate * (1 - 0.85 * smooth(0.88, 1, (b.chewPh / TAU) % 50 / 50));
187
- const prev = b.chewPh;
188
- b.chewPh += dt * TAU * rate;
189
- if (Math.floor(prev / TAU / 50) !== Math.floor(b.chewPh / TAU / 50)) b.chewSide = (b.chewSide || 0) + PI;
190
- b.grind = b.cud;
191
- if (b.cud > 1e-3 && !eat) {
192
- const chew = 0.5 + 0.5 * Math.sin(b.chewPh);
193
- // (the lips stay closed: the lower jaw grinds sideways more than it opens, ~2.5 mm at the lips)
194
- P.jaw = Math.max(P.jaw, 0.013 * chew * b.cud); P.jawOmega = Math.max(P.jawOmega, 18);
195
- mx(P, 'eyelid', 0.2, b.cud * 0.5 * (1 - chewW));
196
- } else if (eat) { P.jawOmega = Math.max(P.jawOmega, 20); P.jaw += 0.03 * chewW * (0.5 + 0.5 * Math.sin(b.chewPh)); }
197
-
198
- // --- tail: a docked stump wiggles in quick bursts, a long tail swishes (flies), both clamp down
199
- // when alarmed
200
- b.tailT -= dt;
201
- if (b.tailT <= 0) { b.tailT = (b.long ? 3 : 2) + e.rand() * 6; b.tailAge = 0; b.tailAmp = 0.5 + 0.5 * e.rand(); }
202
- b.tailAge += dt;
203
- const tdur = b.long ? 1.6 : 0.7;
204
- const tw = b.tailAge < tdur ? Math.sin(PI * b.tailAge / tdur) : 0;
205
- if (tw > 0 && !asleep) {
206
- P.tailWag = Math.max(P.tailWag, b.tailAmp * tw * (b.long ? 0.7 : 1) * (1 - 0.6 * P.groundW));
207
- if (!b.long) mx(P, 'tailLift', 0.2, tw * 0.5);
208
- }
209
- // --- ear flicks (one ear at a time, more often standing)
210
- for (const E of b.ear) {
211
- E.age += dt; E.next -= dt * (0.5 + 0.5 * still);
212
- if (E.next <= 0) { E.next = 2.5 + e.rand() * 7; E.age = 0; E.a = (0.7 + 0.5 * e.rand()) * (1 - P.eyelid * 0.8) * (e.params?.ear === 'droop' ? 0.5 : 1); }
213
- }
214
- // --- dead: the ears fold back along the neck
215
- const dead = L.postures.reduce((m, p) => (p.name === 'death' ? Math.max(m, p.w) : m), 0);
216
- const flat = Math.max(smooth(0.3, 1.0, Math.abs(P.roll)), dead * smooth(0.3, 1, dead));
217
- if (flat > 0) { mx(P, 'earFlat', 0.6, flat); mx(P, 'earTwitch', 0, flat); }
218
- if (dead > 0 && P.headW > 0) P.headPos.y += (0.11 + 0.1 * (e.params?.horns?.turns || 0) * (e.params?.horns?.base || 0) / 0.035) * cfg.s * flat * (b.lamb ? 1.3 : 1);
219
- // (0.5: with the broader, blunter nose a head tucked 0.6 put a nostril corner into uneven
220
- // ground: the engine keeps only the nose's centre and the widest skin of the cheeks clear)
221
- if (dead > 0) { P.headRoll *= 1 - 0.45 * dead; P.headPitch += 0.5 * dead; }
222
- // --- a sheep's ears drop back rather than flatten (and the thin ear base does not tear)
223
- if (P.earFlat > 0.45 && dead < 0.01) P.earFlat = 0.45 + 0.25 * (P.earFlat - 0.45);
224
- // --- lying / sleeping: the tail lies flat
225
- P.tailSide *= 0.3;
226
- // --- any other action interrupts grazing
227
- if (L.oneshots.some((a) => !a.stopping && ['attack', 'jump', 'bleat', 'alert', 'stamp', 'pronk'].includes(a.name))) for (const a of L.oneshots) if (a.name === 'eat' || a.name === 'drink') a.stopping = true;
228
-
229
- // --- attack steering (the ram walks off, turns and charges)
230
- const st = b.steer;
231
- if (st && !L.oneshots.some((a) => a.name === 'attack' && !a.stopping)) b.steer = null;
232
- else if (st) { e.wantSpeed = st.speed; e.wantHeading = st.heading; }
233
-
234
- // --- idle variants of a flock animal
235
- if (e.idleVariants !== false && L.idle.w > 0.98 && !busy(L) && !e.input.look && !e.input.follow) {
236
- b.idleT -= dt;
237
- if (b.idleT <= 0) {
238
- b.idleT = 5 + e.rand() * 8;
239
- const r = e.rand();
240
- if (r < 0.26) L.play('alert');
241
- else if (r < 0.46) L.play('bleat');
242
- else if (r < 0.64) L.play('eat', { duration: 4 + e.rand() * 6 });
243
- else if (r < 0.74) L.play('headshake');
244
- else if (r < 0.82 && !b.ram) L.play('stamp');
245
- else if (r < 0.9 && b.lamb) L.play('pronk');
246
- else { b.ear[0].next = 0; b.ear[1].next = 0.3; }
247
- }
248
- } else if (busy(L)) b.idleT = Math.max(b.idleT, 3);
249
- }
250
-
251
- // ------------------------------------------------------------------------------------------------
252
- // alert: the flock's alarm. The head comes up high, ears forward, the body freezes (breath held),
253
- // staring at something; ewes may stamp a foreleg at the end
254
- const alert = {
255
- kind: 'oneshot', fade: 0.3,
256
- start(inst, L) {
257
- const e = L.engine;
258
- inst.dur = inst.opts.duration ?? 3.2 + e.rand() * 1.5;
259
- const a = e.heading + (e.rand() - 0.5) * 1.4;
260
- inst.look = inst.opts.target ? inst.opts.target.clone() : new THREE.Vector3(e.pos.x + Math.sin(a) * 12 * e.cfg.s, e.pos.y + 0.6 * e.cfg.s, e.pos.z + Math.cos(a) * 12 * e.cfg.s);
261
- inst.stamp = !B(e).ram && e.rand() < 0.35;
262
- },
263
- update(inst, dt, L) {
264
- if (inst.stamp && !inst.stamped && inst.t > inst.dur - 1.4) { inst.stamped = true; L.play('stamp', { quick: true }); }
265
- return inst.t >= inst.dur;
266
- },
267
- apply(P, w, inst, L) {
268
- const e = L.engine, u = 1 / e.cfg.unit, t = inst.t;
269
- const up = w * smooth(0, 0.35, t);
270
- P.look = inst.look; mx(P, 'lookW', 1, up);
271
- mx(P, 'headPitch', -0.3, up); mx(P, 'headRaise', 0.05 * u, up); mx(P, 'neckReach', -0.01 * u, up);
272
- mx(P, 'earFlat', -0.25, up); mx(P, 'earTwitch', 0.1, up); mx(P, 'earSwivel', 0, up);
273
- mx(P, 'breathAmp', 0.2, up); mx(P, 'eyelid', -0.3, up);
274
- mx(P, 'tailLift', -0.2, up); mx(P, 'tailIdle', 0, up);
275
- },
276
- };
277
-
278
- // bleat: head up and forward, mouth open, the jaw trembling (the vibrato of a "baa"), ears back a
279
- // little
280
- const bleat = {
281
- kind: 'oneshot', fade: 0.22,
282
- start(inst, L) { inst.dur = inst.opts.duration ?? 1.3 + 0.7 * L.engine.rand(); inst.said = false; },
283
- update(inst, dt, L) {
284
- if (!inst.said && inst.t > 0.35) { inst.said = true; L.engine.emit('vocalize', { kind: 'bleat' }); }
285
- return inst.t >= inst.dur;
286
- },
287
- apply(P, w, inst, L) {
288
- const e = L.engine, t = inst.t, u = 1 / e.cfg.unit;
289
- const up = w * smooth(0, 0.3, t);
290
- const call = smooth(0.3, 0.45, t) * (1 - smooth(inst.dur - 0.4, inst.dur - 0.12, t));
291
- mx(P, 'lookW', 0.3, up);
292
- mx(P, 'headPitch', -0.4, up); mx(P, 'headRaise', 0.03 * u, up); mx(P, 'neckReach', 0.02 * u, up);
293
- mx(P, 'jaw', 0.34 + 0.06 * Math.sin(t * TAU * 9), call * w); mx(P, 'jawOmega', 30, w);
294
- mx(P, 'earFlat', 0.25, up); mx(P, 'breathAmp', 2.2, call * w);
295
- },
296
- };
297
-
298
- // head shake: fast rotations about the nose axis (flies, rain), ears flapping
299
- const headshake = {
300
- kind: 'oneshot', fade: 0.12,
301
- start(inst) { inst.dur = 1.0; },
302
- update(inst) { return inst.t >= inst.dur; },
303
- apply(P, w, inst) {
304
- const t = inst.t;
305
- const env = smooth(0, 0.12, t) * (1 - smooth(0.55, 0.95, t)) * w;
306
- const s = Math.sin(t * TAU * 4.4);
307
- mx(P, 'headOmega', 5, env);
308
- mx(P, 'headRoll', 0.45 * s, env); mx(P, 'headYaw', 0.14 * s, env); mx(P, 'headPitch', -0.05, env);
309
- mx(P, 'earTwitch', 2.5, env); mx(P, 'eyelid', 0.5, env); mx(P, 'lookW', 0, env);
310
- },
311
- };
312
-
313
- // stamp: a foreleg lifted and brought down hard (alarm / threat), once or twice
314
- const stamp = {
315
- kind: 'oneshot', fade: 0.2, needsStand: true,
316
- start(inst, L) {
317
- const e = L.engine;
318
- inst.n = inst.opts.count ?? (inst.opts.quick ? 1 : 1 + (e.rand() < 0.5 ? 1 : 0));
319
- inst.T = 0.62;
320
- inst.dur = 0.35 + inst.n * inst.T + 0.35;
321
- inst.leg = inst.opts.side === -1 ? 1 : inst.opts.side === 1 ? 0 : (e.rand() < 0.5 ? 0 : 1);
322
- inst.hits = 0;
323
- },
324
- update(inst, dt, L) {
325
- const e = L.engine;
326
- const k = Math.floor((inst.t - 0.35 - 0.34 * inst.T) / inst.T) + 1;
327
- if (k > inst.hits && inst.hits < inst.n) {
328
- inst.hits++;
329
- const leg = e.legs[inst.leg];
330
- e.emit('footstep', { foot: leg.def.key, position: leg.plant.clone(), speed: 0, strength: 1, stamp: true });
331
- if (inst.opts.hit) e.emit('attackHit', { position: leg.plant.clone(), direction: e.forward(e.heading, new THREE.Vector3()), style: 'stamp' });
332
- }
333
- return inst.t >= inst.dur;
334
- },
335
- apply(P, w, inst, L) {
336
- const e = L.engine, cfg = e.cfg, s = cfg.s, t = inst.t, i = inst.leg;
337
- const k = w * smooth(0.1, 0.3, t) * (1 - smooth(inst.dur - 0.35, inst.dur - 0.05, t));
338
- mx(P, 'gaitW', 0, w); mx(P, 'legGait', 0, w);
339
- mx(P, 'headPitch', -0.15, k); mx(P, 'headRaise', 0.02 / cfg.unit, k);
340
- mx(P, 'earFlat', -0.3, k);
341
- mx(P, 'side', -0.012 * (i === 0 ? 1 : -1) / cfg.unit, k); // weight onto the other three legs
342
- // lift high (0 - 0.34 T), strike down fast (0.34 - 0.42 T), rest on the ground
343
- const c = clamp((t - 0.35) / inst.T, 0, inst.n);
344
- const ph = c - Math.floor(c);
345
- const on = c < inst.n ? 1 : 0;
346
- const lift = on * (ph < 0.34 ? Math.sin(0.5 * PI * ph / 0.34) : 1 - smooth(0.34, 0.42, ph));
347
- const leg = e.legs[i];
348
- const tp = tv().copy(leg.plant).addScaledVector(e.forward(e.heading, tv()), 0.02 * s * lift);
349
- tp.y += 0.1 * s * lift + 0.002 * s;
350
- P.legs[i].target.copy(tp);
351
- mxLeg(P, i, 'reach', 1, k);
352
- mx(P, 'dropF', 0.03, k * lift);
353
- },
354
- };
355
-
356
- // pronk (lambs): a stiff-legged bounce straight up with a twist of the body, landing on four feet
357
- const pronk = {
358
- kind: 'oneshot', fade: 0.12, needsStand: true, airborne: true,
359
- start(inst, L) {
360
- const e = L.engine;
361
- ONESHOTS.jump.start(inst, L);
362
- const h = (inst.opts.height ?? 0.35) * e.cfg.s;
363
- inst.vy = Math.sqrt(2 * 9.81 * h);
364
- inst.vh = Math.max(e.speed, 0.4 * e.cfg.sq);
365
- inst.twist = (e.rand() < 0.5 ? -1 : 1) * (0.2 + 0.2 * e.rand());
366
- },
367
- update(inst, dt, L) { return ONESHOTS.jump.update.call(ONESHOTS.jump, inst, dt, L); },
368
- onLand(inst, L) { ONESHOTS.jump.onLand(inst, L); },
369
- apply(P, w, inst, L) {
370
- ONESHOTS.jump.apply(P, w, inst, L);
371
- if (inst.phase === 'air') {
372
- const T = (2 * inst.vy) / 9.81, x = clamp((inst.t - inst.tAir) / T, 0, 1);
373
- const a = Math.sin(PI * x);
374
- // stiff legs (not tucked), the back arched, head down, a twist of the body
375
- // (only in the first part of the flight: the built-in jump sets the legs up for the landing)
376
- const st = 1 - smooth(0.45, 0.7, x);
377
- for (let i = 0; i < 4; i++) { P.legs[i].tuck *= 1 - 0.7 * st; P.legs[i].extend = lerp(P.legs[i].extend, 0.9, 0.7 * st); }
378
- mx(P, 'pitch', 0.05, 0.8); mx(P, 'roll', inst.twist * a, 1); mx(P, 'bend', -inst.twist * 0.6 * a, 1);
379
- mx(P, 'headPitch', 0.3, a); mx(P, 'tailLift', 0.4, 1);
380
- }
381
- },
382
- };
383
-
384
- // attack. Ram: squares up (head high, chin tucked), turns and walks off a few metres, turns back,
385
- // charges and clashes head-on (head lowered in the last strides, forehead / horns first; the
386
- // forequarters thrown up at the impact); the hit at the clash. While running it charges at once. Ewe:
387
- // stamps a foreleg, then a short butt with the forehead. opts.target (Vector3) aims it.
388
- const FAR = 1e6;
389
- const attack = {
390
- kind: 'oneshot', fade: 0.18, needsStand: true,
391
- start(inst, L) {
392
- const e = L.engine, b = B(e), cfg = e.cfg;
393
- inst.ram = inst.opts.style ? inst.opts.style === 'charge' : b.ram;
394
- inst.run = smooth(1.2, 3.0, e.speed / cfg.sq);
395
- inst.target = inst.opts.target ? inst.opts.target.clone() : null;
396
- inst.hit = false;
397
- inst.h = inst.target ? Math.atan2(inst.target.x - e.pos.x, inst.target.z - e.pos.z) : e.heading;
398
- if (inst.ram) {
399
- inst.phase = inst.run > 0.3 ? 'charge' : 'threat';
400
- inst.p0 = e.pos.clone();
401
- inst.dBack = (inst.opts.backOff ?? 2.2) * cfg.s;
402
- inst.vCharge = 4.2 * cfg.sq;
403
- inst.hitAt = FAR; inst.tCharge = inst.phase === 'charge' ? 0 : FAR;
404
- inst.dur = Infinity;
405
- b.steer = null;
406
- } else {
407
- inst.tStamp = inst.run > 0.3 ? 0 : 1.1;
408
- inst.hitAt = inst.tStamp + 0.55;
409
- inst.dur = inst.hitAt + 0.7;
410
- inst.leg = e.rand() < 0.5 ? 0 : 1;
411
- inst.stamped = false;
412
- }
413
- },
414
- update(inst, dt, L) {
415
- const e = L.engine, b = B(e), t = inst.t, cfg = e.cfg;
416
- if (inst.ram) {
417
- const ph = inst.phase;
418
- if (ph === 'threat') { b.steer = { speed: 0, heading: e.heading }; if (t > 0.9) inst.phase = 'away'; }
419
- else if (ph === 'away') {
420
- // turn round and walk off
421
- const off = Math.abs(angDiff(e.heading, inst.h + Math.PI));
422
- b.steer = { speed: 1.0 * cfg.sq * (1 - smooth(0.4, 1.2, off)), heading: inst.h + Math.PI };
423
- const d = Math.hypot(e.pos.x - inst.p0.x, e.pos.z - inst.p0.z);
424
- if (d > inst.dBack || t > 12) { inst.phase = 'turn'; inst.tTurn = t; }
425
- } else if (ph === 'turn') {
426
- // turn back to face the rival
427
- const h = inst.target ? Math.atan2(inst.target.x - e.pos.x, inst.target.z - e.pos.z) : inst.h;
428
- b.steer = { speed: 0, heading: h };
429
- if ((Math.abs(angDiff(e.heading, h)) < 0.12 && e.speed < 0.2 * cfg.sq && t - inst.tTurn > 0.8) || t - inst.tTurn > 5) {
430
- inst.phase = 'charge'; inst.tCharge = t; inst.h = h;
431
- }
432
- } else if (ph === 'charge') {
433
- b.steer = { speed: inst.vCharge * smooth(0, 0.5, t - inst.tCharge), heading: inst.h };
434
- const dist = inst.target ? Math.hypot(inst.target.x - e.pos.x, inst.target.z - e.pos.z) - 0.55 * cfg.s : Math.max(0, inst.dBack + 0.8 * cfg.s - Math.hypot(e.pos.x - inst.p0.x, e.pos.z - inst.p0.z) * 0) - (t - inst.tCharge) * 0.72 * inst.vCharge;
435
- // (the head goes down in the last 0.55 s: predict the clash)
436
- const v = Math.max(e.speed, 0.5);
437
- if (inst.hitAt === FAR && (dist / v < 0.55 || t - inst.tCharge > 4)) inst.hitAt = t + clamp(dist / v, 0.25, 0.55);
438
- if (t >= inst.hitAt) { inst.phase = 'recover'; inst.dur = t + 1.6; }
439
- } else b.steer = { speed: 0, heading: e.heading };
440
- } else if (!inst.stamped && inst.tStamp > 0 && t > 0.62) {
441
- inst.stamped = true;
442
- const leg = e.legs[inst.leg];
443
- e.emit('footstep', { foot: leg.def.key, position: leg.plant.clone(), speed: 0, strength: 1, stamp: true });
444
- }
445
- if (!inst.hit && t >= inst.hitAt) {
446
- inst.hit = true;
447
- const p = new THREE.Vector3().copy(e.headPose.position);
448
- const dir = inst.target ? inst.target.clone().sub(e.pos).setY(0).normalize() : e.forward(e.heading, new THREE.Vector3());
449
- e.emit('attackHit', { position: p, direction: dir, style: 'headbutt' });
450
- }
451
- if (t >= inst.dur) { b.steer = null; return true; }
452
- return false;
453
- },
454
- apply(P, w, inst, L) {
455
- const e = L.engine, cfg = e.cfg, u = 1 / cfg.unit, s = cfg.s, t = inst.t;
456
- if (inst.target && inst.phase !== 'away') { P.look = inst.target; mx(P, 'lookW', 0.8, w * (1 - smooth(inst.hitAt - 0.6, inst.hitAt - 0.3, t))); }
457
- if (inst.ram) {
458
- // threat: head high, chin tucked, ears back; walking off: head low, ears back
459
- const thr = w * smooth(0, 0.4, t) * (1 - smooth(inst.tCharge, inst.tCharge + 0.4, t));
460
- mx(P, 'headPitch', 0.15, thr); mx(P, 'headRaise', 0.03 * u, thr);
461
- mx(P, 'earFlat', 0.7, w); mx(P, 'earTwitch', 0, w); mx(P, 'tailLift', -0.1, w);
462
- // the charge: head goes down in the last strides, the clash, the rebound
463
- const low = w * smooth(inst.hitAt - 0.55, inst.hitAt - 0.1, t) * (1 - smooth(inst.hitAt + 0.25, inst.hitAt + 1.1, t));
464
- mx(P, 'headPitch', 0.75, low); mx(P, 'headRaise', -0.11 * u, low); mx(P, 'neckReach', 0.035 * u, low);
465
- mx(P, 'dropF', 0.12, low);
466
- // impact: the forequarters are thrown up and back, the head recoils
467
- const imp = w * smooth(inst.hitAt - 0.02, inst.hitAt + 0.1, t) * (1 - smooth(inst.hitAt + 0.15, inst.hitAt + 0.7, t));
468
- mx(P, 'dropF', -0.1, imp); mx(P, 'headPitch', 0.35, imp);
469
- } else {
470
- // ewe: stamp (if standing), then a butt: head down and forward, a lunge of the body
471
- if (inst.tStamp > 0) {
472
- const i = inst.leg, leg = e.legs[i];
473
- const k = w * smooth(0.1, 0.25, t) * (1 - smooth(0.85, 1.05, t));
474
- const lift = t < 0.5 ? Math.sin(0.5 * PI * clamp((t - 0.15) / 0.35, 0, 1)) : 1 - smooth(0.5, 0.62, t);
475
- mx(P, 'gaitW', 0, k); mx(P, 'legGait', 0, k);
476
- const tp = tv().copy(leg.plant);
477
- tp.y += 0.09 * s * lift + 0.002 * s;
478
- P.legs[i].target.copy(tp);
479
- mxLeg(P, i, 'reach', 1, k);
480
- mx(P, 'side', -0.012 * (i === 0 ? 1 : -1) * u, k);
481
- mx(P, 'headPitch', -0.2, k); mx(P, 'earFlat', -0.2, k);
482
- }
483
- const b0 = inst.hitAt - 0.45;
484
- const butt = w * smooth(b0, inst.hitAt - 0.05, t) * (1 - smooth(inst.hitAt + 0.15, inst.hitAt + 0.65, t));
485
- mx(P, 'headPitch', 0.7, butt); mx(P, 'headRaise', -0.08 * u, butt); mx(P, 'neckReach', 0.04 * u, butt);
486
- mx(P, 'fwd', 0.05 * u * (inst.run > 0.3 ? 0 : 1), butt); mx(P, 'dropF', 0.1, butt);
487
- mx(P, 'earFlat', 0.6, butt);
488
- }
489
- },
490
- };
491
-
492
- // lie: the engine's kneeling lie (actions.lie: down front, up hind), with the rise finished a little
493
- // before the posture is released: its end-first springs must settle at zero before the posture
494
- // instance is removed, or the legs jump in the last frame of standing up
495
- function lieApply(P, w, inst, L) {
496
- const we = inst.target > 0 ? w : smooth(0.3, 1, w);
497
- POSTURES.lie.apply(P, we, inst, L);
498
- }
499
- const lie = { kind: 'posture', fadeIn: 1.5, fadeOut: 1.4, apply: lieApply };
500
-
501
- // sleep: sternal (sheep rarely lie flat out), head turned back and resting against the flank, eyes
502
- // closed
503
- const sleep = {
504
- kind: 'posture', fadeIn: 1.4, fadeOut: 1.1,
505
- apply(P, w, inst, L) {
506
- lieApply(P, w, inst, L);
507
- const e = L.engine;
508
- const c = sm(clamp(w, 0, 1)) * smooth(1.2, 3.2, inst.t);
509
- mx(P, 'headYaw', inst.side * 0.75, c); mx(P, 'headPitch', 0.4, c); mx(P, 'headRaise', -0.07 / e.cfg.unit, c);
510
- mx(P, 'bend', inst.side * 0.3, c);
511
- mx(P, 'earFlat', 0.4, c); mx(P, 'earTwitch', 0.2, c);
512
- mx(P, 'eyelid', 1, smooth(1.8, 3.2, inst.t) * w);
513
- mx(P, 'breathRate', 0.6, c); mx(P, 'lookW', 0, c); mx(P, 'tailIdle', 0.2, c);
514
- },
515
- };
516
-
517
- void angDiff;
518
- export const hooks = {
519
- pose,
520
- update,
521
- actions: { alert, bleat, headshake, stamp, pronk, attack, lie, sleep },
522
- };