CTI URP Billboard GBuffer Pass.hlsl 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityGBuffer.hlsl"
  2. #if (defined(_NORMALMAP) || (defined(_PARALLAXMAP) && !defined(REQUIRES_TANGENT_SPACE_VIEW_DIR_INTERPOLATOR))) || defined(_DETAIL)
  3. #define REQUIRES_WORLD_SPACE_TANGENT_INTERPOLATOR
  4. #endif
  5. // Structs
  6. struct Attributes {
  7. float4 positionOS : POSITION;
  8. float3 normalOS : NORMAL;
  9. float4 tangentOS : TANGENT;
  10. float2 texcoord : TEXCOORD0;
  11. float3 texcoord1 : TEXCOORD1;
  12. UNITY_VERTEX_INPUT_INSTANCE_ID
  13. };
  14. struct Varyings {
  15. float4 positionCS : SV_POSITION;
  16. float2 uv : TEXCOORD0;
  17. //DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);
  18. half3 vertexSH : TEXCOORD1;
  19. //#ifdef _ADDITIONAL_LIGHTS
  20. float3 positionWS : TEXCOORD2;
  21. //#endif
  22. half3 normalWS : TEXCOORD3;
  23. #if defined(_NORMALMAP) || defined(DEPTHNORMALPASS)
  24. half4 tangentWS : TEXCOORD4;
  25. #endif
  26. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  27. half4 fogFactorAndVertexLight : TEXCOORD5;
  28. #else
  29. half fogFactor : TEXCOORD5;
  30. #endif
  31. // Due to the order of our includes we have to check for both defines
  32. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) || !defined(_MAIN_LIGHT_SHADOWS_CASCADE)
  33. float4 shadowCoord : TEXCOORD7;
  34. #endif
  35. half colorVariation : TEXCOORD8;
  36. //UNITY_VERTEX_INPUT_INSTANCE_ID
  37. UNITY_VERTEX_OUTPUT_STEREO
  38. };
  39. //--------------------------------------
  40. // Include the surface function
  41. #include "Includes/CTI URP Billboard SurfaceData.hlsl"
  42. //--------------------------------------
  43. // Vertex shader
  44. #include "Includes/CTI URP Billboard.hlsl"
  45. Varyings LitGBufferPassVertex (Attributes input)
  46. {
  47. Varyings output = (Varyings)0;
  48. UNITY_SETUP_INSTANCE_ID(input);
  49. //UNITY_TRANSFER_INSTANCE_ID(input, output);
  50. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  51. half4 color;
  52. CTIBillboardVert(input, 0, color);
  53. // Set color variation
  54. output.colorVariation = color.r;
  55. VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
  56. VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
  57. output.uv = input.texcoord; //TRANSFORM_TEX(input.texcoord, _BaseMap);
  58. // Already normalized from normal transform to WS.
  59. output.normalWS = normalInput.normalWS;
  60. #ifdef _NORMALMAP
  61. real sign = input.tangentOS.w * GetOddNegativeScale();
  62. output.tangentWS = half4(normalInput.tangentWS.xyz, sign);
  63. #endif
  64. //OUTPUT_LIGHTMAP_UV(input.staticLightmapUV, unity_LightmapST, output.staticLightmapUV);
  65. //#ifdef DYNAMICLIGHTMAP_ON
  66. // output.dynamicLightmapUV = input.dynamicLightmapUV.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;
  67. //#endif
  68. OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
  69. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  70. half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
  71. output.vertexLighting = vertexLight;
  72. #endif
  73. //#if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
  74. output.positionWS = vertexInput.positionWS;
  75. //#endif
  76. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  77. output.shadowCoord = GetShadowCoord(vertexInput);
  78. #endif
  79. output.positionCS = vertexInput.positionCS;
  80. return output;
  81. }
  82. FragmentOutput LitGBufferPassFragment(Varyings input)
  83. {
  84. //UNITY_SETUP_INSTANCE_ID(input);
  85. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
  86. #if defined(LOD_FADE_CROSSFADE) && !defined(SHADER_API_GLES)
  87. LODDitheringTransition(input.positionCS.xyz, unity_LODFade.x);
  88. #endif
  89. SurfaceData surfaceData;
  90. AdditionalSurfaceData additionalSurfaceData;
  91. // Get the surface description
  92. InitializeBillboardLitSurfaceData(input.colorVariation, input.uv.xy, surfaceData, additionalSurfaceData);
  93. // Transfer all to world space
  94. InputData inputData = (InputData)0;
  95. inputData.positionWS = input.positionWS;
  96. half3 viewDirWS = GetWorldSpaceNormalizeViewDir(input.positionWS);
  97. #ifdef _NORMALMAP
  98. float sgn = input.tangentWS.w;
  99. float3 bitangent = sgn * cross(input.normalWS.xyz, input.tangentWS.xyz);
  100. inputData.normalWS = TransformTangentToWorld(surfaceData.normalTS, half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz));
  101. #else
  102. inputData.normalWS = input.normalWS;
  103. #endif
  104. #if defined (_GBUFFERLIGHTING_VSNORMALS)
  105. // From world to view space
  106. half3 normalVS = TransformWorldToViewDir(inputData.normalWS, true);
  107. // Now "flip" the normal
  108. normalVS.z = abs(normalVS.z);
  109. // From view to world space again
  110. inputData.normalWS = normalize( mul((float3x3)UNITY_MATRIX_I_V, normalVS) );
  111. #endif
  112. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  113. inputData.viewDirectionWS = viewDirWS;
  114. #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
  115. inputData.shadowCoord = input.shadowCoord;
  116. #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  117. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  118. #else
  119. inputData.shadowCoord = float4(0, 0, 0, 0);
  120. #endif
  121. inputData.fogCoord = 0.0; // we don't apply fog in the guffer pass
  122. #ifdef _ADDITIONAL_LIGHTS_VERTEX
  123. inputData.vertexLighting = input.vertexLighting.xyz;
  124. #else
  125. inputData.vertexLighting = half3(0, 0, 0);
  126. #endif
  127. #if defined(DYNAMICLIGHTMAP_ON)
  128. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV, input.vertexSH, inputData.normalWS);
  129. #else
  130. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.vertexSH, inputData.normalWS);
  131. #endif
  132. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  133. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  134. // TODO: Lightlayers(Done) Cookies(Done)
  135. #if defined(_GBUFFERLIGHTING_TRANSMISSION)
  136. uint meshRenderingLayers = GetMeshRenderingLightLayer();
  137. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  138. half4 shadowMask = CalculateShadowMask(inputData);
  139. AmbientOcclusionFactor aoFactor = CreateAmbientOcclusionFactor(inputData, surfaceData);
  140. Light mainLight1 = GetMainLight(inputData, shadowMask, aoFactor);
  141. #if defined(_LIGHT_LAYERS)
  142. UNITY_BRANCH if (IsMatchingLightLayer(mainLight1.layerMask, meshRenderingLayers))
  143. {
  144. #endif
  145. half transPower = _Translucency.y;
  146. half3 transLightDir = mainLight1.direction + inputData.normalWS * _Translucency.w;
  147. half transDot = dot( transLightDir, -inputData.viewDirectionWS );
  148. transDot = exp2(saturate(transDot) * transPower - transPower);
  149. #if defined(_SAMPLE_LIGHT_COOKIES)
  150. real3 cookieColor = SampleMainLightCookie(inputData.positionWS);
  151. mainLight1.color *= float4(cookieColor, 1);
  152. #endif
  153. surfaceData.emission +=
  154. transDot
  155. * (1.0h - saturate(dot(mainLight1.direction, -inputData.normalWS)))
  156. * mainLight1.color * lerp(1, mainLight1.shadowAttenuation, _Translucency.z)
  157. * additionalSurfaceData.translucency * _Translucency.x
  158. * surfaceData.albedo;
  159. // Light unityLight;
  160. // unityLight = GetMainLight();
  161. // unityLight.distanceAttenuation = 1.0; //?
  162. // #if defined(_MAIN_LIGHT_SHADOWS_SCREEN) && Donedefined(_SURFACE_TYPE_TRANSPARENT)
  163. // float4 shadowCoord = float4(screen_uv, 0.0, 1.0);
  164. // #else
  165. // float4 shadowCoord = TransformWorldToShadowCoord(posWS.xyz);
  166. // #endif
  167. // unityLight.shadowAttenuation = MainLightShadow(shadowCoord, posWS.xyz, shadowMask, _MainLightOcclusionProbes);
  168. #if defined(_LIGHT_LAYERS)
  169. }
  170. #endif
  171. #endif
  172. BRDFData brdfData;
  173. InitializeBRDFData(surfaceData.albedo, surfaceData.metallic, surfaceData.specular, surfaceData.smoothness, surfaceData.alpha, brdfData);
  174. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  175. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  176. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceData.occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  177. return BRDFDataToGbuffer(brdfData, inputData, surfaceData.smoothness, surfaceData.emission + color, surfaceData.occlusion);
  178. }