twgl.js 4.22.0 → 4.23.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.
package/dist/4.x/twgl.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 4.22.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 4.23.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
  */
@@ -1592,9 +1592,11 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
1592
1592
  exports.__esModule = true;
1593
1593
  exports.createAttributeSetters = createAttributeSetters;
1594
1594
  exports.createProgram = createProgram;
1595
+ exports.createProgramAsync = createProgramAsync;
1595
1596
  exports.createProgramFromScripts = createProgramFromScripts;
1596
1597
  exports.createProgramFromSources = createProgramFromSources;
1597
1598
  exports.createProgramInfo = createProgramInfo;
1599
+ exports.createProgramInfoAsync = createProgramInfoAsync;
1598
1600
  exports.createProgramInfoFromProgram = createProgramInfoFromProgram;
1599
1601
  exports.createUniformSetters = createUniformSetters;
1600
1602
  exports.createUniformBlockSpecFromProgram = createUniformBlockSpecFromProgram;
@@ -1619,6 +1621,10 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
1619
1621
 
1620
1622
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
1621
1623
 
1624
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
1625
+
1626
+ function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
1627
+
1622
1628
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
1623
1629
 
1624
1630
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
@@ -2415,56 +2421,117 @@ function addLineNumbersWithError(src) {
2415
2421
  * @memberOf module:twgl
2416
2422
  */
2417
2423
 
2424
+ /**
2425
+ * Program Callback
2426
+ * @callback ProgramCallback
2427
+ * @param {string} [err] error message, falsy if no error
2428
+ * @param {WebGLProgram|module:twgl.ProgramInfo} [result] the program or programInfo
2429
+ */
2430
+
2418
2431
 
2419
2432
  var spaceRE = /^[ \t]*\n/;
2433
+ /**
2434
+ * Remove the first end of line because WebGL 2.0 requires
2435
+ * #version 300 es
2436
+ * as the first line. No whitespace allowed before that line
2437
+ * so
2438
+ *
2439
+ * <script>
2440
+ * #version 300 es
2441
+ * </script>
2442
+ *
2443
+ * Has one line before it which is invalid according to GLSL ES 3.00
2444
+ *
2445
+ * @param {string} shaderSource The source of the shader
2446
+ * @returns {{shaderSource: string, lineOffset: number}}
2447
+ */
2448
+
2449
+ function prepShaderSource(shaderSource) {
2450
+ var lineOffset = 0;
2451
+
2452
+ if (spaceRE.test(shaderSource)) {
2453
+ lineOffset = 1;
2454
+ shaderSource = shaderSource.replace(spaceRE, '');
2455
+ }
2456
+
2457
+ return {
2458
+ lineOffset: lineOffset,
2459
+ shaderSource: shaderSource
2460
+ };
2461
+ }
2462
+ /**
2463
+ * @param {module:twgl.ProgramOptions} progOptions
2464
+ * @param {string} msg
2465
+ * @return null
2466
+ * @private
2467
+ */
2468
+
2469
+
2470
+ function reportError(progOptions, msg) {
2471
+ progOptions.errorCallback(msg);
2472
+
2473
+ if (progOptions.callback) {
2474
+ setTimeout(function () {
2475
+ progOptions.callback("".concat(msg, "\n").concat(progOptions.errors.join('\n')));
2476
+ });
2477
+ }
2478
+
2479
+ return null;
2480
+ }
2420
2481
  /**
2421
2482
  * Loads a shader.
2422
2483
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2423
2484
  * @param {string} shaderSource The shader source.
2424
2485
  * @param {number} shaderType The type of shader.
2425
- * @param {module:twgl.ErrorCallback} opt_errorCallback callback for errors.
2486
+ * @param {module:twgl.ProgramOptions} progOptions
2426
2487
  * @return {WebGLShader} The created shader.
2427
2488
  * @private
2428
2489
  */
2429
2490
 
2430
- function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
2431
- var errFn = opt_errorCallback || error; // Create the shader object
2432
2491
 
2433
- var shader = gl.createShader(shaderType); // Remove the first end of line because WebGL 2.0 requires
2434
- // #version 300 es
2435
- // as the first line. No whitespace allowed before that line
2436
- // so
2437
- //
2438
- // <script>
2439
- // #version 300 es
2440
- // </script>
2441
- //
2442
- // Has one line before it which is invalid according to GLSL ES 3.00
2443
- //
2492
+ function loadShader(gl, shaderSource, shaderType, progOptions) {
2493
+ // Create the shader object
2494
+ var shader = gl.createShader(shaderType); // Load the shader source
2444
2495
 
2445
- var lineOffset = 0;
2496
+ gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource); // Compile the shader
2446
2497
 
2447
- if (spaceRE.test(shaderSource)) {
2448
- lineOffset = 1;
2449
- shaderSource = shaderSource.replace(spaceRE, '');
2450
- } // Load the shader source
2498
+ gl.compileShader(shader);
2451
2499
 
2500
+ if (!progOptions.callback && !checkShaderStatus(gl, shaderType, shader, progOptions.errorCallback)) {
2501
+ gl.deleteShader(shader);
2502
+ return null;
2503
+ }
2504
+
2505
+ return shader;
2506
+ }
2507
+ /**
2508
+ * Check Shader status
2509
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2510
+ * @param {number} shaderType The shader type
2511
+ * @param {WebGLShader} shader The shader
2512
+ * @param {ErrorCallback} [errFn] function to receive error message.
2513
+ * @return {bool} true if shader is ok.
2514
+ * @private
2515
+ */
2452
2516
 
