Cel shade c'est mieux mais ALED

This commit is contained in:
CatChow0 2024-09-26 11:30:26 +02:00
parent 71403c614d
commit 30b41922d9
7 changed files with 51 additions and 35 deletions

View File

@ -329,7 +329,6 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
LightBufferType* dataPtr2; LightBufferType* dataPtr2;
unsigned int bufferNumber; unsigned int bufferNumber;
// Transpose the matrices to prepare them for the shader. // Transpose the matrices to prepare them for the shader.
worldMatrix = XMMatrixTranspose(worldMatrix); worldMatrix = XMMatrixTranspose(worldMatrix);
viewMatrix = XMMatrixTranspose(viewMatrix); 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. // Finally set the constant buffer in the vertex shader with the updated values.
deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer); 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. // Lock the light constant buffer so it can be written to.
result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
if (FAILED(result)) if (FAILED(result))
@ -376,13 +372,6 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X
dataPtr2->diffuseColor = diffuseColor; dataPtr2->diffuseColor = diffuseColor;
dataPtr2->lightDirection = lightDirection; dataPtr2->lightDirection = lightDirection;
dataPtr2->lightPosition = lightPosition; 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. // Unlock the constant buffer.
deviceContext->Unmap(m_lightBuffer, 0); 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. // Finally set the light constant buffer in the pixel shader with the updated values.
deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer); deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer);
// Set shader texture resource in the pixel shader.
deviceContext->PSSetShaderResources(0, 1, &texture);
return true; return true;
} }

View File

@ -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); Logger::Get().Log("Could not initialize the fps string", __FILE__, __LINE__, Logger::LogLevel::Error);
return false; return false;
} }
} }
catch (const std::exception& e) 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(); ambientColor[i] = m_Lights[i]->GetPosition();
} }
//Add the 3 first value of the light position to the TrueLightPosition XMFLOAT3 //Add the 3 first value of the first light position to the TrueLightPosition XMFLOAT3
TrueLightPosition.x = lightPosition[0].x; positionX = lightPosition[0].x;
TrueLightPosition.y = lightPosition[0].y; positionY = lightPosition[0].y;
TrueLightPosition.z = lightPosition[0].z; 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. scaleMatrix = XMMatrixScaling(0.5f, 0.5f, 0.5f); // Build the scaling matrix.
rotateMatrix = XMMatrixRotationY(rotation); // Build the rotation 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()); object->Render(m_Direct3D->GetDeviceContext());
if (!m_enableCelShading) { 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); diffuseColor, lightPosition, ambientColor);
if (!result) 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. // Render cel shading globally to the scene using the cel shader if the checkbox is checked.
if (m_enableCelShading) { 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); m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), TrueLightPosition);
if (!result) if (!result)
{ {

View File

@ -137,6 +137,7 @@ private :
int m_numLights; int m_numLights;
XMFLOAT3 TrueLightPosition; XMFLOAT3 TrueLightPosition;
ModelClass* m_LightModel;
// ----------------------------------- // // ----------------------------------- //
// ------------- SHADERS ------------- // // ------------- SHADERS ------------- //

View File

@ -27,25 +27,44 @@ float4 CelShadingPixelShader(PixelInputType input) : SV_TARGET
// Sample the pixel color from the texture. // Sample the pixel color from the texture.
textureColor = shaderTexture.Sample(SampleType, input.tex); textureColor = shaderTexture.Sample(SampleType, input.tex);
// Normalize the normal
float3 normal = normalize(input.normal); float3 normal = normalize(input.normal);
// Calculate the vector from the pixel to the light source // Calculate the light vector from the light position to the world position
float3 lightVector = lightPosition - input.worldPos; float3 lightVector = normalize(lightPosition - input.worldPos);
float distance = length(lightVector);
lightVector = normalize(lightVector);
// Calculate the light intensity based on the light direction. // 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. // 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 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. // Calculate the final color by combining the texture color with the light intensity and diffuse color.

View File

@ -28,15 +28,15 @@ PixelInputType CelShadingVertexShader(VertexInputType input)
input.position.w = 1.0f; input.position.w = 1.0f;
// Calculate the position of the vertex against the world, view, and projection matrices. // Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix); float4 worldPosition = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix); output.position = mul(worldPosition, viewMatrix);
output.position = mul(output.position, projectionMatrix); output.position = mul(output.position, projectionMatrix);
// Pass the normal to the pixel shader // 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 // 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. // Store the texture coordinates for the pixel shader.
output.tex = input.tex; output.tex = input.tex;

View File

@ -3,11 +3,11 @@ Pos=60,60
Size=400,400 Size=400,400
[Window][Khaotic Engine] [Window][Khaotic Engine]
Pos=430,-13 Pos=510,25
Size=392,218 Size=392,273
[Window][Objects] [Window][Objects]
Pos=998,87 Pos=934,36
Size=457,294 Size=457,294
[Window][Terrain] [Window][Terrain]
@ -15,7 +15,7 @@ Pos=60,60
Size=342,82 Size=342,82
[Window][Light] [Window][Light]
Pos=1197,66 Pos=1223,64
Size=345,519 Size=345,519
[Window][Shader Manager] [Window][Shader Manager]