I’m trying to implement a debug shader to visualize the number of lights a given object is receiving. I have a basic solution: an additive fragment shader pass that tints the fragment a little each time it runs. However, this does not lead to very good differentiation between light counts, and it only works up to the 4 pixel light cap (see attached image, with 0-4 lights left-right)
Ideally, different light counts would be displayed as very different colors, but in order to achieve this I would need information about the existing color inside the fragment shader. I might be able to do this by reading/writing a render texture in each pass, but I’m not sure if I’m on the right track.
Any suggestions/examples would be greatly appreciated.
For reference, here is the basic shader I’m currently using:
Shader "Unlit/LightCountVisualization"
{
Properties
{
_MainTex ("Texture", 2D) = "black" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
// Render base objects black
Pass
{
Tags { "LightMode" = "ForwardBase" }
Blend One OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color: COLOR;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float4 color: COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = (fixed4(0,0,0,1));
return col;
}
ENDCG
}
// Add tint for each light
Pass
{
Tags { "LightMode" = "ForwardAdd" }
Blend SrcColor One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color: COLOR;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 color: COLOR;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = (fixed4(0.25,0.1,0.1,0));
return col;
}
ENDCG
}
}
}