xzz пре 5 година
родитељ
комит
69a1ed99bf
4 измењених фајлова са 706 додато и 29 уклоњено
  1. 597
    0
      public/scenes/foo2.gltf
  2. 46
    0
      public/scenes/model.json
  3. 18
    17
      src/index.ts
  4. 45
    12
      src/navigator.ts

+ 597
- 0
public/scenes/foo2.gltf
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку


+ 46
- 0
public/scenes/model.json Прегледај датотеку

@@ -0,0 +1,46 @@
1
+{
2
+	"metadata": {
3
+		"version": 4.5,
4
+		"type": "Object",
5
+		"generator": "Object3D.toJSON"
6
+	},
7
+	"geometries": [
8
+		{
9
+			"uuid": "7A6FECB8-F13F-4A7D-A536-F5965EA50825",
10
+			"type": "TorusBufferGeometry",
11
+			"radius": 1,
12
+			"tube": 0.4,
13
+			"radialSegments": 8,
14
+			"tubularSegments": 6,
15
+			"arc": 6.283185
16
+		}],
17
+	"materials": [
18
+		{
19
+			"uuid": "8ABC72E1-6354-44F6-AA61-F68253ED93DE",
20
+			"type": "MeshStandardMaterial",
21
+			"color": 16777215,
22
+			"roughness": 0.5,
23
+			"metalness": 0.5,
24
+			"emissive": 32832,
25
+			"depthFunc": 3,
26
+			"depthTest": true,
27
+			"depthWrite": true,
28
+			"stencilWrite": false,
29
+			"stencilWriteMask": 255,
30
+			"stencilFunc": 519,
31
+			"stencilRef": 0,
32
+			"stencilFuncMask": 255,
33
+			"stencilFail": 7680,
34
+			"stencilZFail": 7680,
35
+			"stencilZPass": 7680
36
+		}],
37
+	"object": {
38
+		"uuid": "7C61FBE2-0D96-493A-AD3F-BF6EA21C4305",
39
+		"type": "Mesh",
40
+		"name": "abc",
41
+		"layers": 1,
42
+		"matrix": [1,0,0,0,0,1,0,0,0,0,1,0,0,0,-1.862864,1],
43
+		"geometry": "7A6FECB8-F13F-4A7D-A536-F5965EA50825",
44
+		"material": "8ABC72E1-6354-44F6-AA61-F68253ED93DE"
45
+	}
46
+}

+ 18
- 17
src/index.ts Прегледај датотеку

@@ -1,6 +1,6 @@
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, Vector3 } from "three";
3
+import { Object3D, Mesh, TextureLoader, HemisphereLight, Color, AmbientLight, PlaneBufferGeometry, MeshPhongMaterial, Vector3, Scene } from "three";
4 4
 
5 5
 import { GameController } from "./controller"
6 6
 import { OrbitNavigator, KeyPoint } from "./navigator";
