@n3rdw1z4rd/core 0.18.4 → 0.19.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/README.md +72 -10
- package/dist/astar.d.ts +44 -0
- package/dist/astar.js +118 -0
- package/dist/canvas-renderer.d.ts +154 -0
- package/dist/canvas-renderer.js +200 -0
- package/dist/clock.d.ts +69 -0
- package/dist/clock.js +108 -0
- package/dist/color-palette.d.ts +25 -0
- package/dist/color-palette.js +112 -0
- package/dist/color.d.ts +90 -0
- package/dist/color.js +224 -0
- package/dist/css/main.css +1 -3
- package/dist/disposable.d.ts +27 -0
- package/dist/disposable.js +32 -0
- package/dist/dungeon-generator.d.ts +142 -0
- package/dist/dungeon-generator.js +288 -0
- package/dist/ecs.d.ts +87 -0
- package/dist/ecs.js +101 -0
- package/dist/emitter.d.ts +40 -0
- package/dist/emitter.js +54 -0
- package/dist/event-queue.d.ts +29 -0
- package/dist/event-queue.js +40 -0
- package/dist/index.cjs +3813 -0
- package/dist/index.d.cts +2655 -0
- package/dist/index.d.ts +35 -873
- package/dist/index.js +34 -3986
- package/dist/input/action-map.d.ts +79 -0
- package/dist/input/action-map.js +131 -0
- package/dist/input/bindings.d.ts +273 -0
- package/dist/input/bindings.js +250 -0
- package/dist/input/gestures.d.ts +108 -0
- package/dist/input/gestures.js +159 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input.d.ts +297 -0
- package/dist/input/input.js +695 -0
- package/dist/log.d.ts +8 -0
- package/dist/log.js +9 -0
- package/dist/map2d.d.ts +83 -0
- package/dist/map2d.js +120 -0
- package/dist/map3d-CHDJiq5T.d.cts +236 -0
- package/dist/map3d.d.ts +94 -0
- package/dist/map3d.js +148 -0
- package/dist/math.d.ts +176 -0
- package/dist/math.js +206 -0
- package/dist/misc.d.ts +46 -0
- package/dist/misc.js +71 -0
- package/dist/observable.d.ts +43 -0
- package/dist/observable.js +53 -0
- package/dist/particle-renderer.d.ts +47 -0
- package/dist/particle-renderer.js +82 -0
- package/dist/particle-system-2d.d.ts +60 -0
- package/dist/particle-system-2d.js +147 -0
- package/dist/random.d.ts +46 -0
- package/dist/random.js +74 -0
- package/dist/rectangle.d.ts +44 -0
- package/dist/rectangle.js +69 -0
- package/dist/rng.d.ts +72 -0
- package/dist/rng.js +152 -0
- package/dist/signal.d.ts +34 -0
- package/dist/signal.js +41 -0
- package/dist/spatial-partition-2d.d.ts +42 -0
- package/dist/spatial-partition-2d.js +52 -0
- package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
- package/dist/spatial-partition-3d.d.ts +44 -0
- package/dist/spatial-partition-3d.js +57 -0
- package/dist/three/asteroid-mesh.d.ts +41 -0
- package/dist/three/asteroid-mesh.js +44 -0
- package/dist/three/camera-rig.d.ts +140 -0
- package/dist/three/camera-rig.js +197 -0
- package/dist/three/cube-sphere-geometry.d.ts +55 -0
- package/dist/three/cube-sphere-geometry.js +239 -0
- package/dist/three/ecs.d.ts +75 -0
- package/dist/three/ecs.js +64 -0
- package/dist/three/index.d.ts +12 -0
- package/dist/three/index.js +12 -0
- package/dist/three/particle-system-3d.d.ts +53 -0
- package/dist/three/particle-system-3d.js +224 -0
- package/dist/three/renderer.d.ts +38 -0
- package/dist/three/renderer.js +57 -0
- package/dist/three/starfield.d.ts +23 -0
- package/dist/three/starfield.js +64 -0
- package/dist/three/texture-atlas.d.ts +76 -0
- package/dist/three/texture-atlas.js +84 -0
- package/dist/three/texture-utils.d.ts +31 -0
- package/dist/three/texture-utils.js +44 -0
- package/dist/three/voxel-geometry.d.ts +72 -0
- package/dist/three/voxel-geometry.js +231 -0
- package/dist/three/voxel-mesh.d.ts +67 -0
- package/dist/three/voxel-mesh.js +71 -0
- package/dist/three.cjs +1307 -0
- package/dist/three.d.cts +655 -0
- package/dist/tilemap.d.ts +17 -0
- package/dist/tilemap.js +26 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.js +0 -0
- package/dist/vector.d.ts +78 -0
- package/dist/vector.js +169 -0
- package/dist/webgl-renderer.d.ts +89 -0
- package/dist/webgl-renderer.js +140 -0
- package/dist/webgl.d.ts +357 -0
- package/dist/webgl.js +628 -0
- package/dist/worker-interface.d.ts +30 -0
- package/dist/worker-interface.js +39 -0
- package/package.json +55 -13
- package/dist/css/index.ts +0 -1
- package/dist/index.css +0 -104
- package/dist/index.d.mts +0 -873
- package/dist/index.mjs +0 -3982
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
//#region src/spatial-partition-3d.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Anything with a position that a {@link SpatialPartition3d} can bucket.
|
|
4
|
+
*
|
|
5
|
+
* @category Maps & Spatial
|
|
6
|
+
*/
|
|
7
|
+
export interface SpatialPartitionEntity3d {
|
|
8
|
+
/** in [0, 1) */
|
|
9
|
+
x: number;
|
|
10
|
+
/** in [0, 1) */
|
|
11
|
+
y: number;
|
|
12
|
+
/** in [0, 1) */
|
|
13
|
+
z: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Uniform grid over the wrapping unit cube [0, 1)³, for finding nearby entities without comparing
|
|
17
|
+
* every pair. The grid has `ceil(1 / cellSize)` cells per axis.
|
|
18
|
+
*
|
|
19
|
+
* Entities are bucketed by position when added; the partition doesn't track movement, so move an
|
|
20
|
+
* entity between cells yourself (see {@link SpatialPartition3d.getCell}) when it changes cell.
|
|
21
|
+
*
|
|
22
|
+
* @category Maps & Spatial
|
|
23
|
+
*/
|
|
24
|
+
export declare class SpatialPartition3d {
|
|
25
|
+
/** buckets, indexed `cells[cellZ][cellY][cellX]` */
|
|
26
|
+
cells: SpatialPartitionEntity3d[][][][];
|
|
27
|
+
/** cell edge length in unit-cube units, e.g. 0.2 for a 5 x 5 x 5 grid */
|
|
28
|
+
cellSize: number;
|
|
29
|
+
/** the array passed to the constructor; `addEntity()` doesn't add to it */
|
|
30
|
+
entities: SpatialPartitionEntity3d[];
|
|
31
|
+
/** Builds the grid and adds every entity in `entities`. */
|
|
32
|
+
constructor(cellSize: number, entities: SpatialPartitionEntity3d[]);
|
|
33
|
+
/** positions are expected in [0, 1); anything outside wraps around, matching getCellNeighbors */
|
|
34
|
+
getCell(x: number, y: number, z: number): SpatialPartitionEntity3d[];
|
|
35
|
+
/**
|
|
36
|
+
* The 3 x 3 x 3 block of cells around cell (`cx`, `cy`, `cz`), including that cell, wrapping at
|
|
37
|
+
* the edges. Takes cell indices, not positions. Each cell appears once, so with fewer than 3
|
|
38
|
+
* cells per axis the block has fewer than 27 cells.
|
|
39
|
+
*/
|
|
40
|
+
getCellNeighbors(cx: number, cy: number, cz: number): SpatialPartitionEntity3d[][];
|
|
41
|
+
/** Puts `entity` in the cell its current position falls in. */
|
|
42
|
+
addEntity(entity: SpatialPartitionEntity3d): void;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { mod } from "./math.js";
|
|
2
|
+
//#region src/spatial-partition-3d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Uniform grid over the wrapping unit cube [0, 1)³, for finding nearby entities without comparing
|
|
5
|
+
* every pair. The grid has `ceil(1 / cellSize)` cells per axis.
|
|
6
|
+
*
|
|
7
|
+
* Entities are bucketed by position when added; the partition doesn't track movement, so move an
|
|
8
|
+
* entity between cells yourself (see {@link SpatialPartition3d.getCell}) when it changes cell.
|
|
9
|
+
*
|
|
10
|
+
* @category Maps & Spatial
|
|
11
|
+
*/
|
|
12
|
+
var SpatialPartition3d = class {
|
|
13
|
+
/** buckets, indexed `cells[cellZ][cellY][cellX]` */
|
|
14
|
+
cells;
|
|
15
|
+
/** cell edge length in unit-cube units, e.g. 0.2 for a 5 x 5 x 5 grid */
|
|
16
|
+
cellSize;
|
|
17
|
+
/** the array passed to the constructor; `addEntity()` doesn't add to it */
|
|
18
|
+
entities;
|
|
19
|
+
/** Builds the grid and adds every entity in `entities`. */
|
|
20
|
+
constructor(cellSize, entities) {
|
|
21
|
+
this.cellSize = cellSize;
|
|
22
|
+
this.entities = entities;
|
|
23
|
+
const gridSize = Math.ceil(1 / cellSize);
|
|
24
|
+
this.cells = Array.from({ length: gridSize }, () => Array.from({ length: gridSize }, () => Array.from({ length: gridSize }, () => [])));
|
|
25
|
+
for (const entity of this.entities) this.addEntity(entity);
|
|
26
|
+
}
|
|
27
|
+
/** positions are expected in [0, 1); anything outside wraps around, matching getCellNeighbors */
|
|
28
|
+
getCell(x, y, z) {
|
|
29
|
+
const gridSize = this.cells.length;
|
|
30
|
+
const cx = mod(Math.floor(x / this.cellSize), gridSize);
|
|
31
|
+
const cy = mod(Math.floor(y / this.cellSize), gridSize);
|
|
32
|
+
const cz = mod(Math.floor(z / this.cellSize), gridSize);
|
|
33
|
+
return this.cells[cz][cy][cx];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The 3 x 3 x 3 block of cells around cell (`cx`, `cy`, `cz`), including that cell, wrapping at
|
|
37
|
+
* the edges. Takes cell indices, not positions. Each cell appears once, so with fewer than 3
|
|
38
|
+
* cells per axis the block has fewer than 27 cells.
|
|
39
|
+
*/
|
|
40
|
+
getCellNeighbors(cx, cy, cz) {
|
|
41
|
+
const neighbors = /* @__PURE__ */ new Set();
|
|
42
|
+
const gridSize = this.cells.length;
|
|
43
|
+
for (let dz = -1; dz <= 1; dz++) for (let dy = -1; dy <= 1; dy++) for (let dx = -1; dx <= 1; dx++) {
|
|
44
|
+
const nx = mod(cx + dx, gridSize);
|
|
45
|
+
const ny = mod(cy + dy, gridSize);
|
|
46
|
+
const nz = mod(cz + dz, gridSize);
|
|
47
|
+
neighbors.add(this.cells[nz][ny][nx]);
|
|
48
|
+
}
|
|
49
|
+
return [...neighbors];
|
|
50
|
+
}
|
|
51
|
+
/** Puts `entity` in the cell its current position falls in. */
|
|
52
|
+
addEntity(entity) {
|
|
53
|
+
this.getCell(entity.x, entity.y, entity.z).push(entity);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
//#endregion
|
|
57
|
+
export { SpatialPartition3d };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { NoiseFunction3D } from "../types.js";
|
|
2
|
+
import { Mesh } from "three";
|
|
3
|
+
//#region src/three/asteroid-mesh.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Options for the {@link AsteroidMesh} constructor.
|
|
6
|
+
*
|
|
7
|
+
* @category Geometry & Meshes
|
|
8
|
+
*/
|
|
9
|
+
export interface AsteroidOptions {
|
|
10
|
+
/** shapes the surface; expected to return roughly -1..1 */
|
|
11
|
+
noise3d: NoiseFunction3D;
|
|
12
|
+
/** base radius before displacement, default 1 */
|
|
13
|
+
radius?: number;
|
|
14
|
+
/** IcosahedronGeometry subdivision level, default 1 */
|
|
15
|
+
detail?: number;
|
|
16
|
+
/** noise frequency: vertex positions are multiplied by this before sampling, default 1 */
|
|
17
|
+
noiseScale?: number;
|
|
18
|
+
/** each vertex moves by `noise * displacement` times its distance from the center, default 0.1 */
|
|
19
|
+
displacement?: number;
|
|
20
|
+
/** optional extra detail layer, sampled at twice the frequency and half the weight */
|
|
21
|
+
secondaryNoise?: NoiseFunction3D;
|
|
22
|
+
/** 0xRRGGBB, default 0x888888 */
|
|
23
|
+
color?: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Lumpy rock: an icosphere whose vertices are pushed in and out by 3D noise, with a
|
|
27
|
+
* `MeshStandardMaterial`. The geometry is non-indexed, so normals are per face (a faceted look).
|
|
28
|
+
* Deterministic for a deterministic noise function.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { createNoise3D } from 'simplex-noise';
|
|
32
|
+
*
|
|
33
|
+
* const rock = new AsteroidMesh({ noise3d: createNoise3D(() => rng.nextFloat()), radius: 2, detail: 3, displacement: 0.25 });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @category Geometry & Meshes
|
|
37
|
+
*/
|
|
38
|
+
export declare class AsteroidMesh extends Mesh {
|
|
39
|
+
constructor(options: AsteroidOptions);
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { IcosahedronGeometry, Mesh, MeshStandardMaterial } from "three";
|
|
2
|
+
//#region src/three/asteroid-mesh.ts
|
|
3
|
+
/**
|
|
4
|
+
* Lumpy rock: an icosphere whose vertices are pushed in and out by 3D noise, with a
|
|
5
|
+
* `MeshStandardMaterial`. The geometry is non-indexed, so normals are per face (a faceted look).
|
|
6
|
+
* Deterministic for a deterministic noise function.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* import { createNoise3D } from 'simplex-noise';
|
|
10
|
+
*
|
|
11
|
+
* const rock = new AsteroidMesh({ noise3d: createNoise3D(() => rng.nextFloat()), radius: 2, detail: 3, displacement: 0.25 });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @category Geometry & Meshes
|
|
15
|
+
*/
|
|
16
|
+
var AsteroidMesh = class extends Mesh {
|
|
17
|
+
constructor(options) {
|
|
18
|
+
const radius = options.radius ?? 1;
|
|
19
|
+
const detail = options.detail ?? 1;
|
|
20
|
+
const noiseScale = options.noiseScale ?? 1;
|
|
21
|
+
const displacement = options.displacement ?? .1;
|
|
22
|
+
const noise3d = options.noise3d;
|
|
23
|
+
const noise3d2 = options.secondaryNoise;
|
|
24
|
+
const color = options.color ?? 8947848;
|
|
25
|
+
const geometry = new IcosahedronGeometry(radius, detail);
|
|
26
|
+
const material = new MeshStandardMaterial({ color });
|
|
27
|
+
super(geometry, material);
|
|
28
|
+
const position = this.geometry.attributes.position;
|
|
29
|
+
const vertices = position.array;
|
|
30
|
+
for (let i = 0; i < vertices.length; i += 3) {
|
|
31
|
+
const x = vertices[i];
|
|
32
|
+
const y = vertices[i + 1];
|
|
33
|
+
const z = vertices[i + 2];
|
|
34
|
+
const displacementValue = 1 + (noise3d(x * noiseScale, y * noiseScale, z * noiseScale) + (noise3d2 ? .5 * noise3d2(x * noiseScale * 2, y * noiseScale * 2, z * noiseScale * 2) : 0)) * displacement;
|
|
35
|
+
vertices[i] = x * displacementValue;
|
|
36
|
+
vertices[i + 1] = y * displacementValue;
|
|
37
|
+
vertices[i + 2] = z * displacementValue;
|
|
38
|
+
}
|
|
39
|
+
position.needsUpdate = true;
|
|
40
|
+
this.geometry.computeVertexNormals();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
export { AsteroidMesh };
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { Group, Object3D, PerspectiveCamera, Vector3 } from "three";
|
|
2
|
+
//#region src/three/camera-rig.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Options for the {@link CameraRig} constructor.
|
|
5
|
+
*
|
|
6
|
+
* @category Camera
|
|
7
|
+
*/
|
|
8
|
+
export interface CameraRigParams {
|
|
9
|
+
/** camera to use; default: a new PerspectiveCamera built from `fov`, `aspect`, `near`, `far` */
|
|
10
|
+
camera?: PerspectiveCamera;
|
|
11
|
+
/** degrees, vertical; default 70 (ignored when `camera` is given) */
|
|
12
|
+
fov?: number;
|
|
13
|
+
/** default 2 (ignored when `camera` is given) */
|
|
14
|
+
aspect?: number;
|
|
15
|
+
/** default 0.1 (ignored when `camera` is given) */
|
|
16
|
+
near?: number;
|
|
17
|
+
/** default 1000 (ignored when `camera` is given) */
|
|
18
|
+
far?: number;
|
|
19
|
+
/** object to follow; see {@link CameraRig.target} */
|
|
20
|
+
target?: Object3D;
|
|
21
|
+
/** starting zoom distance, default 5; clamped like {@link CameraRig.distance} */
|
|
22
|
+
distance?: number;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Pitch range for a {@link CameraRig}.
|
|
26
|
+
*
|
|
27
|
+
* @category Camera
|
|
28
|
+
*/
|
|
29
|
+
export interface PitchLimits {
|
|
30
|
+
/** radians; negative looks down */
|
|
31
|
+
min: number;
|
|
32
|
+
/** radians; positive looks up */
|
|
33
|
+
max: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Third-person camera that orbits a target and zooms seamlessly into first person.
|
|
37
|
+
*
|
|
38
|
+
* The orbit pivot sits at the target's eye height (`targetOffset`); the camera sits `distance`
|
|
39
|
+
* behind it. Zooming below `firstPersonThreshold` goes to distance 0, which puts the camera at the
|
|
40
|
+
* eyes: first person, with the same yaw and pitch.
|
|
41
|
+
*
|
|
42
|
+
* Add the rig to the scene (not to the target), feed it input with `orbit()` / `dolly()`, and call
|
|
43
|
+
* `update(deltaTime)` once per frame.
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* const rig = new CameraRig({ target: player, distance: 6 });
|
|
47
|
+
* scene.add(rig);
|
|
48
|
+
*
|
|
49
|
+
* addEventListener('mousemove', (e) => rig.orbit(e.movementX, e.movementY));
|
|
50
|
+
* addEventListener('wheel', (e) => rig.dolly(e.deltaY));
|
|
51
|
+
*
|
|
52
|
+
* // each frame
|
|
53
|
+
* rig.update(deltaTime);
|
|
54
|
+
* renderer.render(scene, rig.camera);
|
|
55
|
+
* ```
|
|
56
|
+
*
|
|
57
|
+
* @category Camera
|
|
58
|
+
*/
|
|
59
|
+
export declare class CameraRig extends Group {
|
|
60
|
+
/** the camera to render with; a child of `gimbal` */
|
|
61
|
+
readonly camera: PerspectiveCamera;
|
|
62
|
+
/** pitch */
|
|
63
|
+
readonly gimbal: Group<import("three").Object3DEventMap>;
|
|
64
|
+
/** followed by position, not parented */
|
|
65
|
+
target?: Object3D;
|
|
66
|
+
/** pivot (eye height) above the target's origin, world space */
|
|
67
|
+
targetOffset: Vector3;
|
|
68
|
+
/** radians, around world up */
|
|
69
|
+
yaw: number;
|
|
70
|
+
/** radians, negative looks down */
|
|
71
|
+
pitch: number;
|
|
72
|
+
/** > 0 disables first person */
|
|
73
|
+
minDistance: number;
|
|
74
|
+
/** furthest zoom distance */
|
|
75
|
+
maxDistance: number;
|
|
76
|
+
/** zooming in below this goes all the way to first person */
|
|
77
|
+
firstPersonThreshold: number;
|
|
78
|
+
/** pitch range in third person; default -80° to 30° */
|
|
79
|
+
pitchLimits: PitchLimits;
|
|
80
|
+
/** pitch range in first person; the range blends between the two while zooming */
|
|
81
|
+
firstPersonPitchLimits: PitchLimits;
|
|
82
|
+
/** sideways camera shift in third person (over-the-shoulder) */
|
|
83
|
+
shoulderOffset: number;
|
|
84
|
+
/** the shift fades out as distance goes from this to 0 */
|
|
85
|
+
shoulderFadeDistance: number;
|
|
86
|
+
/** set `target.visible = false` while in first person */
|
|
87
|
+
hideTargetInFirstPerson: boolean;
|
|
88
|
+
/** the camera pulls in rather than passing through these */
|
|
89
|
+
collisionObjects: Object3D[];
|
|
90
|
+
/** distance kept between the camera and a surface */
|
|
91
|
+
collisionPadding: number;
|
|
92
|
+
/** radians per pixel */
|
|
93
|
+
orbitSensitivity: number;
|
|
94
|
+
/** per wheel delta unit (multiplicative zoom) */
|
|
95
|
+
zoomSensitivity: number;
|
|
96
|
+
/** higher = snappier; 0 = instant */
|
|
97
|
+
zoomDamping: number;
|
|
98
|
+
/** 0 = locked to the target */
|
|
99
|
+
followDamping: number;
|
|
100
|
+
/** called from `update()` when the camera switches between first and third person */
|
|
101
|
+
onModeChange?: (firstPerson: boolean) => void;
|
|
102
|
+
private _targetDistance;
|
|
103
|
+
private _distance;
|
|
104
|
+
private _cameraDistance;
|
|
105
|
+
private _recovering;
|
|
106
|
+
private _firstPerson;
|
|
107
|
+
private _initialized;
|
|
108
|
+
private readonly _raycaster;
|
|
109
|
+
private readonly _pivot;
|
|
110
|
+
private readonly _desired;
|
|
111
|
+
private readonly _direction;
|
|
112
|
+
/** Zoom distance the rig is easing towards; 0 = first person. */
|
|
113
|
+
get distance(): number;
|
|
114
|
+
/**
|
|
115
|
+
* Jumps to a zoom distance without easing, clamped to [`minDistance`, `maxDistance`]; below
|
|
116
|
+
* `firstPersonThreshold` becomes 0 (first person) unless `minDistance > 0`.
|
|
117
|
+
*/
|
|
118
|
+
set distance(value: number);
|
|
119
|
+
/** Current distance of the camera from the pivot, after smoothing and collision. */
|
|
120
|
+
get cameraDistance(): number;
|
|
121
|
+
/** whether the camera is currently closer than `firstPersonThreshold` (as of the last `update()`) */
|
|
122
|
+
get isFirstPerson(): boolean;
|
|
123
|
+
constructor(params?: CameraRigParams);
|
|
124
|
+
/** Rotate from pointer movement in pixels (e.g. movementX / movementY). */
|
|
125
|
+
orbit(deltaX: number, deltaY: number): void;
|
|
126
|
+
/** Zoom from wheel movement (e.g. WheelEvent.deltaY); positive zooms out. */
|
|
127
|
+
dolly(delta: number): void;
|
|
128
|
+
/**
|
|
129
|
+
* Follows the target, eases zoom, applies yaw / pitch and collision, and positions the camera.
|
|
130
|
+
* Call once per frame.
|
|
131
|
+
*
|
|
132
|
+
* @param deltaTime seconds since the last update
|
|
133
|
+
*/
|
|
134
|
+
update(deltaTime: number): void;
|
|
135
|
+
private _collide;
|
|
136
|
+
private _clampDistance;
|
|
137
|
+
private _clampPitch;
|
|
138
|
+
private _setFirstPerson;
|
|
139
|
+
}
|
|
140
|
+
//#endregion
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { clamp, lerp, radians, saturate } from "../math.js";
|
|
2
|
+
import { Group, PerspectiveCamera, Raycaster, Vector3 } from "three";
|
|
3
|
+
//#region src/three/camera-rig.ts
|
|
4
|
+
const DEFAULT_DISTANCE = 5;
|
|
5
|
+
const damp = (current, target, damping, deltaTime) => damping > 0 ? lerp(current, target, 1 - Math.exp(-damping * deltaTime)) : target;
|
|
6
|
+
/**
|
|
7
|
+
* Third-person camera that orbits a target and zooms seamlessly into first person.
|
|
8
|
+
*
|
|
9
|
+
* The orbit pivot sits at the target's eye height (`targetOffset`); the camera sits `distance`
|
|
10
|
+
* behind it. Zooming below `firstPersonThreshold` goes to distance 0, which puts the camera at the
|
|
11
|
+
* eyes: first person, with the same yaw and pitch.
|
|
12
|
+
*
|
|
13
|
+
* Add the rig to the scene (not to the target), feed it input with `orbit()` / `dolly()`, and call
|
|
14
|
+
* `update(deltaTime)` once per frame.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const rig = new CameraRig({ target: player, distance: 6 });
|
|
18
|
+
* scene.add(rig);
|
|
19
|
+
*
|
|
20
|
+
* addEventListener('mousemove', (e) => rig.orbit(e.movementX, e.movementY));
|
|
21
|
+
* addEventListener('wheel', (e) => rig.dolly(e.deltaY));
|
|
22
|
+
*
|
|
23
|
+
* // each frame
|
|
24
|
+
* rig.update(deltaTime);
|
|
25
|
+
* renderer.render(scene, rig.camera);
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @category Camera
|
|
29
|
+
*/
|
|
30
|
+
var CameraRig = class extends Group {
|
|
31
|
+
/** the camera to render with; a child of `gimbal` */
|
|
32
|
+
camera;
|
|
33
|
+
/** pitch */
|
|
34
|
+
gimbal = new Group();
|
|
35
|
+
/** followed by position, not parented */
|
|
36
|
+
target;
|
|
37
|
+
/** pivot (eye height) above the target's origin, world space */
|
|
38
|
+
targetOffset = new Vector3(0, 1.6, 0);
|
|
39
|
+
/** radians, around world up */
|
|
40
|
+
yaw = 0;
|
|
41
|
+
/** radians, negative looks down */
|
|
42
|
+
pitch = 0;
|
|
43
|
+
/** > 0 disables first person */
|
|
44
|
+
minDistance = 0;
|
|
45
|
+
/** furthest zoom distance */
|
|
46
|
+
maxDistance = 20;
|
|
47
|
+
/** zooming in below this goes all the way to first person */
|
|
48
|
+
firstPersonThreshold = .5;
|
|
49
|
+
/** pitch range in third person; default -80° to 30° */
|
|
50
|
+
pitchLimits = {
|
|
51
|
+
min: radians(-80),
|
|
52
|
+
max: radians(30)
|
|
53
|
+
};
|
|
54
|
+
/** pitch range in first person; the range blends between the two while zooming */
|
|
55
|
+
firstPersonPitchLimits = {
|
|
56
|
+
min: radians(-89),
|
|
57
|
+
max: radians(89)
|
|
58
|
+
};
|
|
59
|
+
/** sideways camera shift in third person (over-the-shoulder) */
|
|
60
|
+
shoulderOffset = 0;
|
|
61
|
+
/** the shift fades out as distance goes from this to 0 */
|
|
62
|
+
shoulderFadeDistance = 2;
|
|
63
|
+
/** set `target.visible = false` while in first person */
|
|
64
|
+
hideTargetInFirstPerson = true;
|
|
65
|
+
/** the camera pulls in rather than passing through these */
|
|
66
|
+
collisionObjects = [];
|
|
67
|
+
/** distance kept between the camera and a surface */
|
|
68
|
+
collisionPadding = .2;
|
|
69
|
+
/** radians per pixel */
|
|
70
|
+
orbitSensitivity = .005;
|
|
71
|
+
/** per wheel delta unit (multiplicative zoom) */
|
|
72
|
+
zoomSensitivity = .001;
|
|
73
|
+
/** higher = snappier; 0 = instant */
|
|
74
|
+
zoomDamping = 12;
|
|
75
|
+
/** 0 = locked to the target */
|
|
76
|
+
followDamping = 0;
|
|
77
|
+
/** called from `update()` when the camera switches between first and third person */
|
|
78
|
+
onModeChange;
|
|
79
|
+
_targetDistance;
|
|
80
|
+
_distance;
|
|
81
|
+
_cameraDistance;
|
|
82
|
+
_recovering = false;
|
|
83
|
+
_firstPerson = false;
|
|
84
|
+
_initialized = false;
|
|
85
|
+
_raycaster = new Raycaster();
|
|
86
|
+
_pivot = new Vector3();
|
|
87
|
+
_desired = new Vector3();
|
|
88
|
+
_direction = new Vector3();
|
|
89
|
+
/** Zoom distance the rig is easing towards; 0 = first person. */
|
|
90
|
+
get distance() {
|
|
91
|
+
return this._targetDistance;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Jumps to a zoom distance without easing, clamped to [`minDistance`, `maxDistance`]; below
|
|
95
|
+
* `firstPersonThreshold` becomes 0 (first person) unless `minDistance > 0`.
|
|
96
|
+
*/
|
|
97
|
+
set distance(value) {
|
|
98
|
+
this._targetDistance = this._distance = this._cameraDistance = this._clampDistance(value);
|
|
99
|
+
}
|
|
100
|
+
/** Current distance of the camera from the pivot, after smoothing and collision. */
|
|
101
|
+
get cameraDistance() {
|
|
102
|
+
return this._cameraDistance;
|
|
103
|
+
}
|
|
104
|
+
/** whether the camera is currently closer than `firstPersonThreshold` (as of the last `update()`) */
|
|
105
|
+
get isFirstPerson() {
|
|
106
|
+
return this._firstPerson;
|
|
107
|
+
}
|
|
108
|
+
constructor(params = {}) {
|
|
109
|
+
super();
|
|
110
|
+
this.camera = params.camera ?? new PerspectiveCamera(params.fov ?? 70, params.aspect ?? 2, params.near ?? .1, params.far ?? 1e3);
|
|
111
|
+
this.add(this.gimbal);
|
|
112
|
+
this.gimbal.add(this.camera);
|
|
113
|
+
this.target = params.target;
|
|
114
|
+
const distance = this._clampDistance(params.distance ?? DEFAULT_DISTANCE);
|
|
115
|
+
this._targetDistance = this._distance = this._cameraDistance = distance;
|
|
116
|
+
}
|
|
117
|
+
/** Rotate from pointer movement in pixels (e.g. movementX / movementY). */
|
|
118
|
+
orbit(deltaX, deltaY) {
|
|
119
|
+
this.yaw -= deltaX * this.orbitSensitivity;
|
|
120
|
+
this.pitch -= deltaY * this.orbitSensitivity;
|
|
121
|
+
this._clampPitch();
|
|
122
|
+
}
|
|
123
|
+
/** Zoom from wheel movement (e.g. WheelEvent.deltaY); positive zooms out. */
|
|
124
|
+
dolly(delta) {
|
|
125
|
+
if (this._targetDistance === 0 && delta > 0) this._targetDistance = this.firstPersonThreshold;
|
|
126
|
+
else this._targetDistance *= Math.exp(delta * this.zoomSensitivity);
|
|
127
|
+
this._targetDistance = this._clampDistance(this._targetDistance);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Follows the target, eases zoom, applies yaw / pitch and collision, and positions the camera.
|
|
131
|
+
* Call once per frame.
|
|
132
|
+
*
|
|
133
|
+
* @param deltaTime seconds since the last update
|
|
134
|
+
*/
|
|
135
|
+
update(deltaTime) {
|
|
136
|
+
if (this.target) {
|
|
137
|
+
this.target.getWorldPosition(this._pivot).add(this.targetOffset);
|
|
138
|
+
if (this.parent) this.parent.worldToLocal(this._pivot);
|
|
139
|
+
if (!this._initialized || this.followDamping <= 0) this.position.copy(this._pivot);
|
|
140
|
+
else {
|
|
141
|
+
const t = 1 - Math.exp(-this.followDamping * deltaTime);
|
|
142
|
+
this.position.lerp(this._pivot, t);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
this._initialized = true;
|
|
146
|
+
this._targetDistance = this._clampDistance(this._targetDistance);
|
|
147
|
+
this._distance = damp(this._distance, this._targetDistance, this.zoomDamping, deltaTime);
|
|
148
|
+
if (Math.abs(this._distance - this._targetDistance) < 1e-4) this._distance = this._targetDistance;
|
|
149
|
+
this._clampPitch();
|
|
150
|
+
this.rotation.set(0, this.yaw, 0);
|
|
151
|
+
this.gimbal.rotation.set(this.pitch, 0, 0);
|
|
152
|
+
const shoulder = this.shoulderOffset * saturate(this._distance / this.shoulderFadeDistance);
|
|
153
|
+
this._cameraDistance = this._collide(shoulder, this._distance, deltaTime);
|
|
154
|
+
const scale = this._distance > 0 ? this._cameraDistance / this._distance : 0;
|
|
155
|
+
this.camera.position.set(shoulder * scale, 0, this._cameraDistance);
|
|
156
|
+
this._setFirstPerson(this._cameraDistance < this.firstPersonThreshold);
|
|
157
|
+
}
|
|
158
|
+
_collide(shoulder, distance, deltaTime) {
|
|
159
|
+
let allowed = distance;
|
|
160
|
+
if (this.collisionObjects.length > 0 && distance > 0) {
|
|
161
|
+
this.updateMatrixWorld(true);
|
|
162
|
+
const pivot = this.gimbal.getWorldPosition(this._pivot);
|
|
163
|
+
const desired = this.gimbal.localToWorld(this._desired.set(shoulder, 0, distance));
|
|
164
|
+
const length = this._direction.subVectors(desired, pivot).length();
|
|
165
|
+
this._raycaster.set(pivot, this._direction.normalize());
|
|
166
|
+
this._raycaster.far = length;
|
|
167
|
+
const hit = this._raycaster.intersectObjects(this.collisionObjects, true)[0];
|
|
168
|
+
if (hit) allowed = Math.max(0, (hit.distance - this.collisionPadding) / length * distance);
|
|
169
|
+
}
|
|
170
|
+
if (allowed < distance) {
|
|
171
|
+
this._recovering = true;
|
|
172
|
+
return allowed < this._cameraDistance ? allowed : damp(this._cameraDistance, allowed, this.zoomDamping, deltaTime);
|
|
173
|
+
}
|
|
174
|
+
if (this._recovering) {
|
|
175
|
+
const eased = damp(this._cameraDistance, distance, this.zoomDamping, deltaTime);
|
|
176
|
+
if (Math.abs(eased - distance) > .001) return eased;
|
|
177
|
+
this._recovering = false;
|
|
178
|
+
}
|
|
179
|
+
return distance;
|
|
180
|
+
}
|
|
181
|
+
_clampDistance(distance) {
|
|
182
|
+
distance = clamp(distance, this.minDistance, this.maxDistance);
|
|
183
|
+
return this.minDistance <= 0 && distance < this.firstPersonThreshold ? 0 : distance;
|
|
184
|
+
}
|
|
185
|
+
_clampPitch() {
|
|
186
|
+
const t = saturate(this._distance / Math.max(this.firstPersonThreshold, 1e-6));
|
|
187
|
+
this.pitch = clamp(this.pitch, lerp(this.firstPersonPitchLimits.min, this.pitchLimits.min, t), lerp(this.firstPersonPitchLimits.max, this.pitchLimits.max, t));
|
|
188
|
+
}
|
|
189
|
+
_setFirstPerson(firstPerson) {
|
|
190
|
+
if (firstPerson === this._firstPerson) return;
|
|
191
|
+
this._firstPerson = firstPerson;
|
|
192
|
+
if (this.hideTargetInFirstPerson && this.target) this.target.visible = !firstPerson;
|
|
193
|
+
this.onModeChange?.(firstPerson);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
//#endregion
|
|
197
|
+
export { CameraRig };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { NoiseFunction3D } from "../types.js";
|
|
2
|
+
import { BufferGeometry } from "three";
|
|
3
|
+
//#region src/three/cube-sphere-geometry.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* A sphere built from a subdivided cube, for planets and other displaced spheres.
|
|
6
|
+
*
|
|
7
|
+
* Vertices on the cube's edges and corners are shared between faces, so there are no seams:
|
|
8
|
+
* displacement and computed normals are continuous across the whole surface. Points are
|
|
9
|
+
* mapped from cube to sphere with the "spherified cube" mapping, which spaces vertices far
|
|
10
|
+
* more evenly than normalizing (no bunching at the face corners).
|
|
11
|
+
*
|
|
12
|
+
* The geometry has positions, normals and an index; it has no UVs, since one set of UVs can't
|
|
13
|
+
* wrap a sphere without seams (use triplanar mapping or vertex colors for surfaces).
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const planet = new CubeSphereGeometry(10, 64) // radius 5
|
|
17
|
+
* .applyFractalBrownianMotion(noise3d, 5, 0.5, 0.3, 0.4); // up to ±0.4 height
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @category Geometry & Meshes
|
|
21
|
+
*/
|
|
22
|
+
export declare class CubeSphereGeometry extends BufferGeometry {
|
|
23
|
+
readonly size: number;
|
|
24
|
+
readonly segments: number;
|
|
25
|
+
readonly type: string;
|
|
26
|
+
/** `size / 2`: the undisplaced radius */
|
|
27
|
+
readonly radius: number;
|
|
28
|
+
private readonly _directions;
|
|
29
|
+
/**
|
|
30
|
+
* @param size diameter of the sphere
|
|
31
|
+
* @param segments subdivisions along each cube edge
|
|
32
|
+
*/
|
|
33
|
+
constructor(size: number, segments: number);
|
|
34
|
+
/**
|
|
35
|
+
* Sets each vertex to `radius + height(direction)` along its direction from the center,
|
|
36
|
+
* then recomputes normals and bounds. Always starts from the undisplaced sphere, so calling
|
|
37
|
+
* it again replaces the previous displacement rather than adding to it.
|
|
38
|
+
*
|
|
39
|
+
* @param height offset in world units for the unit direction (x, y, z)
|
|
40
|
+
*/
|
|
41
|
+
displace(height: (x: number, y: number, z: number) => number): this;
|
|
42
|
+
/**
|
|
43
|
+
* Displaces the surface with fractal Brownian motion. Noise is sampled at the undisplaced
|
|
44
|
+
* surface point times `scale`; the summed octaves are normalized to the noise's range and
|
|
45
|
+
* multiplied by `amplitude`.
|
|
46
|
+
*/
|
|
47
|
+
applyFractalBrownianMotion(noise3d: NoiseFunction3D, octaves: number, persistence: number, scale: number, amplitude: number): this;
|
|
48
|
+
private static fBm;
|
|
49
|
+
/**
|
|
50
|
+
* maps a point on the [-1, 1] cube onto the unit sphere with even spacing
|
|
51
|
+
* (https://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html)
|
|
52
|
+
*/
|
|
53
|
+
static spherify(x: number, y: number, z: number): [number, number, number];
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|