| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import * as THREE from 'three'
- import { pipeSurface } from '../domain/pipePhysical.js'
- const textures = new Map()
- function surfaceTexture(kind) {
- if (textures.has(kind)) return textures.get(kind)
- const size = 64, data = new Uint8Array(size * size * 4)
- for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) {
- const noise = ((x * 137 + y * 211 + x * y * 17) % 97) / 96
- const grain = kind === 'concrete' ? 0.52 + noise * 0.48
- : kind === 'iron' ? 0.62 + noise * 0.38
- : kind === 'steel' || kind === 'copper' ? 0.7 + (Math.sin(y * 2.7) + 1) * 0.1 + noise * 0.1
- : kind === 'frp' ? 0.76 + Math.sin(x * 0.8 + y * 0.8) * 0.12
- : kind === 'pe' || kind === 'pvc' ? 0.9 + Math.sin(y * 0.4) * 0.06 : 1
- const at = (y * size + x) * 4
- data[at] = data[at + 1] = data[at + 2] = Math.round(grain * 255)
- data[at + 3] = 255
- }
- const texture = new THREE.DataTexture(data, size, size, THREE.RGBAFormat)
- texture.wrapS = texture.wrapT = THREE.RepeatWrapping
- texture.magFilter = THREE.LinearFilter
- texture.minFilter = THREE.LinearMipmapLinearFilter
- texture.generateMipmaps = true
- texture.needsUpdate = true
- const record = { texture, references: 0 }
- textures.set(kind, record)
- return record
- }
- /** Industry tint + reported material reflectance and illustrative surface grain. */
- export function pipeSurfaceMaterial(color, surface = pipeSurface('')) {
- const record = surfaceTexture(surface.kind)
- record.references += 1
- const bumpScale = surface.kind === 'concrete' ? 0.07 : surface.kind === 'iron' ? 0.035
- : surface.kind === 'steel' || surface.kind === 'copper' ? 0.008 : 0.003
- const material = new THREE.MeshStandardMaterial({ color, metalness: surface.metalness, roughness: surface.roughness,
- map: record.texture, bumpMap: record.texture, bumpScale, side: THREE.DoubleSide,
- emissive: color, emissiveIntensity: 0.045 })
- material.userData.pipeSurface = surface.kind
- material.addEventListener('dispose', () => {
- if (--record.references === 0) { record.texture.dispose(); textures.delete(surface.kind) }
- })
- return material
- }
- /** Screen-space inverted hull: highlights the selected model without changing its physical geometry. */
- export function outlineSelectedPipe(root) {
- const meshes = []
- root.traverse((node) => { if (node.isMesh) meshes.push(node) })
- if (!meshes.length) return
- const material = new THREE.ShaderMaterial({
- uniforms: { resolution: { value: new THREE.Vector2(1, 1) }, widthPx: { value: 2 }, color: { value: new THREE.Color('#e3fbff') } },
- side: THREE.BackSide, depthWrite: false, depthTest: true,
- vertexShader: `uniform vec2 resolution; uniform float widthPx;
- void main() {
- vec4 clip = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
- vec2 direction = (projectionMatrix * vec4(normalize(normalMatrix * normal), 0.0)).xy;
- float magnitude = length(direction);
- if (magnitude > 0.00001) clip.xy += direction / magnitude * widthPx * 2.0 / resolution * clip.w;
- gl_Position = clip;
- }`,
- fragmentShader: `uniform vec3 color; void main() { gl_FragColor = vec4(color, 1.0); }`,
- })
- for (const mesh of meshes) {
- const outline = new THREE.Mesh(mesh.geometry, material)
- outline.name = 'selected-pipe-outline'
- outline.raycast = () => {}
- outline.renderOrder = 1
- mesh.add(outline)
- }
- }
|