Skip to content

Commit

Permalink
reverts forwardRef for BrickCursor
Browse files Browse the repository at this point in the history
Scene and its children were being rendered every mouse move, causing render lag in some slower hardwares
  • Loading branch information
Igorxp5 committed May 25, 2024
1 parent 3434fe6 commit e67274b
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 95 deletions.
58 changes: 34 additions & 24 deletions src/components/3D/BrickCursor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,42 @@
/* eslint-disable no-unused-vars */
/* eslint-disable react/prop-types */
/* eslint-disable react/display-name */
import React, { useMemo } from "react";
import React, { forwardRef, useMemo } from "react";
import { getMeasurementsFromDimensions, createGeometry } from "../../utils";
import { Vector3 } from "three";

export const BrickCursor = ({position, dimensions, visible}) => {
const { height, width, depth } = getMeasurementsFromDimensions(dimensions);
export const BrickCursor = forwardRef(
(
{
position = new Vector3(),
dimensions = { x: 1, z: 1 },
visible = true
},
ref
) => {
const { height, width, depth } = getMeasurementsFromDimensions(dimensions);

const brickGeometry = useMemo(() => {
return createGeometry({ width, height, depth, dimensions, knobDim: 0});
}, [width, height, depth, dimensions]);
const brickGeometry = useMemo(() => {
return createGeometry({ width, height, depth, dimensions, knobDim: 0});
}, [width, height, depth, dimensions]);

return (
<>
<group
position={[position.x, position.y, position.z]}
visible={visible}
>
<mesh
geometry={brickGeometry}
return (
<>
<group
ref={ref}
position={[position.x, position.y, position.z]}
visible={visible}
>
<meshBasicMaterial
color={"white"}
transparent={true}
opacity={0.3}
/>
</mesh>
</group>
</>
);
};
<mesh
geometry={brickGeometry}
>
<meshBasicMaterial
color={"white"}
transparent={true}
opacity={0.3}
/>
</mesh>
</group>
</>
);
});
103 changes: 57 additions & 46 deletions src/components/3D/MultiBrickCursor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,65 @@
/* eslint-disable no-unused-vars */
/* eslint-disable react/prop-types */
/* eslint-disable react/display-name */
import React, { useMemo } from "react";
import React, { forwardRef, useMemo } from "react";
import { Vector3 } from "three";
import { getMeasurementsFromDimensions, createGeometry } from "../../utils";

