The rendering function of the model is not functioning

I've been struggling to incorporate my blender model into my website using three.js. Despite following various tutorials, I can confirm that the model works fine outside of three.js. If there's something crucial that I'm missing or need to download, any assistance would be greatly appreciated. I've searched high and low but haven't been able to find a solution.

import {GLTFLoader} from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32465a40575772021c03030a1c03">[email protected]</a>/examples/jsm/loaders/GLTFLoader.js';
import {OrbitControls} from 'https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="deaab6acbbbb9eeef0efefe6">[email protected]</a>/examples/jsm/controls/OrbitControls.js';

class BasicWorldDemo {
  constructor() {
    this._Initialize();
  }

  _Initialize() {
    this._threejs = new THREE.WebGLRenderer({
      antialias: true,
    });
    this._threejs.shadowMap.enabled = true;
    this._threejs.shadowMap.type = THREE.PCFSoftShadowMap;
    this._threejs.setPixelRatio(window.devicePixelRatio);
    this._threejs.setSize(window.innerWidth, window.innerHeight);

    document.body.appendChild(this._threejs.domElement);

    window.addEventListener('resize', () => {
      this._OnWindowResize();
    }, false);

    const fov = 60;
    const aspect = 1920 / 1080;
    const near = 1.0;
    const far = 1000.0;
    this._camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
    this._camera.position.set(75, 20, 0);

    this._scene = new THREE.Scene();

    // Lights setup

    // Controls setup

    // Textures setup

    // Models setup
  
    this._RAF();
  }

  _LoadModel() { 
      const loader = new GLTFLoader();
      loader.load('./resources/untitled.glb',  (gltf) => {
          gltf.Scene.traverse(c => {
              c.castShadow = true;
          });
          this._scene.add(gltf.scene);
      });
  }

  _OnWindowResize() {
    this._camera.aspect = window.innerWidth / window.innerHeight;
    this._camera.updateProjectionMatrix();
    this._threejs.setSize(window.innerWidth, window.innerHeight);
  }

  _RAF() {
    requestAnimationFrame(() => {
      this._threejs.render(this._scene, this._camera);
      this._RAF();
    });
  }
}

let _APP = null;

window.addEventListener('DOMContentLoaded', () => {
  _APP = new BasicWorldDemo();
});

Answer №1

I've noticed a few issues that need attention.

Solutions

Firstly, the use of traverse on gltf.Scene with a capital S is incorrect. It's surprising that your browser didn't raise an error for this. Instead, you should utilize gltf.scene as you did when adding it to your scene.

Additionally, your code snippet does not make a call to _LoadModel. This could be a major reason why your model isn't appearing, unless it's being called elsewhere in your script.

Additional Points

The structure of your implementation is unfamiliar to me, but aside from the mentioned issues, it should function properly. If not, perhaps consider simplifying certain sections such as consolidating everything from _Initialize into the constructor method, and reorganizing your animation loop to avoid creating a new function each iteration. It's possible that there may be scope-related issues causing problems.

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

Oops! Before proceeding, make sure to call TestBed.initTestEnvironment() first