2453
- gl.shaderSource(shader, shaderSource); // Compile the shader
2454
2517
 
2455
- gl.compileShader(shader); // Check the compile status
2518
+ function checkShaderStatus(gl, shaderType, shader, errFn) {
2519
+ errFn = errFn || error; // Check the compile status
2456
2520
 
2457
2521
  var compiled = gl.getShaderParameter(shader, COMPILE_STATUS);
2458
2522
 
2459
2523
  if (!compiled) {
2460
2524
  // Something went wrong during compilation; get the error
2461
2525
  var lastError = gl.getShaderInfoLog(shader);
2526
+
2527
+ var _prepShaderSource = prepShaderSource(gl.getShaderSource(shader)),
2528
+ lineOffset = _prepShaderSource.lineOffset,
2529
+ shaderSource = _prepShaderSource.shaderSource;
2530
+
2462
2531
  errFn("".concat(addLineNumbersWithError(shaderSource, lastError, lineOffset), "\nError compiling ").concat(utils.glEnumToString(gl, shaderType), ": ").concat(lastError));
2463
- gl.deleteShader(shader);
2464
- return null;
2465
2532
  }
2466
2533
 
2467
- return shader;
2534
+ return compiled;
2468
2535
  }
2469
2536
  /**
2470
2537
  * @typedef {Object} ProgramOptions
@@ -2474,6 +2541,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
2474
2541
  * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
2475
2542
  * you can pass an array of names.
2476
2543
  * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
2544
+ * @property {ProgramCallback} [callback] callback for async program compilation.
2477
2545
  * @memberOf module:twgl
2478
2546
  */
2479
2547
 
@@ -2491,6 +2559,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
2491
2559
  function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
2492
2560
  var transformFeedbackVaryings;
2493
2561
  var transformFeedbackMode;
2562
+ var callback;
2494
2563
 
2495
2564
  if (typeof opt_locations === 'function') {
2496
2565
  opt_errorCallback = opt_locations;
@@ -2503,7 +2572,7 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
2503
2572
  } else if (opt_attribs && !Array.isArray(opt_attribs)) {
2504
2573
  // If we have an errorCallback we can just return this object
2505
2574
  // Otherwise we need to construct one with default errorCallback
2506
- if (opt_attribs.errorCallback) {
2575
+ if (opt_attribs.errorCallback && opt_attribs.errors) {
2507
2576
  return opt_attribs;
2508
2577
  }
2509
2578
 
@@ -2512,12 +2581,26 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
2512
2581
  opt_attribs = opt.attribLocations;
2513
2582
  transformFeedbackVaryings = opt.transformFeedbackVaryings;
2514
2583
  transformFeedbackMode = opt.transformFeedbackMode;
2584
+ callback = opt.callback;
2515
2585
  }
2516
2586
 
2587
+ var _errorCallback = opt_errorCallback || error;
2588
+
2589
+ var errors = [];
2517
2590
  var options = {
2518
- errorCallback: opt_errorCallback || error,
2591
+ errorCallback: function errorCallback(msg) {
2592
+ errors.push(msg);
2593
+
2594
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
2595
+ args[_key - 1] = arguments[_key];
2596
+ }
2597
+
2598
+ _errorCallback.apply(void 0, [msg].concat(args));
2599
+ },
2519
2600
  transformFeedbackVaryings: transformFeedbackVaryings,
2520
- transformFeedbackMode: transformFeedbackMode
2601
+ transformFeedbackMode: transformFeedbackMode,
2602
+ callback: callback,
2603
+ errors: errors
2521
2604
  };
