Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import * as THREE from "three"
  2. import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
  3. import { Object3D, Mesh, ShaderMaterial, IcosahedronGeometry, TextureLoader, MeshStandardMaterial, Scene, HemisphereLight, Color, DirectionalLight, Vector3, AmbientLight, PlaneBufferGeometry, MeshPhongMaterial } from "three";
  4. import { GameController } from "./controller"
  5. import { VaringBox } from "./shader_test"
  6. import { GameUtils } from './utils';
  7. export class FooGame {
  8. scene: THREE.Scene;
  9. camera: THREE.Camera;
  10. renderer: THREE.WebGLRenderer;
  11. light: THREE.Light;
  12. cam_fov = 70; //视角大小
  13. cam_aspect = window.innerWidth / window.innerHeight;
  14. gltfLoader: GLTFLoader;
  15. textureLoader: TextureLoader;
  16. updateMap: Object;
  17. controller: GameController;
  18. constructor() {
  19. this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 1, 1000)
  20. this.camera.position.x = -1;
  21. this.camera.position.y = 2;
  22. this.camera.position.z = 1;
  23. this.scene = new THREE.Scene();
  24. this.light = new HemisphereLight(new Color(0XFFFFFF), new Color(0x000000), 1);
  25. this.light.position.set(0, 15, 0);
  26. this.scene.add(new AmbientLight(new Color(0xffffff), 1));
  27. this.scene.add(this.light);
  28. this.renderer = new THREE.WebGLRenderer();
  29. this.renderer.setSize(window.innerWidth, window.innerHeight);
  30. document.body.appendChild(this.renderer.domElement);
  31. this.gltfLoader = new GLTFLoader();
  32. this.updateMap = {};
  33. this.controller = new GameController(document.body);
  34. this.controller.bindMoveKeys(this.camera);
  35. this.controller.bindMouseLookAt(this.camera, this.scene);
  36. this.textureLoader = new TextureLoader();
  37. this.animate();
  38. }
  39. animate() {
  40. requestAnimationFrame(() => {
  41. this.animate();
  42. });
  43. this.updateObjects(this.scene);
  44. this.renderer.render(this.scene, this.camera);
  45. }
  46. private updateObjects(obj: Object3D) {
  47. if ("update" in obj && (typeof obj["update"] == "function")) {
  48. var f: Function = obj["update"];
  49. f.call(obj, obj);
  50. }
  51. if (obj.children && obj.children.length > 0) {
  52. obj.children.forEach(child => {
  53. this.updateObjects(child);
  54. });
  55. }
  56. }
  57. findObjectByName(name: string, parent: Object3D = this.scene) {
  58. let obj = parent.getObjectByName(name);
  59. if (obj) return obj;
  60. if (parent.children && parent.children.length > 0) {
  61. parent.children.forEach(child => {
  62. obj = this.findObjectByName(name, child);
  63. if (obj) {
  64. return obj;
  65. }
  66. });
  67. }
  68. }
  69. static setUpdate(obj: Object3D, fn: Function) {
  70. Object.defineProperty(obj, "update", {
  71. writable: true,
  72. value: fn
  73. });
  74. }
  75. /**
  76. * 加载场景
  77. * @param callback
  78. */
  79. loadScene(callback: Function) {
  80. this.gltfLoader.load("./scenes/campus/Unity2GLTF.gltf", gltf => {
  81. console.log(gltf);
  82. this.scene.add(gltf.scene);
  83. this.scene.background = new THREE.Color('rgb(191,196,234)');
  84. callback();
  85. });
  86. }
  87. }
  88. let game = new FooGame();
  89. game.loadScene(() => {
  90. /*let mat = GameUtils.makeRandomObjects(game.scene, 500);
  91. FooGame.setUpdate(game.scene, (obj: Object3D) => {
  92. mat.uniforms.time.value = performance.now() / 500;
  93. });
  94. */
  95. let geoGround = new PlaneBufferGeometry(200, 200, 100, 200);
  96. let matGround = new MeshPhongMaterial({
  97. color: "rgb(50,50,50)"
  98. });
  99. let ground = new Mesh(geoGround, matGround);
  100. ground.rotation.x = -Math.PI / 2;
  101. ground.position.y = -2;
  102. game.scene.add(ground);
  103. game.camera.lookAt(new THREE.Vector3(0, 0, 0));
  104. /**
  105. * 设置天空盒子
  106. */
  107. let skybox = game.findObjectByName("Sphere");
  108. if (skybox instanceof Mesh) {
  109. let mesh: Mesh = skybox;
  110. if (mesh.material instanceof MeshStandardMaterial) {
  111. let mat: MeshStandardMaterial = mesh.material;
  112. mat.side = THREE.BackSide;
  113. }
  114. }
  115. })