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.
package/dist/4.x/twgl.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 4.22.1 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 4.23.2 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
  */
@@ -651,6 +651,11 @@ function getNumElementsFromNonIndexedArrays(arrays) {
651
651
 
652
652
  var array = arrays[key];
653
653
  var length = getArray(array).length;
654
+
655
+ if (length === undefined) {
656
+ return 1; // There's no arrays
657
+ }
658
+
654
659
  var numComponents = getNumComponents(array, key);
655
660
  var numElements = length / numComponents;
656
661
 
@@ -684,6 +689,11 @@ function getNumElementsFromAttributes(gl, attribs) {
684
689
  }
685
690
 
686
691
  var attrib = attribs[key];
692
+
693
+ if (!attrib.buffer) {
694
+ return 1; // There's no buffer
695
+ }
696
+
687
697
  gl.bindBuffer(ARRAY_BUFFER, attrib.buffer);
688
698
  var numBytes = gl.getBufferParameter(ARRAY_BUFFER, BUFFER_SIZE);
689
699
  gl.bindBuffer(ARRAY_BUFFER, null);
@@ -1592,9 +1602,11 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
1592
1602
  exports.__esModule = true;
1593
1603
  exports.createAttributeSetters = createAttributeSetters;
1594
1604
  exports.createProgram = createProgram;
1605
+ exports.createProgramAsync = createProgramAsync;
1595
1606
  exports.createProgramFromScripts = createProgramFromScripts;
1596
1607
  exports.createProgramFromSources = createProgramFromSources;
1597
1608
  exports.createProgramInfo = createProgramInfo;
1609
+ exports.createProgramInfoAsync = createProgramInfoAsync;
1598
1610
  exports.createProgramInfoFromProgram = createProgramInfoFromProgram;
1599
1611
  exports.createUniformSetters = createUniformSetters;
1600
1612
  exports.createUniformBlockSpecFromProgram = createUniformBlockSpecFromProgram;
@@ -1619,6 +1631,10 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
1619
1631
 
1620
1632
  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
1633
 
1634
+ 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); } }
1635
+
1636
+ 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); }); }; }
1637
+
1622
1638
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
1623
1639
 
1624
1640
  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."); }
@@ -1719,6 +1735,7 @@ var TEXTURE_2D_ARRAY = 0x8C1A;
1719
1735
  var typeMap = {};
1720
1736
  /**
1721
1737
  * Returns the corresponding bind point for a given sampler type
1738
+ * @private
1722
1739
  */
1723
1740
 
