how to set up autoRotate toggle using dat gui for three.js

259 Views Asked by At

im making a program with 3 planets beside each other, i can use a slider to move them on each axis on its own, and i set up an autoRotate but i cant get it on the controls as a toggle that works.

heres my gui controls so far:

  const autoRotate = {
    rotate: false
  };

  function updateCamera() {
    camera.updateProjectionMatrix();
  }
  
  const gui = new dat.GUI();
  gui.add(camera, 'fov', 1, 180).name('zoom').onChange(updateCamera);
  
  //rotate moon
  const moonFolder = gui.addFolder('Rotate Moon');
  moonFolder.add(moon.rotation, "x", 0, Math.PI * 2, 0.01);
  moonFolder.add(moon.rotation, "y", 0, Math.PI * 2, 0.01);
  moonFolder.add(moon.rotation, "z", 0, Math.PI * 2, 0.01);

  // rotate earth
  const earthFolder = gui.addFolder('Rotate Earth');
  earthFolder.add(earth.rotation, "x", 0, Math.PI * 2, 0.01);
  earthFolder.add(earth.rotation, "y", 0, Math.PI * 2, 0.01);
  earthFolder.add(earth.rotation, "z", 0, Math.PI * 2, 0.01);

  // rotate mars
  const marsFolder = gui.addFolder('Rotate Mars');
  marsFolder.add(mars.rotation, "x", 0, Math.PI * 2, 0.01);
  marsFolder.add(mars.rotation, "y", 0, Math.PI * 2, 0.01);
  marsFolder.add(mars.rotation, "z", 0, Math.PI * 2, 0.01);

  gui.add(autoRotate, "rotate").name("autoRotate").onChange(updateCamera);

and heres what i made for autoRotate, it works outside of the controls, but i want it to work on command.

  const rotate = [
    moon.rotateX(0.002),
    moon.rotateY(0.01),
    moon.rotateZ(0.001),

    earth.rotateX(-0.002),
    earth.rotateY(0.01),
    earth.rotateZ(0.002),

    mars.rotateX(0.004),
    mars.rotateY(-0.01),
    mars.rotateZ(0.001),
  ]; 

is there an easier way to do this that im missing?

1

There are 1 best solutions below

0
On

i figured it out, i just had to add the rotations into an if statement in my render function, and added

options{
  rotate: true;
};

before the gui controls