glre 0.14.0 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/utils.ts CHANGED
@@ -1,210 +1,210 @@
1
- /**
2
- * utils
3
- */
4
- export function uniformType(value: number | number[], isMatrix = false) {
5
- let length = typeof value === 'number' ? 0 : value?.length
6
- if (!length) return `uniform1f`
7
- if (!isMatrix) return `uniform${length}fv`
8
- length = Math.sqrt(length) << 0
9
- return `uniformMatrix${length}fv`
10
- }
11
-
12
- export function vertexStride(
13
- count: number,
14
- value: number[],
15
- iboValue?: number[]
16
- ) {
17
- if (iboValue) count = Math.max(...iboValue) + 1
18
- const stride = value.length / count
19
- if (stride !== stride << 0)
20
- console.warn(`Vertex Stride Error: count ${count} is mismatch`)
21
- return stride << 0
22
- }
23
-
24
- /**
25
- * graphics
26
- */
27
- export function createShader(gl: any, source: string, type: unknown) {
28
- const shader = gl.createShader(type)
29
- gl.shaderSource(shader, source)
30
- gl.compileShader(shader)
31
- if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
32
- return shader
33
- } else console.warn(gl.getShaderInfoLog(shader))
34
- }
35
-
36
- export function createProgram(gl: any, vs: any, fs: any) {
37
- const program = gl.createProgram()
38
- gl.attachShader(program, vs)
39
- gl.attachShader(program, fs)
40
- gl.linkProgram(program)
41
- if (gl.getProgramParameter(program, gl.LINK_STATUS)) {
42
- gl.useProgram(program)
43
- return program
44
- } else {
45
- console.log(gl.getProgramInfoLog(program))
46
- return null
47
- }
48
- }
49
-
50
- export function createTfProgram(gl: any, vs: any, fs: any, varyings?: any) {
51
- const pg = gl.createProgram()
52
- gl.attachShader(pg, vs)
53
- gl.attachShader(pg, fs)
54
- gl.transformFeedbackVaryings(pg, varyings, gl.SEPARATE_ATTRIBS)
55
- gl.linkProgram(pg)
56
- if (gl.getProgramParameter(pg, gl.LINK_STATUS)) {
57
- gl.useProgram(pg)
58
- return pg
59
- } else {
60
- console.warn(gl.getProgramInfoLog(pg))
61
- return null
62
- }
63
- }
64
-
65
- export function createVbo(gl: any, data: number[]) {
66
- if (!data) return
67
- const vbo = gl.createBuffer()
68
- gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
69
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW)
70
- gl.bindBuffer(gl.ARRAY_BUFFER, null)
71
- return vbo
72
- }
73
-
74
- export function createIbo(gl: any, data?: number[]) {
75
- if (!data) return
76
- const ibo = gl.createBuffer()
77
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
78
- gl.bufferData(
79
- gl.ELEMENT_ARRAY_BUFFER,
80
- new Int16Array(data),
81
- gl.STATIC_DRAW
82
- )
83
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null)
84
- return ibo
85
- }
86
-
87
- export function createAttribute(
88
- gl: any,
89
- stride: number,
90
- location: any,
91
- vbo: any,
92
- ibo: any
93
- ) {
94
- gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
95
- gl.enableVertexAttribArray(location)
96
- gl.vertexAttribPointer(location, stride, gl.FLOAT, false, 0, 0)
97
- if (ibo) gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
98
- }
99
-
100
- export function createFramebuffer(gl: any, width: number, height: number) {
101
- const frameBuffer = gl.createFramebuffer()
102
- gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer)
103
- const renderBuffer = gl.createRenderbuffer()
104
- gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuffer)
105
- gl.renderbufferStorage(
106
- gl.RENDERBUFFER,
107
- gl.DEPTH_COMPONENT16,
108
- width,
109
- height
110
- )
111
- gl.framebufferRenderbuffer(
112
- gl.FRAMEBUFFER,
113
- gl.DEPTH_ATTACHMENT,
114
- gl.RENDERBUFFER,
115
- renderBuffer
116
- )
117
- const texture = gl.createTexture()
118
- gl.bindTexture(gl.TEXTURE_2D, texture)
119
- gl.texImage2D(
120
- gl.TEXTURE_2D,
121
- 0,
122
- gl.RGBA,
123
- width,
124
- height,
125
- 0,
126
- gl.RGBA,
127
- gl.UNSIGNED_BYTE,
128
- null
129
- )
130
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
131
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
132
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
133
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
134
- gl.framebufferTexture2D(
135
- gl.FRAMEBUFFER,
136
- gl.COLOR_ATTACHMENT0,
137
- gl.TEXTURE_2D,
138
- texture,
139
- 0
140
- )
141
- gl.bindTexture(gl.TEXTURE_2D, null)
142
- gl.bindRenderbuffer(gl.RENDERBUFFER, null)
143
- gl.bindFramebuffer(gl.FRAMEBUFFER, null)
144
- return { frameBuffer, renderBuffer, texture }
145
- }
146
-
147
- export function createFramebufferFloat(
148
- gl: any,
149
- ext: any,
150
- width: number,
151
- height: number
152
- ) {
153
- const flg =
154
- ext.textureFloat != null
155
- ? gl.FLOAT
156
- : ext.textureHalfFloat.HALF_FLOAT_OES
157
- const frameBuffer = gl.createFramebuffer()
158
- const texture = gl.createTexture()
159
- gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer)
160
- gl.bindTexture(gl.TEXTURE_2D, texture)
161
- gl.texImage2D(
162
- gl.TEXTURE_2D,
163
- 0,
164
- gl.RGBA,
165
- width,
166
- height,
167
- 0,
168
- gl.RGBA,
169
- flg,
170
- null
171
- )
172
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
173
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
174
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
175
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
176
- gl.framebufferTexture2D(
177
- gl.FRAMEBUFFER,
178
- gl.COLOR_ATTACHMENT0,
179
- gl.TEXTURE_2D,
180
- texture,
181
- 0
182
- )
183
- gl.bindTexture(gl.TEXTURE_2D, null)
184
- gl.bindFramebuffer(gl.FRAMEBUFFER, null)
185
- return { frameBuffer, texture }
186
- }
187
-
188
- export function createTexture(gl: any, img: any) {
189
- const texture = gl.createTexture()
190
- gl.bindTexture(gl.TEXTURE_2D, texture)
191
- gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img)
192
- gl.generateMipmap(gl.TEXTURE_2D)
193
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
194
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
195
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
196
- gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
197
- gl.bindTexture(gl.TEXTURE_2D, null)
198
- return texture
199
- }
200
-
201
- export function activeTexture(
202
- gl: any,
203
- location: any,
204
- activeUnit: any,
205
- texture: any
206
- ) {
207
- gl.uniform1i(location, activeUnit)
208
- gl.activeTexture(gl['TEXTURE' + activeUnit])
209
- gl.bindTexture(gl.TEXTURE_2D, texture)
210
- }
1
+ /**
2
+ * utils
3
+ */
4
+ export function uniformType(value: number | number[], isMatrix = false) {
5
+ let length = typeof value === 'number' ? 0 : value?.length
6
+ if (!length) return `uniform1f`
7
+ if (!isMatrix) return `uniform${length}fv`
8
+ length = Math.sqrt(length) << 0
9
+ return `uniformMatrix${length}fv`
10
+ }
11
+
12
+ export function vertexStride(
13
+ count: number,
14
+ value: number[],
15
+ iboValue?: number[]
16
+ ) {
17
+ if (iboValue) count = Math.max(...iboValue) + 1
18
+ const stride = value.length / count
19
+ if (stride !== stride << 0)
20
+ console.warn(`Vertex Stride Error: count ${count} is mismatch`)
21
+ return stride << 0
22
+ }
23
+
24
+ /**
25
+ * graphics
26
+ */
27
+ export function createShader(gl: any, source: string, type: unknown) {
28
+ const shader = gl.createShader(type)
29
+ gl.shaderSource(shader, source)
30
+ gl.compileShader(shader)
31
+ if (gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
32
+ return shader
33
+ } else throw 'Could not compile glsl\n\n' + gl.getShaderInfoLog(shader)
34
+ }
35
+
36
+ export function createProgram(gl: any, vs: any, fs: any) {
37
+ const program = gl.createProgram()
38
+ gl.attachShader(program, vs)
39
+ gl.attachShader(program, fs)
40
+ gl.linkProgram(program)
41
+ if (gl.getProgramParameter(program, gl.LINK_STATUS)) {
42
+ gl.useProgram(program)
43
+ return program
44
+ } else {
45
+ console.log(gl.getProgramInfoLog(program))
46
+ return null
47
+ }
48
+ }
49
+
50
+ export function createTfProgram(gl: any, vs: any, fs: any, varyings?: any) {
51
+ const pg = gl.createProgram()
52
+ gl.attachShader(pg, vs)
53
+ gl.attachShader(pg, fs)
54
+ gl.transformFeedbackVaryings(pg, varyings, gl.SEPARATE_ATTRIBS)
55
+ gl.linkProgram(pg)
56
+ if (gl.getProgramParameter(pg, gl.LINK_STATUS)) {
57
+ gl.useProgram(pg)
58
+ return pg
59
+ } else {
60
+ console.warn(gl.getProgramInfoLog(pg))
61
+ return null
62
+ }
63
+ }
64
+
65
+ export function createVbo(gl: any, data: number[]) {
66
+ if (!data) return
67
+ const vbo = gl.createBuffer()
68
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
69
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(data), gl.STATIC_DRAW)
70
+ gl.bindBuffer(gl.ARRAY_BUFFER, null)
71
+ return vbo
72
+ }
73
+
74
+ export function createIbo(gl: any, data?: number[]) {
75
+ if (!data) return
76
+ const ibo = gl.createBuffer()
77
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
78
+ gl.bufferData(
79
+ gl.ELEMENT_ARRAY_BUFFER,
80
+ new Int16Array(data),
81
+ gl.STATIC_DRAW
82
+ )
83
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null)
84
+ return ibo
85
+ }
86
+
87
+ export function createAttribute(
88
+ gl: any,
89
+ stride: number,
90
+ location: any,
91
+ vbo: any,
92
+ ibo: any
93
+ ) {
94
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
95
+ gl.enableVertexAttribArray(location)
96
+ gl.vertexAttribPointer(location, stride, gl.FLOAT, false, 0, 0)
97
+ if (ibo) gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
98
+ }
99
+
100
+ export function createFramebuffer(gl: any, width: number, height: number) {
101
+ const frameBuffer = gl.createFramebuffer()
102
+ gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer)
103
+ const renderBuffer = gl.createRenderbuffer()
104
+ gl.bindRenderbuffer(gl.RENDERBUFFER, renderBuffer)
105
+ gl.renderbufferStorage(
106
+ gl.RENDERBUFFER,
107
+ gl.DEPTH_COMPONENT16,
108
+ width,
109
+ height
110
+ )
111
+ gl.framebufferRenderbuffer(
112
+ gl.FRAMEBUFFER,
113
+ gl.DEPTH_ATTACHMENT,
114
+ gl.RENDERBUFFER,
115
+ renderBuffer
116
+ )
117
+ const texture = gl.createTexture()
118
+ gl.bindTexture(gl.TEXTURE_2D, texture)
119
+ gl.texImage2D(
120
+ gl.TEXTURE_2D,
121
+ 0,
122
+ gl.RGBA,
123
+ width,
124
+ height,
125
+ 0,
126
+ gl.RGBA,
127
+ gl.UNSIGNED_BYTE,
128
+ null
129
+ )
130
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
131
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
132
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
133
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
134
+ gl.framebufferTexture2D(
135
+ gl.FRAMEBUFFER,
136
+ gl.COLOR_ATTACHMENT0,
137
+ gl.TEXTURE_2D,
138
+ texture,
139
+ 0
140
+ )
141
+ gl.bindTexture(gl.TEXTURE_2D, null)
142
+ gl.bindRenderbuffer(gl.RENDERBUFFER, null)
143
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null)
144
+ return { frameBuffer, renderBuffer, texture }
145
+ }
146
+
147
+ export function createFramebufferFloat(
148
+ gl: any,
149
+ ext: any,
150
+ width: number,
151
+ height: number
152
+ ) {
153
+ const flg =
154
+ ext.textureFloat != null
155
+ ? gl.FLOAT
156
+ : ext.textureHalfFloat.HALF_FLOAT_OES
157
+ const frameBuffer = gl.createFramebuffer()
158
+ const texture = gl.createTexture()
159
+ gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffer)
160
+ gl.bindTexture(gl.TEXTURE_2D, texture)
161
+ gl.texImage2D(
162
+ gl.TEXTURE_2D,
163
+ 0,
164
+ gl.RGBA,
165
+ width,
166
+ height,
167
+ 0,
168
+ gl.RGBA,
169
+ flg,
170
+ null
171
+ )
172
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
173
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
174
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
175
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
176
+ gl.framebufferTexture2D(
177
+ gl.FRAMEBUFFER,
178
+ gl.COLOR_ATTACHMENT0,
179
+ gl.TEXTURE_2D,
180
+ texture,
181
+ 0
182
+ )
183
+ gl.bindTexture(gl.TEXTURE_2D, null)
184
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null)
185
+ return { frameBuffer, texture }
186
+ }
187
+
188
+ export function createTexture(gl: any, img: any) {
189
+ const texture = gl.createTexture()
190
+ gl.bindTexture(gl.TEXTURE_2D, texture)
191
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img)
192
+ gl.generateMipmap(gl.TEXTURE_2D)
193
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
194
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
195
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
196
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
197
+ gl.bindTexture(gl.TEXTURE_2D, null)
198
+ return texture
199
+ }
200
+
201
+ export function activeTexture(
202
+ gl: any,
203
+ location: any,
204
+ activeUnit: any,
205
+ texture: any
206
+ ) {
207
+ gl.uniform1i(location, activeUnit)
208
+ gl.activeTexture(gl['TEXTURE' + activeUnit])
209
+ gl.bindTexture(gl.TEXTURE_2D, texture)
210
+ }