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