• 【前端知识】Three 学习日志(九)—— 阵列立方体和相机适配体验


    Three 学习日志(九)—— 阵列立方体和相机适配体验

    一、双层for循环创建阵列模型
    //创建一个长方体几何对象Geometry
    const geometry = new THREE.BoxGeometry(100, 100, 100);
    //材质对象Material
    const material = new THREE.MeshLambertMaterial({
        color: 0x00ffff, //设置材质颜色
        transparent: true,//开启透明
        opacity: 0.5,//设置透明度
    });
    for (let i = 0; i < 10; i++) {
        for (let j = 0; j < 10; j++) {
            const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
            // 在XOZ平面上分布
            mesh.position.set(i * 200, 0, j * 200);
            scene.add(mesh); //网格模型添加到场景中  
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    二、调整相机位置,改变观察范围
    const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
    //在原来相机位置基础上拉远,可以观察到更大的范围
     camera.position.set(800, 800, 800);
     camera.lookAt(0, 0, 0);
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    三、超出视锥体远裁界面的范围的会被剪裁掉,不渲染,可以调整far参数适配
    const camera = new THREE.PerspectiveCamera(30, width / height, 1, 8000);
    camera.position.set(2000, 2000, 2000);
    camera.lookAt(0, 0, 0);
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    四、改变相机观测点
    // 改变相机观察目标点
    camera.lookAt(1000, 0, 1000);
    // 设置相机控件轨道控制器OrbitControls
    const controls = new OrbitControls(camera, renderer.domElement);
    // 相机控件.target属性在OrbitControls.js内部表示相机目标观察点,默认0,0,0
    // console.log('controls.target', controls.target);
    controls.target.set(1000, 0, 1000);
    controls.update();//update()函数内会执行camera.lookAt(controls.targe)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    五、完整代码
    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);
    
            //创建一个长方体几何对象Geometry
            const geometry = new THREE.BoxGeometry(100, 100, 100);
            //材质对象Material
            const material = new THREE.MeshLambertMaterial({
                color: 0x00ffff, //设置材质颜色
                transparent: true,//开启透明
                opacity: 0.5,//设置透明度
            });
            for (let i = 0; i < 10; i++) {
                for (let j = 0; j < 10; j++) {
                    const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
                    // 在XOZ平面上分布
                    mesh.position.set(i * 200, 0, j * 200);
                    scene.add(mesh); //网格模型添加到场景中  
                }
            }
    
            // //环境光强度调整为0.8
            const ambient = new THREE.AmbientLight(0xffffff, 0.4);
            scene.add(ambient);
    
            const width = window.innerWidth; // 窗口宽度
            const height = window.innerHeight; // 窗口高度
            // const camera = new THREE.PerspectiveCamera();
            // camera.position.set(200, 200, 200);
            // camera.lookAt(0, 0, 0);
            // const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
            // //在原来相机位置基础上拉远,可以观察到更大的范围
            // camera.position.set(800, 800, 800);
            // camera.lookAt(0, 0, 0);
    
            // const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
            const camera = new THREE.PerspectiveCamera(30, width / height, 1, 8000);
            // camera.position.set(292, 223, 185);
            // 超出视锥体远裁界面的范围的会被剪裁掉,不渲染,可以调整far参数适配
            camera.position.set(2000, 2000, 2000);
            camera.lookAt(0, 0, 0);
    
            // // 改变相机观察目标点
            camera.lookAt(1000, 0, 1000);
    
            const renderer = new THREE.WebGLRenderer();
            renderer.setSize(width, height);
            renderer.render(scene, camera); //执行渲染操作
            document.body.appendChild(renderer.domElement);
    
            // 设置相机控件轨道控制器OrbitControls
            const controls = new OrbitControls(camera, renderer.domElement);
            // 相机控件.target属性在OrbitControls.js内部表示相机目标观察点,默认0,0,0
            // console.log('controls.target', controls.target);
            controls.target.set(1000, 0, 1000);
            controls.update();//update()函数内会执行camera.lookAt(controls.targe)
        script>
    body>
    <style>
        body {
            overflow: hidden;
            margin: 0px;
        }
    style>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
  • 相关阅读:
    spring boot 分布式session实现
    【Java】线程池技术(三)ThreadPoolExecutor 状态与运行源码解析
    如何实现生产质量精细化管理?
    unity NPR 卡通渲染
    深入理解MySQL:数据类型、查询优化、索引、事务处理和数据备份与恢复
    AI-Prompt 1.0 版简介&公测!你的AI提示词网站!
    33张Java高级进阶技术思维导图,白嫖大佬梳理的技术要点!只需看重点,学习效率提升300%(建议收藏)
    poj 2182 Lost Cows (树状数组 || 线段树)
    LeetCode 1822. 数组元素积的符号
    深度学习环境搭建——之Anaconda3安装配置
  • 原文地址:https://blog.csdn.net/weixin_42919342/article/details/133020440