【前端知识】Three 学习日志(十)—— 常见几何体(长方体、球体、圆柱、矩形平面、圆形平面)

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);
}
三、效果展示

【前端知识】Three 学习日志(十)—— 常见几何体(长方体、球体、圆柱、矩形平面、圆形平面)_第1张图片

四、完整代码
DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Learn Threetitle>
    
    <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>

你可能感兴趣的:(前端知识,Three.js,前端)