File size: 2,655 Bytes
2b7aae2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Matrix4 } from '../math/Matrix4.js';
import * as MathUtils from '../math/MathUtils.js';
import { PerspectiveCamera } from './PerspectiveCamera.js';

const _eyeRight = /*@__PURE__*/ new Matrix4();
const _eyeLeft = /*@__PURE__*/ new Matrix4();
const _projectionMatrix = /*@__PURE__*/ new Matrix4();

class StereoCamera {
	constructor() {
		this.type = 'StereoCamera';

		this.aspect = 1;

		this.eyeSep = 0.064;

		this.cameraL = new PerspectiveCamera();
		this.cameraL.layers.enable(1);
		this.cameraL.matrixAutoUpdate = false;

		this.cameraR = new PerspectiveCamera();
		this.cameraR.layers.enable(2);
		this.cameraR.matrixAutoUpdate = false;

		this._cache = {
			focus: null,
			fov: null,
			aspect: null,
			near: null,
			far: null,
			zoom: null,
			eyeSep: null,
		};
	}

	update(camera) {
		const cache = this._cache;

		const needsUpdate =
			cache.focus !== camera.focus ||
			cache.fov !== camera.fov ||
			cache.aspect !== camera.aspect * this.aspect ||
			cache.near !== camera.near ||
			cache.far !== camera.far ||
			cache.zoom !== camera.zoom ||
			cache.eyeSep !== this.eyeSep;

		if (needsUpdate) {
			cache.focus = camera.focus;
			cache.fov = camera.fov;
			cache.aspect = camera.aspect * this.aspect;
			cache.near = camera.near;
			cache.far = camera.far;
			cache.zoom = camera.zoom;
			cache.eyeSep = this.eyeSep;

			// Off-axis stereoscopic effect based on
			// http://paulbourke.net/stereographics/stereorender/

			_projectionMatrix.copy(camera.projectionMatrix);
			const eyeSepHalf = cache.eyeSep / 2;
			const eyeSepOnProjection = (eyeSepHalf * cache.near) / cache.focus;
			const ymax = (cache.near * Math.tan(MathUtils.DEG2RAD * cache.fov * 0.5)) / cache.zoom;
			let xmin, xmax;

			// translate xOffset

			_eyeLeft.elements[12] = -eyeSepHalf;
			_eyeRight.elements[12] = eyeSepHalf;

			// for left eye

			xmin = -ymax * cache.aspect + eyeSepOnProjection;
			xmax = ymax * cache.aspect + eyeSepOnProjection;

			_projectionMatrix.elements[0] = (2 * cache.near) / (xmax - xmin);
			_projectionMatrix.elements[8] = (xmax + xmin) / (xmax - xmin);

			this.cameraL.projectionMatrix.copy(_projectionMatrix);

			// for right eye

			xmin = -ymax * cache.aspect - eyeSepOnProjection;
			xmax = ymax * cache.aspect - eyeSepOnProjection;

			_projectionMatrix.elements[0] = (2 * cache.near) / (xmax - xmin);
			_projectionMatrix.elements[8] = (xmax + xmin) / (xmax - xmin);

			this.cameraR.projectionMatrix.copy(_projectionMatrix);
		}

		this.cameraL.matrixWorld.copy(camera.matrixWorld).multiply(_eyeLeft);
		this.cameraR.matrixWorld.copy(camera.matrixWorld).multiply(_eyeRight);
	}
}

export { StereoCamera };