three实现三维立体多边形线框旋转动画效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维立体多边形线框旋转动画效果代码,拖动可三维旋转。

代码标签: three 三维 立体 多边形 线框 旋转 动画

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

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

<head>
  <meta charset="UTF-8">
  

  
  
  
<style>
body{
  overflow: hidden;
  margin: 0;
  background-color: black;
}

canvas {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>


  
</head>

<body translate="no">
  <script type="importmap">
  {
    "imports": {
      "three": "//repo.bfw.wiki/bfwrepo/js/module/three/build/163/three.module.js",
      "three/addons/": "//repo.bfw.wiki/bfwrepo/js/module/three/examples/163/jsm/"
    }
  }
</script>


<canvas id="cnv"></canvas>
  
      <script type="module">
import { Scene, PerspectiveCamera, Object3D, Vector3, Euler, Color, Clock, MathUtils } from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";

console.clear();
let mu = MathUtils;

let c = new Color();
let palettes = [
["#FC1FF9", "#BC0EEF", "#443061", "#E42536", "#FC5E31"], //Futurism
["#00F891", "#00D61C", "#021307", "#0346F4", "#05E9E7"], // Radium Bloom
["#FF5BFF", "#D34DEE", "#BF4BF8", "#2AC9F9", "#56F1FF"], // Industrial Glow
["#FE02FF", "#DF21FF", "#8407CE", "#5F03BD", "#2F0049"], // Purple Palm
["#B6FFFE", "#48FDFE", "#F20BF8", "#150390", "#1E0C2D"] // Blacklight
];
let palette = palettes[0];

cnv.offscreenCanvas = document.createElement("canvas");
let offCtx = cnv.offscreenCanvas.getContext("2d");

let u = 0;
let unit = val => u * val;
let resize = () => {
  cnv.height = innerHeight * 0.95;
  cnv.width = innerHeight * 0.95;
  cnv.offscreenCanvas.height = cnv.height;
  cnv.offscreenCanvas.width = cnv.width;
  u = cnv.height * 0.01;

  cnv.style.border = `${unit(1)}px solid ${palette[3]}`;
  cnv.style.borderRadius = `${unit(27.2)}px`;

  drawHole(offCtx);
};
window.addEventListener("resize", resize);
resize();

cnv.addEventListener("dblclick", event => {
  let bcr = event.target.getBoundingClientRect();
  let y = event.clientY - bcr.top;
  let palIdx = Math.floor(y / unit(1) / (100 / palettes.length)); // depends on Y-coord of the click
  palette = palettes[palIdx];
  drawHole(offCtx);
  resize();
  setRingColors();
});

let ctx = cnv.getContext("2d");

let ringsCount = 25;
let rings = Array.from({ length: ringsCount }, (_, idx) => {
  let size = 90 - idx;
  let hSize = size * 0.5;
  let color = palette[mu.randInt(0, palette.length - 1)];
  c.set(color).multiplyScalar(256);
  let a = (1 - idx / (ringsCount - 1)) * 0.875;
  return {
    size: size,
    hSize: hSize,
    radii: Array.from({ length: 4 }, () => Math.random()),
    radiiUnit: new Array(4).fill(0),
    rotation: Math.PI * (0.1 + Math.random() * 0.4) * Math.sign(Math.random() - 0.5) * 0.5,
    rotationInit: Math.PI * 2 * Math.random(),
    color: `rgba(${c.r}, ${c.g}, ${c.b}, ${a})` };

});

class Tetrahedron extends Object3D {
  constructor() {
    super();
    this.vertices = [// https://en.wikipedia.org/wiki/Tetrahedron#Coordinates_for_a_regular_tetrahedron
    new Vector3(Math.sqrt(8 / 9), 0, -(1 / 3)),
    new Vector3(-Math.sqrt(2 / 9), Math.sqrt(2 / 3), -(1 / 3)),
    new Vector3(-Math.sqrt(2 / 9), -Math.sqrt(2 / 3), -(1 / 3)),
    new Vector3(0, 0, 1)];

    this.index = [0, 1, 0, 2, 0, 3, 1, 2, 2, 3, 3, 1];
    this.userData = {
      color: 2,
      rotInit: new Euler().setFromVector3(new Vector3().random().multiplyScalar(Math.PI * 2)),
      rotSpeed: new Euler().setFromVector3(new Vector3().random().multiplyScalar((0.2 + Math.random() * 0.2) * Math.PI * Math.sign(Math.random() - 0.5))),
      position: new Vector3().random().subScalar(0.5).multiplyScalar(10) };

    this.position.copy(this.userData.position);
    th.........完整代码请登录后点击上方下载按钮下载查看

网友评论0