BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Resources / OVRMRCameraFrameLit.shader

OVRMRCameraFrameLit.shader

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRMRCameraFrameLit.shader 2.8 KiB Download Raw
1Shader "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
13 CGPROGRAM
14 #pragma surface surf Lambert alpha:fade
15 #pragma target 3.0
16
17 #include "OVRMRChromaKey.cginc"
18
19 #define TEST_ENVIRONMENT 0
20
21 sampler2D _MainTex;
22 sampler2D _DepthTex;
23 sampler2D _MaskTex;
24
25 float4 _TextureDimension; // (w, h, 1/w, 1/h)
26 float4 _TextureWorldSize; // (width_in_meter, height_in_meter, 0, 0)
27
28 float _SmoothFactor;
29 float _DepthVariationClamp;
30
31 float _CullingDistance;
32
33 struct Input {
34 #if TEST_ENVIRONMENT
35 float2 uv_MainTex;
36 #endif
37 float4 screenPos;
38 };
39
40 fixed4 _Color;
41 fixed _Visible;
42 float4 _FlipParams; // (flip_h, flip_v, 0, 0)
43
44 fixed GetMask(float2 uv)
45 {
46 return tex2D(_MaskTex, uv).r;
47 }
48
49 fixed4 GetCameraColor(float2 colorUV)
50 {
51 fixed4 c = tex2D(_MainTex, colorUV) * _Color;
52 return c;
53 }
54
55 float GetDepth(float2 uv)
56 {
57 float depth = tex2D(_DepthTex, uv).x * 1.0 / 100;
58 return depth;
59 }
60
61 float3 GetNormal(float2 uv)
62 {
63 float dz_x = GetDepth(uv + float2(_TextureDimension.z, 0)) - GetDepth(uv - float2(_TextureDimension.z, 0));
64 float dz_y = GetDepth(uv + float2(0, _TextureDimension.w)) - GetDepth(uv - float2(0, _TextureDimension.w));
65 dz_x = clamp(dz_x, -_DepthVariationClamp, _DepthVariationClamp);
66 dz_y = clamp(dz_y, -_DepthVariationClamp, _DepthVariationClamp);
67 //float dist = 0.01;
68 //float3 normal = cross(float3(dist, 0, dz_x), float3(0, dist, dz_y));
69 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));
70 normal = normalize(normal);
71 return normal;
72 }
73
74 void surf(Input IN, inout SurfaceOutput o) {
75 #if TEST_ENVIRONMENT
76 float2 colorUV = float2(IN.uv_MainTex.x, IN.uv_MainTex.y);
77 #else
78 float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
79 float2 colorUV = float2(screenUV.x, 1.0 - screenUV.y);
80 #endif
81 if (_FlipParams.x > 0.0)
82 {
83 colorUV.x = 1.0 - colorUV.x;
84 }
85 if (_FlipParams.y > 0.0)
86 {
87 colorUV.y = 1.0 - colorUV.y;
88 }
89 float mask = GetMask(colorUV);
90 if (mask == 0.0)
91 {
92 discard;
93 }
94 float4 col = GetColorAfterChromaKey(colorUV, _TextureDimension.zw, 1.0);
95 if (col.a < 0.0)
96 {
97 discard;
98 }
99 float depth = GetDepth(colorUV);
100 if (depth > _CullingDistance)
101 {
102 discard;
103 }
104 float3 normal = GetNormal(colorUV);
105 o.Albedo = col.rgb;
106 o.Normal = normal;
107 o.Alpha = col.a *_Visible;
108 }
109 ENDCG
110 }
111 FallBack "Alpha-Diffuse"
112}