Matrix3X3Node.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Matrix3X3", "Constants And Properties", "Matrix3X3 property" )]
  10. public sealed class Matrix3X3Node : MatrixParentNode
  11. {
  12. private string[,] m_fieldText = new string[ 3, 3 ] { { "0", "0", "0" }, { "0", "0", "0" }, { "0", "0", "0" } };
  13. public Matrix3X3Node() : base() { }
  14. public Matrix3X3Node( int uniqueId, float x, float y, float width, float height ) : base( uniqueId, x, y, width, height ) { }
  15. protected override void CommonInit( int uniqueId )
  16. {
  17. base.CommonInit( uniqueId );
  18. GlobalTypeWarningText = string.Format( GlobalTypeWarningText, "Matrix" );
  19. AddOutputPort( WirePortDataType.FLOAT3x3, Constants.EmptyPortValue );
  20. m_insideSize.Set( Constants.FLOAT_DRAW_WIDTH_FIELD_SIZE * 3 + Constants.FLOAT_WIDTH_SPACING * 2, Constants.FLOAT_DRAW_HEIGHT_FIELD_SIZE * 3 + Constants.FLOAT_WIDTH_SPACING * 2 + Constants.OUTSIDE_WIRE_MARGIN );
  21. //m_defaultValue = new Matrix4x4();
  22. //m_materialValue = new Matrix4x4();
  23. m_drawPreview = false;
  24. }
  25. public override void CopyDefaultsToMaterial()
  26. {
  27. m_materialValue = m_defaultValue;
  28. }
  29. public override void DrawSubProperties()
  30. {
  31. EditorGUILayout.LabelField( Constants.DefaultValueLabel );
  32. for( int row = 0; row < 3; row++ )
  33. {
  34. EditorGUILayout.BeginHorizontal();
  35. for( int column = 0; column < 3; column++ )
  36. {
  37. m_defaultValue[ row, column ] = EditorGUILayoutFloatField( string.Empty, m_defaultValue[ row, column ], GUILayout.MaxWidth( 76 ) );
  38. }
  39. EditorGUILayout.EndHorizontal();
  40. }
  41. }
  42. public override void DrawMaterialProperties()
  43. {
  44. if( m_materialMode )
  45. EditorGUI.BeginChangeCheck();
  46. EditorGUILayout.LabelField( Constants.MaterialValueLabel );
  47. for( int row = 0; row < 3; row++ )
  48. {
  49. EditorGUILayout.BeginHorizontal();
  50. for( int column = 0; column < 3; column++ )
  51. {
  52. m_materialValue[ row, column ] = EditorGUILayoutFloatField( string.Empty, m_materialValue[ row, column ], GUILayout.MaxWidth( 76 ) );
  53. }
  54. EditorGUILayout.EndHorizontal();
  55. }
  56. if( m_materialMode && EditorGUI.EndChangeCheck() )
  57. m_requireMaterialUpdate = true;
  58. }
  59. public override void OnNodeLayout( DrawInfo drawInfo )
  60. {
  61. base.OnNodeLayout( drawInfo );
  62. m_propertyDrawPos.position = m_remainingBox.position;
  63. m_propertyDrawPos.width = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_WIDTH_FIELD_SIZE;
  64. m_propertyDrawPos.height = drawInfo.InvertedZoom * Constants.FLOAT_DRAW_HEIGHT_FIELD_SIZE;
  65. }
  66. public override void DrawGUIControls( DrawInfo drawInfo )
  67. {
  68. base.DrawGUIControls( drawInfo );
  69. if( drawInfo.CurrentEventType != EventType.MouseDown )
  70. return;
  71. Rect hitBox = m_remainingBox;
  72. hitBox.height = m_insideSize.y * drawInfo.InvertedZoom;
  73. bool insideBox = hitBox.Contains( drawInfo.MousePosition );
  74. if( insideBox )
  75. {
  76. GUI.FocusControl( null );
  77. m_isEditingFields = true;
  78. }
  79. else if( m_isEditingFields && !insideBox )
  80. {
  81. GUI.FocusControl( null );
  82. m_isEditingFields = false;
  83. }
  84. }
  85. public override void Draw( DrawInfo drawInfo )
  86. {
  87. base.Draw( drawInfo );
  88. if( !m_isVisible )
  89. return;
  90. if( m_isEditingFields && m_currentParameterType != PropertyType.Global )
  91. {
  92. bool currMode = m_materialMode && m_currentParameterType != PropertyType.Constant;
  93. Matrix4x4 value = currMode ? m_materialValue : m_defaultValue;
  94. EditorGUI.BeginChangeCheck();
  95. for( int row = 0; row < 3; row++ )
  96. {
  97. for( int column = 0; column < 3; column++ )
  98. {
  99. m_propertyDrawPos.position = m_remainingBox.position + Vector2.Scale( m_propertyDrawPos.size, new Vector2( column, row ) ) + new Vector2( Constants.FLOAT_WIDTH_SPACING * drawInfo.InvertedZoom * column, Constants.FLOAT_WIDTH_SPACING * drawInfo.InvertedZoom * row );
  100. value[ row, column ] = EditorGUIFloatField( m_propertyDrawPos, string.Empty, value[ row, column ], UIUtils.MainSkin.textField );
  101. }
  102. }
  103. if( currMode )
  104. {
  105. m_materialValue = value;
  106. }
  107. else
  108. {
  109. m_defaultValue = value;
  110. }
  111. if( EditorGUI.EndChangeCheck() )
  112. {
  113. m_requireMaterialUpdate = m_materialMode;
  114. BeginDelayedDirtyProperty();
  115. }
  116. }
  117. else if( drawInfo.CurrentEventType == EventType.Repaint )
  118. {
  119. bool guiEnabled = GUI.enabled;
  120. //redundant ternary conditional but this makes easier to read that only when m_showCuffer is on that we need to take PropertyType.Property into account
  121. GUI.enabled = ( m_showCBuffer )? m_currentParameterType != PropertyType.Global&& m_currentParameterType != PropertyType.Property : m_currentParameterType != PropertyType.Global;
  122. bool currMode = m_materialMode && m_currentParameterType != PropertyType.Constant;
  123. Matrix4x4 value = currMode ? m_materialValue : m_defaultValue;
  124. for( int row = 0; row < 3; row++ )
  125. {
  126. for( int column = 0; column < 3; column++ )
  127. {
  128. Rect fakeField = m_propertyDrawPos;
  129. fakeField.position = m_remainingBox.position + Vector2.Scale( m_propertyDrawPos.size, new Vector2( column, row ) ) + new Vector2( Constants.FLOAT_WIDTH_SPACING * drawInfo.InvertedZoom * column, Constants.FLOAT_WIDTH_SPACING * drawInfo.InvertedZoom * row );
  130. if( GUI.enabled )
  131. EditorGUIUtility.AddCursorRect( fakeField, MouseCursor.Text );
  132. if( m_previousValue[ row, column ] != value[ row, column ] )
  133. {
  134. m_previousValue[ row, column ] = value[ row, column ];
  135. m_fieldText[ row, column ] = value[ row, column ].ToString();
  136. }
  137. GUI.Label( fakeField, m_fieldText[ row, column ], UIUtils.MainSkin.textField );
  138. }
  139. }
  140. GUI.enabled = guiEnabled;
  141. }
  142. }
  143. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  144. {
  145. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  146. m_precisionString = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, m_outputPorts[ 0 ].DataType );
  147. if( m_currentParameterType != PropertyType.Constant )
  148. {
  149. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  150. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  151. string localVarName = PropertyData( dataCollector.PortCategory ) + "Local3x3";
  152. string localVarValue = string.Format( "float3x3({0}._m00,{0}._m01,{0}._m02,{0}._m10,{0}._m11,{0}._m12,{0}._m20,{0}._m21,{0}._m22 )", PropertyData( dataCollector.PortCategory ) );
  153. RegisterLocalVariable( 0, localVarValue, ref dataCollector, localVarName );
  154. return localVarName;
  155. }
  156. Matrix4x4 value = m_defaultValue;
  157. return m_precisionString + "(" + value[ 0, 0 ] + "," + value[ 0, 1 ] + "," + value[ 0, 2 ] + "," +
  158. +value[ 1, 0 ] + "," + value[ 1, 1 ] + "," + value[ 1, 2 ] + "," +
  159. +value[ 2, 0 ] + "," + value[ 2, 1 ] + "," + value[ 2, 2 ] + ")";
  160. }
  161. public override void UpdateMaterial( Material mat )
  162. {
  163. base.UpdateMaterial( mat );
  164. if( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  165. {
  166. Shader.SetGlobalMatrix( m_propertyName, m_materialValue );
  167. //mat.SetMatrix( m_propertyName, m_materialValue );
  168. }
  169. }
  170. public override bool GetUniformData( out string dataType, out string dataName, ref bool fullValue )
  171. {
  172. dataType = UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, WirePortDataType.FLOAT4x4 );
  173. dataName = m_propertyName;
  174. return true;
  175. }
  176. public override void SetMaterialMode( Material mat, bool fetchMaterialValues )
  177. {
  178. base.SetMaterialMode( mat, fetchMaterialValues );
  179. if( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  180. {
  181. m_materialValue = mat.GetMatrix( m_propertyName );
  182. }
  183. }
  184. public override void ForceUpdateFromMaterial( Material material )
  185. {
  186. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  187. {
  188. m_materialValue = material.GetMatrix( m_propertyName );
  189. PreviewIsDirty = true;
  190. }
  191. }
  192. public override void ReadFromString( ref string[] nodeParams )
  193. {
  194. base.ReadFromString( ref nodeParams );
  195. m_defaultValue = IOUtils.StringToMatrix3x3( GetCurrentParam( ref nodeParams ) );
  196. }
  197. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  198. {
  199. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  200. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Matrix3x3ToString( m_defaultValue ) );
  201. }
  202. public override void ReadAdditionalClipboardData( ref string[] nodeParams )
  203. {
  204. base.ReadAdditionalClipboardData( ref nodeParams );
  205. m_materialValue = IOUtils.StringToMatrix3x3( GetCurrentParam( ref nodeParams ) );
  206. }
  207. public override void WriteAdditionalClipboardData( ref string nodeInfo )
  208. {
  209. base.WriteAdditionalClipboardData( ref nodeInfo );
  210. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Matrix3x3ToString( m_materialValue ) );
  211. }
  212. public override string GetPropertyValStr()
  213. {
  214. return ( m_materialMode && m_currentParameterType != PropertyType.Constant ) ? m_materialValue[ 0, 0 ].ToString( Mathf.Abs( m_materialValue[ 0, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 0, 1 ].ToString( Mathf.Abs( m_materialValue[ 0, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 0, 2 ].ToString( Mathf.Abs( m_materialValue[ 0, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  215. m_materialValue[ 1, 0 ].ToString( Mathf.Abs( m_materialValue[ 1, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 1, 1 ].ToString( Mathf.Abs( m_materialValue[ 1, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 1, 2 ].ToString( Mathf.Abs( m_materialValue[ 1, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  216. m_materialValue[ 2, 0 ].ToString( Mathf.Abs( m_materialValue[ 2, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 2, 1 ].ToString( Mathf.Abs( m_materialValue[ 2, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 2, 2 ].ToString( Mathf.Abs( m_materialValue[ 2, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) :
  217. m_defaultValue[ 0, 0 ].ToString( Mathf.Abs( m_defaultValue[ 0, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 0, 1 ].ToString( Mathf.Abs( m_defaultValue[ 0, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 0, 2 ].ToString( Mathf.Abs( m_defaultValue[ 0, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  218. m_defaultValue[ 1, 0 ].ToString( Mathf.Abs( m_defaultValue[ 1, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 1, 1 ].ToString( Mathf.Abs( m_defaultValue[ 1, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 1, 2 ].ToString( Mathf.Abs( m_defaultValue[ 1, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  219. m_defaultValue[ 2, 0 ].ToString( Mathf.Abs( m_defaultValue[ 2, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 2, 1 ].ToString( Mathf.Abs( m_defaultValue[ 2, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 2, 2 ].ToString( Mathf.Abs( m_defaultValue[ 2, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel );
  220. }
  221. }
  222. }