Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PBRGBufferPass.hlsl 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 = GetWorldSpaceNormalizeViewDir(input.positionWS);
  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. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  33. #elif !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  34. inputData.bakedGI = SAMPLE_GI(input.sh,
  35. GetAbsolutePositionWS(inputData.positionWS),
  36. inputData.normalWS,
  37. inputData.viewDirectionWS,
  38. inputData.positionCS.xy,
  39. input.probeOcclusion,
  40. inputData.shadowMask);
  41. #else
  42. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.sh, inputData.normalWS);
  43. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  44. #endif
  45. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  46. #if defined(DEBUG_DISPLAY)
  47. #if defined(DYNAMICLIGHTMAP_ON)
  48. inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
  49. #endif
  50. #if defined(LIGHTMAP_ON)
  51. inputData.staticLightmapUV = input.staticLightmapUV;
  52. #else
  53. inputData.vertexSH = input.sh;
  54. #endif
  55. #endif
  56. }
  57. PackedVaryings vert(Attributes input)
  58. {
  59. Varyings output = (Varyings)0;
  60. output = BuildVaryings(input);
  61. PackedVaryings packedOutput = (PackedVaryings)0;
  62. packedOutput = PackVaryings(output);
  63. return packedOutput;
  64. }
  65. FragmentOutput frag(PackedVaryings packedInput)
  66. {
  67. Varyings unpacked = UnpackVaryings(packedInput);
  68. UNITY_SETUP_INSTANCE_ID(unpacked);
  69. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
  70. SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);
  71. #if defined(_ALPHATEST_ON)
  72. half alpha = surfaceDescription.Alpha;
  73. clip(alpha - surfaceDescription.AlphaClipThreshold);
  74. #elif _SURFACE_TYPE_TRANSPARENT
  75. half alpha = surfaceDescription.Alpha;
  76. #else
  77. half alpha = 1;
  78. #endif
  79. #if defined(LOD_FADE_CROSSFADE) && USE_UNITY_CROSSFADE
  80. LODFadeCrossFade(unpacked.positionCS);
  81. #endif
  82. InputData inputData;
  83. InitializeInputData(unpacked, surfaceDescription, inputData);
  84. #ifdef VARYINGS_NEED_TEXCOORD0
  85. SETUP_DEBUG_TEXTURE_DATA(inputData, unpacked.texCoord0);
  86. #else
  87. SETUP_DEBUG_TEXTURE_DATA_NO_UV(inputData);
  88. #endif
  89. #ifdef _SPECULAR_SETUP
  90. float3 specular = surfaceDescription.Specular;
  91. float metallic = 1;
  92. #else
  93. float3 specular = 0;
  94. float metallic = surfaceDescription.Metallic;
  95. #endif
  96. #ifdef _DBUFFER
  97. ApplyDecal(unpacked.positionCS,
  98. surfaceDescription.BaseColor,
  99. specular,
  100. inputData.normalWS,
  101. metallic,
  102. surfaceDescription.Occlusion,
  103. surfaceDescription.Smoothness);
  104. #endif
  105. // in LitForwardPass GlobalIllumination (and temporarily LightingPhysicallyBased) are called inside UniversalFragmentPBR
  106. // in Deferred rendering we store the sum of these values (and of emission as well) in the GBuffer
  107. BRDFData brdfData;
  108. InitializeBRDFData(surfaceDescription.BaseColor, metallic, specular, surfaceDescription.Smoothness, alpha, brdfData);
  109. Light mainLight = GetMainLight(inputData.shadowCoord, inputData.positionWS, inputData.shadowMask);
  110. MixRealtimeAndBakedGI(mainLight, inputData.normalWS, inputData.bakedGI, inputData.shadowMask);
  111. half3 color = GlobalIllumination(brdfData, inputData.bakedGI, surfaceDescription.Occlusion, inputData.positionWS, inputData.normalWS, inputData.viewDirectionWS);
  112. return BRDFDataToGbuffer(brdfData, inputData, surfaceDescription.Smoothness, surfaceDescription.Emission + color, surfaceDescription.Occlusion);
  113. }