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 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.14159265359
#define PI2        6.28318530718

mat2 rot(float a) {return mat2(cos(a),sin(a),-sin(a),cos(a));}
vec3 hue(float t) { return .35 + .35*cos(PI2*t*(vec3(1.,.85,.75)+vec3(0.173,0.965,0.118)));}
float hash21(vec2 a) {return fract(sin(dot(a,vec2(27.609,57.583)))*43758.5453);}
float box(vec2 p, vec2 b) {vec2 d = abs(p)-b; return length(max(d,0.)) + min(max(d.x,d.y),0.);}
void main()
{
    vec2 F = gl_FragCoord.xy;
    vec3 C = vec3(0);
    vec2 uv = (2.*F-R.xy)/max(R.x,R.y);
    mat2 r90 = rot(1.5707);
   
    // polar warp
    uv *= rot(T*.095);
    uv = vec2(log(length(uv)), atan(uv.y, uv.x))*1.272;
    uv.x -= T*.25;

    float scale = 2.,
          px = fwidth(uv.x*scale);
    vec2 p = uv;
   
    vec2 id = floor(p*scale);
          p = fract(p*scale)-.5;

    float rnd = hash21(id);
   
    // turn tiles
    if(rnd>.5) p *= r90;
    rnd=fract(rnd*32.54);
    if(rnd>.4) p *= r90;
    if(rnd>.8) p *= r90;
   
    // randomize hash for type
    rnd=fract(rnd*47.13);

    float tk = .06;
    // kind of messy and long winded
    float d = box(p-vec2(.6,.7),vec2(.25,.75))-.15;
    float l = box(p-vec2(.7,.5),vec2(.75,.15))-.15;
    float b = box(p+vec2(0,.7),vec2(.05,.25))-.15;
    float r = box(p+vec2(.6,0),vec2(.15,.05))-.15;
    d = abs(d)-tk;
   
    if(rnd>.92) {
        d = box(p-vec2(-.6,.5),vec2(.25,.15))-.15;
        l = box(p-vec2(.6,.6),vec2(.25,.25))-.15;
        b = box(p+vec2(.6,.6),vec2(.25,.25))-.15;
        .........完整代码请登录后点击上方下载按钮下载查看

网友评论0