webvr_cubes.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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, 1, 10000 );
  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 light = new THREE.DirectionalLight( 0xffffff, 1 );
  64. light.position.set( 1, 1, 1 ).normalize();
  65. scene.add( light );
  66. var geometry = new THREE.BoxGeometry( 20, 20, 20 );
  67. for ( var i = 0; i < 200; i ++ ) {
  68. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  69. object.position.x = Math.random() * 800 - 400;
  70. object.position.y = Math.random() * 800 - 400;
  71. object.position.z = Math.random() * 800 - 400;
  72. object.rotation.x = Math.random() * 2 * Math.PI;
  73. object.rotation.y = Math.random() * 2 * Math.PI;
  74. object.rotation.z = Math.random() * 2 * Math.PI;
  75. object.scale.x = Math.random() + 0.5;
  76. object.scale.y = Math.random() + 0.5;
  77. object.scale.z = Math.random() + 0.5;
  78. scene.add( object );
  79. cubes.push( object );
  80. }
  81. raycaster = new THREE.Raycaster();
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setClearColor( 0x101010 );
  84. renderer.setPixelRatio( window.devicePixelRatio );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. renderer.sortObjects = false;
  87. container.appendChild( renderer.domElement );
  88. controls = new THREE.VRControls( camera );
  89. effect = new THREE.VREffect( renderer );
  90. if ( WEBVR.isAvailable() === true ) {
  91. document.body.appendChild( WEBVR.getButton( effect ) );
  92. }
  93. stats = new Stats();
  94. stats.domElement.style.position = 'absolute';
  95. stats.domElement.style.top = '0';
  96. container.appendChild( stats.domElement );
  97. //
  98. window.addEventListener( 'resize', onWindowResize, false );
  99. }
  100. function onWindowResize() {
  101. camera.aspect = window.innerWidth / window.innerHeight;
  102. camera.updateProjectionMatrix();
  103. effect.setSize( window.innerWidth, window.innerHeight );
  104. }
  105. //
  106. function animate() {
  107. requestAnimationFrame( animate );
  108. render();
  109. stats.update();
  110. }
  111. function render() {
  112. // find intersections
  113. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  114. var intersects = raycaster.intersectObjects( cubes );
  115. if ( intersects.length > 0 ) {
  116. if ( INTERSECTED != intersects[ 0 ].object ) {
  117. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  118. INTERSECTED = intersects[ 0 ].object;
  119. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  120. INTERSECTED.material.emissive.setHex( 0xff0000 );
  121. }
  122. } else {
  123. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  124. INTERSECTED = null;
  125. }
  126. controls.update();
  127. effect.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>