hsiao 5 years ago
parent
commit
901d818746
7 changed files with 357 additions and 147 deletions
  1. 1
    1
      public/index.html
  2. 114
    126
      public/scenes/foo.gltf
  3. 169
    0
      public/scenes/skybox.gltf
  4. BIN
      public/textures/skybox.jpg
  5. BIN
      public/textures/skybox2.jpg
  6. 8
    0
      src/controller.ts
  7. 65
    20
      src/index.ts

+ 1
- 1
public/index.html View File

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

+ 114
- 126
public/scenes/foo.gltf
File diff suppressed because it is too large
View File


+ 169
- 0
public/scenes/skybox.gltf
File diff suppressed because it is too large
View File


BIN
public/textures/skybox.jpg View File


BIN
public/textures/skybox2.jpg View File


+ 8
- 0
src/controller.ts View File

@@ -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
     }
@@ -68,6 +73,7 @@ export class GameController {
68 73
     }
69 74
     private onKey_ESC() {
70 75
         this.callbackMap[27] = () => {
76
+            document.body.style.cursor = "pointer";
71 77
             document.exitPointerLock();
72 78
         };
73 79
     }
@@ -75,6 +81,8 @@ export class GameController {
75 81
     private bindKey() {
76 82
         this.root.onkeypress = (event) => {
77 83
             let c = event.keyCode;
84
+            console.log(c);
85
+            
78 86
             if (this.callbackMap[c]) {
79 87
                 this.callbackMap[c]();
80 88
             }

+ 65
- 20
src/index.ts View File

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

Loading…
Cancel
Save