-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWelcomePageRenderer.js
More file actions
69 lines (65 loc) · 2.59 KB
/
WelcomePageRenderer.js
File metadata and controls
69 lines (65 loc) · 2.59 KB
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
import { mat4, degree2radian as d2r } from "./math.js";
import Render from "./Render.js";
import Camera from "./Camera.js";
import * as glsl from "./glsl.js";
import { waitResource } from "./loadResources.js";
let texImgs = null;
waitResource("./").then(imgs => texImgs = imgs);
class WelcomeRenderer extends Render {
constructor(canvas) {
super(canvas);
this.fitScreen();
new ResizeObserver(async e => {
await new Promise(s => setTimeout(s, 0));
this.fitScreen();
}).observe(canvas);
let vertexPosition = [
-1, 1,-1, -1,-1,-1, -1,-1, 1, -1, 1, 1,
-1, 1, 1, -1,-1, 1, 1,-1, 1, 1, 1, 1,
1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1,
1, 1,-1, 1,-1,-1, -1,-1,-1, -1, 1,-1,
1, 1,-1, -1, 1,-1, -1, 1, 1, 1, 1, 1,
-1,-1,-1, 1,-1,-1, 1,-1, 1, -1,-1, 1,
],
element = (len => {
let base = [0,1,2, 0,2,3], out = [];
for (let i = 0, j = 0; i <= len; j = i++ * 4)
out.push(...base.map(x => x + j));
return out;
})(vertexPosition.length / 12);
this.bos = {
ver: this.createVbo(vertexPosition),
ele: this.createIbo(element),
};
this.prg = this.createProgram("welcomePage", glsl.welcomePage.vert, glsl.welcomePage.frag)
.use().bindTex("uTexture", this.createCubemapsTexture(texImgs, "./"))
.setAtt("aPosition", this.bos.ver);
const {ctx} = this;
ctx.texParameteri(ctx.TEXTURE_CUBE_MAP, ctx.TEXTURE_MAG_FILTER, ctx.LINEAR);
ctx.texParameteri(ctx.TEXTURE_CUBE_MAP, ctx.TEXTURE_MIN_FILTER, ctx.LINEAR);
let mainCamera = this.mainCamera = new Camera(this.aspectRatio, {
viewType: Camera.viewType.lookAt,
fovy: 120, position: [0, 0, 0],
lookAt: [-1, 0, 0], up: [0, 1, 0],
far: 10,
});
this.addCamera(mainCamera);
this.mM = mat4.identity();
this.mvpM = mat4.identity();
};
get vpM() { return this.mainCamera.projview; };
onRender() {
const {ctx, prg, mM, vpM, mvpM, bos} = this;
mat4.rotate(mM, d2r(1 / 70), [0, 1, 0], mM);
mat4.multiply(vpM, mM, mvpM);
prg.use().setUni("uMvpMatrix", mvpM);
ctx.clear(ctx.COLOR_BUFFER_BIT);
ctx.bindBuffer(bos.ele.type, bos.ele);
ctx.drawElements(ctx.TRIANGLES, bos.ele.length, ctx.UNSIGNED_SHORT, 0);
ctx.flush();
};
};
export {
WelcomeRenderer as default,
WelcomeRenderer,
};