55 lines
1.5 KiB
PostScript
55 lines
1.5 KiB
PostScript
cbuffer LightBuffer
|
|
{
|
|
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.
|
|
};
|
|
|
|
Texture2D shaderTexture;
|
|
SamplerState SampleType;
|
|
|
|
struct PixelInputType
|
|
{
|
|
float4 position : SV_POSITION;
|
|
float3 normal : NORMAL;
|
|
float2 tex : TEXCOORD0;
|
|
float3 worldPos : TEXCOORD1; // Add world position
|
|
};
|
|
|
|
float4 CelShadingPixelShader(PixelInputType input) : SV_TARGET
|
|
{
|
|
float4 textureColor;
|
|
float lightIntensity;
|
|
float4 finalColor;
|
|
|
|
// 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 intensity based on the light direction.
|
|
lightIntensity = saturate(dot(normal, lightVector));
|
|
|
|
// Apply a step function to create the cel shading effect.
|
|
if (lightIntensity > 0.5f)
|
|
{
|
|
lightIntensity = 1.0f;
|
|
}
|
|
else
|
|
{
|
|
lightIntensity = 0.3f;
|
|
}
|
|
|
|
// Calculate the final color by combining the texture color with the light intensity and diffuse color.
|
|
finalColor = textureColor * diffuseColor * lightIntensity;
|
|
|
|
return finalColor;
|
|
} |