three实现三维螺旋运动动画

代码语言:html

所属分类:三维

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

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

<style>
body {
  margin: 0;
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
}
</style>

</head>
<body translate="no">
<div id="demo"></div>
<script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/three.js"></script>
<script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/dat.gui-min.js"></script>
<script type="text/javascript" src="http://repo.bfw.wiki/bfwrepo/js/OrbitControls.js"></script>

<script>
class Options {
  constructor() {
    this.face1Color = "#F7B2B7";
    this.face2Color = "#F7717D";
    this.face3Color = "#DE639A";
    this.face4Color = "#7F2982";
    this.face5Color = "#FF00FF";
    this.face6Color = "#FFFFFF";
    this.radius = 200;
    this.width = 150;
    this.height = 10;
    this.depth = 150;
    this.count = 50;
    this.twistAngle = 540;
    this.camerDamping = true;
  }}

var options = new Options();
var items = [];

var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var aspect = SCREEN_WIDTH / SCREEN_HEIGHT;

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(45, aspect, 1, 2000);

var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 0.075;
controls.screenSpacePanning = true;
controls.autoRotate = true;
controls.autoRotateSpeed = 0.2;
controls.update();

camera.position.x = 0;
camera.position.y = 0;
camera.position.z = -1000;
camera.lookAt(scene.position);
camera.updateMatrixWorld();

var light = new THREE.AmbientLight(0x404040);
scene.add(light);

var hemlight = new THREE.HemisphereLight(0xffffbb, 0x080820, 1);
scene.add(hemlight);

var geometry;

let boxConfig = {
  color: 0xffffff,
  vertexColors: THREE.FaceColors };


var material = new THREE.MeshPhongMaterial(boxConfig);

const createCubes = () => {
  items = [];
  let count = options.count;
  let i = 0;
  let rad = options.radius;
  for (i; i < count; i++) {
    var cube = new THREE.Mesh(geometry, material);
    cube.position.x = rad * Math.cos(2 * Math.PI / (count / i));
    cube.position.y = rad * Math.sin(2 * Math.PI / (count / i));
    cube.rotateZ(2 * Math.PI / (count / i));
    cube.rotateY(THREE.Math.degToRad(options.twistAngle) / (count / i));
    scene.add(cube);
    items.push(cube);
  }
};
const clearCubes = () => {
  let count = items.length;
  let i = 0;
  for (i; i < count; i++) {
    scene.remove(items[i]);
  }
};
const updateItems = () => {
  let count = options.count;
  let i = 0;
  let rad = options.radius;
  for (i; i < count; i++) {
    items[i].rotateY(Math.PI / 2 / 250);
  }
  controls.update();
};
const createGeometry = () => {
  geometry = new THREE.BoxGeometry(options.width, options.height, options.depth);
  geometry.faces[0].color.set(options.face1Color);
  geometry.f.........完整代码请登录后点击上方下载按钮下载查看

网友评论0