TexturePropertyNode.cs 32 KB

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