1
0

Scene.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import * as THREE from "three";
  2. import Stats from "three/examples/jsm/libs/stats.module.js";
  3. import Player from "./player/Player.js";
  4. import BoxManager from "./box/BoxManager.js";
  5. import { Mitt } from "./mitt.js";
  6. const stats = new Stats();
  7. export default class Scene extends Mitt {
  8. constructor(domElement) {
  9. super();
  10. this.domElement = domElement;
  11. this.scene = null;
  12. this.renderer = null;
  13. this.orthCamera = null;
  14. this.player = null;
  15. this.sceneType = 1;
  16. this.width = 0;
  17. this.height = 0;
  18. this.inited = false;
  19. this.init = () => {
  20. this.scene = new THREE.Scene();
  21. this.scene.background = new THREE.Color(0xf0f2f5);
  22. this.renderer = new THREE.WebGLRenderer({
  23. canvas: this.domElement,
  24. antialias: true,
  25. });
  26. this.width = this.domElement.clientWidth;
  27. this.height = this.domElement.clientHeight;
  28. this.renderRes = window.devicePixelRatio;
  29. this.renderer.setSize(this.width, this.height);
  30. this.renderer.setPixelRatio(this.renderRes);
  31. console.log(this.width, this.height, this.renderRes);
  32. this.orthCamera = new THREE.OrthographicCamera(
  33. -this.width / 2,
  34. this.width / 2,
  35. this.height / 2,
  36. -this.height / 2,
  37. 0.1,
  38. 1000
  39. );
  40. this.orthCamera.zoom = 250;
  41. this.orthCamera.position.set(0, 10, 0);
  42. this.orthCamera.lookAt(0, 0, 0);
  43. // this.orthCamera.setViewOffset(this.width, this.height, 0, 0);
  44. this.orthCamera.updateProjectionMatrix();
  45. //player
  46. this.player = new Player(this);
  47. //stats
  48. domElement.parentNode.appendChild(stats.dom);
  49. stats.dom.style.pointerEvents = "none";
  50. stats.dom.style.left = "15%";
  51. this.onBindEvent();
  52. this.inited = true;
  53. this.load();
  54. this.animate();
  55. };
  56. }
  57. load = (list, type, data) => {
  58. if (!list) return;
  59. console.log("scene: ", list, type);
  60. //axesHeloer
  61. this.clearScene();
  62. this.sceneType = type;
  63. const axesHelper = new THREE.AxesHelper(1);
  64. this.scene.add(axesHelper);
  65. this.boxManager = new BoxManager(this);
  66. this.boxManager.load(list, type);
  67. //light
  68. this.loadLight();
  69. this.player.load(data);
  70. };
  71. clearScene() {
  72. for (var i = this.scene.children.length - 1; i >= 0; i--) {
  73. let obj = this.scene.children[i];
  74. this.scene.remove(obj);
  75. }
  76. }
  77. loadLight = () => {
  78. const light = new THREE.AmbientLight(0xffffff, 1.5); // 柔和的白光
  79. this.scene.add(light);
  80. };
  81. setCamera = () => {};
  82. toHorizontal = () => {};
  83. toVertical = () => {};
  84. lockView(open) {
  85. if (open) {
  86. this.player.floorplanControls.enablePan = true;
  87. } else {
  88. this.player.floorplanControls.enablePan = false;
  89. }
  90. }
  91. onResize = (width, height) => {
  92. this.width = width !== undefined ? width : this.domElement.clientWidth;
  93. this.height = height !== undefined ? height : this.domElement.clientHeight;
  94. console.log("resize", this.width, this.height);
  95. (this.orthCamera.left = -this.width / 2),
  96. (this.orthCamera.right = this.width / 2),
  97. (this.orthCamera.bottom = -this.height / 2),
  98. (this.orthCamera.top = this.height / 2),
  99. this.orthCamera.updateProjectionMatrix();
  100. this.renderer.setSize(this.width, this.height);
  101. };
  102. render = () => {
  103. if (this.player) {
  104. this.player.update();
  105. this.renderer.render(this.scene, this.orthCamera);
  106. }
  107. };
  108. animate = () => {
  109. stats.begin();
  110. this.render();
  111. stats.end();
  112. requestAnimationFrame(this.animate);
  113. };
  114. onBindEvent = () => {
  115. //window.addEventListener('resize', this.onResize)
  116. };
  117. }