CTI URP Bark GBuffer Pass.hlsl 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  2. // Structs
  3. struct Attributes
  4. {
  5. float4 positionOS : POSITION;
  6. float3 normalOS : NORMAL;
  7. float4 tangentOS : TANGENT;
  8. float2 texcoord : TEXCOORD0;
  9. float2 texcoord1 : TEXCOORD1; // Bending
  10. half4 color : COLOR; // Bending
  11. //float2 staticLightmapUV : TEXCOORD1;
  12. //float2 dynamicLightmapUV : TEXCOORD2;
  13. UNITY_VERTEX_INPUT_INSTANCE_ID
  14. };
  15. struct Varyings
  16. {
  17. float2 uv : TEXCOORD0;
  18. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  19. float3 positionWS : TEXCOORD1;
  20. #endif
  21. half3 normalWS : TEXCOORD2;
  22. #if defined(_NORMALMAP)
  23. half4 tangentWS : TEXCOORD3;
  24. #endif
  25. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  26. half3 vertexLighting : TEXCOORD4;
  27. #endif
  28. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  29. float4 shadowCoord : TEXCOORD5;
  30. #endif
  31. //#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  32. // half3 viewDirTS : TEXCOORD6;
  33. //#endif
  34. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 6);
  35. //#ifdef DYNAMICLIGHTMAP_ON
  36. // float2 dynamicLightmapUV : TEXCOORD8;
  37. //#endif
  38. float4 positionCS : SV_POSITION;
  39. // #if defined(SHADER_STAGE_FRAGMENT)
  40. // FRONT_FACE_TYPE cullFace : FRONT_FACE_SEMANTIC;
  41. // #endif
  42. // CTI specific
  43. #if defined(CTIBARKARRAY)
  44. half3 occlusionVariation : TEXCOORD7;
  45. #else
  46. half2 occlusionVariation : TEXCOORD7;
  47. #endif
  48. //UNITY_VERTEX_INPUT_INSTANCE_ID
  49. UNITY_VERTEX_OUTPUT_STEREO
  50. };
  51. //--------------------------------------
  52. // Include the surface function
  53. #include "Includes/CTI URP Bark SurfaceData.hlsl"
  54. //--------------------------------------
  55. // Vertex shader
  56. #include "Includes/CTI URP Bending.hlsl"
  57. Varyings LitGBufferPassVertex (Attributes input)
  58. {
  59. Varyings output = (Varyings)0;
  60. UNITY_SETUP_INSTANCE_ID(input);
  61. //UNITY_TRANSFER_INSTANCE_ID(input, output);
  62. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  63. CTI_AnimateVertex(
  64. input,
  65. #if defined (_BENDINGCOLRSONLY)
  66. float4(input.color.rg, input.color.ab), // animParams,
  67. #else
  68. float4(input.color.rg, input.texcoord1.xy), // animParams,
  69. #endif
  70. _BaseWindMultipliers
  71. );
  72. // CTI special
  73. #if defined(CTIBARKARRAY)
  74. output.occlusionVariation = input.color.arb;
  75. #else
  76. output.occlusionVariation = input.color.ar;
  77. #endif
  78. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  79. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  80. output.uv = input.texcoord; //TRANSFORM_TEX(input.texcoord, _BaseMap);
  81. // Already normalized from normal transform to WS.
  82. output.normalWS = normalInput.normalWS;
  83. #ifdef _NORMALMAP
  84. real sign = input.tangentOS.w * GetOddNegativeScale();
  85. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  86. #endif
  87. //#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  88. // half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  89. // half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
  90. // output.viewDirTS = viewDirTS;
  91. //#endif
  92. //OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  93. //#ifdef DYNAMICLIGHTMAP_ON
  94. // output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  95. //#endif
  96. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  97. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  98. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  99. output.vertexLighting = vertexLight;
  100. #endif
  101. //#if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  102. output.positionWS = vertexInput.positionWS;
  103. //#endif
  104. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  105. output.shadowCoord = GetShadowCoord(vertexInput);
  106. #endif
  107. output.positionCS = vertexInput.positionCS;
  108. return output;
  109. }
  110. FragmentOutput LitGBufferPassFragment(Varyings input)
  111. {
  112. //UNITY_SETUP_INSTANCE_ID(input);
  113. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  114. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  115. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  116. #endif
  117. SurfaceData surfaceData;
  118. // Get the surface description
  119. #if defined(CTIBARKARRAY)
  120. InitializeStandardLitSurfaceData(input.occlusionVariation.yz, input.uv.xy, surfaceData);
  121. #else
  122. InitializeStandardLitSurfaceData(input.occlusionVariation.y, input.uv.xy, surfaceData);
  123. #endif
  124. // Transfer all to world space
  125. InputData inputData = (InputData)0;
  126. inputData.positionWS = input.positionWS;
  127. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  128. #ifdef _NORMALMAP
  129. float sgn = input.tangentWS.w;
  130. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  131. inputData.normalWS = TransformTangentToWorld(surfaceData.normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  132. #else
  133. inputData.normalWS = input.normalWS;
  134. #endif
  135. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  136. inputData.viewDirectionWS = viewDirWS;
  137. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  138. inputData.shadowCoord = input.shadowCoord;
  139. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  140. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  141. #else
  142. inputData.shadowCoord = float4(0, 0, 0, 0);
  143. #endif
  144. inputData.fogCoord = 0.0; // we don't apply fog in the guffer pass
  145. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  146. inputData.vertexLighting = input.vertexLighting.xyz;
  147. #else
  148. inputData.vertexLighting = half3(0, 0, 0);
  149. #endif
  150. #if defined(DYNAMICLIGHTMAP_ON)
  151. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  152. #else
  153. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  154. #endif
  155. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  156. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  157. #ifdef _DBUFFER
  158. #if defined(_RECEIVEDECALS)
  159. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  160. #endif
  161. #endif
  162. BRDFData brdfData;
  163. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  164. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  165. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  166. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  167. return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
  168. }