Search Unity

How to make a planet look like its outside atmosphere

Discussion in 'Shaders' started by Inferi, Oct 16, 2016.

  1. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
    I would like to make it so my planet looks like it is outside the atmosphere. I use Procedual skybox.

    My planet has a material with standard shader on it looks to "clean". I would like to have that faded look "almost transparent" Colors should be less clear at day.

    I tried using the transparent shaders but that also means that the sun shines through and that is not what I want.

    An example of what I want is in this image: http://www.dailygalaxy.com/photos/uncategorized/2008/04/24/sunset_moon.jpg

    And at night when the sun is not shining on the planet it should be dark or total black.

    How would I do that? , Anyone have some ideas?
     
  2. wetcircuit

    wetcircuit

    Joined:
    Jul 17, 2012
    Posts:
    1,409
  3. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    A trick I used was to reuse the default reflection probe cubemap as a "skybox color lookup" and then rendering my sky objects that additively blend against that, but use alpha blending when drawing to the screen. It means each object will accurately occlude each other if drawn in the correct order, and if you're using the built in procedural skybox the default reflection probe doesn't include the sun so it'll occlude that as well.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    skyObjectShaderExample.png
    Code (CSharp):
    1.  
    2. Shader "Unlit/SkyObject"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "Queue"="Transparent-498" "RenderType"="Transparent" }
    11.         LOD 100
    12.         Blend One OneMinusSrcAlpha // premultiplied alpha
    13.         ZWrite off
    14.         Cull Off
    15.  
    16.         Pass
    17.         {
    18.             CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.             #pragma target 3.0
    22.          
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float4 pos : SV_POSITION;
    34.                 float2 uv : TEXCOORD0;
    35.                 half3 sky : TEXCOORD1;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             float4 _MainTex_ST;
    40.          
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.pos = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    46.                 o.sky = mul(unity_ObjectToWorld, float4(v.vertex.xyz,1)).xyz - _WorldSpaceCameraPos.xyz;
    47.                 return o;
    48.             }
    49.  
    50.             half3 sampleSky(UNITY_ARGS_TEXCUBE(tex), float4 hdr, half3 uvw)
    51.             {
    52.                 fixed4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, uvw, 0);
    53.                 return DecodeHDR(rgbm, hdr);
    54.             }
    55.          
    56.             fixed4 frag (v2f i) : SV_Target
    57.             {
    58.                 // calculate soft and hard horizon edge
    59.                 fixed2 horizon = saturate(i.sky.yy * float2(10.0, 100.0));
    60.  
    61.                 // main texture should be using premultipled alpha
    62.                 fixed4 col = tex2D(_MainTex, i.uv);
    63.  
    64.                 // fade
    65.                 col *= horizon.xxxy;
    66.  
    67.                 // sample default spec cubemap
    68.                 half3 sky = sampleSky(UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, i.sky) * col.a;
    69.                 sky += col.rgb;
    70.  
    71.                 return float4(sky.xyz, col.a);
    72.             }
    73.             ENDCG
    74.         }
    75.     }
    76. }
    skyObject.png

    This isn't a perfect example, as it doesn't move the object to ensure it doesn't clip.
     
    Last edited: Oct 16, 2016
    GreedyVox and Inferi like this.
  6. Inferi

    Inferi

    Joined:
    Sep 28, 2015
    Posts:
    145

    Thank you very much for the help, but this shader seems to only show the texture without shadows.

    I am using a sphere and I need it to get darker at night and in day the atmosphere is giving it a washed out look.

    I was starting to look into shaders and am now following a tutorial about it, so maybe I can make a shader for it someday :)

    I am right now testing "Legacy Shaders/Reflective/Bumped Diffuse" shader. In day it will have a white color in reflective and at night it will be black. then I get the look I am after I think. I only need to figure out the color lerp and then it should be as I want.

    It would be great if a shader could do this, but I have no idea.

    Example with "Legacy Shaders/Reflective/Bumped Diffuse": Night & Day look
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Inferi - Correct, that shader is just a proof of concept on how to go about the effect, not necessarily something production ready.
     
    Inferi likes this.