Mask.shader 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. Shader "DashGame/Mask"
  2. {
  3. Properties
  4. {
  5. _Color("Tint", Color) = (1,1,1,1)
  6. [MaterialToggle] PixelSnap("Pixel snap", Float) = 0
  7. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  8. }
  9. SubShader
  10. {
  11. Tags
  12. {
  13. "Queue" = "Transparent"
  14. "RenderType" = "Transparent"
  15. "PreviewType" = "Plane"
  16. "IgnoreProjector" = "True"
  17. "CanUseSpriteAtlas" = "True"
  18. }
  19. Cull Off
  20. ZWrite Off
  21. Lighting Off
  22. Pass
  23. {
  24. Blend SrcAlpha OneMinusSrcAlpha
  25. CGPROGRAM
  26. #pragma vertex vert
  27. #pragma fragment frag
  28. #pragma multi_compile _ PIXELSNAP_ON
  29. #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
  30. #include "UnityCG.cginc"
  31. struct appdata_t
  32. {
  33. float4 color : COLOR;
  34. float4 vertex : POSITION;
  35. float2 texcoord : TEXCOORD0;
  36. };
  37. struct v2f
  38. {
  39. fixed4 color : COLOR;
  40. float4 vertex : SV_POSITION;
  41. float2 texcoord : TEXCOORD0;
  42. float4 screenPos : TEXCOORD1;
  43. };
  44. float _Angle;
  45. float _RadiusX;
  46. float _RadiusY;
  47. float2 _Center;
  48. fixed4 _Color;
  49. sampler2D _MainTex;
  50. v2f vert(appdata_t IN)
  51. {
  52. v2f OUT;
  53. OUT.color = IN.color * _Color;
  54. OUT.vertex = UnityObjectToClipPos(IN.vertex);
  55. OUT.texcoord = IN.texcoord;
  56. OUT.screenPos = ComputeScreenPos(OUT.vertex);
  57. return OUT;
  58. }
  59. fixed4 SampleSpriteTexture(float2 uv)
  60. {
  61. fixed4 color = tex2D(_MainTex, uv);
  62. return color;
  63. }
  64. fixed4 frag(v2f i) : SV_Target
  65. {
  66. fixed4 col = SampleSpriteTexture(i.texcoord) * i.color;
  67. _Angle = _RadiusY / 4;
  68. float x = i.screenPos.x - _Center.x;
  69. float y = i.screenPos.y - _Center.y;
  70. if (abs(x) < _RadiusX && abs(y) < _RadiusY)
  71. {
  72. float2 ulo = _Center + float2(-_RadiusX, _RadiusY);
  73. float2 uli = ulo + float2(_Angle, -_Angle / (_ScreenParams.y / _ScreenParams.x));
  74. float2 uro = _Center + float2(_RadiusX, _RadiusY);
  75. float2 uri = uro + float2(-_Angle, -_Angle / (_ScreenParams.y / _ScreenParams.x));
  76. float2 lro = _Center + float2(_RadiusX, -_RadiusY);
  77. float2 lri = lro + float2(-_Angle, _Angle / (_ScreenParams.y / _ScreenParams.x));
  78. float2 llo = _Center + float2(-_RadiusX, -_RadiusY);
  79. float2 lli = llo + float2(_Angle, _Angle / (_ScreenParams.y / _ScreenParams.x));
  80. float urox = uro.x - i.screenPos.x;
  81. float urix = i.screenPos.x - uri.x;
  82. float uroy = uro.y - i.screenPos.y;
  83. float uriy = i.screenPos.y - uri.y;
  84. float ulox = i.screenPos.x - ulo.x;
  85. float ulix = uli.x - i.screenPos.x;
  86. float uloy = ulo.y - i.screenPos.y;
  87. float uliy = i.screenPos.y - uli.y;
  88. float lrox = lro.x - i.screenPos.x;
  89. float lrix = i.screenPos.x - lri.x;
  90. float lroy = i.screenPos.y - lro.y;
  91. float lriy = lri.y - i.screenPos.y;
  92. float llox = i.screenPos.x - llo.x;
  93. float llix = lli.x - i.screenPos.x;
  94. float lloy = i.screenPos.y - llo.y;
  95. float lliy = lli.y - i.screenPos.y;
  96. if (ulox > 0 && ulix > 0 && uloy > 0 && uliy > 0)
  97. {
  98. float2 vec = uli - i.screenPos;
  99. vec.y *= _ScreenParams.y / _ScreenParams.x;
  100. if (length(vec) < _Angle)
  101. {
  102. col.a = 0;
  103. }
  104. }
  105. else if (urox > 0 && urix > 0 && uroy > 0 && uriy > 0)
  106. {
  107. float2 vec = uri - i.screenPos;
  108. vec.y *= _ScreenParams.y / _ScreenParams.x;
  109. if (length(vec) < _Angle)
  110. {
  111. col.a = 0;
  112. }
  113. }
  114. else if (lrox > 0 && lrix > 0 && lroy > 0 && lriy > 0)
  115. {
  116. float2 vec = lri - i.screenPos;
  117. vec.y *= _ScreenParams.y / _ScreenParams.x;
  118. if (length(vec) < _Angle)
  119. {
  120. col.a = 0;
  121. }
  122. }
  123. else if (llox > 0 && llix > 0 && lloy > 0 && lliy > 0)
  124. {
  125. float2 vec = lli - i.screenPos;
  126. vec.y *= _ScreenParams.y / _ScreenParams.x;
  127. if (length(vec) < _Angle)
  128. {
  129. col.a = 0;
  130. }
  131. }
  132. else
  133. {
  134. col.a = 0;
  135. }
  136. }
  137. return col;
  138. }
  139. ENDCG
  140. }
  141. }
  142. }