twgl.js 5.5.4 → 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,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 5.5.4 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 6.1.1 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
  */
@@ -5408,7 +5408,7 @@ function deleteProgramAndShaders(gl, program, notThese) {
5408
5408
  try {
5409
5409
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
5410
5410
  var shader = _step.value;
5411
- if (notThese.has(shader)) {
5411
+ if (!notThese.has(shader)) {
5412
5412
  gl.deleteShader(shader);
5413
5413
  }
5414
5414
  }
@@ -5443,8 +5443,8 @@ function createProgramNoCheck(gl, shaders, programOptions) {
5443
5443
  shader = gl.createShader(type);
5444
5444
  gl.shaderSource(shader, prepShaderSource(src).shaderSource);
5445
5445
  gl.compileShader(shader);
5446
- gl.attachShader(program, shader);
5447
5446
  }
5447
+ gl.attachShader(program, shader);
5448
5448
  }
5449
5449
  Object.entries(attribLocations).forEach(function (_ref) {
5450
5450
  var _ref2 = _slicedToArray(_ref, 2),
@@ -6064,6 +6064,7 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
6064
6064
  * @property {ArrayBuffer} array The array buffer that contains the uniform values
6065
6065
  * @property {Float32Array} asFloat A float view on the array buffer. This is useful
6066
6066
  * inspecting the contents of the buffer in the debugger.
6067
+ * @property {Uint8Array} asUint8t A uint8 view on the array buffer.
6067
6068
  * @property {WebGLBuffer} buffer A WebGL buffer that will hold a copy of the uniform values for rendering.
6068
6069
  * @property {number} [offset] offset into buffer
6069
6070
  * @property {Object<string, ArrayBufferView>} uniforms A uniform name to ArrayBufferView map.
@@ -6082,6 +6083,15 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
6082
6083
  * @memberOf module:twgl
6083
6084
  */
6084
6085
 
6086
+ /**
6087
+ * Options to allow createUniformBlockInfo to use an existing buffer and arrayBuffer at an offset
6088
+ * @typedef {Object} UniformBlockInfoOptions
6089
+ * @property {ArrayBuffer} [array] an existing array buffer to use for values
6090
+ * @property {number} [offset] the offset in bytes to use in the array buffer (default = 0)
6091
+ * @property {WebGLBuffer} [buffer] the buffer to use for this uniform block info
6092
+ * @property {number} [bufferOffset] the offset in bytes in the buffer to use (default = use offset above)
6093
+ */
6094
+
6085
6095
  /**
6086
6096
  * Creates a `UniformBlockInfo` for the specified block
6087
6097
  *
@@ -6096,10 +6106,13 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
6096
6106
  * @param {module:twgl.UniformBlockSpec} uniformBlockSpec. A UniformBlockSpec as returned
6097
6107
  * from {@link module:twgl.createUniformBlockSpecFromProgram}.
6098
6108
  * @param {string} blockName The name of the block.
6109
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
6099
6110
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
6100
6111
  * @memberOf module:twgl/programs
6101
6112
  */
6102
6113
  function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockName) {
6114
+ var _options$offset, _options$array, _options$buffer, _options$bufferOffset;
6115
+ var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
6103
6116
  var blockSpecs = uniformBlockSpec.blockSpecs;
6104
6117
  var uniformData = uniformBlockSpec.uniformData;
6105
6118
  var blockSpec = blockSpecs[blockName];
@@ -6110,10 +6123,14 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
6110
6123
  uniforms: {}
6111
6124
  };
6112
6125
  }
6113
- var array = new ArrayBuffer(blockSpec.size);
6114
- var buffer = gl.createBuffer();
6126
+ var offset = (_options$offset = options.offset) !== null && _options$offset !== void 0 ? _options$offset : 0;
6127
+ var array = (_options$array = options.array) !== null && _options$array !== void 0 ? _options$array : new ArrayBuffer(blockSpec.size);
6128
+ var buffer = (_options$buffer = options.buffer) !== null && _options$buffer !== void 0 ? _options$buffer : gl.createBuffer();
6115
6129
  var uniformBufferIndex = blockSpec.index;
6116
6130
  gl.bindBuffer(UNIFORM_BUFFER, buffer);
6131
+ if (!options.buffer) {
6132
+ gl.bufferData(UNIFORM_BUFFER, array.byteLength, DYNAMIC_DRAW);
6133
+ }
6117
6134
  gl.uniformBlockBinding(program, blockSpec.index, uniformBufferIndex);
6118
6135
  var prefix = blockName + ".";
6119
6136
  if (arraySuffixRE.test(prefix)) {
@@ -6135,7 +6152,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
6135
6152
  var typeInfo = typeMap[data.type];
6136
6153
  var Type = typeInfo.Type;
6137
6154
  var byteLength = isArray ? pad(typeInfo.size, 16) * data.size : typeInfo.size * data.size;
6138
- var uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
6155
+ var uniformView = new Type(array, offset + data.offset, byteLength / Type.BYTES_PER_ELEMENT);
6139
6156
  uniforms[name] = uniformView;
6140
6157
  // Note: I'm not sure what to do here. The original
6141
6158
  // idea was to create TypedArray views into each part
@@ -6171,9 +6188,13 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
6171
6188
  array: array,
6172
6189
  asFloat: new Float32Array(array),
6173
6190
  // for debugging
6191
+ asUint8: new Uint8Array(array),
6192
+ // needed for gl.bufferSubData because it doesn't take an array buffer
6174
6193
  buffer: buffer,
6175
6194
  uniforms: uniforms,
6176
- setters: setters
6195
+ setters: setters,
6196
+ offset: (_options$bufferOffset = options.bufferOffset) !== null && _options$bufferOffset !== void 0 ? _options$bufferOffset : offset,
6197
+ size: blockSpec.size
6177
6198
  };
6178
6199
  }
