canvas_geometry_panorama.html 5.3 KB

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