简体   繁体   English

Three.js 如何使用 dat.GUI 在控制面板中将值转换为度数?

[英]Three.js How to convert values to degrees in the control panel using dat.GUI?

I made a simple slider in control panel to modify the position and rotation of an object.我在控制面板中做了一个简单的 slider 来修改 position 和 object 的旋转。 I have no problem with the position, as the values are relative, but I'd like to show the rotation's values in degrees.我对 position 没有问题,因为这些值是相对的,但我想以度为单位显示旋转值。 Here is what I have in the rotation controls:这是我在旋转控件中的内容:

gui
  .add(mesh.rotation, "x")
  .min(Math.PI * -1)
  .max(Math.PI)
  .step(0.01)
  .name("Rotar en X");

And this is how it is shown in the control panel:这就是它在控制面板中的显示方式:

dat.GUI的控制面板

The idea is to show -180 and 180这个想法是显示 -180 和 180

You're going to have to create a placeholder object that holds the values in degrees, then use the .onChange() event to know when the value changes.您将不得不创建一个占位符 object 以度为单位保存值,然后使用.onChange()事件来了解值何时更改。 That's when you can convert the degrees into radians to assign to your mesh rotation.那时您可以将度数转换为弧度以分配给网格旋转。

// Placeholder holds values in degrees
var degrees = {
    x: 0,
    y: 0,
    z: 0
};

gui
    .add(degrees, "x")
    .min(-180)
    .max(180)
    .step(1)
    // This function is called each time the value is changed
    .onChange(() => {
        console.log(degrees.x);

        // Convert the degrees to radians, then assign to mesh rotation
        mesh.rotation.x = THREE.MathUtils.degToRad(degrees.x);
    });

I'm using THREE.MathUtils.degToRad() to convert from degrees to radians.我正在使用THREE.MathUtils.degToRad()将度数转换为弧度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM