Files
Dwinzo_Demo/app/src/modules/builder/wall/Instances/instance/wall.tsx

131 lines
5.4 KiB
TypeScript
Raw Normal View History

2025-06-10 15:28:23 +05:30
import * as THREE from 'three';
import { useMemo, useRef, useState } from 'react';
import * as Constants from '../../../../../types/world/worldConstants';
2025-06-23 09:37:53 +05:30
import defaultMaterial from '../../../../../assets/textures/floor/wall-tex.png';
import material1 from '../../../../../assets/textures/floor/factory wall texture.jpg';
2025-06-10 15:28:23 +05:30
import { useWallStore } from '../../../../../store/builder/useWallStore';
import { useWallClassification } from './helpers/useWallClassification';
import { useFrame, useThree } from '@react-three/fiber';
import { useWallVisibility } from '../../../../../store/builder/store';
import { Decal } from '@react-three/drei';
import { Base } from '@react-three/csg';
2025-06-10 15:28:23 +05:30
function Wall({ wall }: { readonly wall: Wall }) {
const { walls } = useWallStore();
2025-06-23 09:37:53 +05:30
const { getWallType, isWallFlipped } = useWallClassification(walls);
2025-06-10 15:28:23 +05:30
const wallType = getWallType(wall);
const [visible, setVisible] = useState(true);
const { wallVisibility } = useWallVisibility();
const meshRef = useRef<any>();
const { camera } = useThree();
2025-06-23 09:37:53 +05:30
const wallFlipped = isWallFlipped(wall);
const [rawStart, rawEnd] = wall.points;
const [startPoint, endPoint] = wallFlipped ? [rawStart, rawEnd] : [rawEnd, rawStart];
2025-06-10 15:28:23 +05:30
const startX = startPoint.position[0];
const startZ = startPoint.position[2];
const endX = endPoint.position[0];
const endZ = endPoint.position[2];
const wallLength = Math.sqrt((endX - startX) ** 2 + (endZ - startZ) ** 2);
const angle = Math.atan2(endZ - startZ, endX - startX);
const centerX = (startX + endX) / 2;
const centerZ = (startZ + endZ) / 2;
const centerY = wall.wallHeight / 2;
const textureLoader = new THREE.TextureLoader();
2025-06-23 09:37:53 +05:30
const [defaultWallTexture, material1WallTexture] = useMemo(() => {
const inside = textureLoader.load(defaultMaterial);
2025-06-10 15:28:23 +05:30
inside.wrapS = inside.wrapT = THREE.RepeatWrapping;
inside.repeat.set(wallLength / 10, wall.wallHeight / 10);
inside.colorSpace = THREE.SRGBColorSpace;
2025-06-23 09:37:53 +05:30
const outside = textureLoader.load(material1);
2025-06-10 15:28:23 +05:30
outside.wrapS = outside.wrapT = THREE.RepeatWrapping;
outside.repeat.set(wallLength / 10, wall.wallHeight / 10);
outside.colorSpace = THREE.SRGBColorSpace;
return [inside, outside];
}, [wallLength, wall.wallHeight]);
const materials = useMemo(() => {
return [
2025-06-23 09:37:53 +05:30
new THREE.MeshStandardMaterial({ color: Constants.wallConfig.defaultColor, side: THREE.DoubleSide, visible: visible }), // Left
new THREE.MeshStandardMaterial({ color: Constants.wallConfig.defaultColor, side: THREE.DoubleSide, visible: visible }), // Right
new THREE.MeshStandardMaterial({ color: Constants.wallConfig.defaultColor, side: THREE.DoubleSide, visible: visible }), // Top
new THREE.MeshStandardMaterial({ color: Constants.wallConfig.defaultColor, side: THREE.DoubleSide, visible: visible }), // Bottom
2025-06-10 15:28:23 +05:30
new THREE.MeshStandardMaterial({
color: Constants.wallConfig.defaultColor,
side: THREE.DoubleSide,
2025-06-23 09:37:53 +05:30
map: wall.insideMaterial === 'Default Material' ? defaultWallTexture : material1WallTexture,
2025-06-10 15:28:23 +05:30
}),
new THREE.MeshStandardMaterial({
color: Constants.wallConfig.defaultColor,
side: THREE.DoubleSide,
2025-06-23 09:37:53 +05:30
map: wall.outsideMaterial === 'Default Material`' ? defaultWallTexture : material1WallTexture,
2025-06-10 15:28:23 +05:30
}),
];
2025-06-23 09:37:53 +05:30
}, [defaultWallTexture, material1WallTexture, wall]);
2025-06-10 15:28:23 +05:30
2025-06-23 09:37:53 +05:30
const geometry = useMemo(() => new THREE.BoxGeometry(wallLength, wall.wallHeight, wall.wallThickness), [wallLength, wall.wallHeight, wall.wallThickness]);
2025-06-10 15:28:23 +05:30
useFrame(() => {
if (!meshRef.current) return;
const v = new THREE.Vector3();
const u = new THREE.Vector3();
if (!wallVisibility && wallType.type === 'room') {
meshRef.current.getWorldDirection(v);
camera.getWorldDirection(u);
2025-06-23 09:37:53 +05:30
if (!u || !v) return;
2025-06-10 15:28:23 +05:30
setVisible((2 * v.dot(u)) <= 0.1);
} else {
setVisible(true);
}
})
return (
2025-06-23 09:37:53 +05:30
<mesh
2025-06-10 15:28:23 +05:30
name={`Wall-${wall.wallUuid}`}
2025-06-23 09:37:53 +05:30
key={wall.wallUuid}
2025-06-10 15:28:23 +05:30
userData={wall}
>
2025-06-23 09:37:53 +05:30
<Base
ref={meshRef}
geometry={geometry}
position={[centerX, centerY, centerZ]}
rotation={[0, -angle, 0]}
>
2025-06-10 15:28:23 +05:30
{materials.map((material, index) => (
2025-06-23 09:37:53 +05:30
<primitive key={index} visible={visible} object={material} attach={`material-${index}`} />
2025-06-10 15:28:23 +05:30
))}
{wall.decals.map((decal) => {
return (
<Decal
// debug
position={[decal.decalPosition[0], decal.decalPosition[1], wall.wallThickness / 2]}
rotation={[0, 0, decal.decalRotation]}
scale={[decal.decalScale, decal.decalScale, 0.001]}
>
<meshBasicMaterial
2025-06-23 09:37:53 +05:30
map={material1WallTexture}
2025-06-10 15:28:23 +05:30
side={THREE.DoubleSide}
polygonOffset
polygonOffsetFactor={-1}
/>
</Decal>
)
})}
</Base>
2025-06-23 09:37:53 +05:30
</mesh>
2025-06-10 15:28:23 +05:30
);
}
export default Wall;