Three bizarre phenomena encountered when pushing mesh in JavaScript

Excuse my inexperience, I am quite new to javascript and THREE. In the code snippet below, I am attempting to add meshes to a list of objects:

geometry = new THREE.BoxGeometry( 1, 50, 1 );
for ( var i = 0, l = geometry.faces.length; i < l; i ++ ) {

    var face = geometry.faces[ i ];
    face.vertexColors[ 0 ] = new THREE.Color().setHSL( Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
    face.vertexColors[ 1 ] = new THREE.Color().setHSL( Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
    face.vertexColors[ 2 ] = new THREE.Color().setHSL( Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );

}

for ( var i = 0; i < list.length; i=i+2 ) {

    geometry.width = size_list[i];
    geometry.depth = size_list[i+1];

    console.log('geo');
    console.log(geometry.width);
    console.log(geometry.depth);

    material = new THREE.MeshPhongMaterial( { specular: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } );

    var mesh = new THREE.Mesh( geometry, material );
    mesh.position.x = list[i];
    mesh.position.y = 5;
    mesh.position.z = list[i+1];

    console.log('mesh');
    console.log(mesh.geometry.width);
    console.log(mesh.geometry.depth);

    scene.add( mesh );

    material.color.setHSL( Math.random() * 0.2 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );

    objects.push( mesh );

}

All the meshes are rectangles with varying sizes stored in size_list. To prevent program crashes, I have only one geometry object. When attempting to create multiple geometry objects per mesh within the loop, the browser couldn't handle it.

Instead, I modify the properties of the geometry object before creating each mesh and adding it to the object array. The values log correctly as they are changed. However, when viewing the scene, all meshes still display dimensions of (1, 50, 1) from the initial code line. My understanding of JavaScript leads me to believe the issue could be related to how it handles copies and references, but this behavior is unclear to me.

How can I effectively update the meshes without needing a new geometry object for each? And why does the current method not achieve the desired result?

Answer №1

To adjust the size of a mesh, modify its scale attribute.

Follow this method:

 var shape = new THREE.BoxGeometry( 1, 1, 1 ); // define initially

Then within your iteration:

var model = new THREE.Mesh( shape, material ); // reuse the shape

model.scale.set( width[ i ], height[ i ], depth[ i ] );

Implemented in three.js version r.73

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

Is there a way to retrieve real-time information using momentjs in a Vue.js application without needing to refresh the

I have a unique table where I showcase details about a particular website. Among the displayed information is the timestamp indicating when the data was last updated. At the top of the table, my aim is to include an icon that will only appear if the dura ...

Identifying the Operating System and Applying the Appropriate Stylesheet

I am trying to detect the Windows operating system and assign a specific stylesheet for Windows only. Below is the code snippet I have been using: $(function() { if (navigator.appVersion.indexOf("Win")!=-1) { $(document ...

Display an empty string when a value is NULL using jQuery's .append() function

To set an HTML value using the .append() function, I need to include AJAX data values. If the data set contains a null value, it will show as 'null' in the UI. I want to remove that 'null' and display it as blank. However, I can't ...

AngularJS - Move the <li> element to the top when its corresponding checkbox is selected

This particular question pertains to a topic that was previously discussed in another thread The scenario involves using a ng-repeat as shown below: <li ng-repeat="opt in namesCtrl.uniqueCars"> <input type="checkbox" ng-model="namesCtrl.filt ...

The three.js WebVR code example functions properly on the official threejs.org website, however, it fails to run on

After testing out the demo at on my Android 7.0 Samsung Galaxy 7 phone using the Chrome browser and Utopia360 headset, I was able to successfully enter VR mode. However, when I attempted to do the same thing with the exact same code on my local server, I ...

Utilizing ID for Data Filtering

[ { "acronym": "VMF", "defaultValue": "Video & Audio Management Function", "description": "This is defined as the Video and/or Audio Management functionality that can be performed on a Digital Item. The Video & Audio M ...

JavaScript code for validating two passwords is malfunctioning

I'm currently working on a registration form for my website and I'm trying to implement a JS script that compares two password inputs (password and repeat password) and displays a message saying "passwords are not the same." Below is the code I h ...

acquiring data via fetch (transferring information from javascript to php file)

I'm completely new to using fetch and not familiar with Ajax. Currently, I am attempting to transfer data from a JavaScript file (node.js server) to a PHP file (Apache server). The data being sent consists of 2 JSON values labeled as "a" and "b". T ...

Shortcuts for $scope variables in AngularJS templates

Within my controller, I often set: $scope.currentThing.data For different parts of my template, sometimes I require currentThing.data.response[0].hello and other times currentThing.data.otherStuff[0].goodbye //or currentThing.data.anotherThing[0].goo ...

What is the best way to adjust the size of the browser window when opening my page?

I have created a single page scrolling website specifically designed for children. The optimal viewing experience is at 1200 pixels wide. Is there a method to set the window size to that by default when visitors land on the site? ...

Linking JavaScript on the client side to a NodeJS application on the server side

I am new to NodeJS and I am currently exploring the app structure. I have developed a basic app using Socket.IO and MongoJS, which functions as a tracking system that gathers variables from a client-side script and stores them in Mongo. This is how I envi ...

An effective way to mimic an un-exported (private) function within a user module using Jest

In my code, I have a function that handles API requests called "private" and several other functions that initiate specific requests with configuration objects. For example, in the requestUploadStatementFile file. I want to test these public functions, bu ...

Enhancing visual effects with particle trails in Three.js

I want to create a unique sphere with a particle that follows a specific path between two points. The path should start and end at the sphere's radius, peaking slightly above it like an airplane in flight. While I have successfully made a particle m ...

When ran automatically as a cron job, the JavaScript script fails to establish a connection with MongoDB

As I connect to mongodb (on a Ubuntu machine) from a js script, everything runs smoothly when I execute the command from the command line (node <name of the script>). However, when I try executing the same command from cron, the connection times out: ...

Sending the HTML input value to a Knockout view

Can someone assist me with a dilemma I'm facing? Within CRM on Demand, I have a view that needs to extract values from CRM input fields to conduct a search against CRM via web service. If duplicate records are found, the view should display them. Be ...

Is there a way to transfer JSON data from a JSP page to a Spring REST controller efficiently?

Attempting to send data from a jsp page containing datetime, zoneid, checked checkboxes with values, and unchecked checkboxes with null values. Despite sending all this as json to my spring rest controller, I noticed in debug mode that the controller only ...

PHP not delivering a variable via AJAX

I've noticed that there are similar questions on this platform, but I've spent my entire day researching and fixing bugs to figure out why my ajax code doesn't return a response from the php file. All I need is for it to notify me when a use ...

dynamically open ngx-bootstrap accordion panels

I implemented the ngx-bootstrap accordion feature to display a list of blog posts. Below is the template structure: <accordion id="blog-list"> <accordion-group *ngFor="let post of posts; let first = first;" [isOpen]="first" id="post-{{post.i ...

The comparison between exposing and creating objects in a Nodejs router file

Recently, I began using expressjs 4.0.0 and was impressed by the express.Router() object. However, a dilemma arose when I moved all my routes to another file - how do I expose an object to the routes file? In my server.js file: ... var passport = ...

Utilizing the Fetch API to append JSON data to the document in Javascript

I am working on sending JSON data back to my express app using fetch and a POST method. Here is the code snippet I have: fetch('https://development.c9users.io/canadmin',{ method:'POST', body:JSON.string ...