Cg Shader Patterns
Multiple Pixel Lights
Cg shaders that wish to render multiple pixel lights take the following pattern:
- A base texture pass with the PixelOrNone tag. This pass just renders the texture without lighting and with the ambient light taken into account.
- A pixel light pass that renders the texture and the lighting. Its easiest to do this
Unity first renders the PixelOrNone pass and then blend the Pixel passes on top of this per pixel light. You do not need to set blending modes on the pixel lighting pass, Unity automagically blends the lighting pass onto the ambient base pass.
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 "Pixel Light Template" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_Shininess ("Shininess", Range (0.03, 1)) = 0.078125
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_BumpMap ("Bumpmap (RGB)", 2D) = "bump" {}
}
Category {
Tags { "RenderType"="Opaque" }
LOD 400
Blend AppSrcAdd AppDstAdd
Fog { Color [_AddFog] }
SubShader {
Pass { // This pass renders the texture along with the ambient term.
Name "BaseColor"
Tags {"LightMode" = "PixelOrNone"}
Lighting Off
CGPROGRAM
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_builtin
#include "UnityCG.cginc"
#include "AutoLight.cginc"
uniform sampler2D _MainTex;
uniform float4 _Color;
/* Insert your own frag program here but take note to add the ambient term */
float4 frag (v2f_vertex_lit i) : COLOR
{
// VertexLight is a macro from UnityCG.cginc
float4 litColor = VertexLight(i, _MainTex);
// _PPLAmbient is a builtin unity cg variable that gives you the ambient color set in your project render settings
float4 ambient = _PPLAmbient * 2 * _Color;
return litColor * ambient;
}
ENDCG
}
Pass { // The pixel lighting pass goes here. The following is just copy/pasted from Unity's Bump Diffuse shader
Name "PPL"
Tags { "LightMode" = "Pixel" }
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"
struct v2f {
V2F_POS_FOG;
LIGHTING_COORDS
float2 uv;
float2 uv2;
float3 lightDirT;
};
uniform float4 _MainTex_ST, _BumpMap_ST;
v2f vert (appdata_tan v)
{
v2f o;
PositionFog( v.vertex, o.pos, o.fog );
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
o.uv2 = TRANSFORM_TEX(v.texcoord,_BumpMap);
TANGENT_SPACE_ROTATION;
o.lightDirT = mul( rotation, ObjSpaceLightDir( v.vertex ) );
TRANSFER_VERTEX_TO_FRAGMENT(o);
return o;
}
uniform sampler2D _BumpMap;
uniform sampler2D _MainTex;
float4 frag (v2f i) : COLOR
{
float4 texcol = tex2D(_MainTex,i.uv);
// get normal from the normal map
float3 normal = tex2D(_BumpMap, i.uv2).xyz * 2 - 1;
return DiffuseLight( i.lightDirT, normal, texcol, LIGHT_ATTENUATION(i) );
}
ENDCG
}
}
}