Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4001 lines
117 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. using System.Collections.Generic;
  7. namespace AmplifyShaderEditor
  8. {
  9. [Serializable]
  10. public class ParentGraph : ScriptableObject, ISerializationCallbackReceiver
  11. {
  12. private const int MasterNodeLODIncrement = 100;
  13. private const int MaxLodAmount = 9;
  14. public enum NodeLOD
  15. {
  16. LOD0,
  17. LOD1,
  18. LOD2,
  19. LOD3,
  20. LOD4,
  21. LOD5
  22. }
  23. [SerializeField]
  24. private bool m_samplingThroughMacros = false;
  25. private NodeLOD m_lodLevel = NodeLOD.LOD0;
  26. private GUIStyle nodeStyleOff;
  27. private GUIStyle nodeStyleOn;
  28. private GUIStyle nodeTitle;
  29. private GUIStyle commentaryBackground;
  30. public delegate void LODMasterNodesAdded( int lod );
  31. public event LODMasterNodesAdded OnLODMasterNodesAddedEvent;
  32. public delegate void EmptyGraphDetected( ParentGraph graph );
  33. public event EmptyGraphDetected OnEmptyGraphDetectedEvt;
  34. public delegate void NodeEvent( ParentNode node );
  35. public event NodeEvent OnNodeEvent = null;
  36. public event NodeEvent OnNodeRemovedEvent;
  37. public delegate void DuplicateEvent();
  38. public event DuplicateEvent OnDuplicateEvent;
  39. public event MasterNode.OnMaterialUpdated OnMaterialUpdatedEvent;
  40. public event MasterNode.OnMaterialUpdated OnShaderUpdatedEvent;
  41. private bool m_afterDeserializeFlag = true;
  42. private bool m_lateOptionsRefresh = false;
  43. private bool m_foundDuplicates = false;
  44. //[SerializeField]
  45. private AmplifyShaderEditorWindow m_parentWindow = null;
  46. [SerializeField]
  47. private int m_validNodeId;
  48. [SerializeField]
  49. private List<ParentNode> m_nodes = new List<ParentNode>();
  50. // Sampler Nodes registry
  51. [SerializeField]
  52. private UsageListSamplerNodes m_samplerNodes = new UsageListSamplerNodes();
  53. [SerializeField]
  54. private UsageListFloatIntNodes m_floatNodes = new UsageListFloatIntNodes();
  55. [SerializeField]
  56. private UsageListTexturePropertyNodes m_texturePropertyNodes = new UsageListTexturePropertyNodes();
  57. [SerializeField]
  58. private UsageListTextureArrayNodes m_textureArrayNodes = new UsageListTextureArrayNodes();
  59. [SerializeField]
  60. private UsageListPropertyNodes m_propertyNodes = new UsageListPropertyNodes();
  61. [SerializeField]
  62. private UsageListPropertyNodes m_rawPropertyNodes = new UsageListPropertyNodes();
  63. [SerializeField]
  64. private UsageListScreenColorNodes m_screenColorNodes = new UsageListScreenColorNodes();
  65. [SerializeField]
  66. private UsageListRegisterLocalVarNodes m_localVarNodes = new UsageListRegisterLocalVarNodes();
  67. [SerializeField]
  68. private UsageListGlobalArrayNodes m_globalArrayNodes = new UsageListGlobalArrayNodes();
  69. [SerializeField]
  70. private UsageListFunctionInputNodes m_functionInputNodes = new UsageListFunctionInputNodes();
  71. [SerializeField]
  72. private UsageListFunctionNodes m_functionNodes = new UsageListFunctionNodes();
  73. [SerializeField]
  74. private UsageListFunctionOutputNodes m_functionOutputNodes = new UsageListFunctionOutputNodes();
  75. [SerializeField]
  76. private UsageListFunctionSwitchNodes m_functionSwitchNodes = new UsageListFunctionSwitchNodes();
  77. [SerializeField]
  78. private UsageListFunctionSwitchCopyNodes m_functionSwitchCopyNodes = new UsageListFunctionSwitchCopyNodes();
  79. [SerializeField]
  80. private UsageListTemplateMultiPassMasterNodes m_multiPassMasterNodes = new UsageListTemplateMultiPassMasterNodes();
  81. [SerializeField]
  82. private List<UsageListTemplateMultiPassMasterNodes> m_lodMultiPassMasterNodes;
  83. [SerializeField]
  84. private UsageListCustomExpressionsOnFunctionMode m_customExpressionsOnFunctionMode = new UsageListCustomExpressionsOnFunctionMode();
  85. [SerializeField]
  86. private UsageListStaticSwitchNodes m_staticSwitchNodes = new UsageListStaticSwitchNodes();
  87. [SerializeField]
  88. private int m_masterNodeId = Constants.INVALID_NODE_ID;
  89. [SerializeField]
  90. private bool m_isDirty;
  91. [SerializeField]
  92. private bool m_saveIsDirty = false;
  93. [SerializeField]
  94. private int m_nodeClicked;
  95. [SerializeField]
  96. private int m_loadedShaderVersion;
  97. [SerializeField]
  98. private int m_instancePropertyCount = 0;
  99. [SerializeField]
  100. private int m_virtualTextureCount = 0;
  101. [SerializeField]
  102. private int m_graphId = 0;
  103. [SerializeField]
  104. private PrecisionType m_currentPrecision = PrecisionType.Float;
  105. [SerializeField]
  106. private NodeAvailability m_currentCanvasMode = NodeAvailability.SurfaceShader;
  107. [SerializeField]
  108. private TemplateSRPType m_currentSRPType = TemplateSRPType.BuiltIn;
  109. //private List<ParentNode> m_visibleNodes = new List<ParentNode>();
  110. private List<ParentNode> m_nodePreviewList = new List<ParentNode>();
  111. private Dictionary<int, ParentNode> m_nodesDict = new Dictionary<int, ParentNode>();
  112. [NonSerialized]
  113. private List<ParentNode> m_selectedNodes = new List<ParentNode>();
  114. [NonSerialized]
  115. private List<ParentNode> m_markedForDeletion = new List<ParentNode>();
  116. [SerializeField]
  117. private List<WireReference> m_highlightedWires = new List<WireReference>();
  118. private System.Type m_masterNodeDefaultType;
  119. [SerializeField]
  120. private List<PropertyNode> m_internalTemplateNodesList = new List<PropertyNode>();
  121. private Dictionary<int, PropertyNode> m_internalTemplateNodesDict = new Dictionary<int, PropertyNode>();
  122. private NodeGrid m_nodeGrid;
  123. private bool m_markedToDeSelect = false;
  124. private int m_markToSelect = -1;
  125. private bool m_markToReOrder = false;
  126. private bool m_hasUnConnectedNodes = false;
  127. private bool m_checkSelectedWireHighlights = false;
  128. // Bezier info
  129. [SerializeField]
  130. private List<WireBezierReference> m_bezierReferences;
  131. private const int MaxBezierReferences = 50;
  132. private int m_wireBezierCount = 0;
  133. protected int m_normalDependentCount = 0;
  134. private bool m_forceCategoryRefresh = false;
  135. [SerializeField]
  136. private bool m_forceRepositionCheck = false;
  137. private bool m_isLoading = false;
  138. private bool m_isDuplicating = false;
  139. private bool m_changedLightingModel = false;
  140. public void ResetEvents()
  141. {
  142. OnNodeEvent = null;
  143. OnMaterialUpdatedEvent = null;
  144. OnShaderUpdatedEvent = null;
  145. OnEmptyGraphDetectedEvt = null;
  146. OnNodeRemovedEvent = null;
  147. }
  148. public void Init()
  149. {
  150. Undo.undoRedoPerformed += OnUndoRedoCallback;
  151. m_normalDependentCount = 0;
  152. m_nodes = new List<ParentNode>();
  153. m_samplerNodes = new UsageListSamplerNodes();
  154. m_samplerNodes.ContainerGraph = this;
  155. m_samplerNodes.ReorderOnChange = true;
  156. m_floatNodes = new UsageListFloatIntNodes();
  157. m_floatNodes.ContainerGraph = this;
  158. m_texturePropertyNodes = new UsageListTexturePropertyNodes();
  159. m_texturePropertyNodes.ContainerGraph = this;
  160. m_textureArrayNodes = new UsageListTextureArrayNodes();
  161. m_textureArrayNodes.ContainerGraph = this;
  162. m_textureArrayNodes.ReorderOnChange = true;
  163. m_propertyNodes = new UsageListPropertyNodes();
  164. m_propertyNodes.ContainerGraph = this;
  165. m_rawPropertyNodes = new UsageListPropertyNodes();
  166. m_rawPropertyNodes.ContainerGraph = this;
  167. m_customExpressionsOnFunctionMode = new UsageListCustomExpressionsOnFunctionMode();
  168. m_customExpressionsOnFunctionMode.ContainerGraph = this;
  169. m_staticSwitchNodes = new UsageListStaticSwitchNodes();
  170. m_staticSwitchNodes.ContainerGraph = this;
  171. m_staticSwitchNodes.ReorderOnChange = true;
  172. m_screenColorNodes = new UsageListScreenColorNodes();
  173. m_screenColorNodes.ContainerGraph = this;
  174. m_screenColorNodes.ReorderOnChange = true;
  175. m_localVarNodes = new UsageListRegisterLocalVarNodes();
  176. m_localVarNodes.ContainerGraph = this;
  177. m_localVarNodes.ReorderOnChange = true;
  178. m_globalArrayNodes = new UsageListGlobalArrayNodes();
  179. m_globalArrayNodes.ContainerGraph = this;
  180. m_functionInputNodes = new UsageListFunctionInputNodes();
  181. m_functionInputNodes.ContainerGraph = this;
  182. m_functionNodes = new UsageListFunctionNodes();
  183. m_functionNodes.ContainerGraph = this;
  184. m_functionOutputNodes = new UsageListFunctionOutputNodes();
  185. m_functionOutputNodes.ContainerGraph = this;
  186. m_functionSwitchNodes = new UsageListFunctionSwitchNodes();
  187. m_functionSwitchNodes.ContainerGraph = this;
  188. m_functionSwitchCopyNodes = new UsageListFunctionSwitchCopyNodes();
  189. m_functionSwitchCopyNodes.ContainerGraph = this;
  190. m_multiPassMasterNodes = new UsageListTemplateMultiPassMasterNodes();
  191. m_multiPassMasterNodes.ContainerGraph = this;
  192. m_lodMultiPassMasterNodes = new List<UsageListTemplateMultiPassMasterNodes>( MaxLodAmount );
  193. for( int i = 0; i < MaxLodAmount; i++ )
  194. {
  195. m_lodMultiPassMasterNodes.Add( new UsageListTemplateMultiPassMasterNodes() );
  196. }
  197. m_selectedNodes = new List<ParentNode>();
  198. m_markedForDeletion = new List<ParentNode>();
  199. m_highlightedWires = new List<WireReference>();
  200. m_validNodeId = 0;
  201. IsDirty = false;
  202. SaveIsDirty = false;
  203. m_masterNodeDefaultType = typeof( StandardSurfaceOutputNode );
  204. m_bezierReferences = new List<WireBezierReference>( MaxBezierReferences );
  205. for( int i = 0; i < MaxBezierReferences; i++ )
  206. {
  207. m_bezierReferences.Add( new WireBezierReference() );
  208. }
  209. }
  210. private void OnUndoRedoCallback()
  211. {
  212. DeSelectAll();
  213. }
  214. private void OnEnable()
  215. {
  216. hideFlags = HideFlags.HideAndDontSave;
  217. m_nodeGrid = new NodeGrid();
  218. m_internalTemplateNodesDict = new Dictionary<int, PropertyNode>();
  219. m_nodesDict = new Dictionary<int, ParentNode>();
  220. nodeStyleOff = UIUtils.GetCustomStyle( CustomStyle.NodeWindowOff );
  221. nodeStyleOn = UIUtils.GetCustomStyle( CustomStyle.NodeWindowOn );
  222. nodeTitle = UIUtils.GetCustomStyle( CustomStyle.NodeHeader );
  223. commentaryBackground = UIUtils.GetCustomStyle( CustomStyle.CommentaryBackground );
  224. }
  225. public void UpdateRegisters()
  226. {
  227. m_samplerNodes.UpdateNodeArr();
  228. m_propertyNodes.UpdateNodeArr();
  229. m_rawPropertyNodes.UpdateNodeArr();
  230. m_customExpressionsOnFunctionMode.UpdateNodeArr();
  231. m_staticSwitchNodes.UpdateNodeArr();
  232. m_functionInputNodes.UpdateNodeArr();
  233. m_functionNodes.UpdateNodeArr();
  234. m_functionOutputNodes.UpdateNodeArr();
  235. m_functionSwitchNodes.UpdateNodeArr();
  236. m_functionSwitchCopyNodes.UpdateNodeArr();
  237. m_multiPassMasterNodes.UpdateNodeArr();
  238. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  239. {
  240. m_lodMultiPassMasterNodes[ i ].UpdateNodeArr();
  241. }
  242. m_texturePropertyNodes.UpdateNodeArr();
  243. m_textureArrayNodes.UpdateNodeArr();
  244. m_screenColorNodes.UpdateNodeArr();
  245. m_localVarNodes.UpdateNodeArr();
  246. m_globalArrayNodes.UpdateNodeArr();
  247. }
  248. public int GetValidId()
  249. {
  250. return m_validNodeId++;
  251. }
  252. void UpdateIdFromNode( ParentNode node )
  253. {
  254. if( node.UniqueId >= m_validNodeId )
  255. {
  256. m_validNodeId = node.UniqueId + 1;
  257. }
  258. }
  259. public void ResetNodeConnStatus()
  260. {
  261. for( int i = 0; i < m_nodes.Count; i++ )
  262. {
  263. if( m_nodes[ i ].ConnStatus == NodeConnectionStatus.Connected )
  264. {
  265. m_nodes[ i ].ConnStatus = NodeConnectionStatus.Not_Connected;
  266. }
  267. }
  268. }
  269. public void CleanUnusedNodes()
  270. {
  271. List<ParentNode> unusedNodes = new List<ParentNode>();
  272. for( int i = 0; i < m_nodes.Count; i++ )
  273. {
  274. if( m_nodes[ i ].ConnStatus == NodeConnectionStatus.Not_Connected )
  275. {
  276. unusedNodes.Add( m_nodes[ i ] );
  277. }
  278. }
  279. for( int i = 0; i < unusedNodes.Count; i++ )
  280. {
  281. DestroyNode( unusedNodes[ i ] );
  282. }
  283. unusedNodes.Clear();
  284. unusedNodes = null;
  285. IsDirty = true;
  286. }
  287. // Destroy all nodes excluding Master Node
  288. public void ClearGraph()
  289. {
  290. List<ParentNode> list = new List<ParentNode>();
  291. int count = m_nodes.Count;
  292. for( int i = 0; i < count; i++ )
  293. {
  294. if( m_nodes[ i ].UniqueId != m_masterNodeId )
  295. {
  296. list.Add( m_nodes[ i ] );
  297. }
  298. }
  299. while( list.Count > 0 )
  300. {
  301. DestroyNode( list[ 0 ] );
  302. list.RemoveAt( 0 );
  303. }
  304. }
  305. public void CleanNodes()
  306. {
  307. for( int i = 0; i < m_nodes.Count; i++ )
  308. {
  309. if( m_nodes[ i ] != null )
  310. {
  311. Undo.ClearUndo( m_nodes[ i ] );
  312. m_nodes[ i ].Destroy();
  313. GameObject.DestroyImmediate( m_nodes[ i ] );
  314. }
  315. }
  316. ClearInternalTemplateNodes();
  317. m_masterNodeId = Constants.INVALID_NODE_ID;
  318. m_validNodeId = 0;
  319. m_instancePropertyCount = 0;
  320. m_virtualTextureCount = 0;
  321. m_nodesDict.Clear();
  322. m_nodes.Clear();
  323. m_samplerNodes.Clear();
  324. m_propertyNodes.Clear();
  325. m_rawPropertyNodes.Clear();
  326. m_customExpressionsOnFunctionMode.Clear();
  327. m_staticSwitchNodes.Clear();
  328. m_functionInputNodes.Clear();
  329. m_functionNodes.Clear();
  330. m_functionOutputNodes.Clear();
  331. m_functionSwitchNodes.Clear();
  332. m_functionSwitchCopyNodes.Clear();
  333. m_multiPassMasterNodes.Clear();
  334. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  335. {
  336. m_lodMultiPassMasterNodes[ i ].Clear();
  337. }
  338. m_texturePropertyNodes.Clear();
  339. m_textureArrayNodes.Clear();
  340. m_screenColorNodes.Clear();
  341. m_localVarNodes.Clear();
  342. m_globalArrayNodes.Clear();
  343. m_selectedNodes.Clear();
  344. m_markedForDeletion.Clear();
  345. }
  346. public void ResetHighlightedWires()
  347. {
  348. for( int i = 0; i < m_highlightedWires.Count; i++ )
  349. {
  350. m_highlightedWires[ i ].WireStatus = WireStatus.Default;
  351. }
  352. m_highlightedWires.Clear();
  353. }
  354. public void HighlightWiresStartingNode( ParentNode node )
  355. {
  356. for( int outputIdx = 0; outputIdx < node.OutputPorts.Count; outputIdx++ )
  357. {
  358. for( int extIdx = 0; extIdx < node.OutputPorts[ outputIdx ].ExternalReferences.Count; extIdx++ )
  359. {
  360. WireReference wireRef = node.OutputPorts[ outputIdx ].ExternalReferences[ extIdx ];
  361. ParentNode nextNode = GetNode( wireRef.NodeId );
  362. if( nextNode && nextNode.ConnStatus == NodeConnectionStatus.Connected )
  363. {
  364. InputPort port = nextNode.GetInputPortByUniqueId( wireRef.PortId );
  365. if( port.ExternalReferences.Count == 0 || port.ExternalReferences[ 0 ].WireStatus == WireStatus.Highlighted )
  366. {
  367. // if even one wire is already highlighted then this tells us that this node was already been analysed
  368. return;
  369. }
  370. port.ExternalReferences[ 0 ].WireStatus = WireStatus.Highlighted;
  371. m_highlightedWires.Add( port.ExternalReferences[ 0 ] );
  372. HighlightWiresStartingNode( nextNode );
  373. }
  374. }
  375. }
  376. RegisterLocalVarNode regNode = node as RegisterLocalVarNode;
  377. if( (object)regNode != null )
  378. {
  379. int count = regNode.NodeReferences.Count;
  380. for( int i = 0; i < count; i++ )
  381. {
  382. HighlightWiresStartingNode( regNode.NodeReferences[ i ] );
  383. }
  384. }
  385. }
  386. void PropagateHighlightDeselection( ParentNode node, int portId = -1 )
  387. {
  388. if( portId > -1 )
  389. {
  390. InputPort port = node.GetInputPortByUniqueId( portId );
  391. port.ExternalReferences[ 0 ].WireStatus = WireStatus.Default;
  392. }
  393. if( node.Selected )
  394. return;
  395. for( int i = 0; i < node.InputPorts.Count; i++ )
  396. {
  397. if( node.InputPorts[ i ].ExternalReferences.Count > 0 && node.InputPorts[ i ].ExternalReferences[ 0 ].WireStatus == WireStatus.Highlighted )
  398. {
  399. // even though node is deselected, it receives wire highlight from a previous one
  400. return;
  401. }
  402. }
  403. for( int outputIdx = 0; outputIdx < node.OutputPorts.Count; outputIdx++ )
  404. {
  405. for( int extIdx = 0; extIdx < node.OutputPorts[ outputIdx ].ExternalReferences.Count; extIdx++ )
  406. {
  407. WireReference wireRef = node.OutputPorts[ outputIdx ].ExternalReferences[ extIdx ];
  408. ParentNode nextNode = GetNode( wireRef.NodeId );
  409. PropagateHighlightDeselection( nextNode, wireRef.PortId );
  410. }
  411. }
  412. RegisterLocalVarNode regNode = node as RegisterLocalVarNode;
  413. if( (object)regNode != null )
  414. {
  415. int count = regNode.NodeReferences.Count;
  416. for( int i = 0; i < count; i++ )
  417. {
  418. PropagateHighlightDeselection( regNode.NodeReferences[ i ], -1 );
  419. }
  420. }
  421. }
  422. public void ResetNodesData()
  423. {
  424. int count = m_nodes.Count;
  425. for( int i = 0; i < count; i++ )
  426. {
  427. m_nodes[ i ].ResetNodeData();
  428. }
  429. }
  430. public void FullCleanUndoStack()
  431. {
  432. Undo.ClearUndo( this );
  433. int count = m_nodes.Count;
  434. for( int i = 0; i < count; i++ )
  435. {
  436. if( m_nodes[ i ] != null )
  437. {
  438. Undo.ClearUndo( m_nodes[ i ] );
  439. }
  440. }
  441. }
  442. public void FullRegisterOnUndoStack()
  443. {
  444. Undo.RegisterCompleteObjectUndo( this, Constants.UndoRegisterFullGrapId );
  445. int count = m_nodes.Count;
  446. for( int i = 0; i < count; i++ )
  447. {
  448. if( m_nodes[ i ] != null )
  449. {
  450. Undo.RegisterCompleteObjectUndo( m_nodes[ i ], Constants.UndoRegisterFullGrapId );
  451. }
  452. }
  453. }
  454. public void CheckPropertiesAutoRegister( ref MasterNodeDataCollector dataCollector )
  455. {
  456. List<PropertyNode> propertyNodesList = m_rawPropertyNodes.NodesList;
  457. int propertyCount = propertyNodesList.Count;
  458. for( int i = 0; i < propertyCount; i++ )
  459. {
  460. propertyNodesList[ i ].CheckIfAutoRegister( ref dataCollector );
  461. }
  462. propertyNodesList = null;
  463. List<GlobalArrayNode> globalArrayNodeList = m_globalArrayNodes.NodesList;
  464. int globalArrayCount = globalArrayNodeList.Count;
  465. for( int i = 0; i < globalArrayCount; i++ )
  466. {
  467. globalArrayNodeList[ i ].CheckIfAutoRegister( ref dataCollector );
  468. }
  469. globalArrayNodeList = null;
  470. //List<PropertyNode> propertyNodesList = m_propertyNodes.NodesList;
  471. //int propertyCount = propertyNodesList.Count;
  472. //for( int i = 0; i < propertyCount; i++ )
  473. //{
  474. // propertyNodesList[ i ].CheckIfAutoRegister( ref dataCollector );
  475. //}
  476. //propertyNodesList = null;
  477. //List<ScreenColorNode> screenColorNodes = m_screenColorNodes.NodesList;
  478. //int screenColorNodesCount = screenColorNodes.Count;
  479. //for( int i = 0; i < screenColorNodesCount; i++ )
  480. //{
  481. // screenColorNodes[ i ].CheckIfAutoRegister( ref dataCollector );
  482. //}
  483. //screenColorNodes = null;
  484. }
  485. public void SoftDestroy()
  486. {
  487. OnNodeRemovedEvent = null;
  488. m_masterNodeId = Constants.INVALID_NODE_ID;
  489. m_validNodeId = 0;
  490. m_nodeGrid.Destroy();
  491. //m_nodeGrid = null;
  492. ClearInternalTemplateNodes();
  493. for( int i = 0; i < m_nodes.Count; i++ )
  494. {
  495. if( m_nodes[ i ] != null )
  496. {
  497. m_nodes[ i ].Destroy();
  498. GameObject.DestroyImmediate( m_nodes[ i ] );
  499. }
  500. }
  501. m_instancePropertyCount = 0;
  502. m_nodes.Clear();
  503. //m_nodes = null;
  504. m_nodesDict.Clear();
  505. //m_nodesDict = null;
  506. m_samplerNodes.Clear();
  507. //m_samplerNodes = null;
  508. m_propertyNodes.Clear();
  509. m_rawPropertyNodes.Clear();
  510. //m_propertyNodes = null;
  511. m_customExpressionsOnFunctionMode.Clear();
  512. m_staticSwitchNodes.Clear();
  513. m_functionInputNodes.Clear();
  514. //m_functionInputNodes = null;
  515. m_functionNodes.Clear();
  516. //m_functionNodes = null;
  517. m_functionOutputNodes.Clear();
  518. //m_functionOutputNodes = null;
  519. m_functionSwitchNodes.Clear();
  520. //m_functionSwitchNodes = null;
  521. m_functionSwitchCopyNodes.Clear();
  522. //m_functionSwitchCopyNodes = null;
  523. m_texturePropertyNodes.Clear();
  524. //m_texturePropertyNodes = null;
  525. m_textureArrayNodes.Clear();
  526. //m_textureArrayNodes = null;
  527. m_screenColorNodes.Clear();
  528. //m_screenColorNodes = null;
  529. m_localVarNodes.Clear();
  530. //m_localVarNodes = null;
  531. m_globalArrayNodes.Clear();
  532. m_selectedNodes.Clear();
  533. //m_selectedNodes = null;
  534. m_markedForDeletion.Clear();
  535. //m_markedForDeletion = null;
  536. m_nodePreviewList.Clear();
  537. //m_nodePreviewList = null;
  538. IsDirty = true;
  539. OnNodeEvent = null;
  540. OnDuplicateEvent = null;
  541. //m_currentShaderFunction = null;
  542. OnMaterialUpdatedEvent = null;
  543. OnShaderUpdatedEvent = null;
  544. OnEmptyGraphDetectedEvt = null;
  545. nodeStyleOff = null;
  546. nodeStyleOn = null;
  547. nodeTitle = null;
  548. commentaryBackground = null;
  549. OnLODMasterNodesAddedEvent = null;
  550. }
  551. public void Destroy()
  552. {
  553. Undo.undoRedoPerformed -= OnUndoRedoCallback;
  554. for( int i = 0; i < m_nodes.Count; i++ )
  555. {
  556. if( m_nodes[ i ] != null )
  557. {
  558. Undo.ClearUndo( m_nodes[ i ] );
  559. m_nodes[ i ].Destroy();
  560. GameObject.DestroyImmediate( m_nodes[ i ] );
  561. }
  562. }
  563. //Must be before m_propertyNodes.Destroy();
  564. ClearInternalTemplateNodes();
  565. m_internalTemplateNodesDict = null;
  566. m_internalTemplateNodesList = null;
  567. OnNodeRemovedEvent = null;
  568. m_masterNodeId = Constants.INVALID_NODE_ID;
  569. m_validNodeId = 0;
  570. m_instancePropertyCount = 0;
  571. m_nodeGrid.Destroy();
  572. m_nodeGrid = null;
  573. m_nodes.Clear();
  574. m_nodes = null;
  575. m_samplerNodes.Destroy();
  576. m_samplerNodes = null;
  577. m_propertyNodes.Destroy();
  578. m_propertyNodes = null;
  579. m_rawPropertyNodes.Destroy();
  580. m_rawPropertyNodes = null;
  581. m_customExpressionsOnFunctionMode.Destroy();
  582. m_customExpressionsOnFunctionMode = null;
  583. m_staticSwitchNodes.Destroy();
  584. m_staticSwitchNodes = null;
  585. m_functionInputNodes.Destroy();
  586. m_functionInputNodes = null;
  587. m_functionNodes.Destroy();
  588. m_functionNodes = null;
  589. m_functionOutputNodes.Destroy();
  590. m_functionOutputNodes = null;
  591. m_functionSwitchNodes.Destroy();
  592. m_functionSwitchNodes = null;
  593. m_functionSwitchCopyNodes.Destroy();
  594. m_functionSwitchCopyNodes = null;
  595. m_multiPassMasterNodes.Destroy();
  596. m_multiPassMasterNodes = null;
  597. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  598. {
  599. m_lodMultiPassMasterNodes[ i ].Destroy();
  600. m_lodMultiPassMasterNodes[ i ] = null;
  601. }
  602. m_lodMultiPassMasterNodes.Clear();
  603. m_lodMultiPassMasterNodes = null;
  604. m_texturePropertyNodes.Destroy();
  605. m_texturePropertyNodes = null;
  606. m_textureArrayNodes.Destroy();
  607. m_textureArrayNodes = null;
  608. m_screenColorNodes.Destroy();
  609. m_screenColorNodes = null;
  610. m_localVarNodes.Destroy();
  611. m_localVarNodes = null;
  612. m_globalArrayNodes.Destroy();
  613. m_globalArrayNodes = null;
  614. m_selectedNodes.Clear();
  615. m_selectedNodes = null;
  616. m_markedForDeletion.Clear();
  617. m_markedForDeletion = null;
  618. m_nodesDict.Clear();
  619. m_nodesDict = null;
  620. m_nodePreviewList.Clear();
  621. m_nodePreviewList = null;
  622. IsDirty = true;
  623. OnNodeEvent = null;
  624. OnDuplicateEvent = null;
  625. //m_currentShaderFunction = null;
  626. OnMaterialUpdatedEvent = null;
  627. OnShaderUpdatedEvent = null;
  628. OnEmptyGraphDetectedEvt = null;
  629. nodeStyleOff = null;
  630. nodeStyleOn = null;
  631. nodeTitle = null;
  632. commentaryBackground = null;
  633. OnLODMasterNodesAddedEvent = null;
  634. }
  635. void OnNodeChangeSizeEvent( ParentNode node )
  636. {
  637. m_nodeGrid.RemoveNodeFromGrid( node, true );
  638. m_nodeGrid.AddNodeToGrid( node );
  639. }
  640. public void OnNodeFinishMoving( ParentNode node, bool testOnlySelected, InteractionMode interactionMode )
  641. {
  642. if( OnNodeEvent != null )
  643. {
  644. OnNodeEvent( node );
  645. SaveIsDirty = true;
  646. }
  647. m_nodeGrid.RemoveNodeFromGrid( node, true );
  648. m_nodeGrid.AddNodeToGrid( node );
  649. //if( testOnlySelected )
  650. //{
  651. // for( int i = m_visibleNodes.Count - 1; i > -1; i-- )
  652. // {
  653. // if( node.UniqueId != m_visibleNodes[ i ].UniqueId )
  654. // {
  655. // switch( interactionMode )
  656. // {
  657. // case InteractionMode.Target:
  658. // {
  659. // node.OnNodeInteraction( m_visibleNodes[ i ] );
  660. // }
  661. // break;
  662. // case InteractionMode.Other:
  663. // {
  664. // m_visibleNodes[ i ].OnNodeInteraction( node );
  665. // }
  666. // break;
  667. // case InteractionMode.Both:
  668. // {
  669. // node.OnNodeInteraction( m_visibleNodes[ i ] );
  670. // m_visibleNodes[ i ].OnNodeInteraction( node );
  671. // }
  672. // break;
  673. // }
  674. // }
  675. // }
  676. //}
  677. //else
  678. {
  679. for( int i = m_nodes.Count - 1; i > -1; i-- )
  680. {
  681. if( node.UniqueId != m_nodes[ i ].UniqueId )
  682. {
  683. switch( interactionMode )
  684. {
  685. case InteractionMode.Target:
  686. {
  687. node.OnNodeInteraction( m_nodes[ i ] );
  688. }
  689. break;
  690. case InteractionMode.Other:
  691. {
  692. m_nodes[ i ].OnNodeInteraction( node );
  693. }
  694. break;
  695. case InteractionMode.Both:
  696. {
  697. node.OnNodeInteraction( m_nodes[ i ] );
  698. m_nodes[ i ].OnNodeInteraction( node );
  699. }
  700. break;
  701. }
  702. }
  703. }
  704. }
  705. }
  706. public void OnNodeReOrderEvent( ParentNode node, int index )
  707. {
  708. if( node.Depth < index )
  709. {
  710. Debug.LogWarning( "Reorder canceled: This is a specific method for when reordering needs to be done and a its original index is higher than the new one" );
  711. }
  712. else
  713. {
  714. m_nodes.Remove( node );
  715. m_nodes.Insert( index, node );
  716. m_markToReOrder = true;
  717. }
  718. }
  719. public void AddNode( ParentNode node, bool updateId = false, bool addLast = true, bool registerUndo = true, bool fetchMaterialValues = true )
  720. {
  721. if( registerUndo )
  722. {
  723. UIUtils.MarkUndoAction();
  724. Undo.RegisterCompleteObjectUndo( ParentWindow, Constants.UndoCreateNodeId );
  725. Undo.RegisterCompleteObjectUndo( this, Constants.UndoCreateNodeId );
  726. Undo.RegisterCreatedObjectUndo( node, Constants.UndoCreateNodeId );
  727. }
  728. if( OnNodeEvent != null )
  729. {
  730. OnNodeEvent( node );
  731. }
  732. if( updateId )
  733. {
  734. node.UniqueId = GetValidId();
  735. }
  736. else
  737. {
  738. UpdateIdFromNode( node );
  739. }
  740. if( addLast )
  741. {
  742. m_nodes.Add( node );
  743. node.Depth = m_nodes.Count;
  744. }
  745. else
  746. {
  747. m_nodes.Insert( 0, node );
  748. node.Depth = 0;
  749. }
  750. if( m_nodesDict.ContainsKey( node.UniqueId ) )
  751. {
  752. //m_nodesDict[ node.UniqueId ] = node;
  753. m_foundDuplicates = true;
  754. }
  755. else
  756. {
  757. m_nodesDict.Add( node.UniqueId, node );
  758. node.SetMaterialMode( CurrentMaterial, fetchMaterialValues );
  759. }
  760. m_nodeGrid.AddNodeToGrid( node );
  761. node.OnNodeChangeSizeEvent += OnNodeChangeSizeEvent;
  762. node.OnNodeReOrderEvent += OnNodeReOrderEvent;
  763. IsDirty = true;
  764. }
  765. public void CheckForDuplicates()
  766. {
  767. if( m_foundDuplicates )
  768. {
  769. Debug.LogWarning( "Found duplicates:" );
  770. m_foundDuplicates = false;
  771. m_nodesDict.Clear();
  772. int count = m_nodes.Count;
  773. for( int i = 0; i < count; i++ )
  774. {
  775. if( m_nodesDict.ContainsKey( m_nodes[ i ].UniqueId ) )
  776. {
  777. m_nodes[ i ].UniqueId = GetValidId();
  778. m_nodesDict.Add( m_nodes[ i ].UniqueId, m_nodes[ i ] );
  779. Debug.LogWarning( "Assigning new ID to " + m_nodes[ i ].TypeName );
  780. }
  781. else
  782. {
  783. m_nodesDict.Add( m_nodes[ i ].UniqueId, m_nodes[ i ] );
  784. }
  785. }
  786. }
  787. }
  788. public ParentNode GetClickedNode()
  789. {
  790. if( m_nodeClicked < 0 )
  791. return null;
  792. return GetNode( m_nodeClicked );
  793. }
  794. public PropertyNode GetInternalTemplateNode( int nodeId )
  795. {
  796. if( m_internalTemplateNodesDict.Count != m_internalTemplateNodesList.Count )
  797. {
  798. m_internalTemplateNodesDict.Clear();
  799. int count = m_internalTemplateNodesList.Count;
  800. for( int i = 0; i < m_internalTemplateNodesList.Count; i++ )
  801. {
  802. if( m_internalTemplateNodesList[ i ] != null )
  803. m_internalTemplateNodesDict.Add( m_internalTemplateNodesList[ i ].UniqueId, m_internalTemplateNodesList[ i ] );
  804. }
  805. }
  806. if( m_internalTemplateNodesDict.ContainsKey( nodeId ) )
  807. return m_internalTemplateNodesDict[ nodeId ];
  808. return null;
  809. }
  810. public PropertyNode GetInternalTemplateNode( string propertyName )
  811. {
  812. return m_internalTemplateNodesList.Find( ( x ) => x.PropertyName.Equals( propertyName ) );
  813. }
  814. public void AddInternalTemplateNode( TemplateShaderPropertyData data )
  815. {
  816. PropertyNode propertyNode = null;
  817. switch( data.PropertyDataType )
  818. {
  819. case WirePortDataType.FLOAT:
  820. propertyNode = CreateInstance<RangedFloatNode>(); break;
  821. case WirePortDataType.FLOAT4:
  822. propertyNode = CreateInstance<Vector4Node>();
  823. break;
  824. case WirePortDataType.COLOR:
  825. propertyNode = CreateInstance<ColorNode>();
  826. break;
  827. case WirePortDataType.INT:
  828. propertyNode = CreateInstance<IntNode>(); break;
  829. case WirePortDataType.SAMPLER1D:
  830. case WirePortDataType.SAMPLER2D:
  831. case WirePortDataType.SAMPLER3D:
  832. case WirePortDataType.SAMPLERCUBE:
  833. case WirePortDataType.SAMPLER2DARRAY:
  834. propertyNode = CreateInstance<SamplerNode>();
  835. break;
  836. default: return;
  837. }
  838. propertyNode.PropertyNameFromTemplate( data );
  839. // Create a negative unique Id to separate it from
  840. // the regular ids on the main nodes list
  841. // Its begins at -2 since -1 is used to detect invalid values
  842. int uniqueId = -( m_internalTemplateNodesList.Count + 2 );
  843. propertyNode.SetBaseUniqueId( uniqueId );
  844. //Register into Float/Int Nodes list to be available inline
  845. // Unique Id must be already set at this point to properly
  846. // create array
  847. if( data.PropertyDataType == WirePortDataType.FLOAT ||
  848. data.PropertyDataType == WirePortDataType.INT )
  849. m_floatNodes.AddNode( propertyNode );
  850. m_internalTemplateNodesList.Add( propertyNode );
  851. m_internalTemplateNodesDict.Add( uniqueId, propertyNode );
  852. }
  853. public void ClearInternalTemplateNodes()
  854. {
  855. if( m_internalTemplateNodesList != null )
  856. {
  857. int count = m_internalTemplateNodesList.Count;
  858. for( int i = 0; i < count; i++ )
  859. {
  860. m_internalTemplateNodesList[ i ].Destroy();
  861. GameObject.DestroyImmediate( m_internalTemplateNodesList[ i ] );
  862. }
  863. m_internalTemplateNodesList.Clear();
  864. m_internalTemplateNodesDict.Clear();
  865. }
  866. }
  867. public ParentNode GetNode( int nodeId )
  868. {
  869. if( m_nodesDict.Count != m_nodes.Count )
  870. {
  871. m_nodesDict.Clear();
  872. int count = m_nodes.Count;
  873. for( int i = 0; i < count; i++ )
  874. {
  875. if( m_nodes[ i ] != null && !m_nodesDict.ContainsKey( m_nodes[ i ].UniqueId ) )
  876. m_nodesDict.Add( m_nodes[ i ].UniqueId, m_nodes[ i ] );
  877. }
  878. }
  879. if( m_nodesDict.ContainsKey( nodeId ) )
  880. return m_nodesDict[ nodeId ];
  881. return null;
  882. }
  883. public void ForceReOrder()
  884. {
  885. m_nodes.Sort( ( x, y ) => x.Depth.CompareTo( y.Depth ) );
  886. }
  887. public bool Draw( DrawInfo drawInfo )
  888. {
  889. MasterNode masterNode = GetNode( m_masterNodeId ) as MasterNode;
  890. if( m_forceCategoryRefresh && masterNode != null )
  891. {
  892. masterNode.RefreshAvailableCategories();
  893. m_forceCategoryRefresh = false;
  894. }
  895. SaveIsDirty = false;
  896. if( m_afterDeserializeFlag )
  897. {
  898. // this is now done after logic update... templates needs it this way
  899. //m_afterDeserializeFlag = false;
  900. CleanCorruptedNodes();
  901. if( m_nodes.Count == 0 )
  902. {
  903. //TODO: remove this temp from here
  904. NodeAvailability cachedCanvas = CurrentCanvasMode;
  905. ParentWindow.CreateNewGraph( "Empty" );
  906. CurrentCanvasMode = cachedCanvas;
  907. if( OnEmptyGraphDetectedEvt != null )
  908. {
  909. OnEmptyGraphDetectedEvt( this );
  910. SaveIsDirty = false;
  911. }
  912. else
  913. {
  914. SaveIsDirty = true;
  915. }
  916. }
  917. //for( int i = 0; i < m_nodes.Count; i++ )
  918. //{
  919. // m_nodes[ i ].SetContainerGraph( this );
  920. //}
  921. }
  922. if( drawInfo.CurrentEventType == EventType.Repaint )
  923. {
  924. if( m_markedToDeSelect )
  925. DeSelectAll();
  926. if( m_markToSelect > -1 )
  927. {
  928. AddToSelectedNodes( GetNode( m_markToSelect ) );
  929. m_markToSelect = -1;
  930. }
  931. if( m_markToReOrder )
  932. {
  933. m_markToReOrder = false;
  934. int nodesCount = m_nodes.Count;
  935. for( int i = 0; i < nodesCount; i++ )
  936. {
  937. m_nodes[ i ].Depth = i;
  938. }
  939. }
  940. }
  941. if( drawInfo.CurrentEventType == EventType.Repaint )
  942. {
  943. // Resizing Nods per LOD level
  944. NodeLOD newLevel = NodeLOD.LOD0;
  945. float referenceValue;
  946. if( drawInfo.InvertedZoom > 0.5f )
  947. {
  948. newLevel = NodeLOD.LOD0;
  949. referenceValue = 4;
  950. }
  951. else if( drawInfo.InvertedZoom > 0.25f )
  952. {
  953. newLevel = NodeLOD.LOD1;
  954. referenceValue = 2;
  955. }
  956. else if( drawInfo.InvertedZoom > 0.15f )
  957. {
  958. newLevel = NodeLOD.LOD2;
  959. referenceValue = 1;
  960. }
  961. else if( drawInfo.InvertedZoom > 0.1f )
  962. {
  963. newLevel = NodeLOD.LOD3;
  964. referenceValue = 0;
  965. }
  966. else if( drawInfo.InvertedZoom > 0.07f )
  967. {
  968. newLevel = NodeLOD.LOD4;
  969. referenceValue = 0;
  970. }
  971. else
  972. {
  973. newLevel = NodeLOD.LOD5;
  974. referenceValue = 0;
  975. }
  976. // Just a sanity check
  977. nodeStyleOff = UIUtils.GetCustomStyle( CustomStyle.NodeWindowOff );
  978. nodeStyleOn = UIUtils.GetCustomStyle( CustomStyle.NodeWindowOn );//= UIUtils.GetCustomStyle( CustomStyle.NodeWindowOn );
  979. nodeTitle = UIUtils.GetCustomStyle( CustomStyle.NodeHeader );
  980. commentaryBackground = UIUtils.GetCustomStyle( CustomStyle.CommentaryBackground );
  981. if( newLevel != m_lodLevel || ( UIUtils.MainSkin != null && UIUtils.MainSkin.textField.border.left != referenceValue ) )
  982. {
  983. m_lodLevel = newLevel;
  984. switch( m_lodLevel )
  985. {
  986. default:
  987. case NodeLOD.LOD0:
  988. {
  989. UIUtils.MainSkin.textField.border = UIUtils.RectOffsetFour;
  990. nodeStyleOff.border = UIUtils.RectOffsetSix;
  991. UIUtils.NodeWindowOffSquare.border = UIUtils.RectOffsetFour;
  992. nodeStyleOn.border = UIUtils.RectOffsetSix;
  993. UIUtils.NodeWindowOnSquare.border = UIUtils.RectOffsetSix;
  994. nodeTitle.border.left = 6;
  995. nodeTitle.border.right = 6;
  996. nodeTitle.border.top = 6;
  997. nodeTitle.border.bottom = 4;
  998. UIUtils.NodeHeaderSquare.border = UIUtils.RectOffsetFour;
  999. commentaryBackground.border = UIUtils.RectOffsetSix;
  1000. }
  1001. break;
  1002. case NodeLOD.LOD1:
  1003. {
  1004. UIUtils.MainSkin.textField.border = UIUtils.RectOffsetTwo;
  1005. nodeStyleOff.border = UIUtils.RectOffsetFive;
  1006. UIUtils.NodeWindowOffSquare.border = UIUtils.RectOffsetFive;
  1007. nodeStyleOn.border = UIUtils.RectOffsetFive;
  1008. UIUtils.NodeWindowOnSquare.border = UIUtils.RectOffsetFour;
  1009. nodeTitle.border.left = 5;
  1010. nodeTitle.border.right = 5;
  1011. nodeTitle.border.top = 5;
  1012. nodeTitle.border.bottom = 2;
  1013. UIUtils.NodeHeaderSquare.border = UIUtils.RectOffsetThree;
  1014. commentaryBackground.border = UIUtils.RectOffsetFive;
  1015. }
  1016. break;
  1017. case NodeLOD.LOD2:
  1018. {
  1019. UIUtils.MainSkin.textField.border = UIUtils.RectOffsetOne;
  1020. nodeStyleOff.border.left = 2;
  1021. nodeStyleOff.border.right = 2;
  1022. nodeStyleOff.border.top = 2;
  1023. nodeStyleOff.border.bottom = 3;
  1024. UIUtils.NodeWindowOffSquare.border = UIUtils.RectOffsetThree;
  1025. nodeStyleOn.border.left = 4;
  1026. nodeStyleOn.border.right = 4;
  1027. nodeStyleOn.border.top = 4;
  1028. nodeStyleOn.border.bottom = 3;
  1029. UIUtils.NodeWindowOnSquare.border = UIUtils.RectOffsetThree;
  1030. nodeTitle.border = UIUtils.RectOffsetTwo;
  1031. UIUtils.NodeHeaderSquare.border = UIUtils.RectOffsetTwo;
  1032. commentaryBackground.border.left = 2;
  1033. commentaryBackground.border.right = 2;
  1034. commentaryBackground.border.top = 2;
  1035. commentaryBackground.border.bottom = 3;
  1036. }
  1037. break;
  1038. case NodeLOD.LOD3:
  1039. case NodeLOD.LOD4:
  1040. case NodeLOD.LOD5:
  1041. {
  1042. UIUtils.MainSkin.textField.border = UIUtils.RectOffsetZero;
  1043. nodeStyleOff.border.left = 1;
  1044. nodeStyleOff.border.right = 1;
  1045. nodeStyleOff.border.top = 1;
  1046. nodeStyleOff.border.bottom = 2;
  1047. UIUtils.NodeWindowOffSquare.border = UIUtils.RectOffsetTwo;
  1048. nodeStyleOn.border = UIUtils.RectOffsetTwo;
  1049. UIUtils.NodeWindowOnSquare.border = UIUtils.RectOffsetTwo;
  1050. nodeTitle.border = UIUtils.RectOffsetOne;
  1051. UIUtils.NodeHeaderSquare.border = UIUtils.RectOffsetOne;
  1052. commentaryBackground.border.left = 1;
  1053. commentaryBackground.border.right = 1;
  1054. commentaryBackground.border.top = 1;
  1055. commentaryBackground.border.bottom = 2;
  1056. }
  1057. break;
  1058. }
  1059. }
  1060. }
  1061. //m_visibleNodes.Clear();
  1062. //int nullCount = 0;
  1063. m_hasUnConnectedNodes = false;
  1064. bool repaint = false;
  1065. Material currentMaterial = masterNode != null ? masterNode.CurrentMaterial : null;
  1066. EditorGUI.BeginChangeCheck();
  1067. bool repaintMaterialInspector = false;
  1068. int nodeCount = m_nodes.Count;
  1069. for( int i = 0; i < nodeCount; i++ )
  1070. {
  1071. m_nodes[ i ].OnNodeLogicUpdate( drawInfo );
  1072. }
  1073. if( m_afterDeserializeFlag || m_lateOptionsRefresh )
  1074. {
  1075. m_afterDeserializeFlag = false;
  1076. m_lateOptionsRefresh = false;
  1077. if( CurrentCanvasMode == NodeAvailability.TemplateShader )
  1078. {
  1079. RefreshLinkedMasterNodes( true );
  1080. OnRefreshLinkedPortsComplete();
  1081. //If clipboard has cached nodes then a master node replacement will take place
  1082. //We need to re-cache master nodes to ensure applied options are correctly cached
  1083. //As first cache happens before that
  1084. if( m_parentWindow.ClipboardInstance.HasCachedMasterNodes )
  1085. {
  1086. m_parentWindow.ClipboardInstance.AddMultiPassNodesToClipboard( MultiPassMasterNodes.NodesList );
  1087. }
  1088. //RepositionTemplateNodes( CurrentMasterNode );
  1089. }
  1090. }
  1091. if( m_forceRepositionCheck )
  1092. {
  1093. RepositionTemplateNodes( CurrentMasterNode );
  1094. }
  1095. //for( int i = 0; i < m_functionNodes.NodesList.Count; i++ )
  1096. //{
  1097. // m_functionNodes.NodesList[ i ].LogicGraph();
  1098. //}
  1099. //for( int i = 0; i < UIUtils.FunctionSwitchCopyList().Count; i++ )
  1100. //{
  1101. // UIUtils.FunctionSwitchCopyList()[ i ].CheckReference();
  1102. //}
  1103. // Dont use nodeCount variable because node count can change in this loop???
  1104. nodeCount = m_nodes.Count;
  1105. ParentNode node = null;
  1106. for( int i = 0; i < nodeCount; i++ )
  1107. {
  1108. node = m_nodes[ i ];
  1109. if( !node.IsOnGrid )
  1110. {
  1111. m_nodeGrid.AddNodeToGrid( node );
  1112. }
  1113. node.MovingInFrame = false;
  1114. if( drawInfo.CurrentEventType == EventType.Repaint )
  1115. node.OnNodeLayout( drawInfo );
  1116. m_hasUnConnectedNodes = m_hasUnConnectedNodes ||
  1117. ( node.ConnStatus != NodeConnectionStatus.Connected && node.ConnStatus != NodeConnectionStatus.Island );
  1118. if( node.RequireMaterialUpdate && currentMaterial != null )
  1119. {
  1120. node.UpdateMaterial( currentMaterial );
  1121. repaintMaterialInspector = true;
  1122. }
  1123. //if( node.IsVisible )
  1124. // m_visibleNodes.Add( node );
  1125. IsDirty = ( m_isDirty || node.IsDirty );
  1126. SaveIsDirty = ( m_saveIsDirty || node.SaveIsDirty );
  1127. }
  1128. // Handles GUI controls
  1129. nodeCount = m_nodes.Count;
  1130. for( int i = nodeCount - 1; i >= 0; i-- )
  1131. //for ( int i = 0; i < nodeCount; i++ )
  1132. {
  1133. node = m_nodes[ i ];
  1134. bool restoreMouse = false;
  1135. if( drawInfo.CurrentEventType == EventType.MouseDown && m_nodeClicked > -1 && node.UniqueId != m_nodeClicked )
  1136. {
  1137. restoreMouse = true;
  1138. drawInfo.CurrentEventType = EventType.Ignore;
  1139. }
  1140. node.DrawGUIControls( drawInfo );
  1141. if( restoreMouse )
  1142. {
  1143. drawInfo.CurrentEventType = EventType.MouseDown;
  1144. }
  1145. }
  1146. // Draw connection wires
  1147. if( drawInfo.CurrentEventType == EventType.Repaint )
  1148. DrawWires( ParentWindow.WireTexture, drawInfo, ParentWindow.WindowContextPallete.IsActive, ParentWindow.WindowContextPallete.CurrentPosition );
  1149. // Master Draw
  1150. nodeCount = m_nodes.Count;
  1151. for( int i = 0; i < nodeCount; i++ )
  1152. {
  1153. node = m_nodes[ i ];
  1154. bool restoreMouse = false;
  1155. if( drawInfo.CurrentEventType == EventType.MouseDown && m_nodeClicked > -1 && node.UniqueId != m_nodeClicked )
  1156. {
  1157. restoreMouse = true;
  1158. drawInfo.CurrentEventType = EventType.Ignore;
  1159. }
  1160. node.Draw( drawInfo );
  1161. if( restoreMouse )
  1162. {
  1163. drawInfo.CurrentEventType = EventType.MouseDown;
  1164. }
  1165. }
  1166. // Draw Tooltip
  1167. if( drawInfo.CurrentEventType == EventType.Repaint || drawInfo.CurrentEventType == EventType.MouseDown )
  1168. {
  1169. nodeCount = m_nodes.Count;
  1170. for( int i = nodeCount - 1; i >= 0; i-- )
  1171. {
  1172. node = m_nodes[ i ];
  1173. if( node.IsVisible && !node.IsMoving )
  1174. {
  1175. bool showing = node.ShowTooltip( drawInfo );
  1176. if( showing )
  1177. break;
  1178. }
  1179. }
  1180. }
  1181. if( repaintMaterialInspector )
  1182. {
  1183. if( ASEMaterialInspector.Instance != null )
  1184. {
  1185. ASEMaterialInspector.Instance.Repaint();
  1186. }
  1187. }
  1188. if( m_checkSelectedWireHighlights )
  1189. {
  1190. m_checkSelectedWireHighlights = false;
  1191. ResetHighlightedWires();
  1192. for( int i = 0; i < m_selectedNodes.Count; i++ )
  1193. {
  1194. HighlightWiresStartingNode( m_selectedNodes[ i ] );
  1195. }
  1196. }
  1197. if( EditorGUI.EndChangeCheck() )
  1198. {
  1199. SaveIsDirty = true;
  1200. repaint = true;
  1201. }
  1202. if( drawInfo.CurrentEventType == EventType.Repaint )
  1203. {
  1204. // Revert LOD changes to LOD0 (only if it's different)
  1205. if( UIUtils.MainSkin.textField.border.left != 4 )
  1206. {
  1207. UIUtils.MainSkin.textField.border = UIUtils.RectOffsetFour;
  1208. nodeStyleOff.border = UIUtils.RectOffsetSix;
  1209. UIUtils.NodeWindowOffSquare.border = UIUtils.RectOffsetFour;
  1210. nodeStyleOn.border = UIUtils.RectOffsetSix;
  1211. UIUtils.NodeWindowOnSquare.border = UIUtils.RectOffsetSix;
  1212. nodeTitle.border.left = 6;
  1213. nodeTitle.border.right = 6;
  1214. nodeTitle.border.top = 6;
  1215. nodeTitle.border.bottom = 4;
  1216. UIUtils.NodeHeaderSquare.border = UIUtils.RectOffsetFour;
  1217. commentaryBackground.border = UIUtils.RectOffsetSix;
  1218. }
  1219. }
  1220. //if ( nullCount == m_nodes.Count )
  1221. // m_nodes.Clear();
  1222. ChangedLightingModel = false;
  1223. return repaint;
  1224. }
  1225. public bool UpdateMarkForDeletion()
  1226. {
  1227. if( m_markedForDeletion.Count != 0 )
  1228. {
  1229. DeleteMarkedForDeletionNodes();
  1230. return true;
  1231. }
  1232. return false;
  1233. }
  1234. public void DrawWires( Texture2D wireTex, DrawInfo drawInfo, bool contextPaletteActive, Vector3 contextPalettePos )
  1235. {
  1236. //Handles.BeginGUI();
  1237. //Debug.Log(GUI.depth);
  1238. // Draw connected node wires
  1239. m_wireBezierCount = 0;
  1240. for( int nodeIdx = 0; nodeIdx < m_nodes.Count; nodeIdx++ )
  1241. {
  1242. ParentNode node = m_nodes[ nodeIdx ];
  1243. if( (object)node == null )
  1244. return;
  1245. for( int inputPortIdx = 0; inputPortIdx < node.InputPorts.Count; inputPortIdx++ )
  1246. {
  1247. InputPort inputPort = node.InputPorts[ inputPortIdx ];
  1248. if( inputPort.ExternalReferences.Count > 0 && inputPort.Visible )
  1249. {
  1250. bool cleanInvalidConnections = false;
  1251. for( int wireIdx = 0; wireIdx < inputPort.ExternalReferences.Count; wireIdx++ )
  1252. {
  1253. WireReference reference = inputPort.ExternalReferences[ wireIdx ];
  1254. if( reference.NodeId != -1 && reference.PortId != -1 )
  1255. {
  1256. ParentNode outputNode = GetNode( reference.NodeId );
  1257. if( outputNode != null )
  1258. {
  1259. OutputPort outputPort = outputNode.GetOutputPortByUniqueId( reference.PortId );
  1260. Vector3 endPos = new Vector3( inputPort.Position.x, inputPort.Position.y );
  1261. Vector3 startPos = new Vector3( outputPort.Position.x, outputPort.Position.y );
  1262. float x = ( startPos.x < endPos.x ) ? startPos.x : endPos.x;
  1263. float y = ( startPos.y < endPos.y ) ? startPos.y : endPos.y;
  1264. float width = Mathf.Abs( startPos.x - endPos.x ) + outputPort.Position.width;
  1265. float height = Mathf.Abs( startPos.y - endPos.y ) + outputPort.Position.height;
  1266. Rect portsBoundingBox = new Rect( x, y, width, height );
  1267. bool isVisible = node.IsVisible || outputNode.IsVisible;
  1268. if( !isVisible )
  1269. {
  1270. isVisible = drawInfo.TransformedCameraArea.Overlaps( portsBoundingBox );
  1271. }
  1272. if( isVisible )
  1273. {
  1274. Rect bezierBB = DrawBezier( drawInfo.InvertedZoom, startPos, endPos, inputPort.DataType, outputPort.DataType, node.GetInputPortVisualDataTypeByArrayIdx( inputPortIdx ), outputNode.GetOutputPortVisualDataTypeById( reference.PortId ), reference.WireStatus, wireTex, node, outputNode );
  1275. bezierBB.x -= Constants.OUTSIDE_WIRE_MARGIN;
  1276. bezierBB.y -= Constants.OUTSIDE_WIRE_MARGIN;
  1277. bezierBB.width += Constants.OUTSIDE_WIRE_MARGIN * 2;
  1278. bezierBB.height += Constants.OUTSIDE_WIRE_MARGIN * 2;
  1279. if( m_wireBezierCount < m_bezierReferences.Count )
  1280. {
  1281. m_bezierReferences[ m_wireBezierCount ].UpdateInfo( ref bezierBB, inputPort.NodeId, inputPort.PortId, outputPort.NodeId, outputPort.PortId );
  1282. }
  1283. else
  1284. {
  1285. m_bezierReferences.Add( new WireBezierReference( ref bezierBB, inputPort.NodeId, inputPort.PortId, outputPort.NodeId, outputPort.PortId ) );
  1286. }
  1287. m_wireBezierCount++;
  1288. }
  1289. }
  1290. else
  1291. {
  1292. if( DebugConsoleWindow.DeveloperMode )
  1293. UIUtils.ShowMessage( "Detected Invalid connection from node " + node.UniqueId + " port " + inputPortIdx + " to Node " + reference.NodeId + " port " + reference.PortId, MessageSeverity.Error );
  1294. cleanInvalidConnections = true;
  1295. inputPort.ExternalReferences[ wireIdx ].Invalidate();
  1296. }
  1297. }
  1298. }
  1299. if( cleanInvalidConnections )
  1300. {
  1301. inputPort.RemoveInvalidConnections();
  1302. }
  1303. }
  1304. }
  1305. }
  1306. //Draw selected wire
  1307. if( m_parentWindow.WireReferenceUtils.ValidReferences() )
  1308. {
  1309. if( m_parentWindow.WireReferenceUtils.InputPortReference.IsValid )
  1310. {
  1311. InputPort inputPort = GetNode( m_parentWindow.WireReferenceUtils.InputPortReference.NodeId ).GetInputPortByUniqueId( m_parentWindow.WireReferenceUtils.InputPortReference.PortId );
  1312. Vector3 endPos = Vector3.zero;
  1313. if( m_parentWindow.WireReferenceUtils.SnapEnabled )
  1314. {
  1315. Vector2 pos = ( m_parentWindow.WireReferenceUtils.SnapPosition + drawInfo.CameraOffset ) * drawInfo.InvertedZoom;
  1316. endPos = new Vector3( pos.x, pos.y ) + UIUtils.ScaledPortsDelta;
  1317. }
  1318. else
  1319. {
  1320. endPos = contextPaletteActive ? contextPalettePos : new Vector3( Event.current.mousePosition.x, Event.current.mousePosition.y );
  1321. }
  1322. Vector3 startPos = new Vector3( inputPort.Position.x, inputPort.Position.y );
  1323. DrawBezier( drawInfo.InvertedZoom, endPos, startPos, inputPort.DataType, inputPort.DataType, inputPort.DataType, inputPort.DataType, WireStatus.Default, wireTex );
  1324. }
  1325. if( m_parentWindow.WireReferenceUtils.OutputPortReference.IsValid )
  1326. {
  1327. OutputPort outputPort = GetNode( m_parentWindow.WireReferenceUtils.OutputPortReference.NodeId ).GetOutputPortByUniqueId( m_parentWindow.WireReferenceUtils.OutputPortReference.PortId );
  1328. Vector3 endPos = Vector3.zero;
  1329. if( m_parentWindow.WireReferenceUtils.SnapEnabled )
  1330. {
  1331. Vector2 pos = ( m_parentWindow.WireReferenceUtils.SnapPosition + drawInfo.CameraOffset ) * drawInfo.InvertedZoom;
  1332. endPos = new Vector3( pos.x, pos.y ) + UIUtils.ScaledPortsDelta;
  1333. }
  1334. else
  1335. {
  1336. endPos = contextPaletteActive ? contextPalettePos : new Vector3( Event.current.mousePosition.x, Event.current.mousePosition.y );
  1337. }
  1338. Vector3 startPos = new Vector3( outputPort.Position.x, outputPort.Position.y );
  1339. DrawBezier( drawInfo.InvertedZoom, startPos, endPos, outputPort.DataType, outputPort.DataType, outputPort.DataType, outputPort.DataType, WireStatus.Default, wireTex );
  1340. }
  1341. }
  1342. //Handles.EndGUI();
  1343. }
  1344. Rect DrawBezier( float invertedZoom, Vector3 startPos, Vector3 endPos, WirePortDataType inputDataType, WirePortDataType outputDataType, WirePortDataType inputVisualDataType, WirePortDataType outputVisualDataType, WireStatus wireStatus, Texture2D wireTex, ParentNode inputNode = null, ParentNode outputNode = null )
  1345. {
  1346. startPos += UIUtils.ScaledPortsDelta;
  1347. endPos += UIUtils.ScaledPortsDelta;
  1348. // Calculate the 4 points for bezier taking into account wire nodes and their automatic tangents
  1349. float mag = ( endPos - startPos ).magnitude;
  1350. float resizedMag = Mathf.Min( mag * 0.66f, Constants.HORIZONTAL_TANGENT_SIZE * invertedZoom );
  1351. Vector3 startTangent = new Vector3( startPos.x + resizedMag, startPos.y );
  1352. Vector3 endTangent = new Vector3( endPos.x - resizedMag, endPos.y );
  1353. if( (object)inputNode != null && inputNode.GetType() == typeof( WireNode ) )
  1354. endTangent = endPos + ( ( inputNode as WireNode ).TangentDirection ) * mag * 0.33f;
  1355. if( (object)outputNode != null && outputNode.GetType() == typeof( WireNode ) )
  1356. startTangent = startPos - ( ( outputNode as WireNode ).TangentDirection ) * mag * 0.33f;
  1357. ///////////////Draw tangents
  1358. //Rect box1 = new Rect( new Vector2( startTangent.x, startTangent.y ), new Vector2( 10, 10 ) );
  1359. //box1.x -= box1.width * 0.5f;
  1360. //box1.y -= box1.height * 0.5f;
  1361. //GUI.Label( box1, string.Empty, UIUtils.Box );
  1362. //Rect box2 = new Rect( new Vector2( endTangent.x, endTangent.y ), new Vector2( 10, 10 ) );
  1363. //box2.x -= box2.width * 0.5f;
  1364. //box2.y -= box2.height * 0.5f;
  1365. //GUI.Label( box2, string.Empty, UIUtils.Box );
  1366. //m_auxRect.Set( 0, 0, UIUtils.CurrentWindow.position.width, UIUtils.CurrentWindow.position.height );
  1367. //GLDraw.BeginGroup( m_auxRect );
  1368. int ty = 1;
  1369. float wireThickness = 0;
  1370. if( ParentWindow.Options.MultiLinePorts )
  1371. {
  1372. GLDraw.MultiLine = true;
  1373. Shader.SetGlobalFloat( "_InvertedZoom", invertedZoom );
  1374. WirePortDataType smallest = ( (int)outputDataType < (int)inputDataType ? outputDataType : inputDataType );
  1375. smallest = ( (int)smallest < (int)outputVisualDataType ? smallest : outputVisualDataType );
  1376. smallest = ( (int)smallest < (int)inputVisualDataType ? smallest : inputVisualDataType );
  1377. switch( smallest )
  1378. {
  1379. case WirePortDataType.FLOAT2: ty = 2; break;
  1380. case WirePortDataType.FLOAT3: ty = 3; break;
  1381. case WirePortDataType.FLOAT4:
  1382. case WirePortDataType.COLOR:
  1383. {
  1384. ty = 4;
  1385. }
  1386. break;
  1387. default: ty = 1; break;
  1388. }
  1389. wireThickness = Mathf.Lerp( Constants.WIRE_WIDTH * ( ty * invertedZoom * -0.05f + 0.15f ), Constants.WIRE_WIDTH * ( ty * invertedZoom * 0.175f + 0.3f ), invertedZoom + 0.4f );
  1390. }
  1391. else
  1392. {
  1393. GLDraw.MultiLine = false;
  1394. wireThickness = Mathf.Lerp( Constants.WIRE_WIDTH * ( invertedZoom * -0.05f + 0.15f ), Constants.WIRE_WIDTH * ( invertedZoom * 0.175f + 0.3f ), invertedZoom + 0.4f );
  1395. }
  1396. Rect boundBox = new Rect();
  1397. int segments = 11;
  1398. if( LodLevel <= ParentGraph.NodeLOD.LOD4 )
  1399. segments = Mathf.Clamp( Mathf.FloorToInt( mag * 0.2f * invertedZoom ), 11, 35 );
  1400. else
  1401. segments = (int)( invertedZoom * 14.28f * 11 );
  1402. if( ParentWindow.Options.ColoredPorts && wireStatus != WireStatus.Highlighted )
  1403. boundBox = GLDraw.DrawBezier( startPos, startTangent, endPos, endTangent, UIUtils.GetColorForDataType( outputVisualDataType, false, false ), UIUtils.GetColorForDataType( inputVisualDataType, false, false ), wireThickness, segments, ty );
  1404. else
  1405. boundBox = GLDraw.DrawBezier( startPos, startTangent, endPos, endTangent, UIUtils.GetColorFromWireStatus( wireStatus ), wireThickness, segments, ty );
  1406. //GLDraw.EndGroup();
  1407. //GUI.Box( m_auxRect, string.Empty, UIUtils.CurrentWindow.CustomStylesInstance.Box );
  1408. //GUI.Box( boundBox, string.Empty, UIUtils.CurrentWindow.CustomStylesInstance.Box );
  1409. //if ( UIUtils.CurrentWindow.Options.ColoredPorts && wireStatus != WireStatus.Highlighted )
  1410. // Handles.DrawBezier( startPos, endPos, startTangent, endTangent, UIUtils.GetColorForDataType( outputDataType, false, false ), wireTex, wiresTickness );
  1411. //else
  1412. // Handles.DrawBezier( startPos, endPos, startTangent, endTangent, UIUtils.GetColorFromWireStatus( wireStatus ), wireTex, wiresTickness );
  1413. //Handles.DrawLine( startPos, startTangent );
  1414. //Handles.DrawLine( endPos, endTangent );
  1415. float extraBound = 30 * invertedZoom;
  1416. boundBox.xMin -= extraBound;
  1417. boundBox.xMax += extraBound;
  1418. boundBox.yMin -= extraBound;
  1419. boundBox.yMax += extraBound;
  1420. return boundBox;
  1421. }
  1422. public void DrawBezierBoundingBox()
  1423. {
  1424. for( int i = 0; i < m_wireBezierCount; i++ )
  1425. {
  1426. m_bezierReferences[ i ].DebugDraw();
  1427. }
  1428. }
  1429. public WireBezierReference GetWireBezierInPos( Vector2 position )
  1430. {
  1431. for( int i = 0; i < m_wireBezierCount; i++ )
  1432. {
  1433. if( m_bezierReferences[ i ].Contains( position ) )
  1434. return m_bezierReferences[ i ];
  1435. }
  1436. return null;
  1437. }
  1438. public List<WireBezierReference> GetWireBezierListInPos( Vector2 position )
  1439. {
  1440. List<WireBezierReference> list = new List<WireBezierReference>();
  1441. for( int i = 0; i < m_wireBezierCount; i++ )
  1442. {
  1443. if( m_bezierReferences[ i ].Contains( position ) )
  1444. list.Add( m_bezierReferences[ i ] );
  1445. }
  1446. return list;
  1447. }
  1448. public void MoveSelectedNodes( Vector2 delta, bool snap = false )
  1449. {
  1450. //bool validMovement = delta.magnitude > 0.001f;
  1451. //if ( validMovement )
  1452. //{
  1453. // Undo.RegisterCompleteObjectUndo( ParentWindow, Constants.UndoMoveNodesId );
  1454. // for ( int i = 0; i < m_selectedNodes.Count; i++ )
  1455. // {
  1456. // if ( !m_selectedNodes[ i ].MovingInFrame )
  1457. // {
  1458. // Undo.RecordObject( m_selectedNodes[ i ], Constants.UndoMoveNodesId );
  1459. // m_selectedNodes[ i ].Move( delta, snap );
  1460. // }
  1461. // }
  1462. // IsDirty = true;
  1463. //}
  1464. bool performUndo = delta.magnitude > 0.01f;
  1465. if( performUndo )
  1466. {
  1467. Undo.RegisterCompleteObjectUndo( ParentWindow, Constants.UndoMoveNodesId );
  1468. Undo.RegisterCompleteObjectUndo( this, Constants.UndoMoveNodesId );
  1469. }
  1470. for( int i = 0; i < m_selectedNodes.Count; i++ )
  1471. {
  1472. if( !m_selectedNodes[ i ].MovingInFrame )
  1473. {
  1474. if( performUndo )
  1475. m_selectedNodes[ i ].RecordObject( Constants.UndoMoveNodesId );
  1476. m_selectedNodes[ i ].Move( delta, snap );
  1477. }
  1478. }
  1479. IsDirty = true;
  1480. }
  1481. public void SetConnection( int InNodeId, int InPortId, int OutNodeId, int OutPortId )
  1482. {
  1483. ParentNode inNode = GetNode( InNodeId );
  1484. ParentNode outNode = GetNode( OutNodeId );
  1485. InputPort inputPort = null;
  1486. OutputPort outputPort = null;
  1487. if( inNode != null && outNode != null )
  1488. {
  1489. inputPort = inNode.GetInputPortByUniqueId( InPortId );
  1490. outputPort = outNode.GetOutputPortByUniqueId( OutPortId );
  1491. if( inputPort != null && outputPort != null )
  1492. {
  1493. if( inputPort.IsConnectedTo( OutNodeId, OutPortId ) || outputPort.IsConnectedTo( InNodeId, InPortId ) )
  1494. {
  1495. if( DebugConsoleWindow.DeveloperMode )
  1496. UIUtils.ShowMessage( "Node/Port already connected " + InNodeId, MessageSeverity.Error );
  1497. return;
  1498. }
  1499. if( !inputPort.CheckValidType( outputPort.DataType ) )
  1500. {
  1501. if( DebugConsoleWindow.DeveloperMode )
  1502. UIUtils.ShowIncompatiblePortMessage( true, inNode, inputPort, outNode, outputPort );
  1503. return;
  1504. }
  1505. if( !outputPort.CheckValidType( inputPort.DataType ) )
  1506. {
  1507. if( DebugConsoleWindow.DeveloperMode )
  1508. UIUtils.ShowIncompatiblePortMessage( false, outNode, outputPort, inNode, inputPort );
  1509. return;
  1510. }
  1511. if( !inputPort.Available || !outputPort.Available )
  1512. {
  1513. if( DebugConsoleWindow.DeveloperMode )
  1514. UIUtils.ShowMessage( "Ports not available to connection", MessageSeverity.Warning );
  1515. return;
  1516. }
  1517. if( inputPort.ConnectTo( OutNodeId, OutPortId, outputPort.DataType, false ) )
  1518. {
  1519. inNode.OnInputPortConnected( InPortId, OutNodeId, OutPortId );
  1520. }
  1521. if( outputPort.ConnectTo( InNodeId, InPortId, inputPort.DataType, inputPort.TypeLocked ) )
  1522. {
  1523. outNode.OnOutputPortConnected( OutPortId, InNodeId, InPortId );
  1524. }
  1525. }
  1526. else if( (object)inputPort == null )
  1527. {
  1528. if( DebugConsoleWindow.DeveloperMode )
  1529. UIUtils.ShowMessage( "Input Port " + InPortId + " doesn't exist on node " + InNodeId, MessageSeverity.Error );
  1530. }
  1531. else
  1532. {
  1533. if( DebugConsoleWindow.DeveloperMode )
  1534. UIUtils.ShowMessage( "Output Port " + OutPortId + " doesn't exist on node " + OutNodeId, MessageSeverity.Error );
  1535. }
  1536. }
  1537. else if( (object)inNode == null )
  1538. {
  1539. if( DebugConsoleWindow.DeveloperMode )
  1540. UIUtils.ShowMessage( "Input node " + InNodeId + " doesn't exist", MessageSeverity.Error );
  1541. }
  1542. else
  1543. {
  1544. if( DebugConsoleWindow.DeveloperMode )
  1545. UIUtils.ShowMessage( "Output node " + OutNodeId + " doesn't exist", MessageSeverity.Error );
  1546. }
  1547. }
  1548. public void CreateConnection( int inNodeId, int inPortId, int outNodeId, int outPortId, bool registerUndo = true )
  1549. {
  1550. ParentNode outputNode = GetNode( outNodeId );
  1551. if( outputNode != null )
  1552. {
  1553. OutputPort outputPort = outputNode.GetOutputPortByUniqueId( outPortId );
  1554. if( outputPort != null )
  1555. {
  1556. ParentNode inputNode = GetNode( inNodeId );
  1557. InputPort inputPort = inputNode.GetInputPortByUniqueId( inPortId );
  1558. if( !inputPort.CheckValidType( outputPort.DataType ) )
  1559. {
  1560. UIUtils.ShowIncompatiblePortMessage( true, inputNode, inputPort, outputNode, outputPort );
  1561. return;
  1562. }
  1563. if( !outputPort.CheckValidType( inputPort.DataType ) )
  1564. {
  1565. UIUtils.ShowIncompatiblePortMessage( false, outputNode, outputPort, inputNode, inputPort );
  1566. return;
  1567. }
  1568. inputPort.DummyAdd( outputPort.NodeId, outputPort.PortId );
  1569. outputPort.DummyAdd( inNodeId, inPortId );
  1570. if( UIUtils.DetectNodeLoopsFrom( inputNode, new Dictionary<int, int>() ) )
  1571. {
  1572. inputPort.DummyRemove();
  1573. outputPort.DummyRemove();
  1574. m_parentWindow.WireReferenceUtils.InvalidateReferences();
  1575. UIUtils.ShowMessage( "Infinite Loop detected" );
  1576. Event.current.Use();
  1577. return;
  1578. }
  1579. inputPort.DummyRemove();
  1580. outputPort.DummyRemove();
  1581. if( inputPort.IsConnected )
  1582. {
  1583. DeleteConnection( true, inNodeId, inPortId, true, false, registerUndo );
  1584. }
  1585. //link output to input
  1586. if( outputPort.ConnectTo( inNodeId, inPortId, inputPort.DataType, inputPort.TypeLocked ) )
  1587. outputNode.OnOutputPortConnected( outputPort.PortId, inNodeId, inPortId );
  1588. //link input to output
  1589. if( inputPort.ConnectTo( outputPort.NodeId, outputPort.PortId, outputPort.DataType, inputPort.TypeLocked ) )
  1590. inputNode.OnInputPortConnected( inPortId, outputNode.UniqueId, outputPort.PortId );
  1591. MarkWireHighlights();
  1592. }
  1593. SaveIsDirty = true;
  1594. //ParentWindow.ShaderIsModified = true;
  1595. }
  1596. }
  1597. public void DeleteInvalidConnections()
  1598. {
  1599. int count = m_nodes.Count;
  1600. for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  1601. {
  1602. {
  1603. int inputCount = m_nodes[ nodeIdx ].InputPorts.Count;
  1604. for( int inputIdx = 0; inputIdx < inputCount; inputIdx++ )
  1605. {
  1606. if( !m_nodes[ nodeIdx ].InputPorts[ inputIdx ].Visible &&
  1607. m_nodes[ nodeIdx ].InputPorts[ inputIdx ].IsConnected &&
  1608. !m_nodes[ nodeIdx ].InputPorts[ inputIdx ].IsDummy )
  1609. {
  1610. DeleteConnection( true, m_nodes[ nodeIdx ].UniqueId, m_nodes[ nodeIdx ].InputPorts[ inputIdx ].PortId, true, true );
  1611. }
  1612. }
  1613. }
  1614. {
  1615. int outputCount = m_nodes[ nodeIdx ].OutputPorts.Count;
  1616. for( int outputIdx = 0; outputIdx < outputCount; outputIdx++ )
  1617. {
  1618. if( !m_nodes[ nodeIdx ].OutputPorts[ outputIdx ].Visible && m_nodes[ nodeIdx ].OutputPorts[ outputIdx ].IsConnected )
  1619. {
  1620. DeleteConnection( false, m_nodes[ nodeIdx ].UniqueId, m_nodes[ nodeIdx ].OutputPorts[ outputIdx ].PortId, true, true );
  1621. }
  1622. }
  1623. }
  1624. }
  1625. }
  1626. public void DeleteAllConnectionFromNode( int nodeId, bool registerOnLog, bool propagateCallback, bool registerUndo )
  1627. {
  1628. ParentNode node = GetNode( nodeId );
  1629. if( (object)node == null )
  1630. return;
  1631. DeleteAllConnectionFromNode( node, registerOnLog, propagateCallback, registerUndo );
  1632. }
  1633. public void DeleteAllConnectionFromNode( ParentNode node, bool registerOnLog, bool propagateCallback, bool registerUndo )
  1634. {
  1635. for( int i = 0; i < node.InputPorts.Count; i++ )
  1636. {
  1637. if( node.InputPorts[ i ].IsConnected )
  1638. DeleteConnection( true, node.UniqueId, node.InputPorts[ i ].PortId, registerOnLog, propagateCallback, registerUndo );
  1639. }
  1640. for( int i = 0; i < node.OutputPorts.Count; i++ )
  1641. {
  1642. if( node.OutputPorts[ i ].IsConnected )
  1643. DeleteConnection( false, node.UniqueId, node.OutputPorts[ i ].PortId, registerOnLog, propagateCallback, registerUndo );
  1644. }
  1645. }
  1646. public void DeleteConnection( bool isInput, int nodeId, int portId, bool registerOnLog, bool propagateCallback, bool registerUndo = true )
  1647. {
  1648. ParentNode node = GetNode( nodeId );
  1649. if( (object)node == null )
  1650. return;
  1651. if( registerUndo )
  1652. {
  1653. UIUtils.MarkUndoAction();
  1654. Undo.RegisterCompleteObjectUndo( ParentWindow, Constants.UndoDeleteConnectionId );
  1655. Undo.RegisterCompleteObjectUndo( this, Constants.UndoDeleteConnectionId );
  1656. node.RecordObject( Constants.UndoDeleteConnectionId );
  1657. }
  1658. if( isInput )
  1659. {
  1660. InputPort inputPort = node.GetInputPortByUniqueId( portId );
  1661. if( inputPort != null && inputPort.IsConnected )
  1662. {
  1663. if( node.ConnStatus == NodeConnectionStatus.Connected )
  1664. {
  1665. node.DeactivateInputPortNode( portId, false );
  1666. //inputPort.GetOutputNode().DeactivateNode( portId, false );
  1667. m_checkSelectedWireHighlights = true;
  1668. }
  1669. for( int i = 0; i < inputPort.ExternalReferences.Count; i++ )
  1670. {
  1671. WireReference inputReference = inputPort.ExternalReferences[ i ];
  1672. ParentNode outputNode = GetNode( inputReference.NodeId );
  1673. if( registerUndo )
  1674. outputNode.RecordObject( Constants.UndoDeleteConnectionId );
  1675. outputNode.GetOutputPortByUniqueId( inputReference.PortId ).InvalidateConnection( inputPort.NodeId, inputPort.PortId );
  1676. if( propagateCallback )
  1677. outputNode.OnOutputPortDisconnected( inputReference.PortId );
  1678. }
  1679. inputPort.InvalidateAllConnections();
  1680. if( propagateCallback )
  1681. node.OnInputPortDisconnected( portId );
  1682. }
  1683. }
  1684. else
  1685. {
  1686. OutputPort outputPort = node.GetOutputPortByUniqueId( portId );
  1687. if( outputPort != null && outputPort.IsConnected )
  1688. {
  1689. if( propagateCallback )
  1690. node.OnOutputPortDisconnected( portId );
  1691. for( int i = 0; i < outputPort.ExternalReferences.Count; i++ )
  1692. {
  1693. WireReference outputReference = outputPort.ExternalReferences[ i ];
  1694. ParentNode inputNode = GetNode( outputReference.NodeId );
  1695. if( registerUndo )
  1696. inputNode.RecordObject( Constants.UndoDeleteConnectionId );
  1697. if( inputNode.ConnStatus == NodeConnectionStatus.Connected )
  1698. {
  1699. node.DeactivateNode( portId, false );
  1700. m_checkSelectedWireHighlights = true;
  1701. }
  1702. inputNode.GetInputPortByUniqueId( outputReference.PortId ).InvalidateConnection( outputPort.NodeId, outputPort.PortId );
  1703. if( propagateCallback )
  1704. {
  1705. // Removing WireNodes fires this after the rewiring ( and the OnInputPortConnected callback ) which causes incorrect behaviors
  1706. // If is connected is true then we're on that case so we don't fire the OnInputPortDisconnected
  1707. if( !inputNode.GetInputPortByUniqueId( outputReference.PortId ).IsConnected )
  1708. inputNode.OnInputPortDisconnected( outputReference.PortId );
  1709. }
  1710. }
  1711. outputPort.InvalidateAllConnections();
  1712. }
  1713. }
  1714. IsDirty = true;
  1715. SaveIsDirty = true;
  1716. }
  1717. //public void DeleteSelectedNodes()
  1718. //{
  1719. // bool invalidateMasterNode = false;
  1720. // int count = m_selectedNodes.Count;
  1721. // for( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  1722. // {
  1723. // ParentNode node = m_selectedNodes[ nodeIdx ];
  1724. // if( node.UniqueId == m_masterNodeId )
  1725. // {
  1726. // invalidateMasterNode = true;
  1727. // }
  1728. // else
  1729. // {
  1730. // DestroyNode( node );
  1731. // }
  1732. // }
  1733. // if( invalidateMasterNode )
  1734. // {
  1735. // CurrentOutputNode.Selected = false;
  1736. // }
  1737. // //Clear all references
  1738. // m_selectedNodes.Clear();
  1739. // IsDirty = true;
  1740. //}
  1741. public void DeleteNodesOnArray( ref ParentNode[] nodeArray )
  1742. {
  1743. bool invalidateMasterNode = false;
  1744. for( int nodeIdx = 0; nodeIdx < nodeArray.Length; nodeIdx++ )
  1745. {
  1746. ParentNode node = nodeArray[ nodeIdx ];
  1747. if( node.UniqueId == m_masterNodeId )
  1748. {
  1749. FunctionOutput fout = node as FunctionOutput;
  1750. if( fout != null )
  1751. {
  1752. for( int i = 0; i < m_nodes.Count; i++ )
  1753. {
  1754. FunctionOutput secondfout = m_nodes[ i ] as FunctionOutput;
  1755. if( secondfout != null && secondfout != fout )
  1756. {
  1757. secondfout.Function = fout.Function;
  1758. AssignMasterNode( secondfout, false );
  1759. DeselectNode( fout );
  1760. DestroyNode( fout );
  1761. break;
  1762. }
  1763. }
  1764. }
  1765. invalidateMasterNode = true;
  1766. }
  1767. else
  1768. {
  1769. DeselectNode( node );
  1770. DestroyNode( node );
  1771. }
  1772. nodeArray[ nodeIdx ] = null;
  1773. }
  1774. if( invalidateMasterNode && CurrentMasterNode != null )
  1775. {
  1776. CurrentMasterNode.Selected = false;
  1777. }
  1778. //Clear all references
  1779. nodeArray = null;
  1780. IsDirty = true;
  1781. }
  1782. public void MarkWireNodeSequence( WireNode node, bool isInput )
  1783. {
  1784. if( node == null )
  1785. {
  1786. return;
  1787. }
  1788. if( m_markedForDeletion.Contains( node ) )
  1789. return;
  1790. m_markedForDeletion.Add( node );
  1791. if( isInput && node.InputPorts[ 0 ].IsConnected )
  1792. {
  1793. MarkWireNodeSequence( GetNode( node.InputPorts[ 0 ].ExternalReferences[ 0 ].NodeId ) as WireNode, isInput );
  1794. }
  1795. else if( !isInput && node.OutputPorts[ 0 ].IsConnected )
  1796. {
  1797. MarkWireNodeSequence( GetNode( node.OutputPorts[ 0 ].ExternalReferences[ 0 ].NodeId ) as WireNode, isInput );
  1798. }
  1799. }
  1800. public void UndoableDeleteSelectedNodes( List<ParentNode> nodeList )
  1801. {
  1802. if( nodeList.Count == 0 )
  1803. return;
  1804. List<ParentNode> validNode = new List<ParentNode>();
  1805. for( int i = 0; i < nodeList.Count; i++ )
  1806. {
  1807. if( nodeList[ i ] != null && nodeList[ i ].UniqueId != m_masterNodeId )
  1808. {
  1809. validNode.Add( nodeList[ i ] );
  1810. }
  1811. }
  1812. UIUtils.ClearUndoHelper();
  1813. ParentNode[] selectedNodes = new ParentNode[ validNode.Count ];
  1814. for( int i = 0; i < selectedNodes.Length; i++ )
  1815. {
  1816. if( validNode[ i ] != null )
  1817. {
  1818. selectedNodes[ i ] = validNode[ i ];
  1819. UIUtils.CheckUndoNode( selectedNodes[ i ] );
  1820. }
  1821. }
  1822. //Check nodes connected to deleted nodes to preserve connections on undo
  1823. List<ParentNode> extraNodes = new List<ParentNode>();
  1824. for( int selectedNodeIdx = 0; selectedNodeIdx < selectedNodes.Length; selectedNodeIdx++ )
  1825. {
  1826. // Check inputs
  1827. if( selectedNodes[ selectedNodeIdx ] != null )
  1828. {
  1829. int inputIdxCount = selectedNodes[ selectedNodeIdx ].InputPorts.Count;
  1830. if( inputIdxCount > 0 )
  1831. {
  1832. for( int inputIdx = 0; inputIdx < inputIdxCount; inputIdx++ )
  1833. {
  1834. if( selectedNodes[ selectedNodeIdx ].InputPorts[ inputIdx ].IsConnected )
  1835. {
  1836. int nodeIdx = selectedNodes[ selectedNodeIdx ].InputPorts[ inputIdx ].ExternalReferences[ 0 ].NodeId;
  1837. if( nodeIdx > -1 )
  1838. {
  1839. ParentNode node = GetNode( nodeIdx );
  1840. if( node != null && UIUtils.CheckUndoNode( node ) )
  1841. {
  1842. extraNodes.Add( node );
  1843. }
  1844. }
  1845. }
  1846. }
  1847. }
  1848. }
  1849. // Check outputs
  1850. if( selectedNodes[ selectedNodeIdx ] != null )
  1851. {
  1852. int outputIdxCount = selectedNodes[ selectedNodeIdx ].OutputPorts.Count;
  1853. if( outputIdxCount > 0 )
  1854. {
  1855. for( int outputIdx = 0; outputIdx < outputIdxCount; outputIdx++ )
  1856. {
  1857. int inputIdxCount = selectedNodes[ selectedNodeIdx ].OutputPorts[ outputIdx ].ExternalReferences.Count;
  1858. if( inputIdxCount > 0 )
  1859. {
  1860. for( int inputIdx = 0; inputIdx < inputIdxCount; inputIdx++ )
  1861. {
  1862. int nodeIdx = selectedNodes[ selectedNodeIdx ].OutputPorts[ outputIdx ].ExternalReferences[ inputIdx ].NodeId;
  1863. if( nodeIdx > -1 )
  1864. {
  1865. ParentNode node = GetNode( nodeIdx );
  1866. if( UIUtils.CheckUndoNode( node ) )
  1867. {
  1868. extraNodes.Add( node );
  1869. }
  1870. }
  1871. }
  1872. }
  1873. }
  1874. }
  1875. }
  1876. }
  1877. UIUtils.ClearUndoHelper();
  1878. //Record deleted nodes
  1879. UIUtils.MarkUndoAction();
  1880. Undo.RegisterCompleteObjectUndo( ParentWindow, Constants.UndoDeleteNodeId );
  1881. Undo.RegisterCompleteObjectUndo( this, Constants.UndoDeleteNodeId );
  1882. Undo.RecordObjects( selectedNodes, Constants.UndoDeleteNodeId );
  1883. Undo.RecordObjects( extraNodes.ToArray(), Constants.UndoDeleteNodeId );
  1884. //Record deleting connections
  1885. for( int i = 0; i < selectedNodes.Length; i++ )
  1886. {
  1887. CurrentOutputNode.Selected = false;
  1888. selectedNodes[ i ].Alive = false;
  1889. DeleteAllConnectionFromNode( selectedNodes[ i ], false, true, true );
  1890. }
  1891. //Delete
  1892. DeleteNodesOnArray( ref selectedNodes );
  1893. extraNodes.Clear();
  1894. extraNodes = null;
  1895. EditorUtility.SetDirty( ParentWindow );
  1896. ParentWindow.ForceRepaint();
  1897. }
  1898. public void DeleteMarkedForDeletionNodes()
  1899. {
  1900. UndoableDeleteSelectedNodes( m_markedForDeletion );
  1901. m_markedForDeletion.Clear();
  1902. IsDirty = true;
  1903. //bool invalidateMasterNode = false;
  1904. //int count = m_markedForDeletion.Count;
  1905. //for ( int nodeIdx = 0; nodeIdx < count; nodeIdx++ )
  1906. //{
  1907. // ParentNode node = m_markedForDeletion[ nodeIdx ];
  1908. // if ( node.UniqueId == m_masterNodeId )
  1909. // {
  1910. // invalidateMasterNode = true;
  1911. // }
  1912. // else
  1913. // {
  1914. // if ( node.Selected )
  1915. // {
  1916. // m_selectedNodes.Remove( node );
  1917. // node.Selected = false;
  1918. // }
  1919. // DestroyNode( node );
  1920. // }
  1921. //}
  1922. //if ( invalidateMasterNode )
  1923. //{
  1924. // CurrentMasterNode.Selected = false;
  1925. //}
  1926. ////Clear all references
  1927. //m_markedForDeletion.Clear();
  1928. //IsDirty = true;
  1929. }
  1930. public void DestroyNode( int nodeId )
  1931. {
  1932. ParentNode node = GetNode( nodeId );
  1933. DestroyNode( node );
  1934. }
  1935. public void DestroyNode( ParentNode node, bool registerUndo = true, bool destroyMasterNode = false )
  1936. {
  1937. if( node == null )
  1938. {
  1939. UIUtils.ShowMessage( "Attempting to destroying a inexistant node ", MessageSeverity.Warning );
  1940. return;
  1941. }
  1942. if( node.ConnStatus == NodeConnectionStatus.Connected && !m_checkSelectedWireHighlights )
  1943. {
  1944. ResetHighlightedWires();
  1945. m_checkSelectedWireHighlights = true;
  1946. }
  1947. //TODO: check better placement of this code (reconnects wires from wire nodes)
  1948. //if ( node.GetType() == typeof( WireNode ) )
  1949. //{
  1950. // if ( node.InputPorts[ 0 ].ExternalReferences != null && node.InputPorts[ 0 ].ExternalReferences.Count > 0 )
  1951. // {
  1952. // WireReference backPort = node.InputPorts[ 0 ].ExternalReferences[ 0 ];
  1953. // for ( int i = 0; i < node.OutputPorts[ 0 ].ExternalReferences.Count; i++ )
  1954. // {
  1955. // UIUtils.CurrentWindow.ConnectInputToOutput( node.OutputPorts[ 0 ].ExternalReferences[ i ].NodeId, node.OutputPorts[ 0 ].ExternalReferences[ i ].PortId, backPort.NodeId, backPort.PortId );
  1956. // }
  1957. // }
  1958. //}
  1959. if( destroyMasterNode || ( node.UniqueId != m_masterNodeId && !( node is TemplateMultiPassMasterNode )/*!m_multiPassMasterNodes.HasNode( node.UniqueId )*/ ) )
  1960. {
  1961. m_nodeGrid.RemoveNodeFromGrid( node, false );
  1962. //Send Deactivation signal if active
  1963. if( node.ConnStatus == NodeConnectionStatus.Connected )
  1964. {
  1965. node.DeactivateNode( -1, true );
  1966. }
  1967. //Invalidate references
  1968. //Invalidate input references
  1969. for( int inputPortIdx = 0; inputPortIdx < node.InputPorts.Count; inputPortIdx++ )
  1970. {
  1971. InputPort inputPort = node.InputPorts[ inputPortIdx ];
  1972. if( inputPort.IsConnected )
  1973. {
  1974. for( int wireIdx = 0; wireIdx < inputPort.ExternalReferences.Count; wireIdx++ )
  1975. {
  1976. WireReference inputReference = inputPort.ExternalReferences[ wireIdx ];
  1977. ParentNode outputNode = GetNode( inputReference.NodeId );
  1978. outputNode.GetOutputPortByUniqueId( inputReference.PortId ).InvalidateConnection( inputPort.NodeId, inputPort.PortId );
  1979. outputNode.OnOutputPortDisconnected( inputReference.PortId );
  1980. }
  1981. inputPort.InvalidateAllConnections();
  1982. }
  1983. }
  1984. //Invalidate output reference
  1985. for( int outputPortIdx = 0; outputPortIdx < node.OutputPorts.Count; outputPortIdx++ )
  1986. {
  1987. OutputPort outputPort = node.OutputPorts[ outputPortIdx ];
  1988. if( outputPort.IsConnected )
  1989. {
  1990. for( int wireIdx = 0; wireIdx < outputPort.ExternalReferences.Count; wireIdx++ )
  1991. {
  1992. WireReference outputReference = outputPort.ExternalReferences[ wireIdx ];
  1993. ParentNode outnode = GetNode( outputReference.NodeId );
  1994. if( outnode != null )
  1995. {
  1996. outnode.GetInputPortByUniqueId( outputReference.PortId ).InvalidateConnection( outputPort.NodeId, outputPort.PortId );
  1997. outnode.OnInputPortDisconnected( outputReference.PortId );
  1998. }
  1999. }
  2000. outputPort.InvalidateAllConnections();
  2001. }
  2002. }
  2003. //Remove node from main list
  2004. //Undo.RecordObject( node, "Destroying node " + ( node.Attributes != null? node.Attributes.Name: node.GetType().ToString() ) );
  2005. if( registerUndo )
  2006. {
  2007. UIUtils.MarkUndoAction();
  2008. Undo.RegisterCompleteObjectUndo( ParentWindow, Constants.UndoDeleteNodeId );
  2009. Undo.RegisterCompleteObjectUndo( this, Constants.UndoDeleteNodeId );
  2010. node.RecordObjectOnDestroy( Constants.UndoDeleteNodeId );
  2011. }
  2012. if( OnNodeRemovedEvent != null )
  2013. OnNodeRemovedEvent( node );
  2014. m_nodes.Remove( node );
  2015. m_nodesDict.Remove( node.UniqueId );
  2016. node.Destroy();
  2017. if( registerUndo )
  2018. Undo.DestroyObjectImmediate( node );
  2019. else
  2020. DestroyImmediate( node );
  2021. IsDirty = true;
  2022. m_markToReOrder = true;
  2023. }
  2024. //else if( node.UniqueId == m_masterNodeId && node.GetType() == typeof(FunctionOutput) )
  2025. //{
  2026. // Debug.Log( "Attempting to destroy a output node" );
  2027. // DeselectNode( node );
  2028. // UIUtils.ShowMessage( "Attempting to destroy a output node" );
  2029. //}
  2030. else
  2031. {
  2032. TemplateMultiPassMasterNode templateMasterNode = node as TemplateMultiPassMasterNode;
  2033. if( templateMasterNode != null && templateMasterNode.InvalidNode )
  2034. {
  2035. DestroyNode( node, false, true );
  2036. return;
  2037. }
  2038. DeselectNode( node );
  2039. UIUtils.ShowMessage( "Attempting to destroy a master node" );
  2040. }
  2041. }
  2042. void AddToSelectedNodes( ParentNode node )
  2043. {
  2044. node.Selected = true;
  2045. m_selectedNodes.Add( node );
  2046. node.OnNodeStoppedMovingEvent += OnNodeFinishMoving;
  2047. if( node.ConnStatus == NodeConnectionStatus.Connected )
  2048. {
  2049. HighlightWiresStartingNode( node );
  2050. }
  2051. }
  2052. void RemoveFromSelectedNodes( ParentNode node )
  2053. {
  2054. node.Selected = false;
  2055. m_selectedNodes.Remove( node );
  2056. node.OnNodeStoppedMovingEvent -= OnNodeFinishMoving;
  2057. }
  2058. public void SelectNode( ParentNode node, bool append, bool reorder )
  2059. {
  2060. if( node == null )
  2061. return;
  2062. if( append )
  2063. {
  2064. if( !m_selectedNodes.Contains( node ) )
  2065. {
  2066. AddToSelectedNodes( node );
  2067. }
  2068. }
  2069. else
  2070. {
  2071. DeSelectAll();
  2072. AddToSelectedNodes( node );
  2073. }
  2074. if( reorder && !node.ReorderLocked )
  2075. {
  2076. m_nodes.Remove( node );
  2077. m_nodes.Add( node );
  2078. m_markToReOrder = true;
  2079. }
  2080. }
  2081. public void MultipleSelection( Rect selectionArea, bool appendSelection = true )
  2082. {
  2083. if( !appendSelection )
  2084. {
  2085. for( int i = 0; i < m_nodes.Count; i++ )
  2086. {
  2087. if( selectionArea.Overlaps( m_nodes[ i ].Position, true ) )
  2088. {
  2089. RemoveFromSelectedNodes( m_nodes[ i ] );
  2090. }
  2091. }
  2092. m_markedToDeSelect = false;
  2093. ResetHighlightedWires();
  2094. }
  2095. else
  2096. {
  2097. for( int i = 0; i < m_nodes.Count; i++ )
  2098. {
  2099. if( !m_nodes[ i ].Selected && selectionArea.Overlaps( m_nodes[ i ].Position, true ) )
  2100. {
  2101. AddToSelectedNodes( m_nodes[ i ] );
  2102. }
  2103. }
  2104. }
  2105. // reorder nodes and highlight them
  2106. for( int i = 0; i < m_selectedNodes.Count; i++ )
  2107. {
  2108. if( !m_selectedNodes[ i ].ReorderLocked )
  2109. {
  2110. m_nodes.Remove( m_selectedNodes[ i ] );
  2111. m_nodes.Add( m_selectedNodes[ i ] );
  2112. m_markToReOrder = true;
  2113. if( m_selectedNodes[ i ].ConnStatus == NodeConnectionStatus.Connected )
  2114. {
  2115. HighlightWiresStartingNode( m_selectedNodes[ i ] );
  2116. }
  2117. }
  2118. }
  2119. }
  2120. public void SelectAll()
  2121. {
  2122. for( int i = 0; i < m_nodes.Count; i++ )
  2123. {
  2124. if( !m_nodes[ i ].Selected )
  2125. AddToSelectedNodes( m_nodes[ i ] );
  2126. }
  2127. }
  2128. public void SelectMasterNode()
  2129. {
  2130. if( m_masterNodeId != Constants.INVALID_NODE_ID )
  2131. {
  2132. SelectNode( CurrentMasterNode, false, false );
  2133. }
  2134. }
  2135. public void SelectOutputNode()
  2136. {
  2137. if( m_masterNodeId != Constants.INVALID_NODE_ID )
  2138. {
  2139. SelectNode( CurrentOutputNode, false, false );
  2140. }
  2141. }
  2142. public void DeselectNode( int nodeId )
  2143. {
  2144. ParentNode node = GetNode( nodeId );
  2145. if( node )
  2146. {
  2147. m_selectedNodes.Remove( node );
  2148. node.Selected = false;
  2149. }
  2150. }
  2151. public void DeselectNode( ParentNode node )
  2152. {
  2153. m_selectedNodes.Remove( node );
  2154. node.Selected = false;
  2155. PropagateHighlightDeselection( node );
  2156. }
  2157. public void DeSelectAll()
  2158. {
  2159. m_markedToDeSelect = false;
  2160. for( int i = 0; i < m_selectedNodes.Count; i++ )
  2161. {
  2162. m_selectedNodes[ i ].Selected = false;
  2163. m_selectedNodes[ i ].OnNodeStoppedMovingEvent -= OnNodeFinishMoving;
  2164. }
  2165. m_selectedNodes.Clear();
  2166. ResetHighlightedWires();
  2167. }
  2168. public void AssignMasterNode()
  2169. {
  2170. if( m_selectedNodes.Count == 1 )
  2171. {
  2172. OutputNode newOutputNode = m_selectedNodes[ 0 ] as OutputNode;
  2173. MasterNode newMasterNode = newOutputNode as MasterNode;
  2174. if( newOutputNode != null )
  2175. {
  2176. if( m_masterNodeId != Constants.INVALID_NODE_ID && m_masterNodeId != newOutputNode.UniqueId )
  2177. {
  2178. OutputNode oldOutputNode = GetNode( m_masterNodeId ) as OutputNode;
  2179. MasterNode oldMasterNode = oldOutputNode as MasterNode;
  2180. if( oldOutputNode != null )
  2181. {
  2182. oldOutputNode.IsMainOutputNode = false;
  2183. if( oldMasterNode != null )
  2184. {
  2185. oldMasterNode.ClearUpdateEvents();
  2186. }
  2187. }
  2188. }
  2189. m_masterNodeId = newOutputNode.UniqueId;
  2190. newOutputNode.IsMainOutputNode = true;
  2191. if( newMasterNode != null )
  2192. {
  2193. newMasterNode.OnMaterialUpdatedEvent += OnMaterialUpdatedEvent;
  2194. newMasterNode.OnShaderUpdatedEvent += OnShaderUpdatedEvent;
  2195. }
  2196. }
  2197. }
  2198. IsDirty = true;
  2199. }
  2200. public void AssignMasterNode( OutputNode node, bool onlyUpdateGraphId )
  2201. {
  2202. AssignMasterNode( node.UniqueId, onlyUpdateGraphId );
  2203. MasterNode masterNode = node as MasterNode;
  2204. if( masterNode != null )
  2205. {
  2206. masterNode.OnMaterialUpdatedEvent += OnMaterialUpdatedEvent;
  2207. masterNode.OnShaderUpdatedEvent += OnShaderUpdatedEvent;
  2208. }
  2209. }
  2210. public void AssignMasterNode( int nodeId, bool onlyUpdateGraphId )
  2211. {
  2212. if( nodeId < 0 || m_masterNodeId == nodeId )
  2213. return;
  2214. if( m_masterNodeId > Constants.INVALID_NODE_ID )
  2215. {
  2216. OutputNode oldOutputNode = ( GetNode( nodeId ) as OutputNode );
  2217. MasterNode oldMasterNode = oldOutputNode as MasterNode;
  2218. if( oldOutputNode != null )
  2219. {
  2220. oldOutputNode.IsMainOutputNode = false;
  2221. if( oldMasterNode != null )
  2222. {
  2223. oldMasterNode.ClearUpdateEvents();
  2224. }
  2225. }
  2226. }
  2227. if( onlyUpdateGraphId )
  2228. {
  2229. m_masterNodeId = nodeId;
  2230. }
  2231. else
  2232. {
  2233. OutputNode outputNode = ( GetNode( nodeId ) as OutputNode );
  2234. if( outputNode != null )
  2235. {
  2236. outputNode.IsMainOutputNode = true;
  2237. m_masterNodeId = nodeId;
  2238. }
  2239. }
  2240. IsDirty = true;
  2241. }
  2242. public void RefreshOnUndo()
  2243. {
  2244. if( m_nodes != null )
  2245. {
  2246. int count = m_nodes.Count;
  2247. for( int i = 0; i < count; i++ )
  2248. {
  2249. if( m_nodes[ i ] != null )
  2250. {
  2251. m_nodes[ i ].RefreshOnUndo();
  2252. }
  2253. }
  2254. }
  2255. }
  2256. public void DrawGrid( DrawInfo drawInfo )
  2257. {
  2258. m_nodeGrid.DrawGrid( drawInfo );
  2259. }
  2260. public float MaxNodeDist
  2261. {
  2262. get { return m_nodeGrid.MaxNodeDist; }
  2263. }
  2264. public List<ParentNode> GetNodesInGrid( Vector2 transformedMousePos )
  2265. {
  2266. return m_nodeGrid.GetNodesOn( transformedMousePos );
  2267. }
  2268. public void FireMasterNode( Shader selectedShader )
  2269. {
  2270. ( GetNode( m_masterNodeId ) as MasterNode ).Execute( selectedShader );
  2271. }
  2272. public Shader FireMasterNode( string pathname, bool isFullPath )
  2273. {
  2274. return ( GetNode( m_masterNodeId ) as MasterNode ).Execute( pathname, isFullPath );
  2275. }
  2276. private void ForceSignalPropagationOnMasterNodeInternal( UsageListTemplateMultiPassMasterNodes masterNodes )
  2277. {
  2278. int mpCount = masterNodes.Count;
  2279. for( int i = 0; i < mpCount; i++ )
  2280. {
  2281. masterNodes.NodesList[ i ].GenerateSignalPropagation();
  2282. }
  2283. }
  2284. public void ForceSignalPropagationOnMasterNode()
  2285. {
  2286. if( m_multiPassMasterNodes.Count > 0 )
  2287. {
  2288. ForceSignalPropagationOnMasterNodeInternal( m_multiPassMasterNodes );
  2289. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  2290. {
  2291. ForceSignalPropagationOnMasterNodeInternal( m_lodMultiPassMasterNodes[ i ] );
  2292. }
  2293. }
  2294. else if( CurrentOutputNode != null )
  2295. CurrentOutputNode.GenerateSignalPropagation();
  2296. List<FunctionOutput> allOutputs = m_functionOutputNodes.NodesList;
  2297. for( int i = 0; i < allOutputs.Count; i++ )
  2298. {
  2299. allOutputs[ i ].GenerateSignalPropagation();
  2300. }
  2301. //List<RegisterLocalVarNode> localVarNodes = m_localVarNodes.NodesList;
  2302. //int count = localVarNodes.Count;
  2303. //for( int i = 0; i < count; i++ )
  2304. //{
  2305. // localVarNodes[ i ].GenerateSignalPropagation();
  2306. //}
  2307. }
  2308. public void UpdateShaderOnMasterNode( Shader newShader )
  2309. {
  2310. MasterNode mainMasterNode = ( GetNode( m_masterNodeId ) as MasterNode );
  2311. if( mainMasterNode == null )
  2312. {
  2313. Debug.LogError( "No Master Node was detected. Aborting update!" );
  2314. return;
  2315. }
  2316. mainMasterNode.UpdateFromShader( newShader );
  2317. if( HasLODs )
  2318. {
  2319. int passIdx = ( (TemplateMultiPassMasterNode)mainMasterNode ).PassIdx;
  2320. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  2321. {
  2322. if( m_lodMultiPassMasterNodes.Count != 0 && m_lodMultiPassMasterNodes[ i ].NodesList.Count > 0 )
  2323. {
  2324. if( m_lodMultiPassMasterNodes[ i ].NodesList[ passIdx ] != null )
  2325. {
  2326. m_lodMultiPassMasterNodes[ i ].NodesList[ passIdx ].UpdateFromShader( newShader );
  2327. }
  2328. else
  2329. {
  2330. Debug.LogError( "Null master node detected. Aborting update!" );
  2331. return;
  2332. }
  2333. }
  2334. else break;
  2335. }
  2336. }
  2337. }
  2338. public void CopyValuesFromMaterial( Material material )
  2339. {
  2340. Material currMaterial = CurrentMaterial;
  2341. if( currMaterial == material )
  2342. {
  2343. for( int i = 0; i < m_nodes.Count; i++ )
  2344. {
  2345. m_nodes[ i ].ForceUpdateFromMaterial( material );
  2346. }
  2347. }
  2348. }
  2349. public void UpdateMaterialOnMasterNode( Material material )
  2350. {
  2351. MasterNode mainMasterNode = ( GetNode( m_masterNodeId ) as MasterNode );
  2352. mainMasterNode.UpdateMasterNodeMaterial( material );
  2353. if( HasLODs )
  2354. {
  2355. int passIdx = ( (TemplateMultiPassMasterNode)mainMasterNode ).PassIdx;
  2356. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  2357. {
  2358. if( m_lodMultiPassMasterNodes.Count != 0 && m_lodMultiPassMasterNodes[ i ].NodesList.Count > 0 )
  2359. {
  2360. m_lodMultiPassMasterNodes[ i ].NodesList[ passIdx ].UpdateMasterNodeMaterial( material );
  2361. }
  2362. else break;
  2363. }
  2364. }
  2365. }
  2366. public void UpdateMaterialOnPropertyNodes( Material material )
  2367. {
  2368. int propertyCount = m_propertyNodes.Count;
  2369. for(int i = 0;i< propertyCount;i++ )
  2370. {
  2371. m_propertyNodes.NodesList[i].UpdateMaterial( material );
  2372. }
  2373. }
  2374. public void SetMaterialModeOnGraph( Material mat, bool fetchMaterialValues = true )
  2375. {
  2376. for( int i = 0; i < m_nodes.Count; i++ )
  2377. {
  2378. m_nodes[ i ].SetMaterialMode( mat, fetchMaterialValues );
  2379. }
  2380. }
  2381. public ParentNode CheckNodeAt( Vector3 pos, bool checkForRMBIgnore = false )
  2382. {
  2383. ParentNode selectedNode = null;
  2384. // this is checked on the inverse order to give priority to nodes that are drawn on top ( last on the list )
  2385. for( int i = m_nodes.Count - 1; i > -1; i-- )
  2386. {
  2387. if( m_nodes[ i ].Contains( pos ) )
  2388. {
  2389. if( checkForRMBIgnore )
  2390. {
  2391. if( !m_nodes[ i ].RMBIgnore )
  2392. {
  2393. selectedNode = m_nodes[ i ];
  2394. break;
  2395. }
  2396. }
  2397. else
  2398. {
  2399. selectedNode = m_nodes[ i ];
  2400. break;
  2401. }
  2402. }
  2403. }
  2404. return selectedNode;
  2405. }
  2406. public void ResetNodesLocalVariables()
  2407. {
  2408. for( int i = 0; i < m_nodes.Count; i++ )
  2409. {
  2410. m_nodes[ i ].Reset();
  2411. m_nodes[ i ].ResetOutputLocals();
  2412. FunctionNode fnode = m_nodes[ i ] as FunctionNode;
  2413. if( fnode != null )
  2414. {
  2415. if( fnode.Function != null )
  2416. fnode.FunctionGraph.ResetNodesLocalVariables();
  2417. }
  2418. }
  2419. }
  2420. public void ResetNodesLocalVariablesIfNot( MasterNodePortCategory category )
  2421. {
  2422. for( int i = 0; i < m_nodes.Count; i++ )
  2423. {
  2424. m_nodes[ i ].Reset();
  2425. m_nodes[ i ].ResetOutputLocalsIfNot( category );
  2426. FunctionNode fnode = m_nodes[ i ] as FunctionNode;
  2427. if( fnode != null )
  2428. {
  2429. if( fnode.Function != null )
  2430. fnode.FunctionGraph.ResetNodesLocalVariablesIfNot( category );
  2431. }
  2432. }
  2433. }
  2434. public void ResetNodesLocalVariables( ParentNode node )
  2435. {
  2436. if( node is GetLocalVarNode )
  2437. {
  2438. GetLocalVarNode localVarNode = node as GetLocalVarNode;
  2439. if( localVarNode.CurrentSelected != null )
  2440. {
  2441. node = localVarNode.CurrentSelected;
  2442. }
  2443. }
  2444. node.Reset();
  2445. node.ResetOutputLocals();
  2446. int count = node.InputPorts.Count;
  2447. for( int i = 0; i < count; i++ )
  2448. {
  2449. if( node.InputPorts[ i ].IsConnected )
  2450. {
  2451. ResetNodesLocalVariables( m_nodesDict[ node.InputPorts[ i ].GetConnection().NodeId ] );
  2452. }
  2453. }
  2454. }
  2455. public void ResetNodesLocalVariablesIfNot( ParentNode node, MasterNodePortCategory category )
  2456. {
  2457. if( node is GetLocalVarNode )
  2458. {
  2459. GetLocalVarNode localVarNode = node as GetLocalVarNode;
  2460. if( localVarNode.CurrentSelected != null )
  2461. {
  2462. node = localVarNode.CurrentSelected;
  2463. }
  2464. }
  2465. node.Reset();
  2466. node.ResetOutputLocalsIfNot( category );
  2467. int count = node.InputPorts.Count;
  2468. for( int i = 0; i < count; i++ )
  2469. {
  2470. if( node.InputPorts[ i ].IsConnected )
  2471. {
  2472. ResetNodesLocalVariablesIfNot( m_nodesDict[ node.InputPorts[ i ].GetConnection().NodeId ], category );
  2473. }
  2474. }
  2475. }
  2476. public override string ToString()
  2477. {
  2478. string dump = ( "Parent Graph \n" );
  2479. for( int i = 0; i < m_nodes.Count; i++ )
  2480. {
  2481. dump += ( m_nodes[ i ] + "\n" );
  2482. }
  2483. return dump;
  2484. }
  2485. public void OrderNodesByGraphDepth()
  2486. {
  2487. if( CurrentMasterNode != null )
  2488. {
  2489. //CurrentMasterNode.SetupNodeCategories();
  2490. int count = m_nodes.Count;
  2491. for( int i = 0; i < count; i++ )
  2492. {
  2493. if( m_nodes[ i ].ConnStatus == NodeConnectionStatus.Island )
  2494. {
  2495. m_nodes[ i ].CalculateCustomGraphDepth();
  2496. }
  2497. }
  2498. }
  2499. else
  2500. {
  2501. //TODO: remove this dynamic list
  2502. List<OutputNode> allOutputs = new List<OutputNode>();
  2503. for( int i = 0; i < AllNodes.Count; i++ )
  2504. {
  2505. OutputNode temp = AllNodes[ i ] as OutputNode;
  2506. if( temp != null )
  2507. allOutputs.Add( temp );
  2508. }
  2509. for( int j = 0; j < allOutputs.Count; j++ )
  2510. {
  2511. allOutputs[ j ].SetupNodeCategories();
  2512. int count = m_nodes.Count;
  2513. for( int i = 0; i < count; i++ )
  2514. {
  2515. if( m_nodes[ i ].ConnStatus == NodeConnectionStatus.Island )
  2516. {
  2517. m_nodes[ i ].CalculateCustomGraphDepth();
  2518. }
  2519. }
  2520. }
  2521. }
  2522. m_nodes.Sort( ( x, y ) => { return y.GraphDepth.CompareTo( x.GraphDepth ); } );
  2523. }
  2524. public void WriteToString( ref string nodesInfo, ref string connectionsInfo )
  2525. {
  2526. for( int i = 0; i < m_nodes.Count; i++ )
  2527. {
  2528. m_nodes[ i ].FullWriteToString( ref nodesInfo, ref connectionsInfo );
  2529. IOUtils.AddLineTerminator( ref nodesInfo );
  2530. }
  2531. }
  2532. public void Reset()
  2533. {
  2534. SaveIsDirty = false;
  2535. IsDirty = false;
  2536. }
  2537. public void OnBeforeSerialize()
  2538. {
  2539. //DeSelectAll();
  2540. }
  2541. public void OnAfterDeserialize()
  2542. {
  2543. m_afterDeserializeFlag = true;
  2544. }
  2545. public void CleanCorruptedNodes()
  2546. {
  2547. for( int i = 0; i < m_nodes.Count; i++ )
  2548. {
  2549. if( (object)m_nodes[ i ] == null )
  2550. {
  2551. m_nodes.RemoveAt( i );
  2552. CleanCorruptedNodes();
  2553. }
  2554. }
  2555. }
  2556. public void OnDuplicateEventWrapper()
  2557. {
  2558. if( OnDuplicateEvent != null )
  2559. {
  2560. AmplifyShaderEditorWindow temp = UIUtils.CurrentWindow;
  2561. UIUtils.CurrentWindow = ParentWindow;
  2562. OnDuplicateEvent();
  2563. UIUtils.CurrentWindow = temp;
  2564. }
  2565. }
  2566. public ParentNode CreateNode( AmplifyShaderFunction shaderFunction, bool registerUndo, int nodeId = -1, bool addLast = true )
  2567. {
  2568. FunctionNode newNode = ScriptableObject.CreateInstance<FunctionNode>();
  2569. if( newNode )
  2570. {
  2571. newNode.ContainerGraph = this;
  2572. newNode.CommonInit( shaderFunction, nodeId );
  2573. newNode.UniqueId = nodeId;
  2574. AddNode( newNode, nodeId < 0, addLast, registerUndo );
  2575. }
  2576. return newNode;
  2577. }
  2578. public ParentNode CreateNode( AmplifyShaderFunction shaderFunction, bool registerUndo, Vector2 pos, int nodeId = -1, bool addLast = true )
  2579. {
  2580. ParentNode newNode = CreateNode( shaderFunction, registerUndo, nodeId, addLast );
  2581. if( newNode )
  2582. {
  2583. newNode.Vec2Position = pos;
  2584. }
  2585. return newNode;
  2586. }
  2587. public ParentNode CreateNode( System.Type type, bool registerUndo, int nodeId = -1, bool addLast = true )
  2588. {
  2589. ParentNode newNode = ScriptableObject.CreateInstance( type ) as ParentNode;
  2590. if( newNode )
  2591. {
  2592. newNode.ContainerGraph = this;
  2593. newNode.UniqueId = nodeId;
  2594. AddNode( newNode, nodeId < 0, addLast, registerUndo );
  2595. }
  2596. return newNode;
  2597. }
  2598. public ParentNode CreateNode( System.Type type, bool registerUndo, Vector2 pos, int nodeId = -1, bool addLast = true )
  2599. {
  2600. ParentNode newNode = CreateNode( type, registerUndo, nodeId, addLast );
  2601. if( newNode )
  2602. {
  2603. newNode.Vec2Position = pos;
  2604. }
  2605. return newNode;
  2606. }
  2607. public void FireMasterNodeReplacedEvent()
  2608. {
  2609. MasterNode masterNode = CurrentMasterNode;
  2610. int count = m_nodes.Count;
  2611. for( int i = 0; i < count; i++ )
  2612. {
  2613. if( m_nodes[ i ].UniqueId != m_masterNodeId )
  2614. {
  2615. m_nodes[ i ].OnMasterNodeReplaced( masterNode );
  2616. }
  2617. }
  2618. }
  2619. //Used over shader functions to propagate signal into their graphs
  2620. public void FireMasterNodeReplacedEvent( MasterNode masterNode )
  2621. {
  2622. int count = m_nodes.Count;
  2623. for( int i = 0; i < count; i++ )
  2624. {
  2625. if( m_nodes[ i ].UniqueId != masterNode.UniqueId )
  2626. {
  2627. m_nodes[ i ].OnMasterNodeReplaced( masterNode );
  2628. }
  2629. }
  2630. }
  2631. public void CrossCheckTemplateNodes( TemplateDataParent templateData )
  2632. {
  2633. /*Paulo*/
  2634. DeSelectAll();
  2635. TemplateMultiPassMasterNode newMasterNode = null;
  2636. Dictionary<string, TemplateReplaceHelper> nodesDict = new Dictionary<string, TemplateReplaceHelper>();
  2637. int mpNodeCount = m_multiPassMasterNodes.NodesList.Count;
  2638. for( int i = 0; i < mpNodeCount; i++ )
  2639. {
  2640. nodesDict.Add( m_multiPassMasterNodes.NodesList[ i ].OriginalPassName, new TemplateReplaceHelper( m_multiPassMasterNodes.NodesList[ i ] ) );
  2641. }
  2642. TemplateMultiPassMasterNode currMasterNode = GetNode( m_masterNodeId ) as TemplateMultiPassMasterNode;
  2643. TemplateMultiPass multipassData = templateData as TemplateMultiPass;
  2644. m_currentSRPType = multipassData.SubShaders[ 0 ].Modules.SRPType;
  2645. bool sortTemplatesNodes = false;
  2646. Vector2 currentPosition = currMasterNode.Vec2Position;
  2647. for( int subShaderIdx = 0; subShaderIdx < multipassData.SubShaders.Count; subShaderIdx++ )
  2648. {
  2649. for( int passIdx = 0; passIdx < multipassData.SubShaders[ subShaderIdx ].Passes.Count; passIdx++ )
  2650. {
  2651. string currPassName = multipassData.SubShaders[ subShaderIdx ].Passes[ passIdx ].PassNameContainer.Data;
  2652. if( nodesDict.ContainsKey( currPassName ) )
  2653. {
  2654. bool wasMainNode = nodesDict[ currPassName ].MasterNode.IsMainOutputNode;
  2655. currentPosition.y += nodesDict[ currPassName ].MasterNode.Position.height + 10;
  2656. nodesDict[ currPassName ].Used = true;
  2657. nodesDict[ currPassName ].MasterNode.SetTemplate( multipassData, false, false, subShaderIdx, passIdx, SetTemplateSource.NewShader );
  2658. if( wasMainNode && !nodesDict[ currPassName ].MasterNode.IsMainOutputNode )
  2659. {
  2660. nodesDict[ currPassName ].MasterNode.ReleaseResources();
  2661. }
  2662. else if( !wasMainNode && nodesDict[ currPassName ].MasterNode.IsMainOutputNode )
  2663. {
  2664. newMasterNode = nodesDict[ currPassName ].MasterNode;
  2665. }
  2666. }
  2667. else
  2668. {
  2669. sortTemplatesNodes = true;
  2670. TemplateMultiPassMasterNode masterNode = CreateNode( typeof( TemplateMultiPassMasterNode ), false ) as TemplateMultiPassMasterNode;
  2671. if( multipassData.SubShaders[ subShaderIdx ].Passes[ passIdx ].IsMainPass )
  2672. {
  2673. newMasterNode = masterNode;
  2674. currMasterNode.ReleaseResources();
  2675. }
  2676. masterNode.Vec2Position = currentPosition;
  2677. masterNode.SetTemplate( multipassData, true, true, subShaderIdx, passIdx, SetTemplateSource.NewShader );
  2678. //currentPosition.y += masterNode.HeightEstimate + 10;
  2679. }
  2680. }
  2681. }
  2682. foreach( KeyValuePair<string, TemplateReplaceHelper> kvp in nodesDict )
  2683. {
  2684. if( !kvp.Value.Used )
  2685. DestroyNode( kvp.Value.MasterNode, false, true );
  2686. }
  2687. nodesDict.Clear();
  2688. if( newMasterNode != null )
  2689. {
  2690. m_masterNodeId = newMasterNode.UniqueId;
  2691. newMasterNode.OnMaterialUpdatedEvent += OnMaterialUpdatedEvent;
  2692. newMasterNode.OnShaderUpdatedEvent += OnShaderUpdatedEvent;
  2693. newMasterNode.IsMainOutputNode = true;
  2694. }
  2695. if( sortTemplatesNodes )
  2696. {
  2697. m_multiPassMasterNodes.NodesList.Sort( ( x, y ) => ( x.PassIdx.CompareTo( y.PassIdx ) ) );
  2698. }
  2699. }
  2700. public void OnRefreshLinkedPortsComplete()
  2701. {
  2702. OnRefreshLinkedPortsCompleteInternal( m_multiPassMasterNodes );
  2703. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  2704. {
  2705. OnRefreshLinkedPortsCompleteInternal( m_lodMultiPassMasterNodes[ i ] );
  2706. }
  2707. }
  2708. private void OnRefreshLinkedPortsCompleteInternal( UsageListTemplateMultiPassMasterNodes masterNodes )
  2709. {
  2710. int mpCount = masterNodes.Count;
  2711. for( int i = 0; i < mpCount; i++ )
  2712. {
  2713. masterNodes.NodesList[ i ].OnRefreshLinkedPortsComplete();
  2714. }
  2715. }
  2716. public void RefreshLinkedMasterNodes( bool optionsUpdate = false )
  2717. {
  2718. if( DebugConsoleWindow.DeveloperMode )
  2719. Debug.Log( "Refresh linked master nodes" );
  2720. RefreshLinkedMasterNodesInternal( m_multiPassMasterNodes, optionsUpdate );
  2721. for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  2722. {
  2723. RefreshLinkedMasterNodesInternal( m_lodMultiPassMasterNodes[i], optionsUpdate );
  2724. }
  2725. }
  2726. private void RefreshLinkedMasterNodesInternal( UsageListTemplateMultiPassMasterNodes masterNodes, bool optionsUpdate )
  2727. {
  2728. int mpCount = masterNodes.Count;
  2729. if( mpCount > 1 )
  2730. {
  2731. Dictionary<string, List<InputPort>> registeredLinks = new Dictionary<string, List<InputPort>>();
  2732. for( int i = 0; i < mpCount; i++ )
  2733. {
  2734. CheckLinkedPorts( ref registeredLinks, masterNodes.NodesList[ mpCount - 1 - i ] );
  2735. }
  2736. foreach( KeyValuePair<string, List<InputPort>> kvp in registeredLinks )
  2737. {
  2738. int linkCount = kvp.Value.Count;
  2739. if( linkCount == 1 )
  2740. {
  2741. kvp.Value[ 0 ].Visible = true;
  2742. }
  2743. else
  2744. {
  2745. kvp.Value[ 0 ].Visible = true;
  2746. for( int i = 1; i < linkCount; i++ )
  2747. {
  2748. kvp.Value[ i ].SetExternalLink( kvp.Value[ 0 ].NodeId, kvp.Value[ 0 ].PortId );
  2749. kvp.Value[ i ].Visible = false;
  2750. }
  2751. }
  2752. kvp.Value.Clear();
  2753. }
  2754. registeredLinks.Clear();
  2755. registeredLinks = null;
  2756. }
  2757. masterNodes.NodesList.Sort( ( x, y ) => ( x.SubShaderIdx * 1000 + x.PassIdx ).CompareTo( y.SubShaderIdx * 1000 + y.PassIdx ) );
  2758. masterNodes.UpdateNodeArr();
  2759. m_parentWindow.TemplatesManagerInstance.ResetOptionsSetupData();
  2760. for( int i = 0; i < mpCount; i++ )
  2761. {
  2762. int visiblePorts = 0;
  2763. for( int j = 0; j < masterNodes.NodesList[ i ].InputPorts.Count; j++ )
  2764. {
  2765. if( masterNodes.NodesList[ i ].InputPorts[ j ].Visible )
  2766. {
  2767. visiblePorts++;
  2768. }
  2769. }
  2770. if( masterNodes.NodesList[ i ].VisiblePorts != visiblePorts )
  2771. {
  2772. masterNodes.NodesList[ i ].VisiblePorts = visiblePorts;
  2773. ForceRepositionCheck = true;
  2774. }
  2775. masterNodes.NodesList[ i ].Docking = visiblePorts <= 0;
  2776. if( optionsUpdate )
  2777. {
  2778. masterNodes.NodesList[ i ].ForceOptionsRefresh();
  2779. }
  2780. }
  2781. }
  2782. void CheckLinkedPorts( ref Dictionary<string, List<InputPort>> registeredLinks, TemplateMultiPassMasterNode masterNode )
  2783. {
  2784. if( masterNode.HasLinkPorts )
  2785. {
  2786. int inputCount = masterNode.InputPorts.Count;
  2787. for( int i = 0; i < inputCount; i++ )
  2788. {
  2789. if( !string.IsNullOrEmpty( masterNode.InputPorts[ i ].ExternalLinkId ) )
  2790. {
  2791. string linkId = masterNode.InputPorts[ i ].ExternalLinkId;
  2792. if( !registeredLinks.ContainsKey( masterNode.InputPorts[ i ].ExternalLinkId ) )
  2793. {
  2794. registeredLinks.Add( linkId, new List<InputPort>() );
  2795. }
  2796. if( masterNode.IsMainOutputNode )
  2797. {
  2798. registeredLinks[ linkId ].Insert( 0, masterNode.InputPorts[ i ] );
  2799. }
  2800. else
  2801. {
  2802. registeredLinks[ linkId ].Add( masterNode.InputPorts[ i ] );
  2803. }
  2804. }
  2805. else
  2806. {
  2807. masterNode.InputPorts[ i ].Visible = true;
  2808. }
  2809. }
  2810. }
  2811. else
  2812. {
  2813. int inputCount = masterNode.InputPorts.Count;
  2814. for( int i = 0; i < inputCount; i++ )
  2815. {
  2816. masterNode.InputPorts[ i ].Visible = true;
  2817. }
  2818. }
  2819. }
  2820. public MasterNode ReplaceMasterNode( AvailableShaderTypes newType, bool writeDefaultData = false, TemplateDataParent templateData = null )
  2821. {
  2822. DeSelectAll();
  2823. ResetNodeConnStatus();
  2824. MasterNode newMasterNode = null;
  2825. List<TemplateMultiPassMasterNode> nodesToDelete = null;
  2826. int mpNodeCount = m_multiPassMasterNodes.NodesList.Count;
  2827. if( mpNodeCount > 0 )
  2828. {
  2829. nodesToDelete = new List<TemplateMultiPassMasterNode>();
  2830. for( int i = 0; i < mpNodeCount; i++ )
  2831. {
  2832. if( m_multiPassMasterNodes.NodesList[ i ].UniqueId != m_masterNodeId )
  2833. {
  2834. nodesToDelete.Add( m_multiPassMasterNodes.NodesList[ i ] );
  2835. }
  2836. }
  2837. for( int lod = 0; lod < m_lodMultiPassMasterNodes.Count; lod++ )
  2838. {
  2839. int lodNodeCount = m_lodMultiPassMasterNodes[ lod ].Count;
  2840. for( int i = 0; i < lodNodeCount; i++ )
  2841. {
  2842. nodesToDelete.Add( m_lodMultiPassMasterNodes[ lod ].NodesList[ i ] );
  2843. }
  2844. }
  2845. }
  2846. MasterNode currMasterNode = GetNode( m_masterNodeId ) as MasterNode;
  2847. if( currMasterNode != null )
  2848. {
  2849. currMasterNode.ReleaseResources();
  2850. }
  2851. bool refreshLinkedMasterNodes = false;
  2852. switch( newType )
  2853. {
  2854. default:
  2855. case AvailableShaderTypes.SurfaceShader:
  2856. {
  2857. CurrentCanvasMode = NodeAvailability.SurfaceShader;
  2858. m_currentSRPType = TemplateSRPType.BuiltIn;
  2859. newMasterNode = CreateNode( typeof( StandardSurfaceOutputNode ), false ) as MasterNode;
  2860. }
  2861. break;
  2862. case AvailableShaderTypes.Template:
  2863. {
  2864. CurrentCanvasMode = NodeAvailability.TemplateShader;
  2865. if( templateData.TemplateType == TemplateDataType.LegacySinglePass )
  2866. {
  2867. newMasterNode = CreateNode( typeof( TemplateMasterNode ), false ) as MasterNode;
  2868. ( newMasterNode as TemplateMasterNode ).SetTemplate( templateData as TemplateData, writeDefaultData, false );
  2869. m_currentSRPType = TemplateSRPType.BuiltIn;
  2870. }
  2871. else
  2872. {
  2873. /*Paulo*/
  2874. TemplateMultiPass multipassData = templateData as TemplateMultiPass;
  2875. m_currentSRPType = multipassData.SubShaders[ 0 ].Modules.SRPType;
  2876. Vector2 currentPosition = currMasterNode.Vec2Position;
  2877. for( int subShaderIdx = 0; subShaderIdx < multipassData.SubShaders.Count; subShaderIdx++ )
  2878. {
  2879. for( int passIdx = 0; passIdx < multipassData.SubShaders[ subShaderIdx ].Passes.Count; passIdx++ )
  2880. {
  2881. TemplateMultiPassMasterNode masterNode = CreateNode( typeof( TemplateMultiPassMasterNode ), false ) as TemplateMultiPassMasterNode;
  2882. if( multipassData.SubShaders[ subShaderIdx ].Passes[ passIdx ].IsMainPass )
  2883. {
  2884. newMasterNode = masterNode;
  2885. ParentWindow.IsShaderFunctionWindow = false;
  2886. CurrentCanvasMode = NodeAvailability.TemplateShader;
  2887. }
  2888. masterNode.Vec2Position = currentPosition;
  2889. masterNode.SetTemplate( multipassData, true, true, subShaderIdx, passIdx, SetTemplateSource.NewShader );
  2890. //currentPosition.y += masterNode.HeightEstimate + 10;
  2891. }
  2892. }
  2893. refreshLinkedMasterNodes = true;
  2894. //RefreshLinkedMasterNodes();
  2895. }
  2896. }
  2897. break;
  2898. }
  2899. if( currMasterNode != null )
  2900. {
  2901. newMasterNode.CopyFrom( currMasterNode );
  2902. m_masterNodeId = -1;
  2903. DestroyNode( currMasterNode, false, true );
  2904. }
  2905. if( nodesToDelete != null )
  2906. {
  2907. for( int i = 0; i < nodesToDelete.Count; i++ )
  2908. {
  2909. DestroyNode( nodesToDelete[ i ], false, true );
  2910. }
  2911. nodesToDelete.Clear();
  2912. }
  2913. m_masterNodeId = newMasterNode.UniqueId;
  2914. if( refreshLinkedMasterNodes )
  2915. RefreshLinkedMasterNodes( true );
  2916. newMasterNode.OnMaterialUpdatedEvent += OnMaterialUpdatedEvent;
  2917. newMasterNode.OnShaderUpdatedEvent += OnShaderUpdatedEvent;
  2918. newMasterNode.IsMainOutputNode = true;
  2919. OnRefreshLinkedPortsComplete();
  2920. FullCleanUndoStack();
  2921. return newMasterNode;
  2922. }
  2923. private void RepositionTemplateNodes( MasterNode newMasterNode )
  2924. {
  2925. m_forceRepositionCheck = false;
  2926. int dockedElementsBefore = 0;
  2927. int dockedElementsAfter = 0;
  2928. int masterIndex = 0;
  2929. bool foundMaster = false;
  2930. for( int i = 0; i < MultiPassMasterNodes.Count; i++ )
  2931. {
  2932. if( MultiPassMasterNodes.NodesList[ i ].UniqueId == m_masterNodeId )
  2933. {
  2934. foundMaster = true;
  2935. masterIndex = i;
  2936. }
  2937. if( !MultiPassMasterNodes.NodesList[ i ].IsInvisible && MultiPassMasterNodes.NodesList[ i ].Docking )
  2938. {
  2939. if( foundMaster )
  2940. dockedElementsAfter++;
  2941. else
  2942. dockedElementsBefore++;
  2943. }
  2944. }
  2945. if( dockedElementsBefore > 0 )
  2946. {
  2947. newMasterNode.UseSquareNodeTitle = true;
  2948. }
  2949. for( int i = masterIndex - 1; i >= 0; i-- )
  2950. {
  2951. float forwardTracking = 0;
  2952. for( int j = i + 1; j <= masterIndex; j++ )
  2953. {
  2954. if( !MultiPassMasterNodes.NodesList[ i ].IsInvisible && !MultiPassMasterNodes.NodesList[ j ].Docking )
  2955. {
  2956. forwardTracking += MultiPassMasterNodes.NodesList[ j ].HeightEstimate + 10;
  2957. }
  2958. }
  2959. MasterNode node = MultiPassMasterNodes.NodesList[ i ];
  2960. node.Vec2Position = new Vector2( node.Vec2Position.x, newMasterNode.Position.y - forwardTracking - 33 * ( dockedElementsBefore ) );
  2961. }
  2962. for( int i = masterIndex + 1; i < MultiPassMasterNodes.Count; i++ )
  2963. {
  2964. if( MultiPassMasterNodes.NodesList[ i ].UniqueId == newMasterNode.UniqueId || MultiPassMasterNodes.NodesList[ i ].Docking )
  2965. continue;
  2966. float backTracking = 0;
  2967. for( int j = i - 1; j >= masterIndex; j-- )
  2968. {
  2969. if( !MultiPassMasterNodes.NodesList[ i ].IsInvisible && !MultiPassMasterNodes.NodesList[ j ].Docking )
  2970. {
  2971. backTracking += MultiPassMasterNodes.NodesList[ j ].HeightEstimate + 10;
  2972. }
  2973. }
  2974. MasterNode node = MultiPassMasterNodes.NodesList[ i ];
  2975. node.Vec2Position = new Vector2( node.Vec2Position.x, newMasterNode.Position.y + backTracking + 33 * ( dockedElementsAfter ) );
  2976. }
  2977. }
  2978. public void CreateNewEmpty( string name )
  2979. {
  2980. CleanNodes();
  2981. if( m_masterNodeDefaultType == null )
  2982. m_masterNodeDefaultType = typeof( StandardSurfaceOutputNode );
  2983. MasterNode newMasterNode = CreateNode( m_masterNodeDefaultType, false ) as MasterNode;
  2984. newMasterNode.SetName( name );
  2985. m_masterNodeId = newMasterNode.UniqueId;
  2986. ParentWindow.IsShaderFunctionWindow = false;
  2987. CurrentCanvasMode = NodeAvailability.SurfaceShader;
  2988. newMasterNode.OnMaterialUpdatedEvent += OnMaterialUpdatedEvent;
  2989. newMasterNode.OnShaderUpdatedEvent += OnShaderUpdatedEvent;
  2990. newMasterNode.IsMainOutputNode = true;
  2991. LoadedShaderVersion = VersionInfo.FullNumber;
  2992. }
  2993. public void CreateNewEmptyTemplate( string templateGUID )
  2994. {
  2995. CleanNodes();
  2996. TemplateDataParent templateData = m_parentWindow.TemplatesManagerInstance.GetTemplate( templateGUID );
  2997. if( templateData.TemplateType == TemplateDataType.LegacySinglePass )
  2998. {
  2999. TemplateMasterNode newMasterNode = CreateNode( typeof( TemplateMasterNode ), false ) as TemplateMasterNode;
  3000. m_masterNodeId = newMasterNode.UniqueId;
  3001. ParentWindow.IsShaderFunctionWindow = false;
  3002. CurrentCanvasMode = NodeAvailability.TemplateShader;
  3003. m_currentSRPType = TemplateSRPType.BuiltIn;
  3004. newMasterNode.OnMaterialUpdatedEvent += OnMaterialUpdatedEvent;
  3005. newMasterNode.OnShaderUpdatedEvent += OnShaderUpdatedEvent;
  3006. newMasterNode.IsMainOutputNode = true;
  3007. newMasterNode.SetTemplate( templateData as TemplateData, true, true );
  3008. }
  3009. else
  3010. {
  3011. /*Paulo*/
  3012. TemplateMultiPass multipassData = templateData as TemplateMultiPass;
  3013. m_currentSRPType = multipassData.SubShaders[ 0 ].Modules.SRPType;
  3014. Vector2 currentPosition = Vector2.zero;
  3015. for( int subShaderIdx = 0; subShaderIdx < multipassData.SubShaders.Count; subShaderIdx++ )
  3016. {
  3017. for( int passIdx = 0; passIdx < multipassData.SubShaders[ subShaderIdx ].Passes.Count; passIdx++ )
  3018. {
  3019. TemplateMultiPassMasterNode newMasterNode = CreateNode( typeof( TemplateMultiPassMasterNode ), false ) as TemplateMultiPassMasterNode;
  3020. if( multipassData.SubShaders[ subShaderIdx ].Passes[ passIdx ].IsMainPass )
  3021. {
  3022. m_masterNodeId = newMasterNode.UniqueId;
  3023. ParentWindow.IsShaderFunctionWindow = false;
  3024. CurrentCanvasMode = NodeAvailability.TemplateShader;
  3025. newMasterNode.OnMaterialUpdatedEvent += OnMaterialUpdatedEvent;
  3026. newMasterNode.OnShaderUpdatedEvent += OnShaderUpdatedEvent;
  3027. newMasterNode.IsMainOutputNode = true;
  3028. }
  3029. newMasterNode.Vec2Position = currentPosition;
  3030. newMasterNode.SetTemplate( multipassData, true, true, subShaderIdx, passIdx, SetTemplateSource.NewShader );
  3031. //currentPosition.y += newMasterNode.HeightEstimate + 10;
  3032. }
  3033. }
  3034. RefreshLinkedMasterNodes( false );
  3035. OnRefreshLinkedPortsComplete();
  3036. }
  3037. LoadedShaderVersion = VersionInfo.FullNumber;
  3038. }
  3039. public void CreateNewEmptyFunction( AmplifyShaderFunction shaderFunction )
  3040. {
  3041. CleanNodes();
  3042. FunctionOutput newOutputNode = CreateNode( typeof( FunctionOutput ), false ) as FunctionOutput;
  3043. m_masterNodeId = newOutputNode.UniqueId;
  3044. ParentWindow.IsShaderFunctionWindow = true;
  3045. CurrentCanvasMode = NodeAvailability.ShaderFunction;
  3046. newOutputNode.IsMainOutputNode = true;
  3047. }
  3048. public void ForceCategoryRefresh() { m_forceCategoryRefresh = true; }
  3049. public void RefreshExternalReferences()
  3050. {
  3051. int count = m_nodes.Count;
  3052. for( int i = 0; i < count; i++ )
  3053. {
  3054. m_nodes[ i ].RefreshExternalReferences();
  3055. }
  3056. }
  3057. public Vector2 SelectedNodesCentroid
  3058. {
  3059. get
  3060. {
  3061. if( m_selectedNodes.Count == 0 )
  3062. return Vector2.zero;
  3063. Vector2 pos = new Vector2( 0, 0 );
  3064. for( int i = 0; i < m_selectedNodes.Count; i++ )
  3065. {
  3066. pos += m_selectedNodes[ i ].Vec2Position;
  3067. }
  3068. pos /= m_selectedNodes.Count;
  3069. return pos;
  3070. }
  3071. }
  3072. public void AddVirtualTextureCount()
  3073. {
  3074. m_virtualTextureCount += 1;
  3075. }
  3076. public void RemoveVirtualTextureCount()
  3077. {
  3078. m_virtualTextureCount -= 1;
  3079. if( m_virtualTextureCount < 0 )
  3080. {
  3081. Debug.LogWarning( "Invalid virtual texture count" );
  3082. }
  3083. }
  3084. public bool HasVirtualTexture { get { return m_virtualTextureCount > 0; } }
  3085. public void AddInstancePropertyCount()
  3086. {
  3087. m_instancePropertyCount += 1;
  3088. // Debug.Log( "AddInstancePropertyCount "+this.GetInstanceID() + " " + m_instancePropertyCount );
  3089. }
  3090. public void RemoveInstancePropertyCount()
  3091. {
  3092. m_instancePropertyCount -= 1;
  3093. // Debug.Log( "RemoveInstancePropertyCount " + this.GetInstanceID() + " " + m_instancePropertyCount );
  3094. if( m_instancePropertyCount < 0 )
  3095. {
  3096. Debug.LogWarning( "Invalid property instance count" );
  3097. }
  3098. }
  3099. public int InstancePropertyCount { get { return m_instancePropertyCount; } set { m_instancePropertyCount = value; } }
  3100. public bool IsInstancedShader { get { return m_instancePropertyCount > 0; } }
  3101. public void AddNormalDependentCount() { m_normalDependentCount += 1; }
  3102. public void RemoveNormalDependentCount()
  3103. {
  3104. m_normalDependentCount -= 1;
  3105. if( m_normalDependentCount < 0 )
  3106. {
  3107. Debug.LogWarning( "Invalid normal dependentCount count" );
  3108. }
  3109. }
  3110. public void SetModeFromMasterNode()
  3111. {
  3112. MasterNode masterNode = CurrentMasterNode;
  3113. if( masterNode != null )
  3114. {
  3115. switch( masterNode.CurrentMasterNodeCategory )
  3116. {
  3117. default:
  3118. case AvailableShaderTypes.SurfaceShader:
  3119. {
  3120. if( masterNode is StandardSurfaceOutputNode )
  3121. CurrentCanvasMode = ParentWindow.CurrentNodeAvailability;
  3122. else
  3123. CurrentCanvasMode = NodeAvailability.SurfaceShader;
  3124. }
  3125. break;
  3126. case AvailableShaderTypes.Template:
  3127. {
  3128. CurrentCanvasMode = NodeAvailability.TemplateShader;
  3129. }
  3130. break;
  3131. }
  3132. }
  3133. else
  3134. {
  3135. CurrentCanvasMode = NodeAvailability.SurfaceShader;
  3136. }
  3137. }
  3138. public void MarkToDelete( ParentNode node )
  3139. {
  3140. m_markedForDeletion.Add( node );
  3141. }
  3142. public bool IsMasterNode( ParentNode node )
  3143. {
  3144. return ( node.UniqueId == m_masterNodeId ) ||
  3145. m_multiPassMasterNodes.HasNode( node.UniqueId );
  3146. }
  3147. public TemplateMultiPassMasterNode GetMainMasterNodeOfLOD( int lod )
  3148. {
  3149. if( lod == -1 )
  3150. return CurrentMasterNode as TemplateMultiPassMasterNode;
  3151. return m_lodMultiPassMasterNodes[ lod ].NodesList.Find( x => x.IsMainOutputNode );
  3152. }
  3153. public TemplateMultiPassMasterNode GetMasterNodeOfPass( string passName, int lod )
  3154. {
  3155. if( lod == -1 )
  3156. return m_multiPassMasterNodes.NodesList.Find( x => x.PassName.Equals( passName ) );
  3157. return m_lodMultiPassMasterNodes[lod].NodesList.Find( x => x.PassName.Equals( passName ) );
  3158. }
  3159. public void ForceMultiPassMasterNodesRefresh()
  3160. {
  3161. int mainOutputId = 0;
  3162. int count = m_multiPassMasterNodes.Count;
  3163. for( int i = 0; i < count; i++ )
  3164. {
  3165. m_multiPassMasterNodes.NodesList[ i ].ForceTemplateRefresh();
  3166. if( m_multiPassMasterNodes.NodesList[ i ].IsMainOutputNode )
  3167. mainOutputId = i;
  3168. }
  3169. int lodCount = m_lodMultiPassMasterNodes.Count;
  3170. for( int i = 0; i < lodCount; i++ )
  3171. {
  3172. if( m_lodMultiPassMasterNodes[ i ] != null )
  3173. {
  3174. count = m_lodMultiPassMasterNodes[ i ].Count;
  3175. for( int j = 0; j < count; j++ )
  3176. {
  3177. m_lodMultiPassMasterNodes[ i ].NodesList[ j ].ForceTemplateRefresh();
  3178. }
  3179. }
  3180. }
  3181. m_multiPassMasterNodes.NodesList[ mainOutputId ].CheckTemplateChanges();
  3182. }
  3183. public void SetLateOptionsRefresh()
  3184. {
  3185. m_lateOptionsRefresh = true;
  3186. }
  3187. public void CreateLodMasterNodes( TemplateMultiPass templateMultiPass,int index, Vector2 initialPosition )
  3188. {
  3189. for( int lod = 0; lod < m_lodMultiPassMasterNodes.Count; lod++ )
  3190. {
  3191. if( m_lodMultiPassMasterNodes[ lod ].Count == 0 )
  3192. {
  3193. TemplateMultiPassMasterNode reference = CurrentMasterNode as TemplateMultiPassMasterNode;
  3194. int shaderLod = -1;
  3195. if( lod == 0 )
  3196. {
  3197. shaderLod = reference.ShaderLOD - MasterNodeLODIncrement;
  3198. }
  3199. else
  3200. {
  3201. //index == -2 is when user clicks on +/- buttons over the foldout UI
  3202. if( index == -2 )
  3203. {
  3204. shaderLod = m_lodMultiPassMasterNodes[ lod - 1 ].NodesList[ reference.PassIdx ].ShaderLOD - MasterNodeLODIncrement;
  3205. }
  3206. //index == -1 is when user clicks on + button over the main lod master node
  3207. else if( index == -1 )
  3208. {
  3209. int mainShaderLOD = m_lodMultiPassMasterNodes[ 0 ].NodesList[ reference.PassIdx ].ShaderLOD;
  3210. shaderLod = ( reference.ShaderLOD + mainShaderLOD )/2;
  3211. }
  3212. else
  3213. {
  3214. if( m_lodMultiPassMasterNodes[ index ].Count > 0 )
  3215. {
  3216. if( m_lodMultiPassMasterNodes[ index + 1 ].Count > 0 )
  3217. {
  3218. shaderLod = (m_lodMultiPassMasterNodes[ index ].NodesList[ reference.PassIdx ].ShaderLOD +
  3219. m_lodMultiPassMasterNodes[ index + 1 ].NodesList[ reference.PassIdx ].ShaderLOD )/2;
  3220. }
  3221. else
  3222. {
  3223. shaderLod = m_lodMultiPassMasterNodes[ index ].NodesList[ reference.PassIdx ].ShaderLOD - MasterNodeLODIncrement;
  3224. }
  3225. }
  3226. }
  3227. }
  3228. int nodeId = 0;
  3229. TemplateMultiPassMasterNode mainMasterNode = null;
  3230. for( int subShaderIdx = 0; subShaderIdx < templateMultiPass.SubShaders.Count; subShaderIdx++ )
  3231. {
  3232. for( int passIdx = 0; passIdx < templateMultiPass.SubShaders[ subShaderIdx ].Passes.Count; passIdx++ )
  3233. {
  3234. TemplateMultiPassMasterNode masterNode = ScriptableObject.CreateInstance( typeof( TemplateMultiPassMasterNode ) ) as TemplateMultiPassMasterNode;
  3235. masterNode.LODIndex = lod;
  3236. masterNode.ContainerGraph = this;
  3237. masterNode.Vec2Position = initialPosition;
  3238. AddNode( masterNode, true );
  3239. masterNode.SetTemplate( templateMultiPass, true, true, subShaderIdx, passIdx, SetTemplateSource.NewShader );
  3240. masterNode.CopyOptionsFrom( m_multiPassMasterNodes.NodesList[ nodeId++ ] );
  3241. if( masterNode.IsMainOutputNode || ( subShaderIdx == 0 && passIdx == 0 ) )
  3242. {
  3243. masterNode.SetShaderLODValueAndLabel( shaderLod );
  3244. mainMasterNode = masterNode;
  3245. }
  3246. }
  3247. }
  3248. mainMasterNode.ForceOptionsRefresh();
  3249. SortLODMasterNodes();
  3250. if( OnLODMasterNodesAddedEvent != null )
  3251. {
  3252. OnLODMasterNodesAddedEvent( lod );
  3253. }
  3254. TemplateMultiPassMasterNode lodMainMasterNode = CurrentMasterNode as TemplateMultiPassMasterNode;
  3255. lodMainMasterNode.SetShaderLODValueAndLabel( lodMainMasterNode.ShaderLOD );
  3256. return;
  3257. }
  3258. }
  3259. }
  3260. public void DestroyLodMasterNodes( int index )
  3261. {
  3262. if( index < 0 )
  3263. {
  3264. for( int lod = m_lodMultiPassMasterNodes.Count - 1; lod >= 0; lod-- )
  3265. {
  3266. if( m_lodMultiPassMasterNodes[ lod ].Count > 0 )
  3267. {
  3268. while( m_lodMultiPassMasterNodes[ lod ].Count > 0 )
  3269. {
  3270. DestroyNode( m_lodMultiPassMasterNodes[ lod ].NodesList[ 0 ], false, true );
  3271. }
  3272. break;
  3273. }
  3274. }
  3275. }
  3276. else
  3277. {
  3278. while( m_lodMultiPassMasterNodes[ index ].Count > 0 )
  3279. {
  3280. DestroyNode( m_lodMultiPassMasterNodes[ index ].NodesList[ 0 ], false, true );
  3281. }
  3282. }
  3283. SortLODMasterNodes();
  3284. TemplateMultiPassMasterNode lodMainMasterNode = CurrentMasterNode as TemplateMultiPassMasterNode;
  3285. lodMainMasterNode.SetShaderLODValueAndLabel( lodMainMasterNode.ShaderLOD );
  3286. }
  3287. public void SortLODMasterNodes()
  3288. {
  3289. int idx = (CurrentMasterNode as TemplateMultiPassMasterNode).PassIdx;
  3290. m_lodMultiPassMasterNodes.Sort( ( x, y ) =>
  3291. {
  3292. if( x.Count > 0 )
  3293. {
  3294. if( y.Count > 0 )
  3295. {
  3296. return -x.NodesList[ idx ].ShaderLOD.CompareTo( y.NodesList[ idx ].ShaderLOD );
  3297. }
  3298. else
  3299. {
  3300. return -1;
  3301. }
  3302. }
  3303. else
  3304. {
  3305. if( y.Count > 0 )
  3306. {
  3307. return 1;
  3308. }
  3309. }
  3310. return 0;
  3311. });
  3312. for( int lodIdx = 0; lodIdx < m_lodMultiPassMasterNodes.Count; lodIdx++ )
  3313. {
  3314. for( int nodeIdx = 0; nodeIdx < m_lodMultiPassMasterNodes[ lodIdx ].Count; nodeIdx++ )
  3315. {
  3316. m_lodMultiPassMasterNodes[ lodIdx ].NodesList[ nodeIdx ].LODIndex = lodIdx;
  3317. }
  3318. }
  3319. }
  3320. public List<TemplateMultiPassMasterNode> GetMultiPassMasterNodes( int lod )
  3321. {
  3322. if( lod == -1 )
  3323. return m_multiPassMasterNodes.NodesList;
  3324. return m_lodMultiPassMasterNodes[ lod ].NodesList;
  3325. }
  3326. public bool IsNormalDependent { get { return m_normalDependentCount > 0; } }
  3327. public void MarkToDeselect() { m_markedToDeSelect = true; }
  3328. public void MarkToSelect( int nodeId ) { m_markToSelect = nodeId; }
  3329. public void MarkWireHighlights() { m_checkSelectedWireHighlights = true; }
  3330. public List<ParentNode> SelectedNodes { get { return m_selectedNodes; } }
  3331. public List<ParentNode> MarkedForDeletionNodes { get { return m_markedForDeletion; } }
  3332. public int CurrentMasterNodeId { get { return m_masterNodeId; } set { m_masterNodeId = value; } }
  3333. public Shader CurrentShader
  3334. {
  3335. get
  3336. {
  3337. MasterNode masterNode = GetNode( m_masterNodeId ) as MasterNode;
  3338. if( masterNode != null )
  3339. return masterNode.CurrentShader;
  3340. return null;
  3341. }
  3342. }
  3343. public Material CurrentMaterial
  3344. {
  3345. get
  3346. {
  3347. MasterNode masterNode = GetNode( m_masterNodeId ) as MasterNode;
  3348. if( masterNode != null )
  3349. return masterNode.CurrentMaterial;
  3350. return null;
  3351. }
  3352. }
  3353. public NodeAvailability CurrentCanvasMode { get { return m_currentCanvasMode; } set { m_currentCanvasMode = value; ParentWindow.LateRefreshAvailableNodes(); } }
  3354. public OutputNode CurrentOutputNode { get { return GetNode( m_masterNodeId ) as OutputNode; } }
  3355. public FunctionOutput CurrentFunctionOutput { get { return GetNode( m_masterNodeId ) as FunctionOutput; } }
  3356. public MasterNode CurrentMasterNode { get { return GetNode( m_masterNodeId ) as MasterNode; } }
  3357. public StandardSurfaceOutputNode CurrentStandardSurface { get { return GetNode( m_masterNodeId ) as StandardSurfaceOutputNode; } }
  3358. public List<ParentNode> AllNodes { get { return m_nodes; } }
  3359. public int NodeCount { get { return m_nodes.Count; } }
  3360. //public List<ParentNode> VisibleNodes { get { return m_visibleNodes; } }
  3361. public int NodeClicked
  3362. {
  3363. set { m_nodeClicked = value; }
  3364. get { return m_nodeClicked; }
  3365. }
  3366. public bool IsDirty
  3367. {
  3368. set { m_isDirty = value && UIUtils.DirtyMask; }
  3369. get
  3370. {
  3371. bool value = m_isDirty;
  3372. m_isDirty = false;
  3373. return value;
  3374. }
  3375. }
  3376. public bool SaveIsDirty
  3377. {
  3378. set { m_saveIsDirty = value && UIUtils.DirtyMask; }
  3379. get { return m_saveIsDirty; }
  3380. }
  3381. public int LoadedShaderVersion
  3382. {
  3383. get { return m_loadedShaderVersion; }
  3384. set { m_loadedShaderVersion = value; }
  3385. }
  3386. public AmplifyShaderFunction CurrentShaderFunction
  3387. {
  3388. get { if( CurrentFunctionOutput != null ) return CurrentFunctionOutput.Function; else return null; }
  3389. set { if( CurrentFunctionOutput != null ) CurrentFunctionOutput.Function = value; }
  3390. }
  3391. public bool HasUnConnectedNodes { get { return m_hasUnConnectedNodes; } }
  3392. public UsageListSamplerNodes SamplerNodes { get { return m_samplerNodes; } }
  3393. public UsageListFloatIntNodes FloatIntNodes { get { return m_floatNodes; } }
  3394. public UsageListTexturePropertyNodes TexturePropertyNodes { get { return m_texturePropertyNodes; } }
  3395. public UsageListTextureArrayNodes TextureArrayNodes { get { return m_textureArrayNodes; } }
  3396. public UsageListPropertyNodes PropertyNodes { get { return m_propertyNodes; } }
  3397. public UsageListPropertyNodes RawPropertyNodes { get { return m_rawPropertyNodes; } }
  3398. public UsageListCustomExpressionsOnFunctionMode CustomExpressionOnFunctionMode { get { return m_customExpressionsOnFunctionMode; } }
  3399. public UsageListStaticSwitchNodes StaticSwitchNodes { get { return m_staticSwitchNodes; } }
  3400. public UsageListScreenColorNodes ScreenColorNodes { get { return m_screenColorNodes; } }
  3401. public UsageListRegisterLocalVarNodes LocalVarNodes { get { return m_localVarNodes; } }
  3402. public UsageListGlobalArrayNodes GlobalArrayNodes { get { return m_globalArrayNodes; } }
  3403. public UsageListFunctionInputNodes FunctionInputNodes { get { return m_functionInputNodes; } }
  3404. public UsageListFunctionNodes FunctionNodes { get { return m_functionNodes; } }
  3405. public UsageListFunctionOutputNodes FunctionOutputNodes { get { return m_functionOutputNodes; } }
  3406. public UsageListFunctionSwitchNodes FunctionSwitchNodes { get { return m_functionSwitchNodes; } }
  3407. public UsageListFunctionSwitchCopyNodes FunctionSwitchCopyNodes { get { return m_functionSwitchCopyNodes; } }
  3408. public UsageListTemplateMultiPassMasterNodes MultiPassMasterNodes { get { return m_multiPassMasterNodes; } set { m_multiPassMasterNodes = value; } }
  3409. public List<UsageListTemplateMultiPassMasterNodes> LodMultiPassMasternodes { get { return m_lodMultiPassMasterNodes; } }
  3410. public PrecisionType CurrentPrecision
  3411. {
  3412. get { return m_currentPrecision; }
  3413. set { m_currentPrecision = value; }
  3414. }
  3415. public NodeLOD LodLevel
  3416. {
  3417. get { return m_lodLevel; }
  3418. }
  3419. public List<ParentNode> NodePreviewList { get { return m_nodePreviewList; } set { m_nodePreviewList = value; } }
  3420. public void SetGraphId( int id )
  3421. {
  3422. m_graphId = id;
  3423. }
  3424. public int GraphId
  3425. {
  3426. get { return m_graphId; }
  3427. }
  3428. public AmplifyShaderEditorWindow ParentWindow
  3429. {
  3430. get { return m_parentWindow; }
  3431. set { m_parentWindow = value; }
  3432. }
  3433. public bool ChangedLightingModel
  3434. {
  3435. get { return m_changedLightingModel; }
  3436. set { m_changedLightingModel = value; }
  3437. }
  3438. public bool ForceRepositionCheck
  3439. {
  3440. get { return m_forceRepositionCheck; }
  3441. set { m_forceRepositionCheck = value; }
  3442. }
  3443. public bool IsLoading { get { return m_isLoading; } set { m_isLoading = value; } }
  3444. public bool IsDuplicating { get { return m_isDuplicating; } set { m_isDuplicating = value; } }
  3445. public TemplateSRPType CurrentSRPType { get { return m_currentSRPType; }set { m_currentSRPType = value; } }
  3446. public bool IsSRP { get { return m_currentSRPType == TemplateSRPType.Lightweight || m_currentSRPType == TemplateSRPType.HD; } }
  3447. public bool IsHDRP { get { return m_currentSRPType == TemplateSRPType.HD; } }
  3448. public bool IsLWRP { get { return m_currentSRPType == TemplateSRPType.Lightweight; } }
  3449. public bool IsStandardSurface { get { return GetNode( m_masterNodeId ) is StandardSurfaceOutputNode; } }
  3450. public bool SamplingMacros {
  3451. get { return m_samplingThroughMacros; }
  3452. set { m_samplingThroughMacros = value; }
  3453. }
  3454. public bool HasLODs { get { return m_lodMultiPassMasterNodes[ 0 ].Count > 0; } }
  3455. //public bool HasLodMultiPassNodes
  3456. //{
  3457. // get
  3458. // {
  3459. // for( int i = 0; i < m_lodMultiPassMasterNodes.Count; i++ )
  3460. // {
  3461. // if( m_lodMultiPassMasterNodes[ i ].Count > 0 )
  3462. // return true;
  3463. // }
  3464. // return false;
  3465. // }
  3466. //}
  3467. }
  3468. }