6179
6200
 
@@ -6190,11 +6211,13 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
6190
6211
  * @param {module:twgl.ProgramInfo} programInfo a `ProgramInfo`
6191
6212
  * as returned from {@link module:twgl.createProgramInfo}
6192
6213
  * @param {string} blockName The name of the block.
6214
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
6193
6215
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
6194
6216
  * @memberOf module:twgl/programs
6195
6217
  */
6196
6218
  function createUniformBlockInfo(gl, programInfo, blockName) {
6197
- return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName);
6219
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
6220
+ return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName, options);
6198
6221
  }
6199
6222
 
6200
6223
  /**
@@ -6219,8 +6242,9 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
6219
6242
  var uniformBlockSpec = programInfo.uniformBlockSpec || programInfo;
6220
6243
  var blockSpec = uniformBlockSpec.blockSpecs[uniformBlockInfo.name];
6221
6244
  if (blockSpec) {
6245
+ var _uniformBlockInfo$siz;
6222
6246
  var bufferBindIndex = blockSpec.index;
6223
- gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, uniformBlockInfo.array.byteLength);
6247
+ gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, (_uniformBlockInfo$siz = uniformBlockInfo.size) !== null && _uniformBlockInfo$siz !== void 0 ? _uniformBlockInfo$siz : uniformBlockInfo.array.byteLength);
6224
6248
  return true;
6225
6249
  }
6226
6250
  return false;
@@ -6243,7 +6267,7 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
6243
6267
  */
6244
6268
  function setUniformBlock(gl, programInfo, uniformBlockInfo) {
6245
6269
  if (bindUniformBlock(gl, programInfo, uniformBlockInfo)) {
6246
- gl.bufferData(UNIFORM_BUFFER, uniformBlockInfo.array, DYNAMIC_DRAW);
6270
+ gl.bufferSubData(UNIFORM_BUFFER, 0, uniformBlockInfo.asUint8, uniformBlockInfo.offset || 0, uniformBlockInfo.size || 0);
6247
6271
  }
6248
6272
  }
6249
6273
 
