WorldSpaceLightDirHlpNode.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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( "World Space Light Dir", "Lighting", "Computes normalized world space light direction" )]
  10. public sealed class WorldSpaceLightDirHlpNode : HelperParentNode
  11. {
  12. private const string NormalizeOptionStr = "Safe Normalize";
  13. [SerializeField]
  14. private bool m_safeNormalize = false;
  15. protected override void CommonInit( int uniqueId )
  16. {
  17. base.CommonInit( uniqueId );
  18. m_funcType = "UnityWorldSpaceLightDir";
  19. m_inputPorts[ 0 ].Visible = false;
  20. m_outputPorts[ 0 ].ChangeType( WirePortDataType.FLOAT3, false );
  21. m_outputPorts[ 0 ].Name = "XYZ";
  22. AddOutputPort( WirePortDataType.FLOAT, "X" );
  23. AddOutputPort( WirePortDataType.FLOAT, "Y" );
  24. AddOutputPort( WirePortDataType.FLOAT, "Z" );
  25. m_useInternalPortData = false;
  26. m_drawPreviewAsSphere = true;
  27. m_autoWrapProperties = true;
  28. m_textLabelWidth = 120;
  29. m_previewShaderGUID = "2e8dc46eb6fb2124d9f0007caf9567e3";
  30. }
  31. public override void PropagateNodeData( NodeData nodeData, ref MasterNodeDataCollector dataCollector )
  32. {
  33. base.PropagateNodeData( nodeData, ref dataCollector );
  34. if( m_safeNormalize )
  35. dataCollector.SafeNormalizeLightDir = true;
  36. }
  37. public override void DrawProperties()
  38. {
  39. base.DrawProperties();
  40. m_safeNormalize = EditorGUILayoutToggle( NormalizeOptionStr, m_safeNormalize );
  41. EditorGUILayout.HelpBox( "Having safe normalize ON makes sure your light vector is not zero even if there's no lights in your scene.", MessageType.None );
  42. }
  43. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  44. {
  45. if( dataCollector.IsTemplate )
  46. return GetOutputVectorItem( 0, outputId, dataCollector.TemplateDataCollectorInstance.GetWorldSpaceLightDir( CurrentPrecisionType ) ); ;
  47. dataCollector.AddToIncludes( UniqueId, Constants.UnityCgLibFuncs );
  48. dataCollector.AddToInput( UniqueId, SurfaceInputs.WORLD_POS );
  49. return GetOutputVectorItem( 0, outputId, GeneratorUtils.GenerateWorldLightDirection( ref dataCollector, UniqueId, CurrentPrecisionType ) );
  50. }
  51. public override void ReadFromString( ref string[] nodeParams )
  52. {
  53. base.ReadFromString( ref nodeParams );
  54. if( UIUtils.CurrentShaderVersion() > 15201 )
  55. {
  56. m_safeNormalize = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  57. }
  58. }
  59. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  60. {
  61. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  62. IOUtils.AddFieldValueToString( ref nodeInfo, m_safeNormalize );
  63. }
  64. }
  65. }