| 1 | Shader "Unlit/OVROverlayDestRectEditor" |
| 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 | _DestRectLeft ("DestRectLeft", Vector) = (0,0,1,1) |
| 9 | _DestRectRight("DestRectRight", Vector) = (0,0,1,1) |
| 10 | _BackgroundColor("Background Color", Color) = (0.225, 0.225, 0.225, 1) |
| 11 | } |
| 12 | SubShader |
| 13 | { |
| 14 | Tags { "RenderType"="Opaque" } |
| 15 | LOD 100 |
| 16 | |
| 17 | Pass |
| 18 | { |
| 19 | CGPROGRAM |
| 20 | #pragma vertex vert |
| 21 | #pragma fragment frag |
| 22 | |
| 23 | #include "UnityCG.cginc" |
| 24 | |
| 25 | struct appdata |
| 26 | { |
| 27 | float4 vertex : POSITION; |
| 28 | float2 uv : TEXCOORD0; |
| 29 | }; |
| 30 | |
| 31 | struct v2f |
| 32 | { |
| 33 | float2 uv : TEXCOORD0; |
| 34 | float4 vertex : SV_POSITION; |
| 35 | float4 leftDragX : TEXCOORD1; |
| 36 | float4 leftDragY : TEXCOORD2; |
| 37 | float4 rightDragX : TEXCOORD3; |
| 38 | float4 rightDragY : TEXCOORD4; |
| 39 | }; |
| 40 | |
| 41 | sampler2D _MainTex; |
| 42 | float4 _MainTex_ST; |
| 43 | |
| 44 | float4 _SrcRectLeft; |
| 45 | float4 _SrcRectRight; |
| 46 | float4 _DestRectLeft; |
| 47 | float4 _DestRectRight; |
| 48 | |
| 49 | fixed4 _BackgroundColor; |
| 50 | |
| 51 | v2f vert (appdata v) |
| 52 | { |
| 53 | v2f o; |
| 54 | o.vertex = UnityObjectToClipPos(v.vertex); |
| 55 | o.uv = TRANSFORM_TEX(v.uv, _MainTex); |
| 56 | // Add padding |
| 57 | o.uv = (o.uv - 0.5) * (256.0 + 8.0) / (256.0) + 0.5; |
| 58 | |
| 59 | // left |
| 60 | o.leftDragX.x = _DestRectLeft.x; |
| 61 | o.leftDragY.x = _DestRectLeft.y + _DestRectLeft.w * 0.5; |
| 62 | // right |
| 63 | o.leftDragX.y = _DestRectLeft.x + _DestRectLeft.z; |
| 64 | o.leftDragY.y = _DestRectLeft.y + _DestRectLeft.w * 0.5; |
| 65 | // top |
| 66 | o.leftDragX.z = _DestRectLeft.x + _DestRectLeft.z * 0.5; |
| 67 | o.leftDragY.z = _DestRectLeft.y; |
| 68 | // bottom |
| 69 | o.leftDragX.w = _DestRectLeft.x + _DestRectLeft.z * 0.5; |
| 70 | o.leftDragY.w = _DestRectLeft.y + _DestRectLeft.w; |
| 71 | // right |
| 72 | o.rightDragX.x = _DestRectRight.x; |
| 73 | o.rightDragY.x = _DestRectRight.y + _DestRectRight.w * 0.5; |
| 74 | // right |
| 75 | o.rightDragX.y = _DestRectRight.x + _DestRectRight.z; |
| 76 | o.rightDragY.y = _DestRectRight.y + _DestRectRight.w * 0.5; |
| 77 | // top |
| 78 | o.rightDragX.z = _DestRectRight.x + _DestRectRight.z * 0.5; |
| 79 | o.rightDragY.z = _DestRectRight.y; |
| 80 | // bottom |
| 81 | o.rightDragX.w = _DestRectRight.x + _DestRectRight.z * 0.5; |
| 82 | o.rightDragY.w = _DestRectRight.y + _DestRectRight.w; |
| 83 | |
| 84 | return o; |
| 85 | } |
| 86 | |
| 87 | float onDrag(float2 uv, float x, float y) |
| 88 | { |
| 89 | const float pixelSize = 6; |
| 90 | return abs(uv.x - x) < ((pixelSize / 2) / 128.0) && abs(uv.y - y) < ((pixelSize / 2) / 128.0); |
| 91 | } |
| 92 | |
| 93 | float onLine(float2 uv, float4 rect) |
| 94 | { |
| 95 | return |
| 96 | (abs(uv.x - rect.x) < (1 / 128.0) && uv.y >= rect.y && uv.y <= rect.y + rect.w) || |
| 97 | (abs(uv.x - rect.x - rect.z) < (1 / 128.0) && uv.y >= rect.y && uv.y <= rect.y + rect.w) || |
| 98 | (abs(uv.y - rect.y) < (1 / 128.0) && uv.x >= rect.x && uv.x <= rect.x + rect.z) || |
| 99 | (abs(uv.y - rect.y - rect.w) < (1 / 128.0) && uv.x >= rect.x && uv.x <= rect.x + rect.z); |
| 100 | } |
| 101 | |
| 102 | float checkerboard(float2 uv) |
| 103 | { |
| 104 | float x = floor(uv.x * (16 + 2)); |
| 105 | float y = floor(uv.y * 8); |
| 106 | |
| 107 | return 2 * ((x + y) / 2.0 - floor((x + y) / 2.0)); |
| 108 | } |
| 109 | |
| 110 | fixed4 frag (v2f i) : SV_Target |
| 111 | { |
| 112 | float isLeftEye = i.uv < 0.5; |
| 113 | float2 leftUV = float2(i.uv.x * (256.0 + 32.0) / 128.0, 1 - i.uv.y); |
| 114 | float2 rightUV = float2(1 - ((1 - i.uv.x) * (256.0 + 32.0) / 128.0), 1 - i.uv.y); |
| 115 | |
| 116 | float2 uv = i.uv; |
| 117 | float2 textureUV = i.uv; |
| 118 | if (isLeftEye) |
| 119 | { |
| 120 | uv = (leftUV - _DestRectLeft.xy) / _DestRectLeft.zw; |
| 121 | textureUV = uv * _SrcRectLeft.zw + _SrcRectLeft.xy; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | uv = (rightUV - _DestRectRight.xy) / _DestRectRight.zw; |
| 126 | textureUV = uv * _SrcRectRight.zw + _SrcRectRight.xy; |
| 127 | } |
| 128 | |
| 129 | // sample the texture |
| 130 | fixed4 col = tex2D(_MainTex, float2(textureUV.x, 1 - textureUV.y)); |
| 131 | |
| 132 | if (uv.x < 0 || uv.x > 1 || uv.y < 0 || uv.y > 1) |
| 133 | { |
| 134 | col.a = 0; |
| 135 | } |
| 136 | |
| 137 | col.rgb = lerp(0.41 - 0.13 * checkerboard(i.uv), col.rgb, col.a); |
| 138 | |
| 139 | if (i.uv.x < 0 || i.uv.x > 1 || i.uv.y < 0 || i.uv.y > 1 || abs(i.uv.x - 0.5) < (14 / 256.0)) |
| 140 | { |
| 141 | col = _BackgroundColor; |
| 142 | } |
| 143 | |
| 144 | // now draw clipping objects |
| 145 | float left = isLeftEye && (onLine(leftUV, _DestRectLeft) || |
| 146 | onDrag(leftUV, i.leftDragX.x, i.leftDragY.x) || |
| 147 | onDrag(leftUV, i.leftDragX.y, i.leftDragY.y) || |
| 148 | onDrag(leftUV, i.leftDragX.z, i.leftDragY.z) || |
| 149 | onDrag(leftUV, i.leftDragX.w, i.leftDragY.w)); |
| 150 | |
| 151 | float right = (!isLeftEye) && (onLine(rightUV, _DestRectRight) || |
| 152 | onDrag(rightUV, i.rightDragX.x, i.rightDragY.x) || |
| 153 | onDrag(rightUV, i.rightDragX.y, i.rightDragY.y) || |
| 154 | onDrag(rightUV, i.rightDragX.z, i.rightDragY.z) || |
| 155 | onDrag(rightUV, i.rightDragX.w, i.rightDragY.w)); |
| 156 | |
| 157 | return lerp(col, fixed4(left, right, 0, 1), left || right); |
| 158 | } |
| 159 | ENDCG |
| 160 | } |
| 161 | } |
| 162 | } |