Shield

From Unify Community Wiki
Jump to: navigation, search

Author: Omar Rojo (Toack)

Contents

Description

This is an special effect shader to make Shields-like shapes, ideal to put a shield around your spaceships and robots.

Shield.png

Unity 5.0

It seems you can no longer construct float4 using a single arument, changed the shader accordingly. Also o in the vert function needed to be initialised before exit and therefore the function could not be empty, fixed that as well.

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 "Custom/Shield"
{

Properties
{
    _Color("_Color", Color) = (0.0,1.0,0.0,1.0)
    _Inside("_Inside", Range(0.0,2.0) ) = 0.0
    _Rim("_Rim", Range(0.0,1.0) ) = 1.2
    _Texture("_Texture", 2D) = "white" {}
    _Speed("_Speed", Range(0.5,5.0) ) = 0.5
    _Tile("_Tile", Range(1.0,10.0) ) = 5.0
    _Strength("_Strength", Range(0.0,5.0) ) = 1.5
}
   
SubShader
{
    Tags
    {
        "Queue"="Transparent"
        "IgnoreProjector"="True"
        "RenderType"="Transparent"

    }

       
Cull Back
ZWrite On
ZTest LEqual


CGPROGRAM
#pragma surface surf BlinnPhongEditor alpha vertex:vert
//#pragma target 3.0


fixed4 _Color;
sampler2D _CameraDepthTexture;
fixed _Inside;
fixed _Rim;
sampler2D _Texture;
fixed _Speed;
fixed _Tile;
fixed _Strength;

struct EditorSurfaceOutput
    {
        half3 Albedo;
        half3 Normal;
        half3 Emission;
        half3 Gloss;
        half Specular;
        half Alpha;
    };
           
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
{
    half3 spec = light.a * s.Gloss;
   
    half4 c;
   
    c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
   
    c.a = s.Alpha + Luminance(spec);
   
    return c;


}

inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
    viewDir = normalize(viewDir);
    half3 h = normalize (lightDir + viewDir);
   
    half diff = max (0, dot (s.Normal, lightDir));
   
    float nh = max (0, dot (s.Normal, h));
    float3 spec = pow (nh, s.Specular*128.0) * s.Gloss;
   
    half4 res;
    res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
    res.w = spec * Luminance (_LightColor0.rgb);

    return LightingBlinnPhongEditor_PrePass( s, res );
}

struct Input
{
    float4 screenPos;
    float3 viewDir;
    float2 uv_Texture;
};


void vert (inout appdata_full v, out Input o)
{
	UNITY_INITIALIZE_OUTPUT(Input,o);
}
           

void surf (Input IN, inout EditorSurfaceOutput o)
{
    o.Albedo = fixed3(0.0,0.0,0.0);
    o.Normal = fixed3(0.0,0.0,1.0);
    o.Emission = 0.0;
    o.Gloss = 0.0;
    o.Specular = 0.0;
    o.Alpha = 1.0;
    float4 ScreenDepthDiff0= LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r) - IN.screenPos.z;
    float4 Saturate0=fixed4(0.3,0.3,0.3,1.0);//
    float4 Fresnel0_1_NoInput = fixed4(0,0,1,1);
    float dNorm = 1.0 - dot(normalize(float4(IN.viewDir, 1.0).xyz), normalize(Fresnel0_1_NoInput.xyz) );
    float4 Fresnel0 = float4(dNorm,dNorm,dNorm,dNorm);
    float4 Step0=step(Fresnel0,float4( 1.0, 1.0, 1.0, 1.0 ));
    float4 Clamp0=clamp(Step0,_Inside.xxxx,float4( 1.0, 1.0, 1.0, 1.0 ));
    float4 Pow0=pow(Fresnel0,(_Rim).xxxx);
    float4 Multiply5=_Time * _Speed.xxxx;
    float4 UV_Pan0=float4((IN.uv_Texture.xyxy).x,(IN.uv_Texture.xyxy).y + Multiply5.x,(IN.uv_Texture.xyxy).z,(IN.uv_Texture.xyxy).w);
    float4 Multiply1=UV_Pan0 * _Tile.xxxx;
    float4 Tex2D0=tex2D(_Texture,Multiply1.xy);
    float4 Multiply2=Tex2D0 * _Strength.xxxx;
    float4 Multiply0=Pow0 * Multiply2;
    float4 Multiply3=Clamp0 * Multiply0;
    float4 Multiply4=Saturate0 * Multiply3;
    o.Emission = Multiply3.xyz * _Color.rgb;
    o.Alpha =  Multiply3.w * _Color.a;

}
ENDCG
}
Fallback "Diffuse"
}

