static void DrawModelInstanced( Model model, System.Span<Transform> transforms, RenderAttributes attributes )
static void DrawModelInstanced( Model model, int count, RenderAttributes attributes )

book_4_sparkGenerated
code_blocksInput

Description

The DrawModelInstanced method in the Sandbox.Graphics class is used to render multiple instances of a 3D model efficiently. This method leverages instancing, a technique that allows the rendering of many objects with a single draw call, which can significantly improve performance when rendering a large number of identical objects.

Usage

To use the DrawModelInstanced method, you need to provide the following parameters:

  • model: The Sandbox.Model object that represents the 3D model you want to render.
  • transforms: A System.Span<Transform> that contains the transformation matrices for each instance of the model. Each Transform defines the position, rotation, and scale of an instance.
  • attributes: A Sandbox.RenderAttributes object that specifies additional rendering attributes, such as material properties or shader parameters.

Ensure that the transforms span is properly populated with the desired transformations for each instance before calling this method.

Example

// Example usage of DrawModelInstanced

// Assume 'model' is a valid Sandbox.Model object
// Assume 'attributes' is a valid Sandbox.RenderAttributes object

// Create an array of transforms for each instance
Transform[] instanceTransforms = new Transform[100];
for (int i = 0; i < instanceTransforms.Length; i++)
{
    instanceTransforms[i] = Transform.Identity;
    instanceTransforms[i].Position = new Vector3(i * 2.0f, 0, 0); // Position each instance along the x-axis
}

// Convert the array to a Span
Span<Transform> transformsSpan = new Span<Transform>(instanceTransforms);

// Draw the model instances
Graphics.DrawModelInstanced(model, transformsSpan, attributes);