|
@@ -1,77 +1,65 @@
|
1
|
1
|
import * as THREE from "three"
|
2
|
2
|
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
|
3
|
|
-import { Object3D, Mesh, ShaderMaterial } 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
|
+import { OrbitNavigator, KeyPoint } from "./navigator";
|
6
|
7
|
|
7
|
|
-
|
8
|
|
-export class FooGame {
|
|
8
|
+export class ReeGame {
|
9
|
9
|
|
10
|
10
|
scene: THREE.Scene;
|
11
|
11
|
camera: THREE.Camera;
|
12
|
|
- renderer: THREE.WebGLRenderer;
|
13
|
|
-
|
14
|
|
- cam_fov = 70; //视角大小
|
15
|
|
- cam_aspect = window.innerWidth / window.innerHeight;
|
16
|
|
-
|
17
|
|
- loader: GLTFLoader;
|
|
12
|
+ private renderer: THREE.WebGLRenderer;
|
|
13
|
+ private light: THREE.Light;
|
|
14
|
+ private cam_fov = 70; //视角大小
|
|
15
|
+ private cam_aspect = window.innerWidth / window.innerHeight;
|
18
|
16
|
|
19
|
|
- updateMap: Object;
|
|
17
|
+ gltfLoader: GLTFLoader;
|
|
18
|
+ textureLoader: TextureLoader;
|
20
|
19
|
|
21
|
|
- controller: GameController;
|
|
20
|
+ private controller: GameController;
|
|
21
|
+ private renderFlag: boolean = true;
|
22
|
22
|
|
23
|
23
|
constructor() {
|
24
|
|
- this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
|
25
|
|
- this.camera.position.x = 0;
|
26
|
|
- this.camera.position.y = 3;
|
27
|
|
- this.camera.position.z = 5;
|
|
24
|
+ this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 1, 1000)
|
|
25
|
+ this.camera.position.x = -1;
|
|
26
|
+ this.camera.position.y = 2;
|
|
27
|
+ this.camera.position.z = 1;
|
28
|
28
|
|
29
|
29
|
this.scene = new THREE.Scene();
|
|
30
|
+ this.scene.add(this.camera);
|
30
|
31
|
|
|
32
|
+ this.light = new HemisphereLight(new Color(0XFFFFFF), new Color(0x000000), 1);
|
|
33
|
+ this.light.position.set(0, 15, 0);
|
|
34
|
+
|
|
35
|
+ this.scene.add(new AmbientLight(new Color(0xffffff), 1));
|
|
36
|
+ this.scene.add(this.light);
|
31
|
37
|
this.renderer = new THREE.WebGLRenderer();
|
32
|
38
|
this.renderer.setSize(window.innerWidth, window.innerHeight);
|
33
|
39
|
|
34
|
40
|
document.body.appendChild(this.renderer.domElement);
|
35
|
41
|
|
36
|
|
- this.loader = new GLTFLoader();
|
37
|
|
-
|
38
|
|
- this.updateMap = {};
|
39
|
|
-
|
40
|
|
- this.controller = new GameController(document.body);
|
41
|
|
-
|
|
42
|
+ this.gltfLoader = new GLTFLoader();
|
|
43
|
+ let mask = document.getElementById("mask") || new HTMLElement();
|
|
44
|
+ this.controller = new GameController(document.body, mask, this);
|
|
45
|
+ this.textureLoader = new TextureLoader();
|
42
|
46
|
this.animate();
|
43
|
|
- //this.loadScene();
|
44
|
|
- }
|
45
|
|
-
|
46
|
|
- bindKeyAction(box: Object3D) {
|
47
|
|
- this.controller.onKey_A(() => {
|
48
|
|
- if (box) box.position.x -= 0.1;
|
49
|
|
- });
|
50
|
|
- this.controller.onKey_D(() => {
|
51
|
|
- if (box) box.position.x += 0.1;
|
52
|
|
- });
|
53
|
|
- this.controller.onKey_S(() => {
|
54
|
|
- if (box) box.position.z += 0.1;
|
55
|
|
- });
|
56
|
|
- this.controller.onKey_W(() => {
|
57
|
|
- if (box) box.position.z -= 0.1;
|
58
|
|
- });
|
59
|
|
-
|
60
|
47
|
}
|
61
|
48
|
|
62
|
|
- animate() {
|
|
49
|
+ private animate() {
|
63
|
50
|
requestAnimationFrame(() => {
|
64
|
51
|
this.animate();
|
65
|
52
|
});
|
|
53
|
+ if (this.renderFlag) {
|
66
|
54
|
this.updateObjects(this.scene);
|
67
|
55
|
this.renderer.render(this.scene, this.camera);
|
68
|
56
|
}
|
|
57
|
+ }
|
69
|
58
|
|
70
|
59
|
private updateObjects(obj: Object3D) {
|
71
|
60
|
|
72
|
|
- if ("update" in obj && (typeof obj["update"] == "function")) {
|
73
|
|
- var f: Function = obj["update"];
|
74
|
|
- f.call(obj, obj);
|
|
61
|
+ if (obj.userData.update && (typeof obj.userData.update == "function")) {
|
|
62
|
+ obj.userData.update.call(obj, obj);
|
75
|
63
|
}
|
76
|
64
|
|
77
|
65
|
if (obj.children && obj.children.length > 0) {
|
|
@@ -81,6 +69,12 @@ export class FooGame {
|
81
|
69
|
}
|
82
|
70
|
}
|
83
|
71
|
|
|
72
|
+ stop() {
|
|
73
|
+ this.renderFlag = false;
|
|
74
|
+ }
|
|
75
|
+ start() {
|
|
76
|
+ this.renderFlag = true;
|
|
77
|
+ }
|
84
|
78
|
findObjectByName(name: string, parent: Object3D = this.scene) {
|
85
|
79
|
let obj = parent.getObjectByName(name);
|
86
|
80
|
if (obj) return obj;
|
|
@@ -95,73 +89,55 @@ export class FooGame {
|
95
|
89
|
}
|
96
|
90
|
}
|
97
|
91
|
|
98
|
|
- setUpdate(obj: Object3D, fn: Function) {
|
99
|
|
- Object.defineProperty(obj, "update", {
|
100
|
|
- writable: true,
|
101
|
|
- value: fn
|
102
|
|
- });
|
|
92
|
+ static setUpdate(obj: Object3D, fn: Function) {
|
|
93
|
+ let v = fn;
|
|
94
|
+
|
|
95
|
+ if (obj.userData.update && (typeof obj.userData.update == "function")) {
|
|
96
|
+ var f: Function = obj.userData.update;
|
|
97
|
+ v = () => {
|
|
98
|
+ f.call(obj, obj);
|
|
99
|
+ fn.call(obj, obj);
|
|
100
|
+ };
|
|
101
|
+ }
|
|
102
|
+ obj.userData.update = v;
|
103
|
103
|
}
|
104
|
104
|
|
|
105
|
+ /**
|
|
106
|
+ * 加载场景
|
|
107
|
+ * @param callback
|
|
108
|
+ */
|
105
|
109
|
loadScene(callback: Function) {
|
106
|
|
- this.loader.load("./scenes/foo.gltf", gltf => {
|
|
110
|
+
|
|
111
|
+ this.gltfLoader.load("./scenes/campus/Unity2GLTF.gltf", gltf => {
|
107
|
112
|
this.scene.add(gltf.scene);
|
108
|
|
- let box = this.scene.getObjectByName("Box");
|
109
|
|
- box && this.camera.lookAt(box.position);
|
|
113
|
+ this.scene.background = new THREE.Color('rgb(191,196,234)');
|
|
114
|
+ //添加一个地板
|
|
115
|
+ let geoGround = new PlaneBufferGeometry(200, 200, 100, 200);
|
|
116
|
+ let matGround = new MeshPhongMaterial({
|
|
117
|
+ color: "rgb(100,100,100)"
|
|
118
|
+ });
|
|
119
|
+ let ground = new Mesh(geoGround, matGround);
|
|
120
|
+ ground.rotation.x = -Math.PI / 2;
|
|
121
|
+ ground.position.y = -2;
|
|
122
|
+ this.scene.add(ground);
|
|
123
|
+
|
110
|
124
|
callback();
|
111
|
125
|
});
|
112
|
126
|
}
|
113
|
127
|
}
|
114
|
128
|
|
115
|
|
-let game = new FooGame();
|
116
|
|
-
|
117
|
|
-let vertexShader = `
|
118
|
|
-uniform float time;
|
119
|
|
-varying vec3 vPos;
|
120
|
|
-void main() {
|
121
|
|
- vPos=position;
|
122
|
|
- vPos.x += sin(time * vPos.z) * 2.0;
|
123
|
|
- vPos.y += cos(time * vPos.z) * 2.0;
|
124
|
|
- //vPos.z += tan(time * vPos.z) * 4.0;
|
125
|
|
- gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
|
126
|
|
-}
|
127
|
|
-`;
|
128
|
|
-
|
129
|
|
-let fragmentShader = `
|
130
|
|
-varying vec3 vPos;
|
131
|
|
-void main() {
|
132
|
|
- gl_FragColor = vec4( vPos, 1.0 );
|
133
|
|
-}
|
134
|
|
-`;
|
135
|
|
-
|
|
129
|
+let game = new ReeGame();
|
136
|
130
|
game.loadScene(() => {
|
137
|
|
- let box = game.scene.getObjectByName("Box");
|
138
|
|
- if (box) {
|
139
|
|
- game.bindKeyAction(box);
|
140
|
|
- let mat:ShaderMaterial;
|
141
|
|
- if (box instanceof Mesh) {
|
142
|
|
- let mesh: Mesh = box;
|
143
|
|
-
|
144
|
|
- mat = new ShaderMaterial({
|
145
|
|
- uniforms: {
|
146
|
|
- time: {
|
147
|
|
- value: 0
|
148
|
|
- }
|
149
|
|
- },
|
150
|
|
- vertexShader: vertexShader,
|
151
|
|
- fragmentShader: fragmentShader
|
152
|
|
- });
|
153
|
|
- mesh.material = mat;
|
154
|
|
- }
|
155
|
|
-
|
156
|
|
- game.setUpdate(box, (obj: Object3D) => {
|
157
|
|
- //game.camera.rotateOnAxis(obj.position, 0.01)
|
158
|
|
- //obj.position.x += performance.now() * 0.000001;
|
159
|
|
- //obj.position.z += performance.now() * 0.000001;
|
160
|
|
- obj.rotateY(0.01);
|
161
|
131
|
|
162
|
|
- //game.camera.lookAt(obj.position)
|
163
|
|
- mat.uniforms.time.value = performance.now()/500;
|
164
|
|
- });
|
165
|
|
- }
|
166
|
|
-})
|
|
132
|
+ let points: KeyPoint[] = [];
|
|
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();
|
|
142
|
+});
|
167
|
143
|
|