Assigning array materials in ThreeJS allows you to create complex

When I assign framemat to

createScene(ID, geometry, 1, framemat)
, everything works fine. But when I try
createScene( ID, geometry, 1, materials[ID] )
, it doesn't cooperate.


    var jsonLoader = new THREE.JSONLoader(),
    paths = [
        "obj/jgd/GALD-JGD-frame.json", 
        "obj/jgd/GALD-JGD-logo.json", 
        "obj/jgd/GALD-JGD-light.json"
    ],

    materials = [
        "framemat", 
        "logomat", 
        "lightmat"
    ],                  

    objNum = paths.length; 

for ( var id = 0; id < paths.length; id++ ) {

    (function(){ 

        jsonLoader.load( paths[id], function( geometry, materials ) { 

                createScene( id, geometry, 1, materials[id] ) 
            }
        );

    })(); 
}

The fun part of creating a scene:


function createScene( id, geometry, scale, material ) {
    geometry.computeTangents();
    objects[id]= new THREE.Mesh( geometry, material );
    objects[id].scale.x = objects[id].scale.y = objects[id].scale.z = scale;
    scene.add( objects[id] );
}

Answer №1

There seems to be some confusion in your code: you initially define an array called materials, but then you use the same name for the second argument of the JSONLoader to capture textures from a json file. Furthermore, you are attempting to access materials[id] when it's not clear if the correct index exists. This ambiguity can lead to errors and unexpected behavior.

To clarify the situation, consider the following options:

  • If you prefer creating materials independently, stick with the first array and implement a function that converts strings to materials. In this case, remove the second argument from the JSONLoader.

  • If you want to utilize materials from the JSON File directly, the initial materials array is unnecessary and may cause issues. You have two choices:

    • If there is only one material for the entire object, use:

      createScene(..., ..., materials);

    • If multiple materials are required, opt for:

      createScene(..., ..., new THREE.MeshFaceMaterial(materials));

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

Leverage a single JavaScript file across all Vue components without the need for individual imports

My project includes a JavaScript file called Constant.js that stores all API names. //Constant.js export default { api1: 'api1', api2: 'api2', ... ... ... } Is there a way to utilize this file without having to impo ...

Implement an AJAX link on the webpage using Yii framework

I'm currently working on developing a user interface for an application, and I've encountered an issue with my code that involves adding AJAX links using a Yii helper function. echo CHtml::ajaxLink('Link', array('getajaxlinks&apos ...

Tips for coordinating the execution of 2 async.waterfalls in Node.js

I have a series of read commands that need to be executed in sequence. If any of them fail, the processing stops. The array readCommands contains functions for reading... async.waterfall(readCommands, function(err) { if (err) { console.log(e ...

Efficient method for retrieving the values of each cell in a row within an array

I've got a standard table here - 6 columns, multiple rows, all filled with data. Right now, my goal is to collect all this table data into an array: tableData(0) = "1, Hans, Testperson, Munich, Germany" tableData(1) = "2, Petra, Tes ...

Tips for avoiding a form reload on onSubmit during unit testing with jasmine

I'm currently working on a unit test to ensure that a user can't submit a form until all fields have been filled out. The test itself is functioning correctly and passes, but the problem arises when the default behavior of form submission causes ...

The Twilio JWT authentication token has expired

I'm currently utilizing Twilio voice call functionality and everything is working smoothly. However, the Twilio JWT token expires every hour, forcing users to refresh the page periodically. I'm seeking advice on how to extend the token validity p ...

Navigate to a different page using Angular2 routing

Looking for guidance on using redirect in the new Angular 2 router. Specifically interested in examples similar to 'redirectTo' from the beta version. Any demos on 'plnkr.co' would be greatly appreciated! ...

Refresh the div based on the script's output

Currently, I am struggling to make a password change form work properly as I have limited knowledge of jQuery. The form successfully changes the password, but there is no visual feedback for the user. When I submit the form, it routes to changePassword.php ...

Angular 7 features a table with multiple timers indicating the remaining time until expiration

I have a table with multiple rows where I need to display a timer showing the remaining time in days, hours, minutes, and seconds. After researching how to calculate the remaining time using this link, I found a solution that works well. I am calling the ...

Unexpected TypeScript issue: Unable to access the 'flags' property of an undefined entity

Upon creating a new project and running the serve command, I encountered the following error: ERROR in TypeError: Cannot read property 'flags' of undefined Node version: 12.14 NPM version: 6.13 Contents of package.json: { "name": "angular-t ...

Having trouble getting the onClick event to trigger in React?

I have a button in my navbar that triggers a submenu (list of items) to display when clicked. Each item is a separate child component and I want them to trigger an event when clicked. However, the onClick event listener does not seem to be working. Other m ...

Perform a single click and a double click on an anchor element in the document

I am attempting to implement two actions when a user clicks on an anchor tag. The anchor tag will contain a video link. My goal is for the URL to open in a new window when the user single-clicks on the anchor tag, and for the use of the HTML5 download attr ...

Implementing Asynchronous Custom Validators in Angular 4

I've encountered the following code snippet: HTML: <div [class]="new_workflow_row_class" id="new_workflow_row"> <div class="col-sm-6"> <label class="checkmark-container" i18n>New Workflow <input type="che ...

Retrieving localStorage data from another webpage

I recently created an account and I hope my question is clear. I have two separate pages on my website - one for a menu and the other for an HTML game. The menu has two buttons, one to start a new game and the other to continue from where you left off. How ...

The hydration error in next js is causing this code to malfunction

Why am I encountering a hydration error with this code in NextJS? The Items variable is an array of ReactNode's. Any suggestions for an alternative approach? I've searched extensively for information but haven't found anything related to Nex ...

What's the deal with the max element indices/vertices in WebGL2?

In the process of developing a web component using WebGL2 (and three.js) with OES_element_index_uint enabled, I encountered an unexpected anomaly. When drawing a geometry using indexed vertices, strange lines appeared across the middle of the figure: http ...

Tips for eliminating nested switchMaps with early returns

In my project, I have implemented 3 different endpoints that return upcoming, current, and past events. The requirement is to display only the event that is the farthest in the future without making unnecessary calls to all endpoints at once. To achieve th ...

Despite successfully connecting to my webservice, the ajax function encounters an error

<script type='text/javascript'> $(document).ready(function () { $("#submit").click(function (e) { e.preventDefault(); var userName = $("#username").val(); var pwd = $("#password").val(); authenticate(use ...

What causes the error message "ng-model is undefined"?

Within my form, there are three angular-ui-bootstrap typehead elements <form> <div class="form-group"> <label for="fieldname" class="col-md-3 control-label">Name</label> <div class="col-md-6"> <input type="text ...

In the process of transforming my JavaScript code into JQuery

I'm struggling to convert my JavaScript code into jQuery, especially when it comes to calling the function for radio elements by name. The original JavaScript works fine, but I can't seem to get the jQuery version to work correctly. Index HTML ...