From df55562d59687001eadbb859687ff554839cc367 Mon Sep 17 00:00:00 2001 From: GolfOcean334 <130740013+GolfOcean334@users.noreply.github.com> Date: Fri, 12 Apr 2024 15:56:59 +0200 Subject: [PATCH] Fix Water Shader dans le shader manager --- enginecustom/applicationclass.cpp | 1 - enginecustom/shadermanagerclass.cpp | 9 ++-- enginecustom/shadermanagerclass.h | 4 +- enginecustom/water.ps | 72 +++++++++++++++++++++++++++++ enginecustom/watershaderclass.cpp | 4 +- 5 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 enginecustom/water.ps diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 8058c58..f9f5e1e 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -19,7 +19,6 @@ ApplicationClass::ApplicationClass() m_Position = 0; m_Frustum = 0; m_DisplayPlane = 0; - m_ReflectionShader = 0; } diff --git a/enginecustom/shadermanagerclass.cpp b/enginecustom/shadermanagerclass.cpp index dddb93b..4435e07 100644 --- a/enginecustom/shadermanagerclass.cpp +++ b/enginecustom/shadermanagerclass.cpp @@ -378,14 +378,15 @@ bool ShaderManagerClass::RenderRefractionShader(ID3D11DeviceContext* deviceConte return true; } -bool ShaderManagerClass::RenderWaterShader(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, XMMATRIX reflectionMatrix, - ID3D11ShaderResourceView* reflectionTexture, ID3D11ShaderResourceView* refractionTexture, ID3D11ShaderResourceView* normalTexture, - float waterTranslation, float reflectRefractScale) +bool ShaderManagerClass::RenderWaterShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + XMMATRIX reflectionMatrix, ID3D11ShaderResourceView* reflectionTexture, ID3D11ShaderResourceView* refractionTexture, + ID3D11ShaderResourceView* normalTexture, float waterTranslation, float reflectRefractScale) { bool result; - result = m_RefractionShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor, clipPlane); + result = m_WaterShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, reflectionMatrix, reflectionTexture, + refractionTexture, normalTexture, waterTranslation, reflectRefractScale); if (!result) { return false; diff --git a/enginecustom/shadermanagerclass.h b/enginecustom/shadermanagerclass.h index e5685bb..c9258b4 100644 --- a/enginecustom/shadermanagerclass.h +++ b/enginecustom/shadermanagerclass.h @@ -41,8 +41,8 @@ public: bool RenderlightMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*); bool RenderRefractionShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4, XMFLOAT4); - bool RenderWaterShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, - ID3D11ShaderResourceView*, float, float); + bool RenderWaterShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, + ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float); private: TextureShaderClass* m_TextureShader; NormalMapShaderClass* m_NormalMapShader; diff --git a/enginecustom/water.ps b/enginecustom/water.ps new file mode 100644 index 0000000..4dc414e --- /dev/null +++ b/enginecustom/water.ps @@ -0,0 +1,72 @@ +///////////// +// GLOBALS // +///////////// +SamplerState SampleType : register(s0); + +Texture2D reflectionTexture : register(t0); +Texture2D refractionTexture : register(t1); +Texture2D normalTexture : register(t2); + +cbuffer WaterBuffer +{ + float waterTranslation; + float reflectRefractScale; + float2 padding; +}; + + +////////////// +// TYPEDEFS // +////////////// +struct PixelInputType +{ + float4 position : SV_POSITION; + float2 tex : TEXCOORD0; + float4 reflectionPosition : TEXCOORD1; + float4 refractionPosition : TEXCOORD2; +}; + + +//////////////////////////////////////////////////////////////////////////////// +// Pixel Shader +//////////////////////////////////////////////////////////////////////////////// +float4 WaterPixelShader(PixelInputType input) : SV_TARGET +{ + float2 reflectTexCoord; + float2 refractTexCoord; + float4 normalMap; + float3 normal; + float4 reflectionColor; + float4 refractionColor; + float4 color; + + // Move the position the water normal is sampled from to simulate moving water. + input.tex.y += waterTranslation; + + // Calculate the projected reflection texture coordinates. + reflectTexCoord.x = input.reflectionPosition.x / input.reflectionPosition.w / 2.0f + 0.5f; + reflectTexCoord.y = -input.reflectionPosition.y / input.reflectionPosition.w / 2.0f + 0.5f; + + // Calculate the projected refraction texture coordinates. + refractTexCoord.x = input.refractionPosition.x / input.refractionPosition.w / 2.0f + 0.5f; + refractTexCoord.y = -input.refractionPosition.y / input.refractionPosition.w / 2.0f + 0.5f; + + // Sample the normal from the normal map texture. + normalMap = normalTexture.Sample(SampleType, input.tex); + + // Expand the range of the normal from (0,1) to (-1,+1). + normal = (normalMap.xyz * 2.0f) - 1.0f; + + // Re-position the texture coordinate sampling position by the normal map value to simulate the rippling wave effect. + reflectTexCoord = reflectTexCoord + (normal.xy * reflectRefractScale); + refractTexCoord = refractTexCoord + (normal.xy * reflectRefractScale); + + // Sample the texture pixels from the textures using the updated texture coordinates. + reflectionColor = reflectionTexture.Sample(SampleType, reflectTexCoord); + refractionColor = refractionTexture.Sample(SampleType, refractTexCoord); + + // Combine the reflection and refraction results for the final color. + color = lerp(reflectionColor, refractionColor, 0.6f); + + return color; +} \ No newline at end of file diff --git a/enginecustom/watershaderclass.cpp b/enginecustom/watershaderclass.cpp index 8781b34..32b2a1d 100644 --- a/enginecustom/watershaderclass.cpp +++ b/enginecustom/watershaderclass.cpp @@ -31,14 +31,14 @@ bool WaterShaderClass::Initialize(ID3D11Device* device, HWND hwnd) int error; // Set the filename of the vertex shader. - error = wcscpy_s(vsFilename, 128, L"../Engine/water.vs"); + error = wcscpy_s(vsFilename, 128, L"water.vs"); if (error != 0) { return false; } // Set the filename of the pixel shader. - error = wcscpy_s(psFilename, 128, L"../Engine/water.ps"); + error = wcscpy_s(psFilename, 128, L"water.ps"); if (error != 0) { return false;