Cubemap
What is a Cubemap
A cubemap is a special texture consisting of six independent 2D textures, corresponding to the six faces of a cube: positive X, negative X, positive Y, negative Y, positive Z, and negative Z. Together, these six faces represent a panoramic image observed from the center of the cube in all directions.
In shaders, a cubemap is sampled using a three-dimensional direction vector. The GPU selects the appropriate texture face based on the direction vector and performs interpolation.
Uses of Cubemaps
- **Skybox**: Renders the scene background, simulating distant environments.
- **Environment Mapping**: Used for reflection and refraction effects, simulating reflections of the surrounding environment on object surfaces.
- **Image-Based Lighting (IBL)**: Stores precomputed environment lighting information for PBR materials.
- **Dynamic Reflections**: Can render the scene to a cubemap in real time for dynamic reflections.
- **Light Probes**: Captures ambient lighting at specific locations for indirect lighting on dynamic objects.
Creating Cubemaps
Cubemaps can be created in the following ways:
- **Static Images**: Use 6 square textures, each corresponding to a face, import into the engine and set as a cubemap.
- **Panorama Conversion**: Convert an equirectangular panorama image to a cubemap.
- **Real-time Rendering**: Place a camera in the scene, render six directions to render targets, generating a dynamic cubemap.
- **Generate HDR Environment Maps**: Use tools like Substance or Blender for baking.
Sampling Cubemaps
In shaders, use `samplerCUBE` (or `TextureCube`) for sampling. Sampling requires a 3D direction vector that points from the cubemap center in the sampling direction. The GPU finds the cube face intersected by that direction and returns the color at the corresponding texture coordinate.
Cubemaps and Mipmaps
Cubemaps support mipmap chains, each mip level is a downscaled version of the original. In reflections and IBL, roughness is often used to select the mip level, simulating blurred reflections on surfaces with different roughness.
Considerations for Cubemaps
- **Seams**: If the edges of the six faces are inconsistent, visible seams may appear. Ensure texture edges are seamless.
- **Orientation Conventions**: Different engines may have different conventions for cubemap face layout and orientation (e.g., Y-axis flip). Pay attention when importing.
- **Memory Usage**: A cubemap contains six faces, so memory is 6 times that of a normal 2D texture. Optimize as needed.
- **Performance**: Dynamically generating cubemaps requires rendering the scene six times, which is costly; use with caution.
Conclusion
Cubemaps are fundamental for environment effects and widely used in modern rendering pipelines.
FAQ
What is the difference between a cubemap and a 2D texture?
2D textures are sampled using UV coordinates, while cubemaps are sampled using 3D direction vectors, allowing representation of a full environment. A cubemap is composed of six 2D textures.
How to generate a cubemap?
Use 6 square images, convert from a panorama, or render the scene to six faces in real time. Many engines provide tools for automatic generation.
How are cubemaps used in PBR?
In Image-Based Lighting (IBL), cubemaps store environmental radiance, used to compute indirect diffuse and specular reflections. Pre-filtered mipmaps are often used to approximate reflections at different roughness levels.
Why do cubemaps have seams?
Seams usually occur because the six face textures are not continuous at edges, or filtering is inconsistent during sampling. Solutions include using seamless textures and enabling seamless cubemap sampling if hardware supports it.
How to optimize cubemap memory?
Reduce resolution, use compressed formats (like BC6H for HDR), store only necessary mip levels. On mobile, consider using pre-convolved irradiance maps and pre-filtered environment maps.