twgl实现webgl方块翻转动画效果代码
代码语言:html
所属分类:动画
代码描述:twgl实现webgl方块翻转动画效果代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <style> html { height: 100%; } body { background: #333; overflow: hidden; padding: 0; margin: 0; width: 100%; height: 100%; display: flex; align-items: center; } canvas { height: 100%; width: 100%; } </style> </head> <body translate="no"> <canvas id="canvas"></canvas> <!-- VertexShader code here --> <script id="vertexShader" type="x-shader/x-vertex">#version 300 es precision highp float; in vec4 position; void main() { gl_Position = vec4( position ); } </script> <!-- FragmentShader code here --> <script id="fragmentShader" type="x-shader/x-fragment">#version 300 es #if __VERSION__ < 130 #define TEXTURE2D texture2D #else #define TEXTURE2D texture #endif precision highp float; out vec4 fragColor; uniform vec2 u_resolution; uniform vec4 u_mouse; uniform float u_time; uniform sampler2D iChannel0; uniform sampler2D iChannel1; #define R u_resolution #define T u_time #define M u_mouse #define PI 3.1415926 #define PI2 6.2831853 #define MIN_DIST 1e-3 #define MAX_DIST 40. #define eoc(t) (t=t-1.)*t*t+1. #define eic(t) t*t*t vec3 hit,hp; mat2 mx,my,nx,ny,mz,mw,nz,nw; mat2 rot (float a) { return mat2(cos(a),sin(a),-sin(a),cos(a)); } float hash21( vec2 p ) { return fract(sin(dot(p,vec2(23.43,35.23))) *472.323); } // timing functions float lerp (float b, float e, float t) { return clamp((t - b) / (e - b), 0., 1.); } float box(vec3 p, vec3 b){ vec3 d = abs(p)-b; return length(max(d,0.))+min(max(d.x,max(d.y,d.z)),.0); } //globals vec3 s_hit, g_hit; vec2 s_id, g_id; float tspeed=0., tmod=0., t1=0., t3=0., t4=0.; //scales for all things const float scale = .75; const float scale_h = scale/2.; const vec2 s = vec2(scale*2.2); const vec2 pos = vec2(.5,-.5); const vec2[4] ps4 = vec2[4](pos.yx,pos.xx,pos.xy,pos.yy); float pattern(inout vec3 q, vec2 id) { float res = 1e5; float rnd = hash21(id); float ths =length(5.+5.*sin(id*.3)); ths = (rnd*1.37)+fract(ths*4324.35); // turn based on mod time ths += rnd; float t1 = lerp(ths ,ths+ .5,tmod); float t2 = lerp(ths+5. ,ths+ 5.5,tmod); float t3 = lerp(ths+10.,ths+10.5,tmod); float t4 = lerp(ths+15.,ths+15.5,tmod); t1 = eoc(t1); t1 = eic(t1); t2 = eoc(t2); t2 = eic(t2); t3 = eoc(t3); t3 = eic(t3); t4 = eoc(t4); t4 = eic(t4); vec3 nq = q-vec3(0,((t2-t3)+(t1-t4))*scale,0); // messy first draft float rf = 1.5707; nx = rot(rf*(t1-t2)); ny = rot(rf*(t3+t4)); nq.zy *= nx; nq.xz *= ny; nz = rot(rf*(t1-t4)); nw = rot(rf*(t2-t3)); nq.zy *= nz; nq.xz *= nw; float d = box(nq,vec3(scale_h))-.0185; if(d<res) { res = d; q = nq; } return res; } // block map -v4 tap technique from @Shane vec2 map(vec3 q3){ vec2 res = vec2(1e5,0.), p, ip, ct = vec2(0); float t=1e5, y=1e5, m=1.; for(int i =0; i<4; i++){ ct = ps4[i]/2. - ps4[0]/2.; p = q3.xz - ct*s; ip.........完整代码请登录后点击上方下载按钮下载查看
网友评论0