gpt4 book ai didi

javascript - THREE.js 对象不会旋转

转载 作者:行者123 更新时间:2023-12-03 04:37:21 26 4
gpt4 key购买 nike

我有一个 Blender 对象,可以使用 THREE.js 在我的网页上显示,但是当调用我的循环函数时,该对象不会旋转。

我尝试在使用 js 时保持 OOP 方法。

这是我的代码片段。

var scene, camera, renderer, box;

function createScene() {
scene = new THREE.Scene();

renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x3399ff);

camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 10;

container = document.getElementById('world');
container.appendChild(renderer.domElement);
}

function createLight() {
light = new THREE.PointLight(0xffffff, 1.2);
light.position.set(0,0,6);

scene.add(light);
}

function createBox() {
box = new Box();
}

Box = function() {
this.mesh = new THREE.Object3D();

var loader = new THREE.JSONLoader();
loader.load('json/model.json', function(geometry, materials) {
this.mesh = new THREE.Mesh(geometry, new THREE.MultiMaterial(materials));
this.mesh.scale.x = this.mesh.scale.y = this.mesh.scale.z = 0.75;
this.mesh.translation = geometry.center();
scene.add(mesh);
});
}

Box.prototype.rotateBox = function() {
if (!this.mesh) {
return;
}

this.mesh.rotation.x += .001;
this.mesh.rotation.y += .01;
}

function loop() {
requestAnimationFrame(loop);
box.rotateBox();
renderer.render(scene, camera);
}

function init() {
createScene();
createLight();
createBox();
loop();
}

window.addEventListener('load', init, false);

最佳答案

我认为这是一个范围问题。您提供的代码会引发错误。你可以尝试这样的事情:

Box = function() {
this.mesh = false;
var loader = new THREE.JSONLoader();
var scope = this;
loader.load('json/model.json', function(geometry, materials) {
scope.mesh = new THREE.Mesh(geometry, new THREE.MultiMaterial(materials));
scope.mesh.scale.x = scope.mesh.scale.y = scope.mesh.scale.z = 0.75;
scope.mesh.translation = geometry.center();
scene.add(scope.mesh);
});
}

Box.prototype.rotateBox = function() {
if (!this.mesh) {
return;
}

this.mesh.rotation.x += .001;
this.mesh.rotation.y += .01;
}

“光”对象的范围也未处理,需要修复。

关于javascript - THREE.js 对象不会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43237710/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com