Guide to utilizing dat.gui for managing the speed of rotation of a model within three.js?

Trying to control the rotation speed of my model, I decided to use dat.gui for this task. In my render script, the following code snippet was added:

function render() {
                    group.rotation.y -= controls.rotation;
                    renderer.render(scene, camera);
                } 

The parameter controls is used in dat.gui.

var controls = {
                    rotation: -0.004,
                };
                var gui = new dat.GUI();
                gui.add(controls, 'rotation', -0.001, 0.001);

However, upon running the demo, my model disappeared, leaving only the scene visible. To troubleshoot the issue, I made the following modification:

function render() {
                    alert(controls.rotation);
                    group.rotation.y -= controls.rotation;
                    renderer.render(scene, camera);
                } 

After running the modified version, I observed that while the first alert showed -0.004, subsequent alerts displayed 'undefined'. This led me to believe that the demo was only rendering the first frame, and the model was not loading completely, hence invisible.

I am looking to understand why this issue occurred and how to resolve it?

Ps: Here are all the codes used:

<head>
    <meta charset="UTF-8">
    <title>ytest</title>
    <script type="text/javascript" src="JSandCSS/three.js"></script>
    <script type="text/javascript" src="JSandCSS/OrbitControls.js"></script>
    <script type="text/javascript" src="JSandCSS/OBJLoader.js"></script>
    <script type="text/javascript" src="JSandCSS/Raycaster.js"></script>
    <script type="text/javascript" src="dat.gui-master/build/dat.gui.js"></script>
    <script type="text/javascript" src="JSandCSS/stats.min.js"></script>
    <style>
    body {
        margin: 0;
        overflow: hidden;
    }
</style>
</head>

<body>
    <div id="Stats-output">
    </div>
    <div id="WebGL-output">
    </div>
    <div id="form">
    </div>
    <div id="ui">
    </div>
    <script type="text/javascript">
        // JavaScript code goes here
    </script>

</body>

Answer №1

Have you attempted incorporating this code snippet into your rendering process?

group.rotateY(controls.rotation);

Is this code being used to adjust the rotation of the geometry?

Alternatively, you could try the following approach:

var rotationAngle = 0.001;
...
var gui = new dat.GUI();
gui.add(controls, 'rotation', -5, 5).onChange(function(value){
rotationAngle = value * Math.PI / 180;
});
...
function render() {
                    group.rotateY(rotationAngle);
                    renderer.render(scene, camera);
                } 

Answer №2

I can easily identify the solution because of the following:

var controls = new THREE.OrbitControls(camera);
            controls.addEventListener('change', render); 

By deleting that code, the model appeared.

After making the following modification, everything worked correctly:

cameraControls = new THREE.OrbitControls(camera, renderer.domElement);
                cameraControls.addEventListener('change', render);

However, there is still an issue: when rotating with the mouse, it spins faster each time.

Answer №3

I found the solution provided by @adonike to be effective in resolving my issue.

Upon further observation, I noticed that the object continued to rotate even when the dat.gui slider was moved to "0". This prompted me to make some modifications to prevent the rotation when the slider reached this value.

1] One of the modifications involved setting the rotation value to a decimal number close to 1, although the logic behind this workaround remains unclear to me. It seems like it was just a stroke of luck that it worked.

var controls = new function() {
            this.rotation = 0.9; // a value close to 1
        };

2] Another method I tried was setting the rotation steps to a non-decimal value of 1.

// Setting slider steps to 1
    var gui = new dat.GUI();
    gui.add(controls, 'rotation', -5, 5).step(1).listen().onChange(function(value){
    rotationAngle = value * Math.PI / 180;
    });

It's worth noting that this second method only seemed effective when using whole numbers as steps.

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

Leverage the Ajax response within a Jade template

I'm currently utilizing ExpressJS with the Jade template engine. My goal is to achieve the following: Within a Jade file, I need to extract a variable that will be used in a JavaScript file. This JavaScript file will then make an Ajax request to the ...

tips for applying a where clause on a jsonb column in an included table with sequelize

