adjusting the size of separate models within a unified structure

Recently, I created a 3D model of my hometown and I wanted to incorporate real-time data to adjust the height of the buildings. Initially, I loaded each building as an individual mesh and added it to the scene during setup.


var threeObjects = [];
var buildingMesh = new THREE.Mesh(geometry, material);       
threeObjects.push(buildingMesh);
$.each(threeObjects, function(i, buildingMesh) {
    buildingMesh.rotation.x += -3.1415*0.5;
    buildingMesh.castShadow = true;
    buildingMesh.receiveShadow = true;
    scene.add(buildingMesh);
});

However, this process proved to be too slow given that my dataset includes approximately 10,000 buildings. To speed up the rendering, I decided to merge all the geometries into a single geometry and create a mesh from it before adding it to the scene.


singleGeometry.merge(buildingMesh.geometry, buildingMesh.matrix); //in a loop

var faceColorMaterial = new THREE.MeshLambertMaterial( { color: 0xffffff, vertexColors: THREE.VertexColors } );
combinedMesh = new THREE.Mesh(singleGeometry, faceColorMaterial);

scene.add(combinedMesh);

In order to test my concept, I attempted to change the height of a building when clicked. Unfortunately, this functionality did not work as expected. While I was able to access faces and vertices by assigning a new ID, changing the height remained elusive to me.

In my original method, adjusting the scale along the z-axis would have been straightforward:

buildingMesh.scale.z=2;

But since I no longer have separate meshes, I'm unsure how to proceed.

If anyone has experience with Three.js and can offer guidance, I would greatly appreciate it!

Disclaimer: I am relatively new to Three.js, so please bear with me if my question seems basic. I hope for a helpful solution :)

Answer №1

When you amalgamate all your structures into a unified geometry, you're essentially erasing their individuality. Building A becomes indistinguishable from building B because they are merged into one large entity. At its core, geometry consists of mere arrays of points and polygons without any distinguishing features. Therefore, consolidating them might not be the optimal approach.

Instead, leverage the efficient scene graph architecture offered by three.js. Initially, adding all buildings to a single root Object3D ("scene") was on the right track. This way, you benefit from the scene graph's efficiencies while still being able to address each building separately.

To enhance loading efficiency, consider creating the scene graph in a 3D modeling program before importing it into three.js. Establish parent/child relationships there and export it as a single model containing all buildings as child nodes. By following this method, the structure should remain intact upon import.

Answer №2

JCD: My original question wasn't quite answered, but I managed to find a solution on my own. So what I did was combine all the geometries together, using a different method than the standard clone function in geometry.merge(). Instead, I used a shallow reference which allowed me to access specific buildings within threeObjects and resize them using Mesh.scale. Then, I set geometry.verticesNeedUpdate = true to ensure everything worked correctly. To optimize further, I divided the model into 5 separate geometries and only updated the one containing the building.

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

Reveal concealed fields following the selection of a specific option

I'm looking to create a bookmarklet that will automatically fill in values when clicked. Currently, I can select values using: document.getElementById('component').value="IAE-Data Agent"; document.getElementById('component').onch ...

Adding a promise to an array using Javascript

I am facing an issue while attempting to create an array of promises and then calling them using Promise.all. The problem lies in correctly pushing the functions into the array. It seems like they are getting executed instead of being inserted and waiting ...

Is it possible to refresh the options for a select in AngularJS when they are in different controllers?

Two separate controllers are being used here; one for language, known as `langCntl`, and one for words, referred to as `wordCntl`. Within the `wordCntl` controller, there is an attribute called `ln`. This attribute is displayed in a form using `ng-select` ...

Having trouble integrating material design icons into Vuetify

