• 【前端知识】Three 学习日志(四)—— 相机控件


    Three 学习日志(四)—— 相机控件

    一、引入相机控件
    
    <script type="importmap">
            {
                "imports": {
                    "three": "../build/three.module.js",
                    "three/addons/": "../examples/jsm/"
                }
            }
    script>
    
    <script type="module">
        // 引入轨道控制器扩展库OrbitControls.js
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    二、开启相机控件监听
    // 设置相机控件轨道控制器
    const controls = new OrbitControls(camera, renderer.domElement);
    // 如果OrbitControls改变了相机参数,重新调用渲染器渲染三维场景
    controls.addEventListener('change', function () {
    renderer.render(scene, camera); //执行渲染操作
    });//监听鼠标、键盘事件
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    三、效果展示

    在这里插入图片描述

    四、完整代码
    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 = new THREE.BoxGeometry(100, 100, 100);
            const material = new THREE.MeshBasicMaterial({
                color: 0xff0000, //设置材质颜色
                transparent: true,//开启透明
                opacity: 0.5,//设置透明度
            });
            const mesh = new THREE.Mesh(geometry, material); //网格模型对象Mesh
            mesh.position.set(0, 0, 0);
            scene.add(mesh);
            const camera = new THREE.PerspectiveCamera();
            camera.position.set(200, 200, 200);
            camera.lookAt(0, 0, 0);
            const width = 800; //宽度
            const height = 500; //高度
            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>
    
    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
  • 相关阅读:
    【Rust日报】2022-06-25 世界上最大的软件项目的内存安全
    13 个前端可能用得上的 CSS技巧
    python线程类改变类变量
    Python环境配置及基础用法&Pycharm库安装与背景设置及避免Venv文件夹
    阿里业务平台技术质量部——测试开发面试
    【黄啊码】MySQL入门—3、我用select *,老板直接赶我坐火车回家去,买的还是站票
    QT给QLabel设置背景颜色
    一个高精度24位ADC芯片ADS1222的使用方法及参考电路程序成都控制器定制
    第二篇,Stm32f407芯片GPIO编程,寄存器操作,库函数操作和位段操作
    排序之插入排序
  • 原文地址:https://blog.csdn.net/weixin_42919342/article/details/132985371