webvr_cubes.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.min.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/libs/stats.min.js"></script>
  26. <script>
  27. if ( WEBVR.isLatestAvailable() === false ) {
  28. document.body.appendChild( WEBVR.getMessage() );
  29. }
  30. //
  31. var container, stats;
  32. var camera, scene, raycaster, renderer;
  33. var effect, controls;
  34. var cubes = [];
  35. var INTERSECTED;
  36. var radius = 100, theta = 0;
  37. var crosshair;
  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 - interactive cubes';
  49. container.appendChild( info );
  50. scene = new THREE.Scene();
  51. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 1000 );
  52. scene.add( camera );
  53. crosshair = new THREE.Mesh(
  54. new THREE.RingGeometry( 0.5, 1, 32 ),
  55. new THREE.MeshBasicMaterial( {
  56. color: 0xffffff,
  57. opacity: 0.5,
  58. transparent: true
  59. } )
  60. );
  61. crosshair.position.z = - 40;
  62. camera.add( crosshair );
  63. var mesh = new THREE.Mesh(
  64. new THREE.BoxGeometry( 200, 200, 200, 10, 10, 10 ),
  65. new THREE.MeshBasicMaterial( { color: 0x202020, wireframe: true } )
  66. );
  67. scene.add( mesh );
  68. scene.add( new THREE.AmbientLight( 0x202040 ) );
  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( 3, 3, 3 );
  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() * 100 - 50;
  76. object.position.y = Math.random() * 100 - 50;
  77. object.position.z = Math.random() * 100 - 50;
  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.5 ) * 0.1;
  86. object.userData.velocity.y = ( Math.random() - 0.5 ) * 0.1;
  87. object.userData.velocity.z = ( Math.random() - 0.5 ) * 0.1;
  88. scene.add( object );
  89. cubes.push( object );
  90. }
  91. raycaster = new THREE.Raycaster();
  92. renderer = new THREE.WebGLRenderer( { antialias: true } );
  93. renderer.setClearColor( 0x101010 );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.sortObjects = false;
  97. container.appendChild( renderer.domElement );
  98. controls = new THREE.VRControls( camera );
  99. effect = new THREE.VREffect( renderer );
  100. if ( WEBVR.isAvailable() === true ) {
  101. document.body.appendChild( WEBVR.getButton( effect ) );
  102. }
  103. stats = new Stats();
  104. stats.domElement.style.position = 'absolute';
  105. stats.domElement.style.top = '0';
  106. container.appendChild( stats.domElement );
  107. //
  108. window.addEventListener( 'resize', onWindowResize, false );
  109. }
  110. function onWindowResize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. effect.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. //
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. render();
  119. stats.update();
  120. }
  121. function render() {
  122. // find intersections
  123. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  124. var intersects = raycaster.intersectObjects( cubes );
  125. if ( intersects.length > 0 ) {
  126. if ( INTERSECTED != intersects[ 0 ].object ) {
  127. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  128. INTERSECTED = intersects[ 0 ].object;
  129. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  130. INTERSECTED.material.emissive.setHex( 0xff0000 );
  131. }
  132. } else {
  133. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  134. INTERSECTED = null;
  135. }
  136. controls.update();
  137. for ( var i = 0; i < cubes.length; i ++ ) {
  138. var cube = cubes[ i ];
  139. cube.position.add( cube.userData.velocity );
  140. if ( cube.position.x < 100 ) cube.position.x += 200;
  141. if ( cube.position.y < 100 ) cube.position.y += 200;
  142. if ( cube.position.z < 100 ) cube.position.z += 200;
  143. if ( cube.position.x > 100 ) cube.position.x -= 200;
  144. if ( cube.position.y > 100 ) cube.position.y -= 200;
  145. if ( cube.position.z > 100 ) cube.position.z -= 200;
  146. cube.rotation.x += 0.01;
  147. }
  148. effect.render( scene, camera );
  149. }
  150. </script>
  151. </body>
  152. </html>