Having trouble with loading textures in Three.js?

I'm facing an issue while trying to apply a texture to my mesh using three.js. Instead of the expected loaded texture, I am just seeing a plain black object. I followed the examples provided in the three.js documentation ( - Example) and also tried various solutions without success. Here is the code snippet:

//Creating The Sun model
const sunGeometry = new THREE.SphereGeometry(16, 30, 30);
const texture = new THREE.TextureLoader().load('assets/images/sun.jpg');
const sunMaterial = new THREE.MeshBasicMaterial({ map: texture }); // not working
//const sunMaterial = new THREE.MeshBasicMaterial({ color: 'skyblue' }); //working
const sun = new THREE.Mesh(sunGeometry, sunMaterial);

//Adding The Sun to the Scene
scene.add(sun);

renderer.render(scene, camera);

Changing the mesh color works perfectly fine, but applying textures is not successful. I have also checked the image path and everything seems correct.

Answer №1

Immediately after creating your Mesh, you are calling renderer.render(scene, camera);. However, at that point, the texture has not yet loaded. Textures typically take a short amount of time to load, so by the time they are ready, the rendering process has already occurred. This might not be noticeable if you are rendering at 60 frames per second, but if you are only rendering once, you will end up with a black material.

If you want to ensure that render() is called only after the texture has loaded, it is advisable to use the onLoad callback as described in the code example on the documentation page here.

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

Organizing communications in NodeJS messaging application

My latest project involves creating a chat room using NodeJS and socket.io which allows multiple users to connect with unique usernames. Everything is running smoothly, except for one minor issue. I want the messages sent by me to appear in a different co ...

Managing different authentication methods for a single user on Firebase: Tips and Strategies

Currently, I am in the process of developing an authentication system utilizing Firebase. My aim is to incorporate email/password, Google, and Facebook as sign-up and sign-in methods. Initially, everything runs smoothly when a user signs up using each met ...

ajax always returns the same value, even when the input value is different

Each time I click on a different value, the console.log keeps giving me the same value. view image description I have checked the HTML code and found that even though each value attribute is different, it still returns the first value of the attribute. vi ...

Can Vue instances support private computed properties?

Vue is a versatile tool that I utilize for both service classes and components. When it comes to reactive computeds, they prove to be incredibly beneficial. However, I often find myself wanting a clear way to differentiate between public interface compute ...

Incorporate a binary document into a JSPdf file

I am currently utilizing JsPDF to export HTML content into a downloadable PDF. Explore the following example which involves taking some HTML content and generating a downloaded PDF file using JsPdf import React from "react"; import { render } fro ...

¿What is preventing me from merging two arrays within this query handler?

I'm facing an issue while trying to merge arrays from a request with existing arrays in a MongoDB database. Despite my attempts, the arrays do not seem to be merging as expected. Can anyone help me identify what might be causing this problem? router.p ...

Can the color of an icon be altered by selecting a list item in VueJS?

I am currently working on a VueJS Dropdown Menu implementation that involves displaying an icon. When this icon is clicked, a Dropdown Menu containing 3 other icons should appear. Furthermore, upon clicking one of these icons, I intend for the original Dro ...

Looking to preview files on a server before downloading them? If you're getting the error message "Not allowed to load local resource: blob,"

Update: To clarify, we are using <embed #reportPdf width="100%" height="800"> and: this.pdf.nativeElement.src = this._window.URL.createObjectURL(this.re); It functions properly on Safari and Firefox. However, when loaded on Chrome, it displays as ...

Difficulty in connecting React to Node.js with the use of axios

Recently, I embarked on a project using React and Node to create an app that allows users to add people data to a database. The frontend is built with React and can be accessed at localhost:3000, while the backend, developed with Node, runs on localhost:33 ...

It's time to launch my Node.js app - but wait, express.createServer() is no longer supported!

I recently downloaded a Node app to experiment with, and after researching, I found that Express is considered a bit outdated. Can anyone assist me in fixing the code implementation? Here is the code snippet: /** * Module dependencies. */ // base depe ...

Deactivate the submit button when the form is not valid in angularjs

I am currently facing a challenge with my form that contains multiple input fields. To simplify, let's consider an example with just two inputs below. My goal is to have the submit button disabled until all required inputs are filled out. Here is wha ...

Encountering the error message "require is not defined" while running tests in AngularJS with Karma

I have implemented AngularJS with Browserify for my app development. To conduct testing, I have decided to utilize Karma. The configuration file I have set up is as follows: module.exports = function(config) { config.set({ basePath: '', ...

Is it always the case that modifying the props of a child component will trigger a re-render of the parent component, even

I am currently exploring ways to prevent a modal element from re-rendering when it is supposed to be hidden. The tutorial I am following suggests converting the component to a class-based one and using shouldComponentUpdate(). However, I wanted to test if ...

Enhancing your Selenium test case strategies for better performance

I've created a test case that compares two arrays, removing matching elements and throwing an exception for non-matching ones. Although it's functional, the test is quite long and messy. Can anyone suggest ways to optimize or improve it? System ...

Experience the dynamic bouncing marker feature in Next.js React-Leaflet with the powerful tool Leaflet.SmoothMarkerB

I'm a beginner in front-end development and I'm attempting to create a bouncing marker in React-leaflet using the leaflet.smooth_marker_bouncing plugin version 1.3.0 available at this link. Unfortunately, I couldn't find enough documentation ...

When using momentJs to add days, it will return a moment object rather than a

How can I add 7 days to a date using momentJs in my Angular project? let startDate = "2018-08-16T02:00:00.242Z"; let newDate = moment(startDate).add(7, 'days'); I was expecting to receive the date after adding 7 days, but instead I get a momen ...

What is the best way to update the value of a specific key in discord.js?

As I struggle to explain properly due to my limited English proficiency, I am reiterating my question. In my config.json file, there is a key named "status" with a corresponding value of "online". I am attempting to change this value but haven't been ...

Encountering an error message stating 'click' property is undefined when implementing Java Script Executor in Selenium

I encountered an issue: Cannot read property 'click' of undefined when attempting to click a button using JavaScript executor. Despite trying multiple approaches such as action classes and WebDriverWait, I have been unable to successfully click ...

Trim the beginning and the conclusion of an Extruded SVG within Three.js

Struggling with extruding an SVG in three.js and running into some issues with the resulting code. Looking to eliminate the beginnings and ends of the model similar to the reference image for a more polished effect. Any suggestions on how to achieve this ...

"Arranging elements in a stack using Bootstrap's collapse

Currently, I am utilizing the collapse feature of Bootstrap 4.0.0 to show and hide a bootstrap "row". Within this row are four columns - "col-md-3" - containing buttons. The layout of this row is exactly as I desire until I include the "collapse" class, at ...