webvr_multiview.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - ball shooter</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: #48f;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script type="module">
  22. import * as THREE from '../build/three.module.js';
  23. import { BoxLineGeometry } from './jsm/geometries/BoxLineGeometry.js';
  24. import { WEBVR } from './jsm/vr/WebVR.js';
  25. import Stats from './jsm/libs/stats.module.js';
  26. var container;
  27. var camera, scene, renderer;
  28. var room;
  29. var stats;
  30. var count = 0;
  31. var radius = 0.02;
  32. var normal = new THREE.Vector3();
  33. var relativeVelocity = new THREE.Vector3();
  34. var clock = new THREE.Clock();
  35. init();
  36. animate();
  37. function init() {
  38. container = document.createElement( 'div' );
  39. document.body.appendChild( container );
  40. //
  41. var canvas = document.createElement( 'canvas' );
  42. var context = canvas.getContext( 'webgl2', { antialias: false } );
  43. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  44. renderer.setPixelRatio( window.devicePixelRatio );
  45. renderer.setSize( window.innerWidth, window.innerHeight );
  46. renderer.vr.enabled = true;
  47. container.appendChild( renderer.domElement );
  48. var info = document.createElement( 'div' );
  49. info.style.position = 'absolute';
  50. info.style.top = '10px';
  51. info.style.width = '100%';
  52. info.style.textAlign = 'center';
  53. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - ball shooter';
  54. container.appendChild( info );
  55. scene = new THREE.Scene();
  56. scene.background = new THREE.Color( 0x505050 );
  57. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  58. room = new THREE.LineSegments(
  59. new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ),
  60. new THREE.LineBasicMaterial( { color: renderer.multiview.isAvailable() ? 0x99ff99 : 0xff3333 } )
  61. );
  62. room.geometry.translate( 0, 3, 0 );
  63. scene.add( room );
  64. var light = new THREE.HemisphereLight( 0xffffff, 0x444444 );
  65. light.position.set( 1, 1, 1 );
  66. scene.add( light );
  67. var geometry = new THREE.IcosahedronBufferGeometry( radius );
  68. for ( var i = 0; i < 2000; i ++ ) {
  69. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  70. object.position.x = Math.random() * 4 - 2;
  71. object.position.y = Math.random() * 4;
  72. object.position.z = Math.random() * 4 - 2;
  73. object.userData.velocity = new THREE.Vector3();
  74. object.userData.velocity.x = 2 * Math.random() - 1;
  75. object.userData.velocity.y = 2 * Math.random() - 1;
  76. object.userData.velocity.z = 2 * Math.random() - 1;
  77. room.add( object );
  78. }
  79. //
  80. document.body.appendChild( WEBVR.createButton( renderer ) );
  81. //
  82. stats = new Stats();
  83. container.appendChild( stats.dom );
  84. window.addEventListener( 'resize', onWindowResize, false );
  85. }
  86. function onWindowResize() {
  87. camera.aspect = window.innerWidth / window.innerHeight;
  88. camera.updateProjectionMatrix();
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. }
  91. //
  92. function animate() {
  93. renderer.setAnimationLoop( render );
  94. }
  95. function render() {
  96. //
  97. stats.update();
  98. var delta = clock.getDelta();
  99. var range = 3 - radius;
  100. for ( var i = 0; i < room.children.length; i ++ ) {
  101. var object = room.children[ i ];
  102. object.position.x += object.userData.velocity.x * delta;
  103. object.position.y += object.userData.velocity.y * delta;
  104. object.position.z += object.userData.velocity.z * delta;
  105. // keep objects inside room
  106. if ( object.position.x < - range || object.position.x > range ) {
  107. object.position.x = THREE.Math.clamp( object.position.x, - range, range );
  108. object.userData.velocity.x = - object.userData.velocity.x;
  109. }
  110. if ( object.position.y < radius || object.position.y > 6 ) {
  111. object.position.y = Math.max( object.position.y, radius );
  112. object.userData.velocity.y = - object.userData.velocity.y;
  113. }
  114. if ( object.position.z < - range || object.position.z > range ) {
  115. object.position.z = THREE.Math.clamp( object.position.z, - range, range );
  116. object.userData.velocity.z = - object.userData.velocity.z;
  117. }
  118. }
  119. renderer.render( scene, camera );
  120. }
  121. </script>
  122. </body>
  123. </html>
粤ICP备19079148号