three+webgl实现网格地平线日出日落交互动画效果代码

代码语言:html

所属分类:动画

代码描述:three+webgl实现网格地平线日出日落交互动画效果代码

代码标签: three webgl 网格 地平线 日出 日落 交互 动画

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

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

<head>

  <meta charset="UTF-8">

  
  
  
<style>
* {
    margin: 0;
    padding: 0;
  }
  
  html {
    height: 100%;
    background-color: #FFF; 
  }
  
  body {  
    margin: 0;
    height: 100%;
    font-family: 'Noto Sans JP', sans-serif;
    height: 10000px;
  }

  #myCanvas {
    overflow: hidden;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;  
  }
  #myCanvas canvas {  
    display: block;
    width: 100%;
    height: 100%;
  }
</style>



</head>

<body>
<!-- Threejs -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.127.js"></script>
  <!-- tweakpane -->
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/tweakpane.3.0.5.js"></script>

  <div id="myCanvas"></div>

  <script id="fragmentShader" type="x-shader/x-fragment">
    varying vec2 vUv;
    uniform vec2 u_resolution;
    uniform float u_time;
    uniform vec2 u_mouse;
    uniform float u_radius;
    uniform float u_scrollY;

    uniform float u_volume;//

    float random (in float x) {
      return fract(sin(x)*1e4);
    }
    float random (vec2 st) {
      return fract(sin(dot(st.xy,
                            vec2(12.9898,78.233)))*
          43758.5453123);
    }


    
    void line( vec2 a, vec2 l ) {
      l.x *= u_resolution.y/u_resolution.x;
      l += 0.5;
      l *= u_resolution;
      
      vec2 P = gl_FragCoord.xy;
      
      a.x *= u_resolution.y/u_resolution.x;
      a += 0.5;
      a *= u_resolution;
      
      vec2 aP = P-a;
      vec2 al = l-a;
      vec3 al3 = vec3(al, 0.0);
      vec3 aP3 = vec3(aP, 0.0);
 
      float q = length(cross(aP3,al3))/length(al3);
      
      float d = q;
      if ( dot(al, aP) <= 0.0 ) { // before start
          d = distance(P, a);
      }
        else if ( dot(al, al) <= dot(al, aP) ) { // after end
          d = distance(P, l);
      }
      float br = 0.001 * u_resolution.y;
      gl_FragColor.rgb += vec3(0.15, 0.15, 0.45) * br / d;
    }


    void light (vec2 uv) {
      float aspect = u_resolution.x / u_resolution.y;
      vec2 center = vec2( u_mouse.x, u_mouse.y );// 
      float radius = u_volume;
      float lightness = radius / length( uv - center );
      gl_FragColor.rgb += vec3( lightness );
    }
    
    void main() {

      vec2 imageResolution = vec2(1024.0, 1024.0);

      vec2 ratio = vec2(
        min((u_resolution.x / u_resolution.y) / (imageResolution.x / imageResolution.y), 1.0),
        min((u_resolution.y / u_resolution.x) / (imageResolution.y / imageResolution.x), 1.0)
      );

      vec2 uv = vec2(
          vUv.x * ratio.x + (1.0 - ratio.x) * 0.5,
          vUv.y * ratio.y + (1.0 - ratio.y) * 0.5
      );

      light(uv);

      // Horizontal grid lines
      float y = 0.0;
      for (int l=1; l<40; l++) {
        y = -1.0/( fract(u_time) + float(l)*1.2 );
        line(vec2(-2.0, y), vec2(2.0, y));
      }

      for (int l=-20; l<20; l++) {
        float x =  float(l);
        line(vec2(x * 0.01, y), vec2(x, -1.0));
      }

    }
  </script>
  
  <script id="vertexShader" type="x-shader/x-vertex">  
    varying vec2 vUv;
    void main() {
      vUv = uv;
      gl_Position = vec4( position, 1.0 );
    }
  </script>



  
      <script >

class Canvas {
  constructor() {

    this.scrollY = 0;

    this.mouse = new THREE.Vector2(0.5, 0.5);
    this.targetRadius = 0.001; // 
    //ウィンドウサイズ
    this.w = window.innerWidth;
    this.h = window.innerHeight;



    this.renderer = new THREE.WebGLRenderer({
      antialias: true });

    this.renderer.setSize(this.w, this.h); // 
    this.renderer.setPixelRatio(window.devi.........完整代码请登录后点击上方下载按钮下载查看

网友评论0