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
  */
@@ -2433,7 +2433,7 @@ function deleteProgramAndShaders(gl, program, notThese) {
2433
2433
  try {
2434
2434
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
2435
2435
  var shader = _step.value;
2436
- if (notThese.has(shader)) {
2436
+ if (!notThese.has(shader)) {
2437
2437
  gl.deleteShader(shader);
2438
2438
  }
2439
2439
  }
@@ -2468,8 +2468,8 @@ function createProgramNoCheck(gl, shaders, programOptions) {
2468
2468
  shader = gl.createShader(type);
2469
2469
  gl.shaderSource(shader, prepShaderSource(src).shaderSource);
2470
2470
  gl.compileShader(shader);
2471
- gl.attachShader(program, shader);
2472
2471
  }
2472
+ gl.attachShader(program, shader);
2473
2473
  }
2474
2474
  Object.entries(attribLocations).forEach(function (_ref) {
2475
2475
  var _ref2 = _slicedToArray(_ref, 2),
@@ -3089,6 +3089,7 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
3089
3089
  * @property {ArrayBuffer} array The array buffer that contains the uniform values
3090
3090
  * @property {Float32Array} asFloat A float view on the array buffer. This is useful
3091
3091
  * inspecting the contents of the buffer in the debugger.
3092
+ * @property {Uint8Array} asUint8t A uint8 view on the array buffer.
3092
3093
  * @property {WebGLBuffer} buffer A WebGL buffer that will hold a copy of the uniform values for rendering.
3093
3094
  * @property {number} [offset] offset into buffer
3094
3095
  * @property {Object<string, ArrayBufferView>} uniforms A uniform name to ArrayBufferView map.
@@ -3107,6 +3108,15 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
3107
3108
  * @memberOf module:twgl
3108
3109
  */
3109
3110
 
3111
+ /**
3112
+ * Options to allow createUniformBlockInfo to use an existing buffer and arrayBuffer at an offset
3113
+ * @typedef {Object} UniformBlockInfoOptions
3114
+ * @property {ArrayBuffer} [array] an existing array buffer to use for values
3115
+ * @property {number} [offset] the offset in bytes to use in the array buffer (default = 0)
3116
+ * @property {WebGLBuffer} [buffer] the buffer to use for this uniform block info
3117
+ * @property {number} [bufferOffset] the offset in bytes in the buffer to use (default = use offset above)
3118
+ */
3119
+
3110
3120
  /**
3111
3121
  * Creates a `UniformBlockInfo` for the specified block
3112
3122
  *
@@ -3121,10 +3131,13 @@ function createUniformBlockUniformSetter(view, isArray, rows, cols) {
3121
3131
  * @param {module:twgl.UniformBlockSpec} uniformBlockSpec. A UniformBlockSpec as returned
3122
3132
  * from {@link module:twgl.createUniformBlockSpecFromProgram}.
3123
3133
  * @param {string} blockName The name of the block.
3134
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
3124
3135
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
3125
3136
  * @memberOf module:twgl/programs
3126
3137
  */
3127
3138
  function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockName) {
3139
+ var _options$offset, _options$array, _options$buffer, _options$bufferOffset;
3140
+ var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
3128
3141
  var blockSpecs = uniformBlockSpec.blockSpecs;
3129
3142
  var uniformData = uniformBlockSpec.uniformData;
3130
3143
  var blockSpec = blockSpecs[blockName];
@@ -3135,10 +3148,14 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3135
3148
  uniforms: {}
3136
3149
  };
3137
3150
  }
3138
- var array = new ArrayBuffer(blockSpec.size);
3139
- var buffer = gl.createBuffer();
3151
+ var offset = (_options$offset = options.offset) !== null && _options$offset !== void 0 ? _options$offset : 0;
3152
+ var array = (_options$array = options.array) !== null && _options$array !== void 0 ? _options$array : new ArrayBuffer(blockSpec.size);
3153
+ var buffer = (_options$buffer = options.buffer) !== null && _options$buffer !== void 0 ? _options$buffer : gl.createBuffer();
3140
3154
  var uniformBufferIndex = blockSpec.index;
3141
3155
  gl.bindBuffer(UNIFORM_BUFFER, buffer);
3156
+ if (!options.buffer) {
3157
+ gl.bufferData(UNIFORM_BUFFER, array.byteLength, DYNAMIC_DRAW);
3158
+ }
3142
3159
  gl.uniformBlockBinding(program, blockSpec.index, uniformBufferIndex);
3143
3160
  var prefix = blockName + ".";
3144
3161
  if (arraySuffixRE.test(prefix)) {
@@ -3160,7 +3177,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3160
3177
  var typeInfo = typeMap[data.type];
3161
3178
  var Type = typeInfo.Type;
3162
3179
  var byteLength = isArray ? pad(typeInfo.size, 16) * data.size : typeInfo.size * data.size;
3163
- var uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
3180
+ var uniformView = new Type(array, offset + data.offset, byteLength / Type.BYTES_PER_ELEMENT);
3164
3181
  uniforms[name] = uniformView;
3165
3182
  // Note: I'm not sure what to do here. The original
3166
3183
  // idea was to create TypedArray views into each part
@@ -3196,9 +3213,13 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3196
3213
  array: array,
3197
3214
  asFloat: new Float32Array(array),
3198
3215
  // for debugging
3216
+ asUint8: new Uint8Array(array),
3217
+ // needed for gl.bufferSubData because it doesn't take an array buffer
3199
3218
  buffer: buffer,
3200
3219
  uniforms: uniforms,
3201
- setters: setters
3220
+ setters: setters,
3221
+ offset: (_options$bufferOffset = options.bufferOffset) !== null && _options$bufferOffset !== void 0 ? _options$bufferOffset : offset,
3222
+ size: blockSpec.size
3202
3223
  };
3203
3224
  }
