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.

296 lines
9.3 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. public enum VirtualPreset
  9. {
  10. Unity_Legacy,
  11. Unity5,
  12. Alloy,
  13. UBER,
  14. Skyshop,
  15. Lux
  16. }
  17. public enum VirtualChannel
  18. {
  19. Albedo = 0,
  20. Base,
  21. Normal,
  22. Height,
  23. Occlusion,
  24. Displacement,
  25. Specular,
  26. SpecMet,
  27. Material,
  28. }
  29. [Serializable]
  30. [NodeAttributes( "Virtual Texture Object", "Textures", "Represents a Virtual Texture Asset", SortOrderPriority = 1 )]
  31. public class VirtualTextureObject : TexturePropertyNode
  32. {
  33. protected const string VirtualPresetStr = "Layout Preset";
  34. protected const string VirtualChannelStr = "Virtual Layer";
  35. private const string VirtualTextureObjectInfo = "Can only be used alongside a Texture Sample node by connecting to its Tex Input Port.\n" +
  36. "\nProperty name must match the value set on your Virtual Texture.\n" +
  37. "Default e.g Albedo = _MainTex\n" +
  38. "\nName your node according to the respective channel property in your Virtual Texture. The Albedo must be set to _MainTex ( temporary requirement ).";
  39. private readonly string[] ChannelTypeStr = {
  40. "Albedo - D.RGBA",
  41. "Base - D.RGBA",
  42. "Normal - N.GA",
  43. "Height - N.B",
  44. "Occlusion - N.R",
  45. "Displacement - N.B",
  46. "Specular - S.RGBA",
  47. "Specular|Metallic - S.RGBA",
  48. "Material - S.RGBA",};
  49. private readonly string[] Dummy = { string.Empty };
  50. private string[] m_channelTypeStr;
  51. [SerializeField]
  52. protected VirtualPreset m_virtualPreset = VirtualPreset.Unity5;
  53. [SerializeField]
  54. protected VirtualChannel m_virtualChannel = VirtualChannel.Albedo;
  55. [SerializeField]
  56. private int m_selectedChannelInt = 0;
  57. protected override void CommonInit( int uniqueId )
  58. {
  59. base.CommonInit( uniqueId );
  60. ChangeChannels();
  61. }
  62. protected override void OnUniqueIDAssigned()
  63. {
  64. base.OnUniqueIDAssigned();
  65. if ( UniqueId != -1 )
  66. UIUtils.AddVirtualTextureCount();
  67. }
  68. public override void DrawSubProperties()
  69. {
  70. ShowDefaults();
  71. base.DrawSubProperties();
  72. }
  73. public override void DrawMaterialProperties()
  74. {
  75. ShowDefaults();
  76. base.DrawMaterialProperties();
  77. }
  78. new void ShowDefaults()
  79. {
  80. EditorGUI.BeginChangeCheck();
  81. m_virtualPreset = ( VirtualPreset ) EditorGUILayoutEnumPopup( VirtualPresetStr, m_virtualPreset );
  82. if ( EditorGUI.EndChangeCheck() )
  83. {
  84. ChangeChannels();
  85. }
  86. EditorGUI.BeginChangeCheck();
  87. m_selectedChannelInt = EditorGUILayoutPopup( VirtualChannelStr, m_selectedChannelInt, m_channelTypeStr );
  88. if ( EditorGUI.EndChangeCheck() )
  89. {
  90. m_virtualChannel = GetChannel( m_selectedChannelInt );
  91. }
  92. }
  93. public override void DrawProperties()
  94. {
  95. base.DrawProperties();
  96. EditorGUILayout.HelpBox( VirtualTextureObjectInfo, MessageType.Info );
  97. }
  98. private VirtualChannel GetChannel( int popupInt )
  99. {
  100. int remapInt = 0;
  101. switch ( m_virtualPreset )
  102. {
  103. case VirtualPreset.Unity_Legacy:
  104. remapInt = popupInt == 0 ? 1 : popupInt == 1 ? 2 : popupInt == 2 ? 4 : popupInt == 3 ? 5 : 0;
  105. break;
  106. default:
  107. case VirtualPreset.Unity5:
  108. case VirtualPreset.UBER:
  109. remapInt = popupInt == 0 ? 0 : popupInt == 1 ? 7 : popupInt == 2 ? 2 : popupInt == 3 ? 3 : popupInt == 4 ? 4 : 0;
  110. break;
  111. case VirtualPreset.Alloy:
  112. remapInt = popupInt == 0 ? 1 : popupInt == 1 ? 2 : popupInt == 2 ? 8 : popupInt == 3 ? 3 : 0;
  113. break;
  114. case VirtualPreset.Skyshop:
  115. case VirtualPreset.Lux:
  116. remapInt = popupInt == 0 ? 1 : popupInt == 1 ? 2 : popupInt == 2 ? 6 : 0;
  117. break;
  118. }
  119. return ( VirtualChannel ) remapInt;
  120. }
  121. private void ChangeChannels()
  122. {
  123. m_channelTypeStr = Dummy;
  124. switch ( m_virtualPreset )
  125. {
  126. case VirtualPreset.Unity_Legacy:
  127. m_channelTypeStr = new string[] { ChannelTypeStr[ 1 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 4 ], ChannelTypeStr[ 5 ] };
  128. break;
  129. default:
  130. case VirtualPreset.Unity5:
  131. case VirtualPreset.UBER:
  132. m_channelTypeStr = new string[] { ChannelTypeStr[ 0 ], ChannelTypeStr[ 7 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 3 ], ChannelTypeStr[ 4 ] };
  133. break;
  134. case VirtualPreset.Alloy:
  135. m_channelTypeStr = new string[] { ChannelTypeStr[ 1 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 8 ], ChannelTypeStr[ 3 ] };
  136. break;
  137. case VirtualPreset.Skyshop:
  138. case VirtualPreset.Lux:
  139. m_channelTypeStr = new string[] { ChannelTypeStr[ 1 ], ChannelTypeStr[ 2 ], ChannelTypeStr[ 6 ] };
  140. break;
  141. }
  142. }
  143. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  144. {
  145. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
  146. dataCollector.AddToProperties( UniqueId, "[HideInInspector] _VTInfoBlock( \"VT( auto )\", Vector ) = ( 0, 0, 0, 0 )", -1 );
  147. return PropertyName;
  148. }
  149. public override string GetPropertyValue()
  150. {
  151. string propertyValue = string.Empty;
  152. switch ( m_virtualChannel )
  153. {
  154. default:
  155. case VirtualChannel.Albedo:
  156. case VirtualChannel.Base:
  157. propertyValue = PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
  158. break;
  159. case VirtualChannel.Normal:
  160. propertyValue = PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
  161. break;
  162. case VirtualChannel.SpecMet:
  163. propertyValue = PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
  164. break;
  165. }
  166. return PropertyAttributes + propertyValue;
  167. }
  168. public override string GetUniformValue()
  169. {
  170. return "uniform sampler2D " + PropertyName + ";";
  171. }
  172. public override bool GetUniformData( out string dataType, out string dataName )
  173. {
  174. dataType = "sampler2D";
  175. dataName = PropertyName;
  176. return true;
  177. }
  178. public override void ReadFromString( ref string[] nodeParams )
  179. {
  180. base.ReadFromString( ref nodeParams );
  181. string defaultTextureGUID = GetCurrentParam( ref nodeParams );
  182. //m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( textureName );
  183. if( UIUtils.CurrentShaderVersion() > 14101 )
  184. {
  185. m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( defaultTextureGUID ) );
  186. string materialTextureGUID = GetCurrentParam( ref nodeParams );
  187. m_materialValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( materialTextureGUID ) );
  188. }
  189. else
  190. {
  191. m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( defaultTextureGUID );
  192. }
  193. m_isNormalMap = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  194. m_defaultTextureValue = ( TexturePropertyValues ) Enum.Parse( typeof( TexturePropertyValues ), GetCurrentParam( ref nodeParams ) );
  195. m_autocastMode = ( AutoCastType ) Enum.Parse( typeof( AutoCastType ), GetCurrentParam( ref nodeParams ) );
  196. m_virtualPreset = ( VirtualPreset ) Enum.Parse( typeof( VirtualPreset ), GetCurrentParam( ref nodeParams ) );
  197. m_selectedChannelInt = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  198. ChangeChannels();
  199. m_virtualChannel = GetChannel( m_selectedChannelInt );
  200. //m_forceNodeUpdate = true;
  201. //ConfigFromObject( m_defaultValue );
  202. if( m_materialValue == null )
  203. {
  204. ConfigFromObject( m_defaultValue );
  205. }
  206. else
  207. {
  208. CheckTextureImporter( true, true );
  209. }
  210. ConfigureInputPorts();
  211. ConfigureOutputPorts();
  212. }
  213. public override void ReadAdditionalData( ref string[] nodeParams ) { }
  214. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  215. {
  216. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  217. //IOUtils.AddFieldValueToString( ref nodeInfo, ( m_defaultValue != null ) ? AssetDatabase.GetAssetPath( m_defaultValue ) : Constants.NoStringValue );
  218. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_defaultValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_defaultValue ) ) : Constants.NoStringValue );
  219. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_materialValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_materialValue ) ) : Constants.NoStringValue );
  220. IOUtils.AddFieldValueToString( ref nodeInfo, m_isNormalMap.ToString() );
  221. IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultTextureValue );
  222. IOUtils.AddFieldValueToString( ref nodeInfo, m_autocastMode );
  223. IOUtils.AddFieldValueToString( ref nodeInfo, m_virtualPreset );
  224. IOUtils.AddFieldValueToString( ref nodeInfo, m_selectedChannelInt );
  225. }
  226. public override void WriteAdditionalToString( ref string nodeInfo, ref string connectionsInfo ) { }
  227. //public override string PropertyName
  228. //{
  229. // get
  230. // {
  231. // string propertyName = string.Empty;
  232. // switch ( m_virtualChannel )
  233. // {
  234. // default:
  235. // case VirtualChannel.Albedo:
  236. // case VirtualChannel.Base:
  237. // propertyName = "_MainTex";
  238. // break;
  239. // case VirtualChannel.Normal:
  240. // propertyName = "_BumpMap";
  241. // break;
  242. // case VirtualChannel.SpecMet:
  243. // propertyName = "_MetallicGlossMap";
  244. // break;
  245. // case VirtualChannel.Occlusion:
  246. // propertyName = "_OcclusionMap";
  247. // break;
  248. // }
  249. // return propertyName;
  250. // }
  251. //}
  252. public override void Destroy()
  253. {
  254. base.Destroy();
  255. UIUtils.RemoveVirtualTextureCount();
  256. }
  257. public override bool IsNormalMap { get { return m_isNormalMap || m_virtualChannel == VirtualChannel.Normal; } }
  258. public VirtualChannel Channel { get { return m_virtualChannel; } }
  259. }
  260. }