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.

138 lines
4.1 KiB

5 years ago
  1. // Copyright (c) <2015> <Playdead>
  2. // This file is subject to the MIT License as seen in the root of this folder structure (LICENSE.TXT)
  3. // AUTHOR: Lasse Jon Fuglsang Pedersen <lasse@playdead.com>
  4. #ifndef __DEPTH_CGINC__
  5. #define __DEPTH_CGINC__
  6. #include "UnityCG.cginc"
  7. uniform sampler2D_float _CameraDepthTexture;
  8. uniform float4 _CameraDepthTexture_TexelSize;
  9. #if UNITY_REVERSED_Z
  10. #define ZCMP_GT(a, b) (a < b)
  11. #else
  12. #define ZCMP_GT(a, b) (a > b)
  13. #endif
  14. float depth_resolve_linear(float z)
  15. {
  16. #if CAMERA_ORTHOGRAPHIC
  17. #if UNITY_REVERSED_Z
  18. return (1.0 - z) * (_ProjectionParams.z - _ProjectionParams.y) + _ProjectionParams.y;
  19. #else
  20. return z * (_ProjectionParams.z - _ProjectionParams.y) + _ProjectionParams.y;
  21. #endif
  22. #else
  23. return LinearEyeDepth(z);
  24. #endif
  25. }
  26. float depth_sample_linear(float2 uv)
  27. {
  28. return depth_resolve_linear(tex2D(_CameraDepthTexture, uv).x);
  29. }
  30. float3 find_closest_fragment_3x3(float2 uv)
  31. {
  32. float2 dd = abs(_CameraDepthTexture_TexelSize.xy);
  33. float2 du = float2(dd.x, 0.0);
  34. float2 dv = float2(0.0, dd.y);
  35. float3 dtl = float3(-1, -1, tex2D(_CameraDepthTexture, uv - dv - du).x);
  36. float3 dtc = float3( 0, -1, tex2D(_CameraDepthTexture, uv - dv).x);
  37. float3 dtr = float3( 1, -1, tex2D(_CameraDepthTexture, uv - dv + du).x);
  38. float3 dml = float3(-1, 0, tex2D(_CameraDepthTexture, uv - du).x);
  39. float3 dmc = float3( 0, 0, tex2D(_CameraDepthTexture, uv).x);
  40. float3 dmr = float3( 1, 0, tex2D(_CameraDepthTexture, uv + du).x);
  41. float3 dbl = float3(-1, 1, tex2D(_CameraDepthTexture, uv + dv - du).x);
  42. float3 dbc = float3( 0, 1, tex2D(_CameraDepthTexture, uv + dv).x);
  43. float3 dbr = float3( 1, 1, tex2D(_CameraDepthTexture, uv + dv + du).x);
  44. float3 dmin = dtl;
  45. if (ZCMP_GT(dmin.z, dtc.z)) dmin = dtc;
  46. if (ZCMP_GT(dmin.z, dtr.z)) dmin = dtr;
  47. if (ZCMP_GT(dmin.z, dml.z)) dmin = dml;
  48. if (ZCMP_GT(dmin.z, dmc.z)) dmin = dmc;
  49. if (ZCMP_GT(dmin.z, dmr.z)) dmin = dmr;
  50. if (ZCMP_GT(dmin.z, dbl.z)) dmin = dbl;
  51. if (ZCMP_GT(dmin.z, dbc.z)) dmin = dbc;
  52. if (ZCMP_GT(dmin.z, dbr.z)) dmin = dbr;
  53. return float3(uv + dd.xy * dmin.xy, dmin.z);
  54. }
  55. /* UNUSED: tested slower than branching
  56. float2 find_closest_fragment_3x3_packed(in float2 uv)
  57. {
  58. float2 dd = abs(_CameraDepthTexture_TexelSize.xy);
  59. float2 du = float2(dd.x, 0.0);
  60. float2 dv = float2(0.0, dd.y);
  61. const float s = 100000.0;
  62. float dtl = trunc(s * tex2D(_CameraDepthTexture, uv - dv - du).x) + 0.1010;// -+-+
  63. float dtc = trunc(s * tex2D(_CameraDepthTexture, uv - dv).x) + 0.0010;
  64. float dtr = trunc(s * tex2D(_CameraDepthTexture, uv - dv + du).x) + 0.0110;
  65. float dml = trunc(s * tex2D(_CameraDepthTexture, uv - du).x) + 0.1000;
  66. float dmc = trunc(s * tex2D(_CameraDepthTexture, uv).x) + 0.0000;
  67. float dmr = trunc(s * tex2D(_CameraDepthTexture, uv + du).x) + 0.0100;
  68. float dbl = trunc(s * tex2D(_CameraDepthTexture, uv + dv - du).x) + 0.1001;
  69. float dbc = trunc(s * tex2D(_CameraDepthTexture, uv + dv).x) + 0.0001;
  70. float dbr = trunc(s * tex2D(_CameraDepthTexture, uv + dv + du).x) + 0.0101;
  71. float enc = frac(min(dtl, min(dtc, min(dtr, min(dml, min(dmc, min(dmr, min(dbl, min(dbc, dbr)))))))));
  72. float ru = 0.0;
  73. float rv = 0.0;
  74. enc *= 10.0;
  75. ru -= trunc(enc);
  76. enc = frac(enc);
  77. enc *= 10.0;
  78. ru += trunc(enc);
  79. enc = frac(enc);
  80. enc *= 10.0;
  81. rv -= trunc(enc);
  82. enc = frac(enc);
  83. enc *= 10.0;
  84. rv += trunc(enc);
  85. enc = frac(enc);
  86. return uv + dd * float2(ru, rv);
  87. }*/
  88. float3 find_closest_fragment_5tap(float2 uv)
  89. {
  90. float2 dd = abs(_CameraDepthTexture_TexelSize.xy);
  91. float2 du = float2(dd.x, 0.0);
  92. float2 dv = float2(0.0, dd.y);
  93. float2 tl = -dv - du;
  94. float2 tr = -dv + du;
  95. float2 bl = dv - du;
  96. float2 br = dv + du;
  97. float dtl = tex2D(_CameraDepthTexture, uv + tl).x;
  98. float dtr = tex2D(_CameraDepthTexture, uv + tr).x;
  99. float dmc = tex2D(_CameraDepthTexture, uv).x;
  100. float dbl = tex2D(_CameraDepthTexture, uv + bl).x;
  101. float dbr = tex2D(_CameraDepthTexture, uv + br).x;
  102. float dmin = dmc;
  103. float2 dif = 0.0;
  104. if (ZCMP_GT(dmin, dtl)) { dmin = dtl; dif = tl; }
  105. if (ZCMP_GT(dmin, dtr)) { dmin = dtr; dif = tr; }
  106. if (ZCMP_GT(dmin, dbl)) { dmin = dbl; dif = bl; }
  107. if (ZCMP_GT(dmin, dbr)) { dmin = dbr; dif = br; }
  108. return float3(uv + dif, dmin);
  109. }
  110. #endif//__DEPTH_CGINC__