@threlte/extras

<InstancedMeshes>

The component <InstancedMeshes> takes existing THREE.Mesh instances and creates a THREE.InstancedMesh per THREE.Mesh. This is especially useful if you want to instantiate a lot of meshes that have been loaded with hooks like useGltf.

It takes the same arguments as <InstancedMesh>.

Usage

Passing a map

Load a gltf file with the useGltf hook and pass the result to the <InstancedMeshes> component. The slot prop components can be used to instantiate a mesh multiple times.

<script lang="ts">
  import { useGltf, InstancedMeshes } from '@threlte/extras'

  // Let's say the file contains a mesh named "Cube".
  // The hook `useGltf`  will automatically provide a map with
  // all nodes of the gltf file at the key `nodes`. When
  // passing that map to the `<InstancedMeshes>` component, it will
  // automatically filter out all nodes that are not
  // `THREE.Mesh` instances.
  const gltf = useGltf('path/to/file.gltf')
</script>

{#if gltf}
  <!--
    You can use object destructuring
    to access the component <Cube>
  -->
  <InstancedMeshes meshes={$gltf.nodes}>
    {#snippet children({ components: { Cube } })}
      <Cube position.y={2} position.x={-1}>
    {/snippet}
  </InstancedMeshes>
{/if}

When using <InstancedMeshes> with a large gltf file, be aware that <InstancedMeshes> will create a new <InstancedMesh> for each <Mesh> in the gltf file. This can lead to a lot of <InstancedMesh> components, which can have a negative impact on performance. You might want to filter out the meshes you want to instantiate beforehand.

Passing an array

If you don’t want to use the useGltf hook, you can also pass an array of THREE.Mesh instances to the <InstancedMeshes> component.

<script lang="ts">
  import { InstancedMeshes } from '@threlte/extras'
  import { Mesh, BoxGeometry, MeshStandardMaterial } from 'three'

  const meshes = [
    new Mesh(new BoxGeometry(), new MeshStandardMaterial()), // MeshA
    new Mesh(new SphereGeometry(), new MeshStandardMaterial()), // MeshB
    new Mesh(new PlaneGeometry(), new MeshStandardMaterial())  // MeshC
  ]
</script>

<!--
    You can use array destructuring
    to access the components <MeshA>,
    <MeshB> and <MeshC>
  -->
<InstancedMeshes meshes={meshes}>
  {#snippet children({ components: [MeshA, MeshB, MeshC] })}
    <MeshA position.y={2} position.x={-1}>
    <MeshB position.y={-2}>
    <MeshC position.y={0} position.x={1}>
  {/snippet}
</InstancedMeshes>

Component Signature

<InstancedMeshes> extends <T.InstancedMesh> and supports all its props, slot props, bindings and events.

Props

name
type
required
default
description

meshes
THREE.Mesh[] | Record<string, THREE.Mesh>
yes

limit
number
no
1000
Limits the amount of possible <Instance> components.

range
number
no
1000
Limits the amount of drawn <Instance> components.

update
boolean
no
true
Whether the THREE.InstancedMesh should be updated.