TeamFortress2Shader

From Unify Community Wiki
Jump to: navigation, search


Originally done by James O'Hare.

Description

Example of Team Fortress 2 shader in action.
Team Fortress 2's Toon Ramp Texture

This shader takes a toon ramp image file to use as it's lighting model.

Supports diffuse, normal, specular and gloss shading with alphatested transparency.

It also applies a rim light based on the vertical orientation of the surface normal.

Usage

  • Toon Ramp: Controls the shading of the model from dark to light. This is multiplied by the diffuse texture.
  • Rim Color: Color of the rim light. Alpha channel also controls it's strength.
  • Rim Power: The exponent of the rim lighting. Higher values gives a sharper rim light.
  • Specular: The specular level is defined in the red channel of the specular texture. This controls the brightness of the specular highlight.
  • Gloss: The gloss level is defined in the green channel of the specular texture. This controls how sharp (full green) or wide (no green) the specular highlight is. It's best to keep this value non-zero.
  • Rim Mask: The blue channel of the specular texture is used to control the strength of the rim lighting. Full blue = full rim, no blue = no rim. This can also be controlled globally with the alpha value of the Rim Color.

Shader Code

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 "Toon/Team Fortress 2" {
	Properties {
		_Color ("Main Color", Color) = (1,1,1,1)
		_RimColor ("Rim Color", Color) = (0.97,0.88,1,0.75)
		_RimPower ("Rim Power", Float) = 2.5
		_MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "white" {}
		_BumpMap ("Normal (Normal)", 2D) = "bump" {}
		_SpecularTex ("Specular Level (R) Gloss (G) Rim Mask (B)", 2D) = "gray" {}
		_RampTex ("Toon Ramp (RGB)", 2D) = "white" {}
		_Cutoff ("Alphatest Cutoff", Range(0, 1)) = 0.5
	}

	SubShader{
		Tags { "RenderType" = "Opaque" }
		
		CGPROGRAM
			#pragma surface surf TF2 alphatest:_Cutoff
			#pragma target 3.0

			struct Input
			{
				float2 uv_MainTex;
				float3 worldNormal;
				INTERNAL_DATA
			};
			
			sampler2D _MainTex, _SpecularTex, _BumpMap, _RampTex;
			float4 _RimColor;
			float _RimPower;

			inline fixed4 LightingTF2 (SurfaceOutput s, fixed3 lightDir, fixed3 viewDir, fixed atten)
			{
				fixed3 h = normalize (lightDir + viewDir);
				
				fixed NdotL = dot(s.Normal, lightDir) * 0.5 + 0.5;
				fixed3 ramp = tex2D(_RampTex, float2(NdotL * atten)).rgb;
				
				float nh = max (0, dot (s.Normal, h));
				float spec = pow (nh, s.Gloss * 128) * s.Specular;
				
				fixed4 c;
				c.rgb = ((s.Albedo * ramp * _LightColor0.rgb + _LightColor0.rgb * spec) * (atten * 2));
				c.a = s.Alpha;
				return c;
			}

			void surf (Input IN, inout SurfaceOutput o)
			{
				o.Albedo = tex2D(_MainTex, IN.uv_MainTex).rgb;
				o.Alpha = tex2D(_MainTex, IN.uv_MainTex).a;
				o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
				float3 specGloss = tex2D(_SpecularTex, IN.uv_MainTex).rgb;
				o.Specular = specGloss.r;
				o.Gloss = specGloss.g;
				
				half3 rim = pow(max(0, dot(float3(0, 1, 0), WorldNormalVector (IN, o.Normal))), _RimPower) * _RimColor.rgb * _RimColor.a * specGloss.b;
				o.Emission = rim;
			}
		ENDCG
	}
	Fallback "Transparent/Cutout/Bumped Specular"
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools