当前位置:   article > 正文

three.js 通过 CSS3DRenderer 实现全景浏览 (转)_css3drenderer gitee

css3drenderer gitee

  上一篇是通过WebGlRenderer渲染器,加上几何贴图实现的一张全景浏览,但是会出现,图片的大小和画质会被改变,导致渲染的不清晰,那么本篇是通过CSS3DRenderer来实现全景浏览,和上次不同的是这次是六张图,分别铺满一个立方体盒子的六个面。

è¿éåå¾çæè¿°

示例
https://ithanmang.gitee.io/threejs/home/example/css3drenderer/index.html
效果
è¿éåå¾çæè¿°

代码:

  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. <style>
  8. body {
  9. margin: 0;
  10. overflow: hidden;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <script src="https://ithanmang.gitee.io/threejs/home/libs/build/three-r93.js"></script>
  16. <script src="https://ithanmang.gitee.io/threejs/home/libs/examples/js/renderers/CSS3DRenderer.js"></script>
  17. <script>
  18. var camera, scene, renderer;
  19. /* 相机聚焦点 */
  20. var target = new THREE.Vector3();
  21. /* lon:经度, lat:维度 经纬度是相机的聚焦点 */
  22. var lon = 90,
  23. lat = 0;
  24. /* 相机聚焦点,弧度制 */
  25. var phi = 0,
  26. theta = 0;
  27. /* 移动端输入的x,y */
  28. var touchX, touchY;
  29. /* 图片路径 */
  30. var src = 'https://ithanmang.gitee.io/threejs/home/example/css3drenderer/images/siximg/par.front.jpg';
  31. /* 因为图片加载比较慢可以先设置一个背景图 */
  32. document.body.style.backgroundImage = 'url(' + src + ")";
  33. document.body.style.backgroundRepeat = 'no-repeat';
  34. document.body.style.backgroundSize = window.innerWidth + 'px ' + window.innerHeight + 'px';
  35. var img = new Image();
  36. img.src = src;
  37. /* 图片加载完之后调用 */
  38. img.onload = function() {
  39. init(img.width, src);
  40. animate();
  41. };
  42. function init(width, imageSrc) {
  43. img = null;
  44. camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 1000);
  45. scene = new THREE.Scene();
  46. /* 图片路径 */
  47. let src = 'https://ithanmang.gitee.io/threejs/home/example/css3drenderer/images/siximg/par.';
  48. /* 图片格式 */
  49. let format = '.jpg';
  50. /* 以下六个面 拼接成立方体 */
  51. var sides = [{
  52. url: src + 'left' + format,
  53. position: [-width / 2, 0, 0],
  54. /* 左 */
  55. rotation: [0, Math.PI / 2, 0]
  56. },
  57. {
  58. url: src + 'right' + format,
  59. position: [width / 2, 0, 0],
  60. /* 右 */
  61. rotation: [0, -Math.PI / 2, 0]
  62. },
  63. {
  64. url: src + 'top' + format,
  65. position: [0, width / 2, 0],
  66. /* 上 */
  67. rotation: [Math.PI / 2, 0, Math.PI]
  68. },
  69. {
  70. url: src + 'bottom' + format,
  71. position: [0, -width / 2, 0],
  72. /* 下 */
  73. rotation: [-Math.PI / 2, 0, Math.PI]
  74. },
  75. {
  76. url: src + 'back' + format,
  77. position: [0, 0, width / 2],
  78. /* 后 */
  79. rotation: [0, Math.PI, 0]
  80. },
  81. {
  82. url: src + 'front' + format,
  83. position: [0, 0, -width / 2],
  84. /* 前 */
  85. rotation: [0, 0, 0]
  86. }
  87. ];
  88. for(var i = 0; i < sides.length; i++) {
  89. var side = sides[i];
  90. var element = document.createElement('img');
  91. element.width = width + 6;
  92. element.src = side.url;
  93. var css3Loader = new THREE.CSS3DObject(element);
  94. css3Loader.position.fromArray(side.position);
  95. css3Loader.rotation.fromArray(side.rotation);
  96. scene.add(css3Loader);
  97. }
  98. renderer = new THREE.CSS3DRenderer();
  99. renderer.setSize(window.innerWidth, window.innerHeight);
  100. document.body.appendChild(renderer.domElement);
  101. document.addEventListener('mousedown', onDocumentMouseDown, false);
  102. document.addEventListener('wheel', onDocumentMouseWheel, false);
  103. document.addEventListener('touchstart', onDocumentTouchStart, false);
  104. document.addEventListener('touchmove', onDocumentTouchMove, false);
  105. window.addEventListener('resize', onWindowResize, false);
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize(window.innerWidth, window.innerHeight);
  111. }
  112. function onDocumentMouseDown(event) {
  113. event.preventDefault();
  114. document.body.style.cursor = 'move';
  115. document.addEventListener('mousemove', onDocumentMouseMove, false);
  116. document.addEventListener('mouseup', onDocumentMouseUp, false);
  117. }
  118. function onDocumentMouseMove(event) {
  119. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  120. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  121. /* 拖动速度随相机视角的变动而变动 */
  122. var speed = camera.fov * 0.0015;
  123. /* 经纬度平移速度 */
  124. lon -= movementX * speed;
  125. lat += movementY * speed;
  126. }
  127. function onDocumentMouseUp(event) {
  128. event.preventDefault();
  129. document.body.style.cursor = 'auto';
  130. document.removeEventListener('mousemove', onDocumentMouseMove);
  131. document.removeEventListener('mouseup', onDocumentMouseUp);
  132. }
  133. function onDocumentMouseWheel(event) {
  134. /* 缩放速度 */
  135. var speed = 0.008;
  136. var fov = camera.fov + event.deltaY * speed;
  137. /* 角度缩放最值 */
  138. camera.fov = THREE.Math.clamp(fov, 45, 95);
  139. camera.updateProjectionMatrix();
  140. }
  141. function onDocumentTouchStart(event) {
  142. event.preventDefault();
  143. var touch = event.touches[0];
  144. touchX = touch.screenX;
  145. touchY = touch.screenY;
  146. }
  147. function onDocumentTouchMove(event) {
  148. event.preventDefault();
  149. var touch = event.touches[0];
  150. lon -= (touch.screenX - touchX) * 0.1;
  151. lat += (touch.screenY - touchY) * 0.1;
  152. touchX = touch.screenX;
  153. touchY = touch.screenY;
  154. }
  155. function animate() {
  156. requestAnimationFrame(animate);
  157. lat = Math.max(-85, Math.min(85, lat));
  158. phi = THREE.Math.degToRad(90 - lat);
  159. theta = THREE.Math.degToRad(lon);
  160. target.x = Math.sin(phi) * Math.cos(theta);
  161. target.y = Math.cos(phi);
  162. target.z = Math.sin(phi) * Math.sin(theta);
  163. camera.lookAt(target);
  164. renderer.render(scene, camera);
  165. }
  166. </script>
  167. </body>
  168. </html>

说明:

(1)偶然看到一个例子,感觉蛮有趣的,可以运行,留作自己收藏;

(2)修改了一下需要引入的资源的路径,可以直接运行;

(3)文章的作者注释比较详细,看起来也会比较方便;

(4)转载地址:https://blog.csdn.net/ithanmang/article/details/82013276

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/135149
推荐阅读
相关标签
  

闽ICP备14008679号