roamwright 0.2.0 → 0.20.1

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 (140) hide show
  1. package/CONTRIBUTING.md +3 -1
  2. package/README.md +19 -21
  3. package/geometry/bedding.js +141 -0
  4. package/geometry/builder-cells.js +42 -0
  5. package/geometry/builder-frame.js +47 -0
  6. package/geometry/builder.js +189 -177
  7. package/geometry/cell-mask.js +25 -0
  8. package/geometry/creased-sheet.js +34 -34
  9. package/geometry/ear.js +36 -36
  10. package/geometry/erosion.js +65 -42
  11. package/geometry/face-grid.js +65 -65
  12. package/geometry/foliage.js +47 -0
  13. package/geometry/hair-gathers.js +41 -41
  14. package/geometry/hair-locks.js +41 -41
  15. package/geometry/hair-patch.js +18 -18
  16. package/geometry/hair-shell.js +31 -31
  17. package/geometry/head-profile.js +15 -15
  18. package/geometry/head-shape.js +77 -77
  19. package/geometry/heightfield.js +27 -18
  20. package/geometry/index-parts.js +60 -0
  21. package/geometry/instance-visibility.js +41 -0
  22. package/geometry/instances.js +35 -12
  23. package/geometry/outline-grid.js +7 -7
  24. package/geometry/part-ranges.js +18 -0
  25. package/geometry/path-field.js +63 -33
  26. package/geometry/placement.js +52 -52
  27. package/geometry/point-buckets.js +50 -0
  28. package/geometry/primitives.js +63 -47
  29. package/geometry/sagging-tube.js +27 -27
  30. package/geometry/sampled-surface.js +109 -77
  31. package/geometry/scatter-grid.js +46 -6
  32. package/geometry/sculpt.js +93 -93
  33. package/geometry/shore-grid.js +77 -34
  34. package/geometry/strata-field.js +92 -92
  35. package/geometry/strata-tiles.js +91 -91
  36. package/geometry/stream.js +70 -48
  37. package/geometry/terrain-chunks.js +101 -92
  38. package/geometry/terrain-extension.js +6 -6
  39. package/index.js +5 -0
  40. package/math/cell-edge.js +16 -16
  41. package/math/cell-key.js +6 -0
  42. package/math/circle-clearances.js +19 -0
  43. package/math/cloth-textures.js +6 -6
  44. package/math/cloud-textures.js +20 -20
  45. package/math/color.js +14 -14
  46. package/math/curve.js +22 -22
  47. package/math/mountain-ranges.js +216 -165
  48. package/math/noise.js +101 -49
  49. package/math/particle-noise.js +28 -28
  50. package/math/periodic.js +5 -5
  51. package/math/random.js +24 -15
  52. package/math/rectangles.js +52 -0
  53. package/math/rock-texture.js +7 -7
  54. package/math/scalar.js +5 -5
  55. package/math/surface-textures.js +54 -54
  56. package/motion/arm-reach.js +24 -24
  57. package/motion/ballistic-arc.js +11 -0
  58. package/motion/biped-cycle.js +44 -0
  59. package/motion/biped-leg.js +37 -0
  60. package/motion/biped-stride.js +98 -122
  61. package/motion/contact-transition.js +33 -0
  62. package/motion/crowd-alarms.js +39 -39
  63. package/motion/crowd-routine.js +94 -65
  64. package/motion/daily-clock.js +9 -9
  65. package/motion/deck-surface.js +38 -0
  66. package/motion/ground-lookahead.js +29 -0
  67. package/motion/instance-sight.js +8 -8
  68. package/motion/player.js +36 -28
  69. package/motion/quadruped.js +155 -155
  70. package/motion/spaces.js +32 -32
  71. package/motion/weather.js +73 -24
  72. package/motion/wind.js +32 -13
  73. package/package.json +8 -4
  74. package/rendering/caster-motion.js +5 -5
  75. package/rendering/cell-lod.js +147 -82
  76. package/rendering/cloud-dome.js +28 -28
  77. package/rendering/cloud-field.js +2 -2
  78. package/rendering/compilation-schedule.js +68 -11
  79. package/rendering/contact-shadows.js +2 -2
  80. package/rendering/context.js +12 -0
  81. package/rendering/crowd-skinning.js +13 -13
  82. package/rendering/crust.js +18 -12
  83. package/rendering/daylight.js +3 -3
  84. package/rendering/detail-range.js +28 -0
  85. package/rendering/dispose-meshes.js +18 -0
  86. package/rendering/dof.js +9 -9
  87. package/rendering/eye-adapt.js +14 -14
  88. package/rendering/far-shadow.js +8 -8
  89. package/rendering/flocks.js +78 -78
  90. package/rendering/garment-material.js +17 -17
  91. package/rendering/glow-lines.js +13 -13
  92. package/rendering/hair-strands.js +20 -20
  93. package/rendering/height-probe.js +149 -19
  94. package/rendering/label-layout.js +46 -0
  95. package/rendering/label-range.js +38 -0
  96. package/rendering/light-budget.js +52 -52
  97. package/rendering/light-shafts.js +23 -23
  98. package/rendering/local-lights.js +15 -4
  99. package/rendering/material-lod.js +49 -49
  100. package/rendering/particle-emitters.js +90 -90
  101. package/rendering/particles.js +78 -67
  102. package/rendering/piece-batch.js +91 -26
  103. package/rendering/planar-reflection.js +38 -38
  104. package/rendering/post-shaders.js +17 -13
  105. package/rendering/post.js +71 -71
  106. package/rendering/rain-streaks.js +67 -0
  107. package/rendering/road-surface.js +11 -11
  108. package/rendering/sea-shaders.js +11 -11
  109. package/rendering/shader-warmup.js +29 -16
  110. package/rendering/shadow-alignment.js +4 -4
  111. package/rendering/shadow-depth.js +7 -7
  112. package/rendering/sky-occlusion.js +16 -16
  113. package/rendering/sky.js +16 -6
  114. package/rendering/stage-lights.js +6 -6
  115. package/rendering/static-point-shadow.js +3 -3
  116. package/rendering/still-casters.js +1 -1
  117. package/rendering/strata-material.js +10 -10
  118. package/rendering/stream-flow.js +10 -10
  119. package/rendering/taa.js +26 -26
  120. package/rendering/talus.js +15 -15
  121. package/rendering/terrain-rock.js +7 -7
  122. package/rendering/texture-fills.js +8 -8
  123. package/rendering/textures.js +28 -28
  124. package/rendering/wakes.js +24 -24
  125. package/rendering/water-material.js +36 -23
  126. package/rendering/world-matrices.js +38 -38
  127. package/runtime/diagnostic-log.js +2 -2
  128. package/runtime/first-use.js +33 -28
  129. package/runtime/frame-budget.js +49 -0
  130. package/runtime/frame-pacing.js +33 -0
  131. package/runtime/gpu-timing.js +13 -10
  132. package/runtime/grid-cache.js +1 -1
  133. package/runtime/grid-rows.js +44 -5
  134. package/runtime/hand-over.js +41 -23
  135. package/runtime/pool.js +58 -16
  136. package/runtime/quality-fit.js +1 -1
  137. package/runtime/search.js +22 -22
  138. package/runtime/spatial-audio.js +13 -13
  139. package/runtime/withdrawable-task.js +35 -0
  140. package/runtime/worker.js +7 -0
