javascript - 更新加载模型的旋转
问题描述
上周一直在使用three.js 库,我遇到了将转换应用于加载的模型(gltf)的问题。每当我尝试更新旋转时,我都会收到“未捕获的类型错误:无法读取未定义的属性‘位置’。” 我很困惑;好像我只能访问 onLoad 中的 .position/.rotation 属性?
let container;
let camera;
let controls;
let renderer;
let scene;
let mesh;
const mixers = [];
const clock = new THREE.Clock();
function init() {
container = document.querySelector('#scene-container');
scene = new THREE.Scene();
scene.background = new THREE.Color(0x8FBCD4);
createCamera();
createLights();
loadModels();
createRenderer();
createControls();
renderer.setAnimationLoop(() => {
update();
render();
});
}
function createCamera() {
const fov = 35;
const aspect = container.clientWidth / container.clientHeight;
const near = 0.1; //near clipping plane
const far = 100; //far clipping plane
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.set(-1.5, 1.5, 6.5);
}
function createLights() {
const ambientLight = new THREE.HemisphereLight(
0xddeeff, //bright sky color
0x202020, //dim ground color
3 //intensity
);
//this is a specular light
const mainLight = new THREE.DirectionalLight(0xffffff, 10);
mainLight.position.set(10, 10, 10);
scene.add(ambientLight, mainLight);
}
function createRenderer() {
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.gammaFactor = 2.2;
renderer.gammaFactor = true;
renderer.physicallyCorrectLights = true; //allows us to use lux, candela and lumens to set lighting
container.appendChild(renderer.domElement);
}
function loadModels() {
const loader = new THREE.GLTFLoader();
const onLoad = ( gltf, position ) => {
const model = gltf.scene.children[ 0 ]; //get reference to the model
model.position.copy( position ); //passes the position parameter into the models position value
const animation = gltf.animations[ 0 ];
const mixer = new THREE.AnimationMixer( model ); //instances a animation controller
mixers.push( mixer );
const action = mixer.clipAction( animation );
action.play();
mesh = model;
scene.add( model );
};
const onProgress = () => {};
const onError = (errorMessage) => { console.log(errorMessage); };
//loads a position the model and the position
const suzannePosition = new THREE.Vector3(0, 0, 2.5);
loader.load('models/test/suzanne.glb', gltf => onLoad(gltf, suzannePosition), onProgress, onError);
}
function update() {
const delta = clock.getDelta();
for (const mixer of mixers) {
mixer.update(delta);
}
/* ---THIS DOESN'T WORK---
mesh.rotation.x += .01;
console.log(mesh.rotation);
*/
}
function createControls() {
controls = new THREE.OrbitControls(camera, container);
}
function render() {
renderer.render(scene, camera);
}
function onWindowResize() {
camera.aspect = container.clientWidth / container.clientHeight;
//update camera's frustum
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
window.addEventListener('resize', onWindowResize);
init();
解决方案
mesh = model;
此分配发生在 的onLoad()
回调中GLTFLoader.load()
。重要的是要了解此回调是在加载模型时异步执行的。
同时,mesh
是undefined
。因此,将您的代码更改为以下内容以消除运行时错误:
if ( mesh ) mesh.rotation.x += .01;
推荐阅读
- javascript - 如何比较具有相同对象属性的两个值?
- postgresql - 将表名作为参数传递给 Postgresql
- google-chrome-devtools - 如何从另一台机器访问 chromebook 上的 chrome 开发工具
- javascript - 状态改变回调函数
- android - W/DownloadManager:中止下载请求 17:无法创建目标文件 /storage/emulated/0/Ringtone/Fav_Ringtone.mp3
- python - 如何在 C++ 中读取羽毛文件时启用内存映射
- amazon-web-services - Amazon Appstream 2.0 桌面应用程序的性能测试
- sql - 远程服务器上的 SELECT @@ServerName 返回本地 @@ServerName
- php - 当我添加 Bootstrap 时,为什么我的导航栏文本会变高?
- javascript - 用 BeautifulSoup 抓取 fanduel,在 HTML 中找不到可见的值