会旋转的网格球
程序员文章站
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)
上一篇: yarn与npm的命令行小结
下一篇: sql server全文索引的性能问题