06 Three使用dat.gui——图形UI界面

5,278 阅读3分钟

1. dat.gui入门

Dat.gui 是一个 GUI 组件,可以为你的 demo 提供参数的设置

1.1 下载dat.gui

dat.gui - npm (npmjs.com)

npm i dat.gui @types/dat.gui --save --registry=https://registry.npm.taobao.org

image.png

1.2 引入和实例化GUI工具

import * as dat from 'dat.gui'
const gui = new dat.GUI();

//import { GUI } from "dat.gui"
//const gui = new GUI()

image.png

image.png

new GUI([params]) 
.domElement : DOMElement //gui实例的dom节点 
.parent : dat.gui.GUI // 我们创建new dat.GUI({parent:other GUI}),如果有嵌套在别的GUI里,这里可以获取父gui的实例 
.autoPlace : Boolean 
.closeOnTop : Boolean //关闭面板按钮的位置是否在上面 
.preset : String 
.width : Number //GUI面板宽度 
.name : String //GUI的名字 
.closed : Boolean //GUI是否关闭 
.load : Object 
.useLocalStorage : Boolean 
.add(object, property, [min], [max], [step]) ⇒ Controller //在GUI面板上添加操作的 
/*  @object 要操作的对象 
    @property 需要操作的对象中的属性 
    @min 最小值 
    @max 最大值 
    @step 每次拖动滚动条的步幅,就是一次拖动增加多少,默认是1 
    @Controller 操作数据之后能够调用的api 
*/
.addColor(object, property) ⇒ Controller //在GUI面板上增加拾色器 
.remove(controller) //删除控制器 就是我们上面增加的 
.destroy() 
.addFolder(name) ⇒ dat.gui.GUI //增加GUI上文件夹的面板, 
.removeFolder(folder) //删除文件夹 
.open() 
.close() 
.hide() 
.show() 
.getRoot() ⇒ dat.gui.GUI 
.getSaveObject() ⇒ Object

1.3 初始化配置dat.gui

const gui = new dat.GUI()
const options = {
  message: 'dat.gui',
  speed: 0.8,
  displayOutline: false,
  button: function () {}
}
gui.add(options, 'message')
gui.add(options, 'speed', -5, 5)
gui.add(options, 'displayOutline')
gui.add(options, 'button')

GUI 会根据你设置的options属性值对应的数据类型来渲染不同的控件

  • 如果是Number 类型则用 slider来控制
  • 如果是 Boolean 类型,则用 Checkbox来控制
  • 如果是 Function 类型则用 button 来控制
  • 如果是 String 类型则用 input 来控制

image.png

1.4 事件监听

为每一项设置一个监听事件 onChange 和 onFinishChange

options.number = 0.8
var controller = gui.add(options, 'number').min(0).max(10).step(1);
controller.onChange(function(value) {
   console.log("onChange:" + value)
});
controller.onFinishChange(function(value) {
   console.log("onFinishChange" + value)
});

1.gif

gui.add(cube.position, 'x').min(0).max(3).step(0.1).name('移动x轴').onChange(value => {
  console.log('x轴的值:', value)
})

1.gif

1.5 修改物体的颜色

提供4种类型颜色输入控制

options.color0 = '#ffae23' // CSS string
options.color1 = [0, 128, 255] // RGB array
options.color2 = [0, 128, 255, 0.3] // RGB with alpha
options.color3 = { h: 350, s: 0.9, v: 0.3 } // Hue, saturation, value

// 修改立方体颜色
gui.addColor(options, 'color0').name('立方体颜色').onChange((value) => {
    console.log(value)
    cube.material.color.set(value)
})
gui.addColor(options, 'color1')
gui.addColor(options, 'color2')
gui.addColor(options, 'color3')

1.gif

1.6 控制立方体是否显示、运动

gui.add(cube, 'visible').name('立方体是否显示')
options.fn = function () {
  gsap.to(cube.position, {
    x: 3,
    duration: 3,
    repeat: -1,
    yoyo: true
  })
}
// 点击按纽触发fn事件
gui.add(options, 'fn').name('立方体运动')

1.gif

1.7 使用文件夹给选项分组

const folder = gui.addFolder('设置立方体')
folder.add(cube.material, 'wireframe')

1.8 gui实例

1.gif

import * as THREE from 'three'
// 导入轨道控制器
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import gsap from 'gsap'
import * as dat from 'dat.gui'
// console.log('dat', dat)

// 目标:dat.gui

// 1、创建场景
const scene = new THREE.Scene()

// 2、创建相机
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
)

// 设置相机位置
camera.position.set(0, 0, 10)
scene.add(camera)

// 添加物体
// 创建几何体
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1)
const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 })
// 根据几何体和材质创建物体
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial)
// 将几何体添加到场景中
scene.add(cube)
// console.log(cube)

// 初始化渲染器
const renderer = new THREE.WebGLRenderer()
// 设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight)
// console.log(renderer);
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement)

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(3)
scene.add(axesHelper)

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement)

const gui = new dat.GUI()
const options = {
  message: 'dat.gui',
  speed: 0.8,
  displayOutline: false,
  button: function () {}
}
gui.add(options, 'message')
gui.add(options, 'speed', -5, 5)
gui.add(options, 'displayOutline')
gui.add(options, 'button')
// gui
//   .add(cube.position, 'x')
//   .min(0)
//   .max(3)
//   .step(0.1)
//   .name('移动x轴')
//   .onChange((value) => {
//     console.log('x轴的值:', value)
//   })
options.number = 0.8
var controller = gui.add(options, 'number').min(0).max(10).step(1)
controller.onChange(function (value) {
  console.log('onChange:' + value)
})
controller.onFinishChange(function (value) {
  console.log('onFinishChange' + value)
})
options.color0 = '#ffae23' // CSS string
options.color1 = [0, 128, 255] // RGB array
options.color2 = [0, 128, 255, 0.3] // RGB with alpha
options.color3 = { h: 350, s: 0.9, v: 0.3 } // Hue, saturation, value

gui
  .addColor(options, 'color0')
  .name('立方体颜色')
  .onChange((value) => {
    console.log(value)
    cube.material.color.set(value)
  })
gui.addColor(options, 'color1')
gui.addColor(options, 'color2')
gui.addColor(options, 'color3')

gui.add(cube, 'visible').name('立方体是否显示')
options.fn = function () {
  gsap.to(cube.position, {
    x: 3,
    duration: 3,
    repeat: -1,
    yoyo: true
  })
}
gui.add(options, 'fn').name('立方体运动')

const folder = gui.addFolder('设置立方体')
// 材质的线框属性wireframe,布尔值
folder.add(cube.material, 'wireframe')

function render() {
  renderer.render(scene, camera)
  requestAnimationFrame(render)
}

render()