OVROverlayDestRectEditor.shader 4.6 KB

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