hsiao 5 лет назад
Родитель
Сommit
becf4c61fe
2 измененных файлов: 60 добавлений и 6 удалений
  1. 19
    5
      src/index.ts
  2. 41
    1
      src/navigator.ts

+ 19
- 5
src/index.ts Просмотреть файл

@@ -1,8 +1,9 @@
1 1
 import * as THREE from "three"
2 2
 import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
3
-import { Object3D, Mesh, TextureLoader, HemisphereLight, Color, AmbientLight, PlaneBufferGeometry, MeshPhongMaterial } from "three";
3
+import { Object3D, Mesh, TextureLoader, HemisphereLight, Color, AmbientLight, PlaneBufferGeometry, MeshPhongMaterial, Vector3 } from "three";
4 4
 
5 5
 import { GameController } from "./controller"
6
+import { OrbitNavigator, KeyPoint } from "./navigator";
6 7
 
7 8
 export class ReeGame {
8 9
 
@@ -90,9 +91,18 @@ export class ReeGame {
90 91
     }
91 92
 
92 93
     static setUpdate(obj: Object3D, fn: Function) {
94
+        let v = fn;
95
+        if ("update" in obj && (typeof obj["update"] == "function")) {
96
+            var f: Function = obj["update"];
97
+            v = () => {
98
+                f.call(obj, obj);
99
+                fn.call(obj, obj);
100
+            };
101
+        }
102
+
93 103
         Object.defineProperty(obj, "update", {
94 104
             writable: true,
95
-            value: fn
105
+            value: v
96 106
         });
97 107
     }
98 108
 
@@ -103,8 +113,6 @@ export class ReeGame {
103 113
     loadScene(callback: Function) {
104 114
 
105 115
         this.gltfLoader.load("./scenes/campus/Unity2GLTF.gltf", gltf => {
106
-            console.log(gltf);
107
-
108 116
             this.scene.add(gltf.scene);
109 117
             this.scene.background = new THREE.Color('rgb(191,196,234)');
110 118
             //添加一个地板
@@ -124,6 +132,12 @@ export class ReeGame {
124 132
 
125 133
 let game = new ReeGame();
126 134
 game.loadScene(() => {
127
-
135
+    let points: KeyPoint[] = [];
136
+    points.push(new KeyPoint(new Vector3(0, 0, 0)));
137
+    points.push(new KeyPoint(new Vector3(0, 4, 0)));
138
+    points.push(new KeyPoint(new Vector3(0, 0, 4)));
139
+    points.push(new KeyPoint(new Vector3(4, 0, 0)));
140
+    let orbit: OrbitNavigator = new OrbitNavigator(game.camera, points);
141
+    orbit.start();
128 142
 });
129 143
 

+ 41
- 1
src/navigator.ts Просмотреть файл

@@ -1,14 +1,26 @@
1 1
 
2 2
 import { Object3D, Vector3 } from 'three';
3
+import { ReeGame } from './index';
3 4
 
4 5
 export class KeyPoint {
5 6
     point: Vector3 | Object3D = new Vector3();
6
-    lookAt: Vector3 | Object3D = new Vector3();
7
+    lookAt?: Vector3 | Object3D;
8
+
9
+    constructor(p: Vector3 | Object3D, look?: Vector3 | Object3D) {
10
+        this.point = p;
11
+        this.lookAt = look;
12
+    }
7 13
 }
8 14
 
9 15
 export class OrbitNavigator {
10 16
     private target: Object3D;
11 17
     private points: KeyPoint[];
18
+    private idxP0 = 0; // 当前位置索引
19
+    private p0: Vector3 = new Vector3(); //上一个关键点
20
+    private p1: Vector3 = new Vector3(); //下一个关键点
21
+    private direction: Vector3 = new Vector3(); //当前前进方向
22
+
23
+    private vec: Vector3 = new Vector3();
12 24
 
13 25
     constructor(target: Object3D, points: KeyPoint[]) {
14 26
         this.target = target;
@@ -16,7 +28,35 @@ export class OrbitNavigator {
16 28
     }
17 29
 
18 30
     start() {
31
+        this.idxP0 = 0;
32
+        this.calcDirection();
33
+        this.target.position.set(this.p0.x, this.p0.y, this.p0.z);
34
+        ReeGame.setUpdate(this.target, this.update);
35
+        console.log("start");
36
+        
37
+    }
38
+
39
+    private update(obj: Object3D) {
40
+        console.log(this.direction);
41
+
42
+        if (obj.position.distanceTo(this.p1) <= 0.01) {
43
+            this.idxP0++;
44
+            this.calcDirection();
45
+        }
46
+        console.log(this.direction);
47
+
48
+        obj.position.addScaledVector(this.direction, 0.1);
49
+    }
19 50
 
51
+    private calcDirection() {
52
+        if (this.points.length < 2)
53
+            return;
54
+        let p0 = this.points[this.idxP0].point;
55
+        let p1 = this.points[this.idxP0 + 1].point;
56
+        this.p0 = p0 instanceof Vector3 ? p0 : p0.position;
57
+        this.p1 = p1 instanceof Vector3 ? p1 : p1.position;
58
+        this.vec = this.vec.subVectors(this.p0, this.p1);
59
+        this.direction = this.vec;
20 60
     }
21 61
 
22 62
 }

Загрузка…
Отмена
Сохранить