Three.js 切换相机

本文介绍了一个使用Three.js实现的双相机切换渲染的示例。通过设置相机的visible属性,配合渲染器render方法,实现了根据需求在两个不同视角的相机间进行切换的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

假设有两个相机根据需求进行切换.

我的想法是设置渲染器切换渲染的相机即可:renderer.render(scene, camera);然后用visible 属性来判断渲染哪一个相机.

以下为参考例子:

<html>
    <head>
        <title>My first three.js app</title>
        <style>
            body { margin: 0; }
            canvas { width: 100%; height: 100% }
        </style>
    </head>
    <body>
        <script src="../static/three.js-master/build/three.js"></script>
        <script>
            var scene = new THREE.Scene();  // 场景
            var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );  // 摄像机
            var camera2 = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 ); // 第二个摄像机
            camera2.visible = false;//虽然无效,但是通过这个属性判断渲染那个相机
            var renderer = new THREE.WebGLRenderer();  // 渲染器
            renderer.setSize( window.innerWidth, window.innerHeight );  // 渲染器宽度
            document.body.appendChild( renderer.domElement );

            var animate = function () {
                requestAnimationFrame( animate );  // 循环渲染

                cube.rotation.x += 0.01;
                cube.rotation.y += 0.01;
                if(camera.visible)
                {
                    renderer.render( scene, camera ); // 渲染摄像机1
                 }
                else
                {
                    renderer.render( scene, camera2 );// 渲染摄像机2
                 }
            };

            animate();
        </script>
    </body>
</html>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YanisWu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值