three.js中外部导入模型运动的问题

发布于 2022-09-03 12:16:49 字数 6169 浏览 11 评论 0

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
undefined

object1.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 技术交流群。

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

发布评论

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

评论(1

挽你眉间 2022-09-10 12:16:49
  • -。js基础不行啊你。
    首先,全局变量一个object1,objLoader的回调一个形参object1,闹哪样,反正这样写全局的object1是不可能赋值的;

其次,load事件是异步的,也就是还没加载完模型,你就已经调用requestAnimationFrame(move);,那object1.position肯定是没有的,因为这个要等加载完模型才有的(前提是解决第一个问题先)。

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