# DrawMeshInstanced

> Draws the same mesh multiple times using GPU instancing.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.7/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## DrawMeshInstanced(Mesh, int, Material, Matrix4x4\[], int, MaterialPropertyBlock, ShadowCastingMode, bool, int, Camera, LightProbeUsage)

Draws the same mesh multiple times using GPU instancing.

```csharp
public static void DrawMeshInstanced(Mesh mesh, int submeshIndex, Material material, Matrix4x4[] matrices, int count, MaterialPropertyBlock properties, ShadowCastingMode castShadows, bool receiveShadows, int layer, Camera camera, LightProbeUsage lightProbeUsage)
```

### Parameters

**** (\[Mesh]\(/engine/6000.7/script-reference/unityengine/mesh)): The [Mesh](/engine/6000.7/script-reference/unityengine/mesh.md) to draw.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Which subset of the mesh to draw. This applies only to meshes that are composed of several materials.**** (\[Material]\(/engine/6000.7/script-reference/unityengine/material)): [Material](/engine/6000.7/script-reference/unityengine/material.md) to use.**** (\[Matrix4x4\[]]\(/engine/6000.7/script-reference/unityengine/matrix4x4)): The array of object transformation matrices.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The number of instances to be drawn.**** (\[MaterialPropertyBlock]\(/engine/6000.7/script-reference/unityengine/materialpropertyblock)): Additional material properties to apply. See [MaterialPropertyBlock](/engine/6000.7/script-reference/unityengine/materialpropertyblock.md).**** (\[ShadowCastingMode]\(/engine/6000.7/script-reference/unityengine/rendering/shadowcastingmode)): Determines whether the Meshes should cast shadows.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Determines whether the Meshes should receive shadows.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): [Layer](/engine/6000.7/manual/working-with-gameobjects/layers.md) to use.**** (\[Camera]\(/engine/6000.7/script-reference/unityengine/camera)): If `null` (default), the mesh will be drawn in all cameras. Otherwise it will be drawn in the given Camera only.**** (\[LightProbeUsage]\(/engine/6000.7/script-reference/unityengine/rendering/lightprobeusage)): [LightProbeUsage](/engine/6000.7/script-reference/unityengine/rendering/lightprobeusage.md) for the instances.

### Remarks

Similar to [Graphics.DrawMesh](/engine/6000.7/script-reference/unityengine/graphics/drawmesh.md), this function draws meshes for one frame without the overhead of creating unnecessary game objects. This function is now obsolete. Use [Graphics.RenderMeshInstanced](/engine/6000.7/script-reference/unityengine/graphics/rendermeshinstanced.md) instead. Use this function in situations where you want to draw the same mesh for a particular amount of times using an instanced shader. Unity culls and sorts instanced Meshes as a group. It creates an axis-aligned bounding box that contains all the Meshes, calculates the center point, then uses this information to cull and sort the Mesh instances. Note that after culling and sorting the combined instances, Unity does not further cull individual instances by the view frustum or baked occluders. It also does not sort individual instances for transparency or depth efficiency.

The transformation matrix of each instance of the mesh should be packed into the `matrices` array. You can specify the number of instances to draw, or by default it is the length of the `matrices` array. Other per-instance data, if required by the shader, should be provided by creating arrays on the MaterialPropertyBlock argument using [MaterialPropertyBlock.SetFloatArray](/engine/6000.7/script-reference/unityengine/materialpropertyblock/setfloatarray.md), [MaterialPropertyBlock.SetVectorArray](/engine/6000.7/script-reference/unityengine/materialpropertyblock/setvectorarray.md) and [MaterialPropertyBlock.SetMatrixArray](/engine/6000.7/script-reference/unityengine/materialpropertyblock/setmatrixarray.md).

To render the instances with light probes, provide the light probe data via the MaterialPropertyBlock and specify `lightProbeUsage` with [LightProbeUsage.CustomProvided](/engine/6000.7/script-reference/unityengine/rendering/lightprobeusage/customprovided.md). Check [LightProbes.CalculateInterpolatedLightAndOcclusionProbes](/engine/6000.7/script-reference/unityengine/lightprobes/calculateinterpolatedlightandocclusionprobes.md) for the details.

Note: You can only draw a maximum of 1023 instances at once.

InvalidOperationException will be thrown if the material doesn't have [Material.enableInstancing](/engine/6000.7/script-reference/unityengine/material/enableinstancing.md) set to true, or the current platform doesn't support this API (i.e. if GPU instancing is not available). See [SystemInfo.supportsInstancing](/engine/6000.7/script-reference/unityengine/systeminfo/supportsinstancing.md).

