Skip to content

Commit

Permalink
Fixed the built-in target to have collapsible menus to match other pi…
Browse files Browse the repository at this point in the history
…peline (#4884)

* Fixed the built-in target to have collapsible menus to match other pipelines. Fixes fogbugs 1339256
This required adding an assembly dependency on core/Editor for shader graph.
Also Fixed render queue offset not actually being applied and not always exposed
  • Loading branch information
joshua-davis committed Jun 17, 2021
1 parent d4706d9 commit 2b074bd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 50 deletions.
1 change: 1 addition & 0 deletions com.unity.shadergraph/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fixed a ShaderGraph issue where a material inspector could contain an extra set of render queue, GPU instancing, and double-sided GI controls.
- Fixed a Shader Graph issue where property auto generated reference names were not consistent across all property types [1336937].
- Fixed a warning in ShaderGraph about BuiltIn Shader Library assembly having no scripts.
- Fixed ShaderGraph BuiltIn target not having collapsible foldouts in the material inspector [1339256].
- Fixed GPU instancing support in Shadergraph [1319655] (https://issuetracker.unity3d.com/issues/shader-graph-errors-are-thrown-when-a-propertys-shader-declaration-is-set-to-hybrid-per-instance-and-exposed-is-disabled).

## [11.0.0] - 2020-10-21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ namespace UnityEditor.Rendering.BuiltIn.ShaderGraph
{
public class BuiltInBaseShaderGUI : ShaderGUI
{
[Flags]
protected enum Expandable
{
SurfaceOptions = 1 << 0,
SurfaceInputs = 1 << 1,
Advanced = 1 << 2
}

public enum SurfaceType
{
Opaque,
Expand Down Expand Up @@ -39,6 +47,16 @@ protected class Styles
public static readonly int[] ztestValues = (int[])Enum.GetValues(typeof(UnityEditor.Rendering.BuiltIn.ShaderGraph.ZTestMode));
public static readonly string[] ztestNames = Enum.GetNames(typeof(UnityEditor.Rendering.BuiltIn.ShaderGraph.ZTestMode));

// Categories
public static readonly GUIContent SurfaceOptions =
EditorGUIUtility.TrTextContent("Surface Options", "Controls how Built-In RP renders the Material on a screen.");

public static readonly GUIContent SurfaceInputs = EditorGUIUtility.TrTextContent("Surface Inputs",
"These settings describe the look and feel of the surface itself.");

public static readonly GUIContent AdvancedLabel = EditorGUIUtility.TrTextContent("Advanced Options",
"These settings affect behind-the-scenes rendering and underlying calculations.");

public static readonly GUIContent surfaceType = EditorGUIUtility.TrTextContent("Surface Type",
"Select a surface type for your texture. Choose between Opaque or Transparent.");
public static readonly GUIContent blendingMode = EditorGUIUtility.TrTextContent("Blending Mode",
Expand All @@ -51,15 +69,46 @@ protected class Styles
"Specifies the depth test mode. The default is LEqual.");
public static readonly GUIContent alphaClipText = EditorGUIUtility.TrTextContent("Alpha Clipping",
"Makes your Material act like a Cutout shader. Use this to create a transparent effect with hard edges between opaque and transparent areas.");

public static readonly GUIContent queueSlider = EditorGUIUtility.TrTextContent("Sorting Priority",
"Determines the chronological rendering order for a Material. Materials with lower value are rendered first.");
}

public bool m_FirstTimeApply = true;

// By default, everything is expanded, except advanced
readonly MaterialHeaderScopeList m_MaterialScopeList = new MaterialHeaderScopeList(uint.MaxValue & ~(uint)Expandable.Advanced);

// These have to be stored due to how MaterialHeaderScopeList callbacks work (they don't provide this data in the callbacks)
MaterialEditor m_MaterialEditor;
MaterialProperty[] m_Properties;

private const int queueOffsetRange = 50;

override public void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties)
{
m_MaterialEditor = materialEditor;
m_Properties = properties;

Material targetMat = materialEditor.target as Material;

if (m_FirstTimeApply)
{
OnOpenGUI(targetMat, materialEditor, properties);
m_FirstTimeApply = false;
}

ShaderPropertiesGUI(materialEditor, targetMat, properties);
}

public virtual void OnOpenGUI(Material material, MaterialEditor materialEditor, MaterialProperty[] properties)
{
// Generate the foldouts
m_MaterialScopeList.RegisterHeaderScope(Styles.SurfaceOptions, (uint)Expandable.SurfaceOptions, DrawSurfaceOptions);
m_MaterialScopeList.RegisterHeaderScope(Styles.SurfaceInputs, (uint)Expandable.SurfaceInputs, DrawSurfaceInputs);
m_MaterialScopeList.RegisterHeaderScope(Styles.AdvancedLabel, (uint)Expandable.Advanced, DrawAdvancedOptions);
}

public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
{
// Clear all keywords for fresh start
Expand All @@ -72,13 +121,16 @@ public override void AssignNewShaderToMaterial(Material material, Shader oldShad
UnityEditor.Rendering.BuiltIn.ShaderUtils.ResetMaterialKeywords(material);
}

static void ShaderPropertiesGUI(MaterialEditor materialEditor, Material material, MaterialProperty[] properties)
void ShaderPropertiesGUI(MaterialEditor materialEditor, Material material, MaterialProperty[] properties)
{
DrawGui(materialEditor, material, properties);
m_MaterialScopeList.DrawHeaders(materialEditor, material);
}

static void DrawGui(MaterialEditor materialEditor, Material material, MaterialProperty[] properties)
protected virtual void DrawSurfaceOptions(Material material)
{
var materialEditor = m_MaterialEditor;
var properties = m_Properties;

var surfaceTypeProp = FindProperty(Property.Surface(), properties, false);
if (surfaceTypeProp != null)
{
Expand All @@ -101,8 +153,24 @@ static void DrawGui(MaterialEditor materialEditor, Material material, MaterialPr

var alphaClipProp = FindProperty(Property.AlphaClip(), properties, false);
DrawFloatToggleProperty(Styles.alphaClipText, alphaClipProp);
}

protected virtual void DrawSurfaceInputs(Material material)
{
DrawShaderGraphProperties(m_MaterialEditor, material, m_Properties);
}

protected virtual void DrawAdvancedOptions(Material material)
{
m_MaterialEditor.RenderQueueField();
DrawQueueOffsetField(m_MaterialEditor, material, m_Properties);
}

DrawShaderGraphProperties(materialEditor, material, properties);
protected void DrawQueueOffsetField(MaterialEditor materialEditor, Material material, MaterialProperty[] properties)
{
var queueOffsetProp = FindProperty(Property.QueueOffset(), properties, false);
if (queueOffsetProp != null)
materialEditor.IntSliderShaderProperty(queueOffsetProp, -queueOffsetRange, queueOffsetRange, Styles.queueSlider);
}

static void DrawShaderGraphProperties(MaterialEditor materialEditor, Material material, MaterialProperty[] properties)
Expand All @@ -125,6 +193,8 @@ public static void SetupSurface(Material material)
CoreUtils.SetKeyword(material, Keyword.SG_AlphaTestOn, alphaClipping);
CoreUtils.SetKeyword(material, Keyword.SG_AlphaClip, alphaClipping);

int renderQueue = material.shader.renderQueue;

var surfaceTypeProp = Property.Surface();
if (material.HasProperty(surfaceTypeProp))
{
Expand All @@ -133,21 +203,19 @@ public static void SetupSurface(Material material)
if (surfaceType == SurfaceType.Opaque)
{
string renderType;
RenderQueue renderQueue;
if (alphaClipping)
{
renderQueue = RenderQueue.AlphaTest;
renderQueue = (int)RenderQueue.AlphaTest;
renderType = "TransparentCutout";
}
else
{
renderQueue = RenderQueue.Geometry;
renderQueue = (int)RenderQueue.Geometry;
renderType = "Opaque";
}

material.SetOverrideTag("RenderType", "Transparent");
material.SetOverrideTag("RenderType", renderType);
material.renderQueue = (int)renderQueue;
SetBlendMode(material, UnityEngine.Rendering.BlendMode.One, UnityEngine.Rendering.BlendMode.Zero);
material.DisableKeyword(Keyword.SG_AlphaPremultiplyOn);
zwrite = true;
Expand All @@ -169,7 +237,7 @@ public static void SetupSurface(Material material)
CoreUtils.SetKeyword(material, Keyword.SG_AlphaPremultiplyOn, blendMode == BlendMode.Premultiply);
}

material.renderQueue = (int)RenderQueue.Transparent;
renderQueue = (int)RenderQueue.Transparent;
material.SetOverrideTag("RenderType", "Transparent");
}
CoreUtils.SetKeyword(material, Keyword.SG_SurfaceTypeTransparent, surfaceType == SurfaceType.Transparent);
Expand All @@ -186,6 +254,14 @@ public static void SetupSurface(Material material)
}
SetMaterialZWriteProperty(material, zwrite);
}

// must always apply queue offset, even if not set to material control
if (material.HasProperty(Property.QueueOffset()))
renderQueue += (int)material.GetFloat(Property.QueueOffset());

// apply automatic render queue
if (renderQueue != material.renderQueue)
material.renderQueue = renderQueue;
}

static void SetMaterialZWriteProperty(Material material, bool state)
Expand Down Expand Up @@ -236,42 +312,4 @@ public static void DrawFloatToggleProperty(GUIContent styles, MaterialProperty p
EditorGUI.showMixedValue = false;
}
}

// Currently the shader graph project doesn't have a reference to the necessary assembly to access this (RenderPipelines.Core.Editor)
public static partial class MaterialEditorExtension
{
public static int PopupShaderProperty(this MaterialEditor editor, MaterialProperty prop, GUIContent label, string[] displayedOptions)
{
int val = (int)prop.floatValue;

EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = prop.hasMixedValue;
int newValue = EditorGUILayout.Popup(label, val, displayedOptions);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck() && (newValue != val || prop.hasMixedValue))
{
editor.RegisterPropertyChangeUndo(label.text);
prop.floatValue = val = newValue;
}

return val;
}

public static int IntPopupShaderProperty(this MaterialEditor editor, MaterialProperty prop, string label, string[] displayedOptions, int[] optionValues)
{
int val = (int)prop.floatValue;

EditorGUI.BeginChangeCheck();
EditorGUI.showMixedValue = prop.hasMixedValue;
int newValue = EditorGUILayout.IntPopup(label, val, displayedOptions, optionValues);
EditorGUI.showMixedValue = false;
if (EditorGUI.EndChangeCheck() && (newValue != val || prop.hasMixedValue))
{
editor.RegisterPropertyChangeUndo(label);
prop.floatValue = val = newValue;
}

return val;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public override void ProcessPreviewMaterial(Material material)
material.SetFloat(Property.ZTest(), (float)target.zTestMode);
}

material.SetFloat(Property.QueueOffset(), 0.0f);

// call the full unlit material setup function
BuiltInLitGUI.UpdateMaterial(material);
}
Expand Down Expand Up @@ -118,8 +120,9 @@ public override void CollectShaderProperties(PropertyCollector collector, Genera
collector.AddFloatProperty(Property.ZWriteControl(), (float)target.zWriteControl);
collector.AddFloatProperty(Property.ZTest(), (float)target.zTestMode); // ztest mode is designed to directly pass as ztest
collector.AddFloatProperty(Property.Cull(), (float)target.renderFace); // render face enum is designed to directly pass as a cull mode
collector.AddFloatProperty(Property.QueueOffset(), 0.0f);
}

collector.AddFloatProperty(Property.QueueOffset(), 0.0f);
}

