Error in Three.js: The XLoader in THREE library is not recognized as a constructor

After some research, I decided to explore the possibility of importing a DirectX Model and stumbled upon this XLoader. However, despite my best efforts, I am unable to initialize it successfully. As someone who prefers linking directly to libraries for easy updates without having to constantly re-download and re-upload, my test code snippet looks like this:

<html>
<body>


<script src="https://threejs.org/build/three.js"></script>
<script type="module" src="https://threejs.org/examples/jsm/loaders/XLoader.js"></script>
<script>

var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.001, 10000 );

var manager = new THREE.LoadingManager();
var Texloader = new THREE.TextureLoader();

var loader = new THREE.XLoader(manager, Texloader);
loader.load(['FrigateHull.x'], function (object) {
        console.log(object);
},function (xhr) {
        if (xhr.lengthComputable) {
            var percentComplete = xhr.loaded / xhr.total * 100;
            console.log(Math.round(percentComplete, 2) + '% downloaded');
        }},
function (xhr) {
    console.log(xhr);
    });

</script>
</body>
</html>

The error message I keep encountering with this setup is:

TypeError: THREE.XLoader is not a constructor

I have attempted using the import method as shown in the examples, but it still results in the same error. I also tried downloading the git repository to the server and linking to it, only to receive an error about global is undefined. Is there something crucial that I am overlooking, or perhaps another alternative DirectX Model Loader that I could experiment with?

Answer №1

Here is an example of how it should be:

<script src="https://threejs.org/build/three.js"></script>
<script type="module">
      import { XLoader } from "https://threejs.org/examples/jsm/loaders/XLoader.js";
      // ....
      var loader = new XLoader(manager, Texloader);
      // ....
</script>

The error

TypeError: THREE.XLoader is not a constructor
has been fixed, but there are still other issues to address.

Take a look at the content of XLoader.js:

import {
    AnimationClip,
    AnimationMixer,
    Bone,
    BufferGeometry,
    FileLoader,
    Float32BufferAttribute,
    FrontSide,
    Loader,
    LoaderUtils,
    Matrix4,
    Mesh,
    MeshPhongMaterial,
    Quaternion,
    Skeleton,
    SkinnedMesh,
    TextureLoader,
    Uint16BufferAttribute,
    Vector2,
    Vector3
} from "../../../build/three.module.js";

// ...

We seem to be missing the code for ../../../build/three.module.js.

To resolve this issue, it is advised to clone the repository of three.js, and then run npm installnpm start to initiate the project. This approach will prevent complications related to module imports and exports in the browser environment.

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

Tips for making a bookmark for your iframe content

Within this HTML code, there is a list element with the ID "about_ma" and an iframe named "page" that is initially hidden. <div id="navigation"> <ul id="nav"> <li id="about_ma"><a href="index1.php?name=about.php">Ab ...

Error 19: Constraint violation - duplicate entry detected

While trying to set up my app, create a local database, and insert the very first user who has logged in locally, I encountered an error message at a specific point in the code. Here's where it happened: angular.module("greenApp") .service("d ...

Developing a perpetually scrolling container within a webpage

I am attempting to implement a scrollable div on my webpage that can continuously load content. I am currently using the following code snippet for this --> http://jsfiddle.net/cyrus2013/Qq85d/ $(document).ready(function(){ function loadMoreContent() ...

Exploring Elasticsearch: Uncovering search results in any scenario

I've been working on a project where my objective is to receive search engine results under all conditions. Even if I enter a keyword that is not included in the search data or if it is an empty string, I still want to get some kind of result. How can ...

Learning how to utilize localStorage in conjunction with a color picker to modify the --var of :root

After spending an entire afternoon on my JavaScript issue as a beginner, I have a specific goal in mind: allowing the user to select and change the main color of the page. To implement this, I inserted a color picker in my HTML: <input type="color ...

PHP Calculator - Displaying results in both the webpage and an Ajax popup for enhanced user experience

