@margoengine/horses 0.0.0-stage → 1.0.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +347 -2
- package/dist/anatomy.d.ts +75 -0
- package/dist/anatomy.js +89 -0
- package/dist/clip-plan.d.ts +10 -0
- package/dist/crowd.d.ts +76 -0
- package/dist/crowd.js +172 -0
- package/dist/drive.d.ts +39 -0
- package/dist/gaits.d.ts +93 -0
- package/dist/gaits.js +165 -0
- package/dist/grounding.d.ts +14 -0
- package/dist/index.d.ts +176 -0
- package/dist/index.js +1 -0
- package/dist/model-D2H9-6oj.js +1503 -0
- package/dist/model.d.ts +159 -0
- package/dist/model.js +1 -0
- package/dist/scene-scope-BIsfeY-W.js +42 -0
- package/dist/seat.d.ts +93 -0
- package/dist/seat.js +164 -0
- package/dist/settings.d.ts +29 -0
- package/dist/settings.js +126 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/src-CAiGrBte.js +531 -0
- package/dist/strands.d.ts +30 -0
- package/dist/strands.js +135 -0
- package/package.json +84 -4
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/** A colour value: linear RGB. */
|
|
2
|
+
export type SettingColor = readonly [number, number, number];
|
|
3
|
+
/** One gradient stop: a colour at a position (`elevation` unless the row's `stopKey` says otherwise). */
|
|
4
|
+
export type SettingStop = { readonly color: SettingColor };
|
|
5
|
+
/** One value of a setting: a number, a colour, a choice, gradient stops, or null where `nullable`. */
|
|
6
|
+
export type SettingValue = number | SettingColor | string | readonly SettingStop[] | null;
|
|
7
|
+
/** Values by setting name; a name left out takes its default. */
|
|
8
|
+
export type SettingValues = Readonly<Record<string, SettingValue>>;
|
|
9
|
+
/** One slider row, the shape every package's `/settings` entry shares. */
|
|
10
|
+
export interface SettingRow {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly group: string;
|
|
13
|
+
/** `number` is a slider, `color` three sliders or a picker, `choice` a list, `gradient` a stop editor. */
|
|
14
|
+
readonly kind: 'number' | 'color' | 'choice' | 'gradient';
|
|
15
|
+
/** The value with which the target draws exactly as it does without the option (null: follow the weather). */
|
|
16
|
+
readonly default: SettingValue;
|
|
17
|
+
readonly min?: number;
|
|
18
|
+
readonly max?: number;
|
|
19
|
+
readonly step?: number;
|
|
20
|
+
readonly choices?: readonly string[];
|
|
21
|
+
/** May be null: the target uses its own value, or follows the weather when `weather` is set. */
|
|
22
|
+
readonly nullable?: boolean;
|
|
23
|
+
/** The weather master slider that drives this row while it is null. */
|
|
24
|
+
readonly weather?: string;
|
|
25
|
+
/** The key a gradient stop is placed by (default `elevation`). */
|
|
26
|
+
readonly stopKey?: string;
|
|
27
|
+
readonly unit: string;
|
|
28
|
+
readonly about: string;
|
|
29
|
+
/** True: cheap to write every frame (no pipeline, buffer or texture is created), so it can be keyed over a shot. False: writing it rebuilds something. */
|
|
30
|
+
readonly live: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare const numberSetting: (
|
|
33
|
+
name: string,
|
|
34
|
+
group: string,
|
|
35
|
+
value: number,
|
|
36
|
+
min: number,
|
|
37
|
+
max: number,
|
|
38
|
+
step: number,
|
|
39
|
+
unit: string,
|
|
40
|
+
about: string,
|
|
41
|
+
live?: boolean,
|
|
42
|
+
) => SettingRow;
|
|
43
|
+
export declare const colorSetting: (
|
|
44
|
+
name: string,
|
|
45
|
+
group: string,
|
|
46
|
+
value: SettingColor,
|
|
47
|
+
min: number,
|
|
48
|
+
max: number,
|
|
49
|
+
about: string,
|
|
50
|
+
live?: boolean,
|
|
51
|
+
) => SettingRow;
|
|
52
|
+
export declare const choiceSetting: (
|
|
53
|
+
name: string,
|
|
54
|
+
group: string,
|
|
55
|
+
choices: readonly string[],
|
|
56
|
+
about: string,
|
|
57
|
+
live?: boolean,
|
|
58
|
+
) => SettingRow;
|
|
59
|
+
/** The row with a null default that follows the weather master slider `weather` until a value is given. */
|
|
60
|
+
export declare const followsWeather: (row: SettingRow, weather: string) => SettingRow;
|
|
61
|
+
/** Marks `key` of `target` as set by hand (`held` true) or hands it back to the weather (false). */
|
|
62
|
+
export declare function holdFromWeather(target: object, key: string, held?: boolean): void;
|
|
63
|
+
/** True while `key` of `target` holds a value set by hand, which the weather drive leaves alone. */
|
|
64
|
+
export declare const heldFromWeather: (target: object, key: string) => boolean;
|
|
65
|
+
export declare const sameSettingValue: (a: unknown, b: unknown) => boolean;
|
|
66
|
+
export declare function copySettingValue(v: SettingValue): SettingValue;
|
|
67
|
+
/** A fresh object holding every row at its default (a slider panel's state). */
|
|
68
|
+
export declare function settingDefaults(rows: readonly SettingRow[]): Record<string, SettingValue>;
|
|
69
|
+
/** The rows that may change every frame without a rebuild. */
|
|
70
|
+
export declare const liveSettings: (rows: readonly SettingRow[]) => SettingRow[];
|
|
71
|
+
/** A name to row map. */
|
|
72
|
+
export declare const settingIndex: (rows: readonly SettingRow[]) => Map<string, SettingRow>;
|
|
73
|
+
/** @throws RangeError when `v` is not a value of `row`. */
|
|
74
|
+
export declare function checkSettingValue(row: SettingRow, v: unknown, label: string): void;
|
|
75
|
+
/** @throws TypeError for an unknown name; RangeError for a bad value or a null on a row that is not nullable. */
|
|
76
|
+
export declare function checkSettings(
|
|
77
|
+
rows: readonly SettingRow[] | ReadonlyMap<string, SettingRow>,
|
|
78
|
+
values: SettingValues,
|
|
79
|
+
label: string,
|
|
80
|
+
entry?: string,
|
|
81
|
+
): void;
|
|
82
|
+
/** Checks `values`, then calls `write` once per row (or per row `only` accepts) with the given value or the default. */
|
|
83
|
+
export declare function applySettings(
|
|
84
|
+
rows: readonly SettingRow[],
|
|
85
|
+
values: SettingValues,
|
|
86
|
+
label: string,
|
|
87
|
+
write: (row: SettingRow, value: SettingValue) => void,
|
|
88
|
+
only?: (row: SettingRow) => boolean,
|
|
89
|
+
): void;
|
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
import { t as e } from "./scene-scope-BIsfeY-W.js";
|
|
2
|
+
import { GAIT_PATTERNS as t, GRAVITY as n, froude as r, hoofPhase as i, stride as a, strideLength as o } from "./gaits.js";
|
|
3
|
+
import { _ as s, d as c, f as l, g as u, h as d, l as f, o as p, p as m, s as h, u as g } from "./model-D2H9-6oj.js";
|
|
4
|
+
import { cos as _, hypot as v, sin as y } from "@margoengine/math/pinned";
|
|
5
|
+
//#region src/grounding.ts
|
|
6
|
+
var b = (e) => {
|
|
7
|
+
let t = Math.min(1, Math.max(0, e));
|
|
8
|
+
return t * t * (3 - 2 * t);
|
|
9
|
+
}, x = (e, t) => Math.min(.45, Math.max(-.45, Math.atan2(e, t))), S = (e, t) => 1 - b((e - t.time) / t.duration);
|
|
10
|
+
function C(e, t) {
|
|
11
|
+
let n = e.legs.map(() => ({})), r = (e) => [Math.cos(e.heading), Math.sin(e.heading)], i = (e, t, n) => {
|
|
12
|
+
let [i, a] = r(e);
|
|
13
|
+
return [e.x + i * t + a * n, e.z - a * t + i * n];
|
|
14
|
+
}, a = e.body.withers * .039, o = e.body.bodyLength * .4, s = Math.max(e.body.chestWidth, e.body.hipWidth) * .55, c = (e, n, r) => t(...i(e, n, r));
|
|
15
|
+
return {
|
|
16
|
+
advance: (t, r, a, o, s) => {
|
|
17
|
+
if (!n.some((e, t) => !!e.anchor !== a[t])) return;
|
|
18
|
+
let c = m(e, o());
|
|
19
|
+
n.forEach((e, n) => {
|
|
20
|
+
let o = a[n];
|
|
21
|
+
if (!!e.anchor === o) return;
|
|
22
|
+
let l = c[n], u = i(t, l[0], l[2]);
|
|
23
|
+
if (o) {
|
|
24
|
+
if (e.release) {
|
|
25
|
+
let t = S(r, e.release);
|
|
26
|
+
u[0] += e.release.offset[0] * t, u[1] += e.release.offset[1] * t;
|
|
27
|
+
}
|
|
28
|
+
e.anchor = u, e.release = void 0;
|
|
29
|
+
} else e.anchor &&= (e.release = {
|
|
30
|
+
time: r,
|
|
31
|
+
duration: s,
|
|
32
|
+
offset: [e.anchor[0] - u[0], e.anchor[1] - u[1]]
|
|
33
|
+
}, void 0);
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
fit: (l, u, d, f) => {
|
|
37
|
+
let [h, g] = r(l), _ = c(l, 0, o), v = c(l, 0, -o), y = c(l, s, 0), b = c(l, -s, 0), C = x(v - _, 2 * o), w = x(y - b, 2 * s) + (l.bank ?? 0), T = (_ + v + y + b) / 4 - u, E = m(e, f).map(([e, r, o], s) => {
|
|
38
|
+
let f = i(l, e, o), { anchor: p, release: m } = n[s];
|
|
39
|
+
if (p) f[0] = p[0], f[1] = p[1];
|
|
40
|
+
else if (m) {
|
|
41
|
+
let e = S(d, m);
|
|
42
|
+
f[0] += m.offset[0] * e, f[1] += m.offset[1] * e;
|
|
43
|
+
}
|
|
44
|
+
let _ = f[0] - l.x, v = f[1] - l.z, y = {
|
|
45
|
+
x: f[0],
|
|
46
|
+
z: f[1],
|
|
47
|
+
heading: l.heading
|
|
48
|
+
}, b = t(...f);
|
|
49
|
+
return {
|
|
50
|
+
point: [
|
|
51
|
+
h * _ - g * v,
|
|
52
|
+
b - u + Math.max(0, r),
|
|
53
|
+
g * _ + h * v
|
|
54
|
+
],
|
|
55
|
+
pitch: x(c(y, 0, -a) - c(y, 0, a), 2 * a),
|
|
56
|
+
roll: x(c(y, a, 0) - c(y, -a, 0), 2 * a)
|
|
57
|
+
};
|
|
58
|
+
}), [D, O, k] = f.bodyOffset;
|
|
59
|
+
return p(e, {
|
|
60
|
+
...f,
|
|
61
|
+
bodyOffset: [
|
|
62
|
+
D,
|
|
63
|
+
O + T,
|
|
64
|
+
k
|
|
65
|
+
]
|
|
66
|
+
}, E, C, w);
|
|
67
|
+
},
|
|
68
|
+
reset() {
|
|
69
|
+
for (let e of n) e.anchor = e.release = void 0;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
//#endregion
|
|
74
|
+
//#region src/drive.ts
|
|
75
|
+
var w = {
|
|
76
|
+
height: 0,
|
|
77
|
+
forward: 0,
|
|
78
|
+
right: 0
|
|
79
|
+
};
|
|
80
|
+
function T(e, t, n, r, i) {
|
|
81
|
+
if (!t) return w;
|
|
82
|
+
let a = y(i), o = _(i), s = .4 * e.bodyLength, c = -s, l = .5 * e.chestWidth, u = .5 * e.hipWidth, d = (e, i) => t(n + e * o + i * a, r - e * a + i * o), f = d(-l, s), p = d(l, s), m = d(-u, c), h = d(u, c);
|
|
83
|
+
if (![
|
|
84
|
+
f,
|
|
85
|
+
p,
|
|
86
|
+
m,
|
|
87
|
+
h
|
|
88
|
+
].every(Number.isFinite)) throw RangeError("Provide a finite ground elevation in metres at every hoof sample.");
|
|
89
|
+
return {
|
|
90
|
+
height: (f + p + m + h) / 4,
|
|
91
|
+
forward: (f + p - m - h) / (2 * (s - c)),
|
|
92
|
+
right: (p - f + h - m) / (2 * (l + u))
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function E(e, t) {
|
|
96
|
+
return Math.max(-t, Math.min(t, e));
|
|
97
|
+
}
|
|
98
|
+
var D = 2 * Math.PI;
|
|
99
|
+
function O(e, t, r, i, a) {
|
|
100
|
+
let o = n / v(1, r.forward, r.right), s = o * r.forward, c = o * r.right, l = i.strength / i.mass, u = Math.max(0, s), d = u === 0 ? t.speed : t.speed * (l / (l + u)), f = i.grip * o, p = E(E((d - e.speed) / a + s, l), f), m = Math.sqrt(Math.max(0, f * f - p * p)), h = Math.max(0, e.speed + (p - s) * a), g = t.heading - e.heading, _ = E(g - D * Math.floor((g + Math.PI) / D), i.turnRate * a), y = (e.speed + h) / 2, b = y * _ / a, x = E(b + c - e.sidewaysSpeed / a, m) - c, S = y > 0 ? E(x * a / y, Math.abs(_)) : i.grip > 0 ? _ : 0, C = S * _ > 0 ? S : 0, w = y * C / a;
|
|
101
|
+
return {
|
|
102
|
+
speed: h,
|
|
103
|
+
heading: e.heading + C,
|
|
104
|
+
sidewaysSpeed: e.sidewaysSpeed + (x - w) * a,
|
|
105
|
+
lateralAcceleration: w,
|
|
106
|
+
normalGravity: o,
|
|
107
|
+
slipping: Math.abs(b + c - e.sidewaysSpeed / a) > m
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/index.ts
|
|
112
|
+
var k = 1 / 240, ee = (e, t) => {
|
|
113
|
+
if (e !== void 0 && e !== !1) {
|
|
114
|
+
if (!e || typeof e.add != "function") throw TypeError(`addHorse ${t} must have an add() method (Shadows, a GTAO pass) or be false`);
|
|
115
|
+
return e;
|
|
116
|
+
}
|
|
117
|
+
}, te = (e, t) => {
|
|
118
|
+
if (typeof e != "number" || !Number.isFinite(e)) throw RangeError(`Horse ${t} must be a finite number`);
|
|
119
|
+
return e;
|
|
120
|
+
}, ne = (e, t) => [
|
|
121
|
+
e[3] * t[0] + e[0] * t[3] + e[1] * t[2] - e[2] * t[1],
|
|
122
|
+
e[3] * t[1] - e[0] * t[2] + e[1] * t[3] + e[2] * t[0],
|
|
123
|
+
e[3] * t[2] + e[0] * t[1] - e[1] * t[0] + e[2] * t[3],
|
|
124
|
+
e[3] * t[3] - e[0] * t[0] - e[1] * t[1] - e[2] * t[2]
|
|
125
|
+
];
|
|
126
|
+
function re(e, t, n) {
|
|
127
|
+
let r = e[0] * t[0] + e[1] * t[1] + e[2] * t[2] + e[3] * t[3] < 0 ? -1 : 1, i = [
|
|
128
|
+
0,
|
|
129
|
+
1,
|
|
130
|
+
2,
|
|
131
|
+
3
|
|
132
|
+
].map((i) => e[i] * (1 - n) + r * t[i] * n), a = Math.hypot(...i) || 1;
|
|
133
|
+
return i.map((e) => e / a);
|
|
134
|
+
}
|
|
135
|
+
function ie(e, t, n) {
|
|
136
|
+
let r = (e, t) => [
|
|
137
|
+
0,
|
|
138
|
+
1,
|
|
139
|
+
2
|
|
140
|
+
].map((r) => (e?.[r] ?? 0) * (1 - n) + (t?.[r] ?? 0) * n);
|
|
141
|
+
return {
|
|
142
|
+
rotations: e.rotations.map((e, r) => re(e, t.rotations[r], n)),
|
|
143
|
+
bodyOffset: r(e.bodyOffset, t.bodyOffset),
|
|
144
|
+
...e.scapulaOffsets || t.scapulaOffsets ? { scapulaOffsets: [r(e.scapulaOffsets?.[0], t.scapulaOffsets?.[0]), r(e.scapulaOffsets?.[1], t.scapulaOffsets?.[1])] } : {}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
var ae = class {
|
|
148
|
+
constructor(e, t = {}) {
|
|
149
|
+
this.body = e, this.options = t, this.speed = 0, this.distance = 0, this.gait = "stand", this.previous = "stand", this.sidewaysSpeed = 0, this.slipping = !1, this.bank = 0, this.cycleTravel = 0, this.support = {}, this.fade = 1, this.start = /* @__PURE__ */ new Map(), this.x = t.position?.[0] ?? 0, this.z = t.position?.[1] ?? 0, this.heading = t.heading ?? 0;
|
|
150
|
+
for (let [e, n] of [["acceleration", t.acceleration ?? 3], ["turn rate", t.turnRate ?? 1.2]]) if (!(n >= 0) || !Number.isFinite(n)) throw RangeError("Pass a finite nonnegative horse " + e + ".");
|
|
151
|
+
}
|
|
152
|
+
setSupport(e) {
|
|
153
|
+
if (e.ground && typeof e.ground != "function") throw RangeError("Pass a ground function that returns a finite height in metres.");
|
|
154
|
+
for (let [t, n] of [["grip", e.grip ?? .6], ["carried mass", e.loadMass ?? 0]]) if (!(n >= 0) || !Number.isFinite(n)) throw RangeError("Pass a finite nonnegative horse " + t + ".");
|
|
155
|
+
return this.support = { ...e }, this;
|
|
156
|
+
}
|
|
157
|
+
supportHeight(e = this.x, t = this.z, n = this.heading) {
|
|
158
|
+
return T(this.body, this.support.ground, e, t, n).height;
|
|
159
|
+
}
|
|
160
|
+
driven(e, t) {
|
|
161
|
+
if (!(e > 0) || !Number.isFinite(e)) throw RangeError("Pass a finite positive horse step in seconds.");
|
|
162
|
+
let n = T(this.body, this.support.ground, this.x, this.z, this.heading);
|
|
163
|
+
return O(this, t, n, {
|
|
164
|
+
strength: this.body.mass * (this.options.acceleration ?? 3),
|
|
165
|
+
mass: this.body.mass + (this.support.loadMass ?? 0),
|
|
166
|
+
grip: this.support.grip ?? .6,
|
|
167
|
+
turnRate: this.options.turnRate ?? 1.2
|
|
168
|
+
}, e);
|
|
169
|
+
}
|
|
170
|
+
pick(e, n) {
|
|
171
|
+
if (e < .05) return "stand";
|
|
172
|
+
if (n !== "auto") return n;
|
|
173
|
+
let i = r(this.body, e), a = this.gait, o = [
|
|
174
|
+
"walk",
|
|
175
|
+
"trot",
|
|
176
|
+
"canter",
|
|
177
|
+
"gallop"
|
|
178
|
+
], s = "gallop";
|
|
179
|
+
for (let e of o) if (i < t[e].froude[1] * (a === "stand" || o.indexOf(a) <= o.indexOf(e) ? 1.05 : .95)) {
|
|
180
|
+
s = e;
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
return s;
|
|
184
|
+
}
|
|
185
|
+
speedAfter(e, t) {
|
|
186
|
+
return e === 0 ? this.speed : this.driven(e, {
|
|
187
|
+
speed: t,
|
|
188
|
+
heading: this.heading
|
|
189
|
+
}).speed;
|
|
190
|
+
}
|
|
191
|
+
step(e, t) {
|
|
192
|
+
if (e === 0) return;
|
|
193
|
+
let n = this.options.fade ?? .3, r = this.driven(e, t);
|
|
194
|
+
this.speed = r.speed, this.sidewaysSpeed = r.sidewaysSpeed, this.heading = r.heading, this.slipping = r.slipping, this.bank = -Math.atan2(r.lateralAcceleration, r.normalGravity);
|
|
195
|
+
let i = y(this.heading), a = _(this.heading);
|
|
196
|
+
this.x += (i * this.speed + a * this.sidewaysSpeed) * e, this.z += (a * this.speed - i * this.sidewaysSpeed) * e, this.distance += v(this.speed, this.sidewaysSpeed) * e;
|
|
197
|
+
let s = o(this.body, this.speed);
|
|
198
|
+
s > 0 && (this.cycleTravel += e * this.speed / s);
|
|
199
|
+
let c = this.pick(this.speed, t.gait);
|
|
200
|
+
c !== this.gait && (this.previous = this.gait, this.gait = c, this.fade = n > 0 ? 0 : 1, c !== "stand" && !this.start.has(c) && this.start.set(c, this.distance)), this.fade = Math.min(1, this.fade + (n > 0 ? e / n : 1));
|
|
201
|
+
}
|
|
202
|
+
strideCyclesAt(e = this.distance) {
|
|
203
|
+
let t = o(this.body, this.speed);
|
|
204
|
+
return this.cycleTravel + (t > 0 ? (e - this.distance) / t : 0);
|
|
205
|
+
}
|
|
206
|
+
phase(e, t) {
|
|
207
|
+
return this.phaseAt(e, t, this.distance);
|
|
208
|
+
}
|
|
209
|
+
phaseAt(e, t, n) {
|
|
210
|
+
let r = (n - (this.start.get(e) ?? 0)) / t;
|
|
211
|
+
return r - Math.floor(r);
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
async function oe(t, n = {}) {
|
|
215
|
+
let r = n.skin, o = r ? void 0 : h(n), p = r?.skeleton ?? o.skeleton, _ = p.body, v = o?.clips ?? u(_, n.speeds, s(p, c(_))).clips, y = n.acceleration ?? 3, b = n.turnRate ?? 1.2, x = n.fade ?? .3;
|
|
216
|
+
if (!(y > 0 && y < Infinity)) throw RangeError("addHorse acceleration must be above 0 m/s^2");
|
|
217
|
+
if (!(b > 0 && b < Infinity)) throw RangeError("addHorse turnRate must be above 0 radians a second");
|
|
218
|
+
if (!(x >= 0 && x < Infinity)) throw RangeError("addHorse fade must be 0 or more seconds");
|
|
219
|
+
let S = n.ground ?? (() => 0);
|
|
220
|
+
if (typeof S != "function") throw TypeError("addHorse ground must be a function (x, z) => height");
|
|
221
|
+
let w = C(p, S), T = ee(n.shadows, "shadows"), E = ee(n.occlusion, "occlusion"), D = e(t);
|
|
222
|
+
if (r && (r.model.root.engine !== t || typeof r.applyPose != "function")) throw TypeError("Pass an authored horse skin from this engine and a function that applies its pose");
|
|
223
|
+
let [{ parseGLTFAsync: O }, { Animation: re }, { dualQuaternionSkinning: oe }, { install: se }, { hair: ce }] = await Promise.all([
|
|
224
|
+
import("@margoengine/gltf/pbr"),
|
|
225
|
+
import("@margoengine/animation"),
|
|
226
|
+
import("@margoengine/animation/dqs"),
|
|
227
|
+
import("@margoengine/materials"),
|
|
228
|
+
import("@margoengine/materials/hair")
|
|
229
|
+
]), A = r?.model ?? await D.run(() => O(t, o.doc, o.buffers)), le = n.shadows === void 0 ? t.sceneOf(A)?.plugins.get("shadows") : T, j = {};
|
|
230
|
+
!r && n.coatHair !== !1 && (j.Coat = [ce({
|
|
231
|
+
direction: "u",
|
|
232
|
+
strands: 400,
|
|
233
|
+
specular: .45,
|
|
234
|
+
...n.coatHair || {}
|
|
235
|
+
})]), !r && n.maneHair !== !1 && (j.Mane = [ce({
|
|
236
|
+
strands: 1024,
|
|
237
|
+
...n.maneHair || {}
|
|
238
|
+
})], j.Tail = [ce({
|
|
239
|
+
strands: 1024,
|
|
240
|
+
...n.maneHair || {}
|
|
241
|
+
})]);
|
|
242
|
+
let M = [], N = Object.keys(j).length ? se(A, { features: j }) : void 0;
|
|
243
|
+
N && M.push(() => N.dispose());
|
|
244
|
+
let P = new re(A, { skinning: oe }), ue = t, de = ue.sceneOf(A) ?? A.root.scene;
|
|
245
|
+
ue.use(P, de), P.playing = !1, M.push(() => P.dispose());
|
|
246
|
+
for (let e of [le?.add(A), E?.add(A)]) typeof e == "function" && M.push(e);
|
|
247
|
+
let F = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map();
|
|
248
|
+
for (let e of v) e.name !== "stand" && F.set(e.name, {
|
|
249
|
+
...a(_, e.speed, e.name),
|
|
250
|
+
length: e.strideLength,
|
|
251
|
+
duration: e.duration
|
|
252
|
+
});
|
|
253
|
+
let fe = {
|
|
254
|
+
position: n.position,
|
|
255
|
+
heading: n.heading,
|
|
256
|
+
acceleration: y,
|
|
257
|
+
turnRate: b,
|
|
258
|
+
fade: x
|
|
259
|
+
}, pe = () => new ae(_, fe).setSupport({
|
|
260
|
+
ground: n.ground,
|
|
261
|
+
grip: n.grip,
|
|
262
|
+
loadMass: n.loadMass
|
|
263
|
+
}), L = pe(), R = 0, z = 0, B = 0, V = {
|
|
264
|
+
x: L.x,
|
|
265
|
+
z: L.z,
|
|
266
|
+
heading: L.heading,
|
|
267
|
+
distance: L.distance
|
|
268
|
+
}, H = {
|
|
269
|
+
speed: 0,
|
|
270
|
+
heading: n.heading ?? 0,
|
|
271
|
+
gait: "auto"
|
|
272
|
+
}, U = g(p, null, 0, 0), W, G = 0, K = [], q = [], J = /* @__PURE__ */ new Set(), Y = /* @__PURE__ */ new Set(), X = /* @__PURE__ */ new Set(), me = () => {
|
|
273
|
+
let e = Math.min(1, Math.max(0, (R - z * k) / k)), t = Math.atan2(Math.sin(L.heading - V.heading), Math.cos(L.heading - V.heading));
|
|
274
|
+
return {
|
|
275
|
+
x: L.x + (L.x - V.x) * e,
|
|
276
|
+
z: L.z + (L.z - V.z) * e,
|
|
277
|
+
heading: L.heading + t * e,
|
|
278
|
+
distance: L.distance + (L.distance - V.distance) * e,
|
|
279
|
+
bank: L.bank
|
|
280
|
+
};
|
|
281
|
+
}, he = (e, t, n) => {
|
|
282
|
+
let r = Math.min(1, L.speed / (F.get(e.gait).speed * .85));
|
|
283
|
+
return g(p, e, t - Math.floor(t), n, r ** 2 * (3 - 2 * r));
|
|
284
|
+
}, ge = (e, t, n) => {
|
|
285
|
+
if (e === "stand") return g(p, null, 0, n);
|
|
286
|
+
let r = a(_, Math.max(.05, L.speed), e), i = t + (I.get(e) ?? 0);
|
|
287
|
+
return he(r, i, n);
|
|
288
|
+
}, _e = (e, t) => e === "stand" || t === 0 ? 1 : Math.min(1, F.get(e).speed / t), ve = (e) => G + (L.strideCyclesAt(e) - L.strideCyclesAt()) * (1 - L.fade) * _e(L.previous, L.speed), ye = (e) => {
|
|
289
|
+
let t = L.fade < .5 && !W, n = t ? L.previous : L.gait, r = n === "stand" ? void 0 : a(_, Math.max(.05, L.speed), n), o = n === "stand" || !r ? 0 : (t ? ve(e) : L.strideCyclesAt(e)) + (I.get(n) ?? 0);
|
|
290
|
+
return {
|
|
291
|
+
feet: [
|
|
292
|
+
0,
|
|
293
|
+
1,
|
|
294
|
+
2,
|
|
295
|
+
3
|
|
296
|
+
].map((e) => !!(r && i(r, e, o).stance)),
|
|
297
|
+
swing: r ? (1 - r.duty) * r.duration : Math.max(x, k)
|
|
298
|
+
};
|
|
299
|
+
}, Z = (e, t) => {
|
|
300
|
+
let n = ge(L.gait, L.strideCyclesAt(e), t);
|
|
301
|
+
if (L.fade >= 1) return n;
|
|
302
|
+
let r = L.fade, i = r ** 3 * (10 + r * (-15 + 6 * r));
|
|
303
|
+
return ie(W ?? ge(L.previous, ve(e), t), n, i);
|
|
304
|
+
}, be = (e, t) => {
|
|
305
|
+
U = Z(e.distance, R);
|
|
306
|
+
let r = p.strandChains?.[0];
|
|
307
|
+
if (r) {
|
|
308
|
+
let e = [...U.rotations];
|
|
309
|
+
for (let t of r) e[t] = [
|
|
310
|
+
0,
|
|
311
|
+
0,
|
|
312
|
+
0,
|
|
313
|
+
1
|
|
314
|
+
];
|
|
315
|
+
U = {
|
|
316
|
+
...U,
|
|
317
|
+
rotations: e
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
for (let e of q) U = l(p, U, R - e.at, e.value.strength, e.value.side);
|
|
321
|
+
(n.ground || e.bank !== 0) && (U = w.fit(e, t, R, U));
|
|
322
|
+
}, xe = (e, t, n, r, i) => {
|
|
323
|
+
let o = a(_, Math.max(.05, L.speed), e), s = m(p, t), c = m(p, n), l = 0, u = Infinity, d = (e) => {
|
|
324
|
+
let t = m(p, he(o, e, r)), n = m(p, he(o, e + L.speed * i / o.length, r + i)), a = t.reduce((e, t, r) => {
|
|
325
|
+
let [i, a, o] = s[r], l = Math.abs(a) < .025 ? 4 : 1, u = n[r].reduce((e, n, i) => e + (n - t[i] - (c[r][i] - s[r][i])) ** 2, 0);
|
|
326
|
+
return e + (t[0] - i) ** 2 + l * (t[1] - a) ** 2 + (t[2] - o) ** 2 + 2 * u;
|
|
327
|
+
}, 0);
|
|
328
|
+
a < u && (u = a, l = e);
|
|
329
|
+
};
|
|
330
|
+
for (let e = 0; e < 24; e++) d(e / 24);
|
|
331
|
+
let f = l;
|
|
332
|
+
for (let e of [-1 / 72, 1 / 72]) d((f + e + 1) % 1);
|
|
333
|
+
I.set(e, l - L.strideCyclesAt());
|
|
334
|
+
}, Se = [
|
|
335
|
+
"body",
|
|
336
|
+
"scapulaL",
|
|
337
|
+
"scapulaR"
|
|
338
|
+
].map((e) => p.index(e)), Ce = () => {
|
|
339
|
+
let e = me(), t = L.supportHeight(e.x, e.z, e.heading);
|
|
340
|
+
be(e, t);
|
|
341
|
+
for (let e of Y) U = e(U, $);
|
|
342
|
+
let n = A.nodes;
|
|
343
|
+
if (r) r.applyPose(U);
|
|
344
|
+
else {
|
|
345
|
+
U.rotations.forEach((e, t) => t > 0 && n[t]?.setQuaternion(e));
|
|
346
|
+
for (let e of Se) n[e]?.setPosition(f(p, U, e));
|
|
347
|
+
for (let e of X) U.jointTranslations?.has(e) || n[e]?.setPosition(f(p, U, e));
|
|
348
|
+
X.clear();
|
|
349
|
+
for (let [e, t] of U.jointTranslations ?? []) n[e]?.setPosition(t), X.add(e);
|
|
350
|
+
}
|
|
351
|
+
A.root.setPosition([
|
|
352
|
+
e.x,
|
|
353
|
+
t,
|
|
354
|
+
e.z
|
|
355
|
+
]), A.root.setQuaternion([
|
|
356
|
+
0,
|
|
357
|
+
Math.sin(e.heading / 2),
|
|
358
|
+
0,
|
|
359
|
+
Math.cos(e.heading / 2)
|
|
360
|
+
]);
|
|
361
|
+
}, we = (e) => {
|
|
362
|
+
let t = Math.round(e * 1e6) / 1e6;
|
|
363
|
+
for (R = e = Math.abs(t - e) < 1e-9 ? t : e; (z + 1) * k <= e + 1e-9;) {
|
|
364
|
+
let e = z * k;
|
|
365
|
+
for (; B < K.length && K[B].at <= e + 1e-9;) {
|
|
366
|
+
let e = K[B++];
|
|
367
|
+
e.key === "tail" ? q.push(e) : H[e.key] = e.value;
|
|
368
|
+
}
|
|
369
|
+
for (; q.length && e - q[0].at >= 1;) q.shift();
|
|
370
|
+
V = {
|
|
371
|
+
x: L.x,
|
|
372
|
+
z: L.z,
|
|
373
|
+
heading: L.heading,
|
|
374
|
+
distance: L.distance
|
|
375
|
+
};
|
|
376
|
+
let t = L.gait, r = L.pick(L.speedAfter(k, H.speed), H.gait), i = L.fade, a = i < 1, o = Math.min(.12, x * .4), s = r !== t, c = s ? Z(L.distance, e) : void 0, l = s ? Z(L.distance + L.speed * _e(t, L.speed) * o, e + o) : void 0, u = L.strideCyclesAt();
|
|
377
|
+
L.step(k, H);
|
|
378
|
+
let d = (L.strideCyclesAt() - u) * _e(L.previous, L.speed);
|
|
379
|
+
if (c ? (G = u + d * (1 - L.fade / 2), W = a ? c : void 0, L.gait !== "stand" && xe(L.gait, c, l, e, o)) : a && (G += d * (1 - (i + L.fade) / 2)), z++, n.ground || L.bank !== 0) {
|
|
380
|
+
let e = ye(L.distance);
|
|
381
|
+
w.advance(L, z * k, e.feet, () => Z(L.distance, z * k), e.swing);
|
|
382
|
+
}
|
|
383
|
+
if (J.size) {
|
|
384
|
+
let e = R;
|
|
385
|
+
R = z * k, be(L, L.supportHeight());
|
|
386
|
+
for (let e of J) e.step(k, $);
|
|
387
|
+
R = e;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
Ce();
|
|
391
|
+
}, Q = (e) => {
|
|
392
|
+
for (let t = K.length - 1; t >= 0; t--) (K[t].at > R + 1e-9 || K[t].at >= R - 1e-9 && K[t].key === e.key && t >= B) && K.splice(t, 1);
|
|
393
|
+
B = Math.min(B, K.length), K.push(e);
|
|
394
|
+
}, Te = (e) => {
|
|
395
|
+
for (let t = K.length - 1; t >= 0; t--) if (K[t].key === e) return K[t].value;
|
|
396
|
+
return e === "speed" ? 0 : e === "heading" ? n.heading ?? 0 : "auto";
|
|
397
|
+
}, Ee = !1, De = () => {
|
|
398
|
+
if (!Ee) {
|
|
399
|
+
Ee = !0, J.clear(), Y.clear(), X.clear();
|
|
400
|
+
for (let e of M.reverse()) e();
|
|
401
|
+
r || A.dispose?.();
|
|
402
|
+
}
|
|
403
|
+
}, $ = {
|
|
404
|
+
model: A,
|
|
405
|
+
animation: P,
|
|
406
|
+
materials: N,
|
|
407
|
+
body: _,
|
|
408
|
+
skeleton: p,
|
|
409
|
+
clips: v,
|
|
410
|
+
get time() {
|
|
411
|
+
return R;
|
|
412
|
+
},
|
|
413
|
+
get speed() {
|
|
414
|
+
return Te("speed");
|
|
415
|
+
},
|
|
416
|
+
set speed(e) {
|
|
417
|
+
if (!(te(e, "speed") >= 0) || e > 25) throw RangeError("Horse speed must be 0 to 25 m/s");
|
|
418
|
+
Q({
|
|
419
|
+
at: R,
|
|
420
|
+
key: "speed",
|
|
421
|
+
value: e
|
|
422
|
+
});
|
|
423
|
+
},
|
|
424
|
+
get heading() {
|
|
425
|
+
return Te("heading");
|
|
426
|
+
},
|
|
427
|
+
set heading(e) {
|
|
428
|
+
Q({
|
|
429
|
+
at: R,
|
|
430
|
+
key: "heading",
|
|
431
|
+
value: te(e, "heading")
|
|
432
|
+
});
|
|
433
|
+
},
|
|
434
|
+
get gait() {
|
|
435
|
+
return Te("gait");
|
|
436
|
+
},
|
|
437
|
+
set gait(e) {
|
|
438
|
+
if (e !== "auto" && !F.has(e)) throw RangeError("Horse gait must be 'auto', 'walk', 'trot', 'canter' or 'gallop'");
|
|
439
|
+
Q({
|
|
440
|
+
at: R,
|
|
441
|
+
key: "gait",
|
|
442
|
+
value: e
|
|
443
|
+
});
|
|
444
|
+
},
|
|
445
|
+
get currentSpeed() {
|
|
446
|
+
return L.speed;
|
|
447
|
+
},
|
|
448
|
+
get currentGait() {
|
|
449
|
+
return L.gait;
|
|
450
|
+
},
|
|
451
|
+
get distance() {
|
|
452
|
+
return me().distance;
|
|
453
|
+
},
|
|
454
|
+
get pose() {
|
|
455
|
+
return U;
|
|
456
|
+
},
|
|
457
|
+
flickTail(e = 1, t = "left") {
|
|
458
|
+
if (!(e >= 0 && e <= 1) || t !== "left" && t !== "right") throw RangeError("Horse tail flick needs strength 0 to 1 and side left or right");
|
|
459
|
+
Q({
|
|
460
|
+
at: R,
|
|
461
|
+
key: "tail",
|
|
462
|
+
value: {
|
|
463
|
+
strength: e,
|
|
464
|
+
side: t === "left" ? 1 : -1
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
},
|
|
468
|
+
frame() {
|
|
469
|
+
let e = d(p, U), t = me(), n = t.heading, r = Math.cos(n), i = Math.sin(n), a = L.supportHeight(t.x, t.z, t.heading);
|
|
470
|
+
return {
|
|
471
|
+
position: [
|
|
472
|
+
t.x,
|
|
473
|
+
a,
|
|
474
|
+
t.z
|
|
475
|
+
],
|
|
476
|
+
heading: n,
|
|
477
|
+
saddle: [
|
|
478
|
+
t.x + r * e.position[0] + i * e.position[2],
|
|
479
|
+
a + e.position[1],
|
|
480
|
+
t.z - i * e.position[0] + r * e.position[2]
|
|
481
|
+
],
|
|
482
|
+
saddleRotation: ne([
|
|
483
|
+
0,
|
|
484
|
+
Math.sin(n / 2),
|
|
485
|
+
0,
|
|
486
|
+
Math.cos(n / 2)
|
|
487
|
+
], e.rotation)
|
|
488
|
+
};
|
|
489
|
+
},
|
|
490
|
+
advanceTime(e) {
|
|
491
|
+
if (!(e >= 0 && e < Infinity)) throw RangeError("advanceTime needs a finite number of seconds, 0 or more");
|
|
492
|
+
return we(R + e), $;
|
|
493
|
+
},
|
|
494
|
+
seek(e) {
|
|
495
|
+
if (!(e >= 0 && e < Infinity)) throw RangeError("seek needs a finite film time in seconds, 0 or more");
|
|
496
|
+
L = pe(), I.clear(), R = 0, z = 0, B = 0, q.length = 0, w.reset(), W = void 0, G = 0, V = {
|
|
497
|
+
x: L.x,
|
|
498
|
+
z: L.z,
|
|
499
|
+
heading: L.heading,
|
|
500
|
+
distance: L.distance
|
|
501
|
+
}, H = {
|
|
502
|
+
speed: 0,
|
|
503
|
+
heading: n.heading ?? 0,
|
|
504
|
+
gait: "auto"
|
|
505
|
+
};
|
|
506
|
+
for (let e of J) e.reset?.();
|
|
507
|
+
return we(e), $;
|
|
508
|
+
},
|
|
509
|
+
onStep(e, t) {
|
|
510
|
+
if (typeof e != "function") throw TypeError("onStep needs a function (dt, horse) => void");
|
|
511
|
+
let n = {
|
|
512
|
+
step: e,
|
|
513
|
+
reset: t
|
|
514
|
+
};
|
|
515
|
+
return J.add(n), () => void J.delete(n);
|
|
516
|
+
},
|
|
517
|
+
onPose(e) {
|
|
518
|
+
return Y.add(e), () => void Y.delete(e);
|
|
519
|
+
},
|
|
520
|
+
dispose: De,
|
|
521
|
+
[Symbol.dispose]: De
|
|
522
|
+
};
|
|
523
|
+
try {
|
|
524
|
+
Ce();
|
|
525
|
+
} catch (e) {
|
|
526
|
+
throw De(), e;
|
|
527
|
+
}
|
|
528
|
+
return $;
|
|
529
|
+
}
|
|
530
|
+
//#endregion
|
|
531
|
+
export { oe as n, ie as r, ae as t };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Engine } from '@margoengine/core';
|
|
2
|
+
import type { StrandSolver, StrandParams, StrandSpec } from '@margoengine/physics/strands';
|
|
3
|
+
import type { PhysicsWorld } from '@margoengine/physics/world';
|
|
4
|
+
import { type Horse, type HorseOptions } from './index.js';
|
|
5
|
+
/** Effective SI material and drag settings for a horsehair bundle. */
|
|
6
|
+
export type HorseFibreMaterial = Omit<StrandSpec, 'points' | 'anchor'>;
|
|
7
|
+
/** Physics controls for one horse's tail, mane and forelock. */
|
|
8
|
+
export interface HorseStrandSettings extends Omit<StrandParams, 'strands' | 'rate' | 'colliders'> {
|
|
9
|
+
/** Main tail guide effective fibre-bundle properties. */
|
|
10
|
+
readonly tail?: HorseFibreMaterial;
|
|
11
|
+
/** Mane and forelock guide effective fibre-bundle properties. */
|
|
12
|
+
readonly mane?: HorseFibreMaterial;
|
|
13
|
+
}
|
|
14
|
+
/** Options of {@link addStrandHorse}. */
|
|
15
|
+
export interface StrandHorseOptions extends Omit<HorseOptions, 'strandRig'> {
|
|
16
|
+
/** Guide settings, including a shared wind and bed. Defaults to rod guides in still air. */
|
|
17
|
+
readonly strands?: HorseStrandSettings;
|
|
18
|
+
/** Cancels native module loading. */
|
|
19
|
+
readonly signal?: AbortSignal;
|
|
20
|
+
}
|
|
21
|
+
/** The horse with its native solver and the clock advanced by the gait controller. */
|
|
22
|
+
export interface StrandHorse extends Horse {
|
|
23
|
+
readonly strandSolver: StrandSolver;
|
|
24
|
+
readonly strandWorld: PhysicsWorld;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Builds a horse whose weighted mane, forelock and tail follow elastic strand guides. Call the horse's normal
|
|
28
|
+
* `advanceTime` and `seek` methods to step or replay its native physics alongside its gait. Dispose as usual.
|
|
29
|
+
*/
|
|
30
|
+
export declare function addStrandHorse(engine: Engine, options?: StrandHorseOptions): Promise<StrandHorse>;
|