webvr_vive.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - htc vive</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #101010;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. a {
  16. color: #f00;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script src="../build/three.js"></script>
  22. <script src="js/WebVR.js"></script>
  23. <script src="js/effects/VREffect.js"></script>
  24. <script src="js/controls/VRControls.js"></script>
  25. <script src="js/ViveController.js"></script>
  26. <script src="js/loaders/OBJLoader.js"></script>
  27. <script>
  28. if ( WEBVR.isLatestAvailable() === false ) {
  29. document.body.appendChild( WEBVR.getMessage() );
  30. }
  31. //
  32. var clock = new THREE.Clock();
  33. var container;
  34. var camera, scene, renderer;
  35. var effect, controls;
  36. var controller1, controller2;
  37. var room;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. var info = document.createElement( 'div' );
  44. info.style.position = 'absolute';
  45. info.style.top = '10px';
  46. info.style.width = '100%';
  47. info.style.textAlign = 'center';
  48. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - htc vive';
  49. container.appendChild( info );
  50. scene = new THREE.Scene();
  51. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  52. scene.add( camera );
  53. room = new THREE.Mesh(
  54. new THREE.BoxGeometry( 6, 6, 6, 8, 8, 8 ),
  55. new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
  56. );
  57. room.position.y = 3;
  58. scene.add( room );
  59. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  60. var light = new THREE.DirectionalLight( 0xffffff );
  61. light.position.set( 1, 1, 1 ).normalize();
  62. scene.add( light );
  63. var geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
  64. for ( var i = 0; i < 200; i ++ ) {
  65. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  66. object.position.x = Math.random() * 4 - 2;
  67. object.position.y = Math.random() * 4 - 2;
  68. object.position.z = Math.random() * 4 - 2;
  69. object.rotation.x = Math.random() * 2 * Math.PI;
  70. object.rotation.y = Math.random() * 2 * Math.PI;
  71. object.rotation.z = Math.random() * 2 * Math.PI;
  72. object.scale.x = Math.random() + 0.5;
  73. object.scale.y = Math.random() + 0.5;
  74. object.scale.z = Math.random() + 0.5;
  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. var material = new THREE.MeshStandardMaterial();
  82. var loader = new THREE.OBJLoader();
  83. loader.setPath( 'models/obj/cerberus/' );
  84. loader.load( 'Cerberus.obj', function ( group ) {
  85. // var material = new THREE.MeshBasicMaterial( { wireframe: true } );
  86. var loader = new THREE.TextureLoader();
  87. loader.setPath( 'models/obj/cerberus/' );
  88. material.roughness = 1;
  89. material.metalness = 1;
  90. material.map = loader.load( 'Cerberus_A.jpg' );
  91. material.roughnessMap = loader.load( 'Cerberus_R.jpg' );
  92. material.metalnessMap = loader.load( 'Cerberus_M.jpg' );
  93. material.normalMap = loader.load( 'Cerberus_N.jpg' );
  94. material.map.wrapS = THREE.RepeatWrapping;
  95. material.roughnessMap.wrapS = THREE.RepeatWrapping;
  96. material.metalnessMap.wrapS = THREE.RepeatWrapping;
  97. material.normalMap.wrapS = THREE.RepeatWrapping;
  98. group.traverse( function ( child ) {
  99. if ( child instanceof THREE.Mesh ) {
  100. child.material = material;
  101. }
  102. } );
  103. group.position.y = - 2;
  104. group.rotation.y = - Math.PI / 2;
  105. room.add( group );
  106. } );
  107. var loader = new THREE.CubeTextureLoader();
  108. loader.setPath( 'textures/cube/pisa/' );
  109. material.envMap = loader.load( [
  110. "px.png", "nx.png",
  111. "py.png", "ny.png",
  112. "pz.png", "nz.png"
  113. ] );
  114. //
  115. renderer = new THREE.WebGLRenderer( { antialias: true } );
  116. renderer.setClearColor( 0x505050 );
  117. renderer.setPixelRatio( window.devicePixelRatio );
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. renderer.sortObjects = false;
  120. container.appendChild( renderer.domElement );
  121. controls = new THREE.VRControls( camera );
  122. controls.standing = true;
  123. // controllers
  124. controller1 = new THREE.ViveController( 0 );
  125. controller1.standingMatrix = controls.getStandingMatrix();
  126. scene.add( controller1 );
  127. controller2 = new THREE.ViveController( 1 );
  128. controller2.standingMatrix = controls.getStandingMatrix();
  129. scene.add( controller2 );
  130. var loader = new THREE.OBJLoader();
  131. loader.setPath( 'models/obj/vive-controller/' );
  132. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  133. var loader = new THREE.TextureLoader();
  134. loader.setPath( 'models/obj/vive-controller/' );
  135. var controller = object.children[ 0 ];
  136. controller.material.map = loader.load( 'onepointfive_texture.png' );
  137. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  138. controller1.add( object.clone() );
  139. controller2.add( object.clone() );
  140. } );
  141. effect = new THREE.VREffect( renderer );
  142. if ( WEBVR.isAvailable() === true ) {
  143. document.body.appendChild( WEBVR.getButton( effect ) );
  144. }
  145. //
  146. window.addEventListener( 'resize', onWindowResize, false );
  147. }
  148. function onWindowResize() {
  149. camera.aspect = window.innerWidth / window.innerHeight;
  150. camera.updateProjectionMatrix();
  151. effect.setSize( window.innerWidth, window.innerHeight );
  152. }
  153. //
  154. function animate() {
  155. effect.requestAnimationFrame( animate );
  156. render();
  157. }
  158. function render() {
  159. var delta = clock.getDelta() * 60;
  160. controls.update();
  161. for ( var i = 0; i < room.children.length; i ++ ) {
  162. var cube = room.children[ i ];
  163. if ( cube.geometry instanceof THREE.BoxGeometry === false ) continue;
  164. // cube.position.add( cube.userData.velocity );
  165. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  166. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  167. cube.userData.velocity.x = - cube.userData.velocity.x;
  168. }
  169. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  170. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  171. cube.userData.velocity.y = - cube.userData.velocity.y;
  172. }
  173. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  174. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  175. cube.userData.velocity.z = - cube.userData.velocity.z;
  176. }
  177. cube.rotation.x += 0.01 * delta;
  178. }
  179. effect.render( scene, camera );
  180. }
  181. </script>
  182. </body>
  183. </html>