Update the three.js geometry when a user modifies the settings using dat.gui

I am currently working on updating a 3D spiral geometry in a canvas as the user adjusts a slider to change the number of coils. My approach involves using the dat.gui plugin for the interface. I have successfully implemented the slider to update an external parameter, such as the x-scale. However, I am facing difficulties when trying to update an internal parameter within the geometry. Although I can see the updates in the geometry data through console.log, the canvas itself does not reflect these changes. Below is the code snippet for the dat.gui interface:

  const gui = new GUI()
  
  let guiControls = new function() {
    this.coils = 10
  }

  gui.add(guiControls, 'coils', 1, 15).onChange(regenerateGeometry)
  
  function regenerateGeometry() {
    let newGeometry = new THREE.Geometry()
 
    spiralSetup(newGeometry)

    mesh.geometry.dispose()
    mesh.geometry = newGeometry

    render()
  }

Here is the code snippet for the spiral geometry:

function spiralSetup(geometry) {
    for (let i = theta; i < t; i++) {
      let r = Math.exp((i / segments) * (1 / Math.tan(alpha)))
      let x = r * aa * Math.cos(i / segments) * Math.sin(beta)
      let y = r * aa * Math.sin(i / segments) * Math.sin(beta)
      let z = -r * aa * Math.cos(beta)
      
      geometry.vertices.push(new THREE.Vector3(x, y, z))
    }
  }
  
  spiralSetup(startGeometry)

  let material = new THREE.LineBasicMaterial({color: 0xBA42A5})
  let mesh = new THREE.Line(startGeometry, material)

  scene.add(mesh)

You can find the complete code on this jsfiddle link: https://jsfiddle.net/yjsnhf72/

Answer №1

The problem arose from the failure to update the coils in the guiControls class and neglecting to update the variable coils. As a result, the variable t was updated incorrectly. Check out the revised code snippet for the solution.

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

Encountering a ReferenceError: require is undefined error when utilizing contextIsolation: true in conjunction with a preload script

My goal is to implement a select folder popup functionality, and I have written the following code for this purpose. Within the create-window.ts file, I have included these browserOptions.webPreferences: webPreferences: { nodeIntegration: true, conte ...

Ways to sum up the values within a JSON object

Currently, I am using an AJAX call to fetch JSON data from an API. My goal now is to add up all the visits. This is what I have implemented so far. Any suggestions on how I can achieve that? $(document).ready(function() { var X = []; var Y = []; var d ...

The destroySlider() function of BxSlider fails to work due to either an undefined slider or a function that is not

I'm facing an issue with my carousel setup using bxslider. Here is the code snippet responsible for initializing the carousel: jQuery(document).ready(function() { var carouselWidth = 640; var carousel; var carousel_Config = { minSlides: 1, ...

The functionality of ng-show becomes compromised when used in conjunction with ng-animate

Once the animate module is activated, the ng-show functionality seems to stop working. Even though the default value for the ng-show expression is set to false, the element is still displayed and the ng-hide class is not being applied. However, if I deac ...

Node.js captures the Promise and provides detailed feedback

As I embark on my journey with Node.js + Express, currently in the process of structuring my HTTP APIs, I have a controller that utilizes a specific pattern: my_controller.js 'use strict'; var AppApiFactory = function (express, appService) { ...

"Enhancing Efficiency with Tagging and Contextual Autocomplete

I have integrated into my project for tagging and autocompletion features. Currently, I am able to autocomplete a single term successfully: This is the console log: GET .../source.php?term=value01 This is the Javascript code snippet: $("#input-newsear ...

How can I switch to another screen from the menu located within my Parent Component?

I've been working on adding a customized navigation menu to my react-native app, but I'm currently facing the challenge of not being able to navigate to the corresponding screens of the selected menu items. I tried using this.props.navigation.nav ...

Access a specific JSON value using AngularJS

When using AngularJS to read a specific JSON value, I encountered the following issue: $http({method: 'GET', url: urlVersion}). success(function(data, status, headers, config) { console.log("success data " + status); $scope.ext = data.ve ...

Execute Yarn commands from the main directory

In the midst of my project, I have various sub projects - one of which is dedicated to JavaScript. My intention is to employ this JavaScript project as a task runner for the entire endeavor using gulp. However, I've hit a snag in the process. The lay ...

Picking and modifying a newly added HTML input element using Jquery

I am encountering a problem that I haven't been able to find a solution for through research or Google. I managed to successfully add multiple input boxes to my HTML/JQuery project, which are displaying correctly on the webpage. for (i = 0; i < ma ...

How can I redirect to another page when an item is clicked in AngularJS?

Here is an example of HTML code: <div class="item" data-url="link"></div> <div class="item" data-url="link"></div> <div class="item" data-url="link"></div> In jQuery, I can do the following: $('.item').click ...

Struggling to find a solution for changing the color of a select box when an option is chosen

Here's an example of the HTML I'm working with: <select onclick="colorchanger()"> <option name="white" value="0">--Select--</option> <option name="red" value="1">Work</option> <option name="green" value="2" ...

Error: The function $.get is not recognized when using jQuery with babel-node

Currently, I am executing the code below from a test.js file using babel-node in the command line: babel-node test.js installation of jquery npm i --save jquery test.js: import $ from 'jquery'; $.get("www.google.com"); Upon execution, I en ...

Handling a JSON array error when working with a JSON string field

Attempting to create a JSONArray object with nested arrays and json strings, specifically looking at the "res" field. [{ "time": 123813213, "value": [{ "name": "task", "res": "{\"taskName\" : \"NAME\", \"ta ...

Do NodeJS and Express require sanitizing the request body for post requests to prevent cross-site scripting (XSS) attacks?

There is a website I have where users can input data into fields and submit it. Once submitted, the information is sent to my server through a POST request and saved in the database. However, this user-generated content is also returned to the front-end f ...

Quality of ThreeJS 3D Objects

Is anyone aware of how it is possible to achieve such high quality for the 3D objects seen here ? Are these objects exported from programs like 3DsMax or Blender, or is the quality enhanced in threejs? From what I have observed, it seems that the project w ...

Automatically pre-fill and send hidden form

I'm working on a form and have set up a handler for the submit button like this: $( '#submit_btn' ).click( function( data ){ theForm = document.getElementById( 'realForm' ); theForm.meetingName.value = document.getElement ...

Achieving a Full-Screen Three.js Canvas in React: A step-by-step guide on spanning the view height and width of a webpage

I've been working on tweaking the code found in this particular example: How to connect Threejs to React? This is the snippet of code I am focusing on: import React, { Component } from 'react' import * as THREE from 'three' clas ...

Extracting information from a database (HTML, JavaScript)

I am currently working on a table that receives data from the server using ajax: $.each(data, function(i, item) { $('#MyTable tbody').append("<tr>" +"<td>" +data[i].A+ "</td><td>" +d ...

Enhancing the "click-to-pause" feature with jQuery Cycle

Is it possible to delay the click-to-pause feature on my slideshow until after the first slide transition? Currently, the slideshow becomes clickable as soon as the DOM is ready, which is too early for my liking. Here is a simplified version of my current ...