webvr_cubes.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  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>
  26. if ( WEBVR.isLatestAvailable() === false ) {
  27. document.body.appendChild( WEBVR.getMessage() );
  28. }
  29. //
  30. var container;
  31. var camera, scene, raycaster, renderer;
  32. var effect, controls;
  33. var room;
  34. var INTERSECTED;
  35. var crosshair;
  36. init();
  37. animate();
  38. function init() {
  39. container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. var info = document.createElement( 'div' );
  42. info.style.position = 'absolute';
  43. info.style.top = '10px';
  44. info.style.width = '100%';
  45. info.style.textAlign = 'center';
  46. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
  47. container.appendChild( info );
  48. scene = new THREE.Scene();
  49. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  50. scene.add( camera );
  51. crosshair = new THREE.Mesh(
  52. new THREE.RingGeometry( 0.02, 0.04, 32 ),
  53. new THREE.MeshBasicMaterial( {
  54. color: 0xffffff,
  55. opacity: 0.5,
  56. transparent: true
  57. } )
  58. );
  59. crosshair.position.z = - 2;
  60. camera.add( crosshair );
  61. room = new THREE.Mesh(
  62. new THREE.BoxGeometry( 6, 6, 6, 10, 10, 10 ),
  63. new THREE.MeshBasicMaterial( { color: 0x202020, wireframe: true } )
  64. );
  65. scene.add( room );
  66. scene.add( new THREE.HemisphereLight( 0x404020, 0x202040, 0.5 ) );
  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. raycaster = new THREE.Raycaster();
  89. renderer = new THREE.WebGLRenderer( { antialias: true } );
  90. renderer.setClearColor( 0x101010 );
  91. renderer.setPixelRatio( window.devicePixelRatio );
  92. renderer.setSize( window.innerWidth, window.innerHeight );
  93. renderer.sortObjects = false;
  94. container.appendChild( renderer.domElement );
  95. controls = new THREE.VRControls( camera );
  96. effect = new THREE.VREffect( renderer );
  97. if ( WEBVR.isAvailable() === true ) {
  98. document.body.appendChild( WEBVR.getButton( effect ) );
  99. }
  100. renderer.domElement.addEventListener( 'click', function ( event ) {
  101. if ( INTERSECTED ) {
  102. var object = INTERSECTED;
  103. object.userData.velocity.subVectors( object.position, camera.position ).normalize().multiplyScalar( 0.1 );
  104. }
  105. } );
  106. //
  107. window.addEventListener( 'resize', onWindowResize, false );
  108. }
  109. function onWindowResize() {
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. effect.setSize( window.innerWidth, window.innerHeight );
  113. }
  114. //
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. render();
  118. }
  119. function render() {
  120. // find intersections
  121. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  122. var intersects = raycaster.intersectObjects( room.children );
  123. if ( intersects.length > 0 ) {
  124. if ( INTERSECTED != intersects[ 0 ].object ) {
  125. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  126. INTERSECTED = intersects[ 0 ].object;
  127. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  128. INTERSECTED.material.emissive.setHex( 0xff0000 );
  129. }
  130. } else {
  131. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  132. INTERSECTED = undefined;
  133. }
  134. controls.update();
  135. for ( var i = 0; i < room.children.length; i ++ ) {
  136. var cube = room.children[ i ];
  137. cube.userData.velocity.multiplyScalar( 0.999 );
  138. cube.position.add( cube.userData.velocity );
  139. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  140. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  141. cube.userData.velocity.x = - cube.userData.velocity.x;
  142. }
  143. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  144. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  145. cube.userData.velocity.y = - cube.userData.velocity.y;
  146. }
  147. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  148. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  149. cube.userData.velocity.z = - cube.userData.velocity.z;
  150. }
  151. cube.rotation.x += 0.01;
  152. }
  153. effect.render( scene, camera );
  154. }
  155. </script>
  156. </body>
  157. </html>
粤ICP备19079148号