-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgldemo.html
197 lines (187 loc) · 5.55 KB
/
gldemo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script src="math32.js"></script>
<script src="shadercompiler.js"></script>
<script src="imageloader.js"></script>
<script src="sounds.js"></script>
<script src="canvasmanager.js"></script>
<script src="dynamicvao.js"></script>
<script src="scene.js"></script>
<script src="engine.js"></script>
</head>
<body>
<h1>David Bowie Fanfiction: WebGL</h1>
<div id="canvas-container" style="height:80vh; width:100%"></div>
<script id="shader-background-v" type="x-shader/x-vertex">
attribute vec2 vertex;
varying vec2 world_coord;
uniform mat3 inverse_view;
void main() {
gl_Position = vec4(vertex,0.0,1.0);
world_coord = (inverse_view * vec3(vertex,1.0)).xy;
}
</script>
<script id="shader-background-f" type="x-shader/x-fragment">
precision highp float;
varying vec2 world_coord;
uniform float t;
void main() {
float x = dot(world_coord,vec2(0.7,0.3));
float y = dot(world_coord,vec2(0.71,1.3));
float z = dot(world_coord,vec2(-31.0,41.0));
gl_FragColor = vec4(0.8+sin(t*2.0+y)*0.1, 0.8+sin(t*4.0+z)*0.025, 0.8+sin(t+x)*0.3, 1.0);
}
</script>
<script>
/*
* A custom render pass that uses the custom shaders
*/
class BackgroundPass extends RenderPass {
constructor(gl,scene,program) {
super(gl,program);
this.vao = new SimpleVAO(gl,program,scene.square_vao);
this.inverse_view_loc = gl.getUniformLocation(program,"inverse_view");
this.t_loc = gl.getUniformLocation(program,"t");
this.inverse_view = new Mat();
this.t = 0;
}
update(scene,dt) {
Mat.Inv(this.inverse_view,scene.view);
this.t += dt;
}
draw(gl) {
gl.useProgram(this.program);
gl.uniformMatrix3fv(this.inverse_view_loc,false,this.inverse_view.a);
gl.uniform1f(this.t_loc,this.t);
this.vao.draw(gl);
}
}
class DemoEngine extends Engine {
setup() {
// Background pass
this.backgroundpass = new BackgroundPass(this.gl,
this.scene,
this.sc.programs.get('background')
);
this.scene.addPass(this.backgroundpass,1.0);
// Make some sprites
/*this.sprites = new Set();
for (const [name,tex_frame] of this.il.model_frames) {
const s = new Sprite(this,name);
s.angle = Math.PI * 2 * Math.random();
s.scale = 0.5 + 0.5*Math.random();
s.pos = new Vec(Math.random()*2-1, Math.random()*2-1);
s.mirror = Math.random() > 0.5;
}*/
new Robot(this);
}
update(dt) {
super.update(dt);
this.scene.view.a[0] = 1/this.cm.aspectRatio;
}
}
class Robot extends Sprite {
constructor(engine) {
super(engine,"BlueBot/bluebot1.png");
this.frames = [
"BlueBot/bluebot1.png",
"BlueBot/bluebot2.png",
"BlueBot/bluebot3.png",
"BlueBot/bluebot4.png",
"BlueBot/bluebot3.png",
"BlueBot/bluebot2.png",
];
this.frame = 0;
this.frame_age = 0;
this.engine.cm.canvas.addEventListener('click',
(e) => {
const mouse_pos = new Vec(this.engine.cm.mouse_x,this.engine.cm.mouse_y);
const velocity = this.engine.mouse_pos.sub(this.pos).normeq().muleq(3);
new Wave(this.engine,this.pos,velocity);
}
);
}
tick(dt) {
let moving = false;
if (this.engine.cm.isPressed("KeyA")) {
this.pos.addeq(new Vec(-0.1,0.0));
this.mirror = false;
moving = true;
}
if (this.engine.cm.isPressed("KeyD")) {
this.pos.addeq(new Vec(0.1,0.0));
this.mirror = true;
moving = true;
}
this.setImage(this.frames[this.frame]);
this.frame_age += dt;
if ((moving || this.frame !== 0) && this.frame_age > 0.1) {
this.frame = (this.frame + 1) % this.frames.length;
this.frame_age = 0;
}
}
}
class Wave extends Sprite {
constructor(engine,pos,vel) {
super(engine,"BlueBot/bluebot1.png");
this.pos.eq(pos);
this.vel = vel;
this.frames = [
"OrangeWave/1.png",
"OrangeWave/2.png",
"OrangeWave/3.png",
"OrangeWave/4.png",
"OrangeWave/3.png",
"OrangeWave/2.png",
];
this.angle = Math.atan2(vel.a[0],vel.a[1]);
this.frame = 0;
this.frame_age = 0;
}
tick(dt) {
if (this.pos.a[1]>1){
this.vel.a[1] = -this.vel.a[1];
}
if (this.pos.a[1]<-1){
this.vel.a[1] = -this.vel.a[1];
}//handles y array
if (this.pos.a[0]>1){
this.vel.a[0] = -this.vel.a[0];
}
if (this.pos.a[0]<-1){
this.vel.a[0] = -this.vel.a[0];
}//handles x array
this.pos.addeq(this.vel.mul(dt));
this.setImage(this.frames[this.frame]);
this.frame_age += dt;
if (this.frame_age > 0.1) {
this.frame = (this.frame + 1) % this.frames.length;
this.frame_age = 0;
}
}
}
const engine = new DemoEngine(
canvas_container = document.getElementById("canvas-container"),
shader_programs = {
background:['shader-background-v','shader-background-f']
},
images = {
spritesheet:'images/hoverbots.png'
},
frames = {
spritesheet:'images/hoverbots.json'
},
music = {
"negentropy": "sound/Chad_Crouch_-_Negentropy.mp3"
},
sfx = {
"hit": "sound/hit.mp3"
},
);
console.log(engine);
</script>
</div>
</body>
</html>