glre 0.18.0 → 0.20.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/native.ts DELETED
@@ -1,64 +0,0 @@
1
- import { useEffect, useMemo } from 'react'
2
- import { Dimensions } from 'react-native'
3
- import { useMutable } from 'reev/react'
4
- import { gl, createTF } from './index'
5
- import { frame } from 'refr'
6
- import type { GL } from './types'
7
- import type { Fun } from 'refr'
8
-
9
- export const useGL = (props: Partial<GL> = {}, self = gl) => {
10
- const change = () => {
11
- self.resize(
12
- void 0,
13
- self.gl.drawingBufferWidth,
14
- self.gl.drawingBufferHeight
15
- )
16
- }
17
- const memo1 = useMutable(props) as Partial<GL>
18
- const memo2 = useMutable({
19
- ref(gl: any) {
20
- self.el = {}
21
- self.gl = gl
22
- self.mount()
23
- },
24
- mount() {
25
- self.init()
26
- change()
27
- frame.start()
28
- Dimensions.addEventListener('change', change)
29
- },
30
- clean() {
31
- self(memo2)(memo1)
32
- frame.cancel()
33
- },
34
- }) as Partial<GL>
35
-
36
- useEffect(() => self.clean, [self])
37
-
38
- return useMemo(() => self(memo2)(memo1), [self, memo2, memo1])
39
- }
40
- export const useTF = (props: Partial<GL>, self = gl) => {
41
- const memo = useMutable(props) as Partial<GL>
42
- const tf = useMemo(() => createTF(memo, self), [memo, self])
43
- useEffect(() => void tf.mount() || tf.clean, [self])
44
- return tf
45
- }
46
-
47
- export const useTexture = (props: any, self = gl) => {
48
- return self.texture(props)
49
- }
50
-
51
- export const useAttribute = (props: any, self = gl) => {
52
- return self.attribute(props)
53
- }
54
-
55
- export const useUniform = (props: any, self = gl) => {
56
- return self.uniform(props)
57
- }
58
-
59
- export const useFrame = (fun: Fun, self = gl) => {
60
- const ref = useMutable(fun)
61
- useEffect(() => self.frame(fun), [])
62
- useEffect(() => () => self.frame(ref), [])
63
- return self
64
- }
package/qwik.ts DELETED
@@ -1,42 +0,0 @@
1
- import { useSignal, useTask$, useVisibleTask$ } from '@builder.io/qwik'
2
- import { gl } from './index'
3
- import { frame } from 'refr'
4
- import type { GL } from './types'
5
- import type { Fun } from 'reev/types'
6
-
7
- export const useGL = (props?: any, self = gl as unknown as GL) => {
8
- const ref = useSignal<Element>()
9
- useVisibleTask$(({ cleanup }) => {
10
- self(props)
11
- self.el = self.target = ref.value
12
- self.gl = self.target.getContext('webgl2')
13
- self.init()
14
- self.resize()
15
- frame.start()
16
- window.addEventListener('resize', self.resize)
17
- window.addEventListener('mousemove', self.mousemove)
18
- cleanup(() => {
19
- self.clean()
20
- window.removeEventListener('resize', self.resize)
21
- window.removeEventListener('mousemove', self.mousemove)
22
- })
23
- })
24
- return self
25
- }
26
-
27
- export const useTexture = (props: any, self = gl) => {
28
- return self?.texture(props)
29
- }
30
-
31
- export const useAttribute = (props: any, self = gl) => {
32
- return self.attribute(props)
33
- }
34
-
35
- export const useUniform = (props: any, self = gl) => {
36
- return self.uniform(props)
37
- }
38
-
39
- export const useFrame = (fun: Fun, self = gl) => {
40
- useTask$(() => self.frame(fun))
41
- return self
42
- }
package/react.ts DELETED
@@ -1,59 +0,0 @@
1
- import { useEffect, useMemo } from 'react'
2
- import { createTF, gl } from './index'
3
- import { frame } from 'refr'
4
- import { useMutable } from 'reev/react'
5
- import type { GL } from './types'
6
- import type { Fun } from 'refr'
7
-
8
- export const useGL = (props: Partial<GL> = {}, self = gl) => {
9
- const memo1 = useMutable(props) as Partial<GL>
10
- const memo2 = useMutable({
11
- ref(target: unknown) {
12
- if (target) {
13
- self.target = target
14
- self.mount()
15
- } else self.clean()
16
- },
17
- mount() {
18
- self.el = self.target
19
- self.gl = self.target.getContext('webgl2')
20
- self.init()
21
- self.resize()
22
- frame.start()
23
- window.addEventListener('resize', self.resize)
24
- self.el.addEventListener('mousemove', self.mousemove)
25
- },
26
- clean() {
27
- self(memo2)(memo1)
28
- frame.cancel()
29
- window.removeEventListener('resize', self.resize)
30
- },
31
- }) as Partial<GL>
32
-
33
- return useMemo(() => self(memo2)(memo1), [self, memo1, memo2])
34
- }
35
-
36
- export const useTF = (props: Partial<GL>, self = gl) => {
37
- const memo = useMutable(props) as Partial<GL>
38
- const tf = useMemo(() => createTF(memo, self), [memo, self])
39
- useEffect(() => void tf.mount() || tf.clean, [self])
40
- return tf
41
- }
42
-
43
- export const useTexture = (props: any, self = gl) => {
44
- return self.texture(props)
45
- }
46
-
47
- export const useAttribute = (props: any, self = gl) => {
48
- return self.attribute(props)
49
- }
50
-
51
- export const useUniform = (props: any, self = gl) => {
52
- return self.uniform(props)
53
- }
54
-
55
- export const useFrame = (fun: Fun, self = gl) => {
56
- useEffect(() => void self.frame(fun), [])
57
- useEffect(() => () => self.frame(fun), [])
58
- return self
59
- }
package/solid.ts DELETED
@@ -1,52 +0,0 @@
1
- import { onMount, onCleanup } from 'solid-js'
2
- import { frame } from 'refr'
3
- import { createTF, gl } from './index'
4
- import { GL } from './types'
5
- import type { Fun } from 'reev/types'
6
-
7
- export const onGL = (props?: Partial<GL>, self = gl) => {
8
- const memo = {
9
- ref(target: unknown) {
10
- if (target) {
11
- self.target = target
12
- self.mount()
13
- }
14
- },
15
- mount() {
16
- self.el = self.target
17
- self.gl = self.target.getContext('webgl2')
18
- self.init()
19
- self.resize()
20
- frame.start()
21
- window.addEventListener('resize', self.resize)
22
- window.addEventListener('mousemove', self.mousemove)
23
- },
24
- clean() {
25
- self(props)(memo)
26
- frame.cancel()
27
- window.removeEventListener('resize', self.resize)
28
- window.removeEventListener('mousemove', self.mousemove)
29
- },
30
- }
31
- onCleanup(self.clean)
32
- return self(props)(memo)
33
- }
34
-
35
- export const onTF = (props?: Partial<GL>, self = gl) => {
36
- const tf = createTF(props, self)
37
- onMount(() => tf.mount())
38
- onCleanup(() => tf.clean())
39
- return tf
40
- }
41
-
42
- export const onFrame = (fun: Fun, self = gl) => {
43
- onMount(() => self('render', fun))
44
- }
45
-
46
- export const setTexture = (props: any, self = gl) => {
47
- return self.texture(props)
48
- }
49
-
50
- export const setAttribute = (props: any, self = gl) => {
51
- return self.attribute(props)
52
- }
@@ -1,5 +0,0 @@
1
- describe('core', () => {
2
- it('should work', () => {
3
- expect(true).toBeTruthy()
4
- })
5
- })
@@ -1,43 +0,0 @@
1
- import {
2
- interleave,
3
- isTemplateLiteral,
4
- concat,
5
- switchUniformType,
6
- } from 'glre/utils'
7
-
8
- describe('utils', () => {
9
- const _ = (str, ...args) => [str, args] as [any, any]
10
- const _0 = _`foo${false}bar${undefined}baz${null}`
11
- const _1 = _`foo${ 0 }bar${ NaN }baz${ -1 }`
12
- it('interleave merge strings', () => {
13
- expect(interleave(..._0)).toEqual(`foofalsebarundefinedbaznull`)
14
- expect(interleave(..._1)).toEqual(`foo0barNaNbaz-1`)
15
- })
16
- it('isTemplateLiteral', () => {
17
- expect(isTemplateLiteral(_0[0])).toEqual(true)
18
- expect(isTemplateLiteral(_1[0])).toEqual(true)
19
- })
20
-
21
- const headerUniform = `uniform vec2 iMouse;`;
22
- const withUniform = `void main() { gl_FragColor = vec4(iMouse, 0., 1.); }`
23
- const noneUniform = `void main() { gl_FragColor = vec4(0., 1., 0., 1.); }`
24
- it.each`
25
- i | key | shader | toBe
26
- ${0} | ${void 0} | ${ withUniform} | ${headerUniform + withUniform}
27
- ${1} | ${"iMouse"} | ${ withUniform} | ${headerUniform + withUniform}
28
- ${2} | ${"iMouse"} | ${ noneUniform} | ${noneUniform}
29
- ${3} | ${void 0} | ${headerUniform + withUniform} | ${headerUniform + withUniform}
30
- ${4} | ${"iMouse"} | ${headerUniform + withUniform} | ${headerUniform + withUniform}
31
- ${5} | ${"iMouse"} | ${headerUniform + noneUniform} | ${headerUniform + noneUniform}
32
- `('concat $i', ({ key, shader, toBe }) => {
33
- expect(concat(shader, headerUniform, key)).toBe(toBe)
34
- })
35
- it.each`
36
- i | uniformType | uniformKey | isMatrix | value
37
- ${0} | ${'uniform1f'} | ${'float'} | ${false} | ${10}
38
- ${1} | ${'uniform2fv'} | ${'vec2'} | ${false} | ${[0, 1]}
39
- ${2} | ${`uniformMatrix2fv`} | ${'mat2'} | ${true} | ${[0, 1, 2, 3]}
40
- `('switchUniformType $i', ({ value, isMatrix, uniformType, uniformKey }) => {
41
- expect(switchUniformType(value, isMatrix)).toEqual([uniformType, uniformKey])
42
- })
43
- })
package/tsup.config.ts DELETED
@@ -1,16 +0,0 @@
1
- import { defineConfig } from 'tsup'
2
- import { defaultConfig } from '../../tsup.config.base'
3
-
4
- export default defineConfig((options) => {
5
- return defaultConfig(
6
- {
7
- entry: [
8
- 'index.ts',
9
- 'native.ts',
10
- 'react.ts',
11
- 'solid.ts',
12
- ],
13
- },
14
- options
15
- )
16
- })
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
- }