Hello! I’m just starting my journey with three.js (actually react fiber).
I’m creating a test board that will be the basis of a map. I want to divide the board into individual fields. At the moment, a green box is displayed on a given field or not. Ultimately, some object, e.g. a tree, a stone, etc., will be located in each field of the board.
Unfortunately, now with 300,000 triangles, the number of fps drops drastically. What are the best practices for optimizing this type of task? It doesn’t matter whether I display the scene in close-up (only a few squares are visible) or the whole board, I have equally low fps.
I read that Frustum Culling in three.js does not work on instances, and when I tried manually to calculate whether a given rectangle is visible, the fps dropped even more.
Could I ask for tips and advice?
Here’s what my component code looks like:
import React, { useMemo } from 'react';
import { Instance, Instances } from '@react-three/drei';
export interface GridItem {
visible: boolean;
comment: string;
value: number;
}
interface GridProps {
grid: GridItem[][];
size?: number;
}
const Grid: React.FC<GridProps> = ({ grid, size = 200 }) => {
const gridHalfSize = size / 2;
const countX = grid.length;
const countY = grid[0].length;
// filter squares (visible == true)
const visibleSquares = useMemo(() => {
const squares = [];
for (let x = 0; x < countX; x++) {
for (let y = 0; y < countY; y++) {
if (grid[x][y].visible) {
squares.push([x - gridHalfSize, y - gridHalfSize]);
}
}
}
return squares;
}, [grid, gridHalfSize, countX, countY]);
return (
<Instances limit={visibleSquares.length} frustumCulled={false}>
<boxGeometry args={[1, 1, 0.01]} />
<meshBasicMaterial color="green" />
{visibleSquares.map(([x, y], index) => (
<Instance
key={index}
position={[x + 0.5, 0.01, y + 0.5]}
rotation={[Math.PI / 2, 0, 0]}
/>
))}
</Instances>
);
};
export default Grid;