@margoengine/creative 0.0.0-stage → 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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +307 -3
- package/dist/display-C6RC-F2-.js +3 -0
- package/dist/errors-XvcwUnU4.js +7 -0
- package/dist/gpu-DN8sGUpu.js +30 -0
- package/dist/grass-StuKCE_k.js +450 -0
- package/dist/grass-settings.d.ts +63 -0
- package/dist/grass-settings.js +131 -0
- package/dist/grass-terrain.d.ts +33 -0
- package/dist/grass-terrain.js +185 -0
- package/dist/grass.d.ts +106 -0
- package/dist/grass.js +1 -0
- package/dist/hdr-format-DXpjc3bW.js +4 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/msaa-DjFZ9hVW.js +213 -0
- package/dist/paint-aniso.d.ts +36 -0
- package/dist/paint-aniso.js +215 -0
- package/dist/paint.d.ts +56 -0
- package/dist/paint.js +148 -0
- package/dist/particles.d.ts +38 -0
- package/dist/particles.js +79 -0
- package/dist/pass-B5mPip-t.js +27 -0
- package/dist/planet-C2ewqAmf.js +505 -0
- package/dist/planet-atmosphere.d.ts +37 -0
- package/dist/planet-atmosphere.js +196 -0
- package/dist/planet-pass.d.ts +47 -0
- package/dist/planet.d.ts +76 -0
- package/dist/planet.js +1 -0
- package/dist/post.d.ts +108 -0
- package/dist/post.js +265 -0
- package/dist/quality-DFm-cQWC.js +7 -0
- package/dist/shared/settings.d.ts +89 -0
- package/dist/stars-DWF7qnc1.js +90 -0
- package/dist/stars.d.ts +34 -0
- package/dist/stars.js +1 -0
- package/dist/targets-depth.d.ts +35 -0
- package/dist/targets-depth.js +169 -0
- package/dist/targets.d.ts +35 -0
- package/dist/targets.js +42 -0
- package/dist/texture-target-CGqE9pOa.js +131 -0
- package/dist/texture-target.d.ts +49 -0
- package/dist/toon.d.ts +32 -0
- package/dist/toon.js +58 -0
- package/dist/view-rect-TO68f_UR.js +48 -0
- package/package.json +126 -6
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
// @margo/core-built 0.1.0
|
|
2
|
+
import {coreRequirement as __margoCoreRequirement,assertCore as __margoAssertCore} from '@margoengine/core/extension';
|
|
3
|
+
|
|
4
|
+
import { n as e } from "./errors-XvcwUnU4.js";
|
|
5
|
+
import { n as t } from "./view-rect-TO68f_UR.js";
|
|
6
|
+
import { t as n } from "./quality-DFm-cQWC.js";
|
|
7
|
+
import { t as r } from "./display-C6RC-F2-.js";
|
|
8
|
+
import { t as i } from "./hdr-format-DXpjc3bW.js";
|
|
9
|
+
import { MargoError as a } from "@margoengine/math/errors";
|
|
10
|
+
import { finiteVector as o } from "@margoengine/math/validate";
|
|
11
|
+
//#region ../shared/cleanup.js
|
|
12
|
+
var s = class {
|
|
13
|
+
constructor(...e) {
|
|
14
|
+
this.failed = e.length > 0, this.error = e[0];
|
|
15
|
+
}
|
|
16
|
+
run(e) {
|
|
17
|
+
try {
|
|
18
|
+
e();
|
|
19
|
+
} catch (e) {
|
|
20
|
+
this.failed || (this.error = e), this.failed = !0;
|
|
21
|
+
}
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
finish() {
|
|
25
|
+
if (this.failed) throw this.error;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region ../shared/loading.js
|
|
30
|
+
async function c(e, { maxBytes: t, signal: n, label: r = "Asset", output: i = null, expected: a = 0 }) {
|
|
31
|
+
if (!Number.isSafeInteger(t) || t < 0 || i !== null && (!(i instanceof Uint8Array) || i.length !== t)) throw RangeError("Invalid asset byte budget; pass maxBytes as an integer");
|
|
32
|
+
if (n?.throwIfAborted(), !e) throw TypeError(`Empty ${r} response; check the URL`);
|
|
33
|
+
let o = e.getReader(), c = [], l = 0, u = i, d = new s(), f = (e) => {
|
|
34
|
+
try {
|
|
35
|
+
Promise.resolve(o.cancel(e)).catch(() => {});
|
|
36
|
+
} catch {}
|
|
37
|
+
}, p = () => f(n.reason);
|
|
38
|
+
n?.addEventListener("abort", p, { once: !0 });
|
|
39
|
+
try {
|
|
40
|
+
for (!i && Number.isSafeInteger(a) && a > 0 && a <= t && (u = new Uint8Array(a));;) {
|
|
41
|
+
n?.throwIfAborted();
|
|
42
|
+
let { done: e, value: i } = await o.read();
|
|
43
|
+
if (n?.throwIfAborted(), e) break;
|
|
44
|
+
if (!(i instanceof Uint8Array)) throw TypeError(`${r} stream requires byte chunks`);
|
|
45
|
+
if (i.byteLength > t - l) throw RangeError(`${r} byte budget exceeded; raise maxBytes`);
|
|
46
|
+
u && i.length <= u.length - l ? u.set(i, l) : (u && c.push(u.subarray(0, l)), u = null, c.push(i.length < i.buffer.byteLength || !(i.buffer instanceof ArrayBuffer) ? i.slice() : i)), l += i.byteLength;
|
|
47
|
+
}
|
|
48
|
+
} catch (e) {
|
|
49
|
+
f(e), d = new s(e);
|
|
50
|
+
}
|
|
51
|
+
if (d.run(() => n?.removeEventListener("abort", p)).run(() => o.releaseLock()).finish(), i) return i.subarray(0, l);
|
|
52
|
+
if (u) return l < u.length ? u.slice(0, l) : u;
|
|
53
|
+
let m = new Uint8Array(l), h = 0;
|
|
54
|
+
for (let e of c) m.set(e, h), h += e.length;
|
|
55
|
+
return m;
|
|
56
|
+
}
|
|
57
|
+
async function l(t, n) {
|
|
58
|
+
try {
|
|
59
|
+
if (n.signal?.throwIfAborted(), !t.ok) throw e("failed", `${n.label ?? "Asset"} request failed: HTTP ${t.status}; check the URL and that the server serves it`);
|
|
60
|
+
let r = Number(t.headers.get("content-length"));
|
|
61
|
+
if (Number.isFinite(r) && r > n.maxBytes) throw RangeError(`${n.label ?? "Asset"} byte budget exceeded; raise maxBytes`);
|
|
62
|
+
let i = t.headers.get("content-encoding");
|
|
63
|
+
(!i || i === "identity") && (n = {
|
|
64
|
+
...n,
|
|
65
|
+
expected: r
|
|
66
|
+
});
|
|
67
|
+
} catch (e) {
|
|
68
|
+
try {
|
|
69
|
+
Promise.resolve(t.body?.cancel(e)).catch(() => {});
|
|
70
|
+
} catch {}
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
return c(t.body, n);
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/shaders/planet_surface.wgsl?raw
|
|
77
|
+
var u = "struct PlanetFrame {\r\n camera: mat4x4f,\r\n model: mat4x4f,\r\n eyeRadius: vec4f,\r\n sunOffset: vec4f,\r\n atmosphere: vec4f,\r\n settings: vec4f,\r\n};\r\n\r\n@group(0) @binding(0) var<uniform> frame: PlanetFrame;\r\n@group(0) @binding(1) var filtering: sampler;\r\n@group(0) @binding(2) var dayMap: texture_2d<f32>;\r\n@group(0) @binding(3) var nightMap: texture_2d<f32>;\r\n@group(0) @binding(4) var specularMap: texture_2d<f32>;\r\n@group(0) @binding(5) var normalMap: texture_2d<f32>;\r\noverride outputHdr: bool = false;\r\n\r\nstruct VertexOutput {\r\n @builtin(position) clip: vec4f,\r\n @location(0) direction: vec3f,\r\n @location(1) world: vec3f,\r\n};\r\n\r\n@vertex fn vs_main(@location(0) position: vec3f) -> VertexOutput {\r\n var out: VertexOutput;\r\n let world = frame.model * vec4f(position * frame.eyeRadius.w, 1.0);\r\n out.clip = frame.camera * world;\r\n out.direction = position;\r\n out.world = world.xyz;\r\n return out;\r\n}\r\n\r\nfn encodeColor(linear: vec3f) -> vec3f {\r\n if (outputHdr) {return linear;}\r\n return toDisplay(linear);\r\n}\r\n\r\n@fragment fn fs_main(input: VertexOutput) -> @location(0) vec4f {\r\n let local = normalize(input.direction);\r\n let uv = vec2f(atan2(local.z, local.x) / 6.283185307 + 0.5, acos(clamp(local.y, -1.0, 1.0)) / 3.141592654);\r\n let day = textureSample(dayMap, filtering, uv).rgb;\r\n let night = textureSample(nightMap, filtering, uv).rgb;\r\n let mask = textureSample(specularMap, filtering, uv).rg;\r\n let relief = textureSample(normalMap, filtering, uv).rgb * 2.0 - 1.0;\r\n let rotation = mat3x3f(frame.model[0].xyz, frame.model[1].xyz, frame.model[2].xyz);\r\n let geometricNormal = normalize(rotation * local);\r\n var shadingNormal = geometricNormal;\r\n if (frame.settings.x > 0.0) {\r\n let horizontal = length(local.xz);\r\n var tangent = vec3f(1.0, 0.0, 0.0);\r\n if (horizontal > 0.0001) {tangent = vec3f(-local.z, 0.0, local.x) / horizontal;}\r\n let bitangent = cross(local, tangent);\r\n let mapped = tangent * relief.x + bitangent * relief.y + local * relief.z;\r\n if (dot(mapped, mapped) > 0.000001) {shadingNormal = normalize(rotation * mapped);}\r\n }\r\n let light = frame.sunOffset.xyz;\r\n let view = normalize(frame.eyeRadius.xyz - input.world);\r\n let geometricLight = dot(geometricNormal, light);\r\n let diffuse = max(dot(shadingNormal, light), 0.0);\r\n let nightWeight = 1.0 - smoothstep(-0.15, 0.05, geometricLight);\r\n let shininess = mix(256.0, 8.0, mask.g);\r\n let glint = pow(max(dot(reflect(-light, shadingNormal), view), 0.0), shininess) * mask.r * max(geometricLight, 0.0);\r\n let color = day * diffuse + night * frame.settings.z * nightWeight + vec3f(glint);\r\n return vec4f(encodeColor(color), 1.0);\r\n}\r\n", d = "struct PlanetFrame {\r\n camera: mat4x4f,\r\n model: mat4x4f,\r\n eyeRadius: vec4f,\r\n sunOffset: vec4f,\r\n atmosphere: vec4f,\r\n settings: vec4f,\r\n};\r\n\r\n@group(0) @binding(0) var<uniform> frame: PlanetFrame;\r\n@group(0) @binding(1) var filtering: sampler;\r\n@group(0) @binding(2) var cloudMap: texture_2d<f32>;\r\noverride outputHdr: bool = false;\r\n\r\nstruct VertexOutput {\r\n @builtin(position) clip: vec4f,\r\n @location(0) direction: vec3f,\r\n @location(1) world: vec3f,\r\n};\r\n\r\n@vertex fn vs_main(@location(0) position: vec3f) -> VertexOutput {\r\n var out: VertexOutput;\r\n let world = frame.model * vec4f(position * frame.eyeRadius.w * 1.01, 1.0);\r\n out.clip = frame.camera * world;\r\n out.direction = position;\r\n out.world = world.xyz;\r\n return out;\r\n}\r\n\r\nfn encodeColor(linear: vec3f) -> vec3f {\r\n if (outputHdr) {return linear;}\r\n return toDisplay(linear);\r\n}\r\n\r\n@fragment fn fs_main(input: VertexOutput) -> @location(0) vec4f {\r\n let local = normalize(input.direction);\r\n let uv = vec2f(atan2(local.z, local.x) / 6.283185307 + 0.5 + frame.sunOffset.w, acos(clamp(local.y, -1.0, 1.0)) / 3.141592654);\r\n let cloud = textureSample(cloudMap, filtering, uv);\r\n let rotation = mat3x3f(frame.model[0].xyz, frame.model[1].xyz, frame.model[2].xyz);\r\n let normal = normalize(rotation * local);\r\n let view = normalize(frame.eyeRadius.xyz - input.world);\r\n let light = dot(normal, frame.sunOffset.xyz);\r\n let opacity = clamp(cloud.r * cloud.a * frame.settings.y, 0.0, 1.0);\r\n let cloudColor = vec3f(max(light, 0.0) + 0.025);\r\n let rim = pow(1.0 - clamp(dot(view, normal), 0.0, 1.0), 3.5);\r\n let sunlight = smoothstep(-0.15, 0.35, light);\r\n let glow = frame.atmosphere.rgb * frame.atmosphere.a * rim * sunlight;\r\n return vec4f(encodeColor(cloudColor) * opacity + encodeColor(glow), opacity);\r\n}\r\n", f = class {
|
|
78
|
+
constructor(e, t, i, a, s, c = n()) {
|
|
79
|
+
if (this.device = e, this.format = i, this.depthFormat = a, this.samples = c, this.uniformData = /* @__PURE__ */ new Float32Array(48), this.buffers = [], this.textures = [], this.variants = /* @__PURE__ */ new Map(), this.color = {
|
|
80
|
+
view: void 0,
|
|
81
|
+
loadOp: "load",
|
|
82
|
+
storeOp: "store"
|
|
83
|
+
}, this.disposed = !1, this.views = [], o(s.atmosphereColor, 3, "Planet atmosphere color"), !Number.isFinite(Math.fround(s.radius)) || Math.fround(s.radius) <= 0 || !Number.isFinite(Math.fround(s.radius * 1.01))) throw RangeError("Planet radius must be positive and finite");
|
|
84
|
+
if (!Number.isFinite(Math.fround(s.atmosphereIntensity)) || s.atmosphereIntensity < 0 || s.atmosphereColor.some((e) => e < 0)) throw RangeError("Planet atmosphere must be non-negative and finite");
|
|
85
|
+
if (!t.day) throw TypeError("Planet needs a day image");
|
|
86
|
+
for (let n of [
|
|
87
|
+
t.day,
|
|
88
|
+
t.night,
|
|
89
|
+
t.specular,
|
|
90
|
+
t.normal,
|
|
91
|
+
t.clouds
|
|
92
|
+
]) if (n && (!Number.isSafeInteger(n.width) || !Number.isSafeInteger(n.height) || n.width < 1 || n.height < 1 || Math.max(n.width, n.height) > e.limits.maxTextureDimension2D)) throw RangeError("Planet image dimensions exceed GPU limits; downscale the maps to the device maxTextureDimension2D");
|
|
93
|
+
this.uniformData[35] = s.radius, this.uniformData.set(s.atmosphereColor, 40), this.uniformData[43] = s.atmosphereIntensity, this.uniformData[44] = +!!t.normal, this.uniformData[45] = +!!t.clouds, this.uniformData[46] = s.nightIntensity, this.surfaceDepth = {
|
|
94
|
+
view: void 0,
|
|
95
|
+
depthLoadOp: "load",
|
|
96
|
+
depthStoreOp: "store"
|
|
97
|
+
}, this.shellDepth = {
|
|
98
|
+
view: void 0,
|
|
99
|
+
depthReadOnly: !0
|
|
100
|
+
}, a.includes("stencil") && (this.surfaceDepth.stencilReadOnly = !0, this.shellDepth.stencilReadOnly = !0), this.surfaceDescriptor = {
|
|
101
|
+
colorAttachments: [this.color],
|
|
102
|
+
depthStencilAttachment: this.surfaceDepth
|
|
103
|
+
}, this.shellDescriptor = {
|
|
104
|
+
colorAttachments: [this.color],
|
|
105
|
+
depthStencilAttachment: this.shellDepth
|
|
106
|
+
};
|
|
107
|
+
try {
|
|
108
|
+
let n = (t, n) => {
|
|
109
|
+
if (t.byteLength > e.limits.maxBufferSize) throw RangeError("Planet buffers exceed GPU limits; lower the mesh detail");
|
|
110
|
+
let r = e.createBuffer({
|
|
111
|
+
size: t.byteLength,
|
|
112
|
+
usage: n | GPUBufferUsage.COPY_DST
|
|
113
|
+
});
|
|
114
|
+
return this.buffers.push(r), e.queue.writeBuffer(r, 0, t), r;
|
|
115
|
+
};
|
|
116
|
+
this.uniform = n(this.uniformData, GPUBufferUsage.UNIFORM);
|
|
117
|
+
let o = /* @__PURE__ */ new Float32Array(25155), s = /* @__PURE__ */ new Uint32Array(48384), c = 0;
|
|
118
|
+
for (let e = 0; e <= 64; e++) {
|
|
119
|
+
let t = e * Math.PI / 64, n = Math.sin(t), r = Math.cos(t);
|
|
120
|
+
for (let t = 0; t <= 128; t++) {
|
|
121
|
+
let i = t * Math.PI * 2 / 128, a = (e * 129 + t) * 3;
|
|
122
|
+
if (o[a] = n * Math.cos(i), o[a + 1] = r, o[a + 2] = n * Math.sin(i), e < 64 && t < 128) {
|
|
123
|
+
let n = e * 129 + t, r = n + 128 + 1;
|
|
124
|
+
e > 0 && (s[c++] = n, s[c++] = n + 1, s[c++] = r), e < 63 && (s[c++] = n + 1, s[c++] = r + 1, s[c++] = r);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
this.vertex = n(o, GPUBufferUsage.VERTEX), this.index = n(s, GPUBufferUsage.INDEX), this.indexCount = s.length;
|
|
129
|
+
let l = (t, n, r) => {
|
|
130
|
+
let i = e.createTexture({
|
|
131
|
+
size: [t?.width ?? 1, t?.height ?? 1],
|
|
132
|
+
format: n ? "rgba8unorm-srgb" : "rgba8unorm",
|
|
133
|
+
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
|
|
134
|
+
});
|
|
135
|
+
return this.textures.push(i), t ? e.queue.copyExternalImageToTexture({ source: t }, { texture: i }, [t.width, t.height]) : e.queue.writeTexture({ texture: i }, new Uint8Array(r), { bytesPerRow: 4 }, [1, 1]), i.createView();
|
|
136
|
+
};
|
|
137
|
+
this.views = [
|
|
138
|
+
l(t.day, !0, [
|
|
139
|
+
255,
|
|
140
|
+
255,
|
|
141
|
+
255,
|
|
142
|
+
255
|
|
143
|
+
]),
|
|
144
|
+
l(t.night, !0, [
|
|
145
|
+
0,
|
|
146
|
+
0,
|
|
147
|
+
0,
|
|
148
|
+
255
|
|
149
|
+
]),
|
|
150
|
+
l(t.specular, !1, [
|
|
151
|
+
0,
|
|
152
|
+
255,
|
|
153
|
+
0,
|
|
154
|
+
255
|
|
155
|
+
]),
|
|
156
|
+
l(t.normal, !1, [
|
|
157
|
+
128,
|
|
158
|
+
128,
|
|
159
|
+
255,
|
|
160
|
+
255
|
|
161
|
+
]),
|
|
162
|
+
l(t.clouds, !1, [
|
|
163
|
+
0,
|
|
164
|
+
0,
|
|
165
|
+
0,
|
|
166
|
+
255
|
|
167
|
+
])
|
|
168
|
+
], this.sampler = e.createSampler({
|
|
169
|
+
addressModeU: "repeat",
|
|
170
|
+
addressModeV: "clamp-to-edge",
|
|
171
|
+
minFilter: "linear",
|
|
172
|
+
magFilter: "linear"
|
|
173
|
+
}), this.surfaceModule = e.createShaderModule({ code: r + u }), this.shellModule = e.createShaderModule({ code: r + d }), this.variant(i, this.samples, a);
|
|
174
|
+
} catch (e) {
|
|
175
|
+
throw this.dispose(), e;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
variant(e, t, n) {
|
|
179
|
+
if (e !== this.format && e !== "rgba8unorm" && e !== "bgra8unorm" && e !== "rgba16float" && e !== "rg11b10ufloat" || t !== 1 && t !== this.samples) throw new a("unsupported", "Unsupported planet target or device; draw into rgba8unorm, bgra8unorm or rgba16float on the attached device");
|
|
180
|
+
let r = e + ":" + t + ":" + n, o = this.variants.get(r);
|
|
181
|
+
if (o) return o;
|
|
182
|
+
let s = (r, a) => this.device.createRenderPipeline({
|
|
183
|
+
layout: "auto",
|
|
184
|
+
vertex: {
|
|
185
|
+
module: r,
|
|
186
|
+
entryPoint: "vs_main",
|
|
187
|
+
buffers: [{
|
|
188
|
+
arrayStride: 12,
|
|
189
|
+
attributes: [{
|
|
190
|
+
shaderLocation: 0,
|
|
191
|
+
offset: 0,
|
|
192
|
+
format: "float32x3"
|
|
193
|
+
}]
|
|
194
|
+
}]
|
|
195
|
+
},
|
|
196
|
+
fragment: {
|
|
197
|
+
module: r,
|
|
198
|
+
entryPoint: "fs_main",
|
|
199
|
+
constants: { outputHdr: +i(e) },
|
|
200
|
+
targets: [{
|
|
201
|
+
format: e,
|
|
202
|
+
blend: a ? {
|
|
203
|
+
color: {
|
|
204
|
+
srcFactor: "one",
|
|
205
|
+
dstFactor: "one-minus-src-alpha"
|
|
206
|
+
},
|
|
207
|
+
alpha: {
|
|
208
|
+
srcFactor: "one",
|
|
209
|
+
dstFactor: "one-minus-src-alpha"
|
|
210
|
+
}
|
|
211
|
+
} : void 0
|
|
212
|
+
}]
|
|
213
|
+
},
|
|
214
|
+
primitive: {
|
|
215
|
+
topology: "triangle-list",
|
|
216
|
+
cullMode: "back"
|
|
217
|
+
},
|
|
218
|
+
multisample: { count: t },
|
|
219
|
+
depthStencil: {
|
|
220
|
+
format: n,
|
|
221
|
+
depthWriteEnabled: !a,
|
|
222
|
+
depthCompare: "greater-equal"
|
|
223
|
+
}
|
|
224
|
+
}), c = s(this.surfaceModule, !1), l = s(this.shellModule, !0), u = {
|
|
225
|
+
surface: c,
|
|
226
|
+
shell: l,
|
|
227
|
+
surfaceGroup: this.device.createBindGroup({
|
|
228
|
+
layout: c.getBindGroupLayout(0),
|
|
229
|
+
entries: [
|
|
230
|
+
{
|
|
231
|
+
binding: 0,
|
|
232
|
+
resource: { buffer: this.uniform }
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
binding: 1,
|
|
236
|
+
resource: this.sampler
|
|
237
|
+
},
|
|
238
|
+
...this.views.slice(0, 4).map((e, t) => ({
|
|
239
|
+
binding: t + 2,
|
|
240
|
+
resource: e
|
|
241
|
+
}))
|
|
242
|
+
]
|
|
243
|
+
}),
|
|
244
|
+
shellGroup: this.device.createBindGroup({
|
|
245
|
+
layout: l.getBindGroupLayout(0),
|
|
246
|
+
entries: [
|
|
247
|
+
{
|
|
248
|
+
binding: 0,
|
|
249
|
+
resource: { buffer: this.uniform }
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
binding: 1,
|
|
253
|
+
resource: this.sampler
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
binding: 2,
|
|
257
|
+
resource: this.views[4]
|
|
258
|
+
}
|
|
259
|
+
]
|
|
260
|
+
})
|
|
261
|
+
};
|
|
262
|
+
return this.variants.set(r, u), u;
|
|
263
|
+
}
|
|
264
|
+
updateFrame(e, t, n, r, i) {
|
|
265
|
+
if (this.disposed) throw new a("disposed", "Planet pass is disposed; create a new Planet");
|
|
266
|
+
o(e, 16, "Planet camera matrix"), o(t, 16, "Planet model matrix"), o(n, 3, "Planet camera position"), o(r, 3, "Planet sun direction");
|
|
267
|
+
let s = Math.hypot(r[0], r[1], r[2]);
|
|
268
|
+
if (!s || !Number.isFinite(i)) throw RangeError("Planet sun must be nonzero and cloud offset finite");
|
|
269
|
+
let c = this.uniformData;
|
|
270
|
+
c.set(e, 0), c.set(t, 16), c.set(n, 32), c[36] = r[0] / s, c[37] = r[1] / s, c[38] = r[2] / s, c[39] = (i % 1 + 1) % 1, this.device.queue.writeBuffer(this.uniform, 0, c);
|
|
271
|
+
}
|
|
272
|
+
record(e, n) {
|
|
273
|
+
if (this.disposed) throw new a("disposed", "Planet pass is disposed; create a new Planet");
|
|
274
|
+
if (e.device !== this.device) throw new a("unsupported", "Unsupported planet target or device; draw into rgba8unorm, bgra8unorm or rgba16float on the attached device");
|
|
275
|
+
let r = e.descriptor.depthStencil?.format ?? this.depthFormat, i = this.variant(e.format, e.descriptor.multisample?.count ?? 1, r);
|
|
276
|
+
this.surfaceDepth.stencilReadOnly = this.shellDepth.stencilReadOnly = r.includes("stencil") || void 0, this.color.view = e.color, this.surfaceDepth.view = this.shellDepth.view = e.depth;
|
|
277
|
+
let o = e.encoder.beginRenderPass(n ? this.shellDescriptor : this.surfaceDescriptor);
|
|
278
|
+
o.setPipeline(n ? i.shell : i.surface), o.setBindGroup(0, n ? i.shellGroup : i.surfaceGroup), o.setVertexBuffer(0, this.vertex), o.setIndexBuffer(this.index, "uint32"), t(o, e), o.drawIndexed(this.indexCount), o.end();
|
|
279
|
+
}
|
|
280
|
+
dispose() {
|
|
281
|
+
if (!this.disposed) {
|
|
282
|
+
this.disposed = !0;
|
|
283
|
+
for (let e of this.buffers) e.destroy();
|
|
284
|
+
for (let e of this.textures) e.destroy();
|
|
285
|
+
this.buffers.length = this.textures.length = this.views.length = 0, this.variants.clear(), this.sampler = void 0, this.surfaceModule = this.shellModule = void 0;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/planet.ts
|
|
291
|
+
function p(e) {
|
|
292
|
+
if (!e || typeof e != "object") throw TypeError("Expected planet map options");
|
|
293
|
+
if (e.signal !== void 0 && !(e.signal instanceof AbortSignal)) throw TypeError("Expected AbortSignal");
|
|
294
|
+
if (e.fetch !== void 0 && typeof e.fetch != "function") throw TypeError("Planet fetch is not a function; pass a fetch-compatible function or leave it out");
|
|
295
|
+
let t = [];
|
|
296
|
+
for (let [n, r] of [
|
|
297
|
+
["day", e.dayMap],
|
|
298
|
+
["night", e.nightMap],
|
|
299
|
+
["specular", e.specularRoughnessMap],
|
|
300
|
+
["normal", e.normalMap],
|
|
301
|
+
["clouds", e.clouds]
|
|
302
|
+
]) if (r !== void 0 || n === "day") {
|
|
303
|
+
if (!(typeof r == "string" && r.trim() || r instanceof URL)) throw TypeError(`Expected ${n} map URL`);
|
|
304
|
+
t.push([n, String(r)]);
|
|
305
|
+
}
|
|
306
|
+
return t;
|
|
307
|
+
}
|
|
308
|
+
var m = class e {coreRequirement=__margoCoreRequirement("0.1.0",import.meta.url);
|
|
309
|
+
constructor(e) {
|
|
310
|
+
if (this.name = "planet", this.controller = new AbortController(), this.images = { day: void 0 }, this.sources = [], this.sun = /* @__PURE__ */ new Float32Array(3), this.disposed = !1, this.offset = 0, this.surface = (e) => {
|
|
311
|
+
this.pass && e.backend === "webgpu" && (this.pass.updateFrame(e.camera.matrix, this.spin.matrix, e.camera.position, this.sun, this.offset), this.pass.record(e, !1));
|
|
312
|
+
}, this.shell = (e) => {
|
|
313
|
+
this.pass && e.backend === "webgpu" && this.pass.record(e, !0);
|
|
314
|
+
}, !e || typeof e != "object") throw TypeError("Expected planet options");
|
|
315
|
+
let t = e.radius ?? 1, n = e.tilt ?? 23.4, r = e.atmosphere?.intensity ?? 1.2, i = e.atmosphere?.color ?? [
|
|
316
|
+
.3,
|
|
317
|
+
.6,
|
|
318
|
+
1
|
|
319
|
+
];
|
|
320
|
+
for (let e of [
|
|
321
|
+
t * 1.01,
|
|
322
|
+
n,
|
|
323
|
+
r
|
|
324
|
+
]) if (typeof e != "number" || !Number.isFinite(Math.fround(e))) throw RangeError("Planet parameters must be finite Float32 numbers");
|
|
325
|
+
if (typeof t != "number" || Math.fround(t) <= 0 || r < 0) throw RangeError("Planet radius must be positive and atmosphere intensity non-negative");
|
|
326
|
+
if (e.atmosphere !== void 0 && (!e.atmosphere || typeof e.atmosphere != "object")) throw TypeError("Expected atmosphere options");
|
|
327
|
+
if (o(i, 3, "Atmosphere color"), i.some((e) => e < 0)) throw RangeError("Atmosphere color must be non-negative");
|
|
328
|
+
this.config = {
|
|
329
|
+
radius: t,
|
|
330
|
+
atmosphereColor: [
|
|
331
|
+
i[0],
|
|
332
|
+
i[1],
|
|
333
|
+
i[2]
|
|
334
|
+
],
|
|
335
|
+
atmosphereIntensity: r,
|
|
336
|
+
nightIntensity: 3
|
|
337
|
+
}, this.tilt = n * Math.PI / 180, this.setSunDirection(e.sunDirection ?? [
|
|
338
|
+
-1,
|
|
339
|
+
.2,
|
|
340
|
+
.5
|
|
341
|
+
]), this.sources.push(...p(e)), this.fetcher = e.fetch ?? ((e, t) => globalThis.fetch(e, t));
|
|
342
|
+
let a = this;
|
|
343
|
+
this.clouds = {
|
|
344
|
+
get offsetU() {
|
|
345
|
+
return a.check(), a.offset;
|
|
346
|
+
},
|
|
347
|
+
set offsetU(e) {
|
|
348
|
+
if (a.check(), typeof e != "number" || !Number.isFinite(e)) throw RangeError("Cloud offset must be finite");
|
|
349
|
+
a.offset = (e % 1 + 1) % 1;
|
|
350
|
+
}
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
static attach(t, n) {__margoAssertCore(t,__margoCoreRequirement("0.1.0",import.meta.url));
|
|
354
|
+
return e.create(t, n);
|
|
355
|
+
}
|
|
356
|
+
static async create(t, n) {
|
|
357
|
+
let r = new e(n), i = n.signal ? AbortSignal.any([n.signal, r.controller.signal]) : r.controller.signal;
|
|
358
|
+
try {
|
|
359
|
+
return i.throwIfAborted(), t.use(r), await r.load(i), r;
|
|
360
|
+
} catch (e) {
|
|
361
|
+
throw r.dispose(), e;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
get rotation() {
|
|
365
|
+
if (this.check(), !this.spin) throw new a("state", "Planet is not attached; await Planet.attach(engine) first");
|
|
366
|
+
return this.spin.rotation;
|
|
367
|
+
}
|
|
368
|
+
async updateMaps(e) {
|
|
369
|
+
if (this.check(), !this.pass || !this.engine) throw new a("state", "Planet is not ready; await Planet.attach(engine) first");
|
|
370
|
+
if (this.upgrading) throw new a("state", "Planet map update is already pending; await the running update first");
|
|
371
|
+
let t = p(e), n = new AbortController();
|
|
372
|
+
this.upgrading = n;
|
|
373
|
+
let r = AbortSignal.any([
|
|
374
|
+
this.controller.signal,
|
|
375
|
+
n.signal,
|
|
376
|
+
...e.signal ? [e.signal] : []
|
|
377
|
+
]), i = { day: void 0 }, o = new Set(Object.values(this.images)), s, c = !1;
|
|
378
|
+
try {
|
|
379
|
+
for (await this.readMaps(t, i, r, n, this.images, e.fetch);;) {
|
|
380
|
+
r.throwIfAborted(), this.check();
|
|
381
|
+
let e = this.engine.gpu;
|
|
382
|
+
e.pushErrorScope("validation");
|
|
383
|
+
try {
|
|
384
|
+
s = this.createPass(i, e);
|
|
385
|
+
} catch (t) {
|
|
386
|
+
if (await e.popErrorScope().catch(() => null), r.throwIfAborted(), this.check(), this.engine?.gpu !== e) continue;
|
|
387
|
+
throw t;
|
|
388
|
+
}
|
|
389
|
+
let n = null;
|
|
390
|
+
try {
|
|
391
|
+
n = await e.popErrorScope();
|
|
392
|
+
} catch (t) {
|
|
393
|
+
if (r.throwIfAborted(), this.check(), this.engine?.gpu === e) throw t;
|
|
394
|
+
}
|
|
395
|
+
if (r.throwIfAborted(), this.check(), this.engine.gpu !== e) {
|
|
396
|
+
s.dispose(), s = void 0;
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (n) throw new a("failed", `Planet GPU initialization failed: ${n.message}; check the maps against the device limits and attach again`);
|
|
400
|
+
let l = this.pass;
|
|
401
|
+
for (let e of Object.keys(this.images)) delete this.images[e];
|
|
402
|
+
Object.assign(this.images, i), this.sources.length = 0, this.sources.push(...t), this.pass = s, c = !0, l?.dispose();
|
|
403
|
+
let u = new Set(Object.values(i));
|
|
404
|
+
for (let e of o) u.has(e) || e.close();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
} finally {
|
|
408
|
+
if (!c) {
|
|
409
|
+
s?.dispose();
|
|
410
|
+
for (let e of new Set(Object.values(i))) e && !o.has(e) && e.close();
|
|
411
|
+
}
|
|
412
|
+
this.upgrading = void 0;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
setSunDirection(e) {
|
|
416
|
+
this.check(), o(e, 3, "Sun direction");
|
|
417
|
+
let t = Math.hypot(e[0], e[1], e[2]);
|
|
418
|
+
if (!t) throw RangeError("Sun direction must be nonzero");
|
|
419
|
+
for (let n = 0; n < 3; n++) this.sun[n] = e[n] / t;
|
|
420
|
+
}
|
|
421
|
+
check() {
|
|
422
|
+
if (this.disposed) throw new a("disposed", "Planet is disposed; create a new Planet");
|
|
423
|
+
}
|
|
424
|
+
setup(e) {
|
|
425
|
+
if (this.check(), this.engine) throw new a("attached", "Planet is already attached; create a new Planet");
|
|
426
|
+
if (!e.gpu) throw new a("unsupported", "Planet requires WebGPU; use a WebGPU engine; see https://margoengine.com/errors#webgpu");
|
|
427
|
+
this.engine = e, this.parent = e.createMesh(), this.parent.rotation.z = this.tilt, this.spin = e.createMesh(this.parent);
|
|
428
|
+
}
|
|
429
|
+
registerPasses(e) {
|
|
430
|
+
e.add({
|
|
431
|
+
name: "planet:surface",
|
|
432
|
+
order: 3,
|
|
433
|
+
execute: this.surface
|
|
434
|
+
}), e.add({
|
|
435
|
+
name: "planet:shell",
|
|
436
|
+
order: 4,
|
|
437
|
+
execute: this.shell
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
async load(e) {
|
|
441
|
+
await this.readMaps(this.sources, this.images, e, this.controller), e.throwIfAborted(), this.check();
|
|
442
|
+
let t = this.engine.gpu;
|
|
443
|
+
t.pushErrorScope("validation");
|
|
444
|
+
let n;
|
|
445
|
+
try {
|
|
446
|
+
n = this.createPass();
|
|
447
|
+
} catch (e) {
|
|
448
|
+
throw await t.popErrorScope().catch(() => null), e;
|
|
449
|
+
}
|
|
450
|
+
try {
|
|
451
|
+
let r = await t.popErrorScope();
|
|
452
|
+
if (e.throwIfAborted(), this.check(), r) throw new a("failed", `Planet GPU initialization failed: ${r.message}; check the maps against the device limits and attach again`);
|
|
453
|
+
this.engine.gpu === t ? this.pass = n : this.pass ||= this.createPass();
|
|
454
|
+
} finally {
|
|
455
|
+
this.pass !== n && n.dispose();
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
async readMaps(e, t, n, r, i, a = this.fetcher) {
|
|
459
|
+
let o = new Set(Object.values(i ?? {}).filter(Boolean)), s = [...o].reduce((e, t) => e + t.width * t.height * 4, 0), c = e.map(async ([e, r]) => {
|
|
460
|
+
n.throwIfAborted();
|
|
461
|
+
let i = await l(await a(r, { signal: n }), {
|
|
462
|
+
maxBytes: 33554432,
|
|
463
|
+
signal: n,
|
|
464
|
+
label: `Planet ${e} map`
|
|
465
|
+
}), c = await createImageBitmap(new Blob([i]), {
|
|
466
|
+
colorSpaceConversion: "none",
|
|
467
|
+
premultiplyAlpha: "none"
|
|
468
|
+
});
|
|
469
|
+
try {
|
|
470
|
+
n.throwIfAborted(), this.check();
|
|
471
|
+
let r = this.engine.gpu.limits.maxTextureDimension2D;
|
|
472
|
+
if (o.has(c) || (s += c.width * c.height * 4), !Number.isSafeInteger(c.width) || !Number.isSafeInteger(c.height) || c.width < 1 || c.height < 1 || c.width > r || c.height > r || s > 268435456) throw RangeError("Planet maps exceed GPU dimensions or the 256 MiB decoded budget; use smaller maps");
|
|
473
|
+
o.add(c), t[e] = c;
|
|
474
|
+
} catch (e) {
|
|
475
|
+
throw o.has(c) || c.close(), e;
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
try {
|
|
479
|
+
await Promise.all(c);
|
|
480
|
+
} catch (e) {
|
|
481
|
+
throw r.abort(e), await Promise.allSettled(c), e;
|
|
482
|
+
}
|
|
483
|
+
n.throwIfAborted(), this.check();
|
|
484
|
+
}
|
|
485
|
+
createPass(e = this.images, t = this.engine.gpu) {
|
|
486
|
+
return new f(t, e, navigator.gpu.getPreferredCanvasFormat(), t.features.has("depth32float-stencil8") ? "depth32float-stencil8" : "depth32float", this.config, n(this.engine));
|
|
487
|
+
}
|
|
488
|
+
onDeviceRestored(e) {
|
|
489
|
+
if (this.check(), e !== this.engine) throw new a("attached", "Planet belongs to another engine; use objects created by the same engine");
|
|
490
|
+
this.pass?.dispose(), this.pass = void 0, this.sources.every(([e]) => !!this.images[e]) && (this.pass = this.createPass());
|
|
491
|
+
}
|
|
492
|
+
dispose() {
|
|
493
|
+
if (!this.disposed) {
|
|
494
|
+
this.disposed = !0, this.controller.abort(new DOMException("Planet disposed", "AbortError")), this.engine?.sceneOf(this)?.removePlugin(this.name), this.pass?.dispose(), this.pass = void 0;
|
|
495
|
+
for (let e of new Set(Object.values(this.images))) e?.close();
|
|
496
|
+
for (let e of Object.keys(this.images)) delete this.images[e];
|
|
497
|
+
this.spin?.dispose(), this.parent?.dispose(), this.spin = this.parent = void 0, this.engine = void 0;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
[Symbol.dispose ?? Symbol()]() {
|
|
501
|
+
this.dispose();
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
//#endregion
|
|
505
|
+
export { m as t };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { EngineKernel } from '@margoengine/core';
|
|
3
|
+
import type { Engine, EnginePlugin, FrameGraph } from '@margoengine/core/extension';
|
|
4
|
+
// @ts-ignore optional peer: its types apply when it is installed
|
|
5
|
+
import type { AtmosphereParameters } from '@margoengine/sky/planet';
|
|
6
|
+
/** A physical planet in arbitrary world units; atmosphere distances and density scales are kilometres. */
|
|
7
|
+
export interface OrbitAtmosphereOptions {
|
|
8
|
+
/** World-space planet centre. Default [0, 0, 0]. */
|
|
9
|
+
center?: readonly [number, number, number];
|
|
10
|
+
/** World-space ground radius. Default 1. */
|
|
11
|
+
radius?: number;
|
|
12
|
+
/** Unit direction toward the sun, normalized internally. Default [1, 0, 0]. */
|
|
13
|
+
sun?: readonly [number, number, number];
|
|
14
|
+
/** Hillaire medium. Defaults to Earth; groundAlbedo also defines the rendered ground. */
|
|
15
|
+
atmosphere?: AtmosphereParameters;
|
|
16
|
+
/** Solar illuminance. Default 20, range 0..1000000. */
|
|
17
|
+
illuminance?: number;
|
|
18
|
+
/** Exposure stops. Default 0, range -20..20. */
|
|
19
|
+
exposure?: number;
|
|
20
|
+
}
|
|
21
|
+
/** Full physical ground, terminator and atmospheric limb from orbit, using sky's shared Hillaire tables. */
|
|
22
|
+
export declare class OrbitAtmosphere implements EnginePlugin {
|
|
23
|
+
#private;
|
|
24
|
+
readonly options: OrbitAtmosphereOptions;
|
|
25
|
+
readonly name: string;
|
|
26
|
+
enabled: boolean;
|
|
27
|
+
constructor(options?: OrbitAtmosphereOptions);
|
|
28
|
+
/** Attach to a scene, drawing only at clear depth behind other geometry. */
|
|
29
|
+
static attach(engine: EngineKernel, options?: OrbitAtmosphereOptions): OrbitAtmosphere;
|
|
30
|
+
setup(engine: Engine): void;
|
|
31
|
+
registerPasses(graph: FrameGraph): void;
|
|
32
|
+
/** Remove expired per-view resources after a moving frame. */
|
|
33
|
+
onPreUpdate(delta: number): void;
|
|
34
|
+
onDeviceRestored(engine: Engine): void;
|
|
35
|
+
dispose(): void;
|
|
36
|
+
[Symbol.dispose](): void;
|
|
37
|
+
}
|