refactor: 完善材质系统相关代码

- 修复 node-actions.ts 中 forEach 返回值 lint 警告
- 更新材质 schema 定义和渲染器
- wall-cutout.tsx 材质颜色保留修复
This commit is contained in:
Developer
2026-03-30 20:29:29 +08:00
parent d0cdeb2ac4
commit 0be6d84c98
12 changed files with 129 additions and 33 deletions
@@ -3,7 +3,7 @@ import { useMemo, useRef } from 'react'
import { float, mix, positionWorld, smoothstep } from 'three/tsl'
import { BackSide, FrontSide, type Mesh, MeshBasicNodeMaterial } from 'three/webgpu'
import { useNodeEvents } from '../../../hooks/use-node-events'
import { createMaterial, DEFAULT_CEILING_MATERIAL } from '../../../lib/materials'
import { DEFAULT_CEILING_MATERIAL } from '../../../lib/materials'
import { NodeRenderer } from '../node-renderer'
const gridScale = 5
@@ -46,7 +46,10 @@ export const CeilingRenderer = ({ node }: { node: CeilingNode }) => {
const color = props?.color || '#999999'
return createCeilingMaterials(color)
}
return { topMaterial: createCeilingMaterials().topMaterial, bottomMaterial: DEFAULT_CEILING_MATERIAL }
return {
topMaterial: createCeilingMaterials().topMaterial,
bottomMaterial: DEFAULT_CEILING_MATERIAL,
}
}, [node.material, node.material?.preset, node.material?.properties, node.material?.texture])
return (
@@ -35,4 +35,4 @@ export const RoofSegmentRenderer = ({ node }: { node: RoofSegmentNode }) => {
<boxGeometry args={[0, 0, 0]} />
</mesh>
)
}
}
@@ -2,7 +2,7 @@ import { type RoofNode, useRegistry } from '@pascal-app/core'
import { useMemo, useRef } from 'react'
import type * as THREE from 'three'
import { useNodeEvents } from '../../../hooks/use-node-events'
import { createMaterial, DEFAULT_ROOF_MATERIAL } from '../../../lib/materials'
import { createMaterial } from '../../../lib/materials'
import useViewer from '../../../store/use-viewer'
import { NodeRenderer } from '../node-renderer'
import { roofDebugMaterials, roofMaterials } from './roof-materials'
@@ -31,12 +31,7 @@ export const RoofRenderer = ({ node }: { node: RoofNode }) => {
visible={node.visible}
{...handlers}
>
<mesh
castShadow
material={material}
name="merged-roof"
receiveShadow
>
<mesh castShadow material={material} name="merged-roof" receiveShadow>
<boxGeometry args={[0, 0, 0]} />
</mesh>
<group name="segments-wrapper" visible={false}>
@@ -18,7 +18,14 @@ export const SlabRenderer = ({ node }: { node: SlabNode }) => {
}, [node.material, node.material?.preset, node.material?.properties, node.material?.texture])
return (
<mesh castShadow receiveShadow ref={ref} {...handlers} visible={node.visible} material={material}>
<mesh
castShadow
receiveShadow
ref={ref}
{...handlers}
visible={node.visible}
material={material}
>
<boxGeometry args={[0, 0, 0]} />
</mesh>
)
@@ -36,4 +36,5 @@ const useGLTFKTX2 = (path: string): ReturnType<typeof useGLTF> => {
loader.setMeshoptDecoder(MeshoptDecoder)
})
}
export { useGLTFKTX2 }
+4 -1
View File
@@ -34,7 +34,10 @@ export function createMaterial(material?: MaterialSchema): THREE.MeshStandardMat
return threeMaterial
}
export function createDefaultMaterial(color: string = '#ffffff', roughness: number = 0.9): THREE.MeshStandardMaterial {
export function createDefaultMaterial(
color: string = '#ffffff',
roughness: number = 0.9,
): THREE.MeshStandardMaterial {
return new THREE.MeshStandardMaterial({
color,
roughness,
@@ -103,7 +103,12 @@ function getMaterialsForWall(wallNode: WallNode): WallMaterials {
return result
}
function getWallHideState(wallNode: WallNode, wallMesh: Mesh, wallMode: string, cameraDir: Vector3): boolean {
function getWallHideState(
wallNode: WallNode,
wallMesh: Mesh,
wallMode: string,
cameraDir: Vector3,
): boolean {
let hideWall = wallNode.frontSide === 'interior' && wallNode.backSide === 'interior'
if (wallMode === 'up') {
@@ -167,7 +172,10 @@ export const WallCutout = () => {
} else {
const currentMaterial = (wallMesh as Mesh).material
const materials = wallMaterialCache.get(wallId)
if (!materials || currentMaterial !== (hideWall ? materials.invisible : materials.visible)) {
if (
!materials ||
currentMaterial !== (hideWall ? materials.invisible : materials.visible)
) {
const newMaterials = getMaterialsForWall(wallNode)
;(wallMesh as Mesh).material = hideWall ? newMaterials.invisible : newMaterials.visible
}
@@ -202,4 +210,4 @@ export const WallCutout = () => {
}
})
return null
}
}