Issue encountered in Three.js related to the THREE.ObjectLoader

Help Needed with Three.ObjectLoader. I am exporting a scene in JSON Format 4.3, containing geometries, materials, and lights. The scene opens error-free in the Three.js Editor.

Working on firefox with Three.js r70 master. View the generated json here: https://gist.github.com/fraguada/d86637f7987096b361ea

In my viewer project, the loading code snippet is as follows:

var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {

    console.log( item, loaded, total );

};

// instantiate a loader 

var loader = new THREE.ObjectLoader(manager);

loader.load( 
    // resource URL coming from other file
    Name, 
    // Function when resource is loaded 
    function ( result ) 
    { scene.add( result.scene ); }, 
    // Function called when download progresses 
    function ( xhr ) 
    { console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); }, 
    // Function called when download errors 
    function ( xhr ) 
    { console.log( 'An error happened' ); } 
);

Console output shows:

THREE.WebGLRenderer 70  three.min.js (line 513)
100% loaded content.js (line 117)
THREE.Object3D.add: undefined is not an instance of THREE.Object3D. three.min.js (line 164)
js/Test83.js 1 1    content.js (line 86)

Error also seen in unminified three.js at line 7674

The issue persists even if creating geometry and objects in the Three.js editor for export as a scene.

The problem seems to be related to this line of code: scene.add(result.scene); Am I wrong in assuming that THREE.ObjectLoader can process JSON from a file? When I remove scene.add(result.scene); no errors occur, but no visualized geometry. With scenes having multiple meshes, progress logs in the console (10% loaded, 20% loaded, etc).

Any insights would be greatly appreciated.

Answer №1

After conducting further investigation, I discovered that utilizing a THREE.XHRLoader to import the json data is necessary, and then employing the THREE.ObjectLoader to parse the data afterwards. You can achieve this by implementing the following code snippet:

var objLoader = new THREE.ObjectLoader();
var xhrLoader = new THREE.XHRLoader();
xhrLoader.load( 'js/data.json', function ( text ) {
    text = "{ \"scene\" : " + text + " }";
    var jsonData = JSON.parse( text );
    var sceneData = objLoader.parse( jsonData.scene );
},
// Progress callback function
function ( xhr ) { console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); },
// Error callback function
function ( xhr ) { console.log( 'An error occurred' ); });

This approach proves to be effective and was gleaned from examining the code created when exporting a scene using the ThreeJS Editor.

Answer №2

My suggestion is to simply use scene.add( result ).

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

All about the ins and outs of JavaScript and manipulating the

I attempted to use DOM manipulation to change the page background color and div background color by utilizing getElementById and onclick methods. I wrote some code to make this happen, but unfortunately, it did not work as expected. Can anyone identify wh ...

Managing large datasets effectively in NestJS using fast-csv

Currently leveraging NestJS v9, fast-csv v4, and BigQuery for my project. Controller (CSV Upload): @Post('upload') @ApiOperation({ description: 'Upload CSV File' }) @ApiConsumes('multipart/form-data') ... // Code shorten ...

Issue with vue-router not displaying template for nested routes

