ShadowStandardShaderGUI.cs 43 KB

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