Unity 3.3

For some reason the Aubrey Falconer shader below does not shows up in Game mode. I post here an updated version of it, without the soft particles, that takes color alpha in account as well. Thanks to the Aubrey Falconer and the Toack for the sharing their code. ( Ippokratis Bournellis )

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 "Shield"
{

Properties
{
    _Color("_Color", Color) = (0.0,1.0,0.0,1.0)
    _Inside("_Inside", Range(0.0,2.0) ) = 0.0
    _Rim("_Rim", Range(0.0,2.0) ) = 1.2
    _Texture("_Texture", 2D) = "white" {}
    _Speed("_Speed", Range(0.5,5.0) ) = 0.5
    _Tile("_Tile", Range(1.0,10.0) ) = 5.0
    _Strength("_Strength", Range(0.0,5.0) ) = 1.5
}
   
SubShader
{
    Tags
    {
        "Queue"="Transparent"
        "IgnoreProjector"="True"
        "RenderType"="Transparent"

    }

       
Cull Back
ZWrite On
ZTest LEqual


CGPROGRAM
#pragma surface surf BlinnPhongEditor alpha vertex:vert
//#pragma target 3.0


fixed4 _Color;
sampler2D _CameraDepthTexture;
fixed _Inside;
fixed _Rim;
sampler2D _Texture;
fixed _Speed;
fixed _Tile;
fixed _Strength;

struct EditorSurfaceOutput
    {
        half3 Albedo;
        half3 Normal;
        half3 Emission;
        half3 Gloss;
        half Specular;
        half Alpha;
    };
           
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
{
    half3 spec = light.a * s.Gloss;
   
    half4 c;
   
    c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
   
    c.a = s.Alpha + Luminance(spec);
   
    return c;


}

inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
    viewDir = normalize(viewDir);
    half3 h = normalize (lightDir + viewDir);
   
    half diff = max (0, dot (s.Normal, lightDir));
   
    float nh = max (0, dot (s.Normal, h));
    float3 spec = pow (nh, s.Specular*128.0) * s.Gloss;
   
    half4 res;
    res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
    res.w = spec * Luminance (_LightColor0.rgb);

    return LightingBlinnPhongEditor_PrePass( s, res );
}

struct Input
{
    float4 screenPos;
    float3 viewDir;
    float2 uv_Texture;
};


void vert (inout appdata_full v, out Input o)
{

}
           

void surf (Input IN, inout EditorSurfaceOutput o)
{
    o.Albedo = fixed3(0.0,0.0,0.0);
    o.Normal = fixed3(0.0,0.0,1.0);
    o.Emission = 0.0;
    o.Gloss = 0.0;
    o.Specular = 0.0;
    o.Alpha = 1.0;
    float4 ScreenDepthDiff0= LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r) - IN.screenPos.z;
    float4 Saturate0=fixed4(0.3,0.3,0.3,1.0);//
    float4 Fresnel0_1_NoInput = fixed4(0,0,1,1);
    float4 Fresnel0=float4( 1.0 - dot( normalize( float4(IN.viewDir, 1.0).xyz), normalize( Fresnel0_1_NoInput.xyz ) ) );
    float4 Step0=step(Fresnel0,float4( 1.0 ));
    float4 Clamp0=clamp(Step0,_Inside.xxxx,float4( 1.0 ));
    float4 Pow0=pow(Fresnel0,_Rim.xxxx);
    float4 Multiply5=_Time * _Speed.xxxx;
    float4 UV_Pan0=float4((IN.uv_Texture.xyxy).x,(IN.uv_Texture.xyxy).y + Multiply5.x,(IN.uv_Texture.xyxy).z,(IN.uv_Texture.xyxy).w);
    float4 Multiply1=UV_Pan0 * _Tile.xxxx;
    float4 Tex2D0=tex2D(_Texture,Multiply1.xy);
    float4 Multiply2=Tex2D0 * _Strength.xxxx;
    float4 Multiply0=Pow0 * Multiply2;
    float4 Multiply3=Clamp0 * Multiply0;
    float4 Multiply4=Saturate0 * Multiply3;
    o.Emission = Multiply3.xyz * _Color.rgb;
    o.Alpha =  Multiply3.w * _Color.a;

}
ENDCG
}
Fallback "Diffuse"
}

