ObjectScaleNode.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Object Scale", "Object", "Object Scale extracted directly from its transform matrix" )]
  10. public class ObjectScaleNode : ParentNode
  11. {
  12. private const string RotationIndependentScaleStr = "Rotation Independent Scale";
  13. [SerializeField]
  14. private bool m_rotationIndependentScale = false;
  15. protected override void CommonInit( int uniqueId )
  16. {
  17. base.CommonInit( uniqueId );
  18. AddOutputVectorPorts( WirePortDataType.FLOAT3, "XYZ" );
  19. m_drawPreviewAsSphere = true;
  20. m_previewShaderGUID = "5540033c6c52f51468938c1a42bd2730";
  21. m_textLabelWidth = 180;
  22. UpdateMaterialPass();
  23. m_autoWrapProperties = true;
  24. }
  25. public override void DrawProperties()
  26. {
  27. base.DrawProperties();
  28. EditorGUI.BeginChangeCheck();
  29. m_rotationIndependentScale = EditorGUILayoutToggle( RotationIndependentScaleStr, m_rotationIndependentScale );
  30. if( EditorGUI.EndChangeCheck() )
  31. {
  32. UpdateMaterialPass();
  33. }
  34. }
  35. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  36. {
  37. string objectScale = m_rotationIndependentScale ? GeneratorUtils.GenerateRotationIndependentObjectScale( ref dataCollector, UniqueId ):
  38. GeneratorUtils.GenerateObjectScale( ref dataCollector, UniqueId );
  39. return GetOutputVectorItem( 0, outputId, objectScale );
  40. }
  41. public override void ReadFromString( ref string[] nodeParams )
  42. {
  43. base.ReadFromString( ref nodeParams );
  44. if( UIUtils.CurrentShaderVersion() < 17402 )
  45. {
  46. m_rotationIndependentScale = false;
  47. }
  48. else
  49. {
  50. m_rotationIndependentScale = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  51. }
  52. UpdateMaterialPass();
  53. }
  54. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  55. {
  56. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  57. IOUtils.AddFieldValueToString( ref nodeInfo, m_rotationIndependentScale );
  58. }
  59. void UpdateMaterialPass()
  60. {
  61. m_previewMaterialPassId = m_rotationIndependentScale ? 1 : 0;
  62. PreviewIsDirty = true;
  63. }
  64. }
  65. }