From 30b41922d928498fd7da1b1e15cf3a66198b5fd9 Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Thu, 26 Sep 2024 11:30:26 +0200 Subject: [PATCH] Cel shade c'est mieux mais ALED --- enginecustom/CelShadingShader.cpp | 14 +++--------- enginecustom/CelShadingShader.h | 2 +- enginecustom/applicationclass.cpp | 16 ++++++++----- enginecustom/applicationclass.h | 1 + enginecustom/celshading.ps | 37 +++++++++++++++++++++++-------- enginecustom/celshading.vs | 8 +++---- enginecustom/imgui.ini | 8 +++---- 7 files changed, 51 insertions(+), 35 deletions(-) diff --git a/enginecustom/CelShadingShader.cpp b/enginecustom/CelShadingShader.cpp index 29bbc17..478551d 100644 --- a/enginecustom/CelShadingShader.cpp +++ b/enginecustom/CelShadingShader.cpp @@ -329,7 +329,6 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X LightBufferType* dataPtr2; unsigned int bufferNumber; - // Transpose the matrices to prepare them for the shader. worldMatrix = XMMatrixTranspose(worldMatrix); viewMatrix = XMMatrixTranspose(viewMatrix); @@ -359,9 +358,6 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X // Finally set the constant buffer in the vertex shader with the updated values. deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer); - // Set shader texture resource in the pixel shader. - deviceContext->PSSetShaderResources(0, 1, &texture); - // Lock the light constant buffer so it can be written to. result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) @@ -376,13 +372,6 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X dataPtr2->diffuseColor = diffuseColor; dataPtr2->lightDirection = lightDirection; dataPtr2->lightPosition = lightPosition; - dataPtr2->padding = 0.0f; - dataPtr2->padding2 = 0.0f; - - // store the light direction in a string - std::string lightDirectionString = std::to_string(lightDirection.x) + ", " + std::to_string(lightDirection.y) + ", " + std::to_string(lightDirection.z); - Logger::Get().Log(lightDirectionString, __FILE__, __LINE__, Logger::LogLevel::Debug); - // Unlock the constant buffer. deviceContext->Unmap(m_lightBuffer, 0); @@ -393,6 +382,9 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X // Finally set the light constant buffer in the pixel shader with the updated values. deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer); + // Set shader texture resource in the pixel shader. + deviceContext->PSSetShaderResources(0, 1, &texture); + return true; } diff --git a/enginecustom/CelShadingShader.h b/enginecustom/CelShadingShader.h index 6e0c4f3..d152943 100644 --- a/enginecustom/CelShadingShader.h +++ b/enginecustom/CelShadingShader.h @@ -60,4 +60,4 @@ private: ID3D11Buffer* m_lightBuffer; }; -#endif +#endif \ No newline at end of file diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 9b45de2..9cbe31c 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -355,6 +355,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) Logger::Get().Log("Could not initialize the fps string", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } + } catch (const std::exception& e) { @@ -1006,10 +1007,13 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t ambientColor[i] = m_Lights[i]->GetPosition(); } - //Add the 3 first value of the light position to the TrueLightPosition XMFLOAT3 - TrueLightPosition.x = lightPosition[0].x; - TrueLightPosition.y = lightPosition[0].y; - TrueLightPosition.z = lightPosition[0].z; + //Add the 3 first value of the first light position to the TrueLightPosition XMFLOAT3 + positionX = lightPosition[0].x; + positionY = lightPosition[0].y; + positionZ = lightPosition[0].z; + XMFLOAT3 TrueLightPosition = XMFLOAT3(positionX, positionY, positionZ); + + Logger::Get().Log("PositionX: " + std::to_string(positionX) + ", PositionY: " + std::to_string(positionY) + ", PositionZ: " + std::to_string(positionZ), __FILE__, __LINE__, Logger::LogLevel::Debug); scaleMatrix = XMMatrixScaling(0.5f, 0.5f, 0.5f); // Build the scaling matrix. rotateMatrix = XMMatrixRotationY(rotation); // Build the rotation matrix. @@ -1085,7 +1089,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t object->Render(m_Direct3D->GetDeviceContext()); if (!m_enableCelShading) { - result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, object->GetTexture(0), + result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, object->GetTexture(0), diffuseColor, lightPosition, ambientColor); if (!result) @@ -1097,7 +1101,7 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t // Render cel shading globally to the scene using the cel shader if the checkbox is checked. if (m_enableCelShading) { - result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, object->GetTexture(0), + result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, object->GetTexture(0), m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), TrueLightPosition); if (!result) { diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index ff9bcee..fccdea5 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -137,6 +137,7 @@ private : int m_numLights; XMFLOAT3 TrueLightPosition; + ModelClass* m_LightModel; // ----------------------------------- // // ------------- SHADERS ------------- // diff --git a/enginecustom/celshading.ps b/enginecustom/celshading.ps index 520ed49..c613dcf 100644 --- a/enginecustom/celshading.ps +++ b/enginecustom/celshading.ps @@ -27,25 +27,44 @@ float4 CelShadingPixelShader(PixelInputType input) : SV_TARGET // Sample the pixel color from the texture. textureColor = shaderTexture.Sample(SampleType, input.tex); - // Normalize the normal float3 normal = normalize(input.normal); - // Calculate the vector from the pixel to the light source - float3 lightVector = lightPosition - input.worldPos; - float distance = length(lightVector); - lightVector = normalize(lightVector); + // Calculate the light vector from the light position to the world position + float3 lightVector = normalize(lightPosition - input.worldPos); // Calculate the light intensity based on the light direction. - lightIntensity = saturate(dot(normal, lightVector)); + float directionalLightIntensity = saturate(dot(normal, normalize(lightDirection))); + + // Calculate the light intensity based on the light position. + float positionalLightIntensity = saturate(dot(normal, lightVector)); + + // Combine the directional and positional light intensities. + lightIntensity = max(directionalLightIntensity, positionalLightIntensity); // Apply a step function to create the cel shading effect. - if (lightIntensity > 0.5f) + if (lightIntensity > 0.75f) { - lightIntensity = 1.0f; + lightIntensity = 1.0f; // Brightest level + } + else if (lightIntensity > 0.5f) + { + lightIntensity = 0.7f; // Mid-bright level + } + else if (lightIntensity > 0.25f) + { + lightIntensity = 0.4f; // Mid-dark level } else { - lightIntensity = 0.3f; + lightIntensity = 0.1f; // Darkest level + } + + // Simple shadow calculation: if the fragment is behind the light source, it is in shadow. + float3 toLight = normalize(lightPosition - input.worldPos); + float shadow = saturate(dot(normal, toLight)); + if (shadow < 0.1f) + { + lightIntensity *= 0.5f; // Darken the fragment if it is in shadow } // Calculate the final color by combining the texture color with the light intensity and diffuse color. diff --git a/enginecustom/celshading.vs b/enginecustom/celshading.vs index fe58dbc..842d724 100644 --- a/enginecustom/celshading.vs +++ b/enginecustom/celshading.vs @@ -28,15 +28,15 @@ PixelInputType CelShadingVertexShader(VertexInputType input) input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. - output.position = mul(input.position, worldMatrix); - output.position = mul(output.position, viewMatrix); + float4 worldPosition = mul(input.position, worldMatrix); + output.position = mul(worldPosition, viewMatrix); output.position = mul(output.position, projectionMatrix); // Pass the normal to the pixel shader - output.normal = mul(input.normal, (float3x3)worldMatrix); + output.normal = mul((float3x3)worldMatrix, input.normal); // Pass the world position to the pixel shader - output.worldPos = mul(input.position, worldMatrix).xyz; + output.worldPos = worldPosition.xyz; // Store the texture coordinates for the pixel shader. output.tex = input.tex; diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 3b43e0d..6bf74f5 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,11 +3,11 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=430,-13 -Size=392,218 +Pos=510,25 +Size=392,273 [Window][Objects] -Pos=998,87 +Pos=934,36 Size=457,294 [Window][Terrain] @@ -15,7 +15,7 @@ Pos=60,60 Size=342,82 [Window][Light] -Pos=1197,66 +Pos=1223,64 Size=345,519 [Window][Shader Manager]