webgl+canvas实现水液体涟漪动画效果代码

代码语言:html

所属分类:动画

代码描述:webgl+canvas实现水液体涟漪动画效果代码

代码标签: webgl canvas 液体 涟漪 动画

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">
  

  
  
  
<style>
* {
    box-sizing: border-box;
  }
  
  html, body {
    margin: 0;
    min-height: 100vh;
    overflow: hidden;
    
   background:black;
    
    touch-action: none;
  }
  
  canvas {
    width: 100%;
    height: auto;
    object-fit: contain;
  }

  .wrapper {
    z-index:100;

position:absolute;
width:100%;
height:100%;
background:transparent;
opacity:.3;


  }

  .wrapper .choral{
    z-index:100;

position:absolute;
width:100%;
height:100%;

background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/ocean.jpeg);
opacity:.1;
-webkit-animation: changes infinite;
animation-duration:1000s;
animation-delay:0s;
background-size:300%;
animation-direction: alternate-reverse;
background-repeat: no-repeat;

  }

  @keyframes changes {0% {opacity:0;}
 
    
100% {background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/ocean.jpeg);background-size:400%; }  
   
  
  
  }

  


  .wrapper .fish {
    position:absolute;
    top:-5%;
    left:-20%;
    width:200%;
    height:300%;

    background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
    opacity:0;
    border-radius:10%;

    -webkit-animation: shadows infinite;
    animation-duration:22s;
    animation-delay:0s;
    background-size:100%;
    background-repeat: no-repeat;
  }

  .wrapper .fish2 {
    position:absolute;
    top:-10%;
    left:-20%;
    width:160%;
    height:150%;

    background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
    opacity:0;
    border-radius:5%;
    -webkit-animation: shadows infinite;
    animation-duration:22s;
    animation-delay:25s;
    background-size:100%;
    background-repeat: no-repeat;
  }

  .wrapper .fish3 {
    position:absolute;
    top:-5%;
    left:-20%;
    width:150%;
    height:180%;
    border-radius:11%;
    background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
    opacity:0;

    -webkit-animation: shadows infinite;
    animation-duration:22s;
    animation-delay:6s;
    background-size:100%;
    background-repeat: no-repeat;
  }

  .wrapper .fish4 {
    position:absolute;
    top:-20%;
    left:-20%;
    width:200%;
    height:200%;

    background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
    opacity:0;
    border-radius:8%;
    -webkit-animation: shadows infinite;
    animation-duration:22s;
    animation-delay:9s;
    background-size:100%;
    background-repeat: no-repeat;
  }

  .wrapper .fish5 {
    position:absolute;
    top:0%;
    left:-20%;
    width:140%;
    height:140%;

    background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
    opacity:0;
    border-radius: 10%;
    -webkit-animation: shadows infinite;
    animation-duration:22s;
    animation-delay:12s;
    background-size:100%;
    background-repeat: no-repeat;
  }

  .wrapper .fish6 {
    position:absolute;
    top:-10%;
    left:-20%;
    width:140%;
    height:150%;

    background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
    opacity:0;
    border-radius: 10%;
    -webkit-animation: shadows infinite;
    animation-duration:22s;
    animation-delay:15s;
    background-size:100%;
    background-repeat: no-repeat;
  }

  @keyframes shadows { 
    10% {opacity:.1;}

    15% {opacity:.07;background-size:100%}
    20% {background-size:101%}
   
    45% {opacity:0;}
    100% {opacity:0; left:10%; top:1%; background-size:110%}  }


    .wrapper .largefish {
      position:absolute;
      top:0%;
      left:0%;
      width:100%;
      height:100%;
  
      background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
      opacity:0;
  
      -webkit-animation: approaching infinite;
      animation-duration:40s;
      animation-delay:0s;
      background-size:200%;
      background-repeat: no-repeat;
    }

 

    .wrapper .largefish3 {
      position:absolute;
      top:0%;
      left:0%;
      width:100%;
      height:100%;
  
      background-image:url(//repo.bfw.wiki/bfwrepo/images/sea/fish.jpg);
      opacity:0;
  
      -webkit-animation: approaching infinite;
      animation-duration:40s;
      animation-delay:20s;
      background-size:200%;
      background-repeat: no-repeat;
    }


  @keyframes approaching { 
    50% {opacity:.2;left:0%;}
   
    
    100% {opacity:0; background-size:800%;}  }
</style>


</head>

<body>
  <div class="wrapper">
 

    <div class="fish"></div>
    <div class="fish2"></div>
    <div class="fish3"></div>
    <div class="fish4"></div>
    <div class="fish5"></div>
    <div class="fish6"></div>
    <div class="largefish"></div>
    <div class="largefish2"></div>
    <div class="largefish3"></div>
    <div class="seaColor"></div>
    <div class="choral"></div>
  </div>
<!-- partial:index.partial.html -->
<canvas id="canvas">
  
</canvas>


  
      <script  >
/*
  
Credit to Matthias Hurle who taught me how to make water using webgl

 */

/** @type {HTMLCanvasElement} */
const canvas = window.canvas;
const gl = canvas.getContext("webgl2");
const dpr = Math.max(1, .5 * window.devicePixelRatio);
/** @type {Map<string,PointerEvent>} */
const touches = new Map();

const vertexSource = `#version 300 es
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif

in vec2 position;

void main(void) {
    gl_Position = vec4(position, 0., 1.);
}
`;
const fragmentSource = `#version 300 es

#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif

out vec4 waterColor;

uniform vec2 resolution;
uniform float time;
uniform int pointerCount;
uniform vec2 touch;

const vec3 roomsize=vec3(6,5,54)*1.1;
const vec3 boxsize=vec3(0,0,0);
const float walleps=1e-2;

#define T time
#define S smoothstep
#define mouse (touch/resolution)
#define rot(a) mat2(cos(a),-sin(a),sin(a),cos(a))

float tick(float t,float e) {
    return floor(t)+pow(S(.0,1.,S(.0,1.,fract(t))),e);
}

float box(vec3 p,vec3 s,float r) {
    p=abs(p)-s;

    return length(max(p,.0))+
        min(.0,max(max(p.x,p.y),p.z))-r;
}

float mat=.0;
float map(vec3 p) {
    float d=1e5,
    room=-box(p,roomsize,.05),
    bx=box(p-vec3(0,-(roomsize.y-(boxsize.y+.05)),0),boxsize,.05);

    d=min(d, room);
    d=min(d, bx);

    if (d==bx) mat=1.;
    else mat=.0;

    return d;
}

vec3 water2(vec2 uv) {
    vec2
    n=vec2(0),
    q=vec2(0);

    uv*=.775;

    float
    d=dot(uv,uv),
    s=9.,
    a=.02,
    b=sin(T*4.4-d*90.)*.7,
    t=T*1.;

    mat2 m=mat2(.6,1.2,-1.2,.6);
    for (float i=.0; i<30.; i++) {
        n*=m;
        q=uv*s-t+b+i+n;
        a+=dot(sin(q)/s,vec2(.2));
        n-=sin(q);
        s*=1.2;
    }

    vec3 col=vec3(1,3,4)*(a+a)+a+a-d;
    col=exp(-col*8.);
    col=abs(col);
    col=sqrt(col);
    col=exp(-col*4.);

    return col;
}

vec3 water5(vec2 uv) {
    vec2
    n=vec2(0),
    q=vec2(0);

    uv*=.875;

    float
    d=dot(uv,uv),
    s=9.,
    a=.02,
    b=sin(T*4.4-d*90.)*.7,
    t=T*4.;

    mat2 m=mat2(.6,1.2.........完整代码请登录后点击上方下载按钮下载查看

网友评论0