ReflectionProbeNode.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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( "Reflection Probe", "Miscellaneous", "Provides access to the nearest Reflection Probe to the object. Only available on URP.\n\nView Dir OS: View Direction in Object-space\nNormal OS: Normal in Object-space\nLOD: Index of level-of-detail" )]
  10. public class ReflectionProbeNode : ParentNode
  11. {
  12. private const string ReflectionProbeStr = "SHADERGRAPH_REFLECTION_PROBE({0},{1},{2})";
  13. private const string InfoTransformSpace = "Both View Dir and Normal vectors are set in Object Space";
  14. public const string NodeErrorMsg = "Only valid on URP";
  15. public const string ErrorOnCompilationMsg = "Attempting to use URP specific node on incorrect SRP or Builtin RP.";
  16. protected override void CommonInit( int uniqueId )
  17. {
  18. base.CommonInit( uniqueId );
  19. AddInputPort( WirePortDataType.FLOAT3, false, "View Dir OS" );
  20. AddInputPort( WirePortDataType.FLOAT3, false, "Normal OS" );
  21. AddInputPort( WirePortDataType.FLOAT, false, "LOD" );
  22. AddOutputPort( WirePortDataType.FLOAT3, "Out" );
  23. m_autoWrapProperties = true;
  24. m_errorMessageTooltip = NodeErrorMsg;
  25. m_errorMessageTypeIsError = NodeMessageType.Error;
  26. m_previewShaderGUID = "f7d3fa6f91f1f184f89060feb01051a1";
  27. m_drawPreviewAsSphere = true;
  28. }
  29. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  30. {
  31. base.OnNodeLogicUpdate( drawInfo );
  32. m_showErrorMessage = ( ContainerGraph.CurrentCanvasMode == NodeAvailability.SurfaceShader ) ||
  33. ( ContainerGraph.CurrentCanvasMode == NodeAvailability.TemplateShader && ContainerGraph.CurrentSRPType != TemplateSRPType.URP );
  34. }
  35. public override void DrawProperties()
  36. {
  37. base.DrawProperties();
  38. EditorGUILayout.HelpBox( InfoTransformSpace, MessageType.Info );
  39. if ( m_showErrorMessage )
  40. {
  41. EditorGUILayout.HelpBox( NodeErrorMsg, MessageType.Error );
  42. }
  43. }
  44. public override void SetPreviewInputs()
  45. {
  46. base.SetPreviewInputs();
  47. PreviewMaterial.SetInt( "viewDirInput", m_inputPorts[ 0 ].IsConnected ? 1 : 0 );
  48. PreviewMaterial.SetInt( "normalInput", m_inputPorts[ 1 ].IsConnected ? 1 : 0 );
  49. PreviewMaterial.SetInt( "lodInput", m_inputPorts[ 2 ].IsConnected ? 1 : 0 );
  50. }
  51. uint viewDirInput;
  52. uint normalInput;
  53. uint lodInput;
  54. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  55. {
  56. if ( !dataCollector.IsSRP || !dataCollector.TemplateDataCollectorInstance.IsLWRP )
  57. {
  58. UIUtils.ShowMessage( ErrorOnCompilationMsg, MessageSeverity.Error );
  59. return GenerateErrorValue();
  60. }
  61. if ( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  62. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  63. if ( dataCollector.IsSRP && dataCollector.CurrentSRPType == TemplateSRPType.URP )
  64. {
  65. if ( ASEPackageManagerHelper.PackageSRPVersion >= ( int )ASESRPBaseline.ASE_SRP_12 )
  66. {
  67. dataCollector.AddToPragmas( UniqueId, "multi_compile_fragment _ _REFLECTION_PROBE_BLENDING" );
  68. dataCollector.AddToPragmas( UniqueId, "multi_compile_fragment _ _REFLECTION_PROBE_BOX_PROJECTION" );
  69. }
  70. if ( ASEPackageManagerHelper.PackageSRPVersion >= ( int )ASESRPBaseline.ASE_SRP_14 )
  71. {
  72. dataCollector.AddToPragmas( UniqueId, "multi_compile _ _FORWARD_PLUS" );
  73. }
  74. }
  75. string viewDirOS;
  76. if ( m_inputPorts[ 0 ].IsConnected )
  77. {
  78. viewDirOS = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  79. }
  80. else
  81. {
  82. viewDirOS = dataCollector.TemplateDataCollectorInstance.GetViewDir( CurrentPrecisionType, space: ViewSpace.Object );
  83. }
  84. string normalOS;
  85. if ( m_inputPorts[ 1 ].IsConnected )
  86. {
  87. normalOS = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  88. }
  89. else
  90. {
  91. normalOS = dataCollector.TemplateDataCollectorInstance.GetVertexNormal( CurrentPrecisionType );
  92. }
  93. string lod = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  94. RegisterLocalVariable( outputId, string.Format( ReflectionProbeStr, viewDirOS, normalOS, lod ), ref dataCollector );
  95. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  96. }
  97. }
  98. }