mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 21:04:14 +00:00
Switched loadShader() to a Vite-bundled static shader map using import.meta.glob(..., { as: "raw", eager: true })
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user