export const MultiBrickCursor = ({position, anchor, bricks, rotate = false, visible = true}) => {
const meshes = useMemo(() => {
return bricks.map((brick) => {
const dimensions = {
x: !rotate ? brick.dimensions.x : brick.dimensions.z,
z: !rotate ? brick.dimensions.z : brick.dimensions.x
}
const { height, width, depth } = getMeasurementsFromDimensions(dimensions);
const brickGeometry = createGeometry({ width, height, depth, dimensions: dimensions, knobDim: 0});
export const MultiBrickCursor = forwardRef(
(
{
position = new Vector3(),
anchor = new Vector3(),
bricks = [],
rotate = false,
visible = true
},
ref
) => {
const meshes = useMemo(() => {
return bricks.map((brick) => {
const dimensions = {
x: !rotate ? brick.dimensions.x : brick.dimensions.z,
z: !rotate ? brick.dimensions.z : brick.dimensions.x
}
const { height, width, depth } = getMeasurementsFromDimensions(dimensions);
const brickGeometry = createGeometry({ width, height, depth, dimensions: dimensions, knobDim: 0});

return {
uID: brick.uID,
relPosition: new Vector3()
.copy(brick.position)
.sub(anchor)
.applyAxisAngle(new Vector3(0, 1, 0), rotate ? Math.PI / 2 : 0)
.toArray(),
geometry: brickGeometry
}
})
}, [anchor, bricks, rotate]);
return {
uID: brick.uID,
relPosition: new Vector3()
.copy(brick.position)
.sub(anchor)
.applyAxisAngle(new Vector3(0, 1, 0), rotate ? Math.PI / 2 : 0)
.toArray(),
geometry: brickGeometry
}
})
}, [anchor, bricks, rotate]);

return (
<>
<group
position={[position.x, position.y, position.z]}
visible={visible}
>
{meshes.map((mesh) => {
return (
<mesh
key={mesh.uID}
position={mesh.relPosition}
geometry={mesh.geometry}
>
<meshBasicMaterial
color={"white"}
transparent={true}
opacity={0.3}
/>
</mesh>
);
})}
</group>
</>
);
};
return (
<>
<group
ref={ref}
position={[position.x, position.y, position.z]}
visible={visible}
>
{meshes.map((mesh) => {
return (
<mesh
key={mesh.uID}
position={mesh.relPosition}
geometry={mesh.geometry}
>
<meshBasicMaterial
color={"white"}
transparent={true}
opacity={0.3}
/>
</mesh>
);
})}
</group>
</>
);
});
63 changes: 38 additions & 25 deletions src/components/3D/Scene.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export const Scene = () => {

const bricksBoundBox = useRef([]);

const brickCursorRef = useRef();
const multiBrickCursorRef = useRef();

const mode = useStore((state) => state.mode);

const isEditMode = mode === EDIT_MODE;
Expand Down Expand Up @@ -64,22 +67,19 @@ export const Scene = () => {
const anchorZ = useStore((state) => state.anchorZ);
const color = useStore((state) => state.color);

const [rawMousePosition, setRawMousePosition] = useState(new Vector3());
const [mouseIntersect, setMouseIntersect] = useState(new Vector3());

const room = useStore((state) => state.liveblocks.room);
const self = useStore((state) => state.self);

useAnchorShorcuts();

const addBrick = () => {
const measures = getMeasurementsFromDimensions({x: width, z: depth});
const boundingBoxOfBrick = getBoundBoxFromMeasures(mouseIntersect, measures);
const boundingBoxOfBrick = getBoundBoxFromMeasures(brickCursorRef.current.position, measures);
const bricksBoundingBox = bricksBoundBox.current.map((bound) => bound.brickBoundingBox);

if (!doBoundBoxCollideWithBoundBoxSet(boundingBoxOfBrick, bricksBoundingBox)) {
const brickData = {
position: mouseIntersect,
position: new Vector3().copy(brickCursorRef.current.position),
uID: uID(),
dimensions: { x: width, z: depth },
color: color,
Expand All @@ -105,7 +105,7 @@ export const Scene = () => {
.add(brick.position)
.sub(selectedBricksAnchor)
.applyAxisAngle(new Vector3(0, 1, 0), rotate ? Math.PI / 2 : 0)
.add(mouseIntersect);
.add(multiBrickCursorRef.current.position);
const measures = getMeasurementsFromDimensions(dimensions);
const boundingBoxOfBrick = getBoundBoxFromMeasures(position, measures);
if (doBoundBoxCollideWithBoundBoxSet(boundingBoxOfBrick, bricksBoundingBox)) {
Expand All @@ -131,39 +131,52 @@ export const Scene = () => {
const setBrickCursorPosition = (e) => {
e.stopPropagation();

const mousePosition = new Vector3()
const mousePosition = normalizePositionToSceneGrid(
new Vector3()
.copy(e.point)
.add(e.face.normal)
.setY(Math.abs(e.point.y));
.setY(Math.abs(e.point.y))
);

setRawMousePosition(mousePosition);
const translatedXZMousePosition = new Vector3()
.copy(mousePosition)
.add(new Vector3(anchorX * GRID_UNIT.x, 0, anchorZ * GRID_UNIT.z));

updateMouseIntersectWithTranslation();
if (isCreateMode && brickCursorRef.current) {
brickCursorRef.current.userData.mousePosition = mousePosition;
brickCursorRef.current.position.copy(translatedXZMousePosition);
}

if (isEditMode && multiBrickCursorRef.current) {
multiBrickCursorRef.current.userData.mousePosition = mousePosition;
multiBrickCursorRef.current.position.copy(translatedXZMousePosition);
}

room.broadcastEvent({
type: self.id,
data: {
x: mouseIntersect.x,
y: mouseIntersect.y,
z: mouseIntersect.z,
x: translatedXZMousePosition.x,
y: translatedXZMousePosition.y,
z: translatedXZMousePosition.z,
w: width,
d: depth,
},
});

};

const updateMouseIntersectWithTranslation = () => {
const normalizedMousePosition = normalizePositionToSceneGrid(rawMousePosition);
normalizedMousePosition.add(
new Vector3(anchorX * GRID_UNIT.x, 0, anchorZ * GRID_UNIT.z)
);

setMouseIntersect(normalizedMousePosition);
}

useEffect(() => {
updateMouseIntersectWithTranslation();
if (isCreateMode && brickCursorRef.current && brickCursorRef.current.userData.mousePosition) {
brickCursorRef.current.position
.copy(brickCursorRef.current.userData.mousePosition)
.add(new Vector3(anchorX * GRID_UNIT.x, 0, anchorZ * GRID_UNIT.z));
}

if (isEditMode && multiBrickCursorRef.current && multiBrickCursorRef.current.userData.mousePosition) {
multiBrickCursorRef.current.position
.copy(multiBrickCursorRef.current.userData.mousePosition)
.add(new Vector3(anchorX * GRID_UNIT.x, 0, anchorZ * GRID_UNIT.z));
}
}, [anchorX, anchorZ]);

useEffect(() => {
Expand Down Expand Up @@ -233,13 +246,13 @@ export const Scene = () => {
workspaceSize={MIN_WORKSPACE_SIZE}
/>
<BrickCursor
position={mouseIntersect}
ref={brickCursorRef}
visible={isCreateMode}
dimensions={{ x: width, z: depth }}
/>
<MultiBrickCursor
ref={multiBrickCursorRef}
anchor={selectedBricksAnchor}
position={mouseIntersect}
rotate={rotate}
bricks={selectedBricks}
/>
Expand Down

0 comments on commit e67274b

Please sign in to comment.