Light probes in custom vertex/fragment shader

HI!

I would like to use lightprobes in my scene however my objects use a material with a custom shader that is not a surface shader. I have my own vertex and fragment shader but I can’t find any documentation on how to bring in the light probe data into the shader. Any ideas? Thanks!

You can use #pragma debug and open up the compiled surface shader to see what’s going on in there. I have looked at this and the way that the surface shaders do it is to use the Unity provided ShadeSH9 function in the vertex shader. Just give it your world vertex normal and it will spit out a color calculated from light probes.

#include “UnityCG.cginc”
This is where ShadeSH9 is.

Vert:
float3 shl = ShadeSH9(float4(worldNormal,1));

pack this into a varying or multiply your vertex colour by it for frag.

So the following is the shader I wrote however my material is coming in as black when I multiply by the output of the ShadeSH9 function. However when I swap the shader with Unity’s builtin Diffuse shader, I see my object’s shadow based off the light probes. Any idea of what I am doing wrong? Thanks!

Shader"Custom/LightMapTest" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque""BW"="TrueProbes" }
LOD200

Pass
{
CGPROGRAM

#pragmavertexv
#pragmafragmentp
#include "UnityCG.cginc"

sampler2Dunity_Lightmap;
float4unity_LightmapST;

structVertOut
{
float4position : POSITION;
float2uv : TEXCOORD0;
float4vLight : COLOR;
};

VertOutv (float4position : POSITION, float3norm : NORMAL, float2uv : TEXCOORD0 )
{
VertOutv;

v.position = mul( UNITY_MATRIX_MVP, position );
v.uv = uv.xy * unity_LightmapST.xy + unity_LightmapST.zw;

float3normal = normalize( mul( float3x3(_Object2World), norm ) );

v.vLight = float4( ShadeSH9( float4( normal, 0.0 ) ), 1.0 );


returnv;
}

structPixelOut
{
float4color : COLOR;
};

PixelOutp( VertOutIN )
{
PixelOutOUT;

float4lightMapColor = tex2D( unity_Lightmap, IN.uv );
lightMapColor = float4( (8.0 * lightMapColor.a) * lightMapColor.rgb, 1.0 );

float4color = float4( 1.0, 0.0, 0.0, 1.0 ) * lightMapColor;
OUT.color = color * float4( IN.vLight.xyz, 1.0 );

returnOUT;
}

ENDCG
}
}
FallBack"Diffuse"
}

Maybe name of pass or tags?
Try:

Pass {
        Name "FORWARD"
        Tags { "LightMode" = "ForwardBase" }
3 Likes

Unfortunately that didn’t change anything.

Any ideas?

Hi bfogerty,
I just got it working in Unity5, I am not sure if you still need it but I post it here anyway.
Basically you need to have the “BW”=“TrueProbes” and “LightMode” = “ForwardBase” in your tag list. But what I don’t understand is why it must be forwardBase and can’t be the others like deferred.
cheer

2 Likes
  1. Change Rendering Path to Forward in Player Settings
  2. Add “LightMode”=“ForwardBase” to your Tags
  3. I thought the Normal transformation would be better if use this :
    float3 worldNormal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal)
    :wink:

I mean this was 100% 3 years old at the time of your reply, and 2 major unity versions ago, but thankyou for adding your solution all the same :slight_smile:

What is the URP or custom SRP approach to “ShadeSH9”?
Can’t find it anywhere.

1 Like

In “Core RP” Package , EntityLighting.hlsl
7085728--843472--upload_2021-4-28_18-40-36.png

2 Likes