I currently have 2 tables stored in a postgres database. Table_A -------- id BIGINT metadata JSONB Table_B -------- id BIGINT a_id BIGINT metadata JSONB Data Table_A id | metadata 1 | {'gender': 'Male', 'name': 'xyz&ap ...

The issue of fonts module resolution in React-Native remains unsolved

I am embarking on a basic build project using 'create-react-native-app' and yarn as the package manager. My main goal is to incorporate 'native-base' for enhancing the user interface. So far, the only addition to my app.js file is a B ...

Create a JavaScript array containing all the elements from a MySQL table

I am attempting to store my mysql table data in a JavaScript array. My goal is to push each element of the table into the array so that it looks like this: array[0] = [question: information01, answer: information02...] array[1] = [question: information11, ...

Troublesome button appearance

I set up two sections to gather user information using buttons. My goal is for the button styles to change when clicked, making it clear to users which option they have selected, even if they switch between sections. However, I encountered an issue where ...

Top method for utilizing render props in React

Currently, I am employing render model props. However, I have a hunch that there might be an alternative method to achieve the same outcome. Is anyone familiar with another approach? {variable === "nameComponen" && <component/>} {variable === "name ...

Loop through the coefficients of a polynomial created from a string using JavaScript

Summary: Seeking a method to generate a polynomial from specified coordinates, enabling coefficient iteration and optional point evaluation I am currently developing a basic JavaScript/TypeScript algorithm for KZG commitments, which involves multiplying c ...

Update text using jQuery without deleting child elements

I need to modify this particular text content: <body> <p class="server_info"> GT 64 ms <b>|</b> Time: <span id="serverTime">4:21:26</span> <span id="serverDate">11/03/2014</span> ...

What is the best way to pass and save information across different routes in Express using Node.js?

Let's delve into the specific functionalities I envision for my server. Essentially, my project involves user registration and login processes. The URLs for these routes are as follows - localhost:3000/login and localhost:3000/register Upon successf ...

Issue with Vue 3 where radio input remains unchecked after being clicked

I'm currently facing an issue with radio buttons for answers in my questions. Whenever I click on an answer, the radio input does not stay checked and I am unable to disable the other options. My front-end is developed using Vue 3 and the back-end wit ...

The issue arises when attempting to render an SVG with JavaScript embedded inside using the img, object, or

Issue with the title ... please follow these steps: (view codes below) Create an svg + open it separately (name it keysaway.svg) Create html + open it individually When you observe, the svg displays a simple up and down animation but fails to work when l ...

Where can you find the invalid character causing a syntax error in the jQuery $.ajax call?

My jQuery code is calling a WCF method, and although the method returns a Boolean true and logs successfully, the error handler displays "AJAX call failed in CallIsDataReady" with a "Syntax Error: Invalid character." This causes the callUpdateGrid function ...

Is there a way to fully break out of a .forEach loop that's nested within a function?

Here is the method I am currently working with: wordFormDirty = (): boolean => { var self = this; angular.forEach(self.word.wordForms, function (wf, key) { var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId if ...

Validating Credit Card Numbers with Spaces

Currently, I am in the process of creating a credit card form that requires validation using checkValidity to match the specific card pattern that is dynamically added to the input field. For example, if a user enters a Mastercard number such as 545454545 ...

Best practices for updating the value of a specific key within an object that contains recursion in JavaScript/TypeScript

I have a tree component that uses the following data structure type TreeNode = { id: string, parentId: string, renderer: () => React.ReactNode, expanded: boolean, children?: Array<TreeNode>, } Now, I am looking to add functionality for ...

Issue with JavaScript Date.parse function not functioning as expected

I have implemented a date validation method in my application to check if a given date is valid or not. myApp.isValidDate = function(date) { var timestamp; timestamp = Date.parse(date); if (isNaN(timestamp) === false) { return true; } return ...

What causes the temporary halt of context execution in the eventloop framework?

Presenting the code immediately: setTimeout(() => console.log("next macro")); /// next macro Promise.resolve().then(() => gen.next()) /// microtask inside, #2 const gen = (function*(){ console.log("Hello"); yield; /// #3 console.log("W ...

The website is displaying a strange mix of text and HTML when viewed on a PC

While browsing the internet for C programs, I stumbled upon this particular program that caught my eye: <span style='color:#004a43'>#</span><span style='color:#004a43'>include</span><span style='color:#8 ...

What steps should be taken to ensure that Google effectively crawls through an AngularJS application?

According to a source at Allotment Digital, it is unnecessary to provide different or pre-rendered content to Google when using html5mode and removing hashtags from URLs. While some websites state that ajax crawling documents are deprecated, others such a ...

Retrieving ng-model using ng-change in AngularJS

Here is an example of the HTML code I am currently working with: <select ng-model="country" ng-options="c.name for c in countries" ng-change="filterByCountry"></select> This HTML snippet is being populated by the following object containing a ...