@@ -6685,6 +6709,8 @@ function setBuffersAndAttributes(gl, programInfo, buffers) {
6685
6709
  /**
6686
6710
  * @typedef {Object} ProgramInfo
6687
6711
  * @property {WebGLProgram} program A shader program
6712
+ * @property {Object<string, WebGLUniformLocation>} uniformLocations The uniform locations of each uniform
6713
+ * @property {Object<string, number>} attribLocations The locations of each attribute
6688
6714
  * @property {Object<string, function>} uniformSetters object of setters as returned from createUniformSetters,
6689
6715
  * @property {Object<string, function>} attribSetters object of setters as returned from createAttribSetters,
6690
6716
  * @property {module:twgl.UniformBlockSpec} [uniformBlockSpec] a uniform block spec for making UniformBlockInfos with createUniformBlockInfo etc..
@@ -6715,7 +6741,19 @@ function createProgramInfoFromProgram(gl, program) {
6715
6741
  var programInfo = {
6716
6742
  program: program,
6717
6743
  uniformSetters: uniformSetters,
6718
- attribSetters: attribSetters
6744
+ attribSetters: attribSetters,
6745
+ uniformLocations: Object.fromEntries(Object.entries(uniformSetters).map(function (_ref3) {
6746
+ var _ref4 = _slicedToArray(_ref3, 2),
6747
+ k = _ref4[0],
6748
+ v = _ref4[1];
6749
+ return [k, v.location];
6750
+ })),
6751
+ attribLocations: Object.fromEntries(Object.entries(attribSetters).map(function (_ref5) {
6752
+ var _ref6 = _slicedToArray(_ref5, 2),
6753
+ k = _ref6[0],
6754
+ v = _ref6[1];
6755
+ return [k, v.location];
6756
+ }))
6719
6757
  };
6720
6758
  if (utils.isWebGL2(gl)) {
6721
6759
  programInfo.uniformBlockSpec = createUniformBlockSpecFromProgram(gl, program);
@@ -6858,10 +6896,10 @@ function createPrograms(gl, programSpecs) {
6858
6896
  var noDeleteShadersSet = new Set();
6859
6897
 
6860
6898
  // compile and link everything
6861
- var programs = Object.fromEntries(Object.entries(programSpecs).map(function (_ref3) {
6862
- var _ref4 = _slicedToArray(_ref3, 2),
6863
- name = _ref4[0],
6864
- spec = _ref4[1];
6899
+ var programs = Object.fromEntries(Object.entries(programSpecs).map(function (_ref7) {
6900
+ var _ref8 = _slicedToArray(_ref7, 2),
6901
+ name = _ref8[0],
6902
+ spec = _ref8[1];
6865
6903
  var options = _objectSpread({}, programOptions);
6866
6904
  var shaders = Array.isArray(spec) ? spec : spec.shaders;
6867
6905
  if (!Array.isArray(spec)) {
@@ -6921,10 +6959,10 @@ function createPrograms(gl, programSpecs) {
6921
6959
  function createProgramInfos(gl, programSpecs, programOptions) {
6922
6960
  programOptions = getProgramOptions(programOptions);
6923
6961
  function createProgramInfosForPrograms(gl, programs) {
6924
- return Object.fromEntries(Object.entries(programs).map(function (_ref5) {
6925
- var _ref6 = _slicedToArray(_ref5, 2),
6926
- name = _ref6[0],
6927
- program = _ref6[1];
6962
+ return Object.fromEntries(Object.entries(programs).map(function (_ref9) {
6963
+ var _ref10 = _slicedToArray(_ref9, 2),
6964
+ name = _ref10[0],
6965
+ program = _ref10[1];
6928
6966
  return [name, createProgramInfoFromProgram(gl, program)];
6929
6967
  }));
6930
6968
  }
@@ -6999,14 +7037,15 @@ var createProgramInfosAsync = exports.createProgramInfosAsync = wrapCallbackFnTo
6999
7037
 
7000
7038
 
7001
7039
 
7002
- function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
7003
7040
  exports.__esModule = true;
7004
7041
  exports.canFilter = canFilter;
7005
7042
  exports.canGenerateMipmap = canGenerateMipmap;
7006
7043
  exports.createSampler = createSampler;
7007
7044
  exports.createSamplers = createSamplers;
7008
7045
  exports.createTexture = createTexture;
7046
+ exports.createTextureAsync = createTextureAsync;
7009
7047
  exports.createTextures = createTextures;
7048
+ exports.createTexturesAsync = createTexturesAsync;
7010
7049
  exports.getBytesPerElementForInternalFormat = getBytesPerElementForInternalFormat;
7011
7050
  exports.getFormatAndTypeForInternalFormat = getFormatAndTypeForInternalFormat;
7012
7051
  exports.getNumComponentsForFormat = getNumComponentsForFormat;
@@ -7025,7 +7064,12 @@ var typedArrays = _interopRequireWildcard(__webpack_require__(/*! ./typedarrays.
7025
7064
  var helper = _interopRequireWildcard(__webpack_require__(/*! ./helper.js */ "./src/helper.js"));
