ZED_Utils.cginc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. #if !defined(ZED_UTILS)
  3. #define ZED_UTILS
  4. #define MAX_DEPTH 0.9999f
  5. #if UNITY_REVERSED_Z
  6. #define NEAR_DEPTH MAX_DEPTH
  7. #define FAR_DEPTH 1 - MAX_DEPTH
  8. #else
  9. #define NEAR_DEPTH 1 - MAX_DEPTH
  10. #define FAR_DEPTH MAX_DEPTH
  11. #endif
  12. #define MAX_ZED_DEPTH 40
  13. #define MIN_ZED_DEPTH 0.1f
  14. #define ZED_CLAMP(name) name = clamp(name,MIN_ZED_DEPTH, MAX_ZED_DEPTH);
  15. #if UNITY_REVERSED_Z
  16. #define ZED_DEPTH_CLAMP(name) clamp(name,FAR_DEPTH, NEAR_DEPTH)
  17. #else
  18. #define ZED_DEPTH_CLAMP(name) clamp(name,NEAR_DEPTH, FAR_DEPTH)
  19. #endif
  20. #define ZED_PI 3.14159265359
  21. //Compute the depth of ZED to the Unity scale
  22. float computeDepthXYZ(float3 colorXYZ) {
  23. float d = FAR_DEPTH;
  24. //Unity BUG, (isinf(colorXYZ.z) && colorXYZ.z > 0 and isinf(colorXYZ.z) && colorXYZ.z < 0 pass together, and it's not a nan
  25. if (isinf(colorXYZ.z) && colorXYZ.z > 0) d = FAR_DEPTH;
  26. if (isinf(colorXYZ.z) && colorXYZ.z < 0) d += NEAR_DEPTH;
  27. if (d == FAR_DEPTH) return FAR_DEPTH;
  28. else if (d == (FAR_DEPTH + NEAR_DEPTH)) return NEAR_DEPTH;
  29. colorXYZ = clamp(colorXYZ, 0.01, 20);
  30. //reverse Y and Z axes
  31. colorXYZ.b = -colorXYZ.b;
  32. #if SHADER_API_D3D11
  33. colorXYZ.g = -colorXYZ.g;
  34. #elif SHADER_API_GLCORE
  35. colorXYZ.g = -colorXYZ.g * 2 + 1;
  36. #endif
  37. float4 v = float4(colorXYZ, 1);
  38. //Project to unity's coordinate
  39. float4 depthXYZVector = mul(UNITY_MATRIX_P, v);
  40. if (depthXYZVector.w != depthXYZVector.w) return FAR_DEPTH;
  41. depthXYZVector.b /= (depthXYZVector.w);
  42. float depthReal = depthXYZVector.b;
  43. return ZED_DEPTH_CLAMP(depthReal);
  44. }
  45. float3 applyQuatToVec3(float4 q, float3 v)
  46. {
  47. float3 t = 2 * cross(q.xyz, v);
  48. return v + q.w * t + cross(q.xyz, t);
  49. }
  50. //Compute the depth of ZED to the Unity scale
  51. float computeDepthXYZ(float colorXYZ) {
  52. if (isinf(colorXYZ) && colorXYZ > 0) return FAR_DEPTH;
  53. if (isinf(colorXYZ) && colorXYZ < 0) return NEAR_DEPTH;
  54. if (!isinf(colorXYZ) && !isfinite(colorXYZ)) return FAR_DEPTH;
  55. //if (colorXYZ != colorXYZ) return FAR_DEPTH; //Doesn't correctly check for a NaN, and neither does isnan().
  56. //colorXYZ = clamp(colorXYZ, 0.01, 20);
  57. #if SHADER_API_D3D11
  58. colorXYZ = -colorXYZ;
  59. #elif SHADER_API_GLCORE
  60. //colorXYZ = -colorXYZ * 2 + 1;
  61. colorXYZ = -colorXYZ * 2;
  62. #endif
  63. float4 v = float4(0,0, colorXYZ, 1);
  64. //Project to unity's coordinate
  65. float4 depthXYZVector = mul(UNITY_MATRIX_P, v);
  66. if (depthXYZVector.w != depthXYZVector.w) return FAR_DEPTH;
  67. depthXYZVector.b /= (depthXYZVector.w);
  68. float depthReal = depthXYZVector.b;
  69. return ZED_DEPTH_CLAMP(depthReal);
  70. }
  71. //Remove the optical center of the projection matrix for a specific object
  72. float4 GetPosWithoutOpticalCenter(float4 vertex) {
  73. float4x4 copy_projection = UNITY_MATRIX_P;
  74. copy_projection[0][2] = 0;
  75. copy_projection[1][2] = 0;
  76. return mul(mul(mul(copy_projection, UNITY_MATRIX_V), UNITY_MATRIX_M), vertex);
  77. }
  78. //Converts RGB to YUV
  79. float3 RGBtoYUV(float3 rgb)
  80. {
  81. float4x4 RGB2YUV = { 0.182586, 0.614231, 0.062007, 0.062745,
  82. -0.100644, -0.338572, 0.439216, 0.501961,
  83. 0.439216, -0.398942, -0.040274, 0.501961,
  84. 0.000000, 0.000000, 0.000000, 1.000000 };
  85. return mul(RGB2YUV, float4(rgb,1)).rgb;
  86. }
  87. //Algorithm to compute the alpha of a frag depending of the similarity of a color.
  88. //ColorCamera is the color from a texture given by the camera
  89. float computeAlphaYUVFromYUV(float3 colorCamera, in float3 keyColor) {
  90. return distance(keyColor.yz, colorCamera.yz);
  91. }
  92. // Decode uint32 into RGBA
  93. float4 PCDecodeColor(uint data)
  94. {
  95. float b = (data) & 0xff;
  96. float g = (data >> 8) & 0xff;
  97. float r = (data >> 16) & 0xff;
  98. float a = (data >> 24) & 0xff;
  99. return float4(r / 255.f, g / 255.f, b / 255.f, a / 255.f);
  100. }
  101. #endif