twgl.js 4.22.1 → 4.23.2

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 4.22.1 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
1
+ /* @license twgl.js 4.23.2 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
  /*
@@ -548,6 +548,15 @@ function negate$1(m, dst) {
548
548
  return dst;
549
549
  }
550
550
 
551
+ /**
552
+ * Creates a matrix.
553
+ * @return {module:twgl/m4.Mat4} A new matrix.
554
+ * @memberOf module:twgl/m4
555
+ */
556
+ function create$1() {
557
+ return new MatType(16).fill(0);
558
+ }
559
+
551
560
  /**
552
561
  * Copies a matrix.
553
562
  * @param {module:twgl/m4.Mat4} m The matrix.
@@ -1717,6 +1726,7 @@ var m4 = /*#__PURE__*/Object.freeze({
1717
1726
  axisRotate: axisRotate,
1718
1727
  axisRotation: axisRotation,
1719
1728
  copy: copy$1,
1729
+ create: create$1,
1720
1730
  frustum: frustum,
1721
1731
  getAxis: getAxis,
1722
1732
  getTranslation: getTranslation,
@@ -2421,6 +2431,9 @@ function getNumElementsFromNonIndexedArrays(arrays) {
2421
2431
  }
2422
2432
  const array = arrays[key];
2423
2433
  const length = getArray(array).length;
2434
+ if (length === undefined) {
2435
+ return 1; // There's no arrays
2436
+ }
2424
2437
  const numComponents = getNumComponents(array, key);
2425
2438
  const numElements = length / numComponents;
2426
2439
  if (length % numComponents > 0) {
@@ -2446,6 +2459,9 @@ function getNumElementsFromAttributes(gl, attribs) {
2446
2459
  key = Object.keys(attribs)[0];
2447
2460
  }
2448
2461
  const attrib = attribs[key];
2462
+ if (!attrib.buffer) {
2463
+ return 1; // There's no buffer
2464
+ }
2449
2465
  gl.bindBuffer(ARRAY_BUFFER, attrib.buffer);
2450
2466
  const numBytes = gl.getBufferParameter(ARRAY_BUFFER, BUFFER_SIZE);
2451
2467
  gl.bindBuffer(ARRAY_BUFFER, null);
@@ -6078,6 +6094,7 @@ function loadAndUseImage(obj, crossOrigin, callback) {
6078
6094
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
6079
6095
  * This is often the same options you passed in when you created the texture.
6080
6096
  * @memberOf module:twgl/textures
6097
+ * @private
6081
6098
  */
6082
6099
  function setTextureTo1PixelColor(gl, tex, options) {
6083
6100
  options = options || defaults$1.textureOptions;
@@ -6187,6 +6204,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
6187
6204
  * @param {module:twgl.CubemapReadyCallback} [callback] A function to be called when all the images have finished loading. err will
6188
6205
  * be non null if there was an error.
6189
6206
  * @memberOf module:twgl/textures
6207
+ * @private
6190
6208
  */
6191
6209
  function loadCubemapFromUrls(gl, tex, options, callback) {
6192
6210
  callback = callback || noop;
@@ -6270,6 +6288,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
6270
6288
  * @param {module:twgl.ThreeDReadyCallback} [callback] A function to be called when all the images have finished loading. err will
6271
6289
  * be non null if there was an error.
6272
6290
  * @memberOf module:twgl/textures
6291
+ * @private
6273
6292
  */
6274
6293
  function loadSlicesFromUrls(gl, tex, options, callback) {
6275
6294
  callback = callback || noop;
@@ -6848,6 +6867,7 @@ const typeMap = {};
6848
6867
 
6849
6868
  /**
6850
6869
  * Returns the corresponding bind point for a given sampler type
6870
+ * @private
6851
6871
  */
6852
6872
  function getBindPointForSamplerType(gl, type) {
6853
6873
  return typeMap[type].bindPoint;
@@ -7243,56 +7263,103 @@ function addLineNumbersWithError(src, log = '', lineOffset = 0) {
7243
7263
  * @memberOf module:twgl
7244
7264
  */
7245
7265
 
7266
+ /**
7267
+ * Program Callback
7268
+ * @callback ProgramCallback
7269
+ * @param {string} [err] error message, falsy if no error
7270
+ * @param {WebGLProgram|module:twgl.ProgramInfo} [result] the program or programInfo
7271
+ */
7272
+
7246
7273
  const spaceRE = /^[ \t]*\n/;
7247
7274
 
7275
+ /**
7276
+ * Remove the first end of line because WebGL 2.0 requires
7277
+ * #version 300 es
7278
+ * as the first line. No whitespace allowed before that line
7279
+ * so
7280
+ *
7281
+ * <script>
7282
+ * #version 300 es
7283
+ * </script>
7284
+ *
7285
+ * Has one line before it which is invalid according to GLSL ES 3.00
7286
+ *
7287
+ * @param {string} shaderSource The source of the shader
7288
+ * @returns {{shaderSource: string, lineOffset: number}}
7289
+ * @private
7290
+ */
7291
+ function prepShaderSource(shaderSource) {
7292
+ let lineOffset = 0;
7293
+ if (spaceRE.test(shaderSource)) {
7294
+ lineOffset = 1;
7295
+ shaderSource = shaderSource.replace(spaceRE, '');
7296
+ }
7297
+ return {lineOffset, shaderSource};
7298
+ }
7299
+
7300
+ /**
7301
+ * @param {module:twgl.ProgramOptions} progOptions
7302
+ * @param {string} msg
7303
+ * @return null
7304
+ * @private
7305
+ */
7306
+ function reportError(progOptions, msg) {
7307
+ progOptions.errorCallback(msg);
7308
+ if (progOptions.callback) {
7309
+ setTimeout(() => {
7310
+ progOptions.callback(`${msg}\n${progOptions.errors.join('\n')}`);
7311
+ });
7312
+ }
7313
+ return null;
7314
+ }
7315
+
7248
7316
  /**
7249
7317
  * Loads a shader.
7250
7318
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7251
7319
  * @param {string} shaderSource The shader source.
7252
7320
  * @param {number} shaderType The type of shader.
7253
- * @param {module:twgl.ErrorCallback} opt_errorCallback callback for errors.
7321
+ * @param {module:twgl.ProgramOptions} progOptions
7254
7322
  * @return {WebGLShader} The created shader.
7255
7323
  * @private
7256
7324
  */
7257
- function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
7258
- const errFn = opt_errorCallback || error$1;
7325
+ function loadShader(gl, shaderSource, shaderType, progOptions) {
7259
7326
  // Create the shader object
7260
7327
  const shader = gl.createShader(shaderType);
7261
7328
 
7262
- // Remove the first end of line because WebGL 2.0 requires
7263
- // #version 300 es
7264
- // as the first line. No whitespace allowed before that line
7265
- // so
7266
- //
7267
- // <script>
7268
- // #version 300 es
7269
- // </script>
7270
- //
7271
- // Has one line before it which is invalid according to GLSL ES 3.00
7272
- //
7273
- let lineOffset = 0;
7274
- if (spaceRE.test(shaderSource)) {
7275
- lineOffset = 1;
7276
- shaderSource = shaderSource.replace(spaceRE, '');
7277
- }
7278
-
7279
7329
  // Load the shader source
7280
- gl.shaderSource(shader, shaderSource);
7330
+ gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource);
7281
7331
 
7282
7332
  // Compile the shader
7283
7333
  gl.compileShader(shader);
7284
7334
 
7335
+ if (!progOptions.callback && !checkShaderStatus(gl, shaderType, shader, progOptions.errorCallback)) {
7336
+ gl.deleteShader(shader);
7337
+ return null;
7338
+ }
7339
+
7340
+ return shader;
7341
+ }
7342
+
7343
+ /**
7344
+ * Check Shader status
7345
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7346
+ * @param {number} shaderType The shader type
7347
+ * @param {WebGLShader} shader The shader
7348
+ * @param {ErrorCallback} [errFn] function to receive error message.
7349
+ * @return {bool} true if shader is ok.
7350
+ * @private
7351
+ */
7352
+ function checkShaderStatus(gl, shaderType, shader, errFn) {
7353
+ errFn = errFn || error$1;
7285
7354
  // Check the compile status
7286
7355
  const compiled = gl.getShaderParameter(shader, COMPILE_STATUS);
7287
7356
  if (!compiled) {
7288
7357
  // Something went wrong during compilation; get the error
7289
7358
  const lastError = gl.getShaderInfoLog(shader);
7359
+ const {lineOffset, shaderSource} = prepShaderSource(gl.getShaderSource(shader));
7290
7360
  errFn(`${addLineNumbersWithError(shaderSource, lastError, lineOffset)}\nError compiling ${glEnumToString(gl, shaderType)}: ${lastError}`);
7291
- gl.deleteShader(shader);
7292
- return null;
7293
7361
  }
7294
-
7295
- return shader;
7362
+ return compiled;
7296
7363
  }
7297
7364
 
7298
7365
  /**
@@ -7303,6 +7370,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
7303
7370
  * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
7304
7371
  * you can pass an array of names.
7305
7372
  * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
7373
+ * @property {ProgramCallback} [callback] callback for async program compilation.
7306
7374
  * @memberOf module:twgl
7307
7375
  */
7308
7376
 
@@ -7318,6 +7386,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
7318
7386
  function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
7319
7387
  let transformFeedbackVaryings;
7320
7388
  let transformFeedbackMode;
7389
+ let callback;
7321
7390
  if (typeof opt_locations === 'function') {
7322
7391
  opt_errorCallback = opt_locations;
7323
7392
  opt_locations = undefined;
@@ -7328,7 +7397,7 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
7328
7397
  } else if (opt_attribs && !Array.isArray(opt_attribs)) {
7329
7398
  // If we have an errorCallback we can just return this object
7330
7399
  // Otherwise we need to construct one with default errorCallback
7331
- if (opt_attribs.errorCallback) {
7400
+ if (opt_attribs.errorCallback && opt_attribs.errors) {
7332
7401
  return opt_attribs;
7333
7402
  }
7334
7403
  const opt = opt_attribs;
@@ -7336,12 +7405,20 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
7336
7405
  opt_attribs = opt.attribLocations;
7337
7406
  transformFeedbackVaryings = opt.transformFeedbackVaryings;
7338
7407
  transformFeedbackMode = opt.transformFeedbackMode;
7408
+ callback = opt.callback;
7339
7409
  }
7340
7410
 
7411
+ const errorCallback = opt_errorCallback || error$1;
7412
+ const errors = [];
7341
7413
  const options = {
7342
- errorCallback: opt_errorCallback || error$1,
7343
- transformFeedbackVaryings: transformFeedbackVaryings,
7344
- transformFeedbackMode: transformFeedbackMode,
7414
+ errorCallback(msg, ...args) {
7415
+ errors.push(msg);
7416
+ errorCallback(msg, ...args);
7417
+ },
7418
+ transformFeedbackVaryings,
7419
+ transformFeedbackMode,
7420
+ callback,
7421
+ errors,
7345
7422
  };
7346
7423
 
7347
7424
  if (opt_attribs) {
@@ -7379,9 +7456,11 @@ function deleteShaders(gl, shaders) {
7379
7456
  });
7380
7457
  }
7381
7458
 
7459
+ const wait = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));
7460
+
7382
7461
  /**
7383
7462
  * Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
7384
- * program and calls useProgram.
7463
+ * program.
7385
7464
  *
7386
7465
  * NOTE: There are 4 signatures for this function
7387
7466
  *
@@ -7396,11 +7475,13 @@ function deleteShaders(gl, shaders) {
7396
7475
  * @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.
7397
7476
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7398
7477
  * on error. If you want something else pass an callback. It's passed an error message.
7399
- * @return {WebGLProgram?} the created program or null if error.
7478
+ * @return {WebGLProgram?} the created program or null if error of a callback was provided.
7400
7479
  * @memberOf module:twgl/programs
7401
7480
  */
7402
7481
  function createProgram(
7403
7482
  gl, shaders, opt_attribs, opt_locations, opt_errorCallback) {
7483
+ // This code is really convoluted, because it may or may not be async
7484
+ // Maybe it would be better to have a separate function
7404
7485
  const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
7405
7486
  const realShaders = [];
7406
7487
  const newShaders = [];
@@ -7413,7 +7494,7 @@ function createProgram(
7413
7494
  if (elem && elem.type) {
7414
7495
  type = getShaderTypeFromScriptType(gl, elem.type) || type;
7415
7496
  }
7416
- shader = loadShader(gl, src, type, progOptions.errorCallback);
7497
+ shader = loadShader(gl, src, type, progOptions);
7417
7498
  newShaders.push(shader);
7418
7499
  }
7419
7500
  if (isShader(gl, shader)) {
@@ -7422,9 +7503,8 @@ function createProgram(
7422
7503
  }
7423
7504
 
7424
7505
  if (realShaders.length !== shaders.length) {
7425
- progOptions.errorCallback("not enough shaders for program");
7426
7506
  deleteShaders(gl, newShaders);
7427
- return null;
7507
+ return reportError(progOptions, "not enough shaders for program");
7428
7508
  }
7429
7509
 
7430
7510
  const program = gl.createProgram();
@@ -7446,26 +7526,135 @@ function createProgram(
7446
7526
  }
7447
7527
  gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
7448
7528
  }
7529
+
7449
7530
  gl.linkProgram(program);
7531
+ if (progOptions.callback) {
7532
+ checkForProgramLinkCompletionAsync(gl, program, progOptions);
7533
+ return null;
7534
+ } else {
7535
+ if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
7536
+ gl.deleteProgram(program);
7537
+ deleteShaders(gl, newShaders);
7538
+ return null;
7539
+ }
7540
+ return program;
7541
+ }
7542
+ }
7450
7543
 
7544
+ /**
7545
+ * Same as createProgram but returns a promise
7546
+ *
7547
+ * NOTE: There are 4 signatures for this function
7548
+ *
7549
+ * twgl.createProgramAsync(gl, [vs, fs], options);
7550
+ * twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
7551
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
7552
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
7553
+ *
7554
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7555
+ * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
7556
+ * @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
7557
+ * @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.
7558
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7559
+ * on error. If you want something else pass an callback. It's passed an error message.
7560
+ * @return {Promise<WebGLProgram>} The created program
7561
+ * @memberOf module:twgl/programs
7562
+ */
7563
+ function createProgramAsync(gl, shaders, ...args) {
7564
+ return new Promise((resolve, reject) => {
7565
+ const programOptions = getProgramOptions(...args);
7566
+ programOptions.callback = (err, program) => {
7567
+ if (err) {
7568
+ reject(err);
7569
+ } else {
7570
+ resolve(program);
7571
+ }
7572
+ };
7573
+ createProgram(gl, shaders, programOptions);
7574
+ });
7575
+ }
7576
+
7577
+ /**
7578
+ * Same as createProgramInfo but returns a promise
7579
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
7580
+ * to use.
7581
+ * @param {string[]} shaderSources Array of sources for the
7582
+ * shaders or ids. The first is assumed to be the vertex shader,
7583
+ * the second the fragment shader.
7584
+ * @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
7585
+ * @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.
7586
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7587
+ * on error. If you want something else pass an callback. It's passed an error message.
7588
+ * @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
7589
+ * @memberOf module:twgl/programs
7590
+ */
7591
+ function createProgramInfoAsync(gl, shaders, ...args) {
7592
+ return new Promise((resolve, reject) => {
7593
+ const programOptions = getProgramOptions(...args);
7594
+ programOptions.callback = (err, programInfo) => {
7595
+ if (err) {
7596
+ reject(err);
7597
+ } else {
7598
+ resolve(programInfo);
7599
+ }
7600
+ };
7601
+ createProgramInfo(gl, shaders, programOptions);
7602
+ });
7603
+ }
7604
+
7605
+
7606
+ /**
7607
+ * Asynchronously wait for program to link.
7608
+ * Note: if 'KHR_parallel_shader_compile' extension does not
7609
+ * exist then compilation will not be truly async.
7610
+ * @param {WebGLRenderingContext} gl The context
7611
+ * @param {WebGLProgram} program The program
7612
+ * @param {module:twgl.ProgramOptions} progOptions Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in
7613
+ * @private
7614
+ */
7615
+ async function checkForProgramLinkCompletionAsync(gl, program, progOptions) {
7616
+ const ext = gl.getExtension('KHR_parallel_shader_compile');
7617
+ const checkFn = ext
7618
+ ? (gl, program) => gl.getProgramParameter(program, ext.COMPLETION_STATUS_KHR)
7619
+ : () => true;
7620
+
7621
+ let waitTime = 0;
7622
+ do {
7623
+ await wait(waitTime); // must wait at least once
7624
+ waitTime = 1000 / 60;
7625
+ } while (!checkFn(gl, program));
7626
+
7627
+ const success = checkProgramStatus(gl, program, progOptions.errorCallback);
7628
+ const err = success ? undefined : progOptions.errors.join('\n');
7629
+ if (!success) {
7630
+ const errFn = progOptions.errorCallback || error$1;
7631
+ errFn(err);
7632
+ gl.deleteProgram(program);
7633
+ // TODO: delete shaders, but only shaders that were created newly for this
7634
+ // program
7635
+ program = null;
7636
+ }
7637
+ progOptions.callback(err, program);
7638
+ }
7639
+
7640
+ /**
7641
+ * Check a program's link status
7642
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
7643
+ * @param {WebGLProgram} program Program to check
7644
+ * @param {ErrorCallback} [errFn] func for errors
7645
+ * @return {bool} true if program is ok
7646
+ * @private
7647
+ */
7648
+ function checkProgramStatus(gl, program, errFn) {
7649
+ errFn = errFn || error$1;
7451
7650
  // Check the link status
7452
7651
  const linked = gl.getProgramParameter(program, LINK_STATUS);
7453
7652
  if (!linked) {
7454
7653
  // something went wrong with the link
7455
7654
  const lastError = gl.getProgramInfoLog(program);
7456
- progOptions.errorCallback(`${
7457
- realShaders.map(shader => {
7458
- const src = addLineNumbersWithError(gl.getShaderSource(shader), '', 0);
7459
- const type = gl.getShaderParameter(shader, gl.SHADER_TYPE);
7460
- return `${glEnumToString(gl, type)}\n${src}}`;
7461
- }).join('\n')
7462
- }\nError in program linking: ${lastError}`);
7463
-
7464
- gl.deleteProgram(program);
7465
- deleteShaders(gl, newShaders);
7466
- return null;
7655
+ errFn(`Error in program linking: ${lastError}`);
7467
7656
  }
7468
- return program;
7657
+ return linked;
7469
7658
  }
7470
7659
 
7471
7660
  /**
@@ -7474,25 +7663,25 @@ function createProgram(
7474
7663
  * @param {string} scriptId The id of the script tag.
7475
7664
  * @param {number} [opt_shaderType] The type of shader. If not passed in it will
7476
7665
  * be derived from the type of the script tag.
7477
- * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors.
7666
+ * @param {module:twgl.ProgramOptions} [progOptions] callback for errors.
7478
7667
  * @return {WebGLShader?} The created shader or null if error.
7479
7668
  * @private
7480
7669
  */
7481
7670
  function createShaderFromScript(
7482
- gl, scriptId, opt_shaderType, opt_errorCallback) {
7671
+ gl, scriptId, opt_shaderType, progOptions) {
7483
7672
  let shaderSource = "";
7484
7673
  const shaderScript = getElementById(scriptId);
7485
7674
  if (!shaderScript) {
7486
- throw new Error(`unknown script element: ${scriptId}`);
7675
+ return reportError(progOptions, `unknown script element: ${scriptId}`);
7487
7676
  }
7488
7677
  shaderSource = shaderScript.text;
7489
7678
 
7490
7679
  const shaderType = opt_shaderType || getShaderTypeFromScriptType(gl, shaderScript.type);
7491
7680
  if (!shaderType) {
7492
- throw new Error('unknown shader type');
7681
+ return reportError(progOptions, 'unknown shader type');
7493
7682
  }
7494
7683
 
7495
- return loadShader(gl, shaderSource, shaderType, opt_errorCallback);
7684
+ return loadShader(gl, shaderSource, shaderType, progOptions);
7496
7685
  }
7497
7686
 
7498
7687
  /**
@@ -7514,7 +7703,7 @@ function createShaderFromScript(
7514
7703
  * @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.
7515
7704
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7516
7705
  * on error. If you want something else pass an callback. It's passed an error message.
7517
- * @return {WebGLProgram?} the created program or null if error.
7706
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
7518
7707
  * @memberOf module:twgl/programs
7519
7708
  */
7520
7709
  function createProgramFromScripts(
@@ -7523,7 +7712,7 @@ function createProgramFromScripts(
7523
7712
  const shaders = [];
7524
7713
  for (let ii = 0; ii < shaderScriptIds.length; ++ii) {
7525
7714
  const shader = createShaderFromScript(
7526
- gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
7715
+ gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions);
7527
7716
  if (!shader) {
7528
7717
  return null;
7529
7718
  }
@@ -7551,7 +7740,7 @@ function createProgramFromScripts(
7551
7740
  * @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.
7552
7741
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
7553
7742
  * on error. If you want something else pass an callback. It's passed an error message.
7554
- * @return {WebGLProgram?} the created program or null if error.
7743
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
7555
7744
  * @memberOf module:twgl/programs
7556
7745
  */
7557
7746
  function createProgramFromSources(
@@ -7559,9 +7748,8 @@ function createProgramFromSources(
7559
7748
  const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
7560
7749
  const shaders = [];
7561
7750
  for (let ii = 0; ii < shaderSources.length; ++ii) {
7562
- const shader = loadShader(
7563
- gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
7564
- if (!shader) {
7751
+ const shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions);
7752
+ if (!progOptions.callback && !shader) {
7565
7753
  return null;
7566
7754
  }
7567
7755
  shaders.push(shader);
@@ -8472,6 +8660,7 @@ function createAttributeSetters(gl, program) {
8472
8660
  * @param {Object.<string, module:twgl.AttribInfo>} buffers AttribInfos mapped by attribute name.
8473
8661
  * @memberOf module:twgl/programs
8474
8662
  * @deprecated use {@link module:twgl.setBuffersAndAttributes}
8663
+ * @private
8475
8664
  */
8476
8665
  function setAttributes(setters, buffers) {
8477
8666
  for (const name in buffers) {
@@ -8574,6 +8763,8 @@ function createProgramInfoFromProgram(gl, program) {
8574
8763
  return programInfo;
8575
8764
  }
8576
8765
 
8766
+ const notIdRE = /\s|{|}|;/;
8767
+
8577
8768
  /**
8578
8769
  * Creates a ProgramInfo from 2 sources.
8579
8770
  *
@@ -8607,22 +8798,33 @@ function createProgramInfoFromProgram(gl, program) {
8607
8798
  function createProgramInfo(
8608
8799
  gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
8609
8800
  const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
8610
- let good = true;
8801
+ const errors = [];
8611
8802
  shaderSources = shaderSources.map(function(source) {
8612
8803
  // Lets assume if there is no \n it's an id
8613
- if (source.indexOf("\n") < 0) {
8804
+ if (!notIdRE.test(source)) {
8614
8805
  const script = getElementById(source);
8615
8806
  if (!script) {
8616
- progOptions.errorCallback("no element with id: " + source);
8617
- good = false;
8807
+ const err = `no element with id: ${source}`;
8808
+ progOptions.errorCallback(err);
8809
+ errors.push(err);
8618
8810
  } else {
8619
8811
  source = script.text;
8620
8812
  }
8621
8813
  }
8622
8814
  return source;
8623
8815
  });
8624
- if (!good) {
8625
- return null;
8816
+ if (errors.length) {
8817
+ return reportError(progOptions, '');
8818
+ }
8819
+ const origCallback = progOptions.callback;
8820
+ if (origCallback) {
8821
+ progOptions.callback = (err, program) => {
8822
+ let programInfo;
8823
+ if (!err) {
8824
+ programInfo = createProgramInfoFromProgram(gl, program);
8825
+ }
8826
+ origCallback(err, programInfo);
8827
+ };
8626
8828
  }
8627
8829
  const program = createProgramFromSources(gl, shaderSources, progOptions);
8628
8830
  if (!program) {
@@ -8635,9 +8837,11 @@ var programs = /*#__PURE__*/Object.freeze({
8635
8837
  __proto__: null,
8636
8838
  createAttributeSetters: createAttributeSetters,
8637
8839
  createProgram: createProgram,
8840
+ createProgramAsync: createProgramAsync,
8638
8841
  createProgramFromScripts: createProgramFromScripts,
8639
8842
  createProgramFromSources: createProgramFromSources,
8640
8843
  createProgramInfo: createProgramInfo,
8844
+ createProgramInfoAsync: createProgramInfoAsync,
8641
8845
  createProgramInfoFromProgram: createProgramInfoFromProgram,
8642
8846
  createUniformSetters: createUniformSetters,
8643
8847
  createUniformBlockSpecFromProgram: createUniformBlockSpecFromProgram,
@@ -9584,6 +9788,8 @@ function create3DContext(canvas, opt_attribs) {
9584
9788
  * @param {WebGLContextAttributes} [opt_attribs] optional webgl context creation attributes
9585
9789
  * @return {WebGLRenderingContext} The created context.
9586
9790
  * @memberOf module:twgl
9791
+ * @deprecated
9792
+ * @private
9587
9793
  */
9588
9794
  function getWebGLContext(canvas, opt_attribs) {
9589
9795
  const gl = create3DContext(canvas, opt_attribs);
@@ -9662,4 +9868,4 @@ function resizeCanvasToDisplaySize(canvas, multiplier) {
9662
9868
  return false;
9663
9869
  }
9664
9870
 
9665
- export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoFromProgram, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults$2 as setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };
9871
+ export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults$2 as setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };
@@ -7,7 +7,6 @@ export type Defaults = {
7
7
  };
8
8
  export function setDefaults(newDefaults: Defaults): void;
9
9
  export function addExtensionsToContext(gl: WebGLRenderingContext): void;
10
- export function getWebGLContext(canvas: HTMLCanvasElement, opt_attribs?: WebGLContextAttributes): WebGLRenderingContext;
11
10
  export function createContext(canvas: HTMLCanvasElement): WebGLRenderingContext;
12
11
  export function getContext(canvas: HTMLCanvasElement, opt_attribs?: WebGLContextAttributes): WebGLRenderingContext;
13
12
  export function resizeCanvasToDisplaySize(canvas: HTMLCanvasElement, multiplier?: number): boolean;
@@ -89,6 +88,7 @@ export type ProgramOptions = {
89
88
  [key: string]: AttribInfo;
90
89
  } | string[];
91
90
  transformFeedbackMode?: number;
91
+ callback?: ProgramCallback;
92
92
  };
93
93
  export type TransformFeedbackInfo = {
94
94
  index: number;
@@ -255,8 +255,10 @@ export function resizeFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo
255
255
  export function bindFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo?: FramebufferInfo | null, target?: number): void;
256
256
 
257
257
 
258
- export function getBindPointForSamplerType(): void;
258
+ export type ProgramCallback = (err?: string, result?: WebGLProgram | ProgramInfo) => void;
259
259
  export function createProgram(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
260
+ export function createProgramAsync(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<WebGLProgram>;
261
+ export function createProgramInfoAsync(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<ProgramInfo>;
260
262
  export function createProgramFromScripts(gl: WebGLRenderingContext, shaderScriptIds: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
261
263
  export function createProgramFromSources(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
262
264
  export function createUniformSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
@@ -283,11 +285,6 @@ export function setUniformsAndBindTextures(setters: ProgramInfo | {
283
285
  export function createAttributeSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
284
286
  [key: string]: (...params: any[]) => any;
285
287
  };
286
- export function setAttributes(setters: {
287
- [key: string]: (...params: any[]) => any;
288
- }, buffers: {
289
- [key: string]: AttribInfo;
290
- }): void;
291
288
  export function setBuffersAndAttributes(gl: WebGLRenderingContext, setters: ProgramInfo | {
292
289
  [key: string]: (...params: any[]) => any;
293
290
  }, buffers: BufferInfo | VertexArrayInfo): void;
@@ -309,10 +306,7 @@ export function setTextureParameters(gl: WebGLRenderingContext, tex: WebGLTextur
309
306
  export function setSamplerParameters(gl: WebGLRenderingContext, sampler: WebGLSampler, options: TextureOptions): void;
310
307
  export function setTextureFilteringForSize(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, width?: number, height?: number, internalFormat?: number): void;
311
308
  export function setTextureFromElement(gl: WebGLRenderingContext, tex: WebGLTexture, element: HTMLElement, options?: TextureOptions): void;
312
- export function setTextureTo1PixelColor(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions): void;
313
309
  export function loadTextureFromUrl(gl: WebGLRenderingContext, tex: WebGLTexture, options?: TextureOptions, callback?: TextureReadyCallback): HTMLImageElement;
314
- export function loadCubemapFromUrls(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, callback?: CubemapReadyCallback): void;
315
- export function loadSlicesFromUrls(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions, callback?: ThreeDReadyCallback): void;
316
310
  export function setTextureFromArray(gl: WebGLRenderingContext, tex: WebGLTexture, src: number[] | ArrayBufferView, options?: TextureOptions): void;
317
311
  export function setEmptyTexture(gl: WebGLRenderingContext, tex: WebGLTexture, options: TextureOptions): void;
318
312
  export function createTexture(gl: WebGLRenderingContext, options?: TextureOptions, callback?: TextureReadyCallback): WebGLTexture;