123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webvr - daydream</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-06-19 -->
- <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-06-19" content="Alxt96tYGgIr9l6EXU0eeI360zcmzOY6Kuo3kcTfBGIRDOQbgFIZKRQ1joExQ74WZr1einsE+cUMHgSclNHCQQ4AAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUyOTM5NzgyOH0=">
- <!-- Origin Trial Token, feature = WebXR Device API, origin = https://threejs.org, expires = 2018-06-15 -->
- <meta http-equiv="origin-trial" data-feature="WebXR Device API" data-expires="2018-06-15" content="AtJH9g6nn0B87bnjJt+9m1joZXEYDmLSlRvtMr5qJD52hMcm3S86S7jg5I7y2I5cgQglE0rzsXzti5DECQLb8QkAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZSIsImV4cGlyeSI6MTUyOTA4NDY2OH0=">
- <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-06-15 -->
- <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-06-15" content="Aihhr0yXkVlCKF0DIpTbH8WX7ZmEexUhI/95+t8aoLfvBkePMiZ/iOoDPU3xefyfuczkDahH1L6eiPvRsuzITAAAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTI5MDg0NjY4fQ==">
- <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/vr/DaydreamController.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;
- var radius = 0.08;
- var normal = new THREE.Vector3();
- var relativeVelocity = new THREE.Vector3();
- 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="https://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - daydream';
- 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.BoxBufferGeometry( 6, 6, 6, 8, 8, 8 ),
- new THREE.MeshBasicMaterial( { color: 0x808080, 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.IcosahedronBufferGeometry( radius, 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.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.DaydreamController();
- controller.position.set( 0.3, 0.75, 0 );
- scene.add( controller );
- //
- var controllerHelper = new THREE.Line( new THREE.BufferGeometry(), new THREE.LineBasicMaterial( { linewidth: 2 } ) );
- controllerHelper.geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 10 ], 3 ) );
- controller.add( controllerHelper );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- renderer.setAnimationLoop( 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.02 - 0.1 );
- cube.userData.velocity.applyQuaternion( controller.quaternion );
- room.add( cube );
- }
- // keep cubes inside room
- var range = 3 - radius;
- 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;
- }
- for ( var j = i + 1; j < room.children.length; j ++ ) {
- var cube2 = room.children[ j ];
- normal.copy( cube.position ).sub( cube2.position );
- if ( normal.length() < 2 * radius ) {
- normal.multiplyScalar( 0.5 * normal.length() - radius );
- cube.position.sub( normal );
- cube2.position.add( normal );
- normal.normalize();
- relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
- var dot = relativeVelocity.dot( normal );
- normal = normal.multiplyScalar( dot );
- cube.userData.velocity.sub( normal );
- cube2.userData.velocity.add( normal );
- }
- }
- cube.userData.velocity.y -= 0.00098;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|