public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public override void ProcessPreviewMaterial(Material material)
material.SetFloat(Property.ZTest(), (float)target.zTestMode);
}

material.SetFloat(Property.QueueOffset(), 0.0f);

// call the full unlit material setup function
BuiltInUnlitGUI.UpdateMaterial(material);
}
Expand Down Expand Up @@ -80,8 +82,9 @@ public override void CollectShaderProperties(PropertyCollector collector, Genera
collector.AddFloatProperty(Property.ZWriteControl(), (float)target.zWriteControl);
collector.AddFloatProperty(Property.ZTest(), (float)target.zTestMode); // ztest mode is designed to directly pass as ztest
collector.AddFloatProperty(Property.Cull(), (float)target.renderFace); // render face enum is designed to directly pass as a cull mode
collector.AddFloatProperty(Property.QueueOffset(), 0.0f);
}

collector.AddFloatProperty(Property.QueueOffset(), 0.0f);
}

public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)
Expand Down
3 changes: 2 additions & 1 deletion com.unity.shadergraph/Editor/Unity.ShaderGraph.Editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"GUID:a2b0e253c44e0427f9c718f9e5c1c7e3",
"GUID:ade7125e800904674ba0c115208f7ed5",
"GUID:4988cf9794f41d64c884876ab6574b89",
"GUID:df380645f10b7bc4b97d4f5eb6303d95"
"GUID:df380645f10b7bc4b97d4f5eb6303d95",
"GUID:3eae0364be2026648bf74846acb8a731"
],
"includePlatforms": [
"Editor"
Expand Down

0 comments on commit 2b074bd

Please sign in to comment.