当前位置: 首页 > news >正文

1️⃣5️⃣three.js_GUI辅助调试器

15、GUI辅助调试器

  • 3D虚拟工厂在线体验

  • GUI辅助调试器将原本需要修改代码调整参数并刷新页面的操作,简化为直接在GUI中实时调整,实现所见即所得的效果。

  • 导入GUI 库

    //引入GUI辅助调试器
    import { GUI } from 'three/addons/libs/lil-gui.module.min.js'
    
  • 创建GUI辅助调试器对象

    const gui = new GUI() // 实例化一个gui对象
    gui.domElement.style.right = '0px' //靠右
    gui.domElement.style.width = '300px' //宽度
    
  • 修改 JavaScript 对象tempPosition的参数

     const tempPosition = {x: 30,y: 60,z: 50,};// gui界面上增加交互界面,改变tempPosition对应属性gui.add(tempPosition, 'x', 0, 100);gui.add(tempPosition, 'y', 0, 100);gui.add(tempPosition, 'z', 0, 100);
    

    ✅执行结果:
    请添加图片描述

  • 当前修改的tempPosition作为中间变量的方式会同步更新项目中多处引用,这种间接操作不够直观。

  • 我们将改为直接绑定大树。

        //先定位大树1,并克隆、移动、加入场景const tempScene = scene.children.find((t) => t.name == 'Scene')const tempTree = tempScene.children.find((t) => t.name == '树')const tempBigTree = tempTree.children.find((t) => t.name == '大树1')const newTree = tempBigTree.clone()newTree.translateX(100)newTree.translateZ(50)tempTree.add(newTree)// 位置分组const positionFolder = gui.addFolder('位置');positionFolder.add(newTree.position, 'x', -500, 500).name('X坐标');positionFolder.add(newTree.position, 'y', -10, 100).name('Y坐标');positionFolder.add(newTree.position, 'z', -100, 100).name('Z坐标');positionFolder.open(); // 默认展开// 缩放分组const scaleFolder = gui.addFolder('缩放');scaleFolder.add(newTree.scale, 'x', 0.1, 3).name('X轴').step(0.1);scaleFolder.add(newTree.scale, 'y', 0.1, 3).name('Y轴').step(0.1);scaleFolder.add(newTree.scale, 'z', 0.1, 3).name('Z轴').step(0.1);// 颜色控制const colorParams = {color: `#${newTree.children[0].material.color.getHexString()}`};const appearanceFolder = gui.addFolder('外观');appearanceFolder.addColor(colorParams, 'color').name('树叶颜色').onChange((value) => {newTree.children[0].material.color.set(value);});appearanceFolder.add(newTree.children[0].material, 'wireframe').name('线框模式');appearanceFolder.add(newTree.children[0].material, 'transparent').name('透明');appearanceFolder.add(newTree.children[0].material, 'opacity', 0, 1).name('透明度').step(0.1);// 旋转控制const rotationFolder = gui.addFolder('旋转');rotationFolder.add(newTree.rotation, 'y', 0, Math.PI * 2).name('Y轴旋转').step(0.1);// 添加重置按钮const resetControls = {reset: function() {newTree.position.set(0, 0, 0);newTree.scale.set(1, 1, 1);newTree.rotation.set(0, 0, 0);newTree.children[0].material.color.set(0x00ff00); // 默认绿色 gui.updateDisplay();// 更新 GUI 控件}};gui.add(resetControls, 'reset').name('重置所有设置');
    

    ✅执行结果:

    请添加图片描述

  • 这里将参数分为四组:位置、缩放、颜色和旋转。每组包含一个或多个控制,调整这些控制时,大树的形态会实时更新,无需修改代码,直接在 GUI 中即可完成调整。

  • 点击【专栏目录】查看专栏其他内容。

相关文章:

  • Qt进阶开发:鼠标及键盘事件
  • AOSP Android14 Launcher3——RectFSpringAnim窗口动画类详解
  • ComfyUI+Sonic实战,三步实现图片开口说话
  • 单个或批量实现-提取PDF文档中的合同号和姓名并按“合同号_姓名”格式重命名文件。
  • 【文献分享】Model-based evaluation提供了数据和代码
  • day48—双指针-通过删除字母匹配到字典最长单词(LeetCode-524)
  • rk3568main.cc解析
  • 多路转接select服务器
  • Node.js简介(nvm使用)
  • docker-compose搭建kafka
  • Git Flow分支模型
  • L2-2、示范教学与角色扮演:激发模型“模仿力“与“人格“
  • 从单模态到多模态:深度生成模型的演进历程
  • 【武汉理工大学第四届ACM校赛】copy
  • EAL4+与等保2.0:解读中国网络安全双标准
  • 用 Go 优雅地清理 HTML 并抵御 XSS——Bluemonday
  • 嵌入式---超声波测距模块
  • 时间模块 demo
  • 小白学习java第14天(上):数据库
  • 【目标检测】对YOLO系列发展的简单理解
  • “很多中国企业竞争力独一无二”,这场对接会上他频频为协同供应链点赞
  • 大家聊中国式现代化|刘亮:因地制宜发展新质生产力,推动经济高质量发展
  • 海南公布知识产权保护典型案例,一企业违规申请注册“中华”商标被处罚
  • 郑州卫健委通报郑飞医院“血液净化”问题:拟撤销该院血液净化技术备案
  • 18条举措!上海国际金融中心进一步提升跨境金融服务便利化
  • 中方决定对在涉港问题上表现恶劣的美国国会议员等实施制裁