three.js实现裸眼双目平行立体视觉

three.js实现裸眼双目平行立体视觉原理:

利用两个相机、两个渲染器,同时渲染同一个场景。

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Three.js双摄像机同时渲染显示(平行立体视觉效果)</title>
    <style>
        body { margin: 0;background-color: "#000000"; }
        canvas { display: block; }
        #canvas1 { position: absolute; top: 0; left: 0; width: 50%; height: 100%; border: 1px solid;}
        #canvas2 { position: absolute; top: 0; right: 0; width: 50%; height: 100%; border: 1px solid;}
    </style>
</head>
<body>
    <canvas id="canvas1"></canvas>
    <canvas id="canvas2"></canvas>

    <script type="importmap">
		{
		  "imports": {
			"three": "https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js",
			"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/"
		  }
		}
	</script>

	
    <script type="module">

        import * as THREE from 'three';

        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x87CEEB); // 设置背景为天蓝色

        // 创建一个环境光源
        const ambientLight = new THREE.AmbientLight(0x888888); // 颜色为深灰色
        scene.add(ambientLight);

        // 点光源灯光        
        const pointLight = new THREE.PointLight(0xFFFFFF, 500, 1000);
        pointLight.position.set(3, 30, -10); // 设置光的位置
        pointLight.castShadow = true;        

        pointLight.shadow.mapSize.width = 1024;
        pointLight.shadow.mapSize.height = 1024;
        pointLight.shadow.camera.near = 0.5;
        pointLight.shadow.camera.far = 500;

        scene.add(pointLight);
              

        //地基
        const geometry0 = new THREE.BoxGeometry(1000,1,1000);
        const material0 = new THREE.MeshStandardMaterial({
            color: 0xffffff,
            roughness: 0.7,
            metalness: 0.2
        });
        const cube0 = new THREE.Mesh(geometry0, material0);
        cube0.position.set(0, 0, 0);
        cube0.castShadow = true;
        cube0.receiveShadow = true;
        scene.add(cube0);


        // 立方体
        const geometry = new THREE.BoxGeometry(8,8,8);
        const material = new THREE.MeshStandardMaterial({
            color: 0xff0000,
            roughness: 0.7,
            metalness: 0.2
        });
        const cube = new THREE.Mesh(geometry, material);
        cube.position.set(3, 15, -30);
        cube.castShadow = true;
        cube.receiveShadow = true;
        scene.add(cube);

        // 第一个相机
        const camera1 = new THREE.PerspectiveCamera(30, window.innerWidth / 2 / window.innerHeight, 0.1, 1000);
        camera1.position.set(2, 30, 10);
        camera1.lookAt(cube.position);

        // 第二个相机
        const camera2 = new THREE.PerspectiveCamera(30, window.innerWidth / 2 / window.innerHeight, 0.1, 1000);
        camera2.position.set(4, 30, 10);
        camera2.lookAt(cube.position);

        // 第一个渲染器
        const renderer1 = new THREE.WebGLRenderer({ canvas: document.getElementById('canvas1') });
        renderer1.setSize(window.innerWidth / 2, window.innerHeight);
        renderer1.shadowMap.enabled = true;

        // 第二个渲染器
        const renderer2 = new THREE.WebGLRenderer({ canvas: document.getElementById('canvas2') });
        renderer2.setSize(window.innerWidth / 2, window.innerHeight);
        renderer2.shadowMap.enabled = true;

        // 动画循环
        function animate() {
            requestAnimationFrame(animate);

            cube.rotation.x += 0.01;
            cube.rotation.y += 0.01;
            cube.rotation.z += 0.01;

            renderer1.render(scene, camera1);
            renderer2.render(scene, camera2);
        }

        animate();
    </script>
</body>
</html>
相关推荐
Pu_Nine_94 小时前
教程: 在网页中利用原生CSS实现3D旋转动画
css·3d·css3
搞科研的小刘选手6 小时前
【通信&网络安全主题】第六届计算机通信与网络安全国际学术会议(CCNS 2025)
大数据·人工智能·网络安全·vr·通信工程·网络技术·计算机工程
A-上海蛙色元宇宙3DVR陈总11 小时前
红色 VR 大空间:技术赋能红色文化传承的运营价值与实践路径
vr
CAD老兵11 小时前
打造高性能二维图纸渲染引擎系列(一):Batched Geometry 助你轻松渲染百万实体
前端·webgl·three.js
CAD老兵11 小时前
打造高性能二维图纸渲染引擎系列(三):高性能 CAD 文本渲染背后的隐藏工程
前端·webgl·three.js
CAD老兵11 小时前
打造高性能二维图纸渲染引擎系列(二):创建结构化和可扩展的渲染场景
前端·webgl·three.js
勤奋菲菲1 天前
Vue3+Three.js:requestAnimationFrame的详细介绍
开发语言·javascript·three.js·前端可视化
程序猿阿伟1 天前
《3D山地场景渲染进阶:GPU驱动架构下细节与性能平衡的6大技术实践》
3d·架构
2401_863801461 天前
Cesium格式模型制作,3dtiles制作B3DM格式文件制作。数字孪生模型制作
3d
fanstering2 天前
腾讯混元P3-SAM: Native 3D Part Segmentation
笔记·学习·3d·点云