-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl.js
More file actions
139 lines (108 loc) · 2.82 KB
/
gl.js
File metadata and controls
139 lines (108 loc) · 2.82 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
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
const DEBUG = true
export class GL {
static #canvas
static #gl
static #frame = 0
static #time = 0.0 // milliseconds
static #timeOffset = 0.0 // milliseconds
static #frameDuration // milliseconds
static #cbLoad = null
static #cbLoop = null
static #cbResize = null
static #aspectRatio
static #createCanvas() {
GL.#canvas = document.createElement('canvas')
Object.assign(document.body.style, {
margin: '0',
padding: '0',
overflow: 'hidden',
position: 'fixed',
width: '100vw',
height: '100vh'
})
Object.assign(GL.#canvas.style, {
display: 'block',
width: '100%',
height: '100%'
})
}
static #createContext() {
GL.#gl = GL.#canvas.getContext('webgl2', { alpha: false, depth: false, stencil: false, antialias: false })
if (GL.#gl === null) {
throw new Error('WebGL 2.0 not supported, apparently 🤷')
}
const ext = GL.#gl.getExtension('EXT_color_buffer_float')
if (!ext) {
throw new Error('Floating-point rendering not supported! We kind of need that')
}
console.log(GL.#gl.getSupportedExtensions())
}
static #onResize() {
const rect = GL.#canvas.getBoundingClientRect()
const dpr = devicePixelRatio || 1
const width = Math.floor(rect.width * dpr)
const height = Math.floor(rect.height * dpr)
GL.#aspectRatio = width / height
if (GL.#canvas.width !== width || GL.#canvas.height !== height) {
GL.#canvas.width = width
GL.#canvas.height = height
if (GL.#cbResize !== null) {
GL.#cbResize()
}
}
}
static #logStats() {
// console.log(`Frames: ${GL.#frame}`)
// console.log(`FPS: ${10000.0 / GL.#frameDuration}`)
}
static #renderLoop(frameTime) {
if (GL.#frame === 0) {
GL.#timeOffset = frameTime
}
const previousTime = GL.#time
GL.#time = frameTime - GL.#timeOffset
GL.#frameDuration = (frameTime - previousTime)
if (GL.#frame % 120 === 119) {
GL.#logStats()
}
GL.#cbLoop()
++GL.#frame
requestAnimationFrame(GL.#renderLoop)
}
static #onLoad() {
GL.#createCanvas()
GL.#createContext()
if (GL.#cbLoad !== null) {
GL.#cbLoad()
}
document.body.appendChild(GL.#canvas)
GL.#onResize()
addEventListener('resize', GL.#onResize)
requestAnimationFrame(GL.#renderLoop)
}
static init(load, loop, resize) {
GL.#cbLoad = load
GL.#cbLoop = loop
GL.#cbResize = resize
//addEventListener('load', GL.#onLoad)
GL.#onLoad()
}
static get gl() {
return GL.#gl
}
static get canvas() {
return GL.#canvas
}
static get frame() {
return GL.#frame
}
static get time() {
return GL.#time
}
static get timeOffset() {
return GL.#timeOffset
}
static get aspectRatio() {
return GL.#aspectRatio
}
}