【问题标题】:one OrbitControls with two scenes一个带有两个场景的 OrbitControls
【发布时间】:2022-07-01 09:35:39
【问题描述】:

我想将两个 .dae 文件(并排在两个场景中)的鼠标控制与 OrbitControls 同步。我可以使用我的任何一个场景单独控制任何一个对象,但是任何同步控制两个对象的尝试都失败了。 好像我只能有一个 OrbitControls 实例。以下任何一个 'controls#' 行都可以独立工作,但只要我同时拥有这两个行,只有第一个是有效的: controls1 = new OrbitControls(camera1, renderer1.domElement); controls2 = new OrbitControls(camera2, renderer2.domElement);

我是一名化学教授,而不是程序员。感谢任何帮助。谢谢!

<html lang="en">
    <head>
        <title>GD - 2 mols</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <link type="text/css" rel="stylesheet" href="main.css">
    </head>
    <body>
        <h3> VB orbital visualizer: 2 molecules</h3>
        

<div id="molcontainer1"></div>
<div id="molcontainer2"></div>
        
    <!-- Remove this when import maps will be widely supported -->
        <script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>

        <script type="importmap">
            {
                "imports": {
                    "three": "../three/build/three.module.js"
                }
            }
        </script>

        <script type="module">

            import * as THREE from 'three';
            import { ColladaLoader } from '../three/examples/jsm/loaders/ColladaLoader.js';         
            import { OrbitControls } from '../three/examples/jsm/controls/OrbitControls.js';

            let molcontainer1, molcontainer2, camera1, camera2, scene1, scene2, renderer1, renderer2, mol1, mol2;

            init();
            //animate();

            function init() {

                molcontainer1 = document.getElementById( 'molcontainer1' );
                molcontainer2 = document.getElementById( 'molcontainer2' );
                
                //scene, camera, lighting, etc
                
                scene1 = new THREE.Scene();
                scene1.background = new THREE.Color( 0xbbffff ); 
                scene2 = new THREE.Scene();
                scene2.background = new THREE.Color( 0xffcccc );
                
                camera1 = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
                camera1.position.set( 0.4, 0.4, 0.4 );
                camera1.lookAt( 0, 0, 0 );
                camera2 = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 2000 );
                camera2.position.set( 0.4, 0.4, 0.4 );
                camera2.lookAt( 0, 0, 0 );

                const ambientLight1 = new THREE.AmbientLight( 0xffffff, 1 );  // was 0xcccccc
                scene1.add( ambientLight1 ); 
                const ambientLight2 = new THREE.AmbientLight( 0xffffff, 1 );  // was 0xcccccc
                scene2.add( ambientLight2 );

                const directionalLight1 = new THREE.DirectionalLight( 0xffffff, 0.8 );
                directionalLight1.position.set( 1, 1, 1 ).normalize();
                scene1.add( directionalLight1 );
                const directionalLight2 = new THREE.DirectionalLight( 0xffffff, 0.8 );
                directionalLight2.position.set( 1, 1, 1 ).normalize();
                scene2.add( directionalLight2 );

                renderer1 = new THREE.WebGLRenderer();
                renderer1.outputEncoding = THREE.sRGBEncoding;
                renderer1.setPixelRatio( window.devicePixelRatio );
                renderer1.setSize( window.innerWidth/2, window.innerHeight/2 );
                molcontainer1.appendChild( renderer1.domElement );
                
                renderer2 = new THREE.WebGLRenderer();
                renderer2.outputEncoding = THREE.sRGBEncoding;
                renderer2.setPixelRatio( window.devicePixelRatio );
                renderer2.setSize( window.innerWidth/2, window.innerHeight/2 );
                molcontainer2.appendChild( renderer2.domElement );
                
                // loading manager

                const loadingManager = new THREE.LoadingManager( function () {
                    
                    scene1.add( mol1 );
                    scene2.add( mol2 );

                } );

                // assign collada .dae file

                const loader1 = new ColladaLoader( loadingManager );
                loader1.load( './models/molecule.dae', function ( collada ) {

                    mol1 = collada.scene;
                    animate();
                    render();
                } );

                const loader2 = new ColladaLoader( loadingManager );
                loader2.load( './models/CH2CHO.dae', function ( collada ) {

                    mol2 = collada.scene;
                    animate();
                    render();
                } );
                
                // 3d mouse controls    
                
                controls1 = new OrbitControls( camera1, renderer1.domElement );
                controls2 = new OrbitControls( camera2, renderer2.domElement );
                controls1.addEventListener( 'change', render );
                controls2.addEventListener( 'change', render );
                controls1.target.set( 0, 0, 0 ); 
                controls2.target.set( 0, 0, 0 ); 
                //controls.update();
                window.addEventListener( 'resize', onWindowResize ); 
            }
        

            function animate() {
              
                requestAnimationFrame( animate);
                
                window.addEventListener( 'resize', onWindowResize );    // just added this   
                render();
            }

            
            function onWindowResize() {

                camera1.aspect = window.innerWidth / window.innerHeight;
                camera1.updateProjectionMatrix();
                renderer1.setSize( window.innerWidth/2, window.innerHeight/2 );
                camera2.aspect = window.innerWidth / window.innerHeight;
                camera2.updateProjectionMatrix();
                renderer2.setSize( window.innerWidth/2, window.innerHeight/2 );
                
                render();
            }
            
            function render() {

                renderer1.render( scene1, camera1 );
                renderer2.render( scene2, camera2 );
                //controls.update();
            }

        </script>
    </body>
</html>```

【问题讨论】:

    标签: three.js


    【解决方案1】:

    您实际上只需要一个摄像头和一个OrbitControls 来驱动两个渲染器和Scenes。这样做的原因是两者都与渲染器或Scene 没有内在联系;相机基本上只跟踪一组变量,在每一帧上,这些变量允许渲染器计算其投影,OrbitControls 只关心用户与页面(定义部分)的交互以及需要其值的相机变异。 (同样请注意,相机和OrbitControls 在初始化时都不会引用渲染器或Scene,并且您看到的大多数示例都没有使用Scene.Add() 将相机添加到场景图中.)

    因此,您的代码需要进行以下修改。

    首先,将您的molcontainer* divs 包裹在另一个div 中,并为其指定一个ID,例如molwrapper。这个包装器div 将定义页面上的区域(包括两个场景),它将接收OrbitControls 的用户交互。使用document.getElementById() 存储对molwrapper 的引用。

    接下来,删除对camera2 的所有引用并将camera1 重命名为camera,并对controls2controls1 执行相同操作。

    最后,初始化controls(以前是controls1),将它的引用传递给camera(以前是camera1)和molwrapper

    我在 CodePen 上发布了一个有效的example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多