123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webvr - gear vr</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
- <!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-04-03 -->
- <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-04-03" content="AqIrNtaoJSmDZatgEEW2ehuK9ITIiFk3gZ3/gC6Vp0gF85NDYl7IzVPj4Bh7O6SIcr3ICOm6kocYSaS+BPkazgYAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUyMjc2MzAwMn0=">
- <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/loaders/MTLLoader.js"></script>
- <script src="js/loaders/OBJLoader.js"></script>
- <script src="js/vr/GearVRController.js"></script>
- <script src="js/vr/WebVR.js"></script>
- <script>
- var clock = new THREE.Clock();
- var container;
- var camera, scene, renderer;
- var controller;
- var room;
- 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" rel="noopener">three.js</a> webgl - gear vr';
- container.appendChild( info );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x505050 );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
- 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 );
- var geometry = new THREE.IcosahedronGeometry( 0.08, 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.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;
- room.add( object );
- }
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.vr.enabled = true;
- container.appendChild( renderer.domElement );
- document.body.appendChild( WEBVR.createButton( renderer ) );
- controller = new THREE.GearVRController();
- controller.position.set( 0.25, 0.75, 0 );
- scene.add( controller );
- var skyBox = {
- front: 'textures/cube/Bridge2/posz.jpg',
- back: 'textures/cube/Bridge2/negz.jpg',
- left: 'textures/cube/Bridge2/posx.jpg',
- right: 'textures/cube/Bridge2/negx.jpg',
- up: 'textures/cube/Bridge2/posy.jpg',
- down: 'textures/cube/Bridge2/negy.jpg'
- };
- if ( 'SamsungChangeSky' in window ) {
- window.SamsungChangeSky( skyBox );
- }
- var MTL = new THREE.MTLLoader();
- MTL.setPath( 'models/obj/gear_vr_controller/' );
- MTL.load( 'gear_vr_controller.mtl', function ( materials ) {
- materials.preload();
- var OBJ = new THREE.OBJLoader();
- OBJ.setMaterials( materials );
- OBJ.setPath( 'models/obj/gear_vr_controller/' );
- OBJ.load( 'gear_vr_controller.obj', function ( obj ) {
- obj.translateZ( - 0.03 );
- controller.add( obj );
- } );
- } );
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- renderer.animate( render );
- }
- function render() {
- var delta = clock.getDelta() * 60;
- controller.update();
- if ( controller.getTouchpadState() === true ) {
- var cube = room.children[ 0 ];
- room.remove( cube );
- cube.position.copy( controller.position ).sub( room.position );
- cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
- cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
- cube.userData.velocity.z = ( Math.random() * 0.01 - 0.1 );
- cube.userData.velocity.applyQuaternion( controller.quaternion );
- room.add( cube );
- }
- // keep cubes inside room
- var range = 3 - 0.08;
- for ( var i = 0; i < room.children.length; i ++ ) {
- var cube = room.children[ i ];
- cube.position.add( cube.userData.velocity );
- if ( cube.position.x < - range || cube.position.x > range ) {
- cube.position.x = THREE.Math.clamp( cube.position.x, - range, range );
- cube.userData.velocity.x = - cube.userData.velocity.x;
- }
- if ( cube.position.y < - range || cube.position.y > range ) {
- cube.position.y = Math.max( cube.position.y, - range );
- cube.userData.velocity.x *= 0.9;
- cube.userData.velocity.y = - cube.userData.velocity.y * 0.8;
- cube.userData.velocity.z *= 0.9;
- }
- if ( cube.position.z < - range || cube.position.z > range ) {
- cube.position.z = THREE.Math.clamp( cube.position.z, - range, range );
- cube.userData.velocity.z = - cube.userData.velocity.z;
- }
- cube.rotation.x += cube.userData.velocity.x * 2 * delta;
- cube.rotation.y += cube.userData.velocity.y * 2 * delta;
- cube.rotation.z += cube.userData.velocity.z * 2 * delta;
- cube.userData.velocity.y -= 0.00098;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|