可以控制旋转(三级GLTFLOADER)

发布于 2025-01-24 12:44:08 字数 2049 浏览 1 评论 0原文

问题

为什么我不能旋转对象的角度?(scene.Rotation.x += 0.03;)

相机可以,但是场景对象不能。

我一直在寻找很长一段时间,但仍然找不到答案。

这是mycode:

import * as THREE from 'https://unpkg.com/[email protected]/build/three.module.js'
import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js'
import { GLTFLoader } from 'https://unpkg.com/[email protected]/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()

控制_x是无效的

在此处输入图像描述

Problem

Why can't I rotate the angle of an object? (scene.rotation.x += 0.03;)

Camera can ,but scene object can't.

I've been looking for a long time and still can't find the answer.

Here is the myCode:

import * as THREE from 'https://unpkg.com/[email protected]/build/three.module.js'
import { OrbitControls } from 'https://unpkg.com/[email protected]/examples/jsm/controls/OrbitControls.js'
import { GLTFLoader } from 'https://unpkg.com/[email protected]/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()

Control _x is invalid

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2025-01-31 12:44:08

问题在于,您将相机添加到场景中使用scene.Add(camera);这意味着当您旋转场景时,相机 ,所以您不会在最后的渲染中注意到很多动作。

您无需将相机添加到场景中。只需删除线条,

scene.add(camera);

以便您的相机“免费”,并且独立于任何场景转换。

The problem is that you're adding the camera to the scene with scene.add(camera); This means that when you rotate the scene the camera rotates with it, so you won't notice much movement in the final render.

You don't need to add your camera to your scene. Just remove the line

scene.add(camera);

so that your camera is "free" and independent of any scene transformations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文