pipeSurfaceMaterial.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as THREE from 'three'
  2. import { pipeSurface } from '../domain/pipePhysical.js'
  3. const textures = new Map()
  4. function surfaceTexture(kind) {
  5. if (textures.has(kind)) return textures.get(kind)
  6. const size = 64, data = new Uint8Array(size * size * 4)
  7. for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) {
  8. const noise = ((x * 137 + y * 211 + x * y * 17) % 97) / 96
  9. const grain = kind === 'concrete' ? 0.52 + noise * 0.48
  10. : kind === 'iron' ? 0.62 + noise * 0.38
  11. : kind === 'steel' || kind === 'copper' ? 0.7 + (Math.sin(y * 2.7) + 1) * 0.1 + noise * 0.1
  12. : kind === 'frp' ? 0.76 + Math.sin(x * 0.8 + y * 0.8) * 0.12
  13. : kind === 'pe' || kind === 'pvc' ? 0.9 + Math.sin(y * 0.4) * 0.06 : 1
  14. const at = (y * size + x) * 4
  15. data[at] = data[at + 1] = data[at + 2] = Math.round(grain * 255)
  16. data[at + 3] = 255
  17. }
  18. const texture = new THREE.DataTexture(data, size, size, THREE.RGBAFormat)
  19. texture.wrapS = texture.wrapT = THREE.RepeatWrapping
  20. texture.magFilter = THREE.LinearFilter
  21. texture.minFilter = THREE.LinearMipmapLinearFilter
  22. texture.generateMipmaps = true
  23. texture.needsUpdate = true
  24. const record = { texture, references: 0 }
  25. textures.set(kind, record)
  26. return record
  27. }
  28. /** Industry tint + reported material reflectance and illustrative surface grain. */
  29. export function pipeSurfaceMaterial(color, surface = pipeSurface('')) {
  30. const record = surfaceTexture(surface.kind)
  31. record.references += 1
  32. const bumpScale = surface.kind === 'concrete' ? 0.07 : surface.kind === 'iron' ? 0.035
  33. : surface.kind === 'steel' || surface.kind === 'copper' ? 0.008 : 0.003
  34. const material = new THREE.MeshStandardMaterial({ color, metalness: surface.metalness, roughness: surface.roughness,
  35. map: record.texture, bumpMap: record.texture, bumpScale, side: THREE.DoubleSide,
  36. emissive: color, emissiveIntensity: 0.045 })
  37. material.userData.pipeSurface = surface.kind
  38. material.addEventListener('dispose', () => {
  39. if (--record.references === 0) { record.texture.dispose(); textures.delete(surface.kind) }
  40. })
  41. return material
  42. }
  43. /** Screen-space inverted hull: highlights the selected model without changing its physical geometry. */
  44. export function outlineSelectedPipe(root) {
  45. const meshes = []
  46. root.traverse((node) => { if (node.isMesh) meshes.push(node) })
  47. if (!meshes.length) return
  48. const material = new THREE.ShaderMaterial({
  49. uniforms: { resolution: { value: new THREE.Vector2(1, 1) }, widthPx: { value: 2 }, color: { value: new THREE.Color('#e3fbff') } },
  50. side: THREE.BackSide, depthWrite: false, depthTest: true,
  51. vertexShader: `uniform vec2 resolution; uniform float widthPx;
  52. void main() {
  53. vec4 clip = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  54. vec2 direction = (projectionMatrix * vec4(normalize(normalMatrix * normal), 0.0)).xy;
  55. float magnitude = length(direction);
  56. if (magnitude > 0.00001) clip.xy += direction / magnitude * widthPx * 2.0 / resolution * clip.w;
  57. gl_Position = clip;
  58. }`,
  59. fragmentShader: `uniform vec3 color; void main() { gl_FragColor = vec4(color, 1.0); }`,
  60. })
  61. for (const mesh of meshes) {
  62. const outline = new THREE.Mesh(mesh.geometry, material)
  63. outline.name = 'selected-pipe-outline'
  64. outline.raycast = () => {}
  65. outline.renderOrder = 1
  66. mesh.add(outline)
  67. }
  68. }