Skip to content

Commit f676cab

Browse files
authored
Merge pull request #14950 from jsantell/frustum-fix
Use a combined frustum for culling when in VR
2 parents 3395231 + fd5077b commit f676cab

File tree

5 files changed

+194
-15
lines changed

5 files changed

+194
-15
lines changed

examples/files.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ var files = {
325325
"webvr_ballshooter",
326326
"webvr_cubes",
327327
"webvr_dragging",
328+
"webvr_frustum",
328329
"webvr_lorenzattractor",
329330
"webvr_panorama",
330331
"webvr_paint",

examples/webvr_frustum.html

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>three.js webvr - frustum</title>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
7+
<!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-09-11 -->
8+
<meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-09-11" content="AqhFUYKxq/d+E8CDT0fuYRCg8TvlTP52x0Jv7I9t27sLhR30LmcahBRfSwzP89ukjs2+ia99VrrLoRyaFAwJVA0AAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUzNjYyNDAwMH0=">
9+
<!-- Origin Trial Token, feature = WebXR Device API (For Chrome M69+), origin = https://threejs.org, expires = 2018-10-27 -->
10+
<meta http-equiv="origin-trial" data-feature="WebXR Device API (For Chrome M69+)" data-expires="2018-10-27" content="An4ZYOGvf6kVHNxqZxS02TPAvpZESkmBhcVCM/byViDDuEB2XKvCF43aCJjrAU/R8H3WDlv+1bDGTL/XxstHGgoAAABTeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZU02OSIsImV4cGlyeSI6MTU0MDY1NTAyMn0=">
11+
<!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-10-24 -->
12+
<meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-10-24" content="Agrr6lZhlwzv5jmv/mpLZA37DIiVcg3HvX8bH8EWB+OBruV3sUJuzDfYz6qs/je+LcH41DkrmPn4k9RaUaqpQAAAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTQwMzg4NjI0fQ==">
13+
<style>
14+
body {
15+
font-family: Monospace;
16+
background-color: #101010;
17+
color: #fff;
18+
margin: 0px;
19+
overflow: hidden;
20+
}
21+
a {
22+
color: #f00;
23+
}
24+
</style>
25+
</head>
26+
<body>
27+
28+
<script src="../build/three.js"></script>
29+
<script src="js/vr/WebVR.js"></script>
30+
<script src="js/geometries/BoxLineGeometry.js"></script>
31+
32+
<script>
33+
34+
var size = 10;
35+
36+
var container;
37+
var camera, scene, raycaster, renderer;
38+
39+
var room;
40+
41+
init();
42+
animate();
43+
44+
function init() {
45+
46+
container = document.createElement( 'div' );
47+
document.body.appendChild( container );
48+
49+
var info = document.createElement( 'div' );
50+
info.style.position = 'absolute';
51+
info.style.top = '10px';
52+
info.style.width = '100%';
53+
info.style.textAlign = 'center';
54+
info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - interactive cubes';
55+
container.appendChild( info );
56+
57+
scene = new THREE.Scene();
58+
scene.background = new THREE.Color( 0x505050 );
59+
60+
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
61+
scene.add( camera );
62+
63+
room = new THREE.LineSegments(
64+
new THREE.BoxLineGeometry( 6, 6, 6, 10, 10, 10 ),
65+
new THREE.LineBasicMaterial( { color: 0x808080 } )
66+
);
67+
room.position.y = 3;
68+
scene.add( room );
69+
70+
scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
71+
72+
var geometry = new THREE.BoxBufferGeometry( 0.15, 0.15, 0.15 );
73+
74+
for ( var x = 0; x < size; x ++ ) {
75+
for ( var y = 0; y < size; y ++ ) {
76+
77+
var xVal = x / size;
78+
var yVal = y / size;
79+
var color = new THREE.Color( 1, 1, 1 );
80+
color.r = x % 2;
81+
color.g = y % 2;
82+
color.b = 1;
83+
var object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: color } ));
84+
85+
object.position.x = xVal * 8 - 4;
86+
object.position.y = yVal * 8 - 4;
87+
object.position.z = -4;
88+
89+
room.add( object );
90+
91+
}
92+
}
93+
94+
renderer = new THREE.WebGLRenderer( { antialias: true } );
95+
renderer.setPixelRatio( window.devicePixelRatio );
96+
renderer.setSize( window.innerWidth, window.innerHeight );
97+
renderer.vr.enabled = true;
98+
container.appendChild( renderer.domElement );
99+
100+
window.addEventListener( 'resize', onWindowResize, false );
101+
102+
document.body.appendChild( WEBVR.createButton( renderer ) );
103+
104+
}
105+
106+
function onWindowResize() {
107+
108+
camera.aspect = window.innerWidth / window.innerHeight;
109+
camera.updateProjectionMatrix();
110+
111+
renderer.setSize( window.innerWidth, window.innerHeight );
112+
113+
}
114+
115+
function animate() {
116+
117+
renderer.setAnimationLoop( render );
118+
119+
}
120+
121+
function render() {
122+
123+
renderer.render( scene, camera );
124+
125+
}
126+
127+
</script>
128+
</body>
129+
</html>