Unity 3.3 and IOS

This version uses the same algorithm as the above shader (and will work fine on Desktop and WebPlayer), but is designed to be faster on IOS profiles. It has been tested on 3GS hardware. It might also be faster on older PC hardware too.

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 "Transparent Effects/CheapForcefield" {
Properties
 {
     _Color("_Color", Color) = (0,1,0,1)
     _Inside("_Inside", Range(0,0.2) ) = 0
     _Rim("_Rim", Range(1,2) ) = 1.2
     _Texture("_Texture", 2D) = "white" {}
     _Speed("_Speed", Range(0.5,5) ) = 0.5
     _Tile("_Tile", Range(1,10) ) = 5
     _Strength("_Strength", Range(0,5) ) = 1.5
 }
    
 SubShader
 {
     Tags
     {
         "Queue"="Transparent"
         "RenderType"="Transparent"
 
    }
 
       
	 Cull Off
	 ZWrite On
	 ZTest LEqual
 
		
		CGPROGRAM
		#pragma surface surf BlinnPhong alpha

		fixed4 _Color;
		fixed _Inside;
		fixed _Rim;
		sampler2D _Texture;
		fixed _Speed;
		fixed _Tile;
		fixed _Strength;

		struct Input {
			float4 screenPos;
			float3 viewDir;
			float2 uv_Texture;
		};

		inline fixed Fresnel(float3 viewDir)
		{
			float3 up = float3(0.0f,0.0f,1.0f);
			
			float theta = dot( normalize(viewDir), up);
			
			return fixed(1.0f - theta);
		}

		void surf (Input IN, inout SurfaceOutput o) 
		{
			// calculate Fresnel
			fixed fresnel = Fresnel( IN.viewDir );
			
			// fresnel related effects
			fixed stepfresnel = step( fresnel , fixed(1.0f));
			fixed insideContrib = clamp( stepfresnel , _Inside , fixed(1.0f));
			fixed rimContrib = pow( fresnel , _Rim );
			
			// calculate texture coords
			half timeOffset = half(_Time.x) * _Speed;
			half2 texCoords = float2( IN.uv_Texture.x , IN.uv_Texture.y + timeOffset );
			texCoords *= _Tile.xx;
			
			// and get the texture contribution
			fixed  texContrib = tex2D (_Texture, texCoords).x;
			texContrib *= _Strength;
			
			// put the contributions together into the alpha
			o.Alpha = texContrib * rimContrib * insideContrib * _Color.a;
			
			// set the colours
			o.Albedo = 0.0;
			o.Emission = _Color.rgb;
			o.Normal = fixed3(0.0,0.0,1.0);
		}
		ENDCG
	} 
	 Fallback "Diffuse"
}

Unity 3

