Planet

From Unify Community Wiki
Jump to: navigation, search


Author: Nils Daumann.

Contents

Description

The planet shader in action.
The light moved.

This shader adds an atmosphere to a a planet as you can see it on the screenshots. To do so, it renders the object in another pass which extrudes the vertices along its normals. In this pass, only the back of the polygons is rendered which results in the outline, which is than faded our t using a couple of dot products. The Unity 3 Version supports only one point light and doesn´t work without. No ambient. Not tested with the deferred pipeline.

Works on fragment program capable cards (Radeon 9500+, GeForce FX+, Intel 9xx). Supports fog and shadows (Unity 2 version only).

Usage

Typical setup

  • Texture is mapped on the object using its uv set (Tiling and Offset works!).
  • Color is used as ambient color.
  • Atmosphere Color is the color of the atmosphere.
  • Size is the size of the atmosphere. It is scaled in object space, which means that it will be scaled with the model. Size is the thicknes in units*scale.
  • Falloff defines the fading of the atmosphere.
  • Falloff Planet defines the same for the planet surface.
  • Transparency defines how transparent the atmosphere is.
  • Transparency Planet defines the strength fading into the atmospheres color of the planet surface.

ShaderLab Unity 4 - Planet.shader

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 = mul (UNITY_MATRIX_MVP, v.vertex);
                    o.normal = mul((float3x3)_Object2World, v.normal);
                    o.worldvertpos = mul(_Object2World, 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 = mul (UNITY_MATRIX_MVP, v.vertex);
                    o.normal = mul((float3x3)_Object2World, v.normal);
                    o.worldvertpos = mul(_Object2World, 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"
}

Download Unity 3

Media:PlanetU3.zip

ShaderLab Unity 3 - Planet.shader

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
    {
        Pass
        {
            Name "PlanetBase"
            Tags {"LightMode" = "Always"}
            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 = mul (UNITY_MATRIX_MVP, v.vertex);
                    o.normal = v.normal;
                    o.worldvertpos = mul(_Object2World, 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(normalize(i.worldvertpos-_WorldSpaceLightPos0), i.normal);
                }
            ENDCG
        }
 
        Pass
        {
            Name "AtmosphereBase"
            Tags {"LightMode" = "Always"}
            Cull Front
            Blend SrcAlpha One
 
            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 = mul (UNITY_MATRIX_MVP, v.vertex);
                    o.normal = v.normal;
                    o.worldvertpos = mul(_Object2World, 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 = pow(saturate(dot(viewdir, i.normal)), _Falloff);
                    color.a *= _Transparency*_Color*dot(normalize(i.worldvertpos-_WorldSpaceLightPos0), i.normal);
                    return color;
                }
            ENDCG
        }
    }
 
    FallBack "Diffuse"
}

Download Unity 2

Media:Planet.zip

ShaderLab Unity 2 - Planet.shader

