Minor update - celshading rework started

This commit is contained in:
2025-01-28 17:42:23 +01:00
parent 0d5e26266b
commit 151ea9b191
18 changed files with 542 additions and 233 deletions

View File

@@ -1,34 +1,78 @@
cbuffer LightBuffer
/////////////
// GLOBALS //
/////////////
Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0);
cbuffer SunLightBuffer
{
float4 ambientColor;
float4 diffuseColor;
float3 lightDirection;
float padding; // Padding to ensure the structure is a multiple of 16 bytes.
float3 lightPosition; // Add light position
float padding2; // Padding to ensure the structure is a multiple of 16 bytes.
float constantAttenuation;
float linearAttenuation;
float quadraticAttenuation;
float padding3; // Padding to ensure the structure is a multiple of 16 bytes.
float intensity;
};
Texture2D shaderTexture;
SamplerState SampleType;
cbuffer SunLightColorBuffer
{
float4 sunColor;
};
//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
float2 tex : TEXCOORD0;
float3 worldPos : TEXCOORD1; // Add world position
float3 normal : NORMAL;
};
float4 CelShadingPixelShader(PixelInputType input) : SV_TARGET
////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 SunLightPixelShader(PixelInputType input) : SV_TARGET
{
// Normalize the normal
float3 normal = normalize(input.normal);
float4 textureColor;
float4 color;
float lightIntensity;
float4 colorArray;
float4 colorSum;
// Convert the normal to a color
float4 color = float4((normal + 1.0f) * 0.5f, 1.0f);
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
textureColor = shaderTexture.Sample(SampleType, input.tex);
// Calculate the different amounts of light on this pixel based on the direction of the light.
lightIntensity = saturate(dot(input.normal, -lightDirection));
if(lightIntensity > 0.95f)
{
lightIntensity = 1.0f;
}
else if(lightIntensity > 0.5f)
{
lightIntensity = 0.75f;
}
else if(lightIntensity > 0.25f)
{
lightIntensity = 0.5f;
}
else
{
lightIntensity = 0.25f;
}
// Determine the diffuse color amount of the light.
colorArray = (diffuseColor * lightIntensity) * intensity;
// Initialize the sum of colors.
colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);
// Add the light color.
colorSum.r += colorArray.r;
colorSum.g += colorArray.g;
colorSum.b += colorArray.b;
// Multiply the texture pixel by the light color to get the final result.
color = saturate(colorSum) * textureColor;
return color;
}

View File

@@ -1,3 +1,6 @@
/////////////
// GLOBALS //
/////////////
cbuffer MatrixBuffer
{
matrix worldMatrix;
@@ -5,22 +8,42 @@ cbuffer MatrixBuffer
matrix projectionMatrix;
};
cbuffer CameraBuffer
{
float3 cameraPosition;
float padding;
};
cbuffer SunLightBuffer
{
float4 ambientColor; // Color of the ambient light.
float4 diffuseColor; // Color of the diffuse light.
float3 lightDirection; // Direction of the light.
float intensity; // Intensity of the light.
};
//////////////
// TYPEDEFS //
//////////////
struct VertexInputType
{
float4 position : POSITION;
float3 normal : NORMAL;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
};
struct PixelInputType
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
float2 tex : TEXCOORD0;
float3 worldPos : TEXCOORD1; // Add world position
float3 normal : NORMAL;
};
PixelInputType CelShadingVertexShader(VertexInputType input)
////////////////////////////////////////////////////////////////////////////////
// Vertex Shader
////////////////////////////////////////////////////////////////////////////////
PixelInputType SunLightVertexShader(VertexInputType input)
{
PixelInputType output;
@@ -28,18 +51,18 @@ PixelInputType CelShadingVertexShader(VertexInputType input)
input.position.w = 1.0f;
// Calculate the position of the vertex against the world, view, and projection matrices.
float4 worldPosition = mul(input.position, worldMatrix);
output.position = mul(worldPosition, viewMatrix);
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
// Pass the normal to the pixel shader
output.normal = mul((float3x3)worldMatrix, input.normal);
// Pass the world position to the pixel shader
output.worldPos = worldPosition.xyz;
// 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);
// Normalize the normal vector.
output.normal = normalize(output.normal);
return output;
}