2 次程式碼提交

作者 SHA1 備註 提交日期
  hsiao 7877b282ce game utils 5 年之前
  hsiao 901d818746 skybox 5 年之前
共有 8 個檔案被更改,包括 368 行新增141 行删除
  1. 1
    1
      public/index.html
  2. 100
    112
      public/scenes/foo.gltf
  3. 169
    0
      public/scenes/skybox.gltf
  4. 二進制
      public/textures/skybox.jpg
  5. 二進制
      public/textures/skybox2.jpg
  6. 7
    0
      src/controller.ts
  7. 42
    28
      src/index.ts
  8. 49
    0
      src/utils.ts

+ 1
- 1
public/index.html 查看文件

@@ -6,7 +6,7 @@
6 6
     <style>
7 7
         body{
8 8
             margin: 0;
9
-            cursor: none;
9
+            cursor: pointer;
10 10
         }
11 11
     </style>
12 12
     

+ 100
- 112
public/scenes/foo.gltf
文件差異過大導致無法顯示
查看文件


+ 169
- 0
public/scenes/skybox.gltf
文件差異過大導致無法顯示
查看文件


二進制
public/textures/skybox.jpg 查看文件


二進制
public/textures/skybox2.jpg 查看文件


+ 7
- 0
src/controller.ts 查看文件

@@ -15,6 +15,11 @@ export class GameController {
15 15
         this.mouse = { dx: 0, dy: 0 }
16 16
         this.raycaster = new Raycaster();
17 17
 
18
+        this.root.onmouseup=()=>{
19
+            document.body.style.cursor = "none";
20
+            this.root.requestPointerLock();
21
+        };
22
+
18 23
         this.onKey_ESC();
19 24
         this.bindKey();
20 25
     }
@@ -34,6 +39,7 @@ export class GameController {
34 39
             eu.y -= this.mouse.dx * 0.001;
35 40
             eu.x = Math.max(- PI_2, Math.min(PI_2, eu.x));
36 41
             cam.quaternion.setFromEuler(eu);
42
+            
37 43
         };
38 44
     }
39 45
 
@@ -68,6 +74,7 @@ export class GameController {
68 74
     }
69 75
     private onKey_ESC() {
70 76
         this.callbackMap[27] = () => {
77
+            document.body.style.cursor = "pointer";
71 78
             document.exitPointerLock();
72 79
         };
73 80
     }

+ 42
- 28
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 } 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
 

+ 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
+}

Loading…
取消
儲存