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