Can the widthSegments parameter in SphereGeometry be dynamically adjusted?

Within my Three.JS project, I am utilizing a Sphere Geometry that is defined as follows:

        var radius = 1 ;
        var widthSegments = 12;
        var heightSegments = 12;

        var geometry = new THREE.SphereGeometry(radius, widthSegments, heightSegments);
        const material = new THREE.PointsMaterial({
            color: 'white',
            size: 0.01, 
        });

Currently, I desire to dynamically adjust the property widthSegments over time through a function implementation. Here is my attempt:

  var ChangeWidth = function (){
        var time = Date.now() * 0.0005;
        widthSegments = Math.sin(time * 0.7) * 30;
    }

Subsequently, I added this function to my main update function:

   function update() 
        {
            requestAnimationFrame( update );
            renderer.render( scene, camera );
            animate();    
            ChangeWidth();
        }

However, upon testing, no visible changes have occurred. Is it feasible to alter the value of widthSegments and consequently modify the geometry variable accordingly?

UPDATE

A revised approach involves deleting and recreating the geometry with an updated widthSegments value:

function update() {
            scene.add(points);
            requestAnimationFrame(update);
            renderer.render(scene, camera);
            }
        update();

        while (widthSegments < 60){

        // Initially, the older geometry is removed
        var DestroyGeometry = function() {
            scene.remove(points);
            points.geometry.dispose();
        }

        DestroyGeometry();    

        // A new geometry is then created
        const newGeometry = new THREE.SphereBufferGeometry(radius, widthSegments, heightSegments);
        points = new THREE.Points(newGeometry, material);

        // Incrementing the widthSegments value
        widthSegments += 0.5; 

        // Rendering the new geometry
        var Recreate = function() {
            scene.add(points);
            requestAnimationFrame(update);
            renderer.render(scene, camera);
        }

        Recreate();
        }

Despite implementing these changes, the functions DestroyGeometry() and Recreate() do not appear to be functioning correctly. Is there a specific reason why they are placed within a while loop configuration?

Answer №1

Is it possible to adjust widthSegments and modify the geometry accordingly?

Unfortunately, the parameters for all geometry generators are fixed when the geometry is initially created. If you wish to update these parameters dynamically, you will need to delete the old geometry and generate a new one. Here's an example:

scene.remove( points );
points.geometry.dispose(); // free up internal buffers

const newGeometry = new THREE.SphereBufferGeometry( radius, widthSegments, heightSegments );
points = new THREE.Points( newGeometry, material ); // create a new point cloud with updated geometry using existing material
scene.add( points );

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

The expected behavior is not displayed when using Async.waterfall within a promise queue

In our software implementation, we have utilized a promise queue feature using the q library. The sequence of functions is structured as follows: PQFn1 -(then)- PQFn2 - .... Within PQFn1, an array of values is passed to a callback function implemented wi ...

Add HTML and JavaScript code dynamically with JavaScript

Currently, I am working on a project that involves an HTML table with some basic JS interactions triggered by user clicks. The structure looks something like this: htmlfile.html ... ... position action ...

Is it possible to deactivate a button using jQuery without changing its visibility to transparent?

In my current project, I am utilizing jQuery and exploring its unique methods. I have a scenario where I need to disable two buttons based on a specific condition: if (...condition...) { $('button#submit, #hint').prop("disabled", true); } Ho ...

What is the best way to implement an 'onKeyPress' event listener for a <canvas> element in a React application?

I've been working with React for a while now and I understand how its event system functions. However, I've run into an issue where the onKeyPress event doesn't seem to be triggering on a <canvas> element. Surprisingly, it's not w ...

The checksum verification encountered an error during the installation process of the npm package weexpack

I am encountering an issue while trying to install weexpack. It seems that the sha1 checksum verification is failing. npm install -g weexpack npm ERR! code EINTEGRITY npm ERR! sha1-33w+1aJ3w/nUtdgZsFMR0QogCuY= integrity checksum failed when using sha1: w ...

Is Angular's promise implementation asynchronous in nature?

I can't seem to figure out the behavior of Angular's promises. Are promises actually asynchronous? I'm a bit confused about this concept. When using promises in my code to handle a large process (such as reading and writing hundreds of big ...

HTML not rendering Angular object as expected

Having trouble with my Angular project on LinkedInLearning. Can't seem to display object properties in my component, even though I can see them when logging the object. The boolean value renders fine, but not the object properties. Here is the snippe ...

Using the "bind()" method within a routed component

Here is a simplified Angular 1.5.x component example that I've set up in a jsfiddle: appModule.component('mainComponent', { controller: function() { var x = 0; this.broadcast = function() { this.onUpdate({ count: x++ ...

How can union types be used correctly in a generic functional component when type 'U' is not assignable to type 'T'?

I've been researching this issue online and have found a few similar cases, but the concept of Generic convolution is causing confusion in each example. I have tried various solutions, with the most promising one being using Omit which I thought would ...

Developing an identical design and layout but utilizing a distinct domain name for the company

After being given the task of creating a website identical to an existing one with a different domain name on WordPress, I proposed using the parent HTML source code or cloning it to speed up the project. The client agreed, explaining that starting from sc ...

What is the best way to adjust the size of a button using CSS within a ReactJS

I am facing an issue where I need to create a button with a specific width, but the template I'm using already has predefined styles for buttons. When I try to customize the button's style, nothing seems to change. Below is the code snippet: Her ...

iOS creates dynamic images with artifacts that appear on a generated and animated canvas

I am currently developing an HTML5 web application for WeChat, compatible with both iOS and Android devices, using only pure JavaScript without any third-party libraries like jQuery. The main feature of my app involves creating visually appealing animation ...

JavaScript code that moves the active link to the top of the navigation when the window width is less than or equal to 800px

I'm working on a responsive navigation that is fixed at the top and switches from horizontal to vertical when the screen size is less than or equal to 800 pixels wide. However, I'm facing an issue with moving the active link to the top of the na ...

How can I make sure an animation only plays once during the current session?

I'm facing a dilemma with my website page that showcases animations using react-anime and react-typing-animation. The issue at hand is how to animate certain elements only once per session. In other words, if the main page animation triggers when the ...

Is there a way to turn off alerts from Aspx files for HTML and CSS?

Dealing with annoying warnings in my aspx files has been a constant struggle. The "CSS Value is not defined" message pops up when I reference CSS files from different projects, causing unnecessary frustration. Even more frustrating are the warnings about i ...

The Sprite.render Depth attribute is not compatible with three.js

Here is the code snippet: var sprite = new THREE.Sprite(material); sprite.renderDepth = 10; The renderDepth setting specified above is not valid for sprites. Do you know how to address this issue? ...

Is there a way for me to display the image name at the bottom before uploading it, and have a new div created every time I upload an image?

Is there a way to display the image name at the bottom and have it create a new div every time an image is uploaded? I would appreciate any help with this... <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstra ...

Generating HTML using a filter

I have been working on creating a filter that will render HTML tags. Here is the code for my filter: filters: { limitStrLength: function (value, maxLength) { if (value && value.length > maxLength) { let partialVal = value.substr(0, ...

The Document() function requires the parameter "obj" to be an object, instead received the value 14

When using mongoose to save a value with submit and post in a hbs form, an error message "Parameter "obj" to Document() must be an object, got 14" is displayed when checking it in the localhost web. var express = require('express'); var router = ...

How can I update the color of a list item when it is clicked within a foreach loop using knockout js?

Currently, I am encountering an issue with changing the color when a user clicks on a list item using the latest version of knockout js. My goal is to change the color of a list item when it is clicked and maintain that color until another item is clicked, ...