1724
1741
  function getBindPointForSamplerType(gl, type) {
@@ -2415,56 +2432,118 @@ function addLineNumbersWithError(src) {
2415
2432
  * @memberOf module:twgl
2416
2433
  */
2417
2434
 
2435
+ /**
2436
+ * Program Callback
2437
+ * @callback ProgramCallback
2438
+ * @param {string} [err] error message, falsy if no error
2439
+ * @param {WebGLProgram|module:twgl.ProgramInfo} [result] the program or programInfo
2440
+ */
2441
+
2418
2442
 
2419
2443
  var spaceRE = /^[ \t]*\n/;
2444
+ /**
2445
+ * Remove the first end of line because WebGL 2.0 requires
2446
+ * #version 300 es
2447
+ * as the first line. No whitespace allowed before that line
2448
+ * so
2449
+ *
2450
+ * <script>
2451
+ * #version 300 es
2452
+ * </script>
2453
+ *
2454
+ * Has one line before it which is invalid according to GLSL ES 3.00
2455
+ *
2456
+ * @param {string} shaderSource The source of the shader
2457
+ * @returns {{shaderSource: string, lineOffset: number}}
2458
+ * @private
2459
+ */
2460
+
2461
+ function prepShaderSource(shaderSource) {
2462
+ var lineOffset = 0;
2463
+
2464
+ if (spaceRE.test(shaderSource)) {
2465
+ lineOffset = 1;
2466
+ shaderSource = shaderSource.replace(spaceRE, '');
2467
+ }
2468
+
2469
+ return {
2470
+ lineOffset: lineOffset,
2471
+ shaderSource: shaderSource
2472
+ };
2473
+ }
2474
+ /**
2475
+ * @param {module:twgl.ProgramOptions} progOptions
2476
+ * @param {string} msg
2477
+ * @return null
2478
+ * @private
2479
+ */
2480
+
2481
+
2482
+ function reportError(progOptions, msg) {
2483
+ progOptions.errorCallback(msg);
2484
+
2485
+ if (progOptions.callback) {
2486
+ setTimeout(function () {
2487
+ progOptions.callback("".concat(msg, "\n").concat(progOptions.errors.join('\n')));
2488
+ });
2489
+ }
2490
+
2491
+ return null;
2492
+ }
2420
2493
  /**
2421
2494
  * Loads a shader.
2422
2495
  * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2423
2496
  * @param {string} shaderSource The shader source.
2424
2497
  * @param {number} shaderType The type of shader.
2425
- * @param {module:twgl.ErrorCallback} opt_errorCallback callback for errors.
2498
+ * @param {module:twgl.ProgramOptions} progOptions
2426
2499
  * @return {WebGLShader} The created shader.
2427
2500
  * @private
2428
2501
  */
2429
2502
 
2430
- function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
2431
- var errFn = opt_errorCallback || error; // Create the shader object
2432
2503
 
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
- //
2504
+ function loadShader(gl, shaderSource, shaderType, progOptions) {
2505
+ // Create the shader object
2506
+ var shader = gl.createShader(shaderType); // Load the shader source
2444
2507
 
2445
- var lineOffset = 0;
2508
+ gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource); // Compile the shader
2446
2509
 
2447
- if (spaceRE.test(shaderSource)) {
2448
- lineOffset = 1;
2449
- shaderSource = shaderSource.replace(spaceRE, '');
2450
- } // Load the shader source
2510
+ gl.compileShader(shader);
2511
+
2512
+ if (!progOptions.callback && !checkShaderStatus(gl, shaderType, shader, progOptions.errorCallback)) {
2513
+ gl.deleteShader(shader);
2514
+ return null;
2515
+ }
2451
2516
 
2517
+ return shader;
2518
+ }
2519
+ /**
2520
+ * Check Shader status
2521
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2522
+ * @param {number} shaderType The shader type
2523
+ * @param {WebGLShader} shader The shader
2524
+ * @param {ErrorCallback} [errFn] function to receive error message.
2525
+ * @return {bool} true if shader is ok.
2526
+ * @private
2527
+ */
2452
2528
 
2453
- gl.shaderSource(shader, shaderSource); // Compile the shader
2454
2529
 
2455
- gl.compileShader(shader); // Check the compile status
2530
+ function checkShaderStatus(gl, shaderType, shader, errFn) {
2531
+ errFn = errFn || error; // Check the compile status
2456
2532
 
2457
2533
  var compiled = gl.getShaderParameter(shader, COMPILE_STATUS);
2458
2534
 
2459
2535
  if (!compiled) {
2460
2536
  // Something went wrong during compilation; get the error
2461
2537
  var lastError = gl.getShaderInfoLog(shader);
2538
+
2539
+ var _prepShaderSource = prepShaderSource(gl.getShaderSource(shader)),
2540
+ lineOffset = _prepShaderSource.lineOffset,
2541
+ shaderSource = _prepShaderSource.shaderSource;
2542
+
2462
2543
  errFn("".concat(addLineNumbersWithError(shaderSource, lastError, lineOffset), "\nError compiling ").concat(utils.glEnumToString(gl, shaderType), ": ").concat(lastError));
2463
- gl.deleteShader(shader);
2464
- return null;
2465
2544
  }
2466
2545
 
2467
- return shader;
2546
+ return compiled;
2468
2547
  }
2469
2548
  /**
2470
2549
  * @typedef {Object} ProgramOptions
@@ -2474,6 +2553,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
2474
2553
  * a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
2475
2554
  * you can pass an array of names.
2476
2555
  * @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
2556
+ * @property {ProgramCallback} [callback] callback for async program compilation.
2477
2557
  * @memberOf module:twgl
2478
2558
  */
2479
2559
 
@@ -2491,6 +2571,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
2491
2571
  function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
2492
2572
  var transformFeedbackVaryings;
2493
2573
  var transformFeedbackMode;
2574
+ var callback;
2494
2575
 
2495
2576
  if (typeof opt_locations === 'function') {
2496
2577
  opt_errorCallback = opt_locations;
@@ -2503,7 +2584,7 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
2503
2584
  } else if (opt_attribs && !Array.isArray(opt_attribs)) {
2504
2585
  // If we have an errorCallback we can just return this object
2505
2586
  // Otherwise we need to construct one with default errorCallback
2506
- if (opt_attribs.errorCallback) {
2587
+ if (opt_attribs.errorCallback && opt_attribs.errors) {
2507
2588
  return opt_attribs;
2508
2589
  }
2509
2590
 
@@ -2512,12 +2593,26 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
2512
2593
  opt_attribs = opt.attribLocations;
2513
2594
  transformFeedbackVaryings = opt.transformFeedbackVaryings;
2514
2595
  transformFeedbackMode = opt.transformFeedbackMode;
2596
+ callback = opt.callback;
2515
2597
  }
2516
2598
 
2599
+ var _errorCallback = opt_errorCallback || error;
2600
+
2601
+ var errors = [];
2517
2602
  var options = {
2518
- errorCallback: opt_errorCallback || error,
2603
+ errorCallback: function errorCallback(msg) {
2604
+ errors.push(msg);
2605
+
2606
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
2607
+ args[_key - 1] = arguments[_key];
2608
+ }
2609
+
2610
+ _errorCallback.apply(void 0, [msg].concat(args));
2611
+ },
2519
2612
  transformFeedbackVaryings: transformFeedbackVaryings,
2520
- transformFeedbackMode: transformFeedbackMode
2613
+ transformFeedbackMode: transformFeedbackMode,
2614
+ callback: callback,
2615
+ errors: errors
2521
2616
  };
2522
2617
 
2523
2618
  if (opt_attribs) {
@@ -2554,9 +2649,16 @@ function deleteShaders(gl, shaders) {
2554
2649
  gl.deleteShader(shader);
2555
2650
  });
2556
2651
  }
