twgl.js 5.5.3 → 6.1.1

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,4 +1,4 @@
1
- /* @license twgl.js 5.5.3 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
1
+ /* @license twgl.js 6.1.1 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
  /*
@@ -5594,21 +5594,72 @@ function setDefaults$1(newDefaults) {
5594
5594
  */
5595
5595
 
5596
5596
  /**
5597
- * Sets any packing state that will be set based on the options.
5598
- * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5597
+ * Saves the current packing state, sets the packing state as specified
5598
+ * then calls a function, after which the packing state will be restored.
5599
5599
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5600
+ * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5601
+ * @param {function():void} [fn] A function to call, after which the packing state will be restored.
5600
5602
  * @private
5601
5603
  */
5602
- function setPackState(gl, options) {
5604
+ function scopedSetPackState(gl, options, fn) {
5605
+ let colorspaceConversion;
5606
+ let premultiplyAlpha;
5607
+ let flipY;
5608
+
5603
5609
  if (options.colorspaceConversion !== undefined) {
5610
+ colorspaceConversion = gl.getParameter(UNPACK_COLORSPACE_CONVERSION_WEBGL);
5604
5611
  gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, options.colorspaceConversion);
5605
5612
  }
5606
5613
  if (options.premultiplyAlpha !== undefined) {
5614
+ premultiplyAlpha = gl.getParameter(UNPACK_PREMULTIPLY_ALPHA_WEBGL);
5607
5615
  gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, options.premultiplyAlpha);
5608
5616
  }
5609
5617
  if (options.flipY !== undefined) {
5618
+ flipY = gl.getParameter(UNPACK_FLIP_Y_WEBGL);
5610
5619
  gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, options.flipY);
5611
5620
  }
5621
+
5622
+ fn();
5623
+
5624
+ if (colorspaceConversion !== undefined) {
5625
+ gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, colorspaceConversion);
5626
+ }
5627
+ if (premultiplyAlpha !== undefined) {
5628
+ gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
5629
+ }
5630
+ if (flipY !== undefined) {
5631
+ gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, flipY);
5632
+ }
5633
+ }
5634
+
5635
+ /**
5636
+ * returns the property if set or the corresponding state if undefined
5637
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5638
+ * @param {module:twgl.TextureOptions} options
5639
+ * @param {string} property the name of the property to copy
5640
+ * @param {number} pname
5641
+ * @return {module:twgl.TextureOptions}
5642
+ */
5643
+ function getPackStateOption(gl, options, property, pname) {
5644
+ const v = options[property];
5645
+ return {
5646
+ [property]: v === undefined ? gl.getParameter(pname) : v,
5647
+ };
5648
+ }
5649
+
5650
+ /**
5651
+ * Copy the options object and apply pack state
5652
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5653
+ * @param {module:twgl.TextureOptions} options
5654
+ * @return {module:twgl.TextureOptions}
5655
+ */
5656
+ function copyOptionsAndApplyPackState(gl, options) {
5657
+ return {
5658
+ ...options,
5659
+ ...getPackStateOption(gl, options, 'flipY', UNPACK_FLIP_Y_WEBGL),
5660
+ ...getPackStateOption(gl, options, 'premultiplyAlpha', UNPACK_PREMULTIPLY_ALPHA_WEBGL),
5661
+ ...getPackStateOption(gl, options, 'colorspaceConversion', UNPACK_COLORSPACE_CONVERSION_WEBGL),
5662
+ };
5612
5663
  }
5613
5664
 
