import { Cartesian3, Color, CustomShader, LightingModel, UniformType } from 'cesium' import { PIPE_PALETTE } from '../domain/pipeVisual.js' import { pipeSurface } from '../domain/pipePhysical.js' /** Shared source-material reflectance with the established industry tint. */ export function createPipeModelShader(colorKey, surface = pipeSurface('')) { const color = Color.fromCssColorString((PIPE_PALETTE[colorKey] || PIPE_PALETTE.UNKNOWN).color) return new CustomShader({ lightingModel: LightingModel.PBR, uniforms: { u_pipeColor: { type:UniformType.VEC3, value:new Cartesian3(color.red,color.green,color.blue) }, u_pipeMetalness: { type:UniformType.FLOAT, value:surface.metalness }, u_pipeRoughness: { type:UniformType.FLOAT, value:surface.roughness }, u_pipeGrain: { type:UniformType.FLOAT, value:['concrete', 'iron'].includes(surface.kind) ? 0.32 : ['steel', 'copper', 'frp'].includes(surface.kind) ? 0.18 : surface.kind === 'unknown' ? 0 : 0.04 }, u_pipeCoarse: { type:UniformType.FLOAT, value:['concrete', 'iron'].includes(surface.kind) ? 1 : 0 }, }, // Cesium 1.145 的 czm_modelMaterial 没有 metallic 字段(glTF 的 metallic 在采样阶段 // 就被折进 diffuse/specular),写它会报 `'metallic' : no such field in structure` // 并让整个 3D 场景 "Rendering has stopped"。金属度按引擎自身口径摊到这两个字段: // diffuse = mix(color, black, m)(金属无漫反射) // specular = mix(0.04, color, m)(0.04 = glTF 介质 F0,同 czm_dielectricSpecularF0) fragmentShaderText: `void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) { vec3 p = fsInput.attributes.positionMC; float particles = fract(sin(dot(p, vec3(127.1, 311.7, 74.7))) * 43758.5453); float brushed = 0.5 + 0.5 * sin(p.z * 150.0); float grain = mix(brushed, particles, u_pipeCoarse); vec3 linearColor = czm_srgbToLinear(u_pipeColor) * (1.0 - u_pipeGrain * grain); float metalness = clamp(u_pipeMetalness, 0.0, 1.0); material.baseColor = vec4(linearColor, 1.0); material.diffuse = mix(linearColor, vec3(0.0), metalness); material.specular = mix(vec3(0.04), linearColor, metalness); material.roughness = u_pipeRoughness; material.emissive = linearColor * 0.08; material.alpha = 1.0; }`, }) }