|
@@ -1,9 +1,10 @@
|
1
|
1
|
import * as THREE from "three"
|
2
|
2
|
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
|
3
|
|
-import { Object3D, Mesh, ShaderMaterial, IcosahedronGeometry } from "three";
|
|
3
|
+import { Object3D, Mesh, ShaderMaterial, IcosahedronGeometry, TextureLoader, MeshStandardMaterial, Scene, HemisphereLight, Color, DirectionalLight, Vector3 } from "three";
|
4
|
4
|
|
5
|
5
|
import { GameController } from "./controller"
|
6
|
6
|
import { VaringBox } from "./shader_test"
|
|
7
|
+import { GameUtils } from './utils';
|
7
|
8
|
|
8
|
9
|
export class FooGame {
|
9
|
10
|
|
|
@@ -11,29 +12,35 @@ export class FooGame {
|
11
|
12
|
camera: THREE.Camera;
|
12
|
13
|
renderer: THREE.WebGLRenderer;
|
13
|
14
|
|
|
15
|
+ light: THREE.Light;
|
|
16
|
+
|
14
|
17
|
cam_fov = 70; //视角大小
|
15
|
18
|
cam_aspect = window.innerWidth / window.innerHeight;
|
16
|
19
|
|
17
|
|
- loader: GLTFLoader;
|
|
20
|
+ gltfLoader: GLTFLoader;
|
|
21
|
+ textureLoader: TextureLoader;
|
18
|
22
|
|
19
|
23
|
updateMap: Object;
|
20
|
24
|
|
21
|
25
|
controller: GameController;
|
22
|
26
|
|
23
|
27
|
constructor() {
|
24
|
|
- this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
|
|
28
|
+ this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 1, 1000)
|
25
|
29
|
this.camera.position.x = -1;
|
26
|
30
|
this.camera.position.y = 1;
|
27
|
31
|
this.camera.position.z = 1;
|
28
|
32
|
|
29
|
33
|
this.scene = new THREE.Scene();
|
30
|
34
|
|
|
35
|
+ this.light = new DirectionalLight(new Color(0XFFFFFF), 0.4);
|
|
36
|
+ this.light.position.set(0, 15, 0);
|
|
37
|
+ this.scene.add(this.light);
|
31
|
38
|
this.renderer = new THREE.WebGLRenderer();
|
32
|
39
|
this.renderer.setSize(window.innerWidth, window.innerHeight);
|
33
|
40
|
|
34
|
41
|
document.body.appendChild(this.renderer.domElement);
|
35
|
42
|
|
36
|
|
- this.loader = new GLTFLoader();
|
|
43
|
+ this.gltfLoader = new GLTFLoader();
|
37
|
44
|
|
38
|
45
|
this.updateMap = {};
|
39
|
46
|
|
|
@@ -41,6 +48,8 @@ export class FooGame {
|
41
|
48
|
this.controller.bindMoveKeys(this.camera);
|
42
|
49
|
this.controller.bindMouseLookAt(this.camera, this.scene);
|
43
|
50
|
|
|
51
|
+ this.textureLoader = new TextureLoader();
|
|
52
|
+
|
44
|
53
|
this.animate();
|
45
|
54
|
}
|
46
|
55
|
|
|
@@ -52,6 +61,7 @@ export class FooGame {
|
52
|
61
|
this.renderer.render(this.scene, this.camera);
|
53
|
62
|
}
|
54
|
63
|
|
|
64
|
+
|
55
|
65
|
private updateObjects(obj: Object3D) {
|
56
|
66
|
|
57
|
67
|
if ("update" in obj && (typeof obj["update"] == "function")) {
|
|
@@ -87,11 +97,16 @@ export class FooGame {
|
87
|
97
|
});
|
88
|
98
|
}
|
89
|
99
|
|
|
100
|
+ /**
|
|
101
|
+ * 加载场景
|
|
102
|
+ * @param callback
|
|
103
|
+ */
|
90
|
104
|
loadScene(callback: Function) {
|
91
|
|
- this.loader.load("./scenes/foo.gltf", gltf => {
|
|
105
|
+
|
|
106
|
+ this.gltfLoader.load("./scenes/foo.gltf", gltf => {
|
92
|
107
|
this.scene.add(gltf.scene);
|
93
|
|
- let box = this.scene.getObjectByName("Box");
|
94
|
|
- box && this.camera.lookAt(box.position);
|
|
108
|
+ //this.scene.background = new THREE.Color(0xffffff);
|
|
109
|
+
|
95
|
110
|
callback();
|
96
|
111
|
});
|
97
|
112
|
}
|
|
@@ -99,32 +114,31 @@ export class FooGame {
|
99
|
114
|
|
100
|
115
|
let game = new FooGame();
|
101
|
116
|
|
102
|
|
-
|
103
|
|
-
|
104
|
117
|
game.loadScene(() => {
|
105
|
|
- let box = game.scene.getObjectByName("Box");
|
106
|
|
- let vBox = new VaringBox();
|
|
118
|
+
|
|
119
|
+ let mat = GameUtils.makeRandomObjects(game.scene, 500);
|
|
120
|
+ FooGame.setUpdate(game.scene, (obj: Object3D) => {
|
|
121
|
+ mat.uniforms.time.value = performance.now() / 500;
|
|
122
|
+ });
|
|
123
|
+
|
107
|
124
|
game.camera.lookAt(new THREE.Vector3(0, 0, 0));
|
108
|
|
- if (box) {
|
109
|
|
-
|
110
|
|
- FooGame.setUpdate(box, (obj: Mesh) => {
|
111
|
|
- //obj.rotateY(0.01);
|
112
|
|
- obj.material = vBox.material;
|
113
|
|
- if (obj.material instanceof ShaderMaterial) {
|
114
|
|
- let mat: ShaderMaterial = obj.material;
|
115
|
|
- // mat.uniforms.time.value = performance.now() / 500;
|
116
|
|
- }
|
117
|
|
- });
|
118
|
|
- }
|
119
|
125
|
|
120
|
|
- let geo = new IcosahedronGeometry(1, 4);
|
121
|
|
- let box2 = new THREE.Mesh(geo, vBox.material);
|
122
|
|
- box2.position.x = 2;
|
123
|
|
- box2.position.z = 1;
|
124
|
|
- box2.position.y = 2;
|
125
|
126
|
|
126
|
|
- game.scene.add(box2);
|
|
127
|
+ /**
|
|
128
|
+ * 设置天空盒子
|
|
129
|
+ */
|
|
130
|
+ let skybox = game.findObjectByName("Sphere");
|
|
131
|
+ if(skybox instanceof Mesh){
|
|
132
|
+ let mesh:Mesh = skybox;
|
|
133
|
+ if(mesh.material instanceof MeshStandardMaterial){
|
|
134
|
+ let mat:MeshStandardMaterial = mesh.material;
|
|
135
|
+ mat.side = THREE.BackSide;
|
|
136
|
+ }
|
|
137
|
+ }
|
127
|
138
|
|
|
139
|
+ console.log(game.camera.matrix);
|
|
140
|
+ console.log(new Vector3().setFromMatrixPosition(game.camera.matrix));
|
|
141
|
+ console.log(new Vector3().setFromMatrixColumn(game.camera.matrix, 0));
|
128
|
142
|
|
129
|
143
|
})
|
130
|
144
|
|