Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom Depth Test Function for Materials #2415

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sources/engine/Stride.Rendering/Rendering/MaterialPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public MaterialPass(ParameterCollection parameters)
/// </summary>
public CullMode? CullMode;

/// <summary>
/// Overrides depth clip for this material.
/// </summary>
public CompareFunction? DepthFunction;

/// <summary>
/// Overrides the blend state for this material.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ public MaterialAttributes()
[DefaultValue(CullMode.Back)]
public CullMode CullMode { get; set; }

/// <summary>
/// The test used to figure out whether the material should be drawn when beind other models
Eideren marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
/// <userdoc>The test used to figure out whether the material should be drawn when beind other models</userdoc>
Eideren marked this conversation as resolved.
Show resolved Hide resolved
[Display("Depth Function", "Misc")]
[DataMember(135)]
[DefaultValue(CompareFunction.Less)]
public CompareFunction DepthFunction { get; set; } = CompareFunction.Less;

/// <summary>
/// Gets or sets the clear coat shading features for the material.
/// </summary>
Expand All @@ -198,8 +207,7 @@ public void Visit(MaterialGeneratorContext context)
// TODO: Should we apply it to any Diffuse Model?
var isEnergyConservative = (Specular as MaterialSpecularMapFeature)?.IsEnergyConservative ?? false;

var lambert = DiffuseModel as IEnergyConservativeDiffuseModelFeature;
if (lambert != null)
if (DiffuseModel is IEnergyConservativeDiffuseModelFeature lambert)
{
lambert.IsEnergyConservative = isEnergyConservative;
}
Expand Down Expand Up @@ -227,8 +235,8 @@ public void Visit(MaterialGeneratorContext context)

// If hair shading is enabled, ignore the transparency feature to avoid errors during shader compilation.
// Allowing the transparency feature while hair shading is on makes no sense anyway.
if (!(SpecularModel is MaterialSpecularHairModelFeature) &&
!(DiffuseModel is MaterialDiffuseHairModelFeature))
if (SpecularModel is not MaterialSpecularHairModelFeature &&
DiffuseModel is not MaterialDiffuseHairModelFeature)
{
context.Visit(Transparency);
}
Expand All @@ -246,6 +254,15 @@ public void Visit(MaterialGeneratorContext context)
context.MaterialPass.CullMode = CullMode;
}
}

// Only set the DepthFunction when the pass is not overriden (Only based on assumptions from how CullMode operates, not exactly sure how/when this would be used)
if (context.Step == MaterialGeneratorStep.GenerateShader && DepthFunction != CompareFunction.Less)
{
if (context.MaterialPass.DepthFunction == null)
{
context.MaterialPass.DepthFunction = DepthFunction;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ internal class MaterialInfo : MaterialInfoBase
public int PermutationCounter; // Dirty counter against material.Parameters.PermutationCounter
public ParameterCollection MaterialParameters; // Protect against changes of Material.Parameters instance (happens with editor fast reload)
public CullMode? CullMode;
public CompareFunction? DepthFunction;

public ShaderSource VertexStageSurfaceShaders;
public ShaderSource VertexStageStreamInitializer;
Expand All @@ -92,6 +93,7 @@ public MaterialInfo(MaterialPass materialPass)
{
MaterialPass = materialPass;
CullMode = materialPass.CullMode;
DepthFunction = materialPass.DepthFunction;
}
}

Expand Down Expand Up @@ -182,6 +184,12 @@ public override void PrepareEffectPermutations(RenderDrawContext context)
resetPipelineState = true;
}

if (materialInfo != null && materialInfo.DepthFunction != material.DepthFunction)
{
materialInfo.DepthFunction = material.DepthFunction;
resetPipelineState = true;
}

for (int i = 0; i < effectSlotCount; ++i)
{
var staticEffectObjectNode = staticObjectNode * effectSlotCount + i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public override void Process(RenderNodeReference renderNodeReference, ref Render

pipelineState.RasterizerState.CullMode = cullMode;

if (renderMesh.MaterialInfo.DepthFunction is {} newDepthFunction)
pipelineState.DepthStencilState.DepthBufferFunction = newDepthFunction;

if (isMultisample)
{
pipelineState.RasterizerState.MultisampleCount = output.MultisampleCount;
Expand Down