暫無描述
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.

PBRForwardPass.hlsl 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 = GetWorldSpaceNormalizeViewDir(input.positionWS);
  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. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  34. #elif !defined(LIGHTMAP_ON) && (defined(PROBE_VOLUMES_L1) || defined(PROBE_VOLUMES_L2))
  35. inputData.bakedGI = SAMPLE_GI(input.sh,
  36. GetAbsolutePositionWS(inputData.positionWS),
  37. inputData.normalWS,
  38. inputData.viewDirectionWS,
  39. input.positionCS.xy,
  40. input.probeOcclusion,
  41. inputData.shadowMask);
  42. #else
  43. inputData.bakedGI = SAMPLE_GI(input.staticLightmapUV, input.sh, inputData.normalWS);
  44. inputData.shadowMask = SAMPLE_SHADOWMASK(input.staticLightmapUV);
  45. #endif
  46. inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
  47. #if defined(DEBUG_DISPLAY)
  48. #if defined(DYNAMICLIGHTMAP_ON)
  49. inputData.dynamicLightmapUV = input.dynamicLightmapUV.xy;
  50. #endif
  51. #if defined(LIGHTMAP_ON)
  52. inputData.staticLightmapUV = input.staticLightmapUV;
  53. #else
  54. inputData.vertexSH = input.sh;
  55. #endif
  56. inputData.positionCS = input.positionCS;
  57. #endif
  58. }
  59. PackedVaryings vert(Attributes input)
  60. {
  61. Varyings output = (Varyings)0;
  62. output = BuildVaryings(input);
  63. PackedVaryings packedOutput = (PackedVaryings)0;
  64. packedOutput = PackVaryings(output);
  65. return packedOutput;
  66. }
  67. void frag(
  68. PackedVaryings packedInput
  69. , out half4 outColor : SV_Target0
  70. #ifdef _WRITE_RENDERING_LAYERS
  71. , out float4 outRenderingLayers : SV_Target1
  72. #endif
  73. )
  74. {
  75. Varyings unpacked = UnpackVaryings(packedInput);
  76. UNITY_SETUP_INSTANCE_ID(unpacked);
  77. UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked);
  78. SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);
  79. #if defined(_SURFACE_TYPE_TRANSPARENT)
  80. bool isTransparent = true;
  81. #else
  82. bool isTransparent = false;
  83. #endif
  84. #if defined(_ALPHATEST_ON)
  85. half alpha = AlphaDiscard(surfaceDescription.Alpha, surfaceDescription.AlphaClipThreshold);
  86. #elif defined(_SURFACE_TYPE_TRANSPARENT)
  87. half alpha = surfaceDescription.Alpha;
  88. #else
  89. half alpha = half(1.0);
  90. #endif
  91. #if defined(LOD_FADE_CROSSFADE) && USE_UNITY_CROSSFADE
  92. LODFadeCrossFade(unpacked.positionCS);
  93. #endif
  94. InputData inputData;
  95. InitializeInputData(unpacked, surfaceDescription, inputData);
  96. #ifdef VARYINGS_NEED_TEXCOORD0
  97. SETUP_DEBUG_TEXTURE_DATA(inputData, unpacked.texCoord0);
  98. #else
  99. SETUP_DEBUG_TEXTURE_DATA_NO_UV(inputData);
  100. #endif
  101. #ifdef _SPECULAR_SETUP
  102. float3 specular = surfaceDescription.Specular;
  103. float metallic = 1;
  104. #else
  105. float3 specular = 0;
  106. float metallic = surfaceDescription.Metallic;
  107. #endif
  108. half3 normalTS = half3(0, 0, 0);
  109. #if defined(_NORMALMAP) && defined(_NORMAL_DROPOFF_TS)
  110. normalTS = surfaceDescription.NormalTS;
  111. #endif
  112. SurfaceData surface;
  113. surface.albedo = surfaceDescription.BaseColor;
  114. surface.metallic = saturate(metallic);
  115. surface.specular = specular;
  116. surface.smoothness = saturate(surfaceDescription.Smoothness),
  117. surface.occlusion = surfaceDescription.Occlusion,
  118. surface.emission = surfaceDescription.Emission,
  119. surface.alpha = saturate(alpha);
  120. surface.normalTS = normalTS;
  121. surface.clearCoatMask = 0;
  122. surface.clearCoatSmoothness = 1;
  123. #ifdef _CLEARCOAT
  124. surface.clearCoatMask = saturate(surfaceDescription.CoatMask);
  125. surface.clearCoatSmoothness = saturate(surfaceDescription.CoatSmoothness);
  126. #endif
  127. surface.albedo = AlphaModulate(surface.albedo, surface.alpha);
  128. #ifdef _DBUFFER
  129. ApplyDecalToSurfaceData(unpacked.positionCS, surface, inputData);
  130. #endif
  131. half4 color = UniversalFragmentPBR(inputData, surface);
  132. color.rgb = MixFog(color.rgb, inputData.fogCoord);
  133. color.a = OutputAlpha(color.a, isTransparent);
  134. outColor = color;
  135. #ifdef _WRITE_RENDERING_LAYERS
  136. uint renderingLayers = GetMeshRenderingLayer();
  137. outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0);
  138. #endif
  139. }