|
@@ -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, ShaderMaterial, IcosahedronGeometry } from "three";
|
|
3
|
+import { Object3D, Mesh, ShaderMaterial, IcosahedronGeometry, TextureLoader, MeshStandardMaterial, Scene } from "three";
|
4
|
4
|
|
5
|
5
|
import { GameController } from "./controller"
|
6
|
6
|
import { VaringBox } from "./shader_test"
|
|
@@ -14,7 +14,8 @@ export class FooGame {
|
14
|
14
|
cam_fov = 70; //视角大小
|
15
|
15
|
cam_aspect = window.innerWidth / window.innerHeight;
|
16
|
16
|
|
17
|
|
- loader: GLTFLoader;
|
|
17
|
+ gltfLoader: GLTFLoader;
|
|
18
|
+ textureLoader: TextureLoader;
|
18
|
19
|
|
19
|
20
|
updateMap: Object;
|
20
|
21
|
|
|
@@ -33,7 +34,7 @@ export class FooGame {
|
33
|
34
|
|
34
|
35
|
document.body.appendChild(this.renderer.domElement);
|
35
|
36
|
|
36
|
|
- this.loader = new GLTFLoader();
|
|
37
|
+ this.gltfLoader = new GLTFLoader();
|
37
|
38
|
|
38
|
39
|
this.updateMap = {};
|
39
|
40
|
|
|
@@ -41,6 +42,8 @@ export class FooGame {
|
41
|
42
|
this.controller.bindMoveKeys(this.camera);
|
42
|
43
|
this.controller.bindMouseLookAt(this.camera, this.scene);
|
43
|
44
|
|
|
45
|
+ this.textureLoader = new TextureLoader();
|
|
46
|
+
|
44
|
47
|
this.animate();
|
45
|
48
|
}
|
46
|
49
|
|
|
@@ -87,13 +90,59 @@ export class FooGame {
|
87
|
90
|
});
|
88
|
91
|
}
|
89
|
92
|
|
|
93
|
+ /**
|
|
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
|
+ * 加载场景
|
|
110
|
+ * @param callback
|
|
111
|
+ */
|
90
|
112
|
loadScene(callback: Function) {
|
91
|
|
- this.loader.load("./scenes/foo.gltf", gltf => {
|
|
113
|
+ this.gltfLoader.load("./scenes/foo.gltf", gltf => {
|
92
|
114
|
this.scene.add(gltf.scene);
|
93
|
|
- let box = this.scene.getObjectByName("Box");
|
94
|
|
- box && this.camera.lookAt(box.position);
|
|
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
|
+ }
|
|
136
|
+
|
|
137
|
+ //this.scene.add(skybox);
|
|
138
|
+ });
|
|
139
|
+
|
|
140
|
+ /**
|
|
141
|
+ * 加载完毕,回调
|
|
142
|
+ */
|
95
|
143
|
callback();
|
96
|
144
|
});
|
|
145
|
+
|
97
|
146
|
}
|
98
|
147
|
}
|
99
|
148
|
|
|
@@ -102,20 +151,8 @@ let game = new FooGame();
|
102
|
151
|
|
103
|
152
|
|
104
|
153
|
game.loadScene(() => {
|
105
|
|
- let box = game.scene.getObjectByName("Box");
|
106
|
154
|
let vBox = new VaringBox();
|
107
|
|
- 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
|
|
- }
|
|
155
|
+ vBox.material.wireframe = true;
|
119
|
156
|
|
120
|
157
|
let geo = new IcosahedronGeometry(1, 4);
|
121
|
158
|
let box2 = new THREE.Mesh(geo, vBox.material);
|
|
@@ -124,7 +161,15 @@ game.loadScene(() => {
|
124
|
161
|
box2.position.y = 2;
|
125
|
162
|
|
126
|
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
|
+ }
|
|
171
|
+ });
|
127
|
172
|
|
128
|
|
-
|
|
173
|
+ game.camera.lookAt(new THREE.Vector3(0, 0, 0));
|
129
|
174
|
})
|
130
|
175
|
|