3204
3225
 
@@ -3215,11 +3236,13 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3215
3236
  * @param {module:twgl.ProgramInfo} programInfo a `ProgramInfo`
3216
3237
  * as returned from {@link module:twgl.createProgramInfo}
3217
3238
  * @param {string} blockName The name of the block.
3239
+ * @param {module:twgl.UniformBlockInfoOptions} [options] Optional options for using existing an existing buffer and arrayBuffer
3218
3240
  * @return {module:twgl.UniformBlockInfo} The created UniformBlockInfo
3219
3241
  * @memberOf module:twgl/programs
3220
3242
  */
3221
3243
  function createUniformBlockInfo(gl, programInfo, blockName) {
3222
- return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName);
3244
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
3245
+ return createUniformBlockInfoFromProgram(gl, programInfo.program, programInfo.uniformBlockSpec, blockName, options);
3223
3246
  }
3224
3247
 
3225
3248
  /**
@@ -3244,8 +3267,9 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
3244
3267
  var uniformBlockSpec = programInfo.uniformBlockSpec || programInfo;
3245
3268
  var blockSpec = uniformBlockSpec.blockSpecs[uniformBlockInfo.name];
3246
3269
  if (blockSpec) {
3270
+ var _uniformBlockInfo$siz;
3247
3271
  var bufferBindIndex = blockSpec.index;
3248
- gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, uniformBlockInfo.array.byteLength);
3272
+ gl.bindBufferRange(UNIFORM_BUFFER, bufferBindIndex, uniformBlockInfo.buffer, uniformBlockInfo.offset || 0, (_uniformBlockInfo$siz = uniformBlockInfo.size) !== null && _uniformBlockInfo$siz !== void 0 ? _uniformBlockInfo$siz : uniformBlockInfo.array.byteLength);
3249
3273
  return true;
3250
3274
  }
3251
3275
  return false;
@@ -3268,7 +3292,7 @@ function bindUniformBlock(gl, programInfo, uniformBlockInfo) {
3268
3292
  */
3269
3293
  function setUniformBlock(gl, programInfo, uniformBlockInfo) {
3270
3294
  if (bindUniformBlock(gl, programInfo, uniformBlockInfo)) {
3271
- gl.bufferData(UNIFORM_BUFFER, uniformBlockInfo.array, DYNAMIC_DRAW);
3295
+ gl.bufferSubData(UNIFORM_BUFFER, 0, uniformBlockInfo.asUint8, uniformBlockInfo.offset || 0, uniformBlockInfo.size || 0);
3272
3296
  }
3273
3297
  }
3274
3298
 
