glre 0.40.0 → 0.41.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/react.d.ts CHANGED
@@ -155,7 +155,7 @@ type Color = XImpl<'color'>;
155
155
  type Mat2 = XImpl<'mat2'>;
156
156
  type Mat3 = XImpl<'mat3'>;
157
157
  type Mat4 = XImpl<'mat4'>;
158
- type Texture = XImpl<'texture'>;
158
+ type Texture$1 = XImpl<'texture'>;
159
159
  type Sampler2D = XImpl<'sampler2D'>;
160
160
  type StructBase = XImpl<'struct'>;
161
161
  interface ConstantsToType {
@@ -180,7 +180,7 @@ interface ConstantsToType {
180
180
  mat2: Mat2;
181
181
  mat3: Mat3;
182
182
  mat4: Mat4;
183
- texture: Texture;
183
+ texture: Texture$1;
184
184
  sampler2D: Sampler2D;
185
185
  struct: StructBase;
186
186
  }
@@ -386,10 +386,10 @@ type GL = EventState<{
386
386
  uniform(target: {
387
387
  [key: string]: Uniform;
388
388
  }): GL;
389
- _texture?(key: string, value: string): GL;
390
- texture(key: string, value: string): GL;
389
+ _texture?(key: string, value: Texture): GL;
390
+ texture(key: string, value: Texture): GL;
391
391
  texture(target: {
392
- [key: string]: string;
392
+ [key: string]: Texture;
393
393
  }): GL;
394
394
  _attribute?(key: string, value: Attribute, iboValue?: Attribute): GL;
395
395
  attribute(key: string, value: Attribute, iboValue?: Attribute): GL;
@@ -408,6 +408,7 @@ type GL = EventState<{
408
408
  }): GL;
409
409
  }>;
410
410
  type Uniform = number | number[] | Float32Array;
411
+ type Texture = string | HTMLImageElement | HTMLVideoElement;
411
412
  type Attribute = number[] | Float32Array;
412
413
  type Storage = number[] | Float32Array;
