You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
9.8 KiB

  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. namespace AmplifyShaderEditor
  7. {
  8. public enum DisableBatchingTagValues
  9. {
  10. True,
  11. False,
  12. LODFading
  13. }
  14. [Serializable]
  15. public class RenderingOptionsOpHelper
  16. {
  17. private const string RenderingOptionsStr = " Rendering Options";
  18. private readonly static GUIContent EmissionGIFlags = new GUIContent( "Emission GI Flag", "Modifies Emission GI flags" );
  19. 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" );
  20. private readonly static GUIContent DisableBatchingContent = new GUIContent( "Disable Batching", "\nDisables objects to be batched and used with DrawCallBatching Default: False" );
  21. 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" );
  22. 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" );
  23. private readonly static GUIContent ForceEnableInstancingContent = new GUIContent( " Force Enable Instancing", "\nIf True forces instancing on shader independent of having instanced properties" );
  24. #if UNITY_5_6_OR_NEWER
  25. private readonly static GUIContent ForceDisableInstancingContent = new GUIContent( " Force Disable Instancing", "\nIf True forces disable instancing on shader independent of having instanced properties" );
  26. #endif
  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_forceNoShadowCasting = false;
  45. [SerializeField]
  46. private List<CodeGenerationData> m_codeGenerationDataList;
  47. public RenderingOptionsOpHelper()
  48. {
  49. m_codeGenerationDataList = new List<CodeGenerationData>();
  50. m_codeGenerationDataList.Add( new CodeGenerationData( " Exclude Deferred", "exclude_path:deferred" ) );
  51. m_codeGenerationDataList.Add( new CodeGenerationData( " Exclude Forward", "exclude_path:forward" ) );
  52. m_codeGenerationDataList.Add( new CodeGenerationData( " Exclude Legacy Deferred", "exclude_path:prepass" ) );
  53. m_codeGenerationDataList.Add( new CodeGenerationData( " Shadows", "noshadow" ) );
  54. m_codeGenerationDataList.Add( new CodeGenerationData( " Ambient Light", "noambient" ) );
  55. m_codeGenerationDataList.Add( new CodeGenerationData( " Per Vertex Light", "novertexlights" ) );
  56. m_codeGenerationDataList.Add( new CodeGenerationData( " Lightmaps", "nolightmap " ) );
  57. m_codeGenerationDataList.Add( new CodeGenerationData( " Dynamic Global GI", "nodynlightmap" ) );
  58. m_codeGenerationDataList.Add( new CodeGenerationData( " Directional lightmaps", "nodirlightmap" ) );
  59. m_codeGenerationDataList.Add( new CodeGenerationData( " Built-in Fog", "nofog" ) );
  60. m_codeGenerationDataList.Add( new CodeGenerationData( " Meta Pass", "nometa" ) );
  61. m_codeGenerationDataList.Add( new CodeGenerationData( " Add Pass", "noforwardadd" ) );
  62. }
  63. public bool IsOptionActive( string option )
  64. {
  65. return !m_codeGenerationDataList.Find( x => x.Name.Equals( option ) ).IsActive;
  66. }
  67. public void Draw( ParentNode owner )
  68. {
  69. bool value = owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedRenderingOptions;
  70. NodeUtils.DrawPropertyGroup( ref value, RenderingOptionsStr, () =>
  71. {
  72. int codeGenCount = m_codeGenerationDataList.Count;
  73. // Starting from index 4 because other options are already contemplated with m_renderPath and add/receive shadows
  74. for( int i = 4; i < codeGenCount; i++ )
  75. {
  76. m_codeGenerationDataList[ i ].IsActive = !owner.EditorGUILayoutToggleLeft( m_codeGenerationDataList[ i ].Name, !m_codeGenerationDataList[ i ].IsActive );
  77. }
  78. m_lodCrossfade = owner.EditorGUILayoutToggleLeft( LODCrossfadeContent, m_lodCrossfade );
  79. m_ignoreProjector = owner.EditorGUILayoutToggleLeft( IgnoreProjectorContent, m_ignoreProjector );
  80. m_forceNoShadowCasting = owner.EditorGUILayoutToggleLeft( ForceNoShadowCastingContent, m_forceNoShadowCasting );
  81. if( owner.ContainerGraph.IsInstancedShader )
  82. {
  83. GUI.enabled = false;
  84. owner.EditorGUILayoutToggleLeft( ForceEnableInstancingContent, true );
  85. GUI.enabled = true;
  86. }
  87. else
  88. {
  89. m_forceEnableInstancing = owner.EditorGUILayoutToggleLeft( ForceEnableInstancingContent, m_forceEnableInstancing );
  90. }
  91. #if UNITY_5_6_OR_NEWER
  92. m_forceDisableInstancing = owner.EditorGUILayoutToggleLeft( ForceDisableInstancingContent, m_forceDisableInstancing );
  93. #endif
  94. m_specularHighlightToggle = owner.EditorGUILayoutToggleLeft( SpecularHightlightsContent, m_specularHighlightToggle );
  95. m_reflectionsToggle = owner.EditorGUILayoutToggleLeft( ReflectionsContent, m_reflectionsToggle );
  96. m_disableBatching = (DisableBatchingTagValues)owner.EditorGUILayoutEnumPopup( DisableBatchingContent, m_disableBatching );
  97. Material mat = owner.ContainerGraph.CurrentMaterial;
  98. if( mat != null )
  99. {
  100. mat.globalIlluminationFlags = (MaterialGlobalIlluminationFlags)owner.EditorGUILayoutEnumPopup( EmissionGIFlags, mat.globalIlluminationFlags );
  101. }
  102. } );
  103. owner.ContainerGraph.ParentWindow.InnerWindowVariables.ExpandedRenderingOptions = value;
  104. }
  105. public void Build( ref string OptionalParameters )
  106. {
  107. int codeGenCount = m_codeGenerationDataList.Count;
  108. for( int i = 0; i < codeGenCount; i++ )
  109. {
  110. if( m_codeGenerationDataList[ i ].IsActive )
  111. {
  112. OptionalParameters += m_codeGenerationDataList[ i ].Value + Constants.OptionalParametersSep;
  113. }
  114. }
  115. #if UNITY_2017_1_OR_NEWER
  116. if( m_lodCrossfade )
  117. {
  118. OptionalParameters += Constants.LodCrossFadeOption2017 + Constants.OptionalParametersSep;
  119. }
  120. #endif
  121. }
  122. public void ReadFromString( ref uint index, ref string[] nodeParams )
  123. {
  124. for( int i = 0; i < m_codeGenerationDataList.Count; i++ )
  125. {
  126. m_codeGenerationDataList[ i ].IsActive = Convert.ToBoolean( nodeParams[ index++ ] );
  127. }
  128. if( UIUtils.CurrentShaderVersion() > 10005 )
  129. {
  130. m_lodCrossfade = Convert.ToBoolean( nodeParams[ index++ ] );
  131. }
  132. if( UIUtils.CurrentShaderVersion() > 10007 )
  133. {
  134. m_disableBatching = (DisableBatchingTagValues)Enum.Parse( typeof( DisableBatchingTagValues ), nodeParams[ index++ ] );
  135. m_ignoreProjector = Convert.ToBoolean( nodeParams[ index++ ] );
  136. m_forceNoShadowCasting = Convert.ToBoolean( nodeParams[ index++ ] );
  137. }
  138. if( UIUtils.CurrentShaderVersion() > 11002 )
  139. {
  140. m_forceEnableInstancing = Convert.ToBoolean( nodeParams[ index++ ] );
  141. }
  142. if( UIUtils.CurrentShaderVersion() > 15205 )
  143. {
  144. m_forceDisableInstancing = Convert.ToBoolean( nodeParams[ index++ ] );
  145. }
  146. if( UIUtils.CurrentShaderVersion() > 14403 )
  147. {
  148. m_specularHighlightToggle = Convert.ToBoolean( nodeParams[ index++ ] );
  149. m_reflectionsToggle = Convert.ToBoolean( nodeParams[ index++ ] );
  150. }
  151. }
  152. public void WriteToString( ref string nodeInfo )
  153. {
  154. for( int i = 0; i < m_codeGenerationDataList.Count; i++ )
  155. {
  156. IOUtils.AddFieldValueToString( ref nodeInfo, m_codeGenerationDataList[ i ].IsActive );
  157. }
  158. IOUtils.AddFieldValueToString( ref nodeInfo, m_lodCrossfade );
  159. IOUtils.AddFieldValueToString( ref nodeInfo, m_disableBatching );
  160. IOUtils.AddFieldValueToString( ref nodeInfo, m_ignoreProjector );
  161. IOUtils.AddFieldValueToString( ref nodeInfo, m_forceNoShadowCasting );
  162. IOUtils.AddFieldValueToString( ref nodeInfo, m_forceEnableInstancing );
  163. IOUtils.AddFieldValueToString( ref nodeInfo, m_forceDisableInstancing );
  164. IOUtils.AddFieldValueToString( ref nodeInfo, m_specularHighlightToggle );
  165. IOUtils.AddFieldValueToString( ref nodeInfo, m_reflectionsToggle );
  166. }
  167. public void Destroy()
  168. {
  169. m_codeGenerationDataList.Clear();
  170. m_codeGenerationDataList = null;
  171. }
  172. public bool ForceEnableInstancing { get { return m_forceEnableInstancing; } }
  173. public bool ForceDisableInstancing { get { return m_forceDisableInstancing; } }
  174. public bool LodCrossfade { get { return m_lodCrossfade; } }
  175. public bool IgnoreProjectorValue { get { return m_ignoreProjector; } set { m_ignoreProjector = value; } }
  176. public bool SpecularHighlightToggle { get { return m_specularHighlightToggle; } set { m_specularHighlightToggle = value; } }
  177. public bool ReflectionsToggle { get { return m_reflectionsToggle; } set { m_reflectionsToggle = value; } }
  178. public string DisableBatchingTag { get { return ( m_disableBatching != DisableBatchingTagValues.False ) ? string.Format( Constants.TagFormat, "DisableBatching", m_disableBatching ) : string.Empty; } }
  179. public string IgnoreProjectorTag { get { return ( m_ignoreProjector ) ? string.Format( Constants.TagFormat, "IgnoreProjector", "True" ) : string.Empty; } }
  180. public string ForceNoShadowCastingTag { get { return ( m_forceNoShadowCasting ) ? string.Format( Constants.TagFormat, "ForceNoShadowCasting", "True" ) : string.Empty; } }
  181. }
  182. }