ShadeVertexLightsHlpNode.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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( "Shade Vertex Lights", "Lighting", "Computes illumination from four per-vertex lights and ambient, given object space position & normal" )]
  10. public sealed class ShadeVertexLightsHlpNode : ParentNode
  11. {
  12. private const string HelperMessage = "Shade Vertex Lights node only outputs correct results on\nTemplate Vertex/Frag shaders with their LightMode set to Vertex.";
  13. private const string ShadeVertexLightFunc = "ShadeVertexLightsFull({0},{1},{2},{3})";
  14. private const string LightCount = "Light Count";
  15. private const string IsSpotlight = "Is Spotlight";
  16. private const int MinLightCount = 0;
  17. private const int MaxLightCount = 8;
  18. [SerializeField]
  19. private int m_lightCount = 4;
  20. [SerializeField]
  21. private bool m_enableSpotlight = false;
  22. private int _LightCountId;
  23. private int _IsSpotlightId;
  24. protected override void CommonInit( int uniqueId )
  25. {
  26. base.CommonInit( uniqueId );
  27. AddInputPort( WirePortDataType.FLOAT4, false, "Vertex Position" );
  28. AddInputPort( WirePortDataType.FLOAT3, false, "Vertex Normal" );
  29. AddOutputPort( WirePortDataType.FLOAT3, Constants.EmptyPortValue );
  30. m_useInternalPortData = true;
  31. //m_autoWrapProperties = true;
  32. m_textLabelWidth = 90;
  33. m_previewShaderGUID = "3b6075034a85ad047be2d31dd213fb4f";
  34. }
  35. public override void OnEnable()
  36. {
  37. base.OnEnable();
  38. _LightCountId = Shader.PropertyToID( "_LightCount" );
  39. _IsSpotlightId = Shader.PropertyToID( "_IsSpotlight" );
  40. }
  41. public override void DrawProperties()
  42. {
  43. base.DrawProperties();
  44. NodeUtils.DrawPropertyGroup( ref m_propertiesFoldout, Constants.ParameterLabelStr, DrawGeneralProperties );
  45. EditorGUILayout.HelpBox( HelperMessage, MessageType.Info );
  46. }
  47. public override void SetPreviewInputs()
  48. {
  49. base.SetPreviewInputs();
  50. PreviewMaterial.SetInt( _LightCountId, m_lightCount );
  51. PreviewMaterial.SetInt( _IsSpotlightId, ( m_enableSpotlight ? 1 : 0 ) );
  52. }
  53. void DrawGeneralProperties()
  54. {
  55. m_lightCount = EditorGUILayoutIntSlider( LightCount, m_lightCount, MinLightCount, MaxLightCount );
  56. m_enableSpotlight = EditorGUILayoutToggle( IsSpotlight, m_enableSpotlight );
  57. }
  58. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  59. {
  60. if( dataCollector.MasterNodeCategory == AvailableShaderTypes.SurfaceShader )
  61. UIUtils.ShowMessage( UniqueId, HelperMessage, MessageSeverity.Warning );
  62. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  63. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  64. dataCollector.AddToIncludes( UniqueId, Constants.UnityCgLibFuncs );
  65. string vertexPosition = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  66. string vertexNormal = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  67. string value = string.Format( ShadeVertexLightFunc, vertexPosition, vertexNormal, m_lightCount, m_enableSpotlight.ToString().ToLower() );
  68. RegisterLocalVariable( 0, value, ref dataCollector, "shadeVertexLight" + OutputId );
  69. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  70. }
  71. public override void ReadFromString( ref string[] nodeParams )
  72. {
  73. base.ReadFromString( ref nodeParams );
  74. if( UIUtils.CurrentShaderVersion() > 14301 )
  75. {
  76. m_lightCount = Convert.ToInt32( GetCurrentParam( ref nodeParams ) );
  77. m_enableSpotlight = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  78. }
  79. }
  80. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  81. {
  82. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  83. IOUtils.AddFieldValueToString( ref nodeInfo, m_lightCount );
  84. IOUtils.AddFieldValueToString( ref nodeInfo, m_enableSpotlight );
  85. }
  86. }
  87. }