@bigmistqke/view.gl 0.1.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.
@@ -0,0 +1,8 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(rg:*)"
5
+ ],
6
+ "deny": []
7
+ }
8
+ }
package/.prettierrc ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "trailingComma": "all",
3
+ "tabWidth": 2,
4
+ "printWidth": 100,
5
+ "semi": false,
6
+ "singleQuote": true,
7
+ "useTabs": false,
8
+ "arrowParens": "avoid",
9
+ "bracketSpacing": true
10
+ }
package/README.md ADDED
@@ -0,0 +1,338 @@
1
+ # view.gl
2
+
3
+ Minimal library for WebGL shader resource management with type-safe GLSL template literals.
4
+
5
+ ## Table of Contents
6
+
7
+ - [View](#view)
8
+ - [Basic Usage](#basic-usage)
9
+ - [View Types](#view-types)
10
+ - [uniformView](#uniformview)
11
+ - [attributeView](#attributeview)
12
+ - [interleavedAttributeView](#interleavedattributeview)
13
+ - [bufferView](#bufferview)
14
+ - [Drawing Examples](#drawing-examples)
15
+ - [Regular Drawing](#regular-drawing)
16
+ - [Instanced Drawing](#instanced-drawing)
17
+ - [Features](#features)
18
+ - [Tag](#tag)
19
+ - [Basic Usage](#basic-usage-1)
20
+ - [Template Features](#template-features)
21
+ - [Tag Functions](#tag-functions)
22
+ - [Supported Types](#supported-types)
23
+ - [Utils](#utils)
24
+ - [WebGL Utilities](#webgl-utilities)
25
+ - [Type Utilities](#type-utilities)
26
+ - [Type Guards](#type-guards)
27
+ - [Resource Creation](#resource-creation)
28
+
29
+ ## View
30
+
31
+ The view system provides type-safe WebGL resource management for uniforms, attributes, and buffers.
32
+
33
+ ### Basic Usage
34
+
35
+ ```typescript
36
+ import { view } from 'view.gl'
37
+
38
+ const resources = view(gl, program, {
39
+ uniforms: {
40
+ time: { kind: 'float' },
41
+ resolution: { kind: 'vec2' },
42
+ },
43
+ attributes: {
44
+ position: { kind: 'vec3' },
45
+ uv: { kind: 'vec2' },
46
+ },
47
+ buffers: {
48
+ indices: { target: 'ELEMENT_ARRAY_BUFFER' },
49
+ },
50
+ })
51
+
52
+ // Type-safe uniform setting
53
+ resources.uniforms.time.set(performance.now())
54
+ resources.uniforms.resolution.set(canvas.width, canvas.height)
55
+
56
+ // Attribute management
57
+ resources.attributes.position.set(positionData)
58
+ resources.attributes.position.bind()
59
+ ```
60
+
61
+ ### View Types
62
+
63
+ Each view type can be imported individually too.
64
+
65
+ ```typescript
66
+ import { uniformView, attributeView, interleavedAttributeView, bufferView } from 'view.gl'
67
+ ```
68
+
69
+ #### uniformView
70
+
71
+ Manages shader uniform variables.
72
+
73
+ ```typescript
74
+ import { uniformView } from 'view.gl'
75
+
76
+ const uniforms = uniformView(gl, program, {
77
+ time: { kind: 'float' },
78
+ lights: { kind: 'vec3', size: 8 }, // Array uniform: vec3[8]
79
+ transform: { kind: 'mat4' },
80
+ })
81
+
82
+ uniforms.time.set(performance.now())
83
+ uniforms.lights.set(lightData) // Takes Float32Array for array uniforms
84
+ uniforms.transform.set(transformMatrix)
85
+ ```
86
+
87
+ **Settings:**
88
+
89
+ - `kind`: GLSL type (`'float'`, `'vec2'`, `'mat4'`, `'sampler2D'`, etc.)
90
+ - `size`: Array size (optional) - converts uniform to array type with Float32Array setter
91
+
92
+ #### attributeView
93
+
94
+ Manages vertex attributes with buffer creation, binding, and data management.
95
+
96
+ ```typescript
97
+ import { attributeView } from 'view.gl'
98
+
99
+ const attributes = attributeView(gl, program, {
100
+ position: { kind: 'vec3' },
101
+ instanceOffset: { kind: 'vec2', instanced: true },
102
+ })
103
+
104
+ // Regular attribute for vertices
105
+ attributes.position.set(positionData).bind()
106
+
107
+ // Instanced attribute for per-instance data
108
+ attributes.instanceOffset.set(instanceData).bind()
109
+
110
+ // Draw regular geometry
111
+ gl.drawArrays(gl.TRIANGLES, 0, 3)
112
+
113
+ // Draw instanced geometry
114
+ gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 100)
115
+ ```
116
+
117
+ **Settings:**
118
+
119
+ - `kind`: GLSL type (`'float'`, `'vec2'`, `'vec3'`, `'vec4'`, `'mat2'`, `'mat3'`, `'mat4'`, integer types)
120
+ - `instanced`: Boolean - enables instanced rendering with `vertexAttribDivisor`
121
+ - `buffer`: Custom WebGLBuffer (optional) - uses auto-created buffer if not provided
122
+
123
+ #### interleavedAttributeView
124
+
125
+ Manages interleaved vertex data with automatic stride/offset calculation and VAO optimization.
126
+
127
+ ```typescript
128
+ import { interleavedAttributeView } from 'view.gl'
129
+
130
+ const interleavedAttributes = interleavedAttributeView(gl, program, {
131
+ vertexData: {
132
+ layout: [
133
+ { name: 'position', kind: 'vec3' },
134
+ { name: 'normal', kind: 'vec3' },
135
+ { name: 'uv', kind: 'vec2' },
136
+ ],
137
+ },
138
+ instanceData: {
139
+ layout: [
140
+ { name: 'instancePos', kind: 'vec2' },
141
+ { name: 'instanceColor', kind: 'vec3' },
142
+ ],
143
+ instanced: true,
144
+ },
145
+ })
146
+
147
+ // Upload interleaved data
148
+ interleavedAttributes.vertexData.set(interleavedVertexData)
149
+ interleavedAttributes.instanceData.set(interleavedInstanceData)
150
+
151
+ // Bind all attributes in layout
152
+ interleavedAttributes.vertexData.bind()
153
+ interleavedAttributes.instanceData.bind()
154
+ ```
155
+
156
+ **Settings:**
157
+
158
+ - `layout`: Array of attribute definitions with `name` and `kind`
159
+ - `instanced`: Boolean - applies `vertexAttribDivisor` to all attributes in layout
160
+
161
+ #### bufferView
162
+
163
+ Manages generic WebGL buffers for various uses.
164
+
165
+ ```typescript
166
+ import { bufferView } from 'view.gl'
167
+
168
+ const buffers = bufferView(gl, {
169
+ indices: { target: 'ELEMENT_ARRAY_BUFFER' },
170
+ data: { target: 'ARRAY_BUFFER', usage: 'DYNAMIC_DRAW' },
171
+ })
172
+
173
+ buffers.indices.set(indexData)
174
+ buffers.indices.bind()
175
+
176
+ buffers.data.set(dynamicData)
177
+ buffers.data.bind()
178
+ ```
179
+
180
+ **Settings:**
181
+
182
+ - `target`: Buffer target (`'ARRAY_BUFFER'`, `'ELEMENT_ARRAY_BUFFER'`)
183
+ - `usage`: Usage pattern (`'STATIC_DRAW'`, `'DYNAMIC_DRAW'`, `'STREAM_DRAW'`)
184
+
185
+ ### Drawing Examples
186
+
187
+ #### Regular Drawing
188
+
189
+ ```typescript
190
+ const { attributes } = view(gl, program, {
191
+ attributes: {
192
+ position: { kind: 'vec3' },
193
+ uv: { kind: 'vec2' },
194
+ },
195
+ })
196
+
197
+ attributes.position.set(vertices).bind()
198
+ attributes.uv.set(uvData).bind()
199
+
200
+ gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
201
+ ```
202
+
203
+ #### Instanced Drawing
204
+
205
+ ```typescript
206
+ const { attributes } = view(gl, program, {
207
+ attributes: {
208
+ position: { kind: 'vec3' },
209
+ instanceOffset: { kind: 'vec2', instanced: true },
210
+ instanceColor: { kind: 'vec3', instanced: true },
211
+ },
212
+ })
213
+
214
+ attributes.position.set(triangleVertices).bind()
215
+ attributes.instanceOffset.set(instanceOffsets).bind()
216
+ attributes.instanceColor.set(instanceColors).bind()
217
+
218
+ gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, instanceCount)
219
+ ```
220
+
221
+ ### Features
222
+
223
+ - **Automatic Resource Management**: Buffer creation, disposal, and cleanup
224
+ - **Type Safety**: Compile-time type checking for uniform and attribute operations
225
+ - **WebGL Extensions**: Automatic fallback for instanced arrays and vertex array objects
226
+ - **Memory Management**: AbortSignal support for resource cleanup
227
+
228
+ ## Tag
229
+
230
+ The tag system provides GLSL template literal support with embedded resource definitions.
231
+
232
+ ### Template Features
233
+
234
+ - **Embedded Resources**: Define uniforms and attributes directly in GLSL
235
+ - **Type Safety**: Automatic schema generation from template literals
236
+ - **WebGL Version Support**: Automatic syntax conversion for WebGL1/2
237
+ - **Interleaved Attributes**: Multi-attribute layout support
238
+
239
+ ### Tag Functions
240
+
241
+ - `uniform[kind](name, options?)`: Define uniform variables
242
+ - `attribute[kind](name, options?)`: Define vertex attributes
243
+ - `interleave(name, layout, options?)`: Define interleaved attribute layouts
244
+ - `glsl`: Template literal processor for GLSL code
245
+ - `compile(gl, vertex, fragment)`: Compile shaders and extract schema
246
+
247
+ ### Basic Usage
248
+
249
+ ```typescript
250
+ import { glsl, uniform, attribute, compile } from 'view.gl/tag'
251
+
252
+ const vertexShader = glsl`
253
+ ${attribute.vec3('position')}
254
+ ${attribute.vec2('uv')}
255
+ ${uniform.mat4('projection')}
256
+ ${uniform.mat4('model')}
257
+
258
+ varying vec2 vUv;
259
+
260
+ void main() {
261
+ vUv = uv;
262
+ gl_Position = projection * model * vec4(position, 1.0);
263
+ }
264
+ `
265
+
266
+ const fragmentShader = glsl`
267
+ ${uniform.sampler2D('texture')}
268
+
269
+ varying vec2 vUv;
270
+
271
+ void main() {
272
+ gl_FragColor = texture2D(texture, vUv);
273
+ }
274
+ `
275
+
276
+ const { program, schema } = compile(gl, vertexShader, fragmentShader)
277
+ ```
278
+
279
+ ## Utils
280
+
281
+ WebGL utilities and helper functions.
282
+
283
+ ### WebGL Utilities
284
+
285
+ #### createProgram
286
+
287
+ Creates and links a WebGL program from vertex and fragment shader sources.
288
+
289
+ ```typescript
290
+ import { createProgram } from 'view.gl/utils'
291
+
292
+ const program = createProgram(gl, vertexShaderSource, fragmentShaderSource)
293
+ ```
294
+
295
+ #### createTexture
296
+
297
+ Creates a WebGL texture with specified parameters.
298
+
299
+ ```typescript
300
+ import { createTexture } from 'view.gl/utils'
301
+
302
+ const texture = createTexture(
303
+ gl,
304
+ {
305
+ width: 512,
306
+ height: 512,
307
+ internalFormat: 'RGBA',
308
+ format: 'RGBA',
309
+ type: 'UNSIGNED_BYTE',
310
+ minFilter: 'LINEAR',
311
+ magFilter: 'LINEAR',
312
+ wrapS: 'CLAMP_TO_EDGE',
313
+ wrapT: 'CLAMP_TO_EDGE',
314
+ },
315
+ data,
316
+ )
317
+ ```
318
+
319
+ Automatically validates WebGL2-only formats and provides fallbacks for WebGL1.
320
+
321
+ #### createFramebuffer
322
+
323
+ Creates a framebuffer with attached texture for render-to-texture operations.
324
+
325
+ ```typescript
326
+ import { createFramebuffer } from 'view.gl/utils'
327
+
328
+ const { framebuffer, texture } = createFramebuffer(gl, {
329
+ width: 512,
330
+ height: 512,
331
+ attachment: 'color',
332
+ internalFormat: 'RGBA',
333
+ format: 'RGBA',
334
+ type: 'UNSIGNED_BYTE',
335
+ })
336
+ ```
337
+
338
+ Supports color, depth, stencil, and combined depth-stencil attachments with completeness validation.