Setting up a autoRotate toggle feature in three.js using dat.gui - a step-by-step guide

I am currently working on a project involving 3 planets positioned next to each other. I have implemented a slider to independently move them along each axis and have set up an autoRotate feature. However, I am facing difficulty in integrating it as a toggle within the controls.

Below is my current GUI controls setup:

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);

Here is my implementation of autoRotate functionality, which currently works independently but not within the controls:

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),
]; 

I am exploring if there is a simpler approach that I might be overlooking. Any suggestions would be greatly appreciated.

Answer №1

I was able to solve the problem by incorporating the rotations into a conditional statement within my render function, and also included

options{
  rotate: true;
};

prior to configuring the GUI controls.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Declare the variable as a number, yet unexpectedly receive a NaN in the output

I'm facing an issue with a NaN error in my TypeScript code. I've defined a variable type as number and loop through an element to retrieve various balance amounts. These values are in the form of "$..." such as $10.00 and $20.00, so I use a repla ...

The Angular Sortable Containment feature ensures that items cannot be dropped outside of

I am experiencing an issue with a horizontal sortable Angular list where the containment option is not functioning properly, causing the dragged item to not be dropped. Take a look at the sortable list below without a containment option - you may notice t ...

The beams of light are casting delicate ripples across the textured surface beneath me. Is there a way to eliminate their presence?

I recently ran into an issue while working on my three.js project. I have a scene with some mapped objects, and when I add lights to the scene, strange "waves" start appearing. See image here These waves are caused by my camera moving around the scene, c ...

Ways to stop the endless cycle of a node.js script powering a Discord bot

I've developed a Discord bot using JavaScript for node.js that can change a role's color like a rainbow. async function colorChanger (){ for (var i = 0; i < colorArray.length - 1; i++){ myRole.setColor(colorArray[i]); ...

Utilize an A-frame feature to dynamically enable or disable a component through a

I have set up a virtual environment using A-frame () with some raining effects and a button. What I want to achieve is to have a function executed when the button is clicked, that function should remove the rain component from the scene. Here is the code s ...

What is the best way to list the choices associated with a specific category?

The Node.js package I'm currently working with requires an argument of a specific type, which I can see is defined through a TypeScript declaration as follows: export declare type ArgType = 'A' | 'B' | 'C'; I am interes ...

Optimizing my AngularJS bundle is pushing me towards upgrading to Angular 5

Regarding my AngularJS application, it was initially created using 'yo angular-fullstack' with JS scripting instead of TS. It is functional but experiencing performance and user experience issues. The deployment is on AWS ElasticBeanstalk nano i ...

Issue with gulp-uglifyjs arising from an empty return by gulp.src

Within a directory named app, I am iterating through an unknown number of folders to generate a folderName.min.js file for each one. Below is the Gulp script being used: var es = require('event-stream'); var uglify = require('gulp-uglifyjs ...

Issue with Element Not Displaying During Page Load with Quick AJAX Request

I attempted to display a loading gif while making an ajax request that takes a long time to respond, and then hide the loading gif once the response is received. Here is the code snippet : $('.loading-gif').show(); $ajax({url:'',async ...

Tips for recognizing the click, such as determining which specific button was pressed

Currently, I am utilizing Angular 6. On the customer list page, there are three buttons - "NEW", "EDIT", and "VIEW" - all of which render to one component. As a result, it is crucial for me to determine which specific button has been clicked in order to ...

Creating a dropdown button in HTML table cells with JavaScript: A comprehensive guide

I'm attempting to create a drop-down button for two specific columns in my HTML table, but all the rows are being converted into drop-down buttons. I only want the "categorycode" and "categoryname" columns to be drop-down. $(document).ready(functio ...

Managing the onChange event for a MaterialUI dropdown component

I'm encountering an issue with validating the MaterialUI TextField component (Country list) wrapped with Autocomplete. I am trying to use the onChange event to enable the Submit button when the country field is filled in. However, the problem arises w ...

Automatically scroll the chat box to the bottom

I came across a solution on Stackoverflow, but unfortunately, I am having trouble getting it to work properly. ... <div class="panel-body"> <ul id="mtchat" class="chat"> </ul> </div> ... The issue lies in the JavaScript t ...

Having difficulty implementing Jquery UI effects within an HTML Dialog in Google Apps Script

Looking for a scrolling effect in an HTML Dialog with Google Apps Script. I've already tried this link and placed the code in the editor. However, when highlight buttons are clicked, they work but do not cause scrolling within the HTML dialog. < ...

I'm unsure of the most efficient way to condense this statement

$(document).ready(function(){ if ($(window).width() <961){ $('.item').on('click',function(){ /*---do something---*/ }) }else{ $('.item').on('click',function(){ ...

An API built with Mongoose, Express, and Node.js is currently only able to send a single image

I have a buffer array that needs to be converted into images and sent to the user. The issue is that the current code only sends one image: const express = require("express"); const asyncHandler = require("express-async-handler"); const ...

Determine the status of a checkbox in Protractor with JavaScript: Checked or Unchecked?

I'm currently facing a challenge while writing an end-to-end Protractor test. I need to verify whether a checkbox is enabled or not, but it doesn't have a 'checked' property. Is there a way in JavaScript to iterate through a list, check ...

Learn how to dynamically show the chosen option from a dropdown menu in input text fields using AngularJS

html file: <select ng-model="shipping" ng-options="shipping.shipping for shipping in shipAddress"> <option value=''>--Select Address --</option> </select> <form name="shippingForm" class="form-horizontal" role="form" ...

Update the class of the appropriate navigation tab when the corresponding div is scrolled into view

After reading similar questions and doing some research on scrollspy, I don't think it will provide the functionality I need. It seems to only support bootstrap style highlighting. If there is more to it that I'm not aware of, please inform me! ...

Plot a linear chart in Apex using x and y values along a dateTime axis

Greetings! I am currently utilizing Apex chart in conjunction with vue.js and VueApexChart. Below is the value of my options: export const option = { chartOptions: { chart: { height: 350, type: 'line', ...