Scene.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. stats.dom.style.display = "none";
  53. this.onBindEvent();
  54. this.inited = true;
  55. this.load();
  56. this.animate();
  57. };
  58. }
  59. load = (list, type, data) => {
  60. if (!list) return;
  61. console.log("scene: ", list, type, data);
  62. //axesHeloer
  63. this.clearScene();
  64. this.sceneType = type;
  65. // const axesHelper = new THREE.AxesHelper(1);
  66. // this.scene.add(axesHelper);
  67. this.boxManager = new BoxManager(this);
  68. this.boxManager.load(list, type);
  69. //light
  70. this.loadLight();
  71. this.player.load(type, data || []);
  72. };
  73. clearScene() {
  74. for (var i = this.scene.children.length - 1; i >= 0; i--) {
  75. let obj = this.scene.children[i];
  76. this.scene.remove(obj);
  77. }
  78. }
  79. clearDrawScene() {
  80. for (var i = this.scene.children.length - 1; i >= 0; i--) {
  81. let obj = this.scene.children[i];
  82. if (
  83. String(obj.name).includes("marker_") ||
  84. String(obj.name).includes("line_") ||
  85. String(obj.name).includes("line_point_") ||
  86. String(obj.name).includes("circle_")
  87. ) {
  88. this.scene.remove(obj);
  89. }
  90. }
  91. }
  92. deleteItemById(uuid, type) {
  93. for (var i = this.scene.children.length - 1; i >= 0; i--) {
  94. let obj = this.scene.children[i];
  95. if (obj.uuid === uuid) {
  96. this.scene.remove(obj);
  97. }
  98. }
  99. }
  100. loadLight = () => {
  101. const light = new THREE.AmbientLight(0xffffff, 1.5); // 柔和的白光
  102. this.scene.add(light);
  103. };
  104. setCamera = () => {};
  105. toHorizontal = () => {};
  106. toVertical = () => {};
  107. lockView(open) {
  108. if (open) {
  109. this.player.floorplanControls.enablePan = true;
  110. } else {
  111. this.player.floorplanControls.enablePan = false;
  112. }
  113. }
  114. onResize = (width, height) => {
  115. this.width = width !== undefined ? width : this.domElement.clientWidth;
  116. this.height = height !== undefined ? height : this.domElement.clientHeight;
  117. console.log("resize", this.width, this.height);
  118. (this.orthCamera.left = -this.width / 2),
  119. (this.orthCamera.right = this.width / 2),
  120. (this.orthCamera.bottom = -this.height / 2),
  121. (this.orthCamera.top = this.height / 2),
  122. this.orthCamera.updateProjectionMatrix();
  123. this.renderer.setSize(this.width, this.height);
  124. };
  125. render = () => {
  126. if (this.player) {
  127. this.player.update();
  128. this.renderer.render(this.scene, this.orthCamera);
  129. }
  130. };
  131. animate = () => {
  132. stats.begin();
  133. this.render();
  134. stats.end();
  135. requestAnimationFrame(this.animate);
  136. };
  137. editing(item) {
  138. this.player.editing(item);
  139. }
  140. onBindEvent = () => {
  141. //window.addEventListener('resize', this.onResize)
  142. };
  143. }