SamplerNode.cs 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  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. // Disabling Substance Deprecated warning
  9. public enum TexReferenceType
  10. {
  11. Object = 0,
  12. Instance
  13. }
  14. public enum MipType
  15. {
  16. Auto,
  17. MipLevel,
  18. MipBias,
  19. Derivative
  20. }
  21. public enum ReferenceState
  22. {
  23. Self,
  24. Connected,
  25. Instance
  26. }
  27. [Serializable]
  28. [NodeAttributes( "Texture Sample", "Textures", "Samples a chosen texture and returns its color values, <b>Texture</b> and <b>UVs</b> can be overriden and you can select different mip modes and levels. It can also unpack and scale textures marked as normalmaps.", KeyCode.T, true, 0, int.MaxValue, typeof( Texture ), typeof( Texture2D ), typeof( Texture3D ), typeof( Cubemap ), typeof( CustomRenderTexture ), Tags = "Array" )]
  29. public sealed class SamplerNode : TexturePropertyNode
  30. {
  31. private const string MipModeStr = "Mip Mode";
  32. private const string DefaultTextureUseSematicsStr = "Use Semantics";
  33. private const string DefaultTextureIsNormalMapsStr = "Is Normal Map";
  34. private const string NormalScaleStr = "Scale";
  35. private float InstanceIconWidth = 19;
  36. private float InstanceIconHeight = 19;
  37. private readonly Color ReferenceHeaderColor = new Color( 2.66f, 1.02f, 0.6f, 1.0f );
  38. public readonly static int[] AvailableAutoCast = { 0, 1, 2, 3, 4 };
  39. public readonly static string[] AvailableAutoCastStr = { "Auto", "Locked To Texture 1D", "Locked To Texture 2D", "Locked To Texture 3D", "Locked To Cube" };
  40. [SerializeField]
  41. private int m_textureCoordSet = 0;
  42. [SerializeField]
  43. private bool m_autoUnpackNormals = false;
  44. [SerializeField]
  45. private bool m_useSemantics;
  46. [SerializeField]
  47. private string m_samplerType;
  48. [SerializeField]
  49. private MipType m_mipMode = MipType.Auto;
  50. [SerializeField]
  51. private TexReferenceType m_referenceType = TexReferenceType.Object;
  52. [SerializeField]
  53. private int m_referenceArrayId = -1;
  54. [SerializeField]
  55. private int m_referenceNodeId = -1;
  56. private SamplerNode m_referenceSampler = null;
  57. [SerializeField]
  58. private GUIStyle m_referenceStyle = null;
  59. [SerializeField]
  60. private GUIStyle m_referenceIconStyle = null;
  61. [SerializeField]
  62. private GUIContent m_referenceContent = null;
  63. [SerializeField]
  64. private float m_referenceWidth = -1;
  65. [SerializeField]
  66. private SamplerStateAutoGenerator m_samplerStateAutoGenerator = new SamplerStateAutoGenerator();
  67. private Vector4Node m_texCoordsHelper;
  68. private string m_previousAdditionalText = string.Empty;
  69. private int m_cachedUvsId = -1;
  70. private int m_cachedUnpackId = -1;
  71. private int m_cachedLodId = -1;
  72. private InputPort m_texPort;
  73. private InputPort m_uvPort;
  74. private InputPort m_lodPort;
  75. private InputPort m_ddxPort;
  76. private InputPort m_ddyPort;
  77. private InputPort m_normalPort;
  78. private InputPort m_samplerPort;
  79. private InputPort m_indexPort;
  80. private OutputPort m_colorPort;
  81. private TexturePropertyNode m_previewTextProp = null;
  82. private ReferenceState m_state = ReferenceState.Self;
  83. private Rect m_iconPos;
  84. public SamplerNode() : base() { }
  85. public SamplerNode( int uniqueId, float x, float y, float width, float height ) : base( uniqueId, x, y, width, height ) { }
  86. protected override void CommonInit( int uniqueId )
  87. {
  88. base.CommonInit( uniqueId );
  89. if( m_useSamplerArrayIdx < 0 )
  90. {
  91. m_useSamplerArrayIdx = 0;
  92. }
  93. m_defaultTextureValue = TexturePropertyValues.white;
  94. AddInputPort( WirePortDataType.SAMPLER2D, false, "Tex" );
  95. m_inputPorts[ 0 ].CreatePortRestrictions( WirePortDataType.SAMPLER1D, WirePortDataType.SAMPLER2D, WirePortDataType.SAMPLER3D, WirePortDataType.SAMPLERCUBE, WirePortDataType.SAMPLER2DARRAY, WirePortDataType.OBJECT );
  96. AddInputPort( WirePortDataType.FLOAT2, false, "UV" );
  97. AddInputPort( WirePortDataType.FLOAT, false, "Level" );
  98. AddInputPort( WirePortDataType.FLOAT2, false, "DDX" );
  99. AddInputPort( WirePortDataType.FLOAT2, false, "DDY" );
  100. AddInputPort( WirePortDataType.FLOAT, false, NormalScaleStr );
  101. AddInputPort( WirePortDataType.FLOAT, false, "Index" );
  102. AddInputPort( WirePortDataType.SAMPLERSTATE, false, "SS" );
  103. m_inputPorts[ 7 ].CreatePortRestrictions( WirePortDataType.SAMPLERSTATE );
  104. m_texPort = m_inputPorts[ 0 ];
  105. m_uvPort = m_inputPorts[ 1 ];
  106. m_lodPort = m_inputPorts[ 2 ];
  107. m_ddxPort = m_inputPorts[ 3 ];
  108. m_ddyPort = m_inputPorts[ 4 ];
  109. m_normalPort = m_inputPorts[ 5 ];
  110. m_indexPort = m_inputPorts[ 6 ];
  111. m_samplerPort = m_inputPorts[ 7 ];
  112. m_lodPort.AutoDrawInternalData = true;
  113. m_indexPort.AutoDrawInternalData = true;
  114. m_normalPort.AutoDrawInternalData = true;
  115. m_lodPort.Visible = false;
  116. m_ddxPort.Visible = false;
  117. m_ddyPort.Visible = false;
  118. m_indexPort.Visible = false;
  119. m_normalPort.Visible = m_autoUnpackNormals;
  120. m_normalPort.FloatInternalData = 1.0f;
  121. //Remove output port (sampler)
  122. m_outputPortsDict.Remove( m_outputPorts[ 1 ].PortId );
  123. m_outputPorts.RemoveAt( 1 );
  124. m_outputPortsDict.Remove( m_outputPorts[ 0 ].PortId );
  125. m_outputPorts.RemoveAt( 0 );
  126. AddOutputColorPorts( "RGBA", addRGB: true );
  127. m_sortOutputPorts = true;
  128. m_colorPort = m_outputPorts[ 0 ];
  129. m_currentParameterType = PropertyType.Property;
  130. // m_useCustomPrefix = true;
  131. m_customPrefix = "Texture Sample ";
  132. m_referenceContent = new GUIContent( string.Empty );
  133. m_freeType = false;
  134. m_useSemantics = true;
  135. m_drawPicker = false;
  136. ConfigTextureData( TextureType.Texture2D );
  137. m_selectedLocation = PreviewLocation.TopCenter;
  138. m_previewShaderGUID = "7b4e86a89b70ae64993bf422eb406422";
  139. m_errorMessageTooltip = "A texture object marked as normal map is connected to this sampler. Please consider turning on the Unpack Normal Map option";
  140. m_errorMessageTypeIsError = NodeMessageType.Warning;
  141. m_textLabelWidth = 135;
  142. m_customPrecision = false;
  143. }
  144. public override void SetPreviewInputs()
  145. {
  146. //TODO: rewrite this to be faster
  147. base.SetPreviewInputs();
  148. if( m_cachedUvsId == -1 )
  149. m_cachedUvsId = Shader.PropertyToID( "_CustomUVs" );
  150. PreviewMaterial.SetInt( m_cachedUvsId, ( m_uvPort.IsConnected ? 1 : 0 ) );
  151. if( m_cachedUnpackId == -1 )
  152. m_cachedUnpackId = Shader.PropertyToID( "_Unpack" );
  153. PreviewMaterial.SetInt( m_cachedUnpackId, m_autoUnpackNormals ? 1 : 0 );
  154. if( m_cachedLodId == -1 )
  155. m_cachedLodId = Shader.PropertyToID( "_LodType" );
  156. PreviewMaterial.SetInt( m_cachedLodId, ( m_mipMode == MipType.MipLevel ? 1 : ( m_mipMode == MipType.MipBias ? 2 : 0 ) ) );
  157. if( m_typeId == -1 )
  158. m_typeId = Shader.PropertyToID( "_Type" );
  159. bool usingTexture = false;
  160. if( m_texPort.IsConnected )
  161. {
  162. usingTexture = true;
  163. SetPreviewTexture( m_texPort.InputPreviewTexture( ContainerGraph ) );
  164. }
  165. else if( SoftValidReference && m_referenceSampler.TextureProperty != null )
  166. {
  167. if( m_referenceSampler.TextureProperty.Value != null )
  168. {
  169. usingTexture = true;
  170. SetPreviewTexture( m_referenceSampler.TextureProperty.Value );
  171. }
  172. else
  173. {
  174. usingTexture = true;
  175. SetPreviewTexture( m_referenceSampler.PreviewTexture );
  176. }
  177. }
  178. else if( TextureProperty != null )
  179. {
  180. if( TextureProperty.Value != null )
  181. {
  182. usingTexture = true;
  183. SetPreviewTexture( TextureProperty.Value );
  184. }
  185. }
  186. if( m_defaultId == -1 )
  187. m_defaultId = Shader.PropertyToID( "_Default" );
  188. if( usingTexture )
  189. {
  190. PreviewMaterial.SetInt( m_defaultId, 0 );
  191. m_previewMaterialPassId = 1;
  192. }
  193. else
  194. {
  195. PreviewMaterial.SetInt( m_defaultId, ( (int)m_defaultTextureValue ) + 1 );
  196. m_previewMaterialPassId = 0;
  197. }
  198. }
  199. protected override void OnUniqueIDAssigned()
  200. {
  201. base.OnUniqueIDAssigned();
  202. if( m_referenceType == TexReferenceType.Object )
  203. {
  204. UIUtils.RegisterSamplerNode( this );
  205. UIUtils.RegisterPropertyNode( this );
  206. }
  207. m_textureProperty = this;
  208. if( UniqueId > -1 )
  209. ContainerGraph.SamplerNodes.OnReorderEventComplete += OnReorderEventComplete;
  210. }
  211. private void OnReorderEventComplete()
  212. {
  213. if( m_referenceType == TexReferenceType.Instance && m_referenceSampler != null )
  214. {
  215. m_referenceArrayId = ContainerGraph.SamplerNodes.GetNodeRegisterIdx( m_referenceSampler.UniqueId );
  216. }
  217. }
  218. public void ConfigSampler()
  219. {
  220. switch( m_currentType )
  221. {
  222. case TextureType.Texture1D:
  223. m_samplerType = "tex1D";
  224. break;
  225. case TextureType.ProceduralTexture:
  226. case TextureType.Texture2D:
  227. m_samplerType = "tex2D";
  228. break;
  229. case TextureType.Texture2DArray:
  230. m_samplerType = "tex2DArray";
  231. break;
  232. case TextureType.Texture3D:
  233. m_samplerType = "tex3D";
  234. break;
  235. case TextureType.Cube:
  236. m_samplerType = "texCUBE";
  237. break;
  238. }
  239. }
  240. public override void DrawSubProperties()
  241. {
  242. ShowDefaults();
  243. DrawSamplerOptions();
  244. EditorGUI.BeginChangeCheck();
  245. Type currType = ( m_autocastMode == AutoCastType.Auto ) ? typeof( Texture ) : m_textureType;
  246. m_defaultValue = EditorGUILayoutObjectField( Constants.DefaultValueLabel, m_defaultValue, currType, false ) as Texture;
  247. if( EditorGUI.EndChangeCheck() )
  248. {
  249. CheckTextureImporter( true );
  250. SetAdditonalTitleText( string.Format( Constants.PropertyValueLabel, GetPropertyValStr() ) );
  251. ConfigureInputPorts();
  252. ConfigureOutputPorts();
  253. //ResizeNodeToPreview();
  254. }
  255. }
  256. public override void DrawMaterialProperties()
  257. {
  258. ShowDefaults();
  259. DrawSamplerOptions();
  260. EditorGUI.BeginChangeCheck();
  261. Type currType = ( m_autocastMode == AutoCastType.Auto ) ? typeof( Texture ) : m_textureType;
  262. m_materialValue = EditorGUILayoutObjectField( Constants.MaterialValueLabel, m_materialValue, currType, false ) as Texture;
  263. if( EditorGUI.EndChangeCheck() )
  264. {
  265. CheckTextureImporter( true );
  266. SetAdditonalTitleText( string.Format( Constants.PropertyValueLabel, GetPropertyValStr() ) );
  267. ConfigureInputPorts();
  268. ConfigureOutputPorts();
  269. }
  270. }
  271. new void ShowDefaults()
  272. {
  273. m_defaultTextureValue = (TexturePropertyValues)EditorGUILayoutEnumPopup( DefaultTextureStr, m_defaultTextureValue );
  274. //AutoCastType newAutoCast = (AutoCastType)EditorGUILayoutIntPopup( AutoCastModeStr, (int)m_autocastMode, AvailableAutoCastStr, AvailableAutoCast );
  275. AutoCastType newAutoCast = (AutoCastType)EditorGUILayoutEnumPopup( AutoCastModeStr, m_autocastMode );
  276. if( newAutoCast != m_autocastMode )
  277. {
  278. m_autocastMode = newAutoCast;
  279. if( m_autocastMode != AutoCastType.Auto )
  280. {
  281. ConfigTextureData( m_currentType );
  282. ConfigureInputPorts();
  283. ConfigureOutputPorts();
  284. }
  285. }
  286. }
  287. public override void AdditionalCheck()
  288. {
  289. m_autoUnpackNormals = m_isNormalMap;
  290. ConfigureInputPorts();
  291. ConfigureOutputPorts();
  292. }
  293. public override void OnConnectedOutputNodeChanges( int portId, int otherNodeId, int otherPortId, string name, WirePortDataType type )
  294. {
  295. base.OnConnectedOutputNodeChanges( portId, otherNodeId, otherPortId, name, type );
  296. if( portId == m_texPort.PortId )
  297. {
  298. m_texPort.MatchPortToConnection();
  299. m_textureProperty = m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode;
  300. if( m_textureProperty != null )
  301. {
  302. m_currentType = m_textureProperty.CurrentType;
  303. ConfigureInputPorts();
  304. ConfigureOutputPorts();
  305. }
  306. else
  307. {
  308. m_currentType = Constants.WireToTexture[ type ];
  309. ConfigureInputPorts();
  310. ConfigureOutputPorts();
  311. }
  312. }
  313. }
  314. public override void OnInputPortConnected( int portId, int otherNodeId, int otherPortId, bool activateNode = true )
  315. {
  316. base.OnInputPortConnected( portId, otherNodeId, otherPortId, activateNode );
  317. if( portId == m_texPort.PortId )
  318. {
  319. m_texPort.MatchPortToConnection();
  320. m_textureProperty = m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode;
  321. if( m_textureProperty == null )
  322. {
  323. if( Constants.WireToTexture.TryGetValue( m_texPort.ConnectionType() , out m_currentType ) )
  324. {
  325. //m_currentType = Constants.WireToTexture[ m_texPort.ConnectionType() ];
  326. m_textureProperty = this;
  327. // This cast fails only from within shader functions if connected to a Sampler Input
  328. // and in this case property is set by what is connected to that input
  329. UIUtils.UnregisterPropertyNode( this );
  330. UIUtils.UnregisterTexturePropertyNode( this );
  331. }
  332. }
  333. else
  334. {
  335. m_currentType = m_textureProperty.CurrentType;
  336. UIUtils.UnregisterPropertyNode( this );
  337. UIUtils.UnregisterTexturePropertyNode( this );
  338. }
  339. ConfigureInputPorts();
  340. ConfigureOutputPorts();
  341. //ResizeNodeToPreview();
  342. }
  343. UpdateTitle();
  344. }
  345. public override void OnInputPortDisconnected( int portId )
  346. {
  347. base.OnInputPortDisconnected( portId );
  348. if( portId == m_texPort.PortId )
  349. {
  350. m_textureProperty = this;
  351. if( m_referenceType == TexReferenceType.Object )
  352. {
  353. UIUtils.RegisterPropertyNode( this );
  354. UIUtils.RegisterTexturePropertyNode( this );
  355. }
  356. ConfigureOutputPorts();
  357. //ResizeNodeToPreview();
  358. }
  359. UpdateTitle();
  360. }
  361. private void ForceInputPortsChange()
  362. {
  363. m_texPort.ChangeType( Constants.TextureToWire[ m_currentType ], false );
  364. m_normalPort.ChangeType( WirePortDataType.FLOAT, false );
  365. switch( m_currentType )
  366. {
  367. case TextureType.Texture1D:
  368. m_uvPort.ChangeType( WirePortDataType.FLOAT, false );
  369. m_ddxPort.ChangeType( WirePortDataType.FLOAT, false );
  370. m_ddyPort.ChangeType( WirePortDataType.FLOAT, false );
  371. break;
  372. case TextureType.ProceduralTexture:
  373. case TextureType.Texture2D:
  374. case TextureType.Texture2DArray:
  375. m_uvPort.ChangeType( WirePortDataType.FLOAT2, false );
  376. m_ddxPort.ChangeType( WirePortDataType.FLOAT2, false );
  377. m_ddyPort.ChangeType( WirePortDataType.FLOAT2, false );
  378. break;
  379. case TextureType.Texture3D:
  380. case TextureType.Cube:
  381. m_uvPort.ChangeType( WirePortDataType.FLOAT3, false );
  382. m_ddxPort.ChangeType( WirePortDataType.FLOAT3, false );
  383. m_ddyPort.ChangeType( WirePortDataType.FLOAT3, false );
  384. break;
  385. }
  386. }
  387. public override void ConfigureInputPorts()
  388. {
  389. m_normalPort.Visible = AutoUnpackNormals;
  390. switch( m_mipMode )
  391. {
  392. case MipType.Auto:
  393. m_lodPort.Visible = false;
  394. m_ddxPort.Visible = false;
  395. m_ddyPort.Visible = false;
  396. break;
  397. case MipType.MipLevel:
  398. m_lodPort.Name = "Level";
  399. m_lodPort.Visible = true;
  400. m_ddxPort.Visible = false;
  401. m_ddyPort.Visible = false;
  402. break;
  403. case MipType.MipBias:
  404. m_lodPort.Name = "Bias";
  405. m_lodPort.Visible = true;
  406. m_ddxPort.Visible = false;
  407. m_ddyPort.Visible = false;
  408. break;
  409. case MipType.Derivative:
  410. m_lodPort.Visible = false;
  411. m_ddxPort.Visible = true;
  412. m_ddyPort.Visible = true;
  413. break;
  414. }
  415. switch( m_currentType )
  416. {
  417. case TextureType.Texture1D:
  418. m_uvPort.ChangeType( WirePortDataType.FLOAT, false );
  419. m_ddxPort.ChangeType( WirePortDataType.FLOAT, false );
  420. m_ddyPort.ChangeType( WirePortDataType.FLOAT, false );
  421. break;
  422. case TextureType.ProceduralTexture:
  423. case TextureType.Texture2D:
  424. case TextureType.Texture2DArray:
  425. m_uvPort.ChangeType( WirePortDataType.FLOAT2, false );
  426. m_ddxPort.ChangeType( WirePortDataType.FLOAT2, false );
  427. m_ddyPort.ChangeType( WirePortDataType.FLOAT2, false );
  428. break;
  429. case TextureType.Texture3D:
  430. case TextureType.Cube:
  431. m_uvPort.ChangeType( WirePortDataType.FLOAT3, false );
  432. m_ddxPort.ChangeType( WirePortDataType.FLOAT3, false );
  433. m_ddyPort.ChangeType( WirePortDataType.FLOAT3, false );
  434. break;
  435. }
  436. if( m_currentType == TextureType.Texture2DArray )
  437. m_indexPort.Visible = true;
  438. else
  439. m_indexPort.Visible = false;
  440. m_sizeIsDirty = true;
  441. }
  442. public override void ConfigureOutputPorts()
  443. {
  444. m_outputPorts[ m_colorPort.PortId + 4 ].Visible = !AutoUnpackNormals;
  445. m_outputPorts[ m_colorPort.PortId + 5 ].Visible = !AutoUnpackNormals;
  446. if( !AutoUnpackNormals )
  447. {
  448. m_colorPort.ChangeProperties( "RGBA", WirePortDataType.COLOR, false );
  449. m_outputPorts[ m_colorPort.PortId + 1 ].ChangeProperties( "R", WirePortDataType.FLOAT, false );
  450. m_outputPorts[ m_colorPort.PortId + 2 ].ChangeProperties( "G", WirePortDataType.FLOAT, false );
  451. m_outputPorts[ m_colorPort.PortId + 3 ].ChangeProperties( "B", WirePortDataType.FLOAT, false );
  452. m_outputPorts[ m_colorPort.PortId + 4 ].ChangeProperties( "A", WirePortDataType.FLOAT, false );
  453. }
  454. else
  455. {
  456. m_colorPort.ChangeProperties( "XYZ", WirePortDataType.FLOAT3, false );
  457. m_outputPorts[ m_colorPort.PortId + 1 ].ChangeProperties( "X", WirePortDataType.FLOAT, false );
  458. m_outputPorts[ m_colorPort.PortId + 2 ].ChangeProperties( "Y", WirePortDataType.FLOAT, false );
  459. m_outputPorts[ m_colorPort.PortId + 3 ].ChangeProperties( "Z", WirePortDataType.FLOAT, false );
  460. }
  461. m_sizeIsDirty = true;
  462. }
  463. void UpdateTitle()
  464. {
  465. if( m_referenceType == TexReferenceType.Object )
  466. {
  467. SetTitleText( m_propertyInspectorName );
  468. SetAdditonalTitleText( string.Format( Constants.PropertyValueLabel, GetPropertyValStr() ) );
  469. }
  470. m_sizeIsDirty = true;
  471. }
  472. public override void OnObjectDropped( UnityEngine.Object obj )
  473. {
  474. base.OnObjectDropped( obj );
  475. ConfigFromObject( obj );
  476. }
  477. public override void SetupFromCastObject( UnityEngine.Object obj )
  478. {
  479. base.SetupFromCastObject( obj );
  480. ConfigFromObject( obj );
  481. }
  482. void UpdateHeaderColor()
  483. {
  484. m_headerColorModifier = ( m_referenceType == TexReferenceType.Object ) ? Color.white : ReferenceHeaderColor;
  485. }
  486. void ShowSamplerUI()
  487. {
  488. EditorGUI.BeginDisabledGroup( m_samplerPort.IsConnected );
  489. string[] contents = UIUtils.TexturePropertyNodeArr();
  490. string[] arr = new string[ contents.Length + 1 ];
  491. arr[ 0 ] = "<None>";
  492. for( int i = 1; i < contents.Length + 1; i++ )
  493. {
  494. arr[ i ] = contents[ i - 1 ];
  495. }
  496. m_useSamplerArrayIdx = EditorGUILayoutPopup( "Reference Sampler", m_useSamplerArrayIdx, arr );
  497. EditorGUI.EndDisabledGroup();
  498. }
  499. public void DrawSamplerOptions()
  500. {
  501. if( !m_indexPort.IsConnected )
  502. {
  503. m_indexPort.FloatInternalData = EditorGUILayoutFloatField( "Index", m_indexPort.FloatInternalData );
  504. }
  505. m_textureCoordSet = EditorGUILayoutIntPopup( Constants.AvailableUVSetsLabel, m_textureCoordSet, Constants.AvailableUVSetsStr, Constants.AvailableUVSets );
  506. MipType newMipMode = (MipType)EditorGUILayoutEnumPopup( MipModeStr, m_mipMode );
  507. if( newMipMode != m_mipMode )
  508. {
  509. m_mipMode = newMipMode;
  510. ConfigureInputPorts();
  511. ConfigureOutputPorts();
  512. //ResizeNodeToPreview();
  513. }
  514. if( !m_lodPort.IsConnected && m_lodPort.Visible )
  515. {
  516. m_lodPort.FloatInternalData = EditorGUILayoutFloatField( newMipMode == MipType.MipBias ? "Mip Bias" : "Mip Level", m_lodPort.FloatInternalData );
  517. }
  518. if( m_currentType == TextureType.Texture2DArray && ( newMipMode == MipType.Derivative || newMipMode == MipType.MipBias ) && !UIUtils.CurrentWindow.OutsideGraph.IsSRP )
  519. {
  520. EditorGUILayout.HelpBox( "Derivative and Bias mip modes for Texture Arrays only works on some platforms (D3D11 XBOXONE GLES3 GLCORE)", MessageType.Warning );
  521. }
  522. EditorGUI.BeginChangeCheck();
  523. m_autoUnpackNormals = EditorGUILayoutToggle( "Unpack Normal Map", m_autoUnpackNormals );
  524. if( m_autoUnpackNormals && !m_normalPort.IsConnected )
  525. {
  526. m_normalPort.FloatInternalData = EditorGUILayoutFloatField( NormalScaleStr, m_normalPort.FloatInternalData );
  527. }
  528. if( EditorGUI.EndChangeCheck() )
  529. {
  530. ConfigureInputPorts();
  531. ConfigureOutputPorts();
  532. //ResizeNodeToPreview();
  533. }
  534. ShowSamplerUI();
  535. if( m_showErrorMessage )
  536. {
  537. EditorGUILayout.HelpBox( m_errorMessageTooltip, MessageType.Warning );
  538. }
  539. }
  540. public override void DrawMainPropertyBlock()
  541. {
  542. EditorGUI.BeginChangeCheck();
  543. m_referenceType = (TexReferenceType)EditorGUILayoutPopup( Constants.ReferenceTypeStr, (int)m_referenceType, Constants.ReferenceArrayLabels );
  544. if( EditorGUI.EndChangeCheck() )
  545. {
  546. if( m_referenceType == TexReferenceType.Object )
  547. {
  548. UIUtils.RegisterSamplerNode( this );
  549. UIUtils.RegisterPropertyNode( this );
  550. if( !m_texPort.IsConnected )
  551. UIUtils.RegisterTexturePropertyNode( this );
  552. SetTitleText( m_propertyInspectorName );
  553. SetAdditonalTitleText( string.Format( Constants.PropertyValueLabel, GetPropertyValStr() ) );
  554. m_referenceArrayId = -1;
  555. m_referenceNodeId = -1;
  556. m_referenceSampler = null;
  557. m_textureProperty = m_texPort.IsConnected ? m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode : this;
  558. }
  559. else
  560. {
  561. UIUtils.UnregisterSamplerNode( this );
  562. UIUtils.UnregisterPropertyNode( this );
  563. if( !m_texPort.IsConnected )
  564. UIUtils.UnregisterTexturePropertyNode( this );
  565. }
  566. UpdateHeaderColor();
  567. }
  568. if( m_referenceType == TexReferenceType.Object )
  569. {
  570. EditorGUI.BeginChangeCheck();
  571. if( m_texPort.IsConnected )
  572. {
  573. m_drawAttributes = false;
  574. DrawSamplerOptions();
  575. }
  576. else
  577. {
  578. m_drawAttributes = true;
  579. base.DrawMainPropertyBlock();
  580. }
  581. if( EditorGUI.EndChangeCheck() )
  582. {
  583. OnPropertyNameChanged();
  584. }
  585. }
  586. else
  587. {
  588. m_drawAttributes = true;
  589. string[] arr = UIUtils.SamplerNodeArr();
  590. bool guiEnabledBuffer = GUI.enabled;
  591. if( arr != null && arr.Length > 0 )
  592. {
  593. GUI.enabled = true;
  594. }
  595. else
  596. {
  597. m_referenceArrayId = -1;
  598. GUI.enabled = false;
  599. }
  600. EditorGUI.BeginChangeCheck();
  601. m_referenceArrayId = EditorGUILayoutPopup( Constants.AvailableReferenceStr, m_referenceArrayId, arr );
  602. if( EditorGUI.EndChangeCheck() )
  603. {
  604. m_referenceSampler = ContainerGraph.SamplerNodes.GetNode( m_referenceArrayId );
  605. if( m_referenceSampler != null )
  606. {
  607. m_referenceNodeId = m_referenceSampler.UniqueId;
  608. }
  609. else
  610. {
  611. m_referenceArrayId = -1;
  612. m_referenceNodeId = -1;
  613. }
  614. }
  615. GUI.enabled = guiEnabledBuffer;
  616. DrawSamplerOptions();
  617. }
  618. }
  619. public override void OnPropertyNameChanged()
  620. {
  621. base.OnPropertyNameChanged();
  622. UIUtils.UpdateSamplerDataNode( UniqueId, PropertyName );
  623. UIUtils.UpdateTexturePropertyDataNode( UniqueId, PropertyName );
  624. }
  625. public override void DrawGUIControls( DrawInfo drawInfo )
  626. {
  627. base.DrawGUIControls( drawInfo );
  628. if( m_state != ReferenceState.Self && drawInfo.CurrentEventType == EventType.MouseDown && m_previewRect.Contains( drawInfo.MousePosition ) && drawInfo.LeftMouseButtonPressed )
  629. {
  630. UIUtils.FocusOnNode( m_previewTextProp, 1, true );
  631. Event.current.Use();
  632. }
  633. }
  634. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  635. {
  636. base.OnNodeLogicUpdate( drawInfo );
  637. CheckReference();
  638. if( SoftValidReference )
  639. {
  640. m_state = ReferenceState.Instance;
  641. m_previewTextProp = m_referenceSampler.TextureProperty;
  642. }
  643. else if( m_texPort.IsConnected )
  644. {
  645. m_state = ReferenceState.Connected;
  646. m_previewTextProp = TextureProperty;
  647. }
  648. else
  649. {
  650. m_state = ReferenceState.Self;
  651. }
  652. if( m_previewTextProp == null )
  653. m_previewTextProp = this;
  654. }
  655. public override void OnNodeLayout( DrawInfo drawInfo )
  656. {
  657. base.OnNodeLayout( drawInfo );
  658. if( m_drawPreview )
  659. {
  660. m_iconPos = m_globalPosition;
  661. m_iconPos.width = InstanceIconWidth * drawInfo.InvertedZoom;
  662. m_iconPos.height = InstanceIconHeight * drawInfo.InvertedZoom;
  663. m_iconPos.y += 10 * drawInfo.InvertedZoom;
  664. m_iconPos.x += m_globalPosition.width - m_iconPos.width - 5 * drawInfo.InvertedZoom;
  665. }
  666. }
  667. public override void OnNodeRepaint( DrawInfo drawInfo )
  668. {
  669. base.OnNodeRepaint( drawInfo );
  670. if( !m_isVisible )
  671. return;
  672. if( drawInfo.CurrentEventType != EventType.Repaint )
  673. return;
  674. switch( m_state )
  675. {
  676. default:
  677. case ReferenceState.Self:
  678. {
  679. m_drawPreview = false;
  680. //SetTitleText( PropertyInspectorName /*m_propertyInspectorName*/ );
  681. //small optimization, string format or concat on every frame generates garbage
  682. //string tempVal = GetPropertyValStr();
  683. //if ( !m_previousAdditionalText.Equals( tempVal ) )
  684. //{
  685. // m_previousAdditionalText = tempVal;
  686. // m_additionalContent.text = string.Concat( "Value( ", tempVal, " )" );
  687. //}
  688. m_drawPicker = true;
  689. }
  690. break;
  691. case ReferenceState.Connected:
  692. {
  693. m_drawPreview = true;
  694. m_drawPicker = false;
  695. SetTitleText( m_previewTextProp.PropertyInspectorName + " (Input)" );
  696. m_previousAdditionalText = m_previewTextProp.AdditonalTitleContent.text;
  697. SetAdditonalTitleText( m_previousAdditionalText );
  698. // Draw chain lock
  699. GUI.Label( m_iconPos, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerTextureIcon ) );
  700. // Draw frame around preview
  701. GUI.Label( m_previewRect, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerFrame ) );
  702. }
  703. break;
  704. case ReferenceState.Instance:
  705. {
  706. m_drawPreview = true;
  707. m_drawPicker = false;
  708. //SetTitleText( m_previewTextProp.PropertyInspectorName + Constants.InstancePostfixStr );
  709. //m_previousAdditionalText = m_previewTextProp.AdditonalTitleContent.text;
  710. //SetAdditonalTitleText( m_previousAdditionalText );
  711. SetTitleTextOnCallback( m_previewTextProp.PropertyInspectorName, ( instance, newTitle ) => instance.TitleContent.text = newTitle + Constants.InstancePostfixStr );
  712. if( m_previewTextProp.AdditonalTitleContent.text != m_additionalContent.text )
  713. {
  714. PreviewIsDirty = true;
  715. }
  716. SetAdditonalTitleText( m_previewTextProp.AdditonalTitleContent.text );
  717. // Draw chain lock
  718. GUI.Label( m_iconPos, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerTextureIcon ) );
  719. // Draw frame around preview
  720. GUI.Label( m_previewRect, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerFrame ) );
  721. }
  722. break;
  723. }
  724. }
  725. void CheckReference()
  726. {
  727. if( m_referenceType != TexReferenceType.Instance )
  728. {
  729. return;
  730. }
  731. if( m_referenceArrayId > -1 )
  732. {
  733. ParentNode newNode = ContainerGraph.SamplerNodes.GetNode( m_referenceArrayId );
  734. if( newNode == null || newNode.UniqueId != m_referenceNodeId )
  735. {
  736. m_referenceSampler = null;
  737. int count = ContainerGraph.SamplerNodes.NodesList.Count;
  738. for( int i = 0; i < count; i++ )
  739. {
  740. ParentNode node = ContainerGraph.SamplerNodes.GetNode( i );
  741. if( node.UniqueId == m_referenceNodeId )
  742. {
  743. m_referenceSampler = node as SamplerNode;
  744. m_referenceArrayId = i;
  745. break;
  746. }
  747. }
  748. }
  749. else
  750. {
  751. m_texPort.DataType = m_referenceSampler.TexPort.DataType;
  752. // Set current type
  753. TextureType newTextureType = m_referenceSampler.CurrentType;
  754. // Set References Options
  755. AutoCastType newAutoCast = m_referenceSampler.AutocastMode;
  756. if( newAutoCast != m_autocastMode || newTextureType != m_currentType )
  757. {
  758. m_currentType = newTextureType;
  759. m_autocastMode = newAutoCast;
  760. //if( m_autocastMode != AutoCastType.Auto )
  761. {
  762. ConfigTextureData( m_currentType );
  763. ConfigureInputPorts();
  764. ConfigureOutputPorts();
  765. //ResizeNodeToPreview();
  766. }
  767. }
  768. }
  769. }
  770. if( m_referenceSampler == null && m_referenceNodeId > -1 )
  771. {
  772. m_referenceNodeId = -1;
  773. m_referenceArrayId = -1;
  774. }
  775. }
  776. public void SetTitleTextDelay( string newText )
  777. {
  778. if( !newText.Equals( m_content.text ) )
  779. {
  780. m_content.text = newText;
  781. BeginDelayedDirtyProperty();
  782. }
  783. }
  784. public void SetAdditonalTitleTextDelay( string newText )
  785. {
  786. if( !newText.Equals( m_additionalContent.text ) )
  787. {
  788. m_additionalContent.text = newText;
  789. BeginDelayedDirtyProperty();
  790. }
  791. }
  792. private void DrawTexturePropertyPreview( DrawInfo drawInfo, bool instance )
  793. {
  794. if( drawInfo.CurrentEventType != EventType.Repaint )
  795. return;
  796. Rect newPos = m_previewRect;
  797. TexturePropertyNode texProp = null;
  798. if( instance )
  799. texProp = m_referenceSampler.TextureProperty;
  800. else
  801. texProp = TextureProperty;
  802. if( texProp == null )
  803. texProp = this;
  804. float previewSizeX = PreviewSizeX;
  805. float previewSizeY = PreviewSizeY;
  806. newPos.width = previewSizeX * drawInfo.InvertedZoom;
  807. newPos.height = previewSizeY * drawInfo.InvertedZoom;
  808. SetTitleText( texProp.PropertyInspectorName + ( instance ? Constants.InstancePostfixStr : " (Input)" ) );
  809. SetAdditonalTitleText( texProp.AdditonalTitleContent.text );
  810. if( m_referenceStyle == null )
  811. {
  812. m_referenceStyle = UIUtils.GetCustomStyle( CustomStyle.SamplerTextureRef );
  813. }
  814. if( m_referenceIconStyle == null || m_referenceIconStyle.normal == null )
  815. {
  816. m_referenceIconStyle = UIUtils.GetCustomStyle( CustomStyle.SamplerTextureIcon );
  817. if( m_referenceIconStyle != null && m_referenceIconStyle.normal != null && m_referenceIconStyle.normal.background != null )
  818. {
  819. InstanceIconWidth = m_referenceIconStyle.normal.background.width;
  820. InstanceIconHeight = m_referenceIconStyle.normal.background.height;
  821. }
  822. }
  823. Rect iconPos = m_globalPosition;
  824. iconPos.width = InstanceIconWidth * drawInfo.InvertedZoom;
  825. iconPos.height = InstanceIconHeight * drawInfo.InvertedZoom;
  826. iconPos.y += 10 * drawInfo.InvertedZoom;
  827. iconPos.x += m_globalPosition.width - iconPos.width - 5 * drawInfo.InvertedZoom;
  828. //if ( GUI.Button( newPos, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerTextureRef )/* m_referenceStyle */) ||
  829. // GUI.Button( iconPos, string.Empty, m_referenceIconStyle )
  830. // )
  831. //{
  832. // UIUtils.FocusOnNode( texProp, 1, true );
  833. //}
  834. if( texProp.Value != null )
  835. {
  836. DrawPreview( drawInfo, m_previewRect );
  837. GUI.Label( newPos, string.Empty, UIUtils.GetCustomStyle( CustomStyle.SamplerFrame ) );
  838. //UIUtils.GetCustomStyle( CustomStyle.SamplerButton ).fontSize = ( int )Mathf.Round( 9 * drawInfo.InvertedZoom );
  839. }
  840. }
  841. public override string GenerateSamplerPropertyName( int outputId, ref MasterNodeDataCollector dataCollector )
  842. {
  843. string generatedSamplerState = PropertyName;
  844. if( m_forceSamplingMacrosGen )
  845. {
  846. generatedSamplerState = GeneratorUtils.GenerateSamplerState( ref dataCollector, UniqueId, PropertyName, m_variableMode );
  847. }
  848. if( outputId > 0 )
  849. return generatedSamplerState;
  850. else
  851. return PropertyName;
  852. }
  853. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  854. {
  855. if( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  856. {
  857. UIUtils.ShowMessage( UniqueId, m_nodeAttribs.Name + " cannot be used on Master Node Tessellation port" );
  858. return "(-1)";
  859. }
  860. OnPropertyNameChanged();
  861. ConfigSampler();
  862. string portProperty = string.Empty;
  863. if( m_texPort.IsConnected )
  864. portProperty = m_texPort.GenerateShaderForOutput( ref dataCollector, true );
  865. if( SoftValidReference )
  866. {
  867. OrderIndex = m_referenceSampler.RawOrderIndex;
  868. if( m_referenceSampler.TexPort.IsConnected )
  869. {
  870. portProperty = m_referenceSampler.TexPort.GeneratePortInstructions( ref dataCollector );
  871. }
  872. else
  873. {
  874. m_referenceSampler.RegisterProperty( ref dataCollector );
  875. }
  876. }
  877. if( IsObject && ( !m_texPort.IsConnected || portProperty == "0.0" ) )
  878. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalVar );
  879. string valueName = SetFetchedData( ref dataCollector, ignoreLocalVar, outputId, portProperty );
  880. if( TextureProperty is VirtualTextureObject )
  881. {
  882. return valueName;
  883. }
  884. else
  885. {
  886. return GetOutputColorItem( 0, outputId, valueName );
  887. }
  888. }
  889. public string SampleVirtualTexture( VirtualTextureObject node, string coord )
  890. {
  891. string sampler = string.Empty;
  892. switch( node.Channel )
  893. {
  894. default:
  895. case VirtualChannel.Albedo:
  896. case VirtualChannel.Base:
  897. sampler = "VTSampleAlbedo( " + coord + " )";
  898. break;
  899. case VirtualChannel.Normal:
  900. case VirtualChannel.Height:
  901. case VirtualChannel.Occlusion:
  902. case VirtualChannel.Displacement:
  903. sampler = "VTSampleNormal( " + coord + " )";
  904. break;
  905. case VirtualChannel.Specular:
  906. case VirtualChannel.SpecMet:
  907. case VirtualChannel.Material:
  908. sampler = "VTSampleSpecular( " + coord + " )";
  909. break;
  910. }
  911. return sampler;
  912. }
  913. public string SampleTexture( ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar, string portProperty, MipType currMipMode, string propertyName , VariableMode varMode )
  914. {
  915. string samplerValue = string.Empty;
  916. string uvCoords = GetUVCoords( ref dataCollector, ignoreLocalVar, portProperty );
  917. bool isVertex = ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation );
  918. bool useMacros = false;
  919. ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph;
  920. if( outsideGraph.SamplingMacros || m_currentType == TextureType.Texture2DArray )
  921. {
  922. useMacros = Constants.TexSampleSRPMacros.ContainsKey( m_currentType );
  923. }
  924. if( useMacros || m_currentType == TextureType.Texture2DArray )
  925. {
  926. string suffix = string.Empty;
  927. switch( currMipMode )
  928. {
  929. default:
  930. case MipType.Auto: break;
  931. case MipType.MipLevel: suffix = "_LOD"; break;
  932. case MipType.MipBias: suffix = "_BIAS"; break;
  933. case MipType.Derivative: suffix = "_GRAD"; break;
  934. }
  935. if( isVertex )
  936. suffix = "_LOD";
  937. string samplerToUse = string.Empty;
  938. if( !m_samplerPort.IsConnected && m_useSamplerArrayIdx > 0 )
  939. {
  940. TexturePropertyNode samplerNode = UIUtils.GetTexturePropertyNode( m_useSamplerArrayIdx - 1 );
  941. if( samplerNode != null )
  942. {
  943. if( samplerNode.IsConnected )
  944. {
  945. string property = samplerNode.CurrentPropertyReference;
  946. samplerToUse = GeneratorUtils.GenerateSamplerState( ref dataCollector, UniqueId, property, varMode );
  947. }
  948. else
  949. {
  950. UIUtils.ShowMessage( UniqueId, string.Format( "{0} attempting to use sampler from unconnected {1} node. Reference Sampler nodes must be in use for their samplers to be created.", m_propertyName, samplerNode.PropertyName ), MessageSeverity.Warning );
  951. dataCollector.AddToUniforms( UniqueId, string.Format( Constants.SamplerDeclarationSRPMacros[ m_currentType ], propertyName ) );
  952. samplerToUse = propertyName;
  953. }
  954. }
  955. else
  956. {
  957. UIUtils.ShowMessage( UniqueId, m_propertyName + " attempting to use sampler from invalid node.", MessageSeverity.Warning );
  958. dataCollector.AddToUniforms( UniqueId, string.Format( Constants.SamplerDeclarationSRPMacros[ m_currentType ], propertyName ) );
  959. samplerToUse = propertyName;
  960. }
  961. }
  962. else
  963. {
  964. string samplerState = m_samplerPort.GeneratePortInstructions( ref dataCollector );
  965. if( m_samplerPort.IsConnected && !string.IsNullOrEmpty( samplerState ) && !samplerState.Equals( "0" ) )
  966. {
  967. samplerToUse = samplerState;
  968. }
  969. else
  970. {
  971. samplerToUse = GeneratorUtils.GenerateSamplerState( ref dataCollector, UniqueId, propertyName , varMode );
  972. }
  973. }
  974. if( outsideGraph.IsSRP )
  975. {
  976. if( m_currentType == TextureType.Texture3D && ( currMipMode == MipType.MipBias || currMipMode == MipType.Derivative ) )
  977. GeneratorUtils.AddCustom3DSRPMacros( ref dataCollector );
  978. samplerValue = string.Format( Constants.TexSampleSRPMacros[ m_currentType ], suffix, propertyName, samplerToUse, uvCoords );
  979. }
  980. else
  981. {
  982. GeneratorUtils.AddCustomStandardSamplingMacros( ref dataCollector, m_currentType, currMipMode );
  983. samplerValue = string.Format( Constants.TexSampleSamplerStandardMacros[ m_currentType ], suffix, propertyName, samplerToUse, uvCoords );
  984. }
  985. }
  986. else
  987. {
  988. string mipType = "";
  989. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  990. {
  991. mipType = "lod";
  992. }
  993. switch( currMipMode )
  994. {
  995. case MipType.Auto:
  996. break;
  997. case MipType.MipLevel:
  998. mipType = "lod";
  999. break;
  1000. case MipType.MipBias:
  1001. mipType = "bias";
  1002. break;
  1003. case MipType.Derivative:
  1004. break;
  1005. }
  1006. samplerValue = m_samplerType + mipType + "( " + propertyName + ", " + uvCoords + " )";
  1007. }
  1008. return samplerValue;
  1009. }
  1010. public string SetFetchedData( ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar, int outputId, string portProperty = null )
  1011. {
  1012. m_precisionString = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_colorPort.DataType );
  1013. string propertyName = CurrentPropertyReference;
  1014. VariableMode varMode = VarModeReference;
  1015. if( !string.IsNullOrEmpty( portProperty ) && portProperty != "0.0" )
  1016. {
  1017. propertyName = portProperty;
  1018. }
  1019. MipType currMipMode = m_mipMode;
  1020. if( ignoreLocalVar )
  1021. {
  1022. if( TextureProperty is VirtualTextureObject )
  1023. Debug.Log( "TODO" );
  1024. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  1025. {
  1026. currMipMode = MipType.MipLevel;
  1027. }
  1028. string samplerValue = SampleTexture( ref dataCollector, ignoreLocalVar, portProperty, currMipMode, propertyName , varMode );
  1029. AddNormalMapTag( ref dataCollector, ref samplerValue );
  1030. return samplerValue;
  1031. }
  1032. VirtualTextureObject vtex = ( TextureProperty as VirtualTextureObject );
  1033. if( vtex != null )
  1034. {
  1035. string atPathname = AssetDatabase.GUIDToAssetPath( Constants.ATSharedLibGUID );
  1036. if( string.IsNullOrEmpty( atPathname ) )
  1037. {
  1038. UIUtils.ShowMessage( UniqueId, "Could not find Amplify Texture on your project folder. Please install it and re-compile the shader.", MessageSeverity.Error );
  1039. }
  1040. else
  1041. {
  1042. //Need to see if the asset really exists because AssetDatabase.GUIDToAssetPath() can return a valid path if
  1043. // the asset was previously imported and deleted after that
  1044. UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath<UnityEngine.Object>( atPathname );
  1045. if( obj == null )
  1046. {
  1047. UIUtils.ShowMessage( UniqueId, "Could not find Amplify Texture on your project folder. Please install it and re-compile the shader.", MessageSeverity.Error );
  1048. }
  1049. else
  1050. {
  1051. if( m_colorPort.IsLocalValue( dataCollector.PortCategory ) )
  1052. return m_colorPort.LocalValue( dataCollector.PortCategory );
  1053. //string remapPortR = ".r";
  1054. //string remapPortG = ".g";
  1055. //string remapPortB = ".b";
  1056. //string remapPortA = ".a";
  1057. //if ( vtex.Channel == VirtualChannel.Occlusion )
  1058. //{
  1059. // remapPortR = ".r"; remapPortG = ".r"; remapPortB = ".r"; remapPortA = ".r";
  1060. //}
  1061. //else if ( vtex.Channel == VirtualChannel.SpecMet && ( ContainerGraph.CurrentStandardSurface != null && ContainerGraph.CurrentStandardSurface.CurrentLightingModel == StandardShaderLightModel.Standard ) )
  1062. //{
  1063. // remapPortR = ".r"; remapPortG = ".r"; remapPortB = ".r";
  1064. //}
  1065. //else if ( vtex.Channel == VirtualChannel.Height || vtex.Channel == VirtualChannel.Displacement )
  1066. //{
  1067. // remapPortR = ".b"; remapPortG = ".b"; remapPortB = ".b"; remapPortA = ".b";
  1068. //}
  1069. dataCollector.AddToPragmas( UniqueId, IOUtils.VirtualTexturePragmaHeader );
  1070. dataCollector.AddToIncludes( UniqueId, atPathname );
  1071. string lodBias = string.Empty;
  1072. if( dataCollector.IsFragmentCategory )
  1073. {
  1074. lodBias = m_mipMode == MipType.MipLevel ? "Lod" : m_mipMode == MipType.MipBias ? "Bias" : "";
  1075. }
  1076. else
  1077. {
  1078. lodBias = "Lod";
  1079. }
  1080. int virtualCoordId = dataCollector.GetVirtualCoordinatesId( UniqueId, GetVirtualUVCoords( ref dataCollector, ignoreLocalVar, portProperty ), lodBias );
  1081. string virtualSampler = SampleVirtualTexture( vtex, Constants.VirtualCoordNameStr + virtualCoordId );
  1082. string virtualVariable = dataCollector.AddVirtualLocalVariable( UniqueId, "virtualNode" + OutputId, virtualSampler );
  1083. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT4, virtualVariable, virtualSampler );
  1084. AddNormalMapTag( ref dataCollector, ref virtualVariable );
  1085. switch( vtex.Channel )
  1086. {
  1087. default:
  1088. case VirtualChannel.Albedo:
  1089. case VirtualChannel.Base:
  1090. case VirtualChannel.Normal:
  1091. case VirtualChannel.Specular:
  1092. case VirtualChannel.SpecMet:
  1093. case VirtualChannel.Material:
  1094. virtualVariable = GetOutputColorItem( 0, outputId, virtualVariable );
  1095. break;
  1096. case VirtualChannel.Displacement:
  1097. case VirtualChannel.Height:
  1098. {
  1099. if( outputId > 0 )
  1100. virtualVariable += ".b";
  1101. else
  1102. {
  1103. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT4, "virtual_cast_" + OutputId, virtualVariable + ".b" );
  1104. virtualVariable = "virtual_cast_" + OutputId;
  1105. }
  1106. //virtualVariable = UIUtils.CastPortType( dataCollector.PortCategory, m_currentPrecisionType, new NodeCastInfo( UniqueId, outputId ), virtualVariable, WirePortDataType.FLOAT, WirePortDataType.FLOAT4, virtualVariable );
  1107. }
  1108. break;
  1109. case VirtualChannel.Occlusion:
  1110. {
  1111. if( outputId > 0 )
  1112. virtualVariable += ".r";
  1113. else
  1114. {
  1115. dataCollector.AddLocalVariable( UniqueId, CurrentPrecisionType, WirePortDataType.FLOAT4, "virtual_cast_" + OutputId, virtualVariable + ".r" );
  1116. virtualVariable = "virtual_cast_" + OutputId;
  1117. }
  1118. }
  1119. break;
  1120. }
  1121. //for ( int i = 0; i < m_outputPorts.Count; i++ )
  1122. //{
  1123. // if ( m_outputPorts[ i ].IsConnected )
  1124. // {
  1125. // //TODO: make the sampler not generate local variables at all times
  1126. // m_textureFetchedValue = "virtualNode" + OutputId;
  1127. // m_isTextureFetched = true;
  1128. // //dataCollector.AddToLocalVariables( m_uniqueId, m_precisionString + " " + m_textureFetchedValue + " = " + virtualSampler + ";" );
  1129. // if ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  1130. // dataCollector.AddToVertexLocalVariables( UniqueId, m_precisionString + " " + m_textureFetchedValue + " = " + virtualSampler + ";" );
  1131. // else
  1132. // dataCollector.AddToLocalVariables( UniqueId, m_precisionString + " " + m_textureFetchedValue + " = " + virtualSampler + ";" );
  1133. // m_colorPort.SetLocalValue( m_textureFetchedValue );
  1134. // m_outputPorts[ m_colorPort.PortId + 1 ].SetLocalValue( m_textureFetchedValue + remapPortR );
  1135. // m_outputPorts[ m_colorPort.PortId + 2 ].SetLocalValue( m_textureFetchedValue + remapPortG );
  1136. // m_outputPorts[ m_colorPort.PortId + 3 ].SetLocalValue( m_textureFetchedValue + remapPortB );
  1137. // m_outputPorts[ m_colorPort.PortId + 4 ].SetLocalValue( m_textureFetchedValue + remapPortA );
  1138. // return m_textureFetchedValue;
  1139. // }
  1140. //}
  1141. return virtualVariable;
  1142. }
  1143. }
  1144. }
  1145. if( m_colorPort.IsLocalValue( dataCollector.PortCategory ) )
  1146. return m_colorPort.LocalValue( dataCollector.PortCategory );
  1147. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  1148. {
  1149. currMipMode = MipType.MipLevel;
  1150. //mipType = "lod";
  1151. }
  1152. string samplerOp = SampleTexture( ref dataCollector, ignoreLocalVar, portProperty, currMipMode, propertyName , varMode );
  1153. AddNormalMapTag( ref dataCollector, ref samplerOp );
  1154. int connectedPorts = 0;
  1155. for( int i = 0; i < m_outputPorts.Count; i++ )
  1156. {
  1157. if( m_outputPorts[ i ].IsConnected )
  1158. {
  1159. connectedPorts += 1;
  1160. if( connectedPorts > 1 || m_outputPorts[ i ].ConnectionCount > 1 )
  1161. {
  1162. // Create common local var and mark as fetched
  1163. string textureFetchedValue = m_samplerType + "Node" + OutputId;
  1164. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  1165. dataCollector.AddToVertexLocalVariables( UniqueId, m_precisionString + " " + textureFetchedValue + " = " + samplerOp + ";" );
  1166. else
  1167. dataCollector.AddToFragmentLocalVariables( UniqueId, m_precisionString + " " + textureFetchedValue + " = " + samplerOp + ";" );
  1168. m_colorPort.SetLocalValue( textureFetchedValue, dataCollector.PortCategory );
  1169. m_outputPorts[ m_colorPort.PortId + 1 ].SetLocalValue( textureFetchedValue + ".r", dataCollector.PortCategory );
  1170. m_outputPorts[ m_colorPort.PortId + 2 ].SetLocalValue( textureFetchedValue + ".g", dataCollector.PortCategory );
  1171. m_outputPorts[ m_colorPort.PortId + 3 ].SetLocalValue( textureFetchedValue + ".b", dataCollector.PortCategory );
  1172. m_outputPorts[ m_colorPort.PortId + 4 ].SetLocalValue( textureFetchedValue + ".a", dataCollector.PortCategory );
  1173. return textureFetchedValue;
  1174. }
  1175. }
  1176. }
  1177. return samplerOp;
  1178. }
  1179. public string GetUVCoords( ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar, string portProperty )
  1180. {
  1181. bool isVertex = ( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation );
  1182. // make sure the final result is always a float4 with empty 0's in the middle
  1183. string uvAppendix = ", ";
  1184. int coordSize = 3;
  1185. if( m_uvPort.DataType == WirePortDataType.FLOAT2 )
  1186. {
  1187. uvAppendix = ", 0, ";
  1188. coordSize = 2;
  1189. }
  1190. else if( m_uvPort.DataType == WirePortDataType.FLOAT )
  1191. {
  1192. uvAppendix = ", 0, 0, ";
  1193. coordSize = 1;
  1194. }
  1195. string uvs = m_uvPort.GeneratePortInstructions( ref dataCollector );
  1196. // generate automatic UVs if not connected
  1197. if( !m_uvPort.IsConnected )
  1198. {
  1199. string propertyName = CurrentPropertyReference;
  1200. // check for references
  1201. if( !string.IsNullOrEmpty( portProperty ) && portProperty != "0.0" )
  1202. propertyName = portProperty;
  1203. int coordSet = ( ( m_textureCoordSet < 0 ) ? 0 : m_textureCoordSet );
  1204. string uvName = IOUtils.GetUVChannelName( propertyName, coordSet );
  1205. string dummyPropUV = "_tex" /*+ ( coordSize != 2 ? "" + coordSize : "" )*/ + "coord" + ( coordSet > 0 ? ( coordSet + 1 ).ToString() : "" );
  1206. string dummyUV = "uv" + ( coordSet > 0 ? ( coordSet + 1 ).ToString() : "" ) + dummyPropUV;
  1207. string attr = GetPropertyValue();
  1208. bool scaleOffset = true;
  1209. if( attr.IndexOf( "[NoScaleOffset]" ) > -1 )
  1210. scaleOffset = false;
  1211. string texCoordsST = string.Empty;
  1212. if( scaleOffset )
  1213. {
  1214. if( m_texCoordsHelper == null )
  1215. {
  1216. m_texCoordsHelper = CreateInstance<Vector4Node>();
  1217. m_texCoordsHelper.ContainerGraph = ContainerGraph;
  1218. m_texCoordsHelper.SetBaseUniqueId( UniqueId, true );
  1219. m_texCoordsHelper.RegisterPropertyOnInstancing = false;
  1220. m_texCoordsHelper.AddGlobalToSRPBatcher = true;
  1221. }
  1222. if( UIUtils.CurrentWindow.OutsideGraph.IsInstancedShader )
  1223. {
  1224. m_texCoordsHelper.CurrentParameterType = PropertyType.InstancedProperty;
  1225. }
  1226. else
  1227. {
  1228. m_texCoordsHelper.CurrentParameterType = PropertyType.Global;
  1229. }
  1230. m_texCoordsHelper.ResetOutputLocals();
  1231. m_texCoordsHelper.SetRawPropertyName( propertyName + "_ST" );
  1232. texCoordsST = m_texCoordsHelper.GenerateShaderForOutput( 0, ref dataCollector, false );
  1233. }
  1234. string coordInput = string.Empty;
  1235. if( !dataCollector.IsTemplate && coordSet > 3 )
  1236. {
  1237. coordInput = GeneratorUtils.GenerateAutoUVs( ref dataCollector, UniqueId, coordSet, null, m_uvPort.DataType );
  1238. }
  1239. else
  1240. {
  1241. dataCollector.AddToProperties( UniqueId, "[HideInInspector] " + dummyPropUV + "( \"\", 2D ) = \"white\" {}", 9999 );
  1242. if( isVertex )
  1243. {
  1244. coordInput = Constants.VertexShaderInputStr + ".texcoord";
  1245. if( coordSet > 0 )
  1246. coordInput += coordSet.ToString();
  1247. }
  1248. else
  1249. {
  1250. coordInput = Constants.InputVarStr + "." + dummyUV;
  1251. dataCollector.AddToInput( UniqueId, dummyUV, dataCollector.GetMaxTextureChannelSize( coordSet ));
  1252. }
  1253. }
  1254. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.Template )
  1255. {
  1256. string result = string.Empty;
  1257. if( dataCollector.TemplateDataCollectorInstance.GetCustomInterpolatedData( TemplateHelperFunctions.IntToUVChannelInfo[ m_textureCoordSet ], m_uvPort.DataType, PrecisionType.Float, ref result, false, dataCollector.PortCategory ) )
  1258. {
  1259. coordInput = result;
  1260. }
  1261. else
  1262. if( dataCollector.TemplateDataCollectorInstance.HasUV( m_textureCoordSet ) )
  1263. coordInput = dataCollector.TemplateDataCollectorInstance.GetUVName( m_textureCoordSet, m_uvPort.DataType );
  1264. else
  1265. coordInput = dataCollector.TemplateDataCollectorInstance.RegisterUV( m_textureCoordSet, m_uvPort.DataType );
  1266. }
  1267. if( !scaleOffset )
  1268. uvName += OutputId;
  1269. if( coordSize > 2 )
  1270. {
  1271. uvName += coordSize;
  1272. dataCollector.UsingHigherSizeTexcoords = true;
  1273. dataCollector.AddLocalVariable( UniqueId, "float" + coordSize + " " + uvName + " = " + coordInput + ";" );
  1274. if( scaleOffset )
  1275. {
  1276. string scaleOffsetValue = GeneratorUtils.GenerateScaleOffsettedUV( m_currentType , coordInput+".xy" , texCoordsST, false );
  1277. dataCollector.AddLocalVariable( UniqueId , uvName + ".xy = " + scaleOffsetValue+";" );
  1278. }
  1279. }
  1280. else
  1281. {
  1282. if( coordSize == 1 )
  1283. uvName += coordSize;
  1284. if( scaleOffset )
  1285. {
  1286. string scaleOffsetValue = GeneratorUtils.GenerateScaleOffsettedUV( m_currentType , coordInput , texCoordsST, false );
  1287. dataCollector.AddLocalVariable( UniqueId , PrecisionType.Float , m_uvPort.DataType , uvName , scaleOffsetValue );
  1288. }
  1289. else
  1290. dataCollector.AddLocalVariable( UniqueId , PrecisionType.Float , m_uvPort.DataType , uvName , coordInput );
  1291. }
  1292. uvs = uvName;
  1293. }
  1294. ParentGraph outsideGraph = UIUtils.CurrentWindow.OutsideGraph;
  1295. if( m_currentType == TextureType.Texture2DArray )
  1296. {
  1297. string index = m_indexPort.GeneratePortInstructions( ref dataCollector );
  1298. uvs = string.Format( "{0},{1}", uvs, index );
  1299. if( !( ( outsideGraph.SamplingMacros || m_currentType == TextureType.Texture2DArray ) && outsideGraph.IsSRP ) )
  1300. {
  1301. uvs = "float3(" + uvs + ")";
  1302. }
  1303. }
  1304. if( isVertex )
  1305. {
  1306. string lodLevel = m_lodPort.GeneratePortInstructions( ref dataCollector );
  1307. if( ( outsideGraph.SamplingMacros || m_currentType == TextureType.Texture2DArray ) && m_currentType != TextureType.Texture1D )
  1308. return uvs + ", " + lodLevel;
  1309. else
  1310. return UIUtils.PrecisionWirePortToCgType( PrecisionType.Float, WirePortDataType.FLOAT4 ) + "( " + uvs + uvAppendix + lodLevel + ")";
  1311. }
  1312. else
  1313. {
  1314. if( ( m_mipMode == MipType.MipLevel || m_mipMode == MipType.MipBias ) /*&& m_lodPort.IsConnected*/ )
  1315. {
  1316. string lodLevel = m_lodPort.GeneratePortInstructions( ref dataCollector );
  1317. if( ( outsideGraph.SamplingMacros || m_currentType == TextureType.Texture2DArray ) && m_currentType != TextureType.Texture1D )
  1318. return uvs + ", " + lodLevel;
  1319. else
  1320. return UIUtils.PrecisionWirePortToCgType( PrecisionType.Float, WirePortDataType.FLOAT4 ) + "( " + uvs + uvAppendix + lodLevel + ")";
  1321. }
  1322. else if( m_mipMode == MipType.Derivative )
  1323. {
  1324. string ddx = m_ddxPort.GeneratePortInstructions( ref dataCollector );
  1325. string ddy = m_ddyPort.GeneratePortInstructions( ref dataCollector );
  1326. return uvs + ", " + ddx + ", " + ddy;
  1327. }
  1328. else
  1329. {
  1330. return uvs;
  1331. }
  1332. }
  1333. }
  1334. public string GetVirtualUVCoords( ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar, string portProperty )
  1335. {
  1336. string bias = "";
  1337. if( !dataCollector.IsFragmentCategory || m_mipMode == MipType.MipBias || m_mipMode == MipType.MipLevel )
  1338. {
  1339. string lodLevel = m_lodPort.GeneratePortInstructions( ref dataCollector );
  1340. bias += ", " + lodLevel;
  1341. }
  1342. if( m_uvPort.IsConnected )
  1343. {
  1344. string uvs = m_uvPort.GeneratePortInstructions( ref dataCollector );
  1345. return uvs + bias;
  1346. }
  1347. else
  1348. {
  1349. string propertyName = CurrentPropertyReference;
  1350. if( !string.IsNullOrEmpty( portProperty ) )
  1351. {
  1352. propertyName = portProperty;
  1353. }
  1354. string uvChannelName = IOUtils.GetUVChannelName( propertyName, m_textureCoordSet );
  1355. string uvCoord = string.Empty;
  1356. if( dataCollector.IsTemplate )
  1357. {
  1358. string uvName = string.Empty;
  1359. if( dataCollector.TemplateDataCollectorInstance.HasUV( m_textureCoordSet ) )
  1360. {
  1361. uvName = dataCollector.TemplateDataCollectorInstance.GetUVName( m_textureCoordSet, m_uvPort.DataType );
  1362. }
  1363. else
  1364. {
  1365. uvName = dataCollector.TemplateDataCollectorInstance.RegisterUV( m_textureCoordSet, m_uvPort.DataType );
  1366. }
  1367. string attr = GetPropertyValue();
  1368. if( attr.IndexOf( "[NoScaleOffset]" ) > -1 )
  1369. {
  1370. dataCollector.AddLocalVariable( UniqueId, PrecisionType.Float, WirePortDataType.FLOAT2, uvChannelName, uvName );
  1371. }
  1372. else
  1373. {
  1374. dataCollector.AddToUniforms( UniqueId, "uniform float4 " + propertyName + "_ST;" );
  1375. dataCollector.AddLocalVariable( UniqueId , PrecisionType.Float , WirePortDataType.FLOAT2 , uvChannelName , GeneratorUtils.GenerateScaleOffsettedUV( m_currentType , uvName , propertyName,true ) );
  1376. }
  1377. uvCoord = uvChannelName;
  1378. }
  1379. else
  1380. {
  1381. if( dataCollector.PortCategory == MasterNodePortCategory.Vertex || dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  1382. {
  1383. uvCoord = Constants.VertexShaderInputStr + ".texcoord";
  1384. if( m_textureCoordSet > 0 )
  1385. {
  1386. uvCoord += m_textureCoordSet.ToString();
  1387. }
  1388. }
  1389. else
  1390. {
  1391. propertyName = CurrentPropertyReference;
  1392. if( !string.IsNullOrEmpty( portProperty ) && portProperty != "0.0" )
  1393. {
  1394. propertyName = portProperty;
  1395. }
  1396. uvChannelName = IOUtils.GetUVChannelName( propertyName, m_textureCoordSet );
  1397. string dummyPropUV = "_texcoord" + ( m_textureCoordSet > 0 ? ( m_textureCoordSet + 1 ).ToString() : "" );
  1398. string dummyUV = "uv" + ( m_textureCoordSet > 0 ? ( m_textureCoordSet + 1 ).ToString() : "" ) + dummyPropUV;
  1399. dataCollector.AddToProperties( UniqueId, "[HideInInspector] " + dummyPropUV + "( \"\", 2D ) = \"white\" {}", 100 );
  1400. dataCollector.AddToInput( UniqueId, dummyUV, WirePortDataType.FLOAT2 );
  1401. string attr = GetPropertyValue();
  1402. if( attr.IndexOf( "[NoScaleOffset]" ) > -1 )
  1403. {
  1404. dataCollector.AddToLocalVariables( UniqueId, PrecisionType.Float, WirePortDataType.FLOAT2, uvChannelName, Constants.InputVarStr + "." + dummyUV );
  1405. }
  1406. else
  1407. {
  1408. dataCollector.AddToUniforms( UniqueId, "uniform float4 " + propertyName + "_ST;" );
  1409. string offsettedUV = GeneratorUtils.GenerateScaleOffsettedUV( m_currentType , dummyUV , propertyName,true );
  1410. dataCollector.AddToLocalVariables( UniqueId, PrecisionType.Float, WirePortDataType.FLOAT2, uvChannelName, Constants.InputVarStr + "." + offsettedUV );
  1411. }
  1412. uvCoord = uvChannelName;
  1413. }
  1414. }
  1415. return uvCoord + bias;
  1416. }
  1417. }
  1418. private void AddNormalMapTag( ref MasterNodeDataCollector dataCollector, ref string value )
  1419. {
  1420. if( m_autoUnpackNormals )
  1421. {
  1422. bool isScaledNormal = false;
  1423. if( m_normalPort.IsConnected )
  1424. {
  1425. isScaledNormal = true;
  1426. }
  1427. else
  1428. {
  1429. if( m_normalPort.FloatInternalData != 1 )
  1430. {
  1431. isScaledNormal = true;
  1432. }
  1433. }
  1434. string scaleValue = isScaledNormal ? m_normalPort.GeneratePortInstructions( ref dataCollector ) : "1.0f";
  1435. value = GeneratorUtils.GenerateUnpackNormalStr( ref dataCollector, CurrentPrecisionType, UniqueId, OutputId, value, isScaledNormal, scaleValue, UnpackInputMode.Tangent );
  1436. if( isScaledNormal )
  1437. {
  1438. if( !( dataCollector.IsTemplate && dataCollector.IsSRP ) )
  1439. {
  1440. dataCollector.AddToIncludes( UniqueId, Constants.UnityStandardUtilsLibFuncs );
  1441. }
  1442. }
  1443. }
  1444. }
  1445. public override void ReadOutputDataFromString( ref string[] nodeParams )
  1446. {
  1447. base.ReadOutputDataFromString( ref nodeParams );
  1448. ConfigureOutputPorts();
  1449. }
  1450. public override int InputIdFromDeprecated( int oldInputId )
  1451. {
  1452. // this is not a good solution, it doesn't check for the deprecated type and thus assumes it always comes from texture array
  1453. switch( oldInputId )
  1454. {
  1455. default:
  1456. return oldInputId;
  1457. case 0:
  1458. return 1;
  1459. case 1:
  1460. return 6;
  1461. case 2:
  1462. return 2;
  1463. case 3:
  1464. return 5;
  1465. case 4:
  1466. return 3;
  1467. case 5:
  1468. return 4;
  1469. case 6:
  1470. return 0;
  1471. }
  1472. }
  1473. public override void ReadFromDeprecated( ref string[] nodeParams, Type oldType = null )
  1474. {
  1475. base.ReadFromDeprecated( ref nodeParams, oldType );
  1476. if( oldType == typeof( TextureArrayNode ) )
  1477. {
  1478. base.ReadFromStringArray( ref nodeParams );
  1479. string textureName = GetCurrentParam( ref nodeParams );
  1480. m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture2DArray>( textureName );
  1481. if( m_defaultValue )
  1482. {
  1483. m_materialValue = m_defaultValue;
  1484. }
  1485. m_textureCoordSet = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1486. m_referenceType = (TexReferenceType)Enum.Parse( typeof( TexReferenceType ), GetCurrentParam( ref nodeParams ) );
  1487. m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1488. if( m_referenceType == TexReferenceType.Instance )
  1489. {
  1490. UIUtils.UnregisterSamplerNode( this );
  1491. UIUtils.UnregisterPropertyNode( this );
  1492. }
  1493. UpdateHeaderColor();
  1494. if( UIUtils.CurrentShaderVersion() > 3202 )
  1495. m_mipMode = (MipType)Enum.Parse( typeof( MipType ), GetCurrentParam( ref nodeParams ) );
  1496. if( UIUtils.CurrentShaderVersion() > 5105 )
  1497. m_autoUnpackNormals = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  1498. m_currentType = TextureType.Texture2DArray;
  1499. //m_autocastMode = AutoCastType.LockedToTexture2DArray;
  1500. if( m_defaultValue == null )
  1501. {
  1502. ConfigureInputPorts();
  1503. ConfigureOutputPorts();
  1504. //ResizeNodeToPreview();
  1505. }
  1506. else
  1507. {
  1508. if( m_materialValue == null )
  1509. {
  1510. ConfigFromObject( m_defaultValue, false, false );
  1511. }
  1512. else
  1513. {
  1514. CheckTextureImporter( false, false );
  1515. }
  1516. ConfigureInputPorts();
  1517. ConfigureOutputPorts();
  1518. }
  1519. if( !m_isNodeBeingCopied && m_referenceType == TexReferenceType.Object )
  1520. {
  1521. ContainerGraph.SamplerNodes.UpdateDataOnNode( UniqueId, DataToArray );
  1522. }
  1523. // reading input data due to internal data being lost
  1524. int count = 0;
  1525. if( UIUtils.CurrentShaderVersion() > 7003 )
  1526. {
  1527. try
  1528. {
  1529. count = Convert.ToInt32( nodeParams[ m_currentReadParamIdx++ ] );
  1530. }
  1531. catch( Exception e )
  1532. {
  1533. Debug.LogException( e );
  1534. }
  1535. }
  1536. else
  1537. {
  1538. count = ( m_oldInputCount < 0 ) ? m_inputPorts.Count : m_oldInputCount;
  1539. }
  1540. for( int i = 0; i < count && i < nodeParams.Length && m_currentReadParamIdx < nodeParams.Length; i++ )
  1541. {
  1542. if( UIUtils.CurrentShaderVersion() < 5003 )
  1543. {
  1544. int newId = VersionConvertInputPortId( i );
  1545. string InternalData = string.Empty;
  1546. if( UIUtils.CurrentShaderVersion() > 23 )
  1547. {
  1548. Enum.Parse( typeof( WirePortDataType ), nodeParams[ m_currentReadParamIdx++ ] );
  1549. }
  1550. InternalData = nodeParams[ m_currentReadParamIdx++ ];
  1551. if( UIUtils.CurrentShaderVersion() >= 3100 && m_currentReadParamIdx < nodeParams.Length )
  1552. {
  1553. nodeParams[ m_currentReadParamIdx++ ].ToString();
  1554. }
  1555. if( newId == 2 )
  1556. {
  1557. m_indexPort.InternalData = InternalData;
  1558. m_indexPort.UpdatePreviewInternalData();
  1559. }
  1560. if( newId == 3 )
  1561. {
  1562. m_lodPort.InternalData = InternalData;
  1563. m_lodPort.UpdatePreviewInternalData();
  1564. }
  1565. }
  1566. else
  1567. {
  1568. string portIdStr = nodeParams[ m_currentReadParamIdx++ ];
  1569. int portId = -1;
  1570. try
  1571. {
  1572. portId = Convert.ToInt32( portIdStr );
  1573. }
  1574. catch( Exception e )
  1575. {
  1576. Debug.LogException( e );
  1577. }
  1578. Enum.Parse( typeof( WirePortDataType ), nodeParams[ m_currentReadParamIdx++ ] );
  1579. string InternalData = nodeParams[ m_currentReadParamIdx++ ];
  1580. bool isEditable = Convert.ToBoolean( nodeParams[ m_currentReadParamIdx++ ] );
  1581. if( isEditable && m_currentReadParamIdx < nodeParams.Length )
  1582. {
  1583. nodeParams[ m_currentReadParamIdx++ ].ToString();
  1584. }
  1585. if( portId == 1 )
  1586. {
  1587. m_indexPort.InternalData = InternalData;
  1588. m_indexPort.UpdatePreviewInternalData();
  1589. }
  1590. if( portId == 2 )
  1591. {
  1592. m_lodPort.InternalData = InternalData;
  1593. m_lodPort.UpdatePreviewInternalData();
  1594. }
  1595. }
  1596. }
  1597. }
  1598. }
  1599. public override void ReadFromString( ref string[] nodeParams )
  1600. {
  1601. base.ReadFromString( ref nodeParams );
  1602. string defaultTextureGUID = GetCurrentParam( ref nodeParams );
  1603. if( UIUtils.CurrentShaderVersion() > 14101 )
  1604. {
  1605. m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( defaultTextureGUID ) );
  1606. string materialTextureGUID = GetCurrentParam( ref nodeParams );
  1607. m_materialValue = AssetDatabase.LoadAssetAtPath<Texture>( AssetDatabase.GUIDToAssetPath( materialTextureGUID ) );
  1608. }
  1609. else
  1610. {
  1611. m_defaultValue = AssetDatabase.LoadAssetAtPath<Texture>( defaultTextureGUID );
  1612. }
  1613. m_useSemantics = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  1614. m_textureCoordSet = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1615. m_isNormalMap = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  1616. m_defaultTextureValue = (TexturePropertyValues)Enum.Parse( typeof( TexturePropertyValues ), GetCurrentParam( ref nodeParams ) );
  1617. m_autocastMode = (AutoCastType)Enum.Parse( typeof( AutoCastType ), GetCurrentParam( ref nodeParams ) );
  1618. m_autoUnpackNormals = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  1619. if( UIUtils.CurrentShaderVersion() > 12 )
  1620. {
  1621. m_referenceType = (TexReferenceType)Enum.Parse( typeof( TexReferenceType ), GetCurrentParam( ref nodeParams ) );
  1622. if( UIUtils.CurrentShaderVersion() > 22 )
  1623. {
  1624. m_referenceNodeId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1625. }
  1626. else
  1627. {
  1628. m_referenceArrayId = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  1629. }
  1630. if( m_referenceType == TexReferenceType.Instance )
  1631. {
  1632. UIUtils.UnregisterSamplerNode( this );
  1633. UIUtils.UnregisterPropertyNode( this );
  1634. }
  1635. UpdateHeaderColor();
  1636. }
  1637. if( UIUtils.CurrentShaderVersion() > 2406 )
  1638. m_mipMode = (MipType)Enum.Parse( typeof( MipType ), GetCurrentParam( ref nodeParams ) );
  1639. if( UIUtils.CurrentShaderVersion() > 3201 )
  1640. m_currentType = (TextureType)Enum.Parse( typeof( TextureType ), GetCurrentParam( ref nodeParams ) );
  1641. if( m_defaultValue == null )
  1642. {
  1643. ConfigureInputPorts();
  1644. ConfigureOutputPorts();
  1645. //ResizeNodeToPreview();
  1646. }
  1647. else
  1648. {
  1649. if( m_materialValue == null )
  1650. {
  1651. ConfigFromObject( m_defaultValue, false, false );
  1652. }
  1653. else
  1654. {
  1655. CheckTextureImporter( false, false );
  1656. }
  1657. ConfigureInputPorts();
  1658. ConfigureOutputPorts();
  1659. }
  1660. if( !m_isNodeBeingCopied && m_referenceType == TexReferenceType.Object )
  1661. {
  1662. ContainerGraph.SamplerNodes.UpdateDataOnNode( UniqueId, DataToArray );
  1663. }
  1664. if( UIUtils.CurrentShaderVersion() >= 6001 && UIUtils.CurrentShaderVersion() < 7003 )
  1665. {
  1666. m_oldInputCount = 6;
  1667. }
  1668. }
  1669. public override void RefreshExternalReferences()
  1670. {
  1671. base.RefreshExternalReferences();
  1672. ForceInputPortsChange();
  1673. if( m_useSamplerArrayIdx > -1 )
  1674. {
  1675. m_useSamplerArrayIdx = UIUtils.GetTexturePropertyNodeRegisterId( m_useSamplerArrayIdx ) + 1;
  1676. }
  1677. else
  1678. {
  1679. m_useSamplerArrayIdx = 0;
  1680. }
  1681. EditorGUI.BeginChangeCheck();
  1682. if( m_referenceType == TexReferenceType.Instance )
  1683. {
  1684. if( UIUtils.CurrentShaderVersion() > 22 )
  1685. {
  1686. m_referenceSampler = ContainerGraph.GetNode( m_referenceNodeId ) as SamplerNode;
  1687. m_referenceArrayId = ContainerGraph.SamplerNodes.GetNodeRegisterIdx( m_referenceNodeId );
  1688. }
  1689. else
  1690. {
  1691. m_referenceSampler = ContainerGraph.SamplerNodes.GetNode( m_referenceArrayId );
  1692. if( m_referenceSampler != null )
  1693. {
  1694. m_referenceNodeId = m_referenceSampler.UniqueId;
  1695. }
  1696. }
  1697. }
  1698. if( EditorGUI.EndChangeCheck() )
  1699. {
  1700. OnPropertyNameChanged();
  1701. }
  1702. }
  1703. public override void ReadAdditionalData( ref string[] nodeParams ) { }
  1704. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  1705. {
  1706. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  1707. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_defaultValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_defaultValue ) ) : Constants.NoStringValue );
  1708. IOUtils.AddFieldValueToString( ref nodeInfo, ( m_materialValue != null ) ? AssetDatabase.AssetPathToGUID( AssetDatabase.GetAssetPath( m_materialValue ) ) : Constants.NoStringValue );
  1709. IOUtils.AddFieldValueToString( ref nodeInfo, m_useSemantics.ToString() );
  1710. IOUtils.AddFieldValueToString( ref nodeInfo, m_textureCoordSet.ToString() );
  1711. IOUtils.AddFieldValueToString( ref nodeInfo, m_isNormalMap.ToString() );
  1712. IOUtils.AddFieldValueToString( ref nodeInfo, m_defaultTextureValue );
  1713. IOUtils.AddFieldValueToString( ref nodeInfo, m_autocastMode );
  1714. IOUtils.AddFieldValueToString( ref nodeInfo, m_autoUnpackNormals );
  1715. IOUtils.AddFieldValueToString( ref nodeInfo, m_referenceType );
  1716. IOUtils.AddFieldValueToString( ref nodeInfo, ( ( m_referenceSampler != null ) ? m_referenceSampler.UniqueId : -1 ) );
  1717. IOUtils.AddFieldValueToString( ref nodeInfo, m_mipMode );
  1718. IOUtils.AddFieldValueToString( ref nodeInfo, m_currentType );
  1719. }
  1720. public override void WriteAdditionalToString( ref string nodeInfo, ref string connectionsInfo ) { }
  1721. public override int VersionConvertInputPortId( int portId )
  1722. {
  1723. int newPort = portId;
  1724. //change normal scale port to last
  1725. if( UIUtils.CurrentShaderVersion() < 2407 )
  1726. {
  1727. if( portId == 1 )
  1728. newPort = 4;
  1729. }
  1730. if( UIUtils.CurrentShaderVersion() < 2408 )
  1731. {
  1732. newPort = newPort + 1;
  1733. }
  1734. return newPort;
  1735. }
  1736. public override void Destroy()
  1737. {
  1738. base.Destroy();
  1739. //Not calling m_texCoordsHelper.Destroy() on purpose so UIUtils does not incorrectly unregister stuff
  1740. if( m_texCoordsHelper != null )
  1741. {
  1742. DestroyImmediate( m_texCoordsHelper );
  1743. m_texCoordsHelper = null;
  1744. }
  1745. m_samplerStateAutoGenerator.Destroy();
  1746. m_samplerStateAutoGenerator = null;
  1747. m_defaultValue = null;
  1748. m_materialValue = null;
  1749. m_referenceSampler = null;
  1750. m_referenceStyle = null;
  1751. m_referenceContent = null;
  1752. m_texPort = null;
  1753. m_uvPort = null;
  1754. m_lodPort = null;
  1755. m_ddxPort = null;
  1756. m_ddyPort = null;
  1757. m_normalPort = null;
  1758. m_colorPort = null;
  1759. m_samplerPort = null;
  1760. m_indexPort = null;
  1761. if( m_referenceType == TexReferenceType.Object )
  1762. {
  1763. UIUtils.UnregisterSamplerNode( this );
  1764. UIUtils.UnregisterPropertyNode( this );
  1765. }
  1766. if( UniqueId > -1 )
  1767. ContainerGraph.SamplerNodes.OnReorderEventComplete -= OnReorderEventComplete;
  1768. }
  1769. public override string GetPropertyValStr()
  1770. {
  1771. return m_materialMode ? ( m_materialValue != null ? m_materialValue.name : IOUtils.NO_TEXTURES ) : ( m_defaultValue != null ? m_defaultValue.name : IOUtils.NO_TEXTURES );
  1772. }
  1773. public TexturePropertyNode TextureProperty
  1774. {
  1775. get
  1776. {
  1777. if( m_referenceSampler != null )
  1778. {
  1779. m_textureProperty = m_referenceSampler as TexturePropertyNode;
  1780. }
  1781. else if( m_texPort.IsConnected )
  1782. {
  1783. m_textureProperty = m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode;
  1784. }
  1785. if( m_textureProperty == null )
  1786. return this;
  1787. return m_textureProperty;
  1788. }
  1789. }
  1790. public override string GetPropertyValue()
  1791. {
  1792. if( SoftValidReference )
  1793. {
  1794. if( m_referenceSampler.TexPort.IsConnected )
  1795. {
  1796. return string.Empty;
  1797. }
  1798. else
  1799. {
  1800. return m_referenceSampler.TextureProperty.GetPropertyValue();
  1801. }
  1802. }
  1803. else
  1804. if( m_texPort.IsConnected && ( m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode ) != null )
  1805. {
  1806. return TextureProperty.GetPropertyValue();
  1807. }
  1808. switch( m_currentType )
  1809. {
  1810. case TextureType.Texture1D:
  1811. {
  1812. return PropertyAttributes + GetTexture1DPropertyValue();
  1813. }
  1814. case TextureType.ProceduralTexture:
  1815. case TextureType.Texture2D:
  1816. {
  1817. return PropertyAttributes + GetTexture2DPropertyValue();
  1818. }
  1819. case TextureType.Texture3D:
  1820. {
  1821. return PropertyAttributes + GetTexture3DPropertyValue();
  1822. }
  1823. case TextureType.Cube:
  1824. {
  1825. return PropertyAttributes + GetCubePropertyValue();
  1826. }
  1827. case TextureType.Texture2DArray:
  1828. {
  1829. return PropertyAttributes + GetTexture2DArrayPropertyValue();
  1830. }
  1831. }
  1832. return string.Empty;
  1833. }
  1834. public override string GetUniformValue()
  1835. {
  1836. if( SoftValidReference )
  1837. {
  1838. if( m_referenceSampler.TexPort.IsConnected )
  1839. return string.Empty;
  1840. else
  1841. return m_referenceSampler.TextureProperty.GetUniformValue();
  1842. }
  1843. else if( m_texPort.IsConnected && ( m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode ) != null )
  1844. {
  1845. return TextureProperty.GetUniformValue();
  1846. }
  1847. return base.GetUniformValue();
  1848. }
  1849. public override bool GetUniformData( out string dataType, out string dataName, ref bool fullValue )
  1850. {
  1851. if( SoftValidReference )
  1852. {
  1853. if( m_referenceSampler.TexPort.IsConnected )
  1854. {
  1855. base.GetUniformData( out dataType, out dataName, ref fullValue );
  1856. return false;
  1857. }
  1858. else
  1859. return m_referenceSampler.TextureProperty.GetUniformData( out dataType, out dataName, ref fullValue );
  1860. }
  1861. else if( m_texPort.IsConnected && ( m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode ) != null )
  1862. {
  1863. return TextureProperty.GetUniformData( out dataType, out dataName, ref fullValue );
  1864. }
  1865. return base.GetUniformData( out dataType, out dataName, ref fullValue );
  1866. }
  1867. public string UVCoordsName { get { return Constants.InputVarStr + "." + IOUtils.GetUVChannelName( CurrentPropertyReference, m_textureCoordSet ); } }
  1868. public bool HasPropertyReference
  1869. {
  1870. get
  1871. {
  1872. if( m_referenceType == TexReferenceType.Instance && m_referenceArrayId > -1 )
  1873. {
  1874. SamplerNode node = ContainerGraph.SamplerNodes.GetNode( m_referenceArrayId );
  1875. if( node != null )
  1876. return true;
  1877. }
  1878. if( m_texPort.IsConnected )
  1879. {
  1880. return true;
  1881. }
  1882. return false;
  1883. }
  1884. }
  1885. public override string CurrentPropertyReference
  1886. {
  1887. get
  1888. {
  1889. string propertyName = string.Empty;
  1890. if( m_referenceType == TexReferenceType.Instance && m_referenceArrayId > -1 )
  1891. {
  1892. SamplerNode node = ContainerGraph.SamplerNodes.GetNode( m_referenceArrayId );
  1893. propertyName = ( node != null ) ? node.TextureProperty.PropertyName : PropertyName;
  1894. }
  1895. else if( m_texPort.IsConnected && ( m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode ) != null )
  1896. {
  1897. propertyName = TextureProperty.PropertyName;
  1898. }
  1899. else
  1900. {
  1901. propertyName = PropertyName;
  1902. }
  1903. return propertyName;
  1904. }
  1905. }
  1906. public VariableMode VarModeReference
  1907. {
  1908. get
  1909. {
  1910. VariableMode mode;
  1911. if( m_referenceType == TexReferenceType.Instance && m_referenceArrayId > -1 )
  1912. {
  1913. SamplerNode node = ContainerGraph.SamplerNodes.GetNode( m_referenceArrayId );
  1914. mode = ( node != null ) ? node.CurrentVariableMode : m_variableMode;
  1915. }
  1916. else if( m_texPort.IsConnected && ( m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode ) != null )
  1917. {
  1918. mode = TextureProperty.CurrentVariableMode;
  1919. }
  1920. else
  1921. {
  1922. mode = m_variableMode;
  1923. }
  1924. return mode;
  1925. }
  1926. }
  1927. public bool SoftValidReference
  1928. {
  1929. get
  1930. {
  1931. if( m_referenceType == TexReferenceType.Instance && m_referenceArrayId > -1 )
  1932. {
  1933. m_referenceSampler = ContainerGraph.SamplerNodes.GetNode( m_referenceArrayId );
  1934. m_texPort.Locked = true;
  1935. if( m_referenceContent == null )
  1936. m_referenceContent = new GUIContent();
  1937. if( m_referenceSampler != null )
  1938. {
  1939. m_referenceContent.image = m_referenceSampler.Value;
  1940. if( m_referenceWidth != m_referenceSampler.Position.width )
  1941. {
  1942. m_referenceWidth = m_referenceSampler.Position.width;
  1943. m_sizeIsDirty = true;
  1944. }
  1945. }
  1946. else
  1947. {
  1948. m_referenceArrayId = -1;
  1949. m_referenceWidth = -1;
  1950. }
  1951. return m_referenceSampler != null;
  1952. }
  1953. m_texPort.Locked = false;
  1954. return false;
  1955. }
  1956. }
  1957. public override void ForceUpdateFromMaterial( Material material )
  1958. {
  1959. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( PropertyName ) )
  1960. {
  1961. m_materialValue = material.GetTexture( PropertyName );
  1962. CheckTextureImporter( true );
  1963. PreviewIsDirty = true;
  1964. }
  1965. }
  1966. public override void SetContainerGraph( ParentGraph newgraph )
  1967. {
  1968. base.SetContainerGraph( newgraph );
  1969. m_textureProperty = m_texPort.GetOutputNodeWhichIsNotRelay( 0 ) as TexturePropertyNode;
  1970. if( m_textureProperty == null )
  1971. {
  1972. m_textureProperty = this;
  1973. }
  1974. }
  1975. public bool AutoUnpackNormals
  1976. {
  1977. get { return m_autoUnpackNormals; }
  1978. set
  1979. {
  1980. if( value != m_autoUnpackNormals )
  1981. {
  1982. m_autoUnpackNormals = value;
  1983. if( !UIUtils.IsLoading )
  1984. {
  1985. m_defaultTextureValue = value ? TexturePropertyValues.bump : TexturePropertyValues.white;
  1986. }
  1987. }
  1988. }
  1989. }
  1990. public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  1991. {
  1992. base.PropagateNodeData( nodeData, ref dataCollector );
  1993. if( dataCollector.IsTemplate )
  1994. {
  1995. if( !m_texPort.IsConnected )
  1996. dataCollector.TemplateDataCollectorInstance.SetUVUsage( m_textureCoordSet , m_uvPort.DataType );
  1997. }
  1998. else
  1999. {
  2000. if( !m_uvPort.IsConnected )
  2001. dataCollector.SetTextureChannelSize( m_textureCoordSet , m_uvPort.DataType );
  2002. if( m_textureCoordSet > 3 )
  2003. {
  2004. dataCollector.AddCustomAppData( string.Format( TemplateHelperFunctions.TexUVFullSemantic , m_textureCoordSet ) );
  2005. }
  2006. }
  2007. }
  2008. private InputPort TexPort { get { return m_texPort; } }
  2009. public bool IsObject { get { return ( m_referenceType == TexReferenceType.Object ) || ( m_referenceSampler == null ); } }
  2010. }
  2011. }