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.
- package/CONTRIBUTING.md +3 -1
- package/README.md +19 -21
- package/geometry/bedding.js +141 -0
- package/geometry/builder-cells.js +42 -0
- package/geometry/builder-frame.js +47 -0
- package/geometry/builder.js +189 -177
- package/geometry/cell-mask.js +25 -0
- package/geometry/creased-sheet.js +34 -34
- package/geometry/ear.js +36 -36
- package/geometry/erosion.js +65 -42
- package/geometry/face-grid.js +65 -65
- package/geometry/foliage.js +47 -0
- package/geometry/hair-gathers.js +41 -41
- package/geometry/hair-locks.js +41 -41
- package/geometry/hair-patch.js +18 -18
- package/geometry/hair-shell.js +31 -31
- package/geometry/head-profile.js +15 -15
- package/geometry/head-shape.js +77 -77
- package/geometry/heightfield.js +27 -18
- package/geometry/index-parts.js +60 -0
- package/geometry/instance-visibility.js +41 -0
- package/geometry/instances.js +35 -12
- package/geometry/outline-grid.js +7 -7
- package/geometry/part-ranges.js +18 -0
- package/geometry/path-field.js +63 -33
- package/geometry/placement.js +52 -52
- package/geometry/point-buckets.js +50 -0
- package/geometry/primitives.js +63 -47
- package/geometry/sagging-tube.js +27 -27
- package/geometry/sampled-surface.js +109 -77
- package/geometry/scatter-grid.js +46 -6
- package/geometry/sculpt.js +93 -93
- package/geometry/shore-grid.js +77 -34
- package/geometry/strata-field.js +92 -92
- package/geometry/strata-tiles.js +91 -91
- package/geometry/stream.js +70 -48
- package/geometry/terrain-chunks.js +101 -92
- package/geometry/terrain-extension.js +6 -6
- package/index.js +5 -0
- package/math/cell-edge.js +16 -16
- package/math/cell-key.js +6 -0
- package/math/circle-clearances.js +19 -0
- package/math/cloth-textures.js +6 -6
- package/math/cloud-textures.js +20 -20
- package/math/color.js +14 -14
- package/math/curve.js +22 -22
- package/math/mountain-ranges.js +216 -165
- package/math/noise.js +101 -49
- package/math/particle-noise.js +28 -28
- package/math/periodic.js +5 -5
- package/math/random.js +24 -15
- package/math/rectangles.js +52 -0
- package/math/rock-texture.js +7 -7
- package/math/scalar.js +5 -5
- package/math/surface-textures.js +54 -54
- package/motion/arm-reach.js +24 -24
- package/motion/ballistic-arc.js +11 -0
- package/motion/biped-cycle.js +44 -0
- package/motion/biped-leg.js +37 -0
- package/motion/biped-stride.js +98 -122
- package/motion/contact-transition.js +33 -0
- package/motion/crowd-alarms.js +39 -39
- package/motion/crowd-routine.js +94 -65
- package/motion/daily-clock.js +9 -9
- package/motion/deck-surface.js +38 -0
- package/motion/ground-lookahead.js +29 -0
- package/motion/instance-sight.js +8 -8
- package/motion/player.js +36 -28
- package/motion/quadruped.js +155 -155
- package/motion/spaces.js +32 -32
- package/motion/weather.js +73 -24
- package/motion/wind.js +32 -13
- package/package.json +8 -4
- package/rendering/caster-motion.js +5 -5
- package/rendering/cell-lod.js +147 -82
- package/rendering/cloud-dome.js +28 -28
- package/rendering/cloud-field.js +2 -2
- package/rendering/compilation-schedule.js +68 -11
- package/rendering/contact-shadows.js +2 -2
- package/rendering/context.js +12 -0
- package/rendering/crowd-skinning.js +13 -13
- package/rendering/crust.js +18 -12
- package/rendering/daylight.js +3 -3
- package/rendering/detail-range.js +28 -0
- package/rendering/dispose-meshes.js +18 -0
- package/rendering/dof.js +9 -9
- package/rendering/eye-adapt.js +14 -14
- package/rendering/far-shadow.js +8 -8
- package/rendering/flocks.js +78 -78
- package/rendering/garment-material.js +17 -17
- package/rendering/glow-lines.js +13 -13
- package/rendering/hair-strands.js +20 -20
- package/rendering/height-probe.js +149 -19
- package/rendering/label-layout.js +46 -0
- package/rendering/label-range.js +38 -0
- package/rendering/light-budget.js +52 -52
- package/rendering/light-shafts.js +23 -23
- package/rendering/local-lights.js +15 -4
- package/rendering/material-lod.js +49 -49
- package/rendering/particle-emitters.js +90 -90
- package/rendering/particles.js +78 -67
- package/rendering/piece-batch.js +91 -26
- package/rendering/planar-reflection.js +38 -38
- package/rendering/post-shaders.js +17 -13
- package/rendering/post.js +71 -71
- package/rendering/rain-streaks.js +67 -0
- package/rendering/road-surface.js +11 -11
- package/rendering/sea-shaders.js +11 -11
- package/rendering/shader-warmup.js +29 -16
- package/rendering/shadow-alignment.js +4 -4
- package/rendering/shadow-depth.js +7 -7
- package/rendering/sky-occlusion.js +16 -16
- package/rendering/sky.js +16 -6
- package/rendering/stage-lights.js +6 -6
- package/rendering/static-point-shadow.js +3 -3
- package/rendering/still-casters.js +1 -1
- package/rendering/strata-material.js +10 -10
- package/rendering/stream-flow.js +10 -10
- package/rendering/taa.js +26 -26
- package/rendering/talus.js +15 -15
- package/rendering/terrain-rock.js +7 -7
- package/rendering/texture-fills.js +8 -8
- package/rendering/textures.js +28 -28
- package/rendering/wakes.js +24 -24
- package/rendering/water-material.js +36 -23
- package/rendering/world-matrices.js +38 -38
- package/runtime/diagnostic-log.js +2 -2
- package/runtime/first-use.js +33 -28
- package/runtime/frame-budget.js +49 -0
- package/runtime/frame-pacing.js +33 -0
- package/runtime/gpu-timing.js +13 -10
- package/runtime/grid-cache.js +1 -1
- package/runtime/grid-rows.js +44 -5
- package/runtime/hand-over.js +41 -23
- package/runtime/pool.js +58 -16
- package/runtime/quality-fit.js +1 -1
- package/runtime/search.js +22 -22
- package/runtime/spatial-audio.js +13 -13
- package/runtime/withdrawable-task.js +35 -0
- package/runtime/worker.js +7 -0
package/motion/quadruped.js
CHANGED
|
@@ -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
|
|
32
|
-
|
|
33
|
-
t = clamp((u -
|
|
34
|
-
out.travel = lerp(
|
|
35
|
-
out.duty = lerp(
|
|
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
|
|
38
|
-
|
|
39
|
-
out.phase[k] =
|
|
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(
|
|
42
|
-
out.lift = lerp(
|
|
43
|
-
out.flex = lerp(
|
|
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,
|
|
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
|
|
53
|
-
if (
|
|
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[
|
|
56
|
-
bones[
|
|
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((
|
|
59
|
+
const legs = LEGS.map((legBone, k) => {
|
|
60
60
|
const girdle = joints[k > 1 ? BONE.hips : BONE.chest],
|
|
61
|
-
pts = [joints[
|
|
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:
|
|
69
|
+
bone: legBone,
|
|
70
70
|
hind: k > 1,
|
|
71
|
-
habit: k > 1 ?
|
|
72
|
-
px: joints[
|
|
73
|
-
py: joints[
|
|
74
|
-
pz: joints[
|
|
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[
|
|
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:
|
|
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
|
|
115
|
-
// floor }.
|
|
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(
|
|
118
|
-
const s =
|
|
119
|
-
sin = Math.sin(
|
|
120
|
-
cos = Math.cos(
|
|
121
|
-
h =
|
|
122
|
-
|
|
123
|
-
jumped =
|
|
124
|
-
fwd = jumped ? 0 : ((
|
|
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
|
-
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
const u = (
|
|
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
|
-
|
|
133
|
-
gaitAt(
|
|
134
|
-
const travel =
|
|
135
|
-
stride = travel /
|
|
136
|
-
swing = 1 -
|
|
137
|
-
if (moving)
|
|
138
|
-
|
|
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
|
|
143
|
+
for (const leg of rig.legs) carried += leg.air;
|
|
144
144
|
for (let k = 0; k < 4; k++) {
|
|
145
|
-
const
|
|
146
|
-
held =
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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 = ((
|
|
154
|
-
lz = ((
|
|
155
|
-
off = Math.hypot(lx -
|
|
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 = (
|
|
158
|
-
if (!
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
} else if (
|
|
162
|
-
} else if (
|
|
163
|
-
else if (!carried &&
|
|
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 (
|
|
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 -
|
|
170
|
-
tz =
|
|
171
|
-
tx =
|
|
172
|
-
tzw =
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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 =
|
|
182
|
-
wx = lerp(
|
|
183
|
-
wz = lerp(
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
189
|
+
leg.x = lx;
|
|
190
|
+
leg.z = lz;
|
|
191
|
+
leg.lift = 0;
|
|
192
192
|
}
|
|
193
|
-
const wx =
|
|
194
|
-
wz =
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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,
|
|
206
|
+
ease = Math.min(1, deltaSeconds * 16);
|
|
207
207
|
for (let g = 0; g < 2; g++) {
|
|
208
|
-
const
|
|
209
|
-
|
|
210
|
-
load = (
|
|
211
|
-
|
|
212
|
-
|
|
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 =
|
|
215
|
-
yh =
|
|
216
|
-
pitch = -Math.atan2(yf - yh,
|
|
217
|
-
joints =
|
|
218
|
-
|
|
219
|
-
rootY = joints[0][1] + (yf + yh) / 2 +
|
|
220
|
-
rootZ = joints[0][2] +
|
|
221
|
-
reachOf = g => (
|
|
222
|
-
chest = -
|
|
223
|
-
hips = -
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
|
232
|
-
g =
|
|
233
|
-
turn = pitch + (
|
|
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 =
|
|
240
|
-
py = jy +
|
|
241
|
-
pz = jz +
|
|
242
|
-
held =
|
|
243
|
-
|
|
244
|
-
solveLeg(
|
|
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(
|
|
257
|
-
const
|
|
258
|
-
[l1, l2, l3, l4] =
|
|
259
|
-
st =
|
|
260
|
-
off =
|
|
261
|
-
fold =
|
|
262
|
-
carry =
|
|
263
|
-
load =
|
|
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(
|
|
266
|
-
footY = hold ? lerp(
|
|
267
|
-
side =
|
|
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 =
|
|
271
|
-
if (hold) a4 = lerp(a4,
|
|
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 (
|
|
275
|
-
let a3 =
|
|
276
|
-
if (hold) a3 = lerp(a3,
|
|
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) + (
|
|
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(
|
|
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(
|
|
293
|
-
const [l1, l2, l3] =
|
|
294
|
-
pair =
|
|
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,
|
|
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(
|
|
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(
|
|
321
|
-
const
|
|
322
|
-
d1 = a1 -
|
|
323
|
-
d2 = a2 -
|
|
324
|
-
d3 = a3 -
|
|
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
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
|
13
|
-
dx = x -
|
|
14
|
-
dz = z -
|
|
15
|
-
lx = dx *
|
|
16
|
-
lz = dx *
|
|
17
|
-
if (Math.abs(lx) <=
|
|
18
|
-
const 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
|
|
138
|
-
dx = x -
|
|
139
|
-
dz = z -
|
|
140
|
-
if (
|
|
141
|
-
const
|
|
142
|
-
clearance =
|
|
143
|
-
if (
|
|
144
|
-
x =
|
|
145
|
-
z =
|
|
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 *
|
|
150
|
-
lz = dx *
|
|
151
|
-
const px =
|
|
152
|
-
pz =
|
|
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) * (
|
|
155
|
-
else lz = (lz < 0 ? -1 : 1) * (
|
|
156
|
-
x =
|
|
157
|
-
z =
|
|
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
|
|
176
|
-
if (!collide(x + Math.sin(
|
|
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
|
}
|