glre 0.15.0 → 0.17.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/index.ts CHANGED
@@ -1,158 +1,202 @@
1
- import { event, durable, nested } from 'reev'
2
- import { queue, frame } from 'refr'
3
- import {
4
- uniformType,
5
- vertexStride,
6
- createProgram,
7
- createTfProgram,
8
- createShader,
9
- createAttribute,
10
- createTexture,
11
- createVbo,
12
- createIbo,
13
- activeTexture,
14
- } from './utils'
15
- import type { GL } from './types'
16
-
17
- const a_position = [-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]
18
-
19
- const _defaultVertex = `
20
- attribute vec4 a_position;
21
- void main() {
22
- gl_Position = a_position;
23
- }
24
- `
25
-
26
- const _defaultFragment = `
27
- precision mediump float;
28
- uniform vec2 iResolution;
29
- void main() {
30
- gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
31
- }
32
- `
33
-
34
- let iTime = performance.now(),
35
- iPrevTime = 0,
36
- iDeltaTime = 0
37
-
38
- const self = event<Partial<GL>>({
39
- vertex: _defaultVertex,
40
- fragment: _defaultFragment,
41
- size: [0, 0],
42
- mouse: [0, 0],
43
- count: 6,
44
- counter: 0,
45
- init(varying?: string[]) {
46
- const gl = self.gl
47
- const _v = self.vs || self.vert || self.vertex
48
- const _f = self.fs || self.frag || self.fragment
49
- const vs = createShader(gl, _v, gl.VERTEX_SHADER)
50
- const fs = createShader(gl, _f, gl.FRAGMENT_SHADER)
51
- if (self.count === 6) self.attribute({ a_position })
52
- frame(() => void self.render() || 1)
53
- self.pg = varying
54
- ? createTfProgram(gl, vs, fs, varying)
55
- : createProgram(gl, vs, fs)
56
- self.lastActiveUnit = 0
57
- self.activeUnit = nested(() => self.lastActiveUnit++)
58
- self.location = nested((key, isAttribute = false) => {
59
- return isAttribute
60
- ? gl?.getAttribLocation(self.pg, key)
61
- : gl?.getUniformLocation(self.pg, key)
62
- })
63
- },
64
- render() {
65
- self.gl.useProgram(self.pg)
66
- self.frame.flush()
67
- iPrevTime = iTime
68
- iTime = performance.now() / 1000
69
- iDeltaTime = iTime - iPrevTime
70
- self.uniform({ iTime, iPrevTime, iDeltaTime })
71
- },
72
- _uniform(key: string, value = 0, isMatrix = false) {
73
- const type = uniformType(value, isMatrix)
74
- self.frame(() => {
75
- const loc = self.location(key)
76
- if (isMatrix) self.gl[type](loc, false, value)
77
- else self.gl[type](loc, value)
78
- })
79
- },
80
- _attribute(key: string, value: number[], iboValue?: number[]) {
81
- self.frame(() => {
82
- const loc = self.location(key, true)
83
- const vbo = createVbo(self.gl, value)
84
- const ibo = createIbo(self.gl, iboValue)
85
- const stride = vertexStride(self.count, value, iboValue)
86
- createAttribute(self.gl, stride, loc, vbo, ibo)
87
- })
88
- },
89
- _texture(key: string, src: string) {
90
- if (typeof window === 'undefined') return
91
- const image = new Image()
92
- image.addEventListener(
93
- 'load',
94
- (_) => self.load(_, image),
95
- false
96
- )
97
- Object.assign(image, {
98
- src,
99
- alt: key,
100
- crossOrigin: 'anonymous',
101
- })
102
- },
103
- resize(
104
- _e: any,
105
- width = window.innerWidth,
106
- height = window.innerHeight
107
- ) {
108
- self.size[0] = self.el.width = width
109
- self.size[1] = self.el.height = height
110
- self.uniform('iResolution', self.size)
111
- },
112
- mousemove(_e: any, x = _e.clientX, y = _e.clientY) {
113
- const [w, h] = self.size
114
- self.mouse[0] = (x - w / 2) / (w / 2)
115
- self.mouse[1] = -(y - h / 2) / (h / 2)
116
- self.uniform('iMouse', self.mouse)
117
- },
118
- load(_: any, image: any) {
119
- self.frame(() => {
120
- const loc = self.location(image.alt)
121
- const unit = self.activeUnit(image.alt)
122
- const texture = createTexture(self.gl, image)
123
- self.frame(() => {
124
- activeTexture(self.gl, loc, unit, texture)
125
- return true
126
- })
127
- })
128
- },
129
- clear(key = 'COLOR_BUFFER_BIT') {
130
- self.gl.clear(self.gl[key])
131
- },
132
- viewport(size: number[] = self.size) {
133
- self.gl.viewport(0, 0, ...size)
134
- },
135
- drawArrays(mode = 'TRIANGLES') {
136
- self.gl.drawArrays(self.gl[mode], 0, self.count)
137
- },
138
- drawElements(mode = 'TRIANGLES', type = 'UNSIGNED_SHORT') {
139
- self.gl.drawElements(
140
- self.gl[mode],
141
- self.count,
142
- self.gl[type],
143
- 0
144
- )
145
- },
146
- }) as GL
147
-
148
- self.frame = queue()
149
- self.texture = durable(self._texture)
150
- self.uniform = durable(self._uniform)
151
- self.attribute = durable(self._attribute)
152
-
153
- // @TODO
154
- // export const clone = gl.clone()
155
-
156
- export { self as gl }
157
-
158
- export default self
1
+ import { event, durable, nested } from 'reev'
2
+ import { queue, frame } from 'refr'
3
+ import {
4
+ uniformType,
5
+ vertexStride,
6
+ createProgram,
7
+ createTfProgram,
8
+ createShader,
9
+ createAttribute,
10
+ createTexture,
11
+ createVbo,
12
+ createIbo,
13
+ activeTexture,
14
+ } from './utils'
15
+ import type { GL } from './types'
16
+
17
+ const a_position = [-1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, 1]
18
+
19
+ const _defaultVertex = `
20
+ attribute vec4 a_position;
21
+ void main() {
22
+ gl_Position = a_position;
23
+ }
24
+ `
25
+
26
+ const _defaultFragment = `
27
+ precision mediump float;
28
+ uniform vec2 iResolution;
29
+ void main() {
30
+ gl_FragColor = vec4(fract(gl_FragCoord.xy / iResolution), 0, 1);
31
+ }
32
+ `
33
+
34
+ let iTime = performance.now(),
35
+ iPrevTime = 0,
36
+ iDeltaTime = 0
37
+
38
+ export const createGL = (props?: Partial<GL>) => {
39
+ const init = () => {
40
+ self(props)
41
+ const gl = self.gl
42
+ const _v = self.vs || self.vert || self.vertex
43
+ const _f = self.fs || self.frag || self.fragment
44
+ const vs = createShader(gl, _v, gl.VERTEX_SHADER)
45
+ const fs = createShader(gl, _f, gl.FRAGMENT_SHADER)
46
+ if (self.count === 6) self.attribute({ a_position })
47
+ frame(() => void self.render() || 1)
48
+ self.pg = self.varying
49
+ ? createTfProgram(gl, vs, fs, self.varying)
50
+ : createProgram(gl, vs, fs)
51
+ self.lastActiveUnit = 0
52
+ self.activeUnit = nested(() => self.lastActiveUnit++)
53
+ self.location = nested((key, isAttribute = false) => {
54
+ return isAttribute
55
+ ? gl?.getAttribLocation(self.pg, key)
56
+ : gl?.getUniformLocation(self.pg, key)
57
+ })
58
+ }
59
+
60
+ const render = () => {
61
+ self.gl.useProgram(self.pg)
62
+ self.frame.flush()
63
+ iPrevTime = iTime
64
+ iTime = performance.now() / 1000
65
+ iDeltaTime = iTime - iPrevTime
66
+ self.uniform({ iTime, iPrevTime, iDeltaTime })
67
+ }
68
+
69
+ const resize = (
70
+ _e: any,
71
+ width = self.width || window.innerWidth,
72
+ height = self.height || window.innerHeight
73
+ ) => {
74
+ self.size[0] = self.el.width = width
75
+ self.size[1] = self.el.height = height
76
+ self.uniform('iResolution', self.size)
77
+ }
78
+
79
+ const mousemove = (_e: any, x = _e.clientX, y = _e.clientY) => {
80
+ const [w, h] = self.size
81
+ const { top, left } = self.el.getBoundingClientRect()
82
+ self.mouse[0] = (x - top - w / 2) / (w / 2)
83
+ self.mouse[1] = -(y - left - h / 2) / (h / 2)
84
+ self.uniform('iMouse', self.mouse)
85
+ }
86
+
87
+ const load = (_: any, image: any) => {
88
+ self.frame(() => {
89
+ const loc = self.location(image.alt)
90
+ const unit = self.activeUnit(image.alt)
91
+ const tex = createTexture(self.gl, image)
92
+ self.frame(() => {
93
+ activeTexture(self.gl, loc, unit, tex)
94
+ return true
95
+ })
96
+ })
97
+ }
98
+
99
+ const clear = (key = 'COLOR_BUFFER_BIT') => {
100
+ self.gl.clear(self.gl[key])
101
+ }
102
+
103
+ const viewport = (size: number[] = self.size) => {
104
+ self.gl.viewport(0, 0, ...size)
105
+ }
106
+
107
+ const drawArrays = (mode = 'TRIANGLES') => {
108
+ self.gl.drawArrays(self.gl[mode], 0, self.count)
109
+ }
110
+
111
+ const drawElements = (mode = 'TRIANGLES', type = 'UNSIGNED_SHORT') => {
112
+ mode = self.gl[mode]
113
+ type = self.gl[type]
114
+ self.gl.drawElements(mode, self.count, type, 0)
115
+ }
116
+
117
+ const self = event<Partial<GL>>({
118
+ vertex: _defaultVertex,
119
+ fragment: _defaultFragment,
120
+ size: [0, 0],
121
+ mouse: [0, 0],
122
+ count: 6,
123
+ counter: 0,
124
+ init,
125
+ render,
126
+ resize,
127
+ mousemove,
128
+ load,
129
+ clear,
130
+ viewport,
131
+ drawArrays,
132
+ drawElements,
133
+ }) as GL
134
+
135
+ const _texture = (
136
+ alt: string,
137
+ src: string,
138
+ crossOrigin = 'anonymous'
139
+ ) => {
140
+ if (typeof window === 'undefined') return
141
+ const image = new Image()
142
+ const callback = (_: any) => self.load(_, image)
143
+ image.addEventListener('load', callback, false)
144
+ Object.assign(image, { src, alt, crossOrigin })
145
+ }
146
+
147
+ const _uniform = (key: string, value = 0, isMatrix = false) => {
148
+ const type = uniformType(value, isMatrix)
149
+ self.frame(() => {
150
+ const loc = self.location(key)
151
+ if (isMatrix) self.gl[type](loc, false, value)
152
+ else self.gl[type](loc, value)
153
+ })
154
+ }
155
+
156
+ const _attribute = (
157
+ key: string,
158
+ value: number[],
159
+ iboValue?: number[]
160
+ ) => {
161
+ self.frame(() => {
162
+ const loc = self.location(key, true)
163
+ const vbo = createVbo(self.gl, value)
164
+ const ibo = createIbo(self.gl, iboValue)
165
+ const n = self.count
166
+ const stride = vertexStride(n, value, iboValue)
167
+ createAttribute(self.gl, stride, loc, vbo, ibo)
168
+ })
169
+ }
170
+
171
+ self.frame = queue()
172
+ self.texture = durable(_texture)
173
+ self.uniform = durable(_uniform)
174
+ self.attribute = durable(_attribute)
175
+
176
+ return self
177
+ }
178
+
179
+ export const gl = createGL()
180
+
181
+ export const createTF = (props?: Partial<GL>, self = gl) => {
182
+ const mount = () => {
183
+ tf(props)
184
+ tf.el = self.el
185
+ tf.gl = self.gl
186
+ tf.frame = self.frame
187
+ tf.init()
188
+ self({ resize: tf.resize, mousemove: tf.mousemove })
189
+ }
190
+
191
+ const clean = () => {
192
+ tf(props)
193
+ self({ resize: tf.resize, mousemove: tf.mousemove })
194
+ }
195
+
196
+ const tf = createGL()
197
+
198
+ tf({ mount, clean })
199
+ return tf
200
+ }
201
+
202
+ export default self
package/native.ts CHANGED
@@ -1,58 +1,64 @@
1
- import { useEffect, useMemo } from 'react'
2
- import { Dimensions } from 'react-native'
3
- import { useMutable } from 'reev/react'
4
- import { gl } 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
-
41
- export function useTexture(props: any, self = gl) {
42
- return self.texture(props)
43
- }
44
-
45
- export function useAttribute(props: any, self = gl) {
46
- return self.attribute(props)
47
- }
48
-
49
- export function useUniform(props: any, self = gl) {
50
- return self.uniform(props)
51
- }
52
-
53
- export function useFrame(fun: Fun, self = gl) {
54
- const ref = useMutable(fun)
55
- useEffect(() => self.frame(fun), [])
56
- useEffect(() => () => self.frame(ref), [])
57
- return self
58
- }
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
+ }