PBRForwardPass.hlsl 4.7 KB

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