UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

77 lines (76 loc) 1.86 kB
import { DeviceCache } from "../platform/graphics/device-cache.js"; import { SHADER_FORWARD, SHADER_PICK, SHADER_SHADOW, SHADER_PREPASS, SHADER_DEPTH_PICK } from "./constants.js"; const shaderPassDeviceCache = new DeviceCache(); class ShaderPassInfo { index; name; defines = /* @__PURE__ */ new Map(); constructor(name, index, options = {}) { this.name = name; this.index = index; Object.assign(this, options); this.buildShaderDefines(); } buildShaderDefines() { let keyword; if (this.isShadow) { keyword = "SHADOW"; } else if (this.isForward) { keyword = "FORWARD"; } else if (this.index === SHADER_PICK) { keyword = "PICK"; } else if (this.index === SHADER_DEPTH_PICK) { keyword = "PICK"; this.defines.set("DEPTH_PICK_PASS", ""); } this.defines.set(`${keyword}_PASS`, ""); this.defines.set(`${this.name.toUpperCase()}_PASS`, ""); } } class ShaderPass { passesNamed = /* @__PURE__ */ new Map(); passesIndexed = []; nextIndex = 0; constructor() { const add = (name, index, options) => { const info = this.allocate(name, options); }; add("forward", SHADER_FORWARD, { isForward: true }); add("prepass", SHADER_PREPASS); add("shadow", SHADER_SHADOW); add("pick", SHADER_PICK); add("depth_pick", SHADER_DEPTH_PICK); } static get(device) { return shaderPassDeviceCache.get(device, () => { return new ShaderPass(); }); } allocate(name, options) { let info = this.passesNamed.get(name); if (info === void 0) { info = new ShaderPassInfo(name, this.nextIndex, options); this.passesNamed.set(info.name, info); this.passesIndexed[info.index] = info; this.nextIndex++; } return info; } getByIndex(index) { const info = this.passesIndexed[index]; return info; } getByName(name) { return this.passesNamed.get(name); } } export { ShaderPass, ShaderPassInfo };