three+webgl实现一个液态球体旋转动画效果代码

代码语言:html

所属分类:三维

代码描述:three+webgl实现一个液态球体旋转动画效果代码

代码标签: 液态 球体 旋转 动画 效果

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

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

<head>

  <meta charset="UTF-8">
  

  
  
<style>
body {
  background: #000;
  -ms-scroll-chaining: none;
      overscroll-behavior: none;
}
#loading {
  background: #000;
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
  visibility: visible;
  opacity: 1;
  transition: visibility 1.6s, opacity 1.6s;
}
#loading .circle {
  width: 50px;
  height: 50px;
  background: #fff;
  border-radius: 50%;
  opacity: 0;
  transform: scale(0, 0);
  -webkit-animation: circle-animation 1.6s ease-in-out 0s infinite normal none;
          animation: circle-animation 1.6s ease-in-out 0s infinite normal none;
}
#loading.loaded {
  visibility: hidden;
  opacity: 0;
}
#container {
  width: 100vw;
  height: 100vh;
}
#container canvas {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 0;
  outline: none;
}
/** css animation */
@-webkit-keyframes circle-animation {
  0% {
    opacity: 0;
    transform: scale(0, 0);
  }
  50% {
    opacity: 1;
    transform: scale(1, 1);
  }
}
@keyframes circle-animation {
  0% {
    opacity: 0;
    transform: scale(0, 0);
  }
  50% {
    opacity: 1;
    transform: scale(1, 1);
  }
}
</style>



</head>

<body >
  <div id="loading">
  <div class="circle"></div>
</div>
<div id="container"></div>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/Stats-16.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.126.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OrbitControls.126.js"></script>

      <script >
/**
 * I referenced the YouTube by Mr.Yuri Artyukh.
 * URL: https://www.youtube.com/watch?v=8K5wJeVgjrM&t=615s
 * CDN: from https://cdn.jsdelivr.net
 * Thank you so much :)
 */



/** vertex shader source */
const vertexShaderSource = `
uniform float time;
uniform vec2 pixels;

varying vec2 vUv;

float PI = 3.14159265359;

void main () {
  vUv = uv;
  gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

`;

/** fragment shader source */
const fragmentShaderSource = `
uniform float time;
uniform float progress;
uniform sampler2D texture1;
uniform vec4 resolution;
uniform vec2 mouse;
uniform float wheel;

varying vec2 vUv;

vec3 palette( in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d ) {
  return a + b*cos( 6.28318*(c*t+d) );
}

mat4 rotationMatrix(vec3 axis, float angle) {
  axis = normalize(axis);
  float s = sin(angle);
  float c = cos(angle);
  float oc = 1.0 - c;

  return
    mat4(
      oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
      oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
      oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
      0.0, 0.0, 0.0, 1.0
    );
}

vec3 rotate(vec3 v, vec3 axis, float angle) {
	mat4 m = rotationMatrix(axis, angle);
	return (m * vec4(v, 1.0)).xyz;
}

float sineCrazy(vec3 p) {
  return 1.0 - (sin(p.x) + sin(p.y) + sin(p.z)) / 3.0;
}

float sphere(vec3 p) {
  return length(p) - 0.5;
}

float sdBox(vec3 p, vec3 b) {
  vec3 q = abs(p) - b;
  return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);
}

float scene(vec3 p) {
  //vec3 pos = mod(p, 4.0) - 2.0; 
  vec3 p1 = rotate(p, vec3(1.0, 1.0, 1.0), time * 4.0);
  float scale = 15.0 + 10.0 * sin(time * 2.0);
  return max(sphere(p1), (0.85 - sineCrazy(p1 * scale)) / scale);
  //return max(sdBox(p1, vec3(0.5)), (0.85 - sineCrazy(p1 * scale)) / scale);
}

vec3 getNormal(vec3 p) {
  vec2 o = vec2(0.001, 0.0);
  return normalize(
    vec3(
      scene(p + o.xyy) - scene(p - o.xyy),
      scene(p + o.yxy) - scene(p - o.yxy),
      scene(p + o.yyx) - scene(p - o.yyx)
    )
  );
}

void main () {
  vec2 newUV = (vUv - vec2(0.5)) * resolution.zw + vec2(0.5);
  vec2 p = newUV - vec2(0.5);
  
  vec3 camPos = vec3(0.0, 0.0, wheel);
  vec3 ray = normalize(vec3(p, -1.0));
  vec3 rayPos = camPos;
  vec3 light = vec3(0.0, 0.0, 1.0);
  
  float curDist = 0.0;
  float rayLen = 0.0;
  
  vec3 color = vec3(0.0);
  
  for (int i = 0; i < 32; i++) {
    curDist = scene(rayPos);
    rayLen += 0.6 * curDist;
    rayPos = camPos + ray * rayLen;
    
    if (abs(curDist) < 0.001) {
      vec3 n = getNormal(rayPos);
      float diff = dot(n, light);
      color =
        palette(
          diff + time * 2.0,
          vec3(0.5, 0.5, 0.5),
          vec3(0.5, 0.5, 0.5),
          vec3(2.0, 1.0, 0.0),
          vec3(0.5, 0.20, 0.25)
        );
      break;
    }
  }
  
  gl_FragColor = vec4(color, 1.0);
  //gl_FragColor.r -= abs(mouse.x) * 0.5;
  //gl_FragColor.g -= abs(mouse.y) * 0.5;
  //gl_FragColor.b -= abs(length(mouse)) * 0.5;
}

`;

/**
 * class Sketch
 */
class Sketch {
  constructor() {
    this.stats = null;
    this.width = null;
    this.height = null;

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

网友评论0