Three.js中文网 Three.js中文网
首页
免费视频
系统课 (opens new window)
  • Three.js基础课程
  • Vue3+Threejs 3D可视化
  • Threejs进阶课程
  • 展厅3D预览漫游
  • Threejs Shader
  • Blender建模基础
  • Three.js基础课程(旧版本) (opens new window)
  • 文章
WebGPU教程
  • WebGL教程
  • WebGL教程(旧版本) (opens new window)
3D案例
  • 本站部署(打开快) (opens new window)
  • 原英文官网文档 (opens new window)
首页
免费视频
系统课 (opens new window)
  • Three.js基础课程
  • Vue3+Threejs 3D可视化
  • Threejs进阶课程
  • 展厅3D预览漫游
  • Threejs Shader
  • Blender建模基础
  • Three.js基础课程(旧版本) (opens new window)
  • 文章
WebGPU教程
  • WebGL教程
  • WebGL教程(旧版本) (opens new window)
3D案例
  • 本站部署(打开快) (opens new window)
  • 原英文官网文档 (opens new window)
Web3D系统课程视频
  • 0.学前说明

  • 1.Vue+Three.js基础

  • 2.场景搭建Three+Blender

  • 3.渲染效果提升(材质光照)

  • 4.渲染性能、模型压缩

  • 5.封装、进度条、切视角

    • 1. threejs代码封装
    • 2. threejs代码封装2
    • 3. threejs封装3-辅助工具
    • js 3D可视化
    • 5.封装、进度条、切视角
    郭隆邦
    2024-03-23
    目录

    3. threejs封装3-辅助工具

    # threejs代码封装3-辅助工具

    继续threejs代码封装的讲解。

    前面讲解过,平时开发的时候,会通过辅助坐标系AxesHelper观察xyz轴,stats查看渲染帧率,gui可视化调节参数。

    下面就把前面写过的代码,封装成一个类。

    # 封装辅助工具Helper.js

    在src/twin目录下创建一个文件Helper.js。

    把原来代码复制过来修改下即可。

    import * as THREE from 'three';
    import Stats from 'three/examples/jsm/libs/stats.module.js'
    import { GUI } from 'three/examples/jsm/libs/lil-gui.module.min.js'
    
    export default class Helper {
        constructor(scene,directionalLight) {
            scene.add(new THREE.AxesHelper(200));
    
            // 查看帧率
            this.stats = new Stats();
            document.body.appendChild(this.stats.domElement)
            // 辅助调试
            this.gui = new GUI()
            this.gui.domElement.style.right = '0px';
            this.gui.domElement.style.width = '500px';
            this.gui.domElement.style.fontSize = '16px';
            // 平行光调试
            const dirFolder = this.gui.addFolder('平行光');//创建一个子菜单
            dirFolder.add(directionalLight, 'intensity', 0, 5)
            const R = 100;
            dirFolder.add({ angle: 45 }, 'angle', 0, 360).onChange(function (v) {
                const rad = THREE.MathUtils.degToRad(v);
                directionalLight.position.x = R * Math.cos(rad);
                directionalLight.position.z = R * Math.sin(rad);
            }).name('旋转角度')
    
            dirFolder.add({ angle: 45 }, 'angle', 0, 89).onChange(function (v) {
                const rad = THREE.MathUtils.degToRad(v);
                directionalLight.position.y = R * Math.tan(rad);
            }).name('照射角度');
    
            dirFolder.close();
        }
    }
    

    # Helper.js引入CreateTwin.js

    import Helper from './Helper';
    
    class CreateTwin {
      constructor(params = {}) {
        const { 
          logPosTargetBool = false,
          helperBool = true,//默认创建辅助开发工具
        } = params;
        if(helperBool){
          this.helper = new Helper(this.scene, this.directionalLight);
          this.gui = this.helper.gui;       
        }
      }
    }
    // 渲染循环
    this.renderer.setAnimationLoop(() => {
        if (helperBool) this.helper.stats.update();
        this.renderer.render(this.scene, this.camera);
    })
    

    类CreateTwin实例化时候,可以配置参数,是否生成坐标轴、帧率界面、gui界面

    const twin = new CreateTwin({
        helperBool:false,//不添加调试辅助工具
        //logPosTargetBool:true,//打印相机参数
    });
    

    在其他vue组建中获取gui对象,进行设置。

    <script setup>
    import twin from './twin';
    
    twin.loader.load('./收费站.glb', (gltf) => {
      ...
      if (twin.gui) {
        twin.gui.add({ envMapIntensity: 2.0 }, 'envMapIntensity', 0, 5).onChange(function (v) {
          gltf.scene.traverse(function (obj) {
            if (obj.isMesh) {
                obj.material.envMapIntensity = v
            }
          })
        })
      }
    })
    </script>
    
    2. threejs代码封装2

    ← 2. threejs代码封装2

    Theme by Vdoing | Copyright © 2016-2025 豫ICP备16004767号-2
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式