Search Unity

How to add night sky using Unity 5 procedural skybox?

Discussion in 'General Graphics' started by JoRouss, Apr 28, 2015.

  1. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    I'm using Unity 5 and it's new procedural skybox. It looks amazing!

    In my game, I have a day/night cycle. I can't find a way to use a night skybox which will slowly appear (blend) to replace the procedural skybox when the night comes.

    I know a shader exists to blend between two skyboxes, but it won't work with the new procedural skybox.

    I want to be able to keep the procedural skybox, so I can keep my amaizing sunrise/sunset, but be able to add stars at night.





    Is there a way to replace the "Black" color at night with a standard skybox? An image?
    Should I add a huge sphere to my game to act like a skybox and make it transparent on day time?

    I really have no idea how to achieve that. There must be a way to change the night sky...
     
    Last edited: Apr 29, 2015
  2. KingMatthew

    KingMatthew

    Joined:
    Jul 7, 2013
    Posts:
    166
    If you just want to add stars then particles could work very nicely. Make a particle emitter emit in a circle from the edge, make the circle really big, and make the particles spawn a lot more often and live longer. You can attach the particles to the directional light and they rotate with your sun!
     
  3. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    Yes, I tought about that, but it wont appear if the particles are further than the camera clip distance. If I make it follow the position of the player, the particles will sometimes appear over walls or mountains far away.

    My goal is to add more complex sky, like the old skyboxes, at night.
     
  4. KingMatthew

    KingMatthew

    Joined:
    Jul 7, 2013
    Posts:
    166
    You could have another camera be only looking at the stars and put that camera on a different layer than the players. When you set it up the stars can be rendered in front of the skybox, but behind the everything else.
     
    JoRouss likes this.
  5. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    I tought that was impossible!!

    How can I do this, rendering
    1: procedural skybox
    2: Fake skybox (stars), moon, far space objects
    3: The world (terrain, etc.)
    4: arms

    I already have a camera for the player arms, which is drawn on top of the rest.

    Do I need 4 cameras now? I have a feeling that i'm gonna have fps issues... haha

    Or is there another magic way of doing this?

    Thank you
     
  6. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    Actually 3 cameras, number 1 and 2 can be rendered in the same camera I guess
     
  7. JoRouss

    JoRouss

    Joined:
    Apr 1, 2014
    Posts:
    53
    It seems to work!





    But I'm now facing a new problem..

    to simulate a skybox, I made a cube in blender, inverted the normals so I can see it from inside.
    I created a Cubemap and applyed it to my cube with the Skybox>Cubemap shader.

    I can't find a way to play with this object's transparency, to make it fully transparent at day time.

    Thanks for your help.
     
    Last edited: Apr 30, 2015
  8. JTAGames

    JTAGames

    Joined:
    Mar 7, 2019
    Posts:
    13
    Shader "Unlit/UnlitAlphaWithFade"
    {
    Properties
    {
    _Color("Color Tint", Color) = (1,1,1,1)
    _MainTex("Base (RGB) Alpha (A)", 2D) = "white"
    }
    Category
    {
    Lighting Off
    ZWrite Off
    //ZWrite On // uncomment if you have problems like the sprite disappear in some rotations.
    Cull back
    Blend SrcAlpha OneMinusSrcAlpha
    //AlphaTest Greater 0.001 // uncomment if you have problems like the sprites or 3d text have white quads instead of alpha pixels.
    Tags {Queue = Transparent}
    SubShader
    {
    Pass
    {
    SetTexture[_MainTex]
    {
    ConstantColor[_Color]
    Combine Texture * constant
    }
    }
    }
    }
    }
     
  9. JTAGames

    JTAGames

    Joined:
    Mar 7, 2019
    Posts:
    13
    This shader will give you an unlit material with an adjustable alpha
     
  10. Deleted User

    Deleted User

    Guest

    Thanks for sharing! :) Using code tags would make it easier to understand though:
    Code (CSharp):
    1. Shader "Unlit/UnlitAlphaWithFade"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.     }
    7.     SubShader
    8.     {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 100
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.             // make fog work
    18.             #pragma multi_compile_fog
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 UNITY_FOG_COORDS(1)
    32.                 float4 vertex : SV_POSITION;
    33.             };
    34.  
    35.             sampler2D _MainTex;
    36.             float4 _MainTex_ST;
    37.  
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = UnityObjectToClipPos(v.vertex);
    42.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    43.                 UNITY_TRANSFER_FOG(o,o.vertex);
    44.                 return o;
    45.             }
    46.  
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 // sample the texture
    50.                 fixed4 col = tex2D(_MainTex, i.uv);
    51.                 // apply fog
    52.                 UNITY_APPLY_FOG(i.fogCoord, col);
    53.                 return col;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
    Now, how do you use it? There is no accessible material for the procedural skybox; it seems to apply automatically if I drop it in the project but how do you control it?