<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
<
title
>Three.js Multiple Cameras Example</
title
>
<
style
>
body {
margin: 0;
}
</
style
>
</
head
>
<
body
>
<
script
type
=
"module"
>
import * as THREE from
// Create scene
const scene = new THREE.Scene();
// Create cameras
const camera1 = new THREE.PerspectiveCamera(75,
window.innerWidth
/ window.innerHeight, 0.1, 1000);
const camera2 = new THREE.PerspectiveCamera(75,
window.innerWidth
/ window.innerHeight, 0.1, 1000);
// Position cameras
camera1.position.set(0, 0, 5);
camera2.position.set(0, 0, -5);
// Create renderer
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Create cube
const geometry = new THREE.BoxGeometry();
const material =
new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// Handle window resize
window.addEventListener('resize', () => {
const newWidth = window.innerWidth;
const newHeight = window.innerHeight;
camera1.aspect = newWidth / newHeight;
camera1.updateProjectionMatrix();
camera2.aspect = newWidth / newHeight;
camera2.updateProjectionMatrix();
renderer.setSize(newWidth, newHeight);
});
// Animation loop
const animate = () => {
requestAnimationFrame(animate);
// Rotate cube
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
// Render scene with both cameras
renderer.setViewport(0, 0, window.innerWidth / 2,
window.innerHeight);
renderer.setScissor(0, 0, window.innerWidth / 2,
window.innerHeight);
renderer.setScissorTest(true);
renderer.render(scene, camera1);
renderer.setViewport(window.innerWidth / 2,
0, window.innerWidth / 2, window.innerHeight);
renderer.setScissor(window.innerWidth / 2,
0, window.innerWidth / 2, window.innerHeight);
renderer.render(scene, camera2);
};
// Start animation loop
animate();
</
script
>
</
body
>
</
html
>