PositionNode.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace AmplifyShaderEditor
  7. {
  8. [Serializable]
  9. [NodeAttributes( "Position", "Surface Data", "Surface position. Absolute World-space, by default.\n\n" +
  10. "Object: Object-space position, local to object.\n\n" +
  11. "World: Absolute world position, global to scene origin.\n\n" +
  12. "Relative World: Camera-relative world position.\n\n" +
  13. "View: View-space position.\n\n" )]
  14. public sealed class PositionNode : SurfaceShaderINParentNode
  15. {
  16. public enum Space
  17. {
  18. Object,
  19. World,
  20. RelativeWorld,
  21. View
  22. }
  23. private readonly string[] m_outputSpaceStr = { "Object", "World", "Relative World", "View" };
  24. [SerializeField]
  25. private int m_outputSpaceIndex = ( int )Space.World;
  26. private UpperLeftWidgetHelper m_upperLeftWidget = new UpperLeftWidgetHelper();
  27. protected override void CommonInit( int uniqueId )
  28. {
  29. base.CommonInit( uniqueId );
  30. m_currentInput = SurfaceInputs.WORLD_POS;
  31. m_drawPreviewAsSphere = true;
  32. m_previewShaderGUID = "62a28fcb1f1dd5640a572771eb121a3b";
  33. m_hasLeftDropdown = true;
  34. m_textLabelWidth = 65;
  35. m_autoWrapProperties = true;
  36. InitialSetup();
  37. m_previewMaterialPassId = ( int )m_outputSpaceIndex;
  38. ConfigureHeader();
  39. }
  40. public override void Draw( DrawInfo drawInfo )
  41. {
  42. base.Draw( drawInfo );
  43. EditorGUI.BeginChangeCheck();
  44. m_outputSpaceIndex = m_upperLeftWidget.DrawWidget( this, m_outputSpaceIndex, m_outputSpaceStr );
  45. if ( EditorGUI.EndChangeCheck() )
  46. {
  47. ConfigureHeader();
  48. }
  49. }
  50. public override void DrawProperties()
  51. {
  52. EditorGUI.BeginChangeCheck();
  53. m_outputSpaceIndex = EditorGUILayoutPopup( "Mode", m_outputSpaceIndex, m_outputSpaceStr );
  54. if ( EditorGUI.EndChangeCheck() )
  55. {
  56. ConfigureHeader();
  57. }
  58. if ( m_outputSpaceIndex == ( int )Space.World )
  59. {
  60. EditorGUILayout.HelpBox( "World refers to absolute World-Space coordinates, in full coordinates around the origin.", MessageType.Info );
  61. }
  62. else if ( m_outputSpaceIndex == ( int )Space.RelativeWorld )
  63. {
  64. EditorGUILayout.HelpBox( "Relative World refers to Camera-relative World-space, where values are kept relative to the Camera position, making them less prone to the numerical precision degradation of large floating point values.", MessageType.Info );
  65. }
  66. }
  67. void ConfigureHeader()
  68. {
  69. SetAdditonalTitleText( string.Format( Constants.SubTitleModeFormatStr, m_outputSpaceStr[ m_outputSpaceIndex ] ) );
  70. m_previewMaterialPassId = m_outputSpaceIndex;
  71. }
  72. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalVar )
  73. {
  74. if ( dataCollector.PortCategory == MasterNodePortCategory.Tessellation )
  75. {
  76. UIUtils.ShowNoVertexModeNodeMessage( this );
  77. return string.Format( "{0}( 0, 0, 0 )", UIUtils.PrecisionWirePortToCgType( CurrentPrecisionType, WirePortDataType.FLOAT3 ) );
  78. }
  79. if ( dataCollector.IsTemplate )
  80. {
  81. string varName = dataCollector.TemplateDataCollectorInstance.GetPosition( ( Space )m_outputSpaceIndex );
  82. return GetOutputVectorItem( 0, outputId, varName );
  83. }
  84. else
  85. {
  86. string worldPosition = GeneratorUtils.GeneratePosition( ref dataCollector, UniqueId, ( Space )m_outputSpaceIndex );
  87. return GetOutputVectorItem( 0, outputId, worldPosition );
  88. }
  89. }
  90. public override void ReadFromString( ref string[] nodeParams )
  91. {
  92. base.ReadFromString( ref nodeParams );
  93. m_outputSpaceIndex = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  94. ConfigureHeader();
  95. }
  96. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  97. {
  98. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  99. IOUtils.AddFieldValueToString( ref nodeInfo, m_outputSpaceIndex );
  100. }
  101. }
  102. }