@@ -28,37 +28,37 @@ const slope = (y0, z0, y1, z1) => Math.atan2(z0 - z1, y0 - y1);
28
28
  function gaitAt(keys, u, out) {
29
29
  let i = 0;
30
30
  while (i < keys.length - 2 && keys[i + 1][0] < u) i++;
31
- const a = keys[i],
32
- b = keys[i + 1],
33
- t = clamp((u - a[0]) / (b[0] - a[0]), 0, 1);
34
- out.travel = lerp(a[1], b[1], t);
35
- out.duty = lerp(a[2], b[2], t);
31
+ const currentKey = keys[i],
32
+ nextKey = keys[i + 1],
33
+ t = clamp((u - currentKey[0]) / (nextKey[0] - currentKey[0]), 0, 1);
34
+ out.travel = lerp(currentKey[1], nextKey[1], t);
35
+ out.duty = lerp(currentKey[2], nextKey[2], t);
36
36
  for (let k = 0; k < 4; k++) {
37
- let d = b[3][k] - a[3][k];
38
- d -= Math.round(d);
39
- out.phase[k] = a[3][k] + d * t;
37
+ let angleDelta = nextKey[3][k] - currentKey[3][k];
38
+ angleDelta -= Math.round(angleDelta);
39
+ out.phase[k] = currentKey[3][k] + angleDelta * t;
40
40
  }
41
- out.rise = lerp(a[4], b[4], t);
42
- out.lift = lerp(a[5], b[5], t);
43
- out.flex = lerp(a[6], b[6], t);
41
+ out.rise = lerp(currentKey[4], nextKey[4], t);
42
+ out.lift = lerp(currentKey[5], nextKey[5], t);
43
+ out.flex = lerp(currentKey[6], nextKey[6], t);
44
44
  return out;
45
45
  }
46
46
 
47
47
  // The skeleton and the state of one beast. joints: where each bone's joint is at rest; feet: where each foot meets the ground;
48
48
  // o: its kind's gaits and the habits of its legs.
