canvas_geometry_panorama_fisheye.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - panorama fisheye 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: rgb(200,200,200);
  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. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - panorama fisheye demo. cubemap by <a href="http://www.zfight.com/" target="_blank">Jochum Skoglund</a>. (mousewheel: change fov)</div>
  31. <script src="../build/three.min.js"></script>
  32. <script src="js/renderers/Renderer.js"></script>
  33. <script src="js/renderers/CanvasRenderer.js"></script>
  34. <script>
  35. var camera, scene, renderer;
  36. var texture_placeholder,
  37. isUserInteracting = false,
  38. onMouseDownMouseX = 0, onMouseDownMouseY = 0,
  39. lon = 90, onMouseDownLon = 0,
  40. lat = 0, onMouseDownLat = 0,
  41. phi = 0, theta = 0,
  42. target = new THREE.Vector3();
  43. init();
  44. animate();
  45. function init() {
  46. var container, mesh;
  47. container = document.getElementById( 'container' );
  48. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );
  49. scene = new THREE.Scene();
  50. texture_placeholder = document.createElement( 'canvas' );
  51. texture_placeholder.width = 128;
  52. texture_placeholder.height = 128;
  53. var context = texture_placeholder.getContext( '2d' );
  54. context.fillStyle = 'rgb( 200, 200, 200 )';
  55. context.fillRect( 0, 0, texture_placeholder.width, texture_placeholder.height );
  56. var materials = [
  57. loadTexture( 'textures/cube/skybox/px.jpg' ), // right
  58. loadTexture( 'textures/cube/skybox/nx.jpg' ), // left
  59. loadTexture( 'textures/cube/skybox/py.jpg' ), // top
  60. loadTexture( 'textures/cube/skybox/ny.jpg' ), // bottom
  61. loadTexture( 'textures/cube/skybox/pz.jpg' ), // back
  62. loadTexture( 'textures/cube/skybox/nz.jpg' ) // front
  63. ];
  64. mesh = new THREE.Mesh( new THREE.BoxGeometry( 300, 300, 300, 7, 7, 7 ), new THREE.MeshFaceMaterial( materials ) );
  65. mesh.scale.x = - 1;
  66. scene.add( mesh );
  67. for ( var i = 0, l = mesh.geometry.vertices.length; i < l; i ++ ) {
  68. var vertex = mesh.geometry.vertices[ i ];
  69. vertex.normalize();
  70. vertex.multiplyScalar( 550 );
  71. }
  72. renderer = new THREE.CanvasRenderer();
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. container.appendChild( renderer.domElement );
  75. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  76. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  77. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  78. document.addEventListener( 'mousewheel', onDocumentMouseWheel, false );
  79. document.addEventListener( 'DOMMouseScroll', onDocumentMouseWheel, false);
  80. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  81. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  82. //
  83. window.addEventListener( 'resize', onWindowResize, false );
  84. }
  85. function onWindowResize() {
  86. camera.aspect = window.innerWidth / window.innerHeight;
  87. camera.updateProjectionMatrix();
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. }
  90. function loadTexture( path ) {
  91. var texture = new THREE.Texture( texture_placeholder );
  92. var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );
  93. var image = new Image();
  94. image.onload = function () {
  95. texture.image = this;
  96. texture.needsUpdate = true;
  97. };
  98. image.src = path;
  99. return material;
  100. }
  101. function onDocumentMouseDown( event ) {
  102. event.preventDefault();
  103. isUserInteracting = true;
  104. onPointerDownPointerX = event.clientX;
  105. onPointerDownPointerY = event.clientY;
  106. onPointerDownLon = lon;
  107. onPointerDownLat = lat;
  108. }
  109. function onDocumentMouseMove( event ) {
  110. if ( isUserInteracting === true ) {
  111. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  112. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  113. }
  114. }
  115. function onDocumentMouseUp( event ) {
  116. isUserInteracting = false;
  117. }
  118. function onDocumentMouseWheel( event ) {
  119. // WebKit
  120. if ( event.wheelDeltaY ) {
  121. camera.fov -= event.wheelDeltaY * 0.05;
  122. // Opera / Explorer 9
  123. } else if ( event.wheelDelta ) {
  124. camera.fov -= event.wheelDelta * 0.05;
  125. // Firefox
  126. } else if ( event.detail ) {
  127. camera.fov -= event.detail * 0.05;
  128. }
  129. camera.updateProjectionMatrix();
  130. }
  131. function onDocumentTouchStart( event ) {
  132. if ( event.touches.length == 1 ) {
  133. event.preventDefault();
  134. onPointerDownPointerX = event.touches[ 0 ].pageX;
  135. onPointerDownPointerY = event.touches[ 0 ].pageY;
  136. onPointerDownLon = lon;
  137. onPointerDownLat = lat;
  138. }
  139. }
  140. function onDocumentTouchMove( event ) {
  141. if ( event.touches.length == 1 ) {
  142. event.preventDefault();
  143. lon = ( onPointerDownPointerX - event.touches[0].pageX ) * 0.1 + onPointerDownLon;
  144. lat = ( event.touches[0].pageY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  145. }
  146. }
  147. function animate() {
  148. requestAnimationFrame( animate );
  149. update();
  150. }
  151. function update() {
  152. if ( isUserInteracting === false ) {
  153. lon += 0.1;
  154. }
  155. lat = Math.max( - 85, Math.min( 85, lat ) );
  156. phi = THREE.Math.degToRad( 90 - lat );
  157. theta = THREE.Math.degToRad( lon );
  158. target.x = 500 * Math.sin( phi ) * Math.cos( theta );
  159. target.y = 500 * Math.cos( phi );
  160. target.z = 500 * Math.sin( phi ) * Math.sin( theta );
  161. camera.position.copy( target ).negate();
  162. camera.lookAt( target );
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>