CTI URP Leaves GBuffer Pass.hlsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. float3 texcoord2 : TEXCOORD2; // Leaves only: Pivot
  12. //float2 staticLightmapUV : TEXCOORD1;
  13. //float2 dynamicLightmapUV : TEXCOORD2;
  14. UNITY_VERTEX_INPUT_INSTANCE_ID
  15. };
  16. struct Varyings
  17. {
  18. float2 uv : TEXCOORD0;
  19. #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  20. float3 positionWS : TEXCOORD1;
  21. #endif
  22. half3 normalWS : TEXCOORD2;
  23. #if defined(_NORMALMAP)
  24. half4 tangentWS : TEXCOORD3;
  25. #endif
  26. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  27. half3 vertexLighting : TEXCOORD4;
  28. #endif
  29. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  30. float4 shadowCoord : TEXCOORD5;
  31. #endif
  32. //#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  33. // half3 viewDirTS : TEXCOORD6;
  34. //#endif
  35. DECLARE_LIGHTMAP_OR_SH(staticLightmapUV, vertexSH, 7);
  36. //#ifdef DYNAMICLIGHTMAP_ON
  37. // float2 dynamicLightmapUV : TEXCOORD8;
  38. //#endif
  39. float4 positionCS : SV_POSITION;
  40. #if defined(SHADER_STAGE_FRAGMENT)
  41. FRONT_FACE_TYPE cullFace : FRONT_FACE_SEMANTIC;
  42. #endif
  43. // CTI specific
  44. half2 occlusionVariation : TEXCOORD8;
  45. //UNITY_VERTEX_INPUT_INSTANCE_ID
  46. UNITY_VERTEX_OUTPUT_STEREO
  47. };
  48. //--------------------------------------
  49. // Include the surface function
  50. #include "Includes/CTI URP Leaves SurfaceData.hlsl"
  51. //--------------------------------------
  52. // Vertex shader
  53. #include "Includes/CTI URP Bending.hlsl"
  54. Varyings LitGBufferPassVertex (Attributes input)
  55. {
  56. Varyings output = (Varyings)0;
  57. UNITY_SETUP_INSTANCE_ID(input);
  58. //UNITY_TRANSFER_INSTANCE_ID(input, output);
  59. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  60. CTI_AnimateVertex(
  61. input,
  62. #if defined (_BENDINGCOLRSONLY)
  63. float4(input.color.rg, input.color.ab), // animParams,
  64. #else
  65. float4(input.color.rg, input.texcoord1.xy), // animParams,
  66. #endif
  67. _BaseWindMultipliers
  68. );
  69. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  70. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  71. output.uv = input.texcoord; //TRANSFORM_TEX(input.texcoord, _BaseMap);
  72. // Already normalized from normal transform to WS.
  73. output.normalWS = normalInput.normalWS;
  74. #ifdef _NORMALMAP
  75. real sign = input.tangentOS.w * GetOddNegativeScale();
  76. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  77. #endif
  78. //#if defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR)
  79. // half3 viewDirWS = GetWorldSpaceNormalizeViewDir(vertexInput.positionWS);
  80. // half3 viewDirTS = GetViewDirectionTangentSpace(tangentWS, output.normalWS, viewDirWS);
  81. // output.viewDirTS = viewDirTS;
  82. //#endif
  83. //OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  84. //#ifdef DYNAMICLIGHTMAP_ON
  85. // output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  86. //#endif
  87. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  88. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  89. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  90. output.vertexLighting = vertexLight;
  91. #endif
  92. //#if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  93. output.positionWS = vertexInput.positionWS;
  94. //#endif
  95. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  96. output.shadowCoord = GetShadowCoord(vertexInput);
  97. #endif
  98. output.positionCS = vertexInput.positionCS;
  99. return output;
  100. }
  101. FragmentOutput LitGBufferPassFragment(Varyings input, FRONT_FACE_TYPE frontFace : FRONT_FACE_SEMANTIC)
  102. {
  103. //UNITY_SETUP_INSTANCE_ID(input);
  104. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  105. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  106. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  107. #endif
  108. SurfaceData surfaceData;
  109. AdditionalSurfaceData additionalSurfaceData;
  110. // Get the surface description
  111. InitializeLeavesLitSurfaceData(input.occlusionVariation.y, input.uv.xy, surfaceData, additionalSurfaceData);
  112. // Transfer all to world space
  113. InputData inputData = (InputData)0;
  114. inputData.positionWS = input.positionWS;
  115. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  116. bool cullFace = IS_FRONT_VFACE(frontFace, true, false);
  117. #ifdef _NORMALMAP
  118. //surfaceData.normalTS.z *= facing;
  119. #if !defined(_GBUFFERLIGHTING_SIMPLE) && !defined(_GBUFFERLIGHTING_VSNORMALS)
  120. surfaceData.normalTS.z *= cullFace ? 1 : -1;
  121. #endif
  122. float sgn = input.tangentWS.w;
  123. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  124. inputData.normalWS = TransformTangentToWorld(surfaceData.normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  125. #else
  126. inputData.normalWS = input.normalWS; // * facing;
  127. #if !defined(_GBUFFERLIGHTING_SIMPLE) && !defined(_GBUFFERLIGHTING_VSNORMALS)
  128. inputData.normalWS *= cullFace ? 1 : -1;
  129. #endif
  130. #endif
  131. #if defined (_GBUFFERLIGHTING_VSNORMALS)
  132. // From world to view space
  133. half3 normalVS = TransformWorldToViewDir(inputData.normalWS, true);
  134. // Now "flip" the normal
  135. normalVS.z = abs(normalVS.z);
  136. // From view to world space again
  137. inputData.normalWS = normalize( mul((float3x3)UNITY_MATRIX_I_V, normalVS) );
  138. #endif
  139. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  140. inputData.viewDirectionWS = viewDirWS;
  141. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  142. inputData.shadowCoord = input.shadowCoord;
  143. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  144. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  145. #else
  146. inputData.shadowCoord = float4(0, 0, 0, 0);
  147. #endif
  148. inputData.fogCoord = 0.0; // we don't apply fog in the guffer pass
  149. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  150. inputData.vertexLighting = input.vertexLighting.xyz;
  151. #else
  152. inputData.vertexLighting = half3(0, 0, 0);
  153. #endif
  154. #if defined(DYNAMICLIGHTMAP_ON)
  155. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  156. #else
  157. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  158. #endif
  159. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  160. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  161. // // Simple deferred fuzzy lighting
  162. // #if defined(_SIMPLEFUZZ)
  163. // // We just tweak the diffuse
  164. // half NdotV = saturate(dot(inputData.normalWS, inputData.viewDirectionWS ));
  165. // half fuzz = Fuzz(NdotV, _FuzzPower, _FuzzBias);
  166. // fuzz *= additionalSurfaceData.fuzzMask * _FuzzStrength * PI;
  167. // surfaceData.albedo *= 1.0h + fuzz; // * _FuzzAmbient;
  168. // #endif
  169. // TODO: Lightlayers(Done) Cookies(Done)
  170. #if defined(_GBUFFERLIGHTING_TRANSMISSION)
  171. uint meshRenderingLayers = GetMeshRenderingLightLayer();
  172. // Beta 5: must be commented as otherwise screen space shadows bug
  173. //inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  174. half4 shadowMask = CalculateShadowMask(inputData);
  175. AmbientOcclusionFactor aoFactor = CreateAmbientOcclusionFactor(inputData, surfaceData);
  176. Light mainLight1 = GetMainLight(inputData, shadowMask, aoFactor);
  177. #if defined(_LIGHT_LAYERS)
  178. UNITY_BRANCH if (IsMatchingLightLayer(mainLight1.layerMask, meshRenderingLayers))
  179. {
  180. #endif
  181. half transPower = _Translucency.y;
  182. half3 transLightDir = mainLight1.direction + inputData.normalWS * _Translucency.w;
  183. half transDot = dot( transLightDir, -inputData.viewDirectionWS );
  184. transDot = exp2(saturate(transDot) * transPower - transPower);
  185. #if defined(_SAMPLE_LIGHT_COOKIES)
  186. real3 cookieColor = SampleMainLightCookie(inputData.positionWS);
  187. mainLight1.color *= float4(cookieColor, 1);
  188. #endif
  189. surfaceData.emission +=
  190. transDot
  191. * (1.0h - saturate(dot(mainLight1.direction, inputData.normalWS)))
  192. * mainLight1.color * lerp(1, mainLight1.shadowAttenuation, _Translucency.z)
  193. * additionalSurfaceData.translucency * _Translucency.x
  194. * surfaceData.albedo;
  195. // Light unityLight;
  196. // unityLight = GetMainLight();
  197. // unityLight.distanceAttenuation = 1.0; //?
  198. // #if defined(_MAIN_LIGHT_SHADOWS_SCREEN) && Donedefined(_SURFACE_TYPE_TRANSPARENT)
  199. // float4 shadowCoord = float4(screen_uv, 0.0, 1.0);
  200. // #else
  201. // float4 shadowCoord = TransformWorldToShadowCoord(posWS.xyz);
  202. // #endif
  203. // unityLight.shadowAttenuation = MainLightShadow(shadowCoord, posWS.xyz, shadowMask, _MainLightOcclusionProbes);
  204. #if defined(_LIGHT_LAYERS)
  205. }
  206. #endif
  207. #endif
  208. #ifdef _DBUFFER
  209. #if defined(_RECEIVEDECALS)
  210. ApplyDecalToSurfaceData(input.positionCS, surfaceData, inputData);
  211. #endif
  212. #endif
  213. BRDFData brdfData;
  214. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  215. // Simple deferred fuzzy lighting
  216. // For some unknown reason we can't modify brdfData.diffuse here?!
  217. // Is it "frontFace"? that causes this issue?
  218. // So simple fuzz tweaks surfaceData.albedo, see above.
  219. //brdfData.diffuse = half3(1,0,0);
  220. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  221. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  222. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  223. return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
  224. }