RenderingOptionsOpHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace AmplifyShaderEditor
  8. {
  9. public enum DisableBatchingTagValues
  10. {
  11. True,
  12. False,
  13. LODFading
  14. }
  15. [Serializable]
  16. public class RenderingOptionsOpHelper
  17. {
  18. private const string RenderingOptionsStr = " Rendering Options";
  19. private readonly static GUIContent EmissionGIFlags = new GUIContent( "Emission GI Flag", "Modifies Emission GI flags" );
  20. private readonly static GUIContent LODCrossfadeContent = new GUIContent( " LOD Group Cross Fade", "Applies a dither crossfade to be used with LOD groups for smoother transitions. Uses one interpolator\nDefault: OFF" );
  21. private readonly static GUIContent DisableBatchingContent = new GUIContent( "Disable Batching", "\nDisables objects to be batched and used with DrawCallBatching Default: False" );
  22. private readonly static GUIContent IgnoreProjectorContent = new GUIContent( " Ignore Projector", "\nIf True then an object that uses this shader will not be affected by Projectors Default: False" );
  23. private readonly static GUIContent UseDefaultCasterContent = new GUIContent( " Use Default Shadow Caster", "\nIf True always use surface default shadow caster Default: False" );
  24. private readonly static GUIContent ForceNoShadowCastingContent = new GUIContent( " Force No Shadow Casting", "\nIf True then an object that is rendered using this subshader will never cast shadows Default: False" );
  25. private readonly static GUIContent ForceEnableInstancingContent = new GUIContent( " Force Enable Instancing", "\nIf True forces instancing on shader independent of having instanced properties" );
  26. private readonly static GUIContent ForceDisableInstancingContent = new GUIContent( " Force Disable Instancing", "\nIf True forces disable instancing on shader independent of having instanced properties" );
  27. private readonly static GUIContent SpecularHightlightsContent = new GUIContent( " Fwd Specular Highlights Toggle", "\nIf True creates a material toggle to set Unity's internal specular highlight rendering keyword" );
  28. private readonly static GUIContent ReflectionsContent = new GUIContent( " Fwd Reflections Toggle", "\nIf True creates a material toggle to set Unity's internal reflections rendering keyword" );
  29. [SerializeField]
  30. private bool m_forceEnableInstancing = false;
  31. [SerializeField]
  32. private bool m_forceDisableInstancing = false;
  33. [SerializeField]
  34. private bool m_specularHighlightToggle = false;
  35. [SerializeField]
  36. private bool m_reflectionsToggle = false;
  37. [SerializeField]
  38. private bool m_lodCrossfade = false;
  39. [SerializeField]
  40. private DisableBatchingTagValues m_disableBatching = DisableBatchingTagValues.False;
  41. [SerializeField]
  42. private bool m_ignoreProjector = false;
  43. [SerializeField]
  44. private bool m_useDefaultShadowCaster = false;
  45. [SerializeField]
  46. private bool m_forceNoShadowCasting = false;
  47. [SerializeField]
  48. private List<CodeGenerationData> m_codeGenerationDataList;
  49. public RenderingOptionsOpHelper()
  50. {
  51. m_codeGenerationDataList = new List<CodeGenerationData>();
  52. m_codeGenerationDataList.Add( new CodeGenerationData( " Exclude Deferred", "exclude_path:deferred" ) );
  53. m_codeGenerationDataList.Add( new CodeGenerationData( " Exclude Forward", "exclude_path:forward" ) );
  54. m_codeGenerationDataList.Add( new CodeGenerationData( " Exclude Legacy Deferred", "exclude_path:prepass" ) );
  55. m_codeGenerationDataList.Add( new CodeGenerationData( " Shadows", "noshadow" ) );
  56. m_codeGenerationDataList.Add( new CodeGenerationData( " Ambient Light", "noambient" ) );
  57. m_codeGenerationDataList.Add( new CodeGenerationData( " Per Vertex Light", "novertexlights" ) );
  58. m_codeGenerationDataList.Add( new CodeGenerationData( " Lightmaps", "nolightmap " ) );
  59. m_codeGenerationDataList.Add( new CodeGenerationData( " Dynamic Global GI", "nodynlightmap" ) );
  60. m_codeGenerationDataList.Add( new CodeGenerationData( " Directional lightmaps", "nodirlightmap" ) );
  61. m_codeGenerationDataList.Add( new CodeGenerationData( " Built-in Fog", "nofog" ) );
  62. m_codeGenerationDataList.Add( new CodeGenerationData( " Meta Pass", "nometa" ) );
  63. m_codeGenerationDataList.Add( new CodeGenerationData( " Add Pass", "noforwardadd" ) );
  64. }
  65. public bool IsOptionActive( string option )
  66. {
  67. return !m_codeGenerationDataList.Find( x => x.Name.Equals( option ) ).IsActive;
  68. }
  69. public void Draw( StandardSurfaceOutputNode owner )
  70. {
  71. bool value = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedRenderingOptions;
  72. NodeUtils.DrawPropertyGroup( ref value, RenderingOptionsStr, () =>
  73. {
  74. int codeGenCount = m_codeGenerationDataList.Count;
  75. // Starting from index 4 because other options are already contemplated with m_renderPath and add/receive shadows
  76. for( int i = 4; i < codeGenCount; i++ )
  77. {
  78. m_codeGenerationDataList[ i ].IsActive = !owner.EditorGUILayoutToggleLeft( m_codeGenerationDataList[ i ].Name, !m_codeGenerationDataList[ i ].IsActive );
  79. }
  80. m_lodCrossfade = owner.EditorGUILayoutToggleLeft( LODCrossfadeContent, m_lodCrossfade );
  81. m_ignoreProjector = owner.EditorGUILayoutToggleLeft( IgnoreProjectorContent, m_ignoreProjector );
  82. EditorGUI.BeginDisabledGroup( !owner.CastShadows );
  83. m_useDefaultShadowCaster = owner.EditorGUILayoutToggleLeft( UseDefaultCasterContent, m_useDefaultShadowCaster );
  84. EditorGUI.EndDisabledGroup();
  85. m_forceNoShadowCasting = owner.EditorGUILayoutToggleLeft( ForceNoShadowCastingContent, m_forceNoShadowCasting );
  86. if( owner.ContainerGraph.IsInstancedShader )
  87. {
  88. GUI.enabled = false;
  89. owner.EditorGUILayoutToggleLeft( ForceEnableInstancingContent, true );
  90. GUI.enabled = true;
  91. }
  92. else
  93. {
  94. m_forceEnableInstancing = owner.EditorGUILayoutToggleLeft( ForceEnableInstancingContent, m_forceEnableInstancing );
  95. }
  96. m_forceDisableInstancing = owner.EditorGUILayoutToggleLeft( ForceDisableInstancingContent, m_forceDisableInstancing );
  97. m_specularHighlightToggle = owner.EditorGUILayoutToggleLeft( SpecularHightlightsContent, m_specularHighlightToggle );
  98. m_reflectionsToggle = owner.EditorGUILayoutToggleLeft( ReflectionsContent, m_reflectionsToggle );
  99. m_disableBatching = (DisableBatchingTagValues)owner.EditorGUILayoutEnumPopup( DisableBatchingContent, m_disableBatching );
  100. Material mat = owner.ContainerGraph.CurrentMaterial;
  101. if( mat != null )
  102. {
  103. mat.globalIlluminationFlags = (MaterialGlobalIlluminationFlags)owner.EditorGUILayoutEnumPopup( EmissionGIFlags, mat.globalIlluminationFlags );
  104. }
  105. } );
  106. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedRenderingOptions = value;
  107. }
  108. public void Build( ref string OptionalParameters )
  109. {
  110. int codeGenCount = m_codeGenerationDataList.Count;
  111. for( int i = 0; i < codeGenCount; i++ )
  112. {
  113. if( m_codeGenerationDataList[ i ].IsActive )
  114. {
  115. OptionalParameters += m_codeGenerationDataList[ i ].Value + Constants.OptionalParametersSep;
  116. }
  117. }
  118. if( m_lodCrossfade )
  119. {
  120. OptionalParameters += Constants.LodCrossFadeOption2017 + Constants.OptionalParametersSep;
  121. }
  122. }
  123. public void ReadFromString( ref uint index, ref string[] nodeParams )
  124. {
  125. for( int i = 0; i < m_codeGenerationDataList.Count; i++ )
  126. {
  127. m_codeGenerationDataList[ i ].IsActive = Convert.ToBoolean( nodeParams[ index++ ] );
  128. }
  129. if( UIUtils.CurrentShaderVersion() > 10005 )
  130. {
  131. m_lodCrossfade = Convert.ToBoolean( nodeParams[ index++ ] );
  132. }
  133. if( UIUtils.CurrentShaderVersion() > 10007 )
  134. {
  135. m_disableBatching = (DisableBatchingTagValues)Enum.Parse( typeof( DisableBatchingTagValues ), nodeParams[ index++ ] );
  136. m_ignoreProjector = Convert.ToBoolean( nodeParams[ index++ ] );
  137. m_forceNoShadowCasting = Convert.ToBoolean( nodeParams[ index++ ] );
  138. }
  139. if( UIUtils.CurrentShaderVersion() > 11002 )
  140. {
  141. m_forceEnableInstancing = Convert.ToBoolean( nodeParams[ index++ ] );
  142. }
  143. if( UIUtils.CurrentShaderVersion() > 15205 )
  144. {
  145. m_forceDisableInstancing = Convert.ToBoolean( nodeParams[ index++ ] );
  146. }
  147. if( UIUtils.CurrentShaderVersion() > 14403 )
  148. {
  149. m_specularHighlightToggle = Convert.ToBoolean( nodeParams[ index++ ] );
  150. m_reflectionsToggle = Convert.ToBoolean( nodeParams[ index++ ] );
  151. }
  152. if( UIUtils.CurrentShaderVersion() > 16307 )
  153. {
  154. m_useDefaultShadowCaster = Convert.ToBoolean( nodeParams[ index++ ] );
  155. }
  156. }
  157. public void WriteToString( ref string nodeInfo )
  158. {
  159. for( int i = 0; i < m_codeGenerationDataList.Count; i++ )
  160. {
  161. IOUtils.AddFieldValueToString( ref nodeInfo, m_codeGenerationDataList[ i ].IsActive );
  162. }
  163. IOUtils.AddFieldValueToString( ref nodeInfo, m_lodCrossfade );
  164. IOUtils.AddFieldValueToString( ref nodeInfo, m_disableBatching );
  165. IOUtils.AddFieldValueToString( ref nodeInfo, m_ignoreProjector );
  166. IOUtils.AddFieldValueToString( ref nodeInfo, m_forceNoShadowCasting );
  167. IOUtils.AddFieldValueToString( ref nodeInfo, m_forceEnableInstancing );
  168. IOUtils.AddFieldValueToString( ref nodeInfo, m_forceDisableInstancing );
  169. IOUtils.AddFieldValueToString( ref nodeInfo, m_specularHighlightToggle );
  170. IOUtils.AddFieldValueToString( ref nodeInfo, m_reflectionsToggle );
  171. IOUtils.AddFieldValueToString( ref nodeInfo, m_useDefaultShadowCaster );
  172. }
  173. public void Destroy()
  174. {
  175. m_codeGenerationDataList.Clear();
  176. m_codeGenerationDataList = null;
  177. }
  178. public bool UseDefaultShadowCaster { get { return m_useDefaultShadowCaster; } }
  179. public bool ForceEnableInstancing { get { return m_forceEnableInstancing; } }
  180. public bool ForceDisableInstancing { get { return m_forceDisableInstancing; } }
  181. public bool LodCrossfade { get { return m_lodCrossfade; } }
  182. public bool IgnoreProjectorValue { get { return m_ignoreProjector; } set { m_ignoreProjector = value; } }
  183. public bool SpecularHighlightToggle { get { return m_specularHighlightToggle; } set { m_specularHighlightToggle = value; } }
  184. public bool ReflectionsToggle { get { return m_reflectionsToggle; } set { m_reflectionsToggle = value; } }
  185. public string DisableBatchingTag { get { return ( m_disableBatching != DisableBatchingTagValues.False ) ? string.Format( Constants.TagFormat, "DisableBatching", m_disableBatching ) : string.Empty; } }
  186. public string IgnoreProjectorTag { get { return ( m_ignoreProjector ) ? string.Format( Constants.TagFormat, "IgnoreProjector", "True" ) : string.Empty; } }
  187. public string ForceNoShadowCastingTag { get { return ( m_forceNoShadowCasting ) ? string.Format( Constants.TagFormat, "ForceNoShadowCasting", "True" ) : string.Empty; } }
  188. }
  189. }