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.

1035 lines
29 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 TexturePropertyValues
  9. {
  10. white,
  11. black,
  12. gray,
  13. bump
  14. }
  15. public enum TextureType
  16. {
  17. Texture1D,
  18. Texture2D,
  19. Texture3D,
  20. Cube,
  21. Texture2DArray,
  22. ProceduralTexture
  23. }
  24. public enum AutoCastType
  25. {
  26. Auto = 0,
  27. LockedToTexture1D,
  28. LockedToTexture2D,
  29. LockedToTexture3D,
  30. LockedToCube,
  31. LockedToTexture2DArray
  32. }
  33. [Serializable]
  34. [NodeAttributes( "Texture Object", "Textures", "Represents a Texture Asset. Can be used in samplers <b>Tex</b> inputs or shader function inputs to reuse the same texture multiple times.", SortOrderPriority = 1 )]
  35. public class TexturePropertyNode : PropertyNode
  36. {
  37. private const string ObjectSelectorCmdStr = "ObjectSelectorClosed";
  38. protected readonly string[] AvailablePropertyTypeLabels = { PropertyType.Property.ToString(), PropertyType.Global.ToString() };
  39. protected readonly int[] AvailablePropertyTypeValues = { (int)PropertyType.Property, (int)PropertyType.Global };
  40. protected const int OriginalFontSizeUpper = 9;
  41. protected const int OriginalFontSizeLower = 9;
  42. protected const string DefaultTextureStr = "Default Texture";
  43. protected const string AutoCastModeStr = "Auto-Cast Mode";
  44. protected const string AutoUnpackNormalsStr = "Normal";
  45. [SerializeField]
  46. protected Texture m_defaultValue;
  47. [SerializeField]
  48. protected Texture m_materialValue;
  49. [SerializeField]
  50. protected TexturePropertyValues m_defaultTextureValue;
  51. [SerializeField]
  52. protected bool m_isNormalMap;
  53. [SerializeField]
  54. protected System.Type m_textureType = typeof( Texture2D );
  55. //[SerializeField]
  56. //protected bool m_isTextureFetched;
  57. //[SerializeField]
  58. //protected string m_textureFetchedValue;
  59. [SerializeField]
  60. protected TextureType m_currentType = TextureType.Texture2D;
  61. [SerializeField]
  62. protected AutoCastType m_autocastMode = AutoCastType.Auto;
  63. protected int PreviewSizeX = 128;
  64. protected int PreviewSizeY = 128;
  65. protected bool m_linearTexture;
  66. protected TexturePropertyNode m_textureProperty = null;
  67. protected bool m_drawPicker;
  68. protected bool m_drawAutocast = true;
  69. protected int m_cachedSamplerId = -1;
  70. protected int m_cachedSamplerIdArray = -1;
  71. protected int m_cachedSamplerIdCube = -1;
  72. protected int m_cachedSamplerId3D = -1;
  73. protected int m_defaultId = -1;
  74. protected int m_typeId = -1;
  75. private TextureType m_previousType = TextureType.Texture2D;
  76. private string m_labelText = "None (Texture2D)";
  77. protected bool m_isEditingPicker;
  78. public TexturePropertyNode() : base() { }
  79. public TexturePropertyNode( int uniqueId, float x, float y, float width, float height ) : base( uniqueId, x, y, width, height ) { }
  80. protected override void CommonInit( int uniqueId )
  81. {
  82. base.CommonInit( uniqueId );
  83. GlobalTypeWarningText = string.Format( GlobalTypeWarningText, "Texture" );
  84. m_defaultTextureValue = TexturePropertyValues.white;
  85. m_insideSize.Set( PreviewSizeX, PreviewSizeY + 5 );
  86. AddOutputPort( WirePortDataType.SAMPLER2D, "Tex" );
  87. m_outputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.OBJECT );
  88. m_currentParameterType = PropertyType.Property;
  89. m_customPrefix = "Texture ";
  90. m_drawPrecisionUI = false;
  91. m_showVariableMode = true;
  92. m_freeType = false;
  93. m_drawPicker = true;
  94. m_hasLeftDropdown = true;
  95. m_textLabelWidth = 115;
  96. m_longNameSize = 225;
  97. m_availableAttribs.Add( new PropertyAttributes( "No Scale Offset", "[NoScaleOffset]" ) );
  98. m_availableAttribs.Add( new PropertyAttributes( "Normal", "[Normal]" ) );
  99. m_showPreview = true;
  100. m_drawPreviewExpander = false;
  101. m_drawPreview = false;
  102. m_drawPreviewMaskButtons = false;
  103. m_previewShaderGUID = "e53988745ec6e034694ee2640cd3d372";
  104. }
  105. public override void AfterCommonInit()
  106. {
  107. base.AfterCommonInit();
  108. m_hasLeftDropdown = true;
  109. }
  110. protected void SetPreviewTexture( Texture newValue )
  111. {
  112. if( newValue is Cubemap )
  113. {
  114. PreviewMaterial.SetInt( m_typeId, 3 );
  115. if( m_cachedSamplerIdCube == -1 )
  116. m_cachedSamplerIdCube = Shader.PropertyToID( "_Cube" );
  117. PreviewMaterial.SetTexture( m_cachedSamplerIdCube, newValue as Cubemap );
  118. }
  119. else if( newValue is Texture2DArray )
  120. {
  121. PreviewMaterial.SetInt( m_typeId, 4 );
  122. if( m_cachedSamplerIdArray == -1 )
  123. m_cachedSamplerIdArray = Shader.PropertyToID( "_Array" );
  124. PreviewMaterial.SetTexture( m_cachedSamplerIdArray, newValue as Texture2DArray );
  125. }
  126. else if( newValue is Texture3D )
  127. {
  128. PreviewMaterial.SetInt( m_typeId, 2 );
  129. if( m_cachedSamplerId3D == -1 )
  130. m_cachedSamplerId3D = Shader.PropertyToID( "_Sampler3D" );
  131. PreviewMaterial.SetTexture( m_cachedSamplerId3D, newValue as Texture3D );
  132. }
  133. else
  134. {
  135. PreviewMaterial.SetInt( m_typeId, 1 );
  136. if( m_cachedSamplerId == -1 )
  137. m_cachedSamplerId = Shader.PropertyToID( "_Sampler" );
  138. PreviewMaterial.SetTexture( m_cachedSamplerId, newValue );
  139. }
  140. }
  141. public override void SetPreviewInputs()
  142. {
  143. base.SetPreviewInputs();
  144. if( Value == null )
  145. {
  146. if( m_defaultId == -1 )
  147. m_defaultId = Shader.PropertyToID( "_Default" );
  148. PreviewMaterial.SetInt( m_defaultId, ( (int)m_defaultTextureValue ) + 1 );
  149. }
  150. else
  151. {
  152. if( m_defaultId == -1 )
  153. m_defaultId = Shader.PropertyToID( "_Default" );
  154. PreviewMaterial.SetInt( m_defaultId, 0 );
  155. if( m_typeId == -1 )
  156. m_typeId = Shader.PropertyToID( "_Type" );
  157. SetPreviewTexture( Value );
  158. //if( Value is Cubemap )
  159. //{
  160. // PreviewMaterial.SetInt( m_typeId, 3 );
  161. // if( m_cachedSamplerIdCube == -1 )
  162. // m_cachedSamplerIdCube = Shader.PropertyToID( "_Cube" );
  163. // PreviewMaterial.SetTexture( m_cachedSamplerIdCube, Value as Cubemap );
  164. //}
  165. //else if( Value is Texture2DArray )
  166. //{
  167. // PreviewMaterial.SetInt( m_typeId, 4 );
  168. // if( m_cachedSamplerIdArray == -1 )
  169. // m_cachedSamplerIdArray = Shader.PropertyToID( "_Array" );
  170. // PreviewMaterial.SetTexture( m_cachedSamplerIdArray, Value as Texture2DArray );
  171. //}
  172. //else if( Value is Texture3D )
  173. //{
  174. // PreviewMaterial.SetInt( m_typeId, 2 );
  175. // if( m_cachedSamplerId3D == -1 )
  176. // m_cachedSamplerId3D = Shader.PropertyToID( "_Sampler3D" );
  177. // PreviewMaterial.SetTexture( m_cachedSamplerId3D, Value as Texture3D );
  178. //}
  179. //else
  180. //{
  181. // PreviewMaterial.SetInt( m_typeId, 1 );
  182. // if( m_cachedSamplerId == -1 )
  183. // m_cachedSamplerId = Shader.PropertyToID( "_Sampler" );
  184. // PreviewMaterial.SetTexture( m_cachedSamplerId, Value );
  185. //}
  186. }
  187. }
  188. protected override void OnUniqueIDAssigned()
  189. {
  190. base.OnUniqueIDAssigned();
  191. m_textureProperty = this;
  192. UIUtils.RegisterPropertyNode( this );
  193. UIUtils.RegisterTexturePropertyNode( this );
  194. }
  195. protected void ConfigTextureData( TextureType type )
  196. {
  197. switch( m_autocastMode )
  198. {
  199. case AutoCastType.Auto:
  200. {
  201. m_currentType = type;
  202. }
  203. break;
  204. case AutoCastType.LockedToTexture1D:
  205. {
  206. m_currentType = TextureType.Texture1D;
  207. }
  208. break;
  209. case AutoCastType.LockedToTexture2DArray:
  210. {
  211. m_currentType = TextureType.Texture2DArray;
  212. }
  213. break;
  214. case AutoCastType.LockedToTexture2D:
  215. {
  216. m_currentType = TextureType.Texture2D;
  217. }
  218. break;
  219. case AutoCastType.LockedToTexture3D:
  220. {
  221. m_currentType = TextureType.Texture3D;
  222. }
  223. break;
  224. case AutoCastType.LockedToCube:
  225. {
  226. m_currentType = TextureType.Cube;
  227. }
  228. break;
  229. }
  230. ConfigTextureType();
  231. }
  232. protected void ConfigTextureType()
  233. {
  234. switch( m_currentType )
  235. {
  236. case TextureType.Texture1D:
  237. {
  238. m_textureType = typeof( Texture );
  239. }
  240. break;
  241. case TextureType.Texture2DArray:
  242. {
  243. m_textureType = typeof( Texture2DArray );
  244. }
  245. break;
  246. case TextureType.Texture2D:
  247. {
  248. m_textureType = typeof( Texture2D );
  249. }
  250. break;
  251. case TextureType.Texture3D:
  252. {
  253. m_textureType = typeof( Texture3D );
  254. }
  255. break;
  256. case TextureType.Cube:
  257. {
  258. m_textureType = typeof( Cubemap );
  259. }
  260. break;
  261. #if !UNITY_2018_1_OR_NEWER
  262. // Disabling Substance Deprecated warning
  263. #pragma warning disable 0618
  264. case TextureType.ProceduralTexture:
  265. {
  266. m_textureType = typeof( ProceduralTexture );
  267. }
  268. break;
  269. #pragma warning restore 0618
  270. #endif
  271. }
  272. }
  273. protected void DrawTexturePropertyType()
  274. {
  275. PropertyType parameterType = (PropertyType)EditorGUILayoutIntPopup( ParameterTypeStr, (int)m_currentParameterType, AvailablePropertyTypeLabels, AvailablePropertyTypeValues );
  276. if( parameterType != m_currentParameterType )
  277. {
  278. ChangeParameterType( parameterType );
  279. }
  280. }
  281. // Texture1D
  282. public string GetTexture1DPropertyValue()
  283. {
  284. return PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
  285. }
  286. public string GetTexture1DUniformValue()
  287. {
  288. return "uniform sampler1D " + PropertyName + ";";
  289. }
  290. // Texture2D
  291. public string GetTexture2DPropertyValue()
  292. {
  293. return PropertyName + "(\"" + m_propertyInspectorName + "\", 2D) = \"" + m_defaultTextureValue + "\" {}";
  294. }
  295. public string GetTexture2DUniformValue()
  296. {
  297. return "uniform sampler2D " + PropertyName + ";";
  298. }
  299. //Texture3D
  300. public string GetTexture3DPropertyValue()
  301. {
  302. return PropertyName + "(\"" + m_propertyInspectorName + "\", 3D) = \"" + m_defaultTextureValue + "\" {}";
  303. }
  304. public string GetTexture3DUniformValue()
  305. {
  306. return "uniform sampler3D " + PropertyName + ";";
  307. }
  308. // Cube
  309. public string GetCubePropertyValue()
  310. {
  311. return PropertyName + "(\"" + m_propertyInspectorName + "\", CUBE) = \"" + m_defaultTextureValue + "\" {}";
  312. }
  313. public string GetCubeUniformValue()
  314. {
  315. return "uniform samplerCUBE " + PropertyName + ";";
  316. }
  317. // Texture2DArray
  318. public string GetTexture2DArrayPropertyValue()
  319. {
  320. return PropertyName + "(\"" + m_propertyInspectorName + "\", 2DArray) = \"" + m_defaultTextureValue + "\" {}";
  321. }
  322. public string GetTexture2DArrayUniformValue()
  323. {
  324. return "uniform TEXTURE2D_ARRAY( " + PropertyName + " );" + "\nuniform SAMPLER( sampler" + PropertyName + " );";
  325. }
  326. public override void DrawMainPropertyBlock()
  327. {
  328. DrawTexturePropertyType();
  329. base.DrawMainPropertyBlock();
  330. }
  331. public override void DrawSubProperties()
  332. {
  333. ShowDefaults();
  334. EditorGUI.BeginChangeCheck();
  335. Type currType = ( m_autocastMode == AutoCastType.Auto ) ? typeof( Texture ) : m_textureType;
  336. m_defaultValue = EditorGUILayoutObjectField( Constants.DefaultValueLabel, m_defaultValue, currType, false ) as Texture;
  337. if( EditorGUI.EndChangeCheck() )
  338. {
  339. CheckTextureImporter( true );
  340. SetAdditonalTitleText( string.Format( Constants.PropertyValueLabel, GetPropertyValStr() ) );
  341. }
  342. }
  343. public override void DrawMaterialProperties()
  344. {
  345. ShowDefaults();
  346. EditorGUI.BeginChangeCheck();
  347. Type currType = ( m_autocastMode == AutoCastType.Auto ) ? typeof( Texture ) : m_textureType;
  348. m_materialValue = EditorGUILayoutObjectField( Constants.MaterialValueLabel, m_materialValue, currType, false ) as Texture;
  349. if( EditorGUI.EndChangeCheck() )
  350. {
  351. CheckTextureImporter( true );
  352. SetAdditonalTitleText( string.Format( Constants.PropertyValueLabel, GetPropertyValStr() ) );
  353. }
  354. }
  355. new void ShowDefaults()
  356. {
  357. m_defaultTextureValue = (TexturePropertyValues)EditorGUILayoutEnumPopup( DefaultTextureStr, m_defaultTextureValue );
  358. if( !m_drawAutocast )
  359. return;
  360. AutoCastType newAutoCast = (AutoCastType)EditorGUILayoutEnumPopup( AutoCastModeStr, m_autocastMode );
  361. if( newAutoCast != m_autocastMode )
  362. {
  363. m_autocastMode = newAutoCast;
  364. if( m_autocastMode != AutoCastType.Auto )
  365. {
  366. ConfigTextureData( m_currentType );
  367. ConfigureInputPorts();
  368. ConfigureOutputPorts();
  369. }
  370. }
  371. }
  372. private void ConfigurePortsFromReference()
  373. {
  374. m_sizeIsDirty = true;
  375. }
  376. public virtual void ConfigureOutputPorts()
  377. {
  378. switch( m_currentType )
  379. {
  380. case TextureType.Texture1D:
  381. m_outputPorts[ 0 ].ChangeType( WirePortDataType.SAMPLER1D, false );
  382. break;
  383. case TextureType.ProceduralTexture:
  384. case TextureType.Texture2D:
  385. m_outputPorts[ 0 ].ChangeType( WirePortDataType.SAMPLER2D, false );
  386. break;
  387. case TextureType.Texture3D:
  388. m_outputPorts[ 0 ].ChangeType( WirePortDataType.SAMPLER3D, false );
  389. break;
  390. case TextureType.Cube:
  391. m_outputPorts[ 0 ].ChangeType( WirePortDataType.SAMPLERCUBE, false );
  392. break;
  393. case TextureType.Texture2DArray:
  394. m_outputPorts[ 0 ].ChangeType( WirePortDataType.SAMPLER2D, false );
  395. break;
  396. }
  397. m_sizeIsDirty = true;
  398. }
  399. public virtual void ConfigureInputPorts()
  400. {
  401. }
  402. public virtual void AdditionalCheck()
  403. {
  404. }
  405. public virtual void CheckTextureImporter( bool additionalCheck, bool writeDefault = true )
  406. {
  407. m_requireMaterialUpdate = true;
  408. Texture texture = m_materialMode ? m_materialValue : m_defaultValue;
  409. TextureImporter importer = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( texture ) ) as TextureImporter;
  410. if( importer != null )
  411. {
  412. #if UNITY_5_5_OR_NEWER
  413. m_isNormalMap = importer.textureType == TextureImporterType.NormalMap;
  414. #else
  415. m_isNormalMap = importer.normalmap;
  416. #endif
  417. if( writeDefault && !UIUtils.IsLoading )
  418. {
  419. if( m_defaultTextureValue == TexturePropertyValues.bump && !m_isNormalMap )
  420. m_defaultTextureValue = TexturePropertyValues.white;
  421. else if( m_isNormalMap )
  422. m_defaultTextureValue = TexturePropertyValues.bump;
  423. }
  424. if( additionalCheck )
  425. AdditionalCheck();
  426. m_linearTexture = !importer.sRGBTexture;
  427. }
  428. if( ( texture as Texture2DArray ) != null )
  429. {
  430. ConfigTextureData( TextureType.Texture2DArray );
  431. }
  432. else if( ( texture as Texture2D ) != null )
  433. {
  434. ConfigTextureData( TextureType.Texture2D );
  435. }
  436. else if( ( texture as Texture3D ) != null )
  437. {
  438. ConfigTextureData( TextureType.Texture3D );
  439. }
  440. else if( ( texture as Cubemap ) != null )
  441. {
  442. ConfigTextureData( TextureType.Cube );
  443. }
  444. #if !UNITY_2018_1_OR_NEWER
  445. // Disabling Substance Deprecated warning
  446. #pragma warning disable 0618
  447. else if( ( texture as ProceduralTexture ) != null )
  448. {
  449. ConfigTextureData( TextureType.ProceduralTexture );
  450. }
  451. #pragma warning restore 0618
  452. #endif
  453. ConfigureInputPorts();
  454. ConfigureOutputPorts();
  455. }
  456. public override void OnObjectDropped( UnityEngine.Object obj )
  457. {
  458. base.OnObjectDropped( obj );
  459. ConfigFromObject( obj );
  460. }
  461. public override void SetupFromCastObject( UnityEngine.Object obj )
  462. {
  463. base.SetupFromCastObject( obj );
  464. ConfigFromObject( obj );
  465. }
  466. protected void ConfigFromObject( UnityEngine.Object obj, bool writeDefault = true, bool additionalCheck = true )
  467. {
  468. Texture texture = obj as Texture;
  469. if( texture )
  470. {
  471. m_materialValue = texture;
  472. m_defaultValue = texture;
  473. CheckTextureImporter( additionalCheck, writeDefault );
  474. }
  475. }
  476. public override void DrawGUIControls( DrawInfo drawInfo )
  477. {
  478. base.DrawGUIControls( drawInfo );
  479. if( !( drawInfo.CurrentEventType == EventType.MouseDown || drawInfo.CurrentEventType == EventType.MouseUp || drawInfo.CurrentEventType == EventType.ExecuteCommand || drawInfo.CurrentEventType == EventType.DragPerform ) )
  480. return;
  481. bool insideBox = m_previewRect.Contains( drawInfo.MousePosition );
  482. bool closePicker = false;
  483. if( insideBox )
  484. {
  485. m_isEditingPicker = true;
  486. }
  487. else if( m_isEditingPicker && !insideBox && drawInfo.CurrentEventType != EventType.ExecuteCommand )
  488. {
  489. closePicker = true;
  490. }
  491. if( m_isEditingPicker && drawInfo.CurrentEventType == EventType.ExecuteCommand &&
  492. Event.current.commandName.Equals( ObjectSelectorCmdStr ) )
  493. {
  494. closePicker = true;
  495. }
  496. if( closePicker )
  497. {
  498. GUI.FocusControl( null );
  499. m_isEditingPicker = false;
  500. }
  501. }
  502. public override void OnNodeLayout( DrawInfo drawInfo )
  503. {
  504. base.OnNodeLayout( drawInfo );
  505. ConfigTextureType();
  506. }
  507. public override void Draw( DrawInfo drawInfo )
  508. {
  509. base.Draw( drawInfo );
  510. if( m_dropdownEditing )
  511. {
  512. PropertyType parameterType = (PropertyType)EditorGUIIntPopup( m_dropdownRect,(int)m_currentParameterType, AvailablePropertyTypeLabels, AvailablePropertyTypeValues , UIUtils.PropertyPopUp );
  513. if( parameterType != m_currentParameterType )
  514. {
  515. ChangeParameterType( parameterType );
  516. m_dropdownEditing = false;
  517. }
  518. }
  519. if( m_isEditingPicker && m_drawPicker && m_currentParameterType != PropertyType.Global)
  520. {
  521. Rect hitRect = m_previewRect;
  522. hitRect.height = 14 * drawInfo.InvertedZoom;
  523. hitRect.y = m_previewRect.yMax - hitRect.height;
  524. hitRect.width = 4 * 14 * drawInfo.InvertedZoom;
  525. bool restoreMouse = false;
  526. if( Event.current.type == EventType.MouseDown && hitRect.Contains( drawInfo.MousePosition ) )
  527. {
  528. restoreMouse = true;
  529. Event.current.type = EventType.Ignore;
  530. }
  531. EditorGUI.BeginChangeCheck();
  532. m_colorBuffer = GUI.color;
  533. GUI.color = Color.clear;
  534. Type currType = ( m_autocastMode == AutoCastType.Auto ) ? typeof( Texture ) : m_textureType;
  535. if( m_materialMode )
  536. {
  537. m_materialValue = EditorGUIObjectField( m_previewRect, m_materialValue, currType, false ) as Texture;
  538. }
  539. else
  540. {
  541. m_defaultValue = EditorGUIObjectField( m_previewRect, m_defaultValue, currType, false ) as Texture;
  542. }
  543. GUI.color = m_colorBuffer;
  544. if( EditorGUI.EndChangeCheck() )
  545. {
  546. CheckTextureImporter( true );
  547. SetTitleText( m_propertyInspectorName );
  548. SetAdditonalTitleText( string.Format( Constants.PropertyValueLabel, GetPropertyValStr() ) );
  549. ConfigureInputPorts();
  550. ConfigureOutputPorts();
  551. BeginDelayedDirtyProperty();
  552. }
  553. //else if( drawInfo.CurrentEventType == EventType.ExecuteCommand )
  554. //{
  555. // GUI.FocusControl( null );
  556. // m_isEditingPicker = false;
  557. //}
  558. if( restoreMouse )
  559. {
  560. Event.current.type = EventType.MouseDown;
  561. }
  562. if( ( drawInfo.CurrentEventType == EventType.MouseDown || drawInfo.CurrentEventType == EventType.MouseUp ) )
  563. DrawPreviewMaskButtonsLayout( drawInfo, m_previewRect );
  564. }
  565. if( !m_drawPicker )
  566. return;
  567. if( drawInfo.CurrentEventType == EventType.Repaint )
  568. {
  569. DrawTexturePicker( drawInfo );
  570. }
  571. }
  572. protected void DrawTexturePicker( DrawInfo drawInfo )
  573. {
  574. Rect newRect = m_previewRect;
  575. Texture currentValue = m_materialMode ? m_materialValue : m_defaultValue;
  576. //???
  577. //m_showPreview = true;
  578. bool showButtons = m_currentParameterType != PropertyType.Global;
  579. if( currentValue == null )
  580. GUI.Label( newRect, string.Empty, UIUtils.ObjectFieldThumb );
  581. else
  582. DrawPreview( drawInfo, m_previewRect );
  583. if( ContainerGraph.LodLevel <= ParentGraph.NodeLOD.LOD2 )
  584. {
  585. Rect butRect = m_previewRect;
  586. butRect.y -= 1;
  587. butRect.x += 1;
  588. Rect smallButton = newRect;
  589. smallButton.height = 14 * drawInfo.InvertedZoom;
  590. smallButton.y = newRect.yMax - smallButton.height - 2;
  591. smallButton.width = 40 * drawInfo.InvertedZoom;
  592. smallButton.x = newRect.xMax - smallButton.width - 2;
  593. if( currentValue == null )
  594. {
  595. if( m_previousType != m_currentType )
  596. {
  597. m_previousType = m_currentType;
  598. m_labelText = "None (" + m_currentType.ToString() + ")";
  599. }
  600. GUI.Label( newRect, m_labelText, UIUtils.ObjectFieldThumbOverlay );
  601. }
  602. else if( showButtons )
  603. {
  604. DrawPreviewMaskButtonsRepaint( drawInfo, butRect );
  605. }
  606. if( showButtons )
  607. GUI.Label( smallButton, "Select", UIUtils.GetCustomStyle( CustomStyle.SamplerButton ) );
  608. }
  609. GUI.Label( newRect, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerFrame ) );
  610. }
  611. public string BaseGenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  612. {
  613. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
  614. return PropertyName;
  615. }
  616. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  617. {
  618. return BaseGenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
  619. }
  620. public override void UpdateMaterial( Material mat )
  621. {
  622. base.UpdateMaterial( mat );
  623. if( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  624. {
  625. OnPropertyNameChanged();
  626. if( mat.HasProperty( PropertyName ) )
  627. {
  628. mat.SetTexture( PropertyName, m_materialValue );
  629. }
  630. }
  631. }
  632. public override void SetMaterialMode( Material mat, bool fetchMaterialValues )
  633. {
  634. base.SetMaterialMode( mat, fetchMaterialValues );
  635. if( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) )
  636. {
  637. if( mat.HasProperty( PropertyName ) )
  638. {
  639. m_materialValue = mat.GetTexture( PropertyName );
  640. CheckTextureImporter( false, false );
  641. }
  642. }
  643. }
  644. public override void ForceUpdateFromMaterial( Material material )
  645. {
  646. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( PropertyName ) )
  647. {
  648. m_materialValue = material.GetTexture( PropertyName );
  649. CheckTextureImporter( false, false );
  650. }
  651. }
  652. public override bool UpdateShaderDefaults( ref Shader shader, ref TextureDefaultsDataColector defaultCol/* ref string metaStr */)
  653. {
  654. if( m_defaultValue != null )
  655. {
  656. defaultCol.AddValue( PropertyName, m_defaultValue );
  657. }
  658. return true;
  659. }
  660. public override void ReadFromString( ref string[] nodeParams )
  661. {
  662. base.ReadFromString( ref nodeParams );
  663. ReadAdditionalData( ref nodeParams );
  664. }
  665. public virtual void ReadAdditionalData( ref string[] nodeParams )
  666. {
  667. string defaultTextureGUID = GetCurrentParam( ref nodeParams );
  668. //m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( textureName );
  669. if( UIUtils.CurrentShaderVersion() > 14101 )
  670. {
  671. m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( defaultTextureGUID ) );
  672. string materialTextureGUID = GetCurrentParam( ref nodeParams );
  673. m_materialValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( materialTextureGUID ) );
  674. }
  675. else
  676. {
  677. m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( defaultTextureGUID );
  678. }
  679. m_isNormalMap = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  680. m_defaultTextureValue = (TexturePropertyValues)Enum.Parse( typeof( TexturePropertyValues ), GetCurrentParam( ref nodeParams ) );
  681. m_autocastMode = (AutoCastType)Enum.Parse( typeof( AutoCastType ), GetCurrentParam( ref nodeParams ) );
  682. if( UIUtils.CurrentShaderVersion() > 15306 )
  683. {
  684. m_currentType = (TextureType)Enum.Parse( typeof( TextureType ), GetCurrentParam( ref nodeParams ) );
  685. }
  686. else
  687. {
  688. m_currentType = TextureType.Texture2D;
  689. }
  690. ConfigTextureData( m_currentType );
  691. //ConfigFromObject( m_defaultValue );
  692. if( m_materialValue == null )
  693. {
  694. ConfigFromObject( m_defaultValue );
  695. }
  696. else
  697. {
  698. CheckTextureImporter( true, true );
  699. }
  700. ConfigureInputPorts();
  701. ConfigureOutputPorts();
  702. }
  703. public override void ReadAdditionalClipboardData( ref string[] nodeParams )
  704. {
  705. base.ReadAdditionalClipboardData( ref nodeParams );
  706. string textureName = GetCurrentParam( ref nodeParams );
  707. m_materialValue = AssetDatabase.LoadAssetAtPath<Texture>( textureName );
  708. }
  709. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  710. {
  711. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  712. WriteAdditionalToString( ref nodeInfo, ref connectionsInfo );
  713. }
  714. public virtual void WriteAdditionalToString( ref string nodeInfo, ref string connectionsInfo )
  715. {
  716. //IOUtils.AddFieldValueToString( ref nodeInfo, ( m_defaultValue != null ) ? AssetDatabase.GetAssetPath( m_defaultValue ) : Constants.NoStringValue );
  717. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_defaultValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_defaultValue ) ) : Constants.NoStringValue );
  718. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_materialValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_materialValue ) ) : Constants.NoStringValue );
  719. IOUtils.AddFieldValueToString( ref nodeInfo, m_isNormalMap.ToString() );
  720. IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultTextureValue );
  721. IOUtils.AddFieldValueToString( ref nodeInfo, m_autocastMode );
  722. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentType );
  723. }
  724. public override void WriteAdditionalClipboardData( ref string nodeInfo )
  725. {
  726. base.WriteAdditionalClipboardData( ref nodeInfo );
  727. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_materialValue != null ) ? AssetDatabase.GetAssetPath( m_materialValue ) : Constants.NoStringValue );
  728. }
  729. public override void Destroy()
  730. {
  731. base.Destroy();
  732. m_defaultValue = null;
  733. m_materialValue = null;
  734. m_textureProperty = null;
  735. UIUtils.UnregisterPropertyNode( this );
  736. UIUtils.UnregisterTexturePropertyNode( this );
  737. }
  738. public override string GetPropertyValStr()
  739. {
  740. return m_materialMode ? ( m_materialValue != null ? m_materialValue.name : IOUtils.NO_TEXTURES ) : ( m_defaultValue != null ? m_defaultValue.name : IOUtils.NO_TEXTURES );
  741. }
  742. public override string GetPropertyValue()
  743. {
  744. switch( m_currentType )
  745. {
  746. case TextureType.Texture1D:
  747. {
  748. return PropertyAttributes + GetTexture1DPropertyValue();
  749. }
  750. case TextureType.ProceduralTexture:
  751. case TextureType.Texture2D:
  752. {
  753. return PropertyAttributes + GetTexture2DPropertyValue();
  754. }
  755. case TextureType.Texture3D:
  756. {
  757. return PropertyAttributes + GetTexture3DPropertyValue();
  758. }
  759. case TextureType.Cube:
  760. {
  761. return PropertyAttributes + GetCubePropertyValue();
  762. }
  763. case TextureType.Texture2DArray:
  764. {
  765. return PropertyAttributes + GetTexture2DArrayPropertyValue();
  766. }
  767. }
  768. return string.Empty;
  769. }
  770. public override string GetUniformValue()
  771. {
  772. switch( m_currentType )
  773. {
  774. case TextureType.Texture1D:
  775. {
  776. return GetTexture1DUniformValue();
  777. }
  778. case TextureType.ProceduralTexture:
  779. case TextureType.Texture2D:
  780. {
  781. return GetTexture2DUniformValue();
  782. }
  783. case TextureType.Texture3D:
  784. {
  785. return GetTexture3DUniformValue();
  786. }
  787. case TextureType.Cube:
  788. {
  789. return GetCubeUniformValue();
  790. }
  791. case TextureType.Texture2DArray:
  792. {
  793. return GetTexture2DArrayUniformValue();
  794. }
  795. }
  796. return string.Empty;
  797. }
  798. public override bool GetUniformData( out string dataType, out string dataName )
  799. {
  800. if( m_currentType == TextureType.Texture2DArray )
  801. {
  802. MasterNode masterNode = UIUtils.CurrentWindow.OutsideGraph.CurrentMasterNode;
  803. if( masterNode.CurrentDataCollector.IsTemplate && masterNode.CurrentDataCollector.IsSRP )
  804. {
  805. dataType = "TEXTURE2D_ARRAY( " + PropertyName + "";
  806. dataName = ");\nuniform SAMPLER( sampler" + PropertyName + " )";
  807. return true;
  808. }
  809. dataType = "UNITY_DECLARE_TEX2DARRAY(";
  810. dataName = m_propertyName + " )";
  811. return true;
  812. }
  813. dataType = UIUtils.TextureTypeToCgType( m_currentType );
  814. dataName = m_propertyName;
  815. return true;
  816. }
  817. public virtual string CurrentPropertyReference
  818. {
  819. get
  820. {
  821. string propertyName = string.Empty;
  822. propertyName = PropertyName;
  823. return propertyName;
  824. }
  825. }
  826. public Texture Value
  827. {
  828. get { return m_materialMode ? m_materialValue : m_defaultValue; }
  829. set
  830. {
  831. if( m_materialMode )
  832. m_materialValue = value;
  833. else
  834. m_defaultValue = value;
  835. }
  836. }
  837. public Texture MaterialValue
  838. {
  839. get { return m_materialValue; }
  840. set { m_materialValue = value; }
  841. }
  842. public Texture DefaultValue
  843. {
  844. get { return m_defaultValue; }
  845. set { m_defaultValue = value; }
  846. }
  847. public void SetInspectorName( string newName )
  848. {
  849. m_propertyInspectorName = newName;
  850. }
  851. public void SetPropertyName( string newName )
  852. {
  853. m_propertyName = newName;
  854. }
  855. public bool IsValid { get { return m_materialMode ? ( m_materialValue != null ) : ( m_defaultValue != null ); } }
  856. public virtual bool IsNormalMap { get { return m_isNormalMap; } }
  857. public bool IsLinearTexture { get { return m_linearTexture; } }
  858. public override void OnPropertyNameChanged()
  859. {
  860. base.OnPropertyNameChanged();
  861. UIUtils.UpdateTexturePropertyDataNode( UniqueId, PropertyInspectorName );
  862. }
  863. public override void SetGlobalValue() { Shader.SetGlobalTexture( m_propertyName, m_defaultValue ); }
  864. public override void FetchGlobalValue() { m_materialValue = Shader.GetGlobalTexture( m_propertyName ); }
  865. public override string DataToArray { get { return PropertyInspectorName; } }
  866. public TextureType CurrentType { get { return m_currentType; } }
  867. public bool DrawAutocast
  868. {
  869. get { return m_drawAutocast; }
  870. set { m_drawAutocast = value; }
  871. }
  872. public TexturePropertyValues DefaultTextureValue
  873. {
  874. get { return m_defaultTextureValue; }
  875. set { m_defaultTextureValue = value; }
  876. }
  877. public AutoCastType AutocastMode
  878. {
  879. get { return m_autocastMode; }
  880. }
  881. }
  882. }