From f29b72f0e8775d676b2788a28e2530076e4b94e1 Mon Sep 17 00:00:00 2001 From: Brian Peiris Date: Sun, 5 Apr 2015 21:53:38 +0000 Subject: [PATCH] Fix camera orientation glitch and improve VR Cubes Remove the camera animation so that HMD orientation is applied correctly and add a crosshair instead of mouse-based intersection. --- examples/vr_cubes.html | 52 ++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/examples/vr_cubes.html b/examples/vr_cubes.html index 7c9f551fd5..8056385682 100644 --- a/examples/vr_cubes.html +++ b/examples/vr_cubes.html @@ -58,8 +58,9 @@ var vrEffect; var vrControls; - var mouse = new THREE.Vector2(), INTERSECTED; + var INTERSECTED; var radius = 100, theta = 0; + var crosshair; init(); animate(); @@ -81,6 +82,16 @@ scene = new THREE.Scene(); + crosshair = new THREE.Mesh( + new THREE.RingGeometry( 0.5, 1, 32 ), + new THREE.LineBasicMaterial( { + color: 0x00bb00, + transparent: true, + opacity: 0.5 + } ) + ); + scene.add( crosshair ); + var light = new THREE.DirectionalLight( 0xffffff, 1 ); light.position.set( 1, 1, 1 ).normalize(); scene.add( light ); @@ -130,7 +141,9 @@ } ); fullScreenButton.onclick = function() { + vrEffect.setFullScreen( true ); + }; renderer.setClearColor( 0xf0f0f0 ); @@ -143,8 +156,6 @@ stats.domElement.style.top = '0px'; container.appendChild( stats.domElement ); - document.addEventListener( 'mousemove', onDocumentMouseMove, false ); - // window.addEventListener( 'resize', onWindowResize, false ); @@ -160,15 +171,6 @@ } - function onDocumentMouseMove( event ) { - - event.preventDefault(); - - mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1; - mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1; - - } - // function animate() { @@ -182,18 +184,9 @@ function render() { - theta += 0.1; - - camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) ); - camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) ); - camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) ); - camera.lookAt( scene.position ); - - camera.updateMatrixWorld(); - // find intersections - raycaster.setFromCamera( mouse, camera ); + raycaster.setFromCamera( { x: 0, y: 0 }, camera ); var intersects = raycaster.intersectObjects( scene.children ); @@ -218,6 +211,21 @@ } vrControls.update(); + crosshair.quaternion.copy( camera.quaternion ); + crosshair.position.set( 0, 0, 0 ); + if ( INTERSECTED ) { + + crosshair.translateZ( + -scene.position.distanceTo( INTERSECTED.position ) + + INTERSECTED.geometry.boundingSphere.radius + 5 + ); + + } + else { + + crosshair.translateZ(-40); + + } vrEffect.render( scene, camera ); } -- GitLab