当前位置:   article > 正文

three.js实现3D全景(vue2)_3d 全景 three

3d 全景 three

下包

npm i three

引入

  1. import * as THREE from "three";
  2. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

调用方法(有部分数据要存在data里)

  1. createThree() {
  2. // 初始化场景
  3. this.scene = new THREE.Scene();
  4. // 初始化相机
  5. this.camera = new THREE.PerspectiveCamera(
  6. 90,
  7. document.body.clientWidth / document.body.clientHeight,
  8. 0.1,
  9. 100
  10. );
  11. this.camera.position.set(0, 0, 0.1);
  12. // 初始化渲染器
  13. this.renderer = new THREE.WebGLRenderer();
  14. this.renderer.setSize(window.innerWidth, window.innerHeight);
  15. document
  16. .getElementById("container")
  17. .appendChild(this.renderer.domElement);
  18. // 镜头控制器
  19. var controls = new OrbitControls(this.camera, this.renderer.domElement);
  20. // //添加3D物体
  21. // 用立方体实现
  22. // this.useBox();
  23. // 用全景图实现
  24. this.useSphere();
  25. // 调用启动渲染帧;
  26. this.loop();
  27. },
  28. loop() {
  29. requestAnimationFrame(this.loop);
  30. this.renderer.render(this.scene, this.camera);
  31. },
  32. useBox() {
  33. var materials = [];
  34. var texture_left = new THREE.TextureLoader().load(
  35. require("../assets/scene_left.png")
  36. );
  37. materials.push(new THREE.MeshBasicMaterial({ map: texture_left }));
  38. var texture_right = new THREE.TextureLoader().load(
  39. require("../assets/scene_right.png")
  40. );
  41. materials.push(new THREE.MeshBasicMaterial({ map: texture_right }));
  42. var texture_top = new THREE.TextureLoader().load(
  43. require("../assets/scene_top.png")
  44. );
  45. materials.push(new THREE.MeshBasicMaterial({ map: texture_top }));
  46. var texture_bottom = new THREE.TextureLoader().load(
  47. require("../assets/scene_bottom.png")
  48. );
  49. materials.push(new THREE.MeshBasicMaterial({ map: texture_bottom }));
  50. var texture_front = new THREE.TextureLoader().load(
  51. require("../assets/scene_front.png")
  52. );
  53. materials.push(new THREE.MeshBasicMaterial({ map: texture_front }));
  54. var texture_back = new THREE.TextureLoader().load(
  55. require("../assets/scene_back.png")
  56. );
  57. materials.push(new THREE.MeshBasicMaterial({ map: texture_back }));
  58. var box = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1), materials);
  59. // 用线框模式大家可以看得清楚是个立方体而不是矩形
  60. box.material.wireframe = true;
  61. this.scene.add(box);
  62. // 把图片翻转
  63. box.geometry.scale(1, 1, -1);
  64. // 设置相机位置
  65. this.camera.position.set(0, 0, 0.01);
  66. },
  67. useSphere() {
  68. let sphereGeometry = new THREE.SphereGeometry(1, 50, 50);
  69. let texture = new THREE.TextureLoader().load(
  70. require("../assets/scene.jpeg")
  71. );
  72. let sphereMaterial = new THREE.MeshBasicMaterial({ map: texture });
  73. let sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
  74. // sphere.material.wireframe = true;
  75. this.scene.add(sphere);
  76. sphere.geometry.scale(1, 1, -1);
  77. this.camera.position.set(0, 0, 0.01);
  78. },

可以用两种方法:立方体(6张图)和球体(全景图)

都是把图贴到物体上,再把图片翻转,把相机移到物体中间即可。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号