Skip to content

Commit

Permalink
Shader Graph: add pre-pass ignore mask [closes #142]
Browse files Browse the repository at this point in the history
  • Loading branch information
Delt06 committed Nov 10, 2023
1 parent 7678648 commit 1e08573
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,8 @@ public static SubShaderDescriptor DefaultSubShader(ToonTarget target, ToonDefaul
},
};

// cull the shadowcaster pass if we know it will never be used
if (target.CastShadows || target.AllowMaterialOverride)
{
result.passes.Add(CorePasses.ShadowCaster(target));
}

if (target.MayWriteDepth)
{
result.passes.Add(CorePasses.DepthOnly(target));
result.passes.Add(CorePasses.DepthNormals(target));
result.passes.Add(CorePasses.MotionVectors(target));
}
CorePasses.AddPrePasses(target, ref result);
CorePasses.AddShadowCasterPass(target, ref result);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,8 @@ public static SubShaderDescriptor ParticlesUnlit(ToonTarget target, string rende
customTags = customTags,
};

if (target.MayWriteDepth)
{
result.passes.Add(CorePasses.DepthOnly(target));
result.passes.Add(CorePasses.DepthNormals(target));
result.passes.Add(CorePasses.MotionVectors(target));
}

if (target.CastShadows || target.AllowMaterialOverride)
{
result.passes.Add(CorePasses.ShadowCaster(target));
}
CorePasses.AddPrePasses(target, ref result);
CorePasses.AddShadowCasterPass(target, ref result);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ public bool ReceiveShadows
private set => _receiveShadows = value;
}

public PrePassMode IgnoredPrePasses
{
get => _ignoredPrePasses;
private set => _ignoredPrePasses = value;
}

public bool Fog
{
get => _fog;
Expand Down Expand Up @@ -524,6 +530,20 @@ public void AddDefaultSurfacePropertiesGUI(ref TargetPropertyGUIContext context,
}
);
}

context.AddProperty("Ignored Pre-Passes", new EnumFlagsField(PrePassMode.Off) { value = IgnoredPrePasses },
evt =>
{
if (Equals(IgnoredPrePasses, evt.newValue))
{
return;
}
registerUndo("Change Ignored Pre-Passes");
IgnoredPrePasses = (PrePassMode) evt.newValue;
onChange();
}
);
}

public bool TrySetActiveSubTarget(Type subTargetType)
Expand Down Expand Up @@ -587,6 +607,7 @@ private enum ZTestModeForUI
[SerializeField] private bool _controlOutlinesStencilLayer = true;
[SerializeField] private bool _castShadows = true;
[SerializeField] private bool _receiveShadows = true;
[SerializeField] private PrePassMode _ignoredPrePasses = PrePassMode.Off;
[SerializeField] private bool _fog = true;
[SerializeField] private string _customEditorGUI;
// ReSharper restore Unity.RedundantSerializeFieldAttribute
Expand Down Expand Up @@ -685,7 +706,7 @@ internal static void AddTargetSurfaceControlsToPass(ref PassDescriptor pass, Too
AddOutlinesControlToPass(ref pass, target);
}

public static PassDescriptor DepthOnly(ToonTarget target)
private static PassDescriptor DepthOnly(ToonTarget target)
{
ref readonly ToonPasses.Pass pass = ref ToonPasses.DepthOnly;
var result = new PassDescriptor
Expand Down Expand Up @@ -724,7 +745,30 @@ public static PassDescriptor DepthOnly(ToonTarget target)
return result;
}

public static PassDescriptor DepthNormals(ToonTarget target)
public static void AddPrePasses(ToonTarget target, ref SubShaderDescriptor subShaderDescriptor)
{
// cull pre-passes if we know they will never be used
if (target.MayWriteDepth)
{
// skip generating a pre-pass if it is in the ignore mask
if (!target.IgnoredPrePasses.Includes(PrePassMode.Depth))
{
subShaderDescriptor.passes.Add(DepthOnly(target));
}

if (!target.IgnoredPrePasses.Includes(PrePassMode.Depth | PrePassMode.Normals))
{
subShaderDescriptor.passes.Add(DepthNormals(target));
}

if (!target.IgnoredPrePasses.Includes(PrePassMode.MotionVectors))
{
subShaderDescriptor.passes.Add(MotionVectors(target));
}
}
}

private static PassDescriptor DepthNormals(ToonTarget target)
{
ref readonly ToonPasses.Pass pass = ref ToonPasses.DepthNormals;
var result = new PassDescriptor
Expand Down Expand Up @@ -764,7 +808,7 @@ public static PassDescriptor DepthNormals(ToonTarget target)
return result;
}

public static PassDescriptor MotionVectors(ToonTarget target)
private static PassDescriptor MotionVectors(ToonTarget target)
{
ref readonly ToonPasses.Pass pass = ref ToonPasses.MotionVectors;
var result = new PassDescriptor
Expand Down Expand Up @@ -804,7 +848,16 @@ public static PassDescriptor MotionVectors(ToonTarget target)
return result;
}

public static PassDescriptor ShadowCaster(ToonTarget target)
public static void AddShadowCasterPass(ToonTarget target, ref SubShaderDescriptor subShaderDescriptor)
{
// cull the shadowcaster pass if we know it will never be used
if (target.CastShadows || target.AllowMaterialOverride)
{
subShaderDescriptor.passes.Add(ShadowCaster(target));
}
}

private static PassDescriptor ShadowCaster(ToonTarget target)
{
ref readonly ToonPasses.Pass pass = ref ToonPasses.ShadowCaster;
var result = new PassDescriptor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,8 @@ public static SubShaderDescriptor Unlit(ToonTarget target, string renderType, st
},
};

if (target.MayWriteDepth)
{
result.passes.Add(CorePasses.DepthOnly(target));
result.passes.Add(CorePasses.DepthNormals(target));
result.passes.Add(CorePasses.MotionVectors(target));
}

if (target.CastShadows || target.AllowMaterialOverride)
{
result.passes.Add(CorePasses.ShadowCaster(target));
}
CorePasses.AddPrePasses(target, ref result);
CorePasses.AddShadowCasterPass(target, ref result);

return result;
}
Expand Down

0 comments on commit 1e08573

Please sign in to comment.