@@ -27,6 +27,7 @@ export class ReeGame {
27 27
         this.camera.position.z = 1;
28 28
 
29 29
         this.scene = new THREE.Scene();
30
+        this.scene.add(this.camera);
30 31
 
31 32
         this.light = new HemisphereLight(new Color(0XFFFFFF), new Color(0x000000), 1);
32 33
         this.light.position.set(0, 15, 0);
@@ -57,9 +58,8 @@ export class ReeGame {
57 58
 
58 59
     private updateObjects(obj: Object3D) {
59 60
 
60
-        if ("update" in obj && (typeof obj["update"] == "function")) {
61
-            var f: Function = obj["update"];
62
-            f.call(obj, obj);
61
+        if (obj.userData.update && (typeof obj.userData.update == "function")) {
62
+            obj.userData.update.call(obj, obj);
63 63
         }
64 64
 
65 65
         if (obj.children && obj.children.length > 0) {
@@ -91,18 +91,15 @@ export class ReeGame {
91 91
 
92 92
     static setUpdate(obj: Object3D, fn: Function) {
93 93
         let v = fn;
94
-        if ("update" in obj && (typeof obj["update"] == "function")) {
95
-            var f: Function = obj["update"];
94
+
95
+        if (obj.userData.update && (typeof obj.userData.update == "function")) {
96
+            var f: Function = obj.userData.update;
96 97
             v = () => {
97 98
                 f.call(obj, obj);
98 99
                 fn.call(obj, obj);
99 100
             };
100 101
         }
101
-
102
-        Object.defineProperty(obj, "update", {
103
-            writable: true,
104
-            value: v
105
-        });
102
+        obj.userData.update = v;
106 103
     }
107 104
 
108 105
     /**
@@ -131,12 +128,16 @@ export class ReeGame {
131 128
 
132 129
 let game = new ReeGame();
133 130
 game.loadScene(() => {
131
+
134 132
     let points: KeyPoint[] = [];
135
-    points.push(new KeyPoint(new Vector3(0, 0, 0)));
136
-    points.push(new KeyPoint(new Vector3(0, 4, 0)));
137
-    points.push(new KeyPoint(new Vector3(0, 0, 4)));
138
-    points.push(new KeyPoint(new Vector3(4, 0, 0)));
139
-    let orbit: OrbitNavigator = new OrbitNavigator(game.camera, points);
140
-    orbit.start();
133
+    points.push(new KeyPoint(new Vector3(2, 2, 2), new Vector3(0, 2, 0)));
134
+    points.push(new KeyPoint(new Vector3(-20, 15, -20), new Vector3(0, 2, 0)));
135
+    points.push(new KeyPoint(new Vector3(-20, 15, 20), new Vector3(0, 2, 0)));
136
+    points.push(new KeyPoint(new Vector3(20, 15, 20), new Vector3(0, 2, 0)));
137
+    points.push(new KeyPoint(new Vector3(20, 20, -20),new Vector3(0, 2, 0)));
138
+    points.push(new KeyPoint(new Vector3(2, 2, 2)));
139
+    let nav: OrbitNavigator = new OrbitNavigator(game.camera, points, 200, true);
140
+
141
+    nav.start();
141 142
 });
142 143
 

+ 45
- 12
src/navigator.ts Прегледај датотеку

@@ -13,50 +13,83 @@ export class KeyPoint {
13 13
 }
14 14
 
15 15
 export class OrbitNavigator {
16
+    private speed: number;
17
+    private loop: boolean;
18
+    private translateDelta: number;
16 19
     private target: Object3D;
17 20
     private points: KeyPoint[];
18
-    private idxP0 = 0; // 当前位置索引
21
+    private idxMax: number;
22
+    private idxP0 = 0; // 当前下一个位置索引
19 23
     private p0: Vector3 = new Vector3(); //上一个关键点
20 24
     private p1: Vector3 = new Vector3(); //下一个关键点
25
+    private lookAt: Vector3 | undefined;
21 26
     private direction: Vector3 = new Vector3(); //当前前进方向
22 27
 
23 28
     private vec: Vector3 = new Vector3();
24 29
 
25
-    constructor(target: Object3D, points: KeyPoint[]) {
30
+    constructor(target: Object3D, points: KeyPoint[], speed: number, loop: boolean = true) {
26 31
         this.target = target;
27 32
         this.points = points || [];
33
+        this.idxMax = points.length - 2;
34
+
35
+        this.speed = speed;
36
+        this.translateDelta = speed * 0.001;
37
+        this.loop = loop;
28 38
     }
29 39
 
30 40
     start() {
31 41
         this.idxP0 = 0;
32 42
         this.calcDirection();
43
+
33 44
         this.target.position.set(this.p0.x, this.p0.y, this.p0.z);
34
-        ReeGame.setUpdate(this.target, this.update);
45
+        ReeGame.setUpdate(this.target, (obj: Object3D) => {
46
+            this.update(obj);
47
+        });
35 48
         console.log("start");
36
-        
49
+
37 50
     }
38 51
 
52
+    restart() {
53
+        this.idxP0 = 0;
54
+        this.calcDirection();
55
+
56
+        this.target.position.set(this.p0.x, this.p0.y, this.p0.z);
57
+    }
39 58
     private update(obj: Object3D) {
40
-        console.log(this.direction);
41 59
 
42
-        if (obj.position.distanceTo(this.p1) <= 0.01) {
60
+        if (obj.position.distanceTo(this.p1) <= 0.1) {
43 61
             this.idxP0++;
44 62
             this.calcDirection();
45 63
         }
46
-        console.log(this.direction);
47 64
 
48
-        obj.position.addScaledVector(this.direction, 0.1);
65
+        if(this.lookAt){
66
+            obj.lookAt(this.lookAt);
67
+        }
68
+        obj.position.addScaledVector(this.direction, this.translateDelta);
49 69
     }
50 70
 
51 71
     private calcDirection() {
52
-        if (this.points.length < 2)
53
-            return;
72
+        if (this.idxP0 > this.idxMax) {
73
+            if (this.loop) {
74
+                this.restart();
75
+            }
76
+            else {
77
+                this.direction = new Vector3(0, 0, 0);
78
+                return;
79
+            }
80
+        }
81
+
54 82
         let p0 = this.points[this.idxP0].point;
55 83
         let p1 = this.points[this.idxP0 + 1].point;
84
+        let lookAt = this.points[this.idxP0].lookAt;
85
+
86
+        this.lookAt = lookAt && lookAt instanceof Vector3 ? lookAt : lookAt?.position;
56 87
         this.p0 = p0 instanceof Vector3 ? p0 : p0.position;
57 88
         this.p1 = p1 instanceof Vector3 ? p1 : p1.position;
58
-        this.vec = this.vec.subVectors(this.p0, this.p1);
59
-        this.direction = this.vec;
89
+        this.vec = this.vec.subVectors(this.p1, this.p0);
90
+        this.direction = this.vec.normalize();
91
+
92
+
60 93
     }
61 94
 
62 95
 }

Loading…
Откажи
Сачувај