2522
2605
 
2523
2606
  if (opt_attribs) {
@@ -2554,9 +2637,16 @@ function deleteShaders(gl, shaders) {
2554
2637
  gl.deleteShader(shader);
2555
2638
  });
2556
2639
  }
2640
+
2641
+ var wait = function wait() {
2642
+ var ms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
2643
+ return new Promise(function (resolve) {
2644
+ return setTimeout(resolve, ms);
2645
+ });
2646
+ };
2557
2647
  /**
2558
2648
  * Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
2559
- * program and calls useProgram.
2649
+ * program.
2560
2650
  *
2561
2651
  * NOTE: There are 4 signatures for this function
2562
2652
  *
@@ -2571,12 +2661,14 @@ function deleteShaders(gl, shaders) {
2571
2661
  * @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.
2572
2662
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2573
2663
  * on error. If you want something else pass an callback. It's passed an error message.
2574
- * @return {WebGLProgram?} the created program or null if error.
2664
+ * @return {WebGLProgram?} the created program or null if error of a callback was provided.
2575
2665
  * @memberOf module:twgl/programs
2576
2666
  */
2577
2667
 
2578
2668
 
2579
2669
  function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallback) {
2670
+ // This code is really convoluted, because it may or may not be async
2671
+ // Maybe it would be better to have a separate function
2580
2672
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
2581
2673
  var realShaders = [];
2582
2674
  var newShaders = [];
@@ -2593,7 +2685,7 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2593
2685
  type = getShaderTypeFromScriptType(gl, elem.type) || type;
2594
2686
  }
2595
2687
 
2596
- shader = loadShader(gl, src, type, progOptions.errorCallback);
2688
+ shader = loadShader(gl, src, type, progOptions);
2597
2689
  newShaders.push(shader);
2598
2690
  }
2599
2691
 
@@ -2603,9 +2695,8 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2603
2695
  }
2604
2696
 
2605
2697
  if (realShaders.length !== shaders.length) {
2606
- progOptions.errorCallback("not enough shaders for program");
2607
2698
  deleteShaders(gl, newShaders);
2608
- return null;
2699
+ return reportError(progOptions, "not enough shaders for program");
2609
2700
  }
2610
2701
 
2611
2702
  var program = gl.createProgram();
@@ -2633,24 +2724,186 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2633
2724
  gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
2634
2725
  }
2635
2726
 
