ValidateJigsaw.shader 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //验证码拖拽
  2. Shader "JC/UI/ValidateJigsaw"
  3. {
  4. Properties
  5. {
  6. [PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
  7. _Color("Tint", Color) = (1,1,1,1)
  8. _StencilComp("Stencil Comparison", Float) = 8
  9. _Stencil("Stencil ID", Float) = 0
  10. _StencilOp("Stencil Operation", Float) = 0
  11. _StencilWriteMask("Stencil Write Mask", Float) = 255
  12. _StencilReadMask("Stencil Read Mask", Float) = 255
  13. _ColorMask("Color Mask", Float) = 15
  14. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip("Use Alpha Clip", Float) = 0
  15. _NodeWidth("Node Width", Float) = 100
  16. _NodeHeight("Node Height", Float) = 100
  17. _GapColor("Gap Color", Color) = (0,0,0,1)
  18. _GapBorderColor("Gap Border Color", Color) = (1,1,1,1)
  19. _GapBorderWidth("Gap Border Width", Float) = 0.5
  20. _GapSize("Gap Size", Float) = 20
  21. _OriginalPosition("Original Position", Vector) = (40, 40, 0)
  22. _CurrentPosition("Current Position", Vector) = (10, 40, 0)
  23. }
  24. SubShader
  25. {
  26. Tags
  27. {
  28. "Queue" = "Transparent"
  29. "IgnoreProjector" = "True"
  30. "RenderType" = "Transparent"
  31. "PreviewType" = "Plane"
  32. "CanUseSpriteAtlas" = "True"
  33. }
  34. Stencil
  35. {
  36. Ref[_Stencil]
  37. Comp[_StencilComp]
  38. Pass[_StencilOp]
  39. ReadMask[_StencilReadMask]
  40. WriteMask[_StencilWriteMask]
  41. }
  42. Cull Off
  43. Lighting Off
  44. ZWrite Off
  45. ZTest[unity_GUIZTestMode]
  46. Blend SrcAlpha OneMinusSrcAlpha
  47. ColorMask[_ColorMask]
  48. Pass
  49. {
  50. CGPROGRAM
  51. #pragma vertex vert
  52. #pragma fragment frag
  53. #include "UnityCG.cginc"
  54. #include "UnityUI.cginc"
  55. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  56. struct appdata_t
  57. {
  58. float4 vertex :
  59. POSITION;
  60. float4 color :
  61. COLOR;
  62. float2 texcoord :
  63. TEXCOORD0;
  64. };
  65. struct v2f
  66. {
  67. float4 vertex :
  68. SV_POSITION;
  69. fixed4 color :
  70. COLOR;
  71. half2 texcoord :
  72. TEXCOORD0;
  73. float4 worldPosition :
  74. TEXCOORD1;
  75. };
  76. fixed4 _Color;
  77. fixed4 _TextureSampleAdd;
  78. float4 _ClipRect;
  79. float _NodeWidth;
  80. float _NodeHeight;
  81. float4 _GapColor;
  82. float4 _GapBorderColor;
  83. float _GapBorderWidth;
  84. float _GapSize;
  85. float2 _OriginalPosition;
  86. float2 _CurrentPosition;
  87. float2 _GapMapPosition;
  88. float4 _MainTex_TexelSize;
  89. v2f vert(appdata_t IN)
  90. {
  91. v2f OUT;
  92. OUT.worldPosition = IN.vertex;
  93. OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  94. OUT.texcoord = IN.texcoord;
  95. #ifdef UNITY_HALF_TEXEL_OFFSET
  96. OUT.vertex.xy += (_ScreenParams.zw - 1.0)*float2(-1,1);
  97. #endif
  98. OUT.color = IN.color * _Color;
  99. return OUT;
  100. }
  101. sampler2D _MainTex;
  102. fixed4 frag(v2f IN) : SV_Target
  103. {
  104. half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
  105. color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  106. #ifdef UNITY_UI_ALPHACLIP
  107. clip(color.a - 0.001);
  108. #endif
  109. float w = _NodeWidth;
  110. float h = _NodeHeight;
  111. float gbw = _GapBorderWidth;
  112. float gs = _GapSize;
  113. float2 op = _OriginalPosition;
  114. float2 cp = _CurrentPosition;
  115. float x = IN.texcoord.x * w;
  116. float y = IN.texcoord.y * h;
  117. if (x >= op.x && x < op.x + gs && y >= op.y && y < op.y + gs)
  118. {
  119. if (x - op.x < gbw || x - op.x > gs - gbw || y - op.y < gbw || y - op.y > gs - gbw) color.rgba = _GapBorderColor;
  120. else color.rgba = _GapColor;
  121. }
  122. if (x >= cp.x && x < cp.x + gs && y >= cp.y && y < cp.y + gs)
  123. {
  124. if (x - cp.x < gbw || x - cp.x > gs - gbw || y - cp.y < gbw || y - cp.y > gs - gbw) color.rgba = _GapBorderColor;
  125. else
  126. {
  127. _GapMapPosition.x = (x - cp.x + op.x) / w;
  128. _GapMapPosition.y = (y - cp.y + op.y) / h;
  129. float4 _GapMapColor = tex2D(_MainTex, _GapMapPosition);
  130. color.rgba = _GapMapColor;
  131. }
  132. }
  133. return color;
  134. }
  135. ENDCG
  136. }
  137. }
  138. }