From Unify Community Wiki
Revision as of 21:55, 17 January 2012 by NCarter (Talk | contribs)

Jump to: navigation, search



Case study / example with a holey (not holly) Flag.

Handles transparency and takes it into account for casting shadows.


LGPL Lgplv3-147x51.png


Modulate the "_Cutoff" variable to get a more or less holey shadow (size of the holes).

By another hand, please be aware of the fact that this shader seems to properly receive shadows from "Point" and "Spot" lights only (no interesting result from "Directional" ones).


[ Full case study here : ]

Author: Gauthier BOAGLIO.

Shader Files

The include file "ShadowCastCG.cginc" :

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

#include "UnityCG.cginc"

float4 _Color; 
sampler2D _MainTex;
fixed _Cutoff;

struct v2f { 
	float2 uv : TEXCOORD1;
The main file "TransparentShadowCaster.shader" :
<syntaxhighlight lang="shaderlab">
Shader "Selfmade/TransparentShadowCaster" 

 	// Ususal stuffs
	_Color ("Main Color", Color) = (1,1,1,1)
	_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
	_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
	_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}

	// Bump stuffs
	//_Parallax ("Height", Range (0.005, 0.08)) = 0.02
	_BumpMap ("Normalmap", 2D) = "bump" {}
	//_ParallaxMap ("Heightmap (A)", 2D) = "black" {}
	// Shadow Stuff
	_Cutoff ("Shadow Alpha cutoff", Range(0.25,0.9)) = 1.0

	Tags {

	LOD 300

			Name "ShadowCaster"
			Tags { "LightMode" = "ShadowCaster" }
			Fog {Mode Off}
			ZWrite On ZTest Less Cull Off
			Offset 1, 1

// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it does not contain a surface program or both vertex and fragment programs.
#pragma exclude_renderers gles
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest
			#pragma multi_compile_shadowcaster
			#include "ShadowCastCG.cginc"
			v2f vert( appdata_full v )
				v2f o;
			  return o;
			float4 frag( v2f i ) : COLOR
				fixed4 texcol = tex2D( _MainTex, i.uv );
				clip( texcol.a - _Cutoff );

		#pragma surface surf BlinnPhong alpha vertex:vert fullforwardshadows approxview
		#include "ShadowCastCG.cginc"

		half _Shininess;

		sampler2D _BumpMap;
		//sampler2D _ParallaxMap;
		float _Parallax;

		struct Input {
			float2 uv_MainTex;
			float2 uv_BumpMap;
			//float3 viewDir;

		v2f vert (inout appdata_full v) { 
			v2f o;
			return o; 

		void surf (Input IN, inout SurfaceOutput o) {
			// Comment the next 4 following lines to get a standard bumped rendering
			// [Without Parallax usage, which can cause strange result on the back side of the plane]
			/*half h = tex2D (_ParallaxMap, IN.uv_BumpMap).w;
			float2 offset = ParallaxOffset (h, _Parallax, IN.viewDir);
			IN.uv_MainTex += offset;
			IN.uv_BumpMap += offset;*/

			fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
			o.Albedo = tex.rgb * _Color.rgb;
			o.Gloss = tex.a;
			o.Alpha = tex.a * _Color.a;
			//clip(o.Alpha - _Cutoff);
			o.Specular = _Shininess;
			o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
Fallback "Transparent/VertexLit"

Personal tools