Skip to content

Commit 6aadeb2

Browse files
+ 水体的透明以及雾效果
1 parent 6de007b commit 6aadeb2

File tree

4 files changed

+54
-19
lines changed

4 files changed

+54
-19
lines changed

Chapter 10 Blending/Core/Shaders/defaultPS.hlsl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ cbuffer cbPass : register(b1)
2929
{
3030
float4x4 gViewProj;
3131
float3 gEyePosW;
32+
float pad;
3233
float4 gAmbientLight;
3334

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+
3442
// Indices [0, NUM_DIR_LIGHTS) are directional lights;
3543
// indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
3644
// indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
@@ -69,7 +77,9 @@ float4 main(VertexOut pin) : SV_Target0
6977
pin.NormalW = normalize(pin.NormalW);
7078

7179
// Vector from point being lit to eye.
72-
float3 toEyeW = normalize(gEyePosW - pin.PosW);
80+
float3 toEyeW = gEyePosW - pin.PosW;
81+
float distToEye = length(toEyeW);
82+
toEyeW /= distToEye; // normalize
7383

7484
// Indirect lighting.
7585
float4 ambient = gAmbientLight * diffuseAlbedo;
@@ -82,6 +92,13 @@ float4 main(VertexOut pin) : SV_Target0
8292

8393
float4 litColor = ambient + directLight;
8494

95+
// 雾,如果雾的颜色alpha为0,则不处理
96+
if (gFogColor.a > 0)
97+
{
98+
float fogAmount = saturate((distToEye - gFogStart) / gFogRange);
99+
litColor = lerp(litColor, gFogColor, fogAmount);
100+
}
101+
85102
// Common convention to take alpha from diffuse material.
86103
litColor.a = diffuseAlbedo.a;
87104

Chapter 10 Blending/GameApp.cpp

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ void GameApp::Startup(void)
9090
alphaTestPSO.SetRasterizerState(raster);
9191
alphaTestPSO.Finalize();
9292
m_mapPSO[E_EPT_ALPHATEST] = alphaTestPSO;
93+
94+
// 透明混合PSO
95+
GraphicsPSO transparentPSO = defaultPSO;
96+
auto blend = Graphics::BlendTraditional;
97+
blend.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
98+
transparentPSO.SetBlendState(blend);
99+
transparentPSO.Finalize();
100+
m_mapPSO[E_EPT_TRANSPARENT] = transparentPSO;
101+
102+
// 初始化底色
103+
Graphics::g_SceneColorBuffer.SetClearColor({ 0.7f, 0.7f, 0.7f });
93104
}
94105

95106
void GameApp::Cleanup(void)
@@ -515,8 +526,8 @@ void GameApp::buildLandAndWaves()
515526
m_renderItemWaves.modelToWorld = Transpose(Matrix4(kIdentity));
516527
m_renderItemWaves.texTransform = Transpose(Matrix4(AffineTransform(Matrix3::MakeScale(5.0f, 5.0f, 1.0f))));
517528
m_renderItemWaves.matTransform = Transpose(Matrix4(kIdentity));
518-
m_renderItemWaves.diffuseAlbedo = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
519-
m_renderItemWaves.fresnelR0 = XMFLOAT3(0.2f, 0.2f, 0.2f);
529+
m_renderItemWaves.diffuseAlbedo = XMFLOAT4(1.0f, 1.0f, 1.0f, 0.5f);
530+
m_renderItemWaves.fresnelR0 = XMFLOAT3(0.12f, 0.1f, 0.1f);
520531
m_renderItemWaves.roughness = 0.0f;
521532
m_renderItemWaves.srv = MatTextures[1]->GetSRV();
522533

@@ -580,20 +591,7 @@ void GameApp::renderLandAndWaves(GraphicsContext& gfxContext)
580591
// 绘制
581592
gfxContext.DrawIndexedInstanced(m_IndexBufferLand.GetElementCount(), 1, 0, 0, 0);
582593
}
583-
584-
585-
// waves
586-
{
587-
gfxContext.SetIndexBuffer(m_IndexBufferWaves.IndexBufferView());
588-
589-
gfxContext.SetDynamicVB(0, m_verticesWaves.size(), sizeof(Vertex), m_verticesWaves.data());
590-
591-
setShaderParam(gfxContext, m_renderItemWaves);
592-
593-
// 绘制
594-
gfxContext.DrawIndexedInstanced(m_IndexBufferWaves.GetElementCount(), 1, 0, 0, 0);
595-
}
596-
594+
597595
// box
598596
{
599597
// 设置顶点视图
@@ -608,6 +606,20 @@ void GameApp::renderLandAndWaves(GraphicsContext& gfxContext)
608606
// 绘制
609607
gfxContext.DrawIndexedInstanced(m_IndexBufferBox.GetElementCount(), 1, 0, 0, 0);
610608
}
609+
610+
// waves
611+
{
612+
gfxContext.SetIndexBuffer(m_IndexBufferWaves.IndexBufferView());
613+
614+
gfxContext.SetDynamicVB(0, m_verticesWaves.size(), sizeof(Vertex), m_verticesWaves.data());
615+
616+
setShaderParam(gfxContext, m_renderItemWaves);
617+
618+
gfxContext.SetPipelineState(m_mapPSO[E_EPT_TRANSPARENT]);
619+
620+
// 绘制
621+
gfxContext.DrawIndexedInstanced(m_IndexBufferWaves.GetElementCount(), 1, 0, 0, 0);
622+
}
611623
}
612624

613625
void GameApp::UpdateWaves(float deltaT)

Chapter 10 Blending/GameApp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ class GameApp : public GameCore::IGameApp
6464
enum ePSOType
6565
{
6666
E_EPT_DEFAULT = 1,
67-
E_EPT_WIREFRAME = 2,
68-
E_EPT_ALPHATEST = 3,
67+
E_EPT_WIREFRAME = 2, // 只画线
68+
E_EPT_ALPHATEST = 3, // 绘制透明类物体,去掉背面剔除
69+
E_EPT_TRANSPARENT = 4, // 半透混合效果
6970
};
7071
std::unordered_map<int, GraphicsPSO> m_mapPSO;
7172

Chapter 10 Blending/d3dUtil.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ __declspec(align(16)) struct PassConstants
2929
Vector3 eyePosW = { 0.0f, 0.0f, 0.0f }; // 观察点也就是摄像机位置
3030
Vector4 ambientLight = { 0.0f, 0.0f, 0.0f, 1.0f };
3131

32+
Vector4 FogColor = { 0.7f, 0.7f, 0.7f, 0.3f };
33+
float gFogStart = 40.0f;
34+
float gFogRange = 150.0f;
35+
XMFLOAT2 pad;
36+
3237
// Indices [0, NUM_DIR_LIGHTS) are directional lights;
3338
// indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
3439
// indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)

0 commit comments

Comments
 (0)