Issue in THREE.js: The error "THREE.OBJLoader is not a valid constructor" has occurred

As I delve into learning three.js, I find it very intriguing, but have encountered a problem that stumps me.

My current challenge involves loading an OBJ file that I previously created in Blender. In attempting to achieve this, I've been using THREE.OBJLoader. After copying the code from , I'm encountering the error message "THREE.OBJLoader is not a constructor" at line 32.

Despite this issue, everything else seems to be functioning properly: setting up a scene, applying materials, adding shapes like cubes, etc.

To simplify matters, here's the snippet of the code:

var renderer, scene, camera, banana;
var ww = window.innerWidth,
wh = window.innerHeight;

function init(){

renderer = new THREE.WebGLRenderer({canvas : document.getElementById('scene')});
renderer.setSize(ww,wh);

scene = new THREE.Scene();

camera = new THREE.PerspectiveCamera(50,ww/wh, 0.1, 10000 );
camera.position.set(0,0,500);
scene.add(camera);

//Adding a light source in the scene
directionalLight = new THREE.DirectionalLight( 0xffffff, 0.8 );
directionalLight.position.set( 0, 0, 350 );
directionalLight.lookAt(new THREE.Vector3(0,0,0));
scene.add( directionalLight );

//Loading the obj file
loadOBJ();
}

var loadOBJ = function(){

//Manager used in ThreeJs to track loader progress
var manager = new THREE.LoadingManager();
//Obj Loader from Three.js
var loader = new THREE.OBJLoader( manager );
//Initiate loading of the obj file with addBananaInScene as callback upon completion 
loader.load( 'http://mamboleoo.be/learnThree/demos/banana.obj', addBananaInScene);

};

var addBananaInScene = function(object){
banana = object;
//Adjusting position of the banana in the scene
banana.rotation.x = Math.PI/2;
banana.position.y = -200;
banana.position.z = 50;
//Iterating through all children of the loaded object and checking for Mesh instances
object.traverse( function ( child ) {
    //Verifying if child is an instance of Mesh constructor
    if(child instanceof THREE.Mesh){
        child.material.color = new THREE.Color(0X00FF00);
        //For obj files missing vertex normals, ThreeJs will compute them
        child.geometry.computeVertexNormals();
    }
});
//Adding the 3D object to the scene
scene.add(banana);
render();
};


var render = function () {
requestAnimationFrame(render);

//Rotating the banana
banana.rotation.z += .01;

renderer.render(scene, camera);
};

init();

I am hopeful that someone can provide insight into what might be causing this issue.

Warm regards, Christian

Answer №1

It's important to double-check the 'Settings' when copying Codepen examples to ensure you have all the necessary project files included.

In this specific scenario, the used OBJLoader.js is where the OBJLoader constructor originates from. If you're encountering an error, it's likely due to not having a reference to this file. Make sure to include the reference and the issue should be resolved.

Answer №2

Excellent response. However, it is worth noting that the .js file in question does not contain any mention of version numbers. This can be problematic as functionality changes frequently and some functions may become deprecated (for example, projector was functional until around version 71). The lack of a centralized repository for .js files poses challenges for ensuring backward compatibility in three.js projects.

Answer №4

To load objects, try using ObjectLoader.js

var objLoader = new THREE.ObjectLoader();

Please be aware that OBJLoader is currently not functioning properly.

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

Late data is being received in the Redux state of my React application

I am currently retrieving data from two APIs to fetch song lyrics and artist biographies. I need to store this data in my reducer, but I am facing an issue. When I make the second API call to get the biography, the data gets saved in my store successfully, ...

The error message states that the provided callback is not a valid function -

I seem to be encountering an issue with the code snippet below, which is part of a wrapper for the Pipl api: The main function here performs a get request and then retrieves information from the API Any assistance in resolving this error would be greatly ...

Encountering an error while using $state.go function in Angular JS testing

