roamwright 0.1.0 → 0.2.0
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 +39 -8
- package/README.md +53 -24
- package/examples/viaduct-camera.js +40 -0
- package/examples/viaduct-landscape.js +81 -0
- package/examples/viaduct.css +75 -0
- package/examples/viaduct.html +47 -0
- package/examples/viaduct.js +332 -0
- package/geometry/builder.js +11 -14
- package/geometry/creased-sheet.js +127 -0
- package/geometry/ear.js +97 -0
- package/geometry/erosion.js +106 -0
- package/geometry/face-grid.js +143 -0
- package/geometry/garment-tags.js +11 -0
- package/geometry/hair-gathers.js +174 -0
- package/geometry/hair-locks.js +156 -0
- package/geometry/hair-patch.js +42 -0
- package/geometry/hair-shell.js +186 -0
- package/geometry/hair-tags.js +6 -0
- package/geometry/head-profile.js +138 -0
- package/geometry/head-shape.js +499 -0
- package/geometry/heightfield.js +68 -0
- package/geometry/outline-grid.js +46 -0
- package/geometry/path-field.js +65 -0
- package/geometry/sagging-tube.js +58 -0
- package/geometry/sampled-surface.js +101 -0
- package/geometry/scatter-grid.js +32 -0
- package/geometry/sculpt.js +263 -0
- package/geometry/shore-grid.js +67 -0
- package/geometry/strata-field.js +339 -0
- package/geometry/strata-tiles.js +215 -0
- package/geometry/stream.js +94 -0
- package/geometry/terrain-chunks.js +300 -0
- package/geometry/terrain-extension.js +44 -0
- package/math/cell-edge.js +44 -0
- package/math/cloth-textures.js +83 -0
- package/math/cloud-textures.js +112 -0
- package/math/color.js +29 -0
- package/math/curve.js +3 -4
- package/math/mountain-ranges.js +463 -0
- package/math/particle-noise.js +48 -0
- package/math/periodic.js +20 -0
- package/math/rock-texture.js +56 -0
- package/math/surface-textures.js +241 -0
- package/motion/arm-reach.js +74 -0
- package/motion/biped-stride.js +276 -0
- package/motion/crowd-alarms.js +127 -0
- package/motion/crowd-routine.js +183 -0
- package/motion/daily-clock.js +59 -0
- package/motion/instance-sight.js +84 -0
- package/motion/quadruped.js +332 -0
- package/motion/weather.js +59 -0
- package/motion/wind.js +27 -0
- package/package.json +6 -4
- package/rendering/caster-motion.js +79 -0
- package/rendering/cell-lod.js +175 -0
- package/rendering/cloud-dome.js +371 -0
- package/rendering/cloud-field.js +83 -0
- package/rendering/compilation-schedule.js +34 -0
- package/rendering/contact-shadows.js +79 -0
- package/rendering/crowd-skinning.js +133 -0
- package/rendering/crust.js +28 -0
- package/rendering/daylight.js +50 -0
- package/rendering/far-shadow.js +186 -0
- package/rendering/flocks.js +323 -0
- package/rendering/garment-material.js +110 -0
- package/rendering/glow-lines.js +74 -0
- package/rendering/glow-shell.js +90 -0
- package/rendering/hair-strands.js +67 -0
- package/rendering/height-haze.js +129 -0
- package/rendering/height-probe.js +91 -0
- package/rendering/interior-mapping.js +19 -0
- package/rendering/light-budget.js +222 -0
- package/rendering/light-points.js +51 -0
- package/rendering/light-shafts.js +131 -0
- package/rendering/local-lights.js +28 -0
- package/rendering/low-light.js +13 -0
- package/rendering/material-lod.js +110 -0
- package/rendering/particle-emitters.js +223 -0
- package/rendering/particles.js +297 -0
- package/rendering/planar-reflection.js +175 -0
- package/rendering/post-shaders.js +21 -1
- package/rendering/post.js +8 -5
- package/rendering/rain-rings.js +22 -0
- package/rendering/road-surface.js +63 -0
- package/rendering/sea-shaders.js +230 -0
- package/rendering/shader-warmup.js +197 -0
- package/rendering/shadow-alignment.js +28 -0
- package/rendering/shadow-depth.js +129 -0
- package/rendering/sky-occlusion.js +187 -0
- package/rendering/sky.js +169 -0
- package/rendering/stage-lights.js +209 -0
- package/rendering/static-point-shadow.js +119 -0
- package/rendering/still-casters.js +6 -11
- package/rendering/strata-material.js +93 -0
- package/rendering/stream-flow.js +36 -0
- package/rendering/surface-light.js +22 -0
- package/rendering/surface-projection.js +69 -0
- package/rendering/talus.js +26 -0
- package/rendering/terrain-rock.js +48 -0
- package/rendering/texture-fills.js +68 -0
- package/rendering/viewport.js +9 -0
- package/rendering/wakes.js +99 -0
- package/rendering/water-material.js +288 -0
- package/rendering/world-matrices.js +4 -6
- package/runtime/device-quality.js +27 -0
- package/runtime/diagnostic-log.js +80 -0
- package/runtime/events.js +14 -0
- package/runtime/first-use.js +3 -1
- package/runtime/frame-yield.js +14 -0
- package/runtime/grid-cache.js +88 -0
- package/runtime/grid-rows.js +9 -0
- package/runtime/module-preload.js +15 -0
- package/runtime/quality-fit.js +5 -9
- package/runtime/search.js +69 -0
- package/runtime/spatial-audio.js +57 -0
- package/scripts/serve.py +26 -0
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import { clamp, lerp, sstep } from '../math/scalar.js';
|
|
3
|
+
|
|
4
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
5
|
+
// A four-legged body in motion: the skeleton a hound and a horse share, and what moves it.
|
|
6
|
+
// A foot is planted in the world and stays where it was put while the body passes over it; when its turn comes it is
|
|
7
|
+
// lifted, carried to where it will next be needed and set down again. Each leg is then solved back from its foot to
|
|
8
|
+
// its shoulder or hip, so no paw or hoof slides, on a slope, in a turn, or while the pace changes. Which foot's turn
|
|
9
|
+
// it is comes from the gait: the order and timing of the four footfalls, chosen by the speed against the leg's length
|
|
10
|
+
// (so a lapdog trots where a horse still walks). The body's rise and fall, its rocking and the flexing of its back
|
|
11
|
+
// follow from which feet carry it.
|
|
12
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
13
|
+
// Bones: 0 root, 1 hips, 2 chest, 3-4 neck, 5 head, 6 jaw, 7-8 ears (left, right), 9 lip, then four to a leg (upper, lower,
|
|
14
|
+
// cannon or pastern, foot) for the fore left, fore right, hind left and hind right, 26-28 the tail, 29 the crest (the tips of
|
|
15
|
+
// hackles or a mane).
|
|
16
|
+
const BONE = { root: 0, hips: 1, chest: 2, neck: 3, neck2: 4, head: 5, jaw: 6, earL: 7, earR: 8, lip: 9, tail: 26, crest: 29 },
|
|
17
|
+
LEGS = [10, 14, 18, 22],
|
|
18
|
+
PARENT = [-1, 0, 0, 2, 3, 4, 5, 5, 5, 5, 2, 10, 11, 12, 2, 14, 15, 16, 1, 18, 19, 20, 1, 22, 23, 24, 1, 26, 27, 2],
|
|
19
|
+
GRAVITY = 9.81,
|
|
20
|
+
IDLE_STEP = 0.24, // seconds a foot takes to shift while the beast stands
|
|
21
|
+
IDLE_SLACK = 0.14; // how far a standing foot may be from its place, in leg lengths, before it shifts
|
|
22
|
+
|
|
23
|
+
// The angle of a limb segment in the body's upright plane: 0 hanging straight down, positive with its lower end to the rear.
|
|
24
|
+
const slope = (y0, z0, y1, z1) => Math.atan2(z0 - z1, y0 - y1);
|
|
25
|
+
|
|
26
|
+
// A gait at a speed: `keys` are [speed, stance travel, duty, [phase of each foot], rise, lift, flex] in order of speed (the speed
|
|
27
|
+
// is against the leg's length, lengths are in leg lengths); between two keys everything is blended, the phases the short way round.
|
|
28
|
+
function gaitAt(keys, u, out) {
|
|
29
|
+
let i = 0;
|
|
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);
|
|
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;
|
|
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);
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
|
|
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
|
+
// o: its kind's gaits and the habits of its legs.
|
|
49
|
+
function makeRig(joints, feet, o) {
|
|
50
|
+
const bones = joints.map(() => new THREE.Bone());
|
|
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]);
|
|
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]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const legs = LEGS.map((b, k) => {
|
|
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]],
|
|
62
|
+
len = [],
|
|
63
|
+
rest = [];
|
|
64
|
+
for (let i = 0; i < 4; i++) {
|
|
65
|
+
len.push(Math.hypot(pts[i + 1][1] - pts[i][1], pts[i + 1][2] - pts[i][2]));
|
|
66
|
+
rest.push(slope(pts[i][1], pts[i][2], pts[i + 1][1], pts[i + 1][2]));
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
bone: b,
|
|
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],
|
|
75
|
+
fx: feet[k][0], // the foot's own place under the body
|
|
76
|
+
fz: feet[k][2],
|
|
77
|
+
len,
|
|
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
|
|
80
|
+
air: 0, // 1 while the foot is being carried
|
|
81
|
+
s: 0, // how far through its step
|
|
82
|
+
wx: 0, // where it is planted, or was lifted from, in the world
|
|
83
|
+
wz: 0,
|
|
84
|
+
x: feet[k][0], // where it is now, in the body's frame
|
|
85
|
+
y: 0,
|
|
86
|
+
z: feet[k][2],
|
|
87
|
+
stance: 0.5, // how far through its time on the ground
|
|
88
|
+
};
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
bones,
|
|
92
|
+
legs,
|
|
93
|
+
o,
|
|
94
|
+
joints,
|
|
95
|
+
leg: joints[LEGS[2]][1], // the hip's height: the length everything is measured against
|
|
96
|
+
span: joints[LEGS[0]][2] - joints[LEGS[2]][2], // from hip to shoulder
|
|
97
|
+
ph: o.phase ?? Math.random(),
|
|
98
|
+
beat: 0, // the quarter of the stride its hooves last struck in
|
|
99
|
+
v: 0,
|
|
100
|
+
u: 0, // its speed against its leg's length
|
|
101
|
+
px: null,
|
|
102
|
+
pz: 0,
|
|
103
|
+
wait: 0,
|
|
104
|
+
rise: [0, 0], // the fore and hind girdles' rise with the stride,
|
|
105
|
+
ground: [0, 0], // and the height of the ground under each
|
|
106
|
+
pitch: 0,
|
|
107
|
+
y: 0,
|
|
108
|
+
gait: { travel: 0.5, duty: 0.6, phase: [0, 0, 0, 0], rise: 0, lift: 0.1, flex: 0 },
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const _free = { dy: 0, dz: 0, pitch: 0, hips: 0, chest: 0, legs: null };
|
|
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
|
|
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,
|
|
125
|
+
gone = Math.abs(fwd),
|
|
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),
|
|
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;
|
|
139
|
+
// which standing foot is furthest from its place, when the beast is at rest
|
|
140
|
+
let worst = -1,
|
|
141
|
+
worstBy = IDLE_SLACK * h,
|
|
142
|
+
carried = 0;
|
|
143
|
+
for (const L of Q.legs) carried += L.air;
|
|
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;
|
|
147
|
+
if (jumped || held > 0.5) {
|
|
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;
|
|
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);
|
|
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) {
|
|
164
|
+
worst = k;
|
|
165
|
+
worstBy = off;
|
|
166
|
+
}
|
|
167
|
+
if (L.air) {
|
|
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;
|
|
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);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
L.x = lx;
|
|
190
|
+
L.z = lz;
|
|
191
|
+
L.lift = 0;
|
|
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);
|
|
198
|
+
}
|
|
199
|
+
if (worst >= 0) {
|
|
200
|
+
Q.legs[worst].air = 1;
|
|
201
|
+
Q.legs[worst].s = 0;
|
|
202
|
+
Q.wait = IDLE_STEP * 1.5;
|
|
203
|
+
}
|
|
204
|
+
// the body: each girdle sinks as its legs take the weight and rises between, over the ground its own feet stand on
|
|
205
|
+
const lively = sstep(0.03, 0.2, u),
|
|
206
|
+
ease = Math.min(1, dt * 16);
|
|
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);
|
|
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;
|
|
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),
|
|
234
|
+
// the girdle's joint, then the leg's pivot, turned with the body
|
|
235
|
+
gy = joints[g][1] - joints[0][1],
|
|
236
|
+
gz = joints[g][2] - joints[0][2],
|
|
237
|
+
jy = rootY + gy * Math.cos(pitch) - gz * Math.sin(pitch),
|
|
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));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const _qa = new THREE.Quaternion(),
|
|
249
|
+
_qb = new THREE.Quaternion(),
|
|
250
|
+
X_AXIS = new THREE.Vector3(1, 0, 0),
|
|
251
|
+
Z_AXIS = new THREE.Vector3(0, 0, 1);
|
|
252
|
+
|
|
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
|
+
// 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
|
+
// 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),
|
|
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,
|
|
268
|
+
tip = Math.atan2(side, Math.max(0.05, py - footY)), // the plane's lean to the side
|
|
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);
|
|
272
|
+
const ty = fy + l4 * Math.cos(a4),
|
|
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);
|
|
277
|
+
const wy = ty + l3 * Math.cos(a3),
|
|
278
|
+
wz = tz + l3 * Math.sin(a3),
|
|
279
|
+
d = clamp(Math.hypot(wy - py, wz - pz), Math.abs(l1 - l2) + 1e-4, l1 + l2 - 1e-4),
|
|
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),
|
|
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);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// A horse's hind leg (habit.paired): its stifle and hock are tied by tendons (the reciprocal apparatus), so they bend and
|
|
287
|
+
// straighten together and the cannon stays about parallel to the thigh. The thigh and cannon then turn about the hip as one
|
|
288
|
+
// pair, and the gaskin closes the gap from the stifle down and back to the hock: the hock never rides forward under the belly.
|
|
289
|
+
// The thigh swings no further forward than habit.thigh past its rest, so the stifle stays under the hip.
|
|
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
|
+
// 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
|
|
295
|
+
ry = l1 + l3 * Math.cos(pair), // the pair as one arm from the hip, as if the thigh hung straight down
|
|
296
|
+
rz = l3 * Math.sin(pair),
|
|
297
|
+
r = Math.hypot(ry, rz),
|
|
298
|
+
d = Math.max(eased(Math.hypot(py - ty, pz - tz), r + l2), Math.abs(r - l2) + 1e-4),
|
|
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),
|
|
301
|
+
sy = py - l1 * Math.cos(a1), // the stifle
|
|
302
|
+
sz = pz - l1 * Math.sin(a1),
|
|
303
|
+
// the gaskin and cannon close the gap from the stifle, the hock behind: as the pair would, unless the thigh was held back,
|
|
304
|
+
// and then the hock folds the more
|
|
305
|
+
reach = Math.max(eased(Math.hypot(sy - ty, sz - tz), l2 + l3), Math.abs(l2 - l3) + 1e-4),
|
|
306
|
+
a2 = slope(sy, sz, ty, tz) + Math.acos(clamp((l2 * l2 + reach * reach - l3 * l3) / (2 * l2 * reach), -1, 1)),
|
|
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);
|
|
309
|
+
}
|
|
310
|
+
|
|
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
|
|
312
|
+
// a leg pushing off or landing would snap; past EASE of the most it comes in ever slower, and never quite gets there.
|
|
313
|
+
const EASE = 0.92;
|
|
314
|
+
function eased(d, most) {
|
|
315
|
+
const over = d / most - EASE;
|
|
316
|
+
return over <= 0 ? d : most * (EASE + (1 - EASE) * (1 - Math.exp(-over / (1 - EASE))));
|
|
317
|
+
}
|
|
318
|
+
|
|
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];
|
|
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;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
export { BONE, LEGS, makeRig, stepRig };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { sstep } from '../math/scalar.js';
|
|
2
|
+
|
|
3
|
+
export function createWeather({
|
|
4
|
+
random = Math.random,
|
|
5
|
+
dawn: DAWN = 0.25,
|
|
6
|
+
mistWidth: MIST_HOURS = 0.032,
|
|
7
|
+
fullReach: SPELL_FADE = 0.55,
|
|
8
|
+
dustRise: DUST_RISES = 18,
|
|
9
|
+
dustSettle: DUST_SETTLES = 30,
|
|
10
|
+
} = {}) {
|
|
11
|
+
const clock = { land: null, on: false, next: 0 };
|
|
12
|
+
const between = ([least, most]) => least + random() * (most - least);
|
|
13
|
+
// v moved toward goal by at most step.
|
|
14
|
+
const toward = (v, goal, step) => (v < goal ? Math.min(goal, v + step) : Math.max(goal, v - step));
|
|
15
|
+
|
|
16
|
+
// Each frame: env's weather (wet, rain, damp, gust, dust, mist) moved on by dt seconds; hour is the clock, 0 to 1.
|
|
17
|
+
function updateWeather(env, dt, hour, { region, weather: w, x, z, running = true, quiet = false, ambientDust = 0 }) {
|
|
18
|
+
if (!w) {
|
|
19
|
+
env.wet = env.rain = env.damp = env.gust = env.dustStorm = env.mist = 0;
|
|
20
|
+
env.dust = ambientDust;
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (clock.land !== region) {
|
|
24
|
+
// a land just come to: its first spell is a while off
|
|
25
|
+
clock.land = region;
|
|
26
|
+
clock.on = false;
|
|
27
|
+
clock.next = w.spell ? between(w.every) * (0.5 + 0.5 * random()) : 0;
|
|
28
|
+
}
|
|
29
|
+
if (w.spell && running) {
|
|
30
|
+
clock.next -= dt;
|
|
31
|
+
if (clock.next <= 0) {
|
|
32
|
+
clock.on = !clock.on;
|
|
33
|
+
clock.next = between(clock.on ? w.lasts : w.every);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
let spell = !w.spell || quiet ? 0 : env.storm >= 0 ? env.storm : clock.on ? 1 : 0;
|
|
37
|
+
if (spell && w.reach) spell *= sstep(w.reach, w.reach * SPELL_FADE, Math.hypot(x - w.x, z - w.z));
|
|
38
|
+
const dusty = w.spell === 'dust',
|
|
39
|
+
squall = w.spell === 'squall',
|
|
40
|
+
wetGoal = dusty ? 0 : spell,
|
|
41
|
+
raining = wetGoal > 0.5 && env.wet > 0.75;
|
|
42
|
+
// clouds gather in under a minute (a squall's sooner) and break up more slowly; the rain waits for the cloud, and stops before it breaks
|
|
43
|
+
env.wet = toward(env.wet, wetGoal, dt / (wetGoal > env.wet ? (squall ? 22 : 40) : 70));
|
|
44
|
+
env.rain = toward(env.rain, raining ? 1 : 0, dt / (raining ? 14 : 8));
|
|
45
|
+
env.damp = toward(env.damp, env.rain > 0.3 ? 1 : 0, dt / (env.rain > 0.3 ? 45 : 240));
|
|
46
|
+
env.gust = squall ? env.wet : 0;
|
|
47
|
+
// dust: the storm's, what the viewer stands in already (local airborne dust), or a scene's own
|
|
48
|
+
env.dustStorm = toward(
|
|
49
|
+
env.dustStorm,
|
|
50
|
+
dusty ? spell : 0,
|
|
51
|
+
dt / (dusty && spell > env.dustStorm ? DUST_RISES : DUST_SETTLES),
|
|
52
|
+
);
|
|
53
|
+
env.dust = Math.max(env.dustStorm, ambientDust);
|
|
54
|
+
const clear = (1 - env.wet) * (1 - env.dust),
|
|
55
|
+
apart = (hour - DAWN) / MIST_HOURS;
|
|
56
|
+
env.mist = toward(env.mist, w.mist && !quiet ? Math.exp(-apart * apart) * clear : 0, dt / 6);
|
|
57
|
+
}
|
|
58
|
+
return updateWeather;
|
|
59
|
+
}
|
package/motion/wind.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Vector2, Vector3 } from 'three';
|
|
2
|
+
import { clamp } from '../math/scalar.js';
|
|
3
|
+
|
|
4
|
+
export function createWind({ speed: BREEZE = 1, swing: SWING = 0.16, direction: QUARTER = 0, squallGain: SQUALL = 1 } = {}) {
|
|
5
|
+
const wind = { x: 1, z: 0, speed: BREEZE, gust: 0.5 }; // (x, z): the way it blows, a unit vector; speed in metres a second; gust 0 to 1
|
|
6
|
+
// For shaders: xy the wind's velocity in metres a second (world x, z), z its gust from 0 to 1
|
|
7
|
+
// uWindDrift: how far the air has carried anything since the page opened (world x, z, in metres), for patterns that run with it
|
|
8
|
+
const windU = { uWind: { value: new Vector3(BREEZE, 0, 0.5) }, uWindDrift: { value: new Vector2() } };
|
|
9
|
+
let lastT = 0;
|
|
10
|
+
|
|
11
|
+
// Each frame, from the time elapsed in seconds and how hard a squall blows (0 to 1). The gust is handed in, not read
|
|
12
|
+
// from the environment, so that the materials the pool's workers load with a job's module do not bring the renderer with them.
|
|
13
|
+
function updateWind(t, squall) {
|
|
14
|
+
const a = QUARTER + SWING * Math.sin(t * 0.071),
|
|
15
|
+
g = clamp(0.5 + 0.22 * Math.sin(t * 0.21) + 0.18 * Math.sin(t * 0.077 + 1.3) + 0.1 * Math.sin(t * 0.53 + 2.1), 0, 1);
|
|
16
|
+
wind.x = Math.cos(a);
|
|
17
|
+
wind.z = Math.sin(a);
|
|
18
|
+
wind.gust = Math.max(g, squall);
|
|
19
|
+
wind.speed = BREEZE * (0.75 + 0.5 * g) * (1 + SQUALL * squall);
|
|
20
|
+
windU.uWind.value.set(wind.x * wind.speed, wind.z * wind.speed, wind.gust);
|
|
21
|
+
const dt = Math.min(0.25, Math.max(0, t - lastT)); // a long pause carries nothing far
|
|
22
|
+
lastT = t;
|
|
23
|
+
windU.uWindDrift.value.x += wind.x * wind.speed * dt;
|
|
24
|
+
windU.uWindDrift.value.y += wind.z * wind.speed * dt;
|
|
25
|
+
}
|
|
26
|
+
return { wind, uniforms: windU, update: updateWind };
|
|
27
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "roamwright",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "A
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "A Three.js library for building procedural worlds you can explore on foot or in flight. Generate terrain from a seed and batch scenery into meshes in the browser.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Enes TAYLAN",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"rendering",
|
|
26
26
|
"runtime",
|
|
27
27
|
"examples",
|
|
28
|
+
"scripts/serve.py",
|
|
28
29
|
"licenses",
|
|
29
30
|
"LICENSE",
|
|
30
31
|
"README.md",
|
|
@@ -32,8 +33,9 @@
|
|
|
32
33
|
],
|
|
33
34
|
"scripts": {
|
|
34
35
|
"test": "node --test test/*.test.js",
|
|
35
|
-
"demo": "python3
|
|
36
|
-
"test:browser": "node test/browser.mjs"
|
|
36
|
+
"demo": "python3 scripts/serve.py",
|
|
37
|
+
"test:browser": "node test/browser.mjs",
|
|
38
|
+
"test:package": "node test/package.mjs"
|
|
37
39
|
},
|
|
38
40
|
"peerDependencies": {
|
|
39
41
|
"three": "0.170.0 || 0.187.0"
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Coarse movement signatures for point-light shadow refresh. A skinned caster in range
|
|
2
|
+
// returns -1. Instances contribute their nearby transforms and reuse cached sums.
|
|
3
|
+
export function createCasterMotion({ scene, elapsed, listInterval = 2, skinnedRadius = 2 }) {
|
|
4
|
+
let listedAt = -Infinity;
|
|
5
|
+
const skinned = [],
|
|
6
|
+
instanced = [];
|
|
7
|
+
|
|
8
|
+
function list(now) {
|
|
9
|
+
listedAt = now;
|
|
10
|
+
skinned.length = instanced.length = 0;
|
|
11
|
+
scene.traverseVisible(o => {
|
|
12
|
+
if (!o.castShadow) return;
|
|
13
|
+
if (o.isSkinnedMesh) skinned.push(o);
|
|
14
|
+
else if (o.isInstancedMesh) instanced.push(o);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// A sum of where every instance within reach of (x, y, z) stands and how it is turned, or -1 when a figure stands there. A mesh
|
|
19
|
+
// whose instances and own place are as they were keeps the share it had (most never change: a market's stalls, a grove).
|
|
20
|
+
let atX = NaN,
|
|
21
|
+
atY = NaN,
|
|
22
|
+
atZ = NaN,
|
|
23
|
+
lastReach = NaN;
|
|
24
|
+
const cached = new WeakMap();
|
|
25
|
+
function stirsOf(x, y, z, reach) {
|
|
26
|
+
const now = elapsed();
|
|
27
|
+
if (now - listedAt > listInterval) list(now);
|
|
28
|
+
for (let i = 0; i < skinned.length; i++) {
|
|
29
|
+
const o = skinned[i],
|
|
30
|
+
e = o.matrixWorld.elements,
|
|
31
|
+
r = reach + skinnedRadius;
|
|
32
|
+
if (o.visible && (e[12] - x) ** 2 + (e[13] - y) ** 2 + (e[14] - z) ** 2 < r * r) return -1;
|
|
33
|
+
}
|
|
34
|
+
const moved = x !== atX || y !== atY || z !== atZ || reach !== lastReach;
|
|
35
|
+
atX = x;
|
|
36
|
+
atY = y;
|
|
37
|
+
atZ = z;
|
|
38
|
+
lastReach = reach;
|
|
39
|
+
let sum = 0;
|
|
40
|
+
for (let i = 0; i < instanced.length; i++) {
|
|
41
|
+
const o = instanced[i];
|
|
42
|
+
if (!o.visible || !o.count) continue;
|
|
43
|
+
let sample = cached.get(o);
|
|
44
|
+
if (!sample) {
|
|
45
|
+
sample = { matrix: o.matrixWorld.clone(), version: -1, count: 0, sum: 0 };
|
|
46
|
+
cached.set(o, sample);
|
|
47
|
+
}
|
|
48
|
+
const version = o.instanceMatrix.version;
|
|
49
|
+
if (moved || sample.version !== version || sample.count !== o.count || !sample.matrix.equals(o.matrixWorld)) {
|
|
50
|
+
sample.version = version;
|
|
51
|
+
sample.count = o.count;
|
|
52
|
+
sample.matrix.copy(o.matrixWorld);
|
|
53
|
+
sample.sum = shareOf(o, o.matrixWorld.elements, x, y, z, reach);
|
|
54
|
+
}
|
|
55
|
+
sum += sample.sum * (i + 1);
|
|
56
|
+
}
|
|
57
|
+
return sum;
|
|
58
|
+
}
|
|
59
|
+
// One mesh's share: its instances within reach, each by its place in the world (by the mesh's own matrix, mostly none) and turn.
|
|
60
|
+
function shareOf(o, w, x, y, z, reach) {
|
|
61
|
+
if (!o.geometry.boundingSphere) o.geometry.computeBoundingSphere();
|
|
62
|
+
const a = o.instanceMatrix.array,
|
|
63
|
+
r = reach + o.geometry.boundingSphere.radius;
|
|
64
|
+
let sum = 0;
|
|
65
|
+
for (let k = 0; k < o.count; k++) {
|
|
66
|
+
const j = k * 16,
|
|
67
|
+
px = a[j + 12],
|
|
68
|
+
py = a[j + 13],
|
|
69
|
+
pz = a[j + 14],
|
|
70
|
+
wx = w[0] * px + w[4] * py + w[8] * pz + w[12],
|
|
71
|
+
wy = w[1] * px + w[5] * py + w[9] * pz + w[13],
|
|
72
|
+
wz = w[2] * px + w[6] * py + w[10] * pz + w[14];
|
|
73
|
+
if ((wx - x) ** 2 + (wy - y) ** 2 + (wz - z) ** 2 < r * r) sum += (k + 1) * (wx + 3 * wy + 7 * wz + a[j] + 5 * a[j + 2]);
|
|
74
|
+
}
|
|
75
|
+
return sum;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return stirsOf;
|
|
79
|
+
}
|