|
@@ -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, TextureLoader, MeshStandardMaterial, Scene } 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,6 +12,8 @@ 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
|
|
|
@@ -22,13 +25,16 @@ export class FooGame {
|
22
|
25
|
controller: GameController;
|
23
|
26
|
|
24
|
27
|
constructor() {
|
25
|
|
- 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)
|
26
|
29
|
this.camera.position.x = -1;
|
27
|
30
|
this.camera.position.y = 1;
|
28
|
31
|
this.camera.position.z = 1;
|
29
|
32
|
|
30
|
33
|
this.scene = new THREE.Scene();
|
31
|
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);
|
32
|
38
|
this.renderer = new THREE.WebGLRenderer();
|
33
|
39
|
this.renderer.setSize(window.innerWidth, window.innerHeight);
|
34
|
40
|
|
|
@@ -55,6 +61,7 @@ export class FooGame {
|
55
|
61
|
this.renderer.render(this.scene, this.camera);
|
56
|
62
|
}
|
57
|
63
|
|
|
64
|
+
|
58
|
65
|
private updateObjects(obj: Object3D) {
|
59
|
66
|
|
60
|
67
|
if ("update" in obj && (typeof obj["update"] == "function")) {
|
|
@@ -91,85 +98,47 @@ export class FooGame {
|
91
|
98
|
}
|
92
|
99
|
|
93
|
100
|
/**
|
94
|
|
- * 创建球体
|
95
|
|
- */
|
96
|
|
- createSphere() {
|
97
|
|
- let geo = new THREE.SphereBufferGeometry(15, 100,100);
|
98
|
|
-
|
99
|
|
- //let geo = new THREE.BoxBufferGeometry(5,5,5);
|
100
|
|
-
|
101
|
|
- let mat = new THREE.MeshStandardMaterial();
|
102
|
|
- mat.side = THREE.BackSide; //设置渲染面会背面
|
103
|
|
- mat.blending = THREE.NormalBlending;
|
104
|
|
- let obj = new THREE.Mesh(geo, mat);
|
105
|
|
- return obj;
|
106
|
|
- }
|
107
|
|
-
|
108
|
|
- /**
|
109
|
101
|
* 加载场景
|
110
|
102
|
* @param callback
|
111
|
103
|
*/
|
112
|
104
|
loadScene(callback: Function) {
|
|
105
|
+
|
113
|
106
|
this.gltfLoader.load("./scenes/foo.gltf", gltf => {
|
114
|
107
|
this.scene.add(gltf.scene);
|
115
|
|
- let s = this.findObjectByName("Sphere");
|
116
|
|
- if(s && s instanceof Mesh){
|
117
|
|
- let mesh :Mesh = s;
|
118
|
|
- if(mesh.material instanceof MeshStandardMaterial){
|
119
|
|
- let mat:MeshStandardMaterial = mesh.material;
|
120
|
|
- mat .side = THREE.BackSide;
|
121
|
|
- }
|
122
|
|
- }
|
123
|
|
- /**
|
124
|
|
- * 加载二位图片作为场景背景
|
125
|
|
- */
|
126
|
|
- this.textureLoader.load("./textures/skybox2.jpg", (tex) => {
|
127
|
|
- //this.scene.background = tex; //直接设置二维背景
|
128
|
|
-
|
129
|
|
- //设置天空盒子背景
|
130
|
|
- let skybox = this.createSphere();
|
131
|
|
- if (skybox.material instanceof MeshStandardMaterial) {
|
132
|
|
- let mat: MeshStandardMaterial = skybox.material;
|
133
|
|
- mat.emissive = new THREE.Color(0x00ffff);
|
134
|
|
- mat.emissiveMap = tex;
|
135
|
|
- }
|
|
108
|
+ //this.scene.background = new THREE.Color(0xffffff);
|
136
|
109
|
|
137
|
|
- //this.scene.add(skybox);
|
138
|
|
- });
|
139
|
|
-
|
140
|
|
- /**
|
141
|
|
- * 加载完毕,回调
|
142
|
|
- */
|
143
|
110
|
callback();
|
144
|
111
|
});
|
145
|
|
-
|
146
|
112
|
}
|
147
|
113
|
}
|
148
|
114
|
|
149
|
115
|
let game = new FooGame();
|
150
|
116
|
|
151
|
|
-
|
152
|
|
-
|
153
|
117
|
game.loadScene(() => {
|
154
|
|
- let vBox = new VaringBox();
|
155
|
|
- vBox.material.wireframe = true;
|
156
|
|
-
|
157
|
|
- let geo = new IcosahedronGeometry(1, 4);
|
158
|
|
- let box2 = new THREE.Mesh(geo, vBox.material);
|
159
|
|
- box2.position.x = 2;
|
160
|
|
- box2.position.z = 1;
|
161
|
|
- box2.position.y = 2;
|
162
|
|
-
|
163
|
|
- game.scene.add(box2);
|
164
|
|
- FooGame.setUpdate(box2, (obj: Mesh) => {
|
165
|
|
- obj.rotateY(0.01);
|
166
|
|
- obj.material = vBox.material;
|
167
|
|
- if (obj.material instanceof ShaderMaterial) {
|
168
|
|
- let mat: ShaderMaterial = obj.material;
|
169
|
|
- mat.uniforms.time.value = performance.now() / 500;
|
170
|
|
- }
|
|
118
|
+
|
|
119
|
+ let mat = GameUtils.makeRandomObjects(game.scene, 500);
|
|
120
|
+ FooGame.setUpdate(game.scene, (obj: Object3D) => {
|
|
121
|
+ mat.uniforms.time.value = performance.now() / 500;
|
171
|
122
|
});
|
172
|
123
|
|
173
|
124
|
game.camera.lookAt(new THREE.Vector3(0, 0, 0));
|
|
125
|
+
|
|
126
|
+
|
|
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
|
+ }
|
|
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));
|
|
142
|
+
|
174
|
143
|
})
|
175
|
144
|
|