Skip to content

Commit 20dde48

Browse files
+ 添加公告板树的代码
1 parent 3343f0f commit 20dde48

9 files changed

+343
-8
lines changed

Chapter 12 The Geometry Shader/Chapter 12 The Geometry Shader.vcxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@
248248
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
249249
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
250250
</FxCompile>
251+
<FxCompile Include="Core\Shaders\billboardGS.hlsl">
252+
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Geometry</ShaderType>
253+
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Geometry</ShaderType>
254+
</FxCompile>
255+
<FxCompile Include="Core\Shaders\billboardPS.hlsl">
256+
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
257+
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
258+
</FxCompile>
259+
<FxCompile Include="Core\Shaders\billboardVS.hlsl">
260+
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType>
261+
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType>
262+
</FxCompile>
251263
<FxCompile Include="Core\Shaders\Bitonic32InnerSortCS.hlsl" />
252264
<FxCompile Include="Core\Shaders\Bitonic32OuterSortCS.hlsl" />
253265
<FxCompile Include="Core\Shaders\Bitonic32PreSortCS.hlsl" />

Chapter 12 The Geometry Shader/Chapter 12 The Geometry Shader.vcxproj.filters

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,15 @@
835835
<FxCompile Include="Core\Shaders\defaultPS.hlsl">
836836
<Filter>Shaders\default</Filter>
837837
</FxCompile>
838+
<FxCompile Include="Core\Shaders\billboardVS.hlsl">
839+
<Filter>Shaders\default</Filter>
840+
</FxCompile>
841+
<FxCompile Include="Core\Shaders\billboardGS.hlsl">
842+
<Filter>Shaders\default</Filter>
843+
</FxCompile>
844+
<FxCompile Include="Core\Shaders\billboardPS.hlsl">
845+
<Filter>Shaders\default</Filter>
846+
</FxCompile>
838847
</ItemGroup>
839848
<ItemGroup>
840849
<Text Include="Core\Graphics\Command\readme_command.txt">
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Include structures and functions for lighting.
2+
#include "LightingUtil.hlsl"
3+
4+
// Constant data that varies per frame.
5+
cbuffer cbPass : register(b1)
6+
{
7+
float4x4 gViewProj;
8+
float3 gEyePosW;
9+
float pad;
10+
float4 gAmbientLight;
11+
12+
// Allow application to change fog parameters once per frame.
13+
// For example, we may only use fog for certain times of day.
14+
float4 gFogColor;
15+
float gFogStart;
16+
float gFogRange;
17+
float2 pad2;
18+
19+
// Indices [0, NUM_DIR_LIGHTS) are directional lights;
20+
// indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
21+
// indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
22+
// are spot lights for a maximum of MaxLights per object.
23+
Light gLights[MaxLights];
24+
};
25+
26+
struct VertexOut
27+
{
28+
float3 CenterW : POSITION;
29+
float2 SizeW : SIZE;
30+
};
31+
32+
struct GeoOut
33+
{
34+
float4 PosH : SV_POSITION; // 顶点的齐次坐标
35+
float3 PosW : POSITION; // 顶点的世界坐标
36+
float3 NormalW : NORMAL; // 顶点的世界法向量
37+
float2 TexC : TEXCOORD; // 顶点的纹理坐标
38+
uint PrimID : SV_PrimitiveID; // 顶点ID
39+
};
40+
41+
[maxvertexcount(4)]
42+
void main(
43+
point VertexOut gin[1],
44+
uint primID : SV_PrimitiveID,
45+
inout TriangleStream< GeoOut > triStream
46+
)
47+
{
48+
// 计算up向量
49+
float3 up = float3(0.0f, 1.0f, 0.0f);
50+
// 计算目标点到观察点的向量
51+
float3 look = gEyePosW - gin[0].CenterW;
52+
// 保证目标点和观察点在通一个xz平面
53+
look.y = 0.0f;
54+
// 标准化
55+
look = normalize(look);
56+
// 计算右向量
57+
float3 right = cross(up, look);
58+
59+
// 计算公告板树的宽和高
60+
float halfWidth = 0.5f * gin[0].SizeW.x;
61+
float halfHeight = 0.5f * gin[0].SizeW.y;
62+
63+
// 计算树的4个顶点
64+
float4 v[4];
65+
v[0] = float4(gin[0].CenterW + halfWidth * right - halfHeight * up, 1.0f);
66+
v[1] = float4(gin[0].CenterW + halfWidth * right + halfHeight * up, 1.0f);
67+
v[2] = float4(gin[0].CenterW - halfWidth * right - halfHeight * up, 1.0f);
68+
v[3] = float4(gin[0].CenterW - halfWidth * right + halfHeight * up, 1.0f);
69+
70+
// 四个点对应的纹理坐标
71+
float2 texC[4] =
72+
{
73+
float2(0.0f, 1.0f),
74+
float2(0.0f, 0.0f),
75+
float2(1.0f, 1.0f),
76+
float2(1.0f, 0.0f)
77+
};
78+
79+
// 输出图源
80+
GeoOut gout;
81+
[unroll]
82+
for (int i = 0; i < 4; ++i)
83+
{
84+
gout.PosH = mul(v[i], gViewProj); // 顶点由世界坐标系转到投影坐标系
85+
gout.PosW = v[i].xyz; // 顶点的世界坐标
86+
gout.NormalW = look; // 顶点的法向量
87+
gout.TexC = texC[i]; // 顶点的纹理
88+
gout.PrimID = primID; // 该顶点所组成的面的ID
89+
90+
triStream.Append(gout);
91+
}
92+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//***************************************************************************************
2+
// Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
3+
//
4+
// Default shader, currently supports lighting.
5+
//***************************************************************************************
6+
7+
// Defaults for number of lights.
8+
#ifndef NUM_DIR_LIGHTS
9+
#define NUM_DIR_LIGHTS 3
10+
#endif
11+
12+
#ifndef NUM_POINT_LIGHTS
13+
#define NUM_POINT_LIGHTS 0
14+
#endif
15+
16+
#ifndef NUM_SPOT_LIGHTS
17+
#define NUM_SPOT_LIGHTS 0
18+
#endif
19+
20+
// Include structures and functions for lighting.
21+
#include "LightingUtil.hlsl"
22+
23+
Texture2DArray gTreeMapArray : register(t0);
24+
25+
SamplerState gsamAnisotropicWrap : register(s0);
26+
27+
// Constant data that varies per frame.
28+
cbuffer cbPass : register(b1)
29+
{
30+
float4x4 gViewProj;
31+
float3 gEyePosW;
32+
float pad;
33+
float4 gAmbientLight;
34+
35+
// Allow application to change fog parameters once per frame.
36+
// For example, we may only use fog for certain times of day.
37+
float4 gFogColor;
38+
float gFogStart;
39+
float gFogRange;
40+
float2 pad2;
41+
42+
// Indices [0, NUM_DIR_LIGHTS) are directional lights;
43+
// indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
44+
// indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
45+
// are spot lights for a maximum of MaxLights per object.
46+
Light gLights[MaxLights];
47+
};
48+
49+
// Constant data that varies per material.
50+
cbuffer cbMaterial : register(b2)
51+
{
52+
float4 gDiffuseAlbedo;
53+
float3 gFresnelR0;
54+
float gPad;
55+
float gRoughness;
56+
};
57+
58+
struct GeoOut
59+
{
60+
float4 PosH : SV_POSITION; // 顶点的齐次坐标
61+
float3 PosW : POSITION; // 顶点的世界坐标
62+
float3 NormalW : NORMAL; // 顶点的世界法向量
63+
float2 TexC : TEXCOORD; // 顶点的纹理坐标
64+
uint PrimID : SV_PrimitiveID; // 顶点ID
65+
};
66+
67+
float4 main(GeoOut pin) : SV_Target0
68+
{
69+
float3 uvw = float3(pin.TexC, pin.PrimID % 3);
70+
float4 diffuseAlbedo = gTreeMapArray.Sample(gsamAnisotropicWrap, uvw) * gDiffuseAlbedo;
71+
72+
// 透明像素点剔除
73+
// Discard pixel if texture alpha < 0.1. We do this test as soon
74+
// as possible in the shader so that we can potentially exit the
75+
// shader early, thereby skipping the rest of the shader code.
76+
clip(diffuseAlbedo.a - 0.1f);
77+
78+
// Interpolating normal can unnormalize it, so renormalize it.
79+
pin.NormalW = normalize(pin.NormalW);
80+
81+
// Vector from point being lit to eye.
82+
float3 toEyeW = gEyePosW - pin.PosW;
83+
float distToEye = length(toEyeW);
84+
toEyeW /= distToEye; // normalize
85+
86+
// Indirect lighting.
87+
float4 ambient = gAmbientLight * diffuseAlbedo;
88+
89+
const float shininess = 1.0f - gRoughness;
90+
Material mat = { diffuseAlbedo, gFresnelR0, shininess };
91+
float3 shadowFactor = 1.0f;
92+
float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
93+
pin.NormalW, toEyeW, shadowFactor);
94+
95+
float4 litColor = ambient + directLight;
96+
97+
// 雾,如果雾的颜色alpha为0,则不处理
98+
if (gFogColor.a > 0.01)
99+
{
100+
float fogAmount = saturate((distToEye - gFogStart) / gFogRange);
101+
litColor = lerp(litColor, gFogColor, fogAmount);
102+
}
103+
104+
// Common convention to take alpha from diffuse material.
105+
litColor.a = diffuseAlbedo.a;
106+
107+
return litColor;
108+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct VertexIn
2+
{
3+
float3 PosW : POSITION; // 顶点的世界坐标
4+
float2 SizeW : SIZE; // 顶点的宽高
5+
};
6+
7+
struct VertexOut
8+
{
9+
float3 CenterW : POSITION; // 中心点的世界坐标
10+
float2 SizeW : SIZE; // 宽高
11+
};
12+
13+
VertexOut main(VertexIn vin)
14+
{
15+
VertexOut vout;
16+
17+
// 顶点直接传给几何着色器
18+
vout.CenterW = vin.PosW;
19+
vout.SizeW = vin.SizeW;
20+
21+
return vout;
22+
}

0 commit comments

Comments
 (0)