Search Unity

Help with earth glow

Discussion in 'Shaders' started by Skullek, Aug 22, 2019.

  1. Skullek

    Skullek

    Joined:
    Mar 16, 2018
    Posts:
    22
    Hi
    I've got this Earth Shader (LWRP) - It is rotating, has some clouds. It's scaled but it is child of the camera (perspective) so it doesn't move with player. I would like the edges to glow.

    So I modified shader - add some code but glow depends on camera rotation / position. I don't want that.
    I'm not good with shaders so I don't know how to resolve it.

    Can you help me?

    I've got this:
    upload_2019-8-22_14-46-55.png

    Want something like this:
    upload_2019-8-22_14-56-1.png


    Code (CSharp):
    1. inline fixed3 AtmosphereColor(float3 worldSpaceDir)
    2.                 {
    3.                 fixed3 Fresnel1 = forwardVector;
    4.                 fixed3 Fresnel2 = (1.0 - dot(normalize(worldSpaceDir), normalize(Fresnel1))).xxx;
    5.                 fixed3 Pow0 = pow(Fresnel2, _AtmosFalloff);
    6.                 fixed3 Saturate0 = saturate(Pow0);
    7.                 fixed3 Lerp0 = lerp(_AtmosNear, _AtmosFar, Saturate0);
    8.                 fixed3 color = Lerp0 * Saturate0;
    9.                 return color;
    10.                 }
    11.  
    12.                 v2f vert(appdata v)
    13.                 {
    14.                     v2f o;
    15.                     UNITY_INITIALIZE_OUTPUT(v2f, o);
    16.                     o.pos = UnityObjectToClipPos(v.vertex);
    17.                     o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    18.                     o.normal = WorldSpaceNormal(v.normal);
    19.                     float dist0 = length(o.pos.xyz - _CameraPosition * float3(1.0f, 1.0f, 1.0f)) * .025f;
    20.                     dist0 *= -1; dist0 += 2.0f;
    21.                     o.camDistance = pow(dist0, 2.8f)*0.2f;
    22.                 //    o.screenPos = ComputeScreenPos(o.pos) - WorldSpaceVertexPos(v.vertex);
    23.                     o.screenPos = ComputeScreenPos(o.pos);
    24.                     return o;
    25.                 }
    26.  
    27.                 half4 frag(v2f i) : COLOR
    28.                 {
    29.                     half4 color = tex2D(_MainTex, i.uv) * _Color;
    30.                     color.a = 1.0f;
    31.                     float3 screenPos = normalize(i.screenPos);
    32.                     float3 worldNormal = i.normal;
    33.                    
    34.                     fixed rim = 1.0 - saturate(dot(normalize(screenPos), worldNormal));
    35.                     fixed3 emission = (_RimColor.rgb * pow(rim, _RimPower) + AtmosphereColor(-screenPos));
    36.                     color.rgb += emission.rgb * _RimIntensity;
    37.                     return color *_Color * float4(float3(i.camDistance, i.camDistance, i.camDistance) *_CamColorDistModifier, 1.0f);
    38.                     //return color *_Color  *_CamColorDistModifier;
    39.                 }
    40.                 ENDCG
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You need to dot the view direction in world space with your world normal.