UPDATE - the issue was resolved by incorporating $('.popup-with-form').magnificPopup('open'); into the StateChanged function in the JavaScript. Many thanks to @Bollis for the assistance. The Initial Query: I have a basic calculator lo ...

Material UI fails to display any content

I attempted to integrate a navbar into my website, but React is not displaying anything. Even creating a new project did not resolve the issue. Additionally, Stack Overflow restricts posts that contain mostly code and I am unsure why. Here is my App.js im ...

Tips on declaring an object with a nested array of objects in TypeScript

In my code, I have defined two classes. One is called Stuff and the other is called Thing. class Stuff { constructor() { } things: Thing[] = []; name: string; } class Thing { constructor() { } active: boolean; } As I was working on my applicat ...

Can I use javascript/jquery to alter the direction of text in each line?

Looking for assistance with changing text direction on each new line. For instance: text left-to-right text right-to-left text left-to-right text right-to-left... etc. I would like the text direction to change with each word-wrap: break-word. Any help w ...

JavaScript's Set data structure does not allow for the storage of strings

As I retrieve a collection of data consisting of questions from mongoDB, I am attempting to place them in a random question set. However, the set is not accepting any input and is returning empty. module.exports.java = async (req, resp) => { // const ...

`Issue with multiple replacevariablebyhtml functions not functioning properly within PHPDocX`

When attempting to replace multiple variables in a document using the functions replaceVariableByHTML and replaceVariableByText, I encountered an issue. If I only use replaceVariableByText, everything works fine. However, when I add HTML replacement, the ...

Activate the Jquery slider after the AJAX content has been loaded

I attempted to follow the guidance from another question, but I seem to be struggling with the syntax. Upon clicking a div, it triggers a JQuery call that then executes an AJAX function to load a document. The AJAX request involves fetching some complex c ...

Navigate to all hyperlinks in browser without the use of jQuery - specifically for Firefox OS

I stumbled upon this interesting solution on a programming forum. I'm curious, how does this code work without relying on jquery? $('a[href^=http]').click(function(e){ e.preventDefault(); var activity = new MozActivity({ name: ...

All pixels have a value of 1 for gl_FragCoord.x, y, and z, while the depth value is represented by

I'm currently utilizing THREE.js in combination with a shader. My goal is to access the zbuffer information. For the vertex shader: // enabling high precision floats #ifdef GL_ES precision highp float; #endif void main() { gl_Position = project ...

Steps for generating a dropdown menu using API JSON information

I'm new to using Webapi with AngularJS. I have a URL that returns data in JSON format, and I want to extract only the employee names from the data and display them in a dropdown list using AngularJS. Any assistance would be greatly appreciated. ...

utilize optional react useState typings along with JSDoc comments to ensure TypeScript checking for JavaScript code

Utilizing typescript's jsdoc support to type the provided javascript snippet: const [optionalNumber, setOptionalNumber] = useState(null) const handleClick = () => { setOptionalNumber(42) // ^-- Argument of type '42' is not ...

What is the reason behind this code's ability to detect vowels as well as other

I'm currently working on debugging a specific portion of code for my class. I'm having trouble understanding why this JavaScript code is counting all letters instead of just vowels. var text, i, sLength, myChar; var count; var text = prompt("Ple ...

In JavaScript, we have an array of objects where each object contains both another array and an integer value

I'm feeling a bit lost on how to tackle this issue and could use some guidance. I've been struggling to find resources on how to load an array that is nested within an Object. The following function is where I've hit a roadblock: function F ...

Experiencing excessive CPU usage while utilizing a progress bar in Angular

Whenever I try to load a page with 2 progress bars, my CPU usage goes through the roof... I decided to investigate and found that once I removed them, the site's speed improved significantly. Here's a code snippet of one of the progress bars: ...

Is there a way to optimize the re-rendering and redownloading of images files in map() when the useState changes? Perhaps we can consider using useMemo

This Chat application is designed with channels similar to the Slack App. Currently, I am utilizing a map() function for filtering within an array containing all channel data. The issue arises when switching between channels, resulting in re-rendering and ...