413
414
  /**
@@ -501,10 +502,10 @@ declare const createGL: (props?: Partial<GL>) => EventState<{
501
502
  uniform(target: {
502
503
  [key: string]: number | number[] | Float32Array<ArrayBufferLike>;
503
504
  }): GL;
504
- _texture?(key: string, value: string): GL;
505
- texture(key: string, value: string): GL;
505
+ _texture?(key: string, value: string | HTMLImageElement | HTMLVideoElement): GL;
506
+ texture(key: string, value: string | HTMLImageElement | HTMLVideoElement): GL;
506
507
  texture(target: {
507
- [key: string]: string;
508
+ [key: string]: string | HTMLImageElement | HTMLVideoElement;
508
509
  }): GL;
509
510
  _attribute?(key: string, value: number[] | Float32Array<ArrayBufferLike>, iboValue?: number[] | Float32Array<ArrayBufferLike>): GL;
510
511
  attribute(key: string, value: number[] | Float32Array<ArrayBufferLike>, iboValue?: number[] | Float32Array<ArrayBufferLike>): GL;
package/dist/solid.d.ts CHANGED
@@ -155,7 +155,7 @@ type Color = XImpl<'color'>;
155
155
  type Mat2 = XImpl<'mat2'>;
156
156
  type Mat3 = XImpl<'mat3'>;
157
157
  type Mat4 = XImpl<'mat4'>;
158
- type Texture = XImpl<'texture'>;
158
+ type Texture$1 = XImpl<'texture'>;
159
159
  type Sampler2D = XImpl<'sampler2D'>;
160
160
  type StructBase = XImpl<'struct'>;
161
161
  interface ConstantsToType {
@@ -180,7 +180,7 @@ interface ConstantsToType {
180
180
  mat2: Mat2;
181
181
  mat3: Mat3;
182
182
  mat4: Mat4;
183
- texture: Texture;
183
+ texture: Texture$1;
184
184
  sampler2D: Sampler2D;
185
185
  struct: StructBase;
186
186
  }
@@ -386,10 +386,10 @@ type GL = EventState<{
386
386
  uniform(target: {
387
387
  [key: string]: Uniform;
388
388
  }): GL;
389
- _texture?(key: string, value: string): GL;
390
- texture(key: string, value: string): GL;
389
+ _texture?(key: string, value: Texture): GL;
390
+ texture(key: string, value: Texture): GL;
391
391
  texture(target: {
392
- [key: string]: string;
392
+ [key: string]: Texture;
393
393
  }): GL;
394
394
  _attribute?(key: string, value: Attribute, iboValue?: Attribute): GL;
395
395
  attribute(key: string, value: Attribute, iboValue?: Attribute): GL;
@@ -408,6 +408,7 @@ type GL = EventState<{
408
408
  }): GL;
409
409
  }>;
410
410
  type Uniform = number | number[] | Float32Array;
411
+ type Texture = string | HTMLImageElement | HTMLVideoElement;
411
412
  type Attribute = number[] | Float32Array;
412
413
  type Storage = number[] | Float32Array;
413
414
  /**
@@ -501,10 +502,10 @@ declare const createGL: (props?: Partial<GL>) => EventState<{
501
502
  uniform(target: {
502
503
  [key: string]: number | number[] | Float32Array<ArrayBufferLike>;
503
504
  }): GL;
504
- _texture?(key: string, value: string): GL;
505
- texture(key: string, value: string): GL;
505
+ _texture?(key: string, value: string | HTMLImageElement | HTMLVideoElement): GL;
506
+ texture(key: string, value: string | HTMLImageElement | HTMLVideoElement): GL;
506
507
  texture(target: {
507
- [key: string]: string;
508
+ [key: string]: string | HTMLImageElement | HTMLVideoElement;
508
509
  }): GL;
509
510
  _attribute?(key: string, value: number[] | Float32Array<ArrayBufferLike>, iboValue?: number[] | Float32Array<ArrayBufferLike>): GL;
510
511
  attribute(key: string, value: number[] | Float32Array<ArrayBufferLike>, iboValue?: number[] | Float32Array<ArrayBufferLike>): GL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "glre",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "author": "tseijp",
5
5
  "license": "MIT",
6
6
  "private": false,
package/src/node/scope.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { getId } from './utils'
2
1
  import { conversion, create } from './create'
3
2
  import type {
4
3
  FnLayout,
@@ -12,6 +11,7 @@ import type {
12
11
  X,
13
12
  Y,
14
13
  } from './types'
14
+ import { getId } from './utils'
15
15
 
16
16
  let scope: X | null = null
17
17
  let define: X | null = null
@@ -122,8 +122,9 @@ export const Switch = (x: Y) => {
122
122
  }
123
123
 
124
124
  export function Fn<T extends X | Struct | void, Args extends any[]>(fun: (args: Args) => T, layout?: FnLayout) {
125
+ const defaultId = getId()
125
126
  const ret = (...args: any[]) => {
126
- const id = layout?.name || getId()
127
+ const id = layout?.name || defaultId
127
128
  const paramVars: X[] = []
128
129
  const paramDefs: NodeProps[] = []
129
130
  for (let i = 0; i < args.length; i++) {
package/src/node/types.ts CHANGED
@@ -224,7 +224,7 @@ type XImpl<T extends C> = _X<T> & {
224
224
  type C = Constants
225
225
 
226
226
  export type X<T extends C = C> = T extends keyof ConstantsToType ? ConstantsToType[T] : _X<T>
227
- export type Y<T extends C = C> = number | number[] | string | boolean | undefined | X<T>
227
+ export type Y<T extends C = C> = number | number[] | string | boolean | undefined | HTMLElement | X<T>
228
228
 
229
229
  type Methods =
230
230
  | Functions
@@ -17,7 +17,7 @@ import {
17
17
  parseUniformHead,
18
18
  parseVaryingHead,
19
19
  } from './parse'
20
- import { getBluiltin, getConversions, getOperator, initNodeContext, setupEvent } from './utils'
20
+ import { getBluiltin, getConversions, getOperator, initNodeContext, isElement, setupEvent } from './utils'
21
21
  import { is } from '../../utils/helpers'
22
22
  import { mod } from '..'
23
23
  import type { Constants as C, NodeContext, Y } from '../types'
@@ -37,6 +37,7 @@ export const code = <T extends C>(target: Y<T>, c?: NodeContext | null): string
37
37
  return ret + '.0'
38
38
  }
39
39
  if (is.bol(target)) return target ? 'true' : 'false'
40
+ if (isElement(target)) throw `` // IGNORE
40
41
  if (!target) return ''
41
42
  const { type, props = {} } = target
42
43
  const { id = 'i', children = [], fields, initialValues } = props
@@ -1,4 +1,4 @@
1
- import { isConstants, isX, isSwizzle } from './utils'
1
+ import { isConstants, isElement, isX, isSwizzle } from './utils'
2
2
  import {
3
3
  BUILTIN_TYPES,
4
4
  COMPONENT_COUNT_TO_TYPE,
@@ -24,6 +24,7 @@ export const inferPrimitiveType = <T extends C>(x: Y<T>) => {
24
24
  if (is.str(x)) return 'texture' as T
25
25
  if (is.num(x)) return 'float' as T // @TODO FIX: Number.isInteger(x) ? 'int' : 'float'
26
26
  if (is.arr(x)) return COMPONENT_COUNT_TO_TYPE[x.length as keyof typeof COMPONENT_COUNT_TO_TYPE] as T
27
+ if (isElement(x)) return 'texture' as T
27
28
  return 'void' as T
28
29
  }
29
30
 
@@ -8,17 +8,7 @@ import {
8
8
  WGSL_TO_GLSL_BUILTIN,
9
9
  } from './const'
10
10
  import { is } from '../../utils/helpers'
11
- import type {
12
- Constants as C,
13
- Conversions,
14
- Functions,
15
- NodeContext,
16
- NodeTypes,
17
- Operators,
18
- Swizzles,
19
- X,
20
- Y,
21
- } from '../types'
11
+ import type { Constants as C, Conversions, Functions, NodeContext, Operators, Swizzles, X, Y } from '../types'
22
12
  import { storageSize } from '../../utils/program'
23
13
 
24
14
  export const isSwizzle = (key: unknown): key is Swizzles => {
@@ -33,6 +23,11 @@ export const isFunction = (key: unknown): key is Functions => {
33
23
  return FUNCTIONS.includes(key as Functions)
34
24
  }
35
25
 
26
+ export const isElement = (x: unknown): x is Element => {
27
+ if (is.obj(x)) return false
28
+ return x instanceof Element
29
+ }
30
+
36
31
  export const isConversion = (key: unknown): key is Conversions => {
37
32
  return CONVERSIONS.includes(key as Conversions)
38
33
  }
package/src/types.ts CHANGED
@@ -58,9 +58,9 @@ export type GL = EventState<{
58
58
  _uniform?(key: string, value: Uniform, isMatrix?: boolean): GL
59
59
  uniform(key: string, value: Uniform, isMatrix?: boolean): GL
60
60
  uniform(target: { [key: string]: Uniform }): GL
61
- _texture?(key: string, value: string): GL
62
- texture(key: string, value: string): GL
63
- texture(target: { [key: string]: string }): GL
61
+ _texture?(key: string, value: Texture): GL
62
+ texture(key: string, value: Texture): GL
63
+ texture(target: { [key: string]: Texture }): GL
64
64
  _attribute?(key: string, value: Attribute, iboValue?: Attribute): GL
65
65
  attribute(key: string, value: Attribute, iboValue?: Attribute): GL
66
66
  attribute(target: { [key: string]: Attribute }): GL
@@ -73,6 +73,7 @@ export type GL = EventState<{
73
73
  }>
74
74
 
75
75
  type Uniform = number | number[] | Float32Array
76
+ type Texture = string | HTMLImageElement | HTMLVideoElement
76
77
  type Attribute = number[] | Float32Array
77
78
  type Storage = number[] | Float32Array
78
79
 
@@ -1,5 +1,3 @@
1
- import type { GL } from './../types'
2
-
3
1
  export const is = {
4
2
  arr: Array.isArray,
5
3
  bol: (a: unknown): a is boolean => typeof a === 'boolean',
@@ -49,16 +47,35 @@ export const isFloat32 = (value: unknown): value is Float32Array => {
49
47
  return value instanceof Float32Array
50
48
  }
51
49
 
52
- export const loadingImage = (gl: GL, src: string, fun: (source: HTMLImageElement) => void) => {
53
- gl.loading++
50
+ const loadingImage = (src: string, fun: (source: HTMLImageElement) => void) => {
54
51
  const source = new Image()
55
52
  Object.assign(source, { src, crossOrigin: 'anonymous' })
56
- source.decode().then(() => {
57
- fun(source)
58
- gl.loading--
59
- })
53
+ source.decode().then(() => fun(source))
54
+ }
55
+
56
+ const loadingVideo = (src: string, fun: (source: HTMLVideoElement) => void) => {
57
+ const source = document.createElement('video')
58
+ source.crossOrigin = 'anonymous'
59
+ source.muted = true
60
+ source.loop = true
61
+ source.src = src
62
+ source.load()
63
+ source.play()
64
+ source.addEventListener('canplay', fun.bind(null, source), { once: true })
60
65
  }
61
66
 
67
+ export function loadingTexture(src: string, fun: (source: HTMLVideoElement, isVideo: true) => void): void
68
+
69
+ export function loadingTexture(src: string, fun: (source: HTMLImageElement, isVideo: false) => void): void
70
+
71
+ export function loadingTexture(src: string | HTMLImageElement | HTMLVideoElement, fun: Function) {
72
+ if (!is.str(src)) return fun(src, src instanceof HTMLVideoElement)
73
+ const isVideo = /\.(mp4|webm|ogg|avi|mov)$/i.test(src)
74
+ const loader = isVideo ? loadingVideo : loadingImage
75
+ loader(src, (el: HTMLImageElement | HTMLVideoElement) => {
76
+ fun(el as HTMLVideoElement, isVideo)
77
+ })
78
+ }
62
79
  const isValidStride = (stride: number) => [1, 2, 3, 4, 9, 16].includes(stride)
63
80
 
64
81
  const calcStride = (arrayLength: number, count = 3) => {
@@ -67,14 +67,15 @@ export const updateUniform = (c: WebGL2RenderingContext, loc: WebGLUniformLocati
67
67
 
68
68
  export const createTexture = (
69
69
  c: WebGL2RenderingContext,
70
- img: HTMLImageElement,
70
+ el: HTMLImageElement | HTMLVideoElement,
71
71
  loc: WebGLUniformLocation,
72
- unit: number
72
+ unit: number,
73
+ isVideo = false
73
74
  ) => {
74
75
  const texture = c.createTexture()
75
76
  c.bindTexture(c.TEXTURE_2D, texture)
76
- c.texImage2D(c.TEXTURE_2D, 0, c.RGBA, c.RGBA, c.UNSIGNED_BYTE, img)
77
- c.generateMipmap(c.TEXTURE_2D)
77
+ c.texImage2D(c.TEXTURE_2D, 0, c.RGBA, c.RGBA, c.UNSIGNED_BYTE, el)
78
+ if (!isVideo) c.generateMipmap(c.TEXTURE_2D)
78
79
  c.texParameteri(c.TEXTURE_2D, c.TEXTURE_MIN_FILTER, c.LINEAR)
79
80
  c.texParameteri(c.TEXTURE_2D, c.TEXTURE_MAG_FILTER, c.LINEAR)
80
81
  c.texParameteri(c.TEXTURE_2D, c.TEXTURE_WRAP_S, c.CLAMP_TO_EDGE)
@@ -83,6 +84,12 @@ export const createTexture = (
83
84
  c.uniform1i(loc, unit)
84
85
  c.activeTexture(c.TEXTURE0 + unit)
85
86
  c.bindTexture(c.TEXTURE_2D, texture)
87
+ if (isVideo)
88
+ return () => {
89
+ c.activeTexture(c.TEXTURE0 + unit)
90
+ c.bindTexture(c.TEXTURE_2D, texture)
91
+ c.texImage2D(c.TEXTURE_2D, 0, c.RGBA, c.RGBA, c.UNSIGNED_BYTE, el)
92
+ }
86
93
  }
87
94
 
88
95
  /**
@@ -1,5 +1,5 @@
1
1
  import { nested as cached } from 'reev'
2
- import { is, loadingImage, getStride, GLSL_VS, GLSL_FS } from './helpers'
2
+ import { is, getStride, GLSL_VS, GLSL_FS, loadingTexture } from './helpers'
3
3
  import {
4
4
  createArrayBuffer,
5
5
  cleanStorage,
@@ -105,9 +105,13 @@ export const webgl = async (gl: GL) => {
105
105
  }
106
106
 
107
107
  const _texture = (key: string, src: string) => {
108
+ gl.loading++
108
109
  c.useProgram(pg)
109
- loadingImage(gl, src, (source) => {
110
- createTexture(c, source, uniforms(key)!, units(key))
110
+ loadingTexture(src, (source, isVideo) => {
111
+ const unit = units(key)
112
+ const loop = createTexture(c, source, uniforms(key)!, unit, isVideo)
113
+ if (loop) gl({ loop })
114
+ gl.loading--
111
115
  })
112
116
  }
113
117
 
@@ -1,5 +1,5 @@
1
1
  import { nested as cached } from 'reev'
2
- import { is, loadingImage, getStride, WGSL_FS, WGSL_VS } from './helpers'
2
+ import { is, getStride, WGSL_FS, WGSL_VS, loadingTexture } from './helpers'
3
3
  import {
4
4
  createArrayBuffer,
5
5
  createBindGroup,
@@ -105,10 +105,18 @@ export const webgpu = async (gl: GL) => {
105
105
  }
106
106
 
107
107
  const _texture = (key: string, src: string) => {
108
- loadingImage(gl, src, (source) => {
109
- const { width, height } = source
108
+ gl.loading++
109
+ loadingTexture(src, (source, isVideo) => {
110
+ const [width, height] = isVideo
111
+ ? [source.videoWidth, source.videoHeight]
112
+ : [source.width, source.height]
110
113
  const { texture } = textures(key, width, height)
111
- device.queue.copyExternalImageToTexture({ source }, { texture }, { width, height })
114
+ const loop = () => {
115
+ device.queue.copyExternalImageToTexture({ source }, { texture }, { width, height })
116
+ }
117
+ loop()
118
+ if (isVideo) gl({ loop })
119
+ gl.loading--
112
120
  })
113
121
  }
114
122