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.

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