Three 学习日志(十)—— 常见几何体(长方体、球体、圆柱、矩形平面、圆形平面)
一、构建常用几何体
const geometry_list = []
// BoxGeometry:长方体
const geometry_box = new THREE.BoxGeometry(100, 100, 100);
geometry_list.push(geometry_box);
// SphereGeometry:球体
const geometry_sphere = new THREE.SphereGeometry(50);
geometry_list.push(geometry_sphere);
// CylinderGeometry:圆柱
const geometry_cylinder = new THREE.CylinderGeometry(50, 50, 100);
geometry_list.push(geometry_cylinder);
// PlaneGeometry:矩形平面
const geometry_plane = new THREE.PlaneGeometry(100, 50);
geometry_list.push(geometry_plane);
// CircleGeometry:圆形平面
const geometry_circle = new THREE.CircleGeometry(50);
geometry_list.push(geometry_circle);
//通用材质对象Material
const material = new THREE.MeshLambertMaterial({
color: 0x00ffff, // 设置材质颜色
transparent: true,// 开启透明
opacity: 0.5,// 设置透明度
side: THREE.DoubleSide, // 矩形平面、圆形平面默认只有正面可见,需设置side来达到两面可见的目的
});
二、 遍历加入场景中
for (const gIdx in geometry_list) {
const mesh = new THREE.Mesh(geometry_list[gIdx], material); //网格模型对象Mesh
mesh.position.set(gIdx * 150, 0, 0);
scene.add(mesh);
}
三、效果展示
文章来源地址https://www.toymoban.com/news/detail-731678.html
四、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Learn Three</title>
<!-- 引入three,下载地址参考:http://www.webgl3d.cn/pages/aac9ab/#%E7%89%B9%E5%AE%9A%E7%89%88%E6%9C%ACthree-js%E6%96%87%E4%BB%B6%E5%8C%85%E4%B8%8B%E8%BD%BD -->
<script src="../build/three.js"></script>
<!-- 引入相机控件 -->
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "../examples/jsm/"
}
}
</script>
</head>
<body>
<script type="module">
// 引入轨道控制器扩展库OrbitControls.js
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
// 创建3D场景对象Scene
const scene = new THREE.Scene();
const axesHelper = new THREE.AxesHelper(150);
scene.add(axesHelper);
const geometry_list = []
// BoxGeometry:长方体
const geometry_box = new THREE.BoxGeometry(100, 100, 100);
geometry_list.push(geometry_box);
// SphereGeometry:球体
const geometry_sphere = new THREE.SphereGeometry(50);
geometry_list.push(geometry_sphere);
// CylinderGeometry:圆柱
const geometry_cylinder = new THREE.CylinderGeometry(50, 50, 100);
geometry_list.push(geometry_cylinder);
// PlaneGeometry:矩形平面
const geometry_plane = new THREE.PlaneGeometry(100, 50);
geometry_list.push(geometry_plane);
// CircleGeometry:圆形平面
const geometry_circle = new THREE.CircleGeometry(50);
geometry_list.push(geometry_circle);
//材质对象Material
const material = new THREE.MeshLambertMaterial({
color: 0x00ffff, // 设置材质颜色
transparent: true,// 开启透明
opacity: 0.5,// 设置透明度
side: THREE.DoubleSide, // 矩形平面、圆形平面默认只有正面可见,需设置side来达到两面可见的目的
});
for (const gIdx in geometry_list) {
const mesh = new THREE.Mesh(geometry_list[gIdx], material); //网格模型对象Mesh
mesh.position.set(gIdx * 150, 0, 0);
scene.add(mesh);
}
const ambient = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambient);
const camera = new THREE.PerspectiveCamera();
camera.position.set(800, 800, 800);
camera.lookAt(200, 0, 0);
const width = window.innerWidth; // 窗口宽度
const height = window.innerHeight; // 窗口高度
const renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
renderer.render(scene, camera); //执行渲染操作
document.body.appendChild(renderer.domElement);
// 设置相机控件轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 如果OrbitControls改变了相机参数,重新调用渲染器渲染三维场景
controls.addEventListener('change', function () {
renderer.render(scene, camera); //执行渲染操作
});//监听鼠标、键盘事件
</script>
</body>
<style>
body {
overflow: hidden;
margin: 0px;
}
</style>
</html>
文章来源:https://www.toymoban.com/news/detail-731678.html
到了这里,关于【前端知识】Three 学习日志(十)—— 常见几何体(长方体、球体、圆柱、矩形平面、圆形平面)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!