Assignment for RMIT Mixed Reality in 2020
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.

112 lines
2.8 KiB

  1. Shader "Oculus/OVRMRCameraFrameLit" {
  2. Properties{
  3. _Color("Color", Color) = (1,1,1,1)
  4. _MainTex("Albedo (RGB)", 2D) = "white" {}
  5. _DepthTex("Depth (cm)", 2D) = "black" {}
  6. _InconfidenceTex("Inconfidence (0-100)", 2D) = "black" {}
  7. _Visible("Visible", Range(0.0,1.0)) = 1.0
  8. }
  9. SubShader{
  10. Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
  11. LOD 200
  12. CGPROGRAM
  13. #pragma surface surf Lambert alpha:fade
  14. #pragma target 3.0
  15. #include "OVRMRChromaKey.cginc"
  16. #define TEST_ENVIRONMENT 0
  17. sampler2D _MainTex;
  18. sampler2D _DepthTex;
  19. sampler2D _MaskTex;
  20. float4 _TextureDimension; // (w, h, 1/w, 1/h)
  21. float4 _TextureWorldSize; // (width_in_meter, height_in_meter, 0, 0)
  22. float _SmoothFactor;
  23. float _DepthVariationClamp;
  24. float _CullingDistance;
  25. struct Input {
  26. #if TEST_ENVIRONMENT
  27. float2 uv_MainTex;
  28. #endif
  29. float4 screenPos;
  30. };
  31. fixed4 _Color;
  32. fixed _Visible;
  33. float4 _FlipParams; // (flip_h, flip_v, 0, 0)
  34. fixed GetMask(float2 uv)
  35. {
  36. return tex2D(_MaskTex, uv).r;
  37. }
  38. fixed4 GetCameraColor(float2 colorUV)
  39. {
  40. fixed4 c = tex2D(_MainTex, colorUV) * _Color;
  41. return c;
  42. }
  43. float GetDepth(float2 uv)
  44. {
  45. float depth = tex2D(_DepthTex, uv).x * 1.0 / 100;
  46. return depth;
  47. }
  48. float3 GetNormal(float2 uv)
  49. {
  50. float dz_x = GetDepth(uv + float2(_TextureDimension.z, 0)) - GetDepth(uv - float2(_TextureDimension.z, 0));
  51. float dz_y = GetDepth(uv + float2(0, _TextureDimension.w)) - GetDepth(uv - float2(0, _TextureDimension.w));
  52. dz_x = clamp(dz_x, -_DepthVariationClamp, _DepthVariationClamp);
  53. dz_y = clamp(dz_y, -_DepthVariationClamp, _DepthVariationClamp);
  54. //float dist = 0.01;
  55. //float3 normal = cross(float3(dist, 0, dz_x), float3(0, dist, dz_y));
  56. float3 normal = cross(float3(_TextureWorldSize.x * _TextureDimension.z * 2.0 * _SmoothFactor, 0, dz_x), float3(0, _TextureWorldSize.y * _TextureDimension.w * 2.0 * _SmoothFactor, dz_y));
  57. normal = normalize(normal);
  58. return normal;
  59. }
  60. void surf(Input IN, inout SurfaceOutput o) {
  61. #if TEST_ENVIRONMENT
  62. float2 colorUV = float2(IN.uv_MainTex.x, IN.uv_MainTex.y);
  63. #else
  64. float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
  65. float2 colorUV = float2(screenUV.x, 1.0 - screenUV.y);
  66. #endif
  67. if (_FlipParams.x > 0.0)
  68. {
  69. colorUV.x = 1.0 - colorUV.x;
  70. }
  71. if (_FlipParams.y > 0.0)
  72. {
  73. colorUV.y = 1.0 - colorUV.y;
  74. }
  75. float mask = GetMask(colorUV);
  76. if (mask == 0.0)
  77. {
  78. discard;
  79. }
  80. float4 col = GetColorAfterChromaKey(colorUV, _TextureDimension.zw, 1.0);
  81. if (col.a < 0.0)
  82. {
  83. discard;
  84. }
  85. float depth = GetDepth(colorUV);
  86. if (depth > _CullingDistance)
  87. {
  88. discard;
  89. }
  90. float3 normal = GetNormal(colorUV);
  91. o.Albedo = col.rgb;
  92. o.Normal = normal;
  93. o.Alpha = col.a *_Visible;
  94. }
  95. ENDCG
  96. }
  97. FallBack "Alpha-Diffuse"
  98. }