Minor - Start Shadow Map - V10.5.0

This commit is contained in:
2025-05-22 17:28:29 +02:00
parent f9d4523f09
commit d6b7626446
18 changed files with 1305 additions and 531 deletions

View File

@@ -0,0 +1,10 @@
struct PS_INPUT
{
float4 position : SV_POSITION;
};
float4 DepthPixelShader(PS_INPUT input) : SV_TARGET
{
// Pas de couleur, juste la profondeur
return float4(0.0f, 0.0f, 0.0f, 1.0f);
}

View File

@@ -0,0 +1,25 @@
cbuffer MatrixBuffer : register(b0)
{
matrix world;
matrix view;
matrix projection;
};
struct VS_INPUT
{
float3 position : POSITION;
};
struct VS_OUTPUT
{
float4 position : SV_POSITION;
};
VS_OUTPUT DepthVertexShader(VS_INPUT input)
{
VS_OUTPUT output;
float4 worldPosition = mul(float4(input.position, 1.0f), world);
float4 viewPosition = mul(worldPosition, view);
output.position = mul(viewPosition, projection);
return output;
}

View File

@@ -3,6 +3,9 @@
/////////////
Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0);
Texture2D shadowMap : register(t1);
SamplerState shadowSampleType : register(s1);
cbuffer SunLightBuffer
{
float4 ambientColor;
@@ -24,6 +27,7 @@ struct PixelInputType
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float4 lightPosition : TEXCOORD1;
};
////////////////////////////////////////////////////////////////////////////////
@@ -31,23 +35,47 @@ struct PixelInputType
////////////////////////////////////////////////////////////////////////////////
float4 SunLightPixelShader(PixelInputType input) : SV_TARGET
{
// temp return to check if the shader is working
return float4(1.0f, 0.0f, 0.0f, 1.0f);
float4 textureColor;
float4 color;
float lightIntensity;
float4 colorArray;
float4 colorSum;
float bias = 0.001f;
float shadowFactor = 1.0f;
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
// Sample the pixel color from the texture using the sampler at this coordinate
textureColor = shaderTexture.Sample(SampleType, input.tex);
// Calculate shadow factor
// Projection division to normalize coordinates
float2 projectTexCoord;
projectTexCoord.x = input.lightPosition.x / input.lightPosition.w / 2.0f + 0.5f;
projectTexCoord.y = -input.lightPosition.y / input.lightPosition.w / 2.0f + 0.5f;
if((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y))
{
float depthValue = input.lightPosition.z / input.lightPosition.w;
float shadowDepth = shadowMap.Sample(shadowSampleType, projectTexCoord).r;
if(depthValue - bias > shadowDepth)
{
// Pixel est dans l'ombre
shadowFactor = 0.3f; // Ombre non totale pour un effet plus r<EFBFBD>aliste
}
}
// Calculate the different amounts of light on this pixel based on the direction of the light.
lightIntensity = saturate(dot(input.normal, -lightDirection));
// Determine the diffuse color amount of the light.
colorArray = (diffuseColor * lightIntensity) * intensity;
colorArray = (diffuseColor * lightIntensity) * intensity * shadowFactor;
// Initialize the sum of colors.
colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);
colorSum = ambientColor; // Conserver l'<27>clairage ambiant m<EFBFBD>me dans l'ombre
// Add the light color.
colorSum.r += colorArray.r;
@@ -58,4 +86,4 @@ float4 SunLightPixelShader(PixelInputType input) : SV_TARGET
color = saturate(colorSum) * textureColor;
return color;
}
}

View File

@@ -22,6 +22,12 @@ cbuffer SunLightBuffer
float intensity;
};
cbuffer LightViewMatrixBuffer : register(b2)
{
matrix lightViewMatrix;
matrix lightProjectionMatrix;
};
//////////////
// TYPEDEFS //
//////////////
@@ -37,6 +43,7 @@ struct PixelInputType
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
float4 lightPosition : TEXCOORD1;
};
////////////////////////////////////////////////////////////////////////////////
@@ -45,23 +52,30 @@ struct PixelInputType
PixelInputType SunLightVertexShader(VertexInputType input)
{
PixelInputType output;
float4 worldPosition; // D<>claration de la variable manquante
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
// Calculate the position of the vertex against the world matrix
worldPosition = mul(input.position, worldMatrix);
// Calculate position in light view space.
output.lightPosition = mul(worldPosition, lightViewMatrix);
output.lightPosition = mul(output.lightPosition, lightProjectionMatrix);
// 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);
// Standard position calculation
output.position = mul(worldPosition, viewMatrix);
output.position = mul(output.position, projectionMatrix);
// Store the texture coordinates for the pixel shader.
output.tex = input.tex;
// Calculate the normal vector against the world matrix only.
output.normal = mul(input.normal, (float3x3) worldMatrix);
output.normal = mul(input.normal, (float3x3)worldMatrix);
// Normalize the normal vector.
output.normal = normalize(output.normal);
return output;
}
}