|
@@ -0,0 +1,308 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+ <head>
|
|
|
+ <title>three.js webvr - htc vive</title>
|
|
|
+ <meta charset="utf-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
|
+ <style>
|
|
|
+ body {
|
|
|
+ font-family: Monospace;
|
|
|
+ background-color: #101010;
|
|
|
+ color: #fff;
|
|
|
+ margin: 0px;
|
|
|
+ overflow: hidden;
|
|
|
+ }
|
|
|
+ a {
|
|
|
+ color: #f00;
|
|
|
+ }
|
|
|
+ </style>
|
|
|
+ </head>
|
|
|
+ <body>
|
|
|
+
|
|
|
+ <script src="../build/three.js"></script>
|
|
|
+
|
|
|
+ <script src="js/WebVR.js"></script>
|
|
|
+ <script src="js/effects/VREffect.js"></script>
|
|
|
+ <script src="js/controls/VRControls.js"></script>
|
|
|
+ <script src="js/ViveController.js"></script>
|
|
|
+
|
|
|
+ <script src="js/loaders/OBJLoader.js"></script>
|
|
|
+
|
|
|
+ <script>
|
|
|
+
|
|
|
+ if ( WEBVR.isLatestAvailable() === false ) {
|
|
|
+
|
|
|
+ document.body.appendChild( WEBVR.getMessage() );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ var clock = new THREE.Clock();
|
|
|
+
|
|
|
+ var container;
|
|
|
+ var camera, scene, renderer;
|
|
|
+ var effect, controls;
|
|
|
+ var controller1, controller2;
|
|
|
+
|
|
|
+ var raycaster, intersected = [];
|
|
|
+ var tempMatrix = new THREE.Matrix4();
|
|
|
+
|
|
|
+ var room, group;
|
|
|
+
|
|
|
+ init();
|
|
|
+ animate();
|
|
|
+
|
|
|
+ function init() {
|
|
|
+
|
|
|
+ container = document.createElement( 'div' );
|
|
|
+ document.body.appendChild( container );
|
|
|
+
|
|
|
+ var info = document.createElement( 'div' );
|
|
|
+ info.style.position = 'absolute';
|
|
|
+ info.style.top = '10px';
|
|
|
+ info.style.width = '100%';
|
|
|
+ info.style.textAlign = 'center';
|
|
|
+ info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - htc vive';
|
|
|
+ container.appendChild( info );
|
|
|
+
|
|
|
+ scene = new THREE.Scene();
|
|
|
+
|
|
|
+ camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
|
|
|
+ scene.add( camera );
|
|
|
+
|
|
|
+ room = new THREE.Mesh(
|
|
|
+ new THREE.BoxGeometry( 6, 6, 6, 8, 8, 8 ),
|
|
|
+ new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
|
|
|
+ );
|
|
|
+ room.position.y = 3;
|
|
|
+ scene.add( room );
|
|
|
+
|
|
|
+ scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
|
|
|
+
|
|
|
+ var light = new THREE.DirectionalLight( 0xffffff );
|
|
|
+ light.position.set( 1, 1, 1 ).normalize();
|
|
|
+ scene.add( light );
|
|
|
+
|
|
|
+ group = new THREE.Group();
|
|
|
+ scene.add( group );
|
|
|
+
|
|
|
+ var geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
|
|
|
+
|
|
|
+ for ( var i = 0; i < 200; i ++ ) {
|
|
|
+
|
|
|
+ var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
|
|
|
+
|
|
|
+ object.position.x = Math.random() * 4 - 2;
|
|
|
+ object.position.y = Math.random() * 6;
|
|
|
+ object.position.z = Math.random() * 4 - 2;
|
|
|
+
|
|
|
+ object.rotation.x = Math.random() * 2 * Math.PI;
|
|
|
+ object.rotation.y = Math.random() * 2 * Math.PI;
|
|
|
+ object.rotation.z = Math.random() * 2 * Math.PI;
|
|
|
+
|
|
|
+ object.scale.x = Math.random() + 0.5;
|
|
|
+ object.scale.y = Math.random() + 0.5;
|
|
|
+ object.scale.z = Math.random() + 0.5;
|
|
|
+
|
|
|
+ object.userData.velocity = new THREE.Vector3();
|
|
|
+ object.userData.velocity.x = Math.random() * 0.01 - 0.005;
|
|
|
+ object.userData.velocity.y = Math.random() * 0.01 - 0.005;
|
|
|
+ object.userData.velocity.z = Math.random() * 0.01 - 0.005;
|
|
|
+
|
|
|
+ group.add( object );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ renderer = new THREE.WebGLRenderer( { antialias: true } );
|
|
|
+ renderer.setClearColor( 0x505050 );
|
|
|
+ renderer.setPixelRatio( window.devicePixelRatio );
|
|
|
+ renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
+ renderer.sortObjects = false;
|
|
|
+ container.appendChild( renderer.domElement );
|
|
|
+
|
|
|
+ controls = new THREE.VRControls( camera );
|
|
|
+ controls.standing = true;
|
|
|
+
|
|
|
+ // controllers
|
|
|
+
|
|
|
+ controller1 = new THREE.ViveController( 0 );
|
|
|
+ controller1.standingMatrix = controls.getStandingMatrix();
|
|
|
+ controller1.addEventListener( 'triggerdown', onTriggerDown );
|
|
|
+ controller1.addEventListener( 'triggerup', onTriggerUp );
|
|
|
+ scene.add( controller1 );
|
|
|
+
|
|
|
+ controller2 = new THREE.ViveController( 1 );
|
|
|
+ controller2.standingMatrix = controls.getStandingMatrix();
|
|
|
+ controller2.addEventListener( 'triggerdown', onTriggerDown );
|
|
|
+ controller2.addEventListener( 'triggerup', onTriggerUp );
|
|
|
+ scene.add( controller2 );
|
|
|
+
|
|
|
+ var loader = new THREE.OBJLoader();
|
|
|
+ loader.setPath( 'models/obj/vive-controller/' );
|
|
|
+ loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
|
|
|
+
|
|
|
+ var loader = new THREE.TextureLoader();
|
|
|
+ loader.setPath( 'models/obj/vive-controller/' );
|
|
|
+
|
|
|
+ var controller = object.children[ 0 ];
|
|
|
+ controller.material.map = loader.load( 'onepointfive_texture.png' );
|
|
|
+ controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
|
|
|
+
|
|
|
+ controller1.add( object.clone() );
|
|
|
+ controller2.add( object.clone() );
|
|
|
+
|
|
|
+ } );
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ var geometry = new THREE.Geometry();
|
|
|
+ geometry.vertices.push( new THREE.Vector3( 0, 0, 0 ) );
|
|
|
+ geometry.vertices.push( new THREE.Vector3( 0, 0, - 5 ) );
|
|
|
+ var line = new THREE.Line( geometry );
|
|
|
+
|
|
|
+ controller1.add( line.clone() );
|
|
|
+ controller2.add( line.clone() );
|
|
|
+
|
|
|
+ raycaster = new THREE.Raycaster();
|
|
|
+
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ effect = new THREE.VREffect( renderer );
|
|
|
+
|
|
|
+ if ( WEBVR.isAvailable() === true ) {
|
|
|
+
|
|
|
+ document.body.appendChild( WEBVR.getButton( effect ) );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ window.addEventListener( 'resize', onWindowResize, false );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onWindowResize() {
|
|
|
+
|
|
|
+ camera.aspect = window.innerWidth / window.innerHeight;
|
|
|
+ camera.updateProjectionMatrix();
|
|
|
+
|
|
|
+ effect.setSize( window.innerWidth, window.innerHeight );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onTriggerDown( event ) {
|
|
|
+
|
|
|
+ var controller = event.target;
|
|
|
+
|
|
|
+ var intersections = getIntersections( controller );
|
|
|
+
|
|
|
+ if ( intersections.length > 0 ) {
|
|
|
+
|
|
|
+ var intersection = intersections[ 0 ];
|
|
|
+
|
|
|
+ tempMatrix.getInverse( controller.matrixWorld );
|
|
|
+
|
|
|
+ var object = intersection.object;
|
|
|
+ object.matrix.premultiply( tempMatrix );
|
|
|
+ object.matrix.decompose( object.position, object.quaternion, object.scale );
|
|
|
+ controller.add( object );
|
|
|
+
|
|
|
+ controller.userData.selected = object;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function onTriggerUp( event ) {
|
|
|
+
|
|
|
+ var controller = event.target;
|
|
|
+
|
|
|
+ if ( controller.userData.selected !== undefined ) {
|
|
|
+
|
|
|
+ var object = controller.userData.selected;
|
|
|
+
|
|
|
+ object.matrix.premultiply( controller.matrixWorld );
|
|
|
+ object.matrix.decompose( object.position, object.quaternion, object.scale );
|
|
|
+ group.add( object );
|
|
|
+
|
|
|
+ controller.userData.selected = undefined;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function getIntersections( controller ) {
|
|
|
+
|
|
|
+ tempMatrix.identity().extractRotation( controller.matrixWorld );
|
|
|
+
|
|
|
+ raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
|
|
|
+ raycaster.ray.direction.set( 0, 0, -1 ).applyMatrix4( tempMatrix );
|
|
|
+
|
|
|
+ return raycaster.intersectObjects( group.children );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function intersectObjects( controller ) {
|
|
|
+
|
|
|
+ // Do not highlight when already selected
|
|
|
+
|
|
|
+ if ( controller.userData.selected !== undefined ) return;
|
|
|
+
|
|
|
+ var intersections = getIntersections( controller );
|
|
|
+
|
|
|
+ if ( intersections.length > 0 ) {
|
|
|
+
|
|
|
+ var intersection = intersections[ 0 ];
|
|
|
+ intersection.object.material.emissive.r = 1;
|
|
|
+ intersected.push( intersection.object );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function cleanIntersected() {
|
|
|
+
|
|
|
+ while ( intersected.length ) {
|
|
|
+
|
|
|
+ var object = intersected.pop();
|
|
|
+ object.material.emissive.r = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ function animate() {
|
|
|
+
|
|
|
+ effect.requestAnimationFrame( animate );
|
|
|
+ render();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ function render() {
|
|
|
+
|
|
|
+ var delta = clock.getDelta() * 60;
|
|
|
+
|
|
|
+ controller1.update();
|
|
|
+ controller2.update();
|
|
|
+
|
|
|
+ controls.update();
|
|
|
+
|
|
|
+ cleanIntersected();
|
|
|
+
|
|
|
+ intersectObjects( controller1 );
|
|
|
+ intersectObjects( controller2 );
|
|
|
+
|
|
|
+ effect.render( scene, camera );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ </script>
|
|
|
+ </body>
|
|
|
+</html>
|