Currently, I am experimenting with testing a service in Angular. Below is the code snippet I am working on: describe('AddressService', () => { let service: AddressService; let injector: TestBed; let httpTestingController: HttpTesting ...

encountering a soap error while attempting to access an ASP.NET web service

My ASP.Net test web service is operational, but I am consistently encountering 500 errors stating: "System.InvalidOperationException: Request format is invalid: text/xml. at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System ...

Encountering difficulty in retrieving data from an unidentified JSON array using Javascript

Exploring the realm of Javascript and JSON, I find myself faced with a challenge - accessing values in an unnamed JSON array. Unfortunately, as this is not my JSON file, renaming the array is out of the question. Here's a snippet of the JSON Code: [ ...

Looking for guidance on JavaScript - Implementing a different font family for each div element

I need help with customizing the font family for each individual post on my website. Currently, I am able to assign a cursive font to all posts within a class, but I would like each post to have its own unique font. Since I am doing this on load, I am fa ...

Using ajax to send an array to PHP

I have an array named "heart" that is being sent via ajax... var heart = [31,32,33,34,35,36,37,38,39,42,43]; // Sending this data via ajax to php file/ $.ajax({ type: "POST", data:{ 'system': heart }, url: "login-function.php", success: f ...

Implement a feature in Vuejs where the user can easily move to the next field by simply

I need help implementing a feature that moves the focus to the next field when the enter key is pressed. I've tried the following code, but it's not working as expected. When I added a debugger in the focusNext method inputs[index + 1].focus();, ...

How to submit form data with a POST request in Flask using fetch without having to reload

Despite reading numerous similar questions, I am still unable to determine how to achieve my goal. I have multiple forms on a single page and I am trying to submit data from each form without refreshing the page. Below is an example of one of the five form ...

What methods does Flipkart employ to access DOM elements for integration testing purposes?

Something that caught my eye recently is that Flipkart uses mostly random class names in their DOM elements, reminiscent of the styled-components library. I'm curious to know how they go about accessing these DOM elements for integration testing. UP ...

Tips for adding a text input field within a dropdown menu

Could someone provide guidance on how to place an input text box within a dropdown without using bootstrap? I came across this image showing what I am looking for: https://i.stack.imgur.com/f7Vl9.png I prefer to achieve this using only HTML, CSS, and Jav ...

Using a checkbox to enlarge a table

<script type='text/javascript' src='http://code.jquery.com/jquery-1.4.2.js'></script> <script type='text/javascript'> $(window).load(function () { $('.varx').click(function () { ...

Steps for converting a vector using an offset vector

How do I translate a vector using an offset vector? I have a line with two points attached - one at the start of the line and one at the end vector of the line. https://i.sstatic.net/Q4yZm.png My goal is to translate the line and the points based on the ...

Who is the intended audience for the "engines" field in an npm package - consumers or developers?

As the creator of an npm library, I have included the current LTS versions of Node.js and npm in the package manifest under the engines field. This ensures that all contributors use the same versions I utilized for development: Node.js <a href="/cdn-cgi ...

Having trouble toggling journal entries in an HTML journal? The Jquery function might not be working properly

I have been tasked with creating a civil war journal for my 8th grade Social Studies class and I decided to present it as an HTML file featuring the title and date of each journal entry. The goal is to allow users to click on each entry to open it while au ...

Soft keyboard on mobile fails to update when arrows are used in Ajax-populated dropdown menus

I am working on a web form that includes two select fields: Country and City: <select id="country" onchange="getCity(this);"> <option value="">-- Please select your country --</option> <option value="1">Austria& ...

What steps can I take to modify the peeking effect from hover to scrolling?

I have successfully integrated a peeking effect on my website. Check it out here: http://jsfiddle.net/7yrWL/1/ Currently, the effect triggers when I hover over the image. What I want now is for the peeking effect to only activate when the user scrolls to t ...

What is the best way to retrieve the value of a button using javascript?

After trying to extract the value from a button in JavaScript, here is the code I used: for(i=0; i<cars.length;i++){ var p = `<button id="myBtn" onclick="myFunction()" value="${cars[i]}">${cars[i]}</button>` func ...

Why doesn't the 'Range' input type slide on React.js documentation, but it does on CodePen?

Can someone help me figure out why my range slider is not working in my React app? I copied the code from Codepen where it works fine, but in my app, it's not functioning properly. The slider doesn't slide when dragged and clicking on it doesn&a ...

Using AJAX asynchronously in JavaScript commands synchronization

After researching similar questions on SO without finding a satisfactory answer, I encountered an issue with synchronous AJAX calls in my HTML/JavaScript page. Mozilla has deprecated some functionality related to synchronous requests, making it necessary f ...

Issue with AngularJS: Unable to add a new property to an object

Here is the Javascript code I am working with in an AngularJS controller: $scope.schedule = {} $scope.selectEquipment = function(equip) { //alert(equip); $scope.schedule.equipment = equip; } I am successfully receiving the equip variable, but fo ...

What is the best way to modify a particular value buried within a JavaScript object?

Within my node project, I am working with a JavaScript object like the one shown below: that.responseData = { fields: { id: { label: 'ID', value: objectRecord.id, info: '', ex ...