Inspired by the original Shield Shader featured on this page, Aubrey Falconer created a new one using the Strumpy Shader Editor that is compatible with Unity 3, incorporates soft particle blending, automatically moves the plasma UV inside the shader, offers more control over the rim V center strength, and includes the ability to control the strength of the shield's backface which can be used for some pretty neat effects. Here it is!

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 "ForceField"
{
	Properties 
	{
_Color("_Color", Color) = (0,1,0,1)
_SoftParticlesFactor("_SoftParticlesFactor", Range(0.1,3) ) = 2
_Inside("_Inside", Range(0,0.2) ) = 0
_Rim("_Rim", Range(1,2) ) = 1.2
_Texture("_Texture", 2D) = "white" {}
_Speed("_Speed", Range(0.5,5) ) = 0.5
_Tile("_Tile", Range(1,10) ) = 5
_Strength("_Strength", Range(0,5) ) = 1.5

	}
	
	SubShader 
	{
		Tags
		{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"

		}

		
Cull Off
ZWrite On
ZTest LEqual


		CGPROGRAM
#pragma surface surf BlinnPhongEditor alpha vertex:vert
#pragma target 3.0


float4 _Color;
sampler2D _CameraDepthTexture;
float _SoftParticlesFactor;
float _Inside;
float _Rim;
sampler2D _Texture;
float _Speed;
float _Tile;
float _Strength;

			struct EditorSurfaceOutput {
				half3 Albedo;
				half3 Normal;
				half3 Emission;
				half3 Gloss;
				half Specular;
				half Alpha;
			};
			
			inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
			{
half3 spec = light.a * s.Gloss;

half4 c;

c.rgb = (s.Albedo * light.rgb + light.rgb * spec);

c.a = s.Alpha + Luminance(spec);

return c;


			}

			inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
			{
				viewDir = normalize(viewDir);
				half3 h = normalize (lightDir + viewDir);
				
				half diff = max (0, dot (s.Normal, lightDir));
				
				float nh = max (0, dot (s.Normal, h));
				float3 spec = pow (nh, s.Specular*128.0) * s.Gloss;
				
				half4 res;
				res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
				res.w = spec * Luminance (_LightColor0.rgb);

				return LightingBlinnPhongEditor_PrePass( s, res );
			}
			
			struct Input {
				float4 screenPos;
float3 viewDir;
float2 uv_Texture;

			};


			void vert (inout appdata_full v, out Input o) {
float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);


			}
			

			void surf (Input IN, inout EditorSurfaceOutput o) {
				o.Albedo = 0.0;
				o.Normal = float3(0.0,0.0,1.0);
				o.Emission = 0.0;
				o.Gloss = 0.0;
				o.Specular = 0.0;
				o.Alpha = 1.0;
float4 ScreenDepthDiff0= LinearEyeDepth (tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r) - IN.screenPos.z;
float4 Multiply6=ScreenDepthDiff0 * _SoftParticlesFactor.xxxx;
float4 Saturate0=saturate(Multiply6);
float4 Fresnel0_1_NoInput = float4(0,0,1,1);
float4 Fresnel0=float4( 1.0 - dot( normalize( float4(IN.viewDir, 1.0).xyz), normalize( Fresnel0_1_NoInput.xyz ) ) );
float4 Step0=step(Fresnel0,float4( 1.0 ));
float4 Clamp0=clamp(Step0,_Inside.xxxx,float4( 1.0 ));
float4 Pow0=pow(Fresnel0,_Rim.xxxx);
float4 Multiply5=_Time * _Speed.xxxx;
float4 UV_Pan0=float4((IN.uv_Texture.xyxy).x,(IN.uv_Texture.xyxy).y + Multiply5.x,(IN.uv_Texture.xyxy).z,(IN.uv_Texture.xyxy).w);
float4 Multiply1=UV_Pan0 * _Tile.xxxx;
float4 Tex2D0=tex2D(_Texture,Multiply1.xy);
float4 Multiply2=Tex2D0 * _Strength.xxxx;
float4 Multiply0=Pow0 * Multiply2;
float4 Multiply3=Clamp0 * Multiply0;
float4 Multiply4=Saturate0 * Multiply3;
float4 Master0_0_NoInput = float4(0,0,0,0);
float4 Master0_1_NoInput = float4(0,0,1,1);
float4 Master0_3_NoInput = float4(0,0,0,0);
float4 Master0_4_NoInput = float4(0,0,0,0);
float4 Master0_6_NoInput = float4(1,1,1,1);
o.Emission = _Color;
o.Alpha = Multiply4;

			}
		ENDCG
	}
	Fallback "Diffuse"
}


