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

Jump to: navigation, search



Case study / example with a holey Plane and 2 lights (Spot + Directional).

Handles transparency and takes it into account for receiving shadows from both Spot/Point lights and Directional lights.


LGPL Lgplv3-147x51.png


First of all, the Camera that is in charge of rendering the transparent objects MUST have its "Rendering Path" set to "Forward".
Modulate the "_ShadowIntensity" variable to get a more or less darkened shadow (this concerns only Directional Lights). The "ShadowPass" is used to add the shadows related to the "Main Directional Light". (Meaning that the other Directional lights won't be taken in account).


[ Full case study here : ]

Author: Gauthier BOAGLIO.

Shader File

TransparentShadowReceiver.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 "Selfmade/TransparentShadowReceiver" 

 	// Usual 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
	_ShadowIntensity ("Shadow Intensity", Range (0, 1)) = 0.6

	Tags {

	LOD 300

// Main Surface Pass (Handles Spot/Point lights)
		#pragma surface surf BlinnPhong alpha vertex:vert fullforwardshadows approxview

		half _Shininess;

		sampler2D _MainTex;
		float4 _Color;
		sampler2D _BumpMap;
		//sampler2D _ParallaxMap;
		float _Parallax;
		struct v2f { 
			float2 uv : TEXCOORD1;

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

		// Shadow Pass : Adding the shadows (from Directional Light)
		// by blending the light attenuation
		Pass {
			Blend SrcAlpha OneMinusSrcAlpha 
			Name "ShadowPass"
			Tags {"LightMode" = "ForwardBase"}
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdbase
			#pragma fragmentoption ARB_fog_exp2
			#pragma fragmentoption ARB_precision_hint_fastest
			#include "UnityCG.cginc"
			#include "AutoLight.cginc"
			struct v2f { 
				float2 uv_MainTex : TEXCOORD1;
				float4 pos : SV_POSITION;
				float3	lightDir;
			float4 _MainTex_ST;

			sampler2D _MainTex;
			float4 _Color;
			float _ShadowIntensity;
			v2f vert (appdata_full v)
				v2f o;
                o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
				o.lightDir = ObjSpaceLightDir( v.vertex );
				return o;

			float4 frag (v2f i) : COLOR
				float atten = LIGHT_ATTENUATION(i);
				half4 c;
				c.rgb =  0;
				c.a = (1-atten) * _ShadowIntensity * (tex2D(_MainTex, i.uv_MainTex).a); 
				return c;

FallBack "Transparent/Cutout/VertexLit"
Personal tools