three实现水下波动动画效果

代码语言:html

所属分类:动画

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

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<title> Water effect shader</title>
<style>
    body{
  margin: 0;
  padding: 0;
}

canvas{
  display: block;
}

  </style>

</head>
<body translate="no">
<div class="content-canvas"></div>
<script id="vertex-shader" type="x-shader/x-vertex">
  varying vec2 vUv;
  
  void main(){  
    vUv = uv; 
    //modelViewMatrix: es la posición y orientación de la cámara dentro de la escena
    //projectionMatrix: la proyección para la escena de la cámara incluyendo el campo de visión
    vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0);
    gl_Position = projectionMatrix * modelViewPosition;
  }
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
  uniform float time;
  uniform vec2 resolution;
  uniform sampler2D texture1;
  
  varying vec2 vUv;
  
  void main() {  
    vec2 uv1 = vUv;
    // variable que contiene el eje de coordenadas
    vec2 uv = gl_FragCoord.xy/resolution.xy;
    
    float frequency = 15.;
    float amplitude = 0.015;
    
    float x = uv1.y * frequency + time * .7; 
    float y = uv1.x * frequency + time * .3;
    
    uv1.x += cos(x+y) * amplitude * cos(y);
    uv1.y += sin(x-y) * amplitude * cos(y);

    vec4 rgba = texture2D(texture1, uv1);
    gl_FragColor = rgba;
  }
</script>

<script src='http://repo.bfw.wiki/bfwrepo/js/three.js'></script>
<script>
      const init = () => {
  const content = document.querySelector(".content-canvas");
  const s = {
    w: innerWidth,
    h: innerHeight };


  const gl = {
    renderer: new THREE.WebGLRenderer({ antialias: true }),
    camera: new THREE.PerspectiveCamera(75, s.w / s.h, 0.1, 100),
    scene: new THREE.Scene(),
    loader: new THREE.TextureLoader() };


  let time = 0;

  const .........完整代码请登录后点击上方下载按钮下载查看

网友评论0