@@ -3710,6 +3734,8 @@ function setBuffersAndAttributes(gl, programInfo, buffers) {
3710
3734
  /**
3711
3735
  * @typedef {Object} ProgramInfo
3712
3736
  * @property {WebGLProgram} program A shader program
3737
+ * @property {Object<string, WebGLUniformLocation>} uniformLocations The uniform locations of each uniform
3738
+ * @property {Object<string, number>} attribLocations The locations of each attribute
3713
3739
  * @property {Object<string, function>} uniformSetters object of setters as returned from createUniformSetters,
3714
3740
  * @property {Object<string, function>} attribSetters object of setters as returned from createAttribSetters,
3715
3741
  * @property {module:twgl.UniformBlockSpec} [uniformBlockSpec] a uniform block spec for making UniformBlockInfos with createUniformBlockInfo etc..
@@ -3740,7 +3766,19 @@ function createProgramInfoFromProgram(gl, program) {
3740
3766
  var programInfo = {
3741
3767
  program: program,
3742
3768
  uniformSetters: uniformSetters,
3743
- attribSetters: attribSetters
3769
+ attribSetters: attribSetters,
3770
+ uniformLocations: Object.fromEntries(Object.entries(uniformSetters).map(function (_ref3) {
3771
+ var _ref4 = _slicedToArray(_ref3, 2),
3772
+ k = _ref4[0],
3773
+ v = _ref4[1];
3774
+ return [k, v.location];
3775
+ })),
3776
+ attribLocations: Object.fromEntries(Object.entries(attribSetters).map(function (_ref5) {
3777
+ var _ref6 = _slicedToArray(_ref5, 2),
3778
+ k = _ref6[0],
3779
+ v = _ref6[1];
3780
+ return [k, v.location];
3781
+ }))
3744
3782
  };
3745
3783
  if (utils.isWebGL2(gl)) {
3746
3784
  programInfo.uniformBlockSpec = createUniformBlockSpecFromProgram(gl, program);
@@ -3883,10 +3921,10 @@ function createPrograms(gl, programSpecs) {
3883
3921
  var noDeleteShadersSet = new Set();
3884
3922
 
3885
3923
  // compile and link everything
3886
- var programs = Object.fromEntries(Object.entries(programSpecs).map(function (_ref3) {
3887
- var _ref4 = _slicedToArray(_ref3, 2),
3888
- name = _ref4[0],
3889
- spec = _ref4[1];
3924
+ var programs = Object.fromEntries(Object.entries(programSpecs).map(function (_ref7) {
3925
+ var _ref8 = _slicedToArray(_ref7, 2),
3926
+ name = _ref8[0],
3927
+ spec = _ref8[1];
3890
3928
  var options = _objectSpread({}, programOptions);
3891
3929
  var shaders = Array.isArray(spec) ? spec : spec.shaders;
3892
3930
  if (!Array.isArray(spec)) {
@@ -3946,10 +3984,10 @@ function createPrograms(gl, programSpecs) {
3946
3984
  function createProgramInfos(gl, programSpecs, programOptions) {
3947
3985
  programOptions = getProgramOptions(programOptions);
3948
3986
  function createProgramInfosForPrograms(gl, programs) {
3949
- return Object.fromEntries(Object.entries(programs).map(function (_ref5) {
3950
- var _ref6 = _slicedToArray(_ref5, 2),
3951
- name = _ref6[0],
3952
- program = _ref6[1];
3987
+ return Object.fromEntries(Object.entries(programs).map(function (_ref9) {
3988
+ var _ref10 = _slicedToArray(_ref9, 2),
3989
+ name = _ref10[0],
3990
+ program = _ref10[1];
3953
3991
  return [name, createProgramInfoFromProgram(gl, program)];
3954
3992
  }));
3955
3993
  }
@@ -4024,14 +4062,15 @@ var createProgramInfosAsync = exports.createProgramInfosAsync = wrapCallbackFnTo
4024
4062
 
4025
4063
 
4026
4064
 
4027
- 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); }
4028
4065
  exports.__esModule = true;
4029
4066
  exports.canFilter = canFilter;
4030
4067
  exports.canGenerateMipmap = canGenerateMipmap;
4031
4068
  exports.createSampler = createSampler;
4032
4069
  exports.createSamplers = createSamplers;
4033
4070
  exports.createTexture = createTexture;
4071
+ exports.createTextureAsync = createTextureAsync;
4034
4072
  exports.createTextures = createTextures;
4073
+ exports.createTexturesAsync = createTexturesAsync;
4035
4074
  exports.getBytesPerElementForInternalFormat = getBytesPerElementForInternalFormat;
4036
4075
  exports.getFormatAndTypeForInternalFormat = getFormatAndTypeForInternalFormat;
4037
4076
  exports.getNumComponentsForFormat = getNumComponentsForFormat;
@@ -4050,7 +4089,12 @@ var typedArrays = _interopRequireWildcard(__webpack_require__(/*! ./typedarrays.
4050
4089
  var helper = _interopRequireWildcard(__webpack_require__(/*! ./helper.js */ "./src/helper.js"));
4051
4090
  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); }
4052
4091
  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; }
4053
- /*
4092
+ 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); }
4093
+ 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; }
4094
+ 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; }
4095
+ 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; }
4096
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
4097
+ 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); } /*
4054
4098
  * Copyright 2019 Gregg Tavares
4055
4099
  *
4056
4100
  * Permission is hereby granted, free of charge, to any person obtaining a
@@ -4071,7 +4115,6 @@ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e;
4071
4115
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
4072
4116
  * DEALINGS IN THE SOFTWARE.
4073
4117
  */
