twgl.js 4.23.1 → 5.0.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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  TWGL: A Tiny WebGL helper Library<div id="pronounce" style="font-size: xx-small;">[rhymes with wiggle]</div>
2
2
  =====================================================
3
3
 
4
- [![Build Status](https://travis-ci.org/greggman/twgl.js.svg?branch=master)](https://travis-ci.org/greggman/twgl.js)
4
+ [![Build Status](https://github.com/greggman/twgl.js/actions/workflows/test.yml/badge.svg)](https://github.com/greggman/twgl.js/actions/workflows/test.yml)
5
5
 
6
6
  This library's sole purpose is to make using the WebGL API less verbose.
7
7
 
@@ -7,7 +7,6 @@ export type Defaults = {
7
7
  };
8
8
  export function setDefaults(newDefaults: Defaults): void;
9
9
  export function addExtensionsToContext(gl: WebGLRenderingContext): void;
10
- export function getWebGLContext(canvas: HTMLCanvasElement, opt_attribs?: WebGLContextAttributes): WebGLRenderingContext;
11
10
  export function createContext(canvas: HTMLCanvasElement): WebGLRenderingContext;
12
11
  export function getContext(canvas: HTMLCanvasElement, opt_attribs?: WebGLContextAttributes): WebGLRenderingContext;
13
12
  export function resizeCanvasToDisplaySize(canvas: HTMLCanvasElement, multiplier?: number): boolean;
@@ -256,9 +255,7 @@ export function resizeFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo
256
255
  export function bindFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo?: FramebufferInfo | null, target?: number): void;
257
256
 
258
257
 
259
- export function getBindPointForSamplerType(): void;
260
258
  export type ProgramCallback = (err?: string, result?: WebGLProgram | ProgramInfo) => void;
261
- export function prepShaderSource(shaderSource: string): any;
262
259
  export function createProgram(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
263
260
  export function createProgramAsync(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<WebGLProgram>;
264
261
  export function createProgramInfoAsync(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<ProgramInfo>;
@@ -288,11 +285,6 @@ export function setUniformsAndBindTextures(setters: ProgramInfo | {
288
285
  export function createAttributeSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
289
286
  [key: string]: (...params: any[]) => any;
290
287
  };
291
- export function setAttributes(setters: {
292
- [key: string]: (...params: any[]) => any;
293
- }, buffers: {
294
- [key: string]: AttribInfo;
295
- }): void;
296
288
  export function setBuffersAndAttributes(gl: WebGLRenderingContext, setters: ProgramInfo | {
297
289
  [key: string]: (...params: any[]) => any;
298
290
  }, buffers: BufferInfo | VertexArrayInfo): void;
@@ -314,10 +306,7 @@ export function setTextureParameters(gl: WebGLRenderingContext, tex: WebGLTextur
314
306
  export function setSamplerParameters(gl: WebGLRenderingContext, sampler: WebGLSampler, options: TextureOptions): void;
315
307
  export function setTextureFilteringForSize(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, width?: number, height?: number, internalFormat?: number): void;
316
308
  export function setTextureFromElement(gl: WebGLRenderingContext, tex: WebGLTexture, element: HTMLElement, options?: TextureOptions): void;
317
- export function setTextureTo1PixelColor(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions): void;
318
309
  export function loadTextureFromUrl(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, callback?: TextureReadyCallback): HTMLImageElement;
319
- export function loadCubemapFromUrls(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, callback?: CubemapReadyCallback): void;
320
- export function loadSlicesFromUrls(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, callback?: ThreeDReadyCallback): void;
321
310
  export function setTextureFromArray(gl: WebGLRenderingContext, tex: WebGLTexture, src: number[] | ArrayBufferView, options?: TextureOptions): void;
322
311
  export function setEmptyTexture(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions): void;
323
312
  export function createTexture(gl: WebGLRenderingContext, options?: TextureOptions, callback?: TextureReadyCallback): WebGLTexture;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 4.23.1 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 4.24.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
3
3
  * Available via the MIT license.
4
4
  * see: http://github.com/greggman/twgl.js for details
5
5
  */
@@ -349,8 +349,7 @@ function makeTypedArray(array, name) {
349
349
  * @property {boolean} [normalize] whether or not to normalize the data. Default = false
350
350
  * @property {number} [offset] offset into buffer in bytes. Default = 0
351
351
  * @property {number} [stride] the stride in bytes per element. Default = 0
352
- * @property {number} [divisor] the divisor in instances. Default = undefined. Note: undefined = don't call gl.vertexAttribDivisor
353
- * where as anything else = do call it with this value
352
+ * @property {number} [divisor] the divisor in instances. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
354
353
  * @property {WebGLBuffer} buffer the buffer that contains the data for this attribute
355
354
  * @property {number} [drawType] the draw type passed to gl.bufferData. Default = gl.STATIC_DRAW
356
355
  * @memberOf module:twgl
@@ -372,8 +371,7 @@ function makeTypedArray(array, name) {
372
371
  * @property {boolean} [normalize] normalize for `vertexAttribPointer`. Default is true if type is `Int8Array` or `Uint8Array` otherwise false.
373
372
  * @property {number} [stride] stride for `vertexAttribPointer`. Default = 0
374
373
  * @property {number} [offset] offset for `vertexAttribPointer`. Default = 0
375
- * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = undefined. Note: undefined = don't call gl.vertexAttribDivisor
376
- * where as anything else = do call it with this value
374
+ * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
377
375
  * @property {string} [attrib] name of attribute this array maps to. Defaults to same name as array prefixed by the default attribPrefix.
378
376
  * @property {string} [name] synonym for `attrib`.
379
377
  * @property {string} [attribName] synonym for `attrib`.
@@ -651,6 +649,11 @@ function getNumElementsFromNonIndexedArrays(arrays) {
651
649
 
652
650
  var array = arrays[key];
653
651
  var length = getArray(array).length;
652
+
653
+ if (length === undefined) {
654
+ return 1; // There's no arrays
655
+ }
656
+
654
657
  var numComponents = getNumComponents(array, key);
655
658
  var numElements = length / numComponents;
656
659
 
@@ -684,6 +687,11 @@ function getNumElementsFromAttributes(gl, attribs) {
684
687
  }
685
688
 
686
689
  var attrib = attribs[key];
690
+
691
+ if (!attrib.buffer) {
692
+ return 1; // There's no buffer
693
+ }
694
+
687
695
  gl.bindBuffer(ARRAY_BUFFER, attrib.buffer);
688
696
  var numBytes = gl.getBufferParameter(ARRAY_BUFFER, BUFFER_SIZE);
689
697
  gl.bindBuffer(ARRAY_BUFFER, null);
@@ -1243,6 +1251,12 @@ renderbufferFormats[STENCIL_INDEX8] = true;
1243
1251
  function isRenderbufferFormat(format) {
1244
1252
  return renderbufferFormats[format];
1245
1253
  }
1254
+
1255
+ var MAX_COLOR_ATTACHMENT_POINTS = 32; // even an 3090 only supports 8 but WebGL/OpenGL ES define constants for 32
1256
+
1257
+ function isColorAttachmentPoint(attachmentPoint) {
1258
+ return attachmentPoint >= COLOR_ATTACHMENT0 && attachmentPoint < COLOR_ATTACHMENT0 + MAX_COLOR_ATTACHMENT_POINTS;
1259
+ }
1246
1260
  /**
1247
1261
  * @typedef {Object} FramebufferInfo
1248
1262
  * @property {WebGLFramebuffer} framebuffer The WebGLFramebuffer for this framebufferInfo
@@ -1256,6 +1270,9 @@ function isRenderbufferFormat(format) {
1256
1270
  * Creates a framebuffer and attachments.
1257
1271
  *
1258
1272
  * This returns a {@link module:twgl.FramebufferInfo} because it needs to return the attachments as well as the framebuffer.
1273
+ * It also leaves the framebuffer it just created as the currently bound `FRAMEBUFFER`.
1274
+ * Note: If this is WebGL2 or if you called {@link module:twgl.addExtensionsToContext} then it will set the drawBuffers
1275
+ * to `[COLOR_ATTACHMENT0, COLOR_ATTACHMENT1, ...]` for how ever many color attachments were created.
1259
1276
  *
1260
1277
  * The simplest usage
1261
1278
  *
@@ -1297,21 +1314,25 @@ function createFramebufferInfo(gl, attachments, width, height) {
1297
1314
  width = width || gl.drawingBufferWidth;
1298
1315
  height = height || gl.drawingBufferHeight;
1299
1316
  attachments = attachments || defaultAttachments;
1300
- var colorAttachmentCount = 0;
1317
+ var usedColorAttachmentsPoints = [];
1301
1318
  var framebufferInfo = {
1302
1319
  framebuffer: fb,
1303
1320
  attachments: [],
1304
1321
  width: width,
1305
1322
  height: height
1306
1323
  };
1307
- attachments.forEach(function (attachmentOptions) {
1324
+ attachments.forEach(function (attachmentOptions, i) {
1308
1325
  var attachment = attachmentOptions.attachment;
1309
1326
  var samples = attachmentOptions.samples;
1310
1327
  var format = attachmentOptions.format;
1311
1328
  var attachmentPoint = attachmentOptions.attachmentPoint || getAttachmentPointForFormat(format, attachmentOptions.internalFormat);
1312
1329
 
1313
1330
  if (!attachmentPoint) {
1314
- attachmentPoint = COLOR_ATTACHMENT0 + colorAttachmentCount++;
1331
+ attachmentPoint = COLOR_ATTACHMENT0 + i;
1332
+ }
1333
+
1334
+ if (isColorAttachmentPoint(attachmentPoint)) {
1335
+ usedColorAttachmentsPoints.push(attachmentPoint);
1315
1336
  }
1316
1337
 
1317
1338
  if (!attachment) {
@@ -1355,6 +1376,11 @@ function createFramebufferInfo(gl, attachments, width, height) {
1355
1376
 
1356
1377
  framebufferInfo.attachments.push(attachment);
1357
1378
  });
1379
+
1380
+ if (gl.drawBuffers) {
1381
+ gl.drawBuffers(usedColorAttachmentsPoints);
1382
+ }
1383
+
1358
1384
  return framebufferInfo;
1359
1385
  }
1360
1386
  /**
@@ -2938,7 +2964,7 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
2938
2964
  *
2939
2965
  * example:
2940
2966
  *
2941
- * const arrays = twgl.primitives.createPlaneArrays(1);
2967
+ * const arrays = twgl.primitives.createPlaneVertices(1);
2942
2968
  * twgl.primitives.reorientVertices(arrays, m4.rotationX(Math.PI * 0.5));
2943
2969
  * const bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
2944
2970
  *
@@ -4814,6 +4840,7 @@ var TEXTURE_2D_ARRAY = 0x8C1A;
4814
4840
  var typeMap = {};
4815
4841
  /**
4816
4842
  * Returns the corresponding bind point for a given sampler type
4843
+ * @private
4817
4844
  */
4818
4845
 
4819
4846
  function getBindPointForSamplerType(gl, type) {
@@ -5321,8 +5348,8 @@ function floatAttribSetter(gl, index) {
5321
5348
  gl.enableVertexAttribArray(index);
5322
5349
  gl.vertexAttribPointer(index, b.numComponents || b.size, b.type || FLOAT, b.normalize || false, b.stride || 0, b.offset || 0);
5323
5350
 
5324
- if (b.divisor !== undefined) {
5325
- gl.vertexAttribDivisor(index, b.divisor);
5351
+ if (gl.vertexAttribDivisor) {
5352
+ gl.vertexAttribDivisor(index, b.divisor || 0);
5326
5353
  }
5327
5354
  }
5328
5355
  };
@@ -5343,8 +5370,8 @@ function intAttribSetter(gl, index) {
5343
5370
  gl.enableVertexAttribArray(index);
5344
5371
  gl.vertexAttribIPointer(index, b.numComponents || b.size, b.type || INT, b.stride || 0, b.offset || 0);
5345
5372
 
5346
- if (b.divisor !== undefined) {
5347
- gl.vertexAttribDivisor(index, b.divisor);
5373
+ if (gl.vertexAttribDivisor) {
5374
+ gl.vertexAttribDivisor(index, b.divisor || 0);
5348
5375
  }
5349
5376
  }
5350
5377
  };
@@ -5365,8 +5392,8 @@ function uintAttribSetter(gl, index) {
5365
5392
  gl.enableVertexAttribArray(index);
5366
5393
  gl.vertexAttribIPointer(index, b.numComponents || b.size, b.type || UNSIGNED_INT, b.stride || 0, b.offset || 0);
5367
5394
 
5368
- if (b.divisor !== undefined) {
5369
- gl.vertexAttribDivisor(index, b.divisor);
5395
+ if (gl.vertexAttribDivisor) {
5396
+ gl.vertexAttribDivisor(index, b.divisor || 0);
5370
5397
  }
5371
5398
  }
5372
5399
  };
@@ -5390,8 +5417,8 @@ function matAttribSetter(gl, index, typeInfo) {
5390
5417
  gl.enableVertexAttribArray(index + i);
5391
5418
  gl.vertexAttribPointer(index + i, size, type, normalize, stride, offset + rowOffset * i);
5392
5419
 
5393
- if (b.divisor !== undefined) {
5394
- gl.vertexAttribDivisor(index + i, b.divisor);
5420
+ if (gl.vertexAttribDivisor) {
5421
+ gl.vertexAttribDivisor(index + i, b.divisor || 0);
5395
5422
  }
5396
5423
  }
5397
5424
  };
@@ -5533,6 +5560,7 @@ var spaceRE = /^[ \t]*\n/;
5533
5560
  *
5534
5561
  * @param {string} shaderSource The source of the shader
5535
5562
  * @returns {{shaderSource: string, lineOffset: number}}
5563
+ * @private
5536
5564
  */
5537
5565
 
5538
5566
  function prepShaderSource(shaderSource) {
@@ -7062,6 +7090,7 @@ function createAttributeSetters(gl, program) {
7062
7090
  * @param {Object.<string, module:twgl.AttribInfo>} buffers AttribInfos mapped by attribute name.
7063
7091
  * @memberOf module:twgl/programs
7064
7092
  * @deprecated use {@link module:twgl.setBuffersAndAttributes}
7093
+ * @private
7065
7094
  */
7066
7095
 
7067
7096
 
@@ -8898,6 +8927,7 @@ function loadAndUseImage(obj, crossOrigin, callback) {
8898
8927
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
8899
8928
  * This is often the same options you passed in when you created the texture.
8900
8929
  * @memberOf module:twgl/textures
8930
+ * @private
8901
8931
  */
8902
8932
 
8903
8933
 
@@ -9012,6 +9042,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
9012
9042
  * @param {module:twgl.CubemapReadyCallback} [callback] A function to be called when all the images have finished loading. err will
9013
9043
  * be non null if there was an error.
9014
9044
  * @memberOf module:twgl/textures
9045
+ * @private
9015
9046
  */
9016
9047
 
9017
9048
 
@@ -9100,6 +9131,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
9100
9131
  * @param {module:twgl.ThreeDReadyCallback} [callback] A function to be called when all the images have finished loading. err will
9101
9132
  * be non null if there was an error.
9102
9133
  * @memberOf module:twgl/textures
9134
+ * @private
9103
9135
  */
9104
9136
 
9105
9137
 
@@ -10009,6 +10041,8 @@ function create3DContext(canvas, opt_attribs) {
10009
10041
  * @param {WebGLContextAttributes} [opt_attribs] optional webgl context creation attributes
10010
10042
  * @return {WebGLRenderingContext} The created context.
10011
10043
  * @memberOf module:twgl
10044
+ * @deprecated
10045
+ * @private
10012
10046
  */
10013
10047
 
10014
10048
 
@@ -1,4 +1,4 @@
1
- /* @license twgl.js 4.23.1 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
1
+ /* @license twgl.js 4.24.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
2
  Available via the MIT license.
3
3
  see: http://github.com/greggman/twgl.js for details */
4
4
  /*
@@ -2148,8 +2148,7 @@ function makeTypedArray(array, name) {
2148
2148
  * @property {boolean} [normalize] whether or not to normalize the data. Default = false
2149
2149
  * @property {number} [offset] offset into buffer in bytes. Default = 0
2150
2150
  * @property {number} [stride] the stride in bytes per element. Default = 0
2151
- * @property {number} [divisor] the divisor in instances. Default = undefined. Note: undefined = don't call gl.vertexAttribDivisor
2152
- * where as anything else = do call it with this value
2151
+ * @property {number} [divisor] the divisor in instances. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
2153
2152
  * @property {WebGLBuffer} buffer the buffer that contains the data for this attribute
2154
2153
  * @property {number} [drawType] the draw type passed to gl.bufferData. Default = gl.STATIC_DRAW
2155
2154
  * @memberOf module:twgl
@@ -2171,8 +2170,7 @@ function makeTypedArray(array, name) {
2171
2170
  * @property {boolean} [normalize] normalize for `vertexAttribPointer`. Default is true if type is `Int8Array` or `Uint8Array` otherwise false.
2172
2171
  * @property {number} [stride] stride for `vertexAttribPointer`. Default = 0
2173
2172
  * @property {number} [offset] offset for `vertexAttribPointer`. Default = 0
2174
- * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = undefined. Note: undefined = don't call gl.vertexAttribDivisor
2175
- * where as anything else = do call it with this value
2173
+ * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
2176
2174
  * @property {string} [attrib] name of attribute this array maps to. Defaults to same name as array prefixed by the default attribPrefix.
2177
2175
  * @property {string} [name] synonym for `attrib`.
2178
2176
  * @property {string} [attribName] synonym for `attrib`.
@@ -2431,6 +2429,9 @@ function getNumElementsFromNonIndexedArrays(arrays) {
2431
2429
  }
2432
2430
  const array = arrays[key];
2433
2431
  const length = getArray(array).length;
2432
+ if (length === undefined) {
2433
+ return 1; // There's no arrays
2434
+ }
2434
2435
  const numComponents = getNumComponents(array, key);
2435
2436
  const numElements = length / numComponents;
2436
2437
  if (length % numComponents > 0) {
@@ -2456,6 +2457,9 @@ function getNumElementsFromAttributes(gl, attribs) {
2456
2457
  key = Object.keys(attribs)[0];
2457
2458
  }
2458
2459
  const attrib = attribs[key];
2460
+ if (!attrib.buffer) {
2461
+ return 1; // There's no buffer
2462
+ }
2459
2463
  gl.bindBuffer(ARRAY_BUFFER, attrib.buffer);
2460
2464
  const numBytes = gl.getBufferParameter(ARRAY_BUFFER, BUFFER_SIZE);
2461
2465
  gl.bindBuffer(ARRAY_BUFFER, null);
@@ -6088,6 +6092,7 @@ function loadAndUseImage(obj, crossOrigin, callback) {
6088
6092
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6089
6093
  * This is often the same options you passed in when you created the texture.
6090
6094
  * @memberOf module:twgl/textures
6095
+ * @private
6091
6096
  */
6092
6097
  function setTextureTo1PixelColor(gl, tex, options) {
6093
6098
  options = options || defaults$1.textureOptions;
@@ -6197,6 +6202,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
6197
6202
  * @param {module:twgl.CubemapReadyCallback} [callback] A function to be called when all the images have finished loading. err will
6198
6203
  * be non null if there was an error.
6199
6204
  * @memberOf module:twgl/textures
6205
+ * @private
6200
6206
  */
6201
6207
  function loadCubemapFromUrls(gl, tex, options, callback) {
6202
6208
  callback = callback || noop;
@@ -6280,6 +6286,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
6280
6286
  * @param {module:twgl.ThreeDReadyCallback} [callback] A function to be called when all the images have finished loading. err will
6281
6287
  * be non null if there was an error.
6282
6288
  * @memberOf module:twgl/textures
6289
+ * @private
6283
6290
  */
6284
6291
  function loadSlicesFromUrls(gl, tex, options, callback) {
6285
6292
  callback = callback || noop;
@@ -6858,6 +6865,7 @@ const typeMap = {};
6858
6865
 
6859
6866
  /**
6860
6867
  * Returns the corresponding bind point for a given sampler type
6868
+ * @private
6861
6869
  */
6862
6870
  function getBindPointForSamplerType(gl, type) {
6863
6871
  return typeMap[type].bindPoint;
@@ -7131,8 +7139,8 @@ function floatAttribSetter(gl, index) {
7131
7139
  gl.enableVertexAttribArray(index);
7132
7140
  gl.vertexAttribPointer(
7133
7141
  index, b.numComponents || b.size, b.type || FLOAT$3, b.normalize || false, b.stride || 0, b.offset || 0);
7134
- if (b.divisor !== undefined) {
7135
- gl.vertexAttribDivisor(index, b.divisor);
7142
+ if (gl.vertexAttribDivisor) {
7143
+ gl.vertexAttribDivisor(index, b.divisor || 0);
7136
7144
  }
7137
7145
  }
7138
7146
  };
@@ -7152,8 +7160,8 @@ function intAttribSetter(gl, index) {
7152
7160
  gl.enableVertexAttribArray(index);
7153
7161
  gl.vertexAttribIPointer(
7154
7162
  index, b.numComponents || b.size, b.type || INT$3, b.stride || 0, b.offset || 0);
7155
- if (b.divisor !== undefined) {
7156
- gl.vertexAttribDivisor(index, b.divisor);
7163
+ if (gl.vertexAttribDivisor) {
7164
+ gl.vertexAttribDivisor(index, b.divisor || 0);
7157
7165
  }
7158
7166
  }
7159
7167
  };
@@ -7173,8 +7181,8 @@ function uintAttribSetter(gl, index) {
7173
7181
  gl.enableVertexAttribArray(index);
7174
7182
  gl.vertexAttribIPointer(
7175
7183
  index, b.numComponents || b.size, b.type || UNSIGNED_INT$3, b.stride || 0, b.offset || 0);
7176
- if (b.divisor !== undefined) {
7177
- gl.vertexAttribDivisor(index, b.divisor);
7184
+ if (gl.vertexAttribDivisor) {
7185
+ gl.vertexAttribDivisor(index, b.divisor || 0);
7178
7186
  }
7179
7187
  }
7180
7188
  };
@@ -7198,8 +7206,8 @@ function matAttribSetter(gl, index, typeInfo) {
7198
7206
  gl.enableVertexAttribArray(index + i);
7199
7207
  gl.vertexAttribPointer(
7200
7208
  index + i, size, type, normalize, stride, offset + rowOffset * i);
7201
- if (b.divisor !== undefined) {
7202
- gl.vertexAttribDivisor(index + i, b.divisor);
7209
+ if (gl.vertexAttribDivisor) {
7210
+ gl.vertexAttribDivisor(index + i, b.divisor || 0);
7203
7211
  }
7204
7212
  }
7205
7213
  };
@@ -7276,6 +7284,7 @@ const spaceRE = /^[ \t]*\n/;
7276
7284
  *
7277
7285
  * @param {string} shaderSource The source of the shader
7278
7286
  * @returns {{shaderSource: string, lineOffset: number}}
7287
+ * @private
7279
7288
  */
7280
7289
  function prepShaderSource(shaderSource) {
7281
7290
  let lineOffset = 0;
@@ -8649,6 +8658,7 @@ function createAttributeSetters(gl, program) {
8649
8658
  * @param {Object.<string, module:twgl.AttribInfo>} buffers AttribInfos mapped by attribute name.
8650
8659
  * @memberOf module:twgl/programs
8651
8660
  * @deprecated use {@link module:twgl.setBuffersAndAttributes}
8661
+ * @private
8652
8662
  */
8653
8663
  function setAttributes(setters, buffers) {
8654
8664
  for (const name in buffers) {
@@ -9134,6 +9144,12 @@ function isRenderbufferFormat(format) {
9134
9144
  return renderbufferFormats[format];
9135
9145
  }
9136
9146
 
9147
+ const MAX_COLOR_ATTACHMENT_POINTS = 32; // even an 3090 only supports 8 but WebGL/OpenGL ES define constants for 32
9148
+
9149
+ function isColorAttachmentPoint(attachmentPoint) {
9150
+ return attachmentPoint >= COLOR_ATTACHMENT0 && attachmentPoint < COLOR_ATTACHMENT0 + MAX_COLOR_ATTACHMENT_POINTS;
9151
+ }
9152
+
9137
9153
  /**
9138
9154
  * @typedef {Object} FramebufferInfo
9139
9155
  * @property {WebGLFramebuffer} framebuffer The WebGLFramebuffer for this framebufferInfo
@@ -9147,6 +9163,9 @@ function isRenderbufferFormat(format) {
9147
9163
  * Creates a framebuffer and attachments.
9148
9164
  *
9149
9165
  * This returns a {@link module:twgl.FramebufferInfo} because it needs to return the attachments as well as the framebuffer.
9166
+ * It also leaves the framebuffer it just created as the currently bound `FRAMEBUFFER`.
9167
+ * Note: If this is WebGL2 or if you called {@link module:twgl.addExtensionsToContext} then it will set the drawBuffers
9168
+ * to `[COLOR_ATTACHMENT0, COLOR_ATTACHMENT1, ...]` for how ever many color attachments were created.
9150
9169
  *
9151
9170
  * The simplest usage
9152
9171
  *
@@ -9186,20 +9205,24 @@ function createFramebufferInfo(gl, attachments, width, height) {
9186
9205
  width = width || gl.drawingBufferWidth;
9187
9206
  height = height || gl.drawingBufferHeight;
9188
9207
  attachments = attachments || defaultAttachments;
9189
- let colorAttachmentCount = 0;
9208
+ const usedColorAttachmentsPoints = [];
9190
9209
  const framebufferInfo = {
9191
9210
  framebuffer: fb,
9192
9211
  attachments: [],
9193
9212
  width: width,
9194
9213
  height: height,
9195
9214
  };
9196
- attachments.forEach(function(attachmentOptions) {
9215
+
9216
+ attachments.forEach(function(attachmentOptions, i) {
9197
9217
  let attachment = attachmentOptions.attachment;
9198
9218
  const samples = attachmentOptions.samples;
9199
9219
  const format = attachmentOptions.format;
9200
9220
  let attachmentPoint = attachmentOptions.attachmentPoint || getAttachmentPointForFormat(format, attachmentOptions.internalFormat);
9201
9221
  if (!attachmentPoint) {
9202
- attachmentPoint = COLOR_ATTACHMENT0 + colorAttachmentCount++;
9222
+ attachmentPoint = COLOR_ATTACHMENT0 + i;
9223
+ }
9224
+ if (isColorAttachmentPoint(attachmentPoint)) {
9225
+ usedColorAttachmentsPoints.push(attachmentPoint);
9203
9226
  }
9204
9227
  if (!attachment) {
9205
9228
  if (samples !== undefined || isRenderbufferFormat(format)) {
@@ -9247,6 +9270,9 @@ function createFramebufferInfo(gl, attachments, width, height) {
9247
9270
  }
9248
9271
  framebufferInfo.attachments.push(attachment);
9249
9272
  });
9273
+ if (gl.drawBuffers) {
9274
+ gl.drawBuffers(usedColorAttachmentsPoints);
9275
+ }
9250
9276
  return framebufferInfo;
9251
9277
  }
9252
9278
 
@@ -9776,6 +9802,8 @@ function create3DContext(canvas, opt_attribs) {
9776
9802
  * @param {WebGLContextAttributes} [opt_attribs] optional webgl context creation attributes
9777
9803
  * @return {WebGLRenderingContext} The created context.
9778
9804
  * @memberOf module:twgl
9805
+ * @deprecated
9806
+ * @private
9779
9807
  */
9780
9808
  function getWebGLContext(canvas, opt_attribs) {
9781
9809
  const gl = create3DContext(canvas, opt_attribs);
@@ -7,7 +7,6 @@ export type Defaults = {
7
7
  };
8
8
  export function setDefaults(newDefaults: Defaults): void;
9
9
  export function addExtensionsToContext(gl: WebGLRenderingContext): void;
10
- export function getWebGLContext(canvas: HTMLCanvasElement, opt_attribs?: WebGLContextAttributes): WebGLRenderingContext;
11
10
  export function createContext(canvas: HTMLCanvasElement): WebGLRenderingContext;
12
11
  export function getContext(canvas: HTMLCanvasElement, opt_attribs?: WebGLContextAttributes): WebGLRenderingContext;
13
12
  export function resizeCanvasToDisplaySize(canvas: HTMLCanvasElement, multiplier?: number): boolean;
@@ -256,9 +255,7 @@ export function resizeFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo
256
255
  export function bindFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo?: FramebufferInfo | null, target?: number): void;
257
256
 
258
257
 
259
- export function getBindPointForSamplerType(): void;
260
258
  export type ProgramCallback = (err?: string, result?: WebGLProgram | ProgramInfo) => void;
261
- export function prepShaderSource(shaderSource: string): any;
262
259
  export function createProgram(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
263
260
  export function createProgramAsync(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<WebGLProgram>;
264
261
  export function createProgramInfoAsync(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<ProgramInfo>;
@@ -288,11 +285,6 @@ export function setUniformsAndBindTextures(setters: ProgramInfo | {
288
285
  export function createAttributeSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
289
286
  [key: string]: (...params: any[]) => any;
290
287
  };
291
- export function setAttributes(setters: {
292
- [key: string]: (...params: any[]) => any;
293
- }, buffers: {
294
- [key: string]: AttribInfo;
295
- }): void;
296
288
  export function setBuffersAndAttributes(gl: WebGLRenderingContext, setters: ProgramInfo | {
297
289
  [key: string]: (...params: any[]) => any;
298
290
  }, buffers: BufferInfo | VertexArrayInfo): void;
@@ -314,10 +306,7 @@ export function setTextureParameters(gl: WebGLRenderingContext, tex: WebGLTextur
314
306
  export function setSamplerParameters(gl: WebGLRenderingContext, sampler: WebGLSampler, options: TextureOptions): void;
315
307
  export function setTextureFilteringForSize(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, width?: number, height?: number, internalFormat?: number): void;
316
308
  export function setTextureFromElement(gl: WebGLRenderingContext, tex: WebGLTexture, element: HTMLElement, options?: TextureOptions): void;
317
- export function setTextureTo1PixelColor(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions): void;
318
309
  export function loadTextureFromUrl(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, callback?: TextureReadyCallback): HTMLImageElement;
319
- export function loadCubemapFromUrls(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, callback?: CubemapReadyCallback): void;
320
- export function loadSlicesFromUrls(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, callback?: ThreeDReadyCallback): void;
321
310
  export function setTextureFromArray(gl: WebGLRenderingContext, tex: WebGLTexture, src: number[] | ArrayBufferView, options?: TextureOptions): void;
322
311
  export function setEmptyTexture(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions): void;
323
312
  export function createTexture(gl: WebGLRenderingContext, options?: TextureOptions, callback?: TextureReadyCallback): WebGLTexture;
package/dist/4.x/twgl.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 4.23.1 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 4.24.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
3
3
  * Available via the MIT license.
4
4
  * see: http://github.com/greggman/twgl.js for details
5
5
  */
@@ -349,8 +349,7 @@ function makeTypedArray(array, name) {
349
349
  * @property {boolean} [normalize] whether or not to normalize the data. Default = false
350
350
  * @property {number} [offset] offset into buffer in bytes. Default = 0
351
351
  * @property {number} [stride] the stride in bytes per element. Default = 0
352
- * @property {number} [divisor] the divisor in instances. Default = undefined. Note: undefined = don't call gl.vertexAttribDivisor
353
- * where as anything else = do call it with this value
352
+ * @property {number} [divisor] the divisor in instances. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
354
353
  * @property {WebGLBuffer} buffer the buffer that contains the data for this attribute
355
354
  * @property {number} [drawType] the draw type passed to gl.bufferData. Default = gl.STATIC_DRAW
356
355
  * @memberOf module:twgl
@@ -372,8 +371,7 @@ function makeTypedArray(array, name) {
372
371
  * @property {boolean} [normalize] normalize for `vertexAttribPointer`. Default is true if type is `Int8Array` or `Uint8Array` otherwise false.
373
372
  * @property {number} [stride] stride for `vertexAttribPointer`. Default = 0
374
373
  * @property {number} [offset] offset for `vertexAttribPointer`. Default = 0
375
- * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = undefined. Note: undefined = don't call gl.vertexAttribDivisor
376
- * where as anything else = do call it with this value
374
+ * @property {number} [divisor] divisor for `vertexAttribDivisor`. Default = 0. Requires WebGL2 or the ANGLE_instanced_arrays extension.
377
375
  * @property {string} [attrib] name of attribute this array maps to. Defaults to same name as array prefixed by the default attribPrefix.
378
376
  * @property {string} [name] synonym for `attrib`.
379
377
  * @property {string} [attribName] synonym for `attrib`.
@@ -651,6 +649,11 @@ function getNumElementsFromNonIndexedArrays(arrays) {
651
649
 
652
650
  var array = arrays[key];
653
651
  var length = getArray(array).length;
652
+
653
+ if (length === undefined) {
654
+ return 1; // There's no arrays
655
+ }
656
+
654
657
  var numComponents = getNumComponents(array, key);
655
658
  var numElements = length / numComponents;
656
659
 
@@ -684,6 +687,11 @@ function getNumElementsFromAttributes(gl, attribs) {
684
687
  }
685
688
 
686
689
  var attrib = attribs[key];
690
+
691
+ if (!attrib.buffer) {
692
+ return 1; // There's no buffer
693
+ }
694
+
687
695
  gl.bindBuffer(ARRAY_BUFFER, attrib.buffer);
688
696
  var numBytes = gl.getBufferParameter(ARRAY_BUFFER, BUFFER_SIZE);
689
697
  gl.bindBuffer(ARRAY_BUFFER, null);
@@ -1243,6 +1251,12 @@ renderbufferFormats[STENCIL_INDEX8] = true;
1243
1251
  function isRenderbufferFormat(format) {
1244
1252
  return renderbufferFormats[format];
1245
1253
  }
1254
+
1255
+ var MAX_COLOR_ATTACHMENT_POINTS = 32; // even an 3090 only supports 8 but WebGL/OpenGL ES define constants for 32
1256
+
1257
+ function isColorAttachmentPoint(attachmentPoint) {
1258
+ return attachmentPoint >= COLOR_ATTACHMENT0 && attachmentPoint < COLOR_ATTACHMENT0 + MAX_COLOR_ATTACHMENT_POINTS;
1259
+ }
1246
1260
  /**
1247
1261
  * @typedef {Object} FramebufferInfo
1248
1262
  * @property {WebGLFramebuffer} framebuffer The WebGLFramebuffer for this framebufferInfo
@@ -1256,6 +1270,9 @@ function isRenderbufferFormat(format) {
1256
1270
  * Creates a framebuffer and attachments.
1257
1271
  *
1258
1272
  * This returns a {@link module:twgl.FramebufferInfo} because it needs to return the attachments as well as the framebuffer.
1273
+ * It also leaves the framebuffer it just created as the currently bound `FRAMEBUFFER`.
1274
+ * Note: If this is WebGL2 or if you called {@link module:twgl.addExtensionsToContext} then it will set the drawBuffers
1275
+ * to `[COLOR_ATTACHMENT0, COLOR_ATTACHMENT1, ...]` for how ever many color attachments were created.
1259
1276
  *
1260
1277
  * The simplest usage
1261
1278
  *
@@ -1297,21 +1314,25 @@ function createFramebufferInfo(gl, attachments, width, height) {
1297
1314
  width = width || gl.drawingBufferWidth;
1298
1315
  height = height || gl.drawingBufferHeight;
1299
1316
  attachments = attachments || defaultAttachments;
1300
- var colorAttachmentCount = 0;
1317
+ var usedColorAttachmentsPoints = [];
1301
1318
  var framebufferInfo = {
1302
1319
  framebuffer: fb,
1303
1320
  attachments: [],
1304
1321
  width: width,
1305
1322
  height: height
1306
1323
  };
1307
- attachments.forEach(function (attachmentOptions) {
1324
+ attachments.forEach(function (attachmentOptions, i) {
1308
1325
  var attachment = attachmentOptions.attachment;
1309
1326
  var samples = attachmentOptions.samples;
1310
1327
  var format = attachmentOptions.format;
1311
1328
  var attachmentPoint = attachmentOptions.attachmentPoint || getAttachmentPointForFormat(format, attachmentOptions.internalFormat);
1312
1329
 
1313
1330
  if (!attachmentPoint) {
1314
- attachmentPoint = COLOR_ATTACHMENT0 + colorAttachmentCount++;
1331
+ attachmentPoint = COLOR_ATTACHMENT0 + i;
1332
+ }
1333
+
1334
+ if (isColorAttachmentPoint(attachmentPoint)) {
1335
+ usedColorAttachmentsPoints.push(attachmentPoint);
1315
1336
  }
1316
1337
 
1317
1338
  if (!attachment) {
@@ -1355,6 +1376,11 @@ function createFramebufferInfo(gl, attachments, width, height) {
1355
1376
 
1356
1377
  framebufferInfo.attachments.push(attachment);
1357
1378
  });
1379
+
1380
+ if (gl.drawBuffers) {
1381
+ gl.drawBuffers(usedColorAttachmentsPoints);
1382
+ }
1383
+
1358
1384
  return framebufferInfo;
1359
1385
  }
1360
1386
  /**
@@ -1725,6 +1751,7 @@ var TEXTURE_2D_ARRAY = 0x8C1A;
1725
1751
  var typeMap = {};
1726
1752
  /**
1727
1753
  * Returns the corresponding bind point for a given sampler type
1754
+ * @private
1728
1755
  */
1729
1756
 
1730
1757
  function getBindPointForSamplerType(gl, type) {
@@ -2232,8 +2259,8 @@ function floatAttribSetter(gl, index) {
2232
2259
  gl.enableVertexAttribArray(index);
2233
2260
  gl.vertexAttribPointer(index, b.numComponents || b.size, b.type || FLOAT, b.normalize || false, b.stride || 0, b.offset || 0);
2234
2261
 
2235
- if (b.divisor !== undefined) {
2236
- gl.vertexAttribDivisor(index, b.divisor);
2262
+ if (gl.vertexAttribDivisor) {
2263
+ gl.vertexAttribDivisor(index, b.divisor || 0);
2237
2264
  }
2238
2265
  }
2239
2266
  };
@@ -2254,8 +2281,8 @@ function intAttribSetter(gl, index) {
2254
2281
  gl.enableVertexAttribArray(index);
2255
2282
  gl.vertexAttribIPointer(index, b.numComponents || b.size, b.type || INT, b.stride || 0, b.offset || 0);
2256
2283
 
2257
- if (b.divisor !== undefined) {
2258
- gl.vertexAttribDivisor(index, b.divisor);
2284
+ if (gl.vertexAttribDivisor) {
2285
+ gl.vertexAttribDivisor(index, b.divisor || 0);
2259
2286
  }
2260
2287
  }
2261
2288
  };
@@ -2276,8 +2303,8 @@ function uintAttribSetter(gl, index) {
2276
2303
  gl.enableVertexAttribArray(index);
2277
2304
  gl.vertexAttribIPointer(index, b.numComponents || b.size, b.type || UNSIGNED_INT, b.stride || 0, b.offset || 0);
2278
2305
 
2279
- if (b.divisor !== undefined) {
2280
- gl.vertexAttribDivisor(index, b.divisor);
2306
+ if (gl.vertexAttribDivisor) {
2307
+ gl.vertexAttribDivisor(index, b.divisor || 0);
2281
2308
  }
2282
2309
  }
2283
2310
  };
@@ -2301,8 +2328,8 @@ function matAttribSetter(gl, index, typeInfo) {
2301
2328
  gl.enableVertexAttribArray(index + i);
2302
2329
  gl.vertexAttribPointer(index + i, size, type, normalize, stride, offset + rowOffset * i);
2303
2330
 
2304
- if (b.divisor !== undefined) {
2305
- gl.vertexAttribDivisor(index + i, b.divisor);
2331
+ if (gl.vertexAttribDivisor) {
2332
+ gl.vertexAttribDivisor(index + i, b.divisor || 0);
2306
2333
  }
2307
2334
  }
2308
2335
  };
@@ -2444,6 +2471,7 @@ var spaceRE = /^[ \t]*\n/;
2444
2471
  *
2445
2472
  * @param {string} shaderSource The source of the shader
2446
2473
  * @returns {{shaderSource: string, lineOffset: number}}
2474
+ * @private
2447
2475
  */
2448
2476
 
2449
2477
  function prepShaderSource(shaderSource) {
@@ -3973,6 +4001,7 @@ function createAttributeSetters(gl, program) {
3973
4001
  * @param {Object.<string, module:twgl.AttribInfo>} buffers AttribInfos mapped by attribute name.
3974
4002
  * @memberOf module:twgl/programs
3975
4003
  * @deprecated use {@link module:twgl.setBuffersAndAttributes}
4004
+ * @private
3976
4005
  */
3977
4006
 
3978
4007
 
@@ -5809,6 +5838,7 @@ function loadAndUseImage(obj, crossOrigin, callback) {
5809
5838
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
5810
5839
  * This is often the same options you passed in when you created the texture.
5811
5840
  * @memberOf module:twgl/textures
5841
+ * @private
5812
5842
  */
5813
5843
 
5814
5844
 
@@ -5923,6 +5953,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
5923
5953
  * @param {module:twgl.CubemapReadyCallback} [callback] A function to be called when all the images have finished loading. err will
5924
5954
  * be non null if there was an error.
5925
5955
  * @memberOf module:twgl/textures
5956
+ * @private
5926
5957
  */
5927
5958
 
5928
5959
 
@@ -6011,6 +6042,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
6011
6042
  * @param {module:twgl.ThreeDReadyCallback} [callback] A function to be called when all the images have finished loading. err will
6012
6043
  * be non null if there was an error.
6013
6044
  * @memberOf module:twgl/textures
6045
+ * @private
6014
6046
  */
6015
6047
 
6016
6048
 
@@ -6895,6 +6927,8 @@ function create3DContext(canvas, opt_attribs) {
6895
6927
  * @param {WebGLContextAttributes} [opt_attribs] optional webgl context creation attributes
6896
6928
  * @return {WebGLRenderingContext} The created context.
6897
6929
  * @memberOf module:twgl
6930
+ * @deprecated
6931
+ * @private
6898
6932
  */
6899
6933
 
6900
6934
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twgl.js",
3
- "version": "4.23.1",
3
+ "version": "5.0.0",
4
4
  "description": "A Tiny WebGL helper library",
5
5
  "main": "dist/4.x/twgl-full.js",
6
6
  "types": "dist/4.x/twgl-full.d.ts",