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