Skip to content

Fix Material Creation RenderQueue setup + Fix AXF GUI #1185

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

Merged
merged 2 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ class AxFGUI : HDShaderGUI

MaterialUIBlockList uiBlocks = new MaterialUIBlockList
{
new SurfaceOptionUIBlock(MaterialUIBlock.Expandable.Base, features: SurfaceOptionUIBlock.Features.Unlit | SurfaceOptionUIBlock.Features.ReceiveDecal | SurfaceOptionUIBlock.Features.ReceiveSSR),
new SurfaceOptionUIBlock(MaterialUIBlock.Expandable.Base,
features: SurfaceOptionUIBlock.Features.Surface | SurfaceOptionUIBlock.Features.BlendMode | SurfaceOptionUIBlock.Features.DoubleSided |
SurfaceOptionUIBlock.Features.AlphaCutoff | SurfaceOptionUIBlock.Features.AlphaCutoffShadowThreshold | SurfaceOptionUIBlock.Features.DoubleSidedNormalMode |
SurfaceOptionUIBlock.Features.ReceiveSSR | SurfaceOptionUIBlock.Features.ReceiveDecal | SurfaceOptionUIBlock.Features.PreserveSpecularLighting
),
new AxfSurfaceInputsUIBlock(MaterialUIBlock.Expandable.Input),
new AdvancedOptionsUIBlock(MaterialUIBlock.Expandable.Advance, AdvancedOptionsUIBlock.Features.Instancing | AdvancedOptionsUIBlock.Features.SpecularOcclusion | AdvancedOptionsUIBlock.Features.AddPrecomputedVelocity),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,29 +185,29 @@ public override void Convert(Material srcMaterial, Material dstMaterial)
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Opaque, 0, false, false);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Opaque, 0, false, true);
break;
case 1: // Cutout
dstMaterial.SetFloat("_SurfaceType", 0);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 1);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Opaque, 0, true, false);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Opaque, 0, true, true);
break;
case 2: // Fade -> Alpha with depth prepass + Disable preserve specular
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 0);
dstMaterial.SetFloat("_TransparentDepthPrepassEnable", 1);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Transparent, 0, false, false);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Transparent, 0, false, true);
break;
case 3: // Transparent -> Alpha
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Transparent, 0, false, false);
dstMaterial.renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.RenderQueueType.Transparent, 0, false, true);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public override void AssignNewShaderToMaterial(Material material, Shader oldShad
{
base.AssignNewShaderToMaterial(material, oldShader, newShader);

ResetMaterialCustomRenderQueue(material);

SetupMaterialKeywordsAndPassInternal(material);
}

Expand Down Expand Up @@ -65,31 +63,6 @@ public sealed override void OnGUI(MaterialEditor materialEditor, MaterialPropert

protected abstract void OnMaterialGUI(MaterialEditor materialEditor, MaterialProperty[] props);

protected static void ResetMaterialCustomRenderQueue(Material material)
{
HDRenderQueue.RenderQueueType targetQueueType;
switch (material.GetSurfaceType())
{
case SurfaceType.Opaque:
targetQueueType = HDRenderQueue.GetOpaqueEquivalent(HDRenderQueue.GetTypeByRenderQueueValue(material.renderQueue));
break;
case SurfaceType.Transparent:
targetQueueType = HDRenderQueue.GetTransparentEquivalent(HDRenderQueue.GetTypeByRenderQueueValue(material.renderQueue));
break;
default:
throw new ArgumentException("Unknown SurfaceType");
}

// Decal doesn't have properties to compute the render queue
if (material.HasProperty(kTransparentSortPriority) && material.HasProperty(kAlphaCutoffEnabled))
{
float sortingPriority = material.GetFloat(kTransparentSortPriority);
bool alphaTest = material.GetFloat(kAlphaCutoffEnabled) > 0.5f;
bool decalEnable = material.HasProperty(kEnableDecals) && material.GetFloat(kEnableDecals) > 0.0f;
material.renderQueue = HDRenderQueue.ChangeType(targetQueueType, (int)sortingPriority, alphaTest, decalEnable);
}
}

readonly static string[] floatPropertiesToSynchronize = {
kUseSplitLighting
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,6 @@ float GetShaderDefaultFloatValue(string propName)

void DrawAlphaCutoffGUI()
{
EditorGUI.BeginChangeCheck();

// For shadergraphs we show this slider only if the feature is enabled in the shader settings.
bool showAlphaClipThreshold = true;

Expand Down Expand Up @@ -472,15 +470,6 @@ void DrawAlphaCutoffGUI()
}

EditorGUI.showMixedValue = false;

// Update the renderqueue when we change the alphaTest
if (EditorGUI.EndChangeCheck())
{
var renderQueueType = HDRenderQueue.GetTypeByRenderQueueValue(renderQueue);

bool receiveDecal = materials[0].HasProperty(kSupportDecals) && materials[0].GetFloat(kSupportDecals) > 0.0f;
renderQueue = HDRenderQueue.ChangeType(renderQueueType, (int)transparentSortPriority.floatValue, alphaCutoffEnable.floatValue == 1, receiveDecal);
}
}

void DrawDoubleSidedGUI()
Expand Down Expand Up @@ -538,7 +527,6 @@ void DrawSurfaceGUI()
if (EditorGUI.EndChangeCheck())
{
transparentSortPriority.floatValue = HDRenderQueue.ClampsTransparentRangePriority((int)transparentSortPriority.floatValue);
renderQueue = HDRenderQueue.ChangeType(HDRenderQueue.GetTypeByRenderQueueValue(renderQueue), offset: (int)transparentSortPriority.floatValue);
}
}

Expand Down Expand Up @@ -628,19 +616,6 @@ void SurfaceTypePopup()
{
materialEditor.RegisterPropertyChangeUndo(Styles.surfaceTypeText);
surfaceType.floatValue = (float)newMode;
HDRenderQueue.RenderQueueType targetQueueType;
switch(newMode)
{
case SurfaceType.Opaque:
targetQueueType = HDRenderQueue.GetOpaqueEquivalent(HDRenderQueue.GetTypeByRenderQueueValue(material.renderQueue));
break;
case SurfaceType.Transparent:
targetQueueType = HDRenderQueue.GetTransparentEquivalent(HDRenderQueue.GetTypeByRenderQueueValue(material.renderQueue));
break;
default:
throw new ArgumentException("Unknown SurfaceType");
}
renderQueue = HDRenderQueue.ChangeType(targetQueueType, (int)transparentSortPriority.floatValue, alphaTest, receiveDecal);
}
EditorGUI.showMixedValue = false;

Expand Down Expand Up @@ -780,13 +755,7 @@ void DrawLitSurfaceOptions()

if (supportDecals != null)
{
EditorGUI.BeginChangeCheck();
materialEditor.ShaderProperty(supportDecals, Styles.supportDecalsText);
if (EditorGUI.EndChangeCheck())
{
var renderQueueType = HDRenderQueue.GetTypeByRenderQueueValue(renderQueue);
renderQueue = HDRenderQueue.ChangeType(renderQueueType, (int)transparentSortPriority.floatValue, alphaCutoffEnable.floatValue == 1, supportDecals.floatValue > 0.0f);
}
}

if (receivesSSR != null && receivesSSRTransparent != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ static class BaseUnlitGUI
{
public static void SetupBaseUnlitKeywords(this Material material)
{
// First thing, be sure to have an up to date RenderQueue
material.ResetMaterialCustomRenderQueue();

bool alphaTestEnable = material.HasProperty(kAlphaCutoffEnabled) && material.GetFloat(kAlphaCutoffEnabled) > 0.0f;
CoreUtils.SetKeyword(material, "_ALPHATEST_ON", alphaTestEnable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,27 @@ public static CullMode GetOpaqueCullMode(this Material material)

public static CompareFunction GetTransparentZTest(this Material material)
=> material.HasProperty(kZTestTransparent) ? (CompareFunction)material.GetInt(kZTestTransparent) : CompareFunction.LessEqual;

public static void ResetMaterialCustomRenderQueue(this Material material)
{
// using GetOpaqueEquivalent / GetTransparentEquivalent allow to handle the case when we switch surfaceType
HDRenderQueue.RenderQueueType targetQueueType;
switch (material.GetSurfaceType())
{
case SurfaceType.Opaque:
targetQueueType = HDRenderQueue.GetOpaqueEquivalent(HDRenderQueue.GetTypeByRenderQueueValue(material.renderQueue));
break;
case SurfaceType.Transparent:
targetQueueType = HDRenderQueue.GetTransparentEquivalent(HDRenderQueue.GetTypeByRenderQueueValue(material.renderQueue));
break;
default:
throw new ArgumentException("Unknown SurfaceType");
}

float sortingPriority = material.HasProperty(kTransparentSortPriority) ? material.GetFloat(kTransparentSortPriority) : 0.0f;
bool alphaTest = material.HasProperty(kAlphaCutoffEnabled) && material.GetFloat(kAlphaCutoffEnabled) > 0.0f;
bool decalEnable = material.HasProperty(kEnableDecals) && material.GetFloat(kEnableDecals) > 0.0f;
material.renderQueue = HDRenderQueue.ChangeType(targetQueueType, (int)sortingPriority, alphaTest, decalEnable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ public static RenderQueueType GetTransparentEquivalent(RenderQueueType type)
return RenderQueueType.Transparent;
case RenderQueueType.AfterPostProcessOpaque:
return RenderQueueType.AfterPostprocessTransparent;
case RenderQueueType.LowTransparent:
return RenderQueueType.LowTransparent;
default:
//keep transparent mapped to transparent
return type;
Expand Down