Additional Resources: [Graphics.DrawMesh](/engine/6000.7/script-reference/unityengine/graphics/drawmesh.md), \[[Graphics.RenderMeshInstanced](/engine/6000.7/script-reference/unityengine/graphics/rendermeshinstanced.md).

## DrawMeshInstanced(Mesh, int, Material, List\<Matrix4x4>, MaterialPropertyBlock, ShadowCastingMode, bool, int, Camera, LightProbeUsage)

Draws the same mesh multiple times using GPU instancing.

```csharp
public static void DrawMeshInstanced(Mesh mesh, int submeshIndex, Material material, List<Matrix4x4> matrices, MaterialPropertyBlock properties, ShadowCastingMode castShadows, bool receiveShadows, int layer, Camera camera, LightProbeUsage lightProbeUsage)
```

### Parameters

**** (\[Mesh]\(/engine/6000.7/script-reference/unityengine/mesh)): The [Mesh](/engine/6000.7/script-reference/unityengine/mesh.md) to draw.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): Which subset of the mesh to draw. This applies only to meshes that are composed of several materials.**** (\[Material]\(/engine/6000.7/script-reference/unityengine/material)): [Material](/engine/6000.7/script-reference/unityengine/material.md) to use.**** (\[List\<Matrix4x4>]\(https\://learn.microsoft.com/dotnet/api/system.collections.generic.list-1)): The array of object transformation matrices.**** (\[MaterialPropertyBlock]\(/engine/6000.7/script-reference/unityengine/materialpropertyblock)): Additional material properties to apply. See [MaterialPropertyBlock](/engine/6000.7/script-reference/unityengine/materialpropertyblock.md).**** (\[ShadowCastingMode]\(/engine/6000.7/script-reference/unityengine/rendering/shadowcastingmode)): Determines whether the Meshes should cast shadows.**** (\[bool]\(https\://learn.microsoft.com/dotnet/api/system.boolean)): Determines whether the Meshes should receive shadows.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): [Layer](/engine/6000.7/manual/working-with-gameobjects/layers.md) to use.**** (\[Camera]\(/engine/6000.7/script-reference/unityengine/camera)): If `null` (default), the mesh will be drawn in all cameras. Otherwise it will be drawn in the given Camera only.**** (\[LightProbeUsage]\(/engine/6000.7/script-reference/unityengine/rendering/lightprobeusage)): [LightProbeUsage](/engine/6000.7/script-reference/unityengine/rendering/lightprobeusage.md) for the instances.

### Remarks

Similar to [Graphics.DrawMesh](/engine/6000.7/script-reference/unityengine/graphics/drawmesh.md), this function draws meshes for one frame without the overhead of creating unnecessary game objects. This function is now obsolete. Use [Graphics.RenderMeshInstanced](/engine/6000.7/script-reference/unityengine/graphics/rendermeshinstanced.md) instead. Use this function in situations where you want to draw the same mesh for a particular amount of times using an instanced shader. Unity culls and sorts instanced Meshes as a group. It creates an axis-aligned bounding box that contains all the Meshes, calculates the center point, then uses this information to cull and sort the Mesh instances. Note that after culling and sorting the combined instances, Unity does not further cull individual instances by the view frustum or baked occluders. It also does not sort individual instances for transparency or depth efficiency.

The transformation matrix of each instance of the mesh should be packed into the `matrices` array. You can specify the number of instances to draw, or by default it is the length of the `matrices` array. Other per-instance data, if required by the shader, should be provided by creating arrays on the MaterialPropertyBlock argument using [MaterialPropertyBlock.SetFloatArray](/engine/6000.7/script-reference/unityengine/materialpropertyblock/setfloatarray.md), [MaterialPropertyBlock.SetVectorArray](/engine/6000.7/script-reference/unityengine/materialpropertyblock/setvectorarray.md) and [MaterialPropertyBlock.SetMatrixArray](/engine/6000.7/script-reference/unityengine/materialpropertyblock/setmatrixarray.md).

To render the instances with light probes, provide the light probe data via the MaterialPropertyBlock and specify `lightProbeUsage` with [LightProbeUsage.CustomProvided](/engine/6000.7/script-reference/unityengine/rendering/lightprobeusage/customprovided.md). Check [LightProbes.CalculateInterpolatedLightAndOcclusionProbes](/engine/6000.7/script-reference/unityengine/lightprobes/calculateinterpolatedlightandocclusionprobes.md) for the details.

Note: You can only draw a maximum of 1023 instances at once.

InvalidOperationException will be thrown if the material doesn't have [Material.enableInstancing](/engine/6000.7/script-reference/unityengine/material/enableinstancing.md) set to true, or the current platform doesn't support this API (i.e. if GPU instancing is not available). See [SystemInfo.supportsInstancing](/engine/6000.7/script-reference/unityengine/systeminfo/supportsinstancing.md).

Additional Resources: [Graphics.DrawMesh](/engine/6000.7/script-reference/unityengine/graphics/drawmesh.md), \[[Graphics.RenderMeshInstanced](/engine/6000.7/script-reference/unityengine/graphics/rendermeshinstanced.md).
