webvr_oculusgo.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 clock = new THREE.Clock();
  34. var container;
  35. var camera, scene, renderer;
  36. var controller;
  37. var room;
  38. var radius = 0.08;
  39. var normal = new THREE.Vector3();
  40. var relativeVelocity = new THREE.Vector3();
  41. init();
  42. animate();
  43. function init() {
  44. container = document.createElement( 'div' );
  45. document.body.appendChild( container );
  46. var info = document.createElement( 'div' );
  47. info.style.position = 'absolute';
  48. info.style.top = '10px';
  49. info.style.width = '100%';
  50. info.style.textAlign = 'center';
  51. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - oculus go';
  52. container.appendChild( info );
  53. scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0x505050 );
  55. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  56. room = new THREE.Mesh(
  57. new THREE.BoxBufferGeometry( 6, 6, 6, 8, 8, 8 ),
  58. new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
  59. );
  60. room.position.y = 3;
  61. scene.add( room );
  62. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  63. var light = new THREE.DirectionalLight( 0xffffff );
  64. light.position.set( 1, 1, 1 ).normalize();
  65. scene.add( light );
  66. var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
  67. for ( var i = 0; i < 200; i ++ ) {
  68. var object = new THREE.Mesh(
  69. geometry,
  70. new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } )
  71. );
  72. object.position.x = Math.random() * 4 - 2;
  73. object.position.y = Math.random() * 4 - 2;
  74. object.position.z = Math.random() * 4 - 2;
  75. object.userData.velocity = new THREE.Vector3();
  76. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  77. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  78. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  79. room.add( object );
  80. }
  81. renderer = new THREE.WebGLRenderer( { antialias: true } );
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. renderer.vr.enabled = true;
  85. container.appendChild( renderer.domElement );
  86. document.body.appendChild( WEBVR.createButton( renderer ) );
  87. controller = new THREE.GearVRController();
  88. controller.position.set( 0.35, 1.1, -0.3 );
  89. scene.add( controller );
  90. var skyBox = {
  91. front: 'textures/cube/Bridge2/posz.jpg',
  92. back: 'textures/cube/Bridge2/negz.jpg',
  93. left: 'textures/cube/Bridge2/posx.jpg',
  94. right: 'textures/cube/Bridge2/negx.jpg',
  95. up: 'textures/cube/Bridge2/posy.jpg',
  96. down: 'textures/cube/Bridge2/negy.jpg'
  97. };
  98. if ( 'SamsungChangeSky' in window ) {
  99. window.SamsungChangeSky( skyBox );
  100. }
  101. var MTL = new THREE.MTLLoader();
  102. MTL.setPath( 'models/obj/oculus_go_controller/' );
  103. MTL.load( 'oculus_go_controller.mtl', function ( materials ) {
  104. materials.preload();
  105. var OBJ = new THREE.OBJLoader();
  106. OBJ.setMaterials( materials );
  107. OBJ.setPath( 'models/obj/oculus_go_controller/' );
  108. OBJ.load( 'oculus_go_controller.obj', function ( obj ) {
  109. obj.translateZ( - 0.03 );
  110. controller.add( obj );
  111. } );
  112. } );
  113. window.addEventListener( 'resize', onWindowResize, false );
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. function animate() {
  121. renderer.setAnimationLoop( render );
  122. }
  123. function render() {
  124. var delta = clock.getDelta() * 60;
  125. controller.update();
  126. if ( controller.getTouchpadState() === true ) {
  127. var cube = room.children[ 0 ];
  128. room.remove( cube );
  129. cube.position.copy( controller.position ).sub( room.position );
  130. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
  131. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
  132. cube.userData.velocity.z = ( Math.random() * 0.02 - 0.1 );
  133. cube.userData.velocity.applyQuaternion( controller.quaternion );
  134. room.add( cube );
  135. }
  136. // keep cubes inside room
  137. var range = 3 - radius;
  138. for ( var i = 0; i < room.children.length; i ++ ) {
  139. var cube = room.children[ i ];
  140. cube.position.add( cube.userData.velocity );
  141. if ( cube.position.x < - range || cube.position.x > range ) {
  142. cube.position.x = THREE.Math.clamp( cube.position.x, - range, range );
  143. cube.userData.velocity.x = - cube.userData.velocity.x;
  144. }
  145. if ( cube.position.y < - range || cube.position.y > range ) {
  146. cube.position.y = Math.max( cube.position.y, - range );
  147. cube.userData.velocity.x *= 0.9;
  148. cube.userData.velocity.y = - cube.userData.velocity.y * 0.8;
  149. cube.userData.velocity.z *= 0.9;
  150. }
  151. if ( cube.position.z < - range || cube.position.z > range ) {
  152. cube.position.z = THREE.Math.clamp( cube.position.z, - range, range );
  153. cube.userData.velocity.z = - cube.userData.velocity.z;
  154. }
  155. for ( var j = i + 1; j < room.children.length; j ++ ) {
  156. var cube2 = room.children[ j ];
  157. normal.copy( cube.position ).sub( cube2.position );
  158. if ( normal.length() < 2 * radius ) {
  159. normal.multiplyScalar( 0.5 * normal.length() - radius );
  160. cube.position.sub( normal );
  161. cube2.position.add( normal );
  162. normal.normalize();
  163. relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
  164. var dot = relativeVelocity.dot( normal );
  165. normal = normal.multiplyScalar( dot );
  166. cube.userData.velocity.sub( normal );
  167. cube2.userData.velocity.add( normal );
  168. }
  169. }
  170. cube.userData.velocity.y -= 0.00098;
  171. }
  172. renderer.render( scene, camera );
  173. }
  174. </script>
  175. </body>
  176. </html>