2652
+
2653
+ var wait = function wait() {
2654
+ var ms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
2655
+ return new Promise(function (resolve) {
2656
+ return setTimeout(resolve, ms);
2657
+ });
2658
+ };
2557
2659
  /**
2558
2660
  * Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
2559
- * program and calls useProgram.
2661
+ * program.
2560
2662
  *
2561
2663
  * NOTE: There are 4 signatures for this function
2562
2664
  *
@@ -2571,12 +2673,14 @@ function deleteShaders(gl, shaders) {
2571
2673
  * @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
2674
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2573
2675
  * 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.
2676
+ * @return {WebGLProgram?} the created program or null if error of a callback was provided.
2575
2677
  * @memberOf module:twgl/programs
2576
2678
  */
2577
2679
 
2578
2680
 
2579
2681
  function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallback) {
2682
+ // This code is really convoluted, because it may or may not be async
2683
+ // Maybe it would be better to have a separate function
2580
2684
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
2581
2685
  var realShaders = [];
2582
2686
  var newShaders = [];
@@ -2593,7 +2697,7 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2593
2697
  type = getShaderTypeFromScriptType(gl, elem.type) || type;
2594
2698
  }
2595
2699
 
2596
- shader = loadShader(gl, src, type, progOptions.errorCallback);
2700
+ shader = loadShader(gl, src, type, progOptions);
2597
2701
  newShaders.push(shader);
2598
2702
  }
2599
2703
 
@@ -2603,9 +2707,8 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2603
2707
  }
2604
2708
 
2605
2709
  if (realShaders.length !== shaders.length) {
2606
- progOptions.errorCallback("not enough shaders for program");
2607
2710
  deleteShaders(gl, newShaders);
2608
- return null;
2711
+ return reportError(progOptions, "not enough shaders for program");
2609
2712
  }
2610
2713
 
2611
2714
  var program = gl.createProgram();
@@ -2633,24 +2736,186 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2633
2736
  gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
2634
2737
  }
2635
2738
 
