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/dist/index.d.ts +10 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +15 -0
- package/dist/index.mjs.map +1 -0
- package/dist/native.d.ts +66 -0
- package/dist/native.js +15 -0
- package/dist/native.js.map +1 -0
- package/dist/native.mjs +15 -0
- package/dist/native.mjs.map +1 -0
- package/dist/react.d.ts +66 -0
- package/dist/react.js +15 -0
- package/dist/react.js.map +1 -0
- package/dist/react.mjs +15 -0
- package/dist/react.mjs.map +1 -0
- package/dist/solid.d.ts +65 -0
- package/dist/solid.js +15 -0
- package/dist/solid.js.map +1 -0
- package/dist/solid.mjs +15 -0
- package/dist/solid.mjs.map +1 -0
- package/dist/types-20446758.d.ts +78 -0
- package/index.ts +202 -158
- package/native.ts +64 -58
- package/package.json +165 -67
- package/qwik.ts +42 -42
- package/react.ts +59 -60
- package/solid.ts +52 -44
- package/test/index.test.ts +5 -0
- package/test/utils.test.ts +43 -0
- package/tsup.config.ts +16 -0
- package/types.ts +98 -98
- package/utils.ts +210 -210
- package/index.cjs.js +0 -1
- package/index.develop.js +0 -1
- package/index.js +0 -1
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
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
self.
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
self.
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
self.
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
self.
|
|
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
|
-
self.
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
self.
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
)
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
self.
|
|
150
|
-
|
|
151
|
-
self.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
+
}
|