@holoscript/core 8.0.12 → 8.0.14
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 +14 -0
- package/bin/holo-decision.mjs +133 -0
- package/dist/{GLTFPipeline-L76YTXKA.js → GLTFPipeline-6LS3UDYX.js} +6 -6
- package/dist/{GLTFPipeline-ZWJN7GI6.cjs → GLTFPipeline-VTI6PRRQ.cjs} +10 -10
- package/dist/{HoloCompositionParser-FLBZKFD3.js → HoloCompositionParser-GILABNN3.js} +5 -5
- package/dist/{HoloCompositionParser-3XEC7IJC.cjs → HoloCompositionParser-ZVRYVEA5.cjs} +12 -12
- package/dist/{HoloScriptPlusParser-RNUCHOAJ.cjs → HoloScriptPlusParser-WTPRPRPR.cjs} +8 -8
- package/dist/{HoloScriptPlusParser-55AY4MUF.js → HoloScriptPlusParser-ZP53ECSL.js} +5 -5
- package/dist/{SceneIRCompiler-KWUFHZ4O.cjs → SceneIRCompiler-7KQ5NKAU.cjs} +11 -11
- package/dist/{SceneIRCompiler-63KA3M2F.js → SceneIRCompiler-VX75RB6H.js} +6 -6
- package/dist/{chunk-EM2OGPYE.js → chunk-2GKF7JQ6.js} +3 -3
- package/dist/{chunk-HVX7QQYK.js → chunk-3KHRZTNW.js} +3 -3
- package/dist/{chunk-UTUD6GJI.js → chunk-42Z3SVHQ.js} +3 -3
- package/dist/{chunk-Z4IUBW46.js → chunk-47XZ62KM.js} +112 -55
- package/dist/{chunk-GGR7QUKK.cjs → chunk-4CRMVEON.cjs} +37 -37
- package/dist/{chunk-QBY7LRN2.js → chunk-4NEPHK2K.js} +3 -3
- package/dist/{chunk-65S62L4D.cjs → chunk-5B2AJKA5.cjs} +20 -20
- package/dist/{chunk-PJFTTSKE.js → chunk-5T6BWIB7.js} +4 -4
- package/dist/{chunk-KFVNCW4Q.js → chunk-62OYZT6Q.js} +3 -3
- package/dist/{chunk-3KMANSQJ.cjs → chunk-645EWDZW.cjs} +4 -4
- package/dist/{chunk-BP4KTMLY.js → chunk-6FCNMTL5.js} +4 -4
- package/dist/{chunk-77QI552M.js → chunk-73EDVY5D.js} +3 -3
- package/dist/{chunk-7IDJDMPU.cjs → chunk-7FYSSI3C.cjs} +27 -2
- package/dist/{chunk-WNYXT4TE.cjs → chunk-7ITJP33Q.cjs} +3 -3
- package/dist/chunk-7ZKQ32XK.js +1571 -0
- package/dist/{chunk-P7JEKJTZ.cjs → chunk-B6JHTTMR.cjs} +25 -14
- package/dist/{chunk-FG6PYGIZ.js → chunk-BLCWQ225.js} +4 -4
- package/dist/{chunk-7NPZZZD7.js → chunk-C73GVAYC.js} +16 -9
- package/dist/{chunk-RMOEL2GD.cjs → chunk-CESMPUEZ.cjs} +3794 -1137
- package/dist/{chunk-SQXAZHDG.js → chunk-CHIZSMWD.js} +3 -3
- package/dist/{chunk-G35CAQ7I.cjs → chunk-D44CEFY4.cjs} +5 -5
- package/dist/chunk-ERYKKPHW.cjs +1884 -0
- package/dist/{chunk-TYNRXRHF.js → chunk-EYHPTJ3F.js} +3695 -1045
- package/dist/{chunk-J3UHV5T4.cjs → chunk-FGXJPJ3W.cjs} +4 -4
- package/dist/{chunk-IJU6AS67.cjs → chunk-FHR2AVMA.cjs} +23 -23
- package/dist/{chunk-LMB555QB.cjs → chunk-FOM36K5I.cjs} +18 -18
- package/dist/{chunk-JBYBMBMW.js → chunk-FWKEY4LD.js} +3 -2
- package/dist/{chunk-ZRPALBQB.cjs → chunk-GGH3ZYDB.cjs} +5 -5
- package/dist/{chunk-2BYAUHBO.js → chunk-GI5YYZH6.js} +3 -3
- package/dist/{chunk-VTEXJOSD.cjs → chunk-GYJLP4BO.cjs} +30 -30
- package/dist/{chunk-OC6GV5QH.cjs → chunk-HCONR4J6.cjs} +38 -38
- package/dist/{chunk-B3CWTCK2.js → chunk-IA7FCSRY.js} +27 -2
- package/dist/{chunk-5FYMCVBY.js → chunk-IXFNA5TT.js} +4 -4
- package/dist/{chunk-G6KACNMO.js → chunk-JCVSCKUH.js} +4 -4
- package/dist/{chunk-FHVPN7BN.js → chunk-JQCXLKAU.js} +3 -3
- package/dist/{chunk-F3YT6GUF.cjs → chunk-KLM6QD6D.cjs} +14 -14
- package/dist/{chunk-UGQ3QNV6.js → chunk-KOGPVAP6.js} +24 -8
- package/dist/{chunk-CKV5NYAE.js → chunk-KYJD2UMV.js} +4 -4
- package/dist/{chunk-LB3ZZMKT.cjs → chunk-L4DBUX3N.cjs} +10 -10
- package/dist/{chunk-RXQNRZ5F.js → chunk-LGRKBTPM.js} +3 -3
- package/dist/{chunk-OMNHTCS4.cjs → chunk-LHUDAJQT.cjs} +3 -2
- package/dist/{chunk-PDXX5UI4.cjs → chunk-LQNL3IYV.cjs} +38 -38
- package/dist/{chunk-WWNCM473.js → chunk-M7XR3F53.js} +4 -4
- package/dist/{chunk-IFW5IW4L.cjs → chunk-ML4V5PVU.cjs} +670 -627
- package/dist/{chunk-656IAS7B.cjs → chunk-N7IYRAFG.cjs} +21 -14
- package/dist/chunk-N7MIIDLI.cjs +1605 -0
- package/dist/{chunk-USOUXQYD.js → chunk-NWKARJM2.js} +130 -22
- package/dist/{chunk-VVAAZITA.cjs → chunk-OOP4S3GU.cjs} +5 -5
- package/dist/{chunk-TX6ZBIF4.js → chunk-ORBVCOTC.js} +3 -3
- package/dist/{chunk-RRQIAJLQ.cjs → chunk-OSKGCXAQ.cjs} +5 -5
- package/dist/{chunk-QEDMOGF3.cjs → chunk-P65OIMJT.cjs} +7 -7
- package/dist/{chunk-QM7FJB5W.js → chunk-PSWCJQS4.js} +4 -4
- package/dist/{chunk-7N63JHUN.cjs → chunk-PZ46WRHX.cjs} +8 -8
- package/dist/{chunk-AATYYLYK.js → chunk-Q4J2K7FI.js} +16 -5
- package/dist/chunk-QLPRI3QW.js +1875 -0
- package/dist/{chunk-WTH23ISI.js → chunk-R5VKLUMR.js} +4 -4
- package/dist/{chunk-NQN5LFN4.cjs → chunk-REN3LHO6.cjs} +19 -19
- package/dist/{chunk-JEJ7TT54.js → chunk-RVN7OPCP.js} +4 -4
- package/dist/{chunk-ZK7KX5CD.cjs → chunk-SE2Y5BSL.cjs} +6 -6
- package/dist/{chunk-2X3IPCBK.cjs → chunk-SKVBUGMC.cjs} +7 -7
- package/dist/{chunk-GZPUA2TL.cjs → chunk-SNMQUWIO.cjs} +10 -10
- package/dist/{chunk-OIBRWLPY.cjs → chunk-TKDDIE3T.cjs} +54 -3
- package/dist/{chunk-5H5GFBMG.cjs → chunk-TMNAKNV2.cjs} +112 -55
- package/dist/{chunk-5LVFTGKH.js → chunk-TZQKJOPC.js} +3 -3
- package/dist/{chunk-TSTFYGGX.js → chunk-UKEK2NZJ.js} +3 -3
- package/dist/{chunk-3IUHTGDD.cjs → chunk-VA7THINF.cjs} +38 -38
- package/dist/{chunk-Z557DVYN.js → chunk-VMCR5QVI.js} +4 -4
- package/dist/{chunk-IS7NTKY5.cjs → chunk-VTFLKSJL.cjs} +14 -14
- package/dist/{chunk-VO52AJD5.js → chunk-VXHB6ZHY.js} +4 -4
- package/dist/{chunk-S4O7CFHQ.cjs → chunk-VZI7S7P6.cjs} +7 -7
- package/dist/{chunk-BQJ3YBF3.js → chunk-W4AJMMQR.js} +667 -628
- package/dist/{chunk-2KKOYZYI.cjs → chunk-XZC75AJ4.cjs} +5 -5
- package/dist/{chunk-FWCHPCD4.js → chunk-Y2OCKXVD.js} +4 -4
- package/dist/{chunk-EI5G5JLY.cjs → chunk-YBM34FUJ.cjs} +134 -22
- package/dist/{chunk-DUG3OQAS.js → chunk-YDSNDCV3.js} +3 -3
- package/dist/{chunk-566ZB4LZ.cjs → chunk-YM7UUYR5.cjs} +5 -5
- package/dist/{chunk-BCP4E7MD.cjs → chunk-YMI3ED3B.cjs} +26 -10
- package/dist/{chunk-CP7V3ATL.js → chunk-Z7NUL73U.js} +51 -4
- package/dist/cli/holoscript-runner.cjs +11 -11
- package/dist/cli/holoscript-runner.js +4 -4
- package/dist/compiler/agent-inference.cjs +8 -8
- package/dist/compiler/agent-inference.js +4 -4
- package/dist/compiler/android-xr.cjs +7 -7
- package/dist/compiler/android-xr.js +5 -5
- package/dist/compiler/android.cjs +6 -6
- package/dist/compiler/android.js +4 -4
- package/dist/compiler/context.cjs +6 -6
- package/dist/compiler/context.js +3 -3
- package/dist/compiler/daimon-seed.cjs +9 -9
- package/dist/compiler/daimon-seed.js +4 -4
- package/dist/compiler/domain-block-utils.cjs +165 -165
- package/dist/compiler/domain-block-utils.js +4 -4
- package/dist/compiler/dtdl.cjs +7 -7
- package/dist/compiler/dtdl.js +4 -4
- package/dist/compiler/gltf-pipeline.cjs +8 -8
- package/dist/compiler/gltf-pipeline.js +1 -1
- package/dist/compiler/godot.cjs +6 -6
- package/dist/compiler/godot.js +5 -5
- package/dist/compiler/incremental.cjs +8 -8
- package/dist/compiler/incremental.js +3 -3
- package/dist/compiler/index.cjs +871 -207
- package/dist/compiler/index.d.ts +21 -1
- package/dist/compiler/index.js +696 -60
- package/dist/compiler/ios.cjs +6 -6
- package/dist/compiler/ios.js +4 -4
- package/dist/compiler/llm-provider-capabilities.cjs +6 -6
- package/dist/compiler/llm-provider-capabilities.js +3 -3
- package/dist/compiler/openxr.cjs +6 -6
- package/dist/compiler/openxr.js +5 -5
- package/dist/compiler/sdf.cjs +7 -7
- package/dist/compiler/sdf.js +5 -5
- package/dist/compiler/state.cjs +6 -6
- package/dist/compiler/state.js +4 -4
- package/dist/compiler/trait-composition.cjs +7 -7
- package/dist/compiler/trait-composition.js +3 -3
- package/dist/compiler/unity.cjs +6 -6
- package/dist/compiler/unity.js +5 -5
- package/dist/compiler/unreal.cjs +7 -7
- package/dist/compiler/unreal.js +5 -5
- package/dist/compiler/urdf.cjs +14 -14
- package/dist/compiler/urdf.js +5 -5
- package/dist/compiler/usd-physics.cjs +7 -7
- package/dist/compiler/usd-physics.js +4 -4
- package/dist/compiler/visionos.cjs +6 -6
- package/dist/compiler/visionos.js +5 -5
- package/dist/compiler/vrchat.cjs +7 -7
- package/dist/compiler/vrchat.js +5 -5
- package/dist/compiler/wasm.cjs +10 -10
- package/dist/compiler/wasm.js +5 -5
- package/dist/compiler/webgpu.cjs +6 -6
- package/dist/compiler/webgpu.js +5 -5
- package/dist/constants.cjs +3 -3
- package/dist/constants.js +1 -1
- package/dist/entries/scripting.cjs +6 -6
- package/dist/entries/scripting.js +2 -2
- package/dist/evolution/index.cjs +21 -17
- package/dist/evolution/index.d.ts +9 -0
- package/dist/evolution/index.js +6 -6
- package/dist/index.cjs +930 -703
- package/dist/index.d.ts +70 -5
- package/dist/index.js +192 -109
- package/dist/parser.cjs +19 -19
- package/dist/parser.js +5 -5
- package/dist/reconstruction/index.cjs +127 -38
- package/dist/reconstruction/index.d.ts +159 -0
- package/dist/reconstruction/index.js +11 -2
- package/dist/runtime.d.ts +1 -0
- package/dist/traitDocs/traitDocs.cjs +8 -8
- package/dist/traitDocs/traitDocs.js +2 -2
- package/dist/traits/index.cjs +566 -471
- package/dist/traits/index.js +93 -11
- package/package.json +25 -41
- package/dist/chunk-G3BM3CT4.cjs +0 -639
- package/dist/chunk-KEYG7HGY.js +0 -1062
- package/dist/chunk-QX34ELJ7.js +0 -625
- package/dist/chunk-W2GA3EQB.cjs +0 -1065
package/dist/chunk-KEYG7HGY.js
DELETED
|
@@ -1,1062 +0,0 @@
|
|
|
1
|
-
import { init_DomainBlockCompilerMixin, datavizToWebGPU, compileDataVizBlock, compileMaterialBlock, materialToWebGPU, compileParticleBlock, compilePostProcessingBlock, compileProceduralBlock } from './chunk-5LVFTGKH.js';
|
|
2
|
-
import { init_CompilerBase, CompilerBase } from './chunk-DUG3OQAS.js';
|
|
3
|
-
import { __export, __esm } from './chunk-W4GABFCR.js';
|
|
4
|
-
import { ANSCapabilityPath } from '@holoscript/core-types/ans';
|
|
5
|
-
|
|
6
|
-
// src/compiler/WebGPUCompiler.ts
|
|
7
|
-
var WebGPUCompiler_exports = {};
|
|
8
|
-
__export(WebGPUCompiler_exports, {
|
|
9
|
-
WebGPUCompiler: () => WebGPUCompiler
|
|
10
|
-
});
|
|
11
|
-
function isGpuParticleTraitName(name) {
|
|
12
|
-
return typeof name === "string" && (name === "gpu_particle" || name.startsWith("vfx_particle_"));
|
|
13
|
-
}
|
|
14
|
-
function findGpuParticleTrait(traits) {
|
|
15
|
-
return traits?.find((t) => isGpuParticleTraitName(t.name));
|
|
16
|
-
}
|
|
17
|
-
var WebGPUCompiler;
|
|
18
|
-
var init_WebGPUCompiler = __esm({
|
|
19
|
-
"src/compiler/WebGPUCompiler.ts"() {
|
|
20
|
-
init_CompilerBase();
|
|
21
|
-
init_DomainBlockCompilerMixin();
|
|
22
|
-
WebGPUCompiler = class extends CompilerBase {
|
|
23
|
-
constructor(options = {}) {
|
|
24
|
-
super();
|
|
25
|
-
this.compilerName = "WebGPUCompiler";
|
|
26
|
-
this.lines = [];
|
|
27
|
-
this.indentLevel = 0;
|
|
28
|
-
this.objectIndex = 0;
|
|
29
|
-
this.options = {
|
|
30
|
-
entryPoint: options.entryPoint || "main",
|
|
31
|
-
enableCompute: options.enableCompute ?? true,
|
|
32
|
-
msaa: options.msaa || 4,
|
|
33
|
-
indent: options.indent || " ",
|
|
34
|
-
provenanceHash: options.provenanceHash ?? ""
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
getRequiredCapability() {
|
|
38
|
-
return ANSCapabilityPath.WEBGPU;
|
|
39
|
-
}
|
|
40
|
-
compile(composition, agentToken, outputPath) {
|
|
41
|
-
this.validateCompilerAccess(agentToken, outputPath);
|
|
42
|
-
this.lines = [];
|
|
43
|
-
this.indentLevel = 0;
|
|
44
|
-
this.objectIndex = 0;
|
|
45
|
-
this.emit("// Auto-generated by HoloScript WebGPUCompiler");
|
|
46
|
-
this.emit(
|
|
47
|
-
`// Source: composition "${this.escapeStringValue(composition.name, "TypeScript")}"`
|
|
48
|
-
);
|
|
49
|
-
this.emit("// Do not edit manually \u2014 regenerate from .holo source");
|
|
50
|
-
if (this.options.provenanceHash) {
|
|
51
|
-
this.emit(`// Provenance Hash: ${this.options.provenanceHash}`);
|
|
52
|
-
}
|
|
53
|
-
this.emit("");
|
|
54
|
-
this.emitDeviceInit();
|
|
55
|
-
this.emitGeometryHelpers();
|
|
56
|
-
this.emitShaderSources();
|
|
57
|
-
if (composition.environment) this.emitEnvironment(composition.environment);
|
|
58
|
-
this.emitCamera(composition.camera);
|
|
59
|
-
if (composition.objects) {
|
|
60
|
-
for (const obj of composition.objects) this.emitObject(obj);
|
|
61
|
-
}
|
|
62
|
-
if (composition.spatialGroups) {
|
|
63
|
-
for (const group of composition.spatialGroups) this.emitGroup(group);
|
|
64
|
-
}
|
|
65
|
-
if (composition.lights) {
|
|
66
|
-
for (const light of composition.lights) this.emitLight(light);
|
|
67
|
-
}
|
|
68
|
-
if (this.options.enableCompute) this.emitComputeShaders(composition);
|
|
69
|
-
this.emitWebGPUDomainBlocks(composition);
|
|
70
|
-
this.emitRenderLoop(composition);
|
|
71
|
-
return this.lines.join("\n");
|
|
72
|
-
}
|
|
73
|
-
emitWebGPUDomainBlocks(composition) {
|
|
74
|
-
const domainBlocks = composition.domainBlocks ?? [];
|
|
75
|
-
if (domainBlocks.length === 0) return;
|
|
76
|
-
this.emit("");
|
|
77
|
-
this.emit("// === v4.2 Domain Blocks ===");
|
|
78
|
-
for (let i = 0; i < domainBlocks.length; i++) {
|
|
79
|
-
const block = domainBlocks[i];
|
|
80
|
-
const prefix = this.domainBlockVar(block, i);
|
|
81
|
-
switch (block.domain) {
|
|
82
|
-
case "material":
|
|
83
|
-
this.emitMaterialDomainBlock(block, prefix);
|
|
84
|
-
break;
|
|
85
|
-
case "dataviz":
|
|
86
|
-
this.emitMultiline(datavizToWebGPU(compileDataVizBlock(block), prefix));
|
|
87
|
-
break;
|
|
88
|
-
case "vfx":
|
|
89
|
-
this.emitParticleDomainBlock(block, prefix);
|
|
90
|
-
break;
|
|
91
|
-
case "postfx":
|
|
92
|
-
this.emitPostFXDomainBlock(block, prefix);
|
|
93
|
-
break;
|
|
94
|
-
case "procedural":
|
|
95
|
-
this.emitProceduralDomainBlock(block, prefix);
|
|
96
|
-
break;
|
|
97
|
-
default:
|
|
98
|
-
this.emitDomainMetadataBlock(block, prefix);
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
this.emit("");
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
emitMaterialDomainBlock(block, prefix) {
|
|
105
|
-
const mat = compileMaterialBlock(block);
|
|
106
|
-
this.emitMultiline(materialToWebGPU(mat, prefix));
|
|
107
|
-
if (Object.keys(mat.textureMaps).length > 0) {
|
|
108
|
-
this.emit(`const ${prefix}TextureMaps = ${this.json(mat.textureMaps)};`);
|
|
109
|
-
this.emit(`const ${prefix}Textures = await loadMaterialTextureSet(${prefix}TextureMaps);`);
|
|
110
|
-
this.emit(
|
|
111
|
-
`const ${prefix}TextureReceipt = { name: "${this.escapeStringValue(mat.name, "TypeScript")}", maps: Object.keys(${prefix}TextureMaps), loaded: Object.values(${prefix}Textures).filter((t) => t.loaded).length };`
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
emitParticleDomainBlock(block, prefix) {
|
|
116
|
-
const ps = compileParticleBlock(block);
|
|
117
|
-
const count = Math.max(
|
|
118
|
-
1,
|
|
119
|
-
Math.floor(
|
|
120
|
-
this.numberProp(block, ["max_particles", "maxParticles", "count", "rate"], 512)
|
|
121
|
-
)
|
|
122
|
-
);
|
|
123
|
-
const seed = this.hashSeed(`${ps.name}:${ps.keyword}:${count}`);
|
|
124
|
-
this.emit(`// Domain particles: ${this.escapeStringValue(ps.name, "TypeScript")}`);
|
|
125
|
-
this.emit(`const ${prefix}ParticleConfig = ${this.json({
|
|
126
|
-
name: ps.name,
|
|
127
|
-
keyword: ps.keyword,
|
|
128
|
-
traits: ps.traits,
|
|
129
|
-
properties: ps.properties,
|
|
130
|
-
modules: ps.modules
|
|
131
|
-
})};`);
|
|
132
|
-
this.emit(`const ${prefix}ParticleCount = ${count};`);
|
|
133
|
-
this.emit(`const ${prefix}ParticleData = makeParticleCloud(${prefix}ParticleCount, ${seed});`);
|
|
134
|
-
this.emit(
|
|
135
|
-
`const ${prefix}ParticleBuf = createBuffer(device, ${prefix}ParticleData, GPUBufferUsage.VERTEX | GPUBufferUsage.STORAGE);`
|
|
136
|
-
);
|
|
137
|
-
this.emit(`const ${prefix}ParticlePipeline = device.createRenderPipeline({`);
|
|
138
|
-
this.indent();
|
|
139
|
-
this.emit('layout: "auto",');
|
|
140
|
-
this.emit(
|
|
141
|
-
'vertex: { module: device.createShaderModule({ code: WGSL_PARTICLE_RENDER }), entryPoint: "vs_particle",'
|
|
142
|
-
);
|
|
143
|
-
this.emit(
|
|
144
|
-
' buffers: [{ arrayStride: 32, attributes: [{ shaderLocation: 0, offset: 0, format: "float32x3" }, { shaderLocation: 1, offset: 12, format: "float32x3" }, { shaderLocation: 2, offset: 24, format: "float32" }] }] },'
|
|
145
|
-
);
|
|
146
|
-
this.emit(
|
|
147
|
-
'fragment: { module: device.createShaderModule({ code: WGSL_PARTICLE_RENDER }), entryPoint: "fs_particle",'
|
|
148
|
-
);
|
|
149
|
-
this.emit(
|
|
150
|
-
' targets: [{ format, blend: { color: { srcFactor: "src-alpha", dstFactor: "one", operation: "add" }, alpha: { srcFactor: "zero", dstFactor: "one", operation: "add" } } }] },'
|
|
151
|
-
);
|
|
152
|
-
this.emit(
|
|
153
|
-
'primitive: { topology: "point-list" }, depthStencil: { format: "depth24plus", depthWriteEnabled: false, depthCompare: "less" },'
|
|
154
|
-
);
|
|
155
|
-
this.dedent();
|
|
156
|
-
this.emit("});");
|
|
157
|
-
}
|
|
158
|
-
emitPostFXDomainBlock(block, prefix) {
|
|
159
|
-
const pp = compilePostProcessingBlock(block);
|
|
160
|
-
this.emit(`// Domain post-processing: ${this.escapeStringValue(pp.name ?? block.name, "TypeScript")}`);
|
|
161
|
-
this.emit(`const ${prefix}PostFXConfig = ${this.json({
|
|
162
|
-
name: pp.name,
|
|
163
|
-
keyword: pp.keyword,
|
|
164
|
-
effects: pp.effects
|
|
165
|
-
})};`);
|
|
166
|
-
this.emit(`const ${prefix}PostFXPipeline = device.createRenderPipeline({`);
|
|
167
|
-
this.indent();
|
|
168
|
-
this.emit('layout: "auto",');
|
|
169
|
-
this.emit(
|
|
170
|
-
'vertex: { module: device.createShaderModule({ code: WGSL_POSTFX }), entryPoint: "vs_postfx" },'
|
|
171
|
-
);
|
|
172
|
-
this.emit(
|
|
173
|
-
'fragment: { module: device.createShaderModule({ code: WGSL_POSTFX }), entryPoint: "fs_postfx", targets: [{ format, blend: { color: { srcFactor: "src-alpha", dstFactor: "one-minus-src-alpha", operation: "add" }, alpha: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" } } }] },'
|
|
174
|
-
);
|
|
175
|
-
this.emit('primitive: { topology: "triangle-list" }, multisample: { count: MSAA_COUNT },');
|
|
176
|
-
this.dedent();
|
|
177
|
-
this.emit("});");
|
|
178
|
-
}
|
|
179
|
-
emitProceduralDomainBlock(block, prefix) {
|
|
180
|
-
const proc = compileProceduralBlock(block);
|
|
181
|
-
this.emit(`// Domain procedural: ${this.escapeStringValue(proc.name, "TypeScript")}`);
|
|
182
|
-
this.emit(`const ${prefix}ProceduralConfig = ${this.json({
|
|
183
|
-
name: proc.name,
|
|
184
|
-
keyword: proc.keyword,
|
|
185
|
-
genType: proc.genType,
|
|
186
|
-
seed: proc.seed,
|
|
187
|
-
density: proc.density,
|
|
188
|
-
scaleRange: proc.scaleRange,
|
|
189
|
-
noise: proc.noise,
|
|
190
|
-
sourceMesh: proc.sourceMesh,
|
|
191
|
-
traits: proc.traits,
|
|
192
|
-
properties: proc.properties
|
|
193
|
-
})};`);
|
|
194
|
-
this.emit(
|
|
195
|
-
`const ${prefix}ProceduralUniform = createBuffer(device, new Float32Array([${proc.seed ?? 1}, ${proc.density ?? 1}, ${this.numberProp(block, ["count"], 1)}, 0]), GPUBufferUsage.UNIFORM);`
|
|
196
|
-
);
|
|
197
|
-
if (block.keyword === "scatter" || block.keyword === "distribute" || block.properties?.count) {
|
|
198
|
-
const count = Math.max(1, Math.floor(this.numberProp(block, ["count"], 128)));
|
|
199
|
-
this.emit(`const ${prefix}ScatterInstanceCount = ${count};`);
|
|
200
|
-
this.emit(`const ${prefix}ScatterVertices = generateConeVertices(0.08, 0.36, 8);`);
|
|
201
|
-
this.emit(`const ${prefix}ScatterVBO = createBuffer(device, ${prefix}ScatterVertices, GPUBufferUsage.VERTEX);`);
|
|
202
|
-
this.emit(`const ${prefix}ScatterVertexCount = ${prefix}ScatterVertices.length / 6;`);
|
|
203
|
-
this.emit(`const ${prefix}ScatterPipeline = device.createRenderPipeline({`);
|
|
204
|
-
this.indent();
|
|
205
|
-
this.emit('layout: "auto",');
|
|
206
|
-
this.emit(
|
|
207
|
-
'vertex: { module: device.createShaderModule({ code: WGSL_SCATTER }), entryPoint: "vs_scatter",'
|
|
208
|
-
);
|
|
209
|
-
this.emit(
|
|
210
|
-
' buffers: [{ arrayStride: 24, attributes: [{ shaderLocation: 0, offset: 0, format: "float32x3" }, { shaderLocation: 1, offset: 12, format: "float32x3" }] }] },'
|
|
211
|
-
);
|
|
212
|
-
this.emit(
|
|
213
|
-
'fragment: { module: device.createShaderModule({ code: WGSL_SCATTER }), entryPoint: "fs_scatter", targets: [{ format }] },'
|
|
214
|
-
);
|
|
215
|
-
this.emit(
|
|
216
|
-
'primitive: { topology: "triangle-list", cullMode: "back" }, depthStencil: { format: "depth24plus", depthWriteEnabled: true, depthCompare: "less" }, multisample: { count: MSAA_COUNT },'
|
|
217
|
-
);
|
|
218
|
-
this.dedent();
|
|
219
|
-
this.emit("});");
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
emitDomainMetadataBlock(block, prefix) {
|
|
223
|
-
this.emit(`const ${prefix}DomainBlock = ${this.json({
|
|
224
|
-
domain: block.domain,
|
|
225
|
-
keyword: block.keyword,
|
|
226
|
-
name: block.name,
|
|
227
|
-
traits: block.traits,
|
|
228
|
-
properties: block.properties,
|
|
229
|
-
childCount: block.children?.length ?? 0
|
|
230
|
-
})};`);
|
|
231
|
-
}
|
|
232
|
-
// ─── Device Init ──────────────────────────────────────────────────────────
|
|
233
|
-
emitDeviceInit() {
|
|
234
|
-
this.emit("// === WebGPU Device Initialization ===");
|
|
235
|
-
this.emit('const canvas = document.querySelector("canvas") as HTMLCanvasElement;');
|
|
236
|
-
this.emit("const adapter = await navigator.gpu.requestAdapter();");
|
|
237
|
-
this.emit('if (!adapter) throw new Error("WebGPU not supported");');
|
|
238
|
-
this.emit("const device = await adapter.requestDevice();");
|
|
239
|
-
this.emit('document.body.dataset.holoscriptWebgpu = "device-ready";');
|
|
240
|
-
this.emit('const context = canvas.getContext("webgpu") as GPUCanvasContext;');
|
|
241
|
-
this.emit("const format = navigator.gpu.getPreferredCanvasFormat();");
|
|
242
|
-
this.emit('context.configure({ device, format, alphaMode: "premultiplied" });');
|
|
243
|
-
this.emit(`const MSAA_COUNT = ${this.options.msaa};`);
|
|
244
|
-
this.emit(
|
|
245
|
-
"const msaaTexture = device.createTexture({ size: [canvas.width, canvas.height], sampleCount: MSAA_COUNT, format, usage: GPUTextureUsage.RENDER_ATTACHMENT });"
|
|
246
|
-
);
|
|
247
|
-
this.emit(
|
|
248
|
-
'const depthTexture = device.createTexture({ size: [canvas.width, canvas.height], sampleCount: MSAA_COUNT, format: "depth24plus", usage: GPUTextureUsage.RENDER_ATTACHMENT });'
|
|
249
|
-
);
|
|
250
|
-
this.emit("");
|
|
251
|
-
this.emit(
|
|
252
|
-
"function createBuffer(device: GPUDevice, data: Float32Array, usage: number): GPUBuffer {"
|
|
253
|
-
);
|
|
254
|
-
this.indent();
|
|
255
|
-
this.emit(
|
|
256
|
-
"const buf = device.createBuffer({ size: data.byteLength, usage: usage | GPUBufferUsage.COPY_DST });"
|
|
257
|
-
);
|
|
258
|
-
this.emit("device.queue.writeBuffer(buf, 0, data); return buf;");
|
|
259
|
-
this.dedent();
|
|
260
|
-
this.emit("}");
|
|
261
|
-
this.emit("function createStorageBuffer(device: GPUDevice, size: number): GPUBuffer {");
|
|
262
|
-
this.indent();
|
|
263
|
-
this.emit(
|
|
264
|
-
"return device.createBuffer({ size, usage: GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST });"
|
|
265
|
-
);
|
|
266
|
-
this.dedent();
|
|
267
|
-
this.emit("}");
|
|
268
|
-
this.emit("function createFallbackTexture(device, rgba) {");
|
|
269
|
-
this.indent();
|
|
270
|
-
this.emit('const tex = device.createTexture({ size: [1, 1, 1], format: "rgba8unorm", usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST });');
|
|
271
|
-
this.emit("device.queue.writeTexture({ texture: tex }, new Uint8Array(rgba), { bytesPerRow: 4 }, [1, 1, 1]);");
|
|
272
|
-
this.emit("return tex;");
|
|
273
|
-
this.dedent();
|
|
274
|
-
this.emit("}");
|
|
275
|
-
this.emit("async function loadTexture2D(src) {");
|
|
276
|
-
this.indent();
|
|
277
|
-
this.emit('const sampler = device.createSampler({ magFilter: "linear", minFilter: "linear", mipmapFilter: "linear", addressModeU: "repeat", addressModeV: "repeat" });');
|
|
278
|
-
this.emit("try {");
|
|
279
|
-
this.indent();
|
|
280
|
-
this.emit("const resp = await fetch(src);");
|
|
281
|
-
this.emit("if (!resp.ok) throw new Error(`texture fetch ${resp.status}`);");
|
|
282
|
-
this.emit("const bitmap = await createImageBitmap(await resp.blob());");
|
|
283
|
-
this.emit('const texture = device.createTexture({ size: [bitmap.width, bitmap.height, 1], format: "rgba8unorm", usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT });');
|
|
284
|
-
this.emit("device.queue.copyExternalImageToTexture({ source: bitmap }, { texture }, [bitmap.width, bitmap.height]);");
|
|
285
|
-
this.emit("return { src, texture, view: texture.createView(), sampler, loaded: true };");
|
|
286
|
-
this.dedent();
|
|
287
|
-
this.emit("} catch {");
|
|
288
|
-
this.indent();
|
|
289
|
-
this.emit("const texture = createFallbackTexture(device, [255, 255, 255, 255]);");
|
|
290
|
-
this.emit("return { src, texture, view: texture.createView(), sampler, loaded: false };");
|
|
291
|
-
this.dedent();
|
|
292
|
-
this.emit("}");
|
|
293
|
-
this.dedent();
|
|
294
|
-
this.emit("}");
|
|
295
|
-
this.emit("async function loadMaterialTextureSet(maps) {");
|
|
296
|
-
this.indent();
|
|
297
|
-
this.emit("const entries = await Promise.all(Object.entries(maps).map(async ([kind, src]) => [kind, await loadTexture2D(src)]));");
|
|
298
|
-
this.emit("return Object.fromEntries(entries);");
|
|
299
|
-
this.dedent();
|
|
300
|
-
this.emit("}");
|
|
301
|
-
this.emit("const assetLoader = {");
|
|
302
|
-
this.indent();
|
|
303
|
-
this.emit("async load(src) {");
|
|
304
|
-
this.indent();
|
|
305
|
-
this.emit('return { src, kind: "external-model", loaded: false };');
|
|
306
|
-
this.dedent();
|
|
307
|
-
this.emit("},");
|
|
308
|
-
this.dedent();
|
|
309
|
-
this.emit("};");
|
|
310
|
-
this.emit("function makeParticleCloud(count, seed) {");
|
|
311
|
-
this.indent();
|
|
312
|
-
this.emit("let s = seed || 1;");
|
|
313
|
-
this.emit("const rnd = () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };");
|
|
314
|
-
this.emit("const data = new Float32Array(count * 8);");
|
|
315
|
-
this.emit("for (let i = 0; i < count; i++) {");
|
|
316
|
-
this.indent();
|
|
317
|
-
this.emit("const o = i * 8;");
|
|
318
|
-
this.emit("data[o + 0] = rnd() * 1.8 - 0.9; data[o + 1] = rnd() * 1.2 - 0.6; data[o + 2] = rnd() * 1.8 - 0.9;");
|
|
319
|
-
this.emit("data[o + 3] = 0.0; data[o + 4] = 0.25 + rnd(); data[o + 5] = 0.0; data[o + 6] = rnd(); data[o + 7] = 0.0;");
|
|
320
|
-
this.dedent();
|
|
321
|
-
this.emit("}");
|
|
322
|
-
this.emit("return data;");
|
|
323
|
-
this.dedent();
|
|
324
|
-
this.emit("}");
|
|
325
|
-
this.emit("");
|
|
326
|
-
}
|
|
327
|
-
emitGeometryHelpers() {
|
|
328
|
-
this.emit("// === Geometry Helpers ===");
|
|
329
|
-
this.emit("function generateCubeVertices(size) {");
|
|
330
|
-
this.indent();
|
|
331
|
-
this.emit("const s = size / 2;");
|
|
332
|
-
this.emit("return new Float32Array([");
|
|
333
|
-
this.indent();
|
|
334
|
-
this.emit("-s,-s, s, 0,0,1, s,-s, s, 0,0,1, s, s, s, 0,0,1, -s,-s, s, 0,0,1, s, s, s, 0,0,1, -s, s, s, 0,0,1,");
|
|
335
|
-
this.emit(" s,-s,-s, 0,0,-1, -s,-s,-s, 0,0,-1, -s, s,-s, 0,0,-1, s,-s,-s, 0,0,-1, -s, s,-s, 0,0,-1, s, s,-s, 0,0,-1,");
|
|
336
|
-
this.emit("-s,-s,-s, -1,0,0, -s,-s, s, -1,0,0, -s, s, s, -1,0,0, -s,-s,-s, -1,0,0, -s, s, s, -1,0,0, -s, s,-s, -1,0,0,");
|
|
337
|
-
this.emit(" s,-s, s, 1,0,0, s,-s,-s, 1,0,0, s, s,-s, 1,0,0, s,-s, s, 1,0,0, s, s,-s, 1,0,0, s, s, s, 1,0,0,");
|
|
338
|
-
this.emit("-s, s, s, 0,1,0, s, s, s, 0,1,0, s, s,-s, 0,1,0, -s, s, s, 0,1,0, s, s,-s, 0,1,0, -s, s,-s, 0,1,0,");
|
|
339
|
-
this.emit("-s,-s,-s, 0,-1,0, s,-s,-s, 0,-1,0, s,-s, s, 0,-1,0, -s,-s,-s, 0,-1,0, s,-s, s, 0,-1,0, -s,-s, s, 0,-1,0");
|
|
340
|
-
this.dedent();
|
|
341
|
-
this.emit("]);");
|
|
342
|
-
this.dedent();
|
|
343
|
-
this.emit("}");
|
|
344
|
-
this.emit("function generatePlaneVertices(width, depth) {");
|
|
345
|
-
this.indent();
|
|
346
|
-
this.emit("const x = width / 2, z = depth / 2;");
|
|
347
|
-
this.emit("return new Float32Array([-x,0,-z,0,1,0, x,0,-z,0,1,0, x,0,z,0,1,0, -x,0,-z,0,1,0, x,0,z,0,1,0, -x,0,z,0,1,0]);");
|
|
348
|
-
this.dedent();
|
|
349
|
-
this.emit("}");
|
|
350
|
-
this.emit("function generateSphereVertices(radius, lat, lon) {");
|
|
351
|
-
this.indent();
|
|
352
|
-
this.emit("const rings = Math.max(3, lat | 0), segs = Math.max(3, lon | 0); const out = [];");
|
|
353
|
-
this.emit("const P = (ri, si) => {");
|
|
354
|
-
this.emit(" const phi = Math.PI * ri / rings, theta = 2 * Math.PI * si / segs;");
|
|
355
|
-
this.emit(" const nx = Math.sin(phi) * Math.cos(theta), ny = Math.cos(phi), nz = Math.sin(phi) * Math.sin(theta);");
|
|
356
|
-
this.emit(" return [nx * radius, ny * radius, nz * radius, nx, ny, nz];");
|
|
357
|
-
this.emit("};");
|
|
358
|
-
this.emit("for (let ri = 0; ri < rings; ri++) { for (let si = 0; si < segs; si++) {");
|
|
359
|
-
this.emit(" const a = P(ri, si), b = P(ri + 1, si), c = P(ri + 1, si + 1), d = P(ri, si + 1);");
|
|
360
|
-
this.emit(" out.push(...a, ...b, ...c, ...a, ...c, ...d);");
|
|
361
|
-
this.emit("} } return new Float32Array(out);");
|
|
362
|
-
this.dedent();
|
|
363
|
-
this.emit("}");
|
|
364
|
-
this.emit("function generateCylinderVertices(radius, height, segments) {");
|
|
365
|
-
this.indent();
|
|
366
|
-
this.emit("const segs = Math.max(3, segments | 0), hy = height / 2, out = [];");
|
|
367
|
-
this.emit("for (let i = 0; i < segs; i++) {");
|
|
368
|
-
this.emit(" const t0 = 2*Math.PI*i/segs, t1 = 2*Math.PI*(i+1)/segs;");
|
|
369
|
-
this.emit(" const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1);");
|
|
370
|
-
this.emit(" const b0 = [radius*c0, -hy, radius*s0, c0, 0, s0], t0v = [radius*c0, hy, radius*s0, c0, 0, s0];");
|
|
371
|
-
this.emit(" const b1 = [radius*c1, -hy, radius*s1, c1, 0, s1], t1v = [radius*c1, hy, radius*s1, c1, 0, s1];");
|
|
372
|
-
this.emit(" out.push(...b0, ...b1, ...t1v, ...b0, ...t1v, ...t0v);");
|
|
373
|
-
this.emit(" out.push(0, hy, 0, 0, 1, 0, radius*c0, hy, radius*s0, 0, 1, 0, radius*c1, hy, radius*s1, 0, 1, 0);");
|
|
374
|
-
this.emit(" out.push(0, -hy, 0, 0, -1, 0, radius*c1, -hy, radius*s1, 0, -1, 0, radius*c0, -hy, radius*s0, 0, -1, 0);");
|
|
375
|
-
this.emit("} return new Float32Array(out);");
|
|
376
|
-
this.dedent();
|
|
377
|
-
this.emit("}");
|
|
378
|
-
this.emit("function generateConeVertices(radius, height, segments) {");
|
|
379
|
-
this.indent();
|
|
380
|
-
this.emit("const segs = Math.max(3, segments | 0), hy = height / 2, out = [];");
|
|
381
|
-
this.emit("const ny = radius / Math.hypot(radius, height) || 0, nyx = height / Math.hypot(radius, height) || 1;");
|
|
382
|
-
this.emit("for (let i = 0; i < segs; i++) {");
|
|
383
|
-
this.emit(" const t0 = 2*Math.PI*i/segs, t1 = 2*Math.PI*(i+1)/segs;");
|
|
384
|
-
this.emit(" const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1);");
|
|
385
|
-
this.emit(" out.push(0, hy, 0, (c0+c1)*0.5*nyx, ny, (s0+s1)*0.5*nyx);");
|
|
386
|
-
this.emit(" out.push(radius*c0, -hy, radius*s0, c0*nyx, ny, s0*nyx);");
|
|
387
|
-
this.emit(" out.push(radius*c1, -hy, radius*s1, c1*nyx, ny, s1*nyx);");
|
|
388
|
-
this.emit(" out.push(0, -hy, 0, 0, -1, 0, radius*c1, -hy, radius*s1, 0, -1, 0, radius*c0, -hy, radius*s0, 0, -1, 0);");
|
|
389
|
-
this.emit("} return new Float32Array(out);");
|
|
390
|
-
this.dedent();
|
|
391
|
-
this.emit("}");
|
|
392
|
-
this.emit("function generateTorusVertices(radius, tube, radial, tubular) {");
|
|
393
|
-
this.indent();
|
|
394
|
-
this.emit("const R = Math.max(3, radial | 0), T = Math.max(3, tubular | 0), out = [];");
|
|
395
|
-
this.emit("const P = (ri, ti) => {");
|
|
396
|
-
this.emit(" const u = 2*Math.PI*ri/R, v = 2*Math.PI*ti/T;");
|
|
397
|
-
this.emit(" const cu = Math.cos(u), su = Math.sin(u), cv = Math.cos(v), sv = Math.sin(v);");
|
|
398
|
-
this.emit(" const x = (radius + tube*cv)*cu, y = tube*sv, z = (radius + tube*cv)*su;");
|
|
399
|
-
this.emit(" return [x, y, z, cv*cu, sv, cv*su];");
|
|
400
|
-
this.emit("};");
|
|
401
|
-
this.emit("for (let ri = 0; ri < R; ri++) { for (let ti = 0; ti < T; ti++) {");
|
|
402
|
-
this.emit(" const a = P(ri, ti), b = P(ri + 1, ti), c = P(ri + 1, ti + 1), d = P(ri, ti + 1);");
|
|
403
|
-
this.emit(" out.push(...a, ...b, ...c, ...a, ...c, ...d);");
|
|
404
|
-
this.emit("} } return new Float32Array(out);");
|
|
405
|
-
this.dedent();
|
|
406
|
-
this.emit("}");
|
|
407
|
-
this.emit("");
|
|
408
|
-
}
|
|
409
|
-
// ─── Environment ──────────────────────────────────────────────────────────
|
|
410
|
-
emitEnvironment(env) {
|
|
411
|
-
this.emit("// === Environment ===");
|
|
412
|
-
const envData = {};
|
|
413
|
-
for (const prop of env.properties) envData[prop.key] = prop.value;
|
|
414
|
-
const bg = envData.background || envData.skybox || "#000000";
|
|
415
|
-
const bgColor = this.parseColor(bg);
|
|
416
|
-
this.emit(
|
|
417
|
-
`const clearColor: GPUColor = { r: ${bgColor[0]}, g: ${bgColor[1]}, b: ${bgColor[2]}, a: 1.0 };`
|
|
418
|
-
);
|
|
419
|
-
if (envData.fog && typeof envData.fog === "object") {
|
|
420
|
-
const fog = envData.fog;
|
|
421
|
-
const fc = this.parseColor(fog.color || "#cccccc");
|
|
422
|
-
this.emit(
|
|
423
|
-
`const fogUniforms = createBuffer(device, new Float32Array([${fc[0]}, ${fc[1]}, ${fc[2]}, 1.0, ${fog.near ?? 10}, ${fog.far ?? 100}, 0.0, 0.0]), GPUBufferUsage.UNIFORM);`
|
|
424
|
-
);
|
|
425
|
-
}
|
|
426
|
-
if (envData.ambient_light !== void 0) {
|
|
427
|
-
this.emit(
|
|
428
|
-
`const ambientIntensity = ${typeof envData.ambient_light === "number" ? envData.ambient_light : 0.5};`
|
|
429
|
-
);
|
|
430
|
-
}
|
|
431
|
-
this.emit("");
|
|
432
|
-
}
|
|
433
|
-
// ─── Object ───────────────────────────────────────────────────────────────
|
|
434
|
-
emitObject(obj) {
|
|
435
|
-
this.objectIndex++;
|
|
436
|
-
const v = this.sanitizeName(obj.name);
|
|
437
|
-
const meshType = this.findObjProp(obj, "geometry") || this.findObjProp(obj, "mesh") || this.findObjProp(obj, "type") || "cube";
|
|
438
|
-
const modelSrc = this.findObjProp(obj, "source") || this.findObjProp(obj, "src") || this.findObjProp(obj, "model");
|
|
439
|
-
const isModel = meshType === "model" || !!modelSrc;
|
|
440
|
-
const traits = obj.traits || [];
|
|
441
|
-
this.emit(`// Object: ${this.escapeStringValue(obj.name, "TypeScript")}`);
|
|
442
|
-
const isGaussianSplat = traits.some((t) => t.name === "gaussian_splat");
|
|
443
|
-
const isPointCloud = traits.some((t) => t.name === "point_cloud");
|
|
444
|
-
const isGpuParticle = traits.some((t) => isGpuParticleTraitName(t.name));
|
|
445
|
-
if (isModel && modelSrc) {
|
|
446
|
-
this.emit(`const ${v}Asset = await assetLoader.load("${modelSrc}");`);
|
|
447
|
-
this.emit(
|
|
448
|
-
`// Model asset "${this.escapeStringValue(modelSrc, "TypeScript")}" is tracked in ${v}Asset; fallback mesh keeps the render path defined.`
|
|
449
|
-
);
|
|
450
|
-
this.emitMeshObject(v, obj, "cube");
|
|
451
|
-
} else if (isModel) {
|
|
452
|
-
this.emit(`// WARNING: geometry "model" without source URL \u2014 placeholder cube`);
|
|
453
|
-
this.emitMeshObject(v, obj, "cube");
|
|
454
|
-
} else if (isGaussianSplat) {
|
|
455
|
-
this.emitGaussianSplatObject(v, obj);
|
|
456
|
-
} else if (isPointCloud) {
|
|
457
|
-
this.emitPointCloudObject(v, obj);
|
|
458
|
-
} else if (isGpuParticle) {
|
|
459
|
-
this.emitGpuParticleObject(v, obj);
|
|
460
|
-
} else {
|
|
461
|
-
this.emitMeshObject(v, obj, meshType);
|
|
462
|
-
}
|
|
463
|
-
if (traits.some((t) => t.name === "gpu_physics")) {
|
|
464
|
-
this.emit(`const ${v}PhysicsBuf = createStorageBuffer(device, 1024 * 16); // gpu_physics`);
|
|
465
|
-
}
|
|
466
|
-
if (traits.some((t) => t.name === "compute" || t.name === "gpu_buffer")) {
|
|
467
|
-
const ct = traits.find((t) => t.name === "compute" || t.name === "gpu_buffer");
|
|
468
|
-
this.emit(
|
|
469
|
-
`const ${v}ComputeBuf = createStorageBuffer(device, ${ct?.config?.buffer_size || 4096});`
|
|
470
|
-
);
|
|
471
|
-
}
|
|
472
|
-
this.emit("");
|
|
473
|
-
if (obj.children) {
|
|
474
|
-
for (const child of obj.children) this.emitObject(child);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
emitMeshObject(v, obj, meshType) {
|
|
478
|
-
this.emit(`const ${v}Vertices = ${this.geometryVertexDataFn(meshType)};`);
|
|
479
|
-
this.emit(`const ${v}VBO = createBuffer(device, ${v}Vertices, GPUBufferUsage.VERTEX);`);
|
|
480
|
-
this.emit(`const ${v}VertexCount = ${v}Vertices.length / 6;`);
|
|
481
|
-
const pos = this.findObjProp(obj, "position");
|
|
482
|
-
const scale = this.findObjProp(obj, "scale") || this.findObjProp(obj, "size");
|
|
483
|
-
const [px, py, pz] = Array.isArray(pos) ? pos : [0, 0, 0];
|
|
484
|
-
const [sx, sy, sz] = Array.isArray(scale) ? scale : typeof scale === "number" ? [scale, scale, scale] : [1, 1, 1];
|
|
485
|
-
this.emit(
|
|
486
|
-
`const ${v}Model = createBuffer(device, new Float32Array([${sx},0,0,0, 0,${sy},0,0, 0,0,${sz},0, ${px},${py},${pz},1]), GPUBufferUsage.UNIFORM);`
|
|
487
|
-
);
|
|
488
|
-
const material = this.findObjProp(obj, "material");
|
|
489
|
-
const color = this.findObjProp(obj, "color");
|
|
490
|
-
const [cr, cg, cb] = this.extractMaterialColor(material, color);
|
|
491
|
-
const rough = this.extractMaterialProp(material, "roughness", 0.5);
|
|
492
|
-
const metal = this.extractMaterialProp(material, "metalness", 0);
|
|
493
|
-
const [er, eg, eb] = this.extractEmissive(material, obj);
|
|
494
|
-
const emissiveStrength = this.extractMaterialProp(material, "emissive_intensity", 1);
|
|
495
|
-
this.emit(
|
|
496
|
-
`const ${v}Mat = createBuffer(device, new Float32Array([${cr},${cg},${cb},1.0, ${rough},${metal},${emissiveStrength},0, ${er},${eg},${eb},0]), GPUBufferUsage.UNIFORM);`
|
|
497
|
-
);
|
|
498
|
-
this.emit(`const ${v}Pipeline = device.createRenderPipeline({`);
|
|
499
|
-
this.indent();
|
|
500
|
-
this.emit('layout: "auto",');
|
|
501
|
-
this.emit(
|
|
502
|
-
'vertex: { module: device.createShaderModule({ code: WGSL_VERTEX }), entryPoint: "vs_main",'
|
|
503
|
-
);
|
|
504
|
-
this.emit(
|
|
505
|
-
' buffers: [{ arrayStride: 24, attributes: [{ shaderLocation: 0, offset: 0, format: "float32x3" }, { shaderLocation: 1, offset: 12, format: "float32x3" }] }] },'
|
|
506
|
-
);
|
|
507
|
-
this.emit(
|
|
508
|
-
'fragment: { module: device.createShaderModule({ code: WGSL_FRAGMENT }), entryPoint: "fs_main", targets: [{ format }] },'
|
|
509
|
-
);
|
|
510
|
-
this.emit('primitive: { topology: "triangle-list", cullMode: "back" },');
|
|
511
|
-
this.emit(
|
|
512
|
-
`depthStencil: { format: "depth24plus", depthWriteEnabled: true, depthCompare: "less" }, multisample: { count: MSAA_COUNT },`
|
|
513
|
-
);
|
|
514
|
-
this.dedent();
|
|
515
|
-
this.emit("});");
|
|
516
|
-
this.emit(
|
|
517
|
-
`const ${v}Bind = device.createBindGroup({ layout: ${v}Pipeline.getBindGroupLayout(0), entries: [`
|
|
518
|
-
);
|
|
519
|
-
this.emit(
|
|
520
|
-
` { binding: 0, resource: { buffer: ${v}Model } }, { binding: 1, resource: { buffer: ${v}Mat } }, { binding: 2, resource: { buffer: vpUniform } }] });`
|
|
521
|
-
);
|
|
522
|
-
}
|
|
523
|
-
emitGaussianSplatObject(v, obj) {
|
|
524
|
-
const trait = obj.traits?.find((t) => t.name === "gaussian_splat");
|
|
525
|
-
const src = trait?.config?.src || "scene.ply";
|
|
526
|
-
const max = trait?.config?.max_splats || 5e5;
|
|
527
|
-
const sorted = trait?.config?.sorted !== false;
|
|
528
|
-
this.emit(`// Gaussian Splat \u2014 source: ${src} (${sorted ? "radix-sorted" : "unsorted"})`);
|
|
529
|
-
if (sorted) {
|
|
530
|
-
this.emit(
|
|
531
|
-
`// === Wait-Free Radix Sort Pipeline (4x 8-bit Blelloch scan, no global atomics) ===`
|
|
532
|
-
);
|
|
533
|
-
this.emit(`import { GaussianSplatSorter } from '@holoscript/core/gpu/GaussianSplatSorter';`);
|
|
534
|
-
this.emit(`const ${v}Sorter = new GaussianSplatSorter(device, {`);
|
|
535
|
-
this.indent();
|
|
536
|
-
this.emit(`maxSplats: ${max},`);
|
|
537
|
-
this.emit(`canvasWidth: canvas.width,`);
|
|
538
|
-
this.emit(`canvasHeight: canvas.height,`);
|
|
539
|
-
this.dedent();
|
|
540
|
-
this.emit(`});`);
|
|
541
|
-
this.emit(`await ${v}Sorter.initialize();`);
|
|
542
|
-
this.emit(`const ${v}SplatCount = { value: 0 };`);
|
|
543
|
-
} else {
|
|
544
|
-
this.emit(`const ${v}SplatBuf = createStorageBuffer(device, ${max} * 32);`);
|
|
545
|
-
this.emit(`const ${v}SplatCount = { value: 0 };`);
|
|
546
|
-
this.emit(`const ${v}SplatPipeline = device.createRenderPipeline({`);
|
|
547
|
-
this.indent();
|
|
548
|
-
this.emit('layout: "auto",');
|
|
549
|
-
this.emit(
|
|
550
|
-
'vertex: { module: device.createShaderModule({ code: WGSL_SPLAT }), entryPoint: "vs_splat", buffers: [] },'
|
|
551
|
-
);
|
|
552
|
-
this.emit(
|
|
553
|
-
'fragment: { module: device.createShaderModule({ code: WGSL_SPLAT }), entryPoint: "fs_splat",'
|
|
554
|
-
);
|
|
555
|
-
this.emit(
|
|
556
|
-
' targets: [{ format, blend: { color: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" }, alpha: { srcFactor: "one", dstFactor: "one-minus-src-alpha", operation: "add" } } }] },'
|
|
557
|
-
);
|
|
558
|
-
this.emit('primitive: { topology: "triangle-strip" },');
|
|
559
|
-
this.emit(
|
|
560
|
-
'depthStencil: { format: "depth24plus", depthWriteEnabled: false, depthCompare: "less" },'
|
|
561
|
-
);
|
|
562
|
-
this.dedent();
|
|
563
|
-
this.emit("});");
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
emitPointCloudObject(v, obj) {
|
|
567
|
-
const trait = obj.traits?.find((t) => t.name === "point_cloud");
|
|
568
|
-
const max = trait?.config?.max_points || 1e5;
|
|
569
|
-
this.emit(`// Point Cloud (${max} max points)`);
|
|
570
|
-
this.emit(`const ${v}PointBuf = createStorageBuffer(device, ${max} * 16);`);
|
|
571
|
-
this.emit(`const ${v}PointCount = { value: 0 };`);
|
|
572
|
-
this.emit(`const ${v}PointPipeline = device.createRenderPipeline({`);
|
|
573
|
-
this.indent();
|
|
574
|
-
this.emit('layout: "auto",');
|
|
575
|
-
this.emit(
|
|
576
|
-
'vertex: { module: device.createShaderModule({ code: WGSL_POINT }), entryPoint: "vs_point",'
|
|
577
|
-
);
|
|
578
|
-
this.emit(
|
|
579
|
-
' buffers: [{ arrayStride: 16, attributes: [{ shaderLocation: 0, offset: 0, format: "float32x3" }, { shaderLocation: 1, offset: 12, format: "unorm8x4" }] }] },'
|
|
580
|
-
);
|
|
581
|
-
this.emit(
|
|
582
|
-
'fragment: { module: device.createShaderModule({ code: WGSL_POINT }), entryPoint: "fs_point", targets: [{ format }] },'
|
|
583
|
-
);
|
|
584
|
-
this.emit(
|
|
585
|
-
`primitive: { topology: "point-list" }, depthStencil: { format: "depth24plus", depthWriteEnabled: true, depthCompare: "less" }, multisample: { count: MSAA_COUNT },`
|
|
586
|
-
);
|
|
587
|
-
this.dedent();
|
|
588
|
-
this.emit("});");
|
|
589
|
-
}
|
|
590
|
-
emitGpuParticleObject(v, obj) {
|
|
591
|
-
const trait = findGpuParticleTrait(obj.traits);
|
|
592
|
-
const count = trait?.config?.count || 1e4;
|
|
593
|
-
this.emit(`// GPU Particles (${count}) from @${trait?.name ?? "gpu_particle"}`);
|
|
594
|
-
this.emit(`const ${v}ParticleCount = ${count};`);
|
|
595
|
-
this.emit(`const ${v}ParticleBufA = createStorageBuffer(device, ${v}ParticleCount * 32);`);
|
|
596
|
-
this.emit(`const ${v}ParticleBufB = createStorageBuffer(device, ${v}ParticleCount * 32);`);
|
|
597
|
-
this.emit(`const ${v}ParticlePipeline = device.createRenderPipeline({`);
|
|
598
|
-
this.indent();
|
|
599
|
-
this.emit('layout: "auto",');
|
|
600
|
-
this.emit(
|
|
601
|
-
'vertex: { module: device.createShaderModule({ code: WGSL_PARTICLE_RENDER }), entryPoint: "vs_particle",'
|
|
602
|
-
);
|
|
603
|
-
this.emit(
|
|
604
|
-
' buffers: [{ arrayStride: 32, attributes: [{ shaderLocation: 0, offset: 0, format: "float32x3" }, { shaderLocation: 1, offset: 12, format: "float32x3" }, { shaderLocation: 2, offset: 24, format: "float32" }] }] },'
|
|
605
|
-
);
|
|
606
|
-
this.emit(
|
|
607
|
-
'fragment: { module: device.createShaderModule({ code: WGSL_PARTICLE_RENDER }), entryPoint: "fs_particle",'
|
|
608
|
-
);
|
|
609
|
-
this.emit(
|
|
610
|
-
' targets: [{ format, blend: { color: { srcFactor: "src-alpha", dstFactor: "one", operation: "add" }, alpha: { srcFactor: "zero", dstFactor: "one", operation: "add" } } }] },'
|
|
611
|
-
);
|
|
612
|
-
this.emit(
|
|
613
|
-
'primitive: { topology: "point-list" }, depthStencil: { format: "depth24plus", depthWriteEnabled: false, depthCompare: "less" },'
|
|
614
|
-
);
|
|
615
|
-
this.dedent();
|
|
616
|
-
this.emit("});");
|
|
617
|
-
}
|
|
618
|
-
// ─── Spatial Group ────────────────────────────────────────────────────────
|
|
619
|
-
emitGroup(group) {
|
|
620
|
-
const v = this.sanitizeName(group.name);
|
|
621
|
-
this.emit(`// Spatial Group: ${this.escapeStringValue(group.name, "TypeScript")}`);
|
|
622
|
-
const pos = group.properties.find((p) => p.key === "position")?.value;
|
|
623
|
-
const [px, py, pz] = Array.isArray(pos) ? pos : [0, 0, 0];
|
|
624
|
-
this.emit(
|
|
625
|
-
`const ${v}GroupXform = createBuffer(device, new Float32Array([1,0,0,0, 0,1,0,0, 0,0,1,0, ${px},${py},${pz},1]), GPUBufferUsage.UNIFORM);`
|
|
626
|
-
);
|
|
627
|
-
for (const obj of group.objects) this.emitObject(obj);
|
|
628
|
-
if (group.groups) {
|
|
629
|
-
for (const sub of group.groups) this.emitGroup(sub);
|
|
630
|
-
}
|
|
631
|
-
this.emit("");
|
|
632
|
-
}
|
|
633
|
-
// ─── Light ────────────────────────────────────────────────────────────────
|
|
634
|
-
emitLight(light) {
|
|
635
|
-
const v = this.sanitizeName(light.name);
|
|
636
|
-
let color = [1, 1, 1], intensity = 1, pos = [0, 10, 0], dir = [0, -1, 0];
|
|
637
|
-
for (const prop of light.properties) {
|
|
638
|
-
if (prop.key === "color") color = this.parseColor(prop.value);
|
|
639
|
-
else if (prop.key === "intensity") intensity = prop.value;
|
|
640
|
-
else if (prop.key === "position" && Array.isArray(prop.value)) pos = prop.value;
|
|
641
|
-
else if (prop.key === "direction" && Array.isArray(prop.value)) dir = prop.value;
|
|
642
|
-
}
|
|
643
|
-
const ti = { directional: 0, point: 1, spot: 2, hemisphere: 3, ambient: 4, area: 5 }[light.lightType] ?? 0;
|
|
644
|
-
this.emit(
|
|
645
|
-
`// Light: ${this.escapeStringValue(light.name, "TypeScript")} (${light.lightType})`
|
|
646
|
-
);
|
|
647
|
-
this.emit(
|
|
648
|
-
`const ${v}Light = createBuffer(device, new Float32Array([${color[0] * intensity},${color[1] * intensity},${color[2] * intensity},${ti}, ${pos[0]},${pos[1]},${pos[2]},0, ${dir[0]},${dir[1]},${dir[2]},0]), GPUBufferUsage.UNIFORM);`
|
|
649
|
-
);
|
|
650
|
-
this.emit("");
|
|
651
|
-
}
|
|
652
|
-
// ─── Camera ───────────────────────────────────────────────────────────────
|
|
653
|
-
emitCamera(cam) {
|
|
654
|
-
this.emit("// === Camera ===");
|
|
655
|
-
let fov = 60, near = 0.1, far = 1e3, pos = [4, 3, 6], lookAt = [0, 0, 0];
|
|
656
|
-
for (const prop of cam?.properties ?? []) {
|
|
657
|
-
if (prop.key === "fov" || prop.key === "field_of_view") fov = prop.value;
|
|
658
|
-
else if (prop.key === "near") near = prop.value;
|
|
659
|
-
else if (prop.key === "far") far = prop.value;
|
|
660
|
-
else if (prop.key === "position" && Array.isArray(prop.value)) pos = prop.value;
|
|
661
|
-
else if ((prop.key === "look_at" || prop.key === "lookAt") && Array.isArray(prop.value))
|
|
662
|
-
lookAt = prop.value;
|
|
663
|
-
}
|
|
664
|
-
this.emit(`const cameraFov = ${fov} * Math.PI / 180;`);
|
|
665
|
-
this.emit(`const cameraNear = ${near}, cameraFar = ${far};`);
|
|
666
|
-
this.emit(`const cameraPos = new Float32Array([${pos.join(",")}]);`);
|
|
667
|
-
this.emit(`const cameraTarget = new Float32Array([${lookAt.join(",")}]);`);
|
|
668
|
-
this.emit("const cameraUp = new Float32Array([0, 1, 0]);");
|
|
669
|
-
this.emit("const aspect = (canvas.width || 1) / (canvas.height || 1);");
|
|
670
|
-
this.emit("function mat4Multiply(a: Float32Array, b: Float32Array): Float32Array {");
|
|
671
|
-
this.indent();
|
|
672
|
-
this.emit("const o = new Float32Array(16);");
|
|
673
|
-
this.emit("for (let c = 0; c < 4; c++) { for (let r = 0; r < 4; r++) {");
|
|
674
|
-
this.emit(" o[c*4+r] = a[0*4+r]*b[c*4+0] + a[1*4+r]*b[c*4+1] + a[2*4+r]*b[c*4+2] + a[3*4+r]*b[c*4+3];");
|
|
675
|
-
this.emit("} } return o;");
|
|
676
|
-
this.dedent();
|
|
677
|
-
this.emit("}");
|
|
678
|
-
this.emit("function lookAtMatrix(eye: Float32Array, target: Float32Array, up: Float32Array): Float32Array {");
|
|
679
|
-
this.indent();
|
|
680
|
-
this.emit("const zx = eye[0]-target[0], zy = eye[1]-target[1], zz = eye[2]-target[2];");
|
|
681
|
-
this.emit("let zl = Math.hypot(zx, zy, zz) || 1; const fz = [zx/zl, zy/zl, zz/zl];");
|
|
682
|
-
this.emit("const xx = up[1]*fz[2]-up[2]*fz[1], xy = up[2]*fz[0]-up[0]*fz[2], xz = up[0]*fz[1]-up[1]*fz[0];");
|
|
683
|
-
this.emit("let xl = Math.hypot(xx, xy, xz) || 1; const rx = [xx/xl, xy/xl, xz/xl];");
|
|
684
|
-
this.emit("const uy = [fz[1]*rx[2]-fz[2]*rx[1], fz[2]*rx[0]-fz[0]*rx[2], fz[0]*rx[1]-fz[1]*rx[0]];");
|
|
685
|
-
this.emit("return new Float32Array([");
|
|
686
|
-
this.emit(" rx[0], uy[0], fz[0], 0,");
|
|
687
|
-
this.emit(" rx[1], uy[1], fz[1], 0,");
|
|
688
|
-
this.emit(" rx[2], uy[2], fz[2], 0,");
|
|
689
|
-
this.emit(" -(rx[0]*eye[0]+rx[1]*eye[1]+rx[2]*eye[2]),");
|
|
690
|
-
this.emit(" -(uy[0]*eye[0]+uy[1]*eye[1]+uy[2]*eye[2]),");
|
|
691
|
-
this.emit(" -(fz[0]*eye[0]+fz[1]*eye[1]+fz[2]*eye[2]), 1]);");
|
|
692
|
-
this.dedent();
|
|
693
|
-
this.emit("}");
|
|
694
|
-
this.emit("function buildViewProjection(): Float32Array {");
|
|
695
|
-
this.indent();
|
|
696
|
-
this.emit("const f = 1.0 / Math.tan(cameraFov / 2), ri = 1.0 / (cameraNear - cameraFar);");
|
|
697
|
-
this.emit("const proj = new Float32Array([f/aspect,0,0,0, 0,f,0,0, 0,0,cameraFar*ri,-1, 0,0,cameraNear*cameraFar*ri,0]);");
|
|
698
|
-
this.emit("const view = lookAtMatrix(cameraPos, cameraTarget, cameraUp);");
|
|
699
|
-
this.emit("return mat4Multiply(proj, view);");
|
|
700
|
-
this.dedent();
|
|
701
|
-
this.emit("}");
|
|
702
|
-
this.emit(
|
|
703
|
-
"const vpUniform = createBuffer(device, buildViewProjection(), GPUBufferUsage.UNIFORM);"
|
|
704
|
-
);
|
|
705
|
-
this.emit("");
|
|
706
|
-
}
|
|
707
|
-
// ─── Compute Pipelines ────────────────────────────────────────────────────
|
|
708
|
-
emitComputeShaders(composition) {
|
|
709
|
-
const gpuObjs = (composition.objects || []).filter(
|
|
710
|
-
(o) => o.traits?.some(
|
|
711
|
-
(t) => isGpuParticleTraitName(t.name) || t.name === "gpu_physics" || t.name === "compute"
|
|
712
|
-
)
|
|
713
|
-
);
|
|
714
|
-
if (gpuObjs.length === 0) return;
|
|
715
|
-
this.emit("// === Compute Pipelines ===");
|
|
716
|
-
for (const obj of gpuObjs) {
|
|
717
|
-
const v = this.sanitizeName(obj.name);
|
|
718
|
-
const traits = obj.traits || [];
|
|
719
|
-
if (traits.some((t) => isGpuParticleTraitName(t.name))) {
|
|
720
|
-
const count = findGpuParticleTrait(traits)?.config?.count || 1e4;
|
|
721
|
-
this.emit(
|
|
722
|
-
`const ${v}ParticleCompute = device.createComputePipeline({ layout: "auto", compute: { module: device.createShaderModule({ code: WGSL_PARTICLE_COMPUTE }), entryPoint: "cs_particle_update" } });`
|
|
723
|
-
);
|
|
724
|
-
this.emit(`const ${v}ComputeDispatches = ${Math.ceil(count / 64)};`);
|
|
725
|
-
}
|
|
726
|
-
if (traits.some((t) => t.name === "gpu_physics")) {
|
|
727
|
-
this.emit(
|
|
728
|
-
`const ${v}PhysicsCompute = device.createComputePipeline({ layout: "auto", compute: { module: device.createShaderModule({ code: WGSL_PHYSICS_COMPUTE }), entryPoint: "cs_physics_step" } });`
|
|
729
|
-
);
|
|
730
|
-
}
|
|
731
|
-
if (traits.some((t) => t.name === "compute")) {
|
|
732
|
-
const ct = traits.find((t) => t.name === "compute");
|
|
733
|
-
const entry = ct?.config?.shader || "custom_compute";
|
|
734
|
-
const wg = ct?.config?.workgroups || [64, 1, 1];
|
|
735
|
-
this.emit(
|
|
736
|
-
`const ${v}CustomCompute = device.createComputePipeline({ layout: "auto", compute: { module: device.createShaderModule({ code: WGSL_CUSTOM_${v.toUpperCase()} }), entryPoint: "${entry}" } });`
|
|
737
|
-
);
|
|
738
|
-
this.emit(`const ${v}Workgroups = [${wg}];`);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
this.emit("");
|
|
742
|
-
}
|
|
743
|
-
// ─── WGSL Shader Sources ──────────────────────────────────────────────────
|
|
744
|
-
emitShaderSources() {
|
|
745
|
-
this.emit("// === WGSL Shader Sources ===");
|
|
746
|
-
this.emit("const WGSL_VERTEX = /* wgsl */`");
|
|
747
|
-
this.emit("struct Uniforms { model: mat4x4<f32> };");
|
|
748
|
-
this.emit("struct Camera { viewProj: mat4x4<f32> };");
|
|
749
|
-
this.emit("@group(0) @binding(0) var<uniform> u: Uniforms;");
|
|
750
|
-
this.emit("@group(0) @binding(2) var<uniform> cam: Camera;");
|
|
751
|
-
this.emit("struct VIn { @location(0) pos: vec3<f32>, @location(1) norm: vec3<f32> };");
|
|
752
|
-
this.emit(
|
|
753
|
-
"struct VOut { @builtin(position) clip: vec4<f32>, @location(0) wNorm: vec3<f32>, @location(1) wPos: vec3<f32> };"
|
|
754
|
-
);
|
|
755
|
-
this.emit("@vertex fn vs_main(i: VIn) -> VOut {");
|
|
756
|
-
this.emit(" var o: VOut; let w = u.model * vec4<f32>(i.pos, 1.0);");
|
|
757
|
-
this.emit(
|
|
758
|
-
" o.clip = cam.viewProj * w; o.wNorm = normalize((u.model * vec4<f32>(i.norm, 0.0)).xyz); o.wPos = w.xyz; return o; }`;"
|
|
759
|
-
);
|
|
760
|
-
this.emit("");
|
|
761
|
-
this.emit("const WGSL_FRAGMENT = /* wgsl */`");
|
|
762
|
-
this.emit("struct Mat { color: vec4<f32>, rm: vec4<f32>, emissive: vec4<f32> };");
|
|
763
|
-
this.emit("@group(0) @binding(1) var<uniform> mat: Mat;");
|
|
764
|
-
this.emit("struct FIn { @location(0) wNorm: vec3<f32>, @location(1) wPos: vec3<f32> };");
|
|
765
|
-
this.emit("@fragment fn fs_main(i: FIn) -> @location(0) vec4<f32> {");
|
|
766
|
-
this.emit(" let N = normalize(i.wNorm); let L = normalize(vec3<f32>(1.0, 2.0, 1.5));");
|
|
767
|
-
this.emit(" let d = max(dot(N, L), 0.0) * (1.0 - mat.rm[0] * 0.5);");
|
|
768
|
-
this.emit(" let lit = mat.color.rgb * (0.15 + d);");
|
|
769
|
-
this.emit(" let emit = mat.emissive.rgb * mat.rm[2];");
|
|
770
|
-
this.emit(" return vec4<f32>(lit + emit, mat.color.a); }`;");
|
|
771
|
-
this.emit("");
|
|
772
|
-
this.emit("const WGSL_SPLAT = /* wgsl */`");
|
|
773
|
-
this.emit(
|
|
774
|
-
"struct Splat { center: vec3<f32>, opacity: f32, cov: array<f32,6>, sh: vec3<f32> };"
|
|
775
|
-
);
|
|
776
|
-
this.emit("@group(0) @binding(0) var<storage, read> splats: array<Splat>;");
|
|
777
|
-
this.emit(
|
|
778
|
-
"struct SO { @builtin(position) p: vec4<f32>, @location(0) c: vec4<f32>, @location(1) uv: vec2<f32> };"
|
|
779
|
-
);
|
|
780
|
-
this.emit(
|
|
781
|
-
"@vertex fn vs_splat(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> SO {"
|
|
782
|
-
);
|
|
783
|
-
this.emit(" var q = array<vec2<f32>,4>(vec2(-1,-1),vec2(1,-1),vec2(-1,1),vec2(1,1));");
|
|
784
|
-
this.emit(
|
|
785
|
-
" var o: SO; let s = splats[ii]; o.p = vec4<f32>(s.center.xy + q[vi]*0.01, s.center[2], 1.0);"
|
|
786
|
-
);
|
|
787
|
-
this.emit(" o.c = vec4<f32>(s.sh, s.opacity); o.uv = q[vi]; return o; }");
|
|
788
|
-
this.emit("@fragment fn fs_splat(i: SO) -> @location(0) vec4<f32> {");
|
|
789
|
-
this.emit(" let a = i.c.a * exp(-dot(i.uv, i.uv) * 4.0); if (a < 0.01) { discard; }");
|
|
790
|
-
this.emit(" return vec4<f32>(i.c.rgb * a, a); }`;");
|
|
791
|
-
this.emit("");
|
|
792
|
-
this.emit("const WGSL_POINT = /* wgsl */`");
|
|
793
|
-
this.emit("struct PO { @builtin(position) p: vec4<f32>, @location(0) c: vec4<f32> };");
|
|
794
|
-
this.emit(
|
|
795
|
-
"@vertex fn vs_point(@location(0) pos: vec3<f32>, @location(1) col: vec4<f32>) -> PO { var o: PO; o.p = vec4<f32>(pos, 1.0); o.c = col; return o; }"
|
|
796
|
-
);
|
|
797
|
-
this.emit("@fragment fn fs_point(i: PO) -> @location(0) vec4<f32> { return i.c; }`;");
|
|
798
|
-
this.emit("");
|
|
799
|
-
this.emit("const WGSL_PARTICLE_RENDER = /* wgsl */`");
|
|
800
|
-
this.emit("struct PVO { @builtin(position) p: vec4<f32>, @location(0) life: f32 };");
|
|
801
|
-
this.emit(
|
|
802
|
-
"@vertex fn vs_particle(@location(0) pos: vec3<f32>, @location(1) vel: vec3<f32>, @location(2) life: f32) -> PVO { var o: PVO; o.p = vec4<f32>(pos, 1.0); o.life = life; return o; }"
|
|
803
|
-
);
|
|
804
|
-
this.emit("@fragment fn fs_particle(i: PVO) -> @location(0) vec4<f32> {");
|
|
805
|
-
this.emit(
|
|
806
|
-
" let t = clamp(i.life, 0.0, 1.0); return vec4<f32>(mix(vec3(1.0,0.2,0.0), vec3(1.0,1.0,0.5), t), t*0.8); }`;"
|
|
807
|
-
);
|
|
808
|
-
this.emit("");
|
|
809
|
-
this.emit("const WGSL_SCATTER = /* wgsl */`");
|
|
810
|
-
this.emit("struct VIn { @location(0) pos: vec3<f32>, @location(1) norm: vec3<f32> };");
|
|
811
|
-
this.emit("struct VOut { @builtin(position) clip: vec4<f32>, @location(0) shade: f32 };");
|
|
812
|
-
this.emit("fn hash11(v: f32) -> f32 { return fract(sin(v * 12.9898) * 43758.5453); }");
|
|
813
|
-
this.emit("@vertex fn vs_scatter(i: VIn, @builtin(instance_index) instance: u32) -> VOut {");
|
|
814
|
-
this.emit(" let id = f32(instance);");
|
|
815
|
-
this.emit(" let x = hash11(id + 1.0) * 1.8 - 0.9; let z = hash11(id + 7.0) * 1.8 - 0.9;");
|
|
816
|
-
this.emit(" let scale = 0.35 + hash11(id + 13.0) * 0.45;");
|
|
817
|
-
this.emit(" var o: VOut; o.clip = vec4<f32>(i.pos.x * scale + x, i.pos.y * scale - 0.65, i.pos.z * scale + z, 1.0);");
|
|
818
|
-
this.emit(" o.shade = 0.45 + max(i.norm.y, 0.0) * 0.45; return o; }");
|
|
819
|
-
this.emit("@fragment fn fs_scatter(i: VOut) -> @location(0) vec4<f32> { return vec4<f32>(0.08 * i.shade, 0.34 * i.shade, 0.12 * i.shade, 1.0); }`;");
|
|
820
|
-
this.emit("");
|
|
821
|
-
this.emit("const WGSL_POSTFX = /* wgsl */`");
|
|
822
|
-
this.emit("struct VO { @builtin(position) pos: vec4<f32> };");
|
|
823
|
-
this.emit("@vertex fn vs_postfx(@builtin(vertex_index) vi: u32) -> VO {");
|
|
824
|
-
this.emit(" var p = array<vec2<f32>,3>(vec2(-1.0,-1.0), vec2(3.0,-1.0), vec2(-1.0,3.0));");
|
|
825
|
-
this.emit(" var o: VO; o.pos = vec4<f32>(p[vi], 0.0, 1.0); return o; }");
|
|
826
|
-
this.emit("@fragment fn fs_postfx() -> @location(0) vec4<f32> { return vec4<f32>(0.04, 0.08, 0.05, 0.18); }`;");
|
|
827
|
-
this.emit("");
|
|
828
|
-
this.emit("const WGSL_PARTICLE_COMPUTE = /* wgsl */`");
|
|
829
|
-
this.emit("struct P { pos: vec3<f32>, _p0: f32, vel: vec3<f32>, life: f32 };");
|
|
830
|
-
this.emit("@group(0) @binding(0) var<storage, read> pin: array<P>;");
|
|
831
|
-
this.emit("@group(0) @binding(1) var<storage, read_write> pout: array<P>;");
|
|
832
|
-
this.emit("@group(0) @binding(2) var<uniform> dt: f32;");
|
|
833
|
-
this.emit(
|
|
834
|
-
"@compute @workgroup_size(64) fn cs_particle_update(@builtin(global_invocation_id) gid: vec3<u32>) {"
|
|
835
|
-
);
|
|
836
|
-
this.emit(" let i = gid[0]; if (i >= arrayLength(&pin)) { return; } var p = pin[i];");
|
|
837
|
-
this.emit(" p.vel[1] -= 9.81 * dt; p.pos += p.vel * dt; p.life -= dt;");
|
|
838
|
-
this.emit(
|
|
839
|
-
" if (p.life <= 0.0) { p.pos = vec3(0.0); p.vel = vec3(0.0, 5.0, 0.0); p.life = 2.0; }"
|
|
840
|
-
);
|
|
841
|
-
this.emit(" pout[i] = p; }`;");
|
|
842
|
-
this.emit("");
|
|
843
|
-
this.emit("const WGSL_PHYSICS_COMPUTE = /* wgsl */`");
|
|
844
|
-
this.emit("struct Body { pos: vec3<f32>, mass: f32, vel: vec3<f32>, _p: f32 };");
|
|
845
|
-
this.emit("@group(0) @binding(0) var<storage, read_write> bodies: array<Body>;");
|
|
846
|
-
this.emit("@group(0) @binding(1) var<uniform> dt: f32;");
|
|
847
|
-
this.emit(
|
|
848
|
-
"@compute @workgroup_size(64) fn cs_physics_step(@builtin(global_invocation_id) gid: vec3<u32>) {"
|
|
849
|
-
);
|
|
850
|
-
this.emit(" let i = gid[0]; if (i >= arrayLength(&bodies)) { return; } var b = bodies[i];");
|
|
851
|
-
this.emit(" b.vel[1] -= 9.81 * dt; b.pos += b.vel * dt;");
|
|
852
|
-
this.emit(
|
|
853
|
-
" if (b.pos[1] < 0.0) { b.pos[1] = 0.0; b.vel[1] = -b.vel[1] * 0.6; } bodies[i] = b; }`;"
|
|
854
|
-
);
|
|
855
|
-
this.emit("");
|
|
856
|
-
}
|
|
857
|
-
// ─── Render Loop ──────────────────────────────────────────────────────────
|
|
858
|
-
emitRenderLoop(composition) {
|
|
859
|
-
this.emit("// === Render Loop ===");
|
|
860
|
-
this.emit("let frameCount = 0;");
|
|
861
|
-
this.emit("const t0 = performance.now();");
|
|
862
|
-
this.emit("function frame() {");
|
|
863
|
-
this.indent();
|
|
864
|
-
this.emit("frameCount++;");
|
|
865
|
-
this.emit("const time = (performance.now() - t0) / 1000.0;");
|
|
866
|
-
this.emit("const enc = device.createCommandEncoder();");
|
|
867
|
-
const gpuObjs = (composition.objects || []).filter(
|
|
868
|
-
(o) => o.traits?.some(
|
|
869
|
-
(t) => isGpuParticleTraitName(t.name) || t.name === "gpu_physics" || t.name === "compute"
|
|
870
|
-
)
|
|
871
|
-
);
|
|
872
|
-
if (gpuObjs.length > 0 && this.options.enableCompute) {
|
|
873
|
-
this.emit("const cp = enc.beginComputePass();");
|
|
874
|
-
for (const obj of gpuObjs) {
|
|
875
|
-
const v = this.sanitizeName(obj.name);
|
|
876
|
-
if (obj.traits?.some((t) => isGpuParticleTraitName(t.name))) {
|
|
877
|
-
this.emit(
|
|
878
|
-
`cp.setPipeline(${v}ParticleCompute); cp.dispatchWorkgroups(${v}ComputeDispatches);`
|
|
879
|
-
);
|
|
880
|
-
}
|
|
881
|
-
if (obj.traits?.some((t) => t.name === "gpu_physics")) {
|
|
882
|
-
this.emit(`cp.setPipeline(${v}PhysicsCompute); cp.dispatchWorkgroups(16);`);
|
|
883
|
-
}
|
|
884
|
-
if (obj.traits?.some((t) => t.name === "compute")) {
|
|
885
|
-
this.emit(`cp.setPipeline(${v}CustomCompute); cp.dispatchWorkgroups(...${v}Workgroups);`);
|
|
886
|
-
}
|
|
887
|
-
}
|
|
888
|
-
this.emit("cp.end();");
|
|
889
|
-
}
|
|
890
|
-
this.emit("const rp = enc.beginRenderPass({");
|
|
891
|
-
this.indent();
|
|
892
|
-
this.emit(
|
|
893
|
-
'colorAttachments: [{ view: msaaTexture.createView(), resolveTarget: context.getCurrentTexture().createView(), clearValue: clearColor, loadOp: "clear", storeOp: "store" }],'
|
|
894
|
-
);
|
|
895
|
-
this.emit(
|
|
896
|
-
'depthStencilAttachment: { view: depthTexture.createView(), depthClearValue: 1.0, depthLoadOp: "clear", depthStoreOp: "store" },'
|
|
897
|
-
);
|
|
898
|
-
this.dedent();
|
|
899
|
-
this.emit("});");
|
|
900
|
-
if (composition.objects) {
|
|
901
|
-
for (const obj of composition.objects) this.emitDrawCall(obj);
|
|
902
|
-
}
|
|
903
|
-
if (composition.spatialGroups) {
|
|
904
|
-
for (const group of composition.spatialGroups) {
|
|
905
|
-
for (const obj of group.objects) this.emitDrawCall(obj);
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
this.emitDomainBlockDrawCalls(composition, false);
|
|
909
|
-
this.emitDomainBlockDrawCalls(composition, true);
|
|
910
|
-
this.emit("rp.end();");
|
|
911
|
-
this.emit("device.queue.submit([enc.finish()]);");
|
|
912
|
-
this.emit("document.body.dataset.holoscriptWebgpuFrame = String(frameCount);");
|
|
913
|
-
this.emit("requestAnimationFrame(frame);");
|
|
914
|
-
this.dedent();
|
|
915
|
-
this.emit("}");
|
|
916
|
-
this.emit("requestAnimationFrame(frame);");
|
|
917
|
-
}
|
|
918
|
-
emitDrawCall(obj) {
|
|
919
|
-
const v = this.sanitizeName(obj.name);
|
|
920
|
-
const traits = obj.traits || [];
|
|
921
|
-
if (traits.some((t) => t.name === "gaussian_splat")) {
|
|
922
|
-
this.emit(`rp.setPipeline(${v}SplatPipeline); rp.draw(4, ${v}SplatCount.value);`);
|
|
923
|
-
} else if (traits.some((t) => t.name === "point_cloud")) {
|
|
924
|
-
this.emit(`rp.setPipeline(${v}PointPipeline); rp.draw(${v}PointCount.value);`);
|
|
925
|
-
} else if (traits.some((t) => isGpuParticleTraitName(t.name))) {
|
|
926
|
-
this.emit(
|
|
927
|
-
`rp.setPipeline(${v}ParticlePipeline); rp.setVertexBuffer(0, ${v}ParticleBufA); rp.draw(${v}ParticleCount);`
|
|
928
|
-
);
|
|
929
|
-
} else {
|
|
930
|
-
this.emit(
|
|
931
|
-
`rp.setPipeline(${v}Pipeline); rp.setBindGroup(0, ${v}Bind); rp.setVertexBuffer(0, ${v}VBO); rp.draw(${v}VertexCount);`
|
|
932
|
-
);
|
|
933
|
-
}
|
|
934
|
-
if (obj.children) {
|
|
935
|
-
for (const child of obj.children) this.emitDrawCall(child);
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
emitDomainBlockDrawCalls(composition, postFxOnly) {
|
|
939
|
-
const blocks = composition.domainBlocks ?? [];
|
|
940
|
-
for (let i = 0; i < blocks.length; i++) {
|
|
941
|
-
const block = blocks[i];
|
|
942
|
-
const prefix = this.domainBlockVar(block, i);
|
|
943
|
-
if (block.domain === "postfx") {
|
|
944
|
-
if (postFxOnly) {
|
|
945
|
-
this.emit(`rp.setPipeline(${prefix}PostFXPipeline); rp.draw(3);`);
|
|
946
|
-
}
|
|
947
|
-
continue;
|
|
948
|
-
}
|
|
949
|
-
if (postFxOnly) continue;
|
|
950
|
-
if (block.domain === "vfx") {
|
|
951
|
-
this.emit(
|
|
952
|
-
`rp.setPipeline(${prefix}ParticlePipeline); rp.setVertexBuffer(0, ${prefix}ParticleBuf); rp.draw(${prefix}ParticleCount);`
|
|
953
|
-
);
|
|
954
|
-
}
|
|
955
|
-
if (block.domain === "procedural" && (block.keyword === "scatter" || block.keyword === "distribute" || block.properties?.count)) {
|
|
956
|
-
this.emit(
|
|
957
|
-
`rp.setPipeline(${prefix}ScatterPipeline); rp.setVertexBuffer(0, ${prefix}ScatterVBO); rp.draw(${prefix}ScatterVertexCount, ${prefix}ScatterInstanceCount);`
|
|
958
|
-
);
|
|
959
|
-
}
|
|
960
|
-
}
|
|
961
|
-
}
|
|
962
|
-
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
963
|
-
emit(line) {
|
|
964
|
-
this.lines.push(this.options.indent.repeat(this.indentLevel) + line);
|
|
965
|
-
}
|
|
966
|
-
emitMultiline(code) {
|
|
967
|
-
for (const line of code.split("\n")) this.emit(line);
|
|
968
|
-
}
|
|
969
|
-
indent() {
|
|
970
|
-
this.indentLevel++;
|
|
971
|
-
}
|
|
972
|
-
dedent() {
|
|
973
|
-
this.indentLevel = Math.max(0, this.indentLevel - 1);
|
|
974
|
-
}
|
|
975
|
-
sanitizeName(name) {
|
|
976
|
-
return name.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
977
|
-
}
|
|
978
|
-
domainBlockVar(block, index) {
|
|
979
|
-
const raw = `${block.domain}_${block.keyword}_${block.name || index}`;
|
|
980
|
-
const safe = this.sanitizeName(raw);
|
|
981
|
-
return `db${index}_${safe || "domain"}`;
|
|
982
|
-
}
|
|
983
|
-
json(value) {
|
|
984
|
-
return JSON.stringify(value).replace(/</g, "\\u003c");
|
|
985
|
-
}
|
|
986
|
-
numberProp(block, keys, fallback) {
|
|
987
|
-
for (const key of keys) {
|
|
988
|
-
const value = block.properties?.[key];
|
|
989
|
-
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
990
|
-
if (typeof value === "string" && value.trim() !== "") {
|
|
991
|
-
const parsed = Number(value);
|
|
992
|
-
if (Number.isFinite(parsed)) return parsed;
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
return fallback;
|
|
996
|
-
}
|
|
997
|
-
hashSeed(value) {
|
|
998
|
-
let h = 2166136261;
|
|
999
|
-
for (let i = 0; i < value.length; i++) {
|
|
1000
|
-
h ^= value.charCodeAt(i);
|
|
1001
|
-
h = Math.imul(h, 16777619);
|
|
1002
|
-
}
|
|
1003
|
-
return h >>> 0;
|
|
1004
|
-
}
|
|
1005
|
-
findObjProp(obj, key) {
|
|
1006
|
-
return obj.properties?.find((p) => p.key === key)?.value;
|
|
1007
|
-
}
|
|
1008
|
-
parseColor(value) {
|
|
1009
|
-
if (typeof value === "string" && value.startsWith("#")) {
|
|
1010
|
-
const h = value.slice(1);
|
|
1011
|
-
return [
|
|
1012
|
-
parseFloat((parseInt(h.substring(0, 2), 16) / 255).toFixed(4)),
|
|
1013
|
-
parseFloat((parseInt(h.substring(2, 4), 16) / 255).toFixed(4)),
|
|
1014
|
-
parseFloat((parseInt(h.substring(4, 6), 16) / 255).toFixed(4))
|
|
1015
|
-
];
|
|
1016
|
-
}
|
|
1017
|
-
if (Array.isArray(value) && value.length >= 3) return [value[0], value[1], value[2]];
|
|
1018
|
-
return [1, 1, 1];
|
|
1019
|
-
}
|
|
1020
|
-
extractMaterialColor(material, color) {
|
|
1021
|
-
if (color) return this.parseColor(color);
|
|
1022
|
-
if (material && typeof material === "object" && !Array.isArray(material)) {
|
|
1023
|
-
const m = material;
|
|
1024
|
-
if (m.color) return this.parseColor(m.color);
|
|
1025
|
-
}
|
|
1026
|
-
return [0.8, 0.8, 0.8];
|
|
1027
|
-
}
|
|
1028
|
-
extractMaterialProp(material, key, fallback) {
|
|
1029
|
-
if (material && typeof material === "object" && !Array.isArray(material)) {
|
|
1030
|
-
const m = material;
|
|
1031
|
-
if (m[key] !== void 0) return m[key];
|
|
1032
|
-
}
|
|
1033
|
-
return fallback;
|
|
1034
|
-
}
|
|
1035
|
-
extractEmissive(material, obj) {
|
|
1036
|
-
const objEmissive = this.findObjProp(obj, "emissive");
|
|
1037
|
-
if (objEmissive !== void 0) return this.parseColor(objEmissive);
|
|
1038
|
-
if (material && typeof material === "object" && !Array.isArray(material)) {
|
|
1039
|
-
const m = material;
|
|
1040
|
-
if (m.emissive !== void 0) return this.parseColor(m.emissive);
|
|
1041
|
-
}
|
|
1042
|
-
return [0, 0, 0];
|
|
1043
|
-
}
|
|
1044
|
-
geometryVertexDataFn(meshType) {
|
|
1045
|
-
const map = {
|
|
1046
|
-
sphere: "generateSphereVertices(0.5, 16, 16)",
|
|
1047
|
-
orb: "generateSphereVertices(0.5, 16, 16)",
|
|
1048
|
-
plane: "generatePlaneVertices(1.0, 1.0)",
|
|
1049
|
-
cylinder: "generateCylinderVertices(0.5, 1.0, 16)",
|
|
1050
|
-
cone: "generateConeVertices(0.5, 1.0, 16)",
|
|
1051
|
-
pyramid: "generateConeVertices(0.5, 1.0, 16)",
|
|
1052
|
-
torus: "generateTorusVertices(0.5, 0.15, 16, 32)"
|
|
1053
|
-
};
|
|
1054
|
-
return map[meshType] || "generateCubeVertices(1.0)";
|
|
1055
|
-
}
|
|
1056
|
-
};
|
|
1057
|
-
}
|
|
1058
|
-
});
|
|
1059
|
-
|
|
1060
|
-
export { WebGPUCompiler, WebGPUCompiler_exports, init_WebGPUCompiler };
|
|
1061
|
-
//# sourceMappingURL=chunk-KEYG7HGY.js.map
|
|
1062
|
-
//# sourceMappingURL=chunk-KEYG7HGY.js.map
|