2636
- gl.linkProgram(program); // Check the link status
2739
+ gl.linkProgram(program);
2740
+
2741
+ if (progOptions.callback) {
2742
+ checkForProgramLinkCompletionAsync(gl, program, progOptions);
2743
+ return null;
2744
+ } else {
2745
+ if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
2746
+ gl.deleteProgram(program);
2747
+ deleteShaders(gl, newShaders);
2748
+ return null;
2749
+ }
2750
+
2751
+ return program;
2752
+ }
2753
+ }
2754
+ /**
2755
+ * Same as createProgram but returns a promise
2756
+ *
2757
+ * NOTE: There are 4 signatures for this function
2758
+ *
2759
+ * twgl.createProgramAsync(gl, [vs, fs], options);
2760
+ * twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
2761
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
2762
+ * twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
2763
+ *
2764
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2765
+ * @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
2766
+ * @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
2767
+ * @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.
2768
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2769
+ * on error. If you want something else pass an callback. It's passed an error message.
2770
+ * @return {Promise<WebGLProgram>} The created program
2771
+ * @memberOf module:twgl/programs
2772
+ */
2773
+
2774
+
2775
+ function createProgramAsync(gl, shaders) {
2776
+ for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
2777
+ args[_key2 - 2] = arguments[_key2];
2778
+ }
2779
+
2780
+ return new Promise(function (resolve, reject) {
2781
+ var programOptions = getProgramOptions.apply(void 0, args);
2782
+
2783
+ programOptions.callback = function (err, program) {
2784
+ if (err) {
2785
+ reject(err);
2786
+ } else {
2787
+ resolve(program);
2788
+ }
2789
+ };
2790
+
2791
+ createProgram(gl, shaders, programOptions);
2792
+ });
2793
+ }
2794
+ /**
2795
+ * Same as createProgramInfo but returns a promise
2796
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext
2797
+ * to use.
2798
+ * @param {string[]} shaderSources Array of sources for the
2799
+ * shaders or ids. The first is assumed to be the vertex shader,
2800
+ * the second the fragment shader.
2801
+ * @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
2802
+ * @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.
2803
+ * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2804
+ * on error. If you want something else pass an callback. It's passed an error message.
2805
+ * @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
2806
+ * @memberOf module:twgl/programs
2807
+ */
2808
+
2809
+
2810
+ function createProgramInfoAsync(gl, shaders) {
2811
+ for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
2812
+ args[_key3 - 2] = arguments[_key3];
2813
+ }
2814
+
2815
+ return new Promise(function (resolve, reject) {
2816
+ var programOptions = getProgramOptions.apply(void 0, args);
2817
+
2818
+ programOptions.callback = function (err, programInfo) {
2819
+ if (err) {
2820
+ reject(err);
2821
+ } else {
2822
+ resolve(programInfo);
2823
+ }
2824
+ };
2825
+
2826
+ createProgramInfo(gl, shaders, programOptions);
2827
+ });
2828
+ }
2829
+ /**
2830
+ * Asynchronously wait for program to link.
2831
+ * Note: if 'KHR_parallel_shader_compile' extension does not
2832
+ * exist then compilation will not be truly async.
2833
+ * @param {WebGLRenderingContext} gl The context
2834
+ * @param {WebGLProgram} program The program
2835
+ * @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
2836
+ * @private
2837
+ */
2838
+
2839
+
2840
+ function checkForProgramLinkCompletionAsync(_x, _x2, _x3) {
2841
+ return _checkForProgramLinkCompletionAsync.apply(this, arguments);
2842
+ }
2843
+ /**
2844
+ * Check a program's link status
2845
+ * @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
2846
+ * @param {WebGLProgram} program Program to check
2847
+ * @param {ErrorCallback} [errFn] func for errors
2848
+ * @return {bool} true if program is ok
2849
+ * @private
2850
+ */
2851
+
2852
+
2853
+ function _checkForProgramLinkCompletionAsync() {
2854
+ _checkForProgramLinkCompletionAsync = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(gl, program, progOptions) {
2855
+ var ext, checkFn, waitTime, success, err, errFn;
2856
+ return regeneratorRuntime.wrap(function _callee$(_context) {
2857
+ while (1) {
2858
+ switch (_context.prev = _context.next) {
2859
+ case 0:
2860
+ ext = gl.getExtension('KHR_parallel_shader_compile');
2861
+ checkFn = ext ? function (gl, program) {
2862
+ return gl.getProgramParameter(program, ext.COMPLETION_STATUS_KHR);
2863
+ } : function () {
2864
+ return true;
2865
+ };
2866
+ waitTime = 0;
2867
+
2868
+ case 3:
2869
+ _context.next = 5;
2870
+ return wait(waitTime);
2871
+
2872
+ case 5:
2873
+ // must wait at least once
2874
+ waitTime = 1000 / 60;
2875
+
2876
+ case 6:
2877
+ if (!checkFn(gl, program)) {
2878
+ _context.next = 3;
2879
+ break;
2880
+ }
2881
+
2882
+ case 7:
2883
+ success = checkProgramStatus(gl, program, progOptions.errorCallback);
2884
+ err = success ? undefined : progOptions.errors.join('\n');
2885
+
2886
+ if (!success) {
2887
+ errFn = progOptions.errorCallback || error;
2888
+ errFn(err);
2889
+ gl.deleteProgram(program); // TODO: delete shaders, but only shaders that were created newly for this
2890
+ // program
2891
+
2892
+ program = null;
2893
+ }
2894
+
2895
+ progOptions.callback(err, program);
2896
+
2897
+ case 11:
2898
+ case "end":
2899
+ return _context.stop();
2900
+ }
2901
+ }
2902
+ }, _callee);
2903
+ }));
2904
+ return _checkForProgramLinkCompletionAsync.apply(this, arguments);
2905
+ }
2906
+
2907
+ function checkProgramStatus(gl, program, errFn) {
2908
+ errFn = errFn || error; // Check the link status
2637
2909
 
2638
2910
  var linked = gl.getProgramParameter(program, LINK_STATUS);
2639
2911
 
2640
2912
  if (!linked) {
2641
2913
  // something went wrong with the link
2642
2914
  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;
2915
+ errFn("Error in program linking: ".concat(lastError));
2651
2916
  }
2652
2917
 
2653
- return program;
2918
+ return linked;
2654
2919
  }