49
- function makeRig(joints, feet, o) {
49
+ function makeRig(joints, feet, options) {
50
50
  const bones = joints.map(() => new THREE.Bone());
51
51
  for (let i = 0; i < bones.length; i++) {
52
- const p = PARENT[i];
53
- if (p < 0) bones[i].position.set(joints[i][0], joints[i][1], joints[i][2]);
52
+ const parentIndex = PARENT[i];
53
+ if (parentIndex < 0) bones[i].position.set(joints[i][0], joints[i][1], joints[i][2]);
54
54
  else {
55
- bones[i].position.set(joints[i][0] - joints[p][0], joints[i][1] - joints[p][1], joints[i][2] - joints[p][2]);
56
- bones[p].add(bones[i]);
55
+ bones[i].position.set(joints[i][0] - joints[parentIndex][0], joints[i][1] - joints[parentIndex][1], joints[i][2] - joints[parentIndex][2]);
56
+ bones[parentIndex].add(bones[i]);
57
57
  }
58
58
  }
59
- const legs = LEGS.map((b, k) => {
59
+ const legs = LEGS.map((legBone, k) => {
60
60
  const girdle = joints[k > 1 ? BONE.hips : BONE.chest],
61
- pts = [joints[b], joints[b + 1], joints[b + 2], joints[b + 3], feet[k]],
61
+ pts = [joints[legBone], joints[legBone + 1], joints[legBone + 2], joints[legBone + 3], feet[k]],
62
62
  len = [],
63
63
  rest = [];
64
64
  for (let i = 0; i < 4; i++) {
@@ -66,17 +66,17 @@ function makeRig(joints, feet, o) {
66
66
  rest.push(slope(pts[i][1], pts[i][2], pts[i + 1][1], pts[i + 1][2]));
67
67
  }
68
68
  return {
69
- bone: b,
69
+ bone: legBone,
70
70
  hind: k > 1,
71
- habit: k > 1 ? o.hind : o.fore,
72
- px: joints[b][0],
73
- py: joints[b][1] - girdle[1], // the pivot from its girdle's joint
74
- pz: joints[b][2] - girdle[2],
71
+ habit: k > 1 ? options.hind : options.fore,
72
+ px: joints[legBone][0],
73
+ py: joints[legBone][1] - girdle[1], // the pivot from its girdle's joint
74
+ pz: joints[legBone][2] - girdle[2],
75
75
  fx: feet[k][0], // the foot's own place under the body
76
76
  fz: feet[k][2],
77
77
  len,
78
78
  rest,
79
- line: slope(joints[b][1], joints[b][2], joints[b + 3][1], joints[b + 3][2]), // the whole leg's slope at rest
79
+ line: slope(joints[legBone][1], joints[legBone][2], joints[legBone + 3][1], joints[legBone + 3][2]), // the whole leg's slope at rest
80
80
  air: 0, // 1 while the foot is being carried
81
81
  s: 0, // how far through its step
82
82
  wx: 0, // where it is planted, or was lifted from, in the world
@@ -90,11 +90,11 @@ function makeRig(joints, feet, o) {
90
90
  return {
91
91
  bones,
92
92
  legs,
93
- o,
93
+ o: options,
94
94
  joints,
95
95
  leg: joints[LEGS[2]][1], // the hip's height: the length everything is measured against
96
96
  span: joints[LEGS[0]][2] - joints[LEGS[2]][2], // from hip to shoulder
97
- ph: o.phase ?? Math.random(),
97
+ ph: options.phase ?? Math.random(),
98
98
  beat: 0, // the quarter of the stride its hooves last struck in
99
99
  v: 0,
100
100
  u: 0, // its speed against its leg's length
@@ -111,137 +111,137 @@ function makeRig(joints, feet, o) {
111
111
 
112
112
  const _free = { dy: 0, dz: 0, pitch: 0, hips: 0, chest: 0, legs: null };
113
113
 
114
- // Moves a beast's body and legs on by dt. F is where it stands: { x, y, z, yaw, s: its size, ground: (x, z) => height, or null on a
115
- // floor }. M (optional) is its manner: { dy, dz, pitch, hips, chest } shift and turn the body (a crouch, a sit), and legs[k], where
114
+ // Moves a beast's body and legs forward by deltaSeconds. The placement describes where it stands: { x, y, z, yaw, s: its size, ground: (x, z) => height, or null on a
115
+ // floor }. The optional pose describes its posture: { dy, dz, pitch, hips, chest } shift and turn the body (a crouch, a sit), and legs[k], where
116
116
  // given, takes a foot off the ground to a place of its own: { y, z, a3, a4, w } in the body's frame (a leap, a beast lying down).
117
- function stepRig(Q, F, dt, M = _free) {
118
- const s = F.s,
119
- sin = Math.sin(F.yaw),
120
- cos = Math.cos(F.yaw),
121
- h = Q.leg,
122
- G = Q.gait,
123
- jumped = Q.px === null || Math.hypot(F.x - Q.px, F.z - Q.pz) > 3 * h * s, // put down somewhere else: the feet go with it
124
- fwd = jumped ? 0 : ((F.x - Q.px) * sin + (F.z - Q.pz) * cos) / s,
117
+ function stepRig(rig, placement, deltaSeconds, pose = _free) {
118
+ const s = placement.s,
119
+ sin = Math.sin(placement.yaw),
120
+ cos = Math.cos(placement.yaw),
121
+ h = rig.leg,
122
+ gait = rig.gait,
123
+ jumped = rig.px === null || Math.hypot(placement.x - rig.px, placement.z - rig.pz) > 3 * h * s, // put down somewhere else: the feet go with it
124
+ fwd = jumped ? 0 : ((placement.x - rig.px) * sin + (placement.z - rig.pz) * cos) / s,
125
125
  gone = Math.abs(fwd),
126
126
  dir = fwd < 0 ? -1 : 1;
127
- Q.px = F.x;
128
- Q.pz = F.z;
129
- if (dt > 0) Q.v += (gone / dt - Q.v) * Math.min(1, dt * 8);
130
- const u = (Q.v * s) / Math.sqrt(GRAVITY * h * s),
127
+ rig.px = placement.x;
128
+ rig.pz = placement.z;
129
+ if (deltaSeconds > 0) rig.v += (gone / deltaSeconds - rig.v) * Math.min(1, deltaSeconds * 8);
130
+ const u = (rig.v * s) / Math.sqrt(GRAVITY * h * s),
131
131
  moving = u > 0.035;
132
- Q.u = u;
133
- gaitAt(Q.o.gaits, u, G);
134
- const travel = G.travel * h,
135
- stride = travel / G.duty,
136
- swing = 1 - G.duty;
137
- if (moving) Q.ph = (Q.ph + gone / stride) % 1;
138
- Q.wait -= dt;
132
+ rig.u = u;
133
+ gaitAt(rig.o.gaits, u, gait);
134
+ const travel = gait.travel * h,
135
+ stride = travel / gait.duty,
136
+ swing = 1 - gait.duty;
137
+ if (moving) rig.ph = (rig.ph + gone / stride) % 1;
138
+ rig.wait -= deltaSeconds;
139
139
  // which standing foot is furthest from its place, when the beast is at rest
140
140
  let worst = -1,
141
141
  worstBy = IDLE_SLACK * h,
142
142
  carried = 0;
143
- for (const L of Q.legs) carried += L.air;
143
+ for (const leg of rig.legs) carried += leg.air;
144
144
  for (let k = 0; k < 4; k++) {
145
- const L = Q.legs[k],
146
- held = M.legs && M.legs[k] ? M.legs[k].w : 0;
145
+ const leg = rig.legs[k],
146
+ held = pose.legs && pose.legs[k] ? pose.legs[k].w : 0;
147
147
  if (jumped || held > 0.5) {
148
148
  // not on the ground of its own accord: when it comes back it is under the body
149
- L.air = 0;
150
- L.wx = F.x + (L.fx * cos + L.fz * sin) * s;
151
- L.wz = F.z + (-L.fx * sin + L.fz * cos) * s;
149
+ leg.air = 0;
150
+ leg.wx = placement.x + (leg.fx * cos + leg.fz * sin) * s;
151
+ leg.wz = placement.z + (-leg.fx * sin + leg.fz * cos) * s;
152
152
  }
153
- const lx = ((L.wx - F.x) * cos - (L.wz - F.z) * sin) / s, // the planted (or lifted-from) point in the body's frame
154
- lz = ((L.wx - F.x) * sin + (L.wz - F.z) * cos) / s,
155
- off = Math.hypot(lx - L.fx, lz - L.fz);
153
+ const lx = ((leg.wx - placement.x) * cos - (leg.wz - placement.z) * sin) / s, // the planted (or lifted-from) point in the body's frame
154
+ lz = ((leg.wx - placement.x) * sin + (leg.wz - placement.z) * cos) / s,
155
+ off = Math.hypot(lx - leg.fx, lz - leg.fz);
156
156
  if (moving) {
157
- const phi = (Q.ph + G.phase[k] + 1) % 1;
158
- if (!L.air && ((phi >= G.duty && phi < G.duty + 0.5 * swing) || off > 0.75 * h)) {
159
- L.air = 1;
160
- L.s = clamp((phi - G.duty) / swing, 0, 0.5);
161
- } else if (L.air) L.s += gone / stride / swing;
162
- } else if (L.air) L.s += dt / IDLE_STEP;
163
- else if (!carried && Q.wait <= 0 && off > worstBy) {
157
+ const phi = (rig.ph + gait.phase[k] + 1) % 1;
158
+ if (!leg.air && ((phi >= gait.duty && phi < gait.duty + 0.5 * swing) || off > 0.75 * h)) {
159
+ leg.air = 1;
160
+ leg.s = clamp((phi - gait.duty) / swing, 0, 0.5);
161
+ } else if (leg.air) leg.s += gone / stride / swing;
162
+ } else if (leg.air) leg.s += deltaSeconds / IDLE_STEP;
163
+ else if (!carried && rig.wait <= 0 && off > worstBy) {
164
164
  worst = k;
165
165
  worstBy = off;
166
166
  }
167
- if (L.air) {
167
+ if (leg.air) {
168
168
  // carried from where it left the ground to where it will be wanted, both fixed in the world, so it leaves and lands still
169
- const left = Math.max(0, 1 - L.s),
170
- tz = L.fz + (moving ? dir * (travel / 2 + left * swing * stride) : 0),
171
- tx = F.x + (L.fx * cos + tz * sin) * s,
172
- tzw = F.z + (-L.fx * sin + tz * cos) * s;
173
- if (L.s >= 1) {
174
- L.air = 0;
175
- L.wx = tx;
176
- L.wz = tzw;
177
- L.x = L.fx;
178
- L.z = tz;
179
- L.lift = 0;
169
+ const left = Math.max(0, 1 - leg.s),
170
+ tz = leg.fz + (moving ? dir * (travel / 2 + left * swing * stride) : 0),
171
+ tx = placement.x + (leg.fx * cos + tz * sin) * s,
172
+ tzw = placement.z + (-leg.fx * sin + tz * cos) * s;
173
+ if (leg.s >= 1) {
174
+ leg.air = 0;
175
+ leg.wx = tx;
176
+ leg.wz = tzw;
177
+ leg.x = leg.fx;
178
+ leg.z = tz;
179
+ leg.lift = 0;
180
180
  } else {
181
- const e = L.s * L.s * (3 - 2 * L.s),
182
- wx = lerp(L.wx, tx, e),
183
- wz = lerp(L.wz, tzw, e);
184
- L.x = ((wx - F.x) * cos - (wz - F.z) * sin) / s;
185
- L.z = ((wx - F.x) * sin + (wz - F.z) * cos) / s;
186
- L.lift = (moving ? G.lift : 0.07) * h * Math.sin(Math.PI * L.s ** 0.8);
181
+ const e = leg.s * leg.s * (3 - 2 * leg.s),
182
+ wx = lerp(leg.wx, tx, e),
183
+ wz = lerp(leg.wz, tzw, e);
184
+ leg.x = ((wx - placement.x) * cos - (wz - placement.z) * sin) / s;
185
+ leg.z = ((wx - placement.x) * sin + (wz - placement.z) * cos) / s;
186
+ leg.lift = (moving ? gait.lift : 0.07) * h * Math.sin(Math.PI * leg.s ** 0.8);
187
187
  }
188
188
  } else {
189
- L.x = lx;
190
- L.z = lz;
191
- L.lift = 0;
189
+ leg.x = lx;
190
+ leg.z = lz;
191
+ leg.lift = 0;
192
192
  }
193
- const wx = F.x + (L.x * cos + L.z * sin) * s,
194
- wz = F.z + (-L.x * sin + L.z * cos) * s;
195
- L.floor = F.ground ? (F.ground(wx, wz) - F.y) / s : 0;
196
- L.y = L.floor + L.lift;
197
- L.stance = L.air ? 1 : clamp(0.5 + (dir * (L.fz - L.z)) / Math.max(travel, 0.3 * h), 0, 1);
193
+ const wx = placement.x + (leg.x * cos + leg.z * sin) * s,
194
+ wz = placement.z + (-leg.x * sin + leg.z * cos) * s;
195
+ leg.floor = placement.ground ? (placement.ground(wx, wz) - placement.y) / s : 0;
196
+ leg.y = leg.floor + leg.lift;
197
+ leg.stance = leg.air ? 1 : clamp(0.5 + (dir * (leg.fz - leg.z)) / Math.max(travel, 0.3 * h), 0, 1);
198
198
  }
199
199
  if (worst >= 0) {
200
- Q.legs[worst].air = 1;
201
- Q.legs[worst].s = 0;
202
- Q.wait = IDLE_STEP * 1.5;
200
+ rig.legs[worst].air = 1;
201
+ rig.legs[worst].s = 0;
202
+ rig.wait = IDLE_STEP * 1.5;
203
203
  }
204
204
  // the body: each girdle sinks as its legs take the weight and rises between, over the ground its own feet stand on
205
205
  const lively = sstep(0.03, 0.2, u),
206
- ease = Math.min(1, dt * 16);
206
+ ease = Math.min(1, deltaSeconds * 16);
207
207
  for (let g = 0; g < 2; g++) {
208
- const A = Q.legs[g * 2],
209
- B = Q.legs[g * 2 + 1],
210
- load = (A.air ? 0 : Math.sin(Math.PI * A.stance)) + (B.air ? 0 : Math.sin(Math.PI * B.stance));
211
- Q.rise[g] += (G.rise * h * lively * (1.27 * G.duty - load) - Q.rise[g]) * ease;
212
- Q.ground[g] += ((A.floor + B.floor) / 2 - Q.ground[g]) * Math.min(1, dt * 7);
208
+ const firstLegState = rig.legs[g * 2],
209
+ pairedLegState = rig.legs[g * 2 + 1],
210
+ load = (firstLegState.air ? 0 : Math.sin(Math.PI * firstLegState.stance)) + (pairedLegState.air ? 0 : Math.sin(Math.PI * pairedLegState.stance));
211
+ rig.rise[g] += (gait.rise * h * lively * (1.27 * gait.duty - load) - rig.rise[g]) * ease;
212
+ rig.ground[g] += ((firstLegState.floor + pairedLegState.floor) / 2 - rig.ground[g]) * Math.min(1, deltaSeconds * 7);
213
213
  }
214
- const yf = Q.rise[0] + Q.ground[0],
215
- yh = Q.rise[1] + Q.ground[1],
216
- pitch = -Math.atan2(yf - yh, Q.span) + M.pitch,
217
- joints = Q.joints,
218
- B = Q.bones,
219
- rootY = joints[0][1] + (yf + yh) / 2 + M.dy - Q.o.crouch * h * sstep(0.5, 1.8, u), // lower at speed, to reach further
220
- rootZ = joints[0][2] + M.dz,
221
- reachOf = g => (Q.legs[g * 2].z - Q.legs[g * 2].fz + Q.legs[g * 2 + 1].z - Q.legs[g * 2 + 1].fz) / (2 * h),
222
- chest = -G.flex * reachOf(0) + M.chest,
223
- hips = -G.flex * reachOf(1) + M.hips;
224
- Q.pitch = pitch;
225
- Q.y = rootY - joints[0][1];
226
- B[0].position.set(0, rootY, rootZ);
227
- B[0].rotation.x = pitch;
228
- B[BONE.chest].rotation.x = chest;
229
- B[BONE.hips].rotation.x = hips;
214
+ const yf = rig.rise[0] + rig.ground[0],
215
+ yh = rig.rise[1] + rig.ground[1],
216
+ pitch = -Math.atan2(yf - yh, rig.span) + pose.pitch,
217
+ joints = rig.joints,
218
+ bones = rig.bones,
219
+ rootY = joints[0][1] + (yf + yh) / 2 + pose.dy - rig.o.crouch * h * sstep(0.5, 1.8, u), // lower at speed, to reach further
220
+ rootZ = joints[0][2] + pose.dz,
221
+ reachOf = g => (rig.legs[g * 2].z - rig.legs[g * 2].fz + rig.legs[g * 2 + 1].z - rig.legs[g * 2 + 1].fz) / (2 * h),
222
+ chest = -gait.flex * reachOf(0) + pose.chest,
223
+ hips = -gait.flex * reachOf(1) + pose.hips;
224
+ rig.pitch = pitch;
225
+ rig.y = rootY - joints[0][1];
226
+ bones[0].position.set(0, rootY, rootZ);
227
+ bones[0].rotation.x = pitch;
228
+ bones[BONE.chest].rotation.x = chest;
229
+ bones[BONE.hips].rotation.x = hips;
230
230
  for (let k = 0; k < 4; k++) {
231
- const L = Q.legs[k],
232
- g = L.hind ? BONE.hips : BONE.chest,
233
- turn = pitch + (L.hind ? hips : chest),
231
+ const leg = rig.legs[k],
232
+ g = leg.hind ? BONE.hips : BONE.chest,
233
+ turn = pitch + (leg.hind ? hips : chest),
234
234
  // the girdle's joint, then the leg's pivot, turned with the body
235
235
  gy = joints[g][1] - joints[0][1],
236
236
  gz = joints[g][2] - joints[0][2],
237
237
  jy = rootY + gy * Math.cos(pitch) - gz * Math.sin(pitch),
238
238
  jz = rootZ + gy * Math.sin(pitch) + gz * Math.cos(pitch),
239
- glide = L.habit.glide * (L.z - L.fz), // a shoulder blade slides on the ribs with its leg's reach
240
- py = jy + L.py * Math.cos(turn) - (L.pz + glide) * Math.sin(turn),
241
- pz = jz + L.py * Math.sin(turn) + (L.pz + glide) * Math.cos(turn),
242
- held = M.legs && M.legs[k] ? M.legs[k] : null;
243
- B[L.bone].position.z = L.pz + glide;
244
- solveLeg(B, L, py, pz, turn, held, clamp(G.lift / 0.15, 0.5, 1.5));
239
+ glide = leg.habit.glide * (leg.z - leg.fz), // a shoulder blade slides on the ribs with its leg's reach
240
+ py = jy + leg.py * Math.cos(turn) - (leg.pz + glide) * Math.sin(turn),
241
+ pz = jz + leg.py * Math.sin(turn) + (leg.pz + glide) * Math.cos(turn),
242
+ held = pose.legs && pose.legs[k] ? pose.legs[k] : null;
243
+ bones[leg.bone].position.z = leg.pz + glide;
244
+ solveLeg(bones, leg, py, pz, turn, held, clamp(gait.lift / 0.15, 0.5, 1.5));
245
245
  }
246
246
  }
247
247
 
@@ -253,34 +253,34 @@ const _qa = new THREE.Quaternion(),
253
253
  // One leg, from the foot up: the foot's own tilt puts its upper joint, the cannon's slope the joint above, and the two long bones
254
254
  // close the gap to the pivot, the elbow bending back and the stifle forward. The leg works in a plane through its pivot, tipped
255
255
  // sideways to pass through the foot. eager: how high the gait carries its feet.
256
- function solveLeg(B, L, py, pz, turn, held, eager) {
257
- const H = L.habit,
258
- [l1, l2, l3, l4] = L.len,
259
- st = L.stance,
260
- off = L.air ? 0 : sstep(0.72, 1, st), // the heel coming off the ground at the end of its time there
261
- fold = L.air ? Math.sin(Math.PI * L.s) * eager : 0, // a leg folds more the higher the gait carries it
262
- carry = L.air ? (1 - L.s) * (1 - L.s) : 0,
263
- load = L.air ? 0 : Math.sin(Math.PI * st),
256
+ function solveLeg(bones, leg, py, pz, turn, held, eager) {
257
+ const habit = leg.habit,
258
+ [l1, l2, l3, l4] = leg.len,
259
+ st = leg.stance,
260
+ off = leg.air ? 0 : sstep(0.72, 1, st), // the heel coming off the ground at the end of its time there
261
+ fold = leg.air ? Math.sin(Math.PI * leg.s) * eager : 0, // a leg folds more the higher the gait carries it
262
+ carry = leg.air ? (1 - leg.s) * (1 - leg.s) : 0,
263
+ load = leg.air ? 0 : Math.sin(Math.PI * st),
264
264
  hold = held ? held.w : 0,
265
- fz = hold ? lerp(L.z, held.z, hold) : L.z,
266
- footY = hold ? lerp(L.y, held.y, hold) : L.y,
267
- side = L.x - L.px,
265
+ fz = hold ? lerp(leg.z, held.z, hold) : leg.z,
266
+ footY = hold ? lerp(leg.y, held.y, hold) : leg.y,
267
+ side = leg.x - leg.px,
268
268
  tip = Math.atan2(side, Math.max(0.05, py - footY)), // the plane's lean to the side
269
269
  fy = py - Math.hypot(py - footY, side) * Math.sign(py - footY || 1); // the foot's depth below the pivot, in that plane
270
- let a4 = L.rest[3] + H.heel4 * (off + carry) + H.fold4 * fold - H.sink * load;
271
- if (hold) a4 = lerp(a4, L.rest[3] + held.a4, hold);
270
+ let a4 = leg.rest[3] + habit.heel4 * (off + carry) + habit.fold4 * fold - habit.sink * load;
271
+ if (hold) a4 = lerp(a4, leg.rest[3] + held.a4, hold);
272
272
  const ty = fy + l4 * Math.cos(a4),
273
273
  tz = fz + l4 * Math.sin(a4);
274
- if (L.hind && H.paired && !hold) return pairedLeg(B, L, py, pz, ty, tz, a4, turn, tip, fold, off + carry);
275
- let a3 = L.rest[2] + H.follow * (slope(py, pz, ty, tz) - L.line) + H.heel3 * (off + carry) + H.fold3 * fold;
276
- if (hold) a3 = lerp(a3, L.rest[2] + held.a3, hold);
274
+ if (leg.hind && habit.paired && !hold) return pairedLeg(bones, leg, py, pz, ty, tz, a4, turn, tip, fold, off + carry);
275
+ let a3 = leg.rest[2] + habit.follow * (slope(py, pz, ty, tz) - leg.line) + habit.heel3 * (off + carry) + habit.fold3 * fold;
276
+ if (hold) a3 = lerp(a3, leg.rest[2] + held.a3, hold);
277
277
  const wy = ty + l3 * Math.cos(a3),
278
278
  wz = tz + l3 * Math.sin(a3),
279
279
  d = clamp(Math.hypot(wy - py, wz - pz), Math.abs(l1 - l2) + 1e-4, l1 + l2 - 1e-4),
280
280
  bend = Math.acos(clamp((l1 * l1 + d * d - l2 * l2) / (2 * l1 * d), -1, 1)),
281
- a1 = slope(py, pz, wy, wz) + (L.hind ? -bend : bend),
281
+ a1 = slope(py, pz, wy, wz) + (leg.hind ? -bend : bend),
282
282
  a2 = slope(py - l1 * Math.cos(a1), pz - l1 * Math.sin(a1), wy, wz);
283
- setLeg(B, L, turn, tip, a1, a2, a3, a4);
283
+ setLeg(bones, leg, turn, tip, a1, a2, a3, a4);
284
284
  }
285
285
 
286
286
  // A horse's hind leg (habit.paired): its stifle and hock are tied by tendons (the reciprocal apparatus), so they bend and
@@ -289,15 +289,15 @@ function solveLeg(B, L, py, pz, turn, held, eager) {
289
289
  // The thigh swings no further forward than habit.thigh past its rest, so the stifle stays under the hip.
290
290
  // (ty, tz): the fetlock, which the hoof's tilt a4 put; in the air the cannon folds a little more under the thigh (fold3), and it
291
291
  // opens with the heel as the hoof leaves the ground (heel3), the hock straightening to push off.
292
- function pairedLeg(B, L, py, pz, ty, tz, a4, turn, tip, fold, heel) {
293
- const [l1, l2, l3] = L.len,
294
- pair = L.rest[2] - L.rest[0] + L.habit.fold3 * fold + L.habit.heel3 * heel, // the cannon's angle from the thigh
292
+ function pairedLeg(bones, leg, py, pz, ty, tz, a4, turn, tip, fold, heel) {
293
+ const [l1, l2, l3] = leg.len,
294
+ pair = leg.rest[2] - leg.rest[0] + leg.habit.fold3 * fold + leg.habit.heel3 * heel, // the cannon's angle from the thigh
295
295
  ry = l1 + l3 * Math.cos(pair), // the pair as one arm from the hip, as if the thigh hung straight down
296
296
  rz = l3 * Math.sin(pair),
297
297
  r = Math.hypot(ry, rz),
298
298
  d = Math.max(eased(Math.hypot(py - ty, pz - tz), r + l2), Math.abs(r - l2) + 1e-4),
299
299
  open = Math.acos(clamp((r * r + d * d - l2 * l2) / (2 * r * d), -1, 1)),
300
- a1 = Math.max(slope(py, pz, ty, tz) - Math.atan2(rz, ry) - open, L.rest[0] - L.habit.thigh),
300
+ a1 = Math.max(slope(py, pz, ty, tz) - Math.atan2(rz, ry) - open, leg.rest[0] - leg.habit.thigh),
301
301
  sy = py - l1 * Math.cos(a1), // the stifle
302
302
  sz = pz - l1 * Math.sin(a1),
303
303
  // the gaskin and cannon close the gap from the stifle, the hock behind: as the pair would, unless the thigh was held back,
@@ -305,7 +305,7 @@ function pairedLeg(B, L, py, pz, ty, tz, a4, turn, tip, fold, heel) {
305
305
  reach = Math.max(eased(Math.hypot(sy - ty, sz - tz), l2 + l3), Math.abs(l2 - l3) + 1e-4),
306
306
  a2 = slope(sy, sz, ty, tz) + Math.acos(clamp((l2 * l2 + reach * reach - l3 * l3) / (2 * l2 * reach), -1, 1)),
307
307
  a3 = slope(sy - l2 * Math.cos(a2), sz - l2 * Math.sin(a2), ty, tz);
308
- setLeg(B, L, turn, tip, a1, a2, a3, a4);
308
+ setLeg(bones, leg, turn, tip, a1, a2, a3, a4);
309
309
  }
310
310
 
311
311
  // A reach d eased as it nears the most a chain can stretch: near straight, a little more reach turns its joints a long way, and
@@ -317,16 +317,16 @@ function eased(d, most) {
317
317
  }
318
318
 
319
319
  // Turns a leg's bones to the slopes found for them.
320
- function setLeg(B, L, turn, tip, a1, a2, a3, a4) {
321
- const b = L.bone,
322
- d1 = a1 - L.rest[0],
323
- d2 = a2 - L.rest[1],
324
- d3 = a3 - L.rest[2];
320
+ function setLeg(bones, leg, turn, tip, a1, a2, a3, a4) {
321
+ const firstBoneIndex = leg.bone,
322
+ d1 = a1 - leg.rest[0],
323
+ d2 = a2 - leg.rest[1],
324
+ d3 = a3 - leg.rest[2];
325
325
  // the upper bone: out of its girdle's turn, into the leaning plane, then its own swing
326
- B[b].quaternion.setFromAxisAngle(X_AXIS, -turn).multiply(_qa.setFromAxisAngle(Z_AXIS, tip)).multiply(_qb.setFromAxisAngle(X_AXIS, d1));
327
- B[b + 1].rotation.x = d2 - d1;
328
- B[b + 2].rotation.x = d3 - d2;
329
- B[b + 3].rotation.x = a4 - L.rest[3] - d3;
326
+ bones[firstBoneIndex].quaternion.setFromAxisAngle(X_AXIS, -turn).multiply(_qa.setFromAxisAngle(Z_AXIS, tip)).multiply(_qb.setFromAxisAngle(X_AXIS, d1));
327
+ bones[firstBoneIndex + 1].rotation.x = d2 - d1;
328
+ bones[firstBoneIndex + 2].rotation.x = d3 - d2;
329
+ bones[firstBoneIndex + 3].rotation.x = a4 - leg.rest[3] - d3;
330
330
  }
331
331
 
332
332
  export { BONE, LEGS, makeRig, stepRig };
package/motion/spaces.js CHANGED
@@ -1,21 +1,21 @@
1
- // Keep common cells as small numeric keys. A row outside the packed range uses
2
- // a separate key so two distant places can never share a collider bucket.
3
- function cellKey(column, row) {
4
- if (row >= -2048 && row < 2048 && Math.abs(column) < 2 ** 39) return (column + 2048) * 4096 + row + 2048;
5
- return column + ',' + row;
6
- }
1
+ /**
2
+ * Keep terrain heights, raised decks and collision queries together for each space the player can enter.
3
+ * @module
4
+ */
5
+
6
+ import { cellKey } from '../math/cell-key.js';
7
7
 
8
8
  /** Highest rotated rectangular deck at a point. k is rise per unit of local z. */
9
9
  export function deckTop(list, x, z) {
10
10
  let best = -Infinity;
11
11
  for (let i = 0; i < list.length; i++) {
12
- const d = list[i],
13
- dx = x - d.x,
14
- dz = z - d.z,
15
- lx = dx * d.c - dz * d.s,
16
- lz = dx * d.s + dz * d.c;
17
- if (Math.abs(lx) <= d.hx && Math.abs(lz) <= d.hz) {
18
- const y = d.k ? d.y + lz * d.k : d.y;
12
+ const deck = list[i],
13
+ dx = x - deck.x,
14
+ dz = z - deck.z,
15
+ lx = dx * deck.c - dz * deck.s,
16
+ lz = dx * deck.s + dz * deck.c;
17
+ if (Math.abs(lx) <= deck.hx && Math.abs(lz) <= deck.hz) {
18
+ const y = deck.k ? deck.y + lz * deck.k : deck.y;
19
19
  if (y > best) best = y;
20
20
  }
21
21
  }
@@ -134,27 +134,27 @@ export function createSpaces(definition, { deckCellSize = 32, colliderCellSize =
134
134
  if (!list) break;
135
135
  let any = false;
136
136
  for (let i = 0; i < list.length; i++) {
137
- const c = list[i],
138
- dx = x - c.x,
139
- dz = z - c.z;
140
- if (c.r !== undefined) {
141
- const d = Math.hypot(dx, dz),
142
- clearance = c.r + r;
143
- if (d < clearance && d > 1e-4) {
144
- x = c.x + (dx / d) * clearance;
145
- z = c.z + (dz / d) * clearance;
137
+ const collider = list[i],
138
+ dx = x - collider.x,
139
+ dz = z - collider.z;
140
+ if (collider.r !== undefined) {
141
+ const distance = Math.hypot(dx, dz),
142
+ clearance = collider.r + r;
143
+ if (distance < clearance && distance > 1e-4) {
144
+ x = collider.x + (dx / distance) * clearance;
145
+ z = collider.z + (dz / distance) * clearance;
146
146
  any = true;
147
147
  }
148
148
  } else {
149
- let lx = dx * c.c - dz * c.s,
150
- lz = dx * c.s + dz * c.c;
151
- const px = c.hx + r - Math.abs(lx),
152
- pz = c.hz + r - Math.abs(lz);
149
+ let lx = dx * collider.c - dz * collider.s,
150
+ lz = dx * collider.s + dz * collider.c;
151
+ const px = collider.hx + r - Math.abs(lx),
152
+ pz = collider.hz + r - Math.abs(lz);
153
153
  if (px > 0 && pz > 0) {
154
- if (px < pz) lx = (lx < 0 ? -1 : 1) * (c.hx + r);
155
- else lz = (lz < 0 ? -1 : 1) * (c.hz + r);
156
- x = c.x + lx * c.c + lz * c.s;
157
- z = c.z - lx * c.s + lz * c.c;
154
+ if (px < pz) lx = (lx < 0 ? -1 : 1) * (collider.hx + r);
155
+ else lz = (lz < 0 ? -1 : 1) * (collider.hz + r);
156
+ x = collider.x + lx * collider.c + lz * collider.s;
157
+ z = collider.z - lx * collider.s + lz * collider.c;
158
158
  any = true;
159
159
  }
160
160
  }
@@ -172,8 +172,8 @@ export function createSpaces(definition, { deckCellSize = 32, colliderCellSize =
172
172
  function steer(x, z, want, step, r, lean = 1) {
173
173
  if (collide(x, z, r)) return want;
174
174
  for (let i = 0; i < turns.length; i++) {
175
- const a = want + turns[i] * lean;
176
- if (!collide(x + Math.sin(a) * step, z + Math.cos(a) * step, r)) return a;
175
+ const heading = want + turns[i] * lean;
176
+ if (!collide(x + Math.sin(heading) * step, z + Math.cos(heading) * step, r)) return heading;
177
177
  }
178
178
  return undefined;
179
179
  }