Three.js:渲染器无法与 ArrayCamera 一起使用

标签 three.js

我在 html 中有以下内容:

<script type="module">
  import * as THREE from "https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cbbfa3b9aeae8bfbe5faf9fae5fa" rel="noreferrer noopener nofollow">[email protected]</a>/build/three.module.js";
  import { OrbitControls } from "https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2e5a465c4b4b6e1e001f1c1f001f" rel="noreferrer noopener nofollow">[email protected]</a>/examples/jsm/controls/OrbitControls.js";

  const renderer = new THREE.WebGLRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);

  const scene = new THREE.Scene();
  const cameraR = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
  );
  const cameraL = new THREE.PerspectiveCamera(
    75,
    window.innerWidth / window.innerHeight,
    0.1,
    1000
  );
  
  const axesHelper = new THREE.AxesHelper( 5 );
  scene.add( axesHelper );

  const geometry = new THREE.BoxBufferGeometry(1, 1, 1);
  const material = new THREE.MeshNormalMaterial();
  const cube = new THREE.Mesh(geometry, material);
  scene.add(cube);

  cameraR.position.x = 0.5;
  cameraR.position.z = 5;
  cameraL.position.x = -0.5;
  cameraL.position.z = 5;
  
  var cameras = [];
  cameras.push(cameraL)
  cameras.push(cameraR)     
  var cameraVR = new THREE.ArrayCamera( cameras );
  cameraVR.position.set( 0, 0, 5 );

  const controls = new OrbitControls(cameraVR, 
        renderer.domElement);
  controls.update();

  (function animate() {
    requestAnimationFrame(animate);
    
    cube.rotation.x += 0.01;
    cube.rotation.y += 0.01;

    controls.update();
    renderer.render(scene, cameraVR);
  })();

它给了我错误:

enter image description here

这段代码中我缺少什么?

编辑:编辑了之前的代码,因为分配给控件的相机之前是错误的。但问题仍然存在。

最佳答案

您的代码失败,因为您缺少为每个子摄像头添加视口(viewport)定义。 documentation状态:

An instance of ArrayCamera always has an array of sub cameras. It's mandatory to define for each sub camera the viewport property which determines the part of the viewport that is rendered with this camera.

我已相应更新了您的代码:

const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

const scene = new THREE.Scene();
const cameraR = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
cameraR.viewport = new THREE.Vector4( window.innerWidth / 2, 0, window.innerWidth / 2, window.innerHeight );
const cameraL = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);
cameraL.viewport = new THREE.Vector4( 0, 0, window.innerWidth / 2, window.innerHeight );

const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

const geometry = new THREE.BoxBufferGeometry(1, 1, 1);
const material = new THREE.MeshNormalMaterial();
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);

cameraR.position.x = 0.5;
cameraR.position.z = 5;
cameraR.lookAt( 0, 0, 0 );
cameraL.position.x = -0.5;
cameraL.position.z = 5;
cameraL.lookAt( 0, 0, 0 );

var cameras = [];
cameras.push(cameraL)
cameras.push(cameraR)
var cameraVR = new THREE.ArrayCamera(cameras);
cameraVR.position.set(0, 0, 5);


(function animate() {
  requestAnimationFrame(animate);

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

  renderer.render(scene, cameraVR);
})();
body {
      margin: 0;
}
<script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71051903141431415f404340" rel="noreferrer noopener nofollow">[email protected]</a>/build/three.js"></script>

官方阵列相机演示webgl_camera_array还演示了该类的用法。

关于Three.js:渲染器无法与 ArrayCamera 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74739711/

相关文章:

javascript - three.js中 `THREE.OrbitControls`的相机位置变化

image - Three.js 从图像导入 svg 路径

javascript - 如何使用轨道控制添加不同的摄像机?

javascript - 为什么我在加载具有 Material 的 GLTF 模型时得到黑色模型?

Three.js 没有对角线的线框

javascript - 将 Threejs 透视相机转换为正交相机需要什么

javascript - 将 DIV 悬停在 ThreeJS 对象上,使其位置与 3D 世界同步

javascript - 使用 Three.js 程序生成粒子纹理

javascript - 仅当我打开开发人员工具栏时加载模型

javascript - 在 THREE.JS 中手动对 Matrix4 应用旋转和平移