Scene.js 4.2 KB

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