webvr_gearvr.html 6.4 KB

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