Switched loadShader() to a Vite-bundled static shader map using import.meta.glob(..., { as: "raw", eager: true })

This commit is contained in:
scamiv
2026-01-18 00:33:45 +01:00
parent 4a07339e38
commit e37bbbb169
2 changed files with 12 additions and 22 deletions
+11 -21
View File
@@ -1,28 +1,18 @@
/**
* Utility for loading WGSL shader files via Vite ?raw imports.
* Caches loaded shaders to avoid re-importing.
* Utility for loading WGSL shader sources bundled by Vite.
* Uses a static glob so production builds reliably include all shaders.
*/
const shaderCache = new Map<string, Promise<string>>();
const shaderSources = import.meta.glob("../shaders/**/*.wgsl", {
as: "raw",
eager: true,
}) as Record<string, string>;
/**
* Load a shader file from the shaders directory.
* @param path Relative path from shaders/ directory (e.g., "compute/state-update.wgsl")
* @returns Promise resolving to the shader code as a string
*/
export async function loadShader(path: string): Promise<string> {
// Check cache first
if (shaderCache.has(path)) {
return shaderCache.get(path)!;
const key = `../shaders/${path}`;
const src = shaderSources[key];
if (!src) {
throw new Error(`Missing WGSL shader source: ${key}`);
}
// Import shader using Vite ?raw import
const shaderPromise = import(`../shaders/${path}?raw`).then(
(module) => module.default as string,
);
// Cache the promise
shaderCache.set(path, shaderPromise);
return shaderPromise;
return src;
}
@@ -57,7 +57,7 @@ fn fsMain(@builtin(position) pos: vec4f) -> @location(0) vec4f {
territoryRgb = mix(
territoryRgb,
vec3f(1.0, 0.0, 1.0),
clamp(0.35 * defendedStrength, 0.0, 0.35),
clamp(0.8 * defendedStrength, 0.1, 0.35),
);
if (hasFallout) {
// Fallout color is at index 0