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.

183 lines
4.7 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. public class CustomNodeOutputData
  10. {
  11. public string expression;
  12. public string name;
  13. public List<CustomNodeInputData> inputData;
  14. public CustomNodeOutputData( string newName, string newExpression )
  15. {
  16. name = newName;
  17. expression = newExpression;
  18. inputData = new List<CustomNodeInputData>();
  19. }
  20. public void Destroy()
  21. {
  22. inputData.Clear();
  23. inputData = null;
  24. }
  25. public override string ToString()
  26. {
  27. string result = "name: " + name + " outputExpression: " + expression + '\n';
  28. for ( int i = 0; i < inputData.Count; i++ )
  29. {
  30. result += inputData[ i ].ToString() + '\n';
  31. }
  32. return result;
  33. }
  34. }
  35. [Serializable]
  36. public class CustomNodeInputData
  37. {
  38. public int index;
  39. public int length;
  40. public string name;
  41. public CustomNodeInputData( int newIndex, int newLength, string newName )
  42. {
  43. index = newIndex;
  44. length = newLength;
  45. name = newName;
  46. }
  47. public override string ToString()
  48. {
  49. return "index: " + index + " length: " + length + " name: " + name;
  50. }
  51. }
  52. [Serializable]
  53. public class CustomNode : ParentNode
  54. {
  55. [SerializeField]
  56. private List<string> m_includes;
  57. [SerializeField]
  58. private List<CustomNodeOutputData> m_outputData;
  59. protected override void CommonInit( int uniqueId )
  60. {
  61. base.CommonInit( uniqueId );
  62. m_outputData = new List<CustomNodeOutputData>();
  63. }
  64. public void AddIncludes( string newInclude )
  65. {
  66. m_includes.Add( newInclude );
  67. }
  68. protected void AddOutputsFromString( string outputName, string output )
  69. {
  70. AddOutputPort( WirePortDataType.OBJECT, outputName );
  71. CustomNodeOutputData currOutputData = new CustomNodeOutputData( outputName, output );
  72. // Get existing input nodes so we can test for duplicates
  73. Dictionary<string, InputPort> existingPorts = InputPortsDict;
  74. // Create dictionary to prevent duplicates when dealing with expresssions with multiple occurences of an input
  75. Dictionary<string, string> inputDuplicatePrevention = new Dictionary<string, string>();
  76. // Get all inputs on the expression and save their info
  77. int[] indexes = output.AllIndexesOf( Constants.CNIP );
  78. for ( int i = 0; i < indexes.Length; i++ )
  79. {
  80. string name = output.Substring( indexes[ i ], Constants.CNIP.Length + 1 );
  81. currOutputData.inputData.Add( new CustomNodeInputData( indexes[ i ], Constants.CNIP.Length + 1, name ) );
  82. if ( !inputDuplicatePrevention.ContainsKey( name ) && !existingPorts.ContainsKey( name ) )
  83. {
  84. inputDuplicatePrevention.Add( name, name );
  85. AddInputPort( WirePortDataType.OBJECT, false, name );
  86. }
  87. }
  88. inputDuplicatePrevention.Clear();
  89. inputDuplicatePrevention = null;
  90. existingPorts.Clear();
  91. existingPorts = null;
  92. m_outputData.Add( currOutputData );
  93. }
  94. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  95. {
  96. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  97. if ( outputId < m_outputData.Count )
  98. {
  99. Dictionary<string, InputPort> inputs = InputPortsDict;
  100. string value = m_outputData[ outputId ].expression;
  101. for ( int i = 0; i < m_outputData[ outputId ].inputData.Count; i++ )
  102. {
  103. if ( inputs.ContainsKey( m_outputData[ outputId ].inputData[ i ].name ) )
  104. {
  105. InputPort inputPort = inputs[ m_outputData[ outputId ].inputData[ i ].name ];
  106. if ( inputPort != null )
  107. {
  108. string inputValue = inputPort.GenerateShaderForOutput( ref dataCollector, WirePortDataType.OBJECT, ignoreLocalvar );
  109. value = value.Replace( m_outputData[ outputId ].inputData[ i ].name, inputValue );
  110. }
  111. else
  112. {
  113. UIUtils.ShowMessage( m_outputData[ outputId ].inputData[ i ].name + " invalid on the inputs list", MessageSeverity.Error );
  114. return string.Empty;
  115. }
  116. }
  117. else
  118. {
  119. UIUtils.ShowMessage( m_outputData[ outputId ].inputData[ i ].name + " Not found on the inputs list", MessageSeverity.Error );
  120. return string.Empty;
  121. }
  122. }
  123. return value;
  124. }
  125. return string.Empty;
  126. }
  127. public void DumpOutputData()
  128. {
  129. for ( int i = 0; i < m_outputData.Count; i++ )
  130. {
  131. Debug.Log( m_outputData[ i ] );
  132. }
  133. }
  134. public override void Destroy()
  135. {
  136. base.Destroy();
  137. if ( m_outputData != null )
  138. {
  139. for ( int i = 0; i < m_outputData.Count; i++ )
  140. {
  141. m_outputData[ i ].Destroy();
  142. }
  143. m_outputData.Clear();
  144. m_outputData = null;
  145. }
  146. if ( m_includes != null )
  147. {
  148. m_includes.Clear();
  149. m_includes = null;
  150. }
  151. }
  152. }
  153. }