Specular Lighting
This commit is contained in:
@@ -14,6 +14,8 @@ cbuffer LightBuffer
|
||||
float4 diffuseColor;
|
||||
float3 lightDirection;
|
||||
float padding;
|
||||
float specularPower;
|
||||
float4 specularColor;
|
||||
};
|
||||
|
||||
|
||||
@@ -25,6 +27,7 @@ struct PixelInputType
|
||||
float4 position : SV_POSITION;
|
||||
float2 tex : TEXCOORD0;
|
||||
float3 normal : NORMAL;
|
||||
float3 viewDirection : TEXCOORD1;
|
||||
};
|
||||
|
||||
|
||||
@@ -37,6 +40,8 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
||||
float3 lightDir;
|
||||
float lightIntensity;
|
||||
float4 color;
|
||||
float3 reflection;
|
||||
float4 specular;
|
||||
|
||||
|
||||
// Sample the pixel color from the texture using the sampler at this texture coordinate location.
|
||||
@@ -45,6 +50,10 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
||||
// Set the default output color to the ambient light value for all pixels.
|
||||
color = ambientColor;
|
||||
|
||||
// Initialize the specular color.
|
||||
specular = float4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||
|
||||
|
||||
// Invert the light direction for calculations.
|
||||
lightDir = -lightDirection;
|
||||
|
||||
@@ -55,13 +64,22 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET
|
||||
{
|
||||
// Determine the final diffuse color based on the diffuse color and the amount of light intensity.
|
||||
color += (diffuseColor * lightIntensity);
|
||||
}
|
||||
|
||||
// Saturate the final light color.
|
||||
color = saturate(color);
|
||||
// Saturate the ambient and diffuse color.
|
||||
color = saturate(color);
|
||||
|
||||
// Calculate the reflection vector based on the light intensity, normal vector, and light direction.
|
||||
reflection = normalize(2.0f * lightIntensity * input.normal - lightDir);
|
||||
|
||||
// Determine the amount of specular light based on the reflection vector, viewing direction, and specular power.
|
||||
specular = pow(saturate(dot(reflection, input.viewDirection)), specularPower);
|
||||
}
|
||||
|
||||
// Multiply the texture pixel and the final diffuse color to get the final pixel color result.
|
||||
color = color * textureColor;
|
||||
|
||||
// Add the specular component last to the output color.
|
||||
color = saturate(color + specular);
|
||||
|
||||
return color;
|
||||
}
|
||||
Reference in New Issue
Block a user