css3d_panorama.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>three.js css3d - panorama</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js css3d</a> - panorama demo. cubemap by <a href="http://www.humus.name/index.php?page=Textures" target="_blank" rel="noopener">Humus</a>.</div>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { CSS3DRenderer, CSS3DObject } from './jsm/renderers/CSS3DRenderer.js';
  14. var camera, scene, renderer;
  15. var target = new THREE.Vector3();
  16. var lon = 90, lat = 0;
  17. var phi = 0, theta = 0;
  18. var touchX, touchY;
  19. init();
  20. animate();
  21. function init() {
  22. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
  23. scene = new THREE.Scene();
  24. var sides = [
  25. {
  26. url: 'textures/cube/Bridge2/posx.jpg',
  27. position: [ - 512, 0, 0 ],
  28. rotation: [ 0, Math.PI / 2, 0 ]
  29. },
  30. {
  31. url: 'textures/cube/Bridge2/negx.jpg',
  32. position: [ 512, 0, 0 ],
  33. rotation: [ 0, - Math.PI / 2, 0 ]
  34. },
  35. {
  36. url: 'textures/cube/Bridge2/posy.jpg',
  37. position: [ 0, 512, 0 ],
  38. rotation: [ Math.PI / 2, 0, Math.PI ]
  39. },
  40. {
  41. url: 'textures/cube/Bridge2/negy.jpg',
  42. position: [ 0, - 512, 0 ],
  43. rotation: [ - Math.PI / 2, 0, Math.PI ]
  44. },
  45. {
  46. url: 'textures/cube/Bridge2/posz.jpg',
  47. position: [ 0, 0, 512 ],
  48. rotation: [ 0, Math.PI, 0 ]
  49. },
  50. {
  51. url: 'textures/cube/Bridge2/negz.jpg',
  52. position: [ 0, 0, - 512 ],
  53. rotation: [ 0, 0, 0 ]
  54. }
  55. ];
  56. for ( var i = 0; i < sides.length; i ++ ) {
  57. var side = sides[ i ];
  58. var element = document.createElement( 'img' );
  59. element.width = 1026; // 2 pixels extra to close the gap.
  60. element.src = side.url;
  61. var object = new CSS3DObject( element );
  62. object.position.fromArray( side.position );
  63. object.rotation.fromArray( side.rotation );
  64. scene.add( object );
  65. }
  66. renderer = new CSS3DRenderer();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. document.body.appendChild( renderer.domElement );
  69. //
  70. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  71. document.addEventListener( 'wheel', onDocumentMouseWheel, false );
  72. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  73. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. }
  76. function onWindowResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. }
  81. function onDocumentMouseDown( event ) {
  82. event.preventDefault();
  83. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  84. document.addEventListener( 'mouseup', onDocumentMouseUp, false );
  85. }
  86. function onDocumentMouseMove( event ) {
  87. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  88. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  89. lon -= movementX * 0.1;
  90. lat += movementY * 0.1;
  91. }
  92. function onDocumentMouseUp() {
  93. document.removeEventListener( 'mousemove', onDocumentMouseMove );
  94. document.removeEventListener( 'mouseup', onDocumentMouseUp );
  95. }
  96. function onDocumentMouseWheel( event ) {
  97. var fov = camera.fov + event.deltaY * 0.05;
  98. camera.fov = THREE.MathUtils.clamp( fov, 10, 75 );
  99. camera.updateProjectionMatrix();
  100. }
  101. function onDocumentTouchStart( event ) {
  102. event.preventDefault();
  103. var touch = event.touches[ 0 ];
  104. touchX = touch.screenX;
  105. touchY = touch.screenY;
  106. }
  107. function onDocumentTouchMove( event ) {
  108. event.preventDefault();
  109. var touch = event.touches[ 0 ];
  110. lon -= ( touch.screenX - touchX ) * 0.1;
  111. lat += ( touch.screenY - touchY ) * 0.1;
  112. touchX = touch.screenX;
  113. touchY = touch.screenY;
  114. }
  115. function animate() {
  116. requestAnimationFrame( animate );
  117. lon += 0.1;
  118. lat = Math.max( - 85, Math.min( 85, lat ) );
  119. phi = THREE.MathUtils.degToRad( 90 - lat );
  120. theta = THREE.MathUtils.degToRad( lon );
  121. target.x = Math.sin( phi ) * Math.cos( theta );
  122. target.y = Math.cos( phi );
  123. target.z = Math.sin( phi ) * Math.sin( theta );
  124. camera.lookAt( target );
  125. renderer.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>