Having difficulty displaying GLTF object using three.js

I am attempting to display a GLTF object provided by the guidance from three.js (https://github.com/mrdoob/three.js/blob/master/examples/models/gltf/DamagedHelmet)

In order to achieve this, I have used the loader mentioned in their documentation ()

Following the instructions from three.js, I have written the following code to load and render a GLTF file:

"use strict";
const loader = new THREE.GLTFLoader();
const scene = new THREE.Scene();
const ratio = window.innerWidth / window.innerHeight
const camera = new THREE.PerspectiveCamera( 75, ratio, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer();


const path = 'https://raw.githubusercontent.com/mrdoob/three.js/master/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf'

camera.position.set(0,0,0)
camera.lookAt(10,0,0)
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

renderer.render( scene, camera );


loader.load(path, function ( gltf ) {
  gltf.scene.position.x=10
  scene.add( gltf.scene );
}, function ( xhr ) {
  console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
 
}, function ( error ) {
  console.log( 'An error occurred' , path);
});

Unfortunately, even though the browser successfully loads the resource files from the internet and there are no errors in the console, the scene remains black.

Therefore, my question is: how can I correct my codepen to display a GLTF object in a web browser using three.js?

EDIT: thanks to the best answer, here's a working codepen example: https://codepen.io/spocchio/pen/vYJpaLg

Answer №1

I identified 2 issues:

  1. Your scene lacks proper lighting. Without lights, added meshes remain dark and unilluminated.
  2. You are only triggering renderer.render() once, and this occurs before loading the GLTF file. Therefore, no rendering takes place when the helmet is eventually loaded.

I made these necessary adjustments in your Codepen, resulting in the successful display of the helmet.

renderer.render( scene, camera );

// Adding ambient light to the scene
scene.add(new THREE.AmbientLight());

loader.load(path, function ( gltf ) {
  gltf.scene.position.x=10
  scene.add( gltf.scene );

  // Ensure scene is rendered after assets are added
  renderer.render( scene, camera );
}, function ( xhr ) {
  console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );
 
}, function ( error ) {
  console.log( 'An error occurred' , path);
});

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

Recharge Backbone prior to a lockdown

I'm currently utilizing a script within Backbone in a Cordova application (Android) that causes the app to freeze for 5 seconds, and unfortunately I am unable to find an alternative method. Due to this issue, I would like to display a loading message ...

The necessary parameters are not provided for [Route: sender] with the [URI: {name}/{code}]. The missing parameters are name and code

I have encountered a problem while using AJAX and I am struggling to find a solution. Routes: web.php Route::get('/{name}/{code}', 'TestController@counter'); Route::post('/{name}/{code}', 'TestController@sender')-&g ...

The determination of the volume value was impossible using only the values from the object nested within an array

I am currently developing a react app that includes a button labeled Add to create a new calculation. Each calculation consists of two input fields for thickness and surface, as well as an output field for Volume. There is also a button labelled "X" to rem ...

JS issue: Having trouble accessing the array values despite the array being present

I am working on an ajax call where I save the success data in an array. However, when I try to access that data outside of the ajax function and use console to log my array, it appears as expected. Here is a glimpse at what I see on the screen: https://i ...

Error JSON material for MeshFaceMaterial encountered

Successfully loaded my model using the following code: loader.load( "js/charWalk01.js", function( geometry, materials ) { mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial() ); scene.add( mesh ); } ...

Ways to adjust text color after clicking on an element

Just a heads up, I didn't write all of the web-page code so I'm not exactly sure what pdiv is. But I want to see if I can fix this small issue [making text color change when clicked on to show which section you're reading]. This particular ...

When the enter key is pressed, the anchor tag does not function properly after setting the tabindex value to "0" for the surrounding div element

Having an issue with Safari not focusing on my anchor element. I added tabindex="0" around the div to address this, but now the link doesn't work when pressing enter in any browser. Am I missing something here? <div class="used__button&quo ...

Steps to resolve the days.map error:

My map function is, days.map((val)=>val) Upon consoling the days prop, I receive the following output: [Array(7)] 0: (7) ['', '', '', 'Wednesday', '', '', 'Saturday'] length: ...

What is the secret behind Redactor JS's ability to display properly indented code snippets?

If you take a look at the Redactor JS demo and click on the button to show the HTML source code of the displayed content... ...you will notice that the code is properly indented: Most rich text editors or wysiwyg editors typically display code in a singl ...

Exploring Next.js: A Guide to Implementing Browsing History in Your Application

Struggling to add entries to browser history when using Next.js's Link property for page navigation. Unable to push history entry, leading to incorrect page location in my application when going back. Any ideas on implementing this feature in Next.js? ...

When you try to remove an element from an array by its index in Vue JS, the outcome may not be

Here is an array that I have: var hdr = ("name", "date", "start_time", "selling_item", "total_call", "end_time", "ad_num", "area", "order_num"); //this data comes from the database Now, I need to rename these array elements according to prope ...

Utilize Express.js to load a random HTML page

Hey there, it's Nimit! I could really use some assistance with my code. I'm trying to figure out if it's possible to load different HTML pages on the same URL like www.xyz.com/home/random. Can more than one HTML page be randomly loaded? I ...

Comparing Canvas and CSS3 for Website Development

Many discussions focus on comparing games with high levels of interaction, but my project involves a web application that manipulates objects individually. These objects can be images or text, and they can be replaced, resized, rotated, zoomed in, and dele ...

Encountering a Type Error in React Native when attempting to create a 3D cube with Three.js

Following a tutorial on creating a simple app that displays a 3D cube, I encountered an issue with my code: import React from 'react' import {View} from 'react-native' import Expo from 'expo' import {Scene, Mesh, MeshBasicMate ...

Exploring the depths of ng-repeat nesting and utilizing object properties

I am dealing with a complex object that includes various nested objects. Each object has a user's name property, which contains another object with group properties and their values set to either true or false. ReportModel.Taxonomy = { Authentica ...

What are some common applications of the QMap datatype in QML?

Let's say I have a simple QMap declared and initialized with values inside C++ and properly exposed to QML. C++ QMap<QString, QMap<QString, QString>> airflow_buttons_; //simple getter [[nodiscard]] QMap<QString, QMap<QString, QStr ...

notify a designated channel when ready for launch

I'm currently attempting to figure out how to send a message to a channel when the Discord bot is launched. I've experimented with this code: client.on('message', (message) => { client.on('ready', () => { channel = cli ...

Broadcast signals to an overarching frame

I have successfully embedded a chatbot (Angular 14 app) in an iframe and now I need to determine whether the frame should be minimized so it can fit within the parent container. My goal is to send custom events to the receiving frame. let iframeCanvas = do ...

Node.js is indicating that the certificate has expired

When using Mikeal's request library (https://github.com/mikeal/request) to send an https request to a server, I keep encountering an authorization error of CERT_HAS_EXPIRED. request({ url: 'https://www.domain.com/api/endpoint', ...

The state in the React components' array functions is not current

import React, { useState } from "react"; const Person = ({ id, name, age, deleteThisPerson }) => { return ( <div className="person"> <p>{name}</p> <p>{age}</p> <button onClick ...