hsiao 5 jaren geleden
bovenliggende
commit
31116e6405
4 gewijzigde bestanden met toevoegingen van 29 en 452 verwijderingen
  1. 0
    330
      public/scenes/foo.gltf
  2. BIN
      public/textures/sand.jpg
  3. 0
    38
      src/controller.ts
  4. 29
    84
      src/index.ts

+ 0
- 330
public/scenes/foo.gltf
Diff onderdrukt omdat het te groot bestand
Bestand weergeven


BIN
public/textures/sand.jpg Bestand weergeven


+ 0
- 38
src/controller.ts Bestand weergeven

@@ -1,38 +0,0 @@
1
-
2
-export class GameController {
3
-    root: HTMLElement;
4
-
5
-    callbackMap: any;
6
-
7
-    constructor(root: HTMLElement) {
8
-        this.root = root;
9
-        this.callbackMap = {};
10
-        this.bindKey();
11
-    }
12
-
13
-    onKey_A(callback: Function) {
14
-        this.callbackMap["A"] = callback;
15
-    }
16
-
17
-    onKey_S(callback: Function) {
18
-        this.callbackMap["S"] = callback;
19
-    }
20
-
21
-    onKey_W(callback: Function) {
22
-        this.callbackMap["W"] = callback;
23
-    }
24
-    onKey_D(callback: Function) {
25
-        this.callbackMap["D"] = callback;
26
-    }
27
-
28
-    private bindKey() {
29
-        this.root.onkeypress = (event) => {
30
-            let c = event.key.toUpperCase();
31
-            if (this.callbackMap[c]) {
32
-                this.callbackMap[c]();
33
-            }
34
-        };
35
-        console.log(this.root);
36
-
37
-    }
38
-}

+ 29
- 84
src/index.ts Bestand weergeven

@@ -1,8 +1,6 @@
1 1
 import * as THREE from "three"
2 2
 import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
3
-import { Object3D, Mesh, ShaderMaterial } from "three";
4
-
5
-import { GameController } from "./controller"
3
+import { Object3D, Mesh, ShaderMaterial, DirectionalLight, BoxBufferGeometry, TextureLoader, MeshStandardMaterial, Texture, AmbientLight, Vector3, PlaneBufferGeometry } from "three";
6 4
 
7 5
 
8 6
 export class FooGame {
@@ -15,16 +13,14 @@ export class FooGame {
15 13
     cam_aspect = window.innerWidth / window.innerHeight;
16 14
 
17 15
     loader: GLTFLoader;
18
-
16
+    texLoader: TextureLoader;
19 17
     updateMap: Object;
20 18
 
21
-    controller: GameController;
22
-
23 19
     constructor() {
24
-        this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
20
+        this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 1000)
25 21
         this.camera.position.x = 0;
26
-        this.camera.position.y = 3;
27
-        this.camera.position.z = 5;
22
+        this.camera.position.y = 15;
23
+        this.camera.position.z = 0;
28 24
 
29 25
         this.scene = new THREE.Scene();
30 26
 
@@ -34,29 +30,11 @@ export class FooGame {
34 30
         document.body.appendChild(this.renderer.domElement);
35 31
 
36 32
         this.loader = new GLTFLoader();
33
+        this.texLoader = new TextureLoader();
37 34
 
38 35
         this.updateMap = {};
39 36
 
40
-        this.controller = new GameController(document.body);
41
-
42 37
         this.animate();
43
-        //this.loadScene();
44
-    }
45
-
46
-    bindKeyAction(box: Object3D) {
47
-        this.controller.onKey_A(() => {
48
-            if (box) box.position.x -= 0.1;
49
-        });
50
-        this.controller.onKey_D(() => {
51
-            if (box) box.position.x += 0.1;
52
-        });
53
-        this.controller.onKey_S(() => {
54
-            if (box) box.position.z += 0.1;
55
-        });
56
-        this.controller.onKey_W(() => {
57
-            if (box) box.position.z -= 0.1;
58
-        });
59
-
60 38
     }
61 39
 
62 40
     animate() {
@@ -103,65 +81,32 @@ export class FooGame {
103 81
     }
104 82
 
105 83
     loadScene(callback: Function) {
106
-        this.loader.load("./scenes/foo.gltf", gltf => {
107
-            this.scene.add(gltf.scene);
108
-            let box = this.scene.getObjectByName("Box");
109
-            box && this.camera.lookAt(box.position);
110
-            callback();
111
-        });
84
+        let light = new DirectionalLight(0xffffff, 0.8);
85
+        light.position.set(5, 20, 0);
86
+        let light2 = new AmbientLight(0xffffff, 0.9);
87
+        this.scene.add(light);
88
+        this.scene.add(light2);
89
+        this.camera.lookAt(new Vector3(0, 0, 0));
90
+        callback();
112 91
     }
113 92
 }
114 93
 
115 94
 let game = new FooGame();
116
-
117
-let vertexShader =  `
118
-uniform float time;
119
-varying vec3 vPos;
120
-void main() {
121
-	vPos=position;
122
-	vPos.x += sin(time * vPos.z) * 2.0;
123
-	vPos.y += cos(time * vPos.z) * 2.0;
124
-	//vPos.z += tan(time * vPos.z) * 4.0;
125
-	gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
126
-}
127
-`;
128
-
129
-let fragmentShader =  `
130
-varying vec3 vPos;
131
-void main() {
132
-	gl_FragColor = vec4( vPos, 1.0 );
133
-}
134
-`;
135
-
136 95
 game.loadScene(() => {
137
-    let box = game.scene.getObjectByName("Box");
138
-    if (box) {
139
-        game.bindKeyAction(box);
140
-        let mat:ShaderMaterial;
141
-        if (box instanceof Mesh) {
142
-            let mesh: Mesh = box;
143
-            
144
-            mat = new ShaderMaterial({
145
-                uniforms: {
146
-                    time: {
147
-                        value: 0
148
-                    }
149
-                },
150
-                vertexShader: vertexShader,
151
-                fragmentShader: fragmentShader
152
-            });
153
-            mesh.material = mat;
154
-        }
155
-
156
-        game.setUpdate(box, (obj: Object3D) => {
157
-            //game.camera.rotateOnAxis(obj.position, 0.01)
158
-            //obj.position.x += performance.now() * 0.000001;
159
-            //obj.position.z += performance.now() * 0.000001;
160
-            obj.rotateY(0.01);
161
-
162
-            //game.camera.lookAt(obj.position)
163
-            mat.uniforms.time.value = performance.now()/500;
164
-        });
165
-    }
166
-})
96
+    let mat = new MeshStandardMaterial();
97
+    let mesh = new PlaneBufferGeometry(50, 50, 100, 100);
98
+
99
+    game.texLoader.load("./textures/sand.jpg", (tex) => {
100
+        tex.wrapS = THREE.RepeatWrapping;
101
+        tex.wrapT = THREE.RepeatWrapping;
102
+        tex.mapping = THREE.EquirectangularReflectionMapping;
103
+        mat.map = tex;
104
+        let obj = new Mesh(mesh, mat);
105
+        obj.rotation.x = -Math.PI / 2;
106
+        obj.position.y = -3
107
+        game.scene.add(obj);
108
+    });
109
+
110
+
111
+});
167 112
 

Laden…
Annuleren
Opslaan