7026
7065
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
7027
7066
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
7028
- /*
7067
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
7068
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
7069
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
7070
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
7071
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
7072
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } /*
7029
7073
  * Copyright 2019 Gregg Tavares
7030
7074
  *
7031
7075
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -7046,7 +7090,6 @@ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e;
7046
7090
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
7047
7091
  * DEALINGS IN THE SOFTWARE.
7048
7092
  */
7049
-
7050
7093
  /**
7051
7094
  * Low level texture related functions
7052
7095
  *
@@ -8013,21 +8056,62 @@ function setDefaults(newDefaults) {
8013
8056
  */
8014
8057
 
8015
8058
  /**
8016
- * Sets any packing state that will be set based on the options.
8017
- * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
8059
+ * Saves the current packing state, sets the packing state as specified
8060
+ * then calls a function, after which the packing state will be restored.
8018
8061
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
8062
+ * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
8063
+ * @param {function():void} [fn] A function to call, after which the packing state will be restored.
8019
8064
  * @private
8020
8065
  */
8021
- function setPackState(gl, options) {
8066
+ function scopedSetPackState(gl, options, fn) {
8067
+ var colorspaceConversion;
8068
+ var premultiplyAlpha;
8069
+ var flipY;
8022
8070
  if (options.colorspaceConversion !== undefined) {
8071
+ colorspaceConversion = gl.getParameter(UNPACK_COLORSPACE_CONVERSION_WEBGL);
8023
8072
  gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, options.colorspaceConversion);
8024
8073
  }
8025
8074
  if (options.premultiplyAlpha !== undefined) {
8075
+ premultiplyAlpha = gl.getParameter(UNPACK_PREMULTIPLY_ALPHA_WEBGL);
8026
8076
  gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, options.premultiplyAlpha);
8027
8077
  }
8028
8078
  if (options.flipY !== undefined) {
8079
+ flipY = gl.getParameter(UNPACK_FLIP_Y_WEBGL);
8029
8080
  gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, options.flipY);
8030
8081
  }
8082
+ fn();
8083
+ if (colorspaceConversion !== undefined) {
8084
+ gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, colorspaceConversion);
8085
+ }
8086
+ if (premultiplyAlpha !== undefined) {
8087
+ gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
8088
+ }
8089
+ if (flipY !== undefined) {
8090
+ gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, flipY);
8091
+ }
8092
+ }
8093
+
8094
+ /**
8095
+ * returns the property if set or the corresponding state if undefined
8096
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
8097
+ * @param {module:twgl.TextureOptions} options
8098
+ * @param {string} property the name of the property to copy
8099
+ * @param {number} pname
8100
+ * @return {module:twgl.TextureOptions}
8101
+ */
8102
+ function getPackStateOption(gl, options, property, pname) {
8103
+ var v = options[property];
8104
+ return _defineProperty({}, property, v === undefined ? gl.getParameter(pname) : v);
8105
+ }
8106
+
8107
+ /**
8108
+ * Copy the options object and apply pack state
8109
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
8110
+ * @param {module:twgl.TextureOptions} options
8111
+ * @return {module:twgl.TextureOptions}
8112
+ */
8113
+ function copyOptionsAndApplyPackState(gl, options) {
8114
+ return _objectSpread(_objectSpread(_objectSpread(_objectSpread({}, options), getPackStateOption(gl, options, 'flipY', UNPACK_FLIP_Y_WEBGL)), getPackStateOption(gl, options, 'premultiplyAlpha', UNPACK_PREMULTIPLY_ALPHA_WEBGL)), getPackStateOption(gl, options, 'colorspaceConversion', UNPACK_COLORSPACE_CONVERSION_WEBGL));
8031
8115
  }
8032
8116
 
