OVROverlaySrcRectEditor.shader 3.7 KB

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