@hackbuild/cameras 0.1.1 → 0.1.2
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/README.md +135 -71
- package/dist/cameras.iife.js +1 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +1 -1
- package/llms.md +214 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,89 +1,153 @@
|
|
|
1
1
|
# @hackbuild/cameras
|
|
2
2
|
|
|
3
|
-
Gameplay cameras
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
|
3
|
+
Gameplay cameras for a browser sim: follow, group framing, orbit, first person and split views, with shared damping, seeded shake and occlusion pull in.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@hackbuild/cameras) [](../../LICENSE)  
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
- Follows a moving target from behind its heading, and pulls in at once when your raycast says something blocks the view.
|
|
10
|
+
- Frames a group of points so every one stays on screen at a fixed pitch and yaw.
|
|
11
|
+
- Orbits, zooms and pans from deltas, so input binding stays with your app.
|
|
12
|
+
- Puts an eye on an agent's head and hides that head for its own render only.
|
|
13
|
+
- Shakes with seeded value noise and decaying trauma, and splits the screen into one to four views.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @hackbuild/cameras three
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The package imports nothing at run time. It writes `position.set`, `lookAt`, `fov` and `aspect` on the camera you pass, so a three `PerspectiveCamera` fits; three (`>=0.186.1 <0.188.0`) is a peer only for its types and for your scene. The quick start below uses three for the camera.
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
35
24
|
|
|
36
25
|
```ts
|
|
37
26
|
import { Follow, Shake } from "@hackbuild/cameras";
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
camera
|
|
27
|
+
import { PerspectiveCamera } from "three";
|
|
28
|
+
|
|
29
|
+
const camera = new PerspectiveCamera(50, 16 / 9, 0.1, 200);
|
|
30
|
+
// your raycast: here a wall 2 m behind the look target, along -z
|
|
31
|
+
const clear = (from: { z: number }, dir: { z: number }, max: number) => (dir.z < 0 ? Math.min(max, 2 / -dir.z) : max);
|
|
32
|
+
const follow = new Follow(camera, { distance: 4, clear });
|
|
33
|
+
|
|
34
|
+
// shake takes a seeded stream of its own, never the sim's
|
|
35
|
+
let seed = 7;
|
|
36
|
+
const shake = new Shake(() => ((seed = (seed * 16807) % 2147483647) / 2147483647));
|
|
37
|
+
shake.kick(0.4);
|
|
38
|
+
|
|
39
|
+
const dog = { x: 0, y: 0, z: 0, yaw: 0 };
|
|
40
|
+
for (let frame = 0; frame < 60; frame++) {
|
|
41
|
+
dog.z += 0.02;
|
|
42
|
+
follow.update(dog, dog.yaw, 1 / 60);
|
|
43
|
+
camera.position.add(shake.step(1 / 60));
|
|
44
|
+
}
|
|
45
|
+
console.log(camera.position.toArray(), follow.dist);
|
|
44
46
|
```
|
|
45
47
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
+
## Live example
|
|
49
|
+
|
|
50
|
+
- The package page runs every rig on one moving target: [hackbuild-agents/cameras](https://hackbuild.github.io/hackbuild-agents/cameras/).
|
|
51
|
+
- The suite example: [a camera that follows](https://hackbuild.github.io/hackbuild-suite/#/examples/think/cameras).
|
|
52
|
+
- The [dog simulator](https://hackbuild.github.io/dog-simulator/) uses the follow camera for its side view.
|
|
53
|
+
|
|
54
|
+
## Concepts
|
|
55
|
+
|
|
56
|
+
### a camera you pass in
|
|
57
|
+
|
|
58
|
+
Every rig takes a `CameraLike`: an object with `position.set`, `lookAt`, `fov` and `aspect`. The package never imports three, so it adds about 3 kB to a page that already has a renderer, and a test can pass a plain object.
|
|
59
|
+
|
|
60
|
+
### damping that ignores the frame rate
|
|
61
|
+
|
|
62
|
+
`damp(from, to, rate, dt)` is exponential damping toward a target at a rate per second. Two runs at 60 Hz and 30 Hz land on the same value, so the camera feels the same on a slow phone.
|
|
63
|
+
|
|
64
|
+
### follow and occlusion
|
|
65
|
+
|
|
66
|
+
`Follow` sits behind the target's heading (yaw 0 faces +Z), with the target and the yaw damped on their own rates. Pass `clear(from, dir, max)`, your raycast from the look target toward the camera; when the free distance is shorter than `distance`, the camera moves in at once to that distance less `backoff`, never closer than `minDistance`, and eases back out when the view clears.
|
|
67
|
+
|
|
68
|
+
### group framing
|
|
69
|
+
|
|
70
|
+
`Group` keeps every point on screen. The framing centre is pulled toward `center` by `centerPull` first; the distance is then solved so every corner of the padded box round the points (`pad` in x and z, 1 m above and below) projects inside the screen band. Points can leave the screen only when `maxDistance` clamps the distance. `solve(points)` gives the target and distance without damping.
|
|
71
|
+
|
|
72
|
+
### orbit, first person and split views
|
|
73
|
+
|
|
74
|
+
`Orbit` takes `rotate`, `zoom` and `pan` deltas and clamps pitch and distance; call `apply()` after. `FirstPerson` places an eye a little forward and up from the head; `render(draw)` scales the head to 0.001 for that draw and restores it after, since hiding the head object would hide its whole subtree. `splitViewports(n, width, height)` gives one to four rectangles with `yGl` flipped for three's bottom left origin; with three views the third spans the bottom row.
|
|
75
|
+
|
|
76
|
+
### shake on its own stream
|
|
77
|
+
|
|
78
|
+
`Shake` draws three numbers per knot, `frequency` knots a second, and only while it shakes. How many draws fall before a given sim time depends on the frame steps, so give it an `Rng` of its own; sharing the sim's stream would tie the sim to the frame rate.
|
|
79
|
+
|
|
80
|
+
## API overview
|
|
81
|
+
|
|
82
|
+
| export | what it does |
|
|
83
|
+
|---|---|
|
|
84
|
+
| [`Follow`](../../docs/api/cameras/README.md#follow) | third person follow with occlusion pull in |
|
|
85
|
+
| [`Group`](../../docs/api/cameras/README.md#group) | keep every point of a group on screen |
|
|
86
|
+
| [`Orbit`](../../docs/api/cameras/README.md#orbit) | free orbit from rotate, zoom and pan deltas |
|
|
87
|
+
| [`FirstPerson`](../../docs/api/cameras/README.md#firstperson) | an eye on a head that hides the head for its render |
|
|
88
|
+
| [`Shake`](../../docs/api/cameras/README.md#shake) | seeded value noise shake with decaying trauma |
|
|
89
|
+
| [`splitViewports`](../../docs/api/cameras/README.md#splitviewports) | one to four split view rectangles |
|
|
90
|
+
| [`damp`](../../docs/api/cameras/README.md#damp) | frame rate independent exponential damping |
|
|
91
|
+
| [`orbitPosition`](../../docs/api/cameras/README.md#orbitposition) | a camera point on an orbit round a target |
|
|
92
|
+
| [`CameraLike`](../../docs/api/cameras/README.md#cameralike) | the camera surface the rigs write |
|
|
93
|
+
|
|
94
|
+
The full reference is in [docs/api/cameras](../../docs/api/cameras/README.md).
|
|
95
|
+
|
|
96
|
+
## Browser support
|
|
97
|
+
|
|
98
|
+
| feature | needs | Chrome | Safari | Firefox | phone |
|
|
99
|
+
|---|---|---|---|---|---|
|
|
100
|
+
| every rig | plain JavaScript, no DOM | yes | yes (inferred) | yes (inferred) | yes (inferred) |
|
|
101
|
+
| page opened from file:// | the classic script `dist/cameras.iife.js`, global `HackbuildCameras` | yes | yes (inferred) | yes (inferred) | unknown |
|
|
102
|
+
|
|
103
|
+
No WebGPU, wasm or worker is used. Rendering is your renderer's.
|
|
104
|
+
|
|
105
|
+
## Errors and troubleshooting
|
|
106
|
+
|
|
107
|
+
The package throws no errors of its own. What goes wrong is configuration:
|
|
108
|
+
|
|
109
|
+
- The camera snaps to the target every frame: `snap` is passed on every `update`. Pass it only on the first frame or after a teleport.
|
|
110
|
+
- The follow camera clips into walls: there is no `clear` callback. Pass your raycast as `clear(from, dir, max)`.
|
|
111
|
+
- A seeded replay drifts once the camera shakes: `Shake` shares the sim's rng. Give it a stream of its own.
|
|
112
|
+
- A split view is upside down in three: the top left `y` went to `setViewport`. Use `yGl` for three.
|
|
113
|
+
|
|
114
|
+
## Works with
|
|
115
|
+
|
|
116
|
+
- [@hackbuild/senses](../senses/README.md) renders its eye from a head point; `FirstPerson` is the same idea for the player's view.
|
|
117
|
+
- [@hackbuild/agents-views](../agents-views/README.md) draws the eye card through `FirstPerson`, so the dog's own head is hidden in its view.
|
|
118
|
+
- [@hackbuild/mover](../mover/README.md) moves the agents the cameras follow.
|
|
119
|
+
- [@hackbuild/tunables](../tunables/README.md) exposes damping and distance values to a panel for tuning.
|
|
120
|
+
|
|
121
|
+
## Changelog and licences
|
|
122
|
+
|
|
123
|
+
Changes are in the repository [CHANGELOG](../../CHANGELOG.md). Code is MIT ([LICENSE](../../LICENSE)); this README is CC-BY-4.0 with its code samples also MIT. Group framing is written for this library from the shared camera of [crownfall](https://github.com/virgilvox/crownfall) (MIT, same author).
|
|
48
124
|
|
|
49
125
|
## What is verified, and what is not
|
|
50
126
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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).
|
|
127
|
+
| date | check | result |
|
|
128
|
+
|---|---|---|
|
|
129
|
+
| 2026-11-25 | `npx vitest run packages/cameras` | 9 of 9 tests pass (counted) |
|
|
130
|
+
| 2026-11-23 | group framing over random point sets: 1,500 sets per camera, six cameras (fov 40 to 70, aspect 9:16 to 21:9, centre pull 0 to 0.3, groups up to 20 m from the origin), 39,650 points projected with three.js | worst point at 0.876 of the half screen (measured) |
|
|
131
|
+
| 2026-11-23 | the same sets before the distance was solved from projected corners | 379 of 13,414 points off screen at fov 50 and 16:9, worst 1.29 (measured) |
|
|
132
|
+
| 2026-11-23 | shake never kicked, ten minutes at 60 Hz | no draws (counted) |
|
|
133
|
+
| 2026-11-23 | shake kicked once at 0.5 | under 90 draws (counted) |
|
|
134
|
+
| 2026-11-22 | damping at 60 and 30 Hz | agrees to 1e-9 (measured) |
|
|
135
|
+
| 2026-11-22 | follow behind a wall at 1.5 m | pulls in to 1.2 m (measured) |
|
|
136
|
+
| 2026-11-22 | first person render, orbit pan, split viewports for 1, 3 and 4 | head hidden and restored; pan goes to the view's right; rectangles as expected |
|
|
137
|
+
|
|
138
|
+
Not verified by anything:
|
|
139
|
+
|
|
140
|
+
- Any camera in a real browser at a real frame rate has not been looked at by a person.
|
|
141
|
+
- Shake feel and follow smoothing values are starting values, not tuned against users.
|
|
142
|
+
- The two shot and duel cameras, and shared shadow maps for split views, are not written.
|
|
74
143
|
|
|
75
144
|
## Size budget
|
|
76
145
|
|
|
77
146
|
| file | gzip (measured) | ceiling |
|
|
78
147
|
|---|---|---|
|
|
79
|
-
| `dist/index.js` (main entry, imports external) | 3,
|
|
80
|
-
| `dist/cameras.iife.js` (single file build) | 3,
|
|
148
|
+
| `dist/index.js` (main entry, imports external) | 3,251 bytes | 3,413 bytes |
|
|
149
|
+
| `dist/cameras.iife.js` (single file build) | 3,366 bytes | 3,534 bytes |
|
|
81
150
|
|
|
82
|
-
Measured 2026-11-
|
|
151
|
+
Measured 2026-11-25 by `node tools/lint-packages.mjs` (gzip level 9) from the packed
|
|
83
152
|
tarball. Generated from `tools/size-budget.json`; CI fails when a file moves more than
|
|
84
153
|
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.
|
package/dist/cameras.iife.js
CHANGED
|
@@ -282,7 +282,7 @@ var HackbuildCameras = (function(exports) {
|
|
|
282
282
|
};
|
|
283
283
|
/**
|
|
284
284
|
* A first person eye on an agent's head. `render` hides the head (scaled
|
|
285
|
-
* to 0.001,
|
|
285
|
+
* to 0.001, since visible false would hide the whole subtree)
|
|
286
286
|
* for this render only, then restores it.
|
|
287
287
|
*/
|
|
288
288
|
var FirstPerson = class {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
/** Uniform numbers in [0, 1), injected so a seed replays the same shake. */
|
|
1
2
|
export type Rng = () => number;
|
|
3
|
+
/** A point or offset in metres, y up. */
|
|
2
4
|
export interface V3 {
|
|
3
5
|
x: number;
|
|
4
6
|
y: number;
|
|
5
7
|
z: number;
|
|
6
8
|
}
|
|
9
|
+
/** The camera surface the rigs write: a three PerspectiveCamera fits, nothing is imported. */
|
|
7
10
|
export interface CameraLike {
|
|
8
11
|
position: {
|
|
9
12
|
set(x: number, y: number, z: number): unknown;
|
|
@@ -43,6 +46,7 @@ export declare class Shake {
|
|
|
43
46
|
}
|
|
44
47
|
/** Places a camera on an orbit around a target: yaw 0 behind +Z, pitch up from the ground. */
|
|
45
48
|
export declare function orbitPosition(target: V3, yaw: number, pitch: number, dist: number): V3;
|
|
49
|
+
/** Options of {@link Follow}; every field has a default. */
|
|
46
50
|
export interface FollowOptions {
|
|
47
51
|
distance?: number;
|
|
48
52
|
minDistance?: number;
|
|
@@ -72,6 +76,7 @@ export declare class Follow {
|
|
|
72
76
|
/** `heading` is the target's yaw (0 faces +Z). */
|
|
73
77
|
update(p: V3, heading: number, dt: number, snap?: boolean): void;
|
|
74
78
|
}
|
|
79
|
+
/** Options of {@link Group}; every field has a default. */
|
|
75
80
|
export interface GroupOptions {
|
|
76
81
|
center?: V3;
|
|
77
82
|
pitch?: number;
|
|
@@ -124,6 +129,7 @@ export declare class Orbit {
|
|
|
124
129
|
pan(right: number, forward: number): void;
|
|
125
130
|
apply(): void;
|
|
126
131
|
}
|
|
132
|
+
/** An object with a scale, such as a three Object3D head bone. */
|
|
127
133
|
export interface Scalable {
|
|
128
134
|
scale: {
|
|
129
135
|
x: number;
|
|
@@ -134,7 +140,7 @@ export interface Scalable {
|
|
|
134
140
|
}
|
|
135
141
|
/**
|
|
136
142
|
* A first person eye on an agent's head. `render` hides the head (scaled
|
|
137
|
-
* to 0.001,
|
|
143
|
+
* to 0.001, since visible false would hide the whole subtree)
|
|
138
144
|
* for this render only, then restores it.
|
|
139
145
|
*/
|
|
140
146
|
export declare class FirstPerson {
|
package/dist/index.js
CHANGED
|
@@ -234,7 +234,7 @@ export class Orbit {
|
|
|
234
234
|
}
|
|
235
235
|
/**
|
|
236
236
|
* A first person eye on an agent's head. `render` hides the head (scaled
|
|
237
|
-
* to 0.001,
|
|
237
|
+
* to 0.001, since visible false would hide the whole subtree)
|
|
238
238
|
* for this render only, then restores it.
|
|
239
239
|
*/
|
|
240
240
|
export class FirstPerson {
|
package/llms.md
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
# @hackbuild/cameras
|
|
2
|
+
|
|
3
|
+
> Gameplay cameras for a browser sim: follow, group framing, orbit, first person and split views, with shared damping, seeded shake and occlusion pull in.
|
|
4
|
+
|
|
5
|
+
Generated by tools/llms.mjs from the TypeDoc JSON, the README and the examples of hackbuild-agents. Human docs: https://hackbuild.github.io/hackbuild-agents/docs/cameras/
|
|
6
|
+
|
|
7
|
+
## Purpose
|
|
8
|
+
|
|
9
|
+
- Follows a moving target from behind its heading, and pulls in at once when your raycast says something blocks the view.
|
|
10
|
+
- Frames a group of points so every one stays on screen at a fixed pitch and yaw.
|
|
11
|
+
- Orbits, zooms and pans from deltas, so input binding stays with your app.
|
|
12
|
+
- Puts an eye on an agent's head and hides that head for its own render only.
|
|
13
|
+
- Shakes with seeded value noise and decaying trauma, and splits the screen into one to four views.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @hackbuild/cameras three
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The package imports nothing at run time. It writes `position.set`, `lookAt`, `fov` and `aspect` on the camera you pass, so a three `PerspectiveCamera` fits; three (`>=0.186.1 <0.188.0`) is a peer only for its types and for your scene. The quick start below uses three for the camera.
|
|
22
|
+
|
|
23
|
+
## Exports
|
|
24
|
+
|
|
25
|
+
### @hackbuild/cameras
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
interface CameraLike {
|
|
29
|
+
aspect: number;
|
|
30
|
+
fov: number;
|
|
31
|
+
position: { x: number; y: number; z: number; set(x: number, y: number, z: number): unknown };
|
|
32
|
+
lookAt(x: number, y: number, z: number): void;
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The camera surface the rigs write: a three PerspectiveCamera fits, nothing is imported.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
function damp(from: number, to: number, rate: number, dt: number): number
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Frame rate independent exponential damping toward `to` at `rate` per second.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
class FirstPerson {
|
|
46
|
+
constructor(camera: CameraLike, head?: Scalable | null, forward?: number, up?: number);
|
|
47
|
+
readonly camera: CameraLike;
|
|
48
|
+
readonly forward: number;
|
|
49
|
+
readonly head: Scalable | null;
|
|
50
|
+
readonly up: number;
|
|
51
|
+
place(head: V3, yaw: number, pitch: number): void;
|
|
52
|
+
render(draw: () => void): void;
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
A first person eye on an agent's head.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
class Follow {
|
|
60
|
+
constructor(camera: CameraLike, o?: FollowOptions);
|
|
61
|
+
readonly camera: CameraLike;
|
|
62
|
+
dist: number;
|
|
63
|
+
readonly o: Required<Omit<FollowOptions, "clear">> & Pick<FollowOptions, "clear">;
|
|
64
|
+
update(p: V3, heading: number, dt: number, snap?: boolean): void;
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Third person follow behind a moving target, pulled in when something blocks the view.
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
interface FollowOptions {
|
|
72
|
+
backoff?: number;
|
|
73
|
+
clear?: (from: V3, dir: V3, max: number) => number;
|
|
74
|
+
distance?: number;
|
|
75
|
+
lookHeight?: number;
|
|
76
|
+
minDistance?: number;
|
|
77
|
+
pitch?: number;
|
|
78
|
+
smoothing?: number;
|
|
79
|
+
yawSmoothing?: number;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Options of Follow; every field has a default.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
class Group {
|
|
87
|
+
constructor(camera: CameraLike, o?: GroupOptions);
|
|
88
|
+
readonly camera: CameraLike;
|
|
89
|
+
dist: number;
|
|
90
|
+
readonly o: Required<GroupOptions>;
|
|
91
|
+
target: V3;
|
|
92
|
+
solve(points: readonly V3[]): { dist: number; target: V3 };
|
|
93
|
+
update(points: readonly V3[], dt: number, snap?: boolean, shake?: V3): void;
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Keeps every point in view: the crownfall shared camera.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
interface GroupOptions {
|
|
101
|
+
band?: number;
|
|
102
|
+
center?: V3;
|
|
103
|
+
centerPull?: number;
|
|
104
|
+
maxDistance?: number;
|
|
105
|
+
minDistance?: number;
|
|
106
|
+
pad?: number;
|
|
107
|
+
pitch?: number;
|
|
108
|
+
yaw?: number;
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Options of Group; every field has a default.
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
class Orbit {
|
|
116
|
+
constructor(camera: CameraLike, yaw?: number, pitch?: number, dist?: number, target?: V3, limits?: { maxDist: number; maxPitch: number; minDist: number; minPitch: number });
|
|
117
|
+
readonly camera: CameraLike;
|
|
118
|
+
dist: number;
|
|
119
|
+
readonly limits: { maxDist: number; maxPitch: number; minDist: number; minPitch: number };
|
|
120
|
+
pitch: number;
|
|
121
|
+
target: V3;
|
|
122
|
+
yaw: number;
|
|
123
|
+
apply(): void;
|
|
124
|
+
pan(right: number, forward: number): void;
|
|
125
|
+
rotate(dYaw: number, dPitch: number): void;
|
|
126
|
+
zoom(factor: number): void;
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
A free orbit camera driven by deltas (drag, wheel, keys); input binding stays with the app.
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
function orbitPosition(target: V3, yaw: number, pitch: number, dist: number): V3
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Places a camera on an orbit around a target: yaw 0 behind +Z, pitch up from the ground.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
type Rng = () => number
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Uniform numbers in [0, 1), injected so a seed replays the same shake.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
interface Scalable {
|
|
146
|
+
scale: { x: number; y: number; z: number; set(x: number, y: number, z: number): unknown };
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
An object with a scale, such as a three Object3D head bone.
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
class Shake {
|
|
154
|
+
constructor(rng: Rng, frequency?: number, decay?: number);
|
|
155
|
+
readonly decay: number;
|
|
156
|
+
readonly frequency: number;
|
|
157
|
+
readonly amplitude: number;
|
|
158
|
+
kick(amplitude: number, max?: number): void;
|
|
159
|
+
step(dt: number): V3;
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
Shake: seeded value noise on three axes, amplitude decaying at `decay` per second.
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
function splitViewports(n: number, width: number, height: number): { h: number; w: number; x: number; y: number; yGl: number }[]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Viewports for 1 to 4 split views in pixels, top left origin; three wants bottom left, so `y` is flipped for it.
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
interface V3 {
|
|
173
|
+
x: number;
|
|
174
|
+
y: number;
|
|
175
|
+
z: number;
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
A point or offset in metres, y up.
|
|
180
|
+
|
|
181
|
+
## Examples
|
|
182
|
+
|
|
183
|
+
### quick start
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import { Follow, Shake } from "@hackbuild/cameras";
|
|
187
|
+
import { PerspectiveCamera } from "three";
|
|
188
|
+
|
|
189
|
+
const camera = new PerspectiveCamera(50, 16 / 9, 0.1, 200);
|
|
190
|
+
// your raycast: here a wall 2 m behind the look target, along -z
|
|
191
|
+
const clear = (from: { z: number }, dir: { z: number }, max: number) => (dir.z < 0 ? Math.min(max, 2 / -dir.z) : max);
|
|
192
|
+
const follow = new Follow(camera, { distance: 4, clear });
|
|
193
|
+
|
|
194
|
+
// shake takes a seeded stream of its own, never the sim's
|
|
195
|
+
let seed = 7;
|
|
196
|
+
const shake = new Shake(() => ((seed = (seed * 16807) % 2147483647) / 2147483647));
|
|
197
|
+
shake.kick(0.4);
|
|
198
|
+
|
|
199
|
+
const dog = { x: 0, y: 0, z: 0, yaw: 0 };
|
|
200
|
+
for (let frame = 0; frame < 60; frame++) {
|
|
201
|
+
dog.z += 0.02;
|
|
202
|
+
follow.update(dog, dog.yaw, 1 / 60);
|
|
203
|
+
camera.position.add(shake.step(1 / 60));
|
|
204
|
+
}
|
|
205
|
+
console.log(camera.position.toArray(), follow.dist);
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Mistakes to avoid
|
|
209
|
+
|
|
210
|
+
- Importing three inside code that uses this package only for the rigs: the rigs take any `CameraLike` (`position.set`, `lookAt`, `fov`, `aspect`), so a test or a non three renderer passes a plain object.
|
|
211
|
+
- Giving `Shake` the sim's rng: it draws only while shaking and per frame, so a shared stream ties the sim to the frame rate and breaks seeded replays. Give it a stream of its own, and never `Math.random`.
|
|
212
|
+
- Passing `snap = true` to `Follow.update` or `Group.update` every frame: it skips the damping. Snap on the first frame or after a teleport only.
|
|
213
|
+
- Hiding the head with `visible = false` for a first person view: that hides the whole subtree, body included. Use `FirstPerson.render(draw)`, which scales the head to 0.001 for that draw only.
|
|
214
|
+
- Passing `y` from `splitViewports` to three's `setViewport`: three counts from the bottom, so use `yGl`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hackbuild/cameras",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
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
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
26
26
|
"README.md",
|
|
27
|
+
"llms.md",
|
|
27
28
|
"LICENSE",
|
|
28
29
|
"how.ts"
|
|
29
30
|
],
|