Issue with Three.js: GLTF model not positioned correctly at origin point

After attempting to load a glTF model with a 0,0,0 position, I noticed that it appears far from the origin.

Upon trying to rotate the glTF model, I observed that it spins around (indicated by blue dots) the origin instead of spinning from its center.

This seems to be related to a pivot issue. How can this be resolved?

var tree;
function loadGLTFCharacter(path, position){
    // Load a glTF resource
    loader.load(
        // resource URL
        path,
        // called when the resource is loaded
        function ( gltf ) {
            gltf.scene.traverse( function( node ) {

                if ( node.isMesh ) {
                    node.castShadow = true;
                    node.receiveShadow = true;
                }

            } );
            gltf.scene.position.set(position.x, position.y, position.z);
            tree = gltf.scene;
            scene.add( tree );
            
            gltf.animations; // Array<THREE.AnimationClip>
            gltf.scene; // THREE.Group
            gltf.scenes; // Array<THREE.Group>
            gltf.cameras; // Array<THREE.Camera>
            gltf.asset; // Object

        },
        // called while loading is progressing
        function ( xhr ) {

            console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );

        },
        // called when loading has errors
        function ( error ) {

            console.log( 'An error happened' );

        }
    );
}

loadGLTFCharacter('models/characters/tree_detailed.gltf', {x:0,y:0.2,z:0});

https://i.sstatic.net/7Y2hF.png

Answer №1

Upon examining the contents of the .gltf file using a text editor, I discovered an interesting segment:

"nodes": [
{
  "mesh": 0,
  "scale": [
    0.25,
    0.25,
    0.25
  ],
  "translation": [
    6.49107456,
    1.60296546E-31,
    -1.89133477
  ],
  "name": "tree_detailed"
}
],

By altering the values in the translation section to 0, 0, 0, the issue was resolved.

I find it puzzling why someone would create an object with such arbitrary translations

Answer №2

My solution to this issue involved using Blender. Simply adjusting the values in the gltf file did not resolve the problem for me. However, after merging the meshes in Blender's Edit Mode and removing two empty parent objects from the model, everything fell into place. It's worth noting that toggling the option in Blender under Geometry > Origin > Geometry was also essential in solving the issue. Make sure to have the Glft Im/exporter plugin enabled in your preferences.

Answer №3

It's not uncommon for random translations to occur when transferring designs from one software to another in the world of architecture and design. I encountered a similar situation when moving an architectural model from Fusion 360 to Cinema 4d and then to Blender, all with the end goal of exporting it as a gltf file.

During this process, I had to deal with scale discrepancies caused by unit mismatch, leading me to adjust the size accordingly. However, this action resulted in parts of the model appearing in incorrect positions, requiring further translation to correct them.

While these adjustments may seem fine within the original CAD application, they can cause issues when saved in the gltf file and interpreted by three.js, potentially leading to the errors you've experienced.

To prevent such issues, consider the following steps:

  1. Avoid using multiple applications and file formats if possible in your asset production pipeline.
  2. If multiple apps are necessary, pay close attention to export and import settings, especially scale factors and units. Verify the size of imported models against a standard cube in Blender, for example.
  3. Maintain a consistent unit workflow throughout the design process. Remember that in three.js, one unit equals one meter (in the SI system). Use the same unit and scale factor from the outset.
  4. When incorporating third-party assets, check their units, scale factor, positioning, and even the up-axis orientation.

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

Working with intricately structured objects using TypeScript

