No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MetaPass.hlsl 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #ifndef UNITY_META_PASS_INCLUDED
  2. #define UNITY_META_PASS_INCLUDED
  3. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
  4. #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl"
  5. CBUFFER_START(UnityMetaPass)
  6. // x = use uv1 as raster position
  7. // y = use uv2 as raster position
  8. bool4 unity_MetaVertexControl;
  9. // x = return albedo
  10. // y = return normal
  11. bool4 unity_MetaFragmentControl;
  12. // Control which VisualizationMode we will
  13. // display in the editor
  14. int unity_VisualizationMode;
  15. CBUFFER_END
  16. struct UnityMetaInput
  17. {
  18. half3 Albedo;
  19. half3 Emission;
  20. #ifdef EDITOR_VISUALIZATION
  21. float2 VizUV;
  22. float4 LightCoord;
  23. #endif
  24. };
  25. #ifdef EDITOR_VISUALIZATION
  26. // Visualization defines
  27. // Should be kept in sync with the EditorVisualizationMode enum in EditorCameraDrawing.cpp
  28. // First two are unused.
  29. #define EDITORVIZ_PBR_VALIDATION_ALBEDO 0
  30. #define EDITORVIZ_PBR_VALIDATION_METALSPECULAR 1
  31. #define EDITORVIZ_TEXTURE 2
  32. #define EDITORVIZ_SHOWLIGHTMASK 3
  33. uniform sampler2D unity_EditorViz_Texture;
  34. uniform half4 unity_EditorViz_Texture_ST;
  35. uniform int unity_EditorViz_UVIndex;
  36. uniform half4 unity_EditorViz_Decode_HDR;
  37. uniform bool unity_EditorViz_ConvertToLinearSpace;
  38. uniform half4 unity_EditorViz_ColorMul;
  39. uniform half4 unity_EditorViz_ColorAdd;
  40. uniform half unity_EditorViz_Exposure;
  41. uniform sampler2D unity_EditorViz_LightTexture;
  42. uniform sampler2D unity_EditorViz_LightTextureB;
  43. #define unity_EditorViz_ChannelSelect unity_EditorViz_ColorMul
  44. #define unity_EditorViz_Color unity_EditorViz_ColorAdd
  45. #define unity_EditorViz_LightType unity_EditorViz_UVIndex
  46. uniform float4x4 unity_EditorViz_WorldToLight;
  47. #endif // EDITOR_VISUALIZATION
  48. float2 UnityMetaVizUV(int uvIndex, float2 uv0, float2 uv1, float2 uv2, float4 st)
  49. {
  50. if (uvIndex == 0)
  51. return uv0 * st.xy + st.zw;
  52. else if (uvIndex == 1)
  53. return uv1 * st.xy + st.zw;
  54. else
  55. return uv2 * st.xy + st.zw;
  56. }
  57. void UnityEditorVizData(float3 positionOS, float2 uv0, float2 uv1, float2 uv2, float4 st, out float2 VizUV, out float4 LightCoord)
  58. {
  59. #ifdef EDITOR_VISUALIZATION
  60. if (unity_VisualizationMode == EDITORVIZ_TEXTURE)
  61. VizUV = UnityMetaVizUV(unity_EditorViz_UVIndex, uv0, uv1, uv2, unity_EditorViz_Texture_ST);
  62. else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK)
  63. {
  64. VizUV = uv1 * st.xy + st.zw;
  65. LightCoord = mul(unity_EditorViz_WorldToLight, float4(TransformObjectToWorld(positionOS), 1));
  66. }
  67. #endif
  68. }
  69. void UnityEditorVizData(float3 positionOS, float2 uv0, float2 uv1, float2 uv2, out float2 VizUV, out float4 LightCoord)
  70. {
  71. float4 st = unity_LightmapST;
  72. if (unity_MetaVertexControl.y)
  73. st = unity_DynamicLightmapST;
  74. UnityEditorVizData(positionOS, uv0, uv1, uv2, st, VizUV, LightCoord);
  75. }
  76. float4 UnityMetaVertexPosition(float3 vertex, float2 uv1, float2 uv2, float4 lightmapST, float4 dynlightmapST)
  77. {
  78. #ifndef EDITOR_VISUALIZATION
  79. if (unity_MetaVertexControl.x)
  80. {
  81. vertex.xy = uv1 * lightmapST.xy + lightmapST.zw;
  82. // OpenGL right now needs to actually use incoming vertex position,
  83. // so use it in a very dummy way
  84. vertex.z = vertex.z > 0 ? REAL_MIN : 0.0f;
  85. }
  86. if (unity_MetaVertexControl.y)
  87. {
  88. vertex.xy = uv2 * dynlightmapST.xy + dynlightmapST.zw;
  89. // OpenGL right now needs to actually use incoming vertex position,
  90. // so use it in a very dummy way
  91. vertex.z = vertex.z > 0 ? REAL_MIN : 0.0f;
  92. }
  93. return TransformWorldToHClip(vertex);
  94. #else
  95. return TransformObjectToHClip(vertex);
  96. #endif
  97. }
  98. float4 UnityMetaVertexPosition(float3 vertex, float2 uv1, float2 uv2)
  99. {
  100. return UnityMetaVertexPosition(vertex, uv1, uv2, unity_LightmapST, unity_DynamicLightmapST);
  101. }
  102. float unity_OneOverOutputBoost;
  103. float unity_MaxOutputValue;
  104. float unity_UseLinearSpace;
  105. half4 UnityMetaFragment (UnityMetaInput IN)
  106. {
  107. half4 res = 0;
  108. #ifndef EDITOR_VISUALIZATION
  109. if (unity_MetaFragmentControl.x)
  110. {
  111. res = half4(IN.Albedo,1);
  112. // Apply Albedo Boost from LightmapSettings.
  113. res.rgb = clamp(pow(abs(res.rgb), saturate(unity_OneOverOutputBoost)), 0, unity_MaxOutputValue);
  114. }
  115. if (unity_MetaFragmentControl.y)
  116. {
  117. half3 emission;
  118. if (unity_UseLinearSpace)
  119. emission = IN.Emission;
  120. else
  121. emission = Gamma20ToLinear(IN.Emission);
  122. res = half4(emission, 1.0);
  123. }
  124. #else
  125. // SRPs don't support EDITORVIZ_PBR_VALIDATION_ALBEDO or EDITORVIZ_PBR_VALIDATION_METALSPECULAR
  126. if (unity_VisualizationMode == EDITORVIZ_TEXTURE)
  127. {
  128. res = tex2D(unity_EditorViz_Texture, IN.VizUV);
  129. if (unity_EditorViz_Decode_HDR.x > 0)
  130. res = half4(DecodeHDREnvironment(res, unity_EditorViz_Decode_HDR), 1);
  131. if (unity_EditorViz_ConvertToLinearSpace)
  132. res.rgb = LinearToGamma20(res.rgb);
  133. res *= unity_EditorViz_ColorMul;
  134. res += unity_EditorViz_ColorAdd;
  135. res *= exp2(unity_EditorViz_Exposure);
  136. }
  137. else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK)
  138. {
  139. float result = dot(unity_EditorViz_ChannelSelect, tex2D(unity_EditorViz_Texture, IN.VizUV).rgba);
  140. if (result == 0)
  141. discard;
  142. float atten = 1;
  143. if (unity_EditorViz_LightType == 0)
  144. {
  145. // directional: no attenuation
  146. }
  147. else if (unity_EditorViz_LightType == 1)
  148. {
  149. // point
  150. atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
  151. }
  152. else if (unity_EditorViz_LightType == 2)
  153. {
  154. // spot
  155. atten = tex2D(unity_EditorViz_LightTexture, dot(IN.LightCoord.xyz, IN.LightCoord.xyz).xx).r;
  156. float cookie = tex2D(unity_EditorViz_LightTextureB, IN.LightCoord.xy / max(IN.LightCoord.w, 0.0001) + 0.5).w;
  157. atten *= (IN.LightCoord.z > 0) * cookie;
  158. }
  159. clip(atten - 0.001f);
  160. res = float4(unity_EditorViz_Color.xyz * result, unity_EditorViz_Color.w);
  161. }
  162. #endif // EDITOR_VISUALIZATION
  163. return res;
  164. }
  165. #endif // UNITY_META_PASS_INCLUDED