Usage

There is an script to work the animation.

The shader by itself is nothing:

  • Make a good Plasma-like texture for the shader texture
  • Make a good bright-to-dark ramp texture on the u axis

The shader has an additive effect, this is meant to give some light emission effect and also make it work with other meshes.

Caveats

I have no idea what graphics cards it will or won't work on.

C# - Animation.cs

using UnityEngine;
 
public class ShieldUvAnimation : MonoBehaviour 
{
	public GameObject iShield;
	public float iSpeed;
 
	private Material mMaterial;
	private float mTime;
 
	// Use this for initialization
	void Start () 
	{
		mMaterial = iShield.renderer.material;
 
		mTime = 0.0f;
	}
 
	// Update is called once per frame
	void Update () 
	{
		mTime += Time.deltaTime * iSpeed;
 
		mMaterial.SetFloat ("_Offset", Mathf.Repeat (mTime, 1.0f));
	}
}

ShaderLab - ForceField.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 "Shield" {
    Properties {
        _Offset ("Time", Range (0, 1)) = 0.0
        _Color ("Tint (RGB)", Color) = (1,1,1,1)
        _SurfaceTex ("Texture (RGB)", 2D) = "white" {}
        _RampTex ("Facing Ratio Ramp (RGB)", 2D) = "white" {}
    }
    SubShader {
        ZWrite Off
        Tags { "Queue" = "Transparent" }
        Blend One One
        Cull Off

        Pass {  
            CGPROGRAM 
            #pragma vertex vert
            #pragma fragment frag
            #pragma fragmentoption ARB_fog_exp2
            #include "UnityCG.cginc" 
 
            struct v2f {
                V2F_POS_FOG;
                float2 uv : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
                float3 normal : TEXCOORD2;
            };
            
            uniform float _Offset;
            
            v2f vert (appdata_base v) {
                v2f o;
                PositionFog( v.vertex, o.pos, o.fog );
                
                float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
                v.texcoord.x = v.texcoord.x;
                v.texcoord.y = v.texcoord.y + _Offset;
                o.uv = TRANSFORM_UV (1);
                o.uv2 = float2( abs (dot (viewDir, v.normal)), 0.5);
                o.normal = v.normal;
                return o;
            }
            
            uniform float4 _Color;
            uniform sampler2D _RampTex : register(s0);
            uniform sampler2D _SurfaceTex : register(s1);
                
            half4 frag (v2f f) : COLOR 
            {
                f.normal = normalize (f.normal);
                
                half4 ramp = tex2D (_RampTex, f.uv2) * _Color.a;
                half4 thisTex = tex2D (_SurfaceTex, f.uv) * ramp * _Color;
                
                return half4 (thisTex.r, thisTex.g, thisTex.b, ramp.r);
            }
            
            ENDCG 
 
            SetTexture [_RampTex] {combine texture}
            SetTexture [_SurfaceTex] {combine texture}
        }
    }
    Fallback "Transparent/VertexLit"
}

Resources

Here is some examples of the textures used by the project to get you started, you can get the idea from them.

The facing ratio ramp, this can be adjusted as needed (white=opaque black=transparent left=not-facing-camera right=facing-camera)

Ramp.png

Two different plasma like textures, import them as bump maps.

Plasma 1.png

Plasma 2.png

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools