Texture Mapping
Texture mapping is a fundamental technique in computer graphics used to add detail, realism, and visual complexity to 3D models without requiring them to have a very high polygon count. It involves applying a 2D image (the texture) onto the surface of a 3D object.
Core Concepts
The process of texture mapping involves several key components:
- Texture: A 2D image (often a bitmap) that contains the pattern or detail to be applied.
- UV Coordinates: A 2D coordinate system (U and V, analogous to X and Y) that maps points on the 2D texture to points on the 3D surface of the model. Each vertex of a 3D model typically has associated UV coordinates.
- Texture Coordinates Generation: Methods for automatically generating UV coordinates, especially for procedural texturing or complex surfaces.
- Filtering: Techniques used to interpolate texture colors when a texel (texture pixel) doesn't directly map to a screen pixel, preventing aliasing and shimmering. Common methods include nearest-neighbor, bilinear filtering, and trilinear filtering.
- Mipmapping: A technique where pre-calculated, downscaled versions of a texture are stored. The system selects the appropriate mipmap level based on the distance of the object from the camera, significantly improving performance and visual quality by reducing aliasing and texture cache misses.
Texture Coordinates (UV Mapping)
UV coordinates are crucial for defining how a texture wraps around a 3D model. The U coordinate typically represents the horizontal axis of the texture, and the V coordinate represents the vertical axis. A point with UV coordinates (0,0) usually corresponds to the bottom-left corner of the texture, while (1,1) corresponds to the top-right corner. These coordinates are typically assigned to each vertex of the 3D model.
Texture Filtering
When rendering a 3D scene, the relationship between the size of a texel and the size of the pixel it covers on screen can vary greatly. Texture filtering techniques smooth out these transitions.
- Nearest Neighbor: The color of the nearest texel is used. Fast but results in blocky artifacts.
- Bilinear Filtering: The colors of the four nearest texels are linearly interpolated based on the texture coordinates. Provides smoother results than nearest neighbor.
- Trilinear Filtering: Extends bilinear filtering by interpolating between two mipmap levels. This offers superior smoothness and reduces shimmering when the texture is viewed at oblique angles or from a distance.
Mipmapping
Mipmaps (from the Latin 'multum in parvo', meaning 'much in little') are essential for efficient and high-quality texture rendering. They are a set of pre-filtered, reduced-resolution versions of an original texture. When a rendered object is far away, the GPU can use a smaller mipmap level, which requires less memory bandwidth and processing power, while also preventing aliasing artifacts that would occur if a high-resolution texture were heavily downsampled on the fly.
Shader Implementation
In modern graphics APIs like DirectX or Vulkan, texture mapping is primarily handled within shaders. A pixel shader typically samples the texture using the provided UV coordinates and returns the color to be applied to the pixel.
Example Pixel Shader Snippet (HLSL-like)
struct PixelShaderInput {
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD0;
};
Texture2D g_Texture;
SamplerState g_SamplerState;
float4 main(PixelShaderInput input) : SV_TARGET {
// Sample the texture using the interpolated UV coordinates
float4 textureColor = g_Texture.Sample(g_SamplerState, input.TexCoord);
// Return the sampled color
return textureColor;
}
Common Texture Mapping Techniques
- Diffuses/Albedo Mapping: Applying a color map to define the base color of a surface.
- Normal Mapping: Using a texture to store surface normal information, faking complex surface details and dramatically improving perceived geometry without adding polygons.
- Specular Mapping: Controlling the intensity and color of specular highlights.
- Roughness/Metallic Mapping: Defining how light reflects off a surface based on its physical properties (PBR).
- Emissive Mapping: Defining areas of a surface that emit their own light.
- Parallax Mapping / Height Mapping: Techniques that simulate depth and self-shadowing by displacing texture coordinates, creating a more convincing illusion of geometric detail.