Three.js Mesh is successfully loaded but not displaying on the screen

I'm currently attempting to add texture to a sphere using Three.js while running a Python server.

The console displays "Success! image-size: 1024 x 1024". I am having trouble identifying the issue.

Main.js:

const scene = new THREE.Scene()
const geometry = new THREE.BoxGeometry(1, 1, 1)
const texture = texLoader.load('Fabric_Lace_026_height.png', 

// If successful
function(texture) {
    console.log("Success!");
    console.log(texture);
},

// Progress (not utilized)
undefined,

// In case of error
function(err) {
    console.log("Error");
    console.log(err);}
);

const material = new THREE.MeshBasicMaterial({map: texture})
const mesh = new THREE.Mesh(geometry, material)
scene.add(mesh)

const sizes = {width: window.innerWidth,height: window.innerHeight}


const camera = new THREE.PerspectiveCamera(75, sizes.width /sizes.height, 1, 1000)
camera.position.z = 3

const canvas = document.querySelector('canvas.webgl')

const renderer = new THREE.WebGLRenderer({canvas: canvas})
renderer.setSize(sizes.width, sizes.height)
renderer.render(scene, camera)

Answer №1

If you find that your scene is not rendering properly, it may be because you are rendering it too early. Without an animation loop in place, make sure to call the render() function only after the texture has been fully loaded. Here's a way to do it:

const scene = new THREE.Scene();

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 3;

//

const loader = new THREE.TextureLoader();
const texture = loader.load('https://threejs.org/examples/textures/uv_grid_opengl.jpg',

  // On success
  function(texture) {
    renderer.render(scene, camera); // FIX
  },

  // Progress (ignored)
  undefined,

  // On error
  function(err) {
    console.log(err);
  }
);

//

const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({map: texture});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

//

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
body {
      margin: 0;
}
<script src="//cdn.rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>

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

What is the best way to combine cells within a single column?

Exploring the code snippet utilizing ag-grid with Vue 3. <script setup lang="ts"> import "ag-grid-community/dist/styles/ag-grid.css"; import "ag-grid-community/dist/styles/ag-theme-alpine.css"; import { AgGridVue } from ...

How to transform a hyperlink into a clickable element within an absolutely positioned container

I'm currently working on a photo gallery project and using magnific popup to create it. However, I ran into an issue with the hover event on the images that displays an icon when it's hovered over, indicating that it can be clicked to view more. ...

What is the approach for obtaining the specified arrays using two pointer methodology in JavaScript?

input [1,8,9] output [[1],[1,8],[1,8,9],[8],[8,9],[9]] It appears to be a subset array, but I am aiming to achieve this output using a two-pointer approach. Let's assign leftP=0, rightP=0; and then utilizing a for loop, the rightP will iterate until ...

What is the best way to modify directives in response to updates in services?

In my directive (parent-directive), I have a slider called mySlider. When the slider is stopped, it triggers an event that calls an Angular $resource service with two parameters. The service then returns an object. The structure of the directives is as fo ...

Executing AJAX calls within a forEach loop

I'm facing a challenge with a function that carries out a foreach loop on a list of views and needs to make an AJAX request for each view within the loop. Upon receiving the results in the success callback, it checks if a specific ID is returned and, ...

Troubleshooting textarea resize events in Angular 6

In the development of my Angular 6 application, I have encountered an issue with a textarea element. When an error occurs, an asterisk should be displayed next to the textarea in the top-right corner. However, there is a gap between the textarea and the as ...

Update the color of buttons after being clicked - Bootstrap

If I want to change the class "btn-success" to "btn-warning" or update the color code to "#ffc107" upon a successful process, how can I achieve that? Button ; <button type="submit" name="teklifver" value="Teklif Ver" class="btn btn-block btn-success" ...

Converting form fields with varying sets of data into JSON using JavaScript

I am currently working on a form that contains multiple rows with three fields each - a hidden id, code, and exchange. <form id="form_in_question"> <table> <tr> <td>0</td> <td> ...

Is there a way to automate the duplication/copying of files using JavaScript?

I have a GIF file stored in the "assets" directory on my computer. I want to create multiple duplicates of this file within the same directory, each with a unique filename. For example: If there is a GIF file named "0.gif" in the assets directory, I woul ...

Stylish Navigation Menu Logo/Site Name

Currently in the process of upgrading my website and I have my website name included as part of my menu, as opposed to a logo. This has been an easy solution that has worked well for me. For mobile screens, the template I purchased includes a slicknav men ...

Submit a JSON message to the Mandrill API using an HTML form

I am looking to incorporate a HTML contact form on my website and send an email without using PHP. I have attempted to use the Mandrill API to send a JSON message. However, despite setting up my function and calling it from the onsubmit, I am not receivi ...

Design a custom shader to simulate the transition between day and night on Earth, while also incorporating interactive

My query consists of multiple components, kindly address each part separately as I understand that each one comes with its own set of unique challenges. First off, I am in need of assistance in creating a shader that seamlessly transitions between depicti ...

Implementing this type of route in Vue Router - What are the steps I should take?

I am currently delving into Vue and working on a project that involves creating a Vue application with a side menu for user navigation throughout the website. Utilizing Vue Router, my code structure looks like this: <template> <div id="app ...

How to access an HTTP website from an iframe hosted in an HTTPS parent window

I'm currently troubleshooting an SSL issue that is being caused by insecure content loaded within an iframe. Both of my domain addresses are hosted on the same server, but only one has SSL. When I access the first site with SSL and then navigate to th ...

What is the most effective way to create a straightforward user interface in Node Js for the purpose of automation?

Currently, I am employing Selenium webdriver alongside Javascript and Node JS for automating test cases. My initial test case looks like this : var webdriver = require('selenium-webdriver'); var driver = new webdriver.Builder(). withCapabili ...

Can someone show me how to implement arrow functions within React components?

I am facing an issue while working on a node and react project. Whenever I use an arrow function, it shows an error stating that the function is not defined. Despite trying various tutorials and guides, I am unable to resolve this issue. Below is the snipp ...

Having trouble with your jQuery animation on a div?

Check out this jsFiddle example: http://jsfiddle.net/uM68j/ After clicking on one of the links in the demo, the bar is supposed to smoothly slide to the top. However, instead of animating, it immediately jumps to the top. How can I modify the code to ac ...

There seems to be an issue with receiving data through Express.js's req

Currently, I am in the process of learning how to use Node and Express in order to develop an API for an upcoming Angular application that I will be working on. However, I have encountered an issue where the req.body appears to be empty whenever I attempt ...

What is the process of integrating an EJS file into the app.js file?

Within my index.ejs file, I have included a script tag containing JavaScript for DOM manipulation. Now, I am looking to access a variable declared within this file from my app.js file. How can I make this happen? When referencing a variable in an ejs file ...

Sending JSON data to an API endpoint

I am having trouble with making a JSON Post request to the Simpleconsign API, as I keep getting a 0 error from the browser. Below is my code snippet: <script type="text/javascript"> JSONTest = function() { var resultDiv = $("#resultDivContainer ...