Skip to content

Commit

Permalink
fix sh not working properly in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
z3y committed Sep 21, 2024
1 parent 99baaa5 commit 3a873d1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Editor/Importer/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Graphlit
{
[ScriptedImporter(6, new[] { "graphlit", "subgraphlit", "zsg" }, 0)]
[ScriptedImporter(7, new[] { "graphlit", "subgraphlit", "zsg" }, 0)]
public class ShaderGraphImporter : ScriptedImporter
{
internal static Dictionary<string, ShaderGraphView> _graphViews = new();
Expand Down
23 changes: 12 additions & 11 deletions Editor/ShaderNode/Nodes/Functions/Toon Light.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

#include "Main Light.hlsl"

#define OPENLIT_FALLBACK_DIRECTION float4(0.001,0.002,0.001,0)
void ShadeSH9ToonDouble(float3 lightDirection, out float3 shMax, out float3 shMin)
{
#if !defined(LIGHTMAP_ON)
#if !defined(LIGHTMAP_ON) && UNITY_SHOULD_SAMPLE_SH
float3 N = lightDirection * 0.666666;
float4 vB = N.xyzz * N.yzzx;
// L0 L2
Expand Down Expand Up @@ -43,27 +44,27 @@ float OpenLitGray(float3 rgb)
return dot(rgb, float3(1.0/3.0, 1.0/3.0, 1.0/3.0));
}

float3 ComputeCustomLightDirection(float4 lightDirectionOverride)
{
float3 customDir = length(lightDirectionOverride.xyz) * normalize(mul((float3x3)UNITY_MATRIX_M, lightDirectionOverride.xyz));
return lightDirectionOverride.w ? customDir : lightDirectionOverride.xyz;
}

void ComputeLightDirection(float3 mainLightDir, float3 mainLightCol, out float3 lightDirection, out float3 lightDirectionForSH9)
{
float3 mainDir = mainLightDir * OpenLitLuminance(mainLightCol);
#if !defined(LIGHTMAP_ON)
#if !defined(LIGHTMAP_ON) && UNITY_SHOULD_SAMPLE_SH
float3 sh9Dir = unity_SHAr.xyz * 0.333333 + unity_SHAg.xyz * 0.333333 + unity_SHAb.xyz * 0.333333;
float3 sh9DirAbs = float3(sh9Dir.x, abs(sh9Dir.y), sh9Dir.z);
UNITY_FLATTEN
if (!any(unity_SHC.xyz))
{
sh9Dir = 0;
sh9DirAbs = 0;
}
#else
float3 sh9Dir = 0;
float3 sh9DirAbs = 0;
#endif
float3 customDir = ComputeCustomLightDirection(OPENLIT_FALLBACK_DIRECTION);

lightDirection = normalize(sh9DirAbs + mainDir);
// lightDirectionForSH9 = sh9Dir + mainDir;
lightDirectionForSH9 = sh9Dir;
lightDirection = normalize(sh9DirAbs + mainDir + customDir);
lightDirectionForSH9 = sh9Dir + mainDir;
// lightDirectionForSH9 = sh9Dir;
lightDirectionForSH9 = dot(lightDirectionForSH9, lightDirectionForSH9) < 0.000001 ? 0 : normalize(lightDirectionForSH9);
}

Expand Down
2 changes: 1 addition & 1 deletion Editor/Targets/Lit/LitTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public override void OnBeforeBuild(ShaderBuilder builder)
pass.pragmas.Add("#pragma shader_feature_local _ _ALPHAFADE_ON _ALPHATEST_ON _ALPHAPREMULTIPLY_ON _ALPHAMODULATE_ON");

pass.pragmas.Add("#pragma multi_compile_fwdbase");
pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
//pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
pass.pragmas.Add("#pragma multi_compile_fog");
pass.pragmas.Add("#pragma multi_compile_instancing");
pass.pragmas.Add("#pragma shader_feature_fragment VERTEXLIGHT_ON");
Expand Down
2 changes: 1 addition & 1 deletion Editor/Targets/Unlit/UnlitTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public override void OnBeforeBuild(ShaderBuilder builder)
if (_customLighting)
{
pass.pragmas.Add("#pragma multi_compile_fwdbase");
pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
//pass.pragmas.Add("#pragma skip_variants LIGHTPROBE_SH");
pass.pragmas.Add("#pragma shader_feature_fragment VERTEXLIGHT_ON");
}
pass.pragmas.Add("#pragma multi_compile_fog");
Expand Down
7 changes: 7 additions & 0 deletions ShaderLibrary/UnityCG/UnityCG.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
#define USING_LIGHT_MULTI_COMPILE
#endif

// Should SH (light probe / ambient) calculations be performed?
// - When both static and dynamic lightmaps are available, no SH evaluation is performed
// - When static and dynamic lightmaps are not available, SH evaluation is always performed
// - For low level LODs, static lightmap and real-time GI from light probes can be combined together
// - Passes that don't do ambient (additive, shadowcaster etc.) should not do SH either.
#define UNITY_SHOULD_SAMPLE_SH (defined(LIGHTPROBE_SH) && !defined(UNITY_PASS_FORWARDADD) && !defined(UNITY_PASS_PREPASSBASE) && !defined(UNITY_PASS_SHADOWCASTER) && !defined(UNITY_PASS_META))

#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
// Real-support for depth-format cube shadow map.
#define SHADOWS_CUBE_IN_DEPTH_TEX
Expand Down

0 comments on commit 3a873d1

Please sign in to comment.