Description
The DrawModelInstanced
method is a static method of the Sandbox.Graphics
class. It is used to render multiple instances of a 3D model efficiently by utilizing instancing. This method is particularly useful when you need to draw a large number of the same model with different transformations, as it reduces the overhead of issuing multiple draw calls.
Usage
To use the DrawModelInstanced
method, you need to provide the following parameters:
model
: An instance of Sandbox.Model
representing the 3D model you want to render.
transforms
: A System.Span<Transform>
containing the transformation matrices for each instance of the model. Each Transform
defines the position, rotation, and scale of the model instance.
attributes
: An instance of Sandbox.RenderAttributes
that specifies additional rendering attributes, such as material properties or shader parameters.
Ensure that the transforms
span contains the correct number of transformations corresponding to the number of instances you wish to render.
Example
// Example usage of DrawModelInstanced
// Assume 'model' is a preloaded Sandbox.Model
// Assume 'attributes' is a configured Sandbox.RenderAttributes
// Create an array of transforms for each instance
Transform[] instanceTransforms = new Transform[10];
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);