fluxay.effect 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_fluxayTexture: { value: white }
  16. }%
  17. CCProgram vs %{
  18. precision highp float;
  19. #include <cc-global>
  20. #include <cc-local>
  21. in vec3 a_position;
  22. in vec4 a_color;
  23. out vec4 v_color;
  24. #if USE_TEXTURE
  25. in vec2 a_uv0;
  26. out vec2 v_uv0;
  27. #endif
  28. void main () {
  29. vec4 pos = vec4(a_position, 1);
  30. #if CC_USE_MODEL
  31. pos = cc_matViewProj * cc_matWorld * pos;
  32. #else
  33. pos = cc_matViewProj * pos;
  34. #endif
  35. #if USE_TEXTURE
  36. v_uv0 = a_uv0;
  37. #endif
  38. v_color = a_color;
  39. gl_Position = pos;
  40. }
  41. }%
  42. CCProgram fs %{
  43. precision highp float;
  44. #include <alpha-test>
  45. #include <texture>
  46. #include <cc-global>
  47. in vec4 v_color;
  48. #if USE_TEXTURE
  49. in vec2 v_uv0;
  50. uniform sampler2D texture;
  51. #endif
  52. uniform sampler2D u_fluxayTexture; //流光纹理
  53. // 自定义属性
  54. // 所有非 sampler 的 uniform 都必须以 UBO 形式声明
  55. // UBO 成员声明类型和顺序有严格的校验机制,以排除 GL 标准下隐式布局对齐带来的内存消耗
  56. uniform ARGS {
  57. float u_time;
  58. };
  59. void main () {
  60. vec4 o = vec4(1, 1, 1, 1);
  61. #if USE_TEXTURE
  62. CCTexture(texture, v_uv0, o);
  63. #endif
  64. o *= v_color;
  65. #if USE_BGRA
  66. gl_FragColor = o.bgra;
  67. #else
  68. gl_FragColor = o.rgba;
  69. #endif
  70. ALPHA_TEST(o);
  71. // 使用 sin 函数模拟时间变化,周期为 2.0 秒
  72. float simulatedTime = tan(cc_time.x);
  73. // 在底图不透明的地方叠加流光纹理的颜色
  74. vec2 fluxayUV = vec2(v_uv0.x, v_uv0.y);
  75. // 根据时间控制流光纹理的UV
  76. fluxayUV.x -= simulatedTime;
  77. // 获取流光纹理上UV的颜色
  78. vec4 fluxay = texture2D(u_fluxayTexture, fluxayUV);
  79. if(o.a<= 0.8 && simulatedTime > -1.0 && simulatedTime < 1.0 && v_uv0.x > 0.1 && v_uv0.x < 0.9 && v_uv0.y > 0.1 && v_uv0.y < 0.9) {
  80. gl_FragColor = o + fluxay;
  81. } else {
  82. gl_FragColor = o;
  83. }
  84. }
  85. }%