Search Unity

Problem with custom skybox when there is no cubemap

Discussion in 'Shaders' started by Demonarchy, Dec 19, 2020.

  1. Demonarchy

    Demonarchy

    Joined:
    Apr 19, 2018
    Posts:
    2
    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:

    Code (CSharp):
    1. Shader "Example"
    2. {
    3.     Properties
    4.     {
    5.         _StarsCubemap ("Stars Cubemap", Cube) = "black" {}
    6.         _StarsColor("Stars Color", Color) = (1, 1, 1, 1)
    7.         _SkyboxColor("Skybox Color", Color) = (0, 0, 0, 0)
    8.     }
    9.     SubShader
    10.     {
    11.         Tags
    12.         {
    13.             "PreviewType" = "Skybox"
    14.             "Queue" = "Background"
    15.             "RenderType" = "Background"
    16.         }
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 uv0 : TEXCOORD0;
    28.                 float4 vertex : POSITION;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float4 uv0 : TEXCOORD0;
    34.                 float4 vertex : SV_POSITION;
    35.             };
    36.  
    37.             samplerCUBE _StarsCubemap;
    38.             half4 _StarsColor;
    39.             half4 _SkyboxColor;
    40.  
    41.             v2f vert(appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv0 = v.uv0;
    46.                 return o;
    47.             }
    48.  
    49.             fixed4 frag(v2f i) : SV_Target
    50.             {
    51.                 half4 starsMask = texCUBE(_StarsCubemap, i.uv0.xyz);
    52.                 half4 finalColor = lerp(_SkyboxColor, _StarsColor, starsMask.r); // Using red channel as a mask
    53.                 return finalColor;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    59.  
     
    Last edited: Dec 20, 2020
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,349
    Unfortunately I believe the only default cubemap textures Unity provides as a default is that middle grey texture.

    https://docs.unity3d.com/Manual/SL-Properties.html
    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.
     
    Demonarchy likes this.