canvas_geometry_panorama.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.js"></script>
  32. <script src="js/renderers/Projector.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.MultiMaterial( materials ) );
  65. mesh.scale.x = - 1;
  66. scene.add( mesh );
  67. renderer = new THREE.CanvasRenderer();
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. container.appendChild( renderer.domElement );
  71. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  72. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  73. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  74. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  75. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  76. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  77. //
  78. window.addEventListener( 'resize', onWindowResize, false );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. function loadTexture( path ) {
  86. var texture = new THREE.Texture( texture_placeholder );
  87. var material = new THREE.MeshBasicMaterial( { map: texture, overdraw: 0.5 } );
  88. var image = new Image();
  89. image.onload = function () {
  90. texture.image = this;
  91. texture.needsUpdate = true;
  92. };
  93. image.src = path;
  94. return material;
  95. }
  96. function onDocumentMouseDown( event ) {
  97. event.preventDefault();
  98. isUserInteracting = true;
  99. onPointerDownPointerX = event.clientX;
  100. onPointerDownPointerY = event.clientY;
  101. onPointerDownLon = lon;
  102. onPointerDownLat = lat;
  103. }
  104. function onDocumentMouseMove( event ) {
  105. if ( isUserInteracting === true ) {
  106. lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
  107. lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  108. }
  109. }
  110. function onDocumentMouseUp( event ) {
  111. isUserInteracting = false;
  112. }
  113. function onDocumentMouseWheel( event ) {
  114. camera.fov += event.deltaY * 0.05;
  115. camera.updateProjectionMatrix();
  116. }
  117. function onDocumentTouchStart( event ) {
  118. if ( event.touches.length == 1 ) {
  119. event.preventDefault();
  120. onPointerDownPointerX = event.touches[ 0 ].pageX;
  121. onPointerDownPointerY = event.touches[ 0 ].pageY;
  122. onPointerDownLon = lon;
  123. onPointerDownLat = lat;
  124. }
  125. }
  126. function onDocumentTouchMove( event ) {
  127. if ( event.touches.length == 1 ) {
  128. event.preventDefault();
  129. lon = ( onPointerDownPointerX - event.touches[0].pageX ) * 0.1 + onPointerDownLon;
  130. lat = ( event.touches[0].pageY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
  131. }
  132. }
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. update();
  136. }
  137. function update() {
  138. if ( isUserInteracting === false ) {
  139. lon += 0.1;
  140. }
  141. lat = Math.max( - 85, Math.min( 85, lat ) );
  142. phi = THREE.Math.degToRad( 90 - lat );
  143. theta = THREE.Math.degToRad( lon );
  144. target.x = 500 * Math.sin( phi ) * Math.cos( theta );
  145. target.y = 500 * Math.cos( phi );
  146. target.z = 500 * Math.sin( phi ) * Math.sin( theta );
  147. camera.lookAt( target );
  148. renderer.render( scene, camera );
  149. }
  150. </script>
  151. </body>
  152. </html>