5614
5665
  /**
@@ -5897,7 +5948,6 @@ function setTextureFromElement(gl, tex, element, options) {
5897
5948
  const formatType = getFormatAndTypeForInternalFormat(internalFormat);
5898
5949
  const format = options.format || formatType.format;
5899
5950
  const type = options.type || formatType.type;
5900
- setPackState(gl, options);
5901
5951
  gl.bindTexture(target, tex);
5902
5952
  if (target === TEXTURE_CUBE_MAP$1) {
5903
5953
  // guess the parts
@@ -5930,15 +5980,17 @@ function setTextureFromElement(gl, tex, element, options) {
5930
5980
  ctx.canvas.height = size;
5931
5981
  width = size;
5932
5982
  height = size;
5933
- getCubeFacesWithNdx(gl, options).forEach(function(f) {
5934
- const xOffset = slices[f.ndx * 2 + 0] * size;
5935
- const yOffset = slices[f.ndx * 2 + 1] * size;
5936
- ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
5937
- gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
5983
+ scopedSetPackState(gl, options, () => {
5984
+ getCubeFacesWithNdx(gl, options).forEach(function(f) {
5985
+ const xOffset = slices[f.ndx * 2 + 0] * size;
5986
+ const yOffset = slices[f.ndx * 2 + 1] * size;
5987
+ ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
5988
+ gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
5989
+ });
5990
+ // Free up the canvas memory
5991
+ ctx.canvas.width = 1;
5992
+ ctx.canvas.height = 1;
5938
5993
  });
5939
- // Free up the canvas memory
5940
- ctx.canvas.width = 1;
5941
- ctx.canvas.height = 1;
5942
5994
  } else if (typeof createImageBitmap !== 'undefined') {
5943
5995
  // NOTE: It seems like we should prefer ImageBitmap because unlike canvas it's
5944
5996
  // note lossy? (alpha is not premultiplied? although I'm not sure what
@@ -5958,39 +6010,44 @@ function setTextureFromElement(gl, tex, element, options) {
5958
6010
  colorSpaceConversion: 'none',
5959
6011
  })
5960
6012
  .then(function(imageBitmap) {
5961
- setPackState(gl, options);
5962
- gl.bindTexture(target, tex);
5963
- gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
5964
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5965
- setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
5966
- }
6013
+ scopedSetPackState(gl, options, () => {
6014
+ gl.bindTexture(target, tex);
6015
+ gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
6016
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6017
+ setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
6018
+ }
6019
+ });
5967
6020
  });
5968
6021
  });
5969
6022
  }
5970
6023
  } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
5971
- const smallest = Math.min(element.width, element.height);
5972
- const largest = Math.max(element.width, element.height);
5973
- const depth = largest / smallest;
5974
- if (depth % 1 !== 0) {
5975
- throw "can not compute 3D dimensions of element";
5976
- }
5977
- const xMult = element.width === largest ? 1 : 0;
5978
- const yMult = element.height === largest ? 1 : 0;
5979
- gl.pixelStorei(UNPACK_ALIGNMENT, 1);
5980
- gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
5981
- gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
5982
- gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
5983
- gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
5984
- for (let d = 0; d < depth; ++d) {
5985
- const srcX = d * smallest * xMult;
5986
- const srcY = d * smallest * yMult;
5987
- gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
5988
- gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
5989
- gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
5990
- }
5991
- setSkipStateToDefault(gl);
6024
+ scopedSetPackState(gl, options, () => {
6025
+ const smallest = Math.min(element.width, element.height);
6026
+ const largest = Math.max(element.width, element.height);
6027
+ const depth = largest / smallest;
6028
+ if (depth % 1 !== 0) {
6029
+ throw "can not compute 3D dimensions of element";
6030
+ }
6031
+ const xMult = element.width === largest ? 1 : 0;
6032
+ const yMult = element.height === largest ? 1 : 0;
6033
+ gl.pixelStorei(UNPACK_ALIGNMENT, 1);
6034
+ gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
6035
+ gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
6036
+ gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
6037
+ gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
6038
+ for (let d = 0; d < depth; ++d) {
6039
+ const srcX = d * smallest * xMult;
6040
+ const srcY = d * smallest * yMult;
6041
+ gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
6042
+ gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
6043
+ gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
6044
+ }
6045
+ setSkipStateToDefault(gl);
6046
+ });
5992
6047
  } else {
5993
- gl.texImage2D(target, level, internalFormat, format, type, element);
6048
+ scopedSetPackState(gl, options, () => {
6049
+ gl.texImage2D(target, level, internalFormat, format, type, element);
6050
+ });
5994
6051
  }
5995
6052
  if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5996
6053
  setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
@@ -6239,7 +6296,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
6239
6296
  options = options || defaults$1.textureOptions;
6240
6297
  setTextureTo1PixelColor(gl, tex, options);
6241
6298
  // Because it's async we need to copy the options.
6242
- options = Object.assign({}, options);
6299
+ options = copyOptionsAndApplyPackState(gl, options);
6243
6300
  const img = loadAndUseImage(options.src, options.crossOrigin, function(err, img) {
6244
6301
  if (err) {
6245
6302
  callback(err, tex, img);
@@ -6279,7 +6336,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
6279
6336
  }
6280
6337
  setTextureTo1PixelColor(gl, tex, options);
6281
6338
  // Because it's async we need to copy the options.
6282
- options = Object.assign({}, options);
6339
+ options = copyOptionsAndApplyPackState(gl, options);
6283
6340
  let numToLoad = 6;
6284
6341
  const errors = [];
6285
6342
  const faces = getCubeFaceOrder(gl, options);
@@ -6294,24 +6351,25 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
6294
6351
  if (img.width !== img.height) {
6295
6352
  errors.push("cubemap face img is not a square: " + img.src);
6296
6353
  } else {
6297
- setPackState(gl, options);
6298
- gl.bindTexture(target, tex);
6299
-
6300
- // So assuming this is the first image we now have one face that's img sized
6301
- // and 5 faces that are 1x1 pixel so size the other faces
6302
- if (numToLoad === 5) {
6303
- // use the default order
6304
- getCubeFaceOrder().forEach(function(otherTarget) {
6305
- // Should we re-use the same face or a color?
6306
- gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
6307
- });
6308
- } else {
6309
- gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
6310
- }
6311
-
6312
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6313
- gl.generateMipmap(target);
6314
- }
6354
+ scopedSetPackState(gl, options, () => {
6355
+ gl.bindTexture(target, tex);
6356
+
6357
+ // So assuming this is the first image we now have one face that's img sized
6358
+ // and 5 faces that are 1x1 pixel so size the other faces
6359
+ if (numToLoad === 5) {
6360
+ // use the default order
6361
+ getCubeFaceOrder().forEach(function(otherTarget) {
6362
+ // Should we re-use the same face or a color?
6363
+ gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
6364
+ });
6365
+ } else {
6366
+ gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
6367
+ }
6368
+
6369
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6370
+ gl.generateMipmap(target);
6371
+ }
6372
+ });
6315
6373
  }
6316
6374
  }
6317
6375
 
@@ -6359,7 +6417,7 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
6359
6417
  }
6360
6418
  setTextureTo1PixelColor(gl, tex, options);
6361
6419
  // Because it's async we need to copy the options.
6362
- options = Object.assign({}, options);
6420
+ options = copyOptionsAndApplyPackState(gl, options);
6363
6421
  let numToLoad = urls.length;
6364
6422
  const errors = [];
6365
6423
  let imgs; // eslint-disable-line
@@ -6375,43 +6433,44 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
6375
6433
  if (err) {
6376
6434
  errors.push(err);
6377
6435
  } else {
6378
- setPackState(gl, options);
6379
- gl.bindTexture(target, tex);
6380
-
6381
- if (firstImage) {
6382
- firstImage = false;
6383
- width = options.width || img.width;
6384
- height = options.height || img.height;
6385
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
6386
-
6387
- // put it in every slice otherwise some slices will be 0,0,0,0
6388
- for (let s = 0; s < depth; ++s) {
6389
- gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
6390
- }
6391
- } else {
6392
- let src = img;
6393
- let ctx;
6394
- if (img.width !== width || img.height !== height) {
6395
- // Size the image to fix
6396
- ctx = getShared2DContext();
6397
- src = ctx.canvas;
6398
- ctx.canvas.width = width;
6399
- ctx.canvas.height = height;
6400
- ctx.drawImage(img, 0, 0, width, height);
6401
- }
6436
+ scopedSetPackState(gl, options, () => {
6437
+ gl.bindTexture(target, tex);
6402
6438
 
6403
- gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
6439
+ if (firstImage) {
6440
+ firstImage = false;
6441
+ width = options.width || img.width;
6442
+ height = options.height || img.height;
6443
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
6404
6444
 
6405
- // free the canvas memory
6406
- if (ctx && src === ctx.canvas) {
6407
- ctx.canvas.width = 0;
6408
- ctx.canvas.height = 0;
6445
+ // put it in every slice otherwise some slices will be 0,0,0,0
6446
+ for (let s = 0; s < depth; ++s) {
6447
+ gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
6448
+ }
6449
+ } else {
6450
+ let src = img;
6451
+ let ctx;
6452
+ if (img.width !== width || img.height !== height) {
6453
+ // Size the image to fix
6454
+ ctx = getShared2DContext();
6455
+ src = ctx.canvas;
6456
+ ctx.canvas.width = width;
6457
+ ctx.canvas.height = height;
6458
+ ctx.drawImage(img, 0, 0, width, height);
6459
+ }
6460
+
6461
+ gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
6462
+
6463
+ // free the canvas memory
6464
+ if (ctx && src === ctx.canvas) {
6465
+ ctx.canvas.width = 0;
6466
+ ctx.canvas.height = 0;
6467
+ }
6409
6468
  }
6410
- }
6411
6469
 
6412
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6413
- gl.generateMipmap(target);
6414
- }
6470
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
6471
+ gl.generateMipmap(target);
6472
+ }
6473
+ });
6415
6474
  }
6416
6475
 
6417
6476
  if (numToLoad === 0) {
@@ -6489,21 +6548,22 @@ function setTextureFromArray(gl, tex, src, options) {
6489
6548
  }
6490
6549
  setSkipStateToDefault(gl);
6491
6550
  gl.pixelStorei(UNPACK_ALIGNMENT, options.unpackAlignment || 1);
6492
- setPackState(gl, options);
6493
- if (target === TEXTURE_CUBE_MAP$1) {
6494
- const elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
6495
- const faceSize = numElements / 6 * elementsPerElement;
6496
-
6497
- getCubeFacesWithNdx(gl, options).forEach(f => {
6498
- const offset = faceSize * f.ndx;
6499
- const data = src.subarray(offset, offset + faceSize);
6500
- gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
6501
- });
6502
- } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6503
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
6504
- } else {
6505
- gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
6506
- }
6551
+ scopedSetPackState(gl, options, () => {
6552
+ if (target === TEXTURE_CUBE_MAP$1) {
6553
+ const elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
6554
+ const faceSize = numElements / 6 * elementsPerElement;
6555
+
6556
+ getCubeFacesWithNdx(gl, options).forEach(f => {
6557
+ const offset = faceSize * f.ndx;
6558
+ const data = src.subarray(offset, offset + faceSize);
6559
+ gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
6560
+ });
6561
+ } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6562
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
6563
+ } else {
6564
+ gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
6565
+ }
6566
+ });
6507
6567
  return {
6508
6568
  width: width,
6509
6569
  height: height,
@@ -6528,24 +6588,46 @@ function setEmptyTexture(gl, tex, options) {
6528
6588
  const formatType = getFormatAndTypeForInternalFormat(internalFormat);
6529
6589
  const format = options.format || formatType.format;
6530
6590
  const type = options.type || formatType.type;
6531
- setPackState(gl, options);
6532
- if (target === TEXTURE_CUBE_MAP$1) {
6533
- for (let ii = 0; ii < 6; ++ii) {
6534
- gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6591
+ scopedSetPackState(gl, options, () => {
6592
+ if (target === TEXTURE_CUBE_MAP$1) {
6593
+ for (let ii = 0; ii < 6; ++ii) {
6594
+ gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6595
+ }
6596
+ } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6597
+ gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
6598
+ } else {
6599
+ gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
6535
6600
  }
6536
- } else if (target === TEXTURE_3D$1 || target === TEXTURE_2D_ARRAY$1) {
6537
- gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
6538
- } else {
6539
- gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
6540
- }
6601
+ });
6541
6602
  }
6542
6603
 
6543
6604
  /**
6544
6605
  * Creates a texture based on the options passed in.
6545
6606
  *
6607
+ * See {@link module:twgl.TextureOptions}
6608
+ *
6546
6609
  * Note: may reset UNPACK_ALIGNMENT, UNPACK_ROW_LENGTH, UNPACK_IMAGE_HEIGHT, UNPACK_SKIP_IMAGES
6547
6610
  * UNPACK_SKIP_PIXELS, and UNPACK_SKIP_ROWS
6548
6611
  *
6612
+ * UNPACK_FLIP_Y_WEBGL, UNPACK_PREMULTIPLY_ALPHA_WEBGL, UNPACK_COLORSPACE_CONVERSION_WEBGL
6613
+ * are left as is though you can pass in options for flipY, premultiplyAlpha, and colorspaceConversion
6614
+ * to override them.
6615
+ *
6616
+ * As for the behavior of these settings
6617
+ *
6618
+ * ```js
6619
+ * gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
6620
+ * t1 = twgl.createTexture({src: someImage }); // flipped
6621
+ * t2 = twgl.createTexture({src: someImage, flipY: true }); // flipped
6622
+ * t3 = twgl.createTexture({src: someImage, flipY: false }); // not flipped
6623
+ * t4 = twgl.createTexture({src: someImage }); // flipped
6624
+ * ```
6625
+ *
6626
+ * * t1 is flipped because UNPACK_FLIP_Y_WEBGL is true
6627
+ * * t2 is flipped because it was requested
6628
+ * * t3 is not flipped because it was requested
6629
+ * * t4 is flipped because UNPACK_FLIP_Y_WEBGL has been restored to true
6630
+ *
6549
6631
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6550
6632
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6551
6633
  * @param {module:twgl.TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
@@ -6604,6 +6686,42 @@ function createTexture(gl, options, callback) {
6604
6686
  return tex;
6605
6687
  }
6606
6688
 
6689
+ /**
6690
+ * Value returned by createTextureAsync
6691
+ *
6692
+ * @typedef {Object} CreateTextureInfo
6693
+ * @param {WebGLTexture} texture the texture.
6694
+ * @param {module:twgl.TextureSrc} source image(s) used to as the src for the texture
6695
+ * @memberOf module:twgl
6696
+ */
6697
+
6698
+ /**
6699
+ * Creates a texture based on the options passed in.
6700
+ *
6701
+ * see {@link module:twgl/textures.createTexture}.
6702
+ * The only difference is this function returns a promise
6703
+ * where as the other returns a texture and takes a callback.
6704
+ *
6705
+ * Note: this is here for completeness. It is probably better to use
6706
+ * the non-async version as it returns a usable texture immediately
6707
+ * where as this one you have to wait for it to load.
6708
+ *
6709
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6710
+ * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6711
+ * @return {Promise<CreateTextureInfo>} The created texture and source.
6712
+ */
6713
+ function createTextureAsync(gl, options) {
6714
+ return new Promise((resolve, reject) => {
6715
+ createTexture(gl, options, (err, texture, source) => {
6716
+ if (err) {
6717
+ reject(err);
6718
+ } else {
6719
+ resolve({ texture, source });
6720
+ }
6721
+ });
6722
+ });
6723
+ }
6724
+
6607
6725
  /**
6608
6726
  * Resizes a texture based on the options passed in.
6609
6727
  *
@@ -6780,6 +6898,42 @@ function createTextures(gl, textureOptions, callback) {
6780
6898
  return textures;
6781
6899
  }
6782
6900
 
6901
+ /**
6902
+ * Value returned by createTextureAsync
6903
+ *
6904
+ * @typedef {Object} CreateTexturesInfo
6905
+ * @param {Object.<string, WebGLTexture>} textures the created textures by name. Same as returned by {@link module:twgl.createTextures}.
6906
+ * @param {Object.<string, module:twgl.TextureSrc>} sources the image(s) used for the texture by name.
6907
+ * @memberOf module:twgl
6908
+ */
6909
+
6910
+ /**
6911
+ * Creates textures based on the options passed in.
6912
+ *
6913
+ * see {@link module:twgl/textures.createTextures}.
6914
+ * The only difference is this function returns a promise
6915
+ * where as the other returns a texture and takes a callback.
6916
+ *
6917
+ * Note: this is here for completeness. It is probably better to use
6918
+ * the non-async version as it returns usable textures immediately
6919
+ * where as this one you have to wait for them to load.
6920
+ *
6921
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6922
+ * @param {Object.<string,module:twgl.TextureOptions>} options A object of TextureOptions one per texture.
6923
+ * @return {Promise<CreateTexturesInfo>} The created textures and sources.
6924
+ */
6925
+ function createTexturesAsync(gl, options) {
6926
+ return new Promise((resolve, reject) => {
6927
+ createTexture(gl, options, (err, textures, sources) => {
6928
+ if (err) {
6929
+ reject(err);
6930
+ } else {
6931
+ resolve({ textures, sources });
6932
+ }
6933
+ });
6934
+ });
6935
+ }
6936
+
6783
6937
  var textures = /*#__PURE__*/Object.freeze({
6784
6938
  __proto__: null,
6785
6939
  setTextureDefaults_: setDefaults$1,
@@ -6787,6 +6941,7 @@ var textures = /*#__PURE__*/Object.freeze({
6787
6941
  createSamplers: createSamplers,
6788
6942
  setSamplerParameters: setSamplerParameters,
6789
6943
  createTexture: createTexture,
6944
+ createTextureAsync: createTextureAsync,
6790
6945
  setEmptyTexture: setEmptyTexture,
6791
6946
  setTextureFromArray: setTextureFromArray,
6792
6947
  loadTextureFromUrl: loadTextureFromUrl,
@@ -6795,6 +6950,7 @@ var textures = /*#__PURE__*/Object.freeze({
6795
6950
  setTextureParameters: setTextureParameters,
6796
6951
  setDefaultTextureColor: setDefaultTextureColor,
6797
6952
  createTextures: createTextures,
6953
+ createTexturesAsync: createTexturesAsync,
6798
6954
  resizeTexture: resizeTexture,
6799
6955
  canGenerateMipmap: canGenerateMipmap,
6800
6956
  canFilter: canFilter,
@@ -7497,7 +7653,7 @@ function getShaderTypeFromScriptType(gl, scriptType) {
7497
7653
  function deleteProgramAndShaders(gl, program, notThese) {
7498
7654
  const shaders = gl.getAttachedShaders(program);
7499
7655
  for (const shader of shaders) {
7500
- if (notThese.has(shader)) {
7656
+ if (!notThese.has(shader)) {
7501
7657
  gl.deleteShader(shader);
7502
7658
  }
7503
7659
  }
@@ -7526,8 +7682,8 @@ function createProgramNoCheck(gl, shaders, programOptions) {
7526
7682
  shader = gl.createShader(type);
7527
7683
  gl.shaderSource(shader, prepShaderSource(src).shaderSource);
7528
7684
  gl.compileShader(shader);
7529
- gl.attachShader(program, shader);
7530
7685
  }
7686
+ gl.attachShader(program, shader);
7531
7687
  }
7532
7688
 
7533
7689
  Object.entries(attribLocations).forEach(([attrib, loc]) => gl.bindAttribLocation(program, loc, attrib));
@@ -7563,7 +7719,7 @@ function createProgramNoCheck(gl, shaders, programOptions) {
7563
7719
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7564
7720
  * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
7565
7721
  * @param {module:twgl.ProgramOptions|string[]|module:twgl.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
7566
- * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7722
+ * @param {number[]|module:twgl.ErrorCallback} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7567
7723
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7568
7724
  * on error. If you want something else pass an callback. It's passed an error message.
7569
7725
  * @return {WebGLProgram?} the created program or null if error of a callback was provided.
@@ -7631,7 +7787,7 @@ function wrapCallbackFnToAsyncFn(fn) {
7631
7787
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7632
7788
  * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
7633
7789
  * @param {module:twgl.ProgramOptions|string[]|module:twgl.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
7634
- * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7790
+ * @param {number[]|module:twgl.ErrorCallback} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7635
7791
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7636
7792
  * on error. If you want something else pass an callback. It's passed an error message.
7637
7793
  * @return {Promise<WebGLProgram>} The created program
@@ -7648,7 +7804,7 @@ const createProgramAsync = wrapCallbackFnToAsyncFn(createProgram);
7648
7804
  * shaders or ids. The first is assumed to be the vertex shader,
7649
7805
  * the second the fragment shader.
7650
7806
  * @param {module:twgl.ProgramOptions|string[]|module:twgl.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
7651
- * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7807
+ * @param {number[]|module:twgl.ErrorCallback} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7652
7808
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7653
7809
  * on error. If you want something else pass an callback. It's passed an error message.
7654
7810
  * @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
@@ -7715,7 +7871,7 @@ function getProgramErrors(gl, program, errFn) {
7715
7871
  * tags for the shaders. The first is assumed to be the
7716
7872
  * vertex shader, the second the fragment shader.
7717
7873
  * @param {module:twgl.ProgramOptions|string[]|module:twgl.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
7718
- * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7874
+ * @param {number[]|module:twgl.ErrorCallback} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7719
7875
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7720
7876
  * on error. If you want something else pass an callback. It's passed an error message.
7721
7877
  * @return {WebGLProgram?} the created program or null if error or a callback was provided.
@@ -7751,7 +7907,7 @@ function createProgramFromScripts(
7751
7907
  * shaders. The first is assumed to be the vertex shader,
7752
7908
  * the second the fragment shader.
7753
7909
  * @param {module:twgl.ProgramOptions|string[]|module:twgl.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
7754
- * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7910
+ * @param {number[]|module:twgl.ErrorCallback} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
7755
7911
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7756
7912
  * on error. If you want something else pass an callback. It's passed an error message.
7757
7913
  * @return {WebGLProgram?} the created program or null if error or a callback was provided.
@@ -8101,6 +8257,7 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
8101
8257
  * @property {ArrayBuffer} array The array buffer that contains the uniform values
8102
8258
  * @property {Float32Array} asFloat A float view on the array buffer. This is useful
8103
8259
  * inspecting the contents of the buffer in the debugger.
8260
+ * @property {Uint8Array} asUint8t A uint8 view on the array buffer.
8104
8261
  * @property {WebGLBuffer} buffer A WebGL buffer that will hold a copy of the uniform values for rendering.
8105
8262
  * @property {number} [offset] offset into buffer
8106
8263
  * @property {Object<string, ArrayBufferView>} uniforms A uniform name to ArrayBufferView map.
@@ -8119,6 +8276,15 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
8119
8276
  * @memberOf module:twgl
8120
8277
  */
8121
8278
 
8279
+ /**
8280
+ * Options to allow createUniformBlockInfo to use an existing buffer and arrayBuffer at an offset
8281
+ * @typedef {Object} UniformBlockInfoOptions
8282
+ * @property {ArrayBuffer} [array] an existing array buffer to use for values
8283
+ * @property {number} [offset] the offset in bytes to use in the array buffer (default = 0)
8284
+ * @property {WebGLBuffer} [buffer] the buffer to use for this uniform block info
8285
+ * @property {number} [bufferOffset] the offset in bytes in the buffer to use (default = use offset above)
8286
+ */
8287
+
8122
8288
  /**
8123
8289
  * Creates a `UniformBlockInfo` for the specified block
8124
8290
  *
@@ -8133,10 +8299,11 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
8133
8299
  * @param {module:twgl.UniformBlockSpec} uniformBlockSpec. A UniformBlockSpec as returned
8134
8300
  * from {@link module:twgl.createUniformBlockSpecFromProgram}.
8135
8301
  * @param {string} blockName The name of the block.
8302
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
8136
8303
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
8137
8304
  * @memberOf module:twgl/programs
8138
8305
  */
8139
- function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockName) {
8306
+ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockName, options = {}) {
8140
8307
  const blockSpecs = uniformBlockSpec.blockSpecs;
8141
8308
  const uniformData = uniformBlockSpec.uniformData;
8142
8309
  const blockSpec = blockSpecs[blockName];
@@ -8147,10 +8314,14 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8147
8314
  uniforms: {},
8148
8315
  };
8149
8316
  }
8150
- const array = new ArrayBuffer(blockSpec.size);
8151
- const buffer = gl.createBuffer();
8317
+ const offset = options.offset ?? 0;
8318
+ const array = options.array ?? new ArrayBuffer(blockSpec.size);
8319
+ const buffer = options.buffer ?? gl.createBuffer();
8152
8320
  const uniformBufferIndex = blockSpec.index;
8153
8321
  gl.bindBuffer(UNIFORM_BUFFER, buffer);
8322
+ if (!options.buffer) {
8323
+ gl.bufferData(UNIFORM_BUFFER, array.byteLength, DYNAMIC_DRAW);
8324
+ }
8154
8325
  gl.uniformBlockBinding(program, blockSpec.index, uniformBufferIndex);
8155
8326
 
8156
8327
  let prefix = blockName + ".";
@@ -8175,7 +8346,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8175
8346
  const byteLength = isArray
8176
8347
  ? pad(typeInfo.size, 16) * data.size
8177
8348
  : typeInfo.size * data.size;
8178
- const uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
8349
+ const uniformView = new Type(array, offset + data.offset, byteLength / Type.BYTES_PER_ELEMENT);
8179
8350
  uniforms[name] = uniformView;
8180
8351
  // Note: I'm not sure what to do here. The original
8181
8352
  // idea was to create TypedArray views into each part
@@ -8210,9 +8381,12 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8210
8381
  name: blockName,
8211
8382
  array,
8212
8383
  asFloat: new Float32Array(array), // for debugging
8384
+ asUint8: new Uint8Array(array), // needed for gl.bufferSubData because it doesn't take an array buffer
8213
8385
  buffer,
8214
8386
  uniforms,
8215
8387
  setters,
8388
+ offset: options.bufferOffset ?? offset,
8389
+ size: blockSpec.size,
8216
8390
  };
8217
8391
  }
8218
8392
 
@@ -8229,11 +8403,12 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
8229
8403
  * @param {module:twgl.ProgramInfo} programInfo a `ProgramInfo`
8230
8404
  * as returned from {@link module:twgl.createProgramInfo}
8231
8405
  * @param {string} blockName The name of the block.
8406
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
8232
8407
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
8233
8408
  * @memberOf module:twgl/programs
8234
8409
  */
8235
- function createUniformBlockInfo(gl, programInfo, blockName) {
8236
- return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName);
8410
+ function createUniformBlockInfo(gl, programInfo, blockName, options = {}) {
8411
+ return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName, options);
8237
8412
  }
8238
8413
 
8239
8414
  /**
@@ -8259,7 +8434,7 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
8259
8434
  const blockSpec = uniformBlockSpec.blockSpecs[uniformBlockInfo.name];
8260
8435
  if (blockSpec) {
8261
8436
  const bufferBindIndex = blockSpec.index;
8262
- gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, uniformBlockInfo.array.byteLength);
8437
+ gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, uniformBlockInfo.size ?? uniformBlockInfo.array.byteLength);
8263
8438
  return true;
8264
8439
  }
8265
8440
  return false;
@@ -8282,7 +8457,7 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
8282
8457
  */
8283
8458
  function setUniformBlock(gl, programInfo, uniformBlockInfo) {
8284
8459
  if (bindUniformBlock(gl, programInfo, uniformBlockInfo)) {
8285
- gl.bufferData(UNIFORM_BUFFER, uniformBlockInfo.array, DYNAMIC_DRAW);
8460
+ gl.bufferSubData(UNIFORM_BUFFER, 0, uniformBlockInfo.asUint8, uniformBlockInfo.offset || 0, uniformBlockInfo.size || 0);
8286
8461
  }
8287
8462
  }
8288
8463
 
@@ -8727,6 +8902,8 @@ function setBuffersAndAttributes(gl, programInfo, buffers) {
8727
8902
  /**
8728
8903
  * @typedef {Object} ProgramInfo
8729
8904
  * @property {WebGLProgram} program A shader program
8905
+ * @property {Object<string, WebGLUniformLocation>} uniformLocations The uniform locations of each uniform
8906
+ * @property {Object<string, number>} attribLocations The locations of each attribute
8730
8907
  * @property {Object<string, function>} uniformSetters object of setters as returned from createUniformSetters,
8731
8908
  * @property {Object<string, function>} attribSetters object of setters as returned from createAttribSetters,
8732
8909
  * @property {module:twgl.UniformBlockSpec} [uniformBlockSpec] a uniform block spec for making UniformBlockInfos with createUniformBlockInfo etc..
@@ -8758,6 +8935,8 @@ function createProgramInfoFromProgram(gl, program) {
8758
8935
  program,
8759
8936
  uniformSetters,
8760
8937
  attribSetters,
8938
+ uniformLocations: Object.fromEntries(Object.entries(uniformSetters).map(([k, v]) => [k, v.location])),
8939
+ attribLocations: Object.fromEntries(Object.entries(attribSetters).map(([k, v]) => [k, v.location])),
8761
8940
  };
8762
8941
 
8763
8942
  if (isWebGL2(gl)) {
@@ -8794,7 +8973,7 @@ const notIdRE = /\s|{|}|;/;
8794
8973
  * shaders or ids. The first is assumed to be the vertex shader,
8795
8974
  * the second the fragment shader.
8796
8975
  * @param {module:twgl.ProgramOptions|string[]|module:twgl.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
8797
- * @param {number[]} [opt_locations|module:twgl.ErrorCallback] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
8976
+ * @param {number[]|module:twgl.ErrorCallback} [opt_locations] The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback.
8798
8977
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
8799
8978
  * on error. If you want something else pass an callback. It's passed an error message.
8800
8979
  * @return {module:twgl.ProgramInfo?} The created ProgramInfo or null if it failed to link or compile
@@ -10082,4 +10261,4 @@ function resizeCanvasToDisplaySize(canvas, multiplier) {
10082
10261
  return false;
10083
10262
  }
10084
10263
 
10085
- export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createProgramInfos, createProgramInfosAsync, createPrograms, createProgramsAsync, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray$1 as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents$1 as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer$1 as isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults$2 as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };
10264
+ export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createProgramInfos, createProgramInfosAsync, createPrograms, createProgramsAsync, createSampler, createSamplers, createTexture, createTextureAsync, createTextures, createTexturesAsync, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray$1 as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents$1 as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer$1 as isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults$2 as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };