Combining a complete hierarchy of Object3D/Mesh into one merged entity

I'm currently working on a project that involves dynamically generating trees using simple cubes for branches and leaves in the early prototype stages. Each tree consists of a hierarchy of cubes nested with rotations and scaling to create the final structure. The challenge I'm facing is the performance impact when rendering large trees with many cubes, causing the program to slow down significantly.

After researching solutions, I came across the THREE.GeometryUtils.merge() function, which can merge all the branches of a tree into a single object for faster rendering and transformation. However, the issue I'm encountering is that the merge process doesn't consider the parent transforms, only merging the vertices.

My attempts to resolve this issue include the code snippet below. I've tried applying the matrix to the geometry and experimenting with other adjustments, but I haven't achieved the desired outcome yet:

var newGeo = new THREE.Geometry();
var newTree = tree.clone(); 
newTree.traverse(function(child){
    if(child.parent){
        child.applyMatrix(child.parent.matrixWorld);
    }
    THREE.GeometryUtils.merge(newGeo, child);
});

I've created a jsFiddle demo to showcase the problem:
http://jsfiddle.net/JeYhF/2/
The left object shows 4 meshes parented inside each other, while the right object displays the merged combination. Although each component of the combination has individual transforms (11-unit translation in the y-axis and PI/4 rotation in the z-axis), they are not affected by parent transforms. The MergeTree() function in the program is the root of this issue, and it seems to only function properly in Chrome for me.

If you have any advice on how to tackle this problem effectively, I would greatly appreciate it. Thank you

Answer №1

The issue arose from the fact that the matrix world had not been computed prior to the merging process. As a result, all transforms were based on the identity matrix.

newTree.traverse(function(branch){
    if(branch.root){
        branch.calculateMatrixWorld();
        branch.applyMatrix(branch.root.matrixWorld);    
    }
    THREE.GeometryUtils.merge(updatedGeometry, branch);
});

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 <transition> element is not being recognized by VSCode

It seems like my VSCode is highlighting the transition element in red, indicating that it may not be recognizing this element. I'm a bit perplexed by what's happening here. Moreover, the code isn't functioning as expected because there is no ...

Error: The property 'length' cannot be read from an undefined parent causing Uncaught TypeError

Hey there, take a look at this cool stuff http://jsfiddle.net/J9Tza/ <form class="validation"> <div> <input type="email" class="form-control" id="inputEmail" name="email" placeholder="Email" pattern=".{3,200}" title="3 to 200 characters" r ...

Choosing Select2: Customizing the context of formatSelection

I've created a simple custom wrapper for Select2, which has been very helpful. However, I am facing an issue with the formatSelection field. When initializing Select2 through my wrapper, it looks like this: this.elem.select2({ allowClear : option ...

Can you explain the execution process of this Http.post method and provide details about the code path it follows

As I delve into the world of web development, one aspect that has me stumped is the functionality of the Http.post section within a project I stumbled upon on GitHub. Specifically, this pertains to an ExpressJS with Typescript repository I came across. So, ...

How to Handle Errors When Retrieving an AWS S3 Object Stream in Node.js

I am currently working on developing an Express server that will send items from a S3 bucket to the client using Node.js and Express. I came across the following code snippet in the AWS documentation. var s3 = new AWS.S3({apiVersion: '2006-03-01&apo ...

Positioning of dropdown in Material UI select component

Unfortunately, I am encountering an issue with the Menuprops attribute and cannot seem to adjust the position of the drop-down box. Despite following the instructions from other similar queries, the desired outcome remains unachieved. My goal is to have t ...

Creating an Extjs model for a complex nested JSON structure

Take a look at this JSON structure { "id": 123, "name": "Ed", "orders": [ { "id": 50, "total": 100, "order_items": [ { "id": 20 ...

Issue with hook not updating when invoked inside useEffect

I'm encountering an issue with updating the state after fetching data from my API. The API response seems to be correct, but for some reason, my weatherData-hook is not getting updated and it returns undefined. Can anyone point out what mistake I migh ...

The Vue component fails to refresh when the state in the store undergoes changes

Trying to create a simple todo list in Vue, but aiming to abstract everything out and utilize a dummy REST API for practice with production-level Vue projects has left my head spinning. While GET, PUT, and POST requests appear to be functioning properly, I ...

Find and return a specific record from MongoDB if it matches the exact value

model.js import mongoose from 'mongoose'; const { Schema, Types } = mongoose; const participants = { user_id: Types.ObjectId(), isAdmin: Boolean } const groupSchema = new Schema({ id: Types.ObjectId(), // String is shorthand for {type: St ...

Creating a file logging system with log4js to capture Console logs

Is there a way to automatically log all console logs, including failed expectations and exceptions, to a file without using try and catch in JavaScript? In Java's LOG4j, the rootlogger feature does this by default. Is there a similar functionality ava ...

Data is not being refreshed by Ajax

In my forum, users are only allowed to edit and delete their own comments. I have set up an "edit" button that opens a modal when clicked, giving the user access to the data they submitted before. I've written an ajax function to target these fields a ...

What issues are there with JavaScript?

I encountered an issue with my JavaScript code where it is not producing the expected result. var arr = new Array(); var firstobj = { cta: 0, ccc: 0, crc: 0 } for (var ta = 1; ta <= 10; ta++) { arr[ta] = firstobj; } arr[6].cta = 1; conso ...

Customize footer data in ui-grid design

I'm having trouble formatting the aggregate value for a column in ui-grid. Currently, the number display looks like this: total: 6370.046074130321 but I would prefer it to be formatted like this: total: $6370.05 I've attempted using both of ...

When executing class methods, Ember.js encounters errors stating "method does not exist."

I am facing a situation where I need to trigger a model reload when a user clicks a refresh button. In the past, I successfully implemented this with Ember-Model. However, since migrating to Ember-Data, I am encountering an error when attempting to execute ...

Tips for effectively splitting arrays nested within an array using JavaScript

Here is an example of slicing an array to generate a new one: var array= [ [1,"dataA","dataB","dataC","dataD"...], [2,"dataA","dataB","dataC","dataD"...], [3,"dataA","dataB","dataC","dataD"...], [4,"dataA","dataB","dataC","dataD"...]... ...

Tips for altering the appearance of the material-ui slider thumb design when it is in a disabled state

Through the use of withStyles, I have successfully customized the style of the Slider: const CustomSlider = withStyles(theme => ({ disabled: { color: theme.palette.primary.main }, thumb: { height: 24, width: 24, }, }))(Slider); How ...

Utilize AngularJS to compile a roster of participants

Just starting out with AngularJS, and although I have some background in JavaScript, AngularJS seems quite challenging to grasp (at least for me). My current issue is as follows... I have a list of players and I want to allow users (such as coaches or an ...

Execute the index.js file using the parameters specified in the mocha test suite

Within my index.js file, I am passing in specific arguments. // parameters var outFile = process.argv[2] || (() => {throw "missing argument outFile";})(); var templateName = process.argv[3] || (() => {throw "missing argument templateName";})(); My ...

Encountering a parser error during an Ajax request

Attempting to develop a login system with PHP, jQuery, Ajax, and JSON. It successfully validates empty fields, but upon form submission, the Ajax call fails. The response text displays a JSON array in the console, indicating that the PHP part is not the is ...