@bigmistqke/view.gl 0.1.2 → 0.1.3
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/README.md +208 -63
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,34 +1,61 @@
|
|
|
1
|
-
# view.gl
|
|
1
|
+
# 👁️ @bigmistqke/view.gl
|
|
2
2
|
|
|
3
|
-
Minimal library for WebGL
|
|
3
|
+
🔧 Minimal library for managing WebGL uniforms / (interleaved) attributes / buffers / ...
|
|
4
|
+
|
|
5
|
+
- schema-based resource management [`view-gl`](#️-viewgl)
|
|
6
|
+
- type-safe GLSL template literals to compose schemas [`view-gl/tag`](#️-viewgltag)
|
|
4
7
|
|
|
5
8
|
## Table of Contents
|
|
6
9
|
|
|
7
|
-
- [
|
|
8
|
-
|
|
9
|
-
- [
|
|
10
|
-
|
|
11
|
-
- [
|
|
12
|
-
- [
|
|
13
|
-
- [
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
- [
|
|
17
|
-
- [
|
|
18
|
-
|
|
19
|
-
- [
|
|
20
|
-
- [
|
|
21
|
-
- [
|
|
22
|
-
- [
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
10
|
+
- [📦 Install](#-install)
|
|
11
|
+
- [👁️ view.gl](#️-viewgl)
|
|
12
|
+
- [🚀 Basic Usage](#-basic-usage)
|
|
13
|
+
- [👀 View Types](#-view-types)
|
|
14
|
+
- [🎯 uniformView](#-uniformview)
|
|
15
|
+
- [📝 attributeView](#-attributeview)
|
|
16
|
+
- [🔗 interleavedAttributeView](#-interleavedattributeview)
|
|
17
|
+
- [🗂️ bufferView](#-bufferview)
|
|
18
|
+
- [🎨 Drawing Examples](#-drawing-examples)
|
|
19
|
+
- [🖼️ Regular Drawing](#-regular-drawing)
|
|
20
|
+
- [📱 Instanced Drawing](#-instanced-drawing)
|
|
21
|
+
- [🏷️ view.gl/tag](#️-viewgltag)
|
|
22
|
+
- [🚀 Basic Usage](#-basic-usage-1)
|
|
23
|
+
- [📝 Template Features](#-template-features)
|
|
24
|
+
- [⚙️ Tag Functions](#️-tag-functions)
|
|
25
|
+
- [🎯 uniform[kind](name, options?)](#-uniformkindname-options)
|
|
26
|
+
- [📝 attribute[kind](name, options?)](#-attributekindname-options)
|
|
27
|
+
- [🔗 interleave(name, layout, options?)](#-interleavename-layout-options)
|
|
28
|
+
- [📝 glsl](#-glsl)
|
|
29
|
+
- [⚙️ compile(gl, vertex, fragment)](#️-compilegl-vertex-fragment)
|
|
30
|
+
- [🔣 Symbol Support](#-symbol-support)
|
|
31
|
+
- [🛠️ Utils](#️-utils)
|
|
32
|
+
- [🏗️ createProgram](#️-createprogram)
|
|
33
|
+
- [🖼️ createTexture](#️-createtexture)
|
|
34
|
+
- [🖥️ createFramebuffer](#️-createframebuffer)
|
|
35
|
+
|
|
36
|
+
## 📦 Install
|
|
37
|
+
|
|
38
|
+
```shell
|
|
39
|
+
npm install @bigmistqke/view.gl
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```shell
|
|
43
|
+
pnpm add @bigmistqke/view.gl
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```shell
|
|
47
|
+
yarn add @bigmistqke/view.gl
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```shell
|
|
51
|
+
bun add @bigmistqke/view.gl
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## 👁️ view.gl
|
|
28
55
|
|
|
29
56
|
The view system provides type-safe WebGL resource management for uniforms, attributes, and buffers.
|
|
30
57
|
|
|
31
|
-
### Basic Usage
|
|
58
|
+
### 🚀 Basic Usage
|
|
32
59
|
|
|
33
60
|
```typescript
|
|
34
61
|
import { view } from '@bigmistqke/view.gl'
|
|
@@ -56,7 +83,7 @@ attributes.position.set(positionData)
|
|
|
56
83
|
attributes.position.bind()
|
|
57
84
|
```
|
|
58
85
|
|
|
59
|
-
### View Types
|
|
86
|
+
### 👀 View Types
|
|
60
87
|
|
|
61
88
|
Each view type can be imported individually too.
|
|
62
89
|
|
|
@@ -69,7 +96,7 @@ import {
|
|
|
69
96
|
} from '@bigmistqke/view.gl'
|
|
70
97
|
```
|
|
71
98
|
|
|
72
|
-
#### uniformView
|
|
99
|
+
#### 🎯 uniformView
|
|
73
100
|
|
|
74
101
|
Manages shader uniform variables.
|
|
75
102
|
|
|
@@ -92,7 +119,7 @@ uniforms.transform.set(transformMatrix)
|
|
|
92
119
|
- `kind`: GLSL type (`'float'`, `'vec2'`, `'mat4'`, `'sampler2D'`, etc.)
|
|
93
120
|
- `size`: Array size (optional) - converts uniform to array type with Float32Array setter
|
|
94
121
|
|
|
95
|
-
#### attributeView
|
|
122
|
+
#### 📝 attributeView
|
|
96
123
|
|
|
97
124
|
Manages vertex attributes with buffer creation, binding, and data management.
|
|
98
125
|
|
|
@@ -123,7 +150,7 @@ gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 100)
|
|
|
123
150
|
- `instanced`: Boolean - enables instanced rendering with `vertexAttribDivisor`
|
|
124
151
|
- `buffer`: Custom WebGLBuffer (optional) - uses auto-created buffer if not provided
|
|
125
152
|
|
|
126
|
-
#### interleavedAttributeView
|
|
153
|
+
#### 🔗 interleavedAttributeView
|
|
127
154
|
|
|
128
155
|
Manages interleaved vertex data with automatic stride/offset calculation and VAO optimization.
|
|
129
156
|
|
|
@@ -161,7 +188,7 @@ interleavedAttributes.instanceData.bind()
|
|
|
161
188
|
- `layout`: Array of attribute definitions with `name` and `kind`
|
|
162
189
|
- `instanced`: Boolean - applies `vertexAttribDivisor` to all attributes in layout
|
|
163
190
|
|
|
164
|
-
#### bufferView
|
|
191
|
+
#### 🗂️ bufferView
|
|
165
192
|
|
|
166
193
|
Manages generic WebGL buffers for various uses.
|
|
167
194
|
|
|
@@ -185,9 +212,9 @@ buffers.data.bind()
|
|
|
185
212
|
- `target`: Buffer target (`'ARRAY_BUFFER'`, `'ELEMENT_ARRAY_BUFFER'`)
|
|
186
213
|
- `usage`: Usage pattern (`'STATIC_DRAW'`, `'DYNAMIC_DRAW'`, `'STREAM_DRAW'`)
|
|
187
214
|
|
|
188
|
-
### Drawing Examples
|
|
215
|
+
### 🎨 Drawing Examples
|
|
189
216
|
|
|
190
|
-
#### Regular Drawing
|
|
217
|
+
#### 🖼️ Regular Drawing
|
|
191
218
|
|
|
192
219
|
```typescript
|
|
193
220
|
const { attributes } = view(gl, program, {
|
|
@@ -203,7 +230,7 @@ attributes.uv.set(uvData).bind()
|
|
|
203
230
|
gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
|
|
204
231
|
```
|
|
205
232
|
|
|
206
|
-
#### Instanced Drawing
|
|
233
|
+
#### 📱 Instanced Drawing
|
|
207
234
|
|
|
208
235
|
```typescript
|
|
209
236
|
const { attributes } = view(gl, program, {
|
|
@@ -221,34 +248,11 @@ attributes.instanceColor.set(instanceColors).bind()
|
|
|
221
248
|
gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, instanceCount)
|
|
222
249
|
```
|
|
223
250
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
- **Automatic Resource Management**: Buffer creation, disposal, and cleanup
|
|
227
|
-
- **Type Safety**: type checking for uniform and attribute operations
|
|
228
|
-
- **WebGL Extensions**: Automatic fallback for instanced arrays and vertex array objects
|
|
229
|
-
- **Memory Management**: AbortSignal support for scheduling cleanup of resources
|
|
230
|
-
|
|
231
|
-
## Tag
|
|
251
|
+
## 🏷️ view.gl/tag
|
|
232
252
|
|
|
233
253
|
The tag system provides GLSL template literal support with embedded schema definitions.
|
|
234
254
|
|
|
235
|
-
###
|
|
236
|
-
|
|
237
|
-
- **Embedded Resources**: Define uniforms and attributes directly in GLSL
|
|
238
|
-
- **Type Safety**: Automatic schema generation from template literals
|
|
239
|
-
- **WebGL Version Support**: Automatic syntax conversion for WebGL1/2
|
|
240
|
-
- **Interleaved Attributes**: Multi-attribute layout support
|
|
241
|
-
- **Symbol Support**: Use JavaScript symbols as keys (see [View Features](#symbol-support))
|
|
242
|
-
|
|
243
|
-
### Tag Functions
|
|
244
|
-
|
|
245
|
-
- `uniform[kind](name, options?)`: Define uniform variables
|
|
246
|
-
- `attribute[kind](name, options?)`: Define vertex attributes
|
|
247
|
-
- `interleave(name, layout, options?)`: Define interleaved attribute layouts
|
|
248
|
-
- `glsl`: Template literal processor for GLSL code
|
|
249
|
-
- `compile(gl, vertex, fragment)`: Compile shaders and extract schema
|
|
250
|
-
|
|
251
|
-
### Basic Usage
|
|
255
|
+
### 🚀 Basic Usage
|
|
252
256
|
|
|
253
257
|
```typescript
|
|
254
258
|
import { glsl, uniform, attribute, compile } from '@bigmistqke/view.gl/tag'
|
|
@@ -280,7 +284,145 @@ const fragmentShader = glsl`
|
|
|
280
284
|
const { program, schema } = compile(gl, vertexShader, fragmentShader)
|
|
281
285
|
```
|
|
282
286
|
|
|
283
|
-
###
|
|
287
|
+
### 📝 Template Features
|
|
288
|
+
|
|
289
|
+
- **Embedded Resources**: Define uniforms and attributes directly in GLSL
|
|
290
|
+
- **Type Safety**: Automatic schema generation from template literals
|
|
291
|
+
- **WebGL Version Support**: Automatic syntax conversion for WebGL1/2
|
|
292
|
+
- **Interleaved Attributes**: Multi-attribute layout support
|
|
293
|
+
- **Symbol Support**: Use JavaScript symbols as keys (see [Symbol Support](#-symbol-support))
|
|
294
|
+
|
|
295
|
+
### ⚙️ Tag Functions
|
|
296
|
+
|
|
297
|
+
#### 🎯 uniform[kind](name, options?)
|
|
298
|
+
|
|
299
|
+
Define uniform variables in GLSL templates. Supports all GLSL uniform types.
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
import { uniform, glsl } from '@bigmistqke/view.gl/tag'
|
|
303
|
+
|
|
304
|
+
const shader = glsl`
|
|
305
|
+
${uniform.float('time')}
|
|
306
|
+
${uniform.vec2('resolution')}
|
|
307
|
+
${uniform.mat4('projection')}
|
|
308
|
+
${uniform.sampler2D('texture')}
|
|
309
|
+
${uniform.vec3('lights', { size: 8 })} // Array uniform: vec3[8]
|
|
310
|
+
|
|
311
|
+
void main() {
|
|
312
|
+
// Use uniforms in shader code
|
|
313
|
+
}
|
|
314
|
+
`
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
**Available types**: `float`, `int`, `bool`, `vec2`, `vec3`, `vec4`, `ivec2`, `ivec3`, `ivec4`, `bvec2`, `bvec3`, `bvec4`, `mat2`, `mat3`, `mat4`, `sampler2D`, `samplerCube`
|
|
318
|
+
|
|
319
|
+
**Options**:
|
|
320
|
+
|
|
321
|
+
- `size`: Array size (creates array uniform with Float32Array setter)
|
|
322
|
+
|
|
323
|
+
#### 📝 attribute[kind](name, options?)
|
|
324
|
+
|
|
325
|
+
Define vertex attributes in GLSL templates.
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
import { attribute, glsl } from '@bigmistqke/view.gl/tag'
|
|
329
|
+
|
|
330
|
+
const vertexShader = glsl`
|
|
331
|
+
${attribute.vec3('position')}
|
|
332
|
+
${attribute.vec2('uv')}
|
|
333
|
+
${attribute.vec3('normal')}
|
|
334
|
+
${attribute.vec2('instanceOffset', { instanced: true })}
|
|
335
|
+
|
|
336
|
+
void main() {
|
|
337
|
+
gl_Position = vec4(position + vec3(instanceOffset, 0.0), 1.0);
|
|
338
|
+
}
|
|
339
|
+
`
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
**Available types**: `float`, `vec2`, `vec3`, `vec4`, `mat2`, `mat3`, `mat4`, `int`, `ivec2`, `ivec3`, `ivec4`
|
|
343
|
+
|
|
344
|
+
**Options**:
|
|
345
|
+
|
|
346
|
+
- `instanced`: Boolean - enables instanced rendering with `vertexAttribDivisor`
|
|
347
|
+
- `buffer`: Custom WebGLBuffer (optional)
|
|
348
|
+
|
|
349
|
+
#### 🔗 interleave(name, layout, options?)
|
|
350
|
+
|
|
351
|
+
Define interleaved attribute layouts for efficient vertex data.
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
import { interleave, glsl } from '@bigmistqke/view.gl/tag'
|
|
355
|
+
|
|
356
|
+
const vertexShader = glsl`
|
|
357
|
+
${interleave('vertexData', [
|
|
358
|
+
{ key: 'position', kind: 'vec3' },
|
|
359
|
+
{ key: 'normal', kind: 'vec3' },
|
|
360
|
+
{ key: 'uv', kind: 'vec2' },
|
|
361
|
+
])}
|
|
362
|
+
|
|
363
|
+
${interleave(
|
|
364
|
+
'instanceData',
|
|
365
|
+
[
|
|
366
|
+
{ key: 'instancePos', kind: 'vec2' },
|
|
367
|
+
{ key: 'instanceColor', kind: 'vec3' },
|
|
368
|
+
],
|
|
369
|
+
{ instanced: true },
|
|
370
|
+
)}
|
|
371
|
+
|
|
372
|
+
void main() {
|
|
373
|
+
gl_Position = vec4(position + vec3(instancePos, 0.0), 1.0);
|
|
374
|
+
}
|
|
375
|
+
`
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
**Layout format**:
|
|
379
|
+
|
|
380
|
+
- `key`: Attribute name
|
|
381
|
+
- `kind`: GLSL type
|
|
382
|
+
|
|
383
|
+
**Options**:
|
|
384
|
+
|
|
385
|
+
- `instanced`: Boolean - applies `vertexAttribDivisor` to all attributes
|
|
386
|
+
|
|
387
|
+
#### 📝 glsl
|
|
388
|
+
|
|
389
|
+
Template literal processor that handles GLSL code and embedded resources.
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
import { glsl } from '@bigmistqke/view.gl/tag'
|
|
393
|
+
|
|
394
|
+
const shader = glsl`
|
|
395
|
+
precision mediump float;
|
|
396
|
+
|
|
397
|
+
${uniform.vec2('resolution')}
|
|
398
|
+
|
|
399
|
+
void main() {
|
|
400
|
+
vec2 uv = gl_FragCoord.xy / resolution;
|
|
401
|
+
gl_FragColor = vec4(uv, 0.0, 1.0);
|
|
402
|
+
}
|
|
403
|
+
`
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
#### ⚙️ compile(gl, vertex, fragment)
|
|
407
|
+
|
|
408
|
+
Compiles shaders and extracts schema from embedded resources.
|
|
409
|
+
|
|
410
|
+
```typescript
|
|
411
|
+
import { compile } from '@bigmistqke/view.gl/tag'
|
|
412
|
+
|
|
413
|
+
const { program, schema } = compile(gl, vertexShader, fragmentShader)
|
|
414
|
+
|
|
415
|
+
// Schema contains extracted uniforms, attributes, and buffers
|
|
416
|
+
console.log(schema.uniforms) // { time: { kind: 'float' }, ... }
|
|
417
|
+
console.log(schema.attributes) // { position: { kind: 'vec3' }, ... }
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
**Returns**:
|
|
421
|
+
|
|
422
|
+
- `program`: Compiled WebGL program
|
|
423
|
+
- `schema`: Extracted resource definitions for use with `view()`
|
|
424
|
+
|
|
425
|
+
### 🔣 Symbol Support
|
|
284
426
|
|
|
285
427
|
Symbols are supported as keys for uniforms, attributes, and buffers. When interpolated in glsl templates, they're automatically converted to valid GLSL identifiers. This prevents naming collisions between different shader modules and enables private scoping of shader variables.
|
|
286
428
|
|
|
@@ -298,6 +440,9 @@ const shader = glsl`
|
|
|
298
440
|
`
|
|
299
441
|
```
|
|
300
442
|
|
|
443
|
+
<details>
|
|
444
|
+
<summary>Usage with vanilla view.gl</summary>
|
|
445
|
+
|
|
301
446
|
When manually constructing GLSL strings, use `toID()` to convert symbols to valid identifiers:
|
|
302
447
|
|
|
303
448
|
```typescript
|
|
@@ -335,11 +480,11 @@ attributes[a_position].set(vertexData)
|
|
|
335
480
|
uniforms[u_time].set(performance.now())
|
|
336
481
|
```
|
|
337
482
|
|
|
338
|
-
|
|
483
|
+
</details>
|
|
339
484
|
|
|
340
|
-
|
|
485
|
+
## 🛠️ Utils
|
|
341
486
|
|
|
342
|
-
### createProgram
|
|
487
|
+
### 🏗️ createProgram
|
|
343
488
|
|
|
344
489
|
Creates and links a WebGL program from vertex and fragment shader sources.
|
|
345
490
|
|
|
@@ -349,7 +494,7 @@ import { createProgram } from '@bigmistqke/view.gl/utils'
|
|
|
349
494
|
const program = createProgram(gl, vertexShaderSource, fragmentShaderSource)
|
|
350
495
|
```
|
|
351
496
|
|
|
352
|
-
### createTexture
|
|
497
|
+
### 🖼️ createTexture
|
|
353
498
|
|
|
354
499
|
Creates a WebGL texture with specified parameters.
|
|
355
500
|
|
|
@@ -375,7 +520,7 @@ const texture = createTexture(
|
|
|
375
520
|
|
|
376
521
|
Automatically validates WebGL2-only formats and provides fallbacks for WebGL1.
|
|
377
522
|
|
|
378
|
-
### createFramebuffer
|
|
523
|
+
### 🖥️ createFramebuffer
|
|
379
524
|
|
|
380
525
|
Creates a framebuffer with attached texture for render-to-texture operations.
|
|
381
526
|
|