Browse Source

point lock

hsiao 5 years ago
parent
commit
1867a1649c
4 changed files with 120 additions and 76 deletions
  1. 1
    0
      public/index.html
  2. 57
    12
      src/controller.ts
  3. 27
    64
      src/index.ts
  4. 35
    0
      src/shader_test.ts

+ 1
- 0
public/index.html View File

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

+ 57
- 12
src/controller.ts View File

@@ -1,38 +1,83 @@
1
+import { Object3D, Raycaster, Camera, Scene, Euler } from "three";
2
+import { FooGame } from './index';
1 3
 
2 4
 export class GameController {
3 5
     root: HTMLElement;
4
-
6
+    mouse: { dx: number, dy: number };
5 7
     callbackMap: any;
6 8
 
9
+    private raycaster: Raycaster;
10
+
7 11
     constructor(root: HTMLElement) {
8 12
         this.root = root;
9 13
         this.callbackMap = {};
14
+
15
+        this.mouse = { dx: 0, dy: 0 }
16
+        this.raycaster = new Raycaster();
17
+
18
+        this.onKey_ESC();
10 19
         this.bindKey();
11 20
     }
12 21
 
13
-    onKey_A(callback: Function) {
14
-        this.callbackMap["A"] = callback;
22
+    bindMouseLookAt(obj: Camera, scene: Scene) {
23
+        let cam = obj;
24
+        let eu: Euler = new Euler(0, 0, 0, 'YXZ');
25
+        let PI_2 = Math.PI / 2;
26
+
27
+        this.root.onmousemove = (event) => {
28
+            //event.preventDefault();
29
+            this.mouse.dx = event.movementX;
30
+            this.mouse.dy = event.movementY;
31
+
32
+            eu.setFromQuaternion(cam.quaternion);
33
+            eu.x -= this.mouse.dy * 0.001;
34
+            eu.y -= this.mouse.dx * 0.001;
35
+            eu.x = Math.max(- PI_2, Math.min(PI_2, eu.x));
36
+            cam.quaternion.setFromEuler(eu);
37
+        };
38
+    }
39
+
40
+    bindMoveKeys(box: Object3D) {
41
+        this.onKey_A(() => {
42
+            if (box) box.position.x -= 0.1;
43
+        });
44
+        this.onKey_D(() => {
45
+            if (box) box.position.x += 0.1;
46
+        });
47
+        this.onKey_S(() => {
48
+            if (box) box.position.z += 0.1;
49
+        });
50
+        this.onKey_W(() => {
51
+            if (box) box.position.z -= 0.1;
52
+        });
53
+    }
54
+
55
+    private onKey_A(callback: Function) {
56
+        this.callbackMap[65] = callback;
15 57
     }
16 58
 
17
-    onKey_S(callback: Function) {
18
-        this.callbackMap["S"] = callback;
59
+    private onKey_S(callback: Function) {
60
+        this.callbackMap[83] = callback;
19 61
     }
20 62
 
21
-    onKey_W(callback: Function) {
22
-        this.callbackMap["W"] = callback;
63
+    private onKey_W(callback: Function) {
64
+        this.callbackMap[87] = callback;
23 65
     }
24
-    onKey_D(callback: Function) {
25
-        this.callbackMap["D"] = callback;
66
+    private onKey_D(callback: Function) {
67
+        this.callbackMap[68] = callback;
68
+    }
69
+    private onKey_ESC() {
70
+        this.callbackMap[27] = () => {
71
+            document.exitPointerLock();
72
+        };
26 73
     }
27 74
 
28 75
     private bindKey() {
29 76
         this.root.onkeypress = (event) => {
30
-            let c = event.key.toUpperCase();
77
+            let c = event.keyCode;
31 78
             if (this.callbackMap[c]) {
32 79
                 this.callbackMap[c]();
33 80
             }
34 81
         };
35
-        console.log(this.root);
36
-
37 82
     }
38 83
 }

+ 27
- 64
src/index.ts View File

@@ -1,9 +1,9 @@
1 1
 import * as THREE from "three"
2 2
 import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
3
-import { Object3D, Mesh, ShaderMaterial } from "three";
3
+import { Object3D, Mesh, ShaderMaterial, IcosahedronGeometry } from "three";
4 4
 
5 5
 import { GameController } from "./controller"
6
-
6
+import { VaringBox } from "./shader_test"
7 7
 
8 8
 export class FooGame {
9 9
 
@@ -22,9 +22,9 @@ export class FooGame {
22 22
 
23 23
     constructor() {
24 24
         this.camera = new THREE.PerspectiveCamera(this.cam_fov, this.cam_aspect, 0.01, 10)
25
-        this.camera.position.x = 0;
26
-        this.camera.position.y = 3;
27
-        this.camera.position.z = 5;
25
+        this.camera.position.x = -1;
26
+        this.camera.position.y = 1;
27
+        this.camera.position.z = 1;
28 28
 
29 29
         this.scene = new THREE.Scene();
30 30
 
@@ -38,25 +38,10 @@ export class FooGame {
38 38
         this.updateMap = {};
39 39
 
40 40
         this.controller = new GameController(document.body);
41
+        this.controller.bindMoveKeys(this.camera);
42
+        this.controller.bindMouseLookAt(this.camera, this.scene);
41 43
 
42 44
         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 45
     }
61 46
 
62 47
     animate() {
@@ -95,7 +80,7 @@ export class FooGame {
95 80
         }
96 81
     }
97 82
 
98
-    setUpdate(obj: Object3D, fn: Function) {
83
+    static setUpdate(obj: Object3D, fn: Function) {
99 84
         Object.defineProperty(obj, "update", {
100 85
             writable: true,
101 86
             value: fn
@@ -114,54 +99,32 @@ export class FooGame {
114 99
 
115 100
 let game = new FooGame();
116 101
 
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 102
 
129
-let fragmentShader =  `
130
-varying vec3 vPos;
131
-void main() {
132
-	gl_FragColor = vec4( vPos, 1.0 );
133
-}
134
-`;
135 103
 
136 104
 game.loadScene(() => {
137 105
     let box = game.scene.getObjectByName("Box");
106
+    let vBox = new VaringBox();
107
+    game.camera.lookAt(new THREE.Vector3(0, 0, 0));
138 108
     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 109
 
162
-            //game.camera.lookAt(obj.position)
163
-            mat.uniforms.time.value = performance.now()/500;
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
+            }
164 117
         });
165 118
     }
119
+
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
+    game.scene.add(box2);
127
+
128
+
166 129
 })
167 130
 

+ 35
- 0
src/shader_test.ts View File

@@ -0,0 +1,35 @@
1
+import * as THREE from "three"
2
+import { IcosahedronGeometry, MeshStandardMaterial, Mesh, ShaderMaterial } from "three";
3
+
4
+export class VaringBox {
5
+    material: ShaderMaterial;
6
+    private vertexShader = `
7
+        uniform float time;
8
+        varying vec3 vPos;
9
+        void main() {
10
+            vPos=position;
11
+            vPos.x += sin( time + vPos.z * 4.0 ) / 4.0;
12
+            vPos.y += cos( time + vPos.z * 4.0 ) / 4.0;
13
+            gl_Position = projectionMatrix * modelViewMatrix * vec4( vPos, 1.0 );
14
+        }
15
+        `;
16
+
17
+    private fragmentShader = `
18
+        varying vec3 vPos;
19
+        void main() {
20
+            gl_FragColor = vec4( vPos*2.0, 1.0 );
21
+        }
22
+        `;
23
+
24
+    constructor() {
25
+        this.material = new ShaderMaterial({
26
+            uniforms: {
27
+                time: {
28
+                    value: 0
29
+                }
30
+            },
31
+            vertexShader: this.vertexShader,
32
+            fragmentShader: this.fragmentShader
33
+        });
34
+    }
35
+}

Loading…
Cancel
Save