Shading Models
Shading models define how the color of a surface is calculated based on its material properties, light sources, and the viewer's perspective. They are fundamental to creating realistic or stylized visuals in computer graphics.
Introduction to Shading
In rendering, shading is the process of determining the final color of each pixel on a 3D surface. This involves simulating how light interacts with the surface. Different shading models offer varying levels of complexity and realism.
Types of Shading Models
1. Flat Shading
Flat shading calculates a single color for each primitive (e.g., triangle) based on the surface normal and a light source. This results in a faceted appearance, where each polygon is rendered with a uniform color. It's computationally inexpensive but lacks smooth transitions.
Flat Shading Example
Note the distinct color bands for each polygon.
2. Gouraud Shading
Gouraud shading, also known as intensity interpolation shading, calculates the lighting at the vertices of a primitive and then interpolates these color values across the primitive's surface. This results in smoother shading than flat shading, with color gradients, but can suffer from specular aliasing and doesn't accurately represent specular highlights across large polygons.
Gouraud Shading Example
Notice the smoother transitions compared to flat shading.
3. Phong Shading
Phong shading interpolates the surface normal across the primitive and then calculates the lighting equation at each pixel using the interpolated normal. This provides more accurate specular highlights and smoother gradients than Gouraud shading, leading to a more realistic appearance. However, it is computationally more expensive.
Phong Shading Example
Observe the smooth specular highlights and overall realism.
4. Physically Based Rendering (PBR) Shading
Modern graphics often employ Physically Based Rendering (PBR) models. PBR aims to simulate light transport more accurately by taking into account the physical properties of materials (e.g., albedo, roughness, metallicness). This leads to highly realistic and consistent results across different lighting conditions.
Common PBR models include:
- Cook-Torrance: A widely used microfacet-based BRDF model.
- Disney Principled BRDF: A simplified yet effective model used in many applications.
PBR shaders typically use a set of material parameters to define how light interacts with a surface:
- Albedo: The base color of the surface under diffuse lighting.
- Metallic: Indicates how much of the surface's reflectivity is metallic (0 for non-metals, 1 for metals).
- Roughness: Controls the microfacets on the surface, affecting the sharpness of reflections and specular highlights.
- Specular: (Often derived from Metallic) Defines the intensity of specular reflections for non-metals.
- Ambient Occlusion (AO): Simulates how much ambient light is blocked by nearby geometry.
PBR Shading Example
PBR yields highly realistic material responses to light.
Implementation in DirectX
In DirectX, shading is primarily handled by pixel shaders (also known as fragment shaders). The choice of shading model dictates the complexity and calculations performed within the pixel shader. Modern DirectX versions utilize HLSL (High-Level Shading Language) to write these shaders.
A basic Phong shading model in HLSL might involve calculating:
float4 PSMain(PS_INPUT input) : SV_TARGET
{
// Interpolated normal, position, etc. from vertex shader
float3 normal = normalize(input.normal);
float3 viewDir = normalize(cameraPosition - input.worldPos);
float3 lightDir = normalize(lightPosition - input.worldPos);
// Ambient, Diffuse, and Specular components
float3 ambient = ambientColor * material.diffuse;
float3 diffuse = material.diffuse * lightColor * saturate(dot(normal, lightDir));
float3 reflectDir = reflect(-lightDir, normal);
float specularFactor = pow(saturate(dot(viewDir, reflectDir)), material.shininess);
float3 specular = lightColor * material.specular * specularFactor;
float3 finalColor = ambient + diffuse + specular;
return float4(finalColor, 1.0f);
}
PBR implementations are more complex, often involving microfacet models and energy conservation principles to achieve accurate results.
Choosing the Right Shading Model
The choice of shading model depends on the desired visual quality, performance constraints, and the target platform. For real-time applications, performance is a key consideration, while for offline rendering, accuracy and realism are paramount.