cocos creator 流光特效(参考shadertoy效果)

有兴趣的可以去官网:shadertoy官网 

流光特效代码:

// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.  

CCEffect %{
  techniques:
  - passes:
    - vert: vs
      frag: fs
      blendState:
        targets:
        - blend: true
      rasterizerState:
        cullMode: none
      properties:
        texture: { value: white }
        alphaThreshold: { value: 0.5 }
}%


CCProgram vs %{
  precision highp float;

  #include <cc-global>
  #include <cc-local>

  in vec3 a_position;
  in vec4 a_color;
  out vec4 v_color;

  #if USE_TEXTURE
  in vec2 a_uv0;
  out vec2 v_uv0;
  #endif

  void main () {
    vec4 pos = vec4(a_position, 1);

    #if CC_USE_MODEL
    pos = cc_matViewProj * cc_matWorld * pos;
    #else
    pos = cc_matViewProj * pos;
    #endif

    #if USE_TEXTURE
    v_uv0 = a_uv0;
    #endif

    v_color = a_color;

    gl_Position = pos;
  }
}%


CCProgram fs %{
  precision highp float;
  
  #include <alpha-test>

  in vec4 v_color;

  in vec2 v_uv0;
  uniform sampler2D texture;

  #define ANGLE 60.0
  float startX = 0.0;
  float thresholdX = 0.08;
  float blur = 0.01;
  float posInterval = 0.02;
  uniform ARGS {
    vec2 iResolution;
    float time;
  }
  float colFactor(vec2 uv, float thresholdX, float blur){
      float intervalY = uv.y;
      float tempX = intervalY * cos(radians(ANGLE)) + startX;
      float intervalX = uv.x - tempX;
      // 在0.08 ~ 0.04平滑处理
      return smoothstep(thresholdX, thresholdX - blur, abs(intervalX));
  }
  void mainImage( out vec4 fragColor, in vec2 fragCoord ,vec2 res)
  {
      // Normalized pixel coordinates (from 0 to 1)
      vec2 uv = fragCoord/res.xy;
      startX = tan(time * 3.);
      // factor取值范围为0.04~0.08范围内
      float factor = colFactor(uv, thresholdX, blur);
      vec4 texColor = texture2D(texture,v_uv0);
      // 混合操作等价于 mix函数
      vec4 factorColor = factor * vec4(1.0, 1., 1., 1.0) + texColor * (1.0 - factor);
      fragColor = factorColor;
  }
  void main () {
    vec2 res = vec2(300.,300.);
    mainImage(gl_FragColor,v_uv0.xy * res.xy,res);
  }
}%

猜你喜欢

转载自blog.csdn.net/lck8989/article/details/103711989