效果链接: WebGL Vox Demo

glsl代码:

this.vshdCode = 
          "precision mediump float;"
          +"attribute vec3 a_vtx_pos;"
          +"attribute vec2 a_vtx_uv;"
          +"varying highp vec2 v_texUV;"
          +"uniform mediump vec2 u_Stage_size;"
          +"uniform float a_time;"
          +"varying float v_time;"
          +"void main()"
          +"{"
          +"  float px = 0.5 * u_Stage_size.x;"
          +"  float py = 0.5 * u_Stage_size.y;"
          +"  float kw = (2.0/u_Stage_size.x);"
          +"  float kh = (2.0/u_Stage_size.y);"
          +"  gl_Position = vec4((px + a_vtx_pos.x)* kw - 1.0, (py + a_vtx_pos.y) * kh - 1.0,0.0,1.0);"
          +"  v_texUV = a_vtx_uv;"
          +"  v_time = a_time;"
          +"}";
  this.fshdCode = 
            "precision mediump float;"
            +"uniform sampler2D uSampler;"
            +"varying highp vec2 v_texUV;"
            +"varying float v_time;"
            +"void main()"
            +"{"
            +"  vec2 puv = v_texUV;"
            +"  vec4 color = texture2D( uSampler, vec2(v_texUV.x + v_time * 0.01, v_texUV.y) );"
            +"  float f = pow(color.r,2.0);"
            +"  float rad = v_time * 0.05;"
            +"  float cosV = cos(rad);"
            +"	float sinV = sin(rad);"
            +"  float scale = 1.0 + cosV * 0.01;"
            +"  vec2 uvPos = vec2(puv.x - 0.5,puv.y - 0.5);"
            +"  puv.x = v_time * 0.1 + scale * (uvPos.x * cosV - uvPos.y*sinV);"
            +"  puv.y = scale * (uvPos.x * sinV + uvPos.y*cosV);"
            +"  vec4 scolor = texture2D( uSampler, puv );"
            +"  if((scolor.r * scolor.r) > 0.3)"
            +"  {"
            +"    color.bgr = color.rgb + scolor.rgb;"
            +"  }else{"
            +"     scolor.rgb = (1.0 - scolor.rgb);"
            +"  }"
            +"  color = texture2D( uSampler, vec2(cos(scolor.r * 3.0) * rad, scolor.g * scolor.g * rad * 3.0) ).bgra;"
            +"  color.rgb = color.rgb * color.rgb;"
            +"  gl_FragColor = color;"
            +"}";

更多推荐