shader_lighting.effect 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. }%
  16. CCProgram vs %{
  17. precision highp float;
  18. #include <cc-global>
  19. #include <cc-local>
  20. in vec3 a_position;
  21. in vec4 a_color;
  22. out vec4 v_color;
  23. #if USE_TEXTURE
  24. in vec2 a_uv0;
  25. out vec2 v_uv0;
  26. #endif
  27. void main () {
  28. vec4 pos = vec4(a_position, 1);
  29. #if CC_USE_MODEL
  30. pos = cc_matViewProj * cc_matWorld * pos;
  31. #else
  32. pos = cc_matViewProj * pos;
  33. #endif
  34. #if USE_TEXTURE
  35. v_uv0 = a_uv0;
  36. #endif
  37. v_color = a_color;
  38. gl_Position = pos;
  39. }
  40. }%
  41. CCProgram fs %{
  42. precision highp float;
  43. #include <cc-global>
  44. #include <alpha-test>
  45. #include <texture>
  46. in vec4 v_color;
  47. #if USE_TEXTURE
  48. in vec2 v_uv0;
  49. uniform sampler2D texture;
  50. #endif
  51. vec2 getSt(){
  52. vec4 o = vec4(1, 1, 1, 1);
  53. #if USE_TEXTURE
  54. CCTexture(texture, v_uv0, o);
  55. #endif
  56. o *= v_color;
  57. ALPHA_TEST(o);
  58. return v_uv0;
  59. }
  60. float Hash( vec2 p, in float s)
  61. {
  62. vec3 p2 = vec3(p.xy,27.0 * abs(sin(s)));
  63. return fract(sin(dot(p2,vec3(27.1,61.7, 12.4)))*273758.5453123);
  64. }
  65. float noise(in vec2 p, in float s)
  66. {
  67. vec2 i = floor(p);
  68. vec2 f = fract(p);
  69. f *= f * (3.0-2.0*f);
  70. return mix(mix(Hash(i + vec2(0.,0.), s), Hash(i + vec2(1.,0.), s),f.x),
  71. mix(Hash(i + vec2(0.,1.), s), Hash(i + vec2(1.,1.), s),f.x),f.y) * s;
  72. }
  73. float fbm(vec2 p)
  74. {
  75. float v = 0.0;
  76. v += noise(p*2., 0.25);
  77. v += noise(p*8., 0.0625);
  78. return v;
  79. }
  80. const float thickness = 100.0;// 闪电粗细值,值越小,闪电越粗
  81. const float offset = 0.4; // 闪电的偏移值
  82. const vec3 color = vec3(0.68, 0.85, 1.0);//此处可以修改 闪电的颜色
  83. const float rang = 0.45; // 闪电范围参数,一定范围内(小于1.0),值越大闪电边缘光越小
  84. void main () {
  85. vec2 st = getSt();
  86. vec2 position = vec2(offset) - st;
  87. float t = abs(0.5/ ((position.x + fbm(position + cc_time.x)) * thickness));
  88. vec3 finalColor = t / color;
  89. float absv = abs(finalColor.x) + abs(finalColor.y) + abs(finalColor.z);
  90. gl_FragColor = vec4( finalColor,smoothstep(rang,1.0,absv));
  91. }
  92. }%