My route configuration looks like this: const routes = [{ path: '/', component: Home, children: [ { path: "/health", children: [ { path: 'overview', ...

Issues encountered when trying to implement helperText in mui date picker

Can someone assist with this issue? The helper text is not displaying as expected in the following code snippet: <div className={classes.container}> <LocalizationProvider dateAdapter={AdapterDateFns}> <Des ...

Javascript Code for toggling the visibility of a panel

I need help with a JavaScript code that can show or hide a panel depending on the data in a grid. If the grid has data, the panel should be displayed, but if the grid is empty, the panel should be hidden. I attempted to use the following code, but it did ...

Is it possible that the document is corrupted since it is not updating and no error is being displayed?

I am encountering an issue where one specific document does not update, although the same query successfully updates any other _id that I query: Category.findOneAndUpdate( {_id : req.params.id}, {parent : req.body.parent}, function(err,obj){ ...

Algorithm that uses 3D technology to analyze "magnetic distortion" in vertices

Imagine having a solid, smooth 3D shape composed of triangular vertices. If you have the ability to access the array of vertices containing all the x, y, z coordinates, how could you introduce multiple "magnetic" fields to distort this shape? Each magnetic ...

Problem encountered while trying to import npm module in React Native

Working on developing an android app and currently in the process of importing the spotify-web-api-node module. In my index.android.js file, I have added the following line: import SpotifyWebApi from 'spotify-web-api-node'; However, when I try ...

Locating a targeted JSON value within a elaborate JSON structure using Python

Hello, I am currently dealing with a JSON object that has multiple nested properties. When attempting to extract specific values from this object, I encountered difficulties due to the varying structure of the object. Is there a method to efficiently loc ...

What is the best way to establish a global database connection in express 4 router using express.Router()?

Is there a way to pass a global variable in Node.js from a file to a module? I have been attempting to do so with a 'db' variable that represents a MongoDB connection. I tried copying the content of my file for the connections, but it didn't ...

Tips for creating a document containing duplicate items

Hey guys, I'm struggling with this code written in Json that is supposed to generate a document with multiple instances of objects labeled _id and items. I tried using the repeat function and supplied it with a list containing these objects, but I&ap ...

Error message "auth/code-expired" is triggered when Firebase Multi Factor Authentication for a web application detects that the verification code has expired

While working on adding multi-factor authentication to my Angular web application, I encountered an error message stating: "auth/code-expired", "The SMS code has expired. Please resend the verification code to try again.", even though I ...

Display a comprehensive inventory of all bot commands within a designated category

When a user executes a command, I have various commands categorized and would like to present them accordingly. For instance, consider the following command: const Discord = require('discord.js') const { MessageEmbed } = require('discord.js& ...

Navigate within the div by scrolling in increments of 100%

I am facing an issue with a div that contains multiple children set to 100% height. My goal is to scroll exactly the height of one child (which is also 100%) on each scroll. However, I am struggling to prevent scrolling multiple steps at a time. I have tri ...

Dynamically divide canvas screens based on fabricjs dropdown selection

I am attempting to implement split screens in fabric js, such as 1, 2, 4, 8, and 16. The screen should split based on the selection from the dropdown menu. Check out my current code where I have successfully uploaded images. If I click on the images, th ...

Transform the Hue or Color of a PNG using ASP.Net, VB.Net, and jQuery

I am currently developing a web page that can combine multiple PNG images into one flat image for users to download. I am looking to incorporate a feature that allows for hue or color adjustments, similar to the color balance functionality in Photoshop. ...

How to Dynamically Retrieve Keys from JSON Array in Javascript

Could you lend me your expertise by answering a query of mine? Here is a JSON array that I currently have: [{"A":20,"B":32,"C":27,"D":30,"E":40}] My goal is to pull out the keys (A, B, C, D, E) from this JSON array rather than the values. While I have m ...

Ways to improve the cleanliness of the code

I've created a simple life simulation game and I'm looking for ways to optimize the code. Any suggestions on how to make the code cleaner? jsfiddle: https://jsfiddle.net/04vazdrb/ /* JavaScript Implementation of Conway's Game Of Li ...

Why does the <select> input field, populated with $.each, only get filled once when dynamically creating input fields?

I am facing an issue while trying to dynamically add an input field to a form. Everything seems to work fine except for one thing: When I try to populate a select dropdown with options using $.each, it only works for the first dynamically added field. If ...

Can you explain the purpose of the equals sign in ngRepeat?

Can you explain the significance of the equals sign in the ng-repeat attribute value? <li ng-repeat="person in people = (people | orderBy: firstname)"> rather than using: <li ng-repeat="person in people | orderBy: firstname"> I coul ...