Multiple lights in a fragment shader

Hey guys,
I’m trying to write a fragment shader that can take multiple lights. Here’s what I have, a bare-bones shader:

Shader "Custom/TileShader New"
{
	Properties {
		_MainTex ("Texture", 2D) = "white" {}
	}

	SubShader
	{
		Tags { "LightMode" = "ForwardBase" }
		Pass
		{
			Lighting On
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile_fwdaddd
			#include "UnityCG.cginc"
			#include "AutoLight.cginc"

			uniform sampler2D _MainTex;

			struct v2f
			{
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD1;
				LIGHTING_COORDS(3,4)
			};

			v2f vert (appdata_full v)
			{
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.uv = v.texcoord.xy;
				TRANSFER_VERTEX_TO_FRAGMENT(o);
				return o;
			}

			float4 frag (v2f i) : COLOR
			{
				float4 c = tex2D(_MainTex, i.uv);
				return c * LIGHT_ATTENUATION(i);
			}
			ENDCG
		}
	}
	FallBack "Diffuse"
}

It sounds like I should be using ForwardAdd as the LightMode, since I’d like a pass per light, but when I do that, it doesn’t render any lights at all. Right now it’s only picking the most important light to be used. What should I do to get multiple lights working?

Well, I looked at the code here and figured it out: Cg Programming/Unity/Multiple Lights - Wikibooks, open books for an open world
I guess it was a bit more complicated than I thought, with having both vertex lights and pixel lights.

1 Like

The LightMode tag should be inside of the Pass {} brackets as it’s a pass tag.

What you’d want to do for a simple multiple per-pixel light is to duplicate your pass and change the LightMode of the second pass to ForwardAdd.

What does “#pragma multi_compile_fwdaddd” do?

Good point, lots of syntax errors on my end… I just dropped all of that and went with the wikibook code.