@hackbuild/cameras 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +88 -2
- package/dist/cameras.iife.js +359 -0
- package/dist/index.d.ts +157 -0
- package/dist/index.js +286 -0
- package/dist/single.d.ts +1 -0
- package/dist/single.js +2 -0
- package/package.json +40 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Moheeb Zara
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,89 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hackbuild/cameras
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Gameplay cameras that share damping, shake and occlusion handling. They
|
|
4
|
+
drive any camera with `position.set`, `lookAt`, `fov` and `aspect` (a
|
|
5
|
+
three PerspectiveCamera fits) and import nothing at runtime. Group framing
|
|
6
|
+
is written for this library from the shared camera of [crownfall](https://github.com/virgilvox/crownfall) (MIT, same
|
|
7
|
+
author).
|
|
8
|
+
|
|
9
|
+
What is in it:
|
|
10
|
+
|
|
11
|
+
- `Follow`: third person behind a heading with damped target and yaw; a
|
|
12
|
+
`clear(from, dir, max)` callback (your raycast) pulls the camera in at
|
|
13
|
+
once when something blocks the view and eases it back out.
|
|
14
|
+
- `Group`: keeps every point on screen at a fixed pitch and yaw. The
|
|
15
|
+
centre is pulled toward the arena centre first; the distance is then
|
|
16
|
+
solved so every corner of the padded box round the points (`pad` in x
|
|
17
|
+
and z, 1 m above and below) projects inside the screen band. Points
|
|
18
|
+
can leave the screen only when `maxDistance` clamps the distance.
|
|
19
|
+
- `Orbit`: a free orbit driven by deltas (rotate, zoom, pan); input
|
|
20
|
+
binding stays with the app.
|
|
21
|
+
- `FirstPerson`: an eye on an agent's head that scales the head to 0.001
|
|
22
|
+
for its render only (learned in earlier builds: hiding the head object would
|
|
23
|
+
hide the whole subtree).
|
|
24
|
+
- `Shake`: seeded value noise with decaying trauma (`Rng` injected). It
|
|
25
|
+
draws only while shaking and holds two knots. Give it an Rng stream of
|
|
26
|
+
its own, never the sim's: how many draws fall before a given sim time
|
|
27
|
+
depends on the frame steps.
|
|
28
|
+
- `splitViewports`: one to four views, with the y flipped for three.
|
|
29
|
+
- `damp`: frame rate independent exponential damping.
|
|
30
|
+
|
|
31
|
+
Not written: the two shot and duel cameras of the games survey, and
|
|
32
|
+
shared shadow maps for split views.
|
|
33
|
+
|
|
34
|
+
## Use
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { Follow, Shake } from "@hackbuild/cameras";
|
|
38
|
+
|
|
39
|
+
const follow = new Follow(camera, { distance: 4, clear: (from, dir, max) => raycast(from, dir, max) });
|
|
40
|
+
const shake = new Shake(shakeRng); // a stream of its own, never the sim rng
|
|
41
|
+
follow.update(dog.position, dog.yaw, dt);
|
|
42
|
+
const s = shake.step(dt);
|
|
43
|
+
camera.position.x += s.x;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
`dist/cameras.iife.js` is the single file build, a classic script that
|
|
47
|
+
defines `HackbuildCameras`.
|
|
48
|
+
|
|
49
|
+
## What is verified, and what is not
|
|
50
|
+
|
|
51
|
+
### Verified on this machine
|
|
52
|
+
|
|
53
|
+
- 2026-11-23: group framing over random point sets: 1,500 sets per
|
|
54
|
+
camera, six cameras (fov 40 to 70, aspect 9:16 to 21:9, centre pull 0
|
|
55
|
+
to 0.3, groups up to 20 m from the origin, three yaw and pitch pairs),
|
|
56
|
+
39,650 points projected with three.js, the worst at 0.876 of the half
|
|
57
|
+
screen (measured). Shake never kicked draws nothing over ten minutes
|
|
58
|
+
at 60 Hz (before: 32,403 draws and 10,801 knots held, measured); kicked
|
|
59
|
+
once at 0.5 it draws under 90 numbers (counted).
|
|
60
|
+
Before the distance was solved from projected
|
|
61
|
+
corners, 379 of 13,414 points left the screen at fov 50 and 16:9 with
|
|
62
|
+
no centre pull, worst 1.29 (measured in an audit).
|
|
63
|
+
- 2026-11-22: `npx vitest run packages/cameras` passed 7 of 7 tests
|
|
64
|
+
(counted): damping agrees at 60 and 30 Hz to 1e-9, group framing keeps
|
|
65
|
+
three spread points inside the three.js frustum, follow sits behind the
|
|
66
|
+
heading and pulls in to 1.2 m behind a wall at 1.5 m, shake replays from
|
|
67
|
+
a seed and decays, orbit pans to the view's right, the first person
|
|
68
|
+
render hides and restores the head, split viewports for 1, 3 and 4.
|
|
69
|
+
|
|
70
|
+
### Not verified by anything
|
|
71
|
+
|
|
72
|
+
- Any camera in a real browser at real frame rate: nobody has looked.
|
|
73
|
+
- Shake feel and follow smoothing values are starting values (inferred).
|
|
74
|
+
|
|
75
|
+
## Size budget
|
|
76
|
+
|
|
77
|
+
| file | gzip (measured) | ceiling |
|
|
78
|
+
|---|---|---|
|
|
79
|
+
| `dist/index.js` (main entry, imports external) | 3,265 bytes | 3,428 bytes |
|
|
80
|
+
| `dist/cameras.iife.js` (single file build) | 3,378 bytes | 3,546 bytes |
|
|
81
|
+
|
|
82
|
+
Measured 2026-11-23 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
83
|
+
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
|
84
|
+
5 percent from its row.
|
|
85
|
+
|
|
86
|
+
## Licence
|
|
87
|
+
|
|
88
|
+
Code MIT (`LICENSE`, a copy of the repository root one). This README is CC-BY-4.0, its
|
|
89
|
+
code samples also MIT.
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
var HackbuildCameras = (function(exports) {
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
// src/index.ts
|
|
4
|
+
/** Frame rate independent exponential damping toward `to` at `rate` per second. */
|
|
5
|
+
const damp = (from, to, rate, dt) => to + (from - to) * Math.exp(-rate * dt);
|
|
6
|
+
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
|
|
7
|
+
/**
|
|
8
|
+
* Shake: seeded value noise on three axes, amplitude decaying at `decay`
|
|
9
|
+
* per second. It draws from its Rng only while shaking (three numbers per
|
|
10
|
+
* knot, `frequency` knots a second) and holds two knots. Give it an Rng
|
|
11
|
+
* stream of its own: how many draws fall before a given sim time depends
|
|
12
|
+
* on the frame steps, so sharing the sim's stream would tie the sim to the
|
|
13
|
+
* frame rate.
|
|
14
|
+
*/
|
|
15
|
+
var Shake = class {
|
|
16
|
+
rng;
|
|
17
|
+
frequency;
|
|
18
|
+
decay;
|
|
19
|
+
amp = 0;
|
|
20
|
+
t = 0;
|
|
21
|
+
a = null;
|
|
22
|
+
b = [
|
|
23
|
+
0,
|
|
24
|
+
0,
|
|
25
|
+
0
|
|
26
|
+
];
|
|
27
|
+
constructor(rng, frequency = 18, decay = 6) {
|
|
28
|
+
this.rng = rng;
|
|
29
|
+
this.frequency = frequency;
|
|
30
|
+
this.decay = decay;
|
|
31
|
+
}
|
|
32
|
+
/** Adds trauma; amplitudes add and are capped at `max`. */
|
|
33
|
+
kick(amplitude, max = 1) {
|
|
34
|
+
this.amp = Math.min(max, this.amp + amplitude);
|
|
35
|
+
}
|
|
36
|
+
get amplitude() {
|
|
37
|
+
return this.amp;
|
|
38
|
+
}
|
|
39
|
+
knot() {
|
|
40
|
+
return [
|
|
41
|
+
this.rng() * 2 - 1,
|
|
42
|
+
this.rng() * 2 - 1,
|
|
43
|
+
this.rng() * 2 - 1
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
/** The offset for this frame. */
|
|
47
|
+
step(dt) {
|
|
48
|
+
if (this.amp === 0) return {
|
|
49
|
+
x: 0,
|
|
50
|
+
y: 0,
|
|
51
|
+
z: 0
|
|
52
|
+
};
|
|
53
|
+
if (!this.a) {
|
|
54
|
+
this.a = this.knot();
|
|
55
|
+
this.b = this.knot();
|
|
56
|
+
this.t = 0;
|
|
57
|
+
}
|
|
58
|
+
this.t += dt * this.frequency;
|
|
59
|
+
while (this.t >= 1) {
|
|
60
|
+
this.a = this.b;
|
|
61
|
+
this.b = this.knot();
|
|
62
|
+
this.t -= 1;
|
|
63
|
+
}
|
|
64
|
+
const a = this.a;
|
|
65
|
+
const b = this.b;
|
|
66
|
+
const f = this.t;
|
|
67
|
+
const s = f * f * (3 - 2 * f);
|
|
68
|
+
const v = (k) => (a[k] + (b[k] - a[k]) * s) * this.amp;
|
|
69
|
+
const out = {
|
|
70
|
+
x: v(0),
|
|
71
|
+
y: v(1),
|
|
72
|
+
z: v(2)
|
|
73
|
+
};
|
|
74
|
+
this.amp *= Math.exp(-this.decay * dt);
|
|
75
|
+
if (this.amp < 1e-4) {
|
|
76
|
+
this.amp = 0;
|
|
77
|
+
this.a = null;
|
|
78
|
+
}
|
|
79
|
+
return out;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
/** Places a camera on an orbit around a target: yaw 0 behind +Z, pitch up from the ground. */
|
|
83
|
+
function orbitPosition(target, yaw, pitch, dist) {
|
|
84
|
+
const cp = Math.cos(pitch);
|
|
85
|
+
return {
|
|
86
|
+
x: target.x - Math.sin(yaw) * cp * dist,
|
|
87
|
+
y: target.y + Math.sin(pitch) * dist,
|
|
88
|
+
z: target.z - Math.cos(yaw) * cp * dist
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** Third person follow behind a moving target, pulled in when something blocks the view. */
|
|
92
|
+
var Follow = class {
|
|
93
|
+
camera;
|
|
94
|
+
o;
|
|
95
|
+
target = null;
|
|
96
|
+
yaw = 0;
|
|
97
|
+
dist;
|
|
98
|
+
constructor(camera, o = {}) {
|
|
99
|
+
this.camera = camera;
|
|
100
|
+
this.o = {
|
|
101
|
+
distance: 4,
|
|
102
|
+
minDistance: .6,
|
|
103
|
+
pitch: .35,
|
|
104
|
+
lookHeight: 1.2,
|
|
105
|
+
smoothing: 8,
|
|
106
|
+
yawSmoothing: 4,
|
|
107
|
+
backoff: .3,
|
|
108
|
+
...o
|
|
109
|
+
};
|
|
110
|
+
this.dist = this.o.distance;
|
|
111
|
+
}
|
|
112
|
+
/** `heading` is the target's yaw (0 faces +Z). */
|
|
113
|
+
update(p, heading, dt, snap = false) {
|
|
114
|
+
const look = {
|
|
115
|
+
x: p.x,
|
|
116
|
+
y: p.y + this.o.lookHeight,
|
|
117
|
+
z: p.z
|
|
118
|
+
};
|
|
119
|
+
if (!this.target || snap) {
|
|
120
|
+
this.target = { ...look };
|
|
121
|
+
this.yaw = heading;
|
|
122
|
+
} else {
|
|
123
|
+
this.target.x = damp(this.target.x, look.x, this.o.smoothing, dt);
|
|
124
|
+
this.target.y = damp(this.target.y, look.y, this.o.smoothing, dt);
|
|
125
|
+
this.target.z = damp(this.target.z, look.z, this.o.smoothing, dt);
|
|
126
|
+
const d = Math.atan2(Math.sin(heading - this.yaw), Math.cos(heading - this.yaw));
|
|
127
|
+
this.yaw += d * (1 - Math.exp(-this.o.yawSmoothing * dt));
|
|
128
|
+
}
|
|
129
|
+
let want = this.o.distance;
|
|
130
|
+
if (this.o.clear) {
|
|
131
|
+
const ideal = orbitPosition(this.target, this.yaw, this.o.pitch, 1);
|
|
132
|
+
const dir = {
|
|
133
|
+
x: ideal.x - this.target.x,
|
|
134
|
+
y: ideal.y - this.target.y,
|
|
135
|
+
z: ideal.z - this.target.z
|
|
136
|
+
};
|
|
137
|
+
const free = this.o.clear(this.target, dir, this.o.distance);
|
|
138
|
+
if (free < this.o.distance) want = Math.max(this.o.minDistance, free - this.o.backoff);
|
|
139
|
+
}
|
|
140
|
+
this.dist = want < this.dist || snap ? want : damp(this.dist, want, 2, dt);
|
|
141
|
+
const c = orbitPosition(this.target, this.yaw, this.o.pitch, this.dist);
|
|
142
|
+
this.camera.position.set(c.x, c.y, c.z);
|
|
143
|
+
this.camera.lookAt(this.target.x, this.target.y, this.target.z);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
/** Keeps every point in view: the crownfall shared camera. */
|
|
147
|
+
var Group = class {
|
|
148
|
+
camera;
|
|
149
|
+
o;
|
|
150
|
+
target;
|
|
151
|
+
dist;
|
|
152
|
+
constructor(camera, o = {}) {
|
|
153
|
+
this.camera = camera;
|
|
154
|
+
this.o = {
|
|
155
|
+
center: {
|
|
156
|
+
x: 0,
|
|
157
|
+
y: 0,
|
|
158
|
+
z: 0
|
|
159
|
+
},
|
|
160
|
+
pitch: .92,
|
|
161
|
+
yaw: 0,
|
|
162
|
+
pad: 1.2,
|
|
163
|
+
minDistance: 3,
|
|
164
|
+
maxDistance: 60,
|
|
165
|
+
band: 1,
|
|
166
|
+
centerPull: .1,
|
|
167
|
+
...o
|
|
168
|
+
};
|
|
169
|
+
this.target = { ...this.o.center };
|
|
170
|
+
this.dist = this.o.maxDistance;
|
|
171
|
+
}
|
|
172
|
+
/** The distance and centre that frame the points (no damping). */
|
|
173
|
+
solve(points) {
|
|
174
|
+
const pts = points.length ? points : [this.o.center];
|
|
175
|
+
let minX = Infinity;
|
|
176
|
+
let maxX = -Infinity;
|
|
177
|
+
let minY = Infinity;
|
|
178
|
+
let maxY = -Infinity;
|
|
179
|
+
let minZ = Infinity;
|
|
180
|
+
let maxZ = -Infinity;
|
|
181
|
+
for (const p of pts) {
|
|
182
|
+
minX = Math.min(minX, p.x);
|
|
183
|
+
maxX = Math.max(maxX, p.x);
|
|
184
|
+
minY = Math.min(minY, p.y);
|
|
185
|
+
maxY = Math.max(maxY, p.y);
|
|
186
|
+
minZ = Math.min(minZ, p.z);
|
|
187
|
+
maxZ = Math.max(maxZ, p.z);
|
|
188
|
+
}
|
|
189
|
+
const { pad, pitch, band, centerPull: k, center } = this.o;
|
|
190
|
+
const cx = (minX + maxX) / 2;
|
|
191
|
+
const cz = (minZ + maxZ) / 2;
|
|
192
|
+
const target = {
|
|
193
|
+
x: cx + (center.x - cx) * k,
|
|
194
|
+
y: (minY + maxY) / 2,
|
|
195
|
+
z: cz + (center.z - cz) * k
|
|
196
|
+
};
|
|
197
|
+
const yaw = this.o.yaw + Math.PI;
|
|
198
|
+
const cp = Math.cos(pitch);
|
|
199
|
+
const fx = Math.sin(yaw) * cp;
|
|
200
|
+
const fy = -Math.sin(pitch);
|
|
201
|
+
const fz = Math.cos(yaw) * cp;
|
|
202
|
+
const rl = Math.hypot(fz, fx) || 1;
|
|
203
|
+
const rx = -fz / rl;
|
|
204
|
+
const rz = fx / rl;
|
|
205
|
+
const ux = -rz * fy;
|
|
206
|
+
const uy = rz * fx - rx * fz;
|
|
207
|
+
const uz = rx * fy;
|
|
208
|
+
const th = Math.tan(this.camera.fov * Math.PI / 360) * band;
|
|
209
|
+
const tw = Math.tan(this.camera.fov * Math.PI / 360) * this.camera.aspect;
|
|
210
|
+
let need = 0;
|
|
211
|
+
for (const x of [minX - pad, maxX + pad]) for (const y of [minY - 1, maxY + 1]) for (const z of [minZ - pad, maxZ + pad]) {
|
|
212
|
+
const qx = x - target.x;
|
|
213
|
+
const qy = y - target.y;
|
|
214
|
+
const qz = z - target.z;
|
|
215
|
+
const sx = Math.abs(qx * rx + qz * rz);
|
|
216
|
+
const sy = Math.abs(qx * ux + qy * uy + qz * uz);
|
|
217
|
+
need = Math.max(need, Math.max(sx / tw, sy / th) - (qx * fx + qy * fy + qz * fz));
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
target,
|
|
221
|
+
dist: clamp(need, this.o.minDistance, this.o.maxDistance)
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
update(points, dt, snap = false, shake) {
|
|
225
|
+
const s = this.solve(points);
|
|
226
|
+
if (snap) {
|
|
227
|
+
this.target = s.target;
|
|
228
|
+
this.dist = s.dist;
|
|
229
|
+
} else {
|
|
230
|
+
this.target.x = damp(this.target.x, s.target.x, 3.2, dt);
|
|
231
|
+
this.target.y = damp(this.target.y, s.target.y, 3.2, dt);
|
|
232
|
+
this.target.z = damp(this.target.z, s.target.z, 3.2, dt);
|
|
233
|
+
this.dist = damp(this.dist, s.dist, 2.4, dt);
|
|
234
|
+
}
|
|
235
|
+
const c = orbitPosition(this.target, this.o.yaw + Math.PI, this.o.pitch, this.dist);
|
|
236
|
+
this.camera.position.set(c.x + (shake?.x ?? 0), c.y + (shake?.y ?? 0), c.z + (shake?.z ?? 0));
|
|
237
|
+
this.camera.lookAt(this.target.x, this.target.y, this.target.z);
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
/** A free orbit camera driven by deltas (drag, wheel, keys); input binding stays with the app. */
|
|
241
|
+
var Orbit = class {
|
|
242
|
+
camera;
|
|
243
|
+
yaw;
|
|
244
|
+
pitch;
|
|
245
|
+
dist;
|
|
246
|
+
limits;
|
|
247
|
+
target;
|
|
248
|
+
constructor(camera, yaw = 0, pitch = .5, dist = 6, target = {
|
|
249
|
+
x: 0,
|
|
250
|
+
y: .5,
|
|
251
|
+
z: 0
|
|
252
|
+
}, limits = {
|
|
253
|
+
minPitch: .05,
|
|
254
|
+
maxPitch: 1.45,
|
|
255
|
+
minDist: 1,
|
|
256
|
+
maxDist: 60
|
|
257
|
+
}) {
|
|
258
|
+
this.camera = camera;
|
|
259
|
+
this.yaw = yaw;
|
|
260
|
+
this.pitch = pitch;
|
|
261
|
+
this.dist = dist;
|
|
262
|
+
this.limits = limits;
|
|
263
|
+
this.target = { ...target };
|
|
264
|
+
}
|
|
265
|
+
rotate(dYaw, dPitch) {
|
|
266
|
+
this.yaw += dYaw;
|
|
267
|
+
this.pitch = clamp(this.pitch + dPitch, this.limits.minPitch, this.limits.maxPitch);
|
|
268
|
+
}
|
|
269
|
+
zoom(factor) {
|
|
270
|
+
this.dist = clamp(this.dist * factor, this.limits.minDist, this.limits.maxDist);
|
|
271
|
+
}
|
|
272
|
+
/** Moves the target in the ground plane, relative to the view. */
|
|
273
|
+
pan(right, forward) {
|
|
274
|
+
this.target.x += -Math.cos(this.yaw) * right + Math.sin(this.yaw) * forward;
|
|
275
|
+
this.target.z += Math.sin(this.yaw) * right + Math.cos(this.yaw) * forward;
|
|
276
|
+
}
|
|
277
|
+
apply() {
|
|
278
|
+
const c = orbitPosition(this.target, this.yaw, this.pitch, this.dist);
|
|
279
|
+
this.camera.position.set(c.x, c.y, c.z);
|
|
280
|
+
this.camera.lookAt(this.target.x, this.target.y, this.target.z);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
/**
|
|
284
|
+
* A first person eye on an agent's head. `render` hides the head (scaled
|
|
285
|
+
* to 0.001, learned in earlier builds: visible false would hide the whole subtree)
|
|
286
|
+
* for this render only, then restores it.
|
|
287
|
+
*/
|
|
288
|
+
var FirstPerson = class {
|
|
289
|
+
camera;
|
|
290
|
+
head;
|
|
291
|
+
forward;
|
|
292
|
+
up;
|
|
293
|
+
constructor(camera, head = null, forward = .11, up = .07) {
|
|
294
|
+
this.camera = camera;
|
|
295
|
+
this.head = head;
|
|
296
|
+
this.forward = forward;
|
|
297
|
+
this.up = up;
|
|
298
|
+
}
|
|
299
|
+
/** Head position, yaw (0 faces +Z) and pitch (up positive). */
|
|
300
|
+
place(head, yaw, pitch) {
|
|
301
|
+
const d = {
|
|
302
|
+
x: Math.sin(yaw) * Math.cos(pitch),
|
|
303
|
+
y: Math.sin(pitch),
|
|
304
|
+
z: Math.cos(yaw) * Math.cos(pitch)
|
|
305
|
+
};
|
|
306
|
+
const p = {
|
|
307
|
+
x: head.x + d.x * this.forward,
|
|
308
|
+
y: head.y + this.up + d.y * this.forward,
|
|
309
|
+
z: head.z + d.z * this.forward
|
|
310
|
+
};
|
|
311
|
+
this.camera.position.set(p.x, p.y, p.z);
|
|
312
|
+
this.camera.lookAt(p.x + d.x, p.y + d.y, p.z + d.z);
|
|
313
|
+
}
|
|
314
|
+
render(draw) {
|
|
315
|
+
if (!this.head) return draw();
|
|
316
|
+
const s = {
|
|
317
|
+
x: this.head.scale.x,
|
|
318
|
+
y: this.head.scale.y,
|
|
319
|
+
z: this.head.scale.z
|
|
320
|
+
};
|
|
321
|
+
this.head.scale.set(.001, .001, .001);
|
|
322
|
+
try {
|
|
323
|
+
draw();
|
|
324
|
+
} finally {
|
|
325
|
+
this.head.scale.set(s.x, s.y, s.z);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
/** Viewports for 1 to 4 split views in pixels, top left origin; three wants bottom left, so `y` is flipped for it. */
|
|
330
|
+
function splitViewports(n, width, height) {
|
|
331
|
+
const out = [];
|
|
332
|
+
const cols = n <= 1 ? 1 : 2;
|
|
333
|
+
const rows = n <= 2 ? 1 : 2;
|
|
334
|
+
const w = Math.floor(width / cols);
|
|
335
|
+
const h = Math.floor(height / rows);
|
|
336
|
+
for (let i = 0; i < Math.min(n, 4); i++) {
|
|
337
|
+
const c = i % cols;
|
|
338
|
+
const r = Math.floor(i / cols);
|
|
339
|
+
const vw = n === 3 && i === 2 ? width : w;
|
|
340
|
+
out.push({
|
|
341
|
+
x: c * w,
|
|
342
|
+
y: r * h,
|
|
343
|
+
w: vw,
|
|
344
|
+
h,
|
|
345
|
+
yGl: height - (r + 1) * h
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
return out;
|
|
349
|
+
}
|
|
350
|
+
exports.FirstPerson = FirstPerson;
|
|
351
|
+
exports.Follow = Follow;
|
|
352
|
+
exports.Group = Group;
|
|
353
|
+
exports.Orbit = Orbit;
|
|
354
|
+
exports.Shake = Shake;
|
|
355
|
+
exports.damp = damp;
|
|
356
|
+
exports.orbitPosition = orbitPosition;
|
|
357
|
+
exports.splitViewports = splitViewports;
|
|
358
|
+
return exports;
|
|
359
|
+
})({});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export type Rng = () => number;
|
|
2
|
+
export interface V3 {
|
|
3
|
+
x: number;
|
|
4
|
+
y: number;
|
|
5
|
+
z: number;
|
|
6
|
+
}
|
|
7
|
+
export interface CameraLike {
|
|
8
|
+
position: {
|
|
9
|
+
set(x: number, y: number, z: number): unknown;
|
|
10
|
+
x: number;
|
|
11
|
+
y: number;
|
|
12
|
+
z: number;
|
|
13
|
+
};
|
|
14
|
+
lookAt(x: number, y: number, z: number): void;
|
|
15
|
+
fov: number;
|
|
16
|
+
aspect: number;
|
|
17
|
+
}
|
|
18
|
+
/** Frame rate independent exponential damping toward `to` at `rate` per second. */
|
|
19
|
+
export declare const damp: (from: number, to: number, rate: number, dt: number) => number;
|
|
20
|
+
/**
|
|
21
|
+
* Shake: seeded value noise on three axes, amplitude decaying at `decay`
|
|
22
|
+
* per second. It draws from its Rng only while shaking (three numbers per
|
|
23
|
+
* knot, `frequency` knots a second) and holds two knots. Give it an Rng
|
|
24
|
+
* stream of its own: how many draws fall before a given sim time depends
|
|
25
|
+
* on the frame steps, so sharing the sim's stream would tie the sim to the
|
|
26
|
+
* frame rate.
|
|
27
|
+
*/
|
|
28
|
+
export declare class Shake {
|
|
29
|
+
private rng;
|
|
30
|
+
readonly frequency: number;
|
|
31
|
+
readonly decay: number;
|
|
32
|
+
private amp;
|
|
33
|
+
private t;
|
|
34
|
+
private a;
|
|
35
|
+
private b;
|
|
36
|
+
constructor(rng: Rng, frequency?: number, decay?: number);
|
|
37
|
+
/** Adds trauma; amplitudes add and are capped at `max`. */
|
|
38
|
+
kick(amplitude: number, max?: number): void;
|
|
39
|
+
get amplitude(): number;
|
|
40
|
+
private knot;
|
|
41
|
+
/** The offset for this frame. */
|
|
42
|
+
step(dt: number): V3;
|
|
43
|
+
}
|
|
44
|
+
/** Places a camera on an orbit around a target: yaw 0 behind +Z, pitch up from the ground. */
|
|
45
|
+
export declare function orbitPosition(target: V3, yaw: number, pitch: number, dist: number): V3;
|
|
46
|
+
export interface FollowOptions {
|
|
47
|
+
distance?: number;
|
|
48
|
+
minDistance?: number;
|
|
49
|
+
pitch?: number;
|
|
50
|
+
/** height of the look target above the followed point */
|
|
51
|
+
lookHeight?: number;
|
|
52
|
+
/** damping rates per second for the target and the yaw */
|
|
53
|
+
smoothing?: number;
|
|
54
|
+
yawSmoothing?: number;
|
|
55
|
+
/** metres kept in front of an occluder */
|
|
56
|
+
backoff?: number;
|
|
57
|
+
/**
|
|
58
|
+
* The free distance from the look target toward the camera along a unit
|
|
59
|
+
* direction, up to `max` (a raycast against the world); omit for no
|
|
60
|
+
* occlusion handling.
|
|
61
|
+
*/
|
|
62
|
+
clear?: (from: V3, dir: V3, max: number) => number;
|
|
63
|
+
}
|
|
64
|
+
/** Third person follow behind a moving target, pulled in when something blocks the view. */
|
|
65
|
+
export declare class Follow {
|
|
66
|
+
readonly camera: CameraLike;
|
|
67
|
+
readonly o: Required<Omit<FollowOptions, "clear">> & Pick<FollowOptions, "clear">;
|
|
68
|
+
private target;
|
|
69
|
+
private yaw;
|
|
70
|
+
dist: number;
|
|
71
|
+
constructor(camera: CameraLike, o?: FollowOptions);
|
|
72
|
+
/** `heading` is the target's yaw (0 faces +Z). */
|
|
73
|
+
update(p: V3, heading: number, dt: number, snap?: boolean): void;
|
|
74
|
+
}
|
|
75
|
+
export interface GroupOptions {
|
|
76
|
+
center?: V3;
|
|
77
|
+
pitch?: number;
|
|
78
|
+
yaw?: number;
|
|
79
|
+
/** padding around the group (m; crownfall 5.5) */
|
|
80
|
+
pad?: number;
|
|
81
|
+
minDistance?: number;
|
|
82
|
+
maxDistance?: number;
|
|
83
|
+
/** usable share of the screen height (crownfall 0.64, the rest is HUD) */
|
|
84
|
+
band?: number;
|
|
85
|
+
/** pull of the framing centre toward `center` */
|
|
86
|
+
centerPull?: number;
|
|
87
|
+
}
|
|
88
|
+
/** Keeps every point in view: the crownfall shared camera. */
|
|
89
|
+
export declare class Group {
|
|
90
|
+
readonly camera: CameraLike;
|
|
91
|
+
readonly o: Required<GroupOptions>;
|
|
92
|
+
target: V3;
|
|
93
|
+
dist: number;
|
|
94
|
+
constructor(camera: CameraLike, o?: GroupOptions);
|
|
95
|
+
/** The distance and centre that frame the points (no damping). */
|
|
96
|
+
solve(points: readonly V3[]): {
|
|
97
|
+
target: V3;
|
|
98
|
+
dist: number;
|
|
99
|
+
};
|
|
100
|
+
update(points: readonly V3[], dt: number, snap?: boolean, shake?: V3): void;
|
|
101
|
+
}
|
|
102
|
+
/** A free orbit camera driven by deltas (drag, wheel, keys); input binding stays with the app. */
|
|
103
|
+
export declare class Orbit {
|
|
104
|
+
readonly camera: CameraLike;
|
|
105
|
+
yaw: number;
|
|
106
|
+
pitch: number;
|
|
107
|
+
dist: number;
|
|
108
|
+
readonly limits: {
|
|
109
|
+
minPitch: number;
|
|
110
|
+
maxPitch: number;
|
|
111
|
+
minDist: number;
|
|
112
|
+
maxDist: number;
|
|
113
|
+
};
|
|
114
|
+
target: V3;
|
|
115
|
+
constructor(camera: CameraLike, yaw?: number, pitch?: number, dist?: number, target?: V3, limits?: {
|
|
116
|
+
minPitch: number;
|
|
117
|
+
maxPitch: number;
|
|
118
|
+
minDist: number;
|
|
119
|
+
maxDist: number;
|
|
120
|
+
});
|
|
121
|
+
rotate(dYaw: number, dPitch: number): void;
|
|
122
|
+
zoom(factor: number): void;
|
|
123
|
+
/** Moves the target in the ground plane, relative to the view. */
|
|
124
|
+
pan(right: number, forward: number): void;
|
|
125
|
+
apply(): void;
|
|
126
|
+
}
|
|
127
|
+
export interface Scalable {
|
|
128
|
+
scale: {
|
|
129
|
+
x: number;
|
|
130
|
+
y: number;
|
|
131
|
+
z: number;
|
|
132
|
+
set(x: number, y: number, z: number): unknown;
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* A first person eye on an agent's head. `render` hides the head (scaled
|
|
137
|
+
* to 0.001, learned in earlier builds: visible false would hide the whole subtree)
|
|
138
|
+
* for this render only, then restores it.
|
|
139
|
+
*/
|
|
140
|
+
export declare class FirstPerson {
|
|
141
|
+
readonly camera: CameraLike;
|
|
142
|
+
readonly head: Scalable | null;
|
|
143
|
+
readonly forward: number;
|
|
144
|
+
readonly up: number;
|
|
145
|
+
constructor(camera: CameraLike, head?: Scalable | null, forward?: number, up?: number);
|
|
146
|
+
/** Head position, yaw (0 faces +Z) and pitch (up positive). */
|
|
147
|
+
place(head: V3, yaw: number, pitch: number): void;
|
|
148
|
+
render(draw: () => void): void;
|
|
149
|
+
}
|
|
150
|
+
/** Viewports for 1 to 4 split views in pixels, top left origin; three wants bottom left, so `y` is flipped for it. */
|
|
151
|
+
export declare function splitViewports(n: number, width: number, height: number): {
|
|
152
|
+
x: number;
|
|
153
|
+
y: number;
|
|
154
|
+
w: number;
|
|
155
|
+
h: number;
|
|
156
|
+
yGl: number;
|
|
157
|
+
}[];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
// @hackbuild/cameras: gameplay cameras that share damping, shake and
|
|
2
|
+
// occlusion handling. They drive any camera with `position.set`, `lookAt`,
|
|
3
|
+
// `fov` and `aspect` (a three PerspectiveCamera fits) and import nothing at
|
|
4
|
+
// runtime. Group framing is written for this library from the shared
|
|
5
|
+
// camera of crownfall (https://github.com/virgilvox/crownfall, MIT, same
|
|
6
|
+
// author); the first person eye follows a lesson of earlier builds: hide
|
|
7
|
+
// the agent's own head for that render only.
|
|
8
|
+
/** Frame rate independent exponential damping toward `to` at `rate` per second. */
|
|
9
|
+
export const damp = (from, to, rate, dt) => to + (from - to) * Math.exp(-rate * dt);
|
|
10
|
+
const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
|
|
11
|
+
/**
|
|
12
|
+
* Shake: seeded value noise on three axes, amplitude decaying at `decay`
|
|
13
|
+
* per second. It draws from its Rng only while shaking (three numbers per
|
|
14
|
+
* knot, `frequency` knots a second) and holds two knots. Give it an Rng
|
|
15
|
+
* stream of its own: how many draws fall before a given sim time depends
|
|
16
|
+
* on the frame steps, so sharing the sim's stream would tie the sim to the
|
|
17
|
+
* frame rate.
|
|
18
|
+
*/
|
|
19
|
+
export class Shake {
|
|
20
|
+
rng;
|
|
21
|
+
frequency;
|
|
22
|
+
decay;
|
|
23
|
+
amp = 0;
|
|
24
|
+
t = 0;
|
|
25
|
+
a = null;
|
|
26
|
+
b = [0, 0, 0];
|
|
27
|
+
constructor(rng, frequency = 18, decay = 6) {
|
|
28
|
+
this.rng = rng;
|
|
29
|
+
this.frequency = frequency;
|
|
30
|
+
this.decay = decay;
|
|
31
|
+
}
|
|
32
|
+
/** Adds trauma; amplitudes add and are capped at `max`. */
|
|
33
|
+
kick(amplitude, max = 1) {
|
|
34
|
+
this.amp = Math.min(max, this.amp + amplitude);
|
|
35
|
+
}
|
|
36
|
+
get amplitude() {
|
|
37
|
+
return this.amp;
|
|
38
|
+
}
|
|
39
|
+
knot() {
|
|
40
|
+
return [this.rng() * 2 - 1, this.rng() * 2 - 1, this.rng() * 2 - 1];
|
|
41
|
+
}
|
|
42
|
+
/** The offset for this frame. */
|
|
43
|
+
step(dt) {
|
|
44
|
+
if (this.amp === 0)
|
|
45
|
+
return { x: 0, y: 0, z: 0 };
|
|
46
|
+
if (!this.a) {
|
|
47
|
+
this.a = this.knot();
|
|
48
|
+
this.b = this.knot();
|
|
49
|
+
this.t = 0;
|
|
50
|
+
}
|
|
51
|
+
this.t += dt * this.frequency;
|
|
52
|
+
while (this.t >= 1) {
|
|
53
|
+
this.a = this.b;
|
|
54
|
+
this.b = this.knot();
|
|
55
|
+
this.t -= 1;
|
|
56
|
+
}
|
|
57
|
+
const a = this.a;
|
|
58
|
+
const b = this.b;
|
|
59
|
+
const f = this.t;
|
|
60
|
+
const s = f * f * (3 - 2 * f);
|
|
61
|
+
const v = (k) => (a[k] + (b[k] - a[k]) * s) * this.amp;
|
|
62
|
+
const out = { x: v(0), y: v(1), z: v(2) };
|
|
63
|
+
this.amp *= Math.exp(-this.decay * dt);
|
|
64
|
+
if (this.amp < 1e-4) {
|
|
65
|
+
this.amp = 0;
|
|
66
|
+
this.a = null;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Places a camera on an orbit around a target: yaw 0 behind +Z, pitch up from the ground. */
|
|
72
|
+
export function orbitPosition(target, yaw, pitch, dist) {
|
|
73
|
+
const cp = Math.cos(pitch);
|
|
74
|
+
return { x: target.x - Math.sin(yaw) * cp * dist, y: target.y + Math.sin(pitch) * dist, z: target.z - Math.cos(yaw) * cp * dist };
|
|
75
|
+
}
|
|
76
|
+
/** Third person follow behind a moving target, pulled in when something blocks the view. */
|
|
77
|
+
export class Follow {
|
|
78
|
+
camera;
|
|
79
|
+
o;
|
|
80
|
+
target = null;
|
|
81
|
+
yaw = 0;
|
|
82
|
+
dist;
|
|
83
|
+
constructor(camera, o = {}) {
|
|
84
|
+
this.camera = camera;
|
|
85
|
+
this.o = { distance: 4, minDistance: 0.6, pitch: 0.35, lookHeight: 1.2, smoothing: 8, yawSmoothing: 4, backoff: 0.3, ...o };
|
|
86
|
+
this.dist = this.o.distance;
|
|
87
|
+
}
|
|
88
|
+
/** `heading` is the target's yaw (0 faces +Z). */
|
|
89
|
+
update(p, heading, dt, snap = false) {
|
|
90
|
+
const look = { x: p.x, y: p.y + this.o.lookHeight, z: p.z };
|
|
91
|
+
if (!this.target || snap) {
|
|
92
|
+
this.target = { ...look };
|
|
93
|
+
this.yaw = heading;
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.target.x = damp(this.target.x, look.x, this.o.smoothing, dt);
|
|
97
|
+
this.target.y = damp(this.target.y, look.y, this.o.smoothing, dt);
|
|
98
|
+
this.target.z = damp(this.target.z, look.z, this.o.smoothing, dt);
|
|
99
|
+
const d = Math.atan2(Math.sin(heading - this.yaw), Math.cos(heading - this.yaw));
|
|
100
|
+
this.yaw += d * (1 - Math.exp(-this.o.yawSmoothing * dt));
|
|
101
|
+
}
|
|
102
|
+
let want = this.o.distance;
|
|
103
|
+
if (this.o.clear) {
|
|
104
|
+
const ideal = orbitPosition(this.target, this.yaw, this.o.pitch, 1);
|
|
105
|
+
const dir = { x: ideal.x - this.target.x, y: ideal.y - this.target.y, z: ideal.z - this.target.z };
|
|
106
|
+
const free = this.o.clear(this.target, dir, this.o.distance);
|
|
107
|
+
if (free < this.o.distance)
|
|
108
|
+
want = Math.max(this.o.minDistance, free - this.o.backoff);
|
|
109
|
+
}
|
|
110
|
+
// pull in at once when blocked, ease back out when clear
|
|
111
|
+
this.dist = want < this.dist || snap ? want : damp(this.dist, want, 2, dt);
|
|
112
|
+
const c = orbitPosition(this.target, this.yaw, this.o.pitch, this.dist);
|
|
113
|
+
this.camera.position.set(c.x, c.y, c.z);
|
|
114
|
+
this.camera.lookAt(this.target.x, this.target.y, this.target.z);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/** Keeps every point in view: the crownfall shared camera. */
|
|
118
|
+
export class Group {
|
|
119
|
+
camera;
|
|
120
|
+
o;
|
|
121
|
+
target;
|
|
122
|
+
dist;
|
|
123
|
+
constructor(camera, o = {}) {
|
|
124
|
+
this.camera = camera;
|
|
125
|
+
this.o = { center: { x: 0, y: 0, z: 0 }, pitch: 0.92, yaw: 0, pad: 1.2, minDistance: 3, maxDistance: 60, band: 1, centerPull: 0.1, ...o };
|
|
126
|
+
this.target = { ...this.o.center };
|
|
127
|
+
this.dist = this.o.maxDistance;
|
|
128
|
+
}
|
|
129
|
+
/** The distance and centre that frame the points (no damping). */
|
|
130
|
+
solve(points) {
|
|
131
|
+
const pts = points.length ? points : [this.o.center];
|
|
132
|
+
let minX = Infinity;
|
|
133
|
+
let maxX = -Infinity;
|
|
134
|
+
let minY = Infinity;
|
|
135
|
+
let maxY = -Infinity;
|
|
136
|
+
let minZ = Infinity;
|
|
137
|
+
let maxZ = -Infinity;
|
|
138
|
+
for (const p of pts) {
|
|
139
|
+
minX = Math.min(minX, p.x);
|
|
140
|
+
maxX = Math.max(maxX, p.x);
|
|
141
|
+
minY = Math.min(minY, p.y);
|
|
142
|
+
maxY = Math.max(maxY, p.y);
|
|
143
|
+
minZ = Math.min(minZ, p.z);
|
|
144
|
+
maxZ = Math.max(maxZ, p.z);
|
|
145
|
+
}
|
|
146
|
+
const { pad, pitch, band, centerPull: k, center } = this.o;
|
|
147
|
+
// the centre pull comes first, so the distance below frames the pulled centre
|
|
148
|
+
const cx = (minX + maxX) / 2;
|
|
149
|
+
const cz = (minZ + maxZ) / 2;
|
|
150
|
+
const target = { x: cx + (center.x - cx) * k, y: (minY + maxY) / 2, z: cz + (center.z - cz) * k };
|
|
151
|
+
// camera basis for the orbit at this yaw and pitch: b from the target to
|
|
152
|
+
// the camera, f the view direction, r and u the screen axes
|
|
153
|
+
const yaw = this.o.yaw + Math.PI;
|
|
154
|
+
const cp = Math.cos(pitch);
|
|
155
|
+
const fx = Math.sin(yaw) * cp;
|
|
156
|
+
const fy = -Math.sin(pitch);
|
|
157
|
+
const fz = Math.cos(yaw) * cp;
|
|
158
|
+
const rl = Math.hypot(fz, fx) || 1;
|
|
159
|
+
const rx = -fz / rl;
|
|
160
|
+
const rz = fx / rl;
|
|
161
|
+
const ux = -rz * fy;
|
|
162
|
+
const uy = rz * fx - rx * fz;
|
|
163
|
+
const uz = rx * fy;
|
|
164
|
+
const th = Math.tan((this.camera.fov * Math.PI) / 360) * band;
|
|
165
|
+
const tw = Math.tan((this.camera.fov * Math.PI) / 360) * this.camera.aspect;
|
|
166
|
+
// every corner of the padded box (pad in x and z, 1 m above and below)
|
|
167
|
+
// must project inside the screen; depth grows one to one with the
|
|
168
|
+
// distance while the screen offsets stay fixed, so each corner gives a
|
|
169
|
+
// least distance in closed form
|
|
170
|
+
let need = 0;
|
|
171
|
+
for (const x of [minX - pad, maxX + pad])
|
|
172
|
+
for (const y of [minY - 1, maxY + 1])
|
|
173
|
+
for (const z of [minZ - pad, maxZ + pad]) {
|
|
174
|
+
const qx = x - target.x;
|
|
175
|
+
const qy = y - target.y;
|
|
176
|
+
const qz = z - target.z;
|
|
177
|
+
const sx = Math.abs(qx * rx + qz * rz);
|
|
178
|
+
const sy = Math.abs(qx * ux + qy * uy + qz * uz);
|
|
179
|
+
need = Math.max(need, Math.max(sx / tw, sy / th) - (qx * fx + qy * fy + qz * fz));
|
|
180
|
+
}
|
|
181
|
+
return { target, dist: clamp(need, this.o.minDistance, this.o.maxDistance) };
|
|
182
|
+
}
|
|
183
|
+
update(points, dt, snap = false, shake) {
|
|
184
|
+
const s = this.solve(points);
|
|
185
|
+
if (snap) {
|
|
186
|
+
this.target = s.target;
|
|
187
|
+
this.dist = s.dist;
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
this.target.x = damp(this.target.x, s.target.x, 3.2, dt);
|
|
191
|
+
this.target.y = damp(this.target.y, s.target.y, 3.2, dt);
|
|
192
|
+
this.target.z = damp(this.target.z, s.target.z, 3.2, dt);
|
|
193
|
+
this.dist = damp(this.dist, s.dist, 2.4, dt);
|
|
194
|
+
}
|
|
195
|
+
const c = orbitPosition(this.target, this.o.yaw + Math.PI, this.o.pitch, this.dist);
|
|
196
|
+
this.camera.position.set(c.x + (shake?.x ?? 0), c.y + (shake?.y ?? 0), c.z + (shake?.z ?? 0));
|
|
197
|
+
this.camera.lookAt(this.target.x, this.target.y, this.target.z);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/** A free orbit camera driven by deltas (drag, wheel, keys); input binding stays with the app. */
|
|
201
|
+
export class Orbit {
|
|
202
|
+
camera;
|
|
203
|
+
yaw;
|
|
204
|
+
pitch;
|
|
205
|
+
dist;
|
|
206
|
+
limits;
|
|
207
|
+
target;
|
|
208
|
+
constructor(camera, yaw = 0, pitch = 0.5, dist = 6, target = { x: 0, y: 0.5, z: 0 }, limits = { minPitch: 0.05, maxPitch: 1.45, minDist: 1, maxDist: 60 }) {
|
|
209
|
+
this.camera = camera;
|
|
210
|
+
this.yaw = yaw;
|
|
211
|
+
this.pitch = pitch;
|
|
212
|
+
this.dist = dist;
|
|
213
|
+
this.limits = limits;
|
|
214
|
+
this.target = { ...target };
|
|
215
|
+
}
|
|
216
|
+
rotate(dYaw, dPitch) {
|
|
217
|
+
this.yaw += dYaw;
|
|
218
|
+
this.pitch = clamp(this.pitch + dPitch, this.limits.minPitch, this.limits.maxPitch);
|
|
219
|
+
}
|
|
220
|
+
zoom(factor) {
|
|
221
|
+
this.dist = clamp(this.dist * factor, this.limits.minDist, this.limits.maxDist);
|
|
222
|
+
}
|
|
223
|
+
/** Moves the target in the ground plane, relative to the view. */
|
|
224
|
+
pan(right, forward) {
|
|
225
|
+
// with y up and yaw 0 along +z, the right of the view (sin yaw, cos yaw) is (-cos yaw, sin yaw)
|
|
226
|
+
this.target.x += -Math.cos(this.yaw) * right + Math.sin(this.yaw) * forward;
|
|
227
|
+
this.target.z += Math.sin(this.yaw) * right + Math.cos(this.yaw) * forward;
|
|
228
|
+
}
|
|
229
|
+
apply() {
|
|
230
|
+
const c = orbitPosition(this.target, this.yaw, this.pitch, this.dist);
|
|
231
|
+
this.camera.position.set(c.x, c.y, c.z);
|
|
232
|
+
this.camera.lookAt(this.target.x, this.target.y, this.target.z);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* A first person eye on an agent's head. `render` hides the head (scaled
|
|
237
|
+
* to 0.001, learned in earlier builds: visible false would hide the whole subtree)
|
|
238
|
+
* for this render only, then restores it.
|
|
239
|
+
*/
|
|
240
|
+
export class FirstPerson {
|
|
241
|
+
camera;
|
|
242
|
+
head;
|
|
243
|
+
forward;
|
|
244
|
+
up;
|
|
245
|
+
constructor(camera, head = null, forward = 0.11, up = 0.07) {
|
|
246
|
+
this.camera = camera;
|
|
247
|
+
this.head = head;
|
|
248
|
+
this.forward = forward;
|
|
249
|
+
this.up = up;
|
|
250
|
+
}
|
|
251
|
+
/** Head position, yaw (0 faces +Z) and pitch (up positive). */
|
|
252
|
+
place(head, yaw, pitch) {
|
|
253
|
+
const d = { x: Math.sin(yaw) * Math.cos(pitch), y: Math.sin(pitch), z: Math.cos(yaw) * Math.cos(pitch) };
|
|
254
|
+
const p = { x: head.x + d.x * this.forward, y: head.y + this.up + d.y * this.forward, z: head.z + d.z * this.forward };
|
|
255
|
+
this.camera.position.set(p.x, p.y, p.z);
|
|
256
|
+
this.camera.lookAt(p.x + d.x, p.y + d.y, p.z + d.z);
|
|
257
|
+
}
|
|
258
|
+
render(draw) {
|
|
259
|
+
if (!this.head)
|
|
260
|
+
return draw();
|
|
261
|
+
const s = { x: this.head.scale.x, y: this.head.scale.y, z: this.head.scale.z };
|
|
262
|
+
this.head.scale.set(0.001, 0.001, 0.001);
|
|
263
|
+
try {
|
|
264
|
+
draw();
|
|
265
|
+
}
|
|
266
|
+
finally {
|
|
267
|
+
this.head.scale.set(s.x, s.y, s.z);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/** Viewports for 1 to 4 split views in pixels, top left origin; three wants bottom left, so `y` is flipped for it. */
|
|
272
|
+
export function splitViewports(n, width, height) {
|
|
273
|
+
const out = [];
|
|
274
|
+
const cols = n <= 1 ? 1 : 2;
|
|
275
|
+
const rows = n <= 2 ? 1 : 2;
|
|
276
|
+
const w = Math.floor(width / cols);
|
|
277
|
+
const h = Math.floor(height / rows);
|
|
278
|
+
for (let i = 0; i < Math.min(n, 4); i++) {
|
|
279
|
+
const c = i % cols;
|
|
280
|
+
const r = Math.floor(i / cols);
|
|
281
|
+
// three views: the third spans the bottom row
|
|
282
|
+
const vw = n === 3 && i === 2 ? width : w;
|
|
283
|
+
out.push({ x: c * w, y: r * h, w: vw, h, yGl: height - (r + 1) * h });
|
|
284
|
+
}
|
|
285
|
+
return out;
|
|
286
|
+
}
|
package/dist/single.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./index.js";
|
package/dist/single.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/cameras",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Follow, group framing, free orbit, first person eye and split view cameras with shared damping, seeded shake and occlusion backoff. Drives a three camera without importing three.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Moheeb Zara <hackbuildvideo@gmail.com>",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/hackbuild/hackbuild-agents.git",
|
|
11
|
+
"directory": "packages/cameras"
|
|
12
|
+
},
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^22.18.0 || >=24.11.0"
|
|
15
|
+
},
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./iife": "./dist/cameras.iife.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"unpkg": "./dist/cameras.iife.js",
|
|
30
|
+
"jsdelivr": "./dist/cameras.iife.js",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "node ../../tools/build-package.mjs cameras"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"three": ">=0.186.1 <0.188.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"three": {
|
|
39
|
+
"optional": true
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|