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
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. ;)