欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

会旋转的网格球

程序员文章站 2022-04-01 17:00:02
...

会旋转的网格球

示例

会旋转的网格球

HTML

<div id="canvas"></div>

CSS

html,
body {
  width: 80vw;
  height: 80vh;
  background: black;
}

canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
}

JS

let renderer, scene, camera, sphere;

window.onload = function() {
  init();
  animate(0, 1);
}

function init() {
  renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true
  }); 
  renderer.setPixelRatio((window.devicePixelRatio) ? window.devicePixelRatio : 1);
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.getElementById('canvas').appendChild(renderer.domElement);

  scene = new THREE.Scene();
 
  camera = new  THREE.PerspectiveCamera(25, window.innerWidth / window.innerHeight, 1, 1000);
  camera.position.z = 400;
  camera.position.x = 0;
  camera.position.y = 100;
  
  
  scene.add(camera);
  

  sphere = new THREE.Object3D();
  scene.add(sphere);
  sphere.position.y = -100;

  
  const geometry = new THREE.IcosahedronGeometry(10, 2);
  
  
  const structure =  new THREE.MeshPhongMaterial({
    color: 0xB0C4DE,
    wireframe: true,
    side: THREE.DoubleSide
  });
  const mesh = new THREE.Mesh(geometry, structure);
  mesh.scale.x = mesh.scale.y = mesh.scale.z = 25;
  sphere.add(mesh);
  const ambientLight = new THREE.AmbientLight(0xffffff);
  scene.add(ambientLight);
};
function animate(mousex, mousey) {
  sphere.rotation.z = 0;
  sphere.rotation.y = -mousex*.0005;
  sphere.rotation.x = -mousey*.0005;
  renderer.clear();
  renderer.render( scene, camera );
};
canvas.onmousemove = e => animate(e.x, e.y)
相关标签: # 几何图形