Below is the code snippet for a Controller in Angular JS: describe('Controller: homeCtrl', function () { beforeEach(module('incident')); var homeCtrl, $state; beforeEach(inject(function ($controller, _$state_) { $state = _ ...

What is the best way to eliminate the left padding on a TimelineItem component?

When using the Timeline component to display information, there are three columns: TimelinItem, TimelineSeparator, and TimelineContent. I tried setting the padding of TimelineItem to 0 but it didn't work. The <Trace/> component is a sub-compone ...

Utilizing React with Material UI: Customizing MuiTheme within a component using withStyles

Currently utilizing Material UI in conjunction with React. I am attempting to customize the style of the TextField component, which has been configured using a global theme. The global theme has been set up for all TextField components within the applicati ...

Errors that occur when parsing templates in Angular 2

I'm encountering a template parse error with my Angular 2 component using the following template: <div *ngIf="chapter == 1"> <p><h4><br><b>Exercise</b>: Get the Groceries starting point<br></h4 ...

jQuery.ajax encountering failure to read success function

Hey everyone, I need some assistance with web programming. I'm facing an issue with ajax while trying to read a JSON response. The catch is, I have to use Internet Explorer for this task, so switching browsers is not an option. I'm stuck because ...

best method for integrating google maps on a website

I want to optimize the loading speed of Google Maps v3 on my website. Here is my current setup: In a .js file at the end: google.maps.event.addDomListener(window, 'load', initialize); And at the end of the HTML page code: <script type="te ...

enhancement in bringing in ... from

Hey there, I've been working with Vue.js and came across an interesting challenge. In order to use components in Vue.js, you usually need to import them individually. I thought of a solution where we could create a module containing paths to all the c ...

Dynamically attach rows to a table in Angular by triggering a TypeScript method with a button click

I need help creating a button that will add rows to a table dynamically when pressed. However, I am encountering an error when trying to call the function in TypeScript (save_row()). How can I successfully call the function in TypeScript and dynamically a ...

Accessing files for MongoDB cluster

My goal is to retrieve all the documents from a MongoDB cluster. Even though I have followed code examples from various online sources, I am encountering a minor issue. const MongoClient = require('mongodb'); const uri = "mongodb+srv://<user& ...

Managing the Response from Google Geocode API in a Vue JS Component

I am facing an interesting challenge. I have a scenario where users can choose between two options - using their current location or entering a zip code. When a user inputs a zip code, I need to make a call to the Google geocode API to retrieve the central ...

Tips for implementing the handleClick method within a class component, as opposed to using the export default function syntax in

Hey there! I'm a beginner in React and I'm trying to incorporate Material UI into my project. I came across the documentation for the menu section like this, but I'm facing some challenges with writing my code. Specifically, I'm struggl ...

Unable to get the jQuery click event handler to function properly

I am currently working on an asp.net web application in VS 2013. The application utilizes nested master pages, with the main master page containing the following code snippet: <!DOCTYPE html> <script src="http://code.jquery.com/jquery-lat ...

Find the names of keys which have a value of true in reactjs

Greetings, I have a JSON file that contains information about different components. I am trying to extract only the Dashboard and Datatable sections where the "visible" attribute is set to true. { "MTs": { "Dashboard": { "id": 1, "visible ...

Tips for transferring the clicked value to the next page

Recently, I started working with Ionic and developed an application using it. Now, I am facing a challenge where I need to pass the value from the first page to the second page. To illustrate this more clearly, I have attached two photos. Here is the imag ...

What is the process for importing external geometry in pythreejs?

I've been attempting to bring in an external 3D model into my jupyter python notebook using pythreejs without success. The file format doesn't have to be limited to just stl, it could also be json or any other compatible format. My main goal is t ...

What is the process for running an HTML file on a server that utilizes Cordova plugins?

I'm a beginner in Cordova and recently I created an HTML file which I added to the xampp/htdocs directory and it worked fine. My goal is to create a generic app, but now I want to incorporate the Camera Plug-in into my app. Unfortunately, I am unable ...

Can someone provide guidance on how to validate a HEX color in Cypress?

As I dive into testing with Cypress, my lack of experience has become apparent. I am trying to test the background-color CSS property on a specific element, but running into an issue - everything is in RGB format behind the scenes, while I need to work wit ...

Encountered an issue while executing the npm run dev command in a React project

Hello, I am a beginner with React.js and I am currently trying to set up a simple React application locally using webpack. However, when I run the npm run dev command, I encounter the following error: An error occurs in the command prompt after running np ...