/** * Cesium 运行时补丁:为 PostProcessStage 添加 sampler3D uniform 支持。 * * SuperMap 定制的 Cesium.js 缺少对 sampler3D (GL_SAMPLER_3D = 35679) 类型的 * uniform 处理,导致含有 3D 纹理的 PostProcessStage 渲染崩溃。 * * 修复策略: * 包装 WebGL2RenderingContext.prototype.getActiveUniform, * 将 SAMPLER_3D (35679) 报告为 SAMPLER_2D (35678), * 让 Cesium 的正常 uniform 处理流程接管。 * 实际的 3D 纹理绑定由自定义纹理对象的 bind() 方法(使用 GL_TEXTURE_3D)完成。 * * 必须在 Cesium 加载之后、任何 PostProcessStage 创建之前执行。 */ (function patchCesiumSampler3D() { if (typeof Cesium === 'undefined') { console.warn('[CesiumPatch] Cesium 未加载,跳过补丁'); return; } const SAMPLER_3D = 35679; const SAMPLER_2D = 35678; let patchedCount = 0; // ── 包装 getActiveUniform ────────────────────────────────────── // 让 Cesium 把所有 sampler3D uniform 当作 sampler2D 处理。 // 实际 3D 纹理绑定由纹理对象的自定义 bind() 方法自动处理。 function wrapGetActiveUniform(proto) { if (!proto || !proto.getActiveUniform) return false; const orig = proto.getActiveUniform; proto.getActiveUniform = function (program, index) { const info = orig.call(this, program, index); if (info && info.type === SAMPLER_3D) { info.type = SAMPLER_2D; patchedCount++; } return info; }; return true; } // WebGL2 上下文继承自 WebGLRenderingContext const patched2 = wrapGetActiveUniform(WebGL2RenderingContext.prototype); const patched1 = wrapGetActiveUniform(WebGLRenderingContext.prototype); if (patched2 || patched1) { console.log('[CesiumPatch] sampler3D → sampler2D 补丁已应用 ✅'); console.log('[CesiumPatch] WebGL2RenderingContext:', patched2); console.log('[CesiumPatch] WebGLRenderingContext:', patched1); } else { console.warn('[CesiumPatch] 无法找到 getActiveUniform 进行补丁'); } })();