@zylem/game-lib 0.3.15 → 0.3.16
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/dist/.vite/manifest.json +595 -2
- package/dist/actions.d.ts +4 -0
- package/dist/actions.js +10 -0
- package/dist/behaviors.d.ts +3 -0
- package/dist/behaviors.js +8 -0
- package/dist/camera.d.ts +3 -0
- package/dist/camera.js +6 -0
- package/dist/core.d.ts +5 -0
- package/dist/core.js +11 -0
- package/dist/entities.d.ts +8 -0
- package/dist/entities.js +19 -0
- package/dist/lib/actions/behaviors/actions.js +35 -0
- package/dist/lib/actions/behaviors/boundaries/boundary.d.ts +1 -1
- package/dist/lib/actions/behaviors/boundaries/boundary.js +40 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet-2d-collision.js +100 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet-2d-in-bounds.js +37 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet.d.ts +2 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet.js +6 -0
- package/dist/lib/actions/capabilities/moveable.js +108 -0
- package/dist/lib/actions/capabilities/rotatable.js +82 -0
- package/dist/lib/actions/capabilities/transformable.js +9 -0
- package/dist/lib/actions/global-change.js +40 -0
- package/dist/lib/camera/camera.js +19 -0
- package/dist/lib/camera/fixed-2d.js +30 -0
- package/dist/lib/camera/perspective.js +10 -0
- package/dist/lib/camera/third-person.js +42 -0
- package/dist/lib/camera/zylem-camera.js +145 -0
- package/dist/lib/collision/collision-builder.js +46 -0
- package/dist/lib/collision/collision-delegate.js +6 -0
- package/dist/lib/collision/utils.js +24 -0
- package/dist/lib/collision/world.js +77 -0
- package/dist/lib/core/base-node.js +58 -0
- package/dist/lib/core/entity-asset-loader.js +57 -0
- package/dist/lib/core/lifecycle-base.js +20 -0
- package/dist/lib/core/preset-shader.js +30 -0
- package/dist/lib/core/three-addons/Timer.js +103 -0
- package/dist/lib/core/utility.js +20 -0
- package/dist/lib/core/vessel.js +23 -0
- package/dist/lib/debug/console/console-state.js +11 -0
- package/dist/lib/debug/debug-state.js +40 -0
- package/dist/lib/entities/actor.js +125 -0
- package/dist/lib/entities/box.js +68 -0
- package/dist/lib/entities/builder.js +79 -0
- package/dist/lib/entities/create.js +31 -0
- package/dist/lib/entities/delegates/animation.js +58 -0
- package/dist/lib/entities/delegates/debug.js +72 -0
- package/dist/lib/entities/delegates/loader.js +19 -0
- package/dist/lib/entities/destroy.js +15 -0
- package/dist/lib/entities/entity.js +116 -0
- package/dist/lib/entities/plane.js +81 -0
- package/dist/lib/entities/rect.js +160 -0
- package/dist/lib/entities/sphere.js +68 -0
- package/dist/lib/entities/sprite.js +118 -0
- package/dist/lib/entities/text.js +111 -0
- package/dist/lib/entities/zone.js +103 -0
- package/dist/lib/game/game-state.js +17 -0
- package/dist/lib/game/game.js +157 -0
- package/dist/lib/game/zylem-game.js +107 -0
- package/dist/lib/graphics/geometries/XZPlaneGeometry.js +34 -0
- package/dist/lib/graphics/material.js +64 -0
- package/dist/lib/graphics/mesh.js +14 -0
- package/dist/lib/graphics/render-pass.js +56 -0
- package/dist/lib/graphics/shaders/fragment/debug.glsl.js +23 -0
- package/dist/lib/graphics/shaders/fragment/fire.glsl.js +52 -0
- package/dist/lib/graphics/shaders/fragment/standard.glsl.js +11 -0
- package/dist/lib/graphics/shaders/fragment/stars.glsl.js +44 -0
- package/dist/lib/graphics/shaders/vertex/debug.glsl.js +15 -0
- package/dist/lib/graphics/shaders/vertex/object-shader.glsl.js +11 -0
- package/dist/lib/graphics/shaders/vertex/standard.glsl.js +9 -0
- package/dist/lib/graphics/zylem-scene.js +89 -0
- package/dist/lib/input/gamepad-provider.js +58 -0
- package/dist/lib/input/input-manager.js +70 -0
- package/dist/lib/input/keyboard-provider.js +120 -0
- package/dist/lib/stage/debug-entity-cursor.js +53 -0
- package/dist/lib/stage/entity-spawner.js +27 -0
- package/dist/lib/stage/stage-debug-delegate.js +100 -0
- package/dist/lib/stage/stage-state.js +36 -0
- package/dist/lib/stage/stage.js +58 -0
- package/dist/lib/stage/zylem-stage.js +279 -0
- package/dist/lib/systems/transformable.system.js +43 -0
- package/dist/main.d.ts +4 -14
- package/dist/main.js +54 -45933
- package/dist/stage.d.ts +3 -0
- package/dist/stage.js +6 -0
- package/package.json +35 -1
- package/dist/lib/actions/behaviors/index.d.ts +0 -1
- package/dist/main.cjs +0 -4023
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { BaseNode as h } from "../core/base-node.js";
|
|
2
|
+
import { ZylemGame as c } from "./zylem-game.js";
|
|
3
|
+
import { Stage as o, stage as l } from "../stage/stage.js";
|
|
4
|
+
import { setPaused as r } from "../debug/debug-state.js";
|
|
5
|
+
import { GameEntity as u } from "../entities/entity.js";
|
|
6
|
+
import { getGlobalState as m, setGlobalState as p } from "./game-state.js";
|
|
7
|
+
const f = {
|
|
8
|
+
id: "zylem",
|
|
9
|
+
globals: {},
|
|
10
|
+
stages: [
|
|
11
|
+
l()
|
|
12
|
+
]
|
|
13
|
+
};
|
|
14
|
+
function d(n) {
|
|
15
|
+
let e = { ...f };
|
|
16
|
+
const t = [], s = [], a = [];
|
|
17
|
+
return Object.values(n).forEach((i) => {
|
|
18
|
+
if (i instanceof o)
|
|
19
|
+
s.push(i);
|
|
20
|
+
else if (i instanceof u)
|
|
21
|
+
a.push(i);
|
|
22
|
+
else if (i instanceof h)
|
|
23
|
+
a.push(i);
|
|
24
|
+
else if (i.constructor.name === "Object" && typeof i == "object") {
|
|
25
|
+
const g = Object.assign(f, { ...i });
|
|
26
|
+
t.push(g);
|
|
27
|
+
}
|
|
28
|
+
}), t.forEach((i) => {
|
|
29
|
+
e = Object.assign(e, { ...i });
|
|
30
|
+
}), s.forEach((i) => {
|
|
31
|
+
i.addEntities(a);
|
|
32
|
+
}), s.length ? e.stages = s : e.stages[0].addEntities(a), e;
|
|
33
|
+
}
|
|
34
|
+
class R {
|
|
35
|
+
gameRef = null;
|
|
36
|
+
options;
|
|
37
|
+
pendingGlobalChangeHandlers = [];
|
|
38
|
+
update = () => {
|
|
39
|
+
};
|
|
40
|
+
setup = () => {
|
|
41
|
+
};
|
|
42
|
+
destroy = () => {
|
|
43
|
+
};
|
|
44
|
+
refErrorMessage = "lost reference to game";
|
|
45
|
+
constructor(e) {
|
|
46
|
+
this.options = e;
|
|
47
|
+
}
|
|
48
|
+
async start() {
|
|
49
|
+
const e = await this.load();
|
|
50
|
+
return this.gameRef = e, this.setOverrides(), e.start(), this;
|
|
51
|
+
}
|
|
52
|
+
async load() {
|
|
53
|
+
console.log("loading game", this.options);
|
|
54
|
+
const e = d(this.options), t = new c(e, this);
|
|
55
|
+
return await t.loadStage(e.stages[0]), t;
|
|
56
|
+
}
|
|
57
|
+
setOverrides() {
|
|
58
|
+
if (!this.gameRef) {
|
|
59
|
+
console.error(this.refErrorMessage);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (this.gameRef.customSetup = this.setup, this.gameRef.customUpdate = this.update, this.gameRef.customDestroy = this.destroy, this.pendingGlobalChangeHandlers.length) {
|
|
63
|
+
for (const { key: e, callback: t } of this.pendingGlobalChangeHandlers)
|
|
64
|
+
this.gameRef.onGlobalChange(e, t);
|
|
65
|
+
this.pendingGlobalChangeHandlers = [];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async pause() {
|
|
69
|
+
r(!0);
|
|
70
|
+
}
|
|
71
|
+
async resume() {
|
|
72
|
+
r(!1), this.gameRef && (this.gameRef.previousTimeStamp = 0, this.gameRef.timer.reset());
|
|
73
|
+
}
|
|
74
|
+
async reset() {
|
|
75
|
+
if (!this.gameRef) {
|
|
76
|
+
console.error(this.refErrorMessage);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
await this.gameRef.loadStage(this.gameRef.stages[0]);
|
|
80
|
+
}
|
|
81
|
+
async nextStage() {
|
|
82
|
+
if (!this.gameRef) {
|
|
83
|
+
console.error(this.refErrorMessage);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const e = this.gameRef.currentStageId, t = this.gameRef.stages.findIndex((a) => a.stageRef.uuid === e), s = this.gameRef.stages[t + 1];
|
|
87
|
+
if (!s) {
|
|
88
|
+
console.error("next stage called on last stage");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
await this.gameRef.loadStage(s);
|
|
92
|
+
}
|
|
93
|
+
async previousStage() {
|
|
94
|
+
if (!this.gameRef) {
|
|
95
|
+
console.error(this.refErrorMessage);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
const e = this.gameRef.currentStageId, t = this.gameRef.stages.findIndex((a) => a.stageRef.uuid === e), s = this.gameRef.stages[t - 1];
|
|
99
|
+
if (!s) {
|
|
100
|
+
console.error("previous stage called on first stage");
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
await this.gameRef.loadStage(s);
|
|
104
|
+
}
|
|
105
|
+
async goToStage() {
|
|
106
|
+
}
|
|
107
|
+
async end() {
|
|
108
|
+
}
|
|
109
|
+
add(...e) {
|
|
110
|
+
for (const t of e)
|
|
111
|
+
if (t) {
|
|
112
|
+
if (t instanceof o) {
|
|
113
|
+
this.gameRef ? (this.gameRef.stages.push(t), this.gameRef.stageMap.set(t.stageRef.uuid, t)) : this.options.push(t);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (typeof t == "function") {
|
|
117
|
+
try {
|
|
118
|
+
const s = t();
|
|
119
|
+
s instanceof o ? this.gameRef ? (this.gameRef.stages.push(s), this.gameRef.stageMap.set(s.stageRef.uuid, s)) : this.options.push(s) : s && typeof s.then == "function" && s.then((a) => {
|
|
120
|
+
a instanceof o && (this.gameRef ? (this.gameRef.stages.push(a), this.gameRef.stageMap.set(a.stageRef.uuid, a)) : this.options.push(a));
|
|
121
|
+
}).catch((a) => console.error("Failed to add async stage", a));
|
|
122
|
+
} catch (s) {
|
|
123
|
+
console.error("Error executing stage factory", s);
|
|
124
|
+
}
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
t && typeof t.then == "function" && t.then((s) => {
|
|
128
|
+
s instanceof o && (this.gameRef ? (this.gameRef.stages.push(s), this.gameRef.stageMap.set(s.stageRef.uuid, s)) : this.options.push(s));
|
|
129
|
+
}).catch((s) => console.error("Failed to add async stage", s));
|
|
130
|
+
}
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
getGlobal(e) {
|
|
134
|
+
return this.gameRef ? this.gameRef.getGlobal(e) : m(e);
|
|
135
|
+
}
|
|
136
|
+
setGlobal(e, t) {
|
|
137
|
+
if (this.gameRef) {
|
|
138
|
+
this.gameRef.setGlobal(e, t);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
p(e, t);
|
|
142
|
+
}
|
|
143
|
+
onGlobalChange(e, t) {
|
|
144
|
+
if (this.gameRef) {
|
|
145
|
+
this.gameRef.onGlobalChange(e, t);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.pendingGlobalChangeHandlers.push({ key: e, callback: t });
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
function M(...n) {
|
|
152
|
+
return new R(n);
|
|
153
|
+
}
|
|
154
|
+
export {
|
|
155
|
+
R as Game,
|
|
156
|
+
M as game
|
|
157
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { state as i, getGlobalState as r, setGlobalState as n } from "./game-state.js";
|
|
2
|
+
import { setDebugFlag as u, isPaused as h } from "../debug/debug-state.js";
|
|
3
|
+
import { InputManager as g } from "../input/input-manager.js";
|
|
4
|
+
import { Timer as c } from "../core/three-addons/Timer.js";
|
|
5
|
+
import { subscribe as m } from "valtio/vanilla";
|
|
6
|
+
import f from "stats.js";
|
|
7
|
+
class l {
|
|
8
|
+
id;
|
|
9
|
+
initialGlobals = {};
|
|
10
|
+
customSetup = null;
|
|
11
|
+
customUpdate = null;
|
|
12
|
+
customDestroy = null;
|
|
13
|
+
stages = [];
|
|
14
|
+
stageMap = /* @__PURE__ */ new Map();
|
|
15
|
+
currentStageId = "";
|
|
16
|
+
previousTimeStamp = 0;
|
|
17
|
+
totalTime = 0;
|
|
18
|
+
timer;
|
|
19
|
+
inputManager;
|
|
20
|
+
wrapperRef;
|
|
21
|
+
statsRef = null;
|
|
22
|
+
defaultCamera = null;
|
|
23
|
+
static FRAME_LIMIT = 120;
|
|
24
|
+
static FRAME_DURATION = 1e3 / l.FRAME_LIMIT;
|
|
25
|
+
static MAX_DELTA_SECONDS = 1 / 30;
|
|
26
|
+
constructor(t, e) {
|
|
27
|
+
this.wrapperRef = e, this.inputManager = new g(t.input), this.timer = new c(), this.timer.connect(document), this.id = t.id, this.stages = t.stages || [], this.setGlobals(t), t.debug && (this.statsRef = new f(), this.statsRef.showPanel(0), this.statsRef.dom.style.position = "absolute", this.statsRef.dom.style.bottom = "0", this.statsRef.dom.style.right = "0", this.statsRef.dom.style.top = "auto", this.statsRef.dom.style.left = "auto", document.body.appendChild(this.statsRef.dom));
|
|
28
|
+
}
|
|
29
|
+
async loadStage(t) {
|
|
30
|
+
this.unloadCurrentStage();
|
|
31
|
+
const e = t.options[0];
|
|
32
|
+
await t.load(this.id, e?.camera), this.stageMap.set(t.stageRef.uuid, t), this.currentStageId = t.stageRef.uuid, this.defaultCamera = t.stageRef.cameraRef;
|
|
33
|
+
}
|
|
34
|
+
unloadCurrentStage() {
|
|
35
|
+
if (!this.currentStageId)
|
|
36
|
+
return;
|
|
37
|
+
const t = this.getStage(this.currentStageId);
|
|
38
|
+
if (t) {
|
|
39
|
+
if (t?.stageRef)
|
|
40
|
+
try {
|
|
41
|
+
t.stageRef.nodeDestroy({
|
|
42
|
+
me: t.stageRef,
|
|
43
|
+
globals: i.globals
|
|
44
|
+
});
|
|
45
|
+
} catch (e) {
|
|
46
|
+
console.error("Failed to destroy previous stage", e);
|
|
47
|
+
}
|
|
48
|
+
this.stageMap.delete(this.currentStageId);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
setGlobals(t) {
|
|
52
|
+
u(t.debug), this.initialGlobals = { ...t.globals };
|
|
53
|
+
for (const e in this.initialGlobals) {
|
|
54
|
+
const s = this.initialGlobals[e];
|
|
55
|
+
s === void 0 && console.error(`global ${e} is undefined`), this.setGlobal(e, s);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
params() {
|
|
59
|
+
const t = this.currentStage(), e = this.timer.getDelta(), s = this.inputManager.getInputs(e), a = t?.stageRef?.cameraRef || this.defaultCamera;
|
|
60
|
+
return {
|
|
61
|
+
delta: e,
|
|
62
|
+
inputs: s,
|
|
63
|
+
globals: i.globals,
|
|
64
|
+
me: this,
|
|
65
|
+
camera: a
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
start() {
|
|
69
|
+
const t = this.currentStage(), e = this.params();
|
|
70
|
+
t.start({ ...e, me: t.stageRef }), this.customSetup && this.customSetup(e), this.loop(0);
|
|
71
|
+
}
|
|
72
|
+
loop(t) {
|
|
73
|
+
if (this.statsRef && this.statsRef.begin(), !h()) {
|
|
74
|
+
this.timer.update(t);
|
|
75
|
+
const e = this.currentStage(), s = this.params(), a = Math.min(s.delta, l.MAX_DELTA_SECONDS), o = { ...s, delta: a };
|
|
76
|
+
this.customUpdate && this.customUpdate(o), e && e.stageRef.nodeUpdate({ ...o, me: e.stageRef }), this.totalTime += o.delta, i.time = this.totalTime, this.previousTimeStamp = t;
|
|
77
|
+
}
|
|
78
|
+
this.statsRef && this.statsRef.end(), this.outOfLoop(), requestAnimationFrame(this.loop.bind(this));
|
|
79
|
+
}
|
|
80
|
+
outOfLoop() {
|
|
81
|
+
const t = this.currentStage();
|
|
82
|
+
t && t.stageRef.outOfLoop();
|
|
83
|
+
}
|
|
84
|
+
getStage(t) {
|
|
85
|
+
return this.stageMap.get(t);
|
|
86
|
+
}
|
|
87
|
+
currentStage() {
|
|
88
|
+
return this.getStage(this.currentStageId);
|
|
89
|
+
}
|
|
90
|
+
getGlobal(t) {
|
|
91
|
+
return r(t);
|
|
92
|
+
}
|
|
93
|
+
setGlobal(t, e) {
|
|
94
|
+
n(t, e);
|
|
95
|
+
}
|
|
96
|
+
onGlobalChange(t, e) {
|
|
97
|
+
let s = r(t);
|
|
98
|
+
m(i, () => {
|
|
99
|
+
const a = r(t);
|
|
100
|
+
a !== s && (s = a, e(a));
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export {
|
|
105
|
+
l as ZylemGame,
|
|
106
|
+
l as default
|
|
107
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { BufferGeometry as _, Float32BufferAttribute as f } from "three";
|
|
2
|
+
class b extends _ {
|
|
3
|
+
constructor(t = 1, h = 1, l = 1, a = 1) {
|
|
4
|
+
super(), this.type = "XZPlaneGeometry", this.parameters = {
|
|
5
|
+
width: t,
|
|
6
|
+
height: h,
|
|
7
|
+
widthSegments: l,
|
|
8
|
+
heightSegments: a
|
|
9
|
+
};
|
|
10
|
+
const d = t / 2, x = h / 2, n = Math.floor(l), i = Math.floor(a), r = n + 1, w = i + 1, A = t / n, v = h / i, u = [], m = [], g = [], p = [];
|
|
11
|
+
for (let s = 0; s < w; s++) {
|
|
12
|
+
const e = s * v - x;
|
|
13
|
+
for (let o = 0; o < r; o++) {
|
|
14
|
+
const c = o * A - d;
|
|
15
|
+
m.push(c, 0, e), g.push(0, 1, 0), p.push(o / n), p.push(1 - s / i);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
for (let s = 0; s < i; s++)
|
|
19
|
+
for (let e = 0; e < n; e++) {
|
|
20
|
+
const o = e + r * s, c = e + r * (s + 1), X = e + 1 + r * (s + 1), y = e + 1 + r * s;
|
|
21
|
+
u.push(o, c, y), u.push(c, X, y);
|
|
22
|
+
}
|
|
23
|
+
this.setIndex(u), this.setAttribute("position", new f(m, 3)), this.setAttribute("normal", new f(g, 3)), this.setAttribute("uv", new f(p, 2));
|
|
24
|
+
}
|
|
25
|
+
copy(t) {
|
|
26
|
+
return super.copy(t), this.parameters = Object.assign({}, t.parameters), this;
|
|
27
|
+
}
|
|
28
|
+
static fromJSON(t) {
|
|
29
|
+
return new b(t.width, t.height, t.widthSegments, t.heightSegments);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
b as XZPlaneGeometry
|
|
34
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Color as n, Vector2 as m, TextureLoader as c, RepeatWrapping as o, MeshPhongMaterial as p, MeshStandardMaterial as u, ShaderMaterial as f, Vector3 as d } from "three";
|
|
2
|
+
import { shortHash as M, sortedStringify as w } from "../core/utility.js";
|
|
3
|
+
import h from "../core/preset-shader.js";
|
|
4
|
+
class i {
|
|
5
|
+
static batchMaterialMap = /* @__PURE__ */ new Map();
|
|
6
|
+
materials = [];
|
|
7
|
+
batchMaterial(t, a) {
|
|
8
|
+
const r = M(w(t)), e = i.batchMaterialMap.get(r);
|
|
9
|
+
if (e) {
|
|
10
|
+
const s = e.geometryMap.get(a);
|
|
11
|
+
s ? e.geometryMap.set(a, s + 1) : e.geometryMap.set(a, 1);
|
|
12
|
+
} else
|
|
13
|
+
i.batchMaterialMap.set(r, {
|
|
14
|
+
geometryMap: /* @__PURE__ */ new Map([[a, 1]]),
|
|
15
|
+
material: this.materials[0]
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async build(t, a) {
|
|
19
|
+
const { path: r, repeat: e, color: s, shader: l } = t;
|
|
20
|
+
l && this.withShader(l), s && this.withColor(s), await this.setTexture(r ?? null, e), this.materials.length === 0 && this.setColor(new n("#ffffff")), this.batchMaterial(t, a);
|
|
21
|
+
}
|
|
22
|
+
withColor(t) {
|
|
23
|
+
return this.setColor(t), this;
|
|
24
|
+
}
|
|
25
|
+
withShader(t) {
|
|
26
|
+
return this.setShader(t), this;
|
|
27
|
+
}
|
|
28
|
+
async setTexture(t = null, a = new m(1, 1)) {
|
|
29
|
+
if (!t)
|
|
30
|
+
return;
|
|
31
|
+
const e = await new c().loadAsync(t);
|
|
32
|
+
e.repeat = a, e.wrapS = o, e.wrapT = o;
|
|
33
|
+
const s = new p({
|
|
34
|
+
map: e
|
|
35
|
+
});
|
|
36
|
+
this.materials.push(s);
|
|
37
|
+
}
|
|
38
|
+
setColor(t) {
|
|
39
|
+
const a = new u({
|
|
40
|
+
color: t,
|
|
41
|
+
emissiveIntensity: 0.5,
|
|
42
|
+
lightMapIntensity: 0.5,
|
|
43
|
+
fog: !0
|
|
44
|
+
});
|
|
45
|
+
this.materials.push(a);
|
|
46
|
+
}
|
|
47
|
+
setShader(t) {
|
|
48
|
+
const { fragment: a, vertex: r } = h.get(t) ?? h.get("standard"), e = new f({
|
|
49
|
+
uniforms: {
|
|
50
|
+
iResolution: { value: new d(1, 1, 1) },
|
|
51
|
+
iTime: { value: 0 },
|
|
52
|
+
tDiffuse: { value: null },
|
|
53
|
+
tDepth: { value: null },
|
|
54
|
+
tNormal: { value: null }
|
|
55
|
+
},
|
|
56
|
+
vertexShader: r,
|
|
57
|
+
fragmentShader: a
|
|
58
|
+
});
|
|
59
|
+
this.materials.push(e);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export {
|
|
63
|
+
i as MaterialBuilder
|
|
64
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Mesh as n } from "three";
|
|
2
|
+
class h {
|
|
3
|
+
_build(t, r, s) {
|
|
4
|
+
const { batched: i, material: a } = t;
|
|
5
|
+
i && console.warn("warning: mesh batching is not implemented");
|
|
6
|
+
const e = new n(r, s.at(-1));
|
|
7
|
+
return e.position.set(0, 0, 0), e.castShadow = !0, e.receiveShadow = !0, e;
|
|
8
|
+
}
|
|
9
|
+
_postBuild() {
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export {
|
|
13
|
+
h as MeshBuilder
|
|
14
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import * as s from "three";
|
|
2
|
+
import { WebGLRenderTarget as i } from "three";
|
|
3
|
+
import n from "./shaders/fragment/standard.glsl.js";
|
|
4
|
+
import l from "./shaders/vertex/standard.glsl.js";
|
|
5
|
+
import { Pass as o, FullScreenQuad as d } from "three/addons/postprocessing/Pass.js";
|
|
6
|
+
class f extends o {
|
|
7
|
+
fsQuad;
|
|
8
|
+
resolution;
|
|
9
|
+
scene;
|
|
10
|
+
camera;
|
|
11
|
+
rgbRenderTarget;
|
|
12
|
+
normalRenderTarget;
|
|
13
|
+
normalMaterial;
|
|
14
|
+
constructor(e, r, a) {
|
|
15
|
+
super(), this.resolution = e, this.fsQuad = new d(this.material()), this.scene = r, this.camera = a, this.rgbRenderTarget = new i(e.x * 4, e.y * 4), this.normalRenderTarget = new i(e.x * 4, e.y * 4), this.normalMaterial = new s.MeshNormalMaterial();
|
|
16
|
+
}
|
|
17
|
+
render(e, r) {
|
|
18
|
+
e.setRenderTarget(this.rgbRenderTarget), e.render(this.scene, this.camera);
|
|
19
|
+
const a = this.scene.overrideMaterial;
|
|
20
|
+
e.setRenderTarget(this.normalRenderTarget), this.scene.overrideMaterial = this.normalMaterial, e.render(this.scene, this.camera), this.scene.overrideMaterial = a;
|
|
21
|
+
const t = this.fsQuad.material.uniforms;
|
|
22
|
+
t.tDiffuse.value = this.rgbRenderTarget.texture, t.tDepth.value = this.rgbRenderTarget.depthTexture, t.tNormal.value = this.normalRenderTarget.texture, t.iTime.value += 0.01, this.renderToScreen ? e.setRenderTarget(null) : e.setRenderTarget(r), this.fsQuad.render(e);
|
|
23
|
+
}
|
|
24
|
+
material() {
|
|
25
|
+
return new s.ShaderMaterial({
|
|
26
|
+
uniforms: {
|
|
27
|
+
iTime: { value: 0 },
|
|
28
|
+
tDiffuse: { value: null },
|
|
29
|
+
tDepth: { value: null },
|
|
30
|
+
tNormal: { value: null },
|
|
31
|
+
resolution: {
|
|
32
|
+
value: new s.Vector4(this.resolution.x, this.resolution.y, 1 / this.resolution.x, 1 / this.resolution.y)
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
vertexShader: l,
|
|
36
|
+
fragmentShader: n
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
dispose() {
|
|
40
|
+
try {
|
|
41
|
+
this.fsQuad?.dispose?.();
|
|
42
|
+
} catch {
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
this.rgbRenderTarget?.dispose?.(), this.normalRenderTarget?.dispose?.();
|
|
46
|
+
} catch {
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
this.normalMaterial?.dispose?.();
|
|
50
|
+
} catch {
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
export {
|
|
55
|
+
f as default
|
|
56
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
var e = `varying vec3 vBarycentric;
|
|
2
|
+
uniform vec3 baseColor;
|
|
3
|
+
uniform vec3 wireframeColor;
|
|
4
|
+
uniform float wireframeThickness;
|
|
5
|
+
|
|
6
|
+
float edgeFactor() {
|
|
7
|
+
vec3 d = fwidth(vBarycentric);
|
|
8
|
+
vec3 a3 = smoothstep(vec3(0.0), d * wireframeThickness, vBarycentric);
|
|
9
|
+
return min(min(a3.x, a3.y), a3.z);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
void main() {
|
|
13
|
+
float edge = edgeFactor();
|
|
14
|
+
|
|
15
|
+
vec3 wireColor = wireframeColor;
|
|
16
|
+
|
|
17
|
+
vec3 finalColor = mix(wireColor, baseColor, edge);
|
|
18
|
+
|
|
19
|
+
gl_FragColor = vec4(finalColor, 1.0);
|
|
20
|
+
}`;
|
|
21
|
+
export {
|
|
22
|
+
e as default
|
|
23
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
var n = `#include <common>
|
|
2
|
+
|
|
3
|
+
uniform vec3 iResolution;
|
|
4
|
+
uniform float iTime;
|
|
5
|
+
uniform vec2 iOffset;
|
|
6
|
+
varying vec2 vUv;
|
|
7
|
+
|
|
8
|
+
float snoise(vec3 uv, float res)
|
|
9
|
+
{
|
|
10
|
+
const vec3 s = vec3(1e0, 1e2, 1e3);
|
|
11
|
+
|
|
12
|
+
uv *= res;
|
|
13
|
+
|
|
14
|
+
vec3 uv0 = floor(mod(uv, res))*s;
|
|
15
|
+
vec3 uv1 = floor(mod(uv+vec3(1.), res))*s;
|
|
16
|
+
|
|
17
|
+
vec3 f = fract(uv); f = f*f*(3.0-2.0*f);
|
|
18
|
+
|
|
19
|
+
vec4 v = vec4(uv0.x+uv0.y+uv0.z, uv1.x+uv0.y+uv0.z,
|
|
20
|
+
uv0.x+uv1.y+uv0.z, uv1.x+uv1.y+uv0.z);
|
|
21
|
+
|
|
22
|
+
vec4 r = fract(sin(v*1e-1)*1e3);
|
|
23
|
+
float r0 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);
|
|
24
|
+
|
|
25
|
+
r = fract(sin((v + uv1.z - uv0.z)*1e-1)*1e3);
|
|
26
|
+
float r1 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);
|
|
27
|
+
|
|
28
|
+
return mix(r0, r1, f.z)*2.-1.;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
|
|
32
|
+
vec2 p = -.5 + fragCoord.xy / iResolution.xy;
|
|
33
|
+
p.x *= iResolution.x/iResolution.y;
|
|
34
|
+
|
|
35
|
+
float color = 3.0 - (3.*length(2.*p));
|
|
36
|
+
|
|
37
|
+
vec3 coord = vec3(atan(p.x,p.y)/6.2832+.5, length(p)*.4, .5);
|
|
38
|
+
|
|
39
|
+
for(int i = 1; i <= 7; i++)
|
|
40
|
+
{
|
|
41
|
+
float power = pow(2.0, float(i));
|
|
42
|
+
color += (1.5 / power) * snoise(coord + vec3(0.,-iTime*.05, iTime*.01), power*16.);
|
|
43
|
+
}
|
|
44
|
+
fragColor = vec4( color, pow(max(color,0.),2.)*0.4, pow(max(color,0.),3.)*0.15 , 1.0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
void main() {
|
|
48
|
+
mainImage(gl_FragColor, vUv);
|
|
49
|
+
}`;
|
|
50
|
+
export {
|
|
51
|
+
n as default
|
|
52
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
var n = `#include <common>
|
|
2
|
+
|
|
3
|
+
uniform vec3 iResolution;
|
|
4
|
+
uniform float iTime;
|
|
5
|
+
varying vec2 vUv;
|
|
6
|
+
|
|
7
|
+
vec3 palette( float t ) {
|
|
8
|
+
vec3 a = vec3(0.5, 0.5, 0.5);
|
|
9
|
+
vec3 b = vec3(0.5, 0.5, 0.5);
|
|
10
|
+
vec3 c = vec3(1.0, 1.0, 1.0);
|
|
11
|
+
vec3 d = vec3(0.263,0.416,0.557);
|
|
12
|
+
|
|
13
|
+
return a + b*cos( 6.28318*(c*t+d) );
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
|
|
17
|
+
vec2 uv = (fragCoord * 2.0 - iResolution.xy) / iResolution.y;
|
|
18
|
+
vec2 uv0 = uv;
|
|
19
|
+
vec3 finalColor = vec3(0.0);
|
|
20
|
+
|
|
21
|
+
for (float i = 0.0; i < 4.0; i++) {
|
|
22
|
+
uv = fract(uv * 1.5) - 0.5;
|
|
23
|
+
|
|
24
|
+
float d = length(uv) * exp(-length(uv0));
|
|
25
|
+
|
|
26
|
+
vec3 col = palette(length(uv0) + i*.4 + iTime*.4);
|
|
27
|
+
|
|
28
|
+
d = sin(d*5. + iTime)/5.;
|
|
29
|
+
d = abs(d);
|
|
30
|
+
|
|
31
|
+
d = pow(0.01 / d, 1.2);
|
|
32
|
+
|
|
33
|
+
finalColor += col * d;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fragColor = vec4(finalColor, 1.0);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
void main() {
|
|
40
|
+
mainImage(gl_FragColor, vUv);
|
|
41
|
+
}`;
|
|
42
|
+
export {
|
|
43
|
+
n as default
|
|
44
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var e = `varying vec3 vBarycentric;
|
|
2
|
+
|
|
3
|
+
void main() {
|
|
4
|
+
vec3 barycentric = vec3(0.0);
|
|
5
|
+
int index = gl_VertexID % 3;
|
|
6
|
+
if (index == 0) barycentric = vec3(1.0, 0.0, 0.0);
|
|
7
|
+
else if (index == 1) barycentric = vec3(0.0, 1.0, 0.0);
|
|
8
|
+
else barycentric = vec3(0.0, 0.0, 1.0);
|
|
9
|
+
vBarycentric = barycentric;
|
|
10
|
+
|
|
11
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
12
|
+
}`;
|
|
13
|
+
export {
|
|
14
|
+
e as default
|
|
15
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Scene as r, Color as n, TextureLoader as o, AmbientLight as m, DirectionalLight as d, Vector3 as h, GridHelper as l } from "three";
|
|
2
|
+
import { debugState as c } from "../debug/debug-state.js";
|
|
3
|
+
import { getGlobalState as p } from "../game/game-state.js";
|
|
4
|
+
class y {
|
|
5
|
+
type = "Scene";
|
|
6
|
+
_setup;
|
|
7
|
+
scene;
|
|
8
|
+
zylemCamera;
|
|
9
|
+
containerElement = null;
|
|
10
|
+
constructor(e, t, a) {
|
|
11
|
+
const i = new r();
|
|
12
|
+
if (i.background = new n(a.backgroundColor), a.backgroundImage) {
|
|
13
|
+
const s = new o().load(a.backgroundImage);
|
|
14
|
+
i.background = s;
|
|
15
|
+
}
|
|
16
|
+
this.scene = i, this.zylemCamera = t, this.setupContainer(e), this.setupLighting(i), this.setupCamera(i, t), c.on && this.debugScene();
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Setup the container element and append camera's renderer
|
|
20
|
+
*/
|
|
21
|
+
setupContainer(e) {
|
|
22
|
+
let t = document.getElementById(e);
|
|
23
|
+
if (!t) {
|
|
24
|
+
console.warn(`Could not find element with id: ${e}`);
|
|
25
|
+
const a = document.createElement("main");
|
|
26
|
+
a.setAttribute("id", e), document.body.appendChild(a), t = a;
|
|
27
|
+
}
|
|
28
|
+
t?.firstChild && t.removeChild(t.firstChild), this.containerElement = t, t?.appendChild(this.zylemCamera.getDomElement());
|
|
29
|
+
}
|
|
30
|
+
setup() {
|
|
31
|
+
this._setup && this._setup({ me: this, camera: this.zylemCamera, globals: p() });
|
|
32
|
+
}
|
|
33
|
+
destroy() {
|
|
34
|
+
if (this.containerElement && this.zylemCamera)
|
|
35
|
+
try {
|
|
36
|
+
const e = this.zylemCamera.getDomElement();
|
|
37
|
+
e && e.parentElement === this.containerElement && this.containerElement.removeChild(e);
|
|
38
|
+
} catch {
|
|
39
|
+
}
|
|
40
|
+
this.zylemCamera && this.zylemCamera.destroy && this.zylemCamera.destroy(), this.scene && this.scene.traverse((e) => {
|
|
41
|
+
e.geometry && e.geometry.dispose?.(), e.material && (Array.isArray(e.material) ? e.material.forEach((t) => t.dispose?.()) : e.material.dispose?.());
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
update({ delta: e }) {
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Setup camera with the scene
|
|
48
|
+
*/
|
|
49
|
+
setupCamera(e, t) {
|
|
50
|
+
e.add(t.cameraRig), t.setup(e);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Setup scene lighting
|
|
54
|
+
*/
|
|
55
|
+
setupLighting(e) {
|
|
56
|
+
const t = new m(16777215, 2);
|
|
57
|
+
e.add(t);
|
|
58
|
+
const a = new d(16777215, 2);
|
|
59
|
+
a.name = "Light", a.position.set(0, 100, 0), a.castShadow = !0, a.shadow.camera.near = 0.1, a.shadow.camera.far = 2e3, a.shadow.camera.left = -100, a.shadow.camera.right = 100, a.shadow.camera.top = 100, a.shadow.camera.bottom = -100, a.shadow.mapSize.width = 2048, a.shadow.mapSize.height = 2048, e.add(a);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Update renderer size - delegates to camera
|
|
63
|
+
*/
|
|
64
|
+
updateRenderer(e, t) {
|
|
65
|
+
this.zylemCamera.resize(e, t);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Add object to scene
|
|
69
|
+
*/
|
|
70
|
+
add(e, t = new h(0, 0, 0)) {
|
|
71
|
+
e.position.set(t.x, t.y, t.z), this.scene.add(e);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Add game entity to scene
|
|
75
|
+
*/
|
|
76
|
+
addEntity(e) {
|
|
77
|
+
e.group ? this.add(e.group, e.options.position) : e.mesh && this.add(e.mesh, e.options.position);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Add debug helpers to scene
|
|
81
|
+
*/
|
|
82
|
+
debugScene() {
|
|
83
|
+
const a = new l(1e3, 100);
|
|
84
|
+
this.scene.add(a);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
y as ZylemScene
|
|
89
|
+
};
|