Version: 2017.1 (switch to 2017.2b)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Graphics.DrawMeshInstanced

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function DrawMeshInstanced(mesh: Mesh, submeshIndex: int, material: Material, matrices: Matrix4x4[], count: int = matrices.Length, properties: MaterialPropertyBlock = null, castShadows: Rendering.ShadowCastingMode = ShadowCastingMode.On, receiveShadows: bool = true, layer: int = 0, camera: Camera = null): void;
public static void DrawMeshInstanced(Mesh mesh, int submeshIndex, Material material, Matrix4x4[] matrices, int count = matrices.Length, MaterialPropertyBlock properties = null, Rendering.ShadowCastingMode castShadows = ShadowCastingMode.On, bool receiveShadows = true, int layer = 0, Camera camera = null);
public static function DrawMeshInstanced(mesh: Mesh, submeshIndex: int, material: Material, matrices: List<Matrix4x4>, properties: MaterialPropertyBlock = null, castShadows: Rendering.ShadowCastingMode = ShadowCastingMode.On, receiveShadows: bool = true, layer: int = 0, camera: Camera = null): void;
public static void DrawMeshInstanced(Mesh mesh, int submeshIndex, Material material, List<Matrix4x4> matrices, MaterialPropertyBlock properties = null, Rendering.ShadowCastingMode castShadows = ShadowCastingMode.On, bool receiveShadows = true, int layer = 0, Camera camera = null);

Parameters

mesh The Mesh to draw.
submeshIndex Which subset of the mesh to draw. This applies only to meshes that are composed of several materials.
material Material to use.
matrices The array of object transformation matrices.
count The number of instances to be drawn.
properties Additional material properties to apply. See MaterialPropertyBlock.
castShadows Should the mesh cast shadows?
receiveShadows Should the mesh receive shadows?
layer Layer to use.
camera If null (default), the mesh will be drawn in all cameras. Otherwise it will be drawn in the given camera only.

Description

Draw the same mesh multiple times using GPU instancing.

Similar to Graphics.DrawMesh, this function draws meshes for one frame without the overhead of creating unnecessary game objects.

Use this function in situations where you want to draw the same mesh for a particular amount of times using an instanced shader. Meshes are not further culled by the view frustum or baked occluders, nor sorted for transparency or z 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 SetFloatArray, SetVectorArray and SetMatrixArray.

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

InvalidOperationException will be thrown if the material doesn't have Material.enableInstancing set to true, or the current platform doesn't support this API (i.e. if GPU instancing is not available). See SystemInfo.supportsInstancing.

See Also: DrawMesh.