scan-effects.effect 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - passes:
  5. - vert: sprite-vs:vert
  6. frag: sprite-fs:frag
  7. depthStencilState:
  8. depthTest: false
  9. depthWrite: false
  10. blendState:
  11. targets:
  12. - blend: true
  13. blendSrc: src_alpha
  14. blendDst: one_minus_src_alpha
  15. blendDstAlpha: one_minus_src_alpha
  16. rasterizerState:
  17. cullMode: none
  18. properties:
  19. scanColor: { value: [0.5, 0.5, 0.5, 0.2] }
  20. scanWidth: { value: 0.05 }
  21. scanSpeed: { value: 1.0 }
  22. scanIntensity: { value: 2.0 }
  23. alphaThreshold: { value: 0.5 }
  24. }%
  25. CCProgram sprite-vs %{
  26. precision highp float;
  27. #include <builtin/uniforms/cc-global>
  28. #if USE_LOCAL
  29. #include <builtin/uniforms/cc-local>
  30. #endif
  31. #if SAMPLE_FROM_RT
  32. #include <common/common-define>
  33. #endif
  34. in vec3 a_position;
  35. in vec2 a_texCoord;
  36. in vec4 a_color;
  37. out vec4 color;
  38. out vec2 uv0;
  39. vec4 vert () {
  40. vec4 pos = vec4(a_position, 1);
  41. #if USE_LOCAL
  42. pos = cc_matWorld * pos;
  43. #endif
  44. #if USE_PIXEL_ALIGNMENT
  45. pos = cc_matView * pos;
  46. pos.xyz = floor(pos.xyz);
  47. pos = cc_matProj * pos;
  48. #else
  49. pos = cc_matViewProj * pos;
  50. #endif
  51. uv0 = a_texCoord;
  52. #if SAMPLE_FROM_RT
  53. CC_HANDLE_RT_SAMPLE_FLIP(uv0);
  54. #endif
  55. color = a_color;
  56. return pos;
  57. }
  58. }%
  59. CCProgram sprite-fs %{
  60. precision highp float;
  61. #include <builtin/internal/embedded-alpha>
  62. #include <builtin/internal/alpha-test>
  63. #include <builtin/uniforms/cc-global>
  64. in vec4 color;
  65. #if USE_TEXTURE
  66. in vec2 uv0;
  67. #pragma builtin(local)
  68. layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
  69. #endif
  70. uniform ScanUniforms {
  71. vec4 scanColor;
  72. float scanWidth;
  73. float scanSpeed;
  74. float scanIntensity;
  75. };
  76. vec4 frag () {
  77. vec4 o = vec4(1, 1, 1, 1);
  78. #if USE_TEXTURE
  79. o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0);
  80. // 计算扫描线位置 (从右下到左上)
  81. float scanPosition = mod(cc_time.x * scanSpeed, 2.0 + scanWidth);
  82. // 计算当前像素到扫描线的距离 (对角线扫描)
  83. float diagonalPos = (1.0-(uv0.x + uv0.y) * 0.5); // 从右下到左上的对角线位置
  84. float distanceToScan = abs(diagonalPos - scanPosition);
  85. // 创建扫描线效果
  86. float scanEffect = 0.0;
  87. if (distanceToScan < scanWidth) {
  88. // 在扫描线范围内,计算强度
  89. float normalizedDistance = distanceToScan / scanWidth;
  90. scanEffect = (1.0 - normalizedDistance) * scanIntensity;
  91. // 使用平滑过渡
  92. scanEffect = smoothstep(0.0, 1.0, scanEffect);
  93. }
  94. // 应用扫描效果
  95. if (scanEffect > 0.0) {
  96. // 混合原始颜色和扫描颜色
  97. o.rgb = mix(o.rgb, scanColor.rgb, scanEffect * scanColor.a);
  98. // 增加亮度
  99. o.rgb += scanColor.rgb * scanEffect * 0.5;
  100. }
  101. #endif
  102. o *= color;
  103. ALPHA_TEST(o);
  104. return o;
  105. }
  106. }%