Scene3d.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import PanoWireframe from './PanoWireframe.js'
  2. var renderer, camera, scene, controls;
  3. var cameraDefault = new THREE.Vector3(0, 0, 0.001);
  4. var lookAtPoint = new THREE.Vector3(0, 0, 0);
  5. export default class Scene3d {
  6. constructor(options) {
  7. this.dom = options.dom
  8. this.width = options.width
  9. this.height = options.height
  10. this.sceneCode = options.sceneCode
  11. /**
  12. * 初始化渲染器
  13. */
  14. function initRenderer() {
  15. renderer = new THREE.WebGLRenderer({antialias: true});
  16. renderer.setClearColor(0x333333, 1);
  17. renderer.setSize(options.width, options.height);
  18. renderer.shadowMap.enabled = true;
  19. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  20. options.dom.appendChild( renderer.domElement );
  21. }
  22. initRenderer()
  23. /**
  24. * 初始化场景
  25. */
  26. function initScene() {
  27. scene = new THREE.Scene();
  28. }
  29. initScene()
  30. /**
  31. * 初始化相机
  32. */
  33. function initCamera() {
  34. camera = new THREE.PerspectiveCamera(90, options.width / options.height, 0.01, 1000);
  35. camera.position.set(cameraDefault.x, cameraDefault.y, cameraDefault.z);
  36. camera.lookAt(lookAtPoint);
  37. }
  38. initCamera()
  39. /**
  40. * 初始化控制器
  41. */
  42. function initControls() {
  43. controls = new THREE.OrbitControls( camera, renderer.domElement );
  44. controls.rotateSpeed = 2.0; // 旋转速度
  45. controls.zoomSpeed = 1.2; // 缩放速度
  46. controls.panSpeed = 0.8; // 平controls
  47. controls.staticMoving = true; // 静止移动,为 true 则没有惯性
  48. controls.dynamicDampingFactor = 0.3; // 阻尼系数 越小 则滑动越大
  49. controls.saveState()
  50. }
  51. initControls()
  52. const geometry = new THREE.SphereGeometry( 30, 32, 16 );
  53. const material = new THREE.MeshBasicMaterial( { side: 2 } );
  54. this.sky = new THREE.Mesh( geometry, material );
  55. this.sky.scale.x = -1
  56. scene.add( this.sky );
  57. this.panoWireframe = new PanoWireframe(scene, this.sceneCode)
  58. function animate() {
  59. renderer.render(scene, camera);
  60. controls.update();
  61. requestAnimationFrame(animate);
  62. }
  63. animate();
  64. }
  65. async load(panoId) {
  66. let texture = new THREE.TextureLoader().load(`https://4dkk.4dage.com/scene_view_data/${this.sceneCode}/images/pan/high/${panoId}.jpg`)
  67. this.sky.material.map = texture
  68. this.sky.material.needsUpdate = true
  69. this.panoWireframe.clearAll()
  70. let labels = await this.panoWireframe.load(panoId)
  71. return labels
  72. }
  73. }