How is "Mesh Instancing" done?

Started by
4 comments, last by Narf the Mouse 12 years, 6 months ago
One thing I have run into, on the internet, in programming and in games, is "Mesh Instancing", where, AFAIK, a single copy of a mesh is kept, then drawn repeatedly in different positions and orientations.

However, I'm a bit fuzzy on the details.

It can't be as simple as calling "Mesh.Draw();" say, a thousand times, sending a different position vector and matrix4x4 orientation each time, can it? That strikes me as rather slow.

Unless the "secret" is doing it all in one call to "Effect.Pass()"?

In short, what is this thing called "Mesh Instancing" and how does it work?

Thanks.
Advertisement
http://en.wikipedia....etry_instancing
http://msdn.microsof...349(VS.85).aspx
http://www.opengl.or...w_instanced.txt

http://en.wikipedia....etry_instancing
http://msdn.microsof...349(VS.85).aspx
http://www.opengl.or...w_instanced.txt

Thanks.

Need more sleep.
Now the next question - How does this work with shaders? Specifically, would I/how do I adapt my Lighting.fx code to handle this? In general terms, how would I modify this?


struct VertexShaderInput
{
float4 Position : POSITION0;
float4 UV : TEXCOORD0;
float3 Normal : NORMAL0;
};

struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 WorldPosition : TexCOORD0;
float3 ViewVector : TexCOORD3;
float2 UV : TEXCOORD1;
float3 Normal : TEXCOORD2;
};

VertexShaderOutput VertexShaderA( VertexShaderInput input )


and this?


float4 PixelShaderA( VertexShaderOutput input ) : COLOR0


Thanks.
For hardware instancing the vertex data will be read from two different buffers. One containing the model data, the other containing the per instance data.
Per instance data could be a worldmatrix or color for example. The number of elements needed in the instance data buffer is the number of instances you want to draw.


struct VertexShaderInput
{
float4 Position : POSITION0;
float4 UV : TEXCOORD0;
float3 Normal : NORMAL0;

// these come from the instance data stream
float3 instance_offset : TEXCOORD1;
float3 instance_scale : TEXCOORD2;
};

VertexShaderOutput VertexShaderA( VertexShaderInput input )
{
input.Position *= input.instance_scale;
input.Position += input.instance_offset;
...
}


Then in your program code you will use the API specific functions to feed the instance data from a second buffer. (In Dx9 SetStreamSource and SetStreamSourceFreq).
Check out the instancing example in the SDK if you use DirectX.

For hardware instancing the vertex data will be read from two different buffers. One containing the model data, the other containing the per instance data.
Per instance data could be a worldmatrix or color for example. The number of elements needed in the instance data buffer is the number of instances you want to draw.


struct VertexShaderInput
{
float4 Position : POSITION0;
float4 UV : TEXCOORD0;
float3 Normal : NORMAL0;

// these come from the instance data stream
float3 instance_offset : TEXCOORD1;
float3 instance_scale : TEXCOORD2;
};

VertexShaderOutput VertexShaderA( VertexShaderInput input )
{
input.Position *= input.instance_scale;
input.Position += input.instance_offset;
...
}


Then in your program code you will use the API specific functions to feed the instance data from a second buffer. (In Dx9 SetStreamSource and SetStreamSourceFreq).
Check out the instancing example in the SDK if you use DirectX.

Ok, thanks; now I think I got the shader part, too. :)

This topic is closed to new replies.

Advertisement