@bigmistqke/view.gl 0.1.1 → 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/.claude/settings.local.json +9 -0
- package/.github/ISSUE_TEMPLATE/bug-report.yml +31 -0
- package/.github/ISSUE_TEMPLATE/feature-request.yml +27 -0
- package/.github/workflows/codeql.yml +46 -0
- package/.github/workflows/deploy-page.yml +38 -0
- package/.github/workflows/format.yml +40 -0
- package/README.md +286 -84
- package/dev/404.html +19 -0
- package/dev/dist/404.html +19 -0
- package/dev/dist/assets/bezier-DuLTb2d1.js +294 -0
- package/dev/dist/assets/game_of_life-D7qHdFcv.js +202 -0
- package/dev/dist/assets/grid-Ie22U_9K.js +173 -0
- package/dev/dist/assets/index-0qrXMlfv.js +109 -0
- package/dev/dist/assets/index-C2N3u8wj.css +12 -0
- package/dev/dist/assets/main-BZ1N6pp6.js +32 -0
- package/dev/dist/assets/overview-DAxtMIw5.js +78 -0
- package/dev/dist/assets/pix_sim-DIhzM_vu.js +501 -0
- package/dev/dist/assets/symbol-BouRL-Ni.js +65 -0
- package/dev/dist/assets/tag-C_bg62fD.js +516 -0
- package/dev/dist/assets/tetris-IMnkSi9p.js +344 -0
- package/dev/dist/assets/utils-CvCgsM08.js +50 -0
- package/dev/dist/index.html +51 -0
- package/dev/index.html +39 -10
- package/dev/{bezier.ts → src/examples/bezier.ts} +1 -1
- package/dev/{grid.ts → src/examples/grid.ts} +1 -1
- package/dev/{pix_sim.ts → src/examples/pix_sim.ts} +112 -97
- package/dev/src/examples/symbol.ts +79 -0
- package/dev/{tetris.ts → src/examples/tetris.ts} +1 -0
- package/dev/src/index.css +12 -0
- package/dev/src/index.ts +69 -0
- package/dev/{utils.ts → src/utils.ts} +8 -2
- package/dev/vite.config.ts +9 -1
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +81 -27
- package/dist/index.mjs.map +1 -1
- package/dist/tag.d.mts +9 -9
- package/dist/tag.mjs +46 -17
- package/dist/tag.mjs.map +1 -1
- package/dist/{types-DxpS9Bae.d.mts → types-DoFAJoh-.d.mts} +18 -18
- package/package.json +20 -7
- package/src/index.ts +39 -17
- package/src/tag.ts +47 -39
- package/src/types.ts +24 -24
- package/src/{utils.ts → utils/index.ts} +39 -14
- package/src/utils/object.ts +27 -0
- package/test/index.test.ts +1017 -0
- package/test/mocks/webgl.ts +248 -0
- package/test/setup.ts +13 -0
- package/test/tag.test.ts +790 -0
- package/vitest.config.ts +15 -0
- /package/dev/{game_of_life.ts → src/examples/game_of_life.ts} +0 -0
- /package/dev/{index.ts → src/examples/overview.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,41 +1,66 @@
|
|
|
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
|
-
- [Basic Usage](
|
|
20
|
-
- [Template Features](
|
|
21
|
-
- [Tag Functions](
|
|
22
|
-
|
|
23
|
-
- [
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
- [
|
|
28
|
-
|
|
29
|
-
|
|
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
|
|
30
55
|
|
|
31
56
|
The view system provides type-safe WebGL resource management for uniforms, attributes, and buffers.
|
|
32
57
|
|
|
33
|
-
### Basic Usage
|
|
58
|
+
### 🚀 Basic Usage
|
|
34
59
|
|
|
35
60
|
```typescript
|
|
36
|
-
import { view } from 'view.gl'
|
|
61
|
+
import { view } from '@bigmistqke/view.gl'
|
|
37
62
|
|
|
38
|
-
const
|
|
63
|
+
const { uniforms, attributes } = view(gl, program, {
|
|
39
64
|
uniforms: {
|
|
40
65
|
time: { kind: 'float' },
|
|
41
66
|
resolution: { kind: 'vec2' },
|
|
@@ -50,28 +75,33 @@ const resources = view(gl, program, {
|
|
|
50
75
|
})
|
|
51
76
|
|
|
52
77
|
// Type-safe uniform setting
|
|
53
|
-
|
|
54
|
-
|
|
78
|
+
uniforms.time.set(performance.now())
|
|
79
|
+
uniforms.resolution.set(canvas.width, canvas.height)
|
|
55
80
|
|
|
56
81
|
// Attribute management
|
|
57
|
-
|
|
58
|
-
|
|
82
|
+
attributes.position.set(positionData)
|
|
83
|
+
attributes.position.bind()
|
|
59
84
|
```
|
|
60
85
|
|
|
61
|
-
### View Types
|
|
86
|
+
### 👀 View Types
|
|
62
87
|
|
|
63
88
|
Each view type can be imported individually too.
|
|
64
89
|
|
|
65
90
|
```typescript
|
|
66
|
-
import {
|
|
91
|
+
import {
|
|
92
|
+
uniformView,
|
|
93
|
+
attributeView,
|
|
94
|
+
interleavedAttributeView,
|
|
95
|
+
bufferView,
|
|
96
|
+
} from '@bigmistqke/view.gl'
|
|
67
97
|
```
|
|
68
98
|
|
|
69
|
-
#### uniformView
|
|
99
|
+
#### 🎯 uniformView
|
|
70
100
|
|
|
71
101
|
Manages shader uniform variables.
|
|
72
102
|
|
|
73
103
|
```typescript
|
|
74
|
-
import { uniformView } from 'view.gl'
|
|
104
|
+
import { uniformView } from '@bigmistqke/view.gl'
|
|
75
105
|
|
|
76
106
|
const uniforms = uniformView(gl, program, {
|
|
77
107
|
time: { kind: 'float' },
|
|
@@ -89,12 +119,12 @@ uniforms.transform.set(transformMatrix)
|
|
|
89
119
|
- `kind`: GLSL type (`'float'`, `'vec2'`, `'mat4'`, `'sampler2D'`, etc.)
|
|
90
120
|
- `size`: Array size (optional) - converts uniform to array type with Float32Array setter
|
|
91
121
|
|
|
92
|
-
#### attributeView
|
|
122
|
+
#### 📝 attributeView
|
|
93
123
|
|
|
94
124
|
Manages vertex attributes with buffer creation, binding, and data management.
|
|
95
125
|
|
|
96
126
|
```typescript
|
|
97
|
-
import { attributeView } from 'view.gl'
|
|
127
|
+
import { attributeView } from '@bigmistqke/view.gl'
|
|
98
128
|
|
|
99
129
|
const attributes = attributeView(gl, program, {
|
|
100
130
|
position: { kind: 'vec3' },
|
|
@@ -120,25 +150,25 @@ gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, 100)
|
|
|
120
150
|
- `instanced`: Boolean - enables instanced rendering with `vertexAttribDivisor`
|
|
121
151
|
- `buffer`: Custom WebGLBuffer (optional) - uses auto-created buffer if not provided
|
|
122
152
|
|
|
123
|
-
#### interleavedAttributeView
|
|
153
|
+
#### 🔗 interleavedAttributeView
|
|
124
154
|
|
|
125
155
|
Manages interleaved vertex data with automatic stride/offset calculation and VAO optimization.
|
|
126
156
|
|
|
127
157
|
```typescript
|
|
128
|
-
import { interleavedAttributeView } from 'view.gl'
|
|
158
|
+
import { interleavedAttributeView } from '@bigmistqke/view.gl'
|
|
129
159
|
|
|
130
160
|
const interleavedAttributes = interleavedAttributeView(gl, program, {
|
|
131
161
|
vertexData: {
|
|
132
162
|
layout: [
|
|
133
|
-
{
|
|
134
|
-
{
|
|
135
|
-
{
|
|
163
|
+
{ key: 'position', kind: 'vec3' },
|
|
164
|
+
{ key: 'normal', kind: 'vec3' },
|
|
165
|
+
{ key: 'uv', kind: 'vec2' },
|
|
136
166
|
],
|
|
137
167
|
},
|
|
138
168
|
instanceData: {
|
|
139
169
|
layout: [
|
|
140
|
-
{
|
|
141
|
-
{
|
|
170
|
+
{ key: 'instancePos', kind: 'vec2' },
|
|
171
|
+
{ key: 'instanceColor', kind: 'vec3' },
|
|
142
172
|
],
|
|
143
173
|
instanced: true,
|
|
144
174
|
},
|
|
@@ -158,12 +188,12 @@ interleavedAttributes.instanceData.bind()
|
|
|
158
188
|
- `layout`: Array of attribute definitions with `name` and `kind`
|
|
159
189
|
- `instanced`: Boolean - applies `vertexAttribDivisor` to all attributes in layout
|
|
160
190
|
|
|
161
|
-
#### bufferView
|
|
191
|
+
#### 🗂️ bufferView
|
|
162
192
|
|
|
163
193
|
Manages generic WebGL buffers for various uses.
|
|
164
194
|
|
|
165
195
|
```typescript
|
|
166
|
-
import { bufferView } from 'view.gl'
|
|
196
|
+
import { bufferView } from '@bigmistqke/view.gl'
|
|
167
197
|
|
|
168
198
|
const buffers = bufferView(gl, {
|
|
169
199
|
indices: { target: 'ELEMENT_ARRAY_BUFFER' },
|
|
@@ -182,9 +212,9 @@ buffers.data.bind()
|
|
|
182
212
|
- `target`: Buffer target (`'ARRAY_BUFFER'`, `'ELEMENT_ARRAY_BUFFER'`)
|
|
183
213
|
- `usage`: Usage pattern (`'STATIC_DRAW'`, `'DYNAMIC_DRAW'`, `'STREAM_DRAW'`)
|
|
184
214
|
|
|
185
|
-
### Drawing Examples
|
|
215
|
+
### 🎨 Drawing Examples
|
|
186
216
|
|
|
187
|
-
#### Regular Drawing
|
|
217
|
+
#### 🖼️ Regular Drawing
|
|
188
218
|
|
|
189
219
|
```typescript
|
|
190
220
|
const { attributes } = view(gl, program, {
|
|
@@ -200,7 +230,7 @@ attributes.uv.set(uvData).bind()
|
|
|
200
230
|
gl.drawArrays(gl.TRIANGLES, 0, vertexCount)
|
|
201
231
|
```
|
|
202
232
|
|
|
203
|
-
#### Instanced Drawing
|
|
233
|
+
#### 📱 Instanced Drawing
|
|
204
234
|
|
|
205
235
|
```typescript
|
|
206
236
|
const { attributes } = view(gl, program, {
|
|
@@ -218,86 +248,258 @@ attributes.instanceColor.set(instanceColors).bind()
|
|
|
218
248
|
gl.drawArraysInstanced(gl.TRIANGLES, 0, 3, instanceCount)
|
|
219
249
|
```
|
|
220
250
|
|
|
221
|
-
|
|
251
|
+
## 🏷️ view.gl/tag
|
|
252
|
+
|
|
253
|
+
The tag system provides GLSL template literal support with embedded schema definitions.
|
|
254
|
+
|
|
255
|
+
### 🚀 Basic Usage
|
|
222
256
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
- **WebGL Extensions**: Automatic fallback for instanced arrays and vertex array objects
|
|
226
|
-
- **Memory Management**: AbortSignal support for resource cleanup
|
|
257
|
+
```typescript
|
|
258
|
+
import { glsl, uniform, attribute, compile } from '@bigmistqke/view.gl/tag'
|
|
227
259
|
|
|
228
|
-
|
|
260
|
+
const vertexShader = glsl`
|
|
261
|
+
${attribute.vec3('position')}
|
|
262
|
+
${attribute.vec2('uv')}
|
|
263
|
+
${uniform.mat4('projection')}
|
|
264
|
+
${uniform.mat4('model')}
|
|
265
|
+
|
|
266
|
+
varying vec2 vUv;
|
|
267
|
+
|
|
268
|
+
void main() {
|
|
269
|
+
vUv = uv;
|
|
270
|
+
gl_Position = projection * model * vec4(position, 1.0);
|
|
271
|
+
}
|
|
272
|
+
`
|
|
273
|
+
|
|
274
|
+
const fragmentShader = glsl`
|
|
275
|
+
${uniform.sampler2D('texture')}
|
|
276
|
+
|
|
277
|
+
varying vec2 vUv;
|
|
278
|
+
|
|
279
|
+
void main() {
|
|
280
|
+
gl_FragColor = texture2D(texture, vUv);
|
|
281
|
+
}
|
|
282
|
+
`
|
|
229
283
|
|
|
230
|
-
|
|
284
|
+
const { program, schema } = compile(gl, vertexShader, fragmentShader)
|
|
285
|
+
```
|
|
231
286
|
|
|
232
|
-
### Template Features
|
|
287
|
+
### 📝 Template Features
|
|
233
288
|
|
|
234
289
|
- **Embedded Resources**: Define uniforms and attributes directly in GLSL
|
|
235
290
|
- **Type Safety**: Automatic schema generation from template literals
|
|
236
291
|
- **WebGL Version Support**: Automatic syntax conversion for WebGL1/2
|
|
237
292
|
- **Interleaved Attributes**: Multi-attribute layout support
|
|
293
|
+
- **Symbol Support**: Use JavaScript symbols as keys (see [Symbol Support](#-symbol-support))
|
|
238
294
|
|
|
239
|
-
### Tag Functions
|
|
295
|
+
### ⚙️ Tag Functions
|
|
240
296
|
|
|
241
|
-
|
|
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
|
|
297
|
+
#### 🎯 uniform[kind](name, options?)
|
|
246
298
|
|
|
247
|
-
|
|
299
|
+
Define uniform variables in GLSL templates. Supports all GLSL uniform types.
|
|
248
300
|
|
|
249
301
|
```typescript
|
|
250
|
-
import {
|
|
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'
|
|
251
329
|
|
|
252
330
|
const vertexShader = glsl`
|
|
253
331
|
${attribute.vec3('position')}
|
|
254
332
|
${attribute.vec2('uv')}
|
|
255
|
-
${
|
|
256
|
-
${
|
|
333
|
+
${attribute.vec3('normal')}
|
|
334
|
+
${attribute.vec2('instanceOffset', { instanced: true })}
|
|
257
335
|
|
|
258
|
-
|
|
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
|
+
)}
|
|
259
371
|
|
|
260
372
|
void main() {
|
|
261
|
-
|
|
262
|
-
gl_Position = projection * model * vec4(position, 1.0);
|
|
373
|
+
gl_Position = vec4(position + vec3(instancePos, 0.0), 1.0);
|
|
263
374
|
}
|
|
264
375
|
`
|
|
376
|
+
```
|
|
265
377
|
|
|
266
|
-
|
|
267
|
-
|
|
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;
|
|
268
396
|
|
|
269
|
-
|
|
397
|
+
${uniform.vec2('resolution')}
|
|
270
398
|
|
|
271
399
|
void main() {
|
|
272
|
-
|
|
400
|
+
vec2 uv = gl_FragCoord.xy / resolution;
|
|
401
|
+
gl_FragColor = vec4(uv, 0.0, 1.0);
|
|
273
402
|
}
|
|
274
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'
|
|
275
412
|
|
|
276
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' }, ... }
|
|
277
418
|
```
|
|
278
419
|
|
|
279
|
-
|
|
420
|
+
**Returns**:
|
|
421
|
+
|
|
422
|
+
- `program`: Compiled WebGL program
|
|
423
|
+
- `schema`: Extracted resource definitions for use with `view()`
|
|
424
|
+
|
|
425
|
+
### 🔣 Symbol Support
|
|
426
|
+
|
|
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.
|
|
428
|
+
|
|
429
|
+
```typescript
|
|
430
|
+
const u_time = Symbol('time')
|
|
431
|
+
const a_position = Symbol('position')
|
|
432
|
+
|
|
433
|
+
const shader = glsl`
|
|
434
|
+
${attribute.vec3(a_position)}
|
|
435
|
+
${uniform.float(u_time)}
|
|
436
|
+
|
|
437
|
+
void main() {
|
|
438
|
+
gl_Position = vec4(${a_position}, ${u_time});
|
|
439
|
+
}
|
|
440
|
+
`
|
|
441
|
+
```
|
|
442
|
+
|
|
443
|
+
<details>
|
|
444
|
+
<summary>Usage with vanilla view.gl</summary>
|
|
445
|
+
|
|
446
|
+
When manually constructing GLSL strings, use `toID()` to convert symbols to valid identifiers:
|
|
447
|
+
|
|
448
|
+
```typescript
|
|
449
|
+
import { createProgram, view, toID } from 'view.gl'
|
|
450
|
+
|
|
451
|
+
const u_time = Symbol('time')
|
|
452
|
+
const a_position = Symbol('position')
|
|
453
|
+
|
|
454
|
+
const vertex = `
|
|
455
|
+
attribute vec3 ${toID(a_position)};
|
|
456
|
+
uniform float ${toID(u_time)};
|
|
457
|
+
|
|
458
|
+
void main() {
|
|
459
|
+
gl_Position = vec4(${toID(a_position)}, ${toID(u_time)});
|
|
460
|
+
}
|
|
461
|
+
`
|
|
462
|
+
|
|
463
|
+
const fragment = `
|
|
464
|
+
precision mediump float;
|
|
465
|
+
uniform float ${toID(u_time)};
|
|
466
|
+
|
|
467
|
+
void main() {
|
|
468
|
+
gl_FragColor = vec4(1.0, 1.0, 1.0, sin(${toID(u_time)}));
|
|
469
|
+
}
|
|
470
|
+
`
|
|
471
|
+
|
|
472
|
+
const program = createProgram(gl, vertex, fragment)
|
|
473
|
+
|
|
474
|
+
const { attributes, uniforms } = view(gl, program, {
|
|
475
|
+
uniforms: { [u_time]: { kind: 'float' } },
|
|
476
|
+
attributes: { [a_position]: { kind: 'vec3' } },
|
|
477
|
+
})
|
|
478
|
+
|
|
479
|
+
attributes[a_position].set(vertexData)
|
|
480
|
+
uniforms[u_time].set(performance.now())
|
|
481
|
+
```
|
|
280
482
|
|
|
281
|
-
|
|
483
|
+
</details>
|
|
282
484
|
|
|
283
|
-
|
|
485
|
+
## 🛠️ Utils
|
|
284
486
|
|
|
285
|
-
|
|
487
|
+
### 🏗️ createProgram
|
|
286
488
|
|
|
287
489
|
Creates and links a WebGL program from vertex and fragment shader sources.
|
|
288
490
|
|
|
289
491
|
```typescript
|
|
290
|
-
import { createProgram } from 'view.gl/utils'
|
|
492
|
+
import { createProgram } from '@bigmistqke/view.gl/utils'
|
|
291
493
|
|
|
292
494
|
const program = createProgram(gl, vertexShaderSource, fragmentShaderSource)
|
|
293
495
|
```
|
|
294
496
|
|
|
295
|
-
|
|
497
|
+
### 🖼️ createTexture
|
|
296
498
|
|
|
297
499
|
Creates a WebGL texture with specified parameters.
|
|
298
500
|
|
|
299
501
|
```typescript
|
|
300
|
-
import { createTexture } from 'view.gl/utils'
|
|
502
|
+
import { createTexture } from '@bigmistqke/view.gl/utils'
|
|
301
503
|
|
|
302
504
|
const texture = createTexture(
|
|
303
505
|
gl,
|
|
@@ -318,12 +520,12 @@ const texture = createTexture(
|
|
|
318
520
|
|
|
319
521
|
Automatically validates WebGL2-only formats and provides fallbacks for WebGL1.
|
|
320
522
|
|
|
321
|
-
|
|
523
|
+
### 🖥️ createFramebuffer
|
|
322
524
|
|
|
323
525
|
Creates a framebuffer with attached texture for render-to-texture operations.
|
|
324
526
|
|
|
325
527
|
```typescript
|
|
326
|
-
import { createFramebuffer } from 'view.gl/utils'
|
|
528
|
+
import { createFramebuffer } from '@bigmistqke/view.gl/utils'
|
|
327
529
|
|
|
328
530
|
const { framebuffer, texture } = createFramebuffer(gl, {
|
|
329
531
|
width: 512,
|
package/dev/404.html
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>view.gl</title>
|
|
6
|
+
<script>
|
|
7
|
+
// Redirect to index.html with the current path as a query parameter
|
|
8
|
+
const path = window.location.pathname.slice(1); // Remove leading slash
|
|
9
|
+
if (path) {
|
|
10
|
+
window.location.replace(window.location.origin + '/?redirect=' + path);
|
|
11
|
+
} else {
|
|
12
|
+
window.location.replace(window.location.origin);
|
|
13
|
+
}
|
|
14
|
+
</script>
|
|
15
|
+
</head>
|
|
16
|
+
<body>
|
|
17
|
+
Redirecting...
|
|
18
|
+
</body>
|
|
19
|
+
</html>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>view.gl</title>
|
|
6
|
+
<script>
|
|
7
|
+
// Redirect to index.html with the current path as a query parameter
|
|
8
|
+
const path = window.location.pathname.slice(1); // Remove leading slash
|
|
9
|
+
if (path) {
|
|
10
|
+
window.location.replace(window.location.origin + '/?redirect=' + path);
|
|
11
|
+
} else {
|
|
12
|
+
window.location.replace(window.location.origin);
|
|
13
|
+
}
|
|
14
|
+
</script>
|
|
15
|
+
</head>
|
|
16
|
+
<body>
|
|
17
|
+
Redirecting...
|
|
18
|
+
</body>
|
|
19
|
+
</html>
|