src/cameras/ArrayCamera.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import { PerspectiveCamera } from './PerspectiveCamera.js';
6+
import { Vector3 } from '../math/Vector3.js';
67

78
function ArrayCamera( array ) {
89

@@ -16,7 +17,66 @@ ArrayCamera.prototype = Object.assign( Object.create( PerspectiveCamera.prototyp
1617

1718
constructor: ArrayCamera,
1819

19-
isArrayCamera: true
20+
isArrayCamera: true,
21+
22+
/**
23+
* Assumes 2 cameras that are perpendicular and share an X-axis, and that
24+
* the cameras' projection and world matrices have already been set.
25+
* And that near and far planes are identical for both cameras.
26+
*/
27+
setProjectionFromUnion: function () {
28+
var cameraLPos = new Vector3();
29+
var cameraRPos = new Vector3();
30+
31+
return function () {
32+
cameraLPos.setFromMatrixPosition( this.cameras[ 0 ].matrixWorld );
33+
cameraRPos.setFromMatrixPosition( this.cameras[ 1 ].matrixWorld );
34+
35+
var ipd = cameraLPos.distanceTo( cameraRPos );
36+
37+
var projL = this.cameras[ 0 ].projectionMatrix;
38+
var projR = this.cameras[ 1 ].projectionMatrix;
39+
40+
// VR systems will have identical far and near planes, and
41+
// most likely identical top and bottom frustum extents.
42+
// via: https://computergraphics.stackexchange.com/a/4765
43+
var near = projL[ 14 ] / ( projL[ 10 ] - 1 );
44+
var far = projL[ 14 ] / ( projL[ 10 ] + 1 );
45+
46+
var leftFovL = ( projL[ 8 ] - 1 ) / projL[ 0 ];
47+
var rightFovR = ( projR[ 8 ] + 1 ) / projR[ 0 ];
48+
var leftL = leftFovL * near;
49+
var rightR = rightFovR * near;
50+
var topL = near * ( projL[ 9 ] + 1 ) / projL[ 5 ];
51+
var topR = near * ( projR[ 9 ] + 1 ) / projR[ 5 ];
52+
var bottomL = near * ( projL[ 9 ] - 1 ) / projL[ 5 ];
53+
var bottomR = near * ( projR[ 9 ] - 1 ) / projR[ 5 ];
54+
55+
// Calculate the new camera's position offset from the
56+
// left camera.
57+
var zOffset = ipd / (leftFovL + rightFovR);
58+
var xOffset = zOffset * leftFovL;
59+
60+
// TODO: Better way to apply this offset?
61+
this.cameras[ 0 ].matrixWorld.decompose( this.position, this.quaternion, this.scale );
62+
this.translateX(xOffset);
63+
this.translateZ(-zOffset);
64+
this.matrixWorld.compose( this.position, this.quaternion, this.scale );
65+
this.matrixWorldInverse.getInverse(this.matrixWorld);
66+
67+
// Find the union of the frustum values of the cameras and scale
68+
// the values so that the near plane's position does not change in world space,
69+
// although must now be relative to the new union camera.
70+
var near2 = near + zOffset;
71+
var far2 = far + zOffset;
72+
var left = leftL - xOffset;
73+
var right = rightR + (ipd - xOffset)
74+
var top = Math.max( topL, topR );
75+
var bottom = Math.min( bottomL, bottomR );
76+
77+
this.projectionMatrix.makePerspective( left, right, top, bottom, near2, far2 );
78+
}
79+
}(),
2080

2181
} );
2282

src/renderers/webvr/WebVRManager.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,6 @@ function WebVRManager( renderer ) {
290290
cameraL.far = camera.far;
291291
cameraR.far = camera.far;
292292

293-
cameraVR.matrixWorld.copy( camera.matrixWorld );
294-
cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
295-
296293
cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
297294
cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
298295

@@ -326,10 +323,7 @@ function WebVRManager( renderer ) {
326323
cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
327324
cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
328325

329-
// HACK (mrdoob)
330-
// https://github.com/w3c/webvr/issues/203
331-
332-
cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
326+
cameraVR.setProjectionFromUnion();
333327

334328
//
335329

src/renderers/webvr/WebXRManager.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,6 @@ function WebXRManager( renderer ) {
161161
var parent = camera.parent;
162162
var cameras = cameraVR.cameras;
163163

164-
// apply camera.parent to cameraVR
165-
166164
updateCamera( cameraVR, parent );
167165

168166
for ( var i = 0; i < cameras.length; i ++ ) {
@@ -183,6 +181,8 @@ function WebXRManager( renderer ) {
183181

184182
}
185183

184+
cameraVR.setProjectionFromUnion();
185+
186186
return cameraVR;
187187

188188
}
@@ -221,11 +221,6 @@ function WebXRManager( renderer ) {
221221

222222
cameraVR.matrix.copy( camera.matrix );
223223

224-
// HACK (mrdoob)
225-
// https://github.com/w3c/webvr/issues/203
226-
227-
cameraVR.projectionMatrix.copy( camera.projectionMatrix );
228-
229224
}
230225

231226
}

0 commit comments

Comments
 (0)