sprite-water-wave.effect 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. amplitude: { value: 0.05, range: [0.0, 0.5], editor: { tooltip: '振幅' } }
  15. angularVelocity: { value: 10, editor: { tooltip: '角速度' } }
  16. frequency: { value: 10, editor: { tooltip: '频率' } }
  17. offset: { value: 0.5, range: [0.0, 1.0], editor: { tooltip: '偏距' } }
  18. toLeft: { value: true, editor: { type: boolean, tooltip: '向左(方向)' } }
  19. }%
  20. CCProgram vs %{
  21. precision highp float;
  22. #include <cc-global>
  23. #include <cc-local>
  24. in vec3 a_position;
  25. in vec4 a_color;
  26. out vec4 v_color;
  27. #if USE_TEXTURE
  28. in vec2 a_uv0;
  29. out vec2 v_uv0;
  30. #endif
  31. void main () {
  32. vec4 pos = vec4(a_position, 1);
  33. #if CC_USE_MODEL
  34. pos = cc_matViewProj * cc_matWorld * pos;
  35. #else
  36. pos = cc_matViewProj * pos;
  37. #endif
  38. #if USE_TEXTURE
  39. v_uv0 = a_uv0;
  40. #endif
  41. v_color = a_color;
  42. gl_Position = pos;
  43. }
  44. }%
  45. CCProgram fs %{
  46. precision highp float;
  47. // 引入 Cocos Creator 内置的全部变量
  48. #include <cc-global>
  49. #include <texture>
  50. // 顶点颜色(来自顶点着色器)
  51. in vec4 v_color;
  52. // 纹理
  53. #if USE_TEXTURE
  54. in vec2 v_uv0;
  55. uniform sampler2D texture;
  56. #endif
  57. // 自定义属性
  58. uniform Properties {
  59. float amplitude; // 振幅
  60. float angularVelocity; // 角速度
  61. float frequency; // 频率
  62. float offset; // 偏距
  63. bool toLeft; // 是否向左
  64. };
  65. void main () {
  66. // 保存顶点颜色
  67. vec4 color = v_color;
  68. // 叠加纹理颜色
  69. color *= texture(texture, v_uv0);
  70. // 直接丢弃原本就透明的像素
  71. if (color.a == 0.0) discard;
  72. // 初相位(正值表现为向左移动,负值则表现为向右移动)
  73. // cc_time 是 Cocos Creator 提供的运行时间全局变量(类型:vec4)
  74. //float initiaPhase = frequency * cc_time.x;
  75. // 代入正弦曲线公式计算 y 值
  76. // y = Asin(ωx ± φt) + k
  77. float y = amplitude * sin((angularVelocity * v_uv0.x) + ((frequency * cc_time.x) * (toLeft ? 1. : -1.))) + offset;
  78. //float y = amplitude * sin(angularVelocity * v_uv0.x + initiaPhase) + offset;
  79. // 丢弃 y 值以上的像素(左上角为原点 [0.0, 0.0])
  80. if (v_uv0.y < y) discard;
  81. // 输出颜色
  82. gl_FragColor = color;
  83. }
  84. }%