glre 0.19.0 → 0.21.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/types.ts DELETED
@@ -1,98 +0,0 @@
1
- import type { Queue } from 'refr/types'
2
- import type { Nested, EventState } from 'reev/types'
3
-
4
- export type Uniform = number | number[]
5
-
6
- export type Attribute = number[]
7
-
8
- export type Attributes = Record<string, Attribute>
9
-
10
- export type Uniforms = Record<string, Uniform>
11
-
12
- export type GL = EventState<{
13
- /**
14
- * initial value
15
- */
16
- id: string
17
- width: number
18
- height: number
19
- size: [number, number]
20
- mouse: [number, number]
21
- count: number
22
- vs: string
23
- fs: string
24
- vert: string
25
- frag: string
26
- vertex: string
27
- fragment: string
28
- varying: string
29
- int: PrecisionMode
30
- float: PrecisionMode
31
- sampler2D: PrecisionMode
32
- samplerCube: PrecisionMode
33
- lastActiveUnit: number
34
-
35
- /**
36
- * core state
37
- */
38
- gl: any
39
- pg: any
40
- el: any
41
- frame: Queue
42
- target: any
43
- stride: Nested<number>
44
- // @TODO Nested<(key: string, value: number: number[], ibo: number[]) => number>
45
- location: Nested<any>
46
- activeUnit: Nested<number>
47
- default: any
48
-
49
- /**
50
- * events
51
- */
52
- ref?: any
53
- init(varying?: string[]): void
54
- mount(): void
55
- clean(): void
56
- render(): void
57
- mousemove(e: Event): void
58
- resize(e?: Event, width?: number, height?: number): void
59
- load(e?: Event, image?: HTMLImageElement): void
60
-
61
- /**
62
- * setter
63
- */
64
- uniform(key: string, value: Uniform): GL
65
- uniform(target: { [key: string]: Uniform }): GL
66
- texture(key: string, value: string): GL
67
- texture(target: { [key: string]: string }): GL
68
- attribute(key: string, value: Attribute, iboValue?: Attribute): GL
69
- attribute(target: { [key: string]: Attribute }): GL
70
- // config(key?: keyof GL, value?: GL[keyof GL]): GL
71
- // config(target?: Partial<GL>): GL
72
-
73
- /**
74
- * short hands
75
- */
76
- clear(key?: GLClearMode): void
77
- viewport(size?: [number, number]): void
78
- drawArrays(key?: GLDrawMode): void
79
- drawElements(key?: GLDrawMode): void
80
- }>
81
-
82
- export type PrecisionMode = 'highp' | 'mediump' | 'lowp'
83
-
84
- export type GLClearMode =
85
- | 'COLOR_BUFFER_BIT'
86
- | 'DEPTH_BUFFER_BIT'
87
- | 'STENCIL_BUFFER_BIT'
88
-
89
- export type GLDrawMode =
90
- | 'POINTS'
91
- | 'LINE_STRIP'
92
- | 'LINE_LOOP'
93
- | 'LINES'
94
- | 'TRIANGLE_STRIP'
95
- | 'TRIANGLE_FAN'
96
- | 'TRIANGLES'
97
-
98
- export type GLDrawType = 'UNSIGNED_BYTE' | 'UNSIGNED_SHORT' | 'UNSIGNED_INT'
package/utils.ts DELETED
@@ -1,210 +0,0 @@
1
- /**
2
- * utils
3
- */
4
- export const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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 const 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
- }