twgl.js 5.3.1 → 5.4.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.
@@ -1,15 +1,192 @@
1
1
 
2
+ /**
3
+ * Various default settings for twgl.
4
+ *
5
+ * Note: You can call this any number of times. Example:
6
+ *
7
+ * twgl.setDefaults({ textureColor: [1, 0, 0, 1] });
8
+ * twgl.setDefaults({ attribPrefix: 'a_' });
9
+ *
10
+ * is equivalent to
11
+ *
12
+ * twgl.setDefaults({
13
+ * textureColor: [1, 0, 0, 1],
14
+ * attribPrefix: 'a_',
15
+ * });
16
+ *
17
+ * @typedef {Object} Defaults
18
+ * @property {string} [attribPrefix] The prefix to stick on attributes
19
+ *
20
+ * When writing shaders I prefer to name attributes with `a_`, uniforms with `u_` and varyings with `v_`
21
+ * as it makes it clear where they came from. But, when building geometry I prefer using un-prefixed names.
22
+ *
23
+ * In other words I'll create arrays of geometry like this
24
+ *
25
+ * const arrays = {
26
+ * position: ...
27
+ * normal: ...
28
+ * texcoord: ...
29
+ * };
30
+ *
31
+ * But need those mapped to attributes and my attributes start with `a_`.
32
+ *
33
+ * Default: `""`
34
+ *
35
+ * @property {number[]} [textureColor] Array of 4 values in the range 0 to 1
36
+ *
37
+ * The default texture color is used when loading textures from
38
+ * urls. Because the URL will be loaded async we'd like to be
39
+ * able to use the texture immediately. By putting a 1x1 pixel
40
+ * color in the texture we can start using the texture before
41
+ * the URL has loaded.
42
+ *
43
+ * Default: `[0.5, 0.75, 1, 1]`
44
+ *
45
+ * @property {string} [crossOrigin]
46
+ *
47
+ * If not undefined sets the crossOrigin attribute on images
48
+ * that twgl creates when downloading images for textures.
49
+ *
50
+ * Also see {@link TextureOptions}.
51
+ *
52
+ * @property {bool} [addExtensionsToContext]
53
+ *
54
+ * If true, then, when twgl will try to add any supported WebGL extensions
55
+ * directly to the context under their normal GL names. For example
56
+ * if ANGLE_instances_arrays exists then twgl would enable it,
57
+ * add the functions `vertexAttribDivisor`, `drawArraysInstanced`,
58
+ * `drawElementsInstanced`, and the constant `VERTEX_ATTRIB_ARRAY_DIVISOR`
59
+ * to the `WebGLRenderingContext`.
60
+ *
61
+ * @memberOf module:twgl
62
+ */
2
63
  export type Defaults = {
3
64
  attribPrefix?: string;
4
65
  textureColor?: number[];
5
66
  crossOrigin?: string;
6
67
  addExtensionsToContext?: boolean;
7
68
  };
69
+ /**
70
+ * Sets various defaults for twgl.
71
+ *
72
+ * In the interest of terseness which is kind of the point
73
+ * of twgl I've integrated a few of the older functions here
74
+ *
75
+ * @param {Defaults} newDefaults The default settings.
76
+ * @memberOf module:twgl
77
+ */
8
78
  export function setDefaults(newDefaults: Defaults): void;
79
+ /**
80
+ * Attempts to enable all of the following extensions
81
+ * and add their functions and constants to the
82
+ * `WebGLRenderingContext` using their normal non-extension like names.
83
+ *
84
+ * ANGLE_instanced_arrays
85
+ * EXT_blend_minmax
86
+ * EXT_color_buffer_float
87
+ * EXT_color_buffer_half_float
88
+ * EXT_disjoint_timer_query
89
+ * EXT_disjoint_timer_query_webgl2
90
+ * EXT_frag_depth
91
+ * EXT_sRGB
92
+ * EXT_shader_texture_lod
93
+ * EXT_texture_filter_anisotropic
94
+ * OES_element_index_uint
95
+ * OES_standard_derivatives
96
+ * OES_texture_float
97
+ * OES_texture_float_linear
98
+ * OES_texture_half_float
99
+ * OES_texture_half_float_linear
100
+ * OES_vertex_array_object
101
+ * WEBGL_color_buffer_float
102
+ * WEBGL_compressed_texture_atc
103
+ * WEBGL_compressed_texture_etc1
104
+ * WEBGL_compressed_texture_pvrtc
105
+ * WEBGL_compressed_texture_s3tc
106
+ * WEBGL_compressed_texture_s3tc_srgb
107
+ * WEBGL_depth_texture
108
+ * WEBGL_draw_buffers
109
+ *
110
+ * For example if `ANGLE_instanced_arrays` exists then the functions
111
+ * `drawArraysInstanced`, `drawElementsInstanced`, `vertexAttribDivisor`
112
+ * and the constant `VERTEX_ATTRIB_ARRAY_DIVISOR` are added to the
113
+ * `WebGLRenderingContext`.
114
+ *
115
+ * Note that if you want to know if the extension exists you should
116
+ * probably call `gl.getExtension` for each extension. Alternatively
117
+ * you can check for the existence of the functions or constants that
118
+ * are expected to be added. For example
119
+ *
120
+ * if (gl.drawBuffers) {
121
+ * // Either WEBGL_draw_buffers was enabled OR you're running in WebGL2
122
+ * ....
123
+ *
124
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
125
+ * @memberOf module:twgl
126
+ */
9
127
  export function addExtensionsToContext(gl: WebGLRenderingContext): void;
128
+ /**
129
+ * Creates a webgl context.
130
+ *
131
+ * Will return a WebGL2 context if possible.
132
+ *
133
+ * You can check if it's WebGL2 with
134
+ *
135
+ * twgl.isWebGL2(gl);
136
+ *
137
+ * @param {HTMLCanvasElement} canvas The canvas tag to get
138
+ * context from. If one is not passed in one will be
139
+ * created.
140
+ * @return {WebGLRenderingContext} The created context.
141
+ */
10
142
  export function createContext(canvas: HTMLCanvasElement): WebGLRenderingContext;
143
+ /**
144
+ * Gets a WebGL context. Will create a WebGL2 context if possible.
145
+ *
146
+ * You can check if it's WebGL2 with
147
+ *
148
+ * function isWebGL2(gl) {
149
+ * return gl.getParameter(gl.VERSION).indexOf("WebGL 2.0 ") == 0;
150
+ * }
151
+ *
152
+ * Note: For a WebGL1 context will attempt to enable Vertex Array Objects
153
+ * and add WebGL2 entry points. (unless you first set defaults with
154
+ * `twgl.setDefaults({enableVertexArrayObjects: false})`;
155
+ *
156
+ * @param {HTMLCanvasElement} canvas a canvas element.
157
+ * @param {WebGLContextAttributes} [opt_attribs] optional webgl context creation attributes
158
+ * @return {WebGLRenderingContext} The created context.
159
+ * @memberOf module:twgl
160
+ */
11
161
  export function getContext(canvas: HTMLCanvasElement, opt_attribs?: WebGLContextAttributes): WebGLRenderingContext;
162
+ /**
163
+ * Resize a canvas to match the size it's displayed.
164
+ * @param {HTMLCanvasElement} canvas The canvas to resize.
165
+ * @param {number} [multiplier] So you can pass in `window.devicePixelRatio` or other scale value if you want to.
166
+ * @return {boolean} true if the canvas was resized.
167
+ * @memberOf module:twgl
168
+ */
12
169
  export function resizeCanvasToDisplaySize(canvas: HTMLCanvasElement, multiplier?: number): boolean;
