Browse Source

- Added VR example

Mr.doob 15 years ago
parent
commit
c2916dbd07

+ 1 - 0
README.md

@@ -15,6 +15,7 @@ Although this allows 3D for iPhoneOS and Android platforms the performance on th
 
 ### Examples ###
 
+[![geometry/vr](http://github.com/mrdoob/three.js/raw/master/assets/examples/04_vr.png)](http://mrdoob.com/projects/three.js/examples/geometry/vr.html)
 [![geometry/cube](http://github.com/mrdoob/three.js/raw/master/assets/examples/03_cube.png)](http://mrdoob.com/projects/three.js/examples/geometry/cube.html)
 [![particles/random](http://github.com/mrdoob/three.js/raw/master/assets/examples/02_random.png)](http://mrdoob.com/projects/three.js/examples/particles/random.html)
 [![particles/wave](http://github.com/mrdoob/three.js/raw/master/assets/examples/01_waves.png)](http://mrdoob.com/projects/three.js/examples/particles/waves.html)

BIN
assets/examples/04_vr.png


BIN
examples/geometry/textures/skymap_back1024.jpg


BIN
examples/geometry/textures/skymap_bottom1024.jpg


BIN
examples/geometry/textures/skymap_front1024.jpg


BIN
examples/geometry/textures/skymap_left1024.jpg


BIN
examples/geometry/textures/skymap_right1024.jpg


BIN
examples/geometry/textures/skymap_top1024.jpg


+ 210 - 0
examples/geometry/vr.html

@@ -0,0 +1,210 @@
+<!DOCTYPE HTML>
+<html lang="en">
+	<head>
+		<title>three.js - vr</title>
+		<meta charset="utf-8">
+		<style type="text/css">
+			body {
+				background-color: #ffffff;
+				margin: 0px;
+				overflow: hidden;
+			}
+
+			#info {
+				position: absolute;
+				top: 0px; width: 100%;
+				color: #ffffff;
+				padding: 5px;
+				font-family:Monospace;
+				font-size:13px;
+				text-align:center;
+			}
+
+			a {
+				color: #ffffff;
+			}
+		</style>
+	</head>
+	<body>
+
+		<div id="container"></div> 
+		<div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - vr demo. skybox by <a href="http://www.zfight.com/" target="_blank">Jochum Skoglund</a></div>
+
+		<script type="text/javascript" src="../../build/three.js"></script>
+		<script type="text/javascript" src="primitives/Plane.js"></script>
+
+		<script type="text/javascript">
+
+			var SCREEN_WIDTH = window.innerWidth, SCREEN_HEIGHT = window.innerHeight;
+
+			var camera, scene, renderer;
+
+			var texture_placeholder, isUserInteracting = false,
+			onMouseDownMouseX = 0, onMouseDownMouseY = 0,
+			lon = 90, onMouseDownLon = 0,
+			lat = 0, onMouseDownLat = 0,
+			phi = 0, theta = 0;
+
+			init();
+
+			function init() {
+
+				var container, mesh;
+
+				container = document.getElementById('container');
+
+				camera = new THREE.Camera();
+				camera.focus = 300;
+
+				scene = new THREE.Scene();
+
+				texture_placeholder = document.createElement( 'canvas' );
+				texture_placeholder.width = 128;
+				texture_placeholder.height = 128;
+
+				var context = texture_placeholder.getContext( '2d' );
+				context.fillStyle = 'rgba( 200, 200, 200, 1 )';
+				context.fillRect( 0, 0, texture_placeholder.width, texture_placeholder.height );
+
+				mesh = new THREE.Mesh( new Plane( 1000, 1000, 5, 5 ), loadTexture( 'textures/skymap_front1024.jpg' ) );
+				mesh.position.z = -500;
+				scene.add( mesh );
+
+				mesh = new THREE.Mesh( new Plane( 1000, 1000, 5, 5 ), loadTexture( 'textures/skymap_back1024.jpg' ) );
+				mesh.position.z = 500;
+				mesh.rotation.y = 180 * Math.PI / 180;
+				scene.add( mesh );
+
+				mesh = new THREE.Mesh( new Plane( 1000, 1000, 5, 5 ), loadTexture( 'textures/skymap_left1024.jpg' ) );
+				mesh.position.x = -500;
+				mesh.rotation.y = 90 * Math.PI / 180;
+				scene.add( mesh );
+
+				mesh = new THREE.Mesh( new Plane( 1000, 1000, 5, 5 ), loadTexture( 'textures/skymap_right1024.jpg' ) );
+				mesh.position.x = 500;
+				mesh.rotation.y = -90 * Math.PI / 180;
+				scene.add( mesh );
+
+				mesh = new THREE.Mesh( new Plane( 1000, 1000, 5, 5 ), loadTexture( 'textures/skymap_top1024.jpg' ) );
+				mesh.position.y = 500;
+				mesh.rotation.x = 90 * Math.PI / 180;
+				scene.add( mesh );
+
+				mesh = new THREE.Mesh( new Plane( 1000, 1000, 5, 5 ), loadTexture( 'textures/skymap_bottom1024.jpg' ) );
+				mesh.position.y = -500;
+				mesh.rotation.x = -90 * Math.PI / 180;
+				scene.add( mesh );
+
+
+				renderer = new THREE.CanvasRenderer();
+				renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
+
+				container.appendChild( renderer.domElement );
+
+				document.addEventListener( 'mousedown', onDocumentMouseDown, false );
+				document.addEventListener( 'mousemove', onDocumentMouseMove, false );
+				document.addEventListener( 'mouseup', onDocumentMouseUp, false );
+
+				document.addEventListener('touchstart', onDocumentTouchStart, false);
+				document.addEventListener('touchmove', onDocumentTouchMove, false);
+
+			}
+
+			function loadTexture( path ) {
+
+				var material = new THREE.TextureUVMappingMaterial( texture_placeholder );
+
+				var texture = new Image();
+
+				texture.onload = function () {
+
+					material.bitmap = this;
+					render();
+
+				};
+
+				texture.src = path;
+
+				// return [ material, new THREE.ColorStrokeMaterial(1, Math.random() * 0xffffff) ];
+				return material;
+			}
+
+			function onDocumentMouseDown( event ) {
+
+				event.preventDefault();
+
+				isUserInteracting = true;
+
+				onPointerDownPointerX = event.clientX;
+				onPointerDownPointerY = event.clientY;
+
+				onPointerDownLon = lon;
+				onPointerDownLat = lat;
+
+			}
+
+			function onDocumentMouseMove( event ) {
+
+				if (isUserInteracting) {
+
+					lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
+					lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
+
+					render();
+
+				}
+			}
+
+			function onDocumentMouseUp( event ) {
+
+				isUserInteracting = false;
+
+			}
+
+
+			function onDocumentTouchStart( event ) {
+
+				if(event.touches.length == 1) {
+
+					event.preventDefault();
+
+					onPointerDownPointerX = event.touches[0].pageX;
+					onPointerDownPointerY = event.touches[0].pageY;
+
+					onPointerDownLon = lon;
+					onPointerDownLat = lat;
+				}
+			}
+
+			function onDocumentTouchMove( event ) {
+
+				if(event.touches.length == 1) {
+
+					event.preventDefault();
+
+					lon = ( onPointerDownPointerX - event.touches[0].pageX ) * 0.1 + onPointerDownLon;
+					lat = ( event.touches[0].pageY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
+
+					render();
+
+				}
+			}
+
+			function render() {
+
+				lat = Math.max( - 85, Math.min( 85, lat ) );
+				phi = ( 90 - lat ) * Math.PI / 180;
+				theta = ( lon + 180 ) * Math.PI / 180;
+
+				camera.target.position.x = 500 * Math.sin(phi) * Math.cos(theta);
+				camera.target.position.y = 500 * Math.cos(phi);
+				camera.target.position.z = 500 * Math.sin(phi) * Math.sin(theta);
+
+				renderer.render(scene, camera);
+
+			}
+
+		</script>
+
+	</body>
+</html>