RectMask.effect 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - passes:
  5. - vert: vs
  6. frag: fs
  7. blendState:
  8. targets:
  9. - blend: true
  10. rasterizerState:
  11. cullMode: none
  12. properties:
  13. texture: { value: white }
  14. alphaThreshold: { value: 0.5 }
  15. u_UVLeftTop: {
  16. value: [0.0, 0.0],
  17. inspector: {
  18. tooltip: "左顶点百分比",
  19. range: [0.0, 1.0]
  20. }
  21. }
  22. u_UVRightBottom: {
  23. value: [1.0, 1.0],
  24. inspector: {
  25. tooltip: "有下顶点百分比",
  26. range: [0.0, 1.0]
  27. }
  28. }
  29. }%
  30. CCProgram vs %{
  31. precision highp float;
  32. #include <cc-global>
  33. #include <cc-local>
  34. in vec3 a_position;
  35. in vec4 a_color;
  36. out vec4 v_color;
  37. #if USE_TEXTURE
  38. in vec2 a_uv0;
  39. out vec2 v_uv0;
  40. #endif
  41. void main () {
  42. vec4 pos = vec4(a_position, 1);
  43. #if CC_USE_MODEL
  44. pos = cc_matViewProj * cc_matWorld * pos;
  45. #else
  46. pos = cc_matViewProj * pos;
  47. #endif
  48. #if USE_TEXTURE
  49. v_uv0 = a_uv0;
  50. #endif
  51. v_color = a_color;
  52. gl_Position = pos;
  53. }
  54. }%
  55. CCProgram fs %{
  56. precision highp float;
  57. #include <alpha-test>
  58. in vec4 v_color;
  59. uniform Props {
  60. float edge;
  61. };
  62. #if USE_TEXTURE
  63. in vec2 v_uv0;
  64. uniform sampler2D texture;
  65. #endif
  66. uniform ARGS{
  67. vec2 u_UVLeftTop;
  68. vec2 u_UVRightBottom;
  69. };
  70. float insideBox(vec2 v, vec2 bottomLeft, vec2 topRight) {
  71. vec2 s = step(bottomLeft, v) - step(topRight, v);
  72. return s.x * s.y;
  73. }
  74. float insideBox2(vec2 v, vec2 bottomLeft, vec2 topRight) {
  75. float lineWidth = 0.003;
  76. vec2 s = step(bottomLeft+lineWidth, v) - step(topRight-lineWidth, v);
  77. return s.x * s.y;
  78. }
  79. void main () {
  80. // vec4 o = vec4(1, 1, 1, 1);
  81. // o *= texture(texture, v_uv0);
  82. // o *= v_color;
  83. // float t = insideBox(v_uv0, u_UVLeftTop, u_UVRightBottom);
  84. // o.a = t * o.a;
  85. // gl_FragColor = o;
  86. vec4 o = vec4(1, 1, 1, 1);
  87. #if USE_TEXTURE
  88. o *= texture(texture, v_uv0);
  89. #endif
  90. o *= v_color;
  91. // 检查当前像素是否在指定的矩形区域内
  92. vec2 uv = v_uv0;
  93. if (uv.x < u_UVLeftTop.x || uv.x > u_UVRightBottom.x ||
  94. uv.y < u_UVLeftTop.y || uv.y > u_UVRightBottom.y) {
  95. o.a = 0.0;
  96. }
  97. gl_FragColor = o;
  98. }
  99. }%