Shader "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
	{
		Pass
		{
			Name "PlanetBase"
			Tags {"LightMode" = "Always"}
			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
				{
					V2F_POS_FOG;
					float3 normal;
					float3 viewdir;
					float2 texcoord;
				}; 
 
				v2f vert(appdata_base v)
				{
					v2f o;
 
					PositionFog(v.vertex, o.pos, o.fog);
					o.normal = v.normal;
					o.viewdir = _ObjectSpaceCameraPos-v.vertex;
					o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
 
					return o;
				}
 
				float4 frag(v2f i) : COLOR
				{
					i.normal = normalize(i.normal);
					i.viewdir = normalize(i.viewdir);
 
					float4 atmo = _AtmoColor;
					atmo.a = pow(1.0-saturate(dot(i.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;
				}
			ENDCG
		}
 
		Pass
		{
			Name "AtmosphereBase"
			Tags {"LightMode" = "Always"}
			Cull Front
			Blend SrcAlpha One
 
			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
				{
					V2F_POS_FOG;
					float3 normal;
					float3 viewdir;
				}; 
 
				v2f vert(appdata_base v)
				{
					v2f o;
 
					v.vertex.xyz += v.normal*_Size;
					PositionFog(v.vertex, o.pos, o.fog);
					o.normal = v.normal;
					o.viewdir = v.vertex-_ObjectSpaceCameraPos;
 
					return o;
				}
 
				float4 frag(v2f i) : COLOR
				{
					i.normal = normalize(i.normal);
					i.viewdir = normalize(i.viewdir);
 
					float4 color = _AtmoColor;
					color.a = pow(saturate(dot(i.viewdir, i.normal)), _Falloff);
					color.a *= _Transparency*_Color;
					return color;
				}
			ENDCG
		}
 
		Pass
		{
			Name "PlanetLight"
			Tags {"LightMode" = "Pixel"}
			Cull Back
			Blend AppSrcAdd AppDstAdd
 
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
 
				#pragma multi_compile_builtin
				#pragma fragmentoption ARB_fog_exp2
				#pragma fragmentoption ARB_precision_hint_fastest
 
				#include "UnityCG.cginc"
				#include "AutoLight.cginc"
 
				uniform sampler2D _MainTex;
				uniform float4 _MainTex_ST;
				uniform float4 _AtmoColor;
				uniform float _FalloffPlanet;
				uniform float _TransparencyPlanet;
 
				struct v2f
				{
					V2F_POS_FOG;
					LIGHTING_COORDS
					float3 normal;
					float3 viewdir;
					float3 lightdir;
					float2 texcoord;
				}; 
 
				v2f vert(appdata_base v)
				{
					v2f o;
 
					PositionFog(v.vertex, o.pos, o.fog);
					o.normal = v.normal;
					o.viewdir = _ObjectSpaceCameraPos-v.vertex;
					o.lightdir = ObjSpaceLightDir(v.vertex);
					o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
					TRANSFER_VERTEX_TO_FRAGMENT(o);
 
					return o;
				}
 
				float4 frag(v2f i) : COLOR
				{
					i.normal = normalize(i.normal);
					i.viewdir = normalize(i.viewdir);
					i.lightdir = normalize(i.lightdir);
 
					float4 atmo = _AtmoColor;
					atmo.a = pow(1.0-saturate(dot(i.viewdir, i.normal)), _FalloffPlanet);
					atmo.a *= _TransparencyPlanet;
 
					float4 color = tex2D(_MainTex, i.texcoord);
					color.rgb = lerp(color.rgb, atmo.rgb, atmo.a);
					color = DiffuseLight(i.lightdir, i.normal, float4(color.rgb, 1.0), LIGHT_ATTENUATION(i));
 
					return color;
				}
			ENDCG
		}
 
		Pass
		{
			Name "AtmosphereLight"
			Tags {"LightMode" = "Pixel"}
			Cull Front
			Blend SrcAlpha One
 
			CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
 
				#pragma multi_compile_builtin
				#pragma fragmentoption ARB_fog_exp2
				#pragma fragmentoption ARB_precision_hint_fastest
 
				#include "UnityCG.cginc"
				#include "AutoLight.cginc"
 
				uniform float4 _AtmoColor;
				uniform float _Size;
				uniform float _Falloff;
				uniform float _Transparency;
 
				struct v2f
				{
					V2F_POS_FOG;
					LIGHTING_COORDS
					float3 normal;
					float3 viewdir;
					float3 lightdir;
				}; 
 
				v2f vert(appdata_base v)
				{
					v2f o;
 
					v.vertex.xyz += v.normal*_Size;
					PositionFog(v.vertex, o.pos, o.fog);
					o.normal = v.normal;
					o.viewdir = v.vertex-_ObjectSpaceCameraPos;
					o.lightdir = ObjSpaceLightDir(v.vertex);
					TRANSFER_VERTEX_TO_FRAGMENT(o);
 
					return o;
				}
 
				float4 frag(v2f i) : COLOR
				{
					i.normal = normalize(i.normal);
					i.viewdir = normalize(i.viewdir);
					i.lightdir = normalize(i.lightdir);
 
					float4 color = _AtmoColor;
					color.a = pow(saturate(dot(i.viewdir, i.normal)), _Falloff);
					color.a *= _Transparency;
 
					color.a *= DiffuseLight(i.lightdir, i.normal, float4(1.0, 1.0, 1.0, 1.0), LIGHT_ATTENUATION(i));
 
					return color;
				}
			ENDCG
		}
	}
 
	FallBack "Diffuse"
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools