浏览代码

game utils

hsiao 5 年前
父节点
当前提交
7877b282ce
共有 4 个文件被更改,包括 151 次插入134 次删除
  1. 68
    68
      public/scenes/foo.gltf
  2. 1
    2
      src/controller.ts
  3. 33
    64
      src/index.ts
  4. 49
    0
      src/utils.ts

+ 68
- 68
public/scenes/foo.gltf
文件差异内容过多而无法显示
查看文件


+ 1
- 2
src/controller.ts 查看文件

@@ -39,6 +39,7 @@ export class GameController {
39 39
             eu.y -= this.mouse.dx * 0.001;
40 40
             eu.x = Math.max(- PI_2, Math.min(PI_2, eu.x));
41 41
             cam.quaternion.setFromEuler(eu);
42
+            
42 43
         };
43 44
     }
44 45
 
@@ -81,8 +82,6 @@ export class GameController {
81 82
     private bindKey() {
82 83
         this.root.onkeypress = (event) => {
83 84
             let c = event.keyCode;
84
-            console.log(c);
85
-            
86 85
             if (this.callbackMap[c]) {
87 86
                 this.callbackMap[c]();
88 87
             }

+ 33
- 64
src/index.ts 查看文件

@@ -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
 

+ 49
- 0
src/utils.ts 查看文件

@@ -0,0 +1,49 @@
1
+
2
+import { Object3D, BoxBufferGeometry, Color, MeshPhongMaterial, Material, IcosahedronBufferGeometry, TorusKnotBufferGeometry, SphereBufferGeometry } from 'three';
3
+import { Scene } from 'three';
4
+import { MeshStandardMaterial } from 'three';
5
+import { Mesh } from 'three';
6
+import { VaringBox } from './shader_test';
7
+import { ShaderMaterial } from 'three';
8
+import { FooGame } from './index';
9
+export class GameUtils {
10
+
11
+    static makeRandomObjects(scene: Scene, n: number = 100): ShaderMaterial {
12
+        let vBox = new VaringBox();
13
+        let geo = new BoxBufferGeometry(1, 1, 1);
14
+        let geo2 = new IcosahedronBufferGeometry(1, 3);
15
+        let geo3 = new SphereBufferGeometry(1, 40,40);
16
+
17
+        for (let i = 0; i < n; i++) {
18
+            let temp;
19
+            if (i % 2)
20
+                temp = new Mesh(geo, vBox.material);
21
+            if (i % 3) {
22
+                temp = new Mesh(geo3,vBox.material);
23
+            }
24
+            else
25
+                temp = new Mesh(geo2, vBox.material);
26
+
27
+            temp.position.set(Math.random() * 50, Math.random() * 10, Math.random() * 50);
28
+            temp.scale.set(
29
+                Math.random() * 2,
30
+                Math.random() * 2,
31
+                Math.random() * 2
32
+            )
33
+           FooGame.setUpdate(temp,(obj:Mesh)=>{
34
+                obj.position.x += Math.sin(performance.now()*0.0003+ obj.position.z);
35
+                obj.position.y += Math.sin(performance.now()*0.0002+ 4*obj.position.z);  
36
+            });
37
+            scene.add(temp);
38
+        }
39
+
40
+        return vBox.material;
41
+    }
42
+
43
+    static makeBox(): Mesh {
44
+        let vBox = new VaringBox();
45
+        let geo = new BoxBufferGeometry(1, 1, 1);
46
+
47
+        return new Mesh(geo, vBox.material);
48
+    }
49
+}

正在加载...
取消
保存