pipeModelShader.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { Cartesian3, Color, CustomShader, LightingModel, UniformType } from 'cesium'
  2. import { PIPE_PALETTE } from '../domain/pipeVisual.js'
  3. import { pipeSurface } from '../domain/pipePhysical.js'
  4. /** Shared source-material reflectance with the established industry tint. */
  5. export function createPipeModelShader(colorKey, surface = pipeSurface('')) {
  6. const color = Color.fromCssColorString((PIPE_PALETTE[colorKey] || PIPE_PALETTE.UNKNOWN).color)
  7. return new CustomShader({
  8. lightingModel: LightingModel.PBR,
  9. uniforms: {
  10. u_pipeColor: { type:UniformType.VEC3, value:new Cartesian3(color.red,color.green,color.blue) },
  11. u_pipeMetalness: { type:UniformType.FLOAT, value:surface.metalness },
  12. u_pipeRoughness: { type:UniformType.FLOAT, value:surface.roughness },
  13. 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 },
  14. u_pipeCoarse: { type:UniformType.FLOAT, value:['concrete', 'iron'].includes(surface.kind) ? 1 : 0 },
  15. },
  16. // Cesium 1.145 的 czm_modelMaterial 没有 metallic 字段(glTF 的 metallic 在采样阶段
  17. // 就被折进 diffuse/specular),写它会报 `'metallic' : no such field in structure`
  18. // 并让整个 3D 场景 "Rendering has stopped"。金属度按引擎自身口径摊到这两个字段:
  19. // diffuse = mix(color, black, m)(金属无漫反射)
  20. // specular = mix(0.04, color, m)(0.04 = glTF 介质 F0,同 czm_dielectricSpecularF0)
  21. fragmentShaderText: `void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) {
  22. vec3 p = fsInput.attributes.positionMC;
  23. float particles = fract(sin(dot(p, vec3(127.1, 311.7, 74.7))) * 43758.5453);
  24. float brushed = 0.5 + 0.5 * sin(p.z * 150.0);
  25. float grain = mix(brushed, particles, u_pipeCoarse);
  26. vec3 linearColor = czm_srgbToLinear(u_pipeColor) * (1.0 - u_pipeGrain * grain);
  27. float metalness = clamp(u_pipeMetalness, 0.0, 1.0);
  28. material.baseColor = vec4(linearColor, 1.0);
  29. material.diffuse = mix(linearColor, vec3(0.0), metalness);
  30. material.specular = mix(vec3(0.04), linearColor, metalness);
  31. material.roughness = u_pipeRoughness;
  32. material.emissive = linearColor * 0.08;
  33. material.alpha = 1.0;
  34. }`,
  35. })
  36. }