@n3rdw1z4rd/core 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/README.md +2 -0
- package/dist/index.css +113 -0
- package/dist/index.d.mts +359 -0
- package/dist/index.d.ts +359 -0
- package/dist/index.js +1615 -0
- package/dist/index.mjs +1551 -0
- package/package.json +38 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1551 @@
|
|
|
1
|
+
// src/clock-stats.ts
|
|
2
|
+
var ClockStats = class {
|
|
3
|
+
divElement;
|
|
4
|
+
keyColor = "gray";
|
|
5
|
+
valueColor = "lightgray";
|
|
6
|
+
constructor(align = "bottom", justify = "right") {
|
|
7
|
+
this.divElement = document.createElement("div");
|
|
8
|
+
this.divElement.setAttribute("id", "clock-stats");
|
|
9
|
+
this.divElement.style.setProperty("position", "absolute");
|
|
10
|
+
this.divElement.style.setProperty(align, "4px");
|
|
11
|
+
this.divElement.style.setProperty(justify, "4px");
|
|
12
|
+
}
|
|
13
|
+
appendTo(target) {
|
|
14
|
+
this.divElement.parentNode?.removeChild(this.divElement);
|
|
15
|
+
if (target) {
|
|
16
|
+
target.appendChild(this.divElement);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
update(data) {
|
|
20
|
+
this.divElement.innerHTML = Object.entries(data).map(
|
|
21
|
+
([key, value]) => `<span style="color: ${this.keyColor};">${key}:</span> <span style="color: ${this.valueColor};">${value}</span>`
|
|
22
|
+
).join("<br/>");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/clock.ts
|
|
27
|
+
var Clock = class {
|
|
28
|
+
_lastFrameTime = 0;
|
|
29
|
+
_daltaTimeMilliseconds = 0;
|
|
30
|
+
_deltaTimeSeconds = 0;
|
|
31
|
+
_avgDeltaTime = 0;
|
|
32
|
+
_frameCount = 0;
|
|
33
|
+
_frameCountTotal = 0;
|
|
34
|
+
_frameTime = 0;
|
|
35
|
+
_fps = 0;
|
|
36
|
+
_isRunning = false;
|
|
37
|
+
_updateCallback = () => {
|
|
38
|
+
};
|
|
39
|
+
_statsParent;
|
|
40
|
+
_stats;
|
|
41
|
+
_startTime = 0;
|
|
42
|
+
get fps() {
|
|
43
|
+
return this._fps;
|
|
44
|
+
}
|
|
45
|
+
get frames() {
|
|
46
|
+
return this._frameCountTotal;
|
|
47
|
+
}
|
|
48
|
+
get deltaTimeSeconds() {
|
|
49
|
+
return this._deltaTimeSeconds;
|
|
50
|
+
}
|
|
51
|
+
get deltaTimeMilliseconds() {
|
|
52
|
+
return this._daltaTimeMilliseconds;
|
|
53
|
+
}
|
|
54
|
+
get avgDeltaTime() {
|
|
55
|
+
return this._avgDeltaTime;
|
|
56
|
+
}
|
|
57
|
+
get time() {
|
|
58
|
+
return this._lastFrameTime;
|
|
59
|
+
}
|
|
60
|
+
get isRunning() {
|
|
61
|
+
return this._isRunning;
|
|
62
|
+
}
|
|
63
|
+
get elapsedTimeSinceStart() {
|
|
64
|
+
return performance.now() - this._startTime;
|
|
65
|
+
}
|
|
66
|
+
constructor(statsDivParent) {
|
|
67
|
+
this._statsParent = statsDivParent;
|
|
68
|
+
}
|
|
69
|
+
_update(time) {
|
|
70
|
+
this.update(time);
|
|
71
|
+
this._updateCallback(this._deltaTimeSeconds);
|
|
72
|
+
if (this._isRunning) {
|
|
73
|
+
requestAnimationFrame(this._update.bind(this));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
update(time) {
|
|
77
|
+
this._daltaTimeMilliseconds = time - this._lastFrameTime;
|
|
78
|
+
this._deltaTimeSeconds = this._daltaTimeMilliseconds / 1e3;
|
|
79
|
+
this._lastFrameTime = time;
|
|
80
|
+
if (this._frameTime + 1e3 >= time) {
|
|
81
|
+
this._frameCount += 1;
|
|
82
|
+
} else {
|
|
83
|
+
this._frameTime = time;
|
|
84
|
+
this._fps = this._frameCount;
|
|
85
|
+
this._frameCount = 0;
|
|
86
|
+
}
|
|
87
|
+
this._frameCountTotal++;
|
|
88
|
+
this._avgDeltaTime = (this._avgDeltaTime * this._frameCount + this._daltaTimeMilliseconds) / (this._frameCount + 1);
|
|
89
|
+
}
|
|
90
|
+
run(callback) {
|
|
91
|
+
if (!this._isRunning) {
|
|
92
|
+
this._frameCountTotal = 0;
|
|
93
|
+
this._startTime = performance.now();
|
|
94
|
+
this._isRunning = true;
|
|
95
|
+
this._updateCallback = callback;
|
|
96
|
+
requestAnimationFrame(this._update.bind(this));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
runOnce(callback) {
|
|
100
|
+
this._isRunning = false;
|
|
101
|
+
this._updateCallback = callback;
|
|
102
|
+
this._update(0);
|
|
103
|
+
}
|
|
104
|
+
stop() {
|
|
105
|
+
this._isRunning = false;
|
|
106
|
+
}
|
|
107
|
+
getExecuteTime(func) {
|
|
108
|
+
const now = performance.now();
|
|
109
|
+
func();
|
|
110
|
+
return performance.now() - now;
|
|
111
|
+
}
|
|
112
|
+
showStats(data = {}, parent) {
|
|
113
|
+
parent = parent ?? this._statsParent ?? document.body;
|
|
114
|
+
if (!this._stats) {
|
|
115
|
+
this._stats = new ClockStats();
|
|
116
|
+
this._stats.appendTo(parent);
|
|
117
|
+
}
|
|
118
|
+
this._stats.update({
|
|
119
|
+
fps: this._fps,
|
|
120
|
+
"deltaTime(s)": `${this._deltaTimeSeconds.toFixed(3)}`,
|
|
121
|
+
"avgDeltaTime(ms)": `${this._avgDeltaTime.toFixed(3)}`,
|
|
122
|
+
...data
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
// src/logger.ts
|
|
128
|
+
var logdev = console.debug.bind(console, "%c[dbg]", "font-weight: bold; color: #888;");
|
|
129
|
+
var log = logdev;
|
|
130
|
+
var loginf = console.log.bind(console, "%c[inf]", "font-weight: bold;");
|
|
131
|
+
var logwrn = console.log.bind(console, "%c[wrn]", "font-weight: bold; color: orange;");
|
|
132
|
+
var logerr = console.log.bind(console, "%c[err]", "font-weight: bold; color: red;");
|
|
133
|
+
|
|
134
|
+
// src/ecs.ts
|
|
135
|
+
var ECS = class _ECS {
|
|
136
|
+
_globals = /* @__PURE__ */ new Map();
|
|
137
|
+
_entities = /* @__PURE__ */ new Map();
|
|
138
|
+
_components = /* @__PURE__ */ new Map();
|
|
139
|
+
_defaultComponents = [];
|
|
140
|
+
_systems = /* @__PURE__ */ new Map();
|
|
141
|
+
_onTickStartCallbacks = [];
|
|
142
|
+
_onTickEndCallbacks = [];
|
|
143
|
+
get components() {
|
|
144
|
+
return [...this._components.keys()];
|
|
145
|
+
}
|
|
146
|
+
get systems() {
|
|
147
|
+
return [...this._systems.keys()];
|
|
148
|
+
}
|
|
149
|
+
get entities() {
|
|
150
|
+
return [...this._entities.values()];
|
|
151
|
+
}
|
|
152
|
+
uid(length = 8) {
|
|
153
|
+
const numBytes = Math.ceil(length / 2);
|
|
154
|
+
const buffer = new Uint8Array(numBytes);
|
|
155
|
+
window.crypto.getRandomValues(buffer);
|
|
156
|
+
let uid = "";
|
|
157
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
158
|
+
uid += buffer[i].toString(16).padStart(2, "0");
|
|
159
|
+
}
|
|
160
|
+
return uid.substring(0, length);
|
|
161
|
+
}
|
|
162
|
+
createComponent(uid, data = null) {
|
|
163
|
+
if (!this._components.has(uid)) {
|
|
164
|
+
this._components.set(uid, data);
|
|
165
|
+
log("created component:", { uid, data });
|
|
166
|
+
} else {
|
|
167
|
+
logwrn("createComponent: a component already exists with uid:", uid);
|
|
168
|
+
}
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
includeAsDefaultComponents(...components) {
|
|
172
|
+
components.forEach((component) => {
|
|
173
|
+
if (!this._defaultComponents.includes(component)) {
|
|
174
|
+
this._defaultComponents.push(component);
|
|
175
|
+
log("includeAsDefaultComponents:", component);
|
|
176
|
+
} else {
|
|
177
|
+
log("includeAsDefaultComponents: already exists:", component);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
createSystem(uid, ...components) {
|
|
183
|
+
const callback = components.pop();
|
|
184
|
+
const comps = components.filter((comp) => typeof comp === "string");
|
|
185
|
+
if (!this._systems.has(uid)) {
|
|
186
|
+
this._systems.set(uid, { components: comps, callback });
|
|
187
|
+
log("created system:", { uid, components, callback });
|
|
188
|
+
} else {
|
|
189
|
+
logwrn("createSystem: a system already exists with uid:", uid);
|
|
190
|
+
}
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
createEntityWithUid(uid, ...components) {
|
|
194
|
+
if (!this._entities.has(uid)) {
|
|
195
|
+
const componentList = [
|
|
196
|
+
.../* @__PURE__ */ new Set([
|
|
197
|
+
...this._defaultComponents,
|
|
198
|
+
...components
|
|
199
|
+
])
|
|
200
|
+
];
|
|
201
|
+
const comps = /* @__PURE__ */ new Map();
|
|
202
|
+
componentList.forEach((component) => {
|
|
203
|
+
if (this._components.has(component)) {
|
|
204
|
+
const componentData = this._components.get(component);
|
|
205
|
+
const data = {};
|
|
206
|
+
for (const uid2 in componentData) {
|
|
207
|
+
const value = componentData[uid2];
|
|
208
|
+
data[uid2] = typeof value === "function" ? value(uid2) : value;
|
|
209
|
+
}
|
|
210
|
+
comps.set(component, data);
|
|
211
|
+
} else {
|
|
212
|
+
logwrn("createEntityWithUid: missing component:", component);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
if (comps.size === componentList.length) {
|
|
216
|
+
const entity = { uid, components: comps };
|
|
217
|
+
this._entities.set(uid, entity);
|
|
218
|
+
log("created entity:", entity);
|
|
219
|
+
} else {
|
|
220
|
+
logwrn("createEntityWithUid: failed to create an entity with missing components");
|
|
221
|
+
}
|
|
222
|
+
} else {
|
|
223
|
+
logwrn("createEntityWithUid: an entity already exists with uid:", uid);
|
|
224
|
+
}
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
createEntity(...components) {
|
|
228
|
+
const uid = this.uid();
|
|
229
|
+
return this.createEntityWithUid(uid, ...components);
|
|
230
|
+
}
|
|
231
|
+
createEntities(count, ...components) {
|
|
232
|
+
for (; count > 0; count--) {
|
|
233
|
+
this.createEntity(...components);
|
|
234
|
+
}
|
|
235
|
+
return this;
|
|
236
|
+
}
|
|
237
|
+
getEntity(uid) {
|
|
238
|
+
return this._entities.get(uid);
|
|
239
|
+
}
|
|
240
|
+
getEntitiesWithComponents(...components) {
|
|
241
|
+
const filter = components.pop();
|
|
242
|
+
const uids = components.filter((component) => typeof component === "string");
|
|
243
|
+
const entities = [];
|
|
244
|
+
this._entities.forEach((entity) => {
|
|
245
|
+
if (uids.every(
|
|
246
|
+
(component) => [...entity.components.keys()].includes(component)
|
|
247
|
+
)) {
|
|
248
|
+
for (const filterComponent in filter) {
|
|
249
|
+
for (const filterKey in filter[filterComponent]) {
|
|
250
|
+
const entityValue = entity.components.get(filterComponent)[filterKey];
|
|
251
|
+
const filterValue = filter[filterComponent][filterKey];
|
|
252
|
+
if (entityValue === filterValue) {
|
|
253
|
+
entities.push(entity);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
return entities;
|
|
260
|
+
}
|
|
261
|
+
addComponent(uid, component, values = {}) {
|
|
262
|
+
const entity = this._entities.get(uid);
|
|
263
|
+
if (entity) {
|
|
264
|
+
if (this._components.has(component)) {
|
|
265
|
+
const comp = this._components.get(component);
|
|
266
|
+
const componentData = {
|
|
267
|
+
...comp,
|
|
268
|
+
...values
|
|
269
|
+
};
|
|
270
|
+
entity.components.set(component, componentData);
|
|
271
|
+
log("addComponent: added component:", component, "to entity:", uid);
|
|
272
|
+
} else {
|
|
273
|
+
logwrn("addComponent: component not found:", component);
|
|
274
|
+
}
|
|
275
|
+
} else {
|
|
276
|
+
logwrn("addComponent: entity not found:", uid);
|
|
277
|
+
}
|
|
278
|
+
return this;
|
|
279
|
+
}
|
|
280
|
+
onAllEntitiesNow(callback) {
|
|
281
|
+
this._entities.forEach((entity) => callback(entity));
|
|
282
|
+
log("onAllEntitiesNow: executed on all entities:", callback);
|
|
283
|
+
return this;
|
|
284
|
+
}
|
|
285
|
+
duplicateEntity(uid, count = 1, deep = false) {
|
|
286
|
+
const entity = this._entities.get(uid);
|
|
287
|
+
if (entity) {
|
|
288
|
+
if (!deep) {
|
|
289
|
+
for (let i = 0; i < count; i++) this.createEntity(...entity.components.keys());
|
|
290
|
+
} else {
|
|
291
|
+
for (let i = 0; i < count; i++) {
|
|
292
|
+
const newEntity = { uid: this.uid(), components: /* @__PURE__ */ new Map() };
|
|
293
|
+
entity.components.forEach((value, key) => {
|
|
294
|
+
newEntity.components.set(key, value);
|
|
295
|
+
});
|
|
296
|
+
this._entities.set(newEntity.uid, newEntity);
|
|
297
|
+
}
|
|
298
|
+
;
|
|
299
|
+
}
|
|
300
|
+
log("duplicateEntity: duplicated entity:", uid);
|
|
301
|
+
} else {
|
|
302
|
+
logwrn("duplicateEntity: entity not found:", uid);
|
|
303
|
+
}
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
getGlobal(key) {
|
|
307
|
+
return this._globals.get(key);
|
|
308
|
+
}
|
|
309
|
+
setGlobal(key, value) {
|
|
310
|
+
this._globals.set(key, value);
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
beforeTick(callback) {
|
|
314
|
+
this._onTickStartCallbacks.push(callback);
|
|
315
|
+
log("beforeTick: added:", callback);
|
|
316
|
+
return this;
|
|
317
|
+
}
|
|
318
|
+
afterTick(callback) {
|
|
319
|
+
this._onTickEndCallbacks.push(callback);
|
|
320
|
+
log("afterTick: added:", callback);
|
|
321
|
+
return this;
|
|
322
|
+
}
|
|
323
|
+
runSystem(uid) {
|
|
324
|
+
const system = this._systems.get(uid);
|
|
325
|
+
if (system) {
|
|
326
|
+
this._entities.forEach((entity) => {
|
|
327
|
+
if (system.components.every(
|
|
328
|
+
(component) => [...entity.components.keys()].includes(component)
|
|
329
|
+
)) {
|
|
330
|
+
system.callback(entity, Object.fromEntries(entity.components));
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
} else {
|
|
334
|
+
logwrn("runSystem: system not found:", uid);
|
|
335
|
+
}
|
|
336
|
+
return this;
|
|
337
|
+
}
|
|
338
|
+
update() {
|
|
339
|
+
this._onTickStartCallbacks.forEach((cb) => cb());
|
|
340
|
+
this._systems.forEach((_, uid) => this.runSystem(uid));
|
|
341
|
+
this._onTickEndCallbacks.forEach((cb) => cb());
|
|
342
|
+
}
|
|
343
|
+
static get instance() {
|
|
344
|
+
if (!_ECS._instance) {
|
|
345
|
+
_ECS._instance = new _ECS();
|
|
346
|
+
}
|
|
347
|
+
return _ECS._instance;
|
|
348
|
+
}
|
|
349
|
+
static _instance;
|
|
350
|
+
constructor() {
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
// src/emitter.ts
|
|
355
|
+
var Emitter = class {
|
|
356
|
+
listeners = /* @__PURE__ */ new Map();
|
|
357
|
+
constructor() {
|
|
358
|
+
}
|
|
359
|
+
on(...eventName) {
|
|
360
|
+
const listener = eventName.pop();
|
|
361
|
+
const events = eventName.filter((ev) => typeof ev === "string");
|
|
362
|
+
events.forEach((event) => {
|
|
363
|
+
if (!this.listeners.has(event)) {
|
|
364
|
+
this.listeners.set(event, new Array());
|
|
365
|
+
}
|
|
366
|
+
this.listeners.get(event)?.push(listener);
|
|
367
|
+
});
|
|
368
|
+
return this;
|
|
369
|
+
}
|
|
370
|
+
emit(eventName, ...args) {
|
|
371
|
+
this.listeners.get(eventName)?.forEach((listener) => listener(...args, eventName));
|
|
372
|
+
return this;
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
var GlobalEmitter = new Emitter();
|
|
376
|
+
|
|
377
|
+
// src/input.ts
|
|
378
|
+
import { vec2 } from "gl-matrix";
|
|
379
|
+
var Input = class extends Emitter {
|
|
380
|
+
inputThreshold = 200;
|
|
381
|
+
_keyStates = {};
|
|
382
|
+
_mouseButtonStates = {};
|
|
383
|
+
_mousePosition = vec2.create();
|
|
384
|
+
_mousePosition2 = vec2.create();
|
|
385
|
+
get mousePosition() {
|
|
386
|
+
return this._mousePosition;
|
|
387
|
+
}
|
|
388
|
+
get mousePosition2() {
|
|
389
|
+
return this._mousePosition2;
|
|
390
|
+
}
|
|
391
|
+
constructor() {
|
|
392
|
+
super();
|
|
393
|
+
window.addEventListener("keydown", this._onKeyDown.bind(this));
|
|
394
|
+
window.addEventListener("keyup", this._onKeyUp.bind(this));
|
|
395
|
+
window.addEventListener("mousedown", this._onMouseButtonDown.bind(this));
|
|
396
|
+
window.addEventListener("mouseup", this._onMouseButtonUp.bind(this));
|
|
397
|
+
window.addEventListener("mousemove", this._onMouseMove.bind(this));
|
|
398
|
+
window.addEventListener("wheel", this._onWheel.bind(this));
|
|
399
|
+
}
|
|
400
|
+
_getCommonEventProps(ev) {
|
|
401
|
+
const props = {
|
|
402
|
+
timeStamp: ev.timeStamp,
|
|
403
|
+
altKey: ev.altKey,
|
|
404
|
+
ctrlKey: ev.ctrlKey,
|
|
405
|
+
metaKey: ev.metaKey,
|
|
406
|
+
shiftKey: ev.shiftKey
|
|
407
|
+
};
|
|
408
|
+
return props;
|
|
409
|
+
}
|
|
410
|
+
// private _onContextMenu(ev: MouseEvent) {
|
|
411
|
+
// ev.preventDefault();
|
|
412
|
+
// this.emit('contextmenu');
|
|
413
|
+
// return false;
|
|
414
|
+
// }
|
|
415
|
+
_onKeyDown(ev) {
|
|
416
|
+
const props = this._getCommonEventProps(ev);
|
|
417
|
+
const { code, key } = ev;
|
|
418
|
+
if (!ev.repeat) {
|
|
419
|
+
this._keyStates[code] = { state: 1, timeStamp: props.timeStamp };
|
|
420
|
+
this.emit("key_down", { ...props, code, key });
|
|
421
|
+
this.emit(`${code.toLowerCase()}_down`, props);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
_onKeyUp(ev) {
|
|
425
|
+
const props = this._getCommonEventProps(ev);
|
|
426
|
+
const { code, key } = ev;
|
|
427
|
+
const deltaStamp = props.timeStamp - (this._keyStates[code]?.timeStamp ?? 0);
|
|
428
|
+
this._keyStates[code] = { state: 0, timeStamp: props.timeStamp };
|
|
429
|
+
this.emit("key_up", { ...props, code, key });
|
|
430
|
+
this.emit(`${code.toLowerCase()}_up`, props);
|
|
431
|
+
if (deltaStamp < this.inputThreshold) {
|
|
432
|
+
this.emit("key_pressed", { ...props, code, key });
|
|
433
|
+
this.emit(`${code.toLowerCase()}_pressed`, props);
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
_onMouseButtonDown(ev) {
|
|
437
|
+
const props = this._getCommonEventProps(ev);
|
|
438
|
+
const { button } = ev;
|
|
439
|
+
if (!this._mouseButtonStates[button]?.state) {
|
|
440
|
+
this._mouseButtonStates[button] = { state: 1, timeStamp: props.timeStamp };
|
|
441
|
+
this.emit("mouse_button_down", { ...props, button });
|
|
442
|
+
this.emit(`mouse_button_${button}_down`, props);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
_onMouseButtonUp(ev) {
|
|
446
|
+
const props = this._getCommonEventProps(ev);
|
|
447
|
+
const { button } = ev;
|
|
448
|
+
const deltaStamp = props.timeStamp - (this._mouseButtonStates[button]?.timeStamp ?? 0);
|
|
449
|
+
this._mouseButtonStates[button] = { state: 0, timeStamp: props.timeStamp };
|
|
450
|
+
this.emit("mouse_button_up", { ...props, button });
|
|
451
|
+
this.emit(`mouse_button_${button}_up`, props);
|
|
452
|
+
if (deltaStamp < this.inputThreshold) {
|
|
453
|
+
this.emit("mouse_button_clicked", { ...props, button });
|
|
454
|
+
this.emit(`mouse_button_${button}_clicked`, props);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
_onMouseMove(ev) {
|
|
458
|
+
const props = this._getCommonEventProps(ev);
|
|
459
|
+
const { buttons, offsetX, offsetY, movementX, movementY } = ev;
|
|
460
|
+
this._mousePosition = vec2.fromValues(offsetX, offsetY);
|
|
461
|
+
const width = window.innerWidth;
|
|
462
|
+
const height = window.innerHeight;
|
|
463
|
+
this._mousePosition2 = vec2.fromValues(
|
|
464
|
+
ev.clientX / width * 2 - 1,
|
|
465
|
+
-(ev.clientY / height) * 2 + 1
|
|
466
|
+
);
|
|
467
|
+
this.emit("mouse_move", {
|
|
468
|
+
...props,
|
|
469
|
+
buttons,
|
|
470
|
+
x: offsetX,
|
|
471
|
+
y: offsetY,
|
|
472
|
+
deltaX: movementX,
|
|
473
|
+
deltaY: movementY
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
_onWheel(ev) {
|
|
477
|
+
const props = this._getCommonEventProps(ev);
|
|
478
|
+
const { deltaX, deltaY, deltaZ } = ev;
|
|
479
|
+
this.emit("mouse_wheel", {
|
|
480
|
+
...props,
|
|
481
|
+
deltaX,
|
|
482
|
+
deltaY,
|
|
483
|
+
deltaZ
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
isKeyDown(keyCode) {
|
|
487
|
+
return this._keyStates[keyCode]?.state === 1 ? true : false;
|
|
488
|
+
}
|
|
489
|
+
getKeyState(keyCode) {
|
|
490
|
+
return this._keyStates[keyCode]?.state ?? 0;
|
|
491
|
+
}
|
|
492
|
+
isMouseButtonDown(mouseButton) {
|
|
493
|
+
return this._mouseButtonStates[mouseButton]?.state === 1 ? true : false;
|
|
494
|
+
}
|
|
495
|
+
getMouseButtonState(button) {
|
|
496
|
+
return this._mouseButtonStates[button]?.state ?? 0;
|
|
497
|
+
}
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
// src/math.ts
|
|
501
|
+
var DEG2RAD = 0.01745329;
|
|
502
|
+
function clamp(value, min, max) {
|
|
503
|
+
return Math.max(min, Math.min(max, value));
|
|
504
|
+
}
|
|
505
|
+
function deg2rad(value) {
|
|
506
|
+
return value * DEG2RAD;
|
|
507
|
+
}
|
|
508
|
+
function lerp(start, end, scale) {
|
|
509
|
+
return start * (1 - scale) + end * scale;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// src/misc.ts
|
|
513
|
+
function isNullOrUndefined(target) {
|
|
514
|
+
return target === null || target === void 0;
|
|
515
|
+
}
|
|
516
|
+
var getRedYellowGreenGradientHex = (min, max) => {
|
|
517
|
+
if (max === void 0) {
|
|
518
|
+
max = min;
|
|
519
|
+
min = 0;
|
|
520
|
+
}
|
|
521
|
+
if (max < 1) {
|
|
522
|
+
max = 1;
|
|
523
|
+
}
|
|
524
|
+
const percent = min / max;
|
|
525
|
+
const index = clamp(Math.floor(percent * 10), 0, 10);
|
|
526
|
+
return [
|
|
527
|
+
"#ff0000",
|
|
528
|
+
"#ff3300",
|
|
529
|
+
"#ff6600",
|
|
530
|
+
"#ff9900",
|
|
531
|
+
"#ffcc00",
|
|
532
|
+
"#ffff00",
|
|
533
|
+
"#ccff00",
|
|
534
|
+
"#99ff00",
|
|
535
|
+
"#66ff00",
|
|
536
|
+
"#33ff00",
|
|
537
|
+
"#00ff00"
|
|
538
|
+
][index];
|
|
539
|
+
};
|
|
540
|
+
|
|
541
|
+
// src/noise.ts
|
|
542
|
+
import { createNoise2D, createNoise3D, createNoise4D } from "simplex-noise";
|
|
543
|
+
|
|
544
|
+
// src/rng.ts
|
|
545
|
+
import { vec2 as vec22 } from "gl-matrix";
|
|
546
|
+
var Rng = class {
|
|
547
|
+
_seed = Date.now();
|
|
548
|
+
_uid_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
549
|
+
constructor() {
|
|
550
|
+
this._uid_characters = this.shuffle(this._uid_characters);
|
|
551
|
+
this.seed = this.nexti;
|
|
552
|
+
}
|
|
553
|
+
get seed() {
|
|
554
|
+
return this._seed;
|
|
555
|
+
}
|
|
556
|
+
set seed(value) {
|
|
557
|
+
this._seed = value;
|
|
558
|
+
}
|
|
559
|
+
get nextf() {
|
|
560
|
+
this._seed |= 0;
|
|
561
|
+
this._seed = this._seed + 2654435769 | 0;
|
|
562
|
+
let t = this._seed ^ this._seed >>> 16;
|
|
563
|
+
t = Math.imul(t, 569420461);
|
|
564
|
+
t = t ^ t >>> 15;
|
|
565
|
+
t = Math.imul(t, 1935289751);
|
|
566
|
+
return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
|
|
567
|
+
}
|
|
568
|
+
get nexti() {
|
|
569
|
+
return this.nextf * Number.MAX_SAFE_INTEGER | 0;
|
|
570
|
+
}
|
|
571
|
+
range(min, max) {
|
|
572
|
+
if (max === void 0) {
|
|
573
|
+
if (min !== 0) {
|
|
574
|
+
max = min;
|
|
575
|
+
min = 0;
|
|
576
|
+
} else {
|
|
577
|
+
max = Number.MAX_SAFE_INTEGER;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return this.nextf * (max - min + 1) + min | 0;
|
|
581
|
+
}
|
|
582
|
+
choose(...args) {
|
|
583
|
+
if (args.length === 1) {
|
|
584
|
+
if (Array.isArray(args[0])) {
|
|
585
|
+
return args[0][this.range(args[0].length - 1)];
|
|
586
|
+
} else if (typeof args[0] === "string") {
|
|
587
|
+
return args[0].charAt(this.range(args[0].length - 1));
|
|
588
|
+
} else {
|
|
589
|
+
return args[0];
|
|
590
|
+
}
|
|
591
|
+
} else {
|
|
592
|
+
return args[this.range(args.length - 1)];
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
shuffle(value) {
|
|
596
|
+
if (Array.isArray(value)) {
|
|
597
|
+
return value.sort(() => 0.5 - this.nextf);
|
|
598
|
+
} else {
|
|
599
|
+
return value.split("").sort(() => 0.5 - this.nextf).join("");
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
uid(length = 16) {
|
|
603
|
+
const uid = [];
|
|
604
|
+
for (let i = 0; i < length; i++) uid.push(this.choose(this._uid_characters));
|
|
605
|
+
return uid.join("");
|
|
606
|
+
}
|
|
607
|
+
randomMatrix(size) {
|
|
608
|
+
const rows = [];
|
|
609
|
+
for (let i = 0; i < size; i++) {
|
|
610
|
+
const row = [];
|
|
611
|
+
for (let j = 0; j < size; j++) {
|
|
612
|
+
row.push(this.nextf * 2 - 1);
|
|
613
|
+
}
|
|
614
|
+
rows.push(row);
|
|
615
|
+
}
|
|
616
|
+
return rows;
|
|
617
|
+
}
|
|
618
|
+
randomUnitVector() {
|
|
619
|
+
const theta = rng.nextf * 2 * Math.PI;
|
|
620
|
+
return vec22.fromValues(Math.cos(theta), Math.sin(theta));
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
var rng = new Rng();
|
|
624
|
+
|
|
625
|
+
// src/noise.ts
|
|
626
|
+
var Noise = class _Noise {
|
|
627
|
+
static _n2d;
|
|
628
|
+
static _n3d;
|
|
629
|
+
static _n4d;
|
|
630
|
+
static noise2d(x, y) {
|
|
631
|
+
if (!_Noise._n2d) {
|
|
632
|
+
_Noise._n2d = createNoise2D(() => rng.nextf);
|
|
633
|
+
}
|
|
634
|
+
return _Noise._n2d(x, y);
|
|
635
|
+
}
|
|
636
|
+
static noise3d(x, y, z) {
|
|
637
|
+
if (!_Noise._n3d) {
|
|
638
|
+
_Noise._n3d = createNoise3D(() => rng.nextf);
|
|
639
|
+
}
|
|
640
|
+
return _Noise._n3d(x, y, z);
|
|
641
|
+
}
|
|
642
|
+
static noise4d(x, y, z, w) {
|
|
643
|
+
if (!_Noise._n4d) {
|
|
644
|
+
_Noise._n4d = createNoise4D(() => rng.nextf);
|
|
645
|
+
}
|
|
646
|
+
return _Noise._n4d(x, y, z, w);
|
|
647
|
+
}
|
|
648
|
+
static fractal2d(x, y, fractalParams) {
|
|
649
|
+
const octaves = fractalParams.octaves;
|
|
650
|
+
const lacunarity = fractalParams.lacunarity;
|
|
651
|
+
const gain = 0.5;
|
|
652
|
+
let amplitude = fractalParams.amplitude;
|
|
653
|
+
let frequency = fractalParams.frequency;
|
|
654
|
+
let value = 0;
|
|
655
|
+
for (let i = 0; i < octaves; i++) {
|
|
656
|
+
value += amplitude * _Noise.noise2d(
|
|
657
|
+
frequency * x,
|
|
658
|
+
frequency * y
|
|
659
|
+
);
|
|
660
|
+
frequency *= lacunarity;
|
|
661
|
+
amplitude *= gain;
|
|
662
|
+
}
|
|
663
|
+
return value;
|
|
664
|
+
}
|
|
665
|
+
static fractal3d(x, y, z, fractalParams) {
|
|
666
|
+
const octaves = fractalParams.octaves;
|
|
667
|
+
const lacunarity = fractalParams.lacunarity;
|
|
668
|
+
const gain = 0.5;
|
|
669
|
+
let amplitude = fractalParams.amplitude;
|
|
670
|
+
let frequency = fractalParams.frequency;
|
|
671
|
+
let value = 0;
|
|
672
|
+
for (let i = 0; i < octaves; i++) {
|
|
673
|
+
value += amplitude * _Noise.noise3d(
|
|
674
|
+
frequency * x,
|
|
675
|
+
frequency * y,
|
|
676
|
+
frequency * z
|
|
677
|
+
);
|
|
678
|
+
frequency *= lacunarity;
|
|
679
|
+
amplitude *= gain;
|
|
680
|
+
}
|
|
681
|
+
return value;
|
|
682
|
+
}
|
|
683
|
+
};
|
|
684
|
+
|
|
685
|
+
// src/renderer/canvas-renderer.ts
|
|
686
|
+
import { vec2 as vec23 } from "gl-matrix";
|
|
687
|
+
var CanvasRenderer = class {
|
|
688
|
+
ctx;
|
|
689
|
+
clearColor = "#000000";
|
|
690
|
+
fontName = "monospace";
|
|
691
|
+
fontSize = 32;
|
|
692
|
+
textAlign = "center";
|
|
693
|
+
textBaseline = "middle";
|
|
694
|
+
get width() {
|
|
695
|
+
return this.ctx?.canvas.width ?? 0;
|
|
696
|
+
}
|
|
697
|
+
get height() {
|
|
698
|
+
return this.ctx?.canvas.height ?? 0;
|
|
699
|
+
}
|
|
700
|
+
get center() {
|
|
701
|
+
return vec23.fromValues(this.width / 2, this.height / 2);
|
|
702
|
+
}
|
|
703
|
+
constructor(canvas) {
|
|
704
|
+
this.ctx = (canvas ?? document.createElement("canvas")).getContext("2d");
|
|
705
|
+
this.ctx.canvas.style.backgroundColor = "#000000";
|
|
706
|
+
this.ctx.canvas.style.display = "block";
|
|
707
|
+
this.ctx.font = `${this.fontSize}px ${this.fontName}`;
|
|
708
|
+
this.ctx.textAlign = this.textAlign;
|
|
709
|
+
this.ctx.textBaseline = this.textBaseline;
|
|
710
|
+
this.resize();
|
|
711
|
+
this.clear();
|
|
712
|
+
}
|
|
713
|
+
appendTo(target, autoResize = true) {
|
|
714
|
+
if (this.ctx.canvas.parentElement) {
|
|
715
|
+
this.ctx.canvas.parentElement.removeChild(this.ctx.canvas);
|
|
716
|
+
}
|
|
717
|
+
if (target) {
|
|
718
|
+
target.appendChild(this.ctx.canvas);
|
|
719
|
+
if (autoResize) {
|
|
720
|
+
this.resize();
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
resize(displayWidth, displayHeight) {
|
|
725
|
+
let resized = false;
|
|
726
|
+
const { width, height } = this.ctx.canvas.parentElement?.getBoundingClientRect() ?? this.ctx.canvas.getBoundingClientRect();
|
|
727
|
+
displayWidth = 0 | (displayWidth ?? width);
|
|
728
|
+
displayHeight = 0 | (displayHeight ?? height);
|
|
729
|
+
if (this.width !== displayWidth || this.height !== displayHeight) {
|
|
730
|
+
this.ctx.canvas.width = displayWidth;
|
|
731
|
+
this.ctx.canvas.height = displayHeight;
|
|
732
|
+
resized = true;
|
|
733
|
+
}
|
|
734
|
+
return resized;
|
|
735
|
+
}
|
|
736
|
+
clear() {
|
|
737
|
+
this.ctx?.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
|
|
738
|
+
}
|
|
739
|
+
drawPoint(xy, params = {}) {
|
|
740
|
+
const [x, y] = xy;
|
|
741
|
+
const radius = params.radius || 1;
|
|
742
|
+
const fill = params.fill !== false;
|
|
743
|
+
const color = params.color || "#000000";
|
|
744
|
+
this.ctx.fillStyle = color;
|
|
745
|
+
this.ctx.strokeStyle = color;
|
|
746
|
+
this.ctx.beginPath();
|
|
747
|
+
this.ctx.arc(x, y, radius, 0, 2 * Math.PI);
|
|
748
|
+
this.ctx[fill ? "fill" : "stroke"]();
|
|
749
|
+
}
|
|
750
|
+
drawBox(xy, wh, params = {}) {
|
|
751
|
+
const [x, y] = xy;
|
|
752
|
+
const [w, h] = wh;
|
|
753
|
+
const fill = params.fill !== false;
|
|
754
|
+
const color = params.color || "#000000";
|
|
755
|
+
this.ctx.fillStyle = color;
|
|
756
|
+
this.ctx.strokeStyle = color;
|
|
757
|
+
this.ctx.beginPath();
|
|
758
|
+
this.ctx.moveTo(x, y);
|
|
759
|
+
this.ctx.lineTo(x + w, y);
|
|
760
|
+
this.ctx.lineTo(x + w, y + h);
|
|
761
|
+
this.ctx.lineTo(x, y + h);
|
|
762
|
+
this.ctx.closePath();
|
|
763
|
+
this.ctx[fill ? "fill" : "stroke"]();
|
|
764
|
+
}
|
|
765
|
+
drawLine(x1y1x2y2, color = "#000000") {
|
|
766
|
+
const [x1, y1, x2, y2] = x1y1x2y2;
|
|
767
|
+
this.ctx.strokeStyle = color;
|
|
768
|
+
this.ctx.beginPath();
|
|
769
|
+
this.ctx.moveTo(x1, y1);
|
|
770
|
+
this.ctx.lineTo(x2, y2);
|
|
771
|
+
this.ctx.stroke();
|
|
772
|
+
}
|
|
773
|
+
drawText(text, xy, color = "#ffffff", size = this.fontSize, font = this.fontName) {
|
|
774
|
+
const [x, y] = xy;
|
|
775
|
+
this.ctx.fillStyle = color;
|
|
776
|
+
this.ctx.font = `${size}px ${font}`;
|
|
777
|
+
this.ctx.textAlign = this.textAlign;
|
|
778
|
+
this.ctx.textBaseline = this.textBaseline;
|
|
779
|
+
this.ctx.fillText(text, x, y + 0.5);
|
|
780
|
+
}
|
|
781
|
+
// drawNamePlate(name: string, title: string, color = '#bbbbbb', s: number = 12, tile_size: number = 32) {
|
|
782
|
+
// let x = (actor.position.x * tile_size);
|
|
783
|
+
// let y = (actor.position.y * tile_size) - 18;
|
|
784
|
+
// let healthPercent = (actor.health / actor.maxHealth);
|
|
785
|
+
// let healthBarWidth = (healthPercent * tile_size)
|
|
786
|
+
// let healthColor = getRedYellowGreenGradientHex(actor.health, actor.maxHealth);
|
|
787
|
+
// this.drawBox(x, y, tile_size, 3, { fill: true, color: '#666666' });
|
|
788
|
+
// this.drawBox(x, y, healthBarWidth, 3, { fill: true, color: healthColor });
|
|
789
|
+
// if (actor.title) {
|
|
790
|
+
// this.drawBox(x, y - 16, actor.name.length * (this.fontSize / 5), 10, { fill: true, color: '#000000' });
|
|
791
|
+
// this.drawText(actor.name || '', x, y - 16, color, s);
|
|
792
|
+
// this.drawBox(x, y - 7, actor.title.length * (this.fontSize / 5), 10, { fill: true, color: '#000000' });
|
|
793
|
+
// this.drawText(`<${actor.title}>`, x, y - 6, '#00FFFF', s);
|
|
794
|
+
// } else {
|
|
795
|
+
// this.drawBox(x, y - 6, actor.name.length * (this.fontSize / 5), 10, { fill: true, color: '#000000' });
|
|
796
|
+
// this.drawText(actor.name || '', x, y - 6, color, s);
|
|
797
|
+
// }
|
|
798
|
+
// }
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
// src/renderer/webgl.ts
|
|
802
|
+
var ShaderType = ((ShaderType2) => {
|
|
803
|
+
ShaderType2[ShaderType2["VERTEX"] = WebGL2RenderingContext.VERTEX_SHADER] = "VERTEX";
|
|
804
|
+
ShaderType2[ShaderType2["FRAGMENT"] = WebGL2RenderingContext.FRAGMENT_SHADER] = "FRAGMENT";
|
|
805
|
+
return ShaderType2;
|
|
806
|
+
})(ShaderType || {});
|
|
807
|
+
function CreateWebGlContext(canvas) {
|
|
808
|
+
return (canvas ?? document.createElement("canvas")).getContext("webgl2");
|
|
809
|
+
}
|
|
810
|
+
function ResizeWebGlContext(gl, displayWidth, displayHeight) {
|
|
811
|
+
const canvas = gl.canvas;
|
|
812
|
+
const { width, height } = canvas.parentElement?.getBoundingClientRect() ?? canvas.getBoundingClientRect();
|
|
813
|
+
displayWidth = 0 | (displayWidth ?? width);
|
|
814
|
+
displayHeight = 0 | (displayHeight ?? height);
|
|
815
|
+
if (canvas.width !== displayWidth || canvas.height !== displayHeight) {
|
|
816
|
+
gl.canvas.width = displayWidth;
|
|
817
|
+
gl.canvas.height = displayHeight;
|
|
818
|
+
gl.viewport(0, 0, displayWidth, displayHeight);
|
|
819
|
+
return true;
|
|
820
|
+
}
|
|
821
|
+
return false;
|
|
822
|
+
}
|
|
823
|
+
function CompileShader(gl, type, source) {
|
|
824
|
+
const shader = gl.createShader(type);
|
|
825
|
+
gl.shaderSource(shader, source);
|
|
826
|
+
gl.compileShader(shader);
|
|
827
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
828
|
+
throw `CompileShader: failed to compile shader: ${gl.getShaderInfoLog(shader)}`;
|
|
829
|
+
}
|
|
830
|
+
return shader;
|
|
831
|
+
}
|
|
832
|
+
function CreateProgram(gl, vertexShader, fragmentShader) {
|
|
833
|
+
if (typeof vertexShader === "string") {
|
|
834
|
+
vertexShader = CompileShader(gl, ShaderType.VERTEX, vertexShader);
|
|
835
|
+
}
|
|
836
|
+
if (typeof fragmentShader === "string") {
|
|
837
|
+
fragmentShader = CompileShader(gl, ShaderType.FRAGMENT, fragmentShader);
|
|
838
|
+
}
|
|
839
|
+
const program = gl.createProgram();
|
|
840
|
+
gl.attachShader(program, vertexShader);
|
|
841
|
+
gl.attachShader(program, fragmentShader);
|
|
842
|
+
gl.linkProgram(program);
|
|
843
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
844
|
+
throw `CreateProgram: failed to link program: ${gl.getProgramInfoLog(program)}`;
|
|
845
|
+
}
|
|
846
|
+
return program;
|
|
847
|
+
}
|
|
848
|
+
function GetProgramLocations(gl, program) {
|
|
849
|
+
const attributes = {};
|
|
850
|
+
const attributeCount = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
|
|
851
|
+
for (let i = 0; i < attributeCount; i++) {
|
|
852
|
+
const info = gl.getActiveAttrib(program, i);
|
|
853
|
+
attributes[info.name] = gl.getAttribLocation(program, info.name);
|
|
854
|
+
}
|
|
855
|
+
const uniforms = {};
|
|
856
|
+
const uniformCount = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
|
|
857
|
+
for (let i = 0; i < uniformCount; i++) {
|
|
858
|
+
const info = gl.getActiveUniform(program, i);
|
|
859
|
+
uniforms[info.name] = gl.getUniformLocation(program, info.name);
|
|
860
|
+
}
|
|
861
|
+
return { attributes, uniforms };
|
|
862
|
+
}
|
|
863
|
+
function CreateProgramInfo(gl, vertexShaderSource, fragmentShaderSource) {
|
|
864
|
+
const program = CreateProgram(
|
|
865
|
+
gl,
|
|
866
|
+
vertexShaderSource,
|
|
867
|
+
fragmentShaderSource
|
|
868
|
+
);
|
|
869
|
+
const locations = GetProgramLocations(gl, program);
|
|
870
|
+
return { program, ...locations };
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// src/renderer/webgl-renderer.ts
|
|
874
|
+
var WebGlRenderer = class {
|
|
875
|
+
gl;
|
|
876
|
+
programInfo;
|
|
877
|
+
get canvas() {
|
|
878
|
+
return this.gl.canvas;
|
|
879
|
+
}
|
|
880
|
+
get width() {
|
|
881
|
+
return this.gl.canvas.width;
|
|
882
|
+
}
|
|
883
|
+
get height() {
|
|
884
|
+
return this.gl.canvas.height;
|
|
885
|
+
}
|
|
886
|
+
constructor() {
|
|
887
|
+
this.gl = CreateWebGlContext();
|
|
888
|
+
this.gl.enable(this.gl.DEPTH_TEST);
|
|
889
|
+
}
|
|
890
|
+
appendTo(htmlElement) {
|
|
891
|
+
if (this.canvas.parentElement) {
|
|
892
|
+
this.canvas.parentElement.removeChild(this.canvas);
|
|
893
|
+
}
|
|
894
|
+
if (htmlElement) {
|
|
895
|
+
htmlElement.appendChild(this.canvas);
|
|
896
|
+
this.resize();
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
resize(displayWidth, displayHeight) {
|
|
900
|
+
const { width, height } = this.canvas.parentElement?.getBoundingClientRect() ?? this.canvas.getBoundingClientRect();
|
|
901
|
+
displayWidth = 0 | (displayWidth ?? width);
|
|
902
|
+
displayHeight = 0 | (displayHeight ?? height);
|
|
903
|
+
if (this.width !== displayWidth || this.height !== displayHeight) {
|
|
904
|
+
this.gl.canvas.width = displayWidth;
|
|
905
|
+
this.gl.canvas.height = displayHeight;
|
|
906
|
+
this.gl.viewport(0, 0, displayWidth, displayHeight);
|
|
907
|
+
return true;
|
|
908
|
+
}
|
|
909
|
+
return false;
|
|
910
|
+
}
|
|
911
|
+
render(_deltaTime) {
|
|
912
|
+
if (this.programInfo) {
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
};
|
|
916
|
+
|
|
917
|
+
// src/simplex-noise.ts
|
|
918
|
+
var SimplexNoise = class {
|
|
919
|
+
grad3;
|
|
920
|
+
p;
|
|
921
|
+
perm;
|
|
922
|
+
constructor() {
|
|
923
|
+
this.grad3 = [
|
|
924
|
+
[1, 1, 0],
|
|
925
|
+
[-1, 1, 0],
|
|
926
|
+
[1, -1, 0],
|
|
927
|
+
[-1, -1, 0],
|
|
928
|
+
[1, 0, 1],
|
|
929
|
+
[-1, 0, 1],
|
|
930
|
+
[1, 0, -1],
|
|
931
|
+
[-1, 0, -1],
|
|
932
|
+
[0, 1, 1],
|
|
933
|
+
[0, -1, 1],
|
|
934
|
+
[0, 1, -1],
|
|
935
|
+
[0, -1, -1]
|
|
936
|
+
];
|
|
937
|
+
this.p = [];
|
|
938
|
+
for (let i = 0; i < 256; i++) {
|
|
939
|
+
this.p.push(Math.floor(rng.nextf * 256));
|
|
940
|
+
}
|
|
941
|
+
this.perm = [];
|
|
942
|
+
for (let i = 0; i < 512; i++) {
|
|
943
|
+
this.perm.push(this.p[i & 255]);
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
dot(g, x, y, z) {
|
|
947
|
+
return g[0] * x + g[1] * y + (typeof z === "number" ? g[2] * z : 0);
|
|
948
|
+
}
|
|
949
|
+
noise2d(xin, yin) {
|
|
950
|
+
let n0, n1, n2;
|
|
951
|
+
const F2 = 0.5 * (Math.sqrt(3) - 1);
|
|
952
|
+
const s = (xin + yin) * F2;
|
|
953
|
+
let i = Math.floor(xin + s);
|
|
954
|
+
let j = Math.floor(yin + s);
|
|
955
|
+
const G2 = (3 - Math.sqrt(3)) / 6;
|
|
956
|
+
const t = (i + j) * G2;
|
|
957
|
+
const X0 = i - t;
|
|
958
|
+
const Y0 = j - t;
|
|
959
|
+
const x0 = xin - X0;
|
|
960
|
+
const y0 = yin - Y0;
|
|
961
|
+
let i1, j1;
|
|
962
|
+
if (x0 > y0) {
|
|
963
|
+
i1 = 1;
|
|
964
|
+
j1 = 0;
|
|
965
|
+
} else {
|
|
966
|
+
i1 = 0;
|
|
967
|
+
j1 = 1;
|
|
968
|
+
}
|
|
969
|
+
const x1 = x0 - i1 + G2;
|
|
970
|
+
const y1 = y0 - j1 + G2;
|
|
971
|
+
const x2 = x0 - 1 + 2 * G2;
|
|
972
|
+
const y2 = y0 - 1 + 2 * G2;
|
|
973
|
+
const ii = i & 255;
|
|
974
|
+
const jj = j & 255;
|
|
975
|
+
const gi0 = this.perm[ii + this.perm[jj]] % 12;
|
|
976
|
+
const gi1 = this.perm[ii + i1 + this.perm[jj + j1]] % 12;
|
|
977
|
+
const gi2 = this.perm[ii + 1 + this.perm[jj + 1]] % 12;
|
|
978
|
+
let t0 = 0.5 - x0 * x0 - y0 * y0;
|
|
979
|
+
if (t0 < 0) {
|
|
980
|
+
n0 = 0;
|
|
981
|
+
} else {
|
|
982
|
+
t0 *= t0;
|
|
983
|
+
n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0);
|
|
984
|
+
}
|
|
985
|
+
let t1 = 0.5 - x1 * x1 - y1 * y1;
|
|
986
|
+
if (t1 < 0) {
|
|
987
|
+
n1 = 0;
|
|
988
|
+
} else {
|
|
989
|
+
t1 *= t1;
|
|
990
|
+
n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1);
|
|
991
|
+
}
|
|
992
|
+
let t2 = 0.5 - x2 * x2 - y2 * y2;
|
|
993
|
+
if (t2 < 0) {
|
|
994
|
+
n2 = 0;
|
|
995
|
+
} else {
|
|
996
|
+
t2 *= t2;
|
|
997
|
+
n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2);
|
|
998
|
+
}
|
|
999
|
+
return 70 * (n0 + n1 + n2);
|
|
1000
|
+
}
|
|
1001
|
+
// 3D simplex noise
|
|
1002
|
+
noise3d(xin, yin, zin) {
|
|
1003
|
+
let n0, n1, n2, n3;
|
|
1004
|
+
const F3 = 1 / 3;
|
|
1005
|
+
const s = (xin + yin + zin) * F3;
|
|
1006
|
+
const i = Math.floor(xin + s);
|
|
1007
|
+
const j = Math.floor(yin + s);
|
|
1008
|
+
const k = Math.floor(zin + s);
|
|
1009
|
+
const G3 = 1 / 6;
|
|
1010
|
+
const t = (i + j + k) * G3;
|
|
1011
|
+
const X0 = i - t;
|
|
1012
|
+
const Y0 = j - t;
|
|
1013
|
+
const Z0 = k - t;
|
|
1014
|
+
const x0 = xin - X0;
|
|
1015
|
+
const y0 = yin - Y0;
|
|
1016
|
+
const z0 = zin - Z0;
|
|
1017
|
+
let i1, j1, k1;
|
|
1018
|
+
let i2, j2, k2;
|
|
1019
|
+
if (x0 >= y0) {
|
|
1020
|
+
if (y0 >= z0) {
|
|
1021
|
+
i1 = 1;
|
|
1022
|
+
j1 = 0;
|
|
1023
|
+
k1 = 0;
|
|
1024
|
+
i2 = 1;
|
|
1025
|
+
j2 = 1;
|
|
1026
|
+
k2 = 0;
|
|
1027
|
+
} else if (x0 >= z0) {
|
|
1028
|
+
i1 = 1;
|
|
1029
|
+
j1 = 0;
|
|
1030
|
+
k1 = 0;
|
|
1031
|
+
i2 = 1;
|
|
1032
|
+
j2 = 0;
|
|
1033
|
+
k2 = 1;
|
|
1034
|
+
} else {
|
|
1035
|
+
i1 = 0;
|
|
1036
|
+
j1 = 0;
|
|
1037
|
+
k1 = 1;
|
|
1038
|
+
i2 = 1;
|
|
1039
|
+
j2 = 0;
|
|
1040
|
+
k2 = 1;
|
|
1041
|
+
}
|
|
1042
|
+
} else {
|
|
1043
|
+
if (y0 < z0) {
|
|
1044
|
+
i1 = 0;
|
|
1045
|
+
j1 = 0;
|
|
1046
|
+
k1 = 1;
|
|
1047
|
+
i2 = 0;
|
|
1048
|
+
j2 = 1;
|
|
1049
|
+
k2 = 1;
|
|
1050
|
+
} else if (x0 < z0) {
|
|
1051
|
+
i1 = 0;
|
|
1052
|
+
j1 = 1;
|
|
1053
|
+
k1 = 0;
|
|
1054
|
+
i2 = 0;
|
|
1055
|
+
j2 = 1;
|
|
1056
|
+
k2 = 1;
|
|
1057
|
+
} else {
|
|
1058
|
+
i1 = 0;
|
|
1059
|
+
j1 = 1;
|
|
1060
|
+
k1 = 0;
|
|
1061
|
+
i2 = 1;
|
|
1062
|
+
j2 = 1;
|
|
1063
|
+
k2 = 0;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
const x1 = x0 - i1 + G3;
|
|
1067
|
+
const y1 = y0 - j1 + G3;
|
|
1068
|
+
const z1 = z0 - k1 + G3;
|
|
1069
|
+
const x2 = x0 - i2 + 2 * G3;
|
|
1070
|
+
const y2 = y0 - j2 + 2 * G3;
|
|
1071
|
+
const z2 = z0 - k2 + 2 * G3;
|
|
1072
|
+
const x3 = x0 - 1 + 3 * G3;
|
|
1073
|
+
const y3 = y0 - 1 + 3 * G3;
|
|
1074
|
+
const z3 = z0 - 1 + 3 * G3;
|
|
1075
|
+
const ii = i & 255;
|
|
1076
|
+
const jj = j & 255;
|
|
1077
|
+
const kk = k & 255;
|
|
1078
|
+
const gi0 = this.perm[ii + this.perm[jj + this.perm[kk]]] % 12;
|
|
1079
|
+
const gi1 = this.perm[ii + i1 + this.perm[jj + j1 + this.perm[kk + k1]]] % 12;
|
|
1080
|
+
const gi2 = this.perm[ii + i2 + this.perm[jj + j2 + this.perm[kk + k2]]] % 12;
|
|
1081
|
+
const gi3 = this.perm[ii + 1 + this.perm[jj + 1 + this.perm[kk + 1]]] % 12;
|
|
1082
|
+
let t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
|
|
1083
|
+
if (t0 < 0) {
|
|
1084
|
+
n0 = 0;
|
|
1085
|
+
} else {
|
|
1086
|
+
t0 *= t0;
|
|
1087
|
+
n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0, z0);
|
|
1088
|
+
}
|
|
1089
|
+
let t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
|
|
1090
|
+
if (t1 < 0) {
|
|
1091
|
+
n1 = 0;
|
|
1092
|
+
} else {
|
|
1093
|
+
t1 *= t1;
|
|
1094
|
+
n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1, z1);
|
|
1095
|
+
}
|
|
1096
|
+
let t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
|
|
1097
|
+
if (t2 < 0) {
|
|
1098
|
+
n2 = 0;
|
|
1099
|
+
} else {
|
|
1100
|
+
t2 *= t2;
|
|
1101
|
+
n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2, z2);
|
|
1102
|
+
}
|
|
1103
|
+
let t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
|
|
1104
|
+
if (t3 < 0) {
|
|
1105
|
+
n3 = 0;
|
|
1106
|
+
} else {
|
|
1107
|
+
t3 *= t3;
|
|
1108
|
+
n3 = t3 * t3 * this.dot(this.grad3[gi3], x3, y3, z3);
|
|
1109
|
+
}
|
|
1110
|
+
return 32 * (n0 + n1 + n2 + n3);
|
|
1111
|
+
}
|
|
1112
|
+
};
|
|
1113
|
+
|
|
1114
|
+
// src/three/cube-sphere-geometry.ts
|
|
1115
|
+
import { BoxGeometry, Vector3 } from "three";
|
|
1116
|
+
var CubeSphereGeometry = class extends BoxGeometry {
|
|
1117
|
+
constructor(size, segments) {
|
|
1118
|
+
size = Math.floor(size);
|
|
1119
|
+
segments = Math.floor(segments);
|
|
1120
|
+
super(size, size, size, segments, segments, segments);
|
|
1121
|
+
this.size = size;
|
|
1122
|
+
this.segments = segments;
|
|
1123
|
+
this._halfSize = this.size / 2;
|
|
1124
|
+
const attrPositions = this.attributes.position;
|
|
1125
|
+
const position = new Vector3();
|
|
1126
|
+
for (let i = 0; i < attrPositions.count; i++) {
|
|
1127
|
+
position.fromBufferAttribute(attrPositions, i);
|
|
1128
|
+
position.normalize().multiplyScalar(this._halfSize);
|
|
1129
|
+
attrPositions.setXYZ(i, position.x, position.y, position.z);
|
|
1130
|
+
}
|
|
1131
|
+
attrPositions.needsUpdate = true;
|
|
1132
|
+
}
|
|
1133
|
+
_halfSize;
|
|
1134
|
+
applyFractalBrownianMotion(octaves, persistence, scale, amplitude) {
|
|
1135
|
+
const attrPositions = this.attributes.position;
|
|
1136
|
+
const position = new Vector3();
|
|
1137
|
+
for (let i = 0; i < attrPositions.count; i++) {
|
|
1138
|
+
position.fromBufferAttribute(attrPositions, i);
|
|
1139
|
+
position.normalize().multiplyScalar(this._halfSize);
|
|
1140
|
+
const noiseValue = this.fBm(position.x * scale, position.y * scale, position.z * scale, octaves, persistence);
|
|
1141
|
+
position.addScaledVector(position.clone().normalize(), noiseValue * amplitude);
|
|
1142
|
+
attrPositions.setXYZ(i, position.x, position.y, position.z);
|
|
1143
|
+
}
|
|
1144
|
+
attrPositions.needsUpdate = true;
|
|
1145
|
+
}
|
|
1146
|
+
fBm(x, y, z, octaves, persistence) {
|
|
1147
|
+
let total = 0;
|
|
1148
|
+
let frequency = 1;
|
|
1149
|
+
let amplitude = 1;
|
|
1150
|
+
let maxValue = 0;
|
|
1151
|
+
for (let i = 0; i < octaves; i++) {
|
|
1152
|
+
total += Noise.noise3d(x * frequency, y * frequency, z * frequency) * amplitude;
|
|
1153
|
+
maxValue += amplitude;
|
|
1154
|
+
amplitude *= persistence;
|
|
1155
|
+
frequency *= 2;
|
|
1156
|
+
}
|
|
1157
|
+
return total / maxValue;
|
|
1158
|
+
}
|
|
1159
|
+
};
|
|
1160
|
+
|
|
1161
|
+
// src/three/texture-atlas.ts
|
|
1162
|
+
import { TextureLoader, MeshLambertMaterial, NearestFilter } from "three";
|
|
1163
|
+
function LoadTexture(url) {
|
|
1164
|
+
return new Promise((res, rej) => {
|
|
1165
|
+
new TextureLoader().load(
|
|
1166
|
+
url,
|
|
1167
|
+
(texture) => {
|
|
1168
|
+
const width = texture.source.data.width;
|
|
1169
|
+
const height = texture.source.data.height;
|
|
1170
|
+
res({ width, height, texture });
|
|
1171
|
+
},
|
|
1172
|
+
(_ev) => {
|
|
1173
|
+
},
|
|
1174
|
+
(err) => rej(err)
|
|
1175
|
+
);
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1178
|
+
var TextureAtlas = class _TextureAtlas extends MeshLambertMaterial {
|
|
1179
|
+
constructor(textureData, textureWidth, textureHeight = textureWidth, params) {
|
|
1180
|
+
super({
|
|
1181
|
+
map: textureData.texture,
|
|
1182
|
+
alphaTest: 0.1,
|
|
1183
|
+
transparent: true,
|
|
1184
|
+
...params ?? {}
|
|
1185
|
+
});
|
|
1186
|
+
this.textureData = textureData;
|
|
1187
|
+
this.textureWidth = textureWidth;
|
|
1188
|
+
this.textureHeight = textureHeight;
|
|
1189
|
+
this._uw = this.textureWidth / this.textureData.width;
|
|
1190
|
+
this._uh = this.textureHeight / this.textureData.height;
|
|
1191
|
+
this.textureData.texture.magFilter = NearestFilter;
|
|
1192
|
+
}
|
|
1193
|
+
_uw;
|
|
1194
|
+
_uh;
|
|
1195
|
+
getUv(voxel, ux, uy) {
|
|
1196
|
+
return [
|
|
1197
|
+
(voxel + ux) * this._uw,
|
|
1198
|
+
1 - (1 - uy) * this._uh
|
|
1199
|
+
];
|
|
1200
|
+
}
|
|
1201
|
+
static CreateFromUrl(url, textureWidth, textureHeight = textureWidth, params) {
|
|
1202
|
+
return LoadTexture(url).then((textureData) => new _TextureAtlas(
|
|
1203
|
+
textureData,
|
|
1204
|
+
textureWidth,
|
|
1205
|
+
textureHeight,
|
|
1206
|
+
params
|
|
1207
|
+
));
|
|
1208
|
+
}
|
|
1209
|
+
};
|
|
1210
|
+
|
|
1211
|
+
// src/three/threejs-boiler-plate.ts
|
|
1212
|
+
import { AmbientLight, BoxGeometry as BoxGeometry2, DirectionalLight, GridHelper, Mesh, MeshLambertMaterial as MeshLambertMaterial2, PlaneGeometry, Raycaster, Scene, Vector2, WebGLRenderer } from "three";
|
|
1213
|
+
|
|
1214
|
+
// src/three/threejs-camera-rig.ts
|
|
1215
|
+
import { PerspectiveCamera, Group } from "three";
|
|
1216
|
+
var ThreeJsCameraRig = class extends Group {
|
|
1217
|
+
gimbal;
|
|
1218
|
+
camera;
|
|
1219
|
+
target;
|
|
1220
|
+
mouseSensitivity = 0.01;
|
|
1221
|
+
wheelSensitivity = 0.02;
|
|
1222
|
+
minCameraDistance = 2;
|
|
1223
|
+
maxCameraDistance = 100;
|
|
1224
|
+
minTiltAngle = deg2rad(-90);
|
|
1225
|
+
maxTiltAngle = deg2rad(0);
|
|
1226
|
+
clampTiltAngle = false;
|
|
1227
|
+
constructor(params) {
|
|
1228
|
+
super();
|
|
1229
|
+
this.gimbal = new Group();
|
|
1230
|
+
this.add(this.gimbal);
|
|
1231
|
+
this.camera = new PerspectiveCamera(
|
|
1232
|
+
params?.fov ?? 75,
|
|
1233
|
+
params?.aspect ?? 2,
|
|
1234
|
+
params?.near ?? 0.1,
|
|
1235
|
+
params?.far ?? 1e3
|
|
1236
|
+
);
|
|
1237
|
+
this.gimbal.add(this.camera);
|
|
1238
|
+
}
|
|
1239
|
+
orbit(deltaX, deltaY) {
|
|
1240
|
+
(this.target || this).rotateY(-deltaX * this.mouseSensitivity);
|
|
1241
|
+
this.gimbal.rotation.x = this.clampTiltAngle ? clamp(
|
|
1242
|
+
this.gimbal.rotation.x + -deltaY * this.mouseSensitivity,
|
|
1243
|
+
this.minTiltAngle,
|
|
1244
|
+
this.maxTiltAngle
|
|
1245
|
+
) : this.gimbal.rotation.x + -deltaY * this.mouseSensitivity;
|
|
1246
|
+
}
|
|
1247
|
+
dolly(deltaY) {
|
|
1248
|
+
this.camera.position.z = clamp(
|
|
1249
|
+
this.camera.position.z + deltaY * this.wheelSensitivity,
|
|
1250
|
+
this.minCameraDistance,
|
|
1251
|
+
this.maxCameraDistance
|
|
1252
|
+
);
|
|
1253
|
+
}
|
|
1254
|
+
};
|
|
1255
|
+
|
|
1256
|
+
// src/three/threejs-boiler-plate.ts
|
|
1257
|
+
var ThreeJsBoilerPlate = class {
|
|
1258
|
+
clock = new Clock();
|
|
1259
|
+
scene = new Scene();
|
|
1260
|
+
input = new Input();
|
|
1261
|
+
raycaster = new Raycaster();
|
|
1262
|
+
renderer;
|
|
1263
|
+
cameraRig;
|
|
1264
|
+
get canvas() {
|
|
1265
|
+
return this.renderer.domElement;
|
|
1266
|
+
}
|
|
1267
|
+
get camera() {
|
|
1268
|
+
return this.cameraRig.camera;
|
|
1269
|
+
}
|
|
1270
|
+
rng = rng;
|
|
1271
|
+
constructor(params) {
|
|
1272
|
+
rng.seed = params?.seed ?? rng.seed;
|
|
1273
|
+
this.renderer = new WebGLRenderer(params?.renderer);
|
|
1274
|
+
this.cameraRig = new ThreeJsCameraRig(params?.camera);
|
|
1275
|
+
this.scene.add(this.cameraRig);
|
|
1276
|
+
GlobalEmitter.on("mouse_move", ({ deltaX, deltaY }) => {
|
|
1277
|
+
if (this.input.isMouseButtonDown(0)) {
|
|
1278
|
+
this.cameraRig.orbit(deltaX, deltaY);
|
|
1279
|
+
}
|
|
1280
|
+
}).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
|
|
1281
|
+
if (params?.parentElement) {
|
|
1282
|
+
this.appendTo(params.parentElement);
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
appendTo(htmlElement) {
|
|
1286
|
+
if (this.canvas.parentElement) {
|
|
1287
|
+
this.canvas.parentElement.removeChild(this.canvas);
|
|
1288
|
+
}
|
|
1289
|
+
if (htmlElement) {
|
|
1290
|
+
htmlElement.appendChild(this.canvas);
|
|
1291
|
+
this.resize();
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
resize(displayWidth, displayHeight) {
|
|
1295
|
+
const { width, height } = this.canvas.parentElement?.getBoundingClientRect() ?? this.canvas.getBoundingClientRect();
|
|
1296
|
+
displayWidth = 0 | (displayWidth ?? width);
|
|
1297
|
+
displayHeight = 0 | (displayHeight ?? height);
|
|
1298
|
+
let resized = false;
|
|
1299
|
+
if (this.canvas.width !== displayWidth || this.canvas.height !== displayHeight) {
|
|
1300
|
+
this.renderer.setSize(displayWidth, displayHeight);
|
|
1301
|
+
this.cameraRig.camera.aspect = displayWidth / displayHeight;
|
|
1302
|
+
this.cameraRig.camera.updateProjectionMatrix();
|
|
1303
|
+
resized = true;
|
|
1304
|
+
}
|
|
1305
|
+
return resized;
|
|
1306
|
+
}
|
|
1307
|
+
setupBasicScene(params = {}) {
|
|
1308
|
+
this.camera.position.z = params.cameraDistance ?? 5;
|
|
1309
|
+
if (params.ambientLight !== false) this.scene.add(new AmbientLight());
|
|
1310
|
+
if (params.directionalLight !== false) this.scene.add(new DirectionalLight());
|
|
1311
|
+
if (params.gridHelper !== false) this.scene.add(new GridHelper(100, 100, 16711680));
|
|
1312
|
+
}
|
|
1313
|
+
pick() {
|
|
1314
|
+
const pickX = this.input.mousePosition[0] / this.renderer.domElement.width * 2 - 1;
|
|
1315
|
+
const pickY = -(this.input.mousePosition[1] / this.renderer.domElement.height) * 2 + 1;
|
|
1316
|
+
this.raycaster.setFromCamera(new Vector2(pickX, pickY), this.cameraRig.camera);
|
|
1317
|
+
const intersected = this.raycaster.intersectObjects(this.scene.children);
|
|
1318
|
+
return intersected.length ? intersected[0] : null;
|
|
1319
|
+
}
|
|
1320
|
+
static CreateCubeMesh(size = 1, color = 16711680) {
|
|
1321
|
+
const cubeMesh = new Mesh(
|
|
1322
|
+
new BoxGeometry2(size, size, size),
|
|
1323
|
+
new MeshLambertMaterial2({ color })
|
|
1324
|
+
);
|
|
1325
|
+
cubeMesh.name = "CubeMesh";
|
|
1326
|
+
return cubeMesh;
|
|
1327
|
+
}
|
|
1328
|
+
static CreatePlaneMesh(size = 10, segments = 10, color = 16711680) {
|
|
1329
|
+
return new Mesh(
|
|
1330
|
+
new PlaneGeometry(size, size, segments, segments),
|
|
1331
|
+
new MeshLambertMaterial2({ color, wireframe: true })
|
|
1332
|
+
);
|
|
1333
|
+
}
|
|
1334
|
+
};
|
|
1335
|
+
|
|
1336
|
+
// src/three/threejs-player-controller.ts
|
|
1337
|
+
import { Group as Group2, Vector3 as Vector32 } from "three";
|
|
1338
|
+
var ThreeJsPlayerController = class extends Group2 {
|
|
1339
|
+
moveSpeed = 1;
|
|
1340
|
+
cameraRig;
|
|
1341
|
+
velocity;
|
|
1342
|
+
constructor(camera) {
|
|
1343
|
+
super();
|
|
1344
|
+
this.velocity = new Vector32();
|
|
1345
|
+
this.cameraRig = new ThreeJsCameraRig(camera);
|
|
1346
|
+
this.add(this.cameraRig);
|
|
1347
|
+
}
|
|
1348
|
+
update(deltaTime) {
|
|
1349
|
+
if (this.velocity.length()) {
|
|
1350
|
+
this.translateOnAxis(this.velocity.normalize(), this.moveSpeed * deltaTime);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
};
|
|
1354
|
+
|
|
1355
|
+
// src/three/voxel-geometry.ts
|
|
1356
|
+
import { BufferAttribute, BufferGeometry } from "three";
|
|
1357
|
+
function xyz2key(x, y, z) {
|
|
1358
|
+
return `${x},${y},${z}`;
|
|
1359
|
+
}
|
|
1360
|
+
function key2xyz(key) {
|
|
1361
|
+
return key.split(",").map((v) => parseFloat(v));
|
|
1362
|
+
}
|
|
1363
|
+
var VoxelGeometry = class extends BufferGeometry {
|
|
1364
|
+
_voxels = /* @__PURE__ */ new Map();
|
|
1365
|
+
colors = [
|
|
1366
|
+
[1, 0, 0, 1],
|
|
1367
|
+
// Red
|
|
1368
|
+
[0, 1, 0, 1],
|
|
1369
|
+
// Green
|
|
1370
|
+
[0, 0, 1, 1]
|
|
1371
|
+
// Blue
|
|
1372
|
+
];
|
|
1373
|
+
constructor() {
|
|
1374
|
+
super();
|
|
1375
|
+
const size = 1;
|
|
1376
|
+
for (let x = -size; x <= size; x++) {
|
|
1377
|
+
for (let y = -size; y <= size; y++) {
|
|
1378
|
+
for (let z = -size; z <= size; z++) {
|
|
1379
|
+
this._voxels.set(xyz2key(x, y, z), 2);
|
|
1380
|
+
}
|
|
1381
|
+
}
|
|
1382
|
+
}
|
|
1383
|
+
this.updateGeometry();
|
|
1384
|
+
}
|
|
1385
|
+
get(x, y, z) {
|
|
1386
|
+
return this._voxels.get(xyz2key(x, y, z));
|
|
1387
|
+
}
|
|
1388
|
+
set(x, y, z, value) {
|
|
1389
|
+
this._voxels.set(xyz2key(x, y, z), value);
|
|
1390
|
+
}
|
|
1391
|
+
getVoxelCoordsFromFaceIndex(faceIndex, size, height) {
|
|
1392
|
+
const facePerVoxel = 6;
|
|
1393
|
+
const triangleIndex = Math.floor(faceIndex / 2);
|
|
1394
|
+
const voxelIndex = Math.floor(triangleIndex / facePerVoxel);
|
|
1395
|
+
const x = voxelIndex % size;
|
|
1396
|
+
const y = Math.floor(voxelIndex / size % height);
|
|
1397
|
+
const z = Math.floor(voxelIndex / (size * height));
|
|
1398
|
+
return [x, y, z];
|
|
1399
|
+
}
|
|
1400
|
+
// public changeVoxelColor(faceIndex: number, newColor: THREE.Color) {
|
|
1401
|
+
// const colorAttr = voxelMesh.geometry.getAttribute("color") as THREE.BufferAttribute;
|
|
1402
|
+
// if (!colorAttr) return; // Ensure the color attribute exists
|
|
1403
|
+
// const colors = colorAttr.array as Float32Array;
|
|
1404
|
+
// // Each face has 3 vertices, each vertex has 4 components (RGBA)
|
|
1405
|
+
// const vertexIndex = faceIndex * 3 * 4; // Multiply by 4 because each vertex has RGBA
|
|
1406
|
+
// for (let i = 0; i < 3; i++) { // Update all three vertices of the face
|
|
1407
|
+
// colors[vertexIndex + i * 4] = newColor.r;
|
|
1408
|
+
// colors[vertexIndex + i * 4 + 1] = newColor.g;
|
|
1409
|
+
// colors[vertexIndex + i * 4 + 2] = newColor.b;
|
|
1410
|
+
// colors[vertexIndex + i * 4 + 3] = 1; // Ensure full opacity
|
|
1411
|
+
// }
|
|
1412
|
+
// colorAttr.needsUpdate = true; // Tell Three.js to update the buffer
|
|
1413
|
+
// }
|
|
1414
|
+
updateGeometry() {
|
|
1415
|
+
const positions = [];
|
|
1416
|
+
const normals = [];
|
|
1417
|
+
const colors = [];
|
|
1418
|
+
const indices = [];
|
|
1419
|
+
let needsUpdate = false;
|
|
1420
|
+
this._voxels.forEach((voxel, key) => {
|
|
1421
|
+
if (voxel) {
|
|
1422
|
+
const [x, y, z] = key2xyz(key);
|
|
1423
|
+
VoxelFaces.forEach((faces) => {
|
|
1424
|
+
const [dx, dy, dz] = faces[0];
|
|
1425
|
+
const neighborVoxel = this.get(x + dx, y + dy, z + dz) ?? 0;
|
|
1426
|
+
if (!neighborVoxel) {
|
|
1427
|
+
const positionIndex = positions.length / 3;
|
|
1428
|
+
faces.forEach((faceVerts) => {
|
|
1429
|
+
const [
|
|
1430
|
+
nx,
|
|
1431
|
+
ny,
|
|
1432
|
+
nz,
|
|
1433
|
+
px,
|
|
1434
|
+
py,
|
|
1435
|
+
pz
|
|
1436
|
+
/*ux, uy*/
|
|
1437
|
+
] = faceVerts;
|
|
1438
|
+
positions.push(x + px, y + py, z + pz);
|
|
1439
|
+
normals.push(nx, ny, nz);
|
|
1440
|
+
colors.push(...this.colors[voxel - 1]);
|
|
1441
|
+
});
|
|
1442
|
+
indices.push(
|
|
1443
|
+
positionIndex,
|
|
1444
|
+
positionIndex + 1,
|
|
1445
|
+
positionIndex + 2,
|
|
1446
|
+
positionIndex + 2,
|
|
1447
|
+
positionIndex + 1,
|
|
1448
|
+
positionIndex + 3
|
|
1449
|
+
);
|
|
1450
|
+
needsUpdate = true;
|
|
1451
|
+
}
|
|
1452
|
+
});
|
|
1453
|
+
this.setAttribute("position", new BufferAttribute(new Float32Array(positions), 3));
|
|
1454
|
+
this.setAttribute("normal", new BufferAttribute(new Float32Array(normals), 3));
|
|
1455
|
+
this.setAttribute("color", new BufferAttribute(new Float32Array(colors), 4));
|
|
1456
|
+
this.setIndex(indices);
|
|
1457
|
+
}
|
|
1458
|
+
});
|
|
1459
|
+
if (needsUpdate) {
|
|
1460
|
+
this.attributes.position.needsUpdate = true;
|
|
1461
|
+
this.computeVertexNormals();
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
};
|
|
1465
|
+
var lo = -0.5;
|
|
1466
|
+
var hi = 0.5;
|
|
1467
|
+
var VoxelFaces = [
|
|
1468
|
+
// [nx, ny, nz, px, py, pz, ux, uy]
|
|
1469
|
+
[
|
|
1470
|
+
//left
|
|
1471
|
+
[-1, 0, 0, lo, hi, lo, 0, 1],
|
|
1472
|
+
[-1, 0, 0, lo, lo, lo, 0, 0],
|
|
1473
|
+
[-1, 0, 0, lo, hi, hi, 1, 1],
|
|
1474
|
+
[-1, 0, 0, lo, lo, hi, 1, 0]
|
|
1475
|
+
],
|
|
1476
|
+
[
|
|
1477
|
+
//right
|
|
1478
|
+
[1, 0, 0, hi, hi, hi, 0, 1],
|
|
1479
|
+
[1, 0, 0, hi, lo, hi, 0, 0],
|
|
1480
|
+
[1, 0, 0, hi, hi, lo, 1, 1],
|
|
1481
|
+
[1, 0, 0, hi, lo, lo, 1, 0]
|
|
1482
|
+
],
|
|
1483
|
+
[
|
|
1484
|
+
//bottom
|
|
1485
|
+
[0, -1, 0, hi, lo, hi, 1, 0],
|
|
1486
|
+
[0, -1, 0, lo, lo, hi, 0, 0],
|
|
1487
|
+
[0, -1, 0, hi, lo, lo, 1, 1],
|
|
1488
|
+
[0, -1, 0, lo, lo, lo, 0, 1]
|
|
1489
|
+
],
|
|
1490
|
+
[
|
|
1491
|
+
//top
|
|
1492
|
+
[0, 1, 0, lo, hi, hi, 1, 1],
|
|
1493
|
+
[0, 1, 0, hi, hi, hi, 0, 1],
|
|
1494
|
+
[0, 1, 0, lo, hi, lo, 1, 0],
|
|
1495
|
+
[0, 1, 0, hi, hi, lo, 0, 0]
|
|
1496
|
+
],
|
|
1497
|
+
[
|
|
1498
|
+
//back
|
|
1499
|
+
[0, 0, -1, hi, lo, lo, 0, 0],
|
|
1500
|
+
[0, 0, -1, lo, lo, lo, 1, 0],
|
|
1501
|
+
[0, 0, -1, hi, hi, lo, 0, 1],
|
|
1502
|
+
[0, 0, -1, lo, hi, lo, 1, 1]
|
|
1503
|
+
],
|
|
1504
|
+
[
|
|
1505
|
+
//front
|
|
1506
|
+
[0, 0, 1, lo, lo, hi, 0, 0],
|
|
1507
|
+
[0, 0, 1, hi, lo, hi, 1, 0],
|
|
1508
|
+
[0, 0, 1, lo, hi, hi, 0, 1],
|
|
1509
|
+
[0, 0, 1, hi, hi, hi, 1, 1]
|
|
1510
|
+
]
|
|
1511
|
+
];
|
|
1512
|
+
export {
|
|
1513
|
+
CanvasRenderer,
|
|
1514
|
+
Clock,
|
|
1515
|
+
ClockStats,
|
|
1516
|
+
CompileShader,
|
|
1517
|
+
CreateProgram,
|
|
1518
|
+
CreateProgramInfo,
|
|
1519
|
+
CreateWebGlContext,
|
|
1520
|
+
CubeSphereGeometry,
|
|
1521
|
+
ECS,
|
|
1522
|
+
Emitter,
|
|
1523
|
+
GetProgramLocations,
|
|
1524
|
+
GlobalEmitter,
|
|
1525
|
+
Input,
|
|
1526
|
+
LoadTexture,
|
|
1527
|
+
Noise,
|
|
1528
|
+
ResizeWebGlContext,
|
|
1529
|
+
Rng,
|
|
1530
|
+
ShaderType,
|
|
1531
|
+
SimplexNoise,
|
|
1532
|
+
TextureAtlas,
|
|
1533
|
+
ThreeJsBoilerPlate,
|
|
1534
|
+
ThreeJsCameraRig,
|
|
1535
|
+
ThreeJsPlayerController,
|
|
1536
|
+
VoxelGeometry,
|
|
1537
|
+
WebGlRenderer,
|
|
1538
|
+
clamp,
|
|
1539
|
+
deg2rad,
|
|
1540
|
+
getRedYellowGreenGradientHex,
|
|
1541
|
+
isNullOrUndefined,
|
|
1542
|
+
key2xyz,
|
|
1543
|
+
lerp,
|
|
1544
|
+
log,
|
|
1545
|
+
logdev,
|
|
1546
|
+
logerr,
|
|
1547
|
+
loginf,
|
|
1548
|
+
logwrn,
|
|
1549
|
+
rng,
|
|
1550
|
+
xyz2key
|
|
1551
|
+
};
|