Problem with custom skybox when there is no cubemap

I’m using a cubemap with stars (one layer per channel) as a mask over the skybox, tinting both skybox and stars with a separate color.
The shader works fine when there is a cubemap reference but when there is none the cubemap reads a 0.5 grey affecting the skybox color.
I’ve already tried to set its default value to black with no luck.
Any ideas on how to approach this issue?

Here’s a simplified version of the shader:

Shader "Example"
{
    Properties
    {
        _StarsCubemap ("Stars Cubemap", Cube) = "black" {}
        _StarsColor("Stars Color", Color) = (1, 1, 1, 1)
        _SkyboxColor("Skybox Color", Color) = (0, 0, 0, 0)
    }
    SubShader
    {
        Tags
        {
            "PreviewType" = "Skybox"
            "Queue" = "Background"
            "RenderType" = "Background"
        }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 uv0 : TEXCOORD0;
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 uv0 : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            samplerCUBE _StarsCubemap;
            half4 _StarsColor;
            half4 _SkyboxColor;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv0 = v.uv0;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                half4 starsMask = texCUBE(_StarsCubemap, i.uv0.xyz);
                half4 finalColor = lerp(_SkyboxColor, _StarsColor, starsMask.r); // Using red channel as a mask
                return finalColor;
            }
            ENDCG
        }
    }
}

Unfortunately I believe the only default cubemap textures Unity provides as a default is that middle grey texture.

There are two main options to solve this. One is provide your own all black cubemap texture. The other is to have a material property you use to switch the cubemap off.

1 Like