webvr_vive.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - cubes</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/loaders/OBJLoader.js"></script>
  26. <script>
  27. if ( WEBVR.isLatestAvailable() === false ) {
  28. document.body.appendChild( WEBVR.getMessage() );
  29. }
  30. //
  31. var container;
  32. var camera, scene, raycaster, renderer;
  33. var effect, controls;
  34. var room;
  35. var INTERSECTED;
  36. var crosshair;
  37. init();
  38. animate();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. var info = document.createElement( 'div' );
  43. info.style.position = 'absolute';
  44. info.style.top = '10px';
  45. info.style.width = '100%';
  46. info.style.textAlign = 'center';
  47. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
  48. container.appendChild( info );
  49. scene = new THREE.Scene();
  50. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  51. scene.add( camera );
  52. crosshair = new THREE.Mesh(
  53. new THREE.RingGeometry( 0.02, 0.04, 32 ),
  54. new THREE.MeshBasicMaterial( {
  55. color: 0xffffff,
  56. opacity: 0.5,
  57. transparent: true
  58. } )
  59. );
  60. crosshair.position.z = - 2;
  61. camera.add( crosshair );
  62. room = new THREE.Mesh(
  63. new THREE.BoxGeometry( 6, 6, 6, 10, 10, 10 ),
  64. new THREE.MeshBasicMaterial( { color: 0x202020, wireframe: true } )
  65. );
  66. room.position.y = 3;
  67. scene.add( room );
  68. scene.add( new THREE.HemisphereLight( 0x404020, 0x202040, 0.5 ) );
  69. var light = new THREE.DirectionalLight( 0xffffff );
  70. light.position.set( 1, 1, 1 ).normalize();
  71. scene.add( light );
  72. var geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
  73. for ( var i = 0; i < 200; i ++ ) {
  74. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  75. object.position.x = Math.random() * 4 - 2;
  76. object.position.y = Math.random() * 4 - 2;
  77. object.position.z = Math.random() * 4 - 2;
  78. object.rotation.x = Math.random() * 2 * Math.PI;
  79. object.rotation.y = Math.random() * 2 * Math.PI;
  80. object.rotation.z = Math.random() * 2 * Math.PI;
  81. object.scale.x = Math.random() + 0.5;
  82. object.scale.y = Math.random() + 0.5;
  83. object.scale.z = Math.random() + 0.5;
  84. object.userData.velocity = new THREE.Vector3();
  85. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  86. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  87. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  88. room.add( object );
  89. }
  90. var material = new THREE.MeshStandardMaterial();
  91. var path = 'models/obj/cerberus/';
  92. var loader = new THREE.OBJLoader();
  93. loader.load( path + 'Cerberus.obj', function ( group ) {
  94. // var material = new THREE.MeshBasicMaterial( { wireframe: true } );
  95. var loader = new THREE.TextureLoader();
  96. material.roughness = 1;
  97. material.metalness = 1;
  98. material.map = loader.load( path + 'Cerberus_A.jpg' );
  99. material.roughnessMap = loader.load( path + 'Cerberus_R.jpg' );
  100. material.metalnessMap = loader.load( path + 'Cerberus_M.jpg' );
  101. material.normalMap = loader.load( path + 'Cerberus_N.jpg' );
  102. material.map.wrapS = THREE.RepeatWrapping;
  103. material.roughnessMap.wrapS = THREE.RepeatWrapping;
  104. material.metalnessMap.wrapS = THREE.RepeatWrapping;
  105. material.normalMap.wrapS = THREE.RepeatWrapping;
  106. group.traverse( function ( child ) {
  107. if ( child instanceof THREE.Mesh ) {
  108. child.material = material;
  109. }
  110. } );
  111. group.position.y = - 2;
  112. group.rotation.y = - Math.PI / 2;
  113. room.add( group );
  114. } );
  115. var loader = new THREE.CubeTextureLoader();
  116. loader.setPath( 'textures/cube/pisa/' );
  117. material.envMap = loader.load( [
  118. "px.png", "nx.png",
  119. "py.png", "ny.png",
  120. "pz.png", "nz.png"
  121. ] );
  122. raycaster = new THREE.Raycaster();
  123. renderer = new THREE.WebGLRenderer( { antialias: true } );
  124. renderer.setClearColor( 0x101010 );
  125. renderer.setPixelRatio( window.devicePixelRatio );
  126. renderer.setSize( window.innerWidth, window.innerHeight );
  127. renderer.sortObjects = false;
  128. container.appendChild( renderer.domElement );
  129. controls = new THREE.VRControls( camera );
  130. controls.standing = true;
  131. effect = new THREE.VREffect( renderer );
  132. if ( WEBVR.isAvailable() === true ) {
  133. document.body.appendChild( WEBVR.getButton( effect ) );
  134. }
  135. //
  136. window.addEventListener( 'resize', onWindowResize, false );
  137. }
  138. function onWindowResize() {
  139. camera.aspect = window.innerWidth / window.innerHeight;
  140. camera.updateProjectionMatrix();
  141. effect.setSize( window.innerWidth, window.innerHeight );
  142. }
  143. //
  144. function animate() {
  145. requestAnimationFrame( animate );
  146. render();
  147. }
  148. function render() {
  149. // find intersections
  150. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  151. var intersects = raycaster.intersectObjects( room.children );
  152. if ( intersects.length > 0 ) {
  153. if ( INTERSECTED != intersects[ 0 ].object ) {
  154. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  155. INTERSECTED = intersects[ 0 ].object;
  156. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  157. INTERSECTED.material.emissive.setHex( 0xff0000 );
  158. }
  159. } else {
  160. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  161. INTERSECTED = null;
  162. }
  163. controls.update();
  164. for ( var i = 0; i < room.children.length; i ++ ) {
  165. var cube = room.children[ i ];
  166. if ( cube.geometry instanceof THREE.BoxGeometry === false ) continue;
  167. // cube.position.add( cube.userData.velocity );
  168. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  169. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  170. cube.userData.velocity.x = - cube.userData.velocity.x;
  171. }
  172. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  173. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  174. cube.userData.velocity.y = - cube.userData.velocity.y;
  175. }
  176. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  177. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  178. cube.userData.velocity.z = - cube.userData.velocity.z;
  179. }
  180. cube.rotation.x += 0.01;
  181. }
  182. effect.render( scene, camera );
  183. }
  184. </script>
  185. </body>
  186. </html>