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/package.json CHANGED
@@ -1,67 +1,165 @@
1
- {
2
- "name": "glre",
3
- "author": "tseijp",
4
- "license": "MIT",
5
- "version": "0.15.0",
6
- "module": "index.js",
7
- "types": "index.ts",
8
- "main": "index.cjs.js",
9
- "umd": "index.develop.js",
10
- "private": false,
11
- "sideEffect": false,
12
- "publishConfig": {
13
- "access": "public"
14
- },
15
- "dependencies": {
16
- "reev": "0.10.0",
17
- "refr": "0.3.0"
18
- },
19
- "keywords": [
20
- "glsl",
21
- "webgl",
22
- "hooks",
23
- "react",
24
- "reactjs",
25
- "reactive",
26
- "solid",
27
- "solidjs",
28
- "typescript"
29
- ],
30
- "peerDependencies": {
31
- "expo": "*",
32
- "expo-gl": "*",
33
- "react": ">=16.8",
34
- "react-dom": ">=16.8",
35
- "solid-js": "*"
36
- },
37
- "peerDependenciesMeta": {
38
- "expo": {
39
- "optional": true
40
- },
41
- "expo-gl": {
42
- "optional": true
43
- },
44
- "react": {
45
- "optional": true
46
- },
47
- "react-dom": {
48
- "optional": true
49
- },
50
- "solid-js": {
51
- "optional": true
52
- }
53
- },
54
- "files": [
55
- "index.js",
56
- "index.cjs.js",
57
- "index.develop.js",
58
- "index.product.js",
59
- "index.ts",
60
- "native.ts",
61
- "qwik.ts",
62
- "react.ts",
63
- "solid.ts",
64
- "types.ts",
65
- "utils.ts"
66
- ]
67
- }
1
+ {
2
+ "name": "glre",
3
+ "version": "0.17.0",
4
+ "author": "tseijp",
5
+ "license": "MIT",
6
+ "private": false,
7
+ "sideEffect": false,
8
+ "publishConfig": {
9
+ "access": "public"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/tseijp/glre/issues"
13
+ },
14
+ "repository": "tseijp/glre",
15
+ "homepage": "https://glre.tsei.jp",
16
+ "keywords": [
17
+ "glsl",
18
+ "webgl",
19
+ "hooks",
20
+ "react",
21
+ "reactjs",
22
+ "reactive",
23
+ "solid",
24
+ "solidjs",
25
+ "typescript"
26
+ ],
27
+ "main": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "module": "./dist/index.mjs",
30
+ "exports": {
31
+ "./packages.json": "./packages.json",
32
+ ".": {
33
+ "types": "./dist/index.d.ts",
34
+ "import": {
35
+ "types": "./dist/index.d.ts",
36
+ "default": "./dist/index.mjs"
37
+ },
38
+ "module": {
39
+ "types": "./dist/index.d.ts",
40
+ "default": "./dist/index.mjs"
41
+ },
42
+ "require": "./dist/index.js",
43
+ "default": "./dist/index.js"
44
+ },
45
+ "./*": {
46
+ "types": "./dist/index.d.ts",
47
+ "import": {
48
+ "types": "./dist/index.d.ts",
49
+ "default": "./dist/index.mjs"
50
+ },
51
+ "module": {
52
+ "types": "./dist/index.d.ts",
53
+ "default": "./dist/index.mjs"
54
+ },
55
+ "require": "./dist/index.js",
56
+ "default": "./dist/index.js"
57
+ },
58
+ "./react": {
59
+ "types": "./dist/react.d.ts",
60
+ "import": {
61
+ "types": "./dist/react.d.ts",
62
+ "default": "./dist/react.mjs"
63
+ },
64
+ "module": {
65
+ "types": "./dist/react.d.ts",
66
+ "default": "./dist/react.mjs"
67
+ },
68
+ "require": "./dist/react.js",
69
+ "default": "./dist/react.js"
70
+ },
71
+ "./react/*": {
72
+ "types": "./dist/react.d.ts",
73
+ "import": {
74
+ "types": "./dist/react.d.ts",
75
+ "default": "./dist/react.mjs"
76
+ },
77
+ "module": {
78
+ "types": "./dist/react.d.ts",
79
+ "default": "./dist/react.mjs"
80
+ },
81
+ "require": "./dist/react.js",
82
+ "default": "./dist/react.js"
83
+ },
84
+ "./native": {
85
+ "types": "./dist/native.d.ts",
86
+ "import": {
87
+ "types": "./dist/native.d.ts",
88
+ "default": "./dist/native.mjs"
89
+ },
90
+ "module": {
91
+ "types": "./dist/native.d.ts",
92
+ "default": "./dist/native.mjs"
93
+ },
94
+ "require": "./dist/native.js",
95
+ "default": "./dist/native.js"
96
+ },
97
+ "./native/*": {
98
+ "types": "./dist/native.d.ts",
99
+ "import": {
100
+ "types": "./dist/native.d.ts",
101
+ "default": "./dist/native.mjs"
102
+ },
103
+ "module": {
104
+ "types": "./dist/native.d.ts",
105
+ "default": "./dist/native.mjs"
106
+ },
107
+ "require": "./dist/native.js",
108
+ "default": "./dist/native.js"
109
+ },
110
+ "./solid": {
111
+ "types": "./dist/solid.d.ts",
112
+ "import": {
113
+ "types": "./dist/solid.d.ts",
114
+ "default": "./dist/solid.mjs"
115
+ },
116
+ "module": {
117
+ "types": "./dist/solid.d.ts",
118
+ "default": "./dist/solid.mjs"
119
+ },
120
+ "require": "./dist/solid.js",
121
+ "default": "./dist/solid.js"
122
+ },
123
+ "./solid/*": {
124
+ "types": "./dist/solid.d.ts",
125
+ "import": {
126
+ "types": "./dist/solid.d.ts",
127
+ "default": "./dist/solid.mjs"
128
+ },
129
+ "module": {
130
+ "types": "./dist/solid.d.ts",
131
+ "default": "./dist/solid.mjs"
132
+ },
133
+ "require": "./dist/solid.js",
134
+ "default": "./dist/solid.js"
135
+ }
136
+ },
137
+ "dependencies": {
138
+ "reev": "0.12.0",
139
+ "refr": "0.3.0"
140
+ },
141
+ "peerDependencies": {
142
+ "expo": "*",
143
+ "expo-gl": "*",
144
+ "react": ">=16.8",
145
+ "react-dom": ">=16.8",
146
+ "solid-js": "*"
147
+ },
148
+ "peerDependenciesMeta": {
149
+ "expo": {
150
+ "optional": true
151
+ },
152
+ "expo-gl": {
153
+ "optional": true
154
+ },
155
+ "react": {
156
+ "optional": true
157
+ },
158
+ "react-dom": {
159
+ "optional": true
160
+ },
161
+ "solid-js": {
162
+ "optional": true
163
+ }
164
+ }
165
+ }
package/qwik.ts CHANGED
@@ -1,42 +1,42 @@
1
- import { useSignal, 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 function 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 function useTexture(props: any, self = gl) {
28
- return self?.texture(props)
29
- }
30
-
31
- export function useAttribute(props: any, self = gl) {
32
- return self.attribute(props)
33
- }
34
-
35
- export function useUniform(props: any, self = gl) {
36
- return self.uniform(props)
37
- }
38
-
39
- export function useFrame(fun: Fun, self = gl) {
40
- useTask$(() => self.frame(fun))
41
- return self
42
- }
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 CHANGED
@@ -1,60 +1,59 @@
1
- import { useState, useEffect, useMemo } from 'react'
2
- import { gl } from './index'
3
- import { frame } from 'refr'
4
- import { mutable } from 'reev'
5
- import type { GL } from './types'
6
- import type { Fun } from 'refr'
7
- import type { MutableArgs } from 'reev/types'
8
-
9
- export const useMutable = <T extends object>(...args: MutableArgs<T>) => {
10
- const [memo] = useState(() => mutable<T>())
11
- return memo(...args)
12
- }
13
-
14
- export const useGL = (props: Partial<GL> = {}, self = gl) => {
15
- const memo1 = useMutable(props) as Partial<GL>
16
- const memo2 = useMutable({
17
- ref(target: unknown) {
18
- if (target) {
19
- self.target = target
20
- self.mount()
21
- } else self.clean()
22
- },
23
- mount() {
24
- self.el = self.target
25
- self.gl = self.target.getContext('webgl2')
26
- self.init()
27
- self.resize()
28
- frame.start()
29
- window.addEventListener('resize', self.resize)
30
- window.addEventListener('mousemove', self.mousemove)
31
- },
32
- clean() {
33
- self(memo2)(memo1)
34
- frame.cancel()
35
- window.removeEventListener('resize', self.resize)
36
- window.removeEventListener('mousemove', self.mousemove)
37
- },
38
- }) as Partial<GL>
39
-
40
- return useMemo(() => self(memo2)(memo1), [self, memo1, memo2])
41
- }
42
-
43
- export function useTexture(props: any, self = gl) {
44
- return self.texture(props)
45
- }
46
-
47
- export function useAttribute(props: any, self = gl) {
48
- return self.attribute(props)
49
- }
50
-
51
- export function useUniform(props: any, self = gl) {
52
- return self.uniform(props)
53
- }
54
-
55
- export function useFrame(fun: Fun, self = gl) {
56
- const ref = useMutable(fun)
57
- useEffect(() => self.frame(fun), [])
58
- useEffect(() => () => self.frame(ref), [])
59
- return self
60
- }
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 CHANGED
@@ -1,44 +1,52 @@
1
- import { onMount, onCleanup } from 'solid-js'
2
- import { frame } from 'refr'
3
- import { gl } from './index'
4
- import type { Fun } from 'reev/types'
5
-
6
- export function createGL(props?: any, self = gl) {
7
- onCleanup(self.clean)
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
- return self(props)(memo)
32
- }
33
-
34
- export function onFrame(fun: Fun, self = gl) {
35
- onMount(() => self('render', fun))
36
- }
37
-
38
- export function setTexture(props: any, self = gl) {
39
- return self.texture(props)
40
- }
41
-
42
- export function setAttribute(props: any, self = gl) {
43
- return self.attribute(props)
44
- }
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
+ }
@@ -0,0 +1,5 @@
1
+ describe('core', () => {
2
+ it('should work', () => {
3
+ expect(true).toBeTruthy()
4
+ })
5
+ })
@@ -0,0 +1,43 @@
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 ADDED
@@ -0,0 +1,16 @@
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
+ })