8033
8117
  /**
@@ -8311,7 +8395,6 @@ function setTextureFromElement(gl, tex, element, options) {
8311
8395
  var formatType = getFormatAndTypeForInternalFormat(internalFormat);
8312
8396
  var format = options.format || formatType.format;
8313
8397
  var type = options.type || formatType.type;
8314
- setPackState(gl, options);
8315
8398
  gl.bindTexture(target, tex);
8316
8399
  if (target === TEXTURE_CUBE_MAP) {
8317
8400
  // guess the parts
@@ -8344,15 +8427,17 @@ function setTextureFromElement(gl, tex, element, options) {
8344
8427
  ctx.canvas.height = size;
8345
8428
  width = size;
8346
8429
  height = size;
8347
- getCubeFacesWithNdx(gl, options).forEach(function (f) {
8348
- var xOffset = slices[f.ndx * 2 + 0] * size;
8349
- var yOffset = slices[f.ndx * 2 + 1] * size;
8350
- ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
8351
- gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
8430
+ scopedSetPackState(gl, options, function () {
8431
+ getCubeFacesWithNdx(gl, options).forEach(function (f) {
8432
+ var xOffset = slices[f.ndx * 2 + 0] * size;
8433
+ var yOffset = slices[f.ndx * 2 + 1] * size;
8434
+ ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
8435
+ gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
8436
+ });
8437
+ // Free up the canvas memory
8438
+ ctx.canvas.width = 1;
8439
+ ctx.canvas.height = 1;
8352
8440
  });
8353
- // Free up the canvas memory
8354
- ctx.canvas.width = 1;
8355
- ctx.canvas.height = 1;
8356
8441
  } else if (typeof createImageBitmap !== 'undefined') {
8357
8442
  // NOTE: It seems like we should prefer ImageBitmap because unlike canvas it's
8358
8443
  // note lossy? (alpha is not premultiplied? although I'm not sure what
@@ -8371,39 +8456,44 @@ function setTextureFromElement(gl, tex, element, options) {
8371
8456
  premultiplyAlpha: 'none',
8372
8457
  colorSpaceConversion: 'none'
8373
8458
  }).then(function (imageBitmap) {
8374
- setPackState(gl, options);
8375
- gl.bindTexture(target, tex);
8376
- gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
8377
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
8378
- setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
8379
- }
8459
+ scopedSetPackState(gl, options, function () {
8460
+ gl.bindTexture(target, tex);
8461
+ gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
8462
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
8463
+ setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
8464
+ }
8465
+ });
8380
8466
  });
8381
8467
  });
8382
8468
  }
8383
8469
  } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
8384
- var smallest = Math.min(element.width, element.height);
8385
- var largest = Math.max(element.width, element.height);
8386
- var depth = largest / smallest;
8387
- if (depth % 1 !== 0) {
8388
- throw "can not compute 3D dimensions of element";
8389
- }
8390
- var xMult = element.width === largest ? 1 : 0;
8391
- var yMult = element.height === largest ? 1 : 0;
8392
- gl.pixelStorei(UNPACK_ALIGNMENT, 1);
8393
- gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
8394
- gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
8395
- gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
8396
- gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
8397
- for (var d = 0; d < depth; ++d) {
8398
- var srcX = d * smallest * xMult;
8399
- var srcY = d * smallest * yMult;
8400
- gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
8401
- gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
8402
- gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
8403
- }
8404
- setSkipStateToDefault(gl);
8470
+ scopedSetPackState(gl, options, function () {
8471
+ var smallest = Math.min(element.width, element.height);
8472
+ var largest = Math.max(element.width, element.height);
8473
+ var depth = largest / smallest;
8474
+ if (depth % 1 !== 0) {
8475
+ throw "can not compute 3D dimensions of element";
8476
+ }
8477
+ var xMult = element.width === largest ? 1 : 0;
8478
+ var yMult = element.height === largest ? 1 : 0;
8479
+ gl.pixelStorei(UNPACK_ALIGNMENT, 1);
8480
+ gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
8481
+ gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
8482
+ gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
8483
+ gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
8484
+ for (var d = 0; d < depth; ++d) {
8485
+ var srcX = d * smallest * xMult;
8486
+ var srcY = d * smallest * yMult;
8487
+ gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
8488
+ gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
8489
+ gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
8490
+ }
8491
+ setSkipStateToDefault(gl);
8492
+ });
8405
8493
  } else {
8406
- gl.texImage2D(target, level, internalFormat, format, type, element);
8494
+ scopedSetPackState(gl, options, function () {
8495
+ gl.texImage2D(target, level, internalFormat, format, type, element);
8496
+ });
8407
8497
  }
8408
8498
  if (shouldAutomaticallySetTextureFilteringForSize(options)) {
8409
8499
  setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
@@ -8637,7 +8727,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
8637
8727
  options = options || defaults.textureOptions;
8638
8728
  setTextureTo1PixelColor(gl, tex, options);
8639
8729
  // Because it's async we need to copy the options.
8640
- options = Object.assign({}, options);
8730
+ options = copyOptionsAndApplyPackState(gl, options);
8641
8731
  var img = loadAndUseImage(options.src, options.crossOrigin, function (err, img) {
8642
8732
  if (err) {
8643
8733
  callback(err, tex, img);
@@ -8677,7 +8767,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
8677
8767
  }
8678
8768
  setTextureTo1PixelColor(gl, tex, options);
8679
8769
  // Because it's async we need to copy the options.
8680
- options = Object.assign({}, options);
8770
+ options = copyOptionsAndApplyPackState(gl, options);
8681
8771
  var numToLoad = 6;
8682
8772
  var errors = [];
8683
8773
  var faces = getCubeFaceOrder(gl, options);
@@ -8692,23 +8782,24 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
8692
8782
  if (img.width !== img.height) {
8693
8783
  errors.push("cubemap face img is not a square: " + img.src);
8694
8784
  } else {
8695
- setPackState(gl, options);
8696
- gl.bindTexture(target, tex);
8697
-
8698
- // So assuming this is the first image we now have one face that's img sized
8699
- // and 5 faces that are 1x1 pixel so size the other faces
8700
- if (numToLoad === 5) {
8701
- // use the default order
8702
- getCubeFaceOrder(gl).forEach(function (otherTarget) {
8703
- // Should we re-use the same face or a color?
8704
- gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
8705
- });
8706
- } else {
8707
- gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
8708
- }
8709
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
8710
- gl.generateMipmap(target);
8711
- }
8785
+ scopedSetPackState(gl, options, function () {
8786
+ gl.bindTexture(target, tex);
8787
+
8788
+ // So assuming this is the first image we now have one face that's img sized
8789
+ // and 5 faces that are 1x1 pixel so size the other faces
8790
+ if (numToLoad === 5) {
8791
+ // use the default order
8792
+ getCubeFaceOrder(gl).forEach(function (otherTarget) {
8793
+ // Should we re-use the same face or a color?
8794
+ gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
8795
+ });
8796
+ } else {
8797
+ gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
8798
+ }
8799
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
8800
+ gl.generateMipmap(target);
8801
+ }
8802
+ });
8712
8803
  }
8713
8804
  }
8714
8805
  if (numToLoad === 0) {
@@ -8754,7 +8845,7 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
8754
8845
  }
8755
8846
  setTextureTo1PixelColor(gl, tex, options);
8756
8847
  // Because it's async we need to copy the options.
8757
- options = Object.assign({}, options);
8848
+ options = copyOptionsAndApplyPackState(gl, options);
8758
8849
  var numToLoad = urls.length;
8759
8850
  var errors = [];
8760
8851
  var imgs; // eslint-disable-line
@@ -8769,40 +8860,41 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
8769
8860
  if (err) {
8770
8861
  errors.push(err);
8771
8862
  } else {
8772
- setPackState(gl, options);
8773
- gl.bindTexture(target, tex);
8774
- if (firstImage) {
8775
- firstImage = false;
8776
- width = options.width || img.width;
8777
- height = options.height || img.height;
8778
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
8779
-
8780
- // put it in every slice otherwise some slices will be 0,0,0,0
8781
- for (var s = 0; s < depth; ++s) {
8782
- gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
8783
- }
8784
- } else {
8785
- var src = img;
8786
- var ctx;
8787
- if (img.width !== width || img.height !== height) {
8788
- // Size the image to fix
8789
- ctx = getShared2DContext();
8790
- src = ctx.canvas;
8791
- ctx.canvas.width = width;
8792
- ctx.canvas.height = height;
8793
- ctx.drawImage(img, 0, 0, width, height);
8794
- }
8795
- gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
8863
+ scopedSetPackState(gl, options, function () {
8864
+ gl.bindTexture(target, tex);
8865
+ if (firstImage) {
8866
+ firstImage = false;
8867
+ width = options.width || img.width;
8868
+ height = options.height || img.height;
8869
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
8870
+
8871
+ // put it in every slice otherwise some slices will be 0,0,0,0
8872
+ for (var s = 0; s < depth; ++s) {
8873
+ gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
8874
+ }
8875
+ } else {
8876
+ var src = img;
8877
+ var ctx;
8878
+ if (img.width !== width || img.height !== height) {
8879
+ // Size the image to fix
8880
+ ctx = getShared2DContext();
8881
+ src = ctx.canvas;
8882
+ ctx.canvas.width = width;
8883
+ ctx.canvas.height = height;
8884
+ ctx.drawImage(img, 0, 0, width, height);
8885
+ }
8886
+ gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
8796
8887
 
8797
- // free the canvas memory
8798
- if (ctx && src === ctx.canvas) {
8799
- ctx.canvas.width = 0;
8800
- ctx.canvas.height = 0;
8888
+ // free the canvas memory
8889
+ if (ctx && src === ctx.canvas) {
8890
+ ctx.canvas.width = 0;
8891
+ ctx.canvas.height = 0;
8892
+ }
8801
8893
  }
8802
- }
8803
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
8804
- gl.generateMipmap(target);
8805
- }
8894
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
8895
+ gl.generateMipmap(target);
8896
+ }
8897
+ });
8806
8898
  }
8807
8899
  if (numToLoad === 0) {
8808
8900
  callback(errors.length ? errors : undefined, tex, imgs);
@@ -8877,20 +8969,21 @@ function setTextureFromArray(gl, tex, src, options) {
8877
8969
  }
8878
8970
  setSkipStateToDefault(gl);
8879
8971
  gl.pixelStorei(UNPACK_ALIGNMENT, options.unpackAlignment || 1);
8880
- setPackState(gl, options);
8881
- if (target === TEXTURE_CUBE_MAP) {
8882
- var elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
8883
- var faceSize = numElements / 6 * elementsPerElement;
8884
- getCubeFacesWithNdx(gl, options).forEach(function (f) {
8885
- var offset = faceSize * f.ndx;
8886
- var data = src.subarray(offset, offset + faceSize);
8887
- gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
8888
- });
8889
- } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
8890
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
8891
- } else {
8892
- gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
8893
- }
8972
+ scopedSetPackState(gl, options, function () {
8973
+ if (target === TEXTURE_CUBE_MAP) {
8974
+ var elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
8975
+ var faceSize = numElements / 6 * elementsPerElement;
8976
+ getCubeFacesWithNdx(gl, options).forEach(function (f) {
8977
+ var offset = faceSize * f.ndx;
8978
+ var data = src.subarray(offset, offset + faceSize);
8979
+ gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
8980
+ });
8981
+ } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
8982
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
8983
+ } else {
8984
+ gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
8985
+ }
8986
+ });
8894
8987
  return {
8895
8988
  width: width,
8896
8989
  height: height,
@@ -8915,24 +9008,46 @@ function setEmptyTexture(gl, tex, options) {
8915
9008
  var formatType = getFormatAndTypeForInternalFormat(internalFormat);
8916
9009
  var format = options.format || formatType.format;
8917
9010
  var type = options.type || formatType.type;
8918
- setPackState(gl, options);
8919
- if (target === TEXTURE_CUBE_MAP) {
8920
- for (var ii = 0; ii < 6; ++ii) {
8921
- gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
9011
+ scopedSetPackState(gl, options, function () {
9012
+ if (target === TEXTURE_CUBE_MAP) {
9013
+ for (var ii = 0; ii < 6; ++ii) {
9014
+ gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
9015
+ }
9016
+ } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
9017
+ gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
9018
+ } else {
9019
+ gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
8922
9020
  }
8923
- } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
8924
- gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
8925
- } else {
8926
- gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
8927
- }
9021
+ });
8928
9022
  }
8929
9023
 
8930
9024
  /**
8931
9025
  * Creates a texture based on the options passed in.
8932
9026
  *
9027
+ * See {@link module:twgl.TextureOptions}
9028
+ *
8933
9029
  * Note: may reset UNPACK_ALIGNMENT, UNPACK_ROW_LENGTH, UNPACK_IMAGE_HEIGHT, UNPACK_SKIP_IMAGES
8934
9030
  * UNPACK_SKIP_PIXELS, and UNPACK_SKIP_ROWS
8935
9031
  *
9032
+ * UNPACK_FLIP_Y_WEBGL, UNPACK_PREMULTIPLY_ALPHA_WEBGL, UNPACK_COLORSPACE_CONVERSION_WEBGL
9033
+ * are left as is though you can pass in options for flipY, premultiplyAlpha, and colorspaceConversion
9034
+ * to override them.
9035
+ *
9036
+ * As for the behavior of these settings
9037
+ *
9038
+ * ```js
9039
+ * gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
9040
+ * t1 = twgl.createTexture({src: someImage }); // flipped
9041
+ * t2 = twgl.createTexture({src: someImage, flipY: true }); // flipped
9042
+ * t3 = twgl.createTexture({src: someImage, flipY: false }); // not flipped
9043
+ * t4 = twgl.createTexture({src: someImage }); // flipped
9044
+ * ```
9045
+ *
9046
+ * * t1 is flipped because UNPACK_FLIP_Y_WEBGL is true
9047
+ * * t2 is flipped because it was requested
9048
+ * * t3 is not flipped because it was requested
9049
+ * * t4 is flipped because UNPACK_FLIP_Y_WEBGL has been restored to true
9050
+ *
8936
9051
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
8937
9052
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
8938
9053
  * @param {module:twgl.TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
@@ -8986,6 +9101,45 @@ function createTexture(gl, options, callback) {
8986
9101
  return tex;
8987
9102
  }
8988
9103
 
9104
+ /**
9105
+ * Value returned by createTextureAsync
9106
+ *
9107
+ * @typedef {Object} CreateTextureInfo
9108
+ * @param {WebGLTexture} texture the texture.
9109
+ * @param {module:twgl.TextureSrc} source image(s) used to as the src for the texture
9110
+ * @memberOf module:twgl
9111
+ */
9112
+
9113
+ /**
9114
+ * Creates a texture based on the options passed in.
9115
+ *
9116
+ * see {@link module:twgl/textures.createTexture}.
9117
+ * The only difference is this function returns a promise
9118
+ * where as the other returns a texture and takes a callback.
9119
+ *
9120
+ * Note: this is here for completeness. It is probably better to use
9121
+ * the non-async version as it returns a usable texture immediately
9122
+ * where as this one you have to wait for it to load.
9123
+ *
9124
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
9125
+ * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
9126
+ * @return {Promise<CreateTextureInfo>} The created texture and source.
9127
+ */
9128
+ function createTextureAsync(gl, options) {
9129
+ return new Promise(function (resolve, reject) {
9130
+ createTexture(gl, options, function (err, texture, source) {
9131
+ if (err) {
9132
+ reject(err);
9133
+ } else {
9134
+ resolve({
9135
+ texture: texture,
9136
+ source: source
9137
+ });
9138
+ }
9139
+ });
9140
+ });
9141
+ }
9142
+
8989
9143
  /**
8990
9144
  * Resizes a texture based on the options passed in.
8991
9145
  *
@@ -9158,6 +9312,45 @@ function createTextures(gl, textureOptions, callback) {
9158
9312
  return textures;
9159
9313
  }
9160
9314
 
9315
+ /**
9316
+ * Value returned by createTextureAsync
9317
+ *
9318
+ * @typedef {Object} CreateTexturesInfo
9319
+ * @param {Object.<string, WebGLTexture>} textures the created textures by name. Same as returned by {@link module:twgl.createTextures}.
9320
+ * @param {Object.<string, module:twgl.TextureSrc>} sources the image(s) used for the texture by name.
9321
+ * @memberOf module:twgl
9322
+ */
9323
+
9324
+ /**
9325
+ * Creates textures based on the options passed in.
9326
+ *
9327
+ * see {@link module:twgl/textures.createTextures}.
9328
+ * The only difference is this function returns a promise
9329
+ * where as the other returns a texture and takes a callback.
9330
+ *
9331
+ * Note: this is here for completeness. It is probably better to use
9332
+ * the non-async version as it returns usable textures immediately
9333
+ * where as this one you have to wait for them to load.
9334
+ *
9335
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
9336
+ * @param {Object.<string,module:twgl.TextureOptions>} options A object of TextureOptions one per texture.
9337
+ * @return {Promise<CreateTexturesInfo>} The created textures and sources.
9338
+ */
9339
+ function createTexturesAsync(gl, options) {
9340
+ return new Promise(function (resolve, reject) {
9341
+ createTexture(gl, options, function (err, textures, sources) {
9342
+ if (err) {
9343
+ reject(err);
9344
+ } else {
9345
+ resolve({
9346
+ textures: textures,
9347
+ sources: sources
9348
+ });
9349
+ }
9350
+ });
9351
+ });
9352
+ }
9353
+
9161
9354
  /***/ }),
9162
9355
 
9163
9356
  /***/ "./src/twgl-full.js":