sprite-logo-flash.effect 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. sys_time: { value: 0.1, editor: { tooltip: '频率' } }
  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 <cc-global>
  45. #include <texture>
  46. #include <alpha-test>
  47. in vec4 v_color;
  48. in vec2 v_uv0;
  49. uniform sampler2D texture;
  50. // 自定义属性
  51. uniform Properties {
  52. float sys_time; // 频率
  53. };
  54. float inFlash(float angle, vec2 uv, float xLength, float interval, float beginTime, float offX, float loopTime){
  55. //亮度值
  56. float brightness = 0.0;
  57. //倾斜角
  58. float angleInRed = 0.0174444 * angle;
  59. //当前时间
  60. float currentTime = sys_time/interval;
  61. //获取本次光照的起始时间
  62. //float currentTimeInt = floor(currentTime/interval) -1.0;
  63. //currentTimeInt *= interval;
  64. //获取本次光照的流逝时间 = 当前时间 - 起始时间
  65. //float currentTimePassed = currentTime - currentTimeInt;
  66. if(currentTime > beginTime)
  67. {
  68. //底部左边界和右边界
  69. float xBottomLeftBound;
  70. float xBottomRightBound;
  71. //此点边界
  72. float xPointLeftBound;
  73. float xPointRightBound;
  74. float x0 = currentTime - beginTime;
  75. x0 /= loopTime;
  76. //设置右边界
  77. xBottomRightBound = x0;
  78. //设置左边界
  79. xBottomLeftBound = x0 - xLength;
  80. //投影至x的长度 = y/tan(angle)
  81. float xProjL = (uv.y)/tan(angleInRed);
  82. //此点的左边界 = 底部左边界 - 投影至x的长度
  83. xPointLeftBound = xBottomLeftBound - xProjL;
  84. //此点的右边界 = 底部右边界 - 投影至x的长度
  85. xPointRightBound = xBottomRightBound - xProjL;
  86. //边界加上一个偏移
  87. xPointLeftBound += offX;
  88. xPointRightBound += offX;
  89. //如果该点在区域内
  90. if(uv.x > xPointLeftBound && uv.x < xPointRightBound)
  91. {
  92. //得到发光区域的中心点
  93. float midness = (xPointLeftBound + xPointRightBound)/2.0;
  94. //趋近中心点的程度,0表示位于边缘, 1 表示位于中心点
  95. float rate = (xLength - 2.0 * abs(uv.x - midness)) / (xLength);
  96. brightness = rate;
  97. }
  98. }
  99. brightness = max(brightness, 0.0);
  100. //返回颜色 = 纯白色 * 亮度
  101. //vec4 col = vec4 (1,1,1,1) * brightness;
  102. return brightness;
  103. }
  104. void main () {
  105. // 保存顶点颜色
  106. vec4 color = v_color;
  107. // 叠加纹理颜色
  108. color *= texture(texture, v_uv0);
  109. vec4 outp;
  110. //传进i.uv等参数,得到亮度值
  111. float tmpBrightness;
  112. tmpBrightness = inFlash(60.0, v_uv0, 4.0, 0.25, 1.0, 0.15, 0.5);
  113. //图像区域,判定设置为颜色的A >0.5, 输出为材质颜色 + 光亮值
  114. if(color.w >0.5)
  115. {
  116. outp = color + vec4(1,1,1,1) * tmpBrightness/1.5;
  117. }
  118. else
  119. {
  120. outp = vec4(0,0,0,0);
  121. }
  122. gl_FragColor = outp;
  123. }
  124. }%