4074
-
4075
4118
  /**
4076
4119
  * Low level texture related functions
4077
4120
  *
@@ -5038,21 +5081,62 @@ function setDefaults(newDefaults) {
5038
5081
  */
5039
5082
 
5040
5083
  /**
5041
- * Sets any packing state that will be set based on the options.
5042
- * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5084
+ * Saves the current packing state, sets the packing state as specified
5085
+ * then calls a function, after which the packing state will be restored.
5043
5086
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5087
+ * @param {module:twgl.TextureOptions} options A TextureOptions object with whatever parameters you want set.
5088
+ * @param {function():void} [fn] A function to call, after which the packing state will be restored.
5044
5089
  * @private
5045
5090
  */
5046
- function setPackState(gl, options) {
5091
+ function scopedSetPackState(gl, options, fn) {
5092
+ var colorspaceConversion;
5093
+ var premultiplyAlpha;
5094
+ var flipY;
5047
5095
  if (options.colorspaceConversion !== undefined) {
5096
+ colorspaceConversion = gl.getParameter(UNPACK_COLORSPACE_CONVERSION_WEBGL);
5048
5097
  gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, options.colorspaceConversion);
5049
5098
  }
5050
5099
  if (options.premultiplyAlpha !== undefined) {
5100
+ premultiplyAlpha = gl.getParameter(UNPACK_PREMULTIPLY_ALPHA_WEBGL);
5051
5101
  gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, options.premultiplyAlpha);
5052
5102
  }
5053
5103
  if (options.flipY !== undefined) {
5104
+ flipY = gl.getParameter(UNPACK_FLIP_Y_WEBGL);
5054
5105
  gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, options.flipY);
5055
5106
  }
5107
+ fn();
5108
+ if (colorspaceConversion !== undefined) {
5109
+ gl.pixelStorei(UNPACK_COLORSPACE_CONVERSION_WEBGL, colorspaceConversion);
5110
+ }
5111
+ if (premultiplyAlpha !== undefined) {
5112
+ gl.pixelStorei(UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
5113
+ }
5114
+ if (flipY !== undefined) {
5115
+ gl.pixelStorei(UNPACK_FLIP_Y_WEBGL, flipY);
5116
+ }
5117
+ }
5118
+
5119
+ /**
5120
+ * returns the property if set or the corresponding state if undefined
5121
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5122
+ * @param {module:twgl.TextureOptions} options
5123
+ * @param {string} property the name of the property to copy
5124
+ * @param {number} pname
5125
+ * @return {module:twgl.TextureOptions}
5126
+ */
5127
+ function getPackStateOption(gl, options, property, pname) {
5128
+ var v = options[property];
5129
+ return _defineProperty({}, property, v === undefined ? gl.getParameter(pname) : v);
5130
+ }
5131
+
5132
+ /**
5133
+ * Copy the options object and apply pack state
5134
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5135
+ * @param {module:twgl.TextureOptions} options
5136
+ * @return {module:twgl.TextureOptions}
5137
+ */
5138
+ function copyOptionsAndApplyPackState(gl, options) {
5139
+ 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));
5056
5140
  }
5057
5141
 
