@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,239 @@
|
|
|
1
|
+
import { BufferAttribute, BufferGeometry } from "three";
|
|
2
|
+
//#region src/three/cube-sphere-geometry.ts
|
|
3
|
+
const FACES = [
|
|
4
|
+
{
|
|
5
|
+
n: [
|
|
6
|
+
1,
|
|
7
|
+
0,
|
|
8
|
+
0
|
|
9
|
+
],
|
|
10
|
+
u: [
|
|
11
|
+
0,
|
|
12
|
+
0,
|
|
13
|
+
-1
|
|
14
|
+
],
|
|
15
|
+
v: [
|
|
16
|
+
0,
|
|
17
|
+
1,
|
|
18
|
+
0
|
|
19
|
+
]
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
n: [
|
|
23
|
+
-1,
|
|
24
|
+
0,
|
|
25
|
+
0
|
|
26
|
+
],
|
|
27
|
+
u: [
|
|
28
|
+
0,
|
|
29
|
+
0,
|
|
30
|
+
1
|
|
31
|
+
],
|
|
32
|
+
v: [
|
|
33
|
+
0,
|
|
34
|
+
1,
|
|
35
|
+
0
|
|
36
|
+
]
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
n: [
|
|
40
|
+
0,
|
|
41
|
+
1,
|
|
42
|
+
0
|
|
43
|
+
],
|
|
44
|
+
u: [
|
|
45
|
+
1,
|
|
46
|
+
0,
|
|
47
|
+
0
|
|
48
|
+
],
|
|
49
|
+
v: [
|
|
50
|
+
0,
|
|
51
|
+
0,
|
|
52
|
+
-1
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
n: [
|
|
57
|
+
0,
|
|
58
|
+
-1,
|
|
59
|
+
0
|
|
60
|
+
],
|
|
61
|
+
u: [
|
|
62
|
+
1,
|
|
63
|
+
0,
|
|
64
|
+
0
|
|
65
|
+
],
|
|
66
|
+
v: [
|
|
67
|
+
0,
|
|
68
|
+
0,
|
|
69
|
+
1
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
n: [
|
|
74
|
+
0,
|
|
75
|
+
0,
|
|
76
|
+
1
|
|
77
|
+
],
|
|
78
|
+
u: [
|
|
79
|
+
1,
|
|
80
|
+
0,
|
|
81
|
+
0
|
|
82
|
+
],
|
|
83
|
+
v: [
|
|
84
|
+
0,
|
|
85
|
+
1,
|
|
86
|
+
0
|
|
87
|
+
]
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
n: [
|
|
91
|
+
0,
|
|
92
|
+
0,
|
|
93
|
+
-1
|
|
94
|
+
],
|
|
95
|
+
u: [
|
|
96
|
+
-1,
|
|
97
|
+
0,
|
|
98
|
+
0
|
|
99
|
+
],
|
|
100
|
+
v: [
|
|
101
|
+
0,
|
|
102
|
+
1,
|
|
103
|
+
0
|
|
104
|
+
]
|
|
105
|
+
}
|
|
106
|
+
];
|
|
107
|
+
/**
|
|
108
|
+
* A sphere built from a subdivided cube, for planets and other displaced spheres.
|
|
109
|
+
*
|
|
110
|
+
* Vertices on the cube's edges and corners are shared between faces, so there are no seams:
|
|
111
|
+
* displacement and computed normals are continuous across the whole surface. Points are
|
|
112
|
+
* mapped from cube to sphere with the "spherified cube" mapping, which spaces vertices far
|
|
113
|
+
* more evenly than normalizing (no bunching at the face corners).
|
|
114
|
+
*
|
|
115
|
+
* The geometry has positions, normals and an index; it has no UVs, since one set of UVs can't
|
|
116
|
+
* wrap a sphere without seams (use triplanar mapping or vertex colors for surfaces).
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
* const planet = new CubeSphereGeometry(10, 64) // radius 5
|
|
120
|
+
* .applyFractalBrownianMotion(noise3d, 5, 0.5, 0.3, 0.4); // up to ±0.4 height
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @category Geometry & Meshes
|
|
124
|
+
*/
|
|
125
|
+
var CubeSphereGeometry = class CubeSphereGeometry extends BufferGeometry {
|
|
126
|
+
size;
|
|
127
|
+
segments;
|
|
128
|
+
type = "CubeSphereGeometry";
|
|
129
|
+
/** `size / 2`: the undisplaced radius */
|
|
130
|
+
radius;
|
|
131
|
+
_directions;
|
|
132
|
+
/**
|
|
133
|
+
* @param size diameter of the sphere
|
|
134
|
+
* @param segments subdivisions along each cube edge
|
|
135
|
+
*/
|
|
136
|
+
constructor(size, segments) {
|
|
137
|
+
super();
|
|
138
|
+
this.size = size;
|
|
139
|
+
this.segments = segments;
|
|
140
|
+
segments = Math.max(1, Math.floor(segments));
|
|
141
|
+
this.segments = segments;
|
|
142
|
+
this.radius = size / 2;
|
|
143
|
+
const directions = [];
|
|
144
|
+
const indices = [];
|
|
145
|
+
const shared = /* @__PURE__ */ new Map();
|
|
146
|
+
for (const { n, u, v } of FACES) {
|
|
147
|
+
const faceIndices = [];
|
|
148
|
+
for (let j = 0; j <= segments; j++) for (let i = 0; i <= segments; i++) {
|
|
149
|
+
const s = 2 * i - segments;
|
|
150
|
+
const t = 2 * j - segments;
|
|
151
|
+
const X = n[0] * segments + u[0] * s + v[0] * t;
|
|
152
|
+
const Y = n[1] * segments + u[1] * s + v[1] * t;
|
|
153
|
+
const Z = n[2] * segments + u[2] * s + v[2] * t;
|
|
154
|
+
const key = `${X},${Y},${Z}`;
|
|
155
|
+
let index = shared.get(key);
|
|
156
|
+
if (index === void 0) {
|
|
157
|
+
index = directions.length / 3;
|
|
158
|
+
shared.set(key, index);
|
|
159
|
+
directions.push(...CubeSphereGeometry.spherify(X / segments, Y / segments, Z / segments));
|
|
160
|
+
}
|
|
161
|
+
faceIndices.push(index);
|
|
162
|
+
}
|
|
163
|
+
const row = segments + 1;
|
|
164
|
+
for (let j = 0; j < segments; j++) for (let i = 0; i < segments; i++) {
|
|
165
|
+
const a = faceIndices[j * row + i];
|
|
166
|
+
const b = faceIndices[j * row + i + 1];
|
|
167
|
+
const c = faceIndices[(j + 1) * row + i + 1];
|
|
168
|
+
const d = faceIndices[(j + 1) * row + i];
|
|
169
|
+
indices.push(a, b, c, a, c, d);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
this._directions = new Float32Array(directions);
|
|
173
|
+
this.setIndex(indices);
|
|
174
|
+
this.setAttribute("position", new BufferAttribute(new Float32Array(directions.length), 3));
|
|
175
|
+
this.setAttribute("normal", new BufferAttribute(new Float32Array(directions.length), 3));
|
|
176
|
+
this.displace(() => 0);
|
|
177
|
+
this.attributes.normal.array.set(this._directions);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Sets each vertex to `radius + height(direction)` along its direction from the center,
|
|
181
|
+
* then recomputes normals and bounds. Always starts from the undisplaced sphere, so calling
|
|
182
|
+
* it again replaces the previous displacement rather than adding to it.
|
|
183
|
+
*
|
|
184
|
+
* @param height offset in world units for the unit direction (x, y, z)
|
|
185
|
+
*/
|
|
186
|
+
displace(height) {
|
|
187
|
+
const positions = this.attributes.position.array;
|
|
188
|
+
const d = this._directions;
|
|
189
|
+
for (let i = 0; i < d.length; i += 3) {
|
|
190
|
+
const r = this.radius + height(d[i], d[i + 1], d[i + 2]);
|
|
191
|
+
positions[i] = d[i] * r;
|
|
192
|
+
positions[i + 1] = d[i + 1] * r;
|
|
193
|
+
positions[i + 2] = d[i + 2] * r;
|
|
194
|
+
}
|
|
195
|
+
this.attributes.position.needsUpdate = true;
|
|
196
|
+
this.computeVertexNormals();
|
|
197
|
+
this.computeBoundingSphere();
|
|
198
|
+
if (this.boundingBox) this.computeBoundingBox();
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Displaces the surface with fractal Brownian motion. Noise is sampled at the undisplaced
|
|
203
|
+
* surface point times `scale`; the summed octaves are normalized to the noise's range and
|
|
204
|
+
* multiplied by `amplitude`.
|
|
205
|
+
*/
|
|
206
|
+
applyFractalBrownianMotion(noise3d, octaves, persistence, scale, amplitude) {
|
|
207
|
+
const r = this.radius * scale;
|
|
208
|
+
return this.displace((x, y, z) => CubeSphereGeometry.fBm(noise3d, x * r, y * r, z * r, octaves, persistence) * amplitude);
|
|
209
|
+
}
|
|
210
|
+
static fBm(noise3d, x, y, z, octaves, persistence) {
|
|
211
|
+
let total = 0;
|
|
212
|
+
let frequency = 1;
|
|
213
|
+
let amplitude = 1;
|
|
214
|
+
let maxValue = 0;
|
|
215
|
+
for (let i = 0; i < octaves; i++) {
|
|
216
|
+
total += noise3d(x * frequency, y * frequency, z * frequency) * amplitude;
|
|
217
|
+
maxValue += amplitude;
|
|
218
|
+
amplitude *= persistence;
|
|
219
|
+
frequency *= 2;
|
|
220
|
+
}
|
|
221
|
+
return total / maxValue;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* maps a point on the [-1, 1] cube onto the unit sphere with even spacing
|
|
225
|
+
* (https://mathproofs.blogspot.com/2005/07/mapping-cube-to-sphere.html)
|
|
226
|
+
*/
|
|
227
|
+
static spherify(x, y, z) {
|
|
228
|
+
const x2 = x * x;
|
|
229
|
+
const y2 = y * y;
|
|
230
|
+
const z2 = z * z;
|
|
231
|
+
return [
|
|
232
|
+
x * Math.sqrt(1 - y2 / 2 - z2 / 2 + y2 * z2 / 3),
|
|
233
|
+
y * Math.sqrt(1 - z2 / 2 - x2 / 2 + z2 * x2 / 3),
|
|
234
|
+
z * Math.sqrt(1 - x2 / 2 - y2 / 2 + x2 * y2 / 3)
|
|
235
|
+
];
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
//#endregion
|
|
239
|
+
export { CubeSphereGeometry };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Object3D } from "three";
|
|
2
|
+
//#region src/three/ecs.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A component: any plain object of data, stored by name on a {@link ThreeEntity}.
|
|
5
|
+
*
|
|
6
|
+
* @category ECS
|
|
7
|
+
*/
|
|
8
|
+
export type ThreeComponent = Record<string, any>;
|
|
9
|
+
/**
|
|
10
|
+
* An `Object3D` used as an entity: its id and components live in `userData`. Create one with
|
|
11
|
+
* {@link CreateThreeEntity}.
|
|
12
|
+
*
|
|
13
|
+
* @category ECS
|
|
14
|
+
*/
|
|
15
|
+
export interface ThreeEntity extends Object3D {
|
|
16
|
+
/** `Object3D.userData` with the entity's id and components */
|
|
17
|
+
userData: {
|
|
18
|
+
/** unique within the page, from {@link GenerateEntityId} */
|
|
19
|
+
entityId: string;
|
|
20
|
+
/** components by name */
|
|
21
|
+
components: Record<string, ThreeComponent>;
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns a new id `"entity_<n>"` from a module-level counter (unique per page load, not random).
|
|
27
|
+
*
|
|
28
|
+
* @category ECS
|
|
29
|
+
*/
|
|
30
|
+
export declare function GenerateEntityId(): string;
|
|
31
|
+
/**
|
|
32
|
+
* Turns `obj` (default: a new `Group`) into an entity in place: gives it a new id and an empty
|
|
33
|
+
* component set, replacing any it had.
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* const ship = CreateThreeEntity(shipMesh);
|
|
37
|
+
* AddThreeComponent(ship, 'velocity', { x: 0, y: 0, z: 1 });
|
|
38
|
+
* GetThreeComponent<{ x: number, y: number, z: number }>(ship, 'velocity')!.z; // 1
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @returns the same object, typed as a {@link ThreeEntity}
|
|
42
|
+
* @category ECS
|
|
43
|
+
*/
|
|
44
|
+
export declare function CreateThreeEntity(obj?: Object3D): ThreeEntity;
|
|
45
|
+
/**
|
|
46
|
+
* Stores `component` on `entity` under `name`, replacing any component with that name.
|
|
47
|
+
*
|
|
48
|
+
* @category ECS
|
|
49
|
+
*/
|
|
50
|
+
export declare function AddThreeComponent<T extends ThreeComponent>(entity: ThreeEntity, name: string, component: T): void;
|
|
51
|
+
/**
|
|
52
|
+
* The component named `name`, or `undefined` if the entity has none. `T` is not checked.
|
|
53
|
+
*
|
|
54
|
+
* @category ECS
|
|
55
|
+
*/
|
|
56
|
+
export declare function GetThreeComponent<T extends ThreeComponent>(entity: ThreeEntity, name: string): T | undefined;
|
|
57
|
+
/**
|
|
58
|
+
* Removes the component named `name`, if any.
|
|
59
|
+
*
|
|
60
|
+
* @category ECS
|
|
61
|
+
*/
|
|
62
|
+
export declare function RemoveThreeComponent(entity: ThreeEntity, name: string): void;
|
|
63
|
+
/**
|
|
64
|
+
* Whether `obj` has been made an entity (has a `userData.entityId`).
|
|
65
|
+
*
|
|
66
|
+
* @category ECS
|
|
67
|
+
*/
|
|
68
|
+
export declare function IsThreeEntity(obj: Object3D): boolean;
|
|
69
|
+
/**
|
|
70
|
+
* A system: called with all entities (by id) and the frame's delta time, plus any extra arguments.
|
|
71
|
+
*
|
|
72
|
+
* @category ECS
|
|
73
|
+
*/
|
|
74
|
+
export type ThreeSystem = (entities: Map<string, ThreeEntity>, delta: number, ...args: any) => void;
|
|
75
|
+
//#endregion
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Group } from "three";
|
|
2
|
+
//#region src/three/ecs.ts
|
|
3
|
+
let entityCounter = 0;
|
|
4
|
+
/**
|
|
5
|
+
* Returns a new id `"entity_<n>"` from a module-level counter (unique per page load, not random).
|
|
6
|
+
*
|
|
7
|
+
* @category ECS
|
|
8
|
+
*/
|
|
9
|
+
function GenerateEntityId() {
|
|
10
|
+
return `entity_${entityCounter++}`;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Turns `obj` (default: a new `Group`) into an entity in place: gives it a new id and an empty
|
|
14
|
+
* component set, replacing any it had.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const ship = CreateThreeEntity(shipMesh);
|
|
18
|
+
* AddThreeComponent(ship, 'velocity', { x: 0, y: 0, z: 1 });
|
|
19
|
+
* GetThreeComponent<{ x: number, y: number, z: number }>(ship, 'velocity')!.z; // 1
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @returns the same object, typed as a {@link ThreeEntity}
|
|
23
|
+
* @category ECS
|
|
24
|
+
*/
|
|
25
|
+
function CreateThreeEntity(obj) {
|
|
26
|
+
const entity = obj ?? new Group();
|
|
27
|
+
entity.userData.entityId = GenerateEntityId();
|
|
28
|
+
entity.userData.components = {};
|
|
29
|
+
return entity;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Stores `component` on `entity` under `name`, replacing any component with that name.
|
|
33
|
+
*
|
|
34
|
+
* @category ECS
|
|
35
|
+
*/
|
|
36
|
+
function AddThreeComponent(entity, name, component) {
|
|
37
|
+
entity.userData.components[name] = component;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The component named `name`, or `undefined` if the entity has none. `T` is not checked.
|
|
41
|
+
*
|
|
42
|
+
* @category ECS
|
|
43
|
+
*/
|
|
44
|
+
function GetThreeComponent(entity, name) {
|
|
45
|
+
return entity.userData.components[name];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Removes the component named `name`, if any.
|
|
49
|
+
*
|
|
50
|
+
* @category ECS
|
|
51
|
+
*/
|
|
52
|
+
function RemoveThreeComponent(entity, name) {
|
|
53
|
+
delete entity.userData.components[name];
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Whether `obj` has been made an entity (has a `userData.entityId`).
|
|
57
|
+
*
|
|
58
|
+
* @category ECS
|
|
59
|
+
*/
|
|
60
|
+
function IsThreeEntity(obj) {
|
|
61
|
+
return obj.userData.entityId !== void 0;
|
|
62
|
+
}
|
|
63
|
+
//#endregion
|
|
64
|
+
export { AddThreeComponent, CreateThreeEntity, GenerateEntityId, GetThreeComponent, IsThreeEntity, RemoveThreeComponent };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AsteroidMesh, AsteroidOptions } from "./asteroid-mesh.js";
|
|
2
|
+
import { CameraRig, CameraRigParams, PitchLimits } from "./camera-rig.js";
|
|
3
|
+
import { CubeSphereGeometry } from "./cube-sphere-geometry.js";
|
|
4
|
+
import { AddThreeComponent, CreateThreeEntity, GenerateEntityId, GetThreeComponent, IsThreeEntity, RemoveThreeComponent, ThreeComponent, ThreeEntity, ThreeSystem } from "./ecs.js";
|
|
5
|
+
import { ParticleSystem3d } from "./particle-system-3d.js";
|
|
6
|
+
import { Renderer } from "./renderer.js";
|
|
7
|
+
import { CreateStarfield, CreateSystemStar } from "./starfield.js";
|
|
8
|
+
import { CreateSubTexture, LoadTexture, TextureData } from "./texture-utils.js";
|
|
9
|
+
import { AtlasParams, TextureAtlas, UV } from "./texture-atlas.js";
|
|
10
|
+
import { BuildVoxelGeometryOptions, VoxelGeometry, buildVoxelGeometry } from "./voxel-geometry.js";
|
|
11
|
+
import { VoxelBounds, VoxelMesh, VoxelMeshOptions } from "./voxel-mesh.js";
|
|
12
|
+
export { AddThreeComponent, AsteroidMesh, AsteroidOptions, AtlasParams, BuildVoxelGeometryOptions, CameraRig, CameraRigParams, CreateStarfield, CreateSubTexture, CreateSystemStar, CreateThreeEntity, CubeSphereGeometry, GenerateEntityId, GetThreeComponent, IsThreeEntity, LoadTexture, ParticleSystem3d, PitchLimits, RemoveThreeComponent, Renderer, TextureAtlas, TextureData, ThreeComponent, ThreeEntity, ThreeSystem, UV, VoxelBounds, VoxelGeometry, VoxelMesh, VoxelMeshOptions, buildVoxelGeometry };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AsteroidMesh } from "./asteroid-mesh.js";
|
|
2
|
+
import { CameraRig } from "./camera-rig.js";
|
|
3
|
+
import { CubeSphereGeometry } from "./cube-sphere-geometry.js";
|
|
4
|
+
import { AddThreeComponent, CreateThreeEntity, GenerateEntityId, GetThreeComponent, IsThreeEntity, RemoveThreeComponent } from "./ecs.js";
|
|
5
|
+
import { ParticleSystem3d } from "./particle-system-3d.js";
|
|
6
|
+
import { Renderer } from "./renderer.js";
|
|
7
|
+
import { CreateStarfield, CreateSystemStar } from "./starfield.js";
|
|
8
|
+
import { CreateSubTexture, LoadTexture } from "./texture-utils.js";
|
|
9
|
+
import { TextureAtlas } from "./texture-atlas.js";
|
|
10
|
+
import { VoxelGeometry, buildVoxelGeometry } from "./voxel-geometry.js";
|
|
11
|
+
import { VoxelMesh } from "./voxel-mesh.js";
|
|
12
|
+
export { AddThreeComponent, AsteroidMesh, CameraRig, CreateStarfield, CreateSubTexture, CreateSystemStar, CreateThreeEntity, CubeSphereGeometry, GenerateEntityId, GetThreeComponent, IsThreeEntity, LoadTexture, ParticleSystem3d, RemoveThreeComponent, Renderer, TextureAtlas, VoxelGeometry, VoxelMesh, buildVoxelGeometry };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Group, PerspectiveCamera } from "three";
|
|
2
|
+
//#region src/three/particle-system-3d.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* "Particle life" simulation in 3D: 1000 colored points in five species that attract or repel each
|
|
5
|
+
* other according to a random species-by-species matrix, forming moving clusters. Particles live in
|
|
6
|
+
* a wrapping unit cube; the group is offset by -0.5 so the cube is centred on its origin (scale it
|
|
7
|
+
* to resize).
|
|
8
|
+
*
|
|
9
|
+
* Neighbours are found with a {@link SpatialPartition3d}. Points are drawn at a fixed 4 pixels and
|
|
10
|
+
* sorted back to front each update.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const life = new ParticleSystem3d(42);
|
|
14
|
+
* life.scale.setScalar(10);
|
|
15
|
+
* scene.add(life);
|
|
16
|
+
*
|
|
17
|
+
* // each frame
|
|
18
|
+
* life.update(camera, deltaTime);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @category Particles
|
|
22
|
+
*/
|
|
23
|
+
export declare class ParticleSystem3d extends Group {
|
|
24
|
+
/** number of particles; fixed */
|
|
25
|
+
readonly particleCount = 1000;
|
|
26
|
+
/** the seed the starting state came from */
|
|
27
|
+
readonly seed: number;
|
|
28
|
+
/** seconds; larger `dt` values passed to `update()` are capped to this so a stall can't blow the simulation up */
|
|
29
|
+
maxDeltaTime: number;
|
|
30
|
+
private _geometry;
|
|
31
|
+
private _particles;
|
|
32
|
+
private _frictionHalfLife;
|
|
33
|
+
private _range;
|
|
34
|
+
private _forceFactor;
|
|
35
|
+
private _positions;
|
|
36
|
+
private _colors;
|
|
37
|
+
private _cameraLocal;
|
|
38
|
+
private _attractionMatrix;
|
|
39
|
+
private _spatialPartition;
|
|
40
|
+
/** the same seed always produces the same starting state (positions, colors, attraction matrix) */
|
|
41
|
+
constructor(seed?: number);
|
|
42
|
+
private _calcForce;
|
|
43
|
+
private _updateVelocities;
|
|
44
|
+
private _updatePositions;
|
|
45
|
+
private _sortParticles;
|
|
46
|
+
private _updateGeometry;
|
|
47
|
+
/**
|
|
48
|
+
* Advances the simulation by `dt` seconds and refreshes the points, sorted back to front as
|
|
49
|
+
* seen from `camera`. `dt` is capped to {@link ParticleSystem3d.maxDeltaTime}.
|
|
50
|
+
*/
|
|
51
|
+
update(camera: PerspectiveCamera, dt: number): void;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { fract } from "../math.js";
|
|
2
|
+
import { Random } from "../random.js";
|
|
3
|
+
import { rng } from "../rng.js";
|
|
4
|
+
import { randomMatrix } from "../particle-system-2d.js";
|
|
5
|
+
import { SpatialPartition3d } from "../spatial-partition-3d.js";
|
|
6
|
+
import { BufferAttribute, BufferGeometry, DynamicDrawUsage, Group, Points, ShaderMaterial, Sphere, Vector3 } from "three";
|
|
7
|
+
//#region src/three/particle-system-3d.ts
|
|
8
|
+
const vertexShader = `
|
|
9
|
+
uniform float pointSize;
|
|
10
|
+
|
|
11
|
+
varying vec4 vColor;
|
|
12
|
+
|
|
13
|
+
void main() {
|
|
14
|
+
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
15
|
+
|
|
16
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
17
|
+
gl_PointSize = pointSize;
|
|
18
|
+
|
|
19
|
+
vColor = color;
|
|
20
|
+
}`;
|
|
21
|
+
const fragmentShader = `
|
|
22
|
+
varying vec4 vColor;
|
|
23
|
+
|
|
24
|
+
void main() {
|
|
25
|
+
gl_FragColor = vColor;
|
|
26
|
+
}`;
|
|
27
|
+
const Colors = [
|
|
28
|
+
[
|
|
29
|
+
1,
|
|
30
|
+
0,
|
|
31
|
+
0,
|
|
32
|
+
1
|
|
33
|
+
],
|
|
34
|
+
[
|
|
35
|
+
0,
|
|
36
|
+
1,
|
|
37
|
+
0,
|
|
38
|
+
1
|
|
39
|
+
],
|
|
40
|
+
[
|
|
41
|
+
0,
|
|
42
|
+
0,
|
|
43
|
+
1,
|
|
44
|
+
1
|
|
45
|
+
],
|
|
46
|
+
[
|
|
47
|
+
1,
|
|
48
|
+
1,
|
|
49
|
+
0,
|
|
50
|
+
1
|
|
51
|
+
],
|
|
52
|
+
[
|
|
53
|
+
1,
|
|
54
|
+
0,
|
|
55
|
+
1,
|
|
56
|
+
1
|
|
57
|
+
]
|
|
58
|
+
];
|
|
59
|
+
/**
|
|
60
|
+
* "Particle life" simulation in 3D: 1000 colored points in five species that attract or repel each
|
|
61
|
+
* other according to a random species-by-species matrix, forming moving clusters. Particles live in
|
|
62
|
+
* a wrapping unit cube; the group is offset by -0.5 so the cube is centred on its origin (scale it
|
|
63
|
+
* to resize).
|
|
64
|
+
*
|
|
65
|
+
* Neighbours are found with a {@link SpatialPartition3d}. Points are drawn at a fixed 4 pixels and
|
|
66
|
+
* sorted back to front each update.
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* const life = new ParticleSystem3d(42);
|
|
70
|
+
* life.scale.setScalar(10);
|
|
71
|
+
* scene.add(life);
|
|
72
|
+
*
|
|
73
|
+
* // each frame
|
|
74
|
+
* life.update(camera, deltaTime);
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @category Particles
|
|
78
|
+
*/
|
|
79
|
+
var ParticleSystem3d = class extends Group {
|
|
80
|
+
/** number of particles; fixed */
|
|
81
|
+
particleCount = 1e3;
|
|
82
|
+
/** the seed the starting state came from */
|
|
83
|
+
seed;
|
|
84
|
+
/** seconds; larger `dt` values passed to `update()` are capped to this so a stall can't blow the simulation up */
|
|
85
|
+
maxDeltaTime = .1;
|
|
86
|
+
_geometry;
|
|
87
|
+
_particles = [];
|
|
88
|
+
_frictionHalfLife = .04;
|
|
89
|
+
_range = .2;
|
|
90
|
+
_forceFactor = 6;
|
|
91
|
+
_positions;
|
|
92
|
+
_colors;
|
|
93
|
+
_cameraLocal = new Vector3();
|
|
94
|
+
_attractionMatrix;
|
|
95
|
+
_spatialPartition;
|
|
96
|
+
/** the same seed always produces the same starting state (positions, colors, attraction matrix) */
|
|
97
|
+
constructor(seed = rng.nextUint()) {
|
|
98
|
+
super();
|
|
99
|
+
this.seed = seed;
|
|
100
|
+
const uniforms = { pointSize: { value: 4 } };
|
|
101
|
+
this.position.x = -.5;
|
|
102
|
+
this.position.y = -.5;
|
|
103
|
+
this.position.z = -.5;
|
|
104
|
+
this._positions = new Float32Array(this.particleCount * 3);
|
|
105
|
+
this._colors = new Float32Array(this.particleCount * 4);
|
|
106
|
+
this._geometry = new BufferGeometry();
|
|
107
|
+
this._geometry.setAttribute("position", new BufferAttribute(this._positions, 3).setUsage(DynamicDrawUsage));
|
|
108
|
+
this._geometry.setAttribute("color", new BufferAttribute(this._colors, 4).setUsage(DynamicDrawUsage));
|
|
109
|
+
this._geometry.boundingSphere = new Sphere(new Vector3(.5, .5, .5), Math.sqrt(3) / 2);
|
|
110
|
+
const material = new ShaderMaterial({
|
|
111
|
+
uniforms,
|
|
112
|
+
vertexShader,
|
|
113
|
+
fragmentShader,
|
|
114
|
+
depthTest: true,
|
|
115
|
+
depthWrite: false,
|
|
116
|
+
vertexColors: true
|
|
117
|
+
});
|
|
118
|
+
const pointsMesh = new Points(this._geometry, material);
|
|
119
|
+
this.add(pointsMesh);
|
|
120
|
+
for (let i = 0; i < this.particleCount; i++) this._particles.push({
|
|
121
|
+
x: Random.float(seed, i, 0),
|
|
122
|
+
y: Random.float(seed, i, 1),
|
|
123
|
+
z: Random.float(seed, i, 2),
|
|
124
|
+
vx: 0,
|
|
125
|
+
vy: 0,
|
|
126
|
+
vz: 0,
|
|
127
|
+
color: Math.floor(Random.float(seed, i, 3) * Colors.length)
|
|
128
|
+
});
|
|
129
|
+
this._attractionMatrix = randomMatrix(Colors.length, seed);
|
|
130
|
+
this._spatialPartition = new SpatialPartition3d(this._range, this._particles);
|
|
131
|
+
}
|
|
132
|
+
_calcForce(r, a, beta = .3) {
|
|
133
|
+
let f = 0;
|
|
134
|
+
if (r < beta) f = r / beta - 1;
|
|
135
|
+
else if (beta < r && r < 1) f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
|
|
136
|
+
return f;
|
|
137
|
+
}
|
|
138
|
+
_updateVelocities(dt) {
|
|
139
|
+
const frictionFactor = Math.pow(.5, dt / this._frictionHalfLife);
|
|
140
|
+
for (let z = 0; z < this._spatialPartition.cells.length; z++) for (let y = 0; y < this._spatialPartition.cells[z].length; y++) for (let x = 0; x < this._spatialPartition.cells[z][y].length; x++) {
|
|
141
|
+
const cell = this._spatialPartition.cells[z][y][x];
|
|
142
|
+
const neighbors = this._spatialPartition.getCellNeighbors(x, y, z);
|
|
143
|
+
for (let i = 0; i < cell.length; i++) {
|
|
144
|
+
let fx = 0, fy = 0, fz = 0;
|
|
145
|
+
const p1 = cell[i];
|
|
146
|
+
for (const neighborCell of neighbors) for (let j = 0; j < neighborCell.length; j++) {
|
|
147
|
+
if (cell === neighborCell && i === j) continue;
|
|
148
|
+
const p2 = neighborCell[j];
|
|
149
|
+
let dx = p2.x - p1.x;
|
|
150
|
+
let dy = p2.y - p1.y;
|
|
151
|
+
let dz = p2.z - p1.z;
|
|
152
|
+
if (Math.abs(dx) > .5) dx = dx > 0 ? dx - 1 : dx + 1;
|
|
153
|
+
if (Math.abs(dy) > .5) dy = dy > 0 ? dy - 1 : dy + 1;
|
|
154
|
+
if (Math.abs(dz) > .5) dz = dz > 0 ? dz - 1 : dz + 1;
|
|
155
|
+
const dist = Math.hypot(dx, dy, dz);
|
|
156
|
+
if (dist > 0 && dist < this._range) {
|
|
157
|
+
const f = this._calcForce(dist / this._range, this._attractionMatrix[p1.color][p2.color]);
|
|
158
|
+
fx += dx / dist * f;
|
|
159
|
+
fy += dy / dist * f;
|
|
160
|
+
fz += dz / dist * f;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const scale = this._range * this._forceFactor * dt;
|
|
164
|
+
fx *= scale;
|
|
165
|
+
fy *= scale;
|
|
166
|
+
fz *= scale;
|
|
167
|
+
p1.vx *= frictionFactor;
|
|
168
|
+
p1.vy *= frictionFactor;
|
|
169
|
+
p1.vz *= frictionFactor;
|
|
170
|
+
p1.vx += fx;
|
|
171
|
+
p1.vy += fy;
|
|
172
|
+
p1.vz += fz;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
_updatePositions(dt) {
|
|
177
|
+
for (let i = 0; i < this._particles.length; i++) {
|
|
178
|
+
const particle = this._particles[i];
|
|
179
|
+
const oldCell = this._spatialPartition.getCell(particle.x, particle.y, particle.z);
|
|
180
|
+
particle.x += particle.vx * dt;
|
|
181
|
+
particle.y += particle.vy * dt;
|
|
182
|
+
particle.z += particle.vz * dt;
|
|
183
|
+
particle.x = fract(particle.x);
|
|
184
|
+
particle.y = fract(particle.y);
|
|
185
|
+
particle.z = fract(particle.z);
|
|
186
|
+
const newCell = this._spatialPartition.getCell(particle.x, particle.y, particle.z);
|
|
187
|
+
if (newCell !== oldCell) {
|
|
188
|
+
const index = oldCell.indexOf(particle);
|
|
189
|
+
if (index !== -1) oldCell.splice(index, 1);
|
|
190
|
+
newCell.push(particle);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
_sortParticles(camera) {
|
|
195
|
+
const c = this.worldToLocal(camera.getWorldPosition(this._cameraLocal));
|
|
196
|
+
const distanceSq = (p) => (p.x - c.x) ** 2 + (p.y - c.y) ** 2 + (p.z - c.z) ** 2;
|
|
197
|
+
this._particles.sort((a, b) => distanceSq(b) - distanceSq(a));
|
|
198
|
+
}
|
|
199
|
+
_updateGeometry() {
|
|
200
|
+
for (let i = 0; i < this._particles.length; i++) {
|
|
201
|
+
const particle = this._particles[i];
|
|
202
|
+
const color = Colors[particle.color];
|
|
203
|
+
this._positions[i * 3] = particle.x;
|
|
204
|
+
this._positions[i * 3 + 1] = particle.y;
|
|
205
|
+
this._positions[i * 3 + 2] = particle.z;
|
|
206
|
+
this._colors.set(color, i * 4);
|
|
207
|
+
}
|
|
208
|
+
this._geometry.attributes.position.needsUpdate = true;
|
|
209
|
+
this._geometry.attributes.color.needsUpdate = true;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Advances the simulation by `dt` seconds and refreshes the points, sorted back to front as
|
|
213
|
+
* seen from `camera`. `dt` is capped to {@link ParticleSystem3d.maxDeltaTime}.
|
|
214
|
+
*/
|
|
215
|
+
update(camera, dt) {
|
|
216
|
+
dt = Math.min(dt, this.maxDeltaTime);
|
|
217
|
+
this._updateVelocities(dt);
|
|
218
|
+
this._updatePositions(dt);
|
|
219
|
+
this._sortParticles(camera);
|
|
220
|
+
this._updateGeometry();
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
//#endregion
|
|
224
|
+
export { ParticleSystem3d };
|