Adjust the Pivot Point of a GLTF Model in ThreeJS Manually

Hey there, I have a GLTF model that I successfully loaded into my ThreeJS scene by using the code snippet below:

gltfLoader.load('assets/models/coin/scene.gltf', (gltf) => {
        const root = gltf.scene;

        gltf.scene.traverse(function(node){
          if ( node.isMesh ) { 
            node.castShadow = true;
            node.receiveShadow = true;
            if(node.material.map) {
              node.material.map.anisotropy = 16;
            }
          }
        })

        // root.position.set(25, 120, -500);
        root.position.set(0, 0, 0);
        root.scale.set(20, 20, 20);

        coinBox = new THREE.BoxHelper( root, 0xffff00 );
        scene.add(coinBox);

        coin = root;
        scene.add(root);

        //Adding a circle to visualize the pivot point
        const circleGeometry = new THREE.CircleGeometry(3, 64);
        const circleMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
        const circleMesh = new THREE.Mesh(circleGeometry, circleMaterial);
        circleMesh.position.copy(root.position);
        circleMesh.position.y += 1;
        scene.add(circleMesh);
      });

I decided to add a CircleGeometry to get a clear picture of where the Pivot point lies in this object. Interestingly, it appears that the pivot point is not positioned at the center of the model.

Hence, when I try to execute this line of code:

coin.rotation.z += 0.01;, it unintentionally rotates around the incorrect pivot point (shown as the red dot in the GIF).

https://i.sstatic.net/9GxAF.gif

I am wondering if there's any method I can use to manually adjust the pivot point or reposition the center based on the meshes that have been loaded?

Answer №1

While the pivot point cannot be directly changed, there is a workaround that can be done to achieve a similar effect:

  • Start by organizing the 3D object hierarchy in the scene.

  • Define the bounding box for the specific object:

    Create a variable called pivot_box and set it as new THREE.Box3().setFromObject(circleMesh);

  • Calculate the center of the mesh:

    Create a variable called piv_center as new THREE.Vector3();

    Get the center of the bounding box using pivot_box.getCenter(piv_center)

  • Move the mesh to the origin of the axes:

    Adjust the position of circleMesh by subtracting piv_center from it: circleMesh.position.sub(piv_center)

  • Create a new THREE.Group object to act as the parent of the circleMesh (name it something like newPivotBox).

  • Attach circleMesh to the newly defined object.

  • Attach newPivotBox to the scene.

  • Now, interact with the newPivotBox object for rotation, translation, and scaling purposes.

This method helped me address a similar issue before, so I hope it proves helpful to you as well.

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

Utilizing eval properly in JavaScript

One method I am using is to load a different audio file by clicking on different texts within a web page. The jQuery function I have implemented for this purpose is as follows: var audio = document.createElement('audio'); $(".text_sample ...

What is the best way to apply the addClass method to a list element

I've been searching for a solution to this issue for some time now, and while I believed my code was correct, it doesn't appear to be functioning as expected. HTML <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js ...

Tips for implementing an onClick event within a NavBar component in a React application

Embarking on my first React project has been an exciting journey for me. I am relatively new to JavaScript, HTML, CSS, and the world of web app programming. My goal is to showcase some information upon clicking a button. In my main default component, App ...

What is the best way to use ajax to send a specific input value to a database from a pool of multiple input values

Welcome everyone! I'm diving into the world of creating a simple inventory ordering site, but am facing a roadblock with a particular issue: Imagine you have a certain number (n) of items in your inventory. Based on this number, I want to run a &apos ...

What is the best way to incorporate a fadeIn animation to a text in jQuery?

Looking for help with appending the fadeIn() jQuery function to a string that increments an integer within a paragraph. I attempted concatenation without success. Any recommendations on how to solve this issue? $p.text(parseInt($p.text(),10) + 1); ...

Navigating through property objects in Vue: accessing individual elements

I am a newcomer to Vue and after reviewing other questions on this platform, I am struggling to figure out how to pass an object to a child component and reference individual elements within that object. My goal is to have access to all elements of the obj ...

ng-hide is not functioning to display the form

I'm trying to display a form using ng-if when the "add" button is clicked, but it's not working. I've looked at several examples and tried them all, but none have worked for me. Here is the code I am using: angular.module('myFormApp&ap ...

How to dynamically apply the orderBy filter in AngularJS

Attempting to input ordering criteria from a text box and dynamically order the content. The current code is functioning properly, but when attempting to change: orderBy:'age' to orderBy:'{{criteria}}' the functionality breaks. Her ...

Transferring information from one page to the next

How can I efficiently transfer a large amount of user-filled data, including images, between pages in Next.js? I've searched through the Next.js documentation, but haven't found a solution yet. ...

Ways to prevent modal from flickering during event changes

I'm struggling with a current issue and need help identifying the cause and finding a solution. The problem arises from having a nested array of Questions, where I display a Modal onClick to show Sub questions. However, when clicking on the Sub Quest ...

Why is Selectpicker failing to display JSON data with vue js?

After running $('.selectpicker').selectpicker('refresh'); in the console, I noticed that it is loading. Where exactly should I insert this code? This is my HTML code: <form action="" class="form-inline" onsubmit="return false;" me ...

Ways to create a clickable anchor tag without using any text

I am currently designing my own website and incorporating links to various social media platforms using icons. However, I am facing an issue where the links are not clickable. For a detailed look at my problem, you can refer to this JSFiddle: http://jsfid ...

How to maintain row highlight in jQuery datatable even after it is reloaded

I have a datatable set to reload every 10 seconds. When a user clicks on a row, it highlights, but the highlight is lost when the table reloads. Here is a condensed version of my datatable code: $(document).ready(function() { // set up datatable $(& ...

When using Inertia.js with Laravel, a blank page is displayed on mobile devices

Recently, I've been working on a project using Laravel with React and Inertia.js. While everything runs smoothly on my computer and even when served on my network (192.168.x.x), I encountered an issue when trying to access the app on my mobile phone. ...

Utilizing the Vue feature of dynamic route naming within a component's function

Currently, I am working on creating a dynamic view rendering using Vue and Laravel. However, I am facing difficulty in understanding how to pass the dynamic parameter to the component function. Router.map({ '/cms-admin/:page': { comp ...

Guide to navigating to a particular component in React JS

I've designed a web application using React that doesn't contain any HTML, and I've broken down each page into smaller modules. For instance, on the main page, the first module (located at the top) contains a button that, when clicked, shoul ...

Position the div in the center of a container that is 100% width and has floating elements with dynamic widths

I am looking to align 3 divs inside a container div, with the arrangement of [LEFT] [CENTER] [RIGHT]. The container div should be 100% wide without a fixed width, and I want the center div to stay centered even when resizing the container. The left and ...

Utilizing Vue to Customize Chart Settings with Data

I'm currently working on hitting a rest api to retrieve a dataset for a specific piece of equipment. However, I'm struggling to extract the first column of data as an array. I've attempted using computed and methods in vue, but I constantly ...

Difficulties with validating phone numbers

I'm having an issue with my JavaScript code that is supposed to validate a phone number field, but it doesn't seem to be working. Even if I enter incorrect values, the form still submits. Here's the snippet of my code: <script> functi ...

extracting an attribute from a class's search functionality

Let's consider the following HTML structure: <div id="container"> <div> <div class="main" data-id="thisID"> </div> </div> </div> If I want to retrieve the value inside the data-id attribute ...