How can I fix the "t is undefined in three.js" error when adding elements to a scene from an array of mesh?

For the sake of simplifying things, I have removed all unnecessary details in this example in the hopes that it will make it easier for you to assist me.

Checkout this link for more information

Another helpful resource can be found here

The main distinction between the two is that 'scene.add()' has been commented out.

I have created arrays consisting of geometry, materials, and meshes. Although everything seems to be in order, adding the mesh to the scene is proving to be a challenge. I am hopeful that someone can provide me with guidance in the right direction.

View the screenshot of the inspection process

Best regards, newb

Answer №1

After -Sammy shared their response on this thread... Exploring THREE.JS With Multiple STL Meshes

I was able to streamline my code significantly by adding each mesh and material in sequence, assigning them names, and pushing them into arrays for easy reference.
Many thanks to -Sammy for the insight!


const loader = new THREE.STLLoader();   
const meshArray = [];
const matArray = [];
const fileList = ["https://decadentlab.s3.amazonaws.com/cases/001/Upper.stl", "https://decadentlab.s3.amazonaws.com/cases/001/Lower.stl", "https://decadentlab.s3.amazonaws.com/cases/001/Mockup.stl", "https://decadentlab.s3.amazonaws.com/cases/001/Ideal.stl" ];

var callback = function ( geometry ) { 
    const matColors = ["0xE2D4B7", "0xAF9164", "0x437C90", "0x9EBC9E", "0xFCAB64", "0xAE8799", "0x496DDB", "0x717EC3", "0xC95D63", "0xF2AF29"];
    var mats = new THREE.MeshPhongMaterial({
    color: matColors[i],
    specular: 0x555555,
    shininess: 10
    });
    mats.side = THREE.DoubleSide;
    mats.transparent = true;
    mats.opacity = 1;
    mats.name = "material" + i;
    matArray.push( mats );
    console.log( mats );
    var mesh = new THREE.Mesh( geometry, mats );
    mesh.position.set(0, 0, 0);
    mesh.rotation.set(5.5, 0, 0);
    mesh.name = "mesh" + i;
    meshArray.push(mesh);
    console.log( mesh );
    scene.add( mesh );
    i++;
    if (i < fileList.length)  // initiate the next load process within the callback
        loader.load( fileList[i], callback ) ;
};

i = 0;
loader.load( fileList[i], callback ); 

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

Read special characters such as ä, ö from standard input using Node.js

I attempted to read input from the console in node.js using the code snippet below process.stdin.setEncoding("utf8"); process.stdout.setEncoding("utf8"); process.stdin.on('data', function(data) { process.stdout.write('data: ' + dat ...

Retain elements in a numpy array that meet multiple conditions or criteria

Is there a more efficient way to keep values of an array that satisfy multiple conditions? For example: a = np.array([1,3,5,6,4,6,7,8,9]) If I want to filter out values that are greater than 3 and less than 7, my desired output would be: array([5, 6, 4, ...

Is every type of renderer suitable for handling textures effectively?

In this particular scene, we have an earth spinning on its axis, a moon orbiting around the earth, and a light source positioned to the right to create the illusion of an eclipse. Initially, I assumed this task would be straightforward given our prior expe ...

Why am I unable to access all elements within the map function?

Hey there, I have a function related query. Whenever I try to access my data, I can only reach the first index of each array. For instance, I have 5 different images of PlayStation, but on my webpage, I am only able to see one image. How can I resolve this ...

I am unable to submit the form to the server

I'm experiencing an issue with my login form. I've tried submitting it using jQuery, but it's not working as expected. I've searched online for solutions, but they all seem too complex. Here is the code I'm working with: <form ...

Simplifying the structure of deeply nested callbacks

Struggling with learning how to work with callback style programming in Node.js. Specifically, I'm having issues with a query to a MongoDB database. When I pass in a function to execute on the result, it works fine - but I would prefer to simplify and ...

Ionic: How come my image is not loading with HTTP.GET?

I have been attempting to populate a gallery in my Ionic application by fetching images from a JSON file, but I am encountering issues. While following a guide on creating a grid-like image gallery using the Ionic framework (https://blog.nraboy.com/2015/03 ...

Is there a way to retrieve an attribute from a nested object in a loop while using VueJS?

Currently, I am facing an issue while trying to access a property within a nested object using a loop in my vuejs project. It's strange because similar nested objects work fine except for one. <template> <div> <tr v-for=" ...

Error in NextJS: The module 'caniuse-lite/data/features/css-unicode-bidi' could not be located

Issue encountered during the build process. Error: Module 'caniuse-lite/data/features/css-unicode-bidi' not found Attempted to include "caniuse-lite": "^1.0.30001281" and "^1.0.30001390", but the problem persists. ...

Ways to access information received from AngularJS in a different Javascript file

I am currently using Angular to retrieve output from a controller and display it using ng-bind. However, I have another separate JavaScript file that needs to utilize a value returned from Angular. In the example code provided below, the TestAppCtrl is ca ...

Unusual occurrence in Chrome when checking definitions: ReferenceError: x is not defined

Recently, I've come across some odd behavior in Chrome following its latest update. Whenever I try to determine if a variable is defined, it ends up triggering an uncaught error like the one shown below: if(x) { alert('x is defined.'); } T ...

Getting just the main nodes from Firebase using Angularfire

I'm having trouble figuring out how to print just the parent element names from the structure of my database. The image provided shows the layout, but I can't seem to isolate the parent elements successfully. ...

Dealing with multi-line strings in Javascript

I am having an issue with my code: let testStr = "Asdfads"; let htmlStr = `<script>${ testStr }</script>`; // but it works with: <div>${ testStr }</div> console.log(htmlStr); When I run this code, I receive the following error: ...

Bring in JS into Vue from the node_modules directory

Apologies for my limited knowledge in this area, but I am currently working on integrating and importing This Grid System into my Vue project. However, I am facing some challenges. Typically, I import plugins like this: import VuePlugin from 'vue-plu ...

Creating a stylish border around an image using a JavaScript function

I am interested in creating a JavaScript function called addborder(y) that will add a yellow border to an image. This experiment aims to explore the process of manipulating images using JavaScript. <!--HTML--> <img id="img" src="http://pre01.d ...

During the execution of my asynchronous javascript, it interrupts the flow of the other function

How can I ensure that a function executes after another one in Vue.js? I've tried various methods like async/await, callback functions, and .then, but none seem to work seamlessly. Any suggestions for a possible solution? auth_util.js: async auth () ...

Delay Export of React Component Until After Request in Shopify App Development

Being a newbie in Shopify App Development, React, and Next.js, I may have a silly question. Currently, I am making a request to a website and using the response in the React component that I want to export/render. To avoid it being undefined, I need to wai ...

Successfully connected to the database, however the retrieved information is being returned as a

$db_connection = mysqli_connect('localhost', 'root', '', 'fireworks'); $query = "SELECT * FROM name"; $result = mysqli_query($db_connection, $query) or die(); while ($info = mysqli_fetch_assoc($result)); { var_d ...

Using Google Search API to populate a MySQL Database with data

Seeking assistance and understanding for my query. I am looking to extract data from the Google Search API, save it into my database, and then display it. Despite using ajax.html, I encountered an error. The specific data I aim to store includes snippets, ...

Adjusting the alignment of the horizontal bars in react-chartjs-2

I have configured the graph with the following settings: { type: 'horizontalBar', indexAxis: 'y', barThickness: 12, scales: { x: { suggestedMax: 6, suggestedMin: 0, grid: { display ...