twgl+webgl实现三维木柱旋转拼接动画效果代码

代码语言:html

所属分类:三维

代码描述:twgl+webgl实现三维木柱旋转拼接动画效果代码

代码标签: 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 >
  <canvas id="canvas"></canvas>
<!-- VertexShader code here -->
<script id="vertexShader" type="x-shader/x-vertex">#version 300 es
#if __VERSION__ < 130
#define TEXTURE2D texture2D
#else
#define TEXTURE2D texture
#endif
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
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.14159265
#define PI2         6.28318530

#define MIN_DIST    .1e-5
#define MAX_DIST    15.

  
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,84.21)))*4832.3234);}
float lsp(float b, float e, float t){return clamp((t-b)/(e-b),0.,1.); }
float eoc(float t){return (t = t-1.)*t*t+1.; }

float opx(in float d, in float z, in float h){
    vec2 w = vec2( d, abs(z) - h );
  	return min(max(w.x, w.y), 0.) + length(max(w, 0.));
}

vec3 hp,hitpoint;
vec2 gid, sid;
float speed=0.,tspeed=0.,tmod=0.,ga1=0.,ga2=0.,ga3=0.,ga4=0.;
mat2 crot,srot;
  
const float size = 2.;
const float hlf = 1.;
const float tk = .2;
    
vec2 map(vec3 p) {
    vec2 res = vec2(1e5,0.);
 
    p.y += speed;

    vec2 uv = mod(p.xy+hlf,size)-hlf;
    vec2 id = floor((p.xy+hlf)/size);
    
    float rnd = hash21(id);
    if(rnd>.5) uv.y = -uv.y;
    rnd=fract(rnd*32.232);
    
    float sn = hash21(id.yx)*15.;
    sn = mod(sn,10.);
    
    float t1 = lsp(sn,sn+1.,tmod);
    float t2 = lsp(sn+2.,sn+3.,tmod);
    t1 = eoc(t1); t1 = t1*t1*t1;
    t2 = eoc(t2); t2 = t2*t2*t2;
    crot = rot((t1+t2)*1.5707);
    uv.xy *= crot;
    
    vec3 q = vec3(uv.xy,p.z);
    vec2 u2 = vec2(length(uv-hlf),length(uv+hlf));
    vec2 q2 = u2.x<u2.y ? uv-hlf : uv+hlf;
    
    float d1 = length(uv)-(hlf*.925);
    float d2 = abs(length(q2)-hlf)-tk;
    if(rnd>.85) d2 = min(length(q.x)-tk,length(q.y)-tk);

    d2 = max(d1,-d2);
    d2 = opx(d2,q.z+.45,7.5)-.025;
    
    if(d2<res.x) {
        res=vec2(d2,2.);
        hp=q;
        gid=id;
    }

    return res;
}


vec3 normal(vec3 p, float t) {
    float e = MIN_DIST*t;
    vec2 h =vec2(1,-1)*.5773;
    vec3 n = h.xyy * map(p+h.xyy*e).x+
             h.yyx * map(p+h.yyx*e).x+
             h.yxy * map(p+h.yxy*e).x+
             h.xxx * map(p+h.xxx*e).x;
    return normalize(n);
}


vec2 marcher(vec3 ro, vec3 rd, inout vec3 p) {
    float d=0.,m=0.;
    for(int i=0;i<86;i++){
        vec2 t = map(p);
        d += i<32? t.x*.5:t.x;
        m  = t.y;  
        p = ro + rd * d;
        if(t.x<d.........完整代码请登录后点击上方下载按钮下载查看

网友评论0