Issue with assigning material to a three.js object!

Being new to three.js, I have recently started exploring it. However, I am facing an issue where I am unable to assign a material to an object.

var objloader=new THREE.OBJLoader();
var material=new THREE.MeshNormalMaterial();
objloader.load('man.obj', function(object) {
    object.position.y=-30;
    object.material= material;
    scene.add(object);
    render();
});

Answer №1

When assigning materials, make sure you are doing so to Meshes specifically. The object may not be a mesh itself, but it could have a mesh as one of its children. . To achieve this, you can use the following code snippet:

object.traverse( ( obj ) => {

    if ( obj instanceof THREE.Mesh ) obj.material = material;

} );

This will set the material for all mesh children associated with that object.

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

Angular 2 isn't triggering the custom validator for every keypress

I have implemented a custom validator for Reactive forms in Angular 2. However, I am facing an issue where my function is only validating the first key press in the text field. Ideally, the custom function should validate each key press. Can someone please ...

Exploring nested routes with HashRouter in React

I've been working on a dashboard/admin control panel application using React, but I'm facing some challenges when it comes to handling component rendering accurately. Initially, my main App component is structured like this: <React.Fragment&g ...

Updating dropdown values using another dropdown through AJAX in Node.js: A step-by-step guide

My dilemma involves two dropdown menus: one for selecting the main category and another for choosing a subcategory. The goal is to dynamically populate the subcategory based on the main category selection. In my attempts so far, I have utilized JQUERY and ...

Troubleshooting error in data structure for nested dynamic routes with Next.js getStaticPaths

I am working on a page called [categories][price].js and I am attempting to achieve a particular data structure within the getStaticPaths function. For example, I want to have paths like cat1/10, cat1/20, cat1/30, cat2/10, cat2/20, etc. I came across this ...

What steps should be taken to develop a Hybrid Mobile App concept?

We are currently developing our first hybrid mobile application with a monetizable idea in mind. After conducting some research, it seems that to reach our end goal we will need: A Front End UI Framework: options include Ionic or AngularGap (although d ...

JavaScript loop through an array of objects

Is there a way to loop through and retrieve all of the addresses? const name = { john: [ { age: 21, address: 'LA', } ], sam: [ { age: 26, address: 'California' } ] } I have tried using th ...

Keep the Bootstrap dropdown menu open when the search bar is in focus

I am working on a customized bootstrap dropdown menu that needs to remain open when the search bar within the Events dropdown is focused. I have managed to make it open and close on hover, but now I need it to stay open under certain conditions. Below is ...

Executing functions within express - a mystery

I am struggling with a back-end JavaScript issue in Express. For some reason, when I call my function, it is returning undefined. Here's how it looks: // express route structure: exports.check = function(req, res) { // check if the username is prese ...

Attempting to apply a class to selected elements, however, the conditional statement is not functioning as intended

<div> <div class=category> Special Occasions </div> <div class=category> Holidays </div> <div class=category> Graduations </div> <div class=category> Retirements </div> </div> <ul> <li c ...

When encountering a 404 redirect, CSS and JS files may fail to display

I created a unique error message using .htaccess, incorporating CSS and JS in separate folders. Although it functions properly when accessed directly, integrating these resources into the 404 Error message results in only the HTML text being displayed with ...

a function that repeats every single second

A challenge I am facing is creating a countdown timer with a time that refreshes every second. My current implementation uses setInterval, but it only seems to run once instead of every second. Can anyone help me identify the issue in my code? var countDo ...

Unveiling the Mystery: How Browser Thwarts Server Push in HTTP/2.0

After studying the documentation of HTTP/2.0, I discovered that it is feasible to completely close a referenced stream using the RST_STREAM frame. Check it out here: ()! I am curious about how this feature can be implemented in popular web browsers such a ...

What is the process for obtaining the Tag from a React Component?

Not the HTML DOM element tag, the JSX tag, the react class name. As I work on creating an editor, adding items to the canvas array requires me to check and call the appropriate method based on what is being added. A simplified version of my idea: changeS ...

Tips for simulating a service in Angular unit tests?

My current service subscription is making a promise: getTaskData = async() { line 1 let response = this.getTaskSV.getTaskData().toPromise(); line 2 this.loading = false; } I attempted the following approach: it('should load getTaskData', ...

Exploring Rxjs through fundamental queries

Currently, I am working on a small application using Angular 2 and have installed Rxjs 5. However, I have encountered various ways of importing the Rxjs library in tutorials. The code mentioned in the Angular 2 documentation is not functioning properly i ...

Why is the updated index.html not loading with the root request?

I'm currently working on an Angular app and facing an issue with the index.html file not being updated when changes are made. I have noticed that even after modifying the index.html file, requests to localhost:8000 do not reflect the updates. This pro ...

What is the best way to adjust the width of these elements for a perfect fit?

I'm currently working on a website where I have arranged squares in a grid layout. My goal is to make these squares perfect, with equal width and height. The only issue I'm encountering is that they tend to change between fitting two and three sq ...

Can a jQuery/JavaScript script be run standalone?

I've got a bunch of HTML pages saved on my computer and I'm looking to create a JavaScript script that can extract specific text or elements from those pages. I found some jQuery code snippets on Stack Overflow that do what I need, but I'm n ...

Troubleshooting: Issue with Updating Prototype Ajax Function

I am currently utilizing Prototype within the pylons framework and attempting to execute an Ajax call. Below is the structure of my html: <form method="POST" action = "javascript:void(0)" onsubmit = "new Ajax.Updater('graph','/saffron_m ...

Is there a way to improve the efficiency of this JavaScript code?

I'm looking to optimize the code below in order to replace each link's href querystring with the current page URL's querystring if the current page URL has an argument 'myid1' or 'myid2' in the querystring. I want this sc ...