PBRGBufferPass.hlsl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. void InitializeInputData(Varyings input, SurfaceDescription surfaceDescription, out InputData inputData)
  2. {
  3. inputData = (InputData)0;
  4. inputData.positionWS = input.positionWS;
  5. inputData.positionCS = input.positionCS;
  6. #ifdef _NORMALMAP
  7. // IMPORTANT! If we ever support Flip on double sided materials ensure bitangent and tangent are NOT flipped.
  8. float crossSign = (input.tangentWS.w > 0.0 ? 1.0 : -1.0) * GetOddNegativeScale();
  9. float3 bitangent = crossSign * cross(input.normalWS.xyz, input.tangentWS.xyz);
  10. inputData.tangentToWorld = half3x3(input.tangentWS.xyz, bitangent.xyz, input.normalWS.xyz);
  11. #if _NORMAL_DROPOFF_TS
  12. inputData.normalWS = TransformTangentToWorld(surfaceDescription.NormalTS, inputData.tangentToWorld);
  13. #elif _NORMAL_DROPOFF_OS
  14. inputData.normalWS = TransformObjectToWorldNormal(surfaceDescription.NormalOS);
  15. #elif _NORMAL_DROPOFF_WS
  16. inputData.normalWS = surfaceDescription.NormalWS;
  17. #endif
  18. #else
  19. inputData.normalWS = input.normalWS;
  20. #endif
  21. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  22. inputData.viewDirectionWS = SafeNormalize(input.viewDirectionWS);
  23. #if defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  24. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  25. #else
  26. inputData.shadowCoord = float4(0, 0, 0, 0);
  27. #endif
  28. inputData.fogCoord = InitializeInputDataFog(float4(input.positionWS, 1.0), input.fogFactorAndVertexLight.x);
  29. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  30. #if defined(DYNAMICLIGHTMAP_ON)
  31. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.dynamicLightmapUV.xy, input.sh, inputData.normalWS);
  32. #else
  33. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.sh, inputData.normalWS);
  34. #endif
  35. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  36. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  37. #if defined(DEBUG_DISPLAY)
  38. #if defined(DYNAMICLIGHTMAP_ON)
  39. inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
  40. #endif
  41. #if defined(LIGHTMAP_ON)
  42. inputData.staticLightmapUV = input.staticLightmapUV;
  43. #else
  44. inputData.vertexSH = input.sh;
  45. #endif
  46. #endif
  47. }
  48. PackedVaryings vert(Attributes input)
  49. {
  50. Varyings output = (Varyings)0;
  51. output = BuildVaryings(input);
  52. PackedVaryings packedOutput = (PackedVaryings)0;
  53. packedOutput = PackVaryings(output);
  54. return packedOutput;
  55. }
  56. FragmentOutput frag(PackedVaryings packedInput)
  57. {
  58. Varyings unpacked = UnpackVaryings(packedInput);
  59. UNITY_SETUP_INSTANCE_ID(unpacked);
  60. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
  61. SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);
  62. #if _ALPHATEST_ON
  63. half alpha = surfaceDescription.Alpha;
  64. clip(alpha - surfaceDescription.AlphaClipThreshold);
  65. #elif _SURFACE_TYPE_TRANSPARENT
  66. half alpha = surfaceDescription.Alpha;
  67. #else
  68. half alpha = 1;
  69. #endif
  70. InputData inputData;
  71. InitializeInputData(unpacked, surfaceDescription, inputData);
  72. // TODO: Mip debug modes would require this, open question how to do this on ShaderGraph.
  73. //SETUP_DEBUG_TEXTURE_DATA(inputData, unpacked.uv, _MainTex);
  74. #ifdef _SPECULAR_SETUP
  75. float3 specular = surfaceDescription.Specular;
  76. float metallic = 1;
  77. #else
  78. float3 specular = 0;
  79. float metallic = surfaceDescription.Metallic;
  80. #endif
  81. #ifdef _DBUFFER
  82. ApplyDecal(unpacked.positionCS,
  83. surfaceDescription.BaseColor,
  84. specular,
  85. inputData.normalWS,
  86. metallic,
  87. surfaceDescription.Occlusion,
  88. surfaceDescription.Smoothness);
  89. #endif
  90. // in LitForwardPass GlobalIllumination (and temporarily LightingPhysicallyBased) are called inside UniversalFragmentPBR
  91. // in Deferred rendering we store the sum of these values (and of emission as well) in the GBuffer
  92. BRDFData brdfData;
  93. InitializeBRDFData(surfaceDescription.BaseColor, metallic, specular, surfaceDescription.Smoothness, alpha, brdfData);
  94. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  95. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  96. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceDescription.Occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  97. return BRDFDataToGbuffer(brdfData, inputData, surfaceDescription.Smoothness, surfaceDescription.Emission + color, surfaceDescription.Occlusion);
  98. }