Add Floor2DInstance component and integrate into FloorInstances for 2D floor rendering

This commit is contained in:
2025-06-27 16:10:53 +05:30
parent 540bb44e0c
commit bfa4d7bbc6
2 changed files with 60 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import { useMemo } from 'react';
import { DoubleSide, Shape, Vector2 } from 'three';
import { Extrude } from '@react-three/drei';
import * as Constants from '../../../../../types/world/worldConstants';
function Floor2DInstance({ floor }: { floor: Floor }) {
const savedTheme: string | null = localStorage.getItem("theme");
const shape = useMemo(() => {
const shape = new Shape();
const points = floor.points.map(p => new Vector2(p.position[0], p.position[2]));
if (points.length < 3) return null;
shape.moveTo(points[0].x, points[0].y);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i].x, points[i].y);
}
return shape;
}, [floor]);
if (!shape) return null;
return (
<mesh
castShadow
receiveShadow
name={`Floor-2D-${floor.floorUuid}`}
rotation={[Math.PI / 2, 0, 0]}
position={[0, 0, 0]}
userData={floor}
>
<Extrude
name={`Floor-${floor.floorUuid}`}
args={[shape, {
depth: Constants.floorConfig.height,
bevelEnabled: floor.isBeveled,
}]}
userData={floor}
>
<meshBasicMaterial
color={savedTheme === "dark" ? "#d2baff" : "#6f42c1"}
side={DoubleSide}
transparent
opacity={0.4}
depthWrite={false}
/>
</Extrude>
</mesh>
);
}
export default Floor2DInstance;

View File

@@ -6,6 +6,7 @@ import { useToggleView } from '../../../../store/builder/store';
import Line from '../../line/line';
import Point from '../../point/point';
import FloorInstance from './Instance/floorInstance';
import Floor2DInstance from './Instance/floor2DInstance';
function FloorInstances() {
const { floorStore } = useSceneContext();
@@ -69,6 +70,14 @@ function FloorInstances() {
</mesh>
)}
{toggleView && floors.length > 0 && (
<mesh name='Floors-2D-Group'>
{floors.map((floor) => (
<Floor2DInstance key={floor.floorUuid} floor={floor} />
))}
</mesh>
)}
{toggleView && (
<>
<group name='Floor-Points-Group'>