CUBE的几何实例化DEMO
鼠标右键按下并拖动 旋转视角
WSAD 前后左右
RF 上下
QE 左右旋转
上下左右键 上下左右旋转
- 减少立方体
= 增加立方体
shader代码:
1 //-------------------------------------------------------------------------------------- 2 // Global variables 3 //-------------------------------------------------------------------------------------- 4 5 float4x4 g_mWorld : WORLD : register(c0); // World matrix for object 6 float4x4 g_mView : VIEW : register(c4); // View matrix for object 7 float4x4 g_mProj : PROJECTION : register(c8); // Project matrix for object 8 float4 g_params : PARAMS : register(c12); 9 10 //----------------------------------------------------------------------------- 11 // Name: VS_HWInstancing 12 // Type: Vertex shader (HW Instancing) 13 //----------------------------------------------------------------------------- 14 void VS_HWInstancing( 15 float4 vPos : POSITION, 16 float3 vNormal : NORMAL, 17 float4 vColor : COLOR0, 18 float4 vColorInstance : COLOR1, 19 float4 vBoxInstance : COLOR2, 20 out float4 oPos : POSITION, 21 out float4 oColor : COLOR0) 22 { 23 // Use the fourth component of the vBoxInstance to rotate the box: 24 vBoxInstance.w *= 2 * 3.1415f; 25 float4 vRotatedPos = vPos; 26 vRotatedPos.x = vPos.x * cos(vBoxInstance.w) + vPos.z * sin(vBoxInstance.w); 27 vRotatedPos.z = vPos.z * cos(vBoxInstance.w) - vPos.x * sin(vBoxInstance.w); 28 29 // Use the instance position to offset the incoming box corner position: 30 vRotatedPos += float4( (vBoxInstance.xyz) * g_params.x, 0 ); 31 vRotatedPos.x -= g_params.y; 32 vRotatedPos.z -= g_params.y; 33 vRotatedPos.w = 1.0f; 34 35 // Transform the position from object space to homogeneous projection space 36 oPos = mul( vRotatedPos, g_mWorld ); 37 oPos = mul( oPos, g_mView ); 38 oPos = mul( oPos, g_mProj ); 39 40 // color 41 oColor = vColor * vColorInstance; 42 } 43 44 //----------------------------------------------------------------------------- 45 // Name: PS 46 // Type: Pixel shader 47 //----------------------------------------------------------------------------- 48 float4 PS( float4 vColor : COLOR0 ) : COLOR0 49 { 50 return vColor; 51 } 52 53 //----------------------------------------------------------------------------- 54 // Name: THW_Instancing 55 // Type: Technique 56 // Desc: Renders scene through Hardware instancing 57 //----------------------------------------------------------------------------- 58 technique THW_Instancing 59 { 60 pass P0 61 { 62 VertexShader = compile vs_2_0 VS_HWInstancing(); 63 PixelShader = compile ps_2_0 PS(); 64 } 65 }
下载地址: