CUBE的几何实例化DEMO
鼠标右键按下并拖动 旋转视角
WSAD 前后左右
RF 上下
QE 左右旋转
上下左右键 上下左右旋转
- 减少立方体
= 增加立方体
shader代码:
//--------------------------------------------------------------------------------------
// Global variables
//-------------------------------------------------------------------------------------- float4x4 g_mWorld : WORLD : register(c0); // World matrix for object
float4x4 g_mView : VIEW : register(c4); // View matrix for object
float4x4 g_mProj : PROJECTION : register(c8); // Project matrix for object
float4 g_params : PARAMS : register(c12); //-----------------------------------------------------------------------------
// Name: VS_HWInstancing
// Type: Vertex shader (HW Instancing)
//-----------------------------------------------------------------------------
void VS_HWInstancing(
float4 vPos : POSITION,
float3 vNormal : NORMAL,
float4 vColor : COLOR0,
float4 vColorInstance : COLOR1,
float4 vBoxInstance : COLOR2,
out float4 oPos : POSITION,
out float4 oColor : COLOR0)
{
// Use the fourth component of the vBoxInstance to rotate the box:
vBoxInstance.w *= * 3.1415f;
float4 vRotatedPos = vPos;
vRotatedPos.x = vPos.x * cos(vBoxInstance.w) + vPos.z * sin(vBoxInstance.w);
vRotatedPos.z = vPos.z * cos(vBoxInstance.w) - vPos.x * sin(vBoxInstance.w); // Use the instance position to offset the incoming box corner position:
vRotatedPos += float4( (vBoxInstance.xyz) * g_params.x, );
vRotatedPos.x -= g_params.y;
vRotatedPos.z -= g_params.y;
vRotatedPos.w = 1.0f; // Transform the position from object space to homogeneous projection space
oPos = mul( vRotatedPos, g_mWorld );
oPos = mul( oPos, g_mView );
oPos = mul( oPos, g_mProj ); // color
oColor = vColor * vColorInstance;
} //-----------------------------------------------------------------------------
// Name: PS
// Type: Pixel shader
//-----------------------------------------------------------------------------
float4 PS( float4 vColor : COLOR0 ) : COLOR0
{
return vColor;
} //-----------------------------------------------------------------------------
// Name: THW_Instancing
// Type: Technique
// Desc: Renders scene through Hardware instancing
//-----------------------------------------------------------------------------
technique THW_Instancing
{
pass P0
{
VertexShader = compile vs_2_0 VS_HWInstancing();
PixelShader = compile ps_2_0 PS();
}
}
下载地址: