Three.js: Comparing the Process of Updating Geometries with Replacing Them

In my current project, I have a complex scene filled with objects created using the ExtrudeGeometry method. These objects need to be updated every frame, with the extrusion shape and amount constantly changing. The shapes are generated using d3's voronoi algorithm.

If you want to see an example, check out this link.

Currently, I am redrawing every object in the scene each frame by removing and recreating them. This process is very resource-intensive and is leading to performance issues. I'm wondering if there is a way to edit the meshes/geometry directly instead of removing and recreating them. Would this approach improve performance? Are there more efficient methods for updating the scene without sacrificing performance?

The key requirement is the ability to adjust both the extrusion shape and amount dynamically.

Thank you for your help!

Answer №1

If you want to keep the number of faces the same, morph targets are the way to go. Check out this link for more information:

Here's what you need to do:

  1. Start by creating your geometry
  2. Clone the geometry and make any necessary modifications, like adjusting the maximum length of a pillar
  3. Set both geometries as morph targets to your base geometry. Here's an example:

    baseGeo.morphTargets.push( { name: "targetName", vertices: [ modifiedVertexArray ] } );

Once that's done, you can animate the mesh by using mesh.updateMorphTargets()

For more details, visit:

Answer №2

After experimenting, I found a way to optimize performance by avoiding redrawing the scene each time.

http://jsfiddle.net/x00xsdrt/4/

Here's the technique I used:

  1. Created a "template geometry" with ExtrudeGeometry using a dummy 10 sided polygon.

  2. Created multiple "points", each assigned with one of these template geometries.

  3. During each frame, looped through each geometry, updating every vertex to match the new one (via the voronoi algorithm).

  4. If there are excess vertices, grouped them together into a single point. (refer to http://github.com/mrdoob/three.js/wiki/Updates.)

Reflecting on it now, the process appears quite straightforward. Initially, manipulating each vertex seemed daunting, but with simple shapes, it's actually manageable!

For the iteration, polycColumn is a simple 2 item array with the same polygon in both items:

// Set the vertex index
var v = 0;

// Loop over both top and bottom of poly
for (var p=0;p<polyColumn.length;p++) {

    // Iterate over half the vertices
    for (var j=0;j<verts.length/2;j++) {

        // Determine z-index based on top/bottom
        if (p == 1) {
            var z = point.extrudeAmount;
        } else {
            var z = 0;
        }

        // Handle legitimate verts
        if (j < poly.length) {

            verts[v].x = poly[j][0];
            verts[v].y = poly[j][1];
            verts[v].z = z;

        // For extra verts, group them at the same position
        } else {

            verts[v].x = verts[v - 1].x;
            verts[v].y = verts[v - 1].y;
            verts[v].z = z;
        }

        v++;
    }
}

point.mesh.geometry.verticesNeedUpdate = true;

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 pure JavaScript function for changing the background color in CSS is not functioning properly

I'm having trouble understanding why the color does not change after clicking. Can anyone explain? function butt(color) { if(document.getElementById("bt").style.backgroundColor=="green"){ document.getElementById("bt").style.backgrou ...

What is the best way to eliminate excess white space on the right side in WordPress for a mobile perspective?

Is there a quick way to identify which element has shifted beyond the border? It seems like there is excess margin. How can I pinpoint the issue? The link to the broken page on mobile is I applied this style * {border: 2px solid red;} and no elements shif ...

Border becomes dashed when dragging and dropping

I am working on enabling drag and drop functionality for users. When an item is lifted from its original position, a gray dashed box should appear in its place. As the item is moved closer to another spot, the containers adjust to create a target area (gra ...

The functionality of the webservice is not functioning properly

I'm currently working with Express and NodeJS to create a simple hello world webservice. I am attempting to call this webservice in ReactJS using Axios, but I am encountering issues with the response from the webservice. Below is my code for the webse ...

Troubleshooting Cordova's ng-route functionality issue

I am currently working on an Angular application that includes the following code: // app.js var rippleApp = angular.module('rippleApp', ['ngRoute', 'ngAnimate', 'ngAria', 'ngMaterial']); // configure ou ...

The body content of the webpage needs to be updated without altering the header and footer, all while avoiding a page

On my website, I have a header menu that includes buttons for "home" and "about us." The default page is set to the home page. Within the home page, there is a specific link. When this link on the home page or the "about us" button is clicked, I want the ...

Discover the capability to choose dates from different months using the DatePicker component from @material-ui/pickers

I am currently in the midst of a React project that requires the use of the DatePicker component from @material-ui/pickers. The specific mandate is to show dates outside of the current month and allow users to select those days without needing to switch m ...

Is there a way to initiate the server only after webpack has finished bundling all of the bundles

"scripts": { "start": "node server.js", "build": "webpack" }, Is there a way to execute both npm run build and npm start with a single command? "scripts": { "start": " ...

Unable to automatically prompt the button inside the iframe

In this scenario, an Iframe is automatically generated by a JavaScript script. I am looking to simulate a click by triggering a button, but unfortunately, it is not working as expected. You can view the code on JSFiddle. I have attempted various approache ...

Sending information back to the server without causing a postback, all while remaining unseen

I have a straightforward JavaScript function on my ASP.NET page that writes data to a hidden field. However, in order to retrieve this data the form needs to be submitted back to the server. The issue is that submitting the form causes the page to reload ...

Implementing Ionic 4 with HTML2Canvas technology

Looking for a way to convert HTML into an image within an Ionic 4 application. I attempted to use html2canvas, however, it was unsuccessful and displayed the following message in the console: https://i.sstatic.net/4b4Sm.png Below is the code snippet I use ...

Handling mousewheel events for child elements and their parent element

I developed a custom jQuery plugin that replaces the default scrollbar with my own, managing mousewheel and bar dragging events. The issue arises when I place content containing my custom scrollbar within another content also using my scrollbar. When I sc ...

Handling memory in javascript jquery mobile using phonegap

I have encountered an issue while building a jQuery Mobile + PhoneGap app for iOS. The app functions correctly on a web browser, but when bundled with PhoneGap and tested on a phone, it starts to forget JavaScript functions. For instance, panels that shoul ...

Terminate the rethinkdb data stream

Currently delving into the world of RethinkDB and am eager to utilize the changes() method to fetch a changefeed. While I've figured out how to initiate them, the documentation doesn't quite explain how to halt a changefeed. Is it sufficient to ...

Can Google Charts be integrated with $refs in VueJS?

I'm currently working on implementing a timeline chart with Google Charts. The chart is displayed within its own component, which I later invoke from a higher-level parent component. However, I've run into an issue where the drawChart function re ...

Can the GPGPU water example be adapted to work with planes of varying shapes, such as rectangles or boomerangs?

Check out this amazing example: I have a good grasp of how it functions as I have successfully integrated it into a game I am working on, and have made various edits to the shaders and JavaScript. However, my question is, can this water effect be applied ...

Selecting objects in Three.js using the camera but without using the mouse

I am working on a Three.js app where I need to determine the object that the perspective camera is focusing on. In order to achieve this, I consulted the raycaster documentation. Most of the resources I came across discuss using raycasting with a camera an ...

Using Middleware to Access LocaleStorage in Universal Mode

Here's the dilemma: My understanding is that accessing LocaleStorage from Middleware in Universal mode is currently not possible (at least not out-of-the-box.) Question: Is there any way to solve this issue? Are there any modules or solutio ...

Determining the distance between points in an STL file with three.js

I'm currently working on a feature that requires users to click on two different points and calculate the distance between them. However, it seems like the clicks are occurring at random positions, resulting in inaccurate calculations. The calculati ...

React frontend encountered a connectivity issue while trying to establish a network connection

Our app utilizes next.js connected to express, which is further linked to AWS MySql for database management. While attempting to load some products stored in the database, an error message pops up: TypeError: NetworkError when trying to fetch resource. ...