BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / Editor / OVROverlaySrcRectEditor.shader

OVROverlaySrcRectEditor.shader

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVROverlaySrcRectEditor.shader 3.7 KiB Download Raw
1Shader "Unlit/OVROverlaySrcRectEditor"
2{
3 Properties
4 {
5 _MainTex ("Texture", 2D) = "white" {}
6 _SrcRectLeft ("SrcRectLeft", Vector) = (0,0,1,1)
7 _SrcRectRight("SrcRectRight", Vector) = (0,0,1,1)
8 _BackgroundColor("Background Color", Color) = (0.225, 0.225, 0.225, 1)
9 }
10 SubShader
11 {
12 Tags { "RenderType"="Opaque" }
13 LOD 100
14
15 Pass
16 {
17 CGPROGRAM
18 #pragma vertex vert
19 #pragma fragment frag
20
21 #include "UnityCG.cginc"
22
23 struct appdata
24 {
25 float4 vertex : POSITION;
26 float2 uv : TEXCOORD0;
27 };
28
29 struct v2f
30 {
31 float2 uv : TEXCOORD0;
32 float4 vertex : SV_POSITION;
33 float4 leftDragX : TEXCOORD1;
34 float4 leftDragY : TEXCOORD2;
35 float4 rightDragX : TEXCOORD3;
36 float4 rightDragY : TEXCOORD4;
37 };
38
39 sampler2D _MainTex;
40 float4 _MainTex_ST;
41
42 float4 _SrcRectLeft;
43 float4 _SrcRectRight;
44
45 fixed4 _BackgroundColor;
46
47 v2f vert (appdata v)
48 {
49 v2f o;
50 o.vertex = UnityObjectToClipPos(v.vertex);
51 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
52 // Add padding
53 o.uv = (o.uv - 0.5) * (256.0 + 8.0) / (256.0) + 0.5;
54
55 // left
56 o.leftDragX.x = _SrcRectLeft.x;
57 o.leftDragY.x = _SrcRectLeft.y + _SrcRectLeft.w * 0.5;
58 // right
59 o.leftDragX.y = _SrcRectLeft.x + _SrcRectLeft.z;
60 o.leftDragY.y = _SrcRectLeft.y + _SrcRectLeft.w * 0.5;
61 // top
62 o.leftDragX.z = _SrcRectLeft.x + _SrcRectLeft.z * 0.5;
63 o.leftDragY.z = _SrcRectLeft.y;
64 // bottom
65 o.leftDragX.w = _SrcRectLeft.x + _SrcRectLeft.z * 0.5;
66 o.leftDragY.w = _SrcRectLeft.y + _SrcRectLeft.w;
67 // right
68 o.rightDragX.x = _SrcRectRight.x;
69 o.rightDragY.x = _SrcRectRight.y + _SrcRectRight.w * 0.5;
70 // right
71 o.rightDragX.y = _SrcRectRight.x + _SrcRectRight.z;
72 o.rightDragY.y = _SrcRectRight.y + _SrcRectRight.w * 0.5;
73 // top
74 o.rightDragX.z = _SrcRectRight.x + _SrcRectRight.z * 0.5;
75 o.rightDragY.z = _SrcRectRight.y;
76 // bottom
77 o.rightDragX.w = _SrcRectRight.x + _SrcRectRight.z * 0.5;
78 o.rightDragY.w = _SrcRectRight.y + _SrcRectRight.w;
79
80 return o;
81 }
82
83 float onDrag(float2 uv, float x, float y)
84 {
85 const float pixelSize = 6;
86 return abs(uv.x - x) < ((pixelSize / 2) / 256.0) && abs(uv.y - y) < ((pixelSize / 2) / 128.0);
87 }
88
89 float onLine(float2 uv, float4 rect)
90 {
91 return
92 (abs(uv.x - rect.x) < (1 / 256.0) && uv.y >= rect.y && uv.y <= rect.y + rect.w) ||
93 (abs(uv.x - rect.x - rect.z) < (1 / 256.0) && uv.y >= rect.y && uv.y <= rect.y + rect.w) ||
94 (abs(uv.y - rect.y) < (1 / 128.0) && uv.x >= rect.x && uv.x <= rect.x + rect.z) ||
95 (abs(uv.y - rect.y - rect.w) < (1 / 128.0) && uv.x >= rect.x && uv.x <= rect.x + rect.z);
96 }
97
98 float checkerboard(float2 uv)
99 {
100 float x = floor(uv.x * (16));
101 float y = floor(uv.y * 8);
102
103 return 2 * ((x + y) / 2.0 - floor((x + y) / 2.0));
104 }
105
106 fixed4 frag (v2f i) : SV_Target
107 {
108 // sample the texture
109 fixed4 col = tex2D(_MainTex, i.uv);
110
111 col.rgb = lerp(0.41 - 0.13 * checkerboard(i.uv), col.rgb, col.a);
112
113 if (i.uv.x < 0 || i.uv.x > 1 || i.uv.y < 0 || i.uv.y > 1)
114 {
115 col = _BackgroundColor;
116 }
117
118 float2 uv = float2(i.uv.x, 1 - i.uv.y);
119
120 // now draw clipping objects
121 float left = onLine(uv, _SrcRectLeft) ||
122 onDrag(uv, i.leftDragX.x, i.leftDragY.x) ||
123 onDrag(uv, i.leftDragX.y, i.leftDragY.y) ||
124 onDrag(uv, i.leftDragX.z, i.leftDragY.z) ||
125 onDrag(uv, i.leftDragX.w, i.leftDragY.w);
126
127 float right = onLine(uv, _SrcRectRight) ||
128 onDrag(uv, i.rightDragX.x, i.rightDragY.x) ||
129 onDrag(uv, i.rightDragX.y, i.rightDragY.y) ||
130 onDrag(uv, i.rightDragX.z, i.rightDragY.z) ||
131 onDrag(uv, i.rightDragX.w, i.rightDragY.w);
132
133 return lerp(col, fixed4(left, right, 0, 1), left || right);
134 }
135 ENDCG
136 }
137 }
138}