webvr_oculusgo.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - oculus go</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-07-25 -->
  8. <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-07-25" content="AtW06hJFoVWUJTZf5gqymMIlrR60JJi5MsSe44qsHjrCmzDUNmvaTtAVg+K9O9jFpjJtn/W9jvG//hHHaq5HcQoAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUzMjUyNjI5OH0=">
  9. <!-- Origin Trial Token, feature = WebXR Device API, origin = https://threejs.org, expires = 2018-07-21 -->
  10. <meta http-equiv="origin-trial" data-feature="WebXR Device API" data-expires="2018-07-21" content="Anlf1R/bCOUxOEgGI/9TWuzHHNxBMfZSTUMDCN7cLwDj2gpLwgA1K0DPwOzO/O0Jwaur5bsHo7k9KXx+6g+82wIAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZSIsImV4cGlyeSI6MTUzMjE2NjIyNX0=">
  11. <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-07-21 -->
  12. <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-07-21" content="ArDsXbwATKHPmvQiPlEIWNCt4DlEjB7bLj9vOgoNmL8r38U+wQNYZyUvjQIqqzgACciUUuAnxluTIL7nNkI89gcAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTMyMTY2MjI1fQ==">
  13. <style>
  14. body {
  15. font-family: Monospace;
  16. background-color: #101010;
  17. color: #fff;
  18. margin: 0px;
  19. overflow: hidden;
  20. }
  21. a {
  22. color: #f00;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <script src="../build/three.js"></script>
  28. <script src="js/loaders/MTLLoader.js"></script>
  29. <script src="js/loaders/OBJLoader.js"></script>
  30. <script src="js/vr/GearVRController.js"></script>
  31. <script src="js/vr/WebVR.js"></script>
  32. <script>
  33. var container;
  34. var camera, scene, renderer;
  35. var controller;
  36. var room;
  37. var radius = 0.08;
  38. var normal = new THREE.Vector3();
  39. var relativeVelocity = new THREE.Vector3();
  40. init();
  41. animate();
  42. function init() {
  43. container = document.createElement( 'div' );
  44. document.body.appendChild( container );
  45. var info = document.createElement( 'div' );
  46. info.style.position = 'absolute';
  47. info.style.top = '10px';
  48. info.style.width = '100%';
  49. info.style.textAlign = 'center';
  50. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - oculus go';
  51. container.appendChild( info );
  52. scene = new THREE.Scene();
  53. scene.background = new THREE.Color( 0x505050 );
  54. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  55. room = new THREE.Mesh(
  56. new THREE.BoxBufferGeometry( 6, 6, 6, 8, 8, 8 ),
  57. new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
  58. );
  59. room.position.y = 3;
  60. scene.add( room );
  61. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  62. var light = new THREE.DirectionalLight( 0xffffff );
  63. light.position.set( 1, 1, 1 ).normalize();
  64. scene.add( light );
  65. var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
  66. for ( var i = 0; i < 200; i ++ ) {
  67. var object = new THREE.Mesh(
  68. geometry,
  69. new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } )
  70. );
  71. object.position.x = Math.random() * 4 - 2;
  72. object.position.y = Math.random() * 4 - 2;
  73. object.position.z = Math.random() * 4 - 2;
  74. object.userData.velocity = new THREE.Vector3();
  75. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  76. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  77. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  78. room.add( object );
  79. }
  80. renderer = new THREE.WebGLRenderer( { antialias: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.vr.enabled = true;
  84. container.appendChild( renderer.domElement );
  85. document.body.appendChild( WEBVR.createButton( renderer ) );
  86. controller = new THREE.GearVRController();
  87. controller.position.set( 0.35, 1.1, -0.3 );
  88. scene.add( controller );
  89. var skyBox = {
  90. front: 'textures/cube/Bridge2/posz.jpg',
  91. back: 'textures/cube/Bridge2/negz.jpg',
  92. left: 'textures/cube/Bridge2/posx.jpg',
  93. right: 'textures/cube/Bridge2/negx.jpg',
  94. up: 'textures/cube/Bridge2/posy.jpg',
  95. down: 'textures/cube/Bridge2/negy.jpg'
  96. };
  97. if ( 'SamsungChangeSky' in window ) {
  98. window.SamsungChangeSky( skyBox );
  99. }
  100. var MTL = new THREE.MTLLoader();
  101. MTL.setPath( 'models/obj/oculus_go_controller/' );
  102. MTL.load( 'oculus_go_controller.mtl', function ( materials ) {
  103. materials.preload();
  104. var OBJ = new THREE.OBJLoader();
  105. OBJ.setMaterials( materials );
  106. OBJ.setPath( 'models/obj/oculus_go_controller/' );
  107. OBJ.load( 'oculus_go_controller.obj', function ( obj ) {
  108. obj.translateZ( - 0.03 );
  109. controller.add( obj );
  110. } );
  111. } );
  112. window.addEventListener( 'resize', onWindowResize, false );
  113. }
  114. function onWindowResize() {
  115. camera.aspect = window.innerWidth / window.innerHeight;
  116. camera.updateProjectionMatrix();
  117. renderer.setSize( window.innerWidth, window.innerHeight );
  118. }
  119. function animate() {
  120. renderer.setAnimationLoop( render );
  121. }
  122. function render() {
  123. controller.update();
  124. if ( controller.getTouchpadState() === true ) {
  125. var cube = room.children[ 0 ];
  126. room.remove( cube );
  127. cube.position.copy( controller.position ).sub( room.position );
  128. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
  129. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
  130. cube.userData.velocity.z = ( Math.random() * 0.02 - 0.1 );
  131. cube.userData.velocity.applyQuaternion( controller.quaternion );
  132. room.add( cube );
  133. }
  134. // keep cubes inside room
  135. var range = 3 - radius;
  136. for ( var i = 0; i < room.children.length; i ++ ) {
  137. var cube = room.children[ i ];
  138. cube.position.add( cube.userData.velocity );
  139. if ( cube.position.x < - range || cube.position.x > range ) {
  140. cube.position.x = THREE.Math.clamp( cube.position.x, - range, range );
  141. cube.userData.velocity.x = - cube.userData.velocity.x;
  142. }
  143. if ( cube.position.y < - range || cube.position.y > range ) {
  144. cube.position.y = Math.max( cube.position.y, - range );
  145. cube.userData.velocity.x *= 0.9;
  146. cube.userData.velocity.y = - cube.userData.velocity.y * 0.8;
  147. cube.userData.velocity.z *= 0.9;
  148. }
  149. if ( cube.position.z < - range || cube.position.z > range ) {
  150. cube.position.z = THREE.Math.clamp( cube.position.z, - range, range );
  151. cube.userData.velocity.z = - cube.userData.velocity.z;
  152. }
  153. for ( var j = i + 1; j < room.children.length; j ++ ) {
  154. var cube2 = room.children[ j ];
  155. normal.copy( cube.position ).sub( cube2.position );
  156. if ( normal.length() < 2 * radius ) {
  157. normal.multiplyScalar( 0.5 * normal.length() - radius );
  158. cube.position.sub( normal );
  159. cube2.position.add( normal );
  160. normal.normalize();
  161. relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
  162. var dot = relativeVelocity.dot( normal );
  163. normal = normal.multiplyScalar( dot );
  164. cube.userData.velocity.sub( normal );
  165. cube2.userData.velocity.add( normal );
  166. }
  167. }
  168. cube.userData.velocity.y -= 0.00098;
  169. }
  170. renderer.render( scene, camera );
  171. }
  172. </script>
  173. </body>
  174. </html>