2636
- gl.linkProgram(program); // Check the link status
2727
+ gl.linkProgram(program);
2728
+
2729
+ if (progOptions.callback) {
2730
+ checkForProgramLinkCompletionAsync(gl, program, progOptions);
2731
+ return null;
2732
+ } else {
2733
+ if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
2734
+ gl.deleteProgram(program);
2735
+ deleteShaders(gl, newShaders);
2736
+ return null;
2737
+ }
2738
+
2739
+ return program;
2740
+ }
2741
+ }
2742
+ /**
2743
+ * Same as createProgram but returns a promise
2744
+ *
2745
+ * NOTE: There are 4 signatures for this function
2746
+ *
2747
+ * twgl.createProgramAsync(gl, [vs, fs], options);
2748
+ * twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
2749
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
2750
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
2751
+ *
2752
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2753
+ * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
2754
+ * @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
2755
+ * @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.
2756
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2757
+ * on error. If you want something else pass an callback. It's passed an error message.
2758
+ * @return {Promise<WebGLProgram>} The created program
2759
+ * @memberOf module:twgl/programs
2760
+ */
2761
+
2762
+
2763
+ function createProgramAsync(gl, shaders) {
2764
+ for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
2765
+ args[_key2 - 2] = arguments[_key2];
2766
+ }
2767
+
2768
+ return new Promise(function (resolve, reject) {
2769
+ var programOptions = getProgramOptions.apply(void 0, args);
2770
+
2771
+ programOptions.callback = function (err, program) {
2772
+ if (err) {
2773
+ reject(err);
2774
+ } else {
2775
+ resolve(program);
2776
+ }
2777
+ };
2778
+
2779
+ createProgram(gl, shaders, programOptions);
2780
+ });
2781
+ }
2782
+ /**
2783
+ * Same as createProgramInfo but returns a promise
2784
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
2785
+ * to use.
2786
+ * @param {string[]} shaderSources Array of sources for the
2787
+ * shaders or ids. The first is assumed to be the vertex shader,
2788
+ * the second the fragment shader.
2789
+ * @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
2790
+ * @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.
2791
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2792
+ * on error. If you want something else pass an callback. It's passed an error message.
2793
+ * @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
2794
+ * @memberOf module:twgl/programs
2795
+ */
2796
+
2797
+
2798
+ function createProgramInfoAsync(gl, shaders) {
2799
+ for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
2800
+ args[_key3 - 2] = arguments[_key3];
2801
+ }
2802
+
2803
+ return new Promise(function (resolve, reject) {
2804
+ var programOptions = getProgramOptions.apply(void 0, args);
2805
+
2806
+ programOptions.callback = function (err, programInfo) {
2807
+ if (err) {
2808
+ reject(err);
2809
+ } else {
2810
+ resolve(programInfo);
2811
+ }
2812
+ };
2813
+
2814
+ createProgramInfo(gl, shaders, programOptions);
2815
+ });
2816
+ }
2817
+ /**
2818
+ * Asynchronously wait for program to link.
2819
+ * Note: if 'KHR_parallel_shader_compile' extension does not
2820
+ * exist then compilation will not be truly async.
2821
+ * @param {WebGLRenderingContext} gl The context
2822
+ * @param {WebGLProgram} program The program
2823
+ * @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
2824
+ * @private
2825
+ */
2826
+
2827
+
2828
+ function checkForProgramLinkCompletionAsync(_x, _x2, _x3) {
2829
+ return _checkForProgramLinkCompletionAsync.apply(this, arguments);
2830
+ }
2831
+ /**
2832
+ * Check a program's link status
2833
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2834
+ * @param {WebGLProgram} program Program to check
2835
+ * @param {ErrorCallback} [errFn] func for errors
2836
+ * @return {bool} true if program is ok
2837
+ * @private
2838
+ */
2839
+
2840
+
2841
+ function _checkForProgramLinkCompletionAsync() {
2842
+ _checkForProgramLinkCompletionAsync = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(gl, program, progOptions) {
2843
+ var ext, checkFn, waitTime, success, err, errFn;
2844
+ return regeneratorRuntime.wrap(function _callee$(_context) {
2845
+ while (1) {
2846
+ switch (_context.prev = _context.next) {
2847
+ case 0:
2848
+ ext = gl.getExtension('KHR_parallel_shader_compile');
2849
+ checkFn = ext ? function (gl, program) {
2850
+ return gl.getProgramParameter(program, ext.COMPLETION_STATUS_KHR);
2851
+ } : function () {
2852
+ return true;
2853
+ };
2854
+ waitTime = 0;
2855
+
2856
+ case 3:
2857
+ _context.next = 5;
2858
+ return wait(waitTime);
2859
+
2860
+ case 5:
2861
+ // must wait at least once
2862
+ waitTime = 1000 / 60;
2863
+
2864
+ case 6:
2865
+ if (!checkFn(gl, program)) {
2866
+ _context.next = 3;
2867
+ break;
2868
+ }
2869
+
2870
+ case 7:
2871
+ success = checkProgramStatus(gl, program, progOptions.errorCallback);
2872
+ err = success ? undefined : progOptions.errors.join('\n');
2873
+
2874
+ if (!success) {
2875
+ errFn = progOptions.errorCallback || error;
2876
+ errFn(err);
2877
+ gl.deleteProgram(program); // TODO: delete shaders, but only shaders that were created newly for this
2878
+ // program
2879
+
2880
+ program = null;
2881
+ }
2882
+
2883
+ progOptions.callback(err, program);
2884
+
2885
+ case 11:
2886
+ case "end":
2887
+ return _context.stop();
2888
+ }
2889
+ }
2890
+ }, _callee);
2891
+ }));
2892
+ return _checkForProgramLinkCompletionAsync.apply(this, arguments);
2893
+ }
2894
+
2895
+ function checkProgramStatus(gl, program, errFn) {
2896
+ errFn = errFn || error; // Check the link status
2637
2897
 
2638
2898
  var linked = gl.getProgramParameter(program, LINK_STATUS);
2639
2899
 
2640
2900
  if (!linked) {
2641
2901
  // something went wrong with the link
2642
2902
  var lastError = gl.getProgramInfoLog(program);
2643
- progOptions.errorCallback("".concat(realShaders.map(function (shader) {
2644
- var src = addLineNumbersWithError(gl.getShaderSource(shader), '', 0);
2645
- var type = gl.getShaderParameter(shader, gl.SHADER_TYPE);
2646
- return "".concat(utils.glEnumToString(gl, type), "\n").concat(src, "}");
2647
- }).join('\n'), "\nError in program linking: ").concat(lastError));
2648
- gl.deleteProgram(program);
2649
- deleteShaders(gl, newShaders);
2650
- return null;
2903
+ errFn("Error in program linking: ".concat(lastError));
2651
2904
  }
2652
2905
 
2653
- return program;
2906
+ return linked;
2654
2907
  }
