Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by no00ob · Aug 10, 2018 at 04:23 PM · shaderlightinglightshader programmingspace

Proper working atmosphere shader in Unity 2018.

I have found two possible shaders for use, first one seems better, but it doesn't work without directional light, and I need it to work with point light, the second one seems to only work with smaller scaled things, it would also be fine if it worked properly in a big space scene. Shader 1:

 // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
     Shader "SlinDev/Planet"
     {
         Properties
         {
             _MainTex("Texture (RGB)", 2D) = "black" {}
             _Color("Color", Color) = (0, 0, 0, 1)
             _AtmoColor("Atmosphere Color", Color) = (0.5, 0.5, 1.0, 1)
             _Size("Size", Float) = 0.1
             _Falloff("Falloff", Float) = 5
             _FalloffPlanet("Falloff Planet", Float) = 5
             _Transparency("Transparency", Float) = 15
             _TransparencyPlanet("Transparency Planet", Float) = 1
         }
      
         SubShader
         {
             Tags {"LightMode" = "ForwardBase"}
             Pass
             {
                 Name "PlanetBase"
                 Cull Back
      
                 CGPROGRAM
                     #pragma vertex vert
                     #pragma fragment frag
      
                     #pragma fragmentoption ARB_fog_exp2
                     #pragma fragmentoption ARB_precision_hint_fastest
      
                     #include "UnityCG.cginc"
      
                     uniform sampler2D _MainTex;
                     uniform float4 _MainTex_ST;
                     uniform float4 _Color;
                     uniform float4 _AtmoColor;
                     uniform float _FalloffPlanet;
                     uniform float _TransparencyPlanet;
      
                     struct v2f
                     {
                         float4 pos : SV_POSITION;
                         float3 normal : TEXCOORD0;
                         float3 worldvertpos : TEXCOORD1;
                         float2 texcoord : TEXCOORD2;
                     };
      
                     v2f vert(appdata_base v)
                     {
                         v2f o;
      
                         o.pos = UnityObjectToClipPos (v.vertex);
                         o.normal = mul((float3x3)unity_ObjectToWorld, v.normal);
                         o.worldvertpos = mul(unity_ObjectToWorld, v.vertex).xyz;
                         o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
      
                         return o;
                     }
      
                     float4 frag(v2f i) : COLOR
                     {
                         i.normal = normalize(i.normal);
                         float3 viewdir = normalize(_WorldSpaceCameraPos-i.worldvertpos);
      
                         float4 atmo = _AtmoColor;
                         atmo.a = pow(1.0-saturate(dot(viewdir, i.normal)), _FalloffPlanet);
                         atmo.a *= _TransparencyPlanet*_Color;
      
                         float4 color = tex2D(_MainTex, i.texcoord)*_Color;
                         color.rgb = lerp(color.rgb, atmo.rgb, atmo.a);
      
                         return color*dot(_WorldSpaceLightPos0, i.normal);
                     }
                 ENDCG
             }
      
             Tags {"LightMode" = "ForwardBase" "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
             Pass
             {
                 Name "FORWARD"
                 Cull Front
                 Blend SrcAlpha One
                 ZWrite Off
      
                 CGPROGRAM
                     #pragma vertex vert
                     #pragma fragment frag
      
                     #pragma fragmentoption ARB_fog_exp2
                     #pragma fragmentoption ARB_precision_hint_fastest
      
                     #include "UnityCG.cginc"
      
                     uniform float4 _Color;
                     uniform float4 _AtmoColor;
                     uniform float _Size;
                     uniform float _Falloff;
                     uniform float _Transparency;
      
                     struct v2f
                     {
                         float4 pos : SV_POSITION;
                         float3 normal : TEXCOORD0;
                         float3 worldvertpos : TEXCOORD1;
                     };
      
                     v2f vert(appdata_base v)
                     {
                         v2f o;
      
                         v.vertex.xyz += v.normal*_Size;
                         o.pos = UnityObjectToClipPos (v.vertex);
                         o.normal = mul((float3x3)unity_ObjectToWorld, v.normal);
                         o.worldvertpos = mul(unity_ObjectToWorld, v.vertex);
      
                         return o;
                     }
      
                     float4 frag(v2f i) : COLOR
                     {
                         i.normal = normalize(i.normal);
                         float3 viewdir = normalize(i.worldvertpos-_WorldSpaceCameraPos);
      
                         float4 color = _AtmoColor;
                         color.a = dot(viewdir, i.normal);
                         color.a *=dot(i.normal, _WorldSpaceLightPos0);
                         color.a = saturate(color.a);
                         color.a = pow(color.a, _Falloff);
                         color.a *= _Transparency;
                         return color;
                     }
                 ENDCG
             }
         }
  
     FallBack "Diffuse"
 }

Shader 2:

 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
 
 Shader "Custom/Planet" {
     Properties {
         _MainTex("Texture (RGB)", 2D) = "black" {}
         _Color("Color", Color) = (0, 0, 0, 1)
         _AtmoColor("Atmosphere Color", Color) = (0.5, 0.5, 1.0, 1)
         _Size("Atmosphere Size", Range(0, 5)) = 0.12
         _Falloff("Falloff", Float) = 2
         _Transparency("Transparency", Float) = 1
     }
    
     SubShader {
         Pass {
             Name "AtmosphereBase"
             Tags { "Queue" = "Transparent" "IgnoreProjector"="True" "RenderType" = "Transparent" }
             Cull Back          
             ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
          
             CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #include "UnityCG.cginc"
              
                 uniform float4 _Color;
                 uniform float4 _AtmoColor;
                 uniform float _Size;
                 uniform float _Falloff;
                 uniform float _Transparency;
              
                 struct v2f {
                     float4 pos : SV_POSITION;
                     float3 normal : TEXCOORD0;
                     float2 atmofalloff : TEXCOORD1;
                 };
  
                 v2f vert(appdata_base v) {
                     v2f o;
                     v.vertex.xyz += v.normal *     _Size;
                     o.pos = UnityObjectToClipPos (v.vertex);
                     o.normal = v.normal;
                        float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                        o.atmofalloff = float2( pow(saturate(dot(viewDir, v.normal)), _Falloff), 0.5);
                     return o;
                 }
            
                 float4 frag(v2f i) : COLOR {
                     i.normal = normalize(i.normal);  
                     float4 color = _AtmoColor;
                     color.a = i.atmofalloff.x * _Transparency * _Color;
                     return color;
                 }
             ENDCG
         }
        
         // *** surface shader to render the planet itself ***
         Name "PlanetBase"
         Tags { "RenderType"="Opaque" }
        
         CGPROGRAM
             #pragma surface surf Lambert
             #pragma target 3.0
  
             uniform sampler2D _MainTex;
             uniform float4 _Color;
  
             struct Input {
                 float2 uv_MainTex;
             };
            
             void surf (Input IN, inout SurfaceOutput o) {
                 fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
                 o.Albedo = c.rgb;
                 o.Alpha = c.a;
             }
         ENDCG
         // *** surface shader end ***
     }
     FallBack "Diffuse"
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by no00ob · Aug 10, 2018 at 04:49 PM

I got the second one to show by changing the render queue from 2000 to 3000, I guess it changed that render type thingy from "from shader" to "transparent", I can use this for now and actually could probably forever forwards if it had a normal support. ;)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

229 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

apply lighting to custom shader made in shader graph? 0 Answers

Telling a shader where shadows should go 0 Answers

Explain me this light-shader please 0 Answers

2D shader / lighting like Terraria or Starbound 2 Answers

Sending Objects Through Portal effect (Shaders)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges