Unable to manage the rotation in Three.js GLTFLoader

Issue

Why am I unable to rotate the angle of an object? (scene.rotation.x += 0.03;)

The camera can, but the scene object cannot.

I've searched for a solution for a long time without success.

Here is my code:

import * as THREE from 'https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="54203c26313114647a6566627a65">[email protected]</a>/build/three.module.js'
import { OrbitControls } from 'https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6a2bea4b3b396e6f8e7e4e0f8e7">[email protected]</a>/examples/jsm/controls/OrbitControls.js'
import { GLTFLoader } from 'https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="582c302a3d3d186876696a6e7669">[email protected]</a>/examples/jsm/loaders/GLTFLoader.js'

const canvas = document.querySelector('.fan1') // it is my class
const scene = new THREE.Scene()
var ourObj;

const loader = new GLTFLoader()
//load glb file
loader.load('assets/fan.glb', function (glb) {
    const root = glb.scene;
    root.scale.set(10,10,10)
    scene.add(root);
}, function (xhr) {  //xhr
    console.log("xhr");
}, function (error) { //error
    console.log("error");
})

const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(2, 2, 5);
scene.add(light);

const size = {
    width: window.innerWidth,
    height: window.innerHeight
}

const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100); 
camera.position.set(0, 1, 3);
scene.add(camera);


const renderer = new THREE.WebGLRenderer({
    antialias: true,
    alpha: true,
    canvas: canvas
})

renderer.setClearColor("#DDDDDD");
renderer.setSize(size.width, size.height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
var render = function () {
    requestAnimationFrame(render);
    scene.rotation.x += 0.03;
    renderer.render(scene, camera);
};

render()

Error: Control _x is not valid

view image description here

Answer №1

The issue arises when you include the camera in the scene using scene.add(camera); This results in the camera moving in sync with the scene, causing minimal observable changes in the final output.

To prevent this, it is unnecessary to attach the camera to the scene. Simply eliminate the following line:

scene.add(camera);

This way, the camera remains "unattached" and unaffected by any transformations applied to the scene.

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

Can you explain the function of MathUtils.euclideanModulo and how it sets itself apart from the traditional modulo operation?

I'm a little puzzled by the euclideanModulo function in threejs's mathutils. I understand the concept of the modulo operator, but the euclideanModulo function seems to work differently. ( ( n % m ) + m ) % m I tried researching the meaning of "E ...

The GLTF 3D model failed to animate as expected

My current objective is to create an animation for a 3D model that I have imported as a .gltf file. The model contains only one animation. After carefully following the steps outlined in the documentation: And exploring various examples on the internet, ...

How to maintain the selected value in a Vue.js dropdown even after the page is refreshed

When a user selects a value from a dropdown, it is pushed to the URL. If the page is refreshed, the dropdown should default to the selected value in the URL. This functionality is being implemented in Laravel. I have attempted the following approach, but w ...

The Material UI list element fails to appear on the screen

I am encountering an issue where I am trying to display a Material UI list within a drop-down menu (using the Material UI drawer component) for mobile view, but the actual list is not appearing as expected. The code snippet for the list is as follows: con ...

Interact with an external JavaScript function using a button within a web application

I have a JavaScript file called dmreboot_service.js in my /js folder. By running this file with the command node /js/dmreboot_service.js, it successfully triggers a direct method in Azure. My goal is to be able to run this function or file when a button o ...

Issue with Ajax call not displaying the jQuery dialog box

Could you please take a look at this jsFiddle link... The issue lies with a jQuery UI dialog box that uses an ajax request to fetch content. Unfortunately, all I see is a blank dialog and can't seem to pinpoint the problem. A snippet of the HTML cod ...

Circle a component around another on the vertical axis (z-index)

A plugin caught my eye some time back, but I'm having trouble locating it. This nifty tool operates by positioning an element behind another one, then smoothly sliding it to the right. After that, it changes the z-index so the element appears larger i ...

Leverage Jquery within the div element to manipulate the data retrieved from a post ajax request

On my one.jsp page, I have the following post code: $.post("<%=request.getContextPath()%>/two.jsp", { vaedre: fullDate} ,function(result){ $("#theresult").append(result); }); This code generates the followi ...

Enhance your React application by making two API requests in

Below is the React Component that I am working on: export default function Header () { const { isSessionActive, isMenuOpen, isProfileMenuOpen, setIsMenuOpen, closeMenu, URL } = useContext(AppContext) const [profileData, setProfileData] = useState({}) ...

What is the best method for eliminating script tags from a Magento web store's source code?

Below is the source code for the Magento site's homepage. I am looking to eliminate all JavaScript links from the code: ...

What could be causing my jQuery to malfunction with version 1.9.1?

I am facing an issue with my website which primarily operates on jQuery version 1.9.1. However, I need to integrate a plugin that specifically requires version 1.4.1. Is there a simple modification that can be made within the plugin code to ensure compatib ...

The POST function is executed twice, with the first attempt resulting in a failed API call, but the second attempt is

I am experiencing issues with my subscribe dialog, as it seems to be running the API call twice. The first time it fails, but on the second attempt, it succeeds and inserts the email into the database. This double run is causing errors in my AJAX function, ...

Encountering the error message "Uncaught TypeError: $.ajax is undefined"

Recently, I encountered an issue with my form that utilizes ajax to send user information to a php file. The form is embedded within a bootstrap modal and was functioning perfectly until I attempted to add an extra field for enhanced functionality. However ...

Ways to standardize the input email address?

While using express-validator, I came across an issue where I used normalize email for validation of email during sign up and stored the normalized email on the server. Validation code: router.post( "/signup", [ check("name").n ...

Stop the occurrence of numerous ajax requests being triggered by clicking

Having an issue with handling multiple ajax requests. In my scenario, there is a form with a button that triggers a service upon clicking it. This service is responsible for loading a list of items into a table, but currently it only loads one item at a ti ...

Tips for retrieving the concealed input value from the div directly preceding

Is it possible for me to retrieve the hidden input value by clicking on the text "upload profile photo"? I don't have much experience in this area. <div> <input type="hidden" value='<?php echo $list['profil ...

Unused Vue Chart JS options are neglected

I'm encountering an issue with Vue Chart JS v3.5.1 in my Nuxt JS/Vue project where when trying to pass options as a prop, the chart defaults back to its default settings and does not reflect the changes I made. My project includes multiple files: pl ...

Is there a way to retrieve the disabled drop-down field value after submitting the form?

I'm struggling with a dropdown field that becomes disabled once an item is selected. After submitting the form, the dropdown field loses its value and I'm left with an empty field. Any suggestions on how to keep a value in the dropdown after subm ...

Tips for incorporating your personal touch while utilizing Snipcart

I have created an ecommerce platform with GatsbyJS and Snipcart, but I am struggling to override the default theme provided by Snipcart. When I try to change the main default CSS through gatsby-config.js, it does not seem to work. Does anyone have a soluti ...

Filtering data based on dates in AngularJS

I need to implement a data filter based on event date. The options for filtering include the current day, current month, and current year. Below is the current implementation: function filterDate($scope) { var d = new Date(); var curr_date = d.get ...