|
@@ -1,4 +1,8 @@
|
1
|
1
|
import * as THREE from "three"
|
|
2
|
+import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
|
|
3
|
+import { Object3D } from "three";
|
|
4
|
+
|
|
5
|
+
|
2
|
6
|
|
3
|
7
|
export class FooGame {
|
4
|
8
|
|
|
@@ -9,11 +13,15 @@ export class FooGame {
|
9
|
13
|
cam_fov = 70; //视角大小
|
10
|
14
|
cam_aspect = window.innerWidth / window.innerHeight;
|
11
|
15
|
|
|
16
|
+ loader: GLTFLoader;
|
|
17
|
+
|
|
18
|
+ updateMap: Object;
|
|
19
|
+
|
12
|
20
|
constructor() {
|
13
|
21
|
this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
|
14
|
|
- this.camera.position.x = 0;
|
15
|
|
- this.camera.position.y = 1;
|
16
|
|
- this.camera.position.z = 1;
|
|
22
|
+ this.camera.position.x = 3;
|
|
23
|
+ this.camera.position.y = 3;
|
|
24
|
+ this.camera.position.z = 3;
|
17
|
25
|
|
18
|
26
|
this.scene = new THREE.Scene();
|
19
|
27
|
|
|
@@ -22,13 +30,79 @@ export class FooGame {
|
22
|
30
|
|
23
|
31
|
document.body.appendChild(this.renderer.domElement);
|
24
|
32
|
|
|
33
|
+ this.loader = new GLTFLoader();
|
|
34
|
+
|
|
35
|
+ this.updateMap = {};
|
|
36
|
+
|
25
|
37
|
this.animate();
|
|
38
|
+ //this.loadScene();
|
26
|
39
|
}
|
27
|
40
|
|
28
|
41
|
animate() {
|
29
|
|
- requestAnimationFrame(this.animate);
|
|
42
|
+ requestAnimationFrame(() => {
|
|
43
|
+ this.animate();
|
|
44
|
+ });
|
|
45
|
+ this.updateObjects(this.scene);
|
30
|
46
|
this.renderer.render(this.scene, this.camera);
|
31
|
47
|
}
|
|
48
|
+
|
|
49
|
+ private updateObjects(obj: Object3D) {
|
|
50
|
+
|
|
51
|
+ if ("update" in obj && (typeof obj["update"] == "function")) {
|
|
52
|
+ var f: Function = obj["update"];
|
|
53
|
+ f.call(obj, obj);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ if (obj.children && obj.children.length > 0) {
|
|
57
|
+ obj.children.forEach(child => {
|
|
58
|
+ this.updateObjects(child);
|
|
59
|
+ });
|
|
60
|
+ }
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ findObjectByName(name: string, parent: Object3D = this.scene) {
|
|
64
|
+ let obj = parent.getObjectByName(name);
|
|
65
|
+ if (obj) return obj;
|
|
66
|
+
|
|
67
|
+ if (parent.children && parent.children.length > 0) {
|
|
68
|
+ parent.children.forEach(child => {
|
|
69
|
+ obj = this.findObjectByName(name, child);
|
|
70
|
+ if (obj) {
|
|
71
|
+ return obj;
|
|
72
|
+ }
|
|
73
|
+ });
|
|
74
|
+ }
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ setUpdate(obj: Object3D, fn: Function) {
|
|
78
|
+ Object.defineProperty(obj, "update", {
|
|
79
|
+ writable: true,
|
|
80
|
+ value: fn
|
|
81
|
+ });
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ loadScene(callback: Function) {
|
|
85
|
+ this.loader.load("./scenes/foo.gltf", gltf => {
|
|
86
|
+ this.scene.add(gltf.scene);
|
|
87
|
+ let box = this.scene.getObjectByName("Box");
|
|
88
|
+ box && this.camera.lookAt(box.position);
|
|
89
|
+ callback();
|
|
90
|
+ });
|
|
91
|
+ }
|
32
|
92
|
}
|
33
|
93
|
|
34
|
|
-new FooGame();
|
|
94
|
+let game = new FooGame();
|
|
95
|
+
|
|
96
|
+game.loadScene(() => {
|
|
97
|
+ let box = game.scene.getObjectByName("Box");
|
|
98
|
+ if (box) {
|
|
99
|
+ game.setUpdate(box, (obj: Object3D) => {
|
|
100
|
+ //game.camera.rotateOnAxis(obj.position, 0.01)
|
|
101
|
+ obj.position.x+= performance.now()*0.000001;
|
|
102
|
+ obj.position.z+= performance.now()*0.000001;
|
|
103
|
+ obj.rotateY(0.01);
|
|
104
|
+ game.camera.lookAt(obj.position)
|
|
105
|
+ });
|
|
106
|
+ }
|
|
107
|
+})
|
|
108
|
+
|