Planet
Author: Nils Daumann.
Contents |
Description
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
- 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.
Download Unity 3
ShaderLab Unity 3 - Planet.shader
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
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
ShaderLab Unity 2 - Planet.shader
Invalid language.
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic
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"
}