2655
2920
  /**
2656
2921
  * Loads a shader from a script tag.
@@ -2658,28 +2923,28 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
2658
2923
  * @param {string} scriptId The id of the script tag.
2659
2924
  * @param {number} [opt_shaderType] The type of shader. If not passed in it will
2660
2925
  * be derived from the type of the script tag.
2661
- * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors.
2926
+ * @param {module:twgl.ProgramOptions} [progOptions] callback for errors.
2662
2927
  * @return {WebGLShader?} The created shader or null if error.
2663
2928
  * @private
2664
2929
  */
2665
2930
 
2666
2931
 
2667
- function createShaderFromScript(gl, scriptId, opt_shaderType, opt_errorCallback) {
2932
+ function createShaderFromScript(gl, scriptId, opt_shaderType, progOptions) {
2668
2933
  var shaderSource = "";
2669
2934
  var shaderScript = getElementById(scriptId);
2670
2935
 
2671
2936
  if (!shaderScript) {
2672
- throw new Error("unknown script element: ".concat(scriptId));
2937
+ return reportError(progOptions, "unknown script element: ".concat(scriptId));
2673
2938
  }
2674
2939
 
2675
2940
  shaderSource = shaderScript.text;
2676
2941
  var shaderType = opt_shaderType || getShaderTypeFromScriptType(gl, shaderScript.type);
2677
2942
 
2678
2943
  if (!shaderType) {
2679
- throw new Error('unknown shader type');
2944
+ return reportError(progOptions, 'unknown shader type');
2680
2945
  }
2681
2946
 
2682
- return loadShader(gl, shaderSource, shaderType, opt_errorCallback);
2947
+ return loadShader(gl, shaderSource, shaderType, progOptions);
2683
2948
  }
2684
2949
  /**
2685
2950
  * Creates a program from 2 script tags.
@@ -2700,7 +2965,7 @@ function createShaderFromScript(gl, scriptId, opt_shaderType, opt_errorCallback)
2700
2965
  * @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
2966
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2702
2967
  * 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.
2968
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
2704
2969
  * @memberOf module:twgl/programs
2705
2970
  */
2706
2971
 
@@ -2710,7 +2975,7 @@ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_location
2710
2975
  var shaders = [];
2711
2976
 
2712
2977
  for (var ii = 0; ii < shaderScriptIds.length; ++ii) {
2713
- var shader = createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
2978
+ var shader = createShaderFromScript(gl, shaderScriptIds[ii], gl[defaultShaderType[ii]], progOptions);
2714
2979
 
2715
2980
  if (!shader) {
2716
2981
  return null;
@@ -2740,7 +3005,7 @@ function createProgramFromScripts(gl, shaderScriptIds, opt_attribs, opt_location
2740
3005
  * @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
3006
  * @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
2742
3007
  * 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.
3008
+ * @return {WebGLProgram?} the created program or null if error or a callback was provided.
2744
3009
  * @memberOf module:twgl/programs
2745
3010
  */
2746
3011
 
@@ -2750,9 +3015,9 @@ function createProgramFromSources(gl, shaderSources, opt_attribs, opt_locations,
2750
3015
  var shaders = [];
2751
3016
 
2752
3017
  for (var ii = 0; ii < shaderSources.length; ++ii) {
2753
- var shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions.errorCallback);
3018
+ var shader = loadShader(gl, shaderSources[ii], gl[defaultShaderType[ii]], progOptions);
2754
3019
 
2755
- if (!shader) {
3020
+ if (!progOptions.callback && !shader) {
2756
3021
  return null;
2757
3022
  }
2758
3023
 
@@ -3720,6 +3985,7 @@ function createAttributeSetters(gl, program) {
3720
3985
  * @param {Object.<string, module:twgl.AttribInfo>} buffers AttribInfos mapped by attribute name.
3721
3986
  * @memberOf module:twgl/programs
3722
3987
  * @deprecated use {@link module:twgl.setBuffersAndAttributes}
3988
+ * @private
3723
3989
  */
3724
3990
 
3725
3991
 
@@ -3827,6 +4093,8 @@ function createProgramInfoFromProgram(gl, program) {
3827
4093
 
3828
4094
  return programInfo;
3829
4095
  }
4096
+
4097
+ var notIdRE = /\s|{|}|;/;
3830
4098
  /**
3831
4099
  * Creates a ProgramInfo from 2 sources.
3832
4100
  *
@@ -3858,18 +4126,18 @@ function createProgramInfoFromProgram(gl, program) {
3858
4126
  * @memberOf module:twgl/programs
3859
4127
  */
3860
4128
 
3861
-
3862
4129
  function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
3863
4130
  var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
3864
- var good = true;
4131
+ var errors = [];
3865
4132
  shaderSources = shaderSources.map(function (source) {
3866
4133
  // Lets assume if there is no \n it's an id
3867
- if (source.indexOf("\n") < 0) {
4134
+ if (!notIdRE.test(source)) {
3868
4135
  var script = getElementById(source);
3869
4136
 
3870
4137
  if (!script) {
3871
- progOptions.errorCallback("no element with id: " + source);
3872
- good = false;
4138
+ var err = "no element with id: ".concat(source);
4139
+ progOptions.errorCallback(err);
4140
+ errors.push(err);
3873
4141
  } else {
3874
4142
  source = script.text;
3875
4143
  }
@@ -3878,8 +4146,22 @@ function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_er
3878
4146
  return source;
3879
4147
  });
3880
4148
 
3881
- if (!good) {
3882
- return null;
4149
+ if (errors.length) {
4150
+ return reportError(progOptions, '');
4151
+ }
4152
+
4153
+ var origCallback = progOptions.callback;
4154
+
4155
+ if (origCallback) {
4156
+ progOptions.callback = function (err, program) {
4157
+ var programInfo;
4158
+
4159
+ if (!err) {
4160
+ programInfo = createProgramInfoFromProgram(gl, program);
4161
+ }
4162
+
4163
+ origCallback(err, programInfo);
4164
+ };
3883
4165
  }
3884
4166
 
3885
4167
  var program = createProgramFromSources(gl, shaderSources, progOptions);
@@ -5540,6 +5822,7 @@ function loadAndUseImage(obj, crossOrigin, callback) {
5540
5822
  * @param {module:twgl.TextureOptions} [options] A TextureOptions object with whatever parameters you want set.
5541
5823
  * This is often the same options you passed in when you created the texture.
5542
5824
  * @memberOf module:twgl/textures
5825
+ * @private
5543
5826
  */
5544
5827
 
5545
5828
 
@@ -5654,6 +5937,7 @@ function loadTextureFromUrl(gl, tex, options, callback) {
5654
5937
  * @param {module:twgl.CubemapReadyCallback} [callback] A function to be called when all the images have finished loading. err will
5655
5938
  * be non null if there was an error.
5656
5939
  * @memberOf module:twgl/textures
5940
+ * @private
5657
5941
  */
5658
5942
 
5659
5943
 
@@ -5742,6 +6026,7 @@ function loadCubemapFromUrls(gl, tex, options, callback) {
5742
6026
  * @param {module:twgl.ThreeDReadyCallback} [callback] A function to be called when all the images have finished loading. err will
5743
6027
  * be non null if there was an error.
5744
6028
  * @memberOf module:twgl/textures
6029
+ * @private
5745
6030
  */
5746
6031
 
5747
6032
 
@@ -6626,6 +6911,8 @@ function create3DContext(canvas, opt_attribs) {
6626
6911
  * @param {WebGLContextAttributes} [opt_attribs] optional webgl context creation attributes
6627
6912
  * @return {WebGLRenderingContext} The created context.
6628
6913
  * @memberOf module:twgl
6914
+ * @deprecated
6915
+ * @private
6629
6916
  */
6630
6917
 
6631
6918
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twgl.js",
3
- "version": "4.22.1",
3
+ "version": "4.23.2",
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",