This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
/
YUVFeedShaderUnlit.shader
82 lines (75 loc) · 1.78 KB
/
YUVFeedShaderUnlit.shader
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
//
// Copyright (C) Microsoft. All rights reserved.
//
Shader "Microsoft.MixedReality.WebRTC/YUVFeedShader (unlit)"
{
Properties
{
[Toggle(MIRROR)] _Mirror("Horizontal Mirror", Float) = 0
[Toggle(VMIRROR)] _VMirror("Vertical Mirror", Float) = 0
[HideInEditor][NoScaleOffset] _YPlane("Y plane", 2D) = "white" {}
[HideInEditor][NoScaleOffset] _UPlane("U plane", 2D) = "white" {}
[HideInEditor][NoScaleOffset] _VPlane("V plane", 2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile __ MIRROR VMIRROR
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
#if !UNITY_UV_STARTS_AT_TOP
o.uv.y = 1 - v.uv.y;
#endif
#ifdef MIRROR
o.uv.x = 1 - v.uv.x;
#endif
#ifdef VMIRROR
o.uv.y = 1 - v.uv.y;
#endif
return o;
}
sampler2D _YPlane;
sampler2D _UPlane;
sampler2D _VPlane;
half3 yuv2rgb(float3 yuv)
{
// The YUV to RBA conversion, please refer to: http://en.wikipedia.org/wiki/YUV
// Y'UV420p (I420) to RGB888 conversion section.
half y_value = yuv[0];
half u_value = yuv[1];
half v_value = yuv[2];
half r = y_value + 1.370705 * (v_value - 0.5);
half g = y_value - 0.698001 * (v_value - 0.5) - (0.337633 * (u_value - 0.5));
half b = y_value + 1.732446 * (u_value - 0.5);
return half3(r, g, b);
}
fixed4 frag (v2f i) : SV_Target
{
half3 yuv;
yuv.x = tex2D(_YPlane, i.uv).r;
yuv.y = tex2D(_UPlane, i.uv).r;
yuv.z = tex2D(_VPlane, i.uv).r;
return fixed4(yuv2rgb(yuv), 1.0);
}
ENDCG
}
}
}