webvr_vive_camerarig.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - htc vive - camera rig</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/vr/ViveController.js"></script>
  23. <script src="js/vr/WebVR.js"></script>
  24. <script src="js/loaders/OBJLoader.js"></script>
  25. <script>
  26. WEBVR.checkAvailability().catch( function( message ) {
  27. document.body.appendChild( WEBVR.getMessageContainer( message ) );
  28. } );
  29. //
  30. var clock = new THREE.Clock();
  31. var container;
  32. var cameraRig, camera, scene, renderer;
  33. var controller1, controller2;
  34. var scale = 1;
  35. var dragging = {
  36. left: false,
  37. right: false
  38. };
  39. var dragStartPointLeft = new THREE.Vector3();
  40. var dragStartPointRight = new THREE.Vector3();
  41. var room;
  42. init();
  43. animate();
  44. function init() {
  45. container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. var info = document.createElement( 'div' );
  48. info.style.position = 'absolute';
  49. info.style.top = '10px';
  50. info.style.width = '100%';
  51. info.style.textAlign = 'center';
  52. info.innerHTML = '<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - htc vive';
  53. container.appendChild( info );
  54. scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0x505050 );
  56. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 1000 );
  57. cameraRig = new THREE.Group();
  58. cameraRig.add( camera );
  59. scene.add( cameraRig );
  60. room = new THREE.Mesh(
  61. new THREE.BoxGeometry( 6, 6, 6, 8, 8, 8 ),
  62. new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
  63. );
  64. room.position.y = 3;
  65. scene.add( room );
  66. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  67. var light = new THREE.DirectionalLight( 0xffffff );
  68. light.position.set( 1, 1, 1 ).normalize();
  69. scene.add( light );
  70. var geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
  71. for ( var i = 0; i < 200; i ++ ) {
  72. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  73. object.position.x = Math.random() * 4 - 2;
  74. object.position.y = Math.random() * 4 - 2;
  75. object.position.z = Math.random() * 4 - 2;
  76. object.rotation.x = Math.random() * 2 * Math.PI;
  77. object.rotation.y = Math.random() * 2 * Math.PI;
  78. object.rotation.z = Math.random() * 2 * Math.PI;
  79. object.scale.x = Math.random() + 0.5;
  80. object.scale.y = Math.random() + 0.5;
  81. object.scale.z = Math.random() + 0.5;
  82. object.userData.velocity = new THREE.Vector3();
  83. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  84. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  85. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  86. room.add( object );
  87. }
  88. var material = new THREE.MeshStandardMaterial();
  89. var loader = new THREE.OBJLoader();
  90. loader.setPath( 'models/obj/cerberus/' );
  91. loader.load( 'Cerberus.obj', function ( group ) {
  92. // var material = new THREE.MeshBasicMaterial( { wireframe: true } );
  93. var loader = new THREE.TextureLoader();
  94. loader.setPath( 'models/obj/cerberus/' );
  95. material.roughness = 1;
  96. material.metalness = 1;
  97. material.map = loader.load( 'Cerberus_A.jpg' );
  98. material.roughnessMap = loader.load( 'Cerberus_R.jpg' );
  99. material.metalnessMap = loader.load( 'Cerberus_M.jpg' );
  100. material.normalMap = loader.load( 'Cerberus_N.jpg' );
  101. material.map.wrapS = THREE.RepeatWrapping;
  102. material.roughnessMap.wrapS = THREE.RepeatWrapping;
  103. material.metalnessMap.wrapS = THREE.RepeatWrapping;
  104. material.normalMap.wrapS = THREE.RepeatWrapping;
  105. group.traverse( function ( child ) {
  106. if ( child instanceof THREE.Mesh ) {
  107. child.material = material;
  108. }
  109. } );
  110. group.position.y = - 2;
  111. group.rotation.y = - Math.PI / 2;
  112. room.add( group );
  113. } );
  114. var loader = new THREE.CubeTextureLoader();
  115. loader.setPath( 'textures/cube/pisa/' );
  116. material.envMap = loader.load( [
  117. "px.png", "nx.png",
  118. "py.png", "ny.png",
  119. "pz.png", "nz.png"
  120. ] );
  121. //
  122. renderer = new THREE.WebGLRenderer( { antialias: true } );
  123. renderer.setPixelRatio( window.devicePixelRatio );
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. container.appendChild( renderer.domElement );
  126. renderer.vr.enabled = true;
  127. renderer.vr.standing = true;
  128. // controllers
  129. controller1 = new THREE.ViveController( 0 );
  130. controller1.standingMatrix = renderer.vr.getStandingMatrix();
  131. controller1.addEventListener( 'triggerdown', onTriggerDown );
  132. controller1.addEventListener( 'triggerup', onTriggerUp );
  133. cameraRig.add( controller1 );
  134. controller2 = new THREE.ViveController( 1 );
  135. controller2.standingMatrix = renderer.vr.getStandingMatrix();
  136. controller2.addEventListener( 'triggerdown', onTriggerDown );
  137. controller2.addEventListener( 'triggerup', onTriggerUp );
  138. cameraRig.add( controller2 );
  139. var loader = new THREE.OBJLoader();
  140. loader.setPath( 'models/obj/vive-controller/' );
  141. loader.load( 'vr_controller_vive_1_5.obj', function ( object ) {
  142. var loader = new THREE.TextureLoader();
  143. loader.setPath( 'models/obj/vive-controller/' );
  144. var controller = object.children[ 0 ];
  145. controller.material.map = loader.load( 'onepointfive_texture.png' );
  146. controller.material.specularMap = loader.load( 'onepointfive_spec.png' );
  147. controller1.add( object.clone() );
  148. controller2.add( object.clone() );
  149. } );
  150. WEBVR.getVRDisplay( function ( display ) {
  151. renderer.vr.setDevice( display );
  152. document.body.appendChild( WEBVR.getButton( display, renderer.domElement ) );
  153. } );
  154. //
  155. window.addEventListener( 'resize', onWindowResize, false );
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. camera.updateProjectionMatrix();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. }
  162. //
  163. function animate() {
  164. renderer.animate( render );
  165. }
  166. function render() {
  167. var delta = clock.getDelta() * 60;
  168. controller1.update();
  169. controller2.update();
  170. scaleUpdate();
  171. for ( var i = 0; i < room.children.length; i ++ ) {
  172. var cube = room.children[ i ];
  173. if ( cube.geometry instanceof THREE.BoxGeometry === false ) continue;
  174. // cube.position.add( cube.userData.velocity );
  175. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  176. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  177. cube.userData.velocity.x = - cube.userData.velocity.x;
  178. }
  179. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  180. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  181. cube.userData.velocity.y = - cube.userData.velocity.y;
  182. }
  183. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  184. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  185. cube.userData.velocity.z = - cube.userData.velocity.z;
  186. }
  187. cube.rotation.x += 0.01 * delta;
  188. }
  189. renderer.render( scene, camera );
  190. }
  191. var previousControllersSegment = new THREE.Line3();
  192. var currentControllersSegment = new THREE.Line3();
  193. var deltaPreviousSegment = new THREE.Vector3();
  194. var deltaCurrentSegment = new THREE.Vector3();
  195. var currentWorldPositionLeft = new THREE.Vector3();
  196. var currentWorldPositionRight = new THREE.Vector3();
  197. function scaleUpdate () {
  198. if ( !dragging.left || !dragging.right ) {
  199. return false;
  200. }
  201. currentWorldPositionLeft.setFromMatrixPosition( controller1.matrixWorld );
  202. currentWorldPositionRight.setFromMatrixPosition( controller2.matrixWorld );
  203. previousControllersSegment.set( dragStartPointLeft, dragStartPointRight );
  204. currentControllersSegment.set( currentWorldPositionLeft, currentWorldPositionRight );
  205. var dist = ( previousControllersSegment.distance() - currentControllersSegment.distance() );
  206. scale += dist;
  207. cameraRig.scale.set( scale, scale, scale );
  208. dragStartPointLeft.copy( currentWorldPositionLeft );
  209. dragStartPointRight.copy( currentWorldPositionRight );
  210. }
  211. function onTriggerDown (event) {
  212. var target = event.target;
  213. var left = target === controller1;
  214. if (left) {
  215. dragging.left = true;
  216. } else {
  217. dragging.right = true;
  218. }
  219. dragStartPointLeft.setFromMatrixPosition(controller1.matrixWorld);
  220. dragStartPointRight.setFromMatrixPosition(controller2.matrixWorld);
  221. }
  222. function onTriggerUp (event) {
  223. var left = event.target === controller1;
  224. if (left) {
  225. dragging.left = false;
  226. } else {
  227. dragging.right = false;
  228. }
  229. }
  230. </script>
  231. </body>
  232. </html>