-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscene.js
78 lines (75 loc) · 1.98 KB
/
scene.js
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
'use strict'
class Scene {
constructor(gl,view=new Mat()) {
this.view = view;
this.time = 0;
this.passes = [];
this.pass_depths = [];
this.square_verts = new Float32Array([
-1.0,-1.0,
1.0,-1.0,
-1.0,1.0,
1.0,1.0,
]);
this.square_vao = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.square_vao);
gl.bufferData(gl.ARRAY_BUFFER, this.square_verts, gl.STATIC_DRAW);
}
addPass(pass,depth=0.5) {
this.pass_depths.push([pass,depth]);
this.pass_depths.sort((a,b) => b[1]-a[1]); // Sort from deep to shallow
this.passes = [];
for (const [pass,depth] of this.pass_depths) {
this.passes.push(pass);
}
}
update(dt) {
this.time += dt;
for (const pass of this.passes) {
pass.update(this,dt);
}
}
prepare(gl) {
for (const pass of this.passes) {
pass.prepare(gl);
}
}
draw(gl) {
for (const pass of this.passes) {
pass.draw(gl);
}
}
}
class RenderPass {
constructor(gl,program) {
this.program = program;
}
update(scene,dt) {}
prepare(gl) {}
draw(gl) {}
}
// A builtin pass
class SpritePass extends RenderPass{
constructor(gl,scene,program,texture) {
super(gl,program);
this.texture = texture;
this.dvao = new DynamicVAO(gl,program,scene.square_vao,{
model: {type:'mat',dynamic:true},
uv: {type:'mat',dynamic:true},
},8);
this.view_loc = gl.getUniformLocation(program,"view");
this.view = new Mat();
}
update(scene,dt) {
this.view.eq(scene.view);
}
prepare(gl) {
this.dvao.prepare(gl);
}
draw(gl) {
gl.useProgram(this.program);
gl.bindTexture(gl.TEXTURE_2D, this.texture);
gl.uniformMatrix3fv(this.view_loc,false,this.view.a);
this.dvao.draw(gl);
}
}