InTerra_TerrainLitDepthNormalsPass.hlsl 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #ifndef UNIVERSAL_FORWARD_LIT_DEPTH_NORMALS_PASS_INCLUDED
  2. #define UNIVERSAL_FORWARD_LIT_DEPTH_NORMALS_PASS_INCLUDED
  3. #include "InTerra_TerrainLitPasses.hlsl"
  4. // DepthNormal pass
  5. struct AttributesDepthNormal
  6. {
  7. float4 positionOS : POSITION;
  8. half3 normalOS : NORMAL;
  9. float2 texcoord : TEXCOORD0;
  10. UNITY_VERTEX_INPUT_INSTANCE_ID
  11. };
  12. struct VaryingsDepthNormal
  13. {
  14. float4 uvMainAndLM : TEXCOORD0; // xy: control, zw: lightmap
  15. #ifndef TERRAIN_SPLAT_BASEPASS
  16. float4 uvSplat01 : TEXCOORD1; // xy: splat0, zw: splat1
  17. float4 uvSplat23 : TEXCOORD2; // xy: splat2, zw: splat3
  18. #endif
  19. #if (defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  20. half4 normal : TEXCOORD3; // xyz: normal, w: viewDir.x
  21. half4 tangent : TEXCOORD4; // xyz: tangent, w: viewDir.y
  22. half4 bitangent : TEXCOORD5; // xyz: bitangent, w: viewDir.z
  23. half3 positionWS : TEXCOORD6;
  24. #else
  25. half3 normal : TEXCOORD3;
  26. #endif
  27. float4 uvSplat45 : TEXCOORD7;
  28. float4 uvSplat67 : TEXCOORD8;
  29. float4 clipPos : SV_POSITION;
  30. UNITY_VERTEX_OUTPUT_STEREO
  31. };
  32. VaryingsDepthNormal DepthNormalOnlyVertex(AttributesDepthNormal v)
  33. {
  34. VaryingsDepthNormal o = (VaryingsDepthNormal)0;
  35. UNITY_SETUP_INSTANCE_ID(v);
  36. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  37. TerrainInstancing(v.positionOS, v.normalOS, v.texcoord);
  38. const VertexPositionInputs attributes = GetVertexPositionInputs(v.positionOS.xyz);
  39. o.uvMainAndLM.xy = v.texcoord;
  40. o.uvMainAndLM.zw = v.texcoord * unity_LightmapST.xy + unity_LightmapST.zw;
  41. #ifndef TERRAIN_SPLAT_BASEPASS
  42. o.uvSplat01.xy = TRANSFORM_TEX(v.texcoord, _Splat0);
  43. o.uvSplat01.zw = TRANSFORM_TEX(v.texcoord, _Splat1);
  44. #ifndef _LAYERS_TWO
  45. o.uvSplat23.xy = TRANSFORM_TEX(v.texcoord, _Splat2);
  46. o.uvSplat23.zw = TRANSFORM_TEX(v.texcoord, _Splat3);
  47. #ifdef _LAYERS_EIGHT
  48. o.uvSplat45.xy = TRANSFORM_TEX(v.texcoord, _Splat4);
  49. o.uvSplat45.zw = TRANSFORM_TEX(v.texcoord, _Splat5);
  50. o.uvSplat67.xy = TRANSFORM_TEX(v.texcoord, _Splat6);
  51. o.uvSplat67.zw = TRANSFORM_TEX(v.texcoord, _Splat7);
  52. #endif
  53. #endif
  54. #endif
  55. #if (defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  56. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(attributes.positionWS);
  57. float4 vertexTangent = float4(cross(float3(0, 0, 1), v.normalOS), 1.0);
  58. VertexNormalInputs normalInput = GetVertexNormalInputs(v.normalOS, vertexTangent);
  59. o.normal = half4(normalInput.normalWS, viewDirWS.x);
  60. o.tangent = half4(normalInput.tangentWS, viewDirWS.y);
  61. o.bitangent = half4(normalInput.bitangentWS, viewDirWS.z);
  62. o.positionWS = attributes.positionWS;
  63. #else
  64. o.normal = TransformObjectToWorldNormal(v.normalOS);
  65. #endif
  66. o.clipPos = attributes.positionCS;
  67. return o;
  68. }
  69. void SampleNorm(float2 uv[_LAYER_COUNT], half4 blendMask[2], inout float3 mixNormal, half4 mask[_LAYER_COUNT])
  70. {
  71. half3 normal[_LAYER_COUNT];
  72. #define norm(i, blendMask) \
  73. UNITY_BRANCH if (blendMask > 0) \
  74. { \
  75. normal[i] = SampleNormals(i).xyz; \
  76. mixNormal += normal[i].xyz * blendMask.x; \
  77. } \
  78. else \
  79. { \
  80. normal[i] = float3(0, 0, 1); \
  81. } \
  82. norm(0, blendMask[0].r);
  83. #ifndef _LAYERS_ONE
  84. norm(1, blendMask[0].g);
  85. #ifndef _LAYERS_TWO
  86. norm(2, blendMask[0].b);
  87. norm(3, blendMask[0].a);
  88. #ifdef _LAYERS_EIGHT
  89. norm(4, blendMask[1].r);
  90. norm(5, blendMask[1].g);
  91. norm(6, blendMask[1].b);
  92. norm(7, blendMask[1].a);
  93. #endif
  94. #endif
  95. #endif
  96. #undef norm
  97. }
  98. void NormalMapMix(float4 uvMainAndLM, float4 uvSplat01, float4 uvSplat23, float4 uvSplat45, float4 uvSplat67, float3 positionWS, float3 worldNormal, float3 tangentViewDirTerrain, inout half4 blendMask[2], inout half3 mixedNormal)
  99. {
  100. #if defined(_NORMALMAPS) && defined(_DEPTH_NORMALS_MAPS)
  101. half3 mixNormal = half(0.0);
  102. half3 normal[_LAYER_COUNT];
  103. float2 uvSplat[_LAYER_COUNT];
  104. float2 uv[_LAYER_COUNT];
  105. half4 mask[_LAYER_COUNT];
  106. uvSplat[0] = uvSplat01.xy;
  107. uvSplat[1] = uvSplat01.zw;
  108. #ifndef _LAYERS_TWO
  109. uvSplat[2] = uvSplat23.xy;
  110. uvSplat[3] = uvSplat23.zw;
  111. #ifdef _LAYERS_EIGHT
  112. uvSplat[4] = uvSplat45.xy;
  113. uvSplat[5] = uvSplat45.zw;
  114. uvSplat[6] = uvSplat67.xy;
  115. uvSplat[7] = uvSplat67.zw;
  116. #endif
  117. #endif
  118. UvSplat(uv, uvSplat, uvMainAndLM.xy);
  119. #if (defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL) && defined(PARALLAX)
  120. UNITY_BRANCH if (_Terrain_Parallax == 1)
  121. {
  122. float lod = _MipMapLevel + smoothstep(_MipMapFade.x, _MipMapFade.y, (distance(positionWS, _WorldSpaceCameraPos)));
  123. ParallaxUV(uv, tangentViewDirTerrain, blendMask, 1.0f, lod);
  124. }
  125. #endif
  126. SampleMask(mask, uv, blendMask, 1.0f);
  127. #if defined(_TERRAIN_BLEND_HEIGHT) && !defined(TERRAIN_SPLAT_ADDPASS)
  128. #ifdef _TERRAIN_BASEMAP_GEN
  129. if (_NumLayersCount <= 4)
  130. #endif
  131. {
  132. if (_HeightmapBlending == 1)
  133. {
  134. HeightBlend(mask, blendMask, _HeightTransition);
  135. }
  136. }
  137. #endif
  138. SampleNorm(uv, blendMask, mixNormal, mask);
  139. #if HAS_HALF
  140. mixNormal.z += half(0.01);
  141. #else
  142. mixNormal.z += 1e-5f;
  143. #endif
  144. mixedNormal = normalize(mixNormal.xyz);
  145. #endif
  146. }
  147. #if defined(UNITY_2022_2_OR_NEWER) || defined(UNITY_6000_1_OR_NEWER)
  148. void DepthNormalOnlyFragment(
  149. VaryingsDepthNormal IN
  150. , out half4 outNormalWS : SV_Target0
  151. #ifdef _WRITE_RENDERING_LAYERS
  152. , out float4 outRenderingLayers : SV_Target1
  153. #endif
  154. )
  155. #else
  156. half4 DepthNormalOnlyFragment(VaryingsDepthNormal IN) : SV_TARGET
  157. #endif
  158. {
  159. #ifdef _ALPHATEST_ON
  160. ClipHoles(IN.uvMainAndLM.xy);
  161. #endif
  162. float2 splatUV = (IN.uvMainAndLM.xy * (_Control_TexelSize.zw - 1.0f) + 0.5f) * _Control_TexelSize.xy;
  163. half4 blendMask[2];
  164. blendMask[0] = SAMPLE_TEXTURE2D(_Control, sampler_Control, splatUV);
  165. blendMask[1] = float4(0.0f, 0.0f, 0.0f, 0.0f);
  166. float3 tangentViewDir = float3(0.0h, 0.0h, 1.0h);
  167. float3 worldPos = float3(0.0h, 0.0h, 0.0h);;
  168. #if (defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL) && defined(PARALLAX)
  169. worldPos = IN.positionWS;
  170. float3x3 objectToTangent = float3x3((IN.tangent.xyz), (cross(IN.normal.xyz, IN.tangent.xyz)) * -1, IN.normal.xyz);
  171. tangentViewDir = -normalize(mul(objectToTangent, GetWorldSpaceViewDir(worldPos)));
  172. #endif
  173. half3 normalTS = half3(0.0h, 0.0h, 1.0h);
  174. #ifndef _LAYERS_EIGHT
  175. NormalMapMix(IN.uvMainAndLM, IN.uvSplat01, IN.uvSplat23, float4(0.0f, 0.0f, 0.0f, 0.0f), float4(0.0f, 0.0f, 0.0f, 0.0f), worldPos, IN.normal.xyz, tangentViewDir, blendMask, normalTS);
  176. #else
  177. float2 tc = _WorldMapping ? (IN.positionWS.xz / _TerrainSizeXZPosY.xy) : IN.uvMainAndLM.xy;
  178. IN.uvSplat45.xy = TRANSFORM_TEX(tc, _Splat4);
  179. IN.uvSplat45.zw = TRANSFORM_TEX(tc, _Splat5);
  180. IN.uvSplat67.xy = TRANSFORM_TEX(tc, _Splat6);
  181. IN.uvSplat67.zw = TRANSFORM_TEX(tc, _Splat7);
  182. blendMask[1] = SAMPLE_TEXTURE2D(_Control1, sampler_Control, splatUV);
  183. NormalMapMix(IN.uvMainAndLM, IN.uvSplat01, IN.uvSplat23, IN.uvSplat45, IN.uvSplat67, worldPos, IN.normal.xyz, tangentViewDir, blendMask, normalTS);
  184. #endif
  185. #if (defined(_NORMALMAPS) || defined(_TERRAIN_NORMAL_IN_MASK)) && !defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  186. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(-IN.tangent.xyz, IN.bitangent.xyz, IN.normal.xyz));
  187. #elif defined(ENABLE_TERRAIN_PERPIXEL_NORMAL)
  188. half3 viewDirWS = IN.viewDir;
  189. float2 sampleCoords = (IN.uvMainAndLM.xy / _TerrainHeightmapRecipSize.zw + 0.5f) * _TerrainHeightmapRecipSize.xy;
  190. half3 normalWS = TransformObjectToWorldNormal(normalize(SAMPLE_TEXTURE2D(_TerrainNormalmapTexture, sampler_TerrainNormalmapTexture, sampleCoords).rgb * 2 - 1));
  191. half3 tangentWS = cross(GetObjectToWorldMatrix()._13_23_33, normalWS);
  192. half3 normalWS = TransformTangentToWorld(normalTS, half3x3(-tangentWS, cross(normalWS, tangentWS), normalWS));
  193. #else
  194. half3 normalWS = IN.normal;
  195. #endif
  196. normalWS = NormalizeNormalPerPixel(normalWS);
  197. #if defined(UNITY_2022_2_OR_NEWER) || defined(UNITY_6000_1_OR_NEWER)
  198. outNormalWS = half4(normalWS, 0.0);
  199. #ifdef _WRITE_RENDERING_LAYERS
  200. uint renderingLayers = GetMeshRenderingLayer();
  201. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  202. #endif
  203. #else
  204. return half4(normalWS, 0.0);
  205. #endif
  206. }
  207. #endif