Skip to content

Commit 3a873d1

Browse files
committed
fix sh not working properly in some cases
1 parent 99baaa5 commit 3a873d1

File tree

5 files changed

+22
-14
lines changed

5 files changed

+22
-14
lines changed

Editor/Importer/Importer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace Graphlit
1111
{
12-
[ScriptedImporter(6, new[] { "graphlit", "subgraphlit", "zsg" }, 0)]
12+
[ScriptedImporter(7, new[] { "graphlit", "subgraphlit", "zsg" }, 0)]
1313
public class ShaderGraphImporter : ScriptedImporter
1414
{
1515
internal static Dictionary<string, ShaderGraphView> _graphViews = new();

Editor/ShaderNode/Nodes/Functions/Toon Light.hlsl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
#include "Main Light.hlsl"
44

5+
#define OPENLIT_FALLBACK_DIRECTION float4(0.001,0.002,0.001,0)
56
void ShadeSH9ToonDouble(float3 lightDirection, out float3 shMax, out float3 shMin)
67
{
7-
#if !defined(LIGHTMAP_ON)
8+
#if !defined(LIGHTMAP_ON) && UNITY_SHOULD_SAMPLE_SH
89
float3 N = lightDirection * 0.666666;
910
float4 vB = N.xyzz * N.yzzx;
1011
// L0 L2
@@ -43,27 +44,27 @@ float OpenLitGray(float3 rgb)
4344
return dot(rgb, float3(1.0/3.0, 1.0/3.0, 1.0/3.0));
4445
}
4546

47+
float3 ComputeCustomLightDirection(float4 lightDirectionOverride)
48+
{
49+
float3 customDir = length(lightDirectionOverride.xyz) * normalize(mul((float3x3)UNITY_MATRIX_M, lightDirectionOverride.xyz));
50+
return lightDirectionOverride.w ? customDir : lightDirectionOverride.xyz;
51+
}
4652

4753
void ComputeLightDirection(float3 mainLightDir, float3 mainLightCol, out float3 lightDirection, out float3 lightDirectionForSH9)
4854
{
4955
float3 mainDir = mainLightDir * OpenLitLuminance(mainLightCol);
50-
#if !defined(LIGHTMAP_ON)
56+
#if !defined(LIGHTMAP_ON) && UNITY_SHOULD_SAMPLE_SH
5157
float3 sh9Dir = unity_SHAr.xyz * 0.333333 + unity_SHAg.xyz * 0.333333 + unity_SHAb.xyz * 0.333333;
5258
float3 sh9DirAbs = float3(sh9Dir.x, abs(sh9Dir.y), sh9Dir.z);
53-
UNITY_FLATTEN
54-
if (!any(unity_SHC.xyz))
55-
{
56-
sh9Dir = 0;
57-
sh9DirAbs = 0;
58-
}
5959
#else
6060
float3 sh9Dir = 0;
6161
float3 sh9DirAbs = 0;
6262
#endif
63+
float3 customDir = ComputeCustomLightDirection(OPENLIT_FALLBACK_DIRECTION);
6364

64-
lightDirection = normalize(sh9DirAbs + mainDir);
65-
// lightDirectionForSH9 = sh9Dir + mainDir;
66-
lightDirectionForSH9 = sh9Dir;
65+
lightDirection = normalize(sh9DirAbs + mainDir + customDir);
66+
lightDirectionForSH9 = sh9Dir + mainDir;
67+
// lightDirectionForSH9 = sh9Dir;
6768
lightDirectionForSH9 = dot(lightDirectionForSH9, lightDirectionForSH9) < 0.000001 ? 0 : normalize(lightDirectionForSH9);
6869
}
6970

Editor/Targets/Lit/LitTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public override void OnBeforeBuild(ShaderBuilder builder)
201201
pass.pragmas.Add("#pragma shader_feature_local _ _ALPHAFADE_ON _ALPHATEST_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON");
202202

203203
pass.pragmas.Add("#pragma multi_compile_fwdbase");
204-
pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
204+
//pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
205205
pass.pragmas.Add("#pragma multi_compile_fog");
206206
pass.pragmas.Add("#pragma multi_compile_instancing");
207207
pass.pragmas.Add("#pragma shader_feature_fragment VERTEXLIGHT_ON");

Editor/Targets/Unlit/UnlitTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public override void OnBeforeBuild(ShaderBuilder builder)
9797
if (_customLighting)
9898
{
9999
pass.pragmas.Add("#pragma multi_compile_fwdbase");
100-
pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
100+
//pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
101101
pass.pragmas.Add("#pragma shader_feature_fragment VERTEXLIGHT_ON");
102102
}
103103
pass.pragmas.Add("#pragma multi_compile_fog");

ShaderLibrary/UnityCG/UnityCG.hlsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
#define USING_LIGHT_MULTI_COMPILE
2323
#endif
2424

25+
// Should SH (light probe / ambient) calculations be performed?
26+
// - When both static and dynamic lightmaps are available, no SH evaluation is performed
27+
// - When static and dynamic lightmaps are not available, SH evaluation is always performed
28+
// - For low level LODs, static lightmap and real-time GI from light probes can be combined together
29+
// - Passes that don't do ambient (additive, shadowcaster etc.) should not do SH either.
30+
#define UNITY_SHOULD_SAMPLE_SH (defined(LIGHTPROBE_SH) && !defined(UNITY_PASS_FORWARDADD) && !defined(UNITY_PASS_PREPASSBASE) && !defined(UNITY_PASS_SHADOWCASTER) && !defined(UNITY_PASS_META))
31+
2532
#if defined(SHADER_API_D3D11) || defined(SHADER_API_PSSL) || defined(SHADER_API_METAL) || defined(SHADER_API_GLCORE) || defined(SHADER_API_GLES3) || defined(SHADER_API_VULKAN) || defined(SHADER_API_SWITCH) // D3D11, D3D12, XB1, PS4, iOS, macOS, tvOS, glcore, gles3, webgl2.0, Switch
2633
// Real-support for depth-format cube shadow map.
2734
#define SHADOWS_CUBE_IN_DEPTH_TEX

0 commit comments

Comments
 (0)