BakedGINode.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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( "SRP Baked GI", "Miscellaneous", "Gets Baked GI info." )]
  10. public sealed class BakedGINode : ParentNode
  11. {
  12. private const string BakedGIHeaderHDRP = "ASEBakedGI( {0}, {1}, {2}, {3} )";
  13. private readonly string[] BakedGIBodyHDRP =
  14. {
  15. "float3 ASEBakedGI( float3 positionWS, float3 normalWS, float2 uvStaticLightmap, float2 uvDynamicLightmap )\n",
  16. "{\n",
  17. "\tfloat3 positionRWS = GetCameraRelativePositionWS( positionWS );\n",
  18. "\treturn SampleBakedGI( positionRWS, normalWS, uvStaticLightmap, uvDynamicLightmap );\n",
  19. "}\n"
  20. };
  21. private const string BakedGIHeaderHDRP2 = "ASEBakedGI( {0}, {1}, {2}, {3}, {4} )";
  22. private readonly string[] BakedGIBodyHDRP2 =
  23. {
  24. "float3 ASEBakedGI( float3 positionWS, float3 normalWS, uint2 positionSS, float2 uvStaticLightmap, float2 uvDynamicLightmap )\n",
  25. "{\n",
  26. "\tfloat3 positionRWS = GetCameraRelativePositionWS( positionWS );\n",
  27. "\tbool needToIncludeAPV = true;\n",
  28. "\treturn SampleBakedGI( positionRWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap, needToIncludeAPV );\n",
  29. "}\n"
  30. };
  31. private readonly string BakedGIHeaderURP = "ASEBakedGI( {0}, {1}, {2})";
  32. private readonly string[] BakedGIBodyURP =
  33. {
  34. "float3 ASEBakedGI( float3 normalWS, float2 uvStaticLightmap, bool applyScaling )\n",
  35. "{\n",
  36. "#ifdef LIGHTMAP_ON\n",
  37. "\tif( applyScaling )\n",
  38. "\t\tuvStaticLightmap = uvStaticLightmap * unity_LightmapST.xy + unity_LightmapST.zw;\n",
  39. "\treturn SampleLightmap( uvStaticLightmap, normalWS );\n",
  40. "#else\n",
  41. "\treturn SampleSH(normalWS);\n",
  42. "#endif\n",
  43. "}\n"
  44. };
  45. private readonly string BakedGIHeaderURP2 = "ASEBakedGI( {0}, {1}, {2}, {3}, {4}, {5})";
  46. private readonly string[] BakedGIBodyURP2 =
  47. {
  48. "float3 ASEBakedGI( float3 positionWS, float3 normalWS, uint2 positionSS, float2 uvStaticLightmap, float2 uvDynamicLightmap, bool applyScaling )\n",
  49. "{\n",
  50. "#ifdef LIGHTMAP_ON\n",
  51. "\tif (applyScaling)\n",
  52. "\t{\n",
  53. "\t\tuvStaticLightmap = uvStaticLightmap * unity_LightmapST.xy + unity_LightmapST.zw;\n",
  54. "\t\tuvDynamicLightmap = uvDynamicLightmap * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;\n",
  55. "\t}\n",
  56. "#if defined(DYNAMICLIGHTMAP_ON)\n",
  57. "\treturn SampleLightmap(uvStaticLightmap, uvDynamicLightmap, normalWS);\n",
  58. "#else\n",
  59. "\treturn SampleLightmap(uvStaticLightmap, normalWS);\n",
  60. "#endif\n",
  61. "#else\n",
  62. "#if (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))\n",
  63. "\tif (_EnableProbeVolumes)\n",
  64. "\t{\n",
  65. "\t\tfloat3 bakeDiffuseLighting;\n",
  66. "\t\tEvaluateAdaptiveProbeVolume(positionWS, normalWS, GetWorldSpaceNormalizeViewDir(positionWS), positionSS, bakeDiffuseLighting);\n",
  67. "\t\treturn bakeDiffuseLighting;\n",
  68. "\t}\n",
  69. "\telse\n",
  70. "\treturn SampleSH(normalWS);\n",
  71. "#else\n",
  72. "\treturn SampleSH(normalWS);\n",
  73. "#endif\n",
  74. "#endif\n",
  75. "}\n"
  76. };
  77. private const string ApplyScalingStr = "Apply Scaling";
  78. [SerializeField]
  79. private bool m_applyScaling = true;
  80. protected override void CommonInit( int uniqueId )
  81. {
  82. base.CommonInit( uniqueId );
  83. AddInputPort( WirePortDataType.FLOAT3, false, "World Position" );
  84. AddInputPort( WirePortDataType.FLOAT3, false, "World Normal" );
  85. AddInputPort( WirePortDataType.FLOAT2, false, "Static UV" );
  86. AddInputPort( WirePortDataType.FLOAT2, false, "Dynamic UV" );
  87. AddInputPort( WirePortDataType.FLOAT4, false, "Screen Position" );
  88. AddOutputPort( WirePortDataType.FLOAT3, Constants.EmptyPortValue );
  89. m_textLabelWidth = 95;
  90. m_autoWrapProperties = true;
  91. }
  92. public override void DrawProperties()
  93. {
  94. base.DrawProperties();
  95. EditorGUILayout.HelpBox( "Screen Position input must be Normalized (xyz/w), which is the default setting for Screen Position node.", MessageType.Info );
  96. m_applyScaling = EditorGUILayoutToggle( ApplyScalingStr, m_applyScaling );
  97. }
  98. public override string GenerateShaderForOutput( int outputId, ref MasterNodeDataCollector dataCollector, bool ignoreLocalvar )
  99. {
  100. if( !dataCollector.IsSRP )
  101. {
  102. UIUtils.ShowMessage( "Node only intended to use on HDRP and URP rendering pipelines" );
  103. return GenerateErrorValue();
  104. }
  105. if( m_outputPorts[ 0 ].IsLocalValue( dataCollector.PortCategory ) )
  106. return m_outputPorts[ 0 ].LocalValue( dataCollector.PortCategory );
  107. string positionWS;
  108. if ( m_inputPorts[ 0 ].IsConnected )
  109. {
  110. positionWS = m_inputPorts[ 0 ].GeneratePortInstructions( ref dataCollector );
  111. }
  112. else
  113. {
  114. positionWS = GeneratorUtils.GenerateWorldPosition( ref dataCollector, UniqueId );
  115. }
  116. string normalWS;
  117. if ( m_inputPorts[ 1 ].IsConnected )
  118. {
  119. normalWS = m_inputPorts[ 1 ].GeneratePortInstructions( ref dataCollector );
  120. }
  121. else
  122. {
  123. normalWS = GeneratorUtils.GenerateWorldNormal( ref dataCollector, UniqueId );
  124. }
  125. string uvStaticLightmap = m_inputPorts[ 2 ].GeneratePortInstructions( ref dataCollector );
  126. string uvDynamicLightmap = m_inputPorts[ 3 ].GeneratePortInstructions( ref dataCollector );
  127. string screenPos;
  128. string positionSS;
  129. if ( m_inputPorts[ 4 ].IsConnected )
  130. {
  131. screenPos = m_inputPorts[ 4 ].GeneratePortInstructions( ref dataCollector );
  132. positionSS = string.Format( "( uint2 )( {0}.xy * _ScreenSize.xy )", screenPos );
  133. }
  134. else
  135. {
  136. screenPos = GeneratorUtils.GenerateScreenPositionRaw( ref dataCollector, UniqueId, CurrentPrecisionType );
  137. positionSS = string.Format( "( uint2 )( {0}.xy / {0}.w * _ScreenSize.xy )", screenPos );
  138. }
  139. string localVarName = "bakedGI" + OutputId;
  140. if ( dataCollector.TemplateDataCollectorInstance.IsHDRP )
  141. {
  142. bool unityIsBeta = TemplateHelperFunctions.GetUnityBetaVersion( out int betaVersion );
  143. int unityVersion = TemplateHelperFunctions.GetUnityVersion();
  144. if ( ASEPackageManagerHelper.CurrentHDRPBaseline == ASESRPBaseline.ASE_SRP_14 && unityVersion >= 20220326 ||
  145. ASEPackageManagerHelper.CurrentHDRPBaseline == ASESRPBaseline.ASE_SRP_16 && unityVersion >= 20230220 ||
  146. ASEPackageManagerHelper.CurrentHDRPBaseline == ASESRPBaseline.ASE_SRP_17 && unityIsBeta && betaVersion >= 15 ||
  147. ASEPackageManagerHelper.CurrentSRPVersion >= ( int )170003 )
  148. {
  149. dataCollector.AddToPragmas( UniqueId, "multi_compile_fragment _ PROBE_VOLUMES_L1 PROBE_VOLUMES_L2" );
  150. dataCollector.AddFunction( BakedGIBodyHDRP2[ 0 ], BakedGIBodyHDRP2, false );
  151. RegisterLocalVariable( 0, string.Format( BakedGIHeaderHDRP2, positionWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap ), ref dataCollector, localVarName );
  152. }
  153. else
  154. {
  155. dataCollector.AddFunction( BakedGIBodyHDRP[ 0 ], BakedGIBodyHDRP, false );
  156. RegisterLocalVariable( 0, string.Format( BakedGIHeaderHDRP, positionWS, normalWS, uvStaticLightmap, uvDynamicLightmap ), ref dataCollector, localVarName );
  157. }
  158. }
  159. else
  160. {
  161. if ( ASEPackageManagerHelper.CurrentURPBaseline >= ASESRPBaseline.ASE_SRP_15 )
  162. {
  163. dataCollector.AddToPragmas( UniqueId, "multi_compile_fragment _ PROBE_VOLUMES_L1 PROBE_VOLUMES_L2" );
  164. dataCollector.AddFunction( BakedGIBodyURP2[ 0 ], BakedGIBodyURP2, false );
  165. RegisterLocalVariable( 0, string.Format( BakedGIHeaderURP2, positionWS, normalWS, positionSS, uvStaticLightmap, uvDynamicLightmap, m_applyScaling ? "true" : "false" ), ref dataCollector, localVarName );
  166. }
  167. else
  168. {
  169. dataCollector.AddFunction( BakedGIBodyURP[ 0 ], BakedGIBodyURP, false );
  170. RegisterLocalVariable( 0, string.Format( BakedGIHeaderURP, normalWS, uvStaticLightmap, m_applyScaling ? "true" : "false" ), ref dataCollector, localVarName );
  171. }
  172. }
  173. return localVarName;
  174. }
  175. public override void ReadFromString( ref string[] nodeParams )
  176. {
  177. base.ReadFromString( ref nodeParams );
  178. m_applyScaling = Convert.ToBoolean( GetCurrentParam( ref nodeParams ) );
  179. }
  180. public override void WriteToString( ref string nodeInfo, ref string connectionsInfo )
  181. {
  182. base.WriteToString( ref nodeInfo, ref connectionsInfo );
  183. IOUtils.AddFieldValueToString( ref nodeInfo, m_applyScaling );
  184. }
  185. }
  186. }