three.js中外部导入模型运动的问题
three.js中从外部加载的obj模型,已经可以视角跟随鼠标移动(从官网抄的代码),现在想让模型自身运动,然而无法实现。代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Door</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="three.js"></script>
<script src="DDSLoader.js"></script>
<script src="OBJMTLLoader.js"></script>
<script src="MTLLoader.js"></script>
<script src="OBJLoader.js"></script>
<script src="Detector.js"></script>
<script src="stats.min.js"></script>
<script>
var a;
var object1;
var container, stats;
var camera, scene, renderer;
var mouseX = 0, mouseY = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 250;
scene = new THREE.Scene();
var ambient = new THREE.AmbientLight( 0x444444 );
scene.add( ambient );
var directionalLight = new THREE.DirectionalLight( 0xffffff );
directionalLight.position.set( 0, 0, 1 ).normalize();
scene.add( directionalLight );
var material3 = new THREE.MeshLambertMaterial({
color: 0xD3D3D3,
side: THREE.DoubleSid
});
var plane1 = new THREE.PlaneGeometry(15, 15, 8, 8);
plane1.rotateX(-Math.PI / 2);
mesh1 = new THREE.Mesh(plane1,material3);
mesh1.position.setY(-95);
mesh1.position.setZ(-1.5);
mesh1.scale.set(50, 50, 50)
mesh1.receiveShadow = true;
scene.add(mesh1);
THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load( 'model/door_2.mtl', function( materials ) {
materials.preload();
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials( materials );
objLoader.load( 'model/door_2.obj', function ( object1 ) {
object1.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material.side = THREE.DoubleSide;
}
})
object1.position.x = 95;
object1.position.y = - 95;
object1.scale.set(0.08, 0.08, 0.08);
scene.add( object1 );
});
});
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
window.addEventListener( 'resize', onWindowResize, false );
requestAnimationFrame(move);
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseMove( event ) {
mouseX = ( event.clientX - windowHalfX ) / 2;
mouseY = ( event.clientY - windowHalfY ) / 2;
}
function animate() {
requestAnimationFrame( animate );
render();
}
function move() {
a += 0.01;
if (a > Math.PI * 2) {
a -= Math.PI * 2;
}
object1.position.set(2 * Math.cos(a), 0, 2 * Math.sin(a));
object1.rotation.y += 0.01;
renderer.render(scene, camera);
requestAnimationFrame(move);
}
function render() {
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY - camera.position.y ) * .05;
camera.lookAt( scene.position );
renderer.render( scene, camera );
}
</script>
</body>
</html>
报错如下:
THREE.WebGLRenderer 76 three.js (第 24764 行) TypeError: object1 is
undefinedobject1.position.set(2 Math.cos(a), 0, 2 Math.sin(a));
door_2.html (第 196 行,第 17 列) 100% downloaded door_2.html (第 85 行)
OBJLoader: 40.32ms
请问如何解决呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-。js基础不行啊你。
首先,全局变量一个object1,objLoader的回调一个形参object1,闹哪样,反正这样写全局的object1是不可能赋值的;
其次,load事件是异步的,也就是还没加载完模型,你就已经调用requestAnimationFrame(move);,那object1.position肯定是没有的,因为这个要等加载完模型才有的(前提是解决第一个问题先)。