5058
5142
  /**
@@ -5336,7 +5420,6 @@ function setTextureFromElement(gl, tex, element, options) {
5336
5420
  var formatType = getFormatAndTypeForInternalFormat(internalFormat);
5337
5421
  var format = options.format || formatType.format;
5338
5422
  var type = options.type || formatType.type;
5339
- setPackState(gl, options);
5340
5423
  gl.bindTexture(target, tex);
5341
5424
  if (target === TEXTURE_CUBE_MAP) {
5342
5425
  // guess the parts
@@ -5369,15 +5452,17 @@ function setTextureFromElement(gl, tex, element, options) {
5369
5452
  ctx.canvas.height = size;
5370
5453
  width = size;
5371
5454
  height = size;
5372
- getCubeFacesWithNdx(gl, options).forEach(function (f) {
5373
- var xOffset = slices[f.ndx * 2 + 0] * size;
5374
- var yOffset = slices[f.ndx * 2 + 1] * size;
5375
- ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
5376
- gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
5455
+ scopedSetPackState(gl, options, function () {
5456
+ getCubeFacesWithNdx(gl, options).forEach(function (f) {
5457
+ var xOffset = slices[f.ndx * 2 + 0] * size;
5458
+ var yOffset = slices[f.ndx * 2 + 1] * size;
5459
+ ctx.drawImage(element, xOffset, yOffset, size, size, 0, 0, size, size);
5460
+ gl.texImage2D(f.face, level, internalFormat, format, type, ctx.canvas);
5461
+ });
5462
+ // Free up the canvas memory
5463
+ ctx.canvas.width = 1;
5464
+ ctx.canvas.height = 1;
5377
5465
  });
5378
- // Free up the canvas memory
5379
- ctx.canvas.width = 1;
5380
- ctx.canvas.height = 1;
5381
5466
  } else if (typeof createImageBitmap !== 'undefined') {
5382
5467
  // NOTE: It seems like we should prefer ImageBitmap because unlike canvas it's
5383
5468
  // note lossy? (alpha is not premultiplied? although I'm not sure what
@@ -5396,39 +5481,44 @@ function setTextureFromElement(gl, tex, element, options) {
5396
5481
  premultiplyAlpha: 'none',
5397
5482
  colorSpaceConversion: 'none'
5398
5483
  }).then(function (imageBitmap) {
5399
- setPackState(gl, options);
5400
- gl.bindTexture(target, tex);
5401
- gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
5402
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5403
- setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
5404
- }
5484
+ scopedSetPackState(gl, options, function () {
5485
+ gl.bindTexture(target, tex);
5486
+ gl.texImage2D(f.face, level, internalFormat, format, type, imageBitmap);
5487
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5488
+ setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
5489
+ }
5490
+ });
5405
5491
  });
5406
5492
  });
5407
5493
  }
5408
5494
  } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
5409
- var smallest = Math.min(element.width, element.height);
5410
- var largest = Math.max(element.width, element.height);
5411
- var depth = largest / smallest;
5412
- if (depth % 1 !== 0) {
5413
- throw "can not compute 3D dimensions of element";
5414
- }
5415
- var xMult = element.width === largest ? 1 : 0;
5416
- var yMult = element.height === largest ? 1 : 0;
5417
- gl.pixelStorei(UNPACK_ALIGNMENT, 1);
5418
- gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
5419
- gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
5420
- gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
5421
- gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
5422
- for (var d = 0; d < depth; ++d) {
5423
- var srcX = d * smallest * xMult;
5424
- var srcY = d * smallest * yMult;
5425
- gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
5426
- gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
5427
- gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
5428
- }
5429
- setSkipStateToDefault(gl);
5495
+ scopedSetPackState(gl, options, function () {
5496
+ var smallest = Math.min(element.width, element.height);
5497
+ var largest = Math.max(element.width, element.height);
5498
+ var depth = largest / smallest;
5499
+ if (depth % 1 !== 0) {
5500
+ throw "can not compute 3D dimensions of element";
5501
+ }
5502
+ var xMult = element.width === largest ? 1 : 0;
5503
+ var yMult = element.height === largest ? 1 : 0;
5504
+ gl.pixelStorei(UNPACK_ALIGNMENT, 1);
5505
+ gl.pixelStorei(UNPACK_ROW_LENGTH, element.width);
5506
+ gl.pixelStorei(UNPACK_IMAGE_HEIGHT, 0);
5507
+ gl.pixelStorei(UNPACK_SKIP_IMAGES, 0);
5508
+ gl.texImage3D(target, level, internalFormat, smallest, smallest, smallest, 0, format, type, null);
5509
+ for (var d = 0; d < depth; ++d) {
5510
+ var srcX = d * smallest * xMult;
5511
+ var srcY = d * smallest * yMult;
5512
+ gl.pixelStorei(UNPACK_SKIP_PIXELS, srcX);
5513
+ gl.pixelStorei(UNPACK_SKIP_ROWS, srcY);
5514
+ gl.texSubImage3D(target, level, 0, 0, d, smallest, smallest, 1, format, type, element);
5515
+ }
5516
+ setSkipStateToDefault(gl);
5517
+ });
5430
5518
  } else {
5431
- gl.texImage2D(target, level, internalFormat, format, type, element);
5519
+ scopedSetPackState(gl, options, function () {
5520
+ gl.texImage2D(target, level, internalFormat, format, type, element);
5521
+ });
5432
5522
  }
5433
5523
  if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5434
5524
  setTextureFilteringForSize(gl, tex, options, width, height, internalFormat);
@@ -5662,7 +5752,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
5662
5752
  options = options || defaults.textureOptions;
5663
5753
  setTextureTo1PixelColor(gl, tex, options);
5664
5754
  // Because it's async we need to copy the options.
5665
- options = Object.assign({}, options);
5755
+ options = copyOptionsAndApplyPackState(gl, options);
5666
5756
  var img = loadAndUseImage(options.src, options.crossOrigin, function (err, img) {
5667
5757
  if (err) {
5668
5758
  callback(err, tex, img);
@@ -5702,7 +5792,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
5702
5792
  }
5703
5793
  setTextureTo1PixelColor(gl, tex, options);
5704
5794
  // Because it's async we need to copy the options.
5705
- options = Object.assign({}, options);
5795
+ options = copyOptionsAndApplyPackState(gl, options);
5706
5796
  var numToLoad = 6;
5707
5797
  var errors = [];
5708
5798
  var faces = getCubeFaceOrder(gl, options);
@@ -5717,23 +5807,24 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
5717
5807
  if (img.width !== img.height) {
5718
5808
  errors.push("cubemap face img is not a square: " + img.src);
5719
5809
  } else {
5720
- setPackState(gl, options);
5721
- gl.bindTexture(target, tex);
5722
-
5723
- // So assuming this is the first image we now have one face that's img sized
5724
- // and 5 faces that are 1x1 pixel so size the other faces
5725
- if (numToLoad === 5) {
5726
- // use the default order
5727
- getCubeFaceOrder(gl).forEach(function (otherTarget) {
5728
- // Should we re-use the same face or a color?
5729
- gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
5730
- });
5731
- } else {
5732
- gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
5733
- }
5734
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5735
- gl.generateMipmap(target);
5736
- }
5810
+ scopedSetPackState(gl, options, function () {
5811
+ gl.bindTexture(target, tex);
5812
+
5813
+ // So assuming this is the first image we now have one face that's img sized
5814
+ // and 5 faces that are 1x1 pixel so size the other faces
5815
+ if (numToLoad === 5) {
5816
+ // use the default order
5817
+ getCubeFaceOrder(gl).forEach(function (otherTarget) {
5818
+ // Should we re-use the same face or a color?
5819
+ gl.texImage2D(otherTarget, level, internalFormat, format, type, img);
5820
+ });
5821
+ } else {
5822
+ gl.texImage2D(faceTarget, level, internalFormat, format, type, img);
5823
+ }
5824
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5825
+ gl.generateMipmap(target);
5826
+ }
5827
+ });
5737
5828
  }
5738
5829
  }
5739
5830
  if (numToLoad === 0) {
@@ -5779,7 +5870,7 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
5779
5870
  }
5780
5871
  setTextureTo1PixelColor(gl, tex, options);
5781
5872
  // Because it's async we need to copy the options.
5782
- options = Object.assign({}, options);
5873
+ options = copyOptionsAndApplyPackState(gl, options);
5783
5874
  var numToLoad = urls.length;
5784
5875
  var errors = [];
5785
5876
  var imgs; // eslint-disable-line
@@ -5794,40 +5885,41 @@ function loadSlicesFromUrls(gl, tex, options, callback) {
5794
5885
  if (err) {
5795
5886
  errors.push(err);
5796
5887
  } else {
5797
- setPackState(gl, options);
5798
- gl.bindTexture(target, tex);
5799
- if (firstImage) {
5800
- firstImage = false;
5801
- width = options.width || img.width;
5802
- height = options.height || img.height;
5803
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
5804
-
5805
- // put it in every slice otherwise some slices will be 0,0,0,0
5806
- for (var s = 0; s < depth; ++s) {
5807
- gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
5808
- }
5809
- } else {
5810
- var src = img;
5811
- var ctx;
5812
- if (img.width !== width || img.height !== height) {
5813
- // Size the image to fix
5814
- ctx = getShared2DContext();
5815
- src = ctx.canvas;
5816
- ctx.canvas.width = width;
5817
- ctx.canvas.height = height;
5818
- ctx.drawImage(img, 0, 0, width, height);
5819
- }
5820
- gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
5888
+ scopedSetPackState(gl, options, function () {
5889
+ gl.bindTexture(target, tex);
5890
+ if (firstImage) {
5891
+ firstImage = false;
5892
+ width = options.width || img.width;
5893
+ height = options.height || img.height;
5894
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, null);
5895
+
5896
+ // put it in every slice otherwise some slices will be 0,0,0,0
5897
+ for (var s = 0; s < depth; ++s) {
5898
+ gl.texSubImage3D(target, level, 0, 0, s, width, height, 1, format, type, img);
5899
+ }
5900
+ } else {
5901
+ var src = img;
5902
+ var ctx;
5903
+ if (img.width !== width || img.height !== height) {
5904
+ // Size the image to fix
5905
+ ctx = getShared2DContext();
5906
+ src = ctx.canvas;
5907
+ ctx.canvas.width = width;
5908
+ ctx.canvas.height = height;
5909
+ ctx.drawImage(img, 0, 0, width, height);
5910
+ }
5911
+ gl.texSubImage3D(target, level, 0, 0, slice, width, height, 1, format, type, src);
5821
5912
 
5822
- // free the canvas memory
5823
- if (ctx && src === ctx.canvas) {
5824
- ctx.canvas.width = 0;
5825
- ctx.canvas.height = 0;
5913
+ // free the canvas memory
5914
+ if (ctx && src === ctx.canvas) {
5915
+ ctx.canvas.width = 0;
5916
+ ctx.canvas.height = 0;
5917
+ }
5826
5918
  }
5827
- }
5828
- if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5829
- gl.generateMipmap(target);
5830
- }
5919
+ if (shouldAutomaticallySetTextureFilteringForSize(options)) {
5920
+ gl.generateMipmap(target);
5921
+ }
5922
+ });
5831
5923
  }
5832
5924
  if (numToLoad === 0) {
5833
5925
  callback(errors.length ? errors : undefined, tex, imgs);
@@ -5902,20 +5994,21 @@ function setTextureFromArray(gl, tex, src, options) {
5902
5994
  }
5903
5995
  setSkipStateToDefault(gl);
5904
5996
  gl.pixelStorei(UNPACK_ALIGNMENT, options.unpackAlignment || 1);
5905
- setPackState(gl, options);
5906
- if (target === TEXTURE_CUBE_MAP) {
5907
- var elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
5908
- var faceSize = numElements / 6 * elementsPerElement;
5909
- getCubeFacesWithNdx(gl, options).forEach(function (f) {
5910
- var offset = faceSize * f.ndx;
5911
- var data = src.subarray(offset, offset + faceSize);
5912
- gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
5913
- });
5914
- } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
5915
- gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
5916
- } else {
5917
- gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
5918
- }
5997
+ scopedSetPackState(gl, options, function () {
5998
+ if (target === TEXTURE_CUBE_MAP) {
5999
+ var elementsPerElement = bytesPerElement / src.BYTES_PER_ELEMENT;
6000
+ var faceSize = numElements / 6 * elementsPerElement;
6001
+ getCubeFacesWithNdx(gl, options).forEach(function (f) {
6002
+ var offset = faceSize * f.ndx;
6003
+ var data = src.subarray(offset, offset + faceSize);
6004
+ gl.texImage2D(f.face, level, internalFormat, width, height, 0, format, type, data);
6005
+ });
6006
+ } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
6007
+ gl.texImage3D(target, level, internalFormat, width, height, depth, 0, format, type, src);
6008
+ } else {
6009
+ gl.texImage2D(target, level, internalFormat, width, height, 0, format, type, src);
6010
+ }
6011
+ });
5919
6012
  return {
5920
6013
  width: width,
5921
6014
  height: height,
@@ -5940,24 +6033,46 @@ function setEmptyTexture(gl, tex, options) {
5940
6033
  var formatType = getFormatAndTypeForInternalFormat(internalFormat);
5941
6034
  var format = options.format || formatType.format;
5942
6035
  var type = options.type || formatType.type;
5943
- setPackState(gl, options);
5944
- if (target === TEXTURE_CUBE_MAP) {
5945
- for (var ii = 0; ii < 6; ++ii) {
5946
- gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6036
+ scopedSetPackState(gl, options, function () {
6037
+ if (target === TEXTURE_CUBE_MAP) {
6038
+ for (var ii = 0; ii < 6; ++ii) {
6039
+ gl.texImage2D(TEXTURE_CUBE_MAP_POSITIVE_X + ii, level, internalFormat, options.width, options.height, 0, format, type, null);
6040
+ }
6041
+ } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
6042
+ gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
6043
+ } else {
6044
+ gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
5947
6045
  }
5948
- } else if (target === TEXTURE_3D || target === TEXTURE_2D_ARRAY) {
5949
- gl.texImage3D(target, level, internalFormat, options.width, options.height, options.depth, 0, format, type, null);
5950
- } else {
5951
- gl.texImage2D(target, level, internalFormat, options.width, options.height, 0, format, type, null);
5952
- }
6046
+ });
5953
6047
  }
5954
6048
 
5955
6049
  /**
5956
6050
  * Creates a texture based on the options passed in.
5957
6051
  *
6052
+ * See {@link module:twgl.TextureOptions}
6053
+ *
5958
6054
  * Note: may reset UNPACK_ALIGNMENT, UNPACK_ROW_LENGTH, UNPACK_IMAGE_HEIGHT, UNPACK_SKIP_IMAGES
5959
6055
  * UNPACK_SKIP_PIXELS, and UNPACK_SKIP_ROWS
5960
6056
  *
6057
+ * UNPACK_FLIP_Y_WEBGL, UNPACK_PREMULTIPLY_ALPHA_WEBGL, UNPACK_COLORSPACE_CONVERSION_WEBGL
6058
+ * are left as is though you can pass in options for flipY, premultiplyAlpha, and colorspaceConversion
6059
+ * to override them.
6060
+ *
6061
+ * As for the behavior of these settings
6062
+ *
6063
+ * ```js
6064
+ * gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
6065
+ * t1 = twgl.createTexture({src: someImage }); // flipped
6066
+ * t2 = twgl.createTexture({src: someImage, flipY: true }); // flipped
6067
+ * t3 = twgl.createTexture({src: someImage, flipY: false }); // not flipped
6068
+ * t4 = twgl.createTexture({src: someImage }); // flipped
6069
+ * ```
6070
+ *
6071
+ * * t1 is flipped because UNPACK_FLIP_Y_WEBGL is true
6072
+ * * t2 is flipped because it was requested
6073
+ * * t3 is not flipped because it was requested
6074
+ * * t4 is flipped because UNPACK_FLIP_Y_WEBGL has been restored to true
6075
+ *
5961
6076
  * @param {WebGLRenderingContext} gl the WebGLRenderingContext
5962
6077
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
5963
6078
  * @param {module:twgl.TextureReadyCallback} [callback] A callback called when an image has been downloaded and uploaded to the texture.
@@ -6011,6 +6126,45 @@ function createTexture(gl, options, callback) {
6011
6126
  return tex;
6012
6127
  }
6013
6128
 
6129
+ /**
6130
+ * Value returned by createTextureAsync
6131
+ *
6132
+ * @typedef {Object} CreateTextureInfo
6133
+ * @param {WebGLTexture} texture the texture.
6134
+ * @param {module:twgl.TextureSrc} source image(s) used to as the src for the texture
6135
+ * @memberOf module:twgl
6136
+ */
6137
+
6138
+ /**
6139
+ * Creates a texture based on the options passed in.
6140
+ *
6141
+ * see {@link module:twgl/textures.createTexture}.
6142
+ * The only difference is this function returns a promise
6143
+ * where as the other returns a texture and takes a callback.
6144
+ *
6145
+ * Note: this is here for completeness. It is probably better to use
6146
+ * the non-async version as it returns a usable texture immediately
6147
+ * where as this one you have to wait for it to load.
6148
+ *
6149
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6150
+ * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6151
+ * @return {Promise<CreateTextureInfo>} The created texture and source.
6152
+ */
6153
+ function createTextureAsync(gl, options) {
6154
+ return new Promise(function (resolve, reject) {
6155
+ createTexture(gl, options, function (err, texture, source) {
6156
+ if (err) {
6157
+ reject(err);
6158
+ } else {
6159
+ resolve({
6160
+ texture: texture,
6161
+ source: source
6162
+ });
6163
+ }
6164
+ });
6165
+ });
6166
+ }
6167
+
6014
6168
  /**
6015
6169
  * Resizes a texture based on the options passed in.
6016
6170
  *
@@ -6183,6 +6337,45 @@ function createTextures(gl, textureOptions, callback) {
6183
6337
  return textures;
6184
6338
  }
6185
6339
 
6340
+ /**
6341
+ * Value returned by createTextureAsync
6342
+ *
6343
+ * @typedef {Object} CreateTexturesInfo
6344
+ * @param {Object.<string, WebGLTexture>} textures the created textures by name. Same as returned by {@link module:twgl.createTextures}.
6345
+ * @param {Object.<string, module:twgl.TextureSrc>} sources the image(s) used for the texture by name.
6346
+ * @memberOf module:twgl
6347
+ */
6348
+
6349
+ /**
6350
+ * Creates textures based on the options passed in.
6351
+ *
6352
+ * see {@link module:twgl/textures.createTextures}.
6353
+ * The only difference is this function returns a promise
6354
+ * where as the other returns a texture and takes a callback.
6355
+ *
6356
+ * Note: this is here for completeness. It is probably better to use
6357
+ * the non-async version as it returns usable textures immediately
6358
+ * where as this one you have to wait for them to load.
6359
+ *
6360
+ * @param {WebGLRenderingContext} gl the WebGLRenderingContext
6361
+ * @param {Object.<string,module:twgl.TextureOptions>} options A object of TextureOptions one per texture.
6362
+ * @return {Promise<CreateTexturesInfo>} The created textures and sources.
6363
+ */
6364
+ function createTexturesAsync(gl, options) {
6365
+ return new Promise(function (resolve, reject) {
6366
+ createTexture(gl, options, function (err, textures, sources) {
6367
+ if (err) {
6368
+ reject(err);
6369
+ } else {
6370
+ resolve({
6371
+ textures: textures,
6372
+ sources: sources
6373
+ });
6374
+ }
6375
+ });
6376
+ });
6377
+ }
6378
+
6186
6379
  /***/ }),
6187
6380
 
6188
6381
  /***/ "./src/twgl-base.js":