170
+ /**
171
+ * The info for an attribute. This is effectively just the arguments to `gl.vertexAttribPointer` plus the WebGLBuffer
172
+ * for the attribute.
173
+ *
174
+ * @typedef {Object} AttribInfo
175
+ * @property {number[]|ArrayBufferView} [value] a constant value for the attribute. Note: if this is set the attribute will be
176
+ * disabled and set to this constant value and all other values will be ignored.
177
+ * @property {number} [numComponents] the number of components for this attribute.
178
+ * @property {number} [size] synonym for `numComponents`.
179
+ * @property {number} [type] the type of the attribute (eg. `gl.FLOAT`, `gl.UNSIGNED_BYTE`, etc...) Default = `gl.FLOAT`
180
+ * @property {boolean} [normalize] whether or not to normalize the data. Default = false
181
+ * @property {number} [offset] offset into buffer in bytes. Default = 0
182
+ * @property {number} [stride] the stride in bytes per element. Default = 0
183
+ * @property {number} [divisor] the divisor in instances. Default = 0.
184
+ * Requires WebGL2 or the ANGLE_instanced_arrays extension.
185
+ * and, if you're using WebGL1 you must have called {@link addExtensionsToContext}
186
+ * @property {WebGLBuffer} buffer the buffer that contains the data for this attribute
187
+ * @property {number} [drawType] the draw type passed to gl.bufferData. Default = gl.STATIC_DRAW
188
+ * @memberOf module:twgl
189
+ */
13
190
  export type AttribInfo = {
14
191
  value?: number[] | ArrayBufferView;
15
192
  numComponents?: number;
@@ -22,6 +199,43 @@ export type AttribInfo = {
22
199
  buffer: WebGLBuffer;
23
200
  drawType?: number;
24
201
  };
202
+ /**
203
+ * Use this type of array spec when TWGL can't guess the type or number of components of an array
204
+ * @typedef {Object} FullArraySpec
205
+ * @property {number[]|ArrayBufferView} [value] a constant value for the attribute. Note: if this is set the attribute will be
206
+ * disabled and set to this constant value and all other values will be ignored.
207
+ * @property {(number|number[]|ArrayBufferView)} [data] The data of the array. A number alone becomes the number of elements of type.
208
+ * @property {number} [numComponents] number of components for `vertexAttribPointer`. Default is based on the name of the array.
209
+ * If `coord` is in the name assumes `numComponents = 2`.
210
+ * If `color` is in the name assumes `numComponents = 4`.
211
+ * otherwise assumes `numComponents = 3`
212
+ * @property {number|TypedArrayConstructor} [type] type. This is used if `data` is a JavaScript array, or `buffer` is passed in, or `data` is a number.
213
+ * It can either be the constructor for a typedarray. (eg. `Uint8Array`) OR a WebGL type, (eg `gl.UNSIGNED_BYTE`).
214
+ * For example if you want colors in a `Uint8Array` you might have a `FullArraySpec` like `{ type: gl.UNSIGNED_BYTE, data: [255,0,255,255, ...], }`.
215
+ * @property {number} [size] synonym for `numComponents`.
216
+ * @property {boolean} [normalize] normalize for `vertexAttribPointer`. Default is true if type is `Int8Array` or `Uint8Array` otherwise false.
217
+ * @property {number} [stride] stride for `vertexAttribPointer`. Default = 0
218
+ * @property {number} [offset] offset for `vertexAttribPointer`. Default = 0
219
+ * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0.
220
+ * Requires WebGL2 or the ANGLE_instanced_arrays extension.
221
+ * and, if you using WebGL1 you must have called {@link addExtensionsToContext}
222
+ * @property {string} [attrib] name of attribute this array maps to. Defaults to same name as array prefixed by the default attribPrefix.
223
+ * @property {string} [name] synonym for `attrib`.
224
+ * @property {string} [attribName] synonym for `attrib`.
225
+ * @property {WebGLBuffer} [buffer] Buffer to use for this attribute. This lets you use your own buffer
226
+ * but you will need to supply `numComponents` and `type`. You can effectively pass an `AttribInfo`
227
+ * to provide this. Example:
228
+ *
229
+ * const bufferInfo1 = twgl.createBufferInfoFromArrays(gl, {
230
+ * position: [1, 2, 3, ... ],
231
+ * });
232
+ * const bufferInfo2 = twgl.createBufferInfoFromArrays(gl, {
233
+ * position: bufferInfo1.attribs.position, // use the same buffer from bufferInfo1
234
+ * });
235
+ *
236
+ * @property {number} [drawType] the draw type passed to gl.bufferData. Default = gl.STATIC_DRAW
237
+ * @memberOf module:twgl
238
+ */
25
239
  export type FullArraySpec = {
26
240
  value?: number[] | ArrayBufferView;
27
241
  data?: number | number[] | ArrayBufferView;
@@ -38,10 +252,68 @@ export type FullArraySpec = {
38
252
  buffer?: WebGLBuffer;
39
253
  drawType?: number;
40
254
  };
255
+ /**
256
+ * An individual array in {@link Arrays}
257
+ *
258
+ * When passed to {@link createBufferInfoFromArrays} if an ArraySpec is `number[]` or `ArrayBufferView`
259
+ * the types will be guessed based on the name. `indices` will be `Uint16Array`, everything else will
260
+ * be `Float32Array`. If an ArraySpec is a number it's the number of floats for an empty (zeroed) buffer.
261
+ *
262
+ * @typedef {(number|number[]|ArrayBufferView|FullArraySpec)} ArraySpec
263
+ * @memberOf module:twgl
264
+ */
41
265
  export type ArraySpec = number | number[] | ArrayBufferView | FullArraySpec;
266
+ /**
267
+ * This is a JavaScript object of arrays by name. The names should match your shader's attributes. If your
268
+ * attributes have a common prefix you can specify it by calling {@link setAttributePrefix}.
269
+ *
270
+ * Bare JavaScript Arrays
271
+ *
272
+ * var arrays = {
273
+ * position: [-1, 1, 0],
274
+ * normal: [0, 1, 0],
275
+ * ...
276
+ * }
277
+ *
278
+ * Bare TypedArrays
279
+ *
280
+ * var arrays = {
281
+ * position: new Float32Array([-1, 1, 0]),
282
+ * color: new Uint8Array([255, 128, 64, 255]),
283
+ * ...
284
+ * }
285
+ *
286
+ * * Will guess at `numComponents` if not specified based on name.
287
+ *
288
+ * If `coord` is in the name assumes `numComponents = 2`
289
+ *
290
+ * If `color` is in the name assumes `numComponents = 4`
291
+ *
292
+ * otherwise assumes `numComponents = 3`
293
+ *
294
+ * Objects with various fields. See {@link FullArraySpec}.
295
+ *
296
+ * var arrays = {
297
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
298
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
299
+ * normal: { numComponents: 3, data: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], },
300
+ * indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
301
+ * };
302
+ *
303
+ * @typedef {Object.<string, ArraySpec>} Arrays
304
+ * @memberOf module:twgl
305
+ */
42
306
  export type Arrays = {
43
307
  [key: string]: ArraySpec;
44
308
  };
309
+ /**
310
+ * @typedef {Object} BufferInfo
311
+ * @property {number} numElements The number of elements to pass to `gl.drawArrays` or `gl.drawElements`.
312
+ * @property {number} [elementType] The type of indices `UNSIGNED_BYTE`, `UNSIGNED_SHORT` etc..
313
+ * @property {WebGLBuffer} [indices] The indices `ELEMENT_ARRAY_BUFFER` if any indices exist.
314
+ * @property {Object.<string, AttribInfo>} [attribs] The attribs appropriate to call `setAttributes`
315
+ * @memberOf module:twgl
316
+ */
45
317
  export type BufferInfo = {
46
318
  numElements: number;
47
319
  elementType?: number;
@@ -50,6 +322,41 @@ export type BufferInfo = {
50
322
  [key: string]: AttribInfo;
51
323
  };
52
324
  };
325
+ /**
326
+ * A DrawObject is useful for putting objects in to an array and passing them to {@link drawObjectList}.
327
+ *
328
+ * You need either a `BufferInfo` or a `VertexArrayInfo`.
329
+ *
330
+ * @typedef {Object} DrawObject
331
+ * @property {boolean} [active] whether or not to draw. Default = `true` (must be `false` to be not true). In other words `undefined` = `true`
332
+ * @property {number} [type] type to draw eg. `gl.TRIANGLES`, `gl.LINES`, etc...
333
+ * @property {ProgramInfo} programInfo A ProgramInfo as returned from {@link createProgramInfo}
334
+ * @property {BufferInfo} [bufferInfo] A BufferInfo as returned from {@link createBufferInfoFromArrays}
335
+ * @property {VertexArrayInfo} [vertexArrayInfo] A VertexArrayInfo as returned from {@link createVertexArrayInfo}
336
+ * @property {Object<string, ?>} uniforms The values for the uniforms.
337
+ * You can pass multiple objects by putting them in an array. For example
338
+ *
339
+ * var sharedUniforms = {
340
+ * u_fogNear: 10,
341
+ * u_projection: ...
342
+ * ...
343
+ * };
344
+ *
345
+ * var localUniforms = {
346
+ * u_world: ...
347
+ * u_diffuseColor: ...
348
+ * };
349
+ *
350
+ * var drawObj = {
351
+ * ...
352
+ * uniforms: [sharedUniforms, localUniforms],
353
+ * };
354
+ *
355
+ * @property {number} [offset] the offset to pass to `gl.drawArrays` or `gl.drawElements`. Defaults to 0.
356
+ * @property {number} [count] the count to pass to `gl.drawArrays` or `gl.drawElements`. Defaults to bufferInfo.numElements.
357
+ * @property {number} [instanceCount] the number of instances. Defaults to undefined.
358
+ * @memberOf module:twgl
359
+ */
53
360
  export type DrawObject = {
54
361
  active?: boolean;
55
362
  type?: number;
@@ -63,6 +370,37 @@ export type DrawObject = {
63
370
  count?: number;
64
371
  instanceCount?: number;
65
372
  };
373
+ /**
374
+ * The options for a framebuffer attachment.
375
+ *
376
+ * Note: For a `format` that is a texture include all the texture
377
+ * options from {@link TextureOptions} for example
378
+ * `min`, `mag`, `clamp`, etc... Note that unlike {@link TextureOptions}
379
+ * `auto` defaults to `false` for attachment textures but `min` and `mag` default
380
+ * to `gl.LINEAR` and `wrap` defaults to `CLAMP_TO_EDGE`
381
+ *
382
+ * @typedef {Object} AttachmentOptions
383
+ * @property {number} [attachmentPoint] The attachment point. Defaults
384
+ * to `gl.COLOR_ATTACHMENT0 + ndx` unless type is a depth or stencil type
385
+ * then it's gl.DEPTH_ATTACHMENT or `gl.DEPTH_STENCIL_ATTACHMENT` depending
386
+ * on the format or attachment type.
387
+ * @property {number} [format] The format. If one of `gl.RGBA4`,
388
+ * `gl.RGB565`, `gl.RGB5_A1`, `gl.DEPTH_COMPONENT16`,
389
+ * `gl.STENCIL_INDEX8` or `gl.DEPTH_STENCIL` then will create a
390
+ * renderbuffer. Otherwise will create a texture. Default = `gl.RGBA`
391
+ * @property {number} [type] The type. Used for texture. Default = `gl.UNSIGNED_BYTE`.
392
+ * @property {number} [target] The texture target for `gl.framebufferTexture2D`.
393
+ * Defaults to `gl.TEXTURE_2D`. Set to appropriate face for cube maps.
394
+ * @property {number} [samples] The number of samples. Default = 1
395
+ * @property {number} [level] level for `gl.framebufferTexture2D`. Defaults to 0.
396
+ * @property {number} [layer] layer for `gl.framebufferTextureLayer`. Defaults to undefined.
397
+ * If set then `gl.framebufferTextureLayer` is called, if not then `gl.framebufferTexture2D`
398
+ * @property {(WebGLRenderbuffer | WebGLTexture)} [attachment] An existing renderbuffer or texture.
399
+ * If provided will attach this Object. This allows you to share
400
+ * attachments across framebuffers.
401
+ * @memberOf module:twgl
402
+ * @mixes TextureOptions
403
+ */
66
404
  export type AttachmentOptions = TextureOptions & {
67
405
  attachmentPoint?: number;
68
406
  format?: number;
@@ -73,13 +411,40 @@ export type AttachmentOptions = TextureOptions & {
73
411
  layer?: number;
74
412
  attachment?: WebGLRenderbuffer | WebGLTexture;
75
413
  };
414
+ /**
415
+ * @typedef {Object} FramebufferInfo
416
+ * @property {WebGLFramebuffer} framebuffer The WebGLFramebuffer for this framebufferInfo
417
+ * @property {Array.<(WebGLRenderbuffer | WebGLTexture)>} attachments The created attachments in the same order as passed in to {@link createFramebufferInfo}.
418
+ * @property {number} width The width of the framebuffer and its attachments
419
+ * @property {number} height The width of the framebuffer and its attachments
420
+ * @memberOf module:twgl
421
+ */
76
422
  export type FramebufferInfo = {
77
423
  framebuffer: WebGLFramebuffer;
78
424
  attachments: (WebGLRenderbuffer | WebGLTexture)[];
79
425
  width: number;
80
426
  height: number;
81
427
  };
428
+ /**
429
+ * Error Callback
430
+ * @callback ErrorCallback
431
+ * @param {string} msg error message.
432
+ * @param {number} [lineOffset] amount to add to line number
433
+ * @memberOf module:twgl
434
+ */
82
435
  export type ErrorCallback = (msg: string, lineOffset?: number) => void;
436
+ /**
437
+ * @typedef {Object} FullProgramSpec
438
+ * @property {string[]} shaders the shader source or element ids.
439
+ * @property {function(string)} [errorCallback] callback for errors
440
+ * @property {Object.<string,number>|string[]} [attribLocations] a attribute name to location map, or array of attribute names where index = location.
441
+ * @property {(BufferInfo|Object.<string,AttribInfo>|string[])} [transformFeedbackVaryings] If passed
442
+ * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
443
+ * you can pass an array of names.
444
+ * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
445
+ * @property {ProgramCallback} [callback] callback for async program compilation.
446
+ * @memberOf module:twgl
447
+ */
83
448
  export type FullProgramSpec = {
84
449
  shaders: string[];
85
450
  errorCallback?: (...params: any[]) => any;
@@ -92,7 +457,22 @@ export type FullProgramSpec = {
92
457
  transformFeedbackMode?: number;
93
458
  callback?: ProgramCallback;
94
459
  };
460
+ /**
461
+ * @typedef {string[]|FullProgramSpec} ProgramSpec
462
+ * @memberOf module:twgl
463
+ */
95
464
  export type ProgramSpec = string[] | FullProgramSpec;
465
+ /**
466
+ * @typedef {Object} ProgramOptions
467
+ * @property {function(string)} [errorCallback] callback for errors
468
+ * @property {Object.<string,number>|string[]} [attribLocations] a attribute name to location map, or array of attribute names where index = location.
469
+ * @property {(BufferInfo|Object.<string,AttribInfo>|string[])} [transformFeedbackVaryings] If passed
470
+ * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
471
+ * you can pass an array of names.
472
+ * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
473
+ * @property {ProgramCallback} [callback] callback for async program compilation.
474
+ * @memberOf module:twgl
475
+ */
96
476
  export type ProgramOptions = {
97
477
  errorCallback?: (...params: any[]) => any;
98
478
  attribLocations?: {
@@ -104,22 +484,61 @@ export type ProgramOptions = {
104
484
  transformFeedbackMode?: number;
105
485
  callback?: ProgramCallback;
106
486
  };
487
+ /**
488
+ * @typedef {Object} TransformFeedbackInfo
489
+ * @property {number} index index of transform feedback
490
+ * @property {number} type GL type
491
+ * @property {number} size 1 - 4
492
+ * @memberOf module:twgl
493
+ */
107
494
  export type TransformFeedbackInfo = {
108
495
  index: number;
109
496
  type: number;
110
497
  size: number;
111
498
  };
499
+ /**
500
+ * Create TransformFeedbackInfo for passing to bindTransformFeedbackInfo.
501
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
502
+ * @param {WebGLProgram} program an existing WebGLProgram.
503
+ * @return {Object<string, TransformFeedbackInfo>}
504
+ * @memberOf module:twgl
505
+ */
112
506
  export function createTransformFeedbackInfo(gl: WebGLRenderingContext, program: WebGLProgram): {
113
507
  [key: string]: TransformFeedbackInfo;
114
508
  };
509
+ /**
510
+ * Binds buffers for transform feedback.
511
+ *
512
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
513
+ * @param {(ProgramInfo|Object<string, TransformFeedbackInfo>)} transformFeedbackInfo A ProgramInfo or TransformFeedbackInfo.
514
+ * @param {(BufferInfo|Object<string, AttribInfo>)} [bufferInfo] A BufferInfo or set of AttribInfos.
515
+ * @memberOf module:twgl
516
+ */
115
517
  export function bindTransformFeedbackInfo(gl: WebGLRenderingContext, transformFeedbackInfo: ProgramInfo | {
116
518
  [key: string]: TransformFeedbackInfo;
117
519
  }, bufferInfo?: BufferInfo | {
118
520
  [key: string]: AttribInfo;
119
521
  }): void;
522
+ /**
523
+ * Creates a transform feedback and sets the buffers
524
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
525
+ * @param {ProgramInfo} programInfo A ProgramInfo as returned from {@link createProgramInfo}
526
+ * @param {(BufferInfo|Object<string, AttribInfo>)} [bufferInfo] A BufferInfo or set of AttribInfos.
527
+ * @return {WebGLTransformFeedback} the created transform feedback
528
+ * @memberOf module:twgl
529
+ */
120
530
  export function createTransformFeedback(gl: WebGLRenderingContext, programInfo: ProgramInfo, bufferInfo?: BufferInfo | {
121
531
  [key: string]: AttribInfo;
122
532
  }): WebGLTransformFeedback;
533
+ /**
534
+ * @typedef {Object} UniformData
535
+ * @property {string} name The name of the uniform
536
+ * @property {number} type The WebGL type enum for this uniform
537
+ * @property {number} size The number of elements for this uniform
538
+ * @property {number} blockNdx The block index this uniform appears in
539
+ * @property {number} offset The byte offset in the block for this uniform's value
540
+ * @memberOf module:twgl
541
+ */
123
542
  export type UniformData = {
124
543
  name: string;
125
544
  type: number;
@@ -127,6 +546,19 @@ export type UniformData = {
127
546
  blockNdx: number;
128
547
  offset: number;
129
548
  };
549
+ /**
550
+ * The specification for one UniformBlockObject
551
+ *
552
+ * @typedef {Object} BlockSpec
553
+ * @property {number} index The index of the block.
554
+ * @property {number} size The size in bytes needed for the block
555
+ * @property {number[]} uniformIndices The indices of the uniforms used by the block. These indices
556
+ * correspond to entries in a UniformData array in the {@link UniformBlockSpec}.
557
+ * @property {bool} usedByVertexShader Self explanatory
558
+ * @property {bool} usedByFragmentShader Self explanatory
559
+ * @property {bool} used Self explanatory
560
+ * @memberOf module:twgl
561
+ */
130
562
  export type BlockSpec = {
131
563
  index: number;
132
564
  size: number;
@@ -135,12 +567,47 @@ export type BlockSpec = {
135
567
  usedByFragmentShader: boolean;
136
568
  used: boolean;
137
569
  };
570
+ /**
571
+ * A `UniformBlockSpec` represents the data needed to create and bind
572
+ * UniformBlockObjects for a given program
573
+ *
574
+ * @typedef {Object} UniformBlockSpec
575
+ * @property {Object.<string, BlockSpec>} blockSpecs The BlockSpec for each block by block name
576
+ * @property {UniformData[]} uniformData An array of data for each uniform by uniform index.
577
+ * @memberOf module:twgl
578
+ */
138
579
  export type UniformBlockSpec = {
139
580
  blockSpecs: {
140
581
  [key: string]: BlockSpec;
141
582
  };
142
583
  uniformData: UniformData[];
143
584
  };
585
+ /**
586
+ * Represents a UniformBlockObject including an ArrayBuffer with all the uniform values
587
+ * and a corresponding WebGLBuffer to hold those values on the GPU
588
+ *
589
+ * @typedef {Object} UniformBlockInfo
590
+ * @property {string} name The name of the block
591
+ * @property {ArrayBuffer} array The array buffer that contains the uniform values
592
+ * @property {Float32Array} asFloat A float view on the array buffer. This is useful
593
+ * inspecting the contents of the buffer in the debugger.
594
+ * @property {WebGLBuffer} buffer A WebGL buffer that will hold a copy of the uniform values for rendering.
595
+ * @property {number} [offset] offset into buffer
596
+ * @property {Object<string, ArrayBufferView>} uniforms A uniform name to ArrayBufferView map.
597
+ * each Uniform has a correctly typed `ArrayBufferView` into array at the correct offset
598
+ * and length of that uniform. So for example a float uniform would have a 1 float `Float32Array`
599
+ * view. A single mat4 would have a 16 element `Float32Array` view. An ivec2 would have an
600
+ * `Int32Array` view, etc.
601
+ * @property {Object<string, function>} setters A setter for this uniform.
602
+ * The reason to use setters is elements of arrays are padded to vec4 sizes which
603
+ * means if you want to set an array of 4 floats you'd need to set 16 values
604
+ * (or set elements 0, 4, 8, 12). In other words
605
+ * `someBlockInfo.uniforms.some4FloatArrayUniform.set([0, , , , 1, , , , 2, , , , 3])`
606
+ * where as the setter handles just passing in [0, 1, 2, 3] either directly as in
607
+ * `someBlockInfo.setter.some4FloatArrayUniform.set([0, 1, 2, 3])` (not recommended)
608
+ * or via {@link setBlockUniforms}
609
+ * @memberOf module:twgl
610
+ */
144
611
  export type UniformBlockInfo = {
145
612
  name: string;
146
613
  array: ArrayBuffer;
@@ -154,6 +621,15 @@ export type UniformBlockInfo = {
154
621
  [key: string]: (...params: any[]) => any;
155
622
  };
156
623
  };
624
+ /**
625
+ * @typedef {Object} ProgramInfo
626
+ * @property {WebGLProgram} program A shader program
627
+ * @property {Object<string, function>} uniformSetters object of setters as returned from createUniformSetters,
628
+ * @property {Object<string, function>} attribSetters object of setters as returned from createAttribSetters,
629
+ * @property {UniformBlockSpec} [uniformBlockSpec] a uniform block spec for making UniformBlockInfos with createUniformBlockInfo etc..
630
+ * @property {Object<string, TransformFeedbackInfo>} [transformFeedbackInfo] info for transform feedbacks
631
+ * @memberOf module:twgl
632
+ */
157
633
  export type ProgramInfo = {
158
634
  program: WebGLProgram;
159
635
  uniformSetters: {
@@ -167,7 +643,107 @@ export type ProgramInfo = {
167
643
  [key: string]: TransformFeedbackInfo;
168
644
  };
169
645
  };
646
+ /**
647
+ * A function to generate the source for a texture.
648
+ * @callback TextureFunc
649
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
650
+ * @param {TextureOptions} options the texture options
651
+ * @return {*} Returns any of the things documented for `src` for {@link TextureOptions}.
652
+ * @memberOf module:twgl
653
+ */
170
654
  export type TextureFunc = (gl: WebGLRenderingContext, options: TextureOptions) => any;
655
+ /**
656
+ * Texture options passed to most texture functions. Each function will use whatever options
657
+ * are appropriate for its needs. This lets you pass the same options to all functions.
658
+ *
659
+ * Note: A `TexImageSource` is defined in the WebGL spec as a `HTMLImageElement`, `HTMLVideoElement`,
660
+ * `HTMLCanvasElement`, `ImageBitmap`, or `ImageData`.
661
+ *
662
+ * @typedef {Object} TextureOptions
663
+ * @property {number} [target] the type of texture `gl.TEXTURE_2D` or `gl.TEXTURE_CUBE_MAP`. Defaults to `gl.TEXTURE_2D`.
664
+ * @property {number} [level] the mip level to affect. Defaults to 0. Note, if set auto will be considered false unless explicitly set to true.
665
+ * @property {number} [width] the width of the texture. Only used if src is an array or typed array or null.
666
+ * @property {number} [height] the height of a texture. Only used if src is an array or typed array or null.
667
+ * @property {number} [depth] the depth of a texture. Only used if src is an array or type array or null and target is `TEXTURE_3D` .
668
+ * @property {number} [min] the min filter setting (eg. `gl.LINEAR`). Defaults to `gl.NEAREST_MIPMAP_LINEAR`
669
+ * or if texture is not a power of 2 on both dimensions then defaults to `gl.LINEAR`.
670
+ * @property {number} [mag] the mag filter setting (eg. `gl.LINEAR`). Defaults to `gl.LINEAR`
671
+ * @property {number} [minMag] both the min and mag filter settings.
672
+ * @property {number} [internalFormat] internal format for texture. Defaults to `gl.RGBA`
673
+ * @property {number} [format] format for texture. Defaults to `gl.RGBA`.
674
+ * @property {number} [type] type for texture. Defaults to `gl.UNSIGNED_BYTE` unless `src` is ArrayBufferView. If `src`
675
+ * is ArrayBufferView defaults to type that matches ArrayBufferView type.
676
+ * @property {number} [wrap] Texture wrapping for both S and T (and R if TEXTURE_3D or WebGLSampler). Defaults to `gl.REPEAT` for 2D unless src is WebGL1 and src not npot and `gl.CLAMP_TO_EDGE` for cube
677
+ * @property {number} [wrapS] Texture wrapping for S. Defaults to `gl.REPEAT` and `gl.CLAMP_TO_EDGE` for cube. If set takes precedence over `wrap`.
678
+ * @property {number} [wrapT] Texture wrapping for T. Defaults to `gl.REPEAT` and `gl.CLAMP_TO_EDGE` for cube. If set takes precedence over `wrap`.
679
+ * @property {number} [wrapR] Texture wrapping for R. Defaults to `gl.REPEAT` and `gl.CLAMP_TO_EDGE` for cube. If set takes precedence over `wrap`.
680
+ * @property {number} [minLod] TEXTURE_MIN_LOD setting
681
+ * @property {number} [maxLod] TEXTURE_MAX_LOD setting
682
+ * @property {number} [baseLevel] TEXTURE_BASE_LEVEL setting
683
+ * @property {number} [maxLevel] TEXTURE_MAX_LEVEL setting
684
+ * @property {number} [unpackAlignment] The `gl.UNPACK_ALIGNMENT` used when uploading an array. Defaults to 1.
685
+ * @property {number[]|ArrayBufferView} [color] Color to initialize this texture with if loading an image asynchronously.
686
+ * The default use a blue 1x1 pixel texture. You can set another default by calling `twgl.setDefaults`
687
+ * or you can set an individual texture's initial color by setting this property. Example: `[1, .5, .5, 1]` = pink
688
+ * @property {number} [premultiplyAlpha] Whether or not to premultiply alpha. Defaults to whatever the current setting is.
689
+ * This lets you set it once before calling `twgl.createTexture` or `twgl.createTextures` and only override
690
+ * the current setting for specific textures.
691
+ * @property {number} [flipY] Whether or not to flip the texture vertically on upload. Defaults to whatever the current setting is.
692
+ * This lets you set it once before calling `twgl.createTexture` or `twgl.createTextures` and only override
693
+ * the current setting for specific textures.
694
+ * @property {number} [colorspaceConversion] Whether or not to let the browser do colorspace conversion of the texture on upload. Defaults to whatever the current setting is.
695
+ * This lets you set it once before calling `twgl.createTexture` or `twgl.createTextures` and only override
696
+ * the current setting for specific textures.
697
+ * @property {boolean} [auto] If `undefined` or `true`, in WebGL1, texture filtering is set automatically for non-power of 2 images and
698
+ * mips are generated for power of 2 images. In WebGL2 mips are generated if they can be. Note: if `level` is set above
699
+ * then then `auto` is assumed to be `false` unless explicity set to `true`.
700
+ * @property {number[]} [cubeFaceOrder] The order that cube faces are pulled out of an img or set of images. The default is
701
+ *
702
+ * [gl.TEXTURE_CUBE_MAP_POSITIVE_X,
703
+ * gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
704
+ * gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
705
+ * gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
706
+ * gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
707
+ * gl.TEXTURE_CUBE_MAP_NEGATIVE_Z]
708
+ *
709
+ * @property {(number[]|ArrayBufferView|TexImageSource|TexImageSource[]|string|string[]|TextureFunc)} [src] source for texture
710
+ *
711
+ * If `string` then it's assumed to be a URL to an image. The image will be downloaded async. A usable
712
+ * 1x1 pixel texture will be returned immediately. The texture will be updated once the image has downloaded.
713
+ * If `target` is `gl.TEXTURE_CUBE_MAP` will attempt to divide image into 6 square pieces. 1x6, 6x1, 3x2, 2x3.
714
+ * The pieces will be uploaded in `cubeFaceOrder`
715
+ *
716
+ * If `string[]` or `TexImageSource[]` and target is `gl.TEXTURE_CUBE_MAP` then it must have 6 entries, one for each face of a cube map.
717
+ *
718
+ * If `string[]` or `TexImageSource[]` and target is `gl.TEXTURE_2D_ARRAY` then each entry is a slice of the a 2d array texture
719
+ * and will be scaled to the specified width and height OR to the size of the first image that loads.
720
+ *
721
+ * If `TexImageSource` then it wil be used immediately to create the contents of the texture. Examples `HTMLImageElement`,
722
+ * `HTMLCanvasElement`, `HTMLVideoElement`.
723
+ *
724
+ * If `number[]` or `ArrayBufferView` it's assumed to be data for a texture. If `width` or `height` is
725
+ * not specified it is guessed as follows. First the number of elements is computed by `src.length / numComponents`
726
+ * where `numComponents` is derived from `format`. If `target` is `gl.TEXTURE_CUBE_MAP` then `numElements` is divided
727
+ * by 6. Then
728
+ *
729
+ * * If neither `width` nor `height` are specified and `sqrt(numElements)` is an integer then width and height
730
+ * are set to `sqrt(numElements)`. Otherwise `width = numElements` and `height = 1`.
731
+ *
732
+ * * If only one of `width` or `height` is specified then the other equals `numElements / specifiedDimension`.
733
+ *
734
+ * If `number[]` will be converted to `type`.
735
+ *
736
+ * If `src` is a function it will be called with a `WebGLRenderingContext` and these options.
737
+ * Whatever it returns is subject to these rules. So it can return a string url, an `HTMLElement`
738
+ * an array etc...
739
+ *
740
+ * If `src` is undefined then an empty texture will be created of size `width` by `height`.
741
+ *
742
+ * @property {string} [crossOrigin] What to set the crossOrigin property of images when they are downloaded.
743
+ * default: undefined. Also see {@link setDefaults}.
744
+ *
745
+ * @memberOf module:twgl
746
+ */
171
747
  export type TextureOptions = {
172
748
  target?: number;
173
749
  level?: number;
@@ -198,48 +774,886 @@ export type TextureOptions = {
198
774
  src?: number[] | ArrayBufferView | TexImageSource | TexImageSource[] | string | string[] | TextureFunc;
199
775
  crossOrigin?: string;
200
776
  };
777
+ /**
778
+ * The src image(s) used to create a texture.
779
+ *
780
+ * When you call {@link createTexture} or {@link createTextures}
781
+ * you can pass in urls for images to load into the textures. If it's a single url
782
+ * then this will be a single HTMLImageElement. If it's an array of urls used for a cubemap
783
+ * this will be a corresponding array of images for the cubemap.
784
+ *
785
+ * @typedef {HTMLImageElement|HTMLImageElement[]} TextureSrc
786
+ * @memberOf module:twgl
787
+ */
201
788
  export type TextureSrc = HTMLImageElement | HTMLImageElement[];
789
+ /**
790
+ * A callback for when an image finished downloading and been uploaded into a texture
791
+ * @callback TextureReadyCallback
792
+ * @param {*} err If truthy there was an error.
793
+ * @param {WebGLTexture} texture the texture.
794
+ * @param {TextureSrc} source image(s) used to as the src for the texture
795
+ * @memberOf module:twgl
796
+ */
202
797
  export type TextureReadyCallback = (err: any, texture: WebGLTexture, source: TextureSrc) => void;
798
+ /**
799
+ * A callback for when all images have finished downloading and been uploaded into their respective textures
800
+ * @callback TexturesReadyCallback
801
+ * @param {*} err If truthy there was an error.
802
+ * @param {Object.<string, WebGLTexture>} textures the created textures by name. Same as returned by {@link createTextures}.
803
+ * @param {Object.<string, TextureSrc>} sources the image(s) used for the texture by name.
804
+ * @memberOf module:twgl
805
+ */
203
806
  export type TexturesReadyCallback = (err: any, textures: {
204
807
  [key: string]: WebGLTexture;
205
808
  }, sources: {
206
809
  [key: string]: TextureSrc;
207
810
  }) => void;
811
+ /**
812
+ * A callback for when an image finished downloading and been uploaded into a texture
813
+ * @callback CubemapReadyCallback
814
+ * @param {*} err If truthy there was an error.
815
+ * @param {WebGLTexture} tex the texture.
816
+ * @param {HTMLImageElement[]} imgs the images for each face.
817
+ * @memberOf module:twgl
818
+ */
208
819
  export type CubemapReadyCallback = (err: any, tex: WebGLTexture, imgs: HTMLImageElement[]) => void;
820
+ /**
821
+ * A callback for when an image finished downloading and been uploaded into a texture
822
+ * @callback ThreeDReadyCallback
823
+ * @param {*} err If truthy there was an error.
824
+ * @param {WebGLTexture} tex the texture.
825
+ * @param {HTMLImageElement[]} imgs the images for each slice.
826
+ * @memberOf module:twgl
827
+ */
209
828
  export type ThreeDReadyCallback = (err: any, tex: WebGLTexture, imgs: HTMLImageElement[]) => void;
829
+ /**
830
+ * Check if context is WebGL 2.0
831
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
832
+ * @return {bool} true if it's WebGL 2.0
833
+ * @memberOf module:twgl
834
+ */
210
835
  export function isWebGL2(gl: WebGLRenderingContext): boolean;
836
+ /**
837
+ * Check if context is WebGL 1.0
838
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
839
+ * @return {bool} true if it's WebGL 1.0
840
+ * @memberOf module:twgl
841
+ */
211
842
  export function isWebGL1(gl: WebGLRenderingContext): boolean;
843
+ /**
844
+ * Gets a string for WebGL enum
845
+ *
846
+ * Note: Several enums are the same. Without more
847
+ * context (which function) it's impossible to always
848
+ * give the correct enum. As it is, for matching values
849
+ * it gives all enums. Checking the WebGL2RenderingContext
850
+ * that means
851
+ *
852
+ * 0 = ZERO | POINT | NONE | NO_ERROR
853
+ * 1 = ONE | LINES | SYNC_FLUSH_COMMANDS_BIT
854
+ * 32777 = BLEND_EQUATION_RGB | BLEND_EQUATION_RGB
855
+ * 36662 = COPY_READ_BUFFER | COPY_READ_BUFFER_BINDING
856
+ * 36663 = COPY_WRITE_BUFFER | COPY_WRITE_BUFFER_BINDING
857
+ * 36006 = FRAMEBUFFER_BINDING | DRAW_FRAMEBUFFER_BINDING
858
+ *
859
+ * It's also not useful for bits really unless you pass in individual bits.
860
+ * In other words
861
+ *
862
+ * const bits = gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT;
863
+ * twgl.glEnumToString(gl, bits); // not going to work
864
+ *
865
+ * Note that some enums only exist on extensions. If you
866
+ * want them to show up you need to pass the extension at least
867
+ * once. For example
868
+ *
869
+ * const ext = gl.getExtension('WEBGL_compressed_texture_s3tc');
870
+ * if (ext) {
871
+ * twgl.glEnumToString(ext, 0); // just prime the function
872
+ *
873
+ * ..later..
874
+ *
875
+ * const internalFormat = ext.COMPRESSED_RGB_S3TC_DXT1_EXT;
876
+ * console.log(twgl.glEnumToString(gl, internalFormat));
877
+ *
878
+ * Notice I didn't have to pass the extension the second time. This means
879
+ * you can have place that generically gets an enum for texture formats for example.
880
+ * and as long as you primed the function with the extensions
881
+ *
882
+ * If you're using `twgl.addExtensionsToContext` to enable your extensions
883
+ * then twgl will automatically get the extension's enums.
884
+ *
885
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext or any extension object
886
+ * @param {number} value the value of the enum you want to look up.
887
+ * @return {string} enum string or hex value
888
+ * @memberOf module:twgl
889
+ * @function glEnumToString
890
+ */
212
891
  export function glEnumToString(gl: WebGLRenderingContext, value: number): string;
892
+ /**
893
+ * @typedef {Object} VertexArrayInfo
894
+ * @property {number} numElements The number of elements to pass to `gl.drawArrays` or `gl.drawElements`.
895
+ * @property {number} [elementType] The type of indices `UNSIGNED_BYTE`, `UNSIGNED_SHORT` etc..
896
+ * @property {WebGLVertexArrayObject} [vertexArrayObject] a vertex array object
897
+ * @memberOf module:twgl
898
+ */
213
899
  export type VertexArrayInfo = {
214
900
  numElements: number;
215
901
  elementType?: number;
216
902
  vertexArrayObject?: WebGLVertexArrayObject;
217
903
  };
904
+ /**
905
+ * Sets the contents of a buffer attached to an attribInfo
906
+ *
907
+ * This is helper function to dynamically update a buffer.
908
+ *
909
+ * Let's say you make a bufferInfo
910
+ *
911
+ * var arrays = {
912
+ * position: new Float32Array([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]),
913
+ * texcoord: new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]),
914
+ * normal: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]),
915
+ * indices: new Uint16Array([0, 1, 2, 1, 2, 3]),
916
+ * };
917
+ * var bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
918
+ *
919
+ * And you want to dynamically update the positions. You could do this
920
+ *
921
+ * // assuming arrays.position has already been updated with new data.
922
+ * twgl.setAttribInfoBufferFromArray(gl, bufferInfo.attribs.position, arrays.position);
923
+ *
924
+ * @param {WebGLRenderingContext} gl
925
+ * @param {AttribInfo} attribInfo The attribInfo who's buffer contents to set. NOTE: If you have an attribute prefix
926
+ * the name of the attribute will include the prefix.
927
+ * @param {ArraySpec} array Note: it is arguably inefficient to pass in anything but a typed array because anything
928
+ * else will have to be converted to a typed array before it can be used by WebGL. During init time that
929
+ * inefficiency is usually not important but if you're updating data dynamically best to be efficient.
930
+ * @param {number} [offset] an optional offset into the buffer. This is only an offset into the WebGL buffer
931
+ * not the array. To pass in an offset into the array itself use a typed array and create an `ArrayBufferView`
932
+ * for the portion of the array you want to use.
933
+ *
934
+ * var someArray = new Float32Array(1000); // an array with 1000 floats
935
+ * var someSubArray = new Float32Array(someArray.buffer, offsetInBytes, sizeInUnits); // a view into someArray
936
+ *
937
+ * Now you can pass `someSubArray` into setAttribInfoBufferFromArray`
938
+ * @memberOf module:twgl/attributes
939
+ */
218
940
  export function setAttribInfoBufferFromArray(gl: WebGLRenderingContext, attribInfo: AttribInfo, array: ArraySpec, offset?: number): void;
941
+ /**
942
+ * Creates a BufferInfo from an object of arrays.
943
+ *
944
+ * This can be passed to {@link setBuffersAndAttributes} and to
945
+ * {@link module:twgl:drawBufferInfo}.
946
+ *
947
+ * Given an object like
948
+ *
949
+ * var arrays = {
950
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
951
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
952
+ * normal: { numComponents: 3, data: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], },
953
+ * indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
954
+ * };
955
+ *
956
+ * Creates an BufferInfo like this
957
+ *
958
+ * bufferInfo = {
959
+ * numElements: 4, // or whatever the number of elements is
960
+ * indices: WebGLBuffer, // this property will not exist if there are no indices
961
+ * attribs: {
962
+ * position: { buffer: WebGLBuffer, numComponents: 3, },
963
+ * normal: { buffer: WebGLBuffer, numComponents: 3, },
964
+ * texcoord: { buffer: WebGLBuffer, numComponents: 2, },
965
+ * },
966
+ * };
967
+ *
968
+ * The properties of arrays can be JavaScript arrays in which case the number of components
969
+ * will be guessed.
970
+ *
971
+ * var arrays = {
972
+ * position: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0],
973
+ * texcoord: [0, 0, 0, 1, 1, 0, 1, 1],
974
+ * normal: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
975
+ * indices: [0, 1, 2, 1, 2, 3],
976
+ * };
977
+ *
978
+ * They can also be TypedArrays
979
+ *
980
+ * var arrays = {
981
+ * position: new Float32Array([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]),
982
+ * texcoord: new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]),
983
+ * normal: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]),
984
+ * indices: new Uint16Array([0, 1, 2, 1, 2, 3]),
985
+ * };
986
+ *
987
+ * Or AugmentedTypedArrays
988
+ *
989
+ * var positions = createAugmentedTypedArray(3, 4);
990
+ * var texcoords = createAugmentedTypedArray(2, 4);
991
+ * var normals = createAugmentedTypedArray(3, 4);
992
+ * var indices = createAugmentedTypedArray(3, 2, Uint16Array);
993
+ *
994
+ * positions.push([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]);
995
+ * texcoords.push([0, 0, 0, 1, 1, 0, 1, 1]);
996
+ * normals.push([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]);
997
+ * indices.push([0, 1, 2, 1, 2, 3]);
998
+ *
999
+ * var arrays = {
1000
+ * position: positions,
1001
+ * texcoord: texcoords,
1002
+ * normal: normals,
1003
+ * indices: indices,
1004
+ * };
1005
+ *
1006
+ * For the last example it is equivalent to
1007
+ *
1008
+ * var bufferInfo = {
1009
+ * attribs: {
1010
+ * position: { numComponents: 3, buffer: gl.createBuffer(), },
1011
+ * texcoord: { numComponents: 2, buffer: gl.createBuffer(), },
1012
+ * normal: { numComponents: 3, buffer: gl.createBuffer(), },
1013
+ * },
1014
+ * indices: gl.createBuffer(),
1015
+ * numElements: 6,
1016
+ * };
1017
+ *
1018
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.position.buffer);
1019
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.position, gl.STATIC_DRAW);
1020
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.texcoord.buffer);
1021
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.texcoord, gl.STATIC_DRAW);
1022
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.normal.buffer);
1023
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.normal, gl.STATIC_DRAW);
1024
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferInfo.indices);
1025
+ * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, arrays.indices, gl.STATIC_DRAW);
1026
+ *
1027
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1028
+ * @param {Arrays} arrays Your data
1029
+ * @param {BufferInfo} [srcBufferInfo] An existing
1030
+ * buffer info to start from. WebGLBuffers etc specified
1031
+ * in the srcBufferInfo will be used in a new BufferInfo
1032
+ * with any arrays specified overriding the ones in
1033
+ * srcBufferInfo.
1034
+ * @return {BufferInfo} A BufferInfo
1035
+ * @memberOf module:twgl/attributes
1036
+ */
219
1037
  export function createBufferInfoFromArrays(gl: WebGLRenderingContext, arrays: Arrays, srcBufferInfo?: BufferInfo): BufferInfo;
1038
+ /**
1039
+ * Calls `gl.drawElements` or `gl.drawArrays`, whichever is appropriate
1040
+ *
1041
+ * normally you'd call `gl.drawElements` or `gl.drawArrays` yourself
1042
+ * but calling this means if you switch from indexed data to non-indexed
1043
+ * data you don't have to remember to update your draw call.
1044
+ *
1045
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1046
+ * @param {(BufferInfo|VertexArrayInfo)} bufferInfo A BufferInfo as returned from {@link createBufferInfoFromArrays} or
1047
+ * a VertexArrayInfo as returned from {@link createVertexArrayInfo}
1048
+ * @param {number} [type] eg (gl.TRIANGLES, gl.LINES, gl.POINTS, gl.TRIANGLE_STRIP, ...). Defaults to `gl.TRIANGLES`
1049
+ * @param {number} [count] An optional count. Defaults to bufferInfo.numElements
1050
+ * @param {number} [offset] An optional offset. Defaults to 0.
1051
+ * @param {number} [instanceCount] An optional instanceCount. if set then `drawArraysInstanced` or `drawElementsInstanced` will be called
1052
+ * @memberOf module:twgl/draw
1053
+ */
220
1054
  export function drawBufferInfo(gl: WebGLRenderingContext, bufferInfo: BufferInfo | VertexArrayInfo, type?: number, count?: number, offset?: number, instanceCount?: number): void;
1055
+ /**
1056
+ * Draws a list of objects
1057
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1058
+ * @param {DrawObject[]} objectsToDraw an array of objects to draw.
1059
+ * @memberOf module:twgl/draw
1060
+ */
221
1061
  export function drawObjectList(gl: WebGLRenderingContext, objectsToDraw: DrawObject[]): void;
1062
+ /**
1063
+ * Creates a framebuffer and attachments.
1064
+ *
1065
+ * This returns a {@link FramebufferInfo} because it needs to return the attachments as well as the framebuffer.
1066
+ * It also leaves the framebuffer it just created as the currently bound `FRAMEBUFFER`.
1067
+ * Note: If this is WebGL2 or if you called {@link addExtensionsToContext} then it will set the drawBuffers
1068
+ * to `[COLOR_ATTACHMENT0, COLOR_ATTACHMENT1, ...]` for how ever many color attachments were created.
1069
+ *
1070
+ * The simplest usage
1071
+ *
1072
+ * // create an RGBA/UNSIGNED_BYTE texture and DEPTH_STENCIL renderbuffer
1073
+ * const fbi = twgl.createFramebufferInfo(gl);
1074
+ *
1075
+ * More complex usage
1076
+ *
1077
+ * // create an RGB565 renderbuffer and a STENCIL_INDEX8 renderbuffer
1078
+ * const attachments = [
1079
+ * { format: RGB565, mag: NEAREST },
1080
+ * { format: STENCIL_INDEX8 },
1081
+ * ]
1082
+ * const fbi = twgl.createFramebufferInfo(gl, attachments);
1083
+ *
1084
+ * Passing in a specific size
1085
+ *
1086
+ * const width = 256;
1087
+ * const height = 256;
1088
+ * const fbi = twgl.createFramebufferInfo(gl, attachments, width, height);
1089
+ *
1090
+ * **Note!!** It is up to you to check if the framebuffer is renderable by calling `gl.checkFramebufferStatus`.
1091
+ * [WebGL1 only guarantees 3 combinations of attachments work](https://www.khronos.org/registry/webgl/specs/latest/1.0/#6.6).
1092
+ *
1093
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
1094
+ * @param {AttachmentOptions[]} [attachments] which attachments to create. If not provided the default is a framebuffer with an
1095
+ * `RGBA`, `UNSIGNED_BYTE` texture `COLOR_ATTACHMENT0` and a `DEPTH_STENCIL` renderbuffer `DEPTH_STENCIL_ATTACHMENT`.
1096
+ * @param {number} [width] the width for the attachments. Default = size of drawingBuffer
1097
+ * @param {number} [height] the height for the attachments. Default = size of drawingBuffer
1098
+ * @return {FramebufferInfo} the framebuffer and attachments.
1099
+ * @memberOf module:twgl/framebuffers
1100
+ */
222
1101
  export function createFramebufferInfo(gl: WebGLRenderingContext, attachments?: AttachmentOptions[], width?: number, height?: number): FramebufferInfo;
1102
+ /**
1103
+ * Resizes the attachments of a framebuffer.
1104
+ *
1105
+ * You need to pass in the same `attachments` as you passed in {@link createFramebufferInfo}
1106
+ * because TWGL has no idea the format/type of each attachment.
1107
+ *
1108
+ * The simplest usage
1109
+ *
1110
+ * // create an RGBA/UNSIGNED_BYTE texture and DEPTH_STENCIL renderbuffer
1111
+ * const fbi = twgl.createFramebufferInfo(gl);
1112
+ *
1113
+ * ...
1114
+ *
1115
+ * function render() {
1116
+ * if (twgl.resizeCanvasToDisplaySize(gl.canvas)) {
1117
+ * // resize the attachments
1118
+ * twgl.resizeFramebufferInfo(gl, fbi);
1119
+ * }
1120
+ *
1121
+ * More complex usage
1122
+ *
1123
+ * // create an RGB565 renderbuffer and a STENCIL_INDEX8 renderbuffer
1124
+ * const attachments = [
1125
+ * { format: RGB565, mag: NEAREST },
1126
+ * { format: STENCIL_INDEX8 },
1127
+ * ]
1128
+ * const fbi = twgl.createFramebufferInfo(gl, attachments);
1129
+ *
1130
+ * ...
1131
+ *
1132
+ * function render() {
1133
+ * if (twgl.resizeCanvasToDisplaySize(gl.canvas)) {
1134
+ * // resize the attachments to match
1135
+ * twgl.resizeFramebufferInfo(gl, fbi, attachments);
1136
+ * }
1137
+ *
1138
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
1139
+ * @param {FramebufferInfo} framebufferInfo a framebufferInfo as returned from {@link createFramebufferInfo}.
1140
+ * @param {AttachmentOptions[]} [attachments] the same attachments options as passed to {@link createFramebufferInfo}.
1141
+ * @param {number} [width] the width for the attachments. Default = size of drawingBuffer
1142
+ * @param {number} [height] the height for the attachments. Default = size of drawingBuffer
1143
+ * @memberOf module:twgl/framebuffers
1144
+ */
223
1145
  export function resizeFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo: FramebufferInfo, attachments?: AttachmentOptions[], width?: number, height?: number): void;
1146
+ /**
1147
+ * Binds a framebuffer
1148
+ *
1149
+ * This function pretty much solely exists because I spent hours
1150
+ * trying to figure out why something I wrote wasn't working only
1151
+ * to realize I forget to set the viewport dimensions.
1152
+ * My hope is this function will fix that.
1153
+ *
1154
+ * It is effectively the same as
1155
+ *
1156
+ * gl.bindFramebuffer(gl.FRAMEBUFFER, someFramebufferInfo.framebuffer);
1157
+ * gl.viewport(0, 0, someFramebufferInfo.width, someFramebufferInfo.height);
1158
+ *
1159
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
1160
+ * @param {FramebufferInfo|null} [framebufferInfo] a framebufferInfo as returned from {@link createFramebufferInfo}.
1161
+ * If falsy will bind the canvas.
1162
+ * @param {number} [target] The target. If not passed `gl.FRAMEBUFFER` will be used.
1163
+ * @memberOf module:twgl/framebuffers
1164
+ */
224
1165
  export function bindFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo?: FramebufferInfo | null, target?: number): void;
1166
+ /**
1167
+ * Creates a ProgramInfo from 2 sources.
1168
+ *
1169
+ * A ProgramInfo contains
1170
+ *
1171
+ * programInfo = {
1172
+ * program: WebGLProgram,
1173
+ * uniformSetters: object of setters as returned from createUniformSetters,
1174
+ * attribSetters: object of setters as returned from createAttribSetters,
1175
+ * }
1176
+ *
1177
+ * NOTE: There are 4 signatures for this function
1178
+ *
1179
+ * twgl.createProgramInfo(gl, [vs, fs], options);
1180
+ * twgl.createProgramInfo(gl, [vs, fs], opt_errFunc);
1181
+ * twgl.createProgramInfo(gl, [vs, fs], opt_attribs, opt_errFunc);
1182
+ * twgl.createProgramInfo(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
1183
+ *
1184
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
1185
+ * to use.
1186
+ * @param {string[]} shaderSources Array of sources for the
1187
+ * shaders or ids. The first is assumed to be the vertex shader,
1188
+ * the second the fragment shader.
1189
+ * @param {ProgramOptions|string[]|ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
1190
+ * @param {number[]} [opt_locations|ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
1191
+ * @param {ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
1192
+ * on error. If you want something else pass an callback. It's passed an error message.
1193
+ * @return {ProgramInfo?} The created ProgramInfo or null if it failed to link or compile
1194
+ * @memberOf module:twgl/programs
1195
+ */
225
1196
  export function createProgramInfo(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): ProgramInfo;
1197
+ /**
1198
+ * Creates a `UniformBlockInfo` for the specified block
1199
+ *
1200
+ * Note: **If the blockName matches no existing blocks a warning is printed to the console and a dummy
1201
+ * `UniformBlockInfo` is returned**. This is because when debugging GLSL
1202
+ * it is common to comment out large portions of a shader or for example set
1203
+ * the final output to a constant. When that happens blocks get optimized out.
1204
+ * If this function did not create dummy blocks your code would crash when debugging.
1205
+ *
1206
+ * @param {WebGL2RenderingContext} gl A WebGL2RenderingContext
1207
+ * @param {ProgramInfo} programInfo a `ProgramInfo`
1208
+ * as returned from {@link createProgramInfo}
1209
+ * @param {string} blockName The name of the block.
1210
+ * @return {UniformBlockInfo} The created UniformBlockInfo
1211
+ * @memberOf module:twgl/programs
1212
+ */
226
1213
  export function createUniformBlockInfo(gl: WebGL2RenderingContext, programInfo: ProgramInfo, blockName: string): UniformBlockInfo;
1214
+ /**
1215
+ * Binds a uniform block to the matching uniform block point.
1216
+ * Matches by blocks by name so blocks must have the same name not just the same
1217
+ * structure.
1218
+ *
1219
+ * If you have changed any values and you upload the values into the corresponding WebGLBuffer
1220
+ * call {@link setUniformBlock} instead.
1221
+ *
1222
+ * @param {WebGL2RenderingContext} gl A WebGL 2 rendering context.
1223
+ * @param {(ProgramInfo|UniformBlockSpec)} programInfo a `ProgramInfo`
1224
+ * as returned from {@link createProgramInfo} or or `UniformBlockSpec` as
1225
+ * returned from {@link createUniformBlockSpecFromProgram}.
1226
+ * @param {UniformBlockInfo} uniformBlockInfo a `UniformBlockInfo` as returned from
1227
+ * {@link createUniformBlockInfo}.
1228
+ * @return {bool} true if buffer was bound. If the programInfo has no block with the same block name
1229
+ * no buffer is bound.
1230
+ * @memberOf module:twgl/programs
1231
+ */
227
1232
  export function bindUniformBlock(gl: WebGL2RenderingContext, programInfo: ProgramInfo | UniformBlockSpec, uniformBlockInfo: UniformBlockInfo): boolean;
1233
+ /**
1234
+ * Uploads the current uniform values to the corresponding WebGLBuffer
1235
+ * and binds that buffer to the program's corresponding bind point for the uniform block object.
1236
+ *
1237
+ * If you haven't changed any values and you only need to bind the uniform block object
1238
+ * call {@link bindUniformBlock} instead.
1239
+ *
1240
+ * @param {WebGL2RenderingContext} gl A WebGL 2 rendering context.
1241
+ * @param {(ProgramInfo|UniformBlockSpec)} programInfo a `ProgramInfo`
1242
+ * as returned from {@link createProgramInfo} or or `UniformBlockSpec` as
1243
+ * returned from {@link createUniformBlockSpecFromProgram}.
1244
+ * @param {UniformBlockInfo} uniformBlockInfo a `UniformBlockInfo` as returned from
1245
+ * {@link createUniformBlockInfo}.
1246
+ * @memberOf module:twgl/programs
1247
+ */
228
1248
  export function setUniformBlock(gl: WebGL2RenderingContext, programInfo: ProgramInfo | UniformBlockSpec, uniformBlockInfo: UniformBlockInfo): void;
1249
+ /**
1250
+ * Sets values of a uniform block object
1251
+ *
1252
+ * @param {UniformBlockInfo} uniformBlockInfo A UniformBlockInfo as returned by {@link createUniformBlockInfo}.
1253
+ * @param {Object.<string, ?>} values A uniform name to value map where the value is correct for the given
1254
+ * type of uniform. So for example given a block like
1255
+ *
1256
+ * uniform SomeBlock {
1257
+ * float someFloat;
1258
+ * vec2 someVec2;
1259
+ * vec3 someVec3Array[2];
1260
+ * int someInt;
1261
+ * }
1262
+ *
1263
+ * You can set the values of the uniform block with
1264
+ *
1265
+ * twgl.setBlockUniforms(someBlockInfo, {
1266
+ * someFloat: 12.3,
1267
+ * someVec2: [1, 2],
1268
+ * someVec3Array: [1, 2, 3, 4, 5, 6],
1269
+ * someInt: 5,
1270
+ * }
1271
+ *
1272
+ * Arrays can be JavaScript arrays or typed arrays
1273
+ *
1274
+ * You can also fill out structure and array values either via
1275
+ * shortcut. Example
1276
+ *
1277
+ * // -- in shader --
1278
+ * struct Light {
1279
+ * float intensity;
1280
+ * vec4 color;
1281
+ * float nearFar[2];
1282
+ * };
1283
+ * uniform Lights {
1284
+ * Light lights[2];
1285
+ * };
1286
+ *
1287
+ * // in JavaScript
1288
+ *
1289
+ * twgl.setBlockUniforms(someBlockInfo, {
1290
+ * lights: [
1291
+ * { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
1292
+ * { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
1293
+ * ],
1294
+ * });
1295
+ *
1296
+ * or the more traditional way
1297
+ *
1298
+ * twgl.setBlockUniforms(someBlockInfo, {
1299
+ * "lights[0].intensity": 5.0,
1300
+ * "lights[0].color": [1, 0, 0, 1],
1301
+ * "lights[0].nearFar": [0.1, 10],
1302
+ * "lights[1].intensity": 2.0,
1303
+ * "lights[1].color": [0, 0, 1, 1],
1304
+ * "lights[1].nearFar": [0.2, 15],
1305
+ * });
1306
+ *
1307
+ * You can also specify partial paths
1308
+ *
1309
+ * twgl.setBlockUniforms(someBlockInfo, {
1310
+ * 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
1311
+ * });
1312
+ *
1313
+ * But you can not specify leaf array indices.
1314
+ *
1315
+ * twgl.setBlockUniforms(someBlockInfo, {
1316
+ * 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
1317
+ * 'lights[1].nearFar': [0.2, 15], // GOOD
1318
+ * });
1319
+ *
1320
+ * **IMPORTANT!**, packing in a UniformBlock is unintuitive.
1321
+ * For example the actual layout of `someVec3Array` above in memory
1322
+ * is `1, 2, 3, unused, 4, 5, 6, unused`. twgl takes in 6 values
1323
+ * as shown about and copies them, skipping the padding. This might
1324
+ * be confusing if you're already familiar with Uniform blocks.
1325
+ *
1326
+ * If you want to deal with the padding yourself you can access the array
1327
+ * buffer views directly. eg:
1328
+ *
1329
+ * someBlockInfo.someVec3Array.set([1, 2, 3, 0, 4, 5, 6, 0]);
1330
+ *
1331
+ * Any name that doesn't match will be ignored
1332
+ * @memberOf module:twgl/programs
1333
+ */
229
1334
  export function setBlockUniforms(uniformBlockInfo: UniformBlockInfo, values: {
230
1335
  [key: string]: any;
231
1336
  }): void;
1337
+ /**
1338
+ * Set uniforms and binds related textures.
1339
+ *
1340
+ * example:
1341
+ *
1342
+ * const programInfo = createProgramInfo(
1343
+ * gl, ["some-vs", "some-fs"]);
1344
+ *
1345
+ * const tex1 = gl.createTexture();
1346
+ * const tex2 = gl.createTexture();
1347
+ *
1348
+ * ... assume we setup the textures with data ...
1349
+ *
1350
+ * const uniforms = {
1351
+ * u_someSampler: tex1,
1352
+ * u_someOtherSampler: tex2,
1353
+ * u_someColor: [1,0,0,1],
1354
+ * u_somePosition: [0,1,1],
1355
+ * u_someMatrix: [
1356
+ * 1,0,0,0,
1357
+ * 0,1,0,0,
1358
+ * 0,0,1,0,
1359
+ * 0,0,0,0,
1360
+ * ],
1361
+ * };
1362
+ *
1363
+ * gl.useProgram(programInfo.program);
1364
+ *
1365
+ * This will automatically bind the textures AND set the
1366
+ * uniforms.
1367
+ *
1368
+ * twgl.setUniforms(programInfo, uniforms);
1369
+ *
1370
+ * For the example above it is equivalent to
1371
+ *
1372
+ * let texUnit = 0;
1373
+ * gl.activeTexture(gl.TEXTURE0 + texUnit);
1374
+ * gl.bindTexture(gl.TEXTURE_2D, tex1);
1375
+ * gl.uniform1i(u_someSamplerLocation, texUnit++);
1376
+ * gl.activeTexture(gl.TEXTURE0 + texUnit);
1377
+ * gl.bindTexture(gl.TEXTURE_2D, tex2);
1378
+ * gl.uniform1i(u_someSamplerLocation, texUnit++);
1379
+ * gl.uniform4fv(u_someColorLocation, [1, 0, 0, 1]);
1380
+ * gl.uniform3fv(u_somePositionLocation, [0, 1, 1]);
1381
+ * gl.uniformMatrix4fv(u_someMatrix, false, [
1382
+ * 1,0,0,0,
1383
+ * 0,1,0,0,
1384
+ * 0,0,1,0,
1385
+ * 0,0,0,0,
1386
+ * ]);
1387
+ *
1388
+ * Note it is perfectly reasonable to call `setUniforms` multiple times. For example
1389
+ *
1390
+ * const uniforms = {
1391
+ * u_someSampler: tex1,
1392
+ * u_someOtherSampler: tex2,
1393
+ * };
1394
+ *
1395
+ * const moreUniforms {
1396
+ * u_someColor: [1,0,0,1],
1397
+ * u_somePosition: [0,1,1],
1398
+ * u_someMatrix: [
1399
+ * 1,0,0,0,
1400
+ * 0,1,0,0,
1401
+ * 0,0,1,0,
1402
+ * 0,0,0,0,
1403
+ * ],
1404
+ * };
1405
+ *
1406
+ * twgl.setUniforms(programInfo, uniforms);
1407
+ * twgl.setUniforms(programInfo, moreUniforms);
1408
+ *
1409
+ * You can also add WebGLSamplers to uniform samplers as in
1410
+ *
1411
+ * const uniforms = {
1412
+ * u_someSampler: {
1413
+ * texture: someWebGLTexture,
1414
+ * sampler: someWebGLSampler,
1415
+ * },
1416
+ * };
1417
+ *
1418
+ * In which case both the sampler and texture will be bound to the
1419
+ * same unit.
1420
+ *
1421
+ * @param {(ProgramInfo|Object.<string, function>)} setters a `ProgramInfo` as returned from `createProgramInfo` or the setters returned from
1422
+ * `createUniformSetters`.
1423
+ * @param {Object.<string, ?>} values an object with values for the
1424
+ * uniforms.
1425
+ * You can pass multiple objects by putting them in an array or by calling with more arguments.For example
1426
+ *
1427
+ * const sharedUniforms = {
1428
+ * u_fogNear: 10,
1429
+ * u_projection: ...
1430
+ * ...
1431
+ * };
1432
+ *
1433
+ * const localUniforms = {
1434
+ * u_world: ...
1435
+ * u_diffuseColor: ...
1436
+ * };
1437
+ *
1438
+ * twgl.setUniforms(programInfo, sharedUniforms, localUniforms);
1439
+ *
1440
+ * // is the same as
1441
+ *
1442
+ * twgl.setUniforms(programInfo, [sharedUniforms, localUniforms]);
1443
+ *
1444
+ * // is the same as
1445
+ *
1446
+ * twgl.setUniforms(programInfo, sharedUniforms);
1447
+ * twgl.setUniforms(programInfo, localUniforms};
1448
+ *
1449
+ * You can also fill out structure and array values either via
1450
+ * shortcut. Example
1451
+ *
1452
+ * // -- in shader --
1453
+ * struct Light {
1454
+ * float intensity;
1455
+ * vec4 color;
1456
+ * float nearFar[2];
1457
+ * };
1458
+ * uniform Light lights[2];
1459
+ *
1460
+ * // in JavaScript
1461
+ *
1462
+ * twgl.setUniforms(programInfo, {
1463
+ * lights: [
1464
+ * { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
1465
+ * { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
1466
+ * ],
1467
+ * });
1468
+ *
1469
+ * or the more traditional way
1470
+ *
1471
+ * twgl.setUniforms(programInfo, {
1472
+ * "lights[0].intensity": 5.0,
1473
+ * "lights[0].color": [1, 0, 0, 1],
1474
+ * "lights[0].nearFar": [0.1, 10],
1475
+ * "lights[1].intensity": 2.0,
1476
+ * "lights[1].color": [0, 0, 1, 1],
1477
+ * "lights[1].nearFar": [0.2, 15],
1478
+ * });
1479
+ *
1480
+ * You can also specify partial paths
1481
+ *
1482
+ * twgl.setUniforms(programInfo, {
1483
+ * 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
1484
+ * });
1485
+ *
1486
+ * But you can not specify leaf array indices
1487
+ *
1488
+ * twgl.setUniforms(programInfo, {
1489
+ * 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
1490
+ * 'lights[1].nearFar': [0.2, 15], // GOOD
1491
+ * });
1492
+ *
1493
+ * @memberOf module:twgl/programs
1494
+ */
232
1495
  export function setUniforms(setters: ProgramInfo | {
233
1496
  [key: string]: (...params: any[]) => any;
234
1497
  }, values: {
235
1498
  [key: string]: any;
236
1499
  }): void;
1500
+ /**
1501
+ * Sets attributes and buffers including the `ELEMENT_ARRAY_BUFFER` if appropriate
1502
+ *
1503
+ * Example:
1504
+ *
1505
+ * const programInfo = createProgramInfo(
1506
+ * gl, ["some-vs", "some-fs");
1507
+ *
1508
+ * const arrays = {
1509
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
1510
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
1511
+ * };
1512
+ *
1513
+ * const bufferInfo = createBufferInfoFromArrays(gl, arrays);
1514
+ *
1515
+ * gl.useProgram(programInfo.program);
1516
+ *
1517
+ * This will automatically bind the buffers AND set the
1518
+ * attributes.
1519
+ *
1520
+ * setBuffersAndAttributes(gl, programInfo, bufferInfo);
1521
+ *
1522
+ * For the example above it is equivalent to
1523
+ *
1524
+ * gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
1525
+ * gl.enableVertexAttribArray(a_positionLocation);
1526
+ * gl.vertexAttribPointer(a_positionLocation, 3, gl.FLOAT, false, 0, 0);
1527
+ * gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer);
1528
+ * gl.enableVertexAttribArray(a_texcoordLocation);
1529
+ * gl.vertexAttribPointer(a_texcoordLocation, 4, gl.FLOAT, false, 0, 0);
1530
+ *
1531
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext.
1532
+ * @param {(ProgramInfo|Object.<string, function>)} setters A `ProgramInfo` as returned from {@link createProgramInfo} or Attribute setters as returned from {@link createAttributeSetters}
1533
+ * @param {(BufferInfo|VertexArrayInfo)} buffers a `BufferInfo` as returned from {@link createBufferInfoFromArrays}.
1534
+ * or a `VertexArrayInfo` as returned from {@link createVertexArrayInfo}
1535
+ * @memberOf module:twgl/programs
1536
+ */
237
1537
  export function setBuffersAndAttributes(gl: WebGLRenderingContext, setters: ProgramInfo | {
238
1538
  [key: string]: (...params: any[]) => any;
239
1539
  }, buffers: BufferInfo | VertexArrayInfo): void;
1540
+ /**
1541
+ * Sets a texture from an array or typed array. If the width or height is not provided will attempt to
1542
+ * guess the size. See {@link TextureOptions}.
1543
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
1544
+ * @param {WebGLTexture} tex the WebGLTexture to set parameters for
1545
+ * @param {(number[]|ArrayBufferView)} src An array or typed arry with texture data.
1546
+ * @param {TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
1547
+ * This is often the same options you passed in when you created the texture.
1548
+ * @memberOf module:twgl/textures
1549
+ */
240
1550
  export function setTextureFromArray(gl: WebGLRenderingContext, tex: WebGLTexture, src: number[] | ArrayBufferView, options?: TextureOptions): void;
1551
+ /**
1552
+ * Creates a texture based on the options passed in.
1553
+ *
1554
+ * Note: may reset UNPACK_ALIGNMENT, UNPACK_ROW_LENGTH, UNPACK_IMAGE_HEIGHT, UNPACK_SKIP_IMAGES
1555
+ * UNPACK_SKIP_PIXELS, and UNPACK_SKIP_ROWS
1556
+ *
1557
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
1558
+ * @param {TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
1559
+ * @param {TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
1560
+ * @return {WebGLTexture} the created texture.
1561
+ * @memberOf module:twgl/textures
1562
+ */
241
1563
  export function createTexture(gl: WebGLRenderingContext, options?: TextureOptions, callback?: TextureReadyCallback): WebGLTexture;
1564
+ /**
1565
+ * Resizes a texture based on the options passed in.
1566
+ *
1567
+ * Note: This is not a generic resize anything function.
1568
+ * It's mostly used by {@link resizeFramebufferInfo}
1569
+ * It will use `options.src` if it exists to try to determine a `type`
1570
+ * otherwise it will assume `gl.UNSIGNED_BYTE`. No data is provided
1571
+ * for the texture. Texture parameters will be set accordingly
1572
+ *
1573
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
1574
+ * @param {WebGLTexture} tex the texture to resize
1575
+ * @param {TextureOptions} options A TextureOptions object with whatever parameters you want set.
1576
+ * @param {number} [width] the new width. If not passed in will use `options.width`
1577
+ * @param {number} [height] the new height. If not passed in will use `options.height`
1578
+ * @param {number} [depth] the new depth. If not passed in will use `options.depth`
1579
+ * @memberOf module:twgl/textures
1580
+ */
242
1581
  export function resizeTexture(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, width?: number, height?: number, depth?: number): void;
1582
+ /**
1583
+ * Creates a bunch of textures based on the passed in options.
1584
+ *
1585
+ * Example:
1586
+ *
1587
+ * const textures = twgl.createTextures(gl, {
1588
+ * // a power of 2 image
1589
+ * hftIcon: { src: "images/hft-icon-16.png", mag: gl.NEAREST },
1590
+ * // a non-power of 2 image
1591
+ * clover: { src: "images/clover.jpg" },
1592
+ * // From a canvas
1593
+ * fromCanvas: { src: ctx.canvas },
1594
+ * // A cubemap from 6 images
1595
+ * yokohama: {
1596
+ * target: gl.TEXTURE_CUBE_MAP,
1597
+ * src: [
1598
+ * 'images/yokohama/posx.jpg',
1599
+ * 'images/yokohama/negx.jpg',
1600
+ * 'images/yokohama/posy.jpg',
1601
+ * 'images/yokohama/negy.jpg',
1602
+ * 'images/yokohama/posz.jpg',
1603
+ * 'images/yokohama/negz.jpg',
1604
+ * ],
1605
+ * },
1606
+ * // A cubemap from 1 image (can be 1x6, 2x3, 3x2, 6x1)
1607
+ * goldengate: {
1608
+ * target: gl.TEXTURE_CUBE_MAP,
1609
+ * src: 'images/goldengate.jpg',
1610
+ * },
1611
+ * // A 2x2 pixel texture from a JavaScript array
1612
+ * checker: {
1613
+ * mag: gl.NEAREST,
1614
+ * min: gl.LINEAR,
1615
+ * src: [
1616
+ * 255,255,255,255,
1617
+ * 192,192,192,255,
1618
+ * 192,192,192,255,
1619
+ * 255,255,255,255,
1620
+ * ],
1621
+ * },
1622
+ * // a 1x2 pixel texture from a typed array.
1623
+ * stripe: {
1624
+ * mag: gl.NEAREST,
1625
+ * min: gl.LINEAR,
1626
+ * format: gl.LUMINANCE,
1627
+ * src: new Uint8Array([
1628
+ * 255,
1629
+ * 128,
1630
+ * 255,
1631
+ * 128,
1632
+ * 255,
1633
+ * 128,
1634
+ * 255,
1635
+ * 128,
1636
+ * ]),
1637
+ * width: 1,
1638
+ * },
1639
+ * });
1640
+ *
1641
+ * Now
1642
+ *
1643
+ * * `textures.hftIcon` will be a 2d texture
1644
+ * * `textures.clover` will be a 2d texture
1645
+ * * `textures.fromCanvas` will be a 2d texture
1646
+ * * `textures.yohohama` will be a cubemap texture
1647
+ * * `textures.goldengate` will be a cubemap texture
1648
+ * * `textures.checker` will be a 2d texture
1649
+ * * `textures.stripe` will be a 2d texture
1650
+ *
1651
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
1652
+ * @param {Object.<string,TextureOptions>} options A object of TextureOptions one per texture.
1653
+ * @param {TexturesReadyCallback} [callback] A callback called when all textures have been downloaded.
1654
+ * @return {Object.<string,WebGLTexture>} the created textures by name
1655
+ * @memberOf module:twgl/textures
1656
+ */
243
1657
  export function createTextures(gl: WebGLRenderingContext, options: {
244
1658
  [key: string]: TextureOptions;
245
1659
  }, callback?: TexturesReadyCallback): {
@@ -247,79 +1661,1120 @@ export function createTextures(gl: WebGLRenderingContext, options: {
247
1661
  };
248
1662
 
249
1663
 
1664
+ /**
1665
+ * Sets the default attrib prefix
1666
+ *
1667
+ * When writing shaders I prefer to name attributes with `a_`, uniforms with `u_` and varyings with `v_`
1668
+ * as it makes it clear where they came from. But, when building geometry I prefer using un-prefixed names.
1669
+ *
1670
+ * In other words I'll create arrays of geometry like this
1671
+ *
1672
+ * var arrays = {
1673
+ * position: ...
1674
+ * normal: ...
1675
+ * texcoord: ...
1676
+ * };
1677
+ *
1678
+ * But need those mapped to attributes and my attributes start with `a_`.
1679
+ *
1680
+ * @deprecated see {@link setDefaults}
1681
+ * @param {string} prefix prefix for attribs
1682
+ * @memberOf module:twgl/attributes
1683
+ */
250
1684
  export function setAttributePrefix(prefix: string): void;
1685
+ /**
1686
+ * Given typed array creates a WebGLBuffer and copies the typed array
1687
+ * into it.
1688
+ *
1689
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1690
+ * @param {ArrayBuffer|SharedArrayBuffer|ArrayBufferView|WebGLBuffer} typedArray the typed array. Note: If a WebGLBuffer is passed in it will just be returned. No action will be taken
1691
+ * @param {number} [type] the GL bind type for the buffer. Default = `gl.ARRAY_BUFFER`.
1692
+ * @param {number} [drawType] the GL draw type for the buffer. Default = 'gl.STATIC_DRAW`.
1693
+ * @return {WebGLBuffer} the created WebGLBuffer
1694
+ * @memberOf module:twgl/attributes
1695
+ */
251
1696
  export function createBufferFromTypedArray(gl: WebGLRenderingContext, typedArray: ArrayBuffer | SharedArrayBuffer | ArrayBufferView | WebGLBuffer, type?: number, drawType?: number): WebGLBuffer;
1697
+ /**
1698
+ * @typedef {(Int8ArrayConstructor|Uint8ArrayConstructor|Int16ArrayConstructor|Uint16ArrayConstructor|Int32ArrayConstructor|Uint32ArrayConstructor|Float32ArrayConstructor)} TypedArrayConstructor
1699
+ */
252
1700
  export type TypedArrayConstructor = Int8ArrayConstructor | Uint8ArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor;
1701
+ /**
1702
+ * Creates a set of attribute data and WebGLBuffers from set of arrays
1703
+ *
1704
+ * Given
1705
+ *
1706
+ * var arrays = {
1707
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
1708
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
1709
+ * normal: { numComponents: 3, data: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], },
1710
+ * color: { numComponents: 4, data: [255, 255, 255, 255, 255, 0, 0, 255, 0, 0, 255, 255], type: Uint8Array, },
1711
+ * indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
1712
+ * };
1713
+ *
1714
+ * returns something like
1715
+ *
1716
+ * var attribs = {
1717
+ * position: { numComponents: 3, type: gl.FLOAT, normalize: false, buffer: WebGLBuffer, },
1718
+ * texcoord: { numComponents: 2, type: gl.FLOAT, normalize: false, buffer: WebGLBuffer, },
1719
+ * normal: { numComponents: 3, type: gl.FLOAT, normalize: false, buffer: WebGLBuffer, },
1720
+ * color: { numComponents: 4, type: gl.UNSIGNED_BYTE, normalize: true, buffer: WebGLBuffer, },
1721
+ * };
1722
+ *
1723
+ * notes:
1724
+ *
1725
+ * * Arrays can take various forms
1726
+ *
1727
+ * Bare JavaScript Arrays
1728
+ *
1729
+ * var arrays = {
1730
+ * position: [-1, 1, 0],
1731
+ * normal: [0, 1, 0],
1732
+ * ...
1733
+ * }
1734
+ *
1735
+ * Bare TypedArrays
1736
+ *
1737
+ * var arrays = {
1738
+ * position: new Float32Array([-1, 1, 0]),
1739
+ * color: new Uint8Array([255, 128, 64, 255]),
1740
+ * ...
1741
+ * }
1742
+ *
1743
+ * * Will guess at `numComponents` if not specified based on name.
1744
+ *
1745
+ * If `coord` is in the name assumes `numComponents = 2`
1746
+ *
1747
+ * If `color` is in the name assumes `numComponents = 4`
1748
+ *
1749
+ * otherwise assumes `numComponents = 3`
1750
+ *
1751
+ * @param {WebGLRenderingContext} gl The webgl rendering context.
1752
+ * @param {Arrays} arrays The arrays
1753
+ * @param {BufferInfo} [srcBufferInfo] a BufferInfo to copy from
1754
+ * This lets you share buffers. Any arrays you supply will override
1755
+ * the buffers from srcBufferInfo.
1756
+ * @return {Object.<string, AttribInfo>} the attribs
1757
+ * @memberOf module:twgl/attributes
1758
+ */
253
1759
  export function createAttribsFromArrays(gl: WebGLRenderingContext, arrays: Arrays, srcBufferInfo?: BufferInfo): {
254
1760
  [key: string]: AttribInfo;
255
1761
  };
1762
+ /**
1763
+ * Sets the contents of a buffer attached to an attribInfo
1764
+ *
1765
+ * This is helper function to dynamically update a buffer.
1766
+ *
1767
+ * Let's say you make a bufferInfo
1768
+ *
1769
+ * var arrays = {
1770
+ * position: new Float32Array([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]),
1771
+ * texcoord: new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]),
1772
+ * normal: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]),
1773
+ * indices: new Uint16Array([0, 1, 2, 1, 2, 3]),
1774
+ * };
1775
+ * var bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
1776
+ *
1777
+ * And you want to dynamically update the positions. You could do this
1778
+ *
1779
+ * // assuming arrays.position has already been updated with new data.
1780
+ * twgl.setAttribInfoBufferFromArray(gl, bufferInfo.attribs.position, arrays.position);
1781
+ *
1782
+ * @param {WebGLRenderingContext} gl
1783
+ * @param {AttribInfo} attribInfo The attribInfo who's buffer contents to set. NOTE: If you have an attribute prefix
1784
+ * the name of the attribute will include the prefix.
1785
+ * @param {ArraySpec} array Note: it is arguably inefficient to pass in anything but a typed array because anything
1786
+ * else will have to be converted to a typed array before it can be used by WebGL. During init time that
1787
+ * inefficiency is usually not important but if you're updating data dynamically best to be efficient.
1788
+ * @param {number} [offset] an optional offset into the buffer. This is only an offset into the WebGL buffer
1789
+ * not the array. To pass in an offset into the array itself use a typed array and create an `ArrayBufferView`
1790
+ * for the portion of the array you want to use.
1791
+ *
1792
+ * var someArray = new Float32Array(1000); // an array with 1000 floats
1793
+ * var someSubArray = new Float32Array(someArray.buffer, offsetInBytes, sizeInUnits); // a view into someArray
1794
+ *
1795
+ * Now you can pass `someSubArray` into setAttribInfoBufferFromArray`
1796
+ * @memberOf module:twgl/attributes
1797
+ */
256
1798
  export function setAttribInfoBufferFromArray(gl: WebGLRenderingContext, attribInfo: AttribInfo, array: ArraySpec, offset?: number): void;
1799
+ /**
1800
+ * Creates a BufferInfo from an object of arrays.
1801
+ *
1802
+ * This can be passed to {@link setBuffersAndAttributes} and to
1803
+ * {@link module:twgl:drawBufferInfo}.
1804
+ *
1805
+ * Given an object like
1806
+ *
1807
+ * var arrays = {
1808
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
1809
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
1810
+ * normal: { numComponents: 3, data: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1], },
1811
+ * indices: { numComponents: 3, data: [0, 1, 2, 1, 2, 3], },
1812
+ * };
1813
+ *
1814
+ * Creates an BufferInfo like this
1815
+ *
1816
+ * bufferInfo = {
1817
+ * numElements: 4, // or whatever the number of elements is
1818
+ * indices: WebGLBuffer, // this property will not exist if there are no indices
1819
+ * attribs: {
1820
+ * position: { buffer: WebGLBuffer, numComponents: 3, },
1821
+ * normal: { buffer: WebGLBuffer, numComponents: 3, },
1822
+ * texcoord: { buffer: WebGLBuffer, numComponents: 2, },
1823
+ * },
1824
+ * };
1825
+ *
1826
+ * The properties of arrays can be JavaScript arrays in which case the number of components
1827
+ * will be guessed.
1828
+ *
1829
+ * var arrays = {
1830
+ * position: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0],
1831
+ * texcoord: [0, 0, 0, 1, 1, 0, 1, 1],
1832
+ * normal: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
1833
+ * indices: [0, 1, 2, 1, 2, 3],
1834
+ * };
1835
+ *
1836
+ * They can also be TypedArrays
1837
+ *
1838
+ * var arrays = {
1839
+ * position: new Float32Array([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]),
1840
+ * texcoord: new Float32Array([0, 0, 0, 1, 1, 0, 1, 1]),
1841
+ * normal: new Float32Array([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]),
1842
+ * indices: new Uint16Array([0, 1, 2, 1, 2, 3]),
1843
+ * };
1844
+ *
1845
+ * Or AugmentedTypedArrays
1846
+ *
1847
+ * var positions = createAugmentedTypedArray(3, 4);
1848
+ * var texcoords = createAugmentedTypedArray(2, 4);
1849
+ * var normals = createAugmentedTypedArray(3, 4);
1850
+ * var indices = createAugmentedTypedArray(3, 2, Uint16Array);
1851
+ *
1852
+ * positions.push([0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0]);
1853
+ * texcoords.push([0, 0, 0, 1, 1, 0, 1, 1]);
1854
+ * normals.push([0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1]);
1855
+ * indices.push([0, 1, 2, 1, 2, 3]);
1856
+ *
1857
+ * var arrays = {
1858
+ * position: positions,
1859
+ * texcoord: texcoords,
1860
+ * normal: normals,
1861
+ * indices: indices,
1862
+ * };
1863
+ *
1864
+ * For the last example it is equivalent to
1865
+ *
1866
+ * var bufferInfo = {
1867
+ * attribs: {
1868
+ * position: { numComponents: 3, buffer: gl.createBuffer(), },
1869
+ * texcoord: { numComponents: 2, buffer: gl.createBuffer(), },
1870
+ * normal: { numComponents: 3, buffer: gl.createBuffer(), },
1871
+ * },
1872
+ * indices: gl.createBuffer(),
1873
+ * numElements: 6,
1874
+ * };
1875
+ *
1876
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.position.buffer);
1877
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.position, gl.STATIC_DRAW);
1878
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.texcoord.buffer);
1879
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.texcoord, gl.STATIC_DRAW);
1880
+ * gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.attribs.normal.buffer);
1881
+ * gl.bufferData(gl.ARRAY_BUFFER, arrays.normal, gl.STATIC_DRAW);
1882
+ * gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferInfo.indices);
1883
+ * gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, arrays.indices, gl.STATIC_DRAW);
1884
+ *
1885
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1886
+ * @param {Arrays} arrays Your data
1887
+ * @param {BufferInfo} [srcBufferInfo] An existing
1888
+ * buffer info to start from. WebGLBuffers etc specified
1889
+ * in the srcBufferInfo will be used in a new BufferInfo
1890
+ * with any arrays specified overriding the ones in
1891
+ * srcBufferInfo.
1892
+ * @return {BufferInfo} A BufferInfo
1893
+ * @memberOf module:twgl/attributes
1894
+ */
257
1895
  export function createBufferInfoFromArrays(gl: WebGLRenderingContext, arrays: Arrays, srcBufferInfo?: BufferInfo): BufferInfo;
1896
+ /**
1897
+ * Creates a buffer from an array, typed array, or array spec
1898
+ *
1899
+ * Given something like this
1900
+ *
1901
+ * [1, 2, 3],
1902
+ *
1903
+ * or
1904
+ *
1905
+ * new Uint16Array([1,2,3]);
1906
+ *
1907
+ * or
1908
+ *
1909
+ * {
1910
+ * data: [1, 2, 3],
1911
+ * type: Uint8Array,
1912
+ * }
1913
+ *
1914
+ * returns a WebGLBuffer that contains the given data.
1915
+ *
1916
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext.
1917
+ * @param {ArraySpec} array an array, typed array, or array spec.
1918
+ * @param {string} arrayName name of array. Used to guess the type if type can not be derived otherwise.
1919
+ * @return {WebGLBuffer} a WebGLBuffer containing the data in array.
1920
+ * @memberOf module:twgl/attributes
1921
+ */
258
1922
  export function createBufferFromArray(gl: WebGLRenderingContext, array: ArraySpec, arrayName: string): WebGLBuffer;
1923
+ /**
1924
+ * Creates buffers from arrays or typed arrays
1925
+ *
1926
+ * Given something like this
1927
+ *
1928
+ * var arrays = {
1929
+ * positions: [1, 2, 3],
1930
+ * normals: [0, 0, 1],
1931
+ * }
1932
+ *
1933
+ * returns something like
1934
+ *
1935
+ * buffers = {
1936
+ * positions: WebGLBuffer,
1937
+ * normals: WebGLBuffer,
1938
+ * }
1939
+ *
1940
+ * If the buffer is named 'indices' it will be made an ELEMENT_ARRAY_BUFFER.
1941
+ *
1942
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext.
1943
+ * @param {Arrays} arrays
1944
+ * @return {Object<string, WebGLBuffer>} returns an object with one WebGLBuffer per array
1945
+ * @memberOf module:twgl/attributes
1946
+ */
259
1947
  export function createBuffersFromArrays(gl: WebGLRenderingContext, arrays: Arrays): {
260
1948
  [key: string]: WebGLBuffer;
261
1949
  };
262
1950
 
263
1951
 
1952
+ /**
1953
+ * Calls `gl.drawElements` or `gl.drawArrays`, whichever is appropriate
1954
+ *
1955
+ * normally you'd call `gl.drawElements` or `gl.drawArrays` yourself
1956
+ * but calling this means if you switch from indexed data to non-indexed
1957
+ * data you don't have to remember to update your draw call.
1958
+ *
1959
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1960
+ * @param {(BufferInfo|VertexArrayInfo)} bufferInfo A BufferInfo as returned from {@link createBufferInfoFromArrays} or
1961
+ * a VertexArrayInfo as returned from {@link createVertexArrayInfo}
1962
+ * @param {number} [type] eg (gl.TRIANGLES, gl.LINES, gl.POINTS, gl.TRIANGLE_STRIP, ...). Defaults to `gl.TRIANGLES`
1963
+ * @param {number} [count] An optional count. Defaults to bufferInfo.numElements
1964
+ * @param {number} [offset] An optional offset. Defaults to 0.
1965
+ * @param {number} [instanceCount] An optional instanceCount. if set then `drawArraysInstanced` or `drawElementsInstanced` will be called
1966
+ * @memberOf module:twgl/draw
1967
+ */
264
1968
  export function drawBufferInfo(gl: WebGLRenderingContext, bufferInfo: BufferInfo | VertexArrayInfo, type?: number, count?: number, offset?: number, instanceCount?: number): void;
1969
+ /**
1970
+ * Draws a list of objects
1971
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
1972
+ * @param {DrawObject[]} objectsToDraw an array of objects to draw.
1973
+ * @memberOf module:twgl/draw
1974
+ */
265
1975
  export function drawObjectList(gl: WebGLRenderingContext, objectsToDraw: DrawObject[]): void;
266
1976
 
267
1977
 
1978
+ /**
1979
+ * Creates a framebuffer and attachments.
1980
+ *
1981
+ * This returns a {@link FramebufferInfo} because it needs to return the attachments as well as the framebuffer.
1982
+ * It also leaves the framebuffer it just created as the currently bound `FRAMEBUFFER`.
1983
+ * Note: If this is WebGL2 or if you called {@link addExtensionsToContext} then it will set the drawBuffers
1984
+ * to `[COLOR_ATTACHMENT0, COLOR_ATTACHMENT1, ...]` for how ever many color attachments were created.
1985
+ *
1986
+ * The simplest usage
1987
+ *
1988
+ * // create an RGBA/UNSIGNED_BYTE texture and DEPTH_STENCIL renderbuffer
1989
+ * const fbi = twgl.createFramebufferInfo(gl);
1990
+ *
1991
+ * More complex usage
1992
+ *
1993
+ * // create an RGB565 renderbuffer and a STENCIL_INDEX8 renderbuffer
1994
+ * const attachments = [
1995
+ * { format: RGB565, mag: NEAREST },
1996
+ * { format: STENCIL_INDEX8 },
1997
+ * ]
1998
+ * const fbi = twgl.createFramebufferInfo(gl, attachments);
1999
+ *
2000
+ * Passing in a specific size
2001
+ *
2002
+ * const width = 256;
2003
+ * const height = 256;
2004
+ * const fbi = twgl.createFramebufferInfo(gl, attachments, width, height);
2005
+ *
2006
+ * **Note!!** It is up to you to check if the framebuffer is renderable by calling `gl.checkFramebufferStatus`.
2007
+ * [WebGL1 only guarantees 3 combinations of attachments work](https://www.khronos.org/registry/webgl/specs/latest/1.0/#6.6).
2008
+ *
2009
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2010
+ * @param {AttachmentOptions[]} [attachments] which attachments to create. If not provided the default is a framebuffer with an
2011
+ * `RGBA`, `UNSIGNED_BYTE` texture `COLOR_ATTACHMENT0` and a `DEPTH_STENCIL` renderbuffer `DEPTH_STENCIL_ATTACHMENT`.
2012
+ * @param {number} [width] the width for the attachments. Default = size of drawingBuffer
2013
+ * @param {number} [height] the height for the attachments. Default = size of drawingBuffer
2014
+ * @return {FramebufferInfo} the framebuffer and attachments.
2015
+ * @memberOf module:twgl/framebuffers
2016
+ */
268
2017
  export function createFramebufferInfo(gl: WebGLRenderingContext, attachments?: AttachmentOptions[], width?: number, height?: number): FramebufferInfo;
2018
+ /**
2019
+ * Resizes the attachments of a framebuffer.
2020
+ *
2021
+ * You need to pass in the same `attachments` as you passed in {@link createFramebufferInfo}
2022
+ * because TWGL has no idea the format/type of each attachment.
2023
+ *
2024
+ * The simplest usage
2025
+ *
2026
+ * // create an RGBA/UNSIGNED_BYTE texture and DEPTH_STENCIL renderbuffer
2027
+ * const fbi = twgl.createFramebufferInfo(gl);
2028
+ *
2029
+ * ...
2030
+ *
2031
+ * function render() {
2032
+ * if (twgl.resizeCanvasToDisplaySize(gl.canvas)) {
2033
+ * // resize the attachments
2034
+ * twgl.resizeFramebufferInfo(gl, fbi);
2035
+ * }
2036
+ *
2037
+ * More complex usage
2038
+ *
2039
+ * // create an RGB565 renderbuffer and a STENCIL_INDEX8 renderbuffer
2040
+ * const attachments = [
2041
+ * { format: RGB565, mag: NEAREST },
2042
+ * { format: STENCIL_INDEX8 },
2043
+ * ]
2044
+ * const fbi = twgl.createFramebufferInfo(gl, attachments);
2045
+ *
2046
+ * ...
2047
+ *
2048
+ * function render() {
2049
+ * if (twgl.resizeCanvasToDisplaySize(gl.canvas)) {
2050
+ * // resize the attachments to match
2051
+ * twgl.resizeFramebufferInfo(gl, fbi, attachments);
2052
+ * }
2053
+ *
2054
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2055
+ * @param {FramebufferInfo} framebufferInfo a framebufferInfo as returned from {@link createFramebufferInfo}.
2056
+ * @param {AttachmentOptions[]} [attachments] the same attachments options as passed to {@link createFramebufferInfo}.
2057
+ * @param {number} [width] the width for the attachments. Default = size of drawingBuffer
2058
+ * @param {number} [height] the height for the attachments. Default = size of drawingBuffer
2059
+ * @memberOf module:twgl/framebuffers
2060
+ */
269
2061
  export function resizeFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo: FramebufferInfo, attachments?: AttachmentOptions[], width?: number, height?: number): void;
2062
+ /**
2063
+ * Binds a framebuffer
2064
+ *
2065
+ * This function pretty much solely exists because I spent hours
2066
+ * trying to figure out why something I wrote wasn't working only
2067
+ * to realize I forget to set the viewport dimensions.
2068
+ * My hope is this function will fix that.
2069
+ *
2070
+ * It is effectively the same as
2071
+ *
2072
+ * gl.bindFramebuffer(gl.FRAMEBUFFER, someFramebufferInfo.framebuffer);
2073
+ * gl.viewport(0, 0, someFramebufferInfo.width, someFramebufferInfo.height);
2074
+ *
2075
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2076
+ * @param {FramebufferInfo|null} [framebufferInfo] a framebufferInfo as returned from {@link createFramebufferInfo}.
2077
+ * If falsy will bind the canvas.
2078
+ * @param {number} [target] The target. If not passed `gl.FRAMEBUFFER` will be used.
2079
+ * @memberOf module:twgl/framebuffers
2080
+ */
270
2081
  export function bindFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo?: FramebufferInfo | null, target?: number): void;
271
2082
 
272
2083
 
2084
+ /**
2085
+ * Program Callback
2086
+ * @callback ProgramCallback
2087
+ * @param {string} [err] error message, falsy if no error
2088
+ * @param {WebGLProgram|ProgramInfo} [result] the program or programInfo
2089
+ */
273
2090
  export type ProgramCallback = (err?: string, result?: WebGLProgram | ProgramInfo) => void;
2091
+ /**
2092
+ * Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
2093
+ * program.
2094
+ *
2095
+ * NOTE: There are 4 signatures for this function
2096
+ *
2097
+ * twgl.createProgram(gl, [vs, fs], options);
2098
+ * twgl.createProgram(gl, [vs, fs], opt_errFunc);
2099
+ * twgl.createProgram(gl, [vs, fs], opt_attribs, opt_errFunc);
2100
+ * twgl.createProgram(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
2101
+ *
2102
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2103
+ * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
2104
+ * @param {ProgramOptions|string[]|ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
2105
+ * @param {number[]} [opt_locations|ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
2106
+ * @param {ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2107
+ * on error. If you want something else pass an callback. It's passed an error message.
2108
+ * @return {WebGLProgram?} the created program or null if error of a callback was provided.
2109
+ * @memberOf module:twgl/programs
2110
+ */
274
2111
  export function createProgram(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
2112
+ /**
2113
+ * Same as createProgram but returns a promise
2114
+ *
2115
+ * NOTE: There are 4 signatures for this function
2116
+ *
2117
+ * twgl.createProgramAsync(gl, [vs, fs], options);
2118
+ * twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
2119
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
2120
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
2121
+ *
2122
+ * @function
2123
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2124
+ * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
2125
+ * @param {ProgramOptions|string[]|ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
2126
+ * @param {number[]} [opt_locations|ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
2127
+ * @param {ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2128
+ * on error. If you want something else pass an callback. It's passed an error message.
2129
+ * @return {Promise<WebGLProgram>} The created program
2130
+ * @memberOf module:twgl/programs
2131
+ */
275
2132
  export function createProgramAsync(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<WebGLProgram>;
2133
+ /**
2134
+ * Same as createProgramInfo but returns a promise
2135
+ * @function
2136
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
2137
+ * to use.
2138
+ * @param {string[]} shaderSources Array of sources for the
2139
+ * shaders or ids. The first is assumed to be the vertex shader,
2140
+ * the second the fragment shader.
2141
+ * @param {ProgramOptions|string[]|ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
2142
+ * @param {number[]} [opt_locations|ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
2143
+ * @param {ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2144
+ * on error. If you want something else pass an callback. It's passed an error message.
2145
+ * @return {Promise<ProgramInfo>} The created ProgramInfo
2146
+ * @memberOf module:twgl/programs
2147
+ */
276
2148
  export function createProgramInfoAsync(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<ProgramInfo>;
2149
+ /**
2150
+ * Creates a program from 2 script tags.
2151
+ *
2152
+ * NOTE: There are 4 signatures for this function
2153
+ *
2154
+ * twgl.createProgramFromScripts(gl, [vs, fs], opt_options);
2155
+ * twgl.createProgramFromScripts(gl, [vs, fs], opt_errFunc);
2156
+ * twgl.createProgramFromScripts(gl, [vs, fs], opt_attribs, opt_errFunc);
2157
+ * twgl.createProgramFromScripts(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
2158
+ *
2159
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
2160
+ * to use.
2161
+ * @param {string[]} shaderScriptIds Array of ids of the script
2162
+ * tags for the shaders. The first is assumed to be the
2163
+ * vertex shader, the second the fragment shader.
2164
+ * @param {ProgramOptions|string[]|ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
2165
+ * @param {number[]} [opt_locations|ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
2166
+ * @param {ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2167
+ * on error. If you want something else pass an callback. It's passed an error message.
2168
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
2169
+ * @memberOf module:twgl/programs
2170
+ */
277
2171
  export function createProgramFromScripts(gl: WebGLRenderingContext, shaderScriptIds: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
2172
+ /**
2173
+ * Creates a program from 2 sources.
2174
+ *
2175
+ * NOTE: There are 4 signatures for this function
2176
+ *
2177
+ * twgl.createProgramFromSource(gl, [vs, fs], opt_options);
2178
+ * twgl.createProgramFromSource(gl, [vs, fs], opt_errFunc);
2179
+ * twgl.createProgramFromSource(gl, [vs, fs], opt_attribs, opt_errFunc);
2180
+ * twgl.createProgramFromSource(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
2181
+ *
2182
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
2183
+ * to use.
2184
+ * @param {string[]} shaderSources Array of sources for the
2185
+ * shaders. The first is assumed to be the vertex shader,
2186
+ * the second the fragment shader.
2187
+ * @param {ProgramOptions|string[]|ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
2188
+ * @param {number[]} [opt_locations|ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
2189
+ * @param {ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2190
+ * on error. If you want something else pass an callback. It's passed an error message.
2191
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
2192
+ * @memberOf module:twgl/programs
2193
+ */
278
2194
  export function createProgramFromSources(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
2195
+ /**
2196
+ * Creates setter functions for all uniforms of a shader
2197
+ * program.
2198
+ *
2199
+ * @see {@link setUniforms}
2200
+ *
2201
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2202
+ * @param {WebGLProgram} program the program to create setters for.
2203
+ * @returns {Object.<string, function>} an object with a setter by name for each uniform
2204
+ * @memberOf module:twgl/programs
2205
+ */
279
2206
  export function createUniformSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
280
2207
  [key: string]: (...params: any[]) => any;
281
2208
  };
2209
+ /**
2210
+ * Creates a UniformBlockSpec for the given program.
2211
+ *
2212
+ * A UniformBlockSpec represents the data needed to create and bind
2213
+ * UniformBlockObjects
2214
+ *
2215
+ * @param {WebGL2RenderingContext} gl A WebGL2 Rendering Context
2216
+ * @param {WebGLProgram} program A WebGLProgram for a successfully linked program
2217
+ * @return {UniformBlockSpec} The created UniformBlockSpec
2218
+ * @memberOf module:twgl/programs
2219
+ */
282
2220
  export function createUniformBlockSpecFromProgram(gl: WebGL2RenderingContext, program: WebGLProgram): UniformBlockSpec;
2221
+ /**
2222
+ * Creates a `UniformBlockInfo` for the specified block
2223
+ *
2224
+ * Note: **If the blockName matches no existing blocks a warning is printed to the console and a dummy
2225
+ * `UniformBlockInfo` is returned**. This is because when debugging GLSL
2226
+ * it is common to comment out large portions of a shader or for example set
2227
+ * the final output to a constant. When that happens blocks get optimized out.
2228
+ * If this function did not create dummy blocks your code would crash when debugging.
2229
+ *
2230
+ * @param {WebGL2RenderingContext} gl A WebGL2RenderingContext
2231
+ * @param {WebGLProgram} program A WebGLProgram
2232
+ * @param {UniformBlockSpec} uniformBlockSpec. A UniformBlockSpec as returned
2233
+ * from {@link createUniformBlockSpecFromProgram}.
2234
+ * @param {string} blockName The name of the block.
2235
+ * @return {UniformBlockInfo} The created UniformBlockInfo
2236
+ * @memberOf module:twgl/programs
2237
+ */
283
2238
  export function createUniformBlockInfoFromProgram(gl: WebGL2RenderingContext, program: WebGLProgram, blockName: string): UniformBlockInfo;
2239
+ /**
2240
+ * Creates a `UniformBlockInfo` for the specified block
2241
+ *
2242
+ * Note: **If the blockName matches no existing blocks a warning is printed to the console and a dummy
2243
+ * `UniformBlockInfo` is returned**. This is because when debugging GLSL
2244
+ * it is common to comment out large portions of a shader or for example set
2245
+ * the final output to a constant. When that happens blocks get optimized out.
2246
+ * If this function did not create dummy blocks your code would crash when debugging.
2247
+ *
2248
+ * @param {WebGL2RenderingContext} gl A WebGL2RenderingContext
2249
+ * @param {ProgramInfo} programInfo a `ProgramInfo`
2250
+ * as returned from {@link createProgramInfo}
2251
+ * @param {string} blockName The name of the block.
2252
+ * @return {UniformBlockInfo} The created UniformBlockInfo
2253
+ * @memberOf module:twgl/programs
2254
+ */
284
2255
  export function createUniformBlockInfo(gl: WebGL2RenderingContext, programInfo: ProgramInfo, blockName: string): UniformBlockInfo;
2256
+ /**
2257
+ * Binds a uniform block to the matching uniform block point.
2258
+ * Matches by blocks by name so blocks must have the same name not just the same
2259
+ * structure.
2260
+ *
2261
+ * If you have changed any values and you upload the values into the corresponding WebGLBuffer
2262
+ * call {@link setUniformBlock} instead.
2263
+ *
2264
+ * @param {WebGL2RenderingContext} gl A WebGL 2 rendering context.
2265
+ * @param {(ProgramInfo|UniformBlockSpec)} programInfo a `ProgramInfo`
2266
+ * as returned from {@link createProgramInfo} or or `UniformBlockSpec` as
2267
+ * returned from {@link createUniformBlockSpecFromProgram}.
2268
+ * @param {UniformBlockInfo} uniformBlockInfo a `UniformBlockInfo` as returned from
2269
+ * {@link createUniformBlockInfo}.
2270
+ * @return {bool} true if buffer was bound. If the programInfo has no block with the same block name
2271
+ * no buffer is bound.
2272
+ * @memberOf module:twgl/programs
2273
+ */
285
2274
  export function bindUniformBlock(gl: WebGL2RenderingContext, programInfo: ProgramInfo | UniformBlockSpec, uniformBlockInfo: UniformBlockInfo): boolean;
2275
+ /**
2276
+ * Uploads the current uniform values to the corresponding WebGLBuffer
2277
+ * and binds that buffer to the program's corresponding bind point for the uniform block object.
2278
+ *
2279
+ * If you haven't changed any values and you only need to bind the uniform block object
2280
+ * call {@link bindUniformBlock} instead.
2281
+ *
2282
+ * @param {WebGL2RenderingContext} gl A WebGL 2 rendering context.
2283
+ * @param {(ProgramInfo|UniformBlockSpec)} programInfo a `ProgramInfo`
2284
+ * as returned from {@link createProgramInfo} or or `UniformBlockSpec` as
2285
+ * returned from {@link createUniformBlockSpecFromProgram}.
2286
+ * @param {UniformBlockInfo} uniformBlockInfo a `UniformBlockInfo` as returned from
2287
+ * {@link createUniformBlockInfo}.
2288
+ * @memberOf module:twgl/programs
2289
+ */
286
2290
  export function setUniformBlock(gl: WebGL2RenderingContext, programInfo: ProgramInfo | UniformBlockSpec, uniformBlockInfo: UniformBlockInfo): void;
2291
+ /**
2292
+ * Sets values of a uniform block object
2293
+ *
2294
+ * @param {UniformBlockInfo} uniformBlockInfo A UniformBlockInfo as returned by {@link createUniformBlockInfo}.
2295
+ * @param {Object.<string, ?>} values A uniform name to value map where the value is correct for the given
2296
+ * type of uniform. So for example given a block like
2297
+ *
2298
+ * uniform SomeBlock {
2299
+ * float someFloat;
2300
+ * vec2 someVec2;
2301
+ * vec3 someVec3Array[2];
2302
+ * int someInt;
2303
+ * }
2304
+ *
2305
+ * You can set the values of the uniform block with
2306
+ *
2307
+ * twgl.setBlockUniforms(someBlockInfo, {
2308
+ * someFloat: 12.3,
2309
+ * someVec2: [1, 2],
2310
+ * someVec3Array: [1, 2, 3, 4, 5, 6],
2311
+ * someInt: 5,
2312
+ * }
2313
+ *
2314
+ * Arrays can be JavaScript arrays or typed arrays
2315
+ *
2316
+ * You can also fill out structure and array values either via
2317
+ * shortcut. Example
2318
+ *
2319
+ * // -- in shader --
2320
+ * struct Light {
2321
+ * float intensity;
2322
+ * vec4 color;
2323
+ * float nearFar[2];
2324
+ * };
2325
+ * uniform Lights {
2326
+ * Light lights[2];
2327
+ * };
2328
+ *
2329
+ * // in JavaScript
2330
+ *
2331
+ * twgl.setBlockUniforms(someBlockInfo, {
2332
+ * lights: [
2333
+ * { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
2334
+ * { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
2335
+ * ],
2336
+ * });
2337
+ *
2338
+ * or the more traditional way
2339
+ *
2340
+ * twgl.setBlockUniforms(someBlockInfo, {
2341
+ * "lights[0].intensity": 5.0,
2342
+ * "lights[0].color": [1, 0, 0, 1],
2343
+ * "lights[0].nearFar": [0.1, 10],
2344
+ * "lights[1].intensity": 2.0,
2345
+ * "lights[1].color": [0, 0, 1, 1],
2346
+ * "lights[1].nearFar": [0.2, 15],
2347
+ * });
2348
+ *
2349
+ * You can also specify partial paths
2350
+ *
2351
+ * twgl.setBlockUniforms(someBlockInfo, {
2352
+ * 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
2353
+ * });
2354
+ *
2355
+ * But you can not specify leaf array indices.
2356
+ *
2357
+ * twgl.setBlockUniforms(someBlockInfo, {
2358
+ * 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
2359
+ * 'lights[1].nearFar': [0.2, 15], // GOOD
2360
+ * });
2361
+ *
2362
+ * **IMPORTANT!**, packing in a UniformBlock is unintuitive.
2363
+ * For example the actual layout of `someVec3Array` above in memory
2364
+ * is `1, 2, 3, unused, 4, 5, 6, unused`. twgl takes in 6 values
2365
+ * as shown about and copies them, skipping the padding. This might
2366
+ * be confusing if you're already familiar with Uniform blocks.
2367
+ *
2368
+ * If you want to deal with the padding yourself you can access the array
2369
+ * buffer views directly. eg:
2370
+ *
2371
+ * someBlockInfo.someVec3Array.set([1, 2, 3, 0, 4, 5, 6, 0]);
2372
+ *
2373
+ * Any name that doesn't match will be ignored
2374
+ * @memberOf module:twgl/programs
2375
+ */
287
2376
  export function setBlockUniforms(uniformBlockInfo: UniformBlockInfo, values: {
288
2377
  [key: string]: any;
289
2378
  }): void;
2379
+ /**
2380
+ * Set uniforms and binds related textures.
2381
+ *
2382
+ * example:
2383
+ *
2384
+ * const programInfo = createProgramInfo(
2385
+ * gl, ["some-vs", "some-fs"]);
2386
+ *
2387
+ * const tex1 = gl.createTexture();
2388
+ * const tex2 = gl.createTexture();
2389
+ *
2390
+ * ... assume we setup the textures with data ...
2391
+ *
2392
+ * const uniforms = {
2393
+ * u_someSampler: tex1,
2394
+ * u_someOtherSampler: tex2,
2395
+ * u_someColor: [1,0,0,1],
2396
+ * u_somePosition: [0,1,1],
2397
+ * u_someMatrix: [
2398
+ * 1,0,0,0,
2399
+ * 0,1,0,0,
2400
+ * 0,0,1,0,
2401
+ * 0,0,0,0,
2402
+ * ],
2403
+ * };
2404
+ *
2405
+ * gl.useProgram(programInfo.program);
2406
+ *
2407
+ * This will automatically bind the textures AND set the
2408
+ * uniforms.
2409
+ *
2410
+ * twgl.setUniforms(programInfo, uniforms);
2411
+ *
2412
+ * For the example above it is equivalent to
2413
+ *
2414
+ * let texUnit = 0;
2415
+ * gl.activeTexture(gl.TEXTURE0 + texUnit);
2416
+ * gl.bindTexture(gl.TEXTURE_2D, tex1);
2417
+ * gl.uniform1i(u_someSamplerLocation, texUnit++);
2418
+ * gl.activeTexture(gl.TEXTURE0 + texUnit);
2419
+ * gl.bindTexture(gl.TEXTURE_2D, tex2);
2420
+ * gl.uniform1i(u_someSamplerLocation, texUnit++);
2421
+ * gl.uniform4fv(u_someColorLocation, [1, 0, 0, 1]);
2422
+ * gl.uniform3fv(u_somePositionLocation, [0, 1, 1]);
2423
+ * gl.uniformMatrix4fv(u_someMatrix, false, [
2424
+ * 1,0,0,0,
2425
+ * 0,1,0,0,
2426
+ * 0,0,1,0,
2427
+ * 0,0,0,0,
2428
+ * ]);
2429
+ *
2430
+ * Note it is perfectly reasonable to call `setUniforms` multiple times. For example
2431
+ *
2432
+ * const uniforms = {
2433
+ * u_someSampler: tex1,
2434
+ * u_someOtherSampler: tex2,
2435
+ * };
2436
+ *
2437
+ * const moreUniforms {
2438
+ * u_someColor: [1,0,0,1],
2439
+ * u_somePosition: [0,1,1],
2440
+ * u_someMatrix: [
2441
+ * 1,0,0,0,
2442
+ * 0,1,0,0,
2443
+ * 0,0,1,0,
2444
+ * 0,0,0,0,
2445
+ * ],
2446
+ * };
2447
+ *
2448
+ * twgl.setUniforms(programInfo, uniforms);
2449
+ * twgl.setUniforms(programInfo, moreUniforms);
2450
+ *
2451
+ * You can also add WebGLSamplers to uniform samplers as in
2452
+ *
2453
+ * const uniforms = {
2454
+ * u_someSampler: {
2455
+ * texture: someWebGLTexture,
2456
+ * sampler: someWebGLSampler,
2457
+ * },
2458
+ * };
2459
+ *
2460
+ * In which case both the sampler and texture will be bound to the
2461
+ * same unit.
2462
+ *
2463
+ * @param {(ProgramInfo|Object.<string, function>)} setters a `ProgramInfo` as returned from `createProgramInfo` or the setters returned from
2464
+ * `createUniformSetters`.
2465
+ * @param {Object.<string, ?>} values an object with values for the
2466
+ * uniforms.
2467
+ * You can pass multiple objects by putting them in an array or by calling with more arguments.For example
2468
+ *
2469
+ * const sharedUniforms = {
2470
+ * u_fogNear: 10,
2471
+ * u_projection: ...
2472
+ * ...
2473
+ * };
2474
+ *
2475
+ * const localUniforms = {
2476
+ * u_world: ...
2477
+ * u_diffuseColor: ...
2478
+ * };
2479
+ *
2480
+ * twgl.setUniforms(programInfo, sharedUniforms, localUniforms);
2481
+ *
2482
+ * // is the same as
2483
+ *
2484
+ * twgl.setUniforms(programInfo, [sharedUniforms, localUniforms]);
2485
+ *
2486
+ * // is the same as
2487
+ *
2488
+ * twgl.setUniforms(programInfo, sharedUniforms);
2489
+ * twgl.setUniforms(programInfo, localUniforms};
2490
+ *
2491
+ * You can also fill out structure and array values either via
2492
+ * shortcut. Example
2493
+ *
2494
+ * // -- in shader --
2495
+ * struct Light {
2496
+ * float intensity;
2497
+ * vec4 color;
2498
+ * float nearFar[2];
2499
+ * };
2500
+ * uniform Light lights[2];
2501
+ *
2502
+ * // in JavaScript
2503
+ *
2504
+ * twgl.setUniforms(programInfo, {
2505
+ * lights: [
2506
+ * { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
2507
+ * { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
2508
+ * ],
2509
+ * });
2510
+ *
2511
+ * or the more traditional way
2512
+ *
2513
+ * twgl.setUniforms(programInfo, {
2514
+ * "lights[0].intensity": 5.0,
2515
+ * "lights[0].color": [1, 0, 0, 1],
2516
+ * "lights[0].nearFar": [0.1, 10],
2517
+ * "lights[1].intensity": 2.0,
2518
+ * "lights[1].color": [0, 0, 1, 1],
2519
+ * "lights[1].nearFar": [0.2, 15],
2520
+ * });
2521
+ *
2522
+ * You can also specify partial paths
2523
+ *
2524
+ * twgl.setUniforms(programInfo, {
2525
+ * 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
2526
+ * });
2527
+ *
2528
+ * But you can not specify leaf array indices
2529
+ *
2530
+ * twgl.setUniforms(programInfo, {
2531
+ * 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
2532
+ * 'lights[1].nearFar': [0.2, 15], // GOOD
2533
+ * });
2534
+ *
2535
+ * @memberOf module:twgl/programs
2536
+ */
290
2537
  export function setUniforms(setters: ProgramInfo | {
291
2538
  [key: string]: (...params: any[]) => any;
292
2539
  }, values: {
293
2540
  [key: string]: any;
294
2541
  }): void;
2542
+ /**
2543
+ * Alias for `setUniforms`
2544
+ * @function
2545
+ * @param {(ProgramInfo|Object.<string, function>)} setters a `ProgramInfo` as returned from `createProgramInfo` or the setters returned from
2546
+ * `createUniformSetters`.
2547
+ * @param {Object.<string, ?>} values an object with values for the
2548
+ * @memberOf module:twgl/programs
2549
+ */
295
2550
  export function setUniformsAndBindTextures(setters: ProgramInfo | {
296
2551
  [key: string]: (...params: any[]) => any;
297
2552
  }, values: {
298
2553
  [key: string]: any;
299
2554
  }): void;
2555
+ /**
2556
+ * Creates setter functions for all attributes of a shader
2557
+ * program. You can pass this to {@link setBuffersAndAttributes} to set all your buffers and attributes.
2558
+ *
2559
+ * @see {@link setAttributes} for example
2560
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2561
+ * @param {WebGLProgram} program the program to create setters for.
2562
+ * @return {Object.<string, function>} an object with a setter for each attribute by name.
2563
+ * @memberOf module:twgl/programs
2564
+ */
300
2565
  export function createAttributeSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
301
2566
  [key: string]: (...params: any[]) => any;
302
2567
  };
2568
+ /**
2569
+ * Sets attributes and buffers including the `ELEMENT_ARRAY_BUFFER` if appropriate
2570
+ *
2571
+ * Example:
2572
+ *
2573
+ * const programInfo = createProgramInfo(
2574
+ * gl, ["some-vs", "some-fs");
2575
+ *
2576
+ * const arrays = {
2577
+ * position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
2578
+ * texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
2579
+ * };
2580
+ *
2581
+ * const bufferInfo = createBufferInfoFromArrays(gl, arrays);
2582
+ *
2583
+ * gl.useProgram(programInfo.program);
2584
+ *
2585
+ * This will automatically bind the buffers AND set the
2586
+ * attributes.
2587
+ *
2588
+ * setBuffersAndAttributes(gl, programInfo, bufferInfo);
2589
+ *
2590
+ * For the example above it is equivalent to
2591
+ *
2592
+ * gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
2593
+ * gl.enableVertexAttribArray(a_positionLocation);
2594
+ * gl.vertexAttribPointer(a_positionLocation, 3, gl.FLOAT, false, 0, 0);
2595
+ * gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer);
2596
+ * gl.enableVertexAttribArray(a_texcoordLocation);
2597
+ * gl.vertexAttribPointer(a_texcoordLocation, 4, gl.FLOAT, false, 0, 0);
2598
+ *
2599
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext.
2600
+ * @param {(ProgramInfo|Object.<string, function>)} setters A `ProgramInfo` as returned from {@link createProgramInfo} or Attribute setters as returned from {@link createAttributeSetters}
2601
+ * @param {(BufferInfo|VertexArrayInfo)} buffers a `BufferInfo` as returned from {@link createBufferInfoFromArrays}.
2602
+ * or a `VertexArrayInfo` as returned from {@link createVertexArrayInfo}
2603
+ * @memberOf module:twgl/programs
2604
+ */
303
2605
  export function setBuffersAndAttributes(gl: WebGLRenderingContext, setters: ProgramInfo | {
304
2606
  [key: string]: (...params: any[]) => any;
305
2607
  }, buffers: BufferInfo | VertexArrayInfo): void;
2608
+ /**
2609
+ * Creates a ProgramInfo from an existing program.
2610
+ *
2611
+ * A ProgramInfo contains
2612
+ *
2613
+ * programInfo = {
2614
+ * program: WebGLProgram,
2615
+ * uniformSetters: object of setters as returned from createUniformSetters,
2616
+ * attribSetters: object of setters as returned from createAttribSetters,
2617
+ * }
2618
+ *
2619
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
2620
+ * to use.
2621
+ * @param {WebGLProgram} program an existing WebGLProgram.
2622
+ * @return {ProgramInfo} The created ProgramInfo.
2623
+ * @memberOf module:twgl/programs
2624
+ */
306
2625
  export function createProgramInfoFromProgram(gl: WebGLRenderingContext, program: WebGLProgram): ProgramInfo;
2626
+ /**
2627
+ * Creates a ProgramInfo from 2 sources.
2628
+ *
2629
+ * A ProgramInfo contains
2630
+ *
2631
+ * programInfo = {
2632
+ * program: WebGLProgram,
2633
+ * uniformSetters: object of setters as returned from createUniformSetters,
2634
+ * attribSetters: object of setters as returned from createAttribSetters,
2635
+ * }
2636
+ *
2637
+ * NOTE: There are 4 signatures for this function
2638
+ *
2639
+ * twgl.createProgramInfo(gl, [vs, fs], options);
2640
+ * twgl.createProgramInfo(gl, [vs, fs], opt_errFunc);
2641
+ * twgl.createProgramInfo(gl, [vs, fs], opt_attribs, opt_errFunc);
2642
+ * twgl.createProgramInfo(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
2643
+ *
2644
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
2645
+ * to use.
2646
+ * @param {string[]} shaderSources Array of sources for the
2647
+ * shaders or ids. The first is assumed to be the vertex shader,
2648
+ * the second the fragment shader.
2649
+ * @param {ProgramOptions|string[]|ErrorCallback} [opt_attribs] Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
2650
+ * @param {number[]} [opt_locations|ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
2651
+ * @param {ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2652
+ * on error. If you want something else pass an callback. It's passed an error message.
2653
+ * @return {ProgramInfo?} The created ProgramInfo or null if it failed to link or compile
2654
+ * @memberOf module:twgl/programs
2655
+ */
307
2656
  export function createProgramInfo(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): ProgramInfo;
2657
+ /**
2658
+ * Creates multiple programs
2659
+ *
2660
+ * Note: the reason this function exists is because the fastest way to create multiple
2661
+ * programs in WebGL is to create and compile all shaders and link all programs and only
2662
+ * afterwards check if they succeeded. In that way, giving all your shaders
2663
+ *
2664
+ * @see {@link createProgram}
2665
+ *
2666
+ * Example:
2667
+ *
2668
+ * const programs = twgl.createPrograms(gl, {
2669
+ * lambert: [lambertVS, lambertFS],
2670
+ * phong: [phongVS, phoneFS],
2671
+ * particles: {
2672
+ * shaders: [particlesVS, particlesFS],
2673
+ * transformFeedbackVaryings: ['position', 'velocity'],
2674
+ * },
2675
+ * });
2676
+ *
2677
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2678
+ * @param {Object.<string, ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
2679
+ * @param {ProgramOptions} [programOptions] options to apply to all programs
2680
+ * @return {Object.<string, WebGLProgram>?} the created programInfos by name
2681
+ */
308
2682
  export function createPrograms(gl: WebGLRenderingContext, programSpecs: {
309
2683
  [key: string]: ProgramSpec;
310
2684
  }, programOptions?: ProgramOptions): {
311
2685
  [key: string]: WebGLProgram;
312
2686
  };
2687
+ /**
2688
+ * Creates multiple programInfos
2689
+ *
2690
+ * Note: the reason this function exists is because the fastest way to create multiple
2691
+ * programs in WebGL is to create and compile all shaders and link all programs and only
2692
+ * afterwards check if they succeeded. In that way, giving all your shaders
2693
+ *
2694
+ * @see {@link createProgramInfo}
2695
+ *
2696
+ * Examples:
2697
+ *
2698
+ * const programInfos = twgl.createProgramInfos(gl, {
2699
+ * lambert: [lambertVS, lambertFS],
2700
+ * phong: [phongVS, phoneFS],
2701
+ * particles: {
2702
+ * shaders: [particlesVS, particlesFS],
2703
+ * transformFeedbackVaryings: ['position', 'velocity'],
2704
+ * },
2705
+ * });
2706
+ *
2707
+ * or
2708
+ *
2709
+ * const {lambert, phong, particles} = twgl.createProgramInfos(gl, {
2710
+ * lambert: [lambertVS, lambertFS],
2711
+ * phong: [phongVS, phoneFS],
2712
+ * particles: {
2713
+ * shaders: [particlesVS, particlesFS],
2714
+ * transformFeedbackVaryings: ['position', 'velocity'],
2715
+ * },
2716
+ * });
2717
+ *
2718
+ *
2719
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2720
+ * @param {Object.<string, ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
2721
+ * @param {ProgramOptions} [programOptions] options to apply to all programs
2722
+ * @return {Object.<string, ProgramInfo>?} the created programInfos by name
2723
+ */
313
2724
  export function createProgramInfos(gl: WebGLRenderingContext, programSpecs: {
314
2725
  [key: string]: ProgramSpec;
315
2726
  }, programOptions?: ProgramOptions): {
316
2727
  [key: string]: ProgramInfo;
317
2728
  };
2729
+ /**
2730
+ * Creates multiple programs asynchronously
2731
+ *
2732
+ * @see {@link createProgramAsync}
2733
+ *
2734
+ * Example:
2735
+ *
2736
+ * const programs = await twgl.createProgramsAsync(gl, {
2737
+ * lambert: [lambertVS, lambertFS],
2738
+ * phong: [phongVS, phoneFS],
2739
+ * particles: {
2740
+ * shaders: [particlesVS, particlesFS],
2741
+ * transformFeedbackVaryings: ['position', 'velocity'],
2742
+ * },
2743
+ * });
2744
+ *
2745
+ * @function
2746
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2747
+ * @param {Object.<string, ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
2748
+ * @param {ProgramOptions} [programOptions] options to apply to all programs
2749
+ * @return {Object.<string, WebGLProgram>?} the created programInfos by name
2750
+ */
318
2751
  export function createProgramsAsync(gl: WebGLRenderingContext, programSpecs: {
319
2752
  [key: string]: ProgramSpec;
320
2753
  }, programOptions?: ProgramOptions): {
321
2754
  [key: string]: WebGLProgram;
322
2755
  };
2756
+ /**
2757
+ * Creates multiple programInfos asynchronously
2758
+ *
2759
+ * @see {@link createProgramInfoAsync}
2760
+ *
2761
+ * Example:
2762
+ *
2763
+ * const programInfos = await twgl.createProgramInfosAsync(gl, {
2764
+ * lambert: [lambertVS, lambertFS],
2765
+ * phong: [phongVS, phoneFS],
2766
+ * particles: {
2767
+ * shaders: [particlesVS, particlesFS],
2768
+ * transformFeedbackVaryings: ['position', 'velocity'],
2769
+ * },
2770
+ * });
2771
+ *
2772
+ * @function
2773
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2774
+ * @param {Object.<string, ProgramSpec>} programSpecs An object of ProgramSpecs, one per program.
2775
+ * @param {ProgramOptions} [programOptions] options to apply to all programs
2776
+ * @return {Promise<Object.<string, ProgramInfo>>} the created programInfos by name
2777
+ */
323
2778
  export function createProgramInfosAsync(gl: WebGLRenderingContext, programSpecs: {
324
2779
  [key: string]: ProgramSpec;
325
2780
  }, programOptions?: ProgramOptions): Promise<{
@@ -327,25 +2782,260 @@ export function createProgramInfosAsync(gl: WebGLRenderingContext, programSpecs:
327
2782
  }>;
328
2783
 
329
2784
 
2785
+ /**
2786
+ * Gets the number of bytes per element for a given internalFormat / type
2787
+ * @param {number} internalFormat The internalFormat parameter from texImage2D etc..
2788
+ * @param {number} type The type parameter for texImage2D etc..
2789
+ * @return {number} the number of bytes per element for the given internalFormat, type combo
2790
+ * @memberOf module:twgl/textures
2791
+ */
330
2792
  export function getBytesPerElementForInternalFormat(internalFormat: number, type: number): number;
2793
+ /**
2794
+ * Info related to a specific texture internalFormat as returned
2795
+ * from {@link getFormatAndTypeForInternalFormat}.
2796
+ *
2797
+ * @typedef {Object} TextureFormatInfo
2798
+ * @property {number} format Format to pass to texImage2D and related functions
2799
+ * @property {number} type Type to pass to texImage2D and related functions
2800
+ * @memberOf module:twgl/textures
2801
+ */
331
2802
  export type TextureFormatInfo = {
332
2803
  format: number;
333
2804
  type: number;
334
2805
  };
2806
+ /**
2807
+ * Gets the format and type for a given internalFormat
2808
+ *
2809
+ * @param {number} internalFormat The internal format
2810
+ * @return {TextureFormatInfo} the corresponding format and type,
2811
+ * @memberOf module:twgl/textures
2812
+ */
335
2813
  export function getFormatAndTypeForInternalFormat(internalFormat: number): TextureFormatInfo;
2814
+ /**
2815
+ * Gets whether or not we can generate mips for the given
2816
+ * internal format.
2817
+ *
2818
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2819
+ * @param {number} width The width parameter from texImage2D etc..
2820
+ * @param {number} height The height parameter from texImage2D etc..
2821
+ * @param {number} internalFormat The internalFormat parameter from texImage2D etc..
2822
+ * @return {boolean} true if we can generate mips
2823
+ * @memberOf module:twgl/textures
2824
+ */
336
2825
  export function canGenerateMipmap(gl: WebGLRenderingContext, width: number, height: number, internalFormat: number): boolean;
2826
+ /**
2827
+ * Gets whether or not we can generate mips for the given format
2828
+ * @param {number} internalFormat The internalFormat parameter from texImage2D etc..
2829
+ * @return {boolean} true if we can generate mips
2830
+ * @memberOf module:twgl/textures
2831
+ */
337
2832
  export function canFilter(internalFormat: number): boolean;
2833
+ /**
2834
+ * Gets the number of components for a given image format.
2835
+ * @param {number} format the format.
2836
+ * @return {number} the number of components for the format.
2837
+ * @memberOf module:twgl/textures
2838
+ */
338
2839
  export function getNumComponentsForFormat(format: number): number;
2840
+ /**
2841
+ * Sets the default texture color.
2842
+ *
2843
+ * The default texture color is used when loading textures from
2844
+ * urls. Because the URL will be loaded async we'd like to be
2845
+ * able to use the texture immediately. By putting a 1x1 pixel
2846
+ * color in the texture we can start using the texture before
2847
+ * the URL has loaded.
2848
+ *
2849
+ * @param {number[]} color Array of 4 values in the range 0 to 1
2850
+ * @deprecated see {@link setDefaults}
2851
+ * @memberOf module:twgl/textures
2852
+ */
339
2853
  export function setDefaultTextureColor(color: number[]): void;
2854
+ /**
2855
+ * Sets the texture parameters of a texture.
2856
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2857
+ * @param {WebGLTexture} tex the WebGLTexture to set parameters for
2858
+ * @param {TextureOptions} options A TextureOptions object with whatever parameters you want set.
2859
+ * This is often the same options you passed in when you created the texture.
2860
+ * @memberOf module:twgl/textures
2861
+ */
340
2862
  export function setTextureParameters(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions): void;
2863
+ /**
2864
+ * Sets the sampler parameters of a sampler.
2865
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2866
+ * @param {WebGLSampler} sampler the WebGLSampler to set parameters for
2867
+ * @param {TextureOptions} options A TextureOptions object with whatever parameters you want set.
2868
+ * @memberOf module:twgl/textures
2869
+ */
341
2870
  export function setSamplerParameters(gl: WebGLRenderingContext, sampler: WebGLSampler, options: TextureOptions): void;
2871
+ /**
2872
+ * Sets filtering or generates mips for texture based on width or height
2873
+ * If width or height is not passed in uses `options.width` and//or `options.height`
2874
+ *
2875
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2876
+ * @param {WebGLTexture} tex the WebGLTexture to set parameters for
2877
+ * @param {TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
2878
+ * This is often the same options you passed in when you created the texture.
2879
+ * @param {number} [width] width of texture
2880
+ * @param {number} [height] height of texture
2881
+ * @param {number} [internalFormat] The internalFormat parameter from texImage2D etc..
2882
+ * @memberOf module:twgl/textures
2883
+ */
342
2884
  export function setTextureFilteringForSize(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, width?: number, height?: number, internalFormat?: number): void;
2885
+ /**
2886
+ * Set a texture from the contents of an element. Will also set
2887
+ * texture filtering or generate mips based on the dimensions of the element
2888
+ * unless `options.auto === false`. If `target === gl.TEXTURE_CUBE_MAP` will
2889
+ * attempt to slice image into 1x6, 2x3, 3x2, or 6x1 images, one for each face.
2890
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2891
+ * @param {WebGLTexture} tex the WebGLTexture to set parameters for
2892
+ * @param {HTMLElement} element a canvas, img, or video element.
2893
+ * @param {TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
2894
+ * This is often the same options you passed in when you created the texture.
2895
+ * @memberOf module:twgl/textures
2896
+ * @kind function
2897
+ */
343
2898
  export function setTextureFromElement(gl: WebGLRenderingContext, tex: WebGLTexture, element: HTMLElement, options?: TextureOptions): void;
2899
+ /**
2900
+ * Loads a texture from an image from a Url as specified in `options.src`
2901
+ * If `options.color !== false` will set the texture to a 1x1 pixel color so that the texture is
2902
+ * immediately useable. It will be updated with the contents of the image once the image has finished
2903
+ * downloading. Filtering options will be set as appropriate for image unless `options.auto === false`.
2904
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2905
+ * @param {WebGLTexture} tex the WebGLTexture to set parameters for
2906
+ * @param {TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
2907
+ * @param {TextureReadyCallback} [callback] A function to be called when the image has finished loading. err will
2908
+ * be non null if there was an error.
2909
+ * @return {HTMLImageElement} the image being downloaded.
2910
+ * @memberOf module:twgl/textures
2911
+ */
344
2912
  export function loadTextureFromUrl(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, callback?: TextureReadyCallback): HTMLImageElement;
2913
+ /**
2914
+ * Sets a texture from an array or typed array. If the width or height is not provided will attempt to
2915
+ * guess the size. See {@link TextureOptions}.
2916
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2917
+ * @param {WebGLTexture} tex the WebGLTexture to set parameters for
2918
+ * @param {(number[]|ArrayBufferView)} src An array or typed arry with texture data.
2919
+ * @param {TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
2920
+ * This is often the same options you passed in when you created the texture.
2921
+ * @memberOf module:twgl/textures
2922
+ */
345
2923
  export function setTextureFromArray(gl: WebGLRenderingContext, tex: WebGLTexture, src: number[] | ArrayBufferView, options?: TextureOptions): void;
2924
+ /**
2925
+ * Sets a texture with no contents of a certain size. In other words calls `gl.texImage2D` with `null`.
2926
+ * You must set `options.width` and `options.height`.
2927
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2928
+ * @param {WebGLTexture} tex the WebGLTexture to set parameters for
2929
+ * @param {TextureOptions} options A TextureOptions object with whatever parameters you want set.
2930
+ * @memberOf module:twgl/textures
2931
+ */
346
2932
  export function setEmptyTexture(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions): void;
2933
+ /**
2934
+ * Creates a texture based on the options passed in.
2935
+ *
2936
+ * Note: may reset UNPACK_ALIGNMENT, UNPACK_ROW_LENGTH, UNPACK_IMAGE_HEIGHT, UNPACK_SKIP_IMAGES
2937
+ * UNPACK_SKIP_PIXELS, and UNPACK_SKIP_ROWS
2938
+ *
2939
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2940
+ * @param {TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
2941
+ * @param {TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
2942
+ * @return {WebGLTexture} the created texture.
2943
+ * @memberOf module:twgl/textures
2944
+ */
347
2945
  export function createTexture(gl: WebGLRenderingContext, options?: TextureOptions, callback?: TextureReadyCallback): WebGLTexture;
2946
+ /**
2947
+ * Resizes a texture based on the options passed in.
2948
+ *
2949
+ * Note: This is not a generic resize anything function.
2950
+ * It's mostly used by {@link resizeFramebufferInfo}
2951
+ * It will use `options.src` if it exists to try to determine a `type`
2952
+ * otherwise it will assume `gl.UNSIGNED_BYTE`. No data is provided
2953
+ * for the texture. Texture parameters will be set accordingly
2954
+ *
2955
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
2956
+ * @param {WebGLTexture} tex the texture to resize
2957
+ * @param {TextureOptions} options A TextureOptions object with whatever parameters you want set.
2958
+ * @param {number} [width] the new width. If not passed in will use `options.width`
2959
+ * @param {number} [height] the new height. If not passed in will use `options.height`
2960
+ * @param {number} [depth] the new depth. If not passed in will use `options.depth`
2961
+ * @memberOf module:twgl/textures
2962
+ */
348
2963
  export function resizeTexture(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, width?: number, height?: number, depth?: number): void;
2964
+ /**
2965
+ * Creates a bunch of textures based on the passed in options.
2966
+ *
2967
+ * Example:
2968
+ *
2969
+ * const textures = twgl.createTextures(gl, {
2970
+ * // a power of 2 image
2971
+ * hftIcon: { src: "images/hft-icon-16.png", mag: gl.NEAREST },
2972
+ * // a non-power of 2 image
2973
+ * clover: { src: "images/clover.jpg" },
2974
+ * // From a canvas
2975
+ * fromCanvas: { src: ctx.canvas },
2976
+ * // A cubemap from 6 images
2977
+ * yokohama: {
2978
+ * target: gl.TEXTURE_CUBE_MAP,
2979
+ * src: [
2980
+ * 'images/yokohama/posx.jpg',
2981
+ * 'images/yokohama/negx.jpg',
2982
+ * 'images/yokohama/posy.jpg',
2983
+ * 'images/yokohama/negy.jpg',
2984
+ * 'images/yokohama/posz.jpg',
2985
+ * 'images/yokohama/negz.jpg',
2986
+ * ],
2987
+ * },
2988
+ * // A cubemap from 1 image (can be 1x6, 2x3, 3x2, 6x1)
2989
+ * goldengate: {
2990
+ * target: gl.TEXTURE_CUBE_MAP,
2991
+ * src: 'images/goldengate.jpg',
2992
+ * },
2993
+ * // A 2x2 pixel texture from a JavaScript array
2994
+ * checker: {
2995
+ * mag: gl.NEAREST,
2996
+ * min: gl.LINEAR,
2997
+ * src: [
2998
+ * 255,255,255,255,
2999
+ * 192,192,192,255,
3000
+ * 192,192,192,255,
3001
+ * 255,255,255,255,
3002
+ * ],
3003
+ * },
3004
+ * // a 1x2 pixel texture from a typed array.
3005
+ * stripe: {
3006
+ * mag: gl.NEAREST,
3007
+ * min: gl.LINEAR,
3008
+ * format: gl.LUMINANCE,
3009
+ * src: new Uint8Array([
3010
+ * 255,
3011
+ * 128,
3012
+ * 255,
3013
+ * 128,
3014
+ * 255,
3015
+ * 128,
3016
+ * 255,
3017
+ * 128,
3018
+ * ]),
3019
+ * width: 1,
3020
+ * },
3021
+ * });
3022
+ *
3023
+ * Now
3024
+ *
3025
+ * * `textures.hftIcon` will be a 2d texture
3026
+ * * `textures.clover` will be a 2d texture
3027
+ * * `textures.fromCanvas` will be a 2d texture
3028
+ * * `textures.yohohama` will be a cubemap texture
3029
+ * * `textures.goldengate` will be a cubemap texture
3030
+ * * `textures.checker` will be a 2d texture
3031
+ * * `textures.stripe` will be a 2d texture
3032
+ *
3033
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
3034
+ * @param {Object.<string,TextureOptions>} options A object of TextureOptions one per texture.
3035
+ * @param {TexturesReadyCallback} [callback] A callback called when all textures have been downloaded.
3036
+ * @return {Object.<string,WebGLTexture>} the created textures by name
3037
+ * @memberOf module:twgl/textures
3038
+ */
349
3039
  export function createTextures(gl: WebGLRenderingContext, options: {
350
3040
  [key: string]: TextureOptions;
351
3041
  }, callback?: TexturesReadyCallback): {
@@ -353,17 +3043,99 @@ export function createTextures(gl: WebGLRenderingContext, options: {
353
3043
  };
354
3044
 
355
3045
 
3046
+ /**
3047
+ * Get the GL type for a typedArray
3048
+ * @param {ArrayBufferView} typedArray a typedArray
3049
+ * @return {number} the GL type for array. For example pass in an `Int8Array` and `gl.BYTE` will
3050
+ * be returned. Pass in a `Uint32Array` and `gl.UNSIGNED_INT` will be returned
3051
+ * @memberOf module:twgl/typedArray
3052
+ */
356
3053
  export function getGLTypeForTypedArray(typedArray: ArrayBufferView): number;
3054
+ /**
3055
+ * Get the GL type for a typedArray type
3056
+ * @param {ArrayBufferView} typedArrayType a typedArray constructor
3057
+ * @return {number} the GL type for type. For example pass in `Int8Array` and `gl.BYTE` will
3058
+ * be returned. Pass in `Uint32Array` and `gl.UNSIGNED_INT` will be returned
3059
+ * @memberOf module:twgl/typedArray
3060
+ */
357
3061
  export function getGLTypeForTypedArrayType(typedArrayType: ArrayBufferView): number;
3062
+ /**
3063
+ * Get the typed array constructor for a given GL type
3064
+ * @param {number} type the GL type. (eg: `gl.UNSIGNED_INT`)
3065
+ * @return {function} the constructor for a the corresponding typed array. (eg. `Uint32Array`).
3066
+ * @memberOf module:twgl/typedArray
3067
+ */
358
3068
  export function getTypedArrayTypeForGLType(type: number): (...params: any[]) => any;
359
3069
 
360
3070
 
3071
+ /**
3072
+ * Creates a VertexArrayInfo from a BufferInfo and one or more ProgramInfos
3073
+ *
3074
+ * This can be passed to {@link setBuffersAndAttributes} and to
3075
+ * {@link module:twgl:drawBufferInfo}.
3076
+ *
3077
+ * > **IMPORTANT:** Vertex Array Objects are **not** a direct analog for a BufferInfo. Vertex Array Objects
3078
+ * assign buffers to specific attributes at creation time. That means they can only be used with programs
3079
+ * who's attributes use the same attribute locations for the same purposes.
3080
+ *
3081
+ * > Bind your attribute locations by passing an array of attribute names to {@link createProgramInfo}
3082
+ * or use WebGL 2's GLSL ES 3's `layout(location = <num>)` to make sure locations match.
3083
+ *
3084
+ * also
3085
+ *
3086
+ * > **IMPORTANT:** After calling twgl.setBuffersAndAttribute with a BufferInfo that uses a Vertex Array Object
3087
+ * that Vertex Array Object will be bound. That means **ANY MANIPULATION OF ELEMENT_ARRAY_BUFFER or ATTRIBUTES**
3088
+ * will affect the Vertex Array Object state.
3089
+ *
3090
+ * > Call `gl.bindVertexArray(null)` to get back manipulating the global attributes and ELEMENT_ARRAY_BUFFER.
3091
+ *
3092
+ * @param {WebGLRenderingContext} gl A WebGLRenderingContext
3093
+ * @param {ProgramInfo|ProgramInfo[]} programInfo a programInfo or array of programInfos
3094
+ * @param {BufferInfo} bufferInfo BufferInfo as returned from createBufferInfoFromArrays etc...
3095
+ *
3096
+ * You need to make sure every attribute that will be used is bound. So for example assume shader 1
3097
+ * uses attributes A, B, C and shader 2 uses attributes A, B, D. If you only pass in the programInfo
3098
+ * for shader 1 then only attributes A, B, and C will have their attributes set because TWGL doesn't
3099
+ * now attribute D's location.
3100
+ *
3101
+ * So, you can pass in both shader 1 and shader 2's programInfo
3102
+ *
3103
+ * @return {VertexArrayInfo} The created VertexArrayInfo
3104
+ *
3105
+ * @memberOf module:twgl/vertexArrays
3106
+ */
361
3107
  export function createVertexArrayInfo(gl: WebGLRenderingContext, programInfo: ProgramInfo | ProgramInfo[], bufferInfo: BufferInfo): VertexArrayInfo;
3108
+ /**
3109
+ * Creates a vertex array object and then sets the attributes on it
3110
+ *
3111
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
3112
+ * @param {Object.<string, function>} setters Attribute setters as returned from createAttributeSetters
3113
+ * @param {Object.<string, AttribInfo>} attribs AttribInfos mapped by attribute name.
3114
+ * @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
3115
+ *
3116
+ * @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
3117
+ *
3118
+ * @memberOf module:twgl/vertexArrays
3119
+ */
362
3120
  export function createVAOAndSetAttributes(gl: WebGLRenderingContext, setters: {
363
3121
  [key: string]: (...params: any[]) => any;
364
3122
  }, attribs: {
365
3123
  [key: string]: AttribInfo;
366
3124
  }, indices?: WebGLBuffer): WebGLVertexArrayObject | null;
3125
+ /**
3126
+ * Creates a vertex array object and then sets the attributes
3127
+ * on it
3128
+ *
3129
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
3130
+ * to use.
3131
+ * @param {Object.<string, function>| ProgramInfo} programInfo as returned from createProgramInfo or Attribute setters as returned from createAttributeSetters
3132
+ * @param {BufferInfo} bufferInfo BufferInfo as returned from createBufferInfoFromArrays etc...
3133
+ * @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
3134
+ *
3135
+ * @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
3136
+ *
3137
+ * @memberOf module:twgl/vertexArrays
3138
+ */
367
3139
  export function createVAOFromBufferInfo(gl: WebGLRenderingContext, programInfo: {
368
3140
  [key: string]: (...params: any[]) => any;
369
3141
  } | ProgramInfo, bufferInfo: BufferInfo, indices?: WebGLBuffer): WebGLVertexArrayObject | null;