DecodeLightmapHlpNode.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. [Serializable]
  8. [NodeAttributes( "Decode Lightmap", "Miscellaneous", "Decodes color from Unity lightmap (RGBM or dLDR depending on platform)" )]
  9. public sealed class DecodeLightmapHlpNode : ParentNode
  10. {
  11. private const string m_funcStandard = "DecodeLightmap({0})";
  12. private string m_funcSRP = "DecodeLightmap({0},{1})";
  13. private const string DecodeInstructionsLWValueStr = "half4 decodeLightmapInstructions = half4(LIGHTMAP_HDR_MULTIPLIER, LIGHTMAP_HDR_EXPONENT, 0.0h, 0.0h);";
  14. private const string DecodeInstructionsNameStr = "decodeLightmapInstructions";
  15. private readonly string[] DecodeInstructionsHDValueStr =
  16. {
  17. "#ifdef UNITY_LIGHTMAP_FULL_HDR//ase_decode_lightmap_0",
  18. "\tbool useRGBMLightmap = false;//ase_decode_lightmap_1",
  19. "\tfloat4 decodeLightmapInstructions = float4( 0.0, 0.0, 0.0, 0.0 );//ase_decode_lightmap_2",
  20. "#else//ase_decode_lightmap//ase_decode_lightmap_3",
  21. "\tbool useRGBMLightmap = true;//ase_decode_lightmap_4",
  22. "#if defined(UNITY_LIGHTMAP_RGBM_ENCODING)//ase_decode_lightmap_5",
  23. "\tfloat4 decodeLightmapInstructions = float4(34.493242, 2.2, 0.0, 0.0);//ase_decode_lightmap_6",
  24. "#else//ase_decode_lightmap_7",
  25. "\tfloat4 decodeLightmapInstructions = float4( 2.0, 2.2, 0.0, 0.0 );//ase_decode_lightmap_8",
  26. "#endif//ase_decode_lightmap_9",
  27. "#endif//ase_decode_lightmap_10"
  28. };
  29. private string m_localVarName = null;
  30. protected override void CommonInit( int uniqueId )
  31. {
  32. base.CommonInit( uniqueId );
  33. AddInputPort( WirePortDataType.FLOAT4, false, "Value" );
  34. AddInputPort( WirePortDataType.FLOAT4, false, "Instructions" );
  35. AddOutputPort( WirePortDataType.FLOAT3, Constants.EmptyPortValue );
  36. m_previewShaderGUID = "c2d3bee1aee183343b31b9208cb402e9";
  37. m_useInternalPortData = true;
  38. }
  39. public override string GetIncludes()
  40. {
  41. return Constants.UnityCgLibFuncs;
  42. }
  43. protected override void OnUniqueIDAssigned()
  44. {
  45. base.OnUniqueIDAssigned();
  46. m_localVarName = "decodeLightMap" + OutputId;
  47. }
  48. public override void OnNodeLogicUpdate( DrawInfo drawInfo )
  49. {
  50. base.OnNodeLogicUpdate( drawInfo );
  51. m_inputPorts[ 1 ].Visible = m_containerGraph.ParentWindow.IsShaderFunctionWindow || m_containerGraph.IsSRP;
  52. }
  53. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  54. {
  55. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  56. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  57. base.GenerateShaderForOutput( outputId, ref dataCollector, ignoreLocalvar );
  58. string value = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  59. string finalResult = string.Empty;
  60. if( dataCollector.IsTemplate && dataCollector.IsSRP )
  61. {
  62. string instructions = string.Empty;
  63. if( m_inputPorts[ 1 ].IsConnected )
  64. {
  65. instructions = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  66. }
  67. else
  68. {
  69. if( dataCollector.TemplateDataCollectorInstance.IsHDRP )
  70. {
  71. for( int i = 0; i < DecodeInstructionsHDValueStr.Length; i++ )
  72. {
  73. dataCollector.AddLocalVariable( UniqueId, DecodeInstructionsHDValueStr[ i ] );
  74. }
  75. }
  76. else
  77. {
  78. dataCollector.AddLocalVariable( UniqueId, DecodeInstructionsLWValueStr );
  79. }
  80. instructions = DecodeInstructionsNameStr;
  81. }
  82. finalResult = string.Format( m_funcSRP, value , instructions );
  83. }
  84. else
  85. {
  86. dataCollector.AddToIncludes( UniqueId, Constants.UnityCgLibFuncs );
  87. finalResult = string.Format( m_funcStandard, value );
  88. }
  89. RegisterLocalVariable( 0, finalResult, ref dataCollector, m_localVarName );
  90. return GetOutputVectorItem( 0, outputId, m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory ) );
  91. }
  92. }
  93. }