After installing material-design-icons-iconfont using npm, I noticed that the woff files are available in the dist folder once I build the project. Material-design-icons.css /* For IE6-8 */ src: local("Material Icons"), local("MaterialIcons-Regul ...

Tips for utilizing the find function with nested documents in MongoDB in conjunction with Next.js?

I have structured my data in such a way that it is obtained from localhost:3000/api/notes/[noteTitle]/note2 { "data": [ { "_id": "62ff418fa4adfb3a957a2847", "title": "first-note2" ...

How to properly implement ng-if for a select option in Angular.js?

Within the controller, I defined a scope variable: $scope.readOnly = true. In the HTML code: <select ng-if="readOnly" ng-model="readOnly" required> <option value="true" selected>True</option> <option value="false">False ...

Utilizing Javascript to export modules and call multiple functions simultaneously

Is there a way to automate the calling of all functions within a module instead of individually selecting each one? For instance: bettermovies.js module.exports={ printAvatar: function(){ console.log("Avatar"); }, printLord: funct ...

Display the original content of a previous div element after modifying it using JavaScript

I wanted to simplify the code. In my jsp file, when the document is loaded, the select values are filled in automatically. For instance: <div class="col-sm"> <label style="font-size: 20px;"><fmt:message key="cap.workplace"/>: </ ...

Press the button after 60 seconds of inactivity in JavaScript

After a lengthy search and multiple failed attempts with different files, I am in need of the following script: If there is no interaction with a webpage for 1 minute, the script should automatically: - Click on a button. - Repeat the click every 30 secon ...

Guide on setting up haproxy as a reverse proxy for socket.io with SSL to avoid mixed content warnings

Over time, I've incorporated this configuration to integrate haproxy with node.js and socket.io. Highlighted sections from the haproxy setup: frontend wwws bind 0.0.0.0:443 ssl crt /etc/haproxy/ovee.pem timeout client 1h default_backend ...

Issue with Jenkins running Cucumber causing timeout on JavaScript form

My current setup involves Cucumber 1.2.1 with Watir-webdriver 0.6.1 for webpage testing. While the tests run smoothly on my local machine, I encounter a timeout issue on a CI machine (Jenkins) when trying to fill out a javascript form. Despite having emai ...

The SrollToTop function is ineffective when used with a component in Ionic 6/Angular

Recently, I implemented a fabbutton feature that allows users to scroll to the top of a page with just one click. Initially, I tested this functionality without using it as a component, and everything worked perfectly. However, now I want to turn this fabb ...

Failed to verify the myCon module on the localhost server due to an Internal Server Error with a status code of 500

Currently, I am verifying if the user-entered code matches the code stored in the database by sending an AJAX request in the view. In another function where I am using similar code, the request is successful and I receive a response. However, when attempti ...

Post your artwork on Facebook's timeline

I am working on a website that allows users to create custom smartphone cases. One feature I want to implement is the ability for users to share their design on Facebook, including the actual design itself. I have the object and the canvas with the 's ...

Is it necessary to include @types/ before each dependency in react native?

I am interested in converting my current react native application to use typescript. The instructions mention uninstalling existing dependencies and adding new ones, like so: yarn add --dev @types/jest @types/react @types/react-native @types/react-test- ...

Step-by-step guide for dynamically including dropdown options

Is there a way to dynamically add a dropdown using JavaScript? I currently have a dropdown with numbers that creates another dropdown when selected. However, I want to add the dynamic dropdown through JavaScript. How can I achieve this? Below is the PHP ...

One can only iterate through the type 'HTMLCollection' by utilizing the '--downlevelIteration' flag or setting a '--target' of 'es2015' or above

I'm currently working on developing a loader for my static grid. I've incorporated the react-shimmer-skeleton package source code, but I'm encountering issues with eslint in strict mode. You can find the respective repository file by followi ...

Show or conceal a class

Hello there! I've been attempting to create a toggle effect using an anchor link with an "onclick" event to show and hide content. Despite my efforts with jQuery and JavaScript functions, I just can't seem to figure out the right approach. Here& ...

Is it possible to extract attribute names of a DOM object using angular.element? If so, what is the process? If not, is there another way to access this information within Angular?

Is it possible to achieve this? If so, how can we accomplish it? Here is what I have attempted: // Assuming domObj is a dom element var ngObj = angular.element(domObj); var attrNames = ngObj[0].attributes; For example, let's consider the following d ...

Unable to retrieve data from response using promise in Angular 2?

I am struggling to extract the desired data from the response. Despite trying various methods, I can't seem to achieve the expected outcome. facebookLogin(): void { this.fb.login() .then((res: LoginResponse) => { this.acce ...