2655
2908
  /**
2656
2909
  * Loads a shader from a script tag.
@@ -2658,28 +2911,28 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2658
2911
  * @param {string} scriptId The id of the script tag.
2659
2912
  * @param {number} [opt_shaderType] The type of shader. If not passed in it will
2660
2913
  * be derived from the type of the script tag.
2661
- * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors.
2914
+ * @param {module:twgl.ProgramOptions} [progOptions] callback for errors.
2662
2915
  * @return {WebGLShader?} The created shader or null if error.
2663
2916
  * @private
2664
2917
  */
2665
2918
 
2666
2919
 
2667
- function createShaderFromScript(gl, scriptId, opt_shaderType, opt_errorCallback) {
2920
+ function createShaderFromScript(gl, scriptId, opt_shaderType, progOptions) {
2668
2921
  var shaderSource = "";
2669
2922
  var shaderScript = getElementById(scriptId);
2670
2923
 
2671
2924
  if (!shaderScript) {
2672
- throw new Error("unknown script element: ".concat(scriptId));
2925
+ return reportError(progOptions, "unknown script element: ".concat(scriptId));
2673
2926
  }
2674
2927
 
2675
2928
  shaderSource = shaderScript.text;
2676
2929
  var shaderType = opt_shaderType || getShaderTypeFromScriptType(gl, shaderScript.type);
2677
2930
 
2678
2931
  if (!shaderType) {
2679
- throw new Error('unknown shader type');
2932
+ return reportError(progOptions, 'unknown shader type');
2680
2933
  }
2681
2934
 
2682
- return loadShader(gl, shaderSource, shaderType, opt_errorCallback);
2935
+ return loadShader(gl, shaderSource, shaderType, progOptions);
2683
2936
  }
2684
2937
  /**
2685
2938
  * Creates a program from 2 script tags.
@@ -2700,7 +2953,7 @@ function createShaderFromScript(gl, scriptId, opt_shaderType, opt_errorCallback)
2700
2953
  * @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.
2701
2954
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2702
2955
  * on error. If you want something else pass an callback. It's passed an error message.
2703
- * @return {WebGLProgram?} the created program or null if error.
2956
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
2704
2957
  * @memberOf module:twgl/programs
2705
2958
  */
2706
2959
 
@@ -2710,7 +2963,7 @@ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_location
2710
2963
  var shaders = [];
2711
2964
 
2712
2965
  for (var ii = 0; ii < shaderScriptIds.length; ++ii) {
2713
- var shader = createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
2966
+ var shader = createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions);
2714
2967
 
2715
2968
  if (!shader) {
2716
2969
  return null;
@@ -2740,7 +2993,7 @@ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_location
2740
2993
  * @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.
2741
2994
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2742
2995
  * on error. If you want something else pass an callback. It's passed an error message.
2743
- * @return {WebGLProgram?} the created program or null if error.
2996
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
2744
2997
  * @memberOf module:twgl/programs
2745
2998
  */
2746
2999
 
@@ -2750,9 +3003,9 @@ function createProgramFromSources(gl, shaderSources, opt_attribs, opt_locations,
2750
3003
  var shaders = [];
2751
3004
 
2752
3005
  for (var ii = 0; ii < shaderSources.length; ++ii) {
2753
- var shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
3006
+ var shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions);
2754
3007
 
2755
- if (!shader) {
3008
+ if (!progOptions.callback && !shader) {
2756
3009
  return null;
2757
3010
  }
2758
3011
 
@@ -3827,6 +4080,8 @@ function createProgramInfoFromProgram(gl, program) {
3827
4080
 
3828
4081
  return programInfo;
3829
4082
  }
4083
+
4084
+ var notIdRE = /\s|{|}|;/;
3830
4085
  /**
3831
4086
  * Creates a ProgramInfo from 2 sources.
3832
4087
  *
@@ -3858,18 +4113,18 @@ function createProgramInfoFromProgram(gl, program) {
3858
4113
  * @memberOf module:twgl/programs
3859
4114
  */
3860
4115
 
3861
-
3862
4116
  function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
3863
4117
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
3864
- var good = true;
4118
+ var errors = [];
3865
4119
  shaderSources = shaderSources.map(function (source) {
3866
4120
  // Lets assume if there is no \n it's an id
3867
- if (source.indexOf("\n") < 0) {
4121
+ if (!notIdRE.test(source)) {
3868
4122
  var script = getElementById(source);
3869
4123
 
3870
4124
  if (!script) {
3871
- progOptions.errorCallback("no element with id: " + source);
3872
- good = false;
4125
+ var err = "no element with id: ".concat(source);
4126
+ progOptions.errorCallback(err);
4127
+ errors.push(err);
3873
4128
  } else {
3874
4129
  source = script.text;
3875
4130
  }
@@ -3878,8 +4133,22 @@ function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_er
3878
4133
  return source;
3879
4134
  });
3880
4135
 
3881
- if (!good) {
3882
- return null;
4136
+ if (errors.length) {
4137
+ return reportError(progOptions, '');
4138
+ }
4139
+
4140
+ var origCallback = progOptions.callback;
4141
+
4142
+ if (origCallback) {
4143
+ progOptions.callback = function (err, program) {
4144
+ var programInfo;
4145
+
4146
+ if (!err) {
4147
+ programInfo = createProgramInfoFromProgram(gl, program);
4148
+ }
4149
+
4150
+ origCallback(err, programInfo);
4151
+ };
3883
4152
  }
3884
4153
 
3885
4154
  var program = createProgramFromSources(gl, shaderSources, progOptions);
@@ -7226,6 +7495,9 @@ function createVertexArrayInfo(gl, programInfos, bufferInfo) {
7226
7495
  * @param {Object.<string, function>} setters Attribute setters as returned from createAttributeSetters
7227
7496
  * @param {Object.<string, module:twgl.AttribInfo>} attribs AttribInfos mapped by attribute name.
7228
7497
  * @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
7498
+ *
7499
+ * @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
7500
+ *
7229
7501
  * @memberOf module:twgl/vertexArrays
7230
7502
  */
7231
7503
 
@@ -7253,6 +7525,9 @@ function createVAOAndSetAttributes(gl, setters, attribs, indices) {
7253
7525
  * @param {Object.<string, function>| module:twgl.ProgramInfo} programInfo as returned from createProgramInfo or Attribute setters as returned from createAttributeSetters
7254
7526
  * @param {module:twgl.BufferInfo} bufferInfo BufferInfo as returned from createBufferInfoFromArrays etc...
7255
7527
  * @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
7528
+ *
7529
+ * @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
7530
+ *
7256
7531
  * @memberOf module:twgl/vertexArrays
7257
7532
  */
7258
7533
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twgl.js",
3
- "version": "4.22.0",
3
+ "version": "4.23.1",
4
4
  "description": "A Tiny WebGL helper library",
5
5
  "main": "dist/4.x/twgl-full.js",
6
6
  "types": "dist/4.x/twgl-full.d.ts",