Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to add TRANSPARENCY to Skybox>Cubemap

Discussion in 'Shaders' started by JoRouss, May 1, 2015.

  1. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    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.


    Code (CSharp):
    1. Shader "Skybox/CubemapTransparent" {
    2. Properties {
    3.     _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    4.     [Gamma] _Exposure ("Exposure", Range(0, 8)) = 1.0
    5.     _Rotation ("Rotation", Range(0, 360)) = 0
    6.     [NoScaleOffset] _Tex ("Cubemap   (HDR)", Cube) = "grey" {}
    7. }
    8.  
    9. SubShader {
    10.     Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
    11.     Cull Off ZWrite Off
    12.     Blend SrcAlpha OneMinusSrcAlpha
    13.  
    14.     Pass {
    15.      
    16.         CGPROGRAM
    17.         #pragma vertex vert
    18.         #pragma fragment frag
    19.  
    20.         #include "UnityCG.cginc"
    21.  
    22.         samplerCUBE _Tex;
    23.         half4 _Tex_HDR;
    24.         half4 _Tint;
    25.         half _Exposure;
    26.         float _Rotation;
    27.  
    28.         float4 RotateAroundYInDegrees (float4 vertex, float degrees)
    29.         {
    30.             float alpha = degrees * UNITY_PI / 180.0;
    31.             float sina, cosa;
    32.             sincos(alpha, sina, cosa);
    33.             float2x2 m = float2x2(cosa, -sina, sina, cosa);
    34.             return float4(mul(m, vertex.xz), vertex.yw).xzyw;
    35.         }
    36.      
    37.         struct appdata_t {
    38.             float4 vertex : POSITION;
    39.         };
    40.  
    41.         struct v2f {
    42.             float4 vertex : SV_POSITION;
    43.             float3 texcoord : TEXCOORD0;
    44.         };
    45.  
    46.         v2f vert (appdata_t v)
    47.         {
    48.             v2f o;
    49.             o.vertex = mul(UNITY_MATRIX_MVP, RotateAroundYInDegrees(v.vertex, _Rotation));
    50.             o.texcoord = v.vertex;
    51.             return o;
    52.         }
    53.  
    54.         fixed4 frag (v2f i) : SV_Target
    55.         {
    56.             half4 tex = texCUBE (_Tex, i.texcoord);
    57.             half3 c = DecodeHDR (tex, _Tex_HDR);
    58.             c = c * _Tint.rgb * unity_ColorSpaceDouble;
    59.             c *= _Exposure;
    60.             return half4(c, 1);
    61.         }
    62.         ENDCG
    63.     }
    64. }  
    65.  
    66.  
    67. Fallback Off
    68.  
    69. }
    70.  


     
  2. alienheretic

    alienheretic

    Joined:
    Oct 15, 2008
    Posts:
    60
    works fine for me just change return half4(c, 1); to return half4 (c,YOUR ALPHA VALUE);
     
    Roman_Keivan and JoRouss like this.
  3. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    MY HERO <3
     
  4. Feinoko

    Feinoko

    Joined:
    Sep 2, 2015
    Posts:
    4
    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 :(

    Any ideas as to why that may be?

    Cheers,
    Jonathan
     
  5. Feinoko

    Feinoko

    Joined:
    Sep 2, 2015
    Posts:
    4
    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 :)

    Jonathan
     
  6. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    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". :)
     
  7. Feinoko

    Feinoko

    Joined:
    Sep 2, 2015
    Posts:
    4
    Nice :)
    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... :eek:
    Cheers
     
  8. Wompipomp

    Wompipomp

    Joined:
    Aug 31, 2014
    Posts:
    24
    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.
     
  9. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    I made a curve element, my skybox transparency and my global illumination follow this curve.

     
  10. Roman_Keivan

    Roman_Keivan

    Joined:
    May 31, 2019
    Posts:
    21
    Awesome