webgl_video_panorama_equirectangular.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - equirectangular video panorama demo</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. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. #info {
  14. position: absolute;
  15. top: 0px; width: 100%;
  16. color: #ffffff;
  17. padding: 5px;
  18. font-family:Monospace;
  19. font-size:13px;
  20. font-weight: bold;
  21. text-align:center;
  22. }
  23. a {
  24. color: #ffffff;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="container"></div>
  30. <script src="../build/three.min.js"></script>
  31. <script>
  32. var camera, scene, renderer;
  33. var videoTexture;
  34. var texture_placeholder,
  35. isUserInteracting = false,
  36. onMouseDownMouseX = 0, onMouseDownMouseY = 0,
  37. lon = 0, onMouseDownLon = 0,
  38. lat = 0, onMouseDownLat = 0,
  39. phi = 0, theta = 0;
  40. init();
  41. animate();
  42. function init() {
  43. var container, mesh;
  44. container = document.getElementById( 'container' );
  45. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
  46. camera.target = new THREE.Vector3( 0, 0, 0 );
  47. scene = new THREE.Scene();
  48. var geometry = new THREE.SphereGeometry( 500, 60, 40 );
  49. geometry.applyMatrix( new THREE.Matrix4().makeScale( -1, 1, 1 ) );
  50. video = document.createElement('video');
  51. video.width = 640;
  52. video.height = 360;
  53. video.autoplay = true;
  54. video.loop = true;
  55. video.crossOrigin='anonymous'
  56. video.src = "textures/pano.webm";
  57. videoTexture = new THREE.Texture( video );
  58. videoTexture.generateMipmaps = false;
  59. var material = new THREE.MeshBasicMaterial( { map : videoTexture } );
  60. mesh = new THREE.Mesh( geometry, material );
  61. scene.add( mesh );
  62. renderer = new THREE.WebGLRenderer();
  63. renderer.setSize( window.innerWidth, window.innerHeight );
  64. container.appendChild( renderer.domElement );
  65. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  66. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  67. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  68. document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
  69. document.addEventListener( 'DOMMouseScroll', onDocumentMouseWheel, false);
  70. //
  71. window.addEventListener( 'resize', onWindowResize, false );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. function onDocumentMouseDown( event ) {
  79. event.preventDefault();
  80. isUserInteracting = true;
  81. onPointerDownPointerX = event.clientX;
  82. onPointerDownPointerY = event.clientY;
  83. onPointerDownLon = lon;
  84. onPointerDownLat = lat;
  85. }
  86. function onDocumentMouseMove( event ) {
  87. if ( isUserInteracting === true ) {
  88. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  89. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  90. }
  91. }
  92. function onDocumentMouseUp( event ) {
  93. isUserInteracting = false;
  94. }
  95. function onDocumentMouseWheel( event ) {
  96. // WebKit
  97. if ( event.wheelDeltaY ) {
  98. camera.fov -= event.wheelDeltaY * 0.05;
  99. // Opera / Explorer 9
  100. } else if ( event.wheelDelta ) {
  101. camera.fov -= event.wheelDelta * 0.05;
  102. // Firefox
  103. } else if ( event.detail ) {
  104. camera.fov += event.detail * 1.0;
  105. }
  106. camera.updateProjectionMatrix();
  107. }
  108. function animate() {
  109. requestAnimationFrame( animate );
  110. update();
  111. }
  112. function update() {
  113. if( video.readyState === video.HAVE_ENOUGH_DATA ){
  114. videoTexture.needsUpdate = true;
  115. }
  116. //if ( isUserInteracting === false ) {
  117. // lon += 0.5;
  118. //}
  119. lat = Math.max( - 85, Math.min( 85, lat ) );
  120. phi = THREE.Math.degToRad( 90 - lat );
  121. theta = THREE.Math.degToRad( lon );
  122. camera.target.x = 500 * Math.sin( phi ) * Math.cos( theta );
  123. camera.target.y = 500 * Math.cos( phi );
  124. camera.target.z = 500 * Math.sin( phi ) * Math.sin( theta );
  125. camera.lookAt( camera.target );
  126. /*
  127. // distortion
  128. camera.position.copy( camera.target ).negate();
  129. */
  130. renderer.render( scene, camera );
  131. }
  132. </script>
  133. </body>
  134. </html>