Skip to content
On this page

Cubemaps

A cubemap is six square textures glued to the faces of an invisible cube, and you sample it with a direction instead of a UV. One vector, and the answer is whatever color sits on the cube’s surface where that vector points. It is the natural texture for anything that lives on a sphere: skies, reflections, and the inside of rooms.

TEXTURECUBE(_CubeMap);   SAMPLER(sampler_CubeMap);

half4 tex = SAMPLE_TEXTURECUBE(_CubeMap, sampler_CubeMap, dir);

Six faces, one sphere of data

Each face of the cube stores one square view of the surrounding world, and the six together tile a sphere without the pinch of a single 2D map. The sample does not take a UV. It takes a normalized 3D direction, and the hardware routes it to the correct face and interpolates inside it, so you think in terms of “what is the color of the sky in that direction” and the cube answers.

That is exactly what reflections need. A surface does not reflect a UV. It reflects a direction, the view direction mirrored around the surface normal. Hand that reflected direction to the cubemap and you get the color the surface should show, which is the entire classic cube reflection in one sample.

half3 view  = normalize(GetWorldSpaceViewDir(positionWS));
half3 refl  = reflect(-view, normalWS);
half3 sky   = SAMPLE_TEXTURECUBE(_CubeMap, sampler_CubeMap, refl).rgb;

Shader Story: Textures - Cubemaps

Where cubemaps show up

Three roles dominate. Skies: the cube is sampled with the raw view direction and rendered on a box around the world, the classic skybox. Environment reflections: the reflected direction sample above, the cheapest convincing metal and water in the library. Environment lighting: integrating the cube over the hemisphere around a normal gives ambient light, which is the cubemap underpinning of Image Based Lighting.

It is worth noting what the neighboring chapters do differently on purpose. Matcap and IBL in this library use 2D maps because a 2D lookup is cheaper and easier to art direct, trading the cube’s correctness for the artist’s control. The cube is the honest version. The 2D maps are the stylized one. Knowing both exist is the whole point.

Cubemaps also have a mip chain, one per face, and the sampler picks levels the same way the 2D rules in Filtering & Mipmaps do, so a distant reflection blurs and stabilizes the same way.

One last role, and it is the most gamey. Interior mapping, the next door neighbor in Projections, turns the cube inside out and samples it from within to fake the rooms behind a window. Same asset type, opposite direction, very different trick.


What the shader does

The shader samples a cubemap by direction and applies a Fresnel falloff, so the reflection reads strongest at the silhouette. The geometry stays still while the environment slides across the surface with the view angle.

Shader Story: Textures: Cubemaps - schematic

Illustrative schematic: Reflection slides across the surface with the view angle.


View direction, reflect, sample, and a Fresnel edge so the sky appears where it should. The Fresnel chapter is the reference for that last step.

Shader code
Shader "DecompiledArt/Textures/Basics/Cubemaps/Cubemaps"
{
    Properties
    {
        _CubeMap("Cube Map", Cube) = "_Skybox" {}
        [MainColor] _BaseColor("Base Color", Color) = (0.2, 0.3, 0.4, 1)
        _ReflectStrength("Reflection Strength", Range(0, 1)) = 1
        _Power("Fresnel Power", Range(1, 8)) = 3
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }

        Pass
        {
            Name "ForwardOnly"
            Tags { "LightMode" = "UniversalForward" }

            HLSLPROGRAM
            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            TEXTURECUBE(_CubeMap);   SAMPLER(sampler_CubeMap);

            struct Attributes
            {
                float4 positionOS : POSITION;
                float3 normalOS   : NORMAL;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float3 positionWS : TEXCOORD0;
                half3  normalWS   : TEXCOORD1;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                half  _ReflectStrength;
                half  _Power;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
                OUT.normalWS   = TransformObjectToWorldNormal(IN.normalOS);
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                half3 n     = normalize(IN.normalWS);
                half3 view  = normalize(GetWorldSpaceViewDir(IN.positionWS));
                half3 refl  = reflect(-view, n);

                half3 sky = SAMPLE_TEXTURECUBE(_CubeMap, sampler_CubeMap, refl).rgb;

                half fresnel = pow(1.0h - saturate(dot(n, -view)), _Power);
                half3 color = _BaseColor.rgb + sky * fresnel * _ReflectStrength;
                return half4(color, _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

Interior Mapping • Image Based Lighting • Fresnel

Related functions: Dot • Normalize


❤️ Support Shader Story

If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

Your support helps keep this library open and free for everyone.