Browse Source

Added HTC Vive example.

Mr.doob 9 years ago
parent
commit
ad7d5b75b5
2 changed files with 284 additions and 1 deletions
  1. 2 1
      examples/files.js
  2. 282 0
      examples/webvr_vive.html

+ 2 - 1
examples/files.js

@@ -240,7 +240,8 @@ var files = {
 		"webvr_panorama",
 		"webvr_rollercoaster",
 		"webvr_shadow",
-		"webvr_video"
+		"webvr_video",
+		"webvr_vive"
 	],
 	"css3d": [
 		"css3d_molecules",

+ 282 - 0
examples/webvr_vive.html

@@ -0,0 +1,282 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webvr - cubes</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<style>
+			body {
+				font-family: Monospace;
+				background-color: #101010;
+				color: #fff;
+				margin: 0px;
+				overflow: hidden;
+			}
+			a {
+				color: #f00;
+			}
+		</style>
+	</head>
+	<body>
+
+		<script src="../build/three.min.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/loaders/OBJLoader.js"></script>
+
+		<script>
+
+			if ( WEBVR.isLatestAvailable() === false ) {
+
+				document.body.appendChild( WEBVR.getMessage() );
+
+			}
+
+			//
+
+			var container;
+			var camera, scene, raycaster, renderer;
+			var effect, controls;
+
+			var box;
+
+			var INTERSECTED;
+			var crosshair;
+
+			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 - interactive cubes';
+				container.appendChild( info );
+
+				scene = new THREE.Scene();
+
+				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 1000 );
+				scene.add( camera );
+
+				crosshair = new THREE.Mesh(
+					new THREE.RingGeometry( 0.02, 0.04, 32 ),
+					new THREE.MeshBasicMaterial( {
+						color: 0xffffff,
+						opacity: 0.5,
+						transparent: true
+					} )
+				);
+				crosshair.position.z = - 2;
+				camera.add( crosshair );
+
+				box = new THREE.Mesh(
+					new THREE.BoxGeometry( 6, 6, 6, 10, 10, 10 ),
+					new THREE.MeshBasicMaterial( { color: 0x202020, wireframe: true } )
+				);
+				box.position.y = 3;
+				scene.add( box );
+
+				scene.add( new THREE.HemisphereLight( 0x404020, 0x202040, 0.5 ) );
+
+				var light = new THREE.DirectionalLight( 0xffffff );
+				light.position.set( 1, 1, 1 ).normalize();
+				scene.add(light);
+
+				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() * 4 - 2;
+					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;
+
+					box.add( object );
+
+				}
+
+				var material = new THREE.MeshStandardMaterial();
+
+				var path = 'models/obj/cerberus/';
+				var loader = new THREE.OBJLoader();
+				loader.load(path + 'Cerberus.obj', function (group) {
+
+				    // var material = new THREE.MeshBasicMaterial( { wireframe: true } );
+
+				    var loader = new THREE.TextureLoader();
+
+				    material.roughness = 1;
+				    material.metalness = 1;
+
+				    material.map = loader.load(path + 'Cerberus_A.jpg');
+				    material.roughnessMap = loader.load(path + 'Cerberus_R.jpg');
+				    material.metalnessMap = loader.load(path + 'Cerberus_M.jpg');
+				    material.normalMap = loader.load(path + 'Cerberus_N.jpg');
+
+				    material.map.wrapS = THREE.RepeatWrapping;
+				    material.roughnessMap.wrapS = THREE.RepeatWrapping;
+				    material.metalnessMap.wrapS = THREE.RepeatWrapping;
+				    material.normalMap.wrapS = THREE.RepeatWrapping;
+
+				    group.traverse(function (child) {
+
+				        if (child instanceof THREE.Mesh) {
+
+				            child.material = material;
+
+				        }
+
+				    });
+
+				    group.position.y = -2;
+				    group.rotation.y = -Math.PI / 2;
+				    box.add(group);
+
+				});
+
+				var loader = new THREE.CubeTextureLoader();
+				loader.setPath('textures/cube/pisa/');
+				material.envMap = loader.load([
+					"px.png", "nx.png",
+					"py.png", "ny.png",
+					"pz.png", "nz.png"
+				]);
+                
+
+				raycaster = new THREE.Raycaster();
+
+				renderer = new THREE.WebGLRenderer( { antialias: true } );
+				renderer.setClearColor( 0x101010 );
+				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;
+				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 animate() {
+
+				requestAnimationFrame( animate );
+				render();
+
+			}
+
+			function render() {
+
+				// find intersections
+
+				raycaster.setFromCamera( { x: 0, y: 0 }, camera );
+
+				var intersects = raycaster.intersectObjects( box.children );
+
+				if ( intersects.length > 0 ) {
+
+					if ( INTERSECTED != intersects[ 0 ].object ) {
+
+						if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
+
+						INTERSECTED = intersects[ 0 ].object;
+						INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
+						INTERSECTED.material.emissive.setHex( 0xff0000 );
+
+					}
+
+				} else {
+
+					if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
+
+					INTERSECTED = null;
+
+				}
+
+				controls.update();
+
+
+				for ( var i = 0; i < box.children.length; i ++ ) {
+
+				    var cube = box.children[i];
+
+				    if (cube.geometry instanceof THREE.BoxGeometry === false) continue;
+
+					// cube.position.add( cube.userData.velocity );
+
+					if ( cube.position.x < - 3 || cube.position.x > 3 ) {
+
+						cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
+						cube.userData.velocity.x = - cube.userData.velocity.x;
+
+					}
+
+					if ( cube.position.y < - 3 || cube.position.y > 3 ) {
+
+						cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
+						cube.userData.velocity.y = - cube.userData.velocity.y;
+
+					}
+
+					if ( cube.position.z < - 3 || cube.position.z > 3 ) {
+
+						cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
+						cube.userData.velocity.z = - cube.userData.velocity.z;
+
+					}
+
+					cube.rotation.x += 0.01;
+
+				}
+
+				effect.render( scene, camera );
+
+			}
+
+		</script>
+	</body>
+</html>