InputPort.cs 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public sealed class InputPort : WirePort
  10. {
  11. private const string InputDefaultNameStr = "Input";
  12. [SerializeField]
  13. private int m_externalNodeLink = -1;
  14. [SerializeField]
  15. private int m_externalPortLink = -1;
  16. [SerializeField]
  17. private string m_externalLinkId = string.Empty;
  18. [SerializeField]
  19. private bool m_typeLocked;
  20. [SerializeField]
  21. private string m_internalData = string.Empty;
  22. [SerializeField]
  23. private string m_internalDataWrapper = string.Empty;
  24. [SerializeField]
  25. private string m_dataName = string.Empty;
  26. [SerializeField]
  27. private string m_internalDataPropertyLabel = string.Empty;
  28. // this will only is important on master node
  29. [SerializeField]
  30. private MasterNodePortCategory m_category = MasterNodePortCategory.Fragment;
  31. [SerializeField]
  32. private PortGenType m_genType;
  33. private string m_propertyName = string.Empty;
  34. private int m_cachedPropertyId = -1;
  35. private int m_cachedIntShaderID = -1;
  36. private int m_cachedFloatShaderID = -1;
  37. private int m_cachedVectorShaderID = -1;
  38. private int m_cachedColorShaderID = -1;
  39. private int m_cached2DShaderID = -1;
  40. private int m_cachedDefaultTexShaderID = -1;
  41. [SerializeField]
  42. private bool m_drawInternalData = false;
  43. //[SerializeField]
  44. //private RenderTexture m_inputPreview = null;
  45. //[SerializeField]
  46. private RenderTexture m_inputPreviewTexture = null;
  47. private Material m_inputPreviewMaterial = null;
  48. private Shader m_inputPreviewShader = null;
  49. [SerializeField]
  50. private int m_previewInternalInt = 0;
  51. [SerializeField]
  52. private float m_previewInternalFloat = 0;
  53. [SerializeField]
  54. private Vector2 m_previewInternalVec2 = Vector2.zero;
  55. [SerializeField]
  56. private Vector3 m_previewInternalVec3 = Vector3.zero;
  57. [SerializeField]
  58. private Vector4 m_previewInternalVec4 = Vector4.zero;
  59. [SerializeField]
  60. private Color m_previewInternalColor = Color.clear;
  61. [SerializeField]
  62. private Matrix4x4 m_previewInternalMatrix4x4 = Matrix4x4.identity;
  63. private int m_propertyNameInt = 0;
  64. private ParentNode m_node = null;
  65. public InputPort() : base( -1, -1, WirePortDataType.FLOAT, string.Empty ) { m_typeLocked = true; }
  66. public InputPort( int nodeId, int portId, WirePortDataType dataType, string name, bool typeLocked, int orderId = -1, MasterNodePortCategory category = MasterNodePortCategory.Fragment, PortGenType genType = PortGenType.NonCustomLighting ) : base( nodeId, portId, dataType, name, orderId )
  67. {
  68. m_dataName = name;
  69. m_internalDataPropertyLabel = ( string.IsNullOrEmpty( name ) || name.Equals( Constants.EmptyPortValue ) ) ? InputDefaultNameStr : name;
  70. m_typeLocked = typeLocked;
  71. m_category = category;
  72. m_genType = genType;
  73. }
  74. public InputPort( int nodeId, int portId, WirePortDataType dataType, string name, string dataName, bool typeLocked, int orderId = -1, MasterNodePortCategory category = MasterNodePortCategory.Fragment, PortGenType genType = PortGenType.NonCustomLighting ) : base( nodeId, portId, dataType, name, orderId )
  75. {
  76. m_dataName = dataName;
  77. m_internalDataPropertyLabel = ( string.IsNullOrEmpty( name ) || name.Equals( Constants.EmptyPortValue ) ) ? InputDefaultNameStr : name;
  78. m_typeLocked = typeLocked;
  79. m_category = category;
  80. m_genType = genType;
  81. }
  82. public void SetExternalLink( int nodeId, int portId )
  83. {
  84. m_externalNodeLink = nodeId;
  85. m_externalPortLink = portId;
  86. }
  87. public override bool CheckValidType( WirePortDataType dataType )
  88. {
  89. if( m_typeLocked )
  90. return ( dataType == m_dataType );
  91. return base.CheckValidType( dataType );
  92. }
  93. public override void FullDeleteConnections()
  94. {
  95. UIUtils.DeleteConnection( true, m_nodeId, m_portId, true, true );
  96. }
  97. public override void NotifyExternalRefencesOnChange()
  98. {
  99. for( int i = 0; i < m_externalReferences.Count; i++ )
  100. {
  101. ParentNode node = UIUtils.GetNode( m_externalReferences[ i ].NodeId );
  102. if( node )
  103. {
  104. OutputPort port = node.GetOutputPortByUniqueId( m_externalReferences[ i ].PortId );
  105. port.UpdateInfoOnExternalConn( m_nodeId, m_portId, m_dataType );
  106. node.OnConnectedInputNodeChanges( m_externalReferences[ i ].PortId, m_nodeId, m_portId, m_name, m_dataType );
  107. }
  108. }
  109. }
  110. public void UpdatePreviewInternalData()
  111. {
  112. switch( m_dataType )
  113. {
  114. case WirePortDataType.INT: m_previewInternalInt = IntInternalData; break;
  115. case WirePortDataType.FLOAT: m_previewInternalFloat = FloatInternalData; break;
  116. case WirePortDataType.FLOAT2: m_previewInternalVec2 = Vector2InternalData; break;
  117. case WirePortDataType.FLOAT3: m_previewInternalVec3 = Vector3InternalData; break;
  118. case WirePortDataType.FLOAT4: m_previewInternalVec4 = Vector4InternalData; break;
  119. case WirePortDataType.COLOR: m_previewInternalColor = ColorInternalData; break;
  120. }
  121. }
  122. void UpdateVariablesFromInternalData()
  123. {
  124. string[] data = String.IsNullOrEmpty( m_internalData ) ? null : m_internalData.Split( IOUtils.VECTOR_SEPARATOR );
  125. bool reset = ( data == null || data.Length == 0 );
  126. m_internalDataUpdated = false;
  127. try
  128. {
  129. switch( m_dataType )
  130. {
  131. case WirePortDataType.OBJECT:break;
  132. case WirePortDataType.FLOAT: m_previewInternalFloat = reset ? 0 : Convert.ToSingle( data[ 0 ] ); break;
  133. case WirePortDataType.INT:
  134. {
  135. if( reset )
  136. {
  137. m_previewInternalInt = 0;
  138. }
  139. else
  140. {
  141. if( data[ 0 ].Contains( "." ) )
  142. {
  143. m_previewInternalInt = (int)Convert.ToSingle( data[ 0 ] );
  144. }
  145. else
  146. {
  147. m_previewInternalInt = Convert.ToInt32( data[ 0 ] );
  148. }
  149. }
  150. }
  151. break;
  152. case WirePortDataType.FLOAT2:
  153. {
  154. if( reset )
  155. {
  156. m_previewInternalVec2 = Vector2.zero;
  157. }
  158. else
  159. {
  160. if( data.Length < 2 )
  161. {
  162. m_previewInternalVec2.x = Convert.ToSingle( data[ 0 ] );
  163. m_previewInternalVec2.y = 0;
  164. }
  165. else
  166. {
  167. m_previewInternalVec2.x = Convert.ToSingle( data[ 0 ] );
  168. m_previewInternalVec2.y = Convert.ToSingle( data[ 1 ] );
  169. }
  170. }
  171. }
  172. break;
  173. case WirePortDataType.FLOAT3:
  174. {
  175. if( reset )
  176. {
  177. m_previewInternalVec3 = Vector3.zero;
  178. }
  179. else
  180. {
  181. int count = Mathf.Min( data.Length, 3 );
  182. for( int i = 0; i < count; i++ )
  183. {
  184. m_previewInternalVec3[ i ] = Convert.ToSingle( data[ i ] );
  185. }
  186. if( count < 3 )
  187. {
  188. for( int i = count; i < 3; i++ )
  189. {
  190. m_previewInternalVec3[ i ] = 0;
  191. }
  192. }
  193. }
  194. }
  195. break;
  196. case WirePortDataType.FLOAT4:
  197. {
  198. if( reset )
  199. {
  200. m_previewInternalVec4 = Vector4.zero;
  201. }
  202. else
  203. {
  204. int count = Mathf.Min( data.Length, 4 );
  205. for( int i = 0; i < count; i++ )
  206. {
  207. m_previewInternalVec4[ i ] = Convert.ToSingle( data[ i ] );
  208. }
  209. if( count < 4 )
  210. {
  211. for( int i = count; i < 4; i++ )
  212. {
  213. m_previewInternalVec4[ i ] = 0;
  214. }
  215. }
  216. }
  217. }
  218. break;
  219. case WirePortDataType.COLOR:
  220. {
  221. if( reset )
  222. {
  223. m_previewInternalColor = Color.black;
  224. }
  225. else
  226. {
  227. int count = Mathf.Min( data.Length, 4 );
  228. for( int i = 0; i < count; i++ )
  229. {
  230. m_previewInternalColor[ i ] = Convert.ToSingle( data[ i ] );
  231. }
  232. if( count < 4 )
  233. {
  234. for( int i = count; i < 4; i++ )
  235. {
  236. m_previewInternalColor[ i ] = 0;
  237. }
  238. }
  239. }
  240. }
  241. break;
  242. case WirePortDataType.FLOAT3x3:
  243. case WirePortDataType.FLOAT4x4:
  244. {
  245. if( reset )
  246. {
  247. m_previewInternalMatrix4x4 = Matrix4x4.identity;
  248. }
  249. else
  250. {
  251. int count = Mathf.Min( data.Length, 16 );
  252. int overallIdx = 0;
  253. for( int i = 0; i < 4; i++ )
  254. {
  255. for( int j = 0; j < 4; j++ )
  256. {
  257. if( overallIdx < count )
  258. {
  259. m_previewInternalMatrix4x4[ i, j ] = Convert.ToSingle( data[ overallIdx ] );
  260. }
  261. else
  262. {
  263. m_previewInternalMatrix4x4[ i, j ] = ( ( i == j ) ? 1 : 0 );
  264. }
  265. overallIdx++;
  266. }
  267. }
  268. }
  269. }
  270. break;
  271. }
  272. }
  273. catch( Exception e )
  274. {
  275. if( DebugConsoleWindow.DeveloperMode )
  276. Debug.LogException( e );
  277. }
  278. }
  279. void UpdateInternalDataFromVariables( bool forceDecimal = false )
  280. {
  281. switch( m_dataType )
  282. {
  283. case WirePortDataType.OBJECT:break;
  284. case WirePortDataType.FLOAT:
  285. {
  286. if( forceDecimal && m_previewInternalFloat == (int)m_previewInternalFloat )
  287. m_internalData = m_previewInternalFloat.ToString("0.0##############"); // to make sure integer values like 0 or 1 are generated as 0.0 and 1.0
  288. else
  289. m_internalData = m_previewInternalFloat.ToString();
  290. m_internalDataWrapper = string.Empty;
  291. }
  292. break;
  293. case WirePortDataType.INT:
  294. {
  295. m_internalData = m_previewInternalInt.ToString();
  296. m_internalDataWrapper = string.Empty;
  297. }
  298. break;
  299. case WirePortDataType.FLOAT2:
  300. {
  301. m_internalData = m_previewInternalVec2.x.ToString() + IOUtils.VECTOR_SEPARATOR +
  302. m_previewInternalVec2.y.ToString();
  303. m_internalDataWrapper = "float2( {0} )";
  304. }
  305. break;
  306. case WirePortDataType.FLOAT3:
  307. {
  308. m_internalData = m_previewInternalVec3.x.ToString() + IOUtils.VECTOR_SEPARATOR +
  309. m_previewInternalVec3.y.ToString() + IOUtils.VECTOR_SEPARATOR +
  310. m_previewInternalVec3.z.ToString();
  311. m_internalDataWrapper = "float3( {0} )";
  312. }
  313. break;
  314. case WirePortDataType.FLOAT4:
  315. {
  316. m_internalData = m_previewInternalVec4.x.ToString() + IOUtils.VECTOR_SEPARATOR +
  317. m_previewInternalVec4.y.ToString() + IOUtils.VECTOR_SEPARATOR +
  318. m_previewInternalVec4.z.ToString() + IOUtils.VECTOR_SEPARATOR +
  319. m_previewInternalVec4.w.ToString();
  320. m_internalDataWrapper = "float4( {0} )";
  321. }
  322. break;
  323. case WirePortDataType.COLOR:
  324. {
  325. m_internalData = m_previewInternalColor.r.ToString() + IOUtils.VECTOR_SEPARATOR +
  326. m_previewInternalColor.g.ToString() + IOUtils.VECTOR_SEPARATOR +
  327. m_previewInternalColor.b.ToString() + IOUtils.VECTOR_SEPARATOR +
  328. m_previewInternalColor.a.ToString();
  329. m_internalDataWrapper = "float4( {0} )";
  330. }
  331. break;
  332. case WirePortDataType.FLOAT3x3:
  333. {
  334. m_internalData = m_previewInternalMatrix4x4[ 0, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 0, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 0, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  335. m_previewInternalMatrix4x4[ 1, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 1, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 1, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  336. m_previewInternalMatrix4x4[ 2, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 2, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 2, 2 ].ToString();
  337. m_internalDataWrapper = "float3x3( {0} )";
  338. }
  339. break;
  340. case WirePortDataType.FLOAT4x4:
  341. {
  342. m_internalData = m_previewInternalMatrix4x4[ 0, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 0, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 0, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 0, 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  343. m_previewInternalMatrix4x4[ 1, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 1, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 1, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 1, 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  344. m_previewInternalMatrix4x4[ 2, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 2, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 2, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 2, 3 ].ToString() + IOUtils.VECTOR_SEPARATOR +
  345. m_previewInternalMatrix4x4[ 3, 0 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 3, 1 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 3, 2 ].ToString() + IOUtils.VECTOR_SEPARATOR + m_previewInternalMatrix4x4[ 3, 3 ].ToString();
  346. m_internalDataWrapper = "float4x4( {0} )";
  347. }
  348. break;
  349. }
  350. }
  351. //This gets the 3x3 matrix inside of the 4x4
  352. private string Matrix3x3WrappedData()
  353. {
  354. string tempInternal = string.Empty;
  355. string[] data = String.IsNullOrEmpty( m_internalData ) ? null : m_internalData.Split( IOUtils.VECTOR_SEPARATOR );
  356. if( data.Length == 16 )
  357. {
  358. int o = 0;
  359. for( int i = 0; i < 8; i++ )
  360. {
  361. if( i == 3 || i == 6 )
  362. o++;
  363. tempInternal += data[ i + o ] + IOUtils.VECTOR_SEPARATOR;
  364. }
  365. tempInternal += data[ 10 ];
  366. return String.Format( m_internalDataWrapper, tempInternal );
  367. }
  368. else
  369. {
  370. return String.Format( m_internalDataWrapper, m_internalData );
  371. }
  372. }
  373. private string SamplerWrappedData( ref MasterNodeDataCollector dataCollector )
  374. {
  375. m_internalData = "_Sampler" + PortId + UIUtils.GetNode( m_nodeId ).OutputId;
  376. dataCollector.AddToUniforms( m_nodeId, GeneratorUtils.GetPropertyDeclaraction( m_internalData, TextureType.Texture2D, ";" ) );
  377. return m_internalData;
  378. }
  379. //TODO: Replace GenerateShaderForOutput(...) calls to this one
  380. // This is a new similar method to GenerateShaderForOutput(...) which always autocasts
  381. public string GeneratePortInstructions( ref MasterNodeDataCollector dataCollector )
  382. {
  383. InputPort linkPort = ExternalLink;
  384. if( linkPort != null )
  385. {
  386. return linkPort.GeneratePortInstructions( ref dataCollector );
  387. }
  388. string result = string.Empty;
  389. if( m_externalReferences.Count > 0 && !m_locked )
  390. {
  391. result = UIUtils.GetNode( m_externalReferences[ 0 ].NodeId ).GenerateShaderForOutput( m_externalReferences[ 0 ].PortId, ref dataCollector, false );
  392. if( m_externalReferences[ 0 ].DataType != m_dataType )
  393. {
  394. result = UIUtils.CastPortType( ref dataCollector, UIUtils.GetNode( m_nodeId ).CurrentPrecisionType, null, m_externalReferences[ 0 ].DataType, m_dataType, result );
  395. }
  396. }
  397. else
  398. {
  399. UpdateInternalDataFromVariables( true );
  400. if( DataType == WirePortDataType.FLOAT3x3 )
  401. result = Matrix3x3WrappedData();
  402. else if( DataType == WirePortDataType.SAMPLER2D )
  403. result = SamplerWrappedData( ref dataCollector );
  404. else
  405. result = !String.IsNullOrEmpty( m_internalDataWrapper ) ? String.Format( m_internalDataWrapper, m_internalData ) : m_internalData;
  406. }
  407. return result;
  408. }
  409. public string GenerateShaderForOutput( ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  410. {
  411. InputPort linkPort = ExternalLink;
  412. if( linkPort != null )
  413. {
  414. return linkPort.GenerateShaderForOutput( ref dataCollector, ignoreLocalVar );
  415. }
  416. string result = string.Empty;
  417. if( m_externalReferences.Count > 0 && !m_locked )
  418. {
  419. result = UIUtils.GetNode( m_externalReferences[ 0 ].NodeId ).GenerateShaderForOutput( m_externalReferences[ 0 ].PortId, ref dataCollector, ignoreLocalVar );
  420. }
  421. else
  422. {
  423. UpdateInternalDataFromVariables( true );
  424. if( !String.IsNullOrEmpty( m_internalDataWrapper ) )
  425. {
  426. if( DataType == WirePortDataType.FLOAT3x3 )
  427. result = Matrix3x3WrappedData();
  428. else
  429. result = String.Format( m_internalDataWrapper, m_internalData );
  430. }
  431. else
  432. {
  433. result = m_internalData;
  434. }
  435. }
  436. return result;
  437. }
  438. public string GenerateShaderForOutput( ref MasterNodeDataCollector dataCollector, WirePortDataType inputPortType, bool ignoreLocalVar, bool autoCast = false )
  439. {
  440. InputPort linkPort = ExternalLink;
  441. if( linkPort != null )
  442. {
  443. return linkPort.GenerateShaderForOutput( ref dataCollector, inputPortType, ignoreLocalVar, autoCast );
  444. }
  445. string result = string.Empty;
  446. if( m_externalReferences.Count > 0 && !m_locked )
  447. {
  448. result = UIUtils.GetNode( m_externalReferences[ 0 ].NodeId ).GenerateShaderForOutput( m_externalReferences[ 0 ].PortId, ref dataCollector, ignoreLocalVar );
  449. if( autoCast && m_externalReferences[ 0 ].DataType != inputPortType )
  450. {
  451. result = UIUtils.CastPortType( ref dataCollector, UIUtils.GetNode( m_nodeId ).CurrentPrecisionType, null, m_externalReferences[ 0 ].DataType, inputPortType, result );
  452. }
  453. }
  454. else
  455. {
  456. UpdateInternalDataFromVariables( true );
  457. if( !String.IsNullOrEmpty( m_internalDataWrapper ) )
  458. {
  459. if( DataType == WirePortDataType.FLOAT3x3 )
  460. result = Matrix3x3WrappedData();
  461. else
  462. result = String.Format( m_internalDataWrapper, m_internalData );
  463. }
  464. else
  465. {
  466. result = m_internalData;
  467. }
  468. }
  469. return result;
  470. }
  471. public OutputPort GetOutputConnection( int connID = 0 )
  472. {
  473. if( connID < m_externalReferences.Count )
  474. {
  475. return UIUtils.GetNode( m_externalReferences[ connID ].NodeId ).GetOutputPortByUniqueId( m_externalReferences[ connID ].PortId );
  476. }
  477. return null;
  478. }
  479. public ParentNode GetOutputNodeWhichIsNotRelay( int connID = 0 )
  480. {
  481. if( connID < m_externalReferences.Count )
  482. {
  483. ParentNode node = UIUtils.GetNode( m_externalReferences[ connID ].NodeId );
  484. if( node is WireNode || node is RelayNode || node is FunctionInput )
  485. {
  486. return node.InputPorts[ 0 ].GetOutputNodeWhichIsNotRelay( connID );
  487. }
  488. return node;
  489. }
  490. return null;
  491. }
  492. public ParentNode GetOutputNode( int connID = 0 )
  493. {
  494. if( connID < m_externalReferences.Count )
  495. {
  496. return UIUtils.GetNode( m_externalReferences[ connID ].NodeId );
  497. }
  498. return null;
  499. }
  500. public bool TypeLocked
  501. {
  502. get { return m_typeLocked; }
  503. }
  504. public void WriteToString( ref string myString )
  505. {
  506. if( m_externalReferences.Count != 1 || m_isDummy )
  507. {
  508. return;
  509. }
  510. IOUtils.AddTypeToString( ref myString, IOUtils.WireConnectionParam );
  511. IOUtils.AddFieldValueToString( ref myString, m_nodeId );
  512. IOUtils.AddFieldValueToString( ref myString, m_portId );
  513. IOUtils.AddFieldValueToString( ref myString, m_externalReferences[ 0 ].NodeId );
  514. IOUtils.AddFieldValueToString( ref myString, m_externalReferences[ 0 ].PortId );
  515. IOUtils.AddLineTerminator( ref myString );
  516. }
  517. public void ShowInternalData( Rect rect, UndoParentNode owner, bool useCustomLabel = false, string customLabel = null )
  518. {
  519. string label = ( useCustomLabel == true && customLabel != null ) ? customLabel : m_internalDataPropertyLabel;
  520. switch( m_dataType )
  521. {
  522. case WirePortDataType.OBJECT:
  523. {
  524. InternalData = owner.EditorGUITextField( rect, label, InternalData );
  525. }
  526. break;
  527. case WirePortDataType.FLOAT:
  528. {
  529. FloatInternalData = owner.EditorGUIFloatField( rect, label, FloatInternalData );
  530. }
  531. break;
  532. case WirePortDataType.FLOAT2:
  533. {
  534. Vector2InternalData = owner.EditorGUIVector2Field( rect, label, Vector2InternalData );
  535. }
  536. break;
  537. case WirePortDataType.FLOAT3:
  538. {
  539. Vector3InternalData = owner.EditorGUIVector3Field( rect, label, Vector3InternalData );
  540. }
  541. break;
  542. case WirePortDataType.FLOAT4:
  543. {
  544. Vector4InternalData = owner.EditorGUIVector4Field( rect, label, Vector4InternalData );
  545. }
  546. break;
  547. case WirePortDataType.FLOAT3x3:
  548. {
  549. Matrix4x4 matrix = Matrix4x4InternalData;
  550. Vector3 currVec3 = Vector3.zero;
  551. for( int i = 0; i < 3; i++ )
  552. {
  553. Vector4 currVec = matrix.GetRow( i );
  554. currVec3.Set( currVec.x, currVec.y, currVec.z );
  555. EditorGUI.BeginChangeCheck();
  556. currVec3 = owner.EditorGUIVector3Field( rect, label + "[ " + i + " ]", currVec3 );
  557. rect.y += 2*EditorGUIUtility.singleLineHeight;
  558. if( EditorGUI.EndChangeCheck() )
  559. {
  560. currVec.Set( currVec3.x, currVec3.y, currVec3.z, currVec.w );
  561. matrix.SetRow( i, currVec );
  562. }
  563. }
  564. Matrix4x4InternalData = matrix;
  565. }
  566. break;
  567. case WirePortDataType.FLOAT4x4:
  568. {
  569. Matrix4x4 matrix = Matrix4x4InternalData;
  570. for( int i = 0; i < 4; i++ )
  571. {
  572. Vector4 currVec = matrix.GetRow( i );
  573. EditorGUI.BeginChangeCheck();
  574. currVec = owner.EditorGUIVector4Field( rect, label + "[ " + i + " ]", currVec );
  575. rect.y += 2*EditorGUIUtility.singleLineHeight;
  576. if( EditorGUI.EndChangeCheck() )
  577. {
  578. matrix.SetRow( i, currVec );
  579. }
  580. }
  581. Matrix4x4InternalData = matrix;
  582. }
  583. break;
  584. case WirePortDataType.COLOR:
  585. {
  586. ColorInternalData = owner.EditorGUIColorField( rect, label, ColorInternalData );
  587. }
  588. break;
  589. case WirePortDataType.INT:
  590. {
  591. IntInternalData = owner.EditorGUIIntField( rect, label, IntInternalData );
  592. }
  593. break;
  594. }
  595. }
  596. public void ShowInternalData( UndoParentNode owner, bool useCustomLabel = false, string customLabel = null )
  597. {
  598. string label = ( useCustomLabel == true && customLabel != null ) ? customLabel : m_internalDataPropertyLabel;
  599. switch( m_dataType )
  600. {
  601. case WirePortDataType.OBJECT:
  602. case WirePortDataType.FLOAT:
  603. {
  604. FloatInternalData = owner.EditorGUILayoutFloatField( label, FloatInternalData );
  605. }
  606. break;
  607. case WirePortDataType.FLOAT2:
  608. {
  609. Vector2InternalData = owner.EditorGUILayoutVector2Field( label, Vector2InternalData );
  610. }
  611. break;
  612. case WirePortDataType.FLOAT3:
  613. {
  614. Vector3InternalData = owner.EditorGUILayoutVector3Field( label, Vector3InternalData );
  615. }
  616. break;
  617. case WirePortDataType.FLOAT4:
  618. {
  619. Vector4InternalData = owner.EditorGUILayoutVector4Field( label, Vector4InternalData );
  620. }
  621. break;
  622. case WirePortDataType.FLOAT3x3:
  623. {
  624. Matrix4x4 matrix = Matrix4x4InternalData;
  625. Vector3 currVec3 = Vector3.zero;
  626. for( int i = 0; i < 3; i++ )
  627. {
  628. Vector4 currVec = matrix.GetRow( i );
  629. currVec3.Set( currVec.x, currVec.y, currVec.z );
  630. EditorGUI.BeginChangeCheck();
  631. currVec3 = owner.EditorGUILayoutVector3Field( label + "[ " + i + " ]", currVec3 );
  632. if( EditorGUI.EndChangeCheck() )
  633. {
  634. currVec.Set( currVec3.x, currVec3.y, currVec3.z, currVec.w );
  635. matrix.SetRow( i, currVec );
  636. }
  637. }
  638. Matrix4x4InternalData = matrix;
  639. }
  640. break;
  641. case WirePortDataType.FLOAT4x4:
  642. {
  643. Matrix4x4 matrix = Matrix4x4InternalData;
  644. for( int i = 0; i < 4; i++ )
  645. {
  646. Vector4 currVec = matrix.GetRow( i );
  647. EditorGUI.BeginChangeCheck();
  648. currVec = owner.EditorGUILayoutVector4Field( label + "[ " + i + " ]", currVec );
  649. if( EditorGUI.EndChangeCheck() )
  650. {
  651. matrix.SetRow( i, currVec );
  652. }
  653. }
  654. Matrix4x4InternalData = matrix;
  655. }
  656. break;
  657. case WirePortDataType.COLOR:
  658. {
  659. ColorInternalData = owner.EditorGUILayoutColorField( label, ColorInternalData );
  660. }
  661. break;
  662. case WirePortDataType.INT:
  663. {
  664. IntInternalData = owner.EditorGUILayoutIntField( label, IntInternalData );
  665. }
  666. break;
  667. }
  668. }
  669. public bool IsZeroInternalData
  670. {
  671. get
  672. {
  673. switch( m_dataType )
  674. {
  675. case WirePortDataType.FLOAT: return Mathf.Abs(m_previewInternalFloat) < 0.001f;
  676. case WirePortDataType.UINT:
  677. case WirePortDataType.INT: return m_previewInternalInt == 0;
  678. case WirePortDataType.FLOAT2:
  679. return (Mathf.Abs( m_previewInternalVec2.x ) < 0.001f &&
  680. Mathf.Abs( m_previewInternalVec2.y ) < 0.001f);
  681. case WirePortDataType.FLOAT3:
  682. return (Mathf.Abs( m_previewInternalVec3.x ) < 0.001f &&
  683. Mathf.Abs( m_previewInternalVec3.y ) < 0.001f &&
  684. Mathf.Abs( m_previewInternalVec3.z ) < 0.001f );
  685. case WirePortDataType.UINT4:
  686. case WirePortDataType.FLOAT4:
  687. return (Mathf.Abs( m_previewInternalVec4.x ) < 0.001f &&
  688. Mathf.Abs( m_previewInternalVec4.y ) < 0.001f &&
  689. Mathf.Abs( m_previewInternalVec4.z ) < 0.001f &&
  690. Mathf.Abs( m_previewInternalVec4.w ) < 0.001f );
  691. case WirePortDataType.COLOR:
  692. return (Mathf.Abs( m_previewInternalColor.r ) < 0.001f &&
  693. Mathf.Abs( m_previewInternalColor.g ) < 0.001f &&
  694. Mathf.Abs( m_previewInternalColor.b ) < 0.001f &&
  695. Mathf.Abs( m_previewInternalColor.a ) < 0.001f);
  696. }
  697. return true;
  698. }
  699. }
  700. public float FloatInternalData
  701. {
  702. set { m_previewInternalFloat = value; m_internalDataUpdated = false; }
  703. get { return m_previewInternalFloat; }
  704. }
  705. public int IntInternalData
  706. {
  707. set { m_previewInternalInt = value; m_internalDataUpdated = false; }
  708. get { return m_previewInternalInt; }
  709. }
  710. public Vector2 Vector2InternalData
  711. {
  712. set { m_previewInternalVec2 = value; m_internalDataUpdated = false; }
  713. get { return m_previewInternalVec2; }
  714. }
  715. public Vector3 Vector3InternalData
  716. {
  717. set { m_previewInternalVec3 = value; m_internalDataUpdated = false; }
  718. get { return m_previewInternalVec3; }
  719. }
  720. public Vector4 Vector4InternalData
  721. {
  722. set { m_previewInternalVec4 = value; m_internalDataUpdated = false; }
  723. get { return m_previewInternalVec4; }
  724. }
  725. public Color ColorInternalData
  726. {
  727. set { m_previewInternalColor = value; m_internalDataUpdated = false; }
  728. get { return m_previewInternalColor; }
  729. }
  730. public Matrix4x4 Matrix4x4InternalData
  731. {
  732. set { m_previewInternalMatrix4x4 = value; m_internalDataUpdated = false; }
  733. get { return m_previewInternalMatrix4x4; }
  734. }
  735. public string SamplerInternalData
  736. {
  737. set { InternalData = UIUtils.RemoveInvalidCharacters( value ); m_internalDataUpdated = false; }
  738. get { return m_internalData; }
  739. }
  740. public override void ForceClearConnection()
  741. {
  742. UIUtils.DeleteConnection( true, m_nodeId, m_portId, false, true );
  743. }
  744. private bool m_internalDataUpdated = false;
  745. private string m_displayInternalData = string.Empty;
  746. public string DisplayInternalData
  747. {
  748. get
  749. {
  750. if( !m_internalDataUpdated )
  751. {
  752. UpdateInternalDataFromVariables();
  753. m_internalDataUpdated = true;
  754. m_displayInternalData = "( "+ m_internalData + " )";
  755. }
  756. return m_displayInternalData;
  757. }
  758. }
  759. public string InternalData
  760. {
  761. get
  762. {
  763. UpdateInternalDataFromVariables();
  764. return m_internalData;
  765. }
  766. set
  767. {
  768. m_internalData = value;
  769. UpdateVariablesFromInternalData();
  770. }
  771. }
  772. public string WrappedInternalData
  773. {
  774. get
  775. {
  776. UpdateInternalDataFromVariables();
  777. return string.IsNullOrEmpty( m_internalDataWrapper ) ? m_internalData : String.Format( m_internalDataWrapper, m_internalData );
  778. }
  779. }
  780. public override WirePortDataType DataType
  781. {
  782. get { return base.DataType; }
  783. // must be set to update internal data. do not delete
  784. set
  785. {
  786. m_internalDataUpdated = false;
  787. switch( DataType )
  788. {
  789. case WirePortDataType.FLOAT:
  790. {
  791. switch( value )
  792. {
  793. case WirePortDataType.FLOAT2: m_previewInternalVec2.x = m_previewInternalFloat; break;
  794. case WirePortDataType.FLOAT3: m_previewInternalVec3.x = m_previewInternalFloat; break;
  795. case WirePortDataType.FLOAT4: m_previewInternalVec4.x = m_previewInternalFloat; break;
  796. case WirePortDataType.FLOAT3x3:
  797. case WirePortDataType.FLOAT4x4:
  798. {
  799. // @diogo: not applicable
  800. break;
  801. }
  802. case WirePortDataType.COLOR: m_previewInternalColor.r = m_previewInternalFloat; break;
  803. case WirePortDataType.INT: m_previewInternalInt = (int)m_previewInternalFloat; break;
  804. }
  805. break;
  806. }
  807. case WirePortDataType.FLOAT2:
  808. {
  809. switch( value )
  810. {
  811. case WirePortDataType.FLOAT: m_previewInternalFloat = m_previewInternalVec2.x; break;
  812. case WirePortDataType.FLOAT3:
  813. {
  814. m_previewInternalVec3.x = m_previewInternalVec2.x;
  815. m_previewInternalVec3.y = m_previewInternalVec2.y;
  816. break;
  817. }
  818. case WirePortDataType.FLOAT4:
  819. {
  820. m_previewInternalVec4.x = m_previewInternalVec2.x;
  821. m_previewInternalVec4.y = m_previewInternalVec2.y;
  822. break;
  823. }
  824. case WirePortDataType.FLOAT3x3:
  825. case WirePortDataType.FLOAT4x4:
  826. {
  827. // @diogo: not applicable
  828. break;
  829. }
  830. case WirePortDataType.COLOR:
  831. {
  832. m_previewInternalColor.r = m_previewInternalVec2.x;
  833. m_previewInternalColor.g = m_previewInternalVec2.y;
  834. break;
  835. }
  836. case WirePortDataType.INT: m_previewInternalInt = (int)m_previewInternalVec2.x; break;
  837. }
  838. break;
  839. }
  840. case WirePortDataType.FLOAT3:
  841. {
  842. switch( value )
  843. {
  844. case WirePortDataType.FLOAT: m_previewInternalFloat = m_previewInternalVec3.x; break;
  845. case WirePortDataType.FLOAT2:
  846. {
  847. m_previewInternalVec2.x = m_previewInternalVec3.x;
  848. m_previewInternalVec2.y = m_previewInternalVec3.y;
  849. break;
  850. }
  851. case WirePortDataType.FLOAT4:
  852. {
  853. m_previewInternalVec4.x = m_previewInternalVec3.x;
  854. m_previewInternalVec4.y = m_previewInternalVec3.y;
  855. m_previewInternalVec4.z = m_previewInternalVec3.z;
  856. break;
  857. }
  858. case WirePortDataType.FLOAT3x3:
  859. case WirePortDataType.FLOAT4x4:
  860. {
  861. // @diogo: not applicable
  862. break;
  863. }
  864. case WirePortDataType.COLOR:
  865. {
  866. m_previewInternalColor.r = m_previewInternalVec3.x;
  867. m_previewInternalColor.g = m_previewInternalVec3.y;
  868. m_previewInternalColor.b = m_previewInternalVec3.z;
  869. break;
  870. }
  871. case WirePortDataType.INT: m_previewInternalInt = (int)m_previewInternalVec3.x; break;
  872. }
  873. break;
  874. }
  875. case WirePortDataType.FLOAT4:
  876. {
  877. switch( value )
  878. {
  879. case WirePortDataType.FLOAT: m_previewInternalFloat = m_previewInternalVec4.x; break;
  880. case WirePortDataType.FLOAT2:
  881. {
  882. m_previewInternalVec2.x = m_previewInternalVec4.x;
  883. m_previewInternalVec2.y = m_previewInternalVec4.y;
  884. break;
  885. }
  886. case WirePortDataType.FLOAT3:
  887. {
  888. m_previewInternalVec3.x = m_previewInternalVec4.x;
  889. m_previewInternalVec3.y = m_previewInternalVec4.y;
  890. m_previewInternalVec3.z = m_previewInternalVec4.z;
  891. break;
  892. }
  893. case WirePortDataType.FLOAT3x3:
  894. case WirePortDataType.FLOAT4x4:
  895. {
  896. // @diogo: not applicable
  897. break;
  898. }
  899. case WirePortDataType.COLOR:
  900. {
  901. m_previewInternalColor.r = m_previewInternalVec4.x;
  902. m_previewInternalColor.g = m_previewInternalVec4.y;
  903. m_previewInternalColor.b = m_previewInternalVec4.z;
  904. m_previewInternalColor.a = m_previewInternalVec4.w;
  905. break;
  906. }
  907. case WirePortDataType.INT: m_previewInternalInt = (int)m_previewInternalVec4.x; break;
  908. }
  909. break;
  910. }
  911. case WirePortDataType.FLOAT3x3:
  912. case WirePortDataType.FLOAT4x4:
  913. {
  914. // @diogo: not applicable
  915. break;
  916. }
  917. case WirePortDataType.COLOR:
  918. {
  919. switch( value )
  920. {
  921. case WirePortDataType.FLOAT: m_previewInternalFloat = m_previewInternalColor.r; break;
  922. case WirePortDataType.FLOAT2:
  923. {
  924. m_previewInternalVec2.x = m_previewInternalColor.r;
  925. m_previewInternalVec2.y = m_previewInternalColor.g;
  926. break;
  927. }
  928. case WirePortDataType.FLOAT3:
  929. {
  930. m_previewInternalVec3.x = m_previewInternalColor.r;
  931. m_previewInternalVec3.y = m_previewInternalColor.g;
  932. m_previewInternalVec3.z = m_previewInternalColor.b;
  933. break;
  934. }
  935. case WirePortDataType.FLOAT4:
  936. {
  937. m_previewInternalVec4.x = m_previewInternalColor.r;
  938. m_previewInternalVec4.y = m_previewInternalColor.g;
  939. m_previewInternalVec4.z = m_previewInternalColor.b;
  940. m_previewInternalVec4.w = m_previewInternalColor.a;
  941. break;
  942. }
  943. case WirePortDataType.FLOAT3x3:
  944. case WirePortDataType.FLOAT4x4:
  945. {
  946. // @diogo: not applicable
  947. break;
  948. }
  949. case WirePortDataType.INT: m_previewInternalInt = (int)m_previewInternalColor.r; break;
  950. }
  951. break;
  952. }
  953. case WirePortDataType.INT:
  954. {
  955. switch( value )
  956. {
  957. case WirePortDataType.FLOAT: m_previewInternalFloat = m_previewInternalInt; break;
  958. case WirePortDataType.FLOAT2: m_previewInternalVec2.x = m_previewInternalInt; break;
  959. case WirePortDataType.FLOAT3: m_previewInternalVec3.x = m_previewInternalInt; break;
  960. case WirePortDataType.FLOAT4: m_previewInternalVec4.x = m_previewInternalInt; break;
  961. case WirePortDataType.FLOAT3x3:
  962. case WirePortDataType.FLOAT4x4:
  963. {
  964. // @diogo: not applicable
  965. break;
  966. }
  967. case WirePortDataType.COLOR: m_previewInternalColor.r = m_previewInternalInt; break;
  968. }
  969. break;
  970. }
  971. }
  972. base.DataType = value;
  973. }
  974. }
  975. public string DataName
  976. {
  977. get { return m_dataName; }
  978. set { m_dataName = value; }
  979. }
  980. public bool IsFragment { get { return m_category == MasterNodePortCategory.Fragment || m_category == MasterNodePortCategory.Debug; } }
  981. public MasterNodePortCategory Category
  982. {
  983. set { m_category = value; }
  984. get { return m_category; }
  985. }
  986. private int CachedIntPropertyID
  987. {
  988. get
  989. {
  990. if( m_cachedIntShaderID == -1 )
  991. m_cachedIntShaderID = Shader.PropertyToID( "_InputInt" );
  992. return m_cachedIntShaderID;
  993. }
  994. }
  995. private int CachedFloatPropertyID
  996. {
  997. get
  998. {
  999. if( m_cachedFloatShaderID == -1 )
  1000. m_cachedFloatShaderID = Shader.PropertyToID( "_InputFloat" );
  1001. return m_cachedFloatShaderID;
  1002. }
  1003. }
  1004. private int CachedVectorPropertyID
  1005. {
  1006. get
  1007. {
  1008. if( m_cachedVectorShaderID == -1 )
  1009. m_cachedVectorShaderID = Shader.PropertyToID( "_InputVector" );
  1010. return m_cachedVectorShaderID;
  1011. }
  1012. }
  1013. private int CachedColorPropertyID
  1014. {
  1015. get
  1016. {
  1017. if( m_cachedColorShaderID == -1 )
  1018. m_cachedColorShaderID = Shader.PropertyToID( "_InputColor" );
  1019. return m_cachedColorShaderID;
  1020. }
  1021. }
  1022. private int CachedDefaultTexPropertyID
  1023. {
  1024. get
  1025. {
  1026. if( m_cachedDefaultTexShaderID == -1 )
  1027. m_cachedDefaultTexShaderID = Shader.PropertyToID( "_Default" );
  1028. return m_cachedDefaultTexShaderID;
  1029. }
  1030. }
  1031. private int Cached2DPropertyID
  1032. {
  1033. get
  1034. {
  1035. if( m_cached2DShaderID == -1 )
  1036. m_cached2DShaderID = Shader.PropertyToID( "_Input2D" );
  1037. return m_cached2DShaderID;
  1038. }
  1039. }
  1040. public int CachedPropertyId
  1041. {
  1042. get { return m_cachedPropertyId; }
  1043. }
  1044. public bool InputNodeHasPreview( ParentGraph container )
  1045. {
  1046. ParentNode node = null;
  1047. if( m_externalReferences.Count > 0)
  1048. {
  1049. node = container.GetNode( m_externalReferences[ 0 ].NodeId );
  1050. }
  1051. if( node != null )
  1052. return node.HasPreviewShader;
  1053. return false;
  1054. }
  1055. public void PreparePortCacheID()
  1056. {
  1057. if( m_propertyNameInt != PortId || string.IsNullOrEmpty( m_propertyName ) )
  1058. {
  1059. m_propertyNameInt = PortId;
  1060. m_propertyName = "_" + Convert.ToChar( PortId + 65 );
  1061. m_cachedPropertyId = Shader.PropertyToID( m_propertyName );
  1062. }
  1063. if( m_cachedPropertyId == -1 )
  1064. m_cachedPropertyId = Shader.PropertyToID( m_propertyName );
  1065. }
  1066. public void SetPreviewInputTexture( ParentGraph container )
  1067. {
  1068. PreparePortCacheID();
  1069. if( (object)m_node == null )
  1070. {
  1071. m_node = container.GetNode( NodeId );
  1072. //m_node = UIUtils.GetNode( NodeId );
  1073. }
  1074. if( ExternalReferences.Count>0 )
  1075. {
  1076. m_node.PreviewMaterial.SetTexture( m_cachedPropertyId, container.GetNode( ExternalReferences[ 0 ].NodeId ).GetOutputPortByUniqueId( ExternalReferences[ 0 ].PortId ).OutputPreviewTexture );
  1077. }
  1078. //m_node.PreviewMaterial.SetTexture( m_cachedPropertyId, GetOutputConnection( 0 ).OutputPreviewTexture );
  1079. }
  1080. private void SetPortPreviewShader( Shader portShader )
  1081. {
  1082. if( m_inputPreviewShader != portShader )
  1083. {
  1084. m_inputPreviewShader = portShader;
  1085. InputPreviewMaterial.shader = portShader;
  1086. }
  1087. }
  1088. public void SetPreviewInputValue( ParentGraph container )
  1089. {
  1090. if( m_inputPreviewTexture == null )
  1091. {
  1092. m_inputPreviewTexture = new RenderTexture( Constants.PreviewSize , Constants.PreviewSize , 0, Constants.PreviewFormat , RenderTextureReadWrite.Linear );
  1093. m_inputPreviewTexture.wrapMode = TextureWrapMode.Repeat;
  1094. }
  1095. switch( DataType )
  1096. {
  1097. case WirePortDataType.INT:
  1098. {
  1099. SetPortPreviewShader( UIUtils.IntShader );
  1100. InputPreviewMaterial.SetInt( CachedIntPropertyID, m_previewInternalInt );
  1101. }
  1102. break;
  1103. case WirePortDataType.FLOAT:
  1104. {
  1105. SetPortPreviewShader( UIUtils.FloatShader );
  1106. //Debug.Log( m_previewInternalFloat );
  1107. InputPreviewMaterial.SetFloat( CachedFloatPropertyID, m_previewInternalFloat );
  1108. }
  1109. break;
  1110. case WirePortDataType.FLOAT2:
  1111. {
  1112. SetPortPreviewShader( UIUtils.Vector2Shader );
  1113. Vector2 v2 = m_previewInternalVec2;// Vector2InternalData;
  1114. InputPreviewMaterial.SetVector( CachedVectorPropertyID, new Vector4( v2.x, v2.y, 0, 0 ) );
  1115. }
  1116. break;
  1117. case WirePortDataType.FLOAT3:
  1118. {
  1119. SetPortPreviewShader( UIUtils.Vector3Shader );
  1120. Vector3 v3 = m_previewInternalVec3;// Vector3InternalData;
  1121. InputPreviewMaterial.SetVector( CachedVectorPropertyID, new Vector4( v3.x, v3.y, v3.z, 0 ) );
  1122. }
  1123. break;
  1124. case WirePortDataType.FLOAT4:
  1125. {
  1126. SetPortPreviewShader( UIUtils.Vector4Shader );
  1127. InputPreviewMaterial.SetVector( CachedVectorPropertyID, m_previewInternalVec4 );
  1128. }
  1129. break;
  1130. case WirePortDataType.COLOR:
  1131. {
  1132. SetPortPreviewShader( UIUtils.ColorShader );
  1133. InputPreviewMaterial.SetColor( CachedColorPropertyID, m_previewInternalColor );
  1134. }
  1135. break;
  1136. case WirePortDataType.FLOAT3x3:
  1137. case WirePortDataType.FLOAT4x4:
  1138. {
  1139. SetPortPreviewShader( UIUtils.FloatShader );
  1140. InputPreviewMaterial.SetFloat( CachedFloatPropertyID, 1 );
  1141. }
  1142. break;
  1143. case WirePortDataType.SAMPLER2D:
  1144. {
  1145. SetPortPreviewShader( UIUtils.Texture2DShader );
  1146. }
  1147. break;
  1148. default:
  1149. {
  1150. SetPortPreviewShader( UIUtils.FloatShader );
  1151. InputPreviewMaterial.SetFloat( CachedFloatPropertyID, 0 );
  1152. }
  1153. break;
  1154. }
  1155. RenderTexture temp = RenderTexture.active;
  1156. RenderTexture.active = m_inputPreviewTexture;
  1157. Graphics.Blit( null, m_inputPreviewTexture, InputPreviewMaterial );
  1158. RenderTexture.active = temp;
  1159. PreparePortCacheID();
  1160. //if( (object)m_node == null )
  1161. // m_node = UIUtils.GetNode( NodeId );
  1162. if( (object)m_node == null )
  1163. {
  1164. m_node = container.GetNode( NodeId );
  1165. //m_node = UIUtils.GetNode( NodeId );
  1166. }
  1167. //m_propertyName = "_A";
  1168. //Debug.Log( m_propertyName );
  1169. m_node.PreviewMaterial.SetTexture( m_propertyName, m_inputPreviewTexture );
  1170. }
  1171. public override void ChangePortId( int newPortId )
  1172. {
  1173. if( IsConnected )
  1174. {
  1175. int count = ExternalReferences.Count;
  1176. for( int connIdx = 0; connIdx < count; connIdx++ )
  1177. {
  1178. int nodeId = ExternalReferences[ connIdx ].NodeId;
  1179. int portId = ExternalReferences[ connIdx ].PortId;
  1180. ParentNode node = UIUtils.GetNode( nodeId );
  1181. if( node != null )
  1182. {
  1183. OutputPort outputPort = node.GetOutputPortByUniqueId( portId );
  1184. int outputCount = outputPort.ExternalReferences.Count;
  1185. for( int j = 0; j < outputCount; j++ )
  1186. {
  1187. if( outputPort.ExternalReferences[ j ].NodeId == NodeId &&
  1188. outputPort.ExternalReferences[ j ].PortId == PortId )
  1189. {
  1190. outputPort.ExternalReferences[ j ].PortId = newPortId;
  1191. }
  1192. }
  1193. }
  1194. }
  1195. }
  1196. PortId = newPortId;
  1197. }
  1198. public override void Destroy()
  1199. {
  1200. base.Destroy();
  1201. //if ( m_inputPreview != null )
  1202. // UnityEngine.ScriptableObject.DestroyImmediate( m_inputPreview );
  1203. //m_inputPreview = null;
  1204. if( m_inputPreviewTexture != null )
  1205. {
  1206. m_inputPreviewTexture.Release();
  1207. UnityEngine.ScriptableObject.DestroyImmediate( m_inputPreviewTexture );
  1208. }
  1209. m_inputPreviewTexture = null;
  1210. if( m_inputPreviewMaterial != null )
  1211. UnityEngine.ScriptableObject.DestroyImmediate( m_inputPreviewMaterial );
  1212. m_inputPreviewMaterial = null;
  1213. m_inputPreviewShader = null;
  1214. m_node = null;
  1215. }
  1216. public Shader InputPreviewShader
  1217. {
  1218. get
  1219. {
  1220. if( m_inputPreviewShader == null )
  1221. m_inputPreviewShader = AssetDatabase.LoadAssetAtPath<Shader>( AssetDatabase.GUIDToAssetPath( "d9ca47581ac157145bff6f72ac5dd73e" ) ); //ranged float
  1222. if( m_inputPreviewShader == null )
  1223. m_inputPreviewShader = Shader.Find( "Unlit/Colored Transparent" );
  1224. return m_inputPreviewShader;
  1225. }
  1226. set
  1227. {
  1228. m_inputPreviewShader = value;
  1229. }
  1230. }
  1231. public Material InputPreviewMaterial
  1232. {
  1233. get
  1234. {
  1235. if( m_inputPreviewMaterial == null )
  1236. m_inputPreviewMaterial = new Material( InputPreviewShader );
  1237. return m_inputPreviewMaterial;
  1238. }
  1239. //set
  1240. //{
  1241. // m_inputPreviewMaterial = value;
  1242. //}
  1243. }
  1244. public override string Name
  1245. {
  1246. get { return m_name; }
  1247. set
  1248. {
  1249. m_name = value;
  1250. m_internalDataPropertyLabel = ( string.IsNullOrEmpty( value ) || value.Equals( Constants.EmptyPortValue ) ) ? InputDefaultNameStr : value;
  1251. m_dirtyLabelSize = true;
  1252. }
  1253. }
  1254. public string InternalDataName
  1255. {
  1256. get { return m_internalDataPropertyLabel; }
  1257. set { m_internalDataPropertyLabel = value; }
  1258. }
  1259. public bool AutoDrawInternalData
  1260. {
  1261. get { return m_drawInternalData; }
  1262. set { m_drawInternalData = value; }
  1263. }
  1264. public PortGenType GenType
  1265. {
  1266. get { return m_genType; }
  1267. set { m_genType = value; }
  1268. }
  1269. public bool ValidInternalData
  1270. {
  1271. get
  1272. {
  1273. switch( m_dataType )
  1274. {
  1275. case WirePortDataType.FLOAT:
  1276. case WirePortDataType.FLOAT2:
  1277. case WirePortDataType.FLOAT3:
  1278. case WirePortDataType.FLOAT4:
  1279. case WirePortDataType.FLOAT3x3:
  1280. case WirePortDataType.FLOAT4x4:
  1281. case WirePortDataType.COLOR:
  1282. case WirePortDataType.INT: return true;
  1283. case WirePortDataType.OBJECT:
  1284. case WirePortDataType.SAMPLER1D:
  1285. case WirePortDataType.SAMPLER2D:
  1286. case WirePortDataType.SAMPLER3D:
  1287. case WirePortDataType.SAMPLERCUBE:
  1288. case WirePortDataType.SAMPLER2DARRAY:
  1289. case WirePortDataType.SAMPLERSTATE:
  1290. default: return false;
  1291. }
  1292. }
  1293. }
  1294. //public RenderTexture InputPreviewTexture
  1295. //{
  1296. // get
  1297. // {
  1298. // if( IsConnected )
  1299. // return GetOutputConnection( 0 ).OutputPreviewTexture;
  1300. // else
  1301. // return m_inputPreviewTexture;
  1302. // }
  1303. //}
  1304. public RenderTexture InputPreviewTexture( ParentGraph container )
  1305. {
  1306. if( IsConnected )
  1307. {
  1308. if( m_externalReferences.Count > 0 )
  1309. return container.GetNode( m_externalReferences[ 0 ].NodeId ).GetOutputPortByUniqueId( m_externalReferences[ 0 ].PortId ).OutputPreviewTexture;
  1310. else
  1311. return null;
  1312. }
  1313. else
  1314. {
  1315. return m_inputPreviewTexture;
  1316. }
  1317. }
  1318. public string ExternalLinkId
  1319. {
  1320. get { return m_externalLinkId; }
  1321. set
  1322. {
  1323. m_externalLinkId = value;
  1324. if( string.IsNullOrEmpty( value ) )
  1325. {
  1326. m_externalNodeLink = -1;
  1327. m_externalPortLink = -1;
  1328. }
  1329. }
  1330. }
  1331. public bool HasOwnOrLinkConnection { get { return IsConnected || HasConnectedExternalLink; } }
  1332. public bool HasExternalLink { get { return m_externalNodeLink > -1 && m_externalPortLink > -1; } }
  1333. public bool HasConnectedExternalLink
  1334. {
  1335. get
  1336. {
  1337. InputPort link = ExternalLink;
  1338. return ( link != null && link.IsConnected );
  1339. }
  1340. }
  1341. public InputPort ExternalLink
  1342. {
  1343. get
  1344. {
  1345. if( HasExternalLink )
  1346. {
  1347. ParentNode linkNode = UIUtils.GetNode( m_externalNodeLink );
  1348. if( linkNode != null )
  1349. {
  1350. return linkNode.GetInputPortByUniqueId( m_externalPortLink );
  1351. }
  1352. }
  1353. return null;
  1354. }
  1355. }
  1356. public ParentNode ExternalLinkNode
  1357. {
  1358. get
  1359. {
  1360. if( HasExternalLink )
  1361. {
  1362. return UIUtils.GetNode( m_externalNodeLink );
  1363. }
  1364. return null;
  1365. }
  1366. }
  1367. }
  1368. }