glre 0.42.0 → 0.44.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.
@@ -94,7 +94,8 @@ export const getStride = (arrayLength: number, count = 1, error = console.warn)
94
94
 
95
95
  export const GLSL_FS = /* cpp */ `
96
96
  #version 300 es
97
- precision mediump float;
97
+ precision highp float;
98
+ precision highp int;
98
99
  out vec4 fragColor;
99
100
  uniform vec2 iResolution;
100
101
  void main() {
@@ -32,12 +32,7 @@ export const createArrayBuffer = (c: WebGL2RenderingContext, data: number[]) =>
32
32
  return { array, buffer }
33
33
  }
34
34
 
35
- export const setArrayBuffer = (
36
- c: WebGL2RenderingContext,
37
- array: Float32Array,
38
- buffer: WebGLBuffer,
39
- value: number[]
40
- ) => {
35
+ export const setArrayBuffer = (c: WebGL2RenderingContext, array: Float32Array, buffer: WebGLBuffer, value: number[]) => {
41
36
  array.set(value)
42
37
  c.bindBuffer(c.ARRAY_BUFFER, buffer)
43
38
  c.bufferData(c.ARRAY_BUFFER, array, c.STATIC_DRAW)
@@ -65,13 +60,7 @@ export const updateUniform = (c: WebGL2RenderingContext, loc: WebGLUniformLocati
65
60
  c[`uniformMatrix${l as 2}fv`](loc, false, value)
66
61
  }
67
62
 
68
- export const createTexture = (
69
- c: WebGL2RenderingContext,
70
- el: HTMLImageElement | HTMLVideoElement,
71
- loc: WebGLUniformLocation,
72
- unit: number,
73
- isVideo = false
74
- ) => {
63
+ export const createTexture = (c: WebGL2RenderingContext, el: HTMLImageElement | HTMLVideoElement, loc: WebGLUniformLocation, unit: number, isVideo = false) => {
75
64
  const texture = c.createTexture()
76
65
  c.bindTexture(c.TEXTURE_2D, texture)
77
66
  c.texImage2D(c.TEXTURE_2D, 0, c.RGBA, c.RGBA, c.UNSIGNED_BYTE, el)
@@ -100,16 +89,7 @@ interface TextureBuffer {
100
89
  buffer: WebGLFramebuffer
101
90
  }
102
91
 
103
- export const createStorage = (
104
- c: WebGL2RenderingContext,
105
- value: number[],
106
- width: number,
107
- height: number,
108
- ping: TextureBuffer,
109
- pong: TextureBuffer,
110
- unit: number,
111
- array: Float32Array
112
- ) => {
92
+ export const createStorage = (c: WebGL2RenderingContext, value: number[], width: number, height: number, ping: TextureBuffer, pong: TextureBuffer, unit: number, array: Float32Array) => {
113
93
  const particleCount = width * height
114
94
  const vectorSize = value.length / particleCount
115
95
  for (let i = 0; i < particleCount; i++) {
@@ -132,10 +112,7 @@ export const createStorage = (
132
112
  c.texParameteri(c.TEXTURE_2D, c.TEXTURE_WRAP_T, c.CLAMP_TO_EDGE)
133
113
  }
134
114
 
135
- export const cleanStorage = (
136
- c: WebGL2RenderingContext,
137
- map: Iterable<{ ping: TextureBuffer; pong: TextureBuffer }>
138
- ) => {
115
+ export const cleanStorage = (c: WebGL2RenderingContext, map: Iterable<{ ping: TextureBuffer; pong: TextureBuffer }>) => {
139
116
  for (const { ping, pong } of map) {
140
117
  c.deleteTexture(ping.texture)
141
118
  c.deleteTexture(pong.texture)
@@ -144,14 +121,7 @@ export const cleanStorage = (
144
121
  }
145
122
  }
146
123
 
147
- export const createAttachment = (
148
- c: WebGL2RenderingContext,
149
- i: TextureBuffer,
150
- o: TextureBuffer,
151
- loc: WebGLUniformLocation,
152
- unit: number,
153
- index: number
154
- ) => {
124
+ export const createAttachment = (c: WebGL2RenderingContext, i: TextureBuffer, o: TextureBuffer, loc: WebGLUniformLocation, unit: number, index: number) => {
155
125
  c.activeTexture(c.TEXTURE0 + unit)
156
126
  c.bindTexture(c.TEXTURE_2D, i.texture)
157
127
  c.uniform1i(loc, unit)
@@ -168,19 +138,13 @@ export const storageSize = (particleCount: number | number[] = 1024) => {
168
138
  if (is.num(particleCount)) {
169
139
  const sqrt = Math.sqrt(particleCount)
170
140
  const size = Math.ceil(sqrt)
171
- if (!Number.isInteger(sqrt)) {
172
- console.warn(
173
- `GLRE Storage Warning: particleCount (${particleCount}) is not a square. Using ${size}x${size} texture may waste GPU memory. Consider using [width, height] format for optimal storage.`
174
- )
175
- }
141
+ if (!Number.isInteger(sqrt)) console.warn(`GLRE Storage Warning: particleCount (${particleCount}) is not a square. Using ${size}x${size} texture may waste GPU memory. Consider using [width, height] format for optimal storage.`)
176
142
  return { x: size, y: size }
177
143
  }
178
144
  const [x, y, z] = particleCount
179
145
  if (z !== undefined) {
180
146
  const yz = y * z
181
- console.warn(
182
- `GLRE Storage Warning: 3D particleCount [${x}, ${y}, ${z}] specified but WebGL storage textures only support 2D. Flattening to 2D by multiplying height=${y} * depth=${z} = ${yz}.`
183
- )
147
+ console.warn(`GLRE Storage Warning: 3D particleCount [${x}, ${y}, ${z}] specified but WebGL storage textures only support 2D. Flattening to 2D by multiplying height=${y} * depth=${z} = ${yz}.`)
184
148
  return { x, y: yz }
185
149
  }
186
150
  return { x, y }
@@ -1,19 +1,7 @@
1
1
  import { nested as cached } from 'reev'
2
2
  import { is, getStride, GLSL_VS, GLSL_FS, loadingTexture } from './helpers'
3
- import {
4
- createArrayBuffer,
5
- cleanStorage,
6
- createAttachment,
7
- createProgram,
8
- createStorage,
9
- createTexture,
10
- setArrayBuffer,
11
- storageSize,
12
- updateAttrib,
13
- updateInstance,
14
- updateUniform,
15
- } from './program'
16
- import type { GL, WebGLState } from '../types'
3
+ import { createArrayBuffer, cleanStorage, createAttachment, createProgram, createStorage, createTexture, setArrayBuffer, storageSize, updateAttrib, updateInstance, updateUniform } from './program'
4
+ import type { GL } from '../types'
17
5
 
18
6
  const computeProgram = (gl: GL, c: WebGL2RenderingContext) => {
19
7
  if (!gl.cs) return null // ignore if no compute shader
@@ -67,17 +55,17 @@ const computeProgram = (gl: GL, c: WebGL2RenderingContext) => {
67
55
 
68
56
  export const webgl = async (gl: GL) => {
69
57
  const config = { isWebGL: true, gl }
70
- const c = gl.el!.getContext('webgl2')!
58
+ const c = (gl.webgl.context = gl.el!.getContext('webgl2')!)
71
59
  const cp = computeProgram(gl, c)
72
60
  const fs = gl.fs ? (is.str(gl.fs) ? gl.fs : gl.fs!.fragment(config)) : GLSL_FS
73
61
  const vs = gl.vs ? (is.str(gl.vs) ? gl.vs : gl.vs!.vertex(config)) : GLSL_VS
74
- const pg = createProgram(c, fs, vs, gl)!
62
+ const pg = (gl.webgl.program = createProgram(c, fs, vs, gl)!)
75
63
  c.useProgram(pg)
76
64
 
77
65
  let activeUnit = 0 // for texture units
78
66
 
79
67
  const units = cached(() => activeUnit++)
80
- const uniforms = cached((key) => c.getUniformLocation(pg, key))
68
+ const uniforms = (gl.webgl.uniforms = cached((key) => c.getUniformLocation(pg, key)))
81
69
 
82
70
  const attribs = cached((key, value: number[], isInstance = false) => {
83
71
  const stride = getStride(value.length, isInstance ? gl.instanceCount : gl.count, gl.error)
@@ -138,7 +126,10 @@ export const webgl = async (gl: GL) => {
138
126
  c.cullFace(c.BACK)
139
127
  }
140
128
 
141
- const webgl: WebGLState = { context: c, program: pg, storages: cp?.storages }
129
+ if (gl.wireframe) {
130
+ const ext = c.getExtension('WEBGL_polygon_mode')
131
+ ext.polygonModeWEBGL(c.FRONT_AND_BACK, ext.LINE_WEBGL)
132
+ }
142
133
 
143
- return { webgl, render, clean, _attribute, _instance, _uniform, _texture, _storage: cp?._storage }
134
+ return { render, clean, _attribute, _instance, _uniform, _texture, _storage: cp?._storage }
144
135
  }