Matrix4X4Node.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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( "Matrix4X4", "Constants And Properties", "Matrix4X4 property" )]
  10. public sealed class Matrix4X4Node : MatrixParentNode
  11. {
  12. private string[,] m_fieldText = new string[ 4, 4 ] { { "0", "0", "0", "0" }, { "0", "0", "0", "0" }, { "0", "0", "0", "0" }, { "0", "0", "0", "0" } };
  13. public Matrix4X4Node() : base() { }
  14. public Matrix4X4Node( 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.FLOAT4x4, Constants.EmptyPortValue );
  20. m_insideSize.Set( Constants.FLOAT_DRAW_WIDTH_FIELD_SIZE * 4 + Constants.FLOAT_WIDTH_SPACING * 3, Constants.FLOAT_DRAW_HEIGHT_FIELD_SIZE * 4 + Constants.FLOAT_WIDTH_SPACING * 3 + 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 < 4; row++ )
  33. {
  34. EditorGUILayout.BeginHorizontal();
  35. for ( int column = 0; column < 4; column++ )
  36. {
  37. m_defaultValue[ row, column ] = EditorGUILayoutFloatField( string.Empty, m_defaultValue[ row, column ], GUILayout.MaxWidth( 55 ) );
  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 < 4; row++ )
  48. {
  49. EditorGUILayout.BeginHorizontal();
  50. for ( int column = 0; column < 4; column++ )
  51. {
  52. m_materialValue[ row, column ] = EditorGUILayoutFloatField( string.Empty, m_materialValue[ row, column ], GUILayout.MaxWidth( 55 ) );
  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 < 4; row++ )
  96. {
  97. for ( int column = 0; column < 4; 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 < 4; row++ )
  125. {
  126. for ( int column = 0; column < 4; 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. return PropertyData( dataCollector.PortCategory );
  149. Matrix4x4 value = m_defaultValue;
  150. return m_precisionString+"(" + value[ 0, 0 ] + "," + value[ 0, 1 ] + "," + value[ 0, 2 ] + "," + value[ 0, 3 ] + "," +
  151. +value[ 1, 0 ] + "," + value[ 1, 1 ] + "," + value[ 1, 2 ] + "," + value[ 1, 3 ] + "," +
  152. +value[ 2, 0 ] + "," + value[ 2, 1 ] + "," + value[ 2, 2 ] + "," + value[ 2, 3 ] + "," +
  153. +value[ 3, 0 ] + "," + value[ 3, 1 ] + "," + value[ 3, 2 ] + "," + value[ 3, 3 ] + ")";
  154. }
  155. public override void UpdateMaterial( Material mat )
  156. {
  157. base.UpdateMaterial( mat );
  158. if ( UIUtils.IsProperty( m_currentParameterType ) && !InsideShaderFunction )
  159. {
  160. mat.SetMatrix( m_propertyName, m_materialValue );
  161. }
  162. }
  163. public override void SetMaterialMode( Material mat , bool fetchMaterialValues )
  164. {
  165. base.SetMaterialMode( mat , fetchMaterialValues );
  166. if ( fetchMaterialValues && m_materialMode && UIUtils.IsProperty( m_currentParameterType ) && mat.HasProperty( m_propertyName ) )
  167. {
  168. m_materialValue = mat.GetMatrix( m_propertyName );
  169. }
  170. }
  171. public override void ForceUpdateFromMaterial( Material material )
  172. {
  173. if( UIUtils.IsProperty( m_currentParameterType ) && material.HasProperty( m_propertyName ) )
  174. {
  175. m_materialValue = material.GetMatrix( m_propertyName );
  176. PreviewIsDirty = true;
  177. }
  178. }
  179. public override void ReadFromString( ref string[] nodeParams )
  180. {
  181. base.ReadFromString( ref nodeParams );
  182. m_defaultValue = IOUtils.StringToMatrix4x4( GetCurrentParam( ref nodeParams ) );
  183. }
  184. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  185. {
  186. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  187. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Matrix4x4ToString( m_defaultValue ) );
  188. }
  189. public override void ReadAdditionalClipboardData( ref string[] nodeParams )
  190. {
  191. base.ReadAdditionalClipboardData( ref nodeParams );
  192. m_materialValue = IOUtils.StringToMatrix4x4( GetCurrentParam( ref nodeParams ) );
  193. }
  194. public override void WriteAdditionalClipboardData( ref string nodeInfo )
  195. {
  196. base.WriteAdditionalClipboardData( ref nodeInfo );
  197. IOUtils.AddFieldValueToString( ref nodeInfo, IOUtils.Matrix4x4ToString( m_materialValue ) );
  198. }
  199. public override string GetPropertyValStr()
  200. {
  201. 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.VECTOR_SEPARATOR + m_materialValue[ 0, 3 ].ToString( Mathf.Abs( m_materialValue[ 0, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  202. 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.VECTOR_SEPARATOR + m_materialValue[ 1, 3 ].ToString( Mathf.Abs( m_materialValue[ 1, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  203. 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 ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 2, 3 ].ToString( Mathf.Abs( m_materialValue[ 2, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  204. m_materialValue[ 3, 0 ].ToString( Mathf.Abs( m_materialValue[ 3, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 3, 1 ].ToString( Mathf.Abs( m_materialValue[ 3, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 3, 2 ].ToString( Mathf.Abs( m_materialValue[ 3, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_materialValue[ 3, 3 ].ToString( Mathf.Abs( m_materialValue[ 3, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) :
  205. 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.VECTOR_SEPARATOR + m_defaultValue[ 0, 3 ].ToString( Mathf.Abs( m_defaultValue[ 0, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  206. 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.VECTOR_SEPARATOR + m_defaultValue[ 1, 3 ].ToString( Mathf.Abs( m_defaultValue[ 1, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  207. 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 ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 2, 3 ].ToString( Mathf.Abs( m_defaultValue[ 2, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.MATRIX_DATA_SEPARATOR +
  208. m_defaultValue[ 3, 0 ].ToString( Mathf.Abs( m_defaultValue[ 3, 0 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 3, 1 ].ToString( Mathf.Abs( m_defaultValue[ 3, 1 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 3, 2 ].ToString( Mathf.Abs( m_defaultValue[ 3, 2 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel ) + IOUtils.VECTOR_SEPARATOR + m_defaultValue[ 3, 3 ].ToString( Mathf.Abs( m_defaultValue[ 3, 3 ] ) > 1000 ? Constants.PropertyBigMatrixFormatLabel : Constants.PropertyMatrixFormatLabel );
  209. }
  210. }
  211. }