Trying to utilize VS Code for assistance when typing an object with predefined types. An example of a dish object could be: { "id": "dish01", "title": "SALMON CRUNCH", "price": 120, ...

Utilizing helper functions in Node based on their specific types

In my helper module, I have different files like content, user, etc. These files define various helpers that can be used in the router. Below is the code for the router: router.js var helper = require("./helper"); function index(response) { response ...

Following a Node/Npm Update, Sails.js encounters difficulty locating the 'ini' module

While developing an application in Sails.js, I encountered an authentication issue while trying to create user accounts. Despite my efforts to debug the problem, updating Node and NPM only resulted in a different error. module.js:338 throw err; ...

What is the most effective method to query Prisma using a slug without utilizing a React hook?

Retrieve post by ID (slug) from Prisma using getStaticProps() before page generation The challenge arises when attempting to utilize a React hook within getStaticProps. Initially, the plan was to obtain slug names with useRouter and then query for a post ...

div added on the fly not showing up

I'm attempting to dynamically add a div to a webpage using Chrome. Despite following several instructional guides, the code does not seem to be working as expected. I have added style attributes to make it more visible, but the element is not showing ...

Leveraging the power of ES6 capabilities within the Express.js framework of Node

Recently, I've been experimenting with utilizing ES6 features in Express. Interestingly, I discovered that Nodejs now has built-in support for es6, eliminating the need for babel to transpile my code. Here's a snippet from my app.js file: &apos ...

typescript: define the type of an object that behaves like a map

My current approach involves utilizing an object to store a map, where keys are strings and values are of a fixed type T. Upon looking up a key in the object, the type inference automatically assigns it the type T. However, there is a possibility that it ...

Adjusting an image size using JQuery after resizing a canvas will only resize the image itself, not

How can I resize an image within a canvas using JQuery in a way that only the image is resized, not the entire canvas? function resizeImage(width, height){ var image = document.getElementById('resizeImage'), canvas = document.createEleme ...

Resolved plugin issue through CSS adjustments

Take a look at this template 1) After referring to the above template, I developed a fixed plugin using JavaScript. 2) Clicking the icon will trigger the opening of a card. 3) Within the card, I designed a form using mdb bootstrap. Everything seems to ...

An effective way to prevent right-clicking on iframes across all websites

I am facing an issue with disabling right click for the iframe. I've successfully disabled it for the default URL of the IFrame, but when displaying any other webpage, the right click remains usable. Below are the sample codes I have used: document.o ...

Tips for expanding AntD Table to show nested dataSource values

I need help dynamically rendering data into an antD expandable table. The data I have is a nested object with different properties - const values = [ [name = 'Josh', city = 'Sydney', pincode='10000'], [name = 'Mat ...

Exploring the .map() Method in ReactJS

Would it be feasible to integrate another Postgres database table into the current mapping displayed in this code? It would be ideal if it could be done using some sort of array function. {items.map(item => ( <tr key={item.id}& ...

Struggling to eliminate placeholders using regular expressions in JavaScript

In my dynamically generated table, I have some placeholders that need to be removed. These placeholders are in the format of {firm[i][j]}, where i and j are numbers. I attempted to use a regular expression in JavaScript to remove them, but it didn't ...

Tips for retrieving data from Angular dropdown menus

How to Retrieve Selected Value in AngularJS HTML <select data-ng-model='datasource.value' ng-options='sourcetype.dataSourceType as sourcetype.dataSourceDesc for sourcetype in sourcetypes' data-ng-change="getAccountCredentials(sourc ...

Designing tab navigation in React Native

Is there a specific way to customize only the ORANGE tab's style? I am curious to learn how to address this particular scenario. I have attempted various methods to modify the style of the ORANGE tab. My application utilizes navigation 5. <Tab.Navi ...

What steps should I take to ensure that this modal remains persistent?

I am searching for a method to keep this modal persistent even after it appears. Currently, the user can easily close it by clicking outside of the div. <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title ...

Transferring data from a React file to a plain HTML page

What is the best way to transfer information from a React app to a static HTML file? For instance, if I collect data through a form in a React app.js file, how can I then send that data to a basic HTML file? ...

Enhancing data management with Vuex and Firebase database integration

Within my app, I am utilizing Firebase alongside Vuex. One particular action in Vuex looks like this: async deleteTodo({ commit }, id) { await fbs.database().ref(`/todolist/${store.state.auth.userId}/${id}`) .remove() .then ...

My PHP errors and success messages are not being displayed properly after an AJAX success

After making an AJAX call to submit a form, I would like to display either the PHP success message or error message upon completion. This is my current AJAX success function: success: function (data) { resultSuccess = $(data).find("#success") ...

The issue of an undefined Node.js variable post "await"

While I know similar questions have been asked before, I assure you that I've gone through them; however, I'm still facing a challenge. I have a simple code snippet to retrieve a token for a 3rd-party API service: let tok = ''; const g ...