Jump to content
  • Advertisement
Sign in to follow this  
Narf the Mouse

How is "Mesh Instancing" done?

This topic is 2645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!