Scene.js 3.3 KB

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