How to add TRANSPARENCY to Skybox>Cubemap

Hi, I’m trying to add a transparency level to the Skybox>Cubemap shader

Right now, the
_Tint (“Tint Color”, Color) = (.5, .5, .5, .5)
alpha level does not change anything. I’d like it to actually change the transparency level of the material.

I’m using it to add a second “fake” skybox to my scene, so I can simulate a “blending” between the new procedural skybox and a “stars” skybox at night.

I tryed some things, but with no succes. I’m a complete beginner in Shaders.

Shader "Skybox/CubemapTransparent" {
Properties {
    _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
    _Rotation ("Rotation", Range(0, 360)) = 0
    [NoScaleOffset] _Tex ("Cubemap   (HDR)", Cube) = "grey" {}
}

SubShader {
    Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    Cull Off ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass {
      
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        #include "UnityCG.cginc"

        samplerCUBE _Tex;
        half4 _Tex_HDR;
        half4 _Tint;
        half _Exposure;
        float _Rotation;

        float4 RotateAroundYInDegrees (float4 vertex, float degrees)
        {
            float alpha = degrees * UNITY_PI / 180.0;
            float sina, cosa;
            sincos(alpha, sina, cosa);
            float2x2 m = float2x2(cosa, -sina, sina, cosa);
            return float4(mul(m, vertex.xz), vertex.yw).xzyw;
        }
      
        struct appdata_t {
            float4 vertex : POSITION;
        };

        struct v2f {
            float4 vertex : SV_POSITION;
            float3 texcoord : TEXCOORD0;
        };

        v2f vert (appdata_t v)
        {
            v2f o;
            o.vertex = mul(UNITY_MATRIX_MVP, RotateAroundYInDegrees(v.vertex, _Rotation));
            o.texcoord = v.vertex;
            return o;
        }

        fixed4 frag (v2f i) : SV_Target
        {
            half4 tex = texCUBE (_Tex, i.texcoord);
            half3 c = DecodeHDR (tex, _Tex_HDR);
            c = c * _Tint.rgb * unity_ColorSpaceDouble;
            c *= _Exposure;
            return half4(c, 1);
        }
        ENDCG
    }
}   


Fallback Off

}

1 Like

works fine for me just change return half4(c, 1); to return half4 (c,YOUR ALPHA VALUE);

3 Likes

MY HERO <3

Hello,

Many thanks for this, it is very useful!

However I seem to have a problem, my cubemap will be perfectly visible in scene view, but won’t render at runtime:

I’m desperate :frowning:

Any ideas as to why that may be?

Cheers,
Jonathan

Solved above issue by adding a separate camera that renders the skybox cube only by culling everything except that layer. I hd to set flags to “Don’t clear” also.

Anyway, thank you so much for raising this issue and solving it, it really add a nice touch being able to blend in procedural with cubemap skyboxes :slight_smile:

Jonathan

I did the same, I have 3 cameras for my FPS game. 1 short distance for the player arms, so they dont clip through walls, one for everything but the sky, and one for the sky cubemap and the clear flag “Skybox”. :slight_smile:

Nice :slight_smile:
Do you know how to progressively crank up transparency level of your “CubemapTransparent” shader script at runtime?
I’m trying to get the effect of night skybox progressively settling in as the sun goes below horizon… :o
Cheers

Some time ago I also made a Day/Night Cycle with sun and stars. I added my own time of day and a property where I can get the normalized Time of day => -1 for night and 1 for day (as ping pong). I then added an animationcurve with a smooth transition from zero to 1 at around x=0 and put this in as alpha for the skybox.

I made a curve element, my skybox transparency and my global illumination follow this curve.

Awesome

Here’s the complete shader with the fix, thanks JoRouss and alienheretic

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Skybox/CubemapTransparent" {
Properties {
    _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
    _Rotation ("Rotation", Range(0, 360)) = 0
    [NoScaleOffset] _Tex ("Cubemap   (HDR)", Cube) = "grey" {}
    _Transparency ("Transparency", Range(0,1)) = 0.0
}
SubShader {
    Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    Cull Off ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    Pass {
    
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"
        samplerCUBE _Tex;
        half4 _Tex_HDR;
        half4 _Tint;
        half _Exposure;
        float _Rotation;
        float _Transparency;
        float4 RotateAroundYInDegrees (float4 vertex, float degrees)
        {
            float alpha = degrees * UNITY_PI / 180.0;
            float sina, cosa;
            sincos(alpha, sina, cosa);
            float2x2 m = float2x2(cosa, -sina, sina, cosa);
            return float4(mul(m, vertex.xz), vertex.yw).xzyw;
        }
    
        struct appdata_t {
            float4 vertex : POSITION;
        };
        struct v2f {
            float4 vertex : SV_POSITION;
            float3 texcoord : TEXCOORD0;
        };
        v2f vert (appdata_t v)
        {
            v2f o;
            o.vertex = UnityObjectToClipPos(RotateAroundYInDegrees(v.vertex, _Rotation));
            o.texcoord = v.vertex;
            return o;
        }
        fixed4 frag (v2f i) : SV_Target
        {
            half4 tex = texCUBE (_Tex, i.texcoord);
            half3 c = DecodeHDR (tex, _Tex_HDR);
            c = c * _Tint.rgb * unity_ColorSpaceDouble;
            c *= _Exposure;
            return half4(c, _Transparency);
        }
        ENDCG
    }
} 
Fallback Off
}