- Home /
Spherical Hamonics - ShadeSH9 and Surface Shader issue
Hey everyone,
After a loooooong time of research I finally decide to explain to you my issue and I hope someone can figure out what's happening.
I am trying to control Spherical Harmonics in my surface shader. This is very easy, you just need to follow the example provided by @Kuba in this thread Amplifying the Light Probes contribution.
But I don't want to use it like this, I can't apply my ShadeSH9 result in the Emission output. I have a lighting function and I want to apply the ShadeSH9 result inside it in my lighting algorithm. It works great BUT when I add a real-time point light I have strange results. I absolutely don't know how does it comes from (look at the gif to see the strange behaviour).
If I add the ShadeSH9 inside Emission and don't use it inside my lighting function like in the example, there is no problem but the visual result is not good.
Why this is happening ? How can I solve it ?
The scene uses only one point light in forward rendering mode (same issue in deferred) with some objects using probes and others not.
Update 1
If I check the different term of the ShadeSH9 I can see that the glitch is cause by unity_SHAr, unity_SHAg, unity_SHAb, unitySHBr, unitySHBg, unitySHBb.
But why is it failing if I use it inside the Lighting function of the Surface Shader ?! And why this doesn't fail if I apply the result of ShadeSH9 in the Emission output inside the Surface function.
Update 2
Here is the shader provided by @Kuba.
Shader "Custom/ModifyLightProbes" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
_Amount ("SH scale", Float) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf MyLambert noambient vertex:vert nolightmap nodirlightmap
#pragma debug
sampler2D _MainTex;
fixed4 _Color;
float _Amount;
struct Input {
float2 uv_MainTex;
float3 shLight;
};
struct CustomLambertOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half Alpha;
};
half4 LightingMyLambert(CustomLambertOutput o, half3 lightDir, half3 viewDir, half atten) {
half4 color = half4(0.0, 0.0, 0.0, 0.0);
return color;
}
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input, o);
// evaluate SH light
float3 worldN = mul ((float3x3)_Object2World, SCALED_NORMAL);
o.shLight = ShadeSH9(float4 (worldN, 1.0));
}
void surf (Input IN, inout CustomLambertOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
// modify the SH lighting anyway you want,
// here's just simple scaling
float3 shLight = _Amount * IN.shLight;
// emission is just added to the final color,
// so SH light needs to be multiplied by albedo
o.Emission = shLight;
}
ENDCG
}
FallBack "Diffuse"
}
Here is a failing example (Only the different parts)
half4 LightingMyLambert(CustomLambertOutput o, half3 lightDir, half3 viewDir, half atten) {
half4 color = half4(0.0, 0.0, 0.0, 0.0);
color.rgb = o.Albedo; //////////////////// CHANGE
return color;
}
void surf (Input IN, inout CustomLambertOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
// modify the SH lighting anyway you want,
// here's just simple scaling
float3 shLight = _Amount * IN.shLight;
// emission is just added to the final color,
// so SH light needs to be multiplied by albedo
o.Albedo = shLight; //////////////////// CHANGE
}