MixedRealityStandardShaderGUI.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. //using Microsoft.MixedReality.Toolkit.Utilities;
  4. using System;
  5. using System.IO;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using UnityEngine.Rendering;
  9. using Object = UnityEngine.Object;
  10. namespace Microsoft.MixedReality.Toolkit.Editor
  11. {
  12. /// <summary>
  13. /// A custom shader inspector for the "Mixed Reality Toolkit/Standard" shader.
  14. /// </summary>
  15. public class MixedRealityStandardShaderGUI : MixedRealityShaderGUI
  16. {
  17. protected enum AlbedoAlphaMode
  18. {
  19. Transparency,
  20. Metallic,
  21. Smoothness
  22. }
  23. protected static class Styles
  24. {
  25. public static string primaryMapsTitle = "Main Maps";
  26. public static string renderingOptionsTitle = "Rendering Options";
  27. public static string advancedOptionsTitle = "Advanced Options";
  28. public static string fluentOptionsTitle = "Fluent Options";
  29. public static string instancedColorName = "_InstancedColor";
  30. public static string instancedColorFeatureName = "_INSTANCED_COLOR";
  31. public static string stencilComparisonName = "_StencilComparison";
  32. public static string stencilOperationName = "_StencilOperation";
  33. public static string disableAlbedoMapName = "_DISABLE_ALBEDO_MAP";
  34. public static string albedoMapAlphaMetallicName = "_METALLIC_TEXTURE_ALBEDO_CHANNEL_A";
  35. public static string albedoMapAlphaSmoothnessName = "_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A";
  36. public static string propertiesComponentHelp = "Use the {0} component(s) to control {1} properties.";
  37. public static readonly string[] albedoAlphaModeNames = Enum.GetNames(typeof(AlbedoAlphaMode));
  38. public static GUIContent instancedColor = new GUIContent("Instanced Color", "Enable a Unique Color Per Instance");
  39. public static GUIContent albedo = new GUIContent("Albedo", "Albedo (RGB) and Transparency (Alpha)");
  40. 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.");
  41. public static GUIContent alphaCutoff = new GUIContent("Alpha Cutoff", "Threshold for Alpha Cutoff");
  42. public static GUIContent metallic = new GUIContent("Metallic", "Metallic Value");
  43. public static GUIContent smoothness = new GUIContent("Smoothness", "Smoothness Value");
  44. public static GUIContent enableChannelMap = new GUIContent("Channel Map", "Enable Channel Map, a Channel Packing Texture That Follows Unity's Standard Channel Setup");
  45. public static GUIContent channelMap = new GUIContent("Channel Map", "Metallic (Red), Occlusion (Green), Emission (Blue), Smoothness (Alpha)");
  46. public static GUIContent enableNormalMap = new GUIContent("Normal Map", "Enable Normal Map");
  47. public static GUIContent normalMap = new GUIContent("Normal Map");
  48. public static GUIContent normalMapScale = new GUIContent("Scale", "Scales the Normal Map Normal");
  49. public static GUIContent enableEmission = new GUIContent("Emission", "Enable Emission");
  50. public static GUIContent emissiveColor = new GUIContent("Color");
  51. public static GUIContent enableTriplanarMapping = new GUIContent("Triplanar Mapping", "Enable Triplanar Mapping, a technique which programmatically generates UV coordinates");
  52. public static GUIContent enableLocalSpaceTriplanarMapping = new GUIContent("Local Space", "If True Triplanar Mapping is Calculated in Local Space");
  53. public static GUIContent triplanarMappingBlendSharpness = new GUIContent("Blend Sharpness", "The Power of the Blend with the Normal");
  54. public static GUIContent directionalLight = new GUIContent("Directional Light", "Affected by One Unity Directional Light");
  55. public static GUIContent specularHighlights = new GUIContent("Specular Highlights", "Calculate Specular Highlights");
  56. public static GUIContent sphericalHarmonics = new GUIContent("Spherical Harmonics", "Read From Spherical Harmonics Data for Ambient Light");
  57. public static GUIContent reflections = new GUIContent("Reflections", "Calculate Glossy Reflections");
  58. public static GUIContent refraction = new GUIContent("Refraction", "Calculate Refraction");
  59. public static GUIContent refractiveIndex = new GUIContent("Refractive Index", "Ratio of Indices of Refraction at the Surface Interface");
  60. public static GUIContent rimLight = new GUIContent("Rim Light", "Enable Rim (Fresnel) Lighting");
  61. public static GUIContent rimColor = new GUIContent("Color", "Rim Highlight Color");
  62. public static GUIContent rimPower = new GUIContent("Power", "Rim Highlight Saturation");
  63. public static GUIContent vertexColors = new GUIContent("Vertex Colors", "Enable Vertex Color Tinting");
  64. public static GUIContent vertexExtrusion = new GUIContent("Vertex Extrusion", "Enable Vertex Extrusion Along the Vertex Normal");
  65. public static GUIContent vertexExtrusionValue = new GUIContent("Vertex Extrusion Value", "How Far to Extrude the Vertex Along the Vertex Normal");
  66. public static GUIContent blendedClippingWidth = new GUIContent("Blended Clipping Width", "The Width of the Clipping Primitive Clip Fade Region on Non-Cutout Materials");
  67. public static GUIContent clippingBorder = new GUIContent("Clipping Border", "Enable a Border Along the Clipping Primitive's Edge");
  68. public static GUIContent clippingBorderWidth = new GUIContent("Width", "Width of the Clipping Border");
  69. public static GUIContent clippingBorderColor = new GUIContent("Color", "Interpolated Color of the Clipping Border");
  70. public static GUIContent nearPlaneFade = new GUIContent("Near Fade", "Objects Disappear (Turn to Black/Transparent) as the Camera (or Hover/Proximity Light) Nears Them");
  71. public static GUIContent nearLightFade = new GUIContent("Use Light", "A Hover or Proximity Light (Rather Than the Camera) Determines Near Fade Distance");
  72. public static GUIContent fadeBeginDistance = new GUIContent("Fade Begin", "Distance From Camera (or Hover/Proximity Light) to Begin Fade In");
  73. public static GUIContent fadeCompleteDistance = new GUIContent("Fade Complete", "Distance From Camera (or Hover/Proximity Light) When Fade is Fully In");
  74. public static GUIContent fadeMinValue = new GUIContent("Fade Min Value", "Clamps the Fade Amount to a Minimum Value");
  75. public static GUIContent hoverLight = new GUIContent("Hover Light", "Enable utilization of Hover Light(s)");
  76. public static GUIContent enableHoverColorOverride = new GUIContent("Override Color", "Override Global Hover Light Color for this Material");
  77. public static GUIContent hoverColorOverride = new GUIContent("Color", "Override Hover Light Color");
  78. public static GUIContent proximityLight = new GUIContent("Proximity Light", "Enable utilization of Proximity Light(s)");
  79. public static GUIContent enableProximityLightColorOverride = new GUIContent("Override Color", "Override Global Proximity Light Color for this Material");
  80. public static GUIContent proximityLightCenterColorOverride = new GUIContent("Center Color", "The Override Color of the ProximityLight Gradient at the Center (RGB) and (A) is Gradient Extent");
  81. public static GUIContent proximityLightMiddleColorOverride = new GUIContent("Middle Color", "The Override Color of the ProximityLight Gradient at the Middle (RGB) and (A) is Gradient Extent");
  82. 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");
  83. public static GUIContent proximityLightSubtractive = new GUIContent("Subtractive", "Proximity Lights Remove Light from a Surface, Used to Mimic a Shadow");
  84. public static GUIContent proximityLightTwoSided = new GUIContent("Two Sided", "Proximity Lights Apply to Both Sides of a Surface");
  85. public static GUIContent fluentLightIntensity = new GUIContent("Light Intensity", "Intensity Scaler for All Hover and Proximity Lights");
  86. public static GUIContent roundCorners = new GUIContent("Round Corners", "(Assumes UVs Specify Borders of Surface, Works Best on Unity Cube, Quad, and Plane)");
  87. public static GUIContent roundCornerRadius = new GUIContent("Unit Radius", "Rounded Rectangle Corner Unit Sphere Radius");
  88. public static GUIContent roundCornerMargin = new GUIContent("Margin %", "Distance From Geometry Edge");
  89. public static GUIContent borderLight = new GUIContent("Border Light", "Enable Border Lighting (Assumes UVs Specify Borders of Surface, Works Best on Unity Cube, Quad, and Plane)");
  90. public static GUIContent borderLightUsesHoverColor = new GUIContent("Use Hover Color", "Border Color Comes From Hover Light Color Override");
  91. public static GUIContent borderLightReplacesAlbedo = new GUIContent("Replace Albedo", "Border Light Replaces Albedo (Replacement Rather Than Additive)");
  92. public static GUIContent borderLightOpaque = new GUIContent("Opaque Borders", "Borders Override Alpha Value to Appear Opaque");
  93. public static GUIContent borderWidth = new GUIContent("Width %", "Uniform Width Along Border as a % of the Smallest XYZ Dimension");
  94. public static GUIContent borderMinValue = new GUIContent("Brightness", "Brightness Scaler");
  95. public static GUIContent edgeSmoothingValue = new GUIContent("Edge Smoothing Value", "Smooths Edges When Round Corners and Transparency Is Enabled");
  96. public static GUIContent borderLightOpaqueAlpha = new GUIContent("Alpha", "Alpha value of \"opaque\" borders.");
  97. public static GUIContent innerGlow = new GUIContent("Inner Glow", "Enable Inner Glow (Assumes UVs Specify Borders of Surface, Works Best on Unity Cube, Quad, and Plane)");
  98. public static GUIContent innerGlowColor = new GUIContent("Color", "Inner Glow Color (RGB) and Intensity (A)");
  99. public static GUIContent innerGlowPower = new GUIContent("Power", "Power Exponent to Control Glow");
  100. public static GUIContent iridescence = new GUIContent("Iridescence", "Simulated Iridescence via Albedo Changes with the Angle of Observation)");
  101. public static GUIContent iridescentSpectrumMap = new GUIContent("Spectrum Map", "Spectrum of Colors to Apply (Usually a Texture with ROYGBIV from Left to Right)");
  102. public static GUIContent iridescenceIntensity = new GUIContent("Intensity", "Intensity of Iridescence");
  103. public static GUIContent iridescenceThreshold = new GUIContent("Threshold", "Threshold Window to Sample From the Spectrum Map");
  104. public static GUIContent iridescenceAngle = new GUIContent("Angle", "Surface Angle");
  105. public static GUIContent environmentColoring = new GUIContent("Environment Coloring", "Change Color Based on View");
  106. public static GUIContent environmentColorThreshold = new GUIContent("Threshold", "Threshold When Environment Coloring Should Appear Based on Surface Normal");
  107. public static GUIContent environmentColorIntensity = new GUIContent("Intensity", "Intensity (or Brightness) of the Environment Coloring");
  108. public static GUIContent environmentColorX = new GUIContent("X-Axis Color", "Color Along the World Space X-Axis");
  109. public static GUIContent environmentColorY = new GUIContent("Y-Axis Color", "Color Along the World Space Y-Axis");
  110. public static GUIContent environmentColorZ = new GUIContent("Z-Axis Color", "Color Along the World Space Z-Axis");
  111. public static GUIContent stencil = new GUIContent("Enable Stencil Testing", "Enabled Stencil Testing Operations");
  112. public static GUIContent stencilReference = new GUIContent("Stencil Reference", "Value to Compared Against (if Comparison is Anything but Always) and/or the Value to be Written to the Buffer (if Either Pass, Fail or ZFail is Set to Replace)");
  113. public static GUIContent stencilComparison = new GUIContent("Stencil Comparison", "Function to Compare the Reference Value to");
  114. public static GUIContent stencilOperation = new GUIContent("Stencil Operation", "What to do When the Stencil Test Passes");
  115. public static GUIContent ignoreZScale = new GUIContent("Ignore Z Scale", "For Features That Use Object Scale (Round Corners, Border Light, etc.), Ignore the Z Scale of the Object");
  116. }
  117. protected MaterialProperty instancedColor;
  118. protected MaterialProperty albedoMap;
  119. protected MaterialProperty albedoColor;
  120. protected MaterialProperty albedoAlphaMode;
  121. protected MaterialProperty albedoAssignedAtRuntime;
  122. protected MaterialProperty alphaCutoff;
  123. protected MaterialProperty enableChannelMap;
  124. protected MaterialProperty channelMap;
  125. protected MaterialProperty enableNormalMap;
  126. protected MaterialProperty normalMap;
  127. protected MaterialProperty normalMapScale;
  128. protected MaterialProperty enableEmission;
  129. protected MaterialProperty emissiveColor;
  130. protected MaterialProperty enableTriplanarMapping;
  131. protected MaterialProperty enableLocalSpaceTriplanarMapping;
  132. protected MaterialProperty triplanarMappingBlendSharpness;
  133. protected MaterialProperty metallic;
  134. protected MaterialProperty smoothness;
  135. protected MaterialProperty directionalLight;
  136. protected MaterialProperty specularHighlights;
  137. protected MaterialProperty sphericalHarmonics;
  138. protected MaterialProperty reflections;
  139. protected MaterialProperty refraction;
  140. protected MaterialProperty refractiveIndex;
  141. protected MaterialProperty rimLight;
  142. protected MaterialProperty rimColor;
  143. protected MaterialProperty rimPower;
  144. protected MaterialProperty vertexColors;
  145. protected MaterialProperty vertexExtrusion;
  146. protected MaterialProperty vertexExtrusionValue;
  147. protected MaterialProperty blendedClippingWidth;
  148. protected MaterialProperty clippingBorder;
  149. protected MaterialProperty clippingBorderWidth;
  150. protected MaterialProperty clippingBorderColor;
  151. protected MaterialProperty nearPlaneFade;
  152. protected MaterialProperty nearLightFade;
  153. protected MaterialProperty fadeBeginDistance;
  154. protected MaterialProperty fadeCompleteDistance;
  155. protected MaterialProperty fadeMinValue;
  156. protected MaterialProperty hoverLight;
  157. protected MaterialProperty enableHoverColorOverride;
  158. protected MaterialProperty hoverColorOverride;
  159. protected MaterialProperty proximityLight;
  160. protected MaterialProperty enableProximityLightColorOverride;
  161. protected MaterialProperty proximityLightCenterColorOverride;
  162. protected MaterialProperty proximityLightMiddleColorOverride;
  163. protected MaterialProperty proximityLightOuterColorOverride;
  164. protected MaterialProperty proximityLightSubtractive;
  165. protected MaterialProperty proximityLightTwoSided;
  166. protected MaterialProperty fluentLightIntensity;
  167. protected MaterialProperty roundCorners;
  168. protected MaterialProperty roundCornerRadius;
  169. protected MaterialProperty roundCornerMargin;
  170. protected MaterialProperty borderLight;
  171. protected MaterialProperty borderLightUsesHoverColor;
  172. protected MaterialProperty borderLightReplacesAlbedo;
  173. protected MaterialProperty borderLightOpaque;
  174. protected MaterialProperty borderWidth;
  175. protected MaterialProperty borderMinValue;
  176. protected MaterialProperty edgeSmoothingValue;
  177. protected MaterialProperty borderLightOpaqueAlpha;
  178. protected MaterialProperty innerGlow;
  179. protected MaterialProperty innerGlowColor;
  180. protected MaterialProperty innerGlowPower;
  181. protected MaterialProperty iridescence;
  182. protected MaterialProperty iridescentSpectrumMap;
  183. protected MaterialProperty iridescenceIntensity;
  184. protected MaterialProperty iridescenceThreshold;
  185. protected MaterialProperty iridescenceAngle;
  186. protected MaterialProperty environmentColoring;
  187. protected MaterialProperty environmentColorThreshold;
  188. protected MaterialProperty environmentColorIntensity;
  189. protected MaterialProperty environmentColorX;
  190. protected MaterialProperty environmentColorY;
  191. protected MaterialProperty environmentColorZ;
  192. protected MaterialProperty stencil;
  193. protected MaterialProperty stencilReference;
  194. protected MaterialProperty stencilComparison;
  195. protected MaterialProperty stencilOperation;
  196. protected MaterialProperty ignoreZScale;
  197. protected override void FindProperties(MaterialProperty[] props)
  198. {
  199. base.FindProperties(props);
  200. instancedColor = FindProperty(Styles.instancedColorName, props);
  201. albedoMap = FindProperty("_MainTex", props);
  202. albedoColor = FindProperty("_Color", props);
  203. albedoAlphaMode = FindProperty("_AlbedoAlphaMode", props);
  204. albedoAssignedAtRuntime = FindProperty("_AlbedoAssignedAtRuntime", props);
  205. alphaCutoff = FindProperty("_Cutoff", props);
  206. metallic = FindProperty("_Metallic", props);
  207. smoothness = FindProperty("_Smoothness", props);
  208. enableChannelMap = FindProperty("_EnableChannelMap", props);
  209. channelMap = FindProperty("_ChannelMap", props);
  210. enableNormalMap = FindProperty("_EnableNormalMap", props);
  211. normalMap = FindProperty("_NormalMap", props);
  212. normalMapScale = FindProperty("_NormalMapScale", props);
  213. enableEmission = FindProperty("_EnableEmission", props);
  214. emissiveColor = FindProperty("_EmissiveColor", props);
  215. enableTriplanarMapping = FindProperty("_EnableTriplanarMapping", props);
  216. enableLocalSpaceTriplanarMapping = FindProperty("_EnableLocalSpaceTriplanarMapping", props);
  217. triplanarMappingBlendSharpness = FindProperty("_TriplanarMappingBlendSharpness", props);
  218. directionalLight = FindProperty("_DirectionalLight", props);
  219. specularHighlights = FindProperty("_SpecularHighlights", props);
  220. sphericalHarmonics = FindProperty("_SphericalHarmonics", props);
  221. reflections = FindProperty("_Reflections", props);
  222. refraction = FindProperty("_Refraction", props);
  223. refractiveIndex = FindProperty("_RefractiveIndex", props);
  224. rimLight = FindProperty("_RimLight", props);
  225. rimColor = FindProperty("_RimColor", props);
  226. rimPower = FindProperty("_RimPower", props);
  227. vertexColors = FindProperty("_VertexColors", props);
  228. vertexExtrusion = FindProperty("_VertexExtrusion", props);
  229. vertexExtrusionValue = FindProperty("_VertexExtrusionValue", props);
  230. blendedClippingWidth = FindProperty("_BlendedClippingWidth", props);
  231. clippingBorder = FindProperty("_ClippingBorder", props);
  232. clippingBorderWidth = FindProperty("_ClippingBorderWidth", props);
  233. clippingBorderColor = FindProperty("_ClippingBorderColor", props);
  234. nearPlaneFade = FindProperty("_NearPlaneFade", props);
  235. nearLightFade = FindProperty("_NearLightFade", props);
  236. fadeBeginDistance = FindProperty("_FadeBeginDistance", props);
  237. fadeCompleteDistance = FindProperty("_FadeCompleteDistance", props);
  238. fadeMinValue = FindProperty("_FadeMinValue", props);
  239. hoverLight = FindProperty("_HoverLight", props);
  240. enableHoverColorOverride = FindProperty("_EnableHoverColorOverride", props);
  241. hoverColorOverride = FindProperty("_HoverColorOverride", props);
  242. proximityLight = FindProperty("_ProximityLight", props);
  243. enableProximityLightColorOverride = FindProperty("_EnableProximityLightColorOverride", props);
  244. proximityLightCenterColorOverride = FindProperty("_ProximityLightCenterColorOverride", props);
  245. proximityLightMiddleColorOverride = FindProperty("_ProximityLightMiddleColorOverride", props);
  246. proximityLightOuterColorOverride = FindProperty("_ProximityLightOuterColorOverride", props);
  247. proximityLightSubtractive = FindProperty("_ProximityLightSubtractive", props);
  248. proximityLightTwoSided = FindProperty("_ProximityLightTwoSided", props);
  249. fluentLightIntensity = FindProperty("_FluentLightIntensity", props);
  250. roundCorners = FindProperty("_RoundCorners", props);
  251. roundCornerRadius = FindProperty("_RoundCornerRadius", props);
  252. roundCornerMargin = FindProperty("_RoundCornerMargin", props);
  253. borderLight = FindProperty("_BorderLight", props);
  254. borderLightUsesHoverColor = FindProperty("_BorderLightUsesHoverColor", props);
  255. borderLightReplacesAlbedo = FindProperty("_BorderLightReplacesAlbedo", props);
  256. borderLightOpaque = FindProperty("_BorderLightOpaque", props);
  257. borderWidth = FindProperty("_BorderWidth", props);
  258. borderMinValue = FindProperty("_BorderMinValue", props);
  259. edgeSmoothingValue = FindProperty("_EdgeSmoothingValue", props);
  260. borderLightOpaqueAlpha = FindProperty("_BorderLightOpaqueAlpha", props);
  261. innerGlow = FindProperty("_InnerGlow", props);
  262. innerGlowColor = FindProperty("_InnerGlowColor", props);
  263. innerGlowPower = FindProperty("_InnerGlowPower", props);
  264. iridescence = FindProperty("_Iridescence", props);
  265. iridescentSpectrumMap = FindProperty("_IridescentSpectrumMap", props);
  266. iridescenceIntensity = FindProperty("_IridescenceIntensity", props);
  267. iridescenceThreshold = FindProperty("_IridescenceThreshold", props);
  268. iridescenceAngle = FindProperty("_IridescenceAngle", props);
  269. environmentColoring = FindProperty("_EnvironmentColoring", props);
  270. environmentColorThreshold = FindProperty("_EnvironmentColorThreshold", props);
  271. environmentColorIntensity = FindProperty("_EnvironmentColorIntensity", props);
  272. environmentColorX = FindProperty("_EnvironmentColorX", props);
  273. environmentColorY = FindProperty("_EnvironmentColorY", props);
  274. environmentColorZ = FindProperty("_EnvironmentColorZ", props);
  275. stencil = FindProperty("_Stencil", props);
  276. stencilReference = FindProperty("_StencilReference", props);
  277. stencilComparison = FindProperty(Styles.stencilComparisonName, props);
  278. stencilOperation = FindProperty(Styles.stencilOperationName, props);
  279. ignoreZScale = FindProperty("_IgnoreZScale", props);
  280. }
  281. public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] props)
  282. {
  283. Material material = (Material)materialEditor.target;
  284. base.OnGUI(materialEditor, props);
  285. MainMapOptions(materialEditor, material);
  286. RenderingOptions(materialEditor, material);
  287. FluentOptions(materialEditor, material);
  288. AdvancedOptions(materialEditor, material);
  289. }
  290. public override void AssignNewShaderToMaterial(Material material, Shader oldShader, Shader newShader)
  291. {
  292. // Cache old shader properties with potentially different names than the new shader.
  293. float? smoothness = GetFloatProperty(material, "_Glossiness");
  294. float? diffuse = GetFloatProperty(material, "_UseDiffuse");
  295. float? specularHighlights = GetFloatProperty(material, "_SpecularHighlights");
  296. float? normalMap = null;
  297. Texture normalMapTexture = material.GetTexture("_BumpMap");
  298. float? normalMapScale = GetFloatProperty(material, "_BumpScale");
  299. float? emission = null;
  300. Color? emissionColor = GetColorProperty(material, "_EmissionColor");
  301. float? reflections = null;
  302. float? rimLighting = null;
  303. Vector4? textureScaleOffset = null;
  304. float? cullMode = GetFloatProperty(material, "_Cull");
  305. if (oldShader)
  306. {
  307. if (oldShader.name.Contains("Standard"))
  308. {
  309. normalMap = material.IsKeywordEnabled("_NORMALMAP") ? 1.0f : 0.0f;
  310. emission = material.IsKeywordEnabled("_EMISSION") ? 1.0f : 0.0f;
  311. reflections = GetFloatProperty(material, "_GlossyReflections");
  312. }
  313. else if (oldShader.name.Contains("Fast Configurable"))
  314. {
  315. normalMap = material.IsKeywordEnabled("_USEBUMPMAP_ON") ? 1.0f : 0.0f;
  316. emission = GetFloatProperty(material, "_UseEmissionColor");
  317. reflections = GetFloatProperty(material, "_UseReflections");
  318. rimLighting = GetFloatProperty(material, "_UseRimLighting");
  319. textureScaleOffset = GetVectorProperty(material, "_TextureScaleOffset");
  320. }
  321. }
  322. base.AssignNewShaderToMaterial(material, oldShader, newShader);
  323. // Apply old shader properties to the new shader.
  324. SetShaderFeatureActive(material, null, "_Smoothness", smoothness);
  325. SetShaderFeatureActive(material, "_DIRECTIONAL_LIGHT", "_DirectionalLight", diffuse);
  326. SetShaderFeatureActive(material, "_SPECULAR_HIGHLIGHTS", "_SpecularHighlights", specularHighlights);
  327. SetShaderFeatureActive(material, "_NORMAL_MAP", "_EnableNormalMap", normalMap);
  328. if (normalMapTexture)
  329. {
  330. material.SetTexture("_NormalMap", normalMapTexture);
  331. }
  332. SetShaderFeatureActive(material, null, "_NormalMapScale", normalMapScale);
  333. SetShaderFeatureActive(material, "_EMISSION", "_EnableEmission", emission);
  334. SetColorProperty(material, "_EmissiveColor", emissionColor);
  335. SetShaderFeatureActive(material, "_REFLECTIONS", "_Reflections", reflections);
  336. SetShaderFeatureActive(material, "_RIM_LIGHT", "_RimLight", rimLighting);
  337. SetVectorProperty(material, "_MainTex_ST", textureScaleOffset);
  338. SetShaderFeatureActive(material, null, "_CullMode", cullMode);
  339. // Setup the rendering mode based on the old shader.
  340. if (oldShader == null || !oldShader.name.Contains(LegacyShadersPath))
  341. {
  342. SetupMaterialWithRenderingMode(material, (RenderingMode)material.GetFloat(BaseStyles.renderingModeName), CustomRenderingMode.Opaque, -1);
  343. }
  344. else
  345. {
  346. RenderingMode mode = RenderingMode.Opaque;
  347. if (oldShader.name.Contains(TransparentCutoutShadersPath))
  348. {
  349. mode = RenderingMode.TransparentCutout;
  350. }
  351. else if (oldShader.name.Contains(TransparentShadersPath))
  352. {
  353. mode = RenderingMode.Transparent;
  354. }
  355. material.SetFloat(BaseStyles.renderingModeName, (float)mode);
  356. MaterialChanged(material);
  357. }
  358. }
  359. protected override void MaterialChanged(Material material)
  360. {
  361. SetupMaterialWithAlbedo(material, albedoMap, albedoAlphaMode, albedoAssignedAtRuntime);
  362. base.MaterialChanged(material);
  363. }
  364. protected void MainMapOptions(MaterialEditor materialEditor, Material material)
  365. {
  366. GUILayout.Label(Styles.primaryMapsTitle, EditorStyles.boldLabel);
  367. materialEditor.TexturePropertySingleLine(Styles.albedo, albedoMap, albedoColor);
  368. if (albedoMap.textureValue == null)
  369. {
  370. materialEditor.ShaderProperty(albedoAssignedAtRuntime, Styles.albedoAssignedAtRuntime, 2);
  371. }
  372. materialEditor.ShaderProperty(enableChannelMap, Styles.enableChannelMap);
  373. if (PropertyEnabled(enableChannelMap))
  374. {
  375. EditorGUI.indentLevel += 2;
  376. materialEditor.TexturePropertySingleLine(Styles.channelMap, channelMap);
  377. GUILayout.Box("Metallic (Red), Occlusion (Green), Emission (Blue), Smoothness (Alpha)", EditorStyles.helpBox, new GUILayoutOption[0]);
  378. EditorGUI.indentLevel -= 2;
  379. }
  380. if (!PropertyEnabled(enableChannelMap))
  381. {
  382. EditorGUI.indentLevel += 2;
  383. albedoAlphaMode.floatValue = EditorGUILayout.Popup(albedoAlphaMode.displayName, (int)albedoAlphaMode.floatValue, Styles.albedoAlphaModeNames);
  384. if ((RenderingMode)renderingMode.floatValue == RenderingMode.TransparentCutout ||
  385. (RenderingMode)renderingMode.floatValue == RenderingMode.Custom)
  386. {
  387. materialEditor.ShaderProperty(alphaCutoff, Styles.alphaCutoff.text);
  388. }
  389. if ((AlbedoAlphaMode)albedoAlphaMode.floatValue != AlbedoAlphaMode.Metallic)
  390. {
  391. materialEditor.ShaderProperty(metallic, Styles.metallic);
  392. }
  393. if ((AlbedoAlphaMode)albedoAlphaMode.floatValue != AlbedoAlphaMode.Smoothness)
  394. {
  395. materialEditor.ShaderProperty(smoothness, Styles.smoothness);
  396. }
  397. SetupMaterialWithAlbedo(material, albedoMap, albedoAlphaMode, albedoAssignedAtRuntime);
  398. EditorGUI.indentLevel -= 2;
  399. }
  400. if (PropertyEnabled(directionalLight) ||
  401. PropertyEnabled(reflections) ||
  402. PropertyEnabled(rimLight) ||
  403. PropertyEnabled(environmentColoring))
  404. {
  405. materialEditor.ShaderProperty(enableNormalMap, Styles.enableNormalMap);
  406. if (PropertyEnabled(enableNormalMap))
  407. {
  408. EditorGUI.indentLevel += 2;
  409. materialEditor.TexturePropertySingleLine(Styles.normalMap, normalMap, normalMap.textureValue != null ? normalMapScale : null);
  410. EditorGUI.indentLevel -= 2;
  411. }
  412. }
  413. materialEditor.ShaderProperty(enableEmission, Styles.enableEmission);
  414. if (PropertyEnabled(enableEmission))
  415. {
  416. materialEditor.ShaderProperty(emissiveColor, Styles.emissiveColor, 2);
  417. }
  418. materialEditor.ShaderProperty(enableTriplanarMapping, Styles.enableTriplanarMapping);
  419. if (PropertyEnabled(enableTriplanarMapping))
  420. {
  421. materialEditor.ShaderProperty(enableLocalSpaceTriplanarMapping, Styles.enableLocalSpaceTriplanarMapping, 2);
  422. materialEditor.ShaderProperty(triplanarMappingBlendSharpness, Styles.triplanarMappingBlendSharpness, 2);
  423. }
  424. EditorGUILayout.Space();
  425. materialEditor.TextureScaleOffsetProperty(albedoMap);
  426. }
  427. protected void RenderingOptions(MaterialEditor materialEditor, Material material)
  428. {
  429. EditorGUILayout.Space();
  430. GUILayout.Label(Styles.renderingOptionsTitle, EditorStyles.boldLabel);
  431. materialEditor.ShaderProperty(directionalLight, Styles.directionalLight);
  432. if (PropertyEnabled(directionalLight))
  433. {
  434. materialEditor.ShaderProperty(specularHighlights, Styles.specularHighlights, 2);
  435. }
  436. materialEditor.ShaderProperty(sphericalHarmonics, Styles.sphericalHarmonics);
  437. materialEditor.ShaderProperty(reflections, Styles.reflections);
  438. if (PropertyEnabled(reflections))
  439. {
  440. materialEditor.ShaderProperty(refraction, Styles.refraction, 2);
  441. if (PropertyEnabled(refraction))
  442. {
  443. materialEditor.ShaderProperty(refractiveIndex, Styles.refractiveIndex, 4);
  444. }
  445. }
  446. materialEditor.ShaderProperty(rimLight, Styles.rimLight);
  447. if (PropertyEnabled(rimLight))
  448. {
  449. materialEditor.ShaderProperty(rimColor, Styles.rimColor, 2);
  450. materialEditor.ShaderProperty(rimPower, Styles.rimPower, 2);
  451. }
  452. materialEditor.ShaderProperty(vertexColors, Styles.vertexColors);
  453. materialEditor.ShaderProperty(vertexExtrusion, Styles.vertexExtrusion);
  454. if (PropertyEnabled(vertexExtrusion))
  455. {
  456. materialEditor.ShaderProperty(vertexExtrusionValue, Styles.vertexExtrusionValue, 2);
  457. }
  458. if ((RenderingMode)renderingMode.floatValue != RenderingMode.Opaque &&
  459. (RenderingMode)renderingMode.floatValue != RenderingMode.TransparentCutout)
  460. {
  461. materialEditor.ShaderProperty(blendedClippingWidth, Styles.blendedClippingWidth);
  462. // GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ClippingPrimitive), "other clipping"), EditorStyles.helpBox, new GUILayoutOption[0]);
  463. }
  464. materialEditor.ShaderProperty(clippingBorder, Styles.clippingBorder);
  465. if (PropertyEnabled(clippingBorder))
  466. {
  467. materialEditor.ShaderProperty(clippingBorderWidth, Styles.clippingBorderWidth, 2);
  468. materialEditor.ShaderProperty(clippingBorderColor, Styles.clippingBorderColor, 2);
  469. // GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ClippingPrimitive), "other clipping"), EditorStyles.helpBox, new GUILayoutOption[0]);
  470. }
  471. materialEditor.ShaderProperty(nearPlaneFade, Styles.nearPlaneFade);
  472. if (PropertyEnabled(nearPlaneFade))
  473. {
  474. materialEditor.ShaderProperty(nearLightFade, Styles.nearLightFade, 2);
  475. materialEditor.ShaderProperty(fadeBeginDistance, Styles.fadeBeginDistance, 2);
  476. materialEditor.ShaderProperty(fadeCompleteDistance, Styles.fadeCompleteDistance, 2);
  477. materialEditor.ShaderProperty(fadeMinValue, Styles.fadeMinValue, 2);
  478. }
  479. }
  480. protected void FluentOptions(MaterialEditor materialEditor, Material material)
  481. {
  482. EditorGUILayout.Space();
  483. GUILayout.Label(Styles.fluentOptionsTitle, EditorStyles.boldLabel);
  484. RenderingMode mode = (RenderingMode)renderingMode.floatValue;
  485. CustomRenderingMode customMode = (CustomRenderingMode)customRenderingMode.floatValue;
  486. materialEditor.ShaderProperty(hoverLight, Styles.hoverLight);
  487. if (PropertyEnabled(hoverLight))
  488. {
  489. // GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(HoverLight), Styles.hoverLight.text), EditorStyles.helpBox, new GUILayoutOption[0]);
  490. materialEditor.ShaderProperty(enableHoverColorOverride, Styles.enableHoverColorOverride, 2);
  491. if (PropertyEnabled(enableHoverColorOverride))
  492. {
  493. materialEditor.ShaderProperty(hoverColorOverride, Styles.hoverColorOverride, 4);
  494. }
  495. }
  496. materialEditor.ShaderProperty(proximityLight, Styles.proximityLight);
  497. if (PropertyEnabled(proximityLight))
  498. {
  499. materialEditor.ShaderProperty(enableProximityLightColorOverride, Styles.enableProximityLightColorOverride, 2);
  500. if (PropertyEnabled(enableProximityLightColorOverride))
  501. {
  502. materialEditor.ShaderProperty(proximityLightCenterColorOverride, Styles.proximityLightCenterColorOverride, 4);
  503. materialEditor.ShaderProperty(proximityLightMiddleColorOverride, Styles.proximityLightMiddleColorOverride, 4);
  504. materialEditor.ShaderProperty(proximityLightOuterColorOverride, Styles.proximityLightOuterColorOverride, 4);
  505. }
  506. materialEditor.ShaderProperty(proximityLightSubtractive, Styles.proximityLightSubtractive, 2);
  507. materialEditor.ShaderProperty(proximityLightTwoSided, Styles.proximityLightTwoSided, 2);
  508. // GUILayout.Box(string.Format(Styles.propertiesComponentHelp, nameof(ProximityLight), Styles.proximityLight.text), EditorStyles.helpBox, new GUILayoutOption[0]);
  509. }
  510. materialEditor.ShaderProperty(borderLight, Styles.borderLight);
  511. if (PropertyEnabled(borderLight))
  512. {
  513. materialEditor.ShaderProperty(borderWidth, Styles.borderWidth, 2);
  514. materialEditor.ShaderProperty(borderMinValue, Styles.borderMinValue, 2);
  515. materialEditor.ShaderProperty(borderLightReplacesAlbedo, Styles.borderLightReplacesAlbedo, 2);
  516. if (PropertyEnabled(hoverLight) && PropertyEnabled(enableHoverColorOverride))
  517. {
  518. materialEditor.ShaderProperty(borderLightUsesHoverColor, Styles.borderLightUsesHoverColor, 2);
  519. }
  520. if (mode == RenderingMode.TransparentCutout || mode == RenderingMode.Transparent ||
  521. (mode == RenderingMode.Custom && customMode == CustomRenderingMode.TransparentCutout) ||
  522. (mode == RenderingMode.Custom && customMode == CustomRenderingMode.Transparent))
  523. {
  524. materialEditor.ShaderProperty(borderLightOpaque, Styles.borderLightOpaque, 2);
  525. if (PropertyEnabled(borderLightOpaque))
  526. {
  527. materialEditor.ShaderProperty(borderLightOpaqueAlpha, Styles.borderLightOpaqueAlpha, 4);
  528. }
  529. }
  530. }
  531. if (PropertyEnabled(hoverLight) || PropertyEnabled(proximityLight) || PropertyEnabled(borderLight))
  532. {
  533. materialEditor.ShaderProperty(fluentLightIntensity, Styles.fluentLightIntensity);
  534. }
  535. materialEditor.ShaderProperty(roundCorners, Styles.roundCorners);
  536. if (PropertyEnabled(roundCorners))
  537. {
  538. materialEditor.ShaderProperty(roundCornerRadius, Styles.roundCornerRadius, 2);
  539. materialEditor.ShaderProperty(roundCornerMargin, Styles.roundCornerMargin, 2);
  540. }
  541. if (PropertyEnabled(roundCorners) || PropertyEnabled(borderLight))
  542. {
  543. materialEditor.ShaderProperty(edgeSmoothingValue, Styles.edgeSmoothingValue);
  544. }
  545. materialEditor.ShaderProperty(innerGlow, Styles.innerGlow);
  546. if (PropertyEnabled(innerGlow))
  547. {
  548. materialEditor.ShaderProperty(innerGlowColor, Styles.innerGlowColor, 2);
  549. materialEditor.ShaderProperty(innerGlowPower, Styles.innerGlowPower, 2);
  550. }
  551. materialEditor.ShaderProperty(iridescence, Styles.iridescence);
  552. if (PropertyEnabled(iridescence))
  553. {
  554. EditorGUI.indentLevel += 2;
  555. materialEditor.TexturePropertySingleLine(Styles.iridescentSpectrumMap, iridescentSpectrumMap);
  556. EditorGUI.indentLevel -= 2;
  557. materialEditor.ShaderProperty(iridescenceIntensity, Styles.iridescenceIntensity, 2);
  558. materialEditor.ShaderProperty(iridescenceThreshold, Styles.iridescenceThreshold, 2);
  559. materialEditor.ShaderProperty(iridescenceAngle, Styles.iridescenceAngle, 2);
  560. }
  561. materialEditor.ShaderProperty(environmentColoring, Styles.environmentColoring);
  562. if (PropertyEnabled(environmentColoring))
  563. {
  564. materialEditor.ShaderProperty(environmentColorThreshold, Styles.environmentColorThreshold, 2);
  565. materialEditor.ShaderProperty(environmentColorIntensity, Styles.environmentColorIntensity, 2);
  566. materialEditor.ShaderProperty(environmentColorX, Styles.environmentColorX, 2);
  567. materialEditor.ShaderProperty(environmentColorY, Styles.environmentColorY, 2);
  568. materialEditor.ShaderProperty(environmentColorZ, Styles.environmentColorZ, 2);
  569. }
  570. }
  571. protected void AdvancedOptions(MaterialEditor materialEditor, Material material)
  572. {
  573. EditorGUILayout.Space();
  574. GUILayout.Label(Styles.advancedOptionsTitle, EditorStyles.boldLabel);
  575. EditorGUI.BeginChangeCheck();
  576. materialEditor.ShaderProperty(renderQueueOverride, BaseStyles.renderQueueOverride);
  577. if (EditorGUI.EndChangeCheck())
  578. {
  579. MaterialChanged(material);
  580. }
  581. // Show the RenderQueueField but do not allow users to directly manipulate it. That is done via the renderQueueOverride.
  582. GUI.enabled = false;
  583. materialEditor.RenderQueueField();
  584. // Enable instancing to disable batching. Static and dynamic batching will normalize the object scale, which breaks
  585. // features which utilize object scale.
  586. GUI.enabled = !ScaleRequired();
  587. if (!GUI.enabled && !material.enableInstancing)
  588. {
  589. material.enableInstancing = true;
  590. }
  591. materialEditor.EnableInstancingField();
  592. if (material.enableInstancing)
  593. {
  594. GUI.enabled = true;
  595. materialEditor.ShaderProperty(instancedColor, Styles.instancedColor, 2);
  596. }
  597. else
  598. {
  599. // When instancing is disable, disable instanced color.
  600. SetShaderFeatureActive(material, Styles.instancedColorFeatureName, Styles.instancedColorName, 0.0f);
  601. }
  602. materialEditor.ShaderProperty(stencil, Styles.stencil);
  603. if (PropertyEnabled(stencil))
  604. {
  605. materialEditor.ShaderProperty(stencilReference, Styles.stencilReference, 2);
  606. materialEditor.ShaderProperty(stencilComparison, Styles.stencilComparison, 2);
  607. materialEditor.ShaderProperty(stencilOperation, Styles.stencilOperation, 2);
  608. }
  609. else
  610. {
  611. // When stencil is disable, revert to the default stencil operations. Note, when tested on D3D11 hardware the stencil state
  612. // is still set even when the CompareFunction.Disabled is selected, but this does not seem to affect performance.
  613. material.SetInt(Styles.stencilComparisonName, (int)CompareFunction.Disabled);
  614. material.SetInt(Styles.stencilOperationName, (int)StencilOp.Keep);
  615. }
  616. if (ScaleRequired())
  617. {
  618. materialEditor.ShaderProperty(ignoreZScale, Styles.ignoreZScale);
  619. }
  620. }
  621. protected bool ScaleRequired()
  622. {
  623. return PropertyEnabled(roundCorners) ||
  624. PropertyEnabled(borderLight) ||
  625. (PropertyEnabled(enableTriplanarMapping) && PropertyEnabled(enableLocalSpaceTriplanarMapping));
  626. }
  627. protected static void SetupMaterialWithAlbedo(Material material, MaterialProperty albedoMap, MaterialProperty albedoAlphaMode, MaterialProperty albedoAssignedAtRuntime)
  628. {
  629. if (albedoMap.textureValue || PropertyEnabled(albedoAssignedAtRuntime))
  630. {
  631. material.DisableKeyword(Styles.disableAlbedoMapName);
  632. }
  633. else
  634. {
  635. material.EnableKeyword(Styles.disableAlbedoMapName);
  636. }
  637. switch ((AlbedoAlphaMode)albedoAlphaMode.floatValue)
  638. {
  639. case AlbedoAlphaMode.Transparency:
  640. {
  641. material.DisableKeyword(Styles.albedoMapAlphaMetallicName);
  642. material.DisableKeyword(Styles.albedoMapAlphaSmoothnessName);
  643. }
  644. break;
  645. case AlbedoAlphaMode.Metallic:
  646. {
  647. material.EnableKeyword(Styles.albedoMapAlphaMetallicName);
  648. material.DisableKeyword(Styles.albedoMapAlphaSmoothnessName);
  649. }
  650. break;
  651. case AlbedoAlphaMode.Smoothness:
  652. {
  653. material.DisableKeyword(Styles.albedoMapAlphaMetallicName);
  654. material.EnableKeyword(Styles.albedoMapAlphaSmoothnessName);
  655. }
  656. break;
  657. }
  658. }
  659. [MenuItem("Mixed Reality Toolkit/Utilities/Upgrade MRTK Standard Shader for Lightweight Render Pipeline")]
  660. protected static void UpgradeShaderForLightweightRenderPipeline()
  661. {
  662. if (EditorUtility.DisplayDialog("Upgrade MRTK Standard Shader?",
  663. "This will alter the MRTK Standard Shader for use with Unity's Lightweight Render Pipeline. You cannot undo this action.",
  664. "Ok",
  665. "Cancel"))
  666. {
  667. string shaderName = "Mixed Reality Toolkit/Standard";
  668. string path = AssetDatabase.GetAssetPath(Shader.Find(shaderName));
  669. if (!string.IsNullOrEmpty(path))
  670. {
  671. try
  672. {
  673. string upgradedShader = File.ReadAllText(path);
  674. upgradedShader = upgradedShader.Replace("Tags{ \"RenderType\" = \"Opaque\" \"LightMode\" = \"ForwardBase\" }",
  675. "Tags{ \"RenderType\" = \"Opaque\" \"LightMode\" = \"LightweightForward\" }");
  676. upgradedShader = upgradedShader.Replace("//#define _LIGHTWEIGHT_RENDER_PIPELINE",
  677. "#define _LIGHTWEIGHT_RENDER_PIPELINE");
  678. File.WriteAllText(path, upgradedShader);
  679. AssetDatabase.Refresh();
  680. Debug.LogFormat("Upgraded {0} for use with the Lightweight Render Pipeline.", path);
  681. }
  682. catch (Exception e)
  683. {
  684. Debug.LogException(e);
  685. }
  686. }
  687. else
  688. {
  689. Debug.LogErrorFormat("Failed to get asset path to: {0}", shaderName);
  690. }
  691. }
  692. }
  693. [MenuItem("Mixed Reality Toolkit/Utilities/Upgrade MRTK Standard Shader for Lightweight Render Pipeline", true)]
  694. protected static bool UpgradeShaderForLightweightRenderPipelineValidate()
  695. {
  696. // If a scriptable render pipeline is not present, no need to upgrade the shader.
  697. return GraphicsSettings.renderPipelineAsset != null;
  698. }
  699. }
  700. }