three.jsreact-three-fiberreact-three-drei

Large number of polygons with react-three-fiber


I need to display thousands of polygons (2d) of different shapes with react-three-fiber.

I thought the best option would be to use instances of the same polygon with, let's say 100 vertices. And then to move the vertices of each instances in its vertex shader according to its shape.

I'm able to show triangles with react-three-drei Instances, but I cannot change the position of the vertices of each of those triangles.

Edit many polygons


Solution

  • While the approach you're describing (use instancing, and in the vertex shader use the instance ID or other data to procedurally rearrange vertices) is possible, it could be fairly complex depending on the topologies of the shapes.

    I think that THREE.BatchedMesh is probably a better option here. It allows you to draw any number of distinct geometries with different vertices in a single draw call, as long as they share a single material. For an example using BatchedMesh in R3F, see:

    https://codesandbox.io/p/sandbox/batchedmesh-3pf9d2

    The example uses a custom material, but if you only need per-instance color (and not per-instance values of other PBR properties) then the built-in MeshStandardMaterial would be fine.