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

LODDitheringTransition.hlsl 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Shadergraph-friendly implementation of LODDitheringTransition.
  2. // The function as defined in Common.hlsl terminates on clip(f).
  3. // However, since it does not return or output anything, shadergraph
  4. // doesn't recognize it as code that gets used. This file can be removed
  5. // and replaced with a string custom function if Shader Graph ever adds
  6. // support for flagging custom function nodes as used, even if not
  7. // connected to anything.
  8. #ifndef SHADERGRAPH_CROSSFADE_INCLUDED
  9. #define SHADERGRAPH_CROSSFADE_INCLUDED
  10. #ifndef UNITY_MATERIAL_INCLUDED
  11. uint2 ComputeFadeMaskSeed(float3 V, uint2 positionSS)
  12. {
  13. uint2 fadeMaskSeed;
  14. // Is this a reasonable quality gate?
  15. #if defined(SHADER_QUALITY_HIGH)
  16. if (IsPerspectiveProjection())
  17. {
  18. // Start with the world-space direction V. It is independent from the orientation of the camera,
  19. // and only depends on the position of the camera and the position of the fragment.
  20. // Now, project and transform it into [-1, 1].
  21. float2 pv = PackNormalOctQuadEncode(V);
  22. // Rescale it to account for the resolution of the screen.
  23. pv *= _ScreenParams.xy;
  24. // The camera only sees a small portion of the sphere, limited by hFoV and vFoV.
  25. // Therefore, we must rescale again (before quantization), roughly, by 1/tan(FoV/2).
  26. pv *= UNITY_MATRIX_P._m00_m11;
  27. // Truncate and quantize.
  28. fadeMaskSeed = asuint((int2)pv);
  29. }
  30. else
  31. #endif
  32. {
  33. // Can't use the view direction, it is the same across the entire screen.
  34. fadeMaskSeed = positionSS;
  35. }
  36. return fadeMaskSeed;
  37. }
  38. #endif
  39. void LODDitheringTransitionSG_float(float3 viewDirWS, float4 screenPos, out float multiplyAlpha)
  40. {
  41. #if !defined(SHADER_STAGE_RAY_TRACING)
  42. float p = GenerateHashedRandomFloat(ComputeFadeMaskSeed(viewDirWS, screenPos.xy));
  43. float f = unity_LODFade.x - CopySign(p, unity_LODFade.x);
  44. multiplyAlpha = f < 0 ? 0.0f : 1.0f;
  45. #endif
  46. }
  47. void LODDitheringTransitionSG_half(float3 viewDirWS, float4 screenPos, out half halfAlpha)
  48. {
  49. #if !defined(SHADER_STAGE_RAY_TRACING)
  50. float p = GenerateHashedRandomFloat(ComputeFadeMaskSeed(viewDirWS, screenPos.xy));
  51. float f = unity_LODFade.x - CopySign(p, unity_LODFade.x);
  52. float multiplyAlpha = f < 0 ? 0.0f : 1.0f;
  53. halfAlpha = (half)multiplyAlpha;
  54. #endif
  55. }
  56. #endif