From e21702695f56d5f043d0ac0af9b3213803fe93b4 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Tue, 6 Aug 2019 13:41:10 -0400 Subject: [PATCH] Revert "Update Wireframe Shader to new MRTK standard (#254)" (#264) This reverts commit 98ef57f430e5e4cf5ff2a5347c492c427344c1e0. --- .../Inspectors/MixedRealityShaderGUI.cs | 408 ------------- .../Inspectors/MixedRealityShaderGUI.cs.meta | 11 - .../MixedRealityStandardShaderGUI.cs | 547 +++++++++++++----- .../MixedRealityWireframeShaderGUI.cs | 106 ---- .../MixedRealityWireframeShaderGUI.cs.meta | 11 - .../Shaders/MixedRealityStandard.shader | 208 ++----- ...Wireframe.shader => XRTK_Wireframe.shader} | 86 ++- ...shader.meta => XRTK_Wireframe.shader.meta} | 2 +- 8 files changed, 481 insertions(+), 898 deletions(-) delete mode 100644 XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs delete mode 100644 XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs.meta delete mode 100644 XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs delete mode 100644 XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs.meta rename XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/{MixedRealityWireframe.shader => XRTK_Wireframe.shader} (62%) rename XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/{MixedRealityWireframe.shader.meta => XRTK_Wireframe.shader.meta} (80%) diff --git a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs b/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs deleted file mode 100644 index 09b60246d..000000000 --- a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs +++ /dev/null @@ -1,408 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System; -using UnityEditor; -using UnityEngine; -using UnityEngine.Rendering; - -namespace XRTK.Inspectors -{ - /// - /// A custom base shader inspector for Mixed Reality Toolkit shaders. - /// - public abstract class MixedRealityShaderGUI : ShaderGUI - { - protected enum RenderingMode - { - Opaque, - TransparentCutout, - Transparent, - PremultipliedTransparent, - Additive, - Custom - } - - protected enum CustomRenderingMode - { - Opaque, - TransparentCutout, - Transparent - } - - protected enum DepthWrite - { - Off, - On - } - - protected static class BaseStyles - { - public static string renderingOptionsTitle = "Rendering Options"; - public static string advancedOptionsTitle = "Advanced Options"; - public static string renderTypeName = "RenderType"; - public static string renderingModeName = "_Mode"; - public static string customRenderingModeName = "_CustomMode"; - public static string sourceBlendName = "_SrcBlend"; - public static string destinationBlendName = "_DstBlend"; - public static string blendOperationName = "_BlendOp"; - public static string depthTestName = "_ZTest"; - public static string depthWriteName = "_ZWrite"; - public static string depthOffsetFactorName = "_ZOffsetFactor"; - public static string depthOffsetUnitsName = "_ZOffsetUnits"; - public static string colorWriteMaskName = "_ColorWriteMask"; - - public static string cullModeName = "_CullMode"; - public static string renderQueueOverrideName = "_RenderQueueOverride"; - - public static string alphaTestOnName = "_ALPHATEST_ON"; - public static string alphaBlendOnName = "_ALPHABLEND_ON"; - - public static readonly string[] renderingModeNames = Enum.GetNames(typeof(RenderingMode)); - public static readonly string[] customRenderingModeNames = Enum.GetNames(typeof(CustomRenderingMode)); - public static readonly string[] depthWriteNames = Enum.GetNames(typeof(DepthWrite)); - public static GUIContent sourceBlend = new GUIContent("Source Blend", "Blend Mode of Newly Calculated Color"); - public static GUIContent destinationBlend = new GUIContent("Destination Blend", "Blend Mode of Existing Color"); - public static GUIContent blendOperation = new GUIContent("Blend Operation", "Operation for Blending New Color With Existing Color"); - public static GUIContent depthTest = new GUIContent("Depth Test", "How Should Depth Testing Be Performed."); - public static GUIContent depthWrite = new GUIContent("Depth Write", "Controls Whether Pixels From This Material Are Written to the Depth Buffer"); - public static GUIContent depthOffsetFactor = new GUIContent("Depth Offset Factor", "Scales the Maximum Z Slope, with Respect to X or Y of the Polygon"); - public static GUIContent depthOffsetUnits = new GUIContent("Depth Offset Units", "Scales the Minimum Resolvable Depth Buffer Value"); - public static GUIContent colorWriteMask = new GUIContent("Color Write Mask", "Color Channel Writing Mask"); - public static GUIContent cullMode = new GUIContent("Cull Mode", "Triangle Culling Mode"); - public static GUIContent renderQueueOverride = new GUIContent("Render Queue Override", "Manually Override the Render Queue"); - } - - protected bool initialized; - - protected MaterialProperty renderingMode; - protected MaterialProperty customRenderingMode; - protected MaterialProperty sourceBlend; - protected MaterialProperty destinationBlend; - protected MaterialProperty blendOperation; - protected MaterialProperty depthTest; - protected MaterialProperty depthWrite; - protected MaterialProperty depthOffsetFactor; - protected MaterialProperty depthOffsetUnits; - protected MaterialProperty colorWriteMask; - protected MaterialProperty cullMode; - protected MaterialProperty renderQueueOverride; - - protected const string LegacyShadersPath = "Legacy Shaders/"; - protected const string TransparentShadersPath = "/Transparent/"; - protected const string TransparentCutoutShadersPath = "/Transparent/Cutout/"; - - public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props) - { - Material material = (Material)materialEditor.target; - - FindProperties(props); - Initialize(material); - - RenderingModeOptions(materialEditor); - } - - protected virtual void FindProperties(MaterialProperty[] props) - { - renderingMode = FindProperty(BaseStyles.renderingModeName, props); - customRenderingMode = FindProperty(BaseStyles.customRenderingModeName, props); - sourceBlend = FindProperty(BaseStyles.sourceBlendName, props); - destinationBlend = FindProperty(BaseStyles.destinationBlendName, props); - blendOperation = FindProperty(BaseStyles.blendOperationName, props); - depthTest = FindProperty(BaseStyles.depthTestName, props); - depthWrite = FindProperty(BaseStyles.depthWriteName, props); - depthOffsetFactor = FindProperty(BaseStyles.depthOffsetFactorName, props); - depthOffsetUnits = FindProperty(BaseStyles.depthOffsetUnitsName, props); - colorWriteMask = FindProperty(BaseStyles.colorWriteMaskName, props); - - cullMode = FindProperty(BaseStyles.cullModeName, props); - renderQueueOverride = FindProperty(BaseStyles.renderQueueOverrideName, props); - } - - protected void Initialize(Material material) - { - if (!initialized) - { - MaterialChanged(material); - initialized = true; - } - } - - protected virtual void MaterialChanged(Material material) - { - SetupMaterialWithRenderingMode(material, - (RenderingMode)renderingMode.floatValue, - (CustomRenderingMode)customRenderingMode.floatValue, - (int)renderQueueOverride.floatValue); - } - - protected void RenderingModeOptions(MaterialEditor materialEditor) - { - EditorGUI.BeginChangeCheck(); - - EditorGUI.showMixedValue = renderingMode.hasMixedValue; - RenderingMode mode = (RenderingMode)renderingMode.floatValue; - EditorGUI.BeginChangeCheck(); - mode = (RenderingMode)EditorGUILayout.Popup(renderingMode.displayName, (int)mode, BaseStyles.renderingModeNames); - - if (EditorGUI.EndChangeCheck()) - { - materialEditor.RegisterPropertyChangeUndo(renderingMode.displayName); - renderingMode.floatValue = (float)mode; - - var targets = renderingMode.targets; - - foreach (var target in targets) - { - MaterialChanged((Material)target); - } - } - - EditorGUI.showMixedValue = false; - - if ((RenderingMode)renderingMode.floatValue == RenderingMode.Custom) - { - EditorGUI.indentLevel += 2; - customRenderingMode.floatValue = EditorGUILayout.Popup(customRenderingMode.displayName, (int)customRenderingMode.floatValue, BaseStyles.customRenderingModeNames); - materialEditor.ShaderProperty(sourceBlend, BaseStyles.sourceBlend); - materialEditor.ShaderProperty(destinationBlend, BaseStyles.destinationBlend); - materialEditor.ShaderProperty(blendOperation, BaseStyles.blendOperation); - materialEditor.ShaderProperty(depthTest, BaseStyles.depthTest); - depthWrite.floatValue = EditorGUILayout.Popup(depthWrite.displayName, (int)depthWrite.floatValue, BaseStyles.depthWriteNames); - materialEditor.ShaderProperty(depthOffsetFactor, BaseStyles.depthOffsetFactor); - materialEditor.ShaderProperty(depthOffsetUnits, BaseStyles.depthOffsetUnits); - materialEditor.ShaderProperty(colorWriteMask, BaseStyles.colorWriteMask); - EditorGUI.indentLevel -= 2; - } - - materialEditor.ShaderProperty(cullMode, BaseStyles.cullMode); - } - - protected static void SetupMaterialWithRenderingMode(Material material, RenderingMode mode, CustomRenderingMode customMode, int renderQueueOverride) - { - // If we aren't switching to Custom, then set default values for all RenderingMode types. Otherwise keep whatever user had before - if (mode != RenderingMode.Custom) - { - material.SetInt(BaseStyles.blendOperationName, (int)BlendOp.Add); - material.SetInt(BaseStyles.depthTestName, (int)CompareFunction.LessEqual); - material.SetFloat(BaseStyles.depthOffsetFactorName, 0.0f); - material.SetFloat(BaseStyles.depthOffsetUnitsName, 0.0f); - material.SetInt(BaseStyles.colorWriteMaskName, (int)ColorWriteMask.All); - } - - switch (mode) - { - case RenderingMode.Opaque: - { - material.SetOverrideTag(BaseStyles.renderTypeName, BaseStyles.renderingModeNames[(int)RenderingMode.Opaque]); - material.SetInt(BaseStyles.customRenderingModeName, (int)CustomRenderingMode.Opaque); - material.SetInt(BaseStyles.sourceBlendName, (int)BlendMode.One); - material.SetInt(BaseStyles.destinationBlendName, (int)BlendMode.Zero); - material.SetInt(BaseStyles.depthWriteName, (int)DepthWrite.On); - material.DisableKeyword(BaseStyles.alphaTestOnName); - material.DisableKeyword(BaseStyles.alphaBlendOnName); - material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Geometry; - } - break; - - case RenderingMode.TransparentCutout: - { - material.SetOverrideTag(BaseStyles.renderTypeName, BaseStyles.renderingModeNames[(int)RenderingMode.TransparentCutout]); - material.SetInt(BaseStyles.customRenderingModeName, (int)CustomRenderingMode.TransparentCutout); - material.SetInt(BaseStyles.sourceBlendName, (int)BlendMode.One); - material.SetInt(BaseStyles.destinationBlendName, (int)BlendMode.Zero); - material.SetInt(BaseStyles.depthWriteName, (int)DepthWrite.On); - material.EnableKeyword(BaseStyles.alphaTestOnName); - material.DisableKeyword(BaseStyles.alphaBlendOnName); - material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.AlphaTest; - } - break; - - case RenderingMode.Transparent: - { - material.SetOverrideTag(BaseStyles.renderTypeName, BaseStyles.renderingModeNames[(int)RenderingMode.Transparent]); - material.SetInt(BaseStyles.customRenderingModeName, (int)CustomRenderingMode.Transparent); - material.SetInt(BaseStyles.sourceBlendName, (int)BlendMode.SrcAlpha); - material.SetInt(BaseStyles.destinationBlendName, (int)BlendMode.OneMinusSrcAlpha); - material.SetInt(BaseStyles.depthWriteName, (int)DepthWrite.Off); - material.DisableKeyword(BaseStyles.alphaTestOnName); - material.EnableKeyword(BaseStyles.alphaBlendOnName); - material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Transparent; - } - break; - - case RenderingMode.PremultipliedTransparent: - { - material.SetOverrideTag(BaseStyles.renderTypeName, BaseStyles.renderingModeNames[(int)RenderingMode.Transparent]); - material.SetInt(BaseStyles.customRenderingModeName, (int)CustomRenderingMode.Transparent); - material.SetInt(BaseStyles.sourceBlendName, (int)BlendMode.One); - material.SetInt(BaseStyles.destinationBlendName, (int)BlendMode.OneMinusSrcAlpha); - material.SetInt(BaseStyles.depthWriteName, (int)DepthWrite.Off); - material.DisableKeyword(BaseStyles.alphaTestOnName); - material.EnableKeyword(BaseStyles.alphaBlendOnName); - material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Transparent; - } - break; - - case RenderingMode.Additive: - { - material.SetOverrideTag(BaseStyles.renderTypeName, BaseStyles.renderingModeNames[(int)RenderingMode.Transparent]); - material.SetInt(BaseStyles.customRenderingModeName, (int)CustomRenderingMode.Transparent); - material.SetInt(BaseStyles.sourceBlendName, (int)BlendMode.One); - material.SetInt(BaseStyles.destinationBlendName, (int)BlendMode.One); - material.SetInt(BaseStyles.depthWriteName, (int)DepthWrite.Off); - material.DisableKeyword(BaseStyles.alphaTestOnName); - material.EnableKeyword(BaseStyles.alphaBlendOnName); - material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Transparent; - } - break; - - case RenderingMode.Custom: - { - material.SetOverrideTag(BaseStyles.renderTypeName, BaseStyles.customRenderingModeNames[(int)customMode]); - // _SrcBlend, _DstBlend, _BlendOp, _ZTest, _ZWrite, _ColorWriteMask are controlled by UI. - - switch (customMode) - { - case CustomRenderingMode.Opaque: - { - material.DisableKeyword(BaseStyles.alphaTestOnName); - material.DisableKeyword(BaseStyles.alphaBlendOnName); - } - break; - - case CustomRenderingMode.TransparentCutout: - { - material.EnableKeyword(BaseStyles.alphaTestOnName); - material.DisableKeyword(BaseStyles.alphaBlendOnName); - } - break; - - case CustomRenderingMode.Transparent: - { - material.DisableKeyword(BaseStyles.alphaTestOnName); - material.EnableKeyword(BaseStyles.alphaBlendOnName); - } - break; - } - - material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : material.renderQueue; - } - break; - } - } - - /// - /// Check whether shader feature is enabled - /// - /// float property to check against - /// false if 0.0f, true otherwise - protected static bool PropertyEnabled(MaterialProperty property) - { - return !property.floatValue.Equals(0.0f); - } - - /// - /// Get the value of a given float property for a material - /// - /// material to check - /// name of property against material - /// if has property, then value of that property for current material, null otherwise - protected static float? GetFloatProperty(Material material, string propertyName) - { - if (material.HasProperty(propertyName)) - { - return material.GetFloat(propertyName); - } - - return null; - } - - /// - /// Get the value of a given vector property for a material - /// - /// material to check - /// name of property against material - /// if has property, then value of that property for current material, null otherwise - protected static Vector4? GetVectorProperty(Material material, string propertyName) - { - if (material.HasProperty(propertyName)) - { - return material.GetVector(propertyName); - } - - return null; - } - - /// - /// Get the value of a given color property for a material - /// - /// material to check - /// name of property against material - /// if has property, then value of that property for current material, null otherwise - protected static Color? GetColorProperty(Material material, string propertyName) - { - if (material.HasProperty(propertyName)) - { - return material.GetColor(propertyName); - } - - return null; - } - - /// - /// Sets the shader feature controlled by keyword and property name parameters active or inactive - /// - /// Material to modify - /// Keyword of shader feature - /// Associated property name for shader feature - /// float to be treated as a boolean flag for setting shader feature active or inactive - protected static void SetShaderFeatureActive(Material material, string keywordName, string propertyName, float? propertyValue) - { - if (propertyValue.HasValue) - { - if (keywordName != null) - { - if (!propertyValue.Value.Equals(0.0f)) - { - material.EnableKeyword(keywordName); - } - else - { - material.DisableKeyword(keywordName); - } - } - - material.SetFloat(propertyName, propertyValue.Value); - } - } - - /// - /// Sets vector property against associated material - /// - /// material to control - /// name of property to set - /// value of property to set - protected static void SetVectorProperty(Material material, string propertyName, Vector4? propertyValue) - { - if (propertyValue.HasValue) - { - material.SetVector(propertyName, propertyValue.Value); - } - } - - /// - /// Set color property against associated material - /// - /// material to control - /// name of property to set - /// value of property to set - protected static void SetColorProperty(Material material, string propertyName, Color? propertyValue) - { - if (propertyValue.HasValue) - { - material.SetColor(propertyName, propertyValue.Value); - } - } - } -} \ No newline at end of file diff --git a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs.meta b/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs.meta deleted file mode 100644 index 2e5cd20c3..000000000 --- a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityShaderGUI.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 5c455a0029df0144a8d8bd9b27f781eb -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {fileID: 2800000, guid: 8ac5213854cf4dbabd140decf8df1946, type: 3} - userData: - assetBundleName: - assetBundleVariant: diff --git a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityStandardShaderGUI.cs b/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityStandardShaderGUI.cs index 5684badd2..31e4ea2b0 100644 --- a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityStandardShaderGUI.cs +++ b/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityStandardShaderGUI.cs @@ -2,20 +2,37 @@ // Licensed under the MIT License. using System; -using System.IO; using UnityEditor; using UnityEngine; using UnityEngine.Rendering; using XRTK.Utilities; using XRTK.Utilities.Rendering; +using Object = UnityEngine.Object; namespace XRTK.Inspectors { /// /// A custom shader inspector for the "Mixed Reality Toolkit/Standard" shader. /// - public class MixedRealityStandardShaderGUI : MixedRealityShaderGUI + public class MixedRealityStandardShaderGUI : ShaderGUI { + protected enum RenderingMode + { + Opaque, + TransparentCutout, + Transparent, + PremultipliedTransparent, + Additive, + Custom + } + + protected enum CustomRenderingMode + { + Opaque, + TransparentCutout, + Transparent + } + protected enum AlbedoAlphaMode { Transparency, @@ -23,24 +40,52 @@ protected enum AlbedoAlphaMode Smoothness } + protected enum DepthWrite + { + Off, + On + } + protected static class Styles { public static string primaryMapsTitle = "Main Maps"; public static string renderingOptionsTitle = "Rendering Options"; public static string advancedOptionsTitle = "Advanced Options"; public static string fluentOptionsTitle = "Fluent Options"; + public static string renderTypeName = "RenderType"; + public static string renderingModeName = "_Mode"; + public static string customRenderingModeName = "_CustomMode"; + public static string sourceBlendName = "_SrcBlend"; + public static string destinationBlendName = "_DstBlend"; + public static string blendOperationName = "_BlendOp"; + public static string depthTestName = "_ZTest"; + public static string depthWriteName = "_ZWrite"; + public static string colorWriteMaskName = "_ColorWriteMask"; public static string instancedColorName = "_InstancedColor"; public static string instancedColorFeatureName = "_INSTANCED_COLOR"; public static string stencilComparisonName = "_StencilComparison"; public static string stencilOperationName = "_StencilOperation"; + public static string alphaTestOnName = "_ALPHATEST_ON"; + public static string alphaBlendOnName = "_ALPHABLEND_ON"; public static string disableAlbedoMapName = "_DISABLE_ALBEDO_MAP"; public static string albedoMapAlphaMetallicName = "_METALLIC_TEXTURE_ALBEDO_CHANNEL_A"; public static string albedoMapAlphaSmoothnessName = "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A"; - public static string propertiesComponentHelp = "Use the {0} component(s) to control {1} properties."; + public static string propertiesComponentHelp = "Use the {0} component to control {1} properties."; + public static readonly string[] renderingModeNames = Enum.GetNames(typeof(RenderingMode)); + public static readonly string[] customRenderingModeNames = Enum.GetNames(typeof(CustomRenderingMode)); public static readonly string[] albedoAlphaModeNames = Enum.GetNames(typeof(AlbedoAlphaMode)); + public static readonly string[] depthWriteNames = Enum.GetNames(typeof(DepthWrite)); + public static GUIContent sourceBlend = new GUIContent("Source Blend", "Blend Mode of Newly Calculated Color"); + public static GUIContent destinationBlend = new GUIContent("Destination Blend", "Blend Mode of Existing Color"); + public static GUIContent blendOperation = new GUIContent("Blend Operation", "Operation for Blending New Color With Existing Color"); + public static GUIContent depthTest = new GUIContent("Depth Test", "How Should Depth Testing Be Performed."); + public static GUIContent depthWrite = new GUIContent("Depth Write", "Controls Whether Pixels From This Object Are Written to the Depth Buffer"); + public static GUIContent colorWriteMask = new GUIContent("Color Write Mask", "Color Channel Writing Mask"); public static GUIContent instancedColor = new GUIContent("Instanced Color", "Enable a Unique Color Per Instance"); + public static GUIContent cullMode = new GUIContent("Cull Mode", "Triangle Culling Mode"); + public static GUIContent renderQueueOverride = new GUIContent("Render Queue Override", "Manually Override the Render Queue"); public static GUIContent albedo = new GUIContent("Albedo", "Albedo (RGB) and Transparency (Alpha)"); - public static GUIContent albedoAssignedAtRuntime = new GUIContent("Assigned at Runtime", "As an optimization albedo operations are disabled when no albedo texture is specified. If a albedo texture will be specified at runtime enable this option."); + public static GUIContent albedoAssignedAtRuntime = new GUIContent("Albedo Assigned at Runtime", "As an optimization albedo operations are disabled when no albedo texture is specified. If a albedo texture will be specified at runtime enable this option."); public static GUIContent alphaCutoff = new GUIContent("Alpha Cutoff", "Threshold for Alpha Cutoff"); public static GUIContent metallic = new GUIContent("Metallic", "Metallic Value"); public static GUIContent smoothness = new GUIContent("Smoothness", "Smoothness Value"); @@ -64,28 +109,21 @@ protected static class Styles public static GUIContent rimColor = new GUIContent("Color", "Rim Highlight Color"); public static GUIContent rimPower = new GUIContent("Power", "Rim Highlight Saturation"); public static GUIContent vertexColors = new GUIContent("Vertex Colors", "Enable Vertex Color Tinting"); - public static GUIContent vertexExtrusion = new GUIContent("Vertex Extrusion", "Enable Vertex Extrusion Along the Vertex Normal"); - public static GUIContent vertexExtrusionValue = new GUIContent("Vertex Extrusion Value", "How Far to Extrude the Vertex Along the Vertex Normal"); - public static GUIContent blendedClippingWidth = new GUIContent("Blended Clipping Width", "The Width of the Clipping Primitive Clip Fade Region on Non-Cutout Materials"); + public static GUIContent clippingPlane = new GUIContent("Clipping Plane", "Enable Clipping Against a Plane"); + public static GUIContent clippingSphere = new GUIContent("Clipping Sphere", "Enable Clipping Against a Sphere"); + public static GUIContent clippingBox = new GUIContent("Clipping Box", "Enable Clipping Against a Box"); public static GUIContent clippingBorder = new GUIContent("Clipping Border", "Enable a Border Along the Clipping Primitive's Edge"); public static GUIContent clippingBorderWidth = new GUIContent("Width", "Width of the Clipping Border"); public static GUIContent clippingBorderColor = new GUIContent("Color", "Interpolated Color of the Clipping Border"); public static GUIContent nearPlaneFade = new GUIContent("Near Fade", "Objects Disappear (Turn to Black/Transparent) as the Camera (or Hover/Proximity Light) Nears Them"); public static GUIContent nearLightFade = new GUIContent("Use Light", "A Hover or Proximity Light (Rather Than the Camera) Determines Near Fade Distance"); - public static GUIContent fadeBeginDistance = new GUIContent("Fade Begin", "Distance From Camera (or Hover/Proximity Light) to Begin Fade In"); - public static GUIContent fadeCompleteDistance = new GUIContent("Fade Complete", "Distance From Camera (or Hover/Proximity Light) When Fade is Fully In"); - public static GUIContent fadeMinValue = new GUIContent("Fade Min Value", "Clamps the Fade Amount to a Minimum Value"); + public static GUIContent fadeBeginDistance = new GUIContent("Fade Begin", "Distance From Camera to Begin Fade In"); + public static GUIContent fadeCompleteDistance = new GUIContent("Fade Complete", "Distance From Camera When Fade is Fully In"); public static GUIContent hoverLight = new GUIContent("Hover Light", "Enable utilization of Hover Light(s)"); - public static GUIContent enableHoverColorOverride = new GUIContent("Override Color", "Override Global Hover Light Color for this Material"); + public static GUIContent enableHoverColorOverride = new GUIContent("Override Color", "Override Global Hover Light Color"); public static GUIContent hoverColorOverride = new GUIContent("Color", "Override Hover Light Color"); public static GUIContent proximityLight = new GUIContent("Proximity Light", "Enable utilization of Proximity Light(s)"); - public static GUIContent enableProximityLightColorOverride = new GUIContent("Override Color", "Override Global Proximity Light Color for this Material"); - public static GUIContent proximityLightCenterColorOverride = new GUIContent("Center Color", "The Override Color of the ProximityLight Gradient at the Center (RGB) and (A) is Gradient Extent"); - public static GUIContent proximityLightMiddleColorOverride = new GUIContent("Middle Color", "The Override Color of the ProximityLight Gradient at the Middle (RGB) and (A) is Gradient Extent"); - public static GUIContent proximityLightOuterColorOverride = new GUIContent("Outer Color", "The Override Color of the ProximityLight Gradient at the Outer Edge (RGB) and (A) is Gradient Extent"); - public static GUIContent proximityLightSubtractive = new GUIContent("Subtractive", "Proximity Lights Remove Light from a Surface, Used to Mimic a Shadow"); public static GUIContent proximityLightTwoSided = new GUIContent("Two Sided", "Proximity Lights Apply to Both Sides of a Surface"); - public static GUIContent fluentLightIntensity = new GUIContent("Light Intensity", "Intensity Scaler for All Hover and Proximity Lights"); public static GUIContent roundCorners = new GUIContent("Round Corners", "(Assumes UVs Specify Borders of Surface, Works Best on Unity Cube, Quad, and Plane)"); public static GUIContent roundCornerRadius = new GUIContent("Unit Radius", "Rounded Rectangle Corner Unit Sphere Radius"); public static GUIContent roundCornerMargin = new GUIContent("Margin %", "Distance From Geometry Edge"); @@ -117,7 +155,19 @@ protected static class Styles public static GUIContent stencilOperation = new GUIContent("Stencil Operation", "What to do When the Stencil Test Passes"); } + protected bool initialized; + + protected MaterialProperty renderingMode; + protected MaterialProperty customRenderingMode; + protected MaterialProperty sourceBlend; + protected MaterialProperty destinationBlend; + protected MaterialProperty blendOperation; + protected MaterialProperty depthTest; + protected MaterialProperty depthWrite; + protected MaterialProperty colorWriteMask; protected MaterialProperty instancedColor; + protected MaterialProperty cullMode; + protected MaterialProperty renderQueueOverride; protected MaterialProperty albedoMap; protected MaterialProperty albedoColor; protected MaterialProperty albedoAlphaMode; @@ -145,9 +195,9 @@ protected static class Styles protected MaterialProperty rimColor; protected MaterialProperty rimPower; protected MaterialProperty vertexColors; - protected MaterialProperty vertexExtrusion; - protected MaterialProperty vertexExtrusionValue; - protected MaterialProperty blendedClippingWidth; + protected MaterialProperty clippingPlane; + protected MaterialProperty clippingSphere; + protected MaterialProperty clippingBox; protected MaterialProperty clippingBorder; protected MaterialProperty clippingBorderWidth; protected MaterialProperty clippingBorderColor; @@ -155,18 +205,11 @@ protected static class Styles protected MaterialProperty nearLightFade; protected MaterialProperty fadeBeginDistance; protected MaterialProperty fadeCompleteDistance; - protected MaterialProperty fadeMinValue; protected MaterialProperty hoverLight; protected MaterialProperty enableHoverColorOverride; protected MaterialProperty hoverColorOverride; protected MaterialProperty proximityLight; - protected MaterialProperty enableProximityLightColorOverride; - protected MaterialProperty proximityLightCenterColorOverride; - protected MaterialProperty proximityLightMiddleColorOverride; - protected MaterialProperty proximityLightOuterColorOverride; - protected MaterialProperty proximityLightSubtractive; protected MaterialProperty proximityLightTwoSided; - protected MaterialProperty fluentLightIntensity; protected MaterialProperty roundCorners; protected MaterialProperty roundCornerRadius; protected MaterialProperty roundCornerMargin; @@ -196,12 +239,22 @@ protected static class Styles protected MaterialProperty stencilReference; protected MaterialProperty stencilComparison; protected MaterialProperty stencilOperation; + private static readonly int BumpMap = Shader.PropertyToID("_BumpMap"); + private static readonly int NormalMap = Shader.PropertyToID("_NormalMap"); - protected override void FindProperties(MaterialProperty[] props) + protected void FindProperties(MaterialProperty[] props) { - base.FindProperties(props); - + renderingMode = FindProperty(Styles.renderingModeName, props); + customRenderingMode = FindProperty(Styles.customRenderingModeName, props); + sourceBlend = FindProperty(Styles.sourceBlendName, props); + destinationBlend = FindProperty(Styles.destinationBlendName, props); + blendOperation = FindProperty(Styles.blendOperationName, props); + depthTest = FindProperty(Styles.depthTestName, props); + depthWrite = FindProperty(Styles.depthWriteName, props); + colorWriteMask = FindProperty(Styles.colorWriteMaskName, props); instancedColor = FindProperty(Styles.instancedColorName, props); + cullMode = FindProperty("_CullMode", props); + renderQueueOverride = FindProperty("_RenderQueueOverride", props); albedoMap = FindProperty("_MainTex", props); albedoColor = FindProperty("_Color", props); albedoAlphaMode = FindProperty("_AlbedoAlphaMode", props); @@ -229,9 +282,9 @@ protected override void FindProperties(MaterialProperty[] props) rimColor = FindProperty("_RimColor", props); rimPower = FindProperty("_RimPower", props); vertexColors = FindProperty("_VertexColors", props); - vertexExtrusion = FindProperty("_VertexExtrusion", props); - vertexExtrusionValue = FindProperty("_VertexExtrusionValue", props); - blendedClippingWidth = FindProperty("_BlendedClippingWidth", props); + clippingPlane = FindProperty("_ClippingPlane", props); + clippingSphere = FindProperty("_ClippingSphere", props); + clippingBox = FindProperty("_ClippingBox", props); clippingBorder = FindProperty("_ClippingBorder", props); clippingBorderWidth = FindProperty("_ClippingBorderWidth", props); clippingBorderColor = FindProperty("_ClippingBorderColor", props); @@ -239,18 +292,11 @@ protected override void FindProperties(MaterialProperty[] props) nearLightFade = FindProperty("_NearLightFade", props); fadeBeginDistance = FindProperty("_FadeBeginDistance", props); fadeCompleteDistance = FindProperty("_FadeCompleteDistance", props); - fadeMinValue = FindProperty("_FadeMinValue", props); hoverLight = FindProperty("_HoverLight", props); enableHoverColorOverride = FindProperty("_EnableHoverColorOverride", props); hoverColorOverride = FindProperty("_HoverColorOverride", props); proximityLight = FindProperty("_ProximityLight", props); - enableProximityLightColorOverride = FindProperty("_EnableProximityLightColorOverride", props); - proximityLightCenterColorOverride = FindProperty("_ProximityLightCenterColorOverride", props); - proximityLightMiddleColorOverride = FindProperty("_ProximityLightMiddleColorOverride", props); - proximityLightOuterColorOverride = FindProperty("_ProximityLightOuterColorOverride", props); - proximityLightSubtractive = FindProperty("_ProximityLightSubtractive", props); proximityLightTwoSided = FindProperty("_ProximityLightTwoSided", props); - fluentLightIntensity = FindProperty("_FluentLightIntensity", props); roundCorners = FindProperty("_RoundCorners", props); roundCornerRadius = FindProperty("_RoundCornerRadius", props); roundCornerMargin = FindProperty("_RoundCornerMargin", props); @@ -284,10 +330,12 @@ protected override void FindProperties(MaterialProperty[] props) public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props) { - Material material = (Material)materialEditor.target; + var material = (Material)materialEditor.target; - base.OnGUI(materialEditor, props); + FindProperties(props); + Initialize(material); + RenderingModeOptions(materialEditor); MainMapOptions(materialEditor, material); RenderingOptions(materialEditor, material); FluentOptions(materialEditor, material); @@ -297,32 +345,32 @@ public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] pro public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader) { // Cache old shader properties with potentially different names than the new shader. - float? smoothness = GetFloatProperty(material, "_Glossiness"); + float? smoothnessProperty = GetFloatProperty(material, "_Glossiness"); float? diffuse = GetFloatProperty(material, "_UseDiffuse"); - float? specularHighlights = GetFloatProperty(material, "_SpecularHighlights"); - float? normalMap = null; - Texture normalMapTexture = material.GetTexture("_BumpMap"); - float? normalMapScale = GetFloatProperty(material, "_BumpScale"); + float? specularHighlightsProperty = GetFloatProperty(material, "_SpecularHighlights"); + float? normalMapProperty = null; + Texture normalMapTexture = material.GetTexture(BumpMap); + float? normalMapScaleProperty = GetFloatProperty(material, "_BumpScale"); float? emission = null; Color? emissionColor = GetColorProperty(material, "_EmissionColor"); - float? reflections = null; + float? reflectionsProperty = null; float? rimLighting = null; Vector4? textureScaleOffset = null; - float? cullMode = GetFloatProperty(material, "_Cull"); + float? cullModeProperty = GetFloatProperty(material, "_Cull"); if (oldShader) { if (oldShader.name.Contains("Standard")) { - normalMap = material.IsKeywordEnabled("_NORMALMAP") ? 1.0f : 0.0f; + normalMapProperty = material.IsKeywordEnabled("_NORMALMAP") ? 1.0f : 0.0f; emission = material.IsKeywordEnabled("_EMISSION") ? 1.0f : 0.0f; - reflections = GetFloatProperty(material, "_GlossyReflections"); + reflectionsProperty = GetFloatProperty(material, "_GlossyReflections"); } else if (oldShader.name.Contains("Fast Configurable")) { - normalMap = material.IsKeywordEnabled("_USEBUMPMAP_ON") ? 1.0f : 0.0f; + normalMapProperty = material.IsKeywordEnabled("_USEBUMPMAP_ON") ? 1.0f : 0.0f; emission = GetFloatProperty(material, "_UseEmissionColor"); - reflections = GetFloatProperty(material, "_UseReflections"); + reflectionsProperty = GetFloatProperty(material, "_UseReflections"); rimLighting = GetFloatProperty(material, "_UseRimLighting"); textureScaleOffset = GetVectorProperty(material, "_TextureScaleOffset"); } @@ -331,73 +379,116 @@ public override void AssignNewShaderToMaterial(Material material, Shader oldShad base.AssignNewShaderToMaterial(material, oldShader, newShader); // Apply old shader properties to the new shader. - SetShaderFeatureActive(material, null, "_Smoothness", smoothness); - SetShaderFeatureActive(material, "_DIRECTIONAL_LIGHT", "_DirectionalLight", diffuse); - SetShaderFeatureActive(material, "_SPECULAR_HIGHLIGHTS", "_SpecularHighlights", specularHighlights); - SetShaderFeatureActive(material, "_NORMAL_MAP", "_EnableNormalMap", normalMap); + SetFloatProperty(material, null, "_Smoothness", smoothnessProperty); + SetFloatProperty(material, "_DIRECTIONAL_LIGHT", "_DirectionalLight", diffuse); + SetFloatProperty(material, "_SPECULAR_HIGHLIGHTS", "_SpecularHighlights", specularHighlightsProperty); + SetFloatProperty(material, "_NORMAL_MAP", "_EnableNormalMap", normalMapProperty); if (normalMapTexture) { - material.SetTexture("_NormalMap", normalMapTexture); + material.SetTexture(NormalMap, normalMapTexture); } - SetShaderFeatureActive(material, null, "_NormalMapScale", normalMapScale); - SetShaderFeatureActive(material, "_EMISSION", "_EnableEmission", emission); + SetFloatProperty(material, null, "_NormalMapScale", normalMapScaleProperty); + SetFloatProperty(material, "_EMISSION", "_EnableEmission", emission); SetColorProperty(material, "_EmissiveColor", emissionColor); - SetShaderFeatureActive(material, "_REFLECTIONS", "_Reflections", reflections); - SetShaderFeatureActive(material, "_RIM_LIGHT", "_RimLight", rimLighting); + SetFloatProperty(material, "_REFLECTIONS", "_Reflections", reflectionsProperty); + SetFloatProperty(material, "_RIM_LIGHT", "_RimLight", rimLighting); SetVectorProperty(material, "_MainTex_ST", textureScaleOffset); - SetShaderFeatureActive(material, null, "_CullMode", cullMode); + SetFloatProperty(material, null, "_CullMode", cullModeProperty); // Setup the rendering mode based on the old shader. - if (oldShader == null || !oldShader.name.Contains(LegacyShadersPath)) + if (oldShader == null || !oldShader.name.Contains("Legacy Shaders/")) { - SetupMaterialWithRenderingMode(material, (RenderingMode)material.GetFloat(BaseStyles.renderingModeName), CustomRenderingMode.Opaque, -1); + SetupMaterialWithRenderingMode(material, (RenderingMode)material.GetFloat(Styles.renderingModeName), CustomRenderingMode.Opaque, -1); } else { RenderingMode mode = RenderingMode.Opaque; - if (oldShader.name.Contains(TransparentCutoutShadersPath)) + if (oldShader.name.Contains("/Transparent/Cutout/")) { mode = RenderingMode.TransparentCutout; } - else if (oldShader.name.Contains(TransparentShadersPath)) + else if (oldShader.name.Contains("/Transparent/")) { mode = RenderingMode.Transparent; } - material.SetFloat(BaseStyles.renderingModeName, (float)mode); + material.SetFloat(Styles.renderingModeName, (float)mode); MaterialChanged(material); } } - protected override void MaterialChanged(Material material) + protected void Initialize(Material material) { - SetupMaterialWithAlbedo(material, albedoMap, albedoAlphaMode, albedoAssignedAtRuntime); + if (!initialized) + { + MaterialChanged(material); + initialized = true; + } + } - base.MaterialChanged(material); + protected void MaterialChanged(Material material) + { + SetupMaterialWithAlbedo(material, albedoMap, albedoAlphaMode, albedoAssignedAtRuntime); + SetupMaterialWithRenderingMode(material, (RenderingMode)renderingMode.floatValue, (CustomRenderingMode)customRenderingMode.floatValue, (int)renderQueueOverride.floatValue); } - protected void MainMapOptions(MaterialEditor materialEditor, Material material) + protected void RenderingModeOptions(MaterialEditor materialEditor) { - GUILayout.Label(Styles.primaryMapsTitle, EditorStyles.boldLabel); + EditorGUI.BeginChangeCheck(); - materialEditor.TexturePropertySingleLine(Styles.albedo, albedoMap, albedoColor); + EditorGUI.showMixedValue = renderingMode.hasMixedValue; + RenderingMode mode = (RenderingMode)renderingMode.floatValue; + EditorGUI.BeginChangeCheck(); + mode = (RenderingMode)EditorGUILayout.Popup(renderingMode.displayName, (int)mode, Styles.renderingModeNames); - if (albedoMap.textureValue == null) + if (EditorGUI.EndChangeCheck()) + { + materialEditor.RegisterPropertyChangeUndo(renderingMode.displayName); + renderingMode.floatValue = (float)mode; + } + + EditorGUI.showMixedValue = false; + + if (EditorGUI.EndChangeCheck()) + { + foreach (var target in renderingMode.targets) + { + MaterialChanged((Material)target); + } + } + + if ((RenderingMode)renderingMode.floatValue == RenderingMode.Custom) { - materialEditor.ShaderProperty(albedoAssignedAtRuntime, Styles.albedoAssignedAtRuntime, 2); + EditorGUI.indentLevel += 2; + customRenderingMode.floatValue = EditorGUILayout.Popup(customRenderingMode.displayName, (int)customRenderingMode.floatValue, Styles.customRenderingModeNames); + materialEditor.ShaderProperty(sourceBlend, Styles.sourceBlend); + materialEditor.ShaderProperty(destinationBlend, Styles.destinationBlend); + materialEditor.ShaderProperty(blendOperation, Styles.blendOperation); + materialEditor.ShaderProperty(depthTest, Styles.depthTest); + depthWrite.floatValue = EditorGUILayout.Popup(depthWrite.displayName, (int)depthWrite.floatValue, Styles.depthWriteNames); + materialEditor.ShaderProperty(colorWriteMask, Styles.colorWriteMask); + EditorGUI.indentLevel -= 2; } + materialEditor.ShaderProperty(cullMode, Styles.cullMode); + } + + protected void MainMapOptions(MaterialEditor materialEditor, Material material) + { + GUILayout.Label(Styles.primaryMapsTitle, EditorStyles.boldLabel); + + materialEditor.TexturePropertySingleLine(Styles.albedo, albedoMap, albedoColor); materialEditor.ShaderProperty(enableChannelMap, Styles.enableChannelMap); if (PropertyEnabled(enableChannelMap)) { EditorGUI.indentLevel += 2; materialEditor.TexturePropertySingleLine(Styles.channelMap, channelMap); - GUILayout.Box("Metallic (Red), Occlusion (Green), Emission (Blue), Smoothness (Alpha)", EditorStyles.helpBox, new GUILayoutOption[0]); + GUILayout.Box("Metallic (Red), Occlusion (Green), Emission (Blue), Smoothness (Alpha)", EditorStyles.helpBox); EditorGUI.indentLevel -= 2; } @@ -407,8 +498,7 @@ protected void MainMapOptions(MaterialEditor materialEditor, Material material) albedoAlphaMode.floatValue = EditorGUILayout.Popup(albedoAlphaMode.displayName, (int)albedoAlphaMode.floatValue, Styles.albedoAlphaModeNames); - if ((RenderingMode)renderingMode.floatValue == RenderingMode.TransparentCutout || - (RenderingMode)renderingMode.floatValue == RenderingMode.Custom) + if ((RenderingMode)renderingMode.floatValue == RenderingMode.TransparentCutout) { materialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoff.text); } @@ -498,27 +588,36 @@ protected void RenderingOptions(MaterialEditor materialEditor, Material material materialEditor.ShaderProperty(vertexColors, Styles.vertexColors); - materialEditor.ShaderProperty(vertexExtrusion, Styles.vertexExtrusion); + materialEditor.ShaderProperty(clippingPlane, Styles.clippingPlane); - if (PropertyEnabled(vertexExtrusion)) + if (PropertyEnabled(clippingPlane)) { - materialEditor.ShaderProperty(vertexExtrusionValue, Styles.vertexExtrusionValue, 2); + GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ClippingPlane), Styles.clippingPlane.text), EditorStyles.helpBox); } - if ((RenderingMode)renderingMode.floatValue != RenderingMode.Opaque && - (RenderingMode)renderingMode.floatValue != RenderingMode.TransparentCutout) + materialEditor.ShaderProperty(clippingSphere, Styles.clippingSphere); + + if (PropertyEnabled(clippingSphere)) { - materialEditor.ShaderProperty(blendedClippingWidth, Styles.blendedClippingWidth); - GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ClippingPrimitive), "other clipping"), EditorStyles.helpBox, new GUILayoutOption[0]); + GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ClippingSphere), Styles.clippingSphere.text), EditorStyles.helpBox); } - materialEditor.ShaderProperty(clippingBorder, Styles.clippingBorder); + materialEditor.ShaderProperty(clippingBox, Styles.clippingBox); - if (PropertyEnabled(clippingBorder)) + if (PropertyEnabled(clippingBox)) { - materialEditor.ShaderProperty(clippingBorderWidth, Styles.clippingBorderWidth, 2); - materialEditor.ShaderProperty(clippingBorderColor, Styles.clippingBorderColor, 2); - GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ClippingPrimitive), "other clipping"), EditorStyles.helpBox, new GUILayoutOption[0]); + GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ClippingBox), Styles.clippingBox.text), EditorStyles.helpBox); + } + + if (PropertyEnabled(clippingPlane) || PropertyEnabled(clippingSphere) || PropertyEnabled(clippingBox)) + { + materialEditor.ShaderProperty(clippingBorder, Styles.clippingBorder); + + if (PropertyEnabled(clippingBorder)) + { + materialEditor.ShaderProperty(clippingBorderWidth, Styles.clippingBorderWidth, 2); + materialEditor.ShaderProperty(clippingBorderColor, Styles.clippingBorderColor, 2); + } } materialEditor.ShaderProperty(nearPlaneFade, Styles.nearPlaneFade); @@ -528,7 +627,6 @@ protected void RenderingOptions(MaterialEditor materialEditor, Material material materialEditor.ShaderProperty(nearLightFade, Styles.nearLightFade, 2); materialEditor.ShaderProperty(fadeBeginDistance, Styles.fadeBeginDistance, 2); materialEditor.ShaderProperty(fadeCompleteDistance, Styles.fadeCompleteDistance, 2); - materialEditor.ShaderProperty(fadeMinValue, Styles.fadeMinValue, 2); } } @@ -536,14 +634,14 @@ protected void FluentOptions(MaterialEditor materialEditor, Material material) { EditorGUILayout.Space(); GUILayout.Label(Styles.fluentOptionsTitle, EditorStyles.boldLabel); - RenderingMode mode = (RenderingMode)renderingMode.floatValue; - CustomRenderingMode customMode = (CustomRenderingMode)customRenderingMode.floatValue; + var mode = (RenderingMode)renderingMode.floatValue; + var customMode = (CustomRenderingMode)customRenderingMode.floatValue; materialEditor.ShaderProperty(hoverLight, Styles.hoverLight); if (PropertyEnabled(hoverLight)) { - GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(HoverLight), Styles.hoverLight.text), EditorStyles.helpBox, new GUILayoutOption[0]); + GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(HoverLight), Styles.hoverLight.text), EditorStyles.helpBox); materialEditor.ShaderProperty(enableHoverColorOverride, Styles.enableHoverColorOverride, 2); @@ -557,18 +655,16 @@ protected void FluentOptions(MaterialEditor materialEditor, Material material) if (PropertyEnabled(proximityLight)) { - materialEditor.ShaderProperty(enableProximityLightColorOverride, Styles.enableProximityLightColorOverride, 2); + materialEditor.ShaderProperty(proximityLightTwoSided, Styles.proximityLightTwoSided, 2); + GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ProximityLight), Styles.proximityLight.text), EditorStyles.helpBox); + } - if (PropertyEnabled(enableProximityLightColorOverride)) - { - materialEditor.ShaderProperty(proximityLightCenterColorOverride, Styles.proximityLightCenterColorOverride, 4); - materialEditor.ShaderProperty(proximityLightMiddleColorOverride, Styles.proximityLightMiddleColorOverride, 4); - materialEditor.ShaderProperty(proximityLightOuterColorOverride, Styles.proximityLightOuterColorOverride, 4); - } + materialEditor.ShaderProperty(roundCorners, Styles.roundCorners); - materialEditor.ShaderProperty(proximityLightSubtractive, Styles.proximityLightSubtractive, 2); - materialEditor.ShaderProperty(proximityLightTwoSided, Styles.proximityLightTwoSided, 2); - GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ProximityLight), Styles.proximityLight.text), EditorStyles.helpBox, new GUILayoutOption[0]); + if (PropertyEnabled(roundCorners)) + { + materialEditor.ShaderProperty(roundCornerRadius, Styles.roundCornerRadius, 2); + materialEditor.ShaderProperty(roundCornerMargin, Styles.roundCornerMargin, 2); } materialEditor.ShaderProperty(borderLight, Styles.borderLight); @@ -580,7 +676,7 @@ protected void FluentOptions(MaterialEditor materialEditor, Material material) materialEditor.ShaderProperty(borderMinValue, Styles.borderMinValue, 2); materialEditor.ShaderProperty(borderLightReplacesAlbedo, Styles.borderLightReplacesAlbedo, 2); - + if (PropertyEnabled(hoverLight) && PropertyEnabled(enableHoverColorOverride)) { materialEditor.ShaderProperty(borderLightUsesHoverColor, Styles.borderLightUsesHoverColor, 2); @@ -599,19 +695,6 @@ protected void FluentOptions(MaterialEditor materialEditor, Material material) } } - if (PropertyEnabled(hoverLight) || PropertyEnabled(proximityLight) || PropertyEnabled(borderLight)) - { - materialEditor.ShaderProperty(fluentLightIntensity, Styles.fluentLightIntensity); - } - - materialEditor.ShaderProperty(roundCorners, Styles.roundCorners); - - if (PropertyEnabled(roundCorners)) - { - materialEditor.ShaderProperty(roundCornerRadius, Styles.roundCornerRadius, 2); - materialEditor.ShaderProperty(roundCornerMargin, Styles.roundCornerMargin, 2); - } - if (PropertyEnabled(roundCorners) || PropertyEnabled(borderLight)) { materialEditor.ShaderProperty(edgeSmoothingValue, Styles.edgeSmoothingValue); @@ -656,7 +739,7 @@ protected void AdvancedOptions(MaterialEditor materialEditor, Material material) EditorGUI.BeginChangeCheck(); - materialEditor.ShaderProperty(renderQueueOverride, BaseStyles.renderQueueOverride); + materialEditor.ShaderProperty(renderQueueOverride, Styles.renderQueueOverride); if (EditorGUI.EndChangeCheck()) { @@ -667,15 +750,20 @@ protected void AdvancedOptions(MaterialEditor materialEditor, Material material) GUI.enabled = false; materialEditor.RenderQueueField(); - // Enable instancing to disable batching. Static and dynamic batching will normalize the object scale, which breaks - // features which utilize object scale. - GUI.enabled = !ScaleRequired(); + // When round corner or border light features are used, enable instancing to disable batching. Static and dynamic + // batching will normalize the object scale, which breaks border related features. + GUI.enabled = !PropertyEnabled(roundCorners) && !PropertyEnabled(borderLight); if (!GUI.enabled && !material.enableInstancing) { material.enableInstancing = true; } + if (albedoMap.textureValue == null) + { + materialEditor.ShaderProperty(albedoAssignedAtRuntime, Styles.albedoAssignedAtRuntime); + } + materialEditor.EnableInstancingField(); if (material.enableInstancing) @@ -686,7 +774,7 @@ protected void AdvancedOptions(MaterialEditor materialEditor, Material material) else { // When instancing is disable, disable instanced color. - SetShaderFeatureActive(material, Styles.instancedColorFeatureName, Styles.instancedColorName, 0.0f); + SetFloatProperty(material, Styles.instancedColorFeatureName, Styles.instancedColorName, 0.0f); } materialEditor.ShaderProperty(stencil, Styles.stencil); @@ -706,13 +794,6 @@ protected void AdvancedOptions(MaterialEditor materialEditor, Material material) } } - protected bool ScaleRequired() - { - return PropertyEnabled(roundCorners) || - PropertyEnabled(borderLight) || - (PropertyEnabled(enableTriplanarMapping) && PropertyEnabled(enableLocalSpaceTriplanarMapping)); - } - protected static void SetupMaterialWithAlbedo(Material material, MaterialProperty albedoMap, MaterialProperty albedoAlphaMode, MaterialProperty albedoAssignedAtRuntime) { if (albedoMap.textureValue || PropertyEnabled(albedoAssignedAtRuntime)) @@ -749,48 +830,194 @@ protected static void SetupMaterialWithAlbedo(Material material, MaterialPropert } } - [MenuItem("Mixed Reality Toolkit/Utilities/Upgrade MRTK Standard Shader for Lightweight Render Pipeline")] - protected static void UpgradeShaderForLightweightRenderPipeline() + protected static void SetupMaterialWithRenderingMode(Material material, RenderingMode mode, CustomRenderingMode customMode, int renderQueueOverride) { - if (EditorUtility.DisplayDialog("Upgrade MRTK Standard Shader?", - "This will alter the MRTK Standard Shader for use with Unity's Lightweight Render Pipeline. You cannot undo this action.", - "Ok", - "Cancel")) + switch (mode) { - string shaderName = "Mixed Reality Toolkit/Standard"; - string path = AssetDatabase.GetAssetPath(Shader.Find(shaderName)); + case RenderingMode.Opaque: + { + material.SetOverrideTag(Styles.renderTypeName, Styles.renderingModeNames[(int)RenderingMode.Opaque]); + material.SetInt(Styles.customRenderingModeName, (int)CustomRenderingMode.Opaque); + material.SetInt(Styles.sourceBlendName, (int)BlendMode.One); + material.SetInt(Styles.destinationBlendName, (int)BlendMode.Zero); + material.SetInt(Styles.blendOperationName, (int)BlendOp.Add); + material.SetInt(Styles.depthTestName, (int)CompareFunction.LessEqual); + material.SetInt(Styles.depthWriteName, (int)DepthWrite.On); + material.SetInt(Styles.colorWriteMaskName, (int)ColorWriteMask.All); + material.DisableKeyword(Styles.alphaTestOnName); + material.DisableKeyword(Styles.alphaBlendOnName); + material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Geometry; + } + break; - if (!string.IsNullOrEmpty(path)) - { - try + case RenderingMode.TransparentCutout: { - string upgradedShader = File.ReadAllText(path); - upgradedShader = upgradedShader.Replace("Tags{ \"RenderType\" = \"Opaque\" \"LightMode\" = \"ForwardBase\" }", - "Tags{ \"RenderType\" = \"Opaque\" \"LightMode\" = \"LightweightForward\" }"); - upgradedShader = upgradedShader.Replace("//#define _LIGHTWEIGHT_RENDER_PIPELINE", - "#define _LIGHTWEIGHT_RENDER_PIPELINE"); - File.WriteAllText(path, upgradedShader); - AssetDatabase.Refresh(); - - Debug.LogFormat("Upgraded {0} for use with the Lightweight Render Pipeline.", path); + material.SetOverrideTag(Styles.renderTypeName, Styles.renderingModeNames[(int)RenderingMode.TransparentCutout]); + material.SetInt(Styles.customRenderingModeName, (int)CustomRenderingMode.TransparentCutout); + material.SetInt(Styles.sourceBlendName, (int)BlendMode.One); + material.SetInt(Styles.destinationBlendName, (int)BlendMode.Zero); + material.SetInt(Styles.blendOperationName, (int)BlendOp.Add); + material.SetInt(Styles.depthTestName, (int)CompareFunction.LessEqual); + material.SetInt(Styles.depthWriteName, (int)DepthWrite.On); + material.SetInt(Styles.colorWriteMaskName, (int)ColorWriteMask.All); + material.EnableKeyword(Styles.alphaTestOnName); + material.DisableKeyword(Styles.alphaBlendOnName); + material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.AlphaTest; } - catch (Exception e) + break; + + case RenderingMode.Transparent: { - Debug.LogException(e); + material.SetOverrideTag(Styles.renderTypeName, Styles.renderingModeNames[(int)RenderingMode.Transparent]); + material.SetInt(Styles.customRenderingModeName, (int)CustomRenderingMode.Transparent); + material.SetInt(Styles.sourceBlendName, (int)BlendMode.SrcAlpha); + material.SetInt(Styles.destinationBlendName, (int)BlendMode.OneMinusSrcAlpha); + material.SetInt(Styles.blendOperationName, (int)BlendOp.Add); + material.SetInt(Styles.depthTestName, (int)CompareFunction.LessEqual); + material.SetInt(Styles.depthWriteName, (int)DepthWrite.Off); + material.SetInt(Styles.colorWriteMaskName, (int)ColorWriteMask.All); + material.DisableKeyword(Styles.alphaTestOnName); + material.EnableKeyword(Styles.alphaBlendOnName); + material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Transparent; } - } - else + break; + + case RenderingMode.PremultipliedTransparent: + { + material.SetOverrideTag(Styles.renderTypeName, Styles.renderingModeNames[(int)RenderingMode.Transparent]); + material.SetInt(Styles.customRenderingModeName, (int)CustomRenderingMode.Transparent); + material.SetInt(Styles.sourceBlendName, (int)BlendMode.One); + material.SetInt(Styles.destinationBlendName, (int)BlendMode.OneMinusSrcAlpha); + material.SetInt(Styles.blendOperationName, (int)BlendOp.Add); + material.SetInt(Styles.depthTestName, (int)CompareFunction.LessEqual); + material.SetInt(Styles.depthWriteName, (int)DepthWrite.Off); + material.SetInt(Styles.colorWriteMaskName, (int)ColorWriteMask.All); + material.DisableKeyword(Styles.alphaTestOnName); + material.EnableKeyword(Styles.alphaBlendOnName); + material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Transparent; + } + break; + + case RenderingMode.Additive: + { + material.SetOverrideTag(Styles.renderTypeName, Styles.renderingModeNames[(int)RenderingMode.Transparent]); + material.SetInt(Styles.customRenderingModeName, (int)CustomRenderingMode.Transparent); + material.SetInt(Styles.sourceBlendName, (int)BlendMode.One); + material.SetInt(Styles.destinationBlendName, (int)BlendMode.One); + material.SetInt(Styles.blendOperationName, (int)BlendOp.Add); + material.SetInt(Styles.depthTestName, (int)CompareFunction.LessEqual); + material.SetInt(Styles.depthWriteName, (int)DepthWrite.Off); + material.SetInt(Styles.colorWriteMaskName, (int)ColorWriteMask.All); + material.DisableKeyword(Styles.alphaTestOnName); + material.EnableKeyword(Styles.alphaBlendOnName); + material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : (int)RenderQueue.Transparent; + } + break; + + case RenderingMode.Custom: + { + material.SetOverrideTag(Styles.renderTypeName, Styles.customRenderingModeNames[(int)customMode]); + // _SrcBlend, _DstBlend, _BlendOp, _ZTest, _ZWrite, _ColorWriteMask are controlled by UI. + + switch (customMode) + { + case CustomRenderingMode.Opaque: + { + material.DisableKeyword(Styles.alphaTestOnName); + material.DisableKeyword(Styles.alphaBlendOnName); + } + break; + + case CustomRenderingMode.TransparentCutout: + { + material.EnableKeyword(Styles.alphaTestOnName); + material.DisableKeyword(Styles.alphaBlendOnName); + } + break; + + case CustomRenderingMode.Transparent: + { + material.DisableKeyword(Styles.alphaTestOnName); + material.EnableKeyword(Styles.alphaBlendOnName); + } + break; + } + + material.renderQueue = (renderQueueOverride >= 0) ? renderQueueOverride : material.renderQueue; + } + break; + } + } + + protected static bool PropertyEnabled(MaterialProperty property) + { + return !property.floatValue.Equals(0.0f); + } + + protected static float? GetFloatProperty(Material material, string propertyName) + { + if (material.HasProperty(propertyName)) + { + return material.GetFloat(propertyName); + } + + return null; + } + + protected static Vector4? GetVectorProperty(Material material, string propertyName) + { + if (material.HasProperty(propertyName)) + { + return material.GetVector(propertyName); + } + + return null; + } + + protected static Color? GetColorProperty(Material material, string propertyName) + { + if (material.HasProperty(propertyName)) + { + return material.GetColor(propertyName); + } + + return null; + } + + protected static void SetFloatProperty(Material material, string keywordName, string propertyName, float? propertyValue) + { + if (propertyValue.HasValue) + { + if (keywordName != null) { - Debug.LogErrorFormat("Failed to get asset path to: {0}", shaderName); + if (!propertyValue.Value.Equals(0.0f)) + { + material.EnableKeyword(keywordName); + } + else + { + material.DisableKeyword(keywordName); + } } + + material.SetFloat(propertyName, propertyValue.Value); + } + } + + protected static void SetVectorProperty(Material material, string propertyName, Vector4? propertyValue) + { + if (propertyValue.HasValue) + { + material.SetVector(propertyName, propertyValue.Value); } } - [MenuItem("Mixed Reality Toolkit/Utilities/Upgrade MRTK Standard Shader for Lightweight Render Pipeline", true)] - protected static bool UpgradeShaderForLightweightRenderPipelineValidate() + protected static void SetColorProperty(Material material, string propertyName, Color? propertyValue) { - // If a scriptable render pipeline is not present, no need to upgrade the shader. - return GraphicsSettings.renderPipelineAsset != null; + if (propertyValue.HasValue) + { + material.SetColor(propertyName, propertyValue.Value); + } } } } diff --git a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs b/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs deleted file mode 100644 index 265191f22..000000000 --- a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using UnityEditor; -using UnityEngine; - -namespace XRTK.Inspectors -{ - /// - /// A custom shader inspector for the "Mixed Reality Toolkit/Wireframe" shader. - /// - public class MixedRealityWireframeShaderGUI : MixedRealityShaderGUI - { - protected static class Styles - { - public static string mainPropertiesTitle = "Main Properties"; - public static string advancedOptionsTitle = "Advanced Options"; - - public static GUIContent baseColor = new GUIContent("Base Color", "Color of faces"); - public static GUIContent wireColor = new GUIContent("Wire Color", "Color of wires"); - public static GUIContent wireThickness = new GUIContent("Wire Thickness", "Thickness of wires"); - } - - protected MaterialProperty baseColor; - protected MaterialProperty wireColor; - protected MaterialProperty wireThickness; - - public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props) - { - Material material = (Material)materialEditor.target; - - base.OnGUI(materialEditor, props); - - GUILayout.Label(Styles.mainPropertiesTitle, EditorStyles.boldLabel); - materialEditor.ShaderProperty(baseColor, Styles.baseColor); - materialEditor.ShaderProperty(wireColor, Styles.wireColor); - materialEditor.ShaderProperty(wireThickness, Styles.wireThickness); - - AdvancedOptions(materialEditor, material); - } - - protected override void FindProperties(MaterialProperty[] props) - { - base.FindProperties(props); - - baseColor = FindProperty("_BaseColor", props); - wireColor = FindProperty("_WireColor", props); - wireThickness = FindProperty("_WireThickness", props); - } - - public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader) - { - float? cullMode = GetFloatProperty(material, "_Cull"); - - base.AssignNewShaderToMaterial(material, oldShader, newShader); - - SetShaderFeatureActive(material, null, BaseStyles.cullModeName, cullMode); - - // Setup the rendering mode based on the old shader. - if (oldShader == null || !oldShader.name.Contains(LegacyShadersPath)) - { - SetupMaterialWithRenderingMode(material, (RenderingMode)material.GetFloat(BaseStyles.renderingModeName), CustomRenderingMode.Opaque, -1); - } - else - { - RenderingMode mode = RenderingMode.Opaque; - - if (oldShader.name.Contains(TransparentCutoutShadersPath)) - { - mode = RenderingMode.TransparentCutout; - } - else if (oldShader.name.Contains(TransparentShadersPath)) - { - mode = RenderingMode.Transparent; - } - - material.SetFloat(BaseStyles.renderingModeName, (float)mode); - - MaterialChanged(material); - } - } - - protected void AdvancedOptions(MaterialEditor materialEditor, Material material) - { - GUILayout.Label(Styles.advancedOptionsTitle, EditorStyles.boldLabel); - - EditorGUILayout.Space(); - - EditorGUI.BeginChangeCheck(); - - materialEditor.ShaderProperty(renderQueueOverride, BaseStyles.renderQueueOverride); - - if (EditorGUI.EndChangeCheck()) - { - MaterialChanged(material); - } - - // Show the RenderQueueField but do not allow users to directly manipulate it. That is done via the renderQueueOverride. - GUI.enabled = false; - materialEditor.RenderQueueField(); - GUI.enabled = true; - - materialEditor.EnableInstancingField(); - } - } -} diff --git a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs.meta b/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs.meta deleted file mode 100644 index f0c7db055..000000000 --- a/XRTK-Core/Packages/com.xrtk.core/Inspectors/MixedRealityWireframeShaderGUI.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: beab471bae7ba484d8c3a51dc9c3cbf4 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {fileID: 2800000, guid: 8ac5213854cf4dbabd140decf8df1946, type: 3} - userData: - assetBundleName: - assetBundleVariant: diff --git a/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityStandard.shader b/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityStandard.shader index 806db5e22..24cac1ed7 100644 --- a/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityStandard.shader +++ b/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityStandard.shader @@ -1,4 +1,4 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. +// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. Shader "Mixed Reality Toolkit/Standard" @@ -35,30 +35,23 @@ Shader "Mixed Reality Toolkit/Standard" _RimColor("Rim Color", Color) = (0.5, 0.5, 0.5, 1.0) _RimPower("Rim Power", Range(0.0, 8.0)) = 0.25 [Toggle(_VERTEX_COLORS)] _VertexColors("Vertex Colors", Float) = 0.0 - [Toggle(_VERTEX_EXTRUSION)] _VertexExtrusion("Vertex Extrusion", Float) = 0.0 - _VertexExtrusionValue("Vertex Extrusion Value", Float) = 0.0 - _BlendedClippingWidth("Blended Clipping With", Range(0.0, 10.0)) = 1.0 + [Toggle(_CLIPPING_PLANE)] _ClippingPlane("Clipping Plane", Float) = 0.0 + [Toggle(_CLIPPING_SPHERE)] _ClippingSphere("Clipping Sphere", Float) = 0.0 + [Toggle(_CLIPPING_BOX)] _ClippingBox("Clipping Box", Float) = 0.0 [Toggle(_CLIPPING_BORDER)] _ClippingBorder("Clipping Border", Float) = 0.0 - _ClippingBorderWidth("Clipping Border Width", Range(0.0, 1.0)) = 0.025 + _ClippingBorderWidth("Clipping Border Width", Range(0.005, 1.0)) = 0.025 _ClippingBorderColor("Clipping Border Color", Color) = (1.0, 0.2, 0.0, 1.0) [Toggle(_NEAR_PLANE_FADE)] _NearPlaneFade("Near Plane Fade", Float) = 0.0 [Toggle(_NEAR_LIGHT_FADE)] _NearLightFade("Near Light Fade", Float) = 0.0 - _FadeBeginDistance("Fade Begin Distance", Range(0.0, 10.0)) = 0.85 - _FadeCompleteDistance("Fade Complete Distance", Range(0.0, 10.0)) = 0.5 - _FadeMinValue("Fade Min Value", Range(0.0, 1.0)) = 0.0 + _FadeBeginDistance("Fade Begin Distance", Range(0.01, 10.0)) = 0.85 + _FadeCompleteDistance("Fade Complete Distance", Range(0.01, 10.0)) = 0.5 // Fluent options. [Toggle(_HOVER_LIGHT)] _HoverLight("Hover Light", Float) = 1.0 [Toggle(_HOVER_COLOR_OVERRIDE)] _EnableHoverColorOverride("Hover Color Override", Float) = 0.0 _HoverColorOverride("Hover Color Override", Color) = (1.0, 1.0, 1.0, 1.0) [Toggle(_PROXIMITY_LIGHT)] _ProximityLight("Proximity Light", Float) = 0.0 - [Toggle(_PROXIMITY_LIGHT_COLOR_OVERRIDE)] _EnableProximityLightColorOverride("Proximity Light Color Override", Float) = 0.0 - [HDR]_ProximityLightCenterColorOverride("Proximity Light Center Color Override", Color) = (1.0, 0.0, 0.0, 0.0) - [HDR]_ProximityLightMiddleColorOverride("Proximity Light Middle Color Override", Color) = (0.0, 1.0, 0.0, 0.5) - [HDR]_ProximityLightOuterColorOverride("Proximity Light Outer Color Override", Color) = (0.0, 0.0, 1.0, 1.0) - [Toggle(_PROXIMITY_LIGHT_SUBTRACTIVE)] _ProximityLightSubtractive("Proximity Light Subtractive", Float) = 0.0 [Toggle(_PROXIMITY_LIGHT_TWO_SIDED)] _ProximityLightTwoSided("Proximity Light Two Sided", Float) = 0.0 - _FluentLightIntensity("Fluent Light Intensity", Range(0.0, 1.0)) = 1.0 [Toggle(_ROUND_CORNERS)] _RoundCorners("Round Corners", Float) = 0.0 _RoundCornerRadius("Round Corner Radius", Range(0.0, 0.5)) = 0.25 _RoundCornerMargin("Round Corner Margin", Range(0.0, 0.5)) = 0.01 @@ -68,7 +61,7 @@ Shader "Mixed Reality Toolkit/Standard" [Toggle(_BORDER_LIGHT_OPAQUE)] _BorderLightOpaque("Border Light Opaque", Float) = 0.0 _BorderWidth("Border Width", Range(0.0, 1.0)) = 0.1 _BorderMinValue("Border Min Value", Range(0.0, 1.0)) = 0.1 - _EdgeSmoothingValue("Edge Smoothing Value", Range(0.0, 0.2)) = 0.002 + _EdgeSmoothingValue("Edge Smoothing Value", Range(0.0001, 0.2)) = 0.002 _BorderLightOpaqueAlpha("Border Light Opaque Alpha", Range(0.0, 1.0)) = 1.0 [Toggle(_INNER_GLOW)] _InnerGlow("Inner Glow", Float) = 0.0 _InnerGlowColor("Inner Glow Color (RGB) and Intensity (A)", Color) = (1.0, 1.0, 1.0, 0.75) @@ -93,8 +86,6 @@ Shader "Mixed Reality Toolkit/Standard" [Enum(UnityEngine.Rendering.BlendOp)] _BlendOp("Blend Operation", Float) = 0 // "Add" [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("Depth Test", Float) = 4 // "LessEqual" [Enum(DepthWrite)] _ZWrite("Depth Write", Float) = 1 // "On" - _ZOffsetFactor("Depth Offset Factor", Float) = 0 // "Zero" - _ZOffsetUnits("Depth Offset Units", Float) = 0 // "Zero" [Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask("Color Write Mask", Float) = 15 // "All" [Enum(UnityEngine.Rendering.CullMode)] _CullMode("Cull Mode", Float) = 2 // "Back" _RenderQueueOverride("Render Queue Override", Range(-1.0, 5000)) = -1 @@ -125,9 +116,6 @@ Shader "Mixed Reality Toolkit/Standard" #include "UnityCG.cginc" #include "UnityMetaPass.cginc" - // This define will get commented in by the UpgradeShaderForLightweightRenderPipeline method. - //#define _LIGHTWEIGHT_RENDER_PIPELINE - struct v2f { float4 vertex : SV_POSITION; @@ -150,15 +138,7 @@ Shader "Mixed Reality Toolkit/Standard" fixed4 _Color; fixed4 _EmissiveColor; - -#if defined(_LIGHTWEIGHT_RENDER_PIPELINE) - CBUFFER_START(_LightBuffer) - float4 _MainLightPosition; - half4 _MainLightColor; - CBUFFER_END -#else fixed4 _LightColor0; -#endif half4 frag(v2f i) : SV_Target { @@ -173,11 +153,7 @@ Shader "Mixed Reality Toolkit/Standard" output.Emission += _EmissiveColor; #endif #endif -#if defined(_LIGHTWEIGHT_RENDER_PIPELINE) - output.SpecularColor = _MainLightColor.rgb; -#else output.SpecularColor = _LightColor0.rgb; -#endif return UnityMetaFragment(output); } @@ -187,14 +163,13 @@ Shader "Mixed Reality Toolkit/Standard" Pass { Name "Main" - Tags{ "RenderType" = "Opaque" "LightMode" = "ForwardBase" } + Tags{ "RenderType" = "Opaque" "LightMode" = "ForwardBase" "PerformanceChecks" = "False" } LOD 100 Blend[_SrcBlend][_DstBlend] BlendOp[_BlendOp] ZTest[_ZTest] ZWrite[_ZWrite] Cull[_CullMode] - Offset[_ZOffsetFactor],[_ZOffsetUnits] ColorMask[_ColorWriteMask] Stencil @@ -214,10 +189,8 @@ Shader "Mixed Reality Toolkit/Standard" #pragma multi_compile_instancing #pragma multi_compile _ LIGHTMAP_ON + #pragma multi_compile _ UNITY_COLORSPACE_GAMMA #pragma multi_compile _ _MULTI_HOVER_LIGHT - #pragma multi_compile _ _CLIPPING_PLANE - #pragma multi_compile _ _CLIPPING_SPHERE - #pragma multi_compile _ _CLIPPING_BOX #pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON #pragma shader_feature _DISABLE_ALBEDO_MAP @@ -234,15 +207,15 @@ Shader "Mixed Reality Toolkit/Standard" #pragma shader_feature _REFRACTION #pragma shader_feature _RIM_LIGHT #pragma shader_feature _VERTEX_COLORS - #pragma shader_feature _VERTEX_EXTRUSION + #pragma shader_feature _CLIPPING_PLANE + #pragma shader_feature _CLIPPING_SPHERE + #pragma shader_feature _CLIPPING_BOX #pragma shader_feature _CLIPPING_BORDER #pragma shader_feature _NEAR_PLANE_FADE #pragma shader_feature _NEAR_LIGHT_FADE #pragma shader_feature _HOVER_LIGHT #pragma shader_feature _HOVER_COLOR_OVERRIDE #pragma shader_feature _PROXIMITY_LIGHT - #pragma shader_feature _PROXIMITY_LIGHT_COLOR_OVERRIDE - #pragma shader_feature _PROXIMITY_LIGHT_SUBTRACTIVE #pragma shader_feature _PROXIMITY_LIGHT_TWO_SIDED #pragma shader_feature _ROUND_CORNERS #pragma shader_feature _BORDER_LIGHT @@ -260,9 +233,6 @@ Shader "Mixed Reality Toolkit/Standard" #include "UnityStandardConfig.cginc" #include "UnityStandardUtils.cginc" - // This define will get commented in by the UpgradeShaderForLightweightRenderPipeline method. - //#define _LIGHTWEIGHT_RENDER_PIPELINE - #if defined(_TRIPLANAR_MAPPING) || defined(_DIRECTIONAL_LIGHT) || defined(_SPHERICAL_HARMONICS) || defined(_REFLECTIONS) || defined(_RIM_LIGHT) || defined(_PROXIMITY_LIGHT) || defined(_ENVIRONMENT_COLORING) #define _NORMAL #else @@ -349,11 +319,8 @@ Shader "Mixed Reality Toolkit/Standard" #if defined(_VERTEX_COLORS) fixed4 color : COLOR0; #endif -#if defined(_SPHERICAL_HARMONICS) - fixed3 ambient : COLOR1; -#endif #if defined(_IRIDESCENCE) - fixed3 iridescentColor : COLOR2; + fixed3 iridescentColor : COLOR1; #endif #if defined(_WORLD_POSITION) #if defined(_NEAR_PLANE_FADE) @@ -367,15 +334,15 @@ Shader "Mixed Reality Toolkit/Standard" #endif #if defined(_NORMAL) #if defined(_TRIPLANAR_MAPPING) - fixed3 worldNormal : COLOR3; - fixed3 triplanarNormal : COLOR4; + fixed3 worldNormal : COLOR2; + fixed3 triplanarNormal : COLOR3; float3 triplanarPosition : TEXCOORD6; #elif defined(_NORMAL_MAP) - fixed3 tangentX : COLOR3; - fixed3 tangentY : COLOR4; - fixed3 tangentZ : COLOR5; + fixed3 tangentX : COLOR2; + fixed3 tangentY : COLOR3; + fixed3 tangentZ : COLOR4; #else - fixed3 worldNormal : COLOR3; + fixed3 worldNormal : COLOR2; #endif #endif UNITY_VERTEX_OUTPUT_STEREO @@ -419,15 +386,8 @@ Shader "Mixed Reality Toolkit/Standard" #endif #if defined(_DIRECTIONAL_LIGHT) -#if defined(_LIGHTWEIGHT_RENDER_PIPELINE) - CBUFFER_START(_LightBuffer) - float4 _MainLightPosition; - half4 _MainLightColor; - CBUFFER_END -#else fixed4 _LightColor0; #endif -#endif #if defined(_REFRACTION) fixed _RefractiveIndex; @@ -438,10 +398,6 @@ Shader "Mixed Reality Toolkit/Standard" fixed _RimPower; #endif -#if defined(_VERTEX_EXTRUSION) - float _VertexExtrusionValue; -#endif - #if defined(_CLIPPING_PLANE) fixed _ClipPlaneSide; float4 _ClipPlane; @@ -458,10 +414,6 @@ Shader "Mixed Reality Toolkit/Standard" float4x4 _ClipBoxInverseTransform; #endif -#if defined(_CLIPPING_PRIMITIVE) - float _BlendedClippingWidth; -#endif - #if defined(_CLIPPING_BORDER) fixed _ClippingBorderWidth; fixed3 _ClippingBorderColor; @@ -470,7 +422,6 @@ Shader "Mixed Reality Toolkit/Standard" #if defined(_NEAR_PLANE_FADE) float _FadeBeginDistance; float _FadeCompleteDistance; - fixed _FadeMinValue; #endif #if defined(_HOVER_LIGHT) || defined(_NEAR_LIGHT_FADE) @@ -490,16 +441,7 @@ Shader "Mixed Reality Toolkit/Standard" #define PROXIMITY_LIGHT_COUNT 2 #define PROXIMITY_LIGHT_DATA_SIZE 6 float4 _ProximityLightData[PROXIMITY_LIGHT_COUNT * PROXIMITY_LIGHT_DATA_SIZE]; -#if defined(_PROXIMITY_LIGHT_COLOR_OVERRIDE) - float4 _ProximityLightCenterColorOverride; - float4 _ProximityLightMiddleColorOverride; - float4 _ProximityLightOuterColorOverride; -#endif -#endif - -#if defined(_HOVER_LIGHT) || defined(_PROXIMITY_LIGHT) || defined(_BORDER_LIGHT) - fixed _FluentLightIntensity; -#endif +#endif #if defined(_ROUND_CORNERS) fixed _RoundCornerRadius; @@ -572,18 +514,18 @@ Shader "Mixed Reality Toolkit/Standard" inline float ProximityLight(float4 proximityLight, float4 proximityLightParams, float4 proximityLightPulseParams, float3 worldPosition, float3 worldNormal, out fixed colorValue) { float proximityLightDistance = dot(proximityLight.xyz - worldPosition, worldNormal); + float normalizedProximityLightDistance = saturate(proximityLightDistance * proximityLightParams.y); #if defined(_PROXIMITY_LIGHT_TWO_SIDED) - worldNormal = IF(proximityLightDistance < 0.0, -worldNormal, worldNormal); - proximityLightDistance = abs(proximityLightDistance); + float3 projectedProximityLight = proximityLight.xyz - (worldNormal * proximityLightDistance); +#else + float3 projectedProximityLight = proximityLight.xyz - (worldNormal * saturate(proximityLightDistance)); #endif - float normalizedProximityLightDistance = saturate(proximityLightDistance * proximityLightParams.y); - float3 projectedProximityLight = proximityLight.xyz - (worldNormal * abs(proximityLightDistance)); float projectedProximityLightDistance = length(projectedProximityLight - worldPosition); - float attenuation = (1.0 - normalizedProximityLightDistance) * proximityLight.w; + float attenuation = (1.0 - pow(normalizedProximityLightDistance, 2.0)) * proximityLight.w; colorValue = saturate(projectedProximityLightDistance * proximityLightParams.z); float pulse = step(proximityLightPulseParams.x, projectedProximityLightDistance) * proximityLightPulseParams.y; - return smoothstep(1.0, 0.0, projectedProximityLightDistance / (proximityLightParams.x * max(pow(normalizedProximityLightDistance, 0.25), proximityLightParams.w))) * pulse * attenuation; + return smoothstep(1.0, 0.0, projectedProximityLightDistance / (proximityLightParams.x * max(normalizedProximityLightDistance, proximityLightParams.w))) * pulse * attenuation; } inline fixed3 MixProximityLightColor(fixed4 centerColor, fixed4 middleColor, fixed4 outerColor, fixed t) @@ -658,25 +600,10 @@ Shader "Mixed Reality Toolkit/Standard" #if defined(_INSTANCED_COLOR) UNITY_TRANSFER_INSTANCE_ID(v, o); #endif - float4 vertexPosition = v.vertex; - -#if defined(_WORLD_POSITION) || defined(_VERTEX_EXTRUSION) - float3 worldVertexPosition = mul(unity_ObjectToWorld, vertexPosition).xyz; -#endif - -#if defined(_NORMAL) || defined(_VERTEX_EXTRUSION) - fixed3 worldNormal = UnityObjectToWorldNormal(v.normal); -#endif - -#if defined(_VERTEX_EXTRUSION) - worldVertexPosition += worldNormal * _VertexExtrusionValue; - vertexPosition = mul(unity_WorldToObject, float4(worldVertexPosition, 1.0)); -#endif - - o.position = UnityObjectToClipPos(vertexPosition); + o.position = UnityObjectToClipPos(v.vertex); #if defined(_WORLD_POSITION) - o.worldPosition.xyz = worldVertexPosition; + o.worldPosition.xyz = mul(unity_ObjectToWorld, v.vertex).xyz; #endif #if defined(_NEAR_PLANE_FADE) @@ -698,9 +625,9 @@ Shader "Mixed Reality Toolkit/Standard" fadeDistance = min(fadeDistance, NearLightDistance(_ProximityLightData[dataIndex], o.worldPosition)); } #else - float fadeDistance = -UnityObjectToViewPos(vertexPosition).z; + float fadeDistance = -UnityObjectToViewPos(v.vertex.xyz).z; #endif - o.worldPosition.w = max(saturate(mad(fadeDistance, rangeInverse, -_FadeCompleteDistance * rangeInverse)), _FadeMinValue); + o.worldPosition.w = saturate(mad(fadeDistance, rangeInverse, -_FadeCompleteDistance * rangeInverse)); #endif #if defined(_SCALE) @@ -781,10 +708,6 @@ Shader "Mixed Reality Toolkit/Standard" o.color = v.color; #endif -#if defined(_SPHERICAL_HARMONICS) - o.ambient = ShadeSH9(float4(worldNormal, 1.0)); -#endif - #if defined(_IRIDESCENCE) float3 rightTangent = normalize(mul((float3x3)unity_ObjectToWorld, float3(1.0, 0.0, 0.0))); float3 incidentWithCenter = normalize(mul(unity_ObjectToWorld, float4(0.0, 0.0, 0.0, 1.0)) - _WorldSpaceCameraPos); @@ -793,11 +716,13 @@ Shader "Mixed Reality Toolkit/Standard" #endif #if defined(_NORMAL) + fixed3 worldNormal = UnityObjectToWorldNormal(v.normal); + #if defined(_TRIPLANAR_MAPPING) o.worldNormal = worldNormal; #if defined(_LOCAL_SPACE_TRIPLANAR_MAPPING) o.triplanarNormal = v.normal; - o.triplanarPosition = vertexPosition; + o.triplanarPosition = v.vertex; #else o.triplanarNormal = worldNormal; o.triplanarPosition = o.worldPosition; @@ -820,7 +745,7 @@ Shader "Mixed Reality Toolkit/Standard" #if defined(SHADER_API_D3D11) && !defined(_ALPHA_CLIP) && !defined(_TRANSPARENT) [earlydepthstencil] #endif - fixed4 frag(v2f i, fixed facing : VFACE) : SV_Target + fixed4 frag(v2f i) : SV_Target { #if defined(_INSTANCED_COLOR) UNITY_SETUP_INSTANCE_ID(i); @@ -887,7 +812,12 @@ Shader "Mixed Reality Toolkit/Standard" #endif #if defined(_CLIPPING_BORDER) fixed3 primitiveBorderColor = lerp(_ClippingBorderColor, fixed3(0.0, 0.0, 0.0), primitiveDistance / _ClippingBorderWidth); - albedo.rgb += primitiveBorderColor * IF((primitiveDistance < _ClippingBorderWidth), 1.0, 0.0); + albedo.rgb += primitiveBorderColor * ((primitiveDistance < _ClippingBorderWidth) ? 1.0 : 0.0); +#endif +#if defined(_ALPHA_CLIP) + albedo *= (primitiveDistance > 0.0); +#else + albedo *= saturate(primitiveDistance); #endif #endif @@ -923,7 +853,7 @@ Shader "Mixed Reality Toolkit/Standard" #endif fixed pointToLight = 1.0; - fixed3 fluentLightColor = fixed3(0.0, 0.0, 0.0); + fixed3 lightColor = fixed3(0.0, 0.0, 0.0); // Hover light. #if defined(_HOVER_LIGHT) @@ -936,11 +866,11 @@ Shader "Mixed Reality Toolkit/Standard" fixed hoverValue = HoverLight(_HoverLightData[dataIndex], _HoverLightData[dataIndex + 1].w, i.worldPosition.xyz); pointToLight += hoverValue; #if !defined(_HOVER_COLOR_OVERRIDE) - fluentLightColor += lerp(fixed3(0.0, 0.0, 0.0), _HoverLightData[dataIndex + 1].rgb, hoverValue); + lightColor += lerp(fixed3(0.0, 0.0, 0.0), _HoverLightData[dataIndex + 1].rgb, hoverValue); #endif } #if defined(_HOVER_COLOR_OVERRIDE) - fluentLightColor = _HoverColorOverride.rgb * pointToLight; + lightColor = _HoverColorOverride.rgb; #endif #endif @@ -956,16 +886,8 @@ Shader "Mixed Reality Toolkit/Standard" fixed colorValue; fixed proximityValue = ProximityLight(_ProximityLightData[dataIndex], _ProximityLightData[dataIndex + 1], _ProximityLightData[dataIndex + 2], i.worldPosition.xyz, i.worldNormal, colorValue); pointToLight += proximityValue; -#if defined(_PROXIMITY_LIGHT_COLOR_OVERRIDE) - fixed3 proximityColor = MixProximityLightColor(_ProximityLightCenterColorOverride, _ProximityLightMiddleColorOverride, _ProximityLightOuterColorOverride, colorValue); -#else fixed3 proximityColor = MixProximityLightColor(_ProximityLightData[dataIndex + 3], _ProximityLightData[dataIndex + 4], _ProximityLightData[dataIndex + 5], colorValue); -#endif -#if defined(_PROXIMITY_LIGHT_SUBTRACTIVE) - fluentLightColor -= lerp(fixed3(0.0, 0.0, 0.0), proximityColor, proximityValue); -#else - fluentLightColor += lerp(fixed3(0.0, 0.0, 0.0), proximityColor, proximityValue); -#endif + lightColor += lerp(fixed3(0.0, 0.0, 0.0), proximityColor, proximityValue); } #endif @@ -987,14 +909,14 @@ Shader "Mixed Reality Toolkit/Standard" #else fixed3 borderColor = fixed3(1.0, 1.0, 1.0); #endif - fixed3 borderContribution = borderColor * borderValue * _BorderMinValue * _FluentLightIntensity; + fixed3 borderContribution = borderColor * borderValue * _BorderMinValue; #if defined(_BORDER_LIGHT_REPLACES_ALBEDO) albedo.rgb = lerp(albedo.rgb, borderContribution, borderValue); #else albedo.rgb += borderContribution; #endif #if defined(_HOVER_LIGHT) || defined(_PROXIMITY_LIGHT) - albedo.rgb += (fluentLightColor * borderValue * pointToLight * _FluentLightIntensity) * 2.0; + albedo.rgb += (lightColor * borderValue * pointToLight) * 2.0; #endif #if defined(_BORDER_LIGHT_OPAQUE) albedo.a = max(albedo.a, borderValue * _BorderLightOpaqueAlpha); @@ -1003,15 +925,11 @@ Shader "Mixed Reality Toolkit/Standard" #if defined(_ROUND_CORNERS) albedo *= roundCornerClip; - pointToLight *= roundCornerClip; #endif #if defined(_ALPHA_CLIP) #if !defined(_ALPHATEST_ON) _Cutoff = 0.5; -#endif -#if defined(_CLIPPING_PRIMITIVE) - albedo *= (primitiveDistance > 0.0); #endif clip(albedo.a - _Cutoff); albedo.a = 1.0; @@ -1048,23 +966,19 @@ Shader "Mixed Reality Toolkit/Standard" worldNormal.x = dot(i.tangentX, tangentNormal); worldNormal.y = dot(i.tangentY, tangentNormal); worldNormal.z = dot(i.tangentZ, tangentNormal); - worldNormal = normalize(worldNormal) * facing; + worldNormal = normalize(worldNormal); #endif #else - worldNormal = normalize(i.worldNormal) * facing; + worldNormal = normalize(i.worldNormal); #endif #endif // Blinn phong lighting. #if defined(_DIRECTIONAL_LIGHT) -#if defined(_LIGHTWEIGHT_RENDER_PIPELINE) - float4 directionalLightDirection = _MainLightPosition; -#else - float4 directionalLightDirection = _WorldSpaceLightPos0; -#endif - fixed diffuse = max(0.0, dot(worldNormal, directionalLightDirection)); + fixed diffuse = max(0.0, dot(worldNormal, _WorldSpaceLightPos0)); + #if defined(_SPECULAR_HIGHLIGHTS) - fixed halfVector = max(0.0, dot(worldNormal, normalize(directionalLightDirection + worldViewDir))); + fixed halfVector = max(0.0, dot(worldNormal, normalize(_WorldSpaceLightPos0 + worldViewDir))); fixed specular = saturate(pow(halfVector, _Shininess * pow(_Smoothness, 4.0)) * _Smoothness * 0.5); #else fixed specular = 0.0; @@ -1096,7 +1010,7 @@ Shader "Mixed Reality Toolkit/Standard" // Final lighting mix. fixed4 output = albedo; #if defined(_SPHERICAL_HARMONICS) - fixed3 ambient = i.ambient; + fixed3 ambient = ShadeSH9(float4(worldNormal, 1.0)); #else fixed3 ambient = glstate_lightmodel_ambient + fixed3(0.25, 0.25, 0.25); #endif @@ -1104,13 +1018,8 @@ Shader "Mixed Reality Toolkit/Standard" #if defined(_DIRECTIONAL_LIGHT) fixed oneMinusMetallic = (1.0 - _Metallic); output.rgb = lerp(output.rgb, ibl, minProperty); -#if defined(_LIGHTWEIGHT_RENDER_PIPELINE) - fixed3 directionalLightColor = _MainLightColor.rgb; -#else - fixed3 directionalLightColor = _LightColor0.rgb; -#endif - output.rgb *= lerp((ambient + directionalLightColor * diffuse + directionalLightColor * specular) * max(oneMinusMetallic, _MinMetallicLightContribution), albedo, minProperty); - output.rgb += (directionalLightColor * albedo * specular) + (directionalLightColor * specular * _Smoothness); + output.rgb *= lerp((ambient + _LightColor0.rgb * diffuse + _LightColor0.rgb * specular) * max(oneMinusMetallic, _MinMetallicLightContribution), albedo, minProperty); + output.rgb += (_LightColor0.rgb * albedo * specular) + (_LightColor0.rgb * specular * _Smoothness); output.rgb += ibl * oneMinusMetallic * _IblContribution; #elif defined(_REFLECTIONS) output.rgb = lerp(output.rgb, ibl, minProperty); @@ -1156,12 +1065,7 @@ Shader "Mixed Reality Toolkit/Standard" // Hover and proximity lighting should occur after near plane fading. #if defined(_HOVER_LIGHT) || defined(_PROXIMITY_LIGHT) - output.rgb += fluentLightColor * _FluentLightIntensity * pointToLight; -#endif - - // Perform non-alpha clipped primitive clipping on the final output. -#if defined(_CLIPPING_PRIMITIVE) && !defined(_ALPHA_CLIP) - output *= saturate(primitiveDistance * (1.0f / _BlendedClippingWidth)); + output.rgb += lightColor * pointToLight; #endif return output; } diff --git a/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityWireframe.shader b/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/XRTK_Wireframe.shader similarity index 62% rename from XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityWireframe.shader rename to XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/XRTK_Wireframe.shader index f3085acd3..b06af2667 100644 --- a/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityWireframe.shader +++ b/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/XRTK_Wireframe.shader @@ -8,34 +8,13 @@ Shader "Mixed Reality Toolkit/Wireframe" { Properties { - // Advanced options. - [Enum(RenderingMode)] _Mode("Rendering Mode", Float) = 0 // "Opaque" - [Enum(CustomRenderingMode)] _CustomMode("Mode", Float) = 0 // "Opaque" - [Enum(UnityEngine.Rendering.BlendMode)] _SrcBlend("Source Blend", Float) = 1 // "One" - [Enum(UnityEngine.Rendering.BlendMode)] _DstBlend("Destination Blend", Float) = 0 // "Zero" - [Enum(UnityEngine.Rendering.BlendOp)] _BlendOp("Blend Operation", Float) = 0 // "Add" - [Enum(UnityEngine.Rendering.CompareFunction)] _ZTest("Depth Test", Float) = 4 // "LessEqual" - [Enum(DepthWrite)] _ZWrite("Depth Write", Float) = 1 // "On" - _ZOffsetFactor("Depth Offset Factor", Float) = 0 // "Zero" - _ZOffsetUnits("Depth Offset Units", Float) = 0 // "Zero" - [Enum(UnityEngine.Rendering.ColorWriteMask)] _ColorWriteMask("Color Write Mask", Float) = 15 // "All" - [Enum(UnityEngine.Rendering.CullMode)] _CullMode("Cull Mode", Float) = 2 // "Back" - _RenderQueueOverride("Render Queue Override", Range(-1.0, 5000)) = -1 - _BaseColor("Base color", Color) = (0.0, 0.0, 0.0, 1.0) _WireColor("Wire color", Color) = (1.0, 1.0, 1.0, 1.0) _WireThickness("Wire thickness", Range(0, 800)) = 100 } - SubShader + SubShader { Tags { "RenderType" = "Opaque" } - Blend[_SrcBlend][_DstBlend] - BlendOp[_BlendOp] - ZTest[_ZTest] - ZWrite[_ZWrite] - Cull[_CullMode] - Offset[_ZOffsetFactor],[_ZOffsetUnits] - ColorMask[_ColorWriteMask] Pass { @@ -46,10 +25,6 @@ Shader "Mixed Reality Toolkit/Wireframe" #pragma geometry geom #pragma fragment frag - #if defined(SHADER_API_D3D11) - #pragma target 5.0 - #endif - #include "UnityCG.cginc" float4 _BaseColor; @@ -59,20 +34,18 @@ Shader "Mixed Reality Toolkit/Wireframe" // Based on approach described in Shader-Based Wireframe Drawing (2008) // http://orbit.dtu.dk/en/publications/id(13e2122d-bec7-48de-beca-03ce6ea1c3f1).html - struct v2g + struct appdata { - float4 projectionSpaceVertex : SV_POSITION; - UNITY_VERTEX_OUTPUT_STEREO + float4 vertex : POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID }; - v2g vert(appdata_base v) + struct v2g { - UNITY_SETUP_INSTANCE_ID(v); - v2g o; - o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex); - UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); - return o; - } + float4 projectionSpaceVertex : SV_POSITION; + float4 worldSpacePosition : TEXCOORD1; + UNITY_VERTEX_OUTPUT_STEREO_EYE_INDEX + }; // worldSpacePosition is to counteract the effect of perspective-correct interpolation so that the lines // look the same thickness regardless of their depth in the scene. @@ -84,9 +57,22 @@ Shader "Mixed Reality Toolkit/Wireframe" UNITY_VERTEX_OUTPUT_STEREO }; + v2g vert(appdata v) + { + v2g o; + UNITY_SETUP_INSTANCE_ID(v); + UNITY_INITIALIZE_OUTPUT_STEREO_EYE_INDEX(o); + + o.projectionSpaceVertex = UnityObjectToClipPos(v.vertex); + o.worldSpacePosition = mul(unity_ObjectToWorld, v.vertex); + return o; + } + [maxvertexcount(3)] void geom(triangle v2g i[3], inout TriangleStream triStream) { + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i[0]); + // Calculate the vectors that define the triangle from the input points. float2 point0 = i[0].projectionSpaceVertex.xy / i[0].projectionSpaceVertex.w; float2 point1 = i[1].projectionSpaceVertex.xy / i[1].projectionSpaceVertex.w; @@ -108,34 +94,36 @@ Shader "Mixed Reality Toolkit/Wireframe" // Output each original vertex with its distance to the opposing line defined // by the other two vertices. g2f o; + UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); [unroll] for (uint idx = 0; idx < 3; ++idx) { - o.projectionSpaceVertex = i[idx].projectionSpaceVertex ; + o.projectionSpaceVertex = i[idx].projectionSpaceVertex; o.worldSpacePosition = 1.0 / o.projectionSpaceVertex.w; o.dist = distScale[idx] * o.projectionSpaceVertex.w * wireScale; - UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(i[idx], o); triStream.Append(o); } } float4 frag(g2f i) : COLOR { - // Calculate minimum distance to one of the triangle lines, making sure to correct + // Calculate minimum distance to one of the triangle lines, making sure to correct // for perspective-correct interpolation. float dist = min(i.dist[0], min(i.dist[1], i.dist[2])) * i.worldSpacePosition; - // Make the intensity of the line very bright along the triangle edges but fall-off very - // quickly. - float I = exp2(-2 * dist * dist); + // Make the intensity of the line very bright along the triangle edges but fall-off very + // quickly. + float I = exp2(-2 * dist * dist); - return I * _WireColor + (1 - I) * _BaseColor; - } - ENDCG + // Fade out the alpha but not the color so we don't get any weird halo effects from + // a fade to a different color. + float4 color = I * _WireColor + (1 - I) * _BaseColor; + color.a = I; + return color; } + ENDCG } - - FallBack "Diffuse" - CustomEditor "XRTK.Inspectors.MixedRealityWireframeShaderGUI" -} \ No newline at end of file + } + FallBack "Diffuse" +} diff --git a/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityWireframe.shader.meta b/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/XRTK_Wireframe.shader.meta similarity index 80% rename from XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityWireframe.shader.meta rename to XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/XRTK_Wireframe.shader.meta index dd042e4a8..e9d91c4c6 100644 --- a/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/MixedRealityWireframe.shader.meta +++ b/XRTK-Core/Packages/com.xrtk.core/StandardAssets/Shaders/XRTK_Wireframe.shader.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 5c1653eb4e20b76499141de7bc57c063 +guid: 4a2ab4faa2fc4a009bcfdf058d1e0659 ShaderImporter: externalObjects: {} defaultTextures: []