PBRForwardPass.hlsl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. void BuildInputData(Varyings input, float3 normal, out InputData inputData)
  2. {
  3. inputData.positionWS = input.positionWS;
  4. #ifdef _NORMALMAP
  5. #if _NORMAL_DROPOFF_TS
  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.normalWS = TransformTangentToWorld(normal, half3x3(input.tangentWS.xyz, bitangent, input.normalWS.xyz));
  10. #elif _NORMAL_DROPOFF_OS
  11. inputData.normalWS = TransformObjectToWorldNormal(normal);
  12. #elif _NORMAL_DROPOFF_WS
  13. inputData.normalWS = normal;
  14. #endif
  15. #else
  16. inputData.normalWS = input.normalWS;
  17. #endif
  18. inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
  19. inputData.viewDirectionWS = SafeNormalize(input.viewDirectionWS);
  20. #if defined(MAIN_LIGHT_CALCULATE_SHADOWS)
  21. inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
  22. #else
  23. inputData.shadowCoord = float4(0, 0, 0, 0);
  24. #endif
  25. inputData.fogCoord = input.fogFactorAndVertexLight.x;
  26. inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
  27. inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.sh, inputData.normalWS);
  28. }
  29. PackedVaryings vert(Attributes input)
  30. {
  31. Varyings output = (Varyings)0;
  32. output = BuildVaryings(input);
  33. PackedVaryings packedOutput = (PackedVaryings)0;
  34. packedOutput = PackVaryings(output);
  35. return packedOutput;
  36. }
  37. half4 frag(PackedVaryings packedInput) : SV_TARGET
  38. {
  39. Varyings unpacked = UnpackVaryings(packedInput);
  40. UNITY_SETUP_INSTANCE_ID(unpacked);
  41. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
  42. SurfaceDescriptionInputs surfaceDescriptionInputs = BuildSurfaceDescriptionInputs(unpacked);
  43. SurfaceDescription surfaceDescription = SurfaceDescriptionFunction(surfaceDescriptionInputs);
  44. #if _AlphaClip
  45. clip(surfaceDescription.Alpha - surfaceDescription.AlphaClipThreshold);
  46. #endif
  47. InputData inputData;
  48. BuildInputData(unpacked, surfaceDescription.Normal, inputData);
  49. #ifdef _SPECULAR_SETUP
  50. float3 specular = surfaceDescription.Specular;
  51. float metallic = 1;
  52. #else
  53. float3 specular = 0;
  54. float metallic = surfaceDescription.Metallic;
  55. #endif
  56. half4 color = UniversalFragmentPBR(
  57. inputData,
  58. surfaceDescription.Albedo,
  59. metallic,
  60. specular,
  61. surfaceDescription.Smoothness,
  62. surfaceDescription.Occlusion,
  63. surfaceDescription.Emission,
  64. surfaceDescription.Alpha);
  65. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  66. return color;
  67. }