twgl.js 4.21.2 → 4.23.0
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/README.md +1 -1
- package/dist/4.x/twgl-full.d.ts +12 -2
- package/dist/4.x/twgl-full.js +352 -53
- package/dist/4.x/twgl-full.module.js +268 -55
- package/dist/4.x/twgl.d.ts +11 -2
- package/dist/4.x/twgl.js +341 -53
- package/package.json +2 -5
package/dist/4.x/twgl.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* @license twgl.js 4.
|
|
2
|
+
* @license twgl.js 4.23.0 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
|
*/
|
|
@@ -1197,6 +1197,7 @@ var LINEAR = 0x2601;
|
|
|
1197
1197
|
* @property {number} [type] The type. Used for texture. Default = `gl.UNSIGNED_BYTE`.
|
|
1198
1198
|
* @property {number} [target] The texture target for `gl.framebufferTexture2D`.
|
|
1199
1199
|
* Defaults to `gl.TEXTURE_2D`. Set to appropriate face for cube maps.
|
|
1200
|
+
* @property {number} [samples] The number of samples. Default = 1
|
|
1200
1201
|
* @property {number} [level] level for `gl.framebufferTexture2D`. Defaults to 0.
|
|
1201
1202
|
* @property {number} [layer] layer for `gl.framebufferTextureLayer`. Defaults to undefined.
|
|
1202
1203
|
* If set then `gl.framebufferTextureLayer` is called, if not then `gl.framebufferTexture2D`
|
|
@@ -1305,6 +1306,7 @@ function createFramebufferInfo(gl, attachments, width, height) {
|
|
|
1305
1306
|
};
|
|
1306
1307
|
attachments.forEach(function (attachmentOptions) {
|
|
1307
1308
|
var attachment = attachmentOptions.attachment;
|
|
1309
|
+
var samples = attachmentOptions.samples;
|
|
1308
1310
|
var format = attachmentOptions.format;
|
|
1309
1311
|
var attachmentPoint = attachmentOptions.attachmentPoint || getAttachmentPointForFormat(format, attachmentOptions.internalFormat);
|
|
1310
1312
|
|
|
@@ -1313,10 +1315,15 @@ function createFramebufferInfo(gl, attachments, width, height) {
|
|
|
1313
1315
|
}
|
|
1314
1316
|
|
|
1315
1317
|
if (!attachment) {
|
|
1316
|
-
if (isRenderbufferFormat(format)) {
|
|
1318
|
+
if (samples !== undefined || isRenderbufferFormat(format)) {
|
|
1317
1319
|
attachment = gl.createRenderbuffer();
|
|
1318
1320
|
gl.bindRenderbuffer(RENDERBUFFER, attachment);
|
|
1319
|
-
|
|
1321
|
+
|
|
1322
|
+
if (samples > 1) {
|
|
1323
|
+
gl.renderbufferStorageMultisample(RENDERBUFFER, samples, format, width, height);
|
|
1324
|
+
} else {
|
|
1325
|
+
gl.renderbufferStorage(RENDERBUFFER, format, width, height);
|
|
1326
|
+
}
|
|
1320
1327
|
} else {
|
|
1321
1328
|
var textureOptions = Object.assign({}, attachmentOptions);
|
|
1322
1329
|
textureOptions.width = width;
|
|
@@ -1404,10 +1411,16 @@ function resizeFramebufferInfo(gl, framebufferInfo, attachments, width, height)
|
|
|
1404
1411
|
attachments.forEach(function (attachmentOptions, ndx) {
|
|
1405
1412
|
var attachment = framebufferInfo.attachments[ndx];
|
|
1406
1413
|
var format = attachmentOptions.format;
|
|
1414
|
+
var samples = attachmentOptions.samples;
|
|
1407
1415
|
|
|
1408
|
-
if (helper.isRenderbuffer(gl, attachment)) {
|
|
1416
|
+
if (samples !== undefined || helper.isRenderbuffer(gl, attachment)) {
|
|
1409
1417
|
gl.bindRenderbuffer(RENDERBUFFER, attachment);
|
|
1410
|
-
|
|
1418
|
+
|
|
1419
|
+
if (samples > 1) {
|
|
1420
|
+
gl.renderbufferStorageMultisample(RENDERBUFFER, samples, format, width, height);
|
|
1421
|
+
} else {
|
|
1422
|
+
gl.renderbufferStorage(RENDERBUFFER, format, width, height);
|
|
1423
|
+
}
|
|
1411
1424
|
} else if (helper.isTexture(gl, attachment)) {
|
|
1412
1425
|
textures.resizeTexture(gl, attachment, attachmentOptions, width, height);
|
|
1413
1426
|
} else {
|
|
@@ -1579,9 +1592,11 @@ function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "functi
|
|
|
1579
1592
|
exports.__esModule = true;
|
|
1580
1593
|
exports.createAttributeSetters = createAttributeSetters;
|
|
1581
1594
|
exports.createProgram = createProgram;
|
|
1595
|
+
exports.createProgramAsync = createProgramAsync;
|
|
1582
1596
|
exports.createProgramFromScripts = createProgramFromScripts;
|
|
1583
1597
|
exports.createProgramFromSources = createProgramFromSources;
|
|
1584
1598
|
exports.createProgramInfo = createProgramInfo;
|
|
1599
|
+
exports.createProgramInfoAsync = createProgramInfoAsync;
|
|
1585
1600
|
exports.createProgramInfoFromProgram = createProgramInfoFromProgram;
|
|
1586
1601
|
exports.createUniformSetters = createUniformSetters;
|
|
1587
1602
|
exports.createUniformBlockSpecFromProgram = createUniformBlockSpecFromProgram;
|
|
@@ -1606,6 +1621,10 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
|
|
|
1606
1621
|
|
|
1607
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; }
|
|
1608
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
|
+
|
|
1609
1628
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
1610
1629
|
|
|
1611
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."); }
|
|
@@ -2402,56 +2421,91 @@ function addLineNumbersWithError(src) {
|
|
|
2402
2421
|
* @memberOf module:twgl
|
|
2403
2422
|
*/
|
|
2404
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
|
+
|
|
2405
2431
|
|
|
2406
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
|
+
}
|
|
2407
2462
|
/**
|
|
2408
2463
|
* Loads a shader.
|
|
2409
2464
|
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
2410
2465
|
* @param {string} shaderSource The shader source.
|
|
2411
2466
|
* @param {number} shaderType The type of shader.
|
|
2412
|
-
* @param {module:twgl.ErrorCallback} opt_errorCallback callback for errors.
|
|
2413
2467
|
* @return {WebGLShader} The created shader.
|
|
2414
2468
|
* @private
|
|
2415
2469
|
*/
|
|
2416
2470
|
|
|
2417
|
-
function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
|
|
2418
|
-
var errFn = opt_errorCallback || error; // Create the shader object
|
|
2419
2471
|
|
|
2420
|
-
|
|
2421
|
-
//
|
|
2422
|
-
|
|
2423
|
-
// so
|
|
2424
|
-
//
|
|
2425
|
-
// <script>
|
|
2426
|
-
// #version 300 es
|
|
2427
|
-
// </script>
|
|
2428
|
-
//
|
|
2429
|
-
// Has one line before it which is invalid according to GLSL ES 3.00
|
|
2430
|
-
//
|
|
2472
|
+
function loadShader(gl, shaderSource, shaderType) {
|
|
2473
|
+
// Create the shader object
|
|
2474
|
+
var shader = gl.createShader(shaderType); // Load the shader source
|
|
2431
2475
|
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
if (spaceRE.test(shaderSource)) {
|
|
2435
|
-
lineOffset = 1;
|
|
2436
|
-
shaderSource = shaderSource.replace(spaceRE, '');
|
|
2437
|
-
} // Load the shader source
|
|
2476
|
+
gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource); // Compile the shader
|
|
2438
2477
|
|
|
2478
|
+
gl.compileShader(shader);
|
|
2479
|
+
return shader;
|
|
2480
|
+
}
|
|
2481
|
+
/**
|
|
2482
|
+
* Check Shader status
|
|
2483
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
2484
|
+
* @param {WebGLShader} shader The shader
|
|
2485
|
+
* @param {ErrorCallback} [errFn] function to receive error message.
|
|
2486
|
+
* @returns {bool} true if shader is ok.
|
|
2487
|
+
*/
|
|
2439
2488
|
|
|
2440
|
-
gl.shaderSource(shader, shaderSource); // Compile the shader
|
|
2441
2489
|
|
|
2442
|
-
|
|
2490
|
+
function checkShaderStatus(gl, shaderType, shader, errFn) {
|
|
2491
|
+
errFn = errFn || error; // Check the compile status
|
|
2443
2492
|
|
|
2444
2493
|
var compiled = gl.getShaderParameter(shader, COMPILE_STATUS);
|
|
2445
2494
|
|
|
2446
2495
|
if (!compiled) {
|
|
2447
2496
|
// Something went wrong during compilation; get the error
|
|
2448
2497
|
var lastError = gl.getShaderInfoLog(shader);
|
|
2449
|
-
|
|
2450
|
-
gl.
|
|
2451
|
-
|
|
2498
|
+
|
|
2499
|
+
var _shaderType = gl.getShaderParameter(shader, gl.SHADER_TYPE);
|
|
2500
|
+
|
|
2501
|
+
var _prepShaderSource = prepShaderSource(gl.getShaderSource(shader)),
|
|
2502
|
+
lineOffset = _prepShaderSource.lineOffset,
|
|
2503
|
+
shaderSource = _prepShaderSource.shaderSource;
|
|
2504
|
+
|
|
2505
|
+
errFn("".concat(addLineNumbersWithError(shaderSource, lastError, lineOffset), "\nError compiling ").concat(utils.glEnumToString(gl, _shaderType), ": ").concat(lastError));
|
|
2452
2506
|
}
|
|
2453
2507
|
|
|
2454
|
-
return
|
|
2508
|
+
return compiled;
|
|
2455
2509
|
}
|
|
2456
2510
|
/**
|
|
2457
2511
|
* @typedef {Object} ProgramOptions
|
|
@@ -2461,6 +2515,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
|
|
|
2461
2515
|
* a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
|
|
2462
2516
|
* you can pass an array of names.
|
|
2463
2517
|
* @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
|
|
2518
|
+
* @property {ProgramCallback} [callback] callback for async program compilation.
|
|
2464
2519
|
* @memberOf module:twgl
|
|
2465
2520
|
*/
|
|
2466
2521
|
|
|
@@ -2478,6 +2533,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
|
|
|
2478
2533
|
function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
|
|
2479
2534
|
var transformFeedbackVaryings;
|
|
2480
2535
|
var transformFeedbackMode;
|
|
2536
|
+
var callback;
|
|
2481
2537
|
|
|
2482
2538
|
if (typeof opt_locations === 'function') {
|
|
2483
2539
|
opt_errorCallback = opt_locations;
|
|
@@ -2499,12 +2555,14 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
|
|
|
2499
2555
|
opt_attribs = opt.attribLocations;
|
|
2500
2556
|
transformFeedbackVaryings = opt.transformFeedbackVaryings;
|
|
2501
2557
|
transformFeedbackMode = opt.transformFeedbackMode;
|
|
2558
|
+
callback = opt.callback;
|
|
2502
2559
|
}
|
|
2503
2560
|
|
|
2504
2561
|
var options = {
|
|
2505
2562
|
errorCallback: opt_errorCallback || error,
|
|
2506
2563
|
transformFeedbackVaryings: transformFeedbackVaryings,
|
|
2507
|
-
transformFeedbackMode: transformFeedbackMode
|
|
2564
|
+
transformFeedbackMode: transformFeedbackMode,
|
|
2565
|
+
callback: callback
|
|
2508
2566
|
};
|
|
2509
2567
|
|
|
2510
2568
|
if (opt_attribs) {
|
|
@@ -2541,6 +2599,13 @@ function deleteShaders(gl, shaders) {
|
|
|
2541
2599
|
gl.deleteShader(shader);
|
|
2542
2600
|
});
|
|
2543
2601
|
}
|
|
2602
|
+
|
|
2603
|
+
var wait = function wait() {
|
|
2604
|
+
var ms = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
2605
|
+
return new Promise(function (resolve) {
|
|
2606
|
+
return setTimeout(resolve, ms);
|
|
2607
|
+
});
|
|
2608
|
+
};
|
|
2544
2609
|
/**
|
|
2545
2610
|
* Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
|
|
2546
2611
|
* program and calls useProgram.
|
|
@@ -2564,6 +2629,8 @@ function deleteShaders(gl, shaders) {
|
|
|
2564
2629
|
|
|
2565
2630
|
|
|
2566
2631
|
function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallback) {
|
|
2632
|
+
// This code is really convoluted, because it may or may not be async
|
|
2633
|
+
// Maybe it would be better to have a separate function
|
|
2567
2634
|
var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
|
|
2568
2635
|
var realShaders = [];
|
|
2569
2636
|
var newShaders = [];
|
|
@@ -2580,7 +2647,13 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
|
|
|
2580
2647
|
type = getShaderTypeFromScriptType(gl, elem.type) || type;
|
|
2581
2648
|
}
|
|
2582
2649
|
|
|
2583
|
-
shader = loadShader(gl, src, type
|
|
2650
|
+
shader = loadShader(gl, src, type);
|
|
2651
|
+
|
|
2652
|
+
if (!progOptions.callback && !checkShaderStatus(gl, shader, opt_errorCallback)) {
|
|
2653
|
+
gl.deleteShader(shader);
|
|
2654
|
+
shader = null;
|
|
2655
|
+
}
|
|
2656
|
+
|
|
2584
2657
|
newShaders.push(shader);
|
|
2585
2658
|
}
|
|
2586
2659
|
|
|
@@ -2620,24 +2693,196 @@ function createProgram(gl, shaders, opt_attribs, opt_locations, opt_errorCallbac
|
|
|
2620
2693
|
gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
|
|
2621
2694
|
}
|
|
2622
2695
|
|
|
2623
|
-
gl.linkProgram(program);
|
|
2696
|
+
gl.linkProgram(program);
|
|
2697
|
+
|
|
2698
|
+
if (progOptions.callback) {
|
|
2699
|
+
checkForProgramLinkCompletionAsync(gl, program, progOptions);
|
|
2700
|
+
return null;
|
|
2701
|
+
} else {
|
|
2702
|
+
if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
|
|
2703
|
+
gl.deleteProgram(program);
|
|
2704
|
+
deleteShaders(gl, newShaders);
|
|
2705
|
+
return null;
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2708
|
+
return program;
|
|
2709
|
+
}
|
|
2710
|
+
}
|
|
2711
|
+
/**
|
|
2712
|
+
* Same as createProgram but returns a promise
|
|
2713
|
+
*
|
|
2714
|
+
* NOTE: There are 4 signatures for this function
|
|
2715
|
+
*
|
|
2716
|
+
* twgl.createProgramAsync(gl, [vs, fs], options);
|
|
2717
|
+
* twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
|
|
2718
|
+
* twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
|
|
2719
|
+
* twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
|
|
2720
|
+
*
|
|
2721
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
2722
|
+
* @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
|
|
2723
|
+
* @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
|
|
2724
|
+
* @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.
|
|
2725
|
+
* @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
|
|
2726
|
+
* on error. If you want something else pass an callback. It's passed an error message.
|
|
2727
|
+
* @return {Promise<WebGLProgram>} The created program
|
|
2728
|
+
* @memberOf module:twgl/programs
|
|
2729
|
+
*/
|
|
2730
|
+
|
|
2731
|
+
|
|
2732
|
+
function createProgramAsync(gl, shaders) {
|
|
2733
|
+
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
2734
|
+
args[_key - 2] = arguments[_key];
|
|
2735
|
+
}
|
|
2736
|
+
|
|
2737
|
+
return new Promise(function (resolve, reject) {
|
|
2738
|
+
var programOptions = getProgramOptions.apply(void 0, args);
|
|
2739
|
+
|
|
2740
|
+
programOptions.callback = function (err, program) {
|
|
2741
|
+
if (err) {
|
|
2742
|
+
reject(err);
|
|
2743
|
+
} else {
|
|
2744
|
+
resolve(program);
|
|
2745
|
+
}
|
|
2746
|
+
};
|
|
2747
|
+
|
|
2748
|
+
twgl.createProgram(gl, shaders, programOptions);
|
|
2749
|
+
});
|
|
2750
|
+
}
|
|
2751
|
+
/**
|
|
2752
|
+
* Same as createProgramInfo but returns a promise
|
|
2753
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext
|
|
2754
|
+
* to use.
|
|
2755
|
+
* @param {string[]} shaderSources Array of sources for the
|
|
2756
|
+
* shaders or ids. The first is assumed to be the vertex shader,
|
|
2757
|
+
* the second the fragment shader.
|
|
2758
|
+
* @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
|
|
2759
|
+
* @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.
|
|
2760
|
+
* @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
|
|
2761
|
+
* on error. If you want something else pass an callback. It's passed an error message.
|
|
2762
|
+
* @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
|
|
2763
|
+
* @memberOf module:twgl/programs
|
|
2764
|
+
*/
|
|
2765
|
+
|
|
2766
|
+
|
|
2767
|
+
function createProgramInfoAsync(gl, shaders) {
|
|
2768
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
|
|
2769
|
+
args[_key2 - 2] = arguments[_key2];
|
|
2770
|
+
}
|
|
2771
|
+
|
|
2772
|
+
return new Promise(function (resolve, reject) {
|
|
2773
|
+
var programOptions = getProgramOptions.apply(void 0, args);
|
|
2774
|
+
|
|
2775
|
+
programOptions.callback = function (err, programInfo) {
|
|
2776
|
+
if (err) {
|
|
2777
|
+
reject(err);
|
|
2778
|
+
} else {
|
|
2779
|
+
resolve(programInfo);
|
|
2780
|
+
}
|
|
2781
|
+
};
|
|
2782
|
+
|
|
2783
|
+
twgl.createProgramInfo(gl, shaders, programOptions);
|
|
2784
|
+
});
|
|
2785
|
+
}
|
|
2786
|
+
/**
|
|
2787
|
+
* Asynchronously wait for program to link.
|
|
2788
|
+
* Note: if 'KHR_parallel_shader_compile' extension does not
|
|
2789
|
+
* exist then compilation will not be truly async.
|
|
2790
|
+
* @param {WebGLRenderingContext} gl The context
|
|
2791
|
+
* @param {WebGLProgram} program The program
|
|
2792
|
+
* @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
|
|
2793
|
+
*/
|
|
2794
|
+
|
|
2795
|
+
|
|
2796
|
+
function checkForProgramLinkCompletionAsync(_x, _x2, _x3) {
|
|
2797
|
+
return _checkForProgramLinkCompletionAsync.apply(this, arguments);
|
|
2798
|
+
}
|
|
2799
|
+
/**
|
|
2800
|
+
* Check a program's link status
|
|
2801
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
2802
|
+
* @param {WebGLProgram} program Program to check
|
|
2803
|
+
* @param {ErrorCallback} [errFn] func for errors
|
|
2804
|
+
* @return {bool} true if program is ok
|
|
2805
|
+
*/
|
|
2806
|
+
|
|
2807
|
+
|
|
2808
|
+
function _checkForProgramLinkCompletionAsync() {
|
|
2809
|
+
_checkForProgramLinkCompletionAsync = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(gl, program, progOptions) {
|
|
2810
|
+
var ext, checkFn, waitTime, errMsgs, success, err, errFn;
|
|
2811
|
+
return regeneratorRuntime.wrap(function _callee$(_context) {
|
|
2812
|
+
while (1) {
|
|
2813
|
+
switch (_context.prev = _context.next) {
|
|
2814
|
+
case 0:
|
|
2815
|
+
ext = gl.getExtension('KHR_parallel_shader_compile');
|
|
2816
|
+
checkFn = ext ? function (gl, program) {
|
|
2817
|
+
return gl.getProgramParameter(program, ext.COMPLETION_STATUS_KHR);
|
|
2818
|
+
} : function () {
|
|
2819
|
+
return true;
|
|
2820
|
+
};
|
|
2821
|
+
waitTime = 0;
|
|
2822
|
+
|
|
2823
|
+
case 3:
|
|
2824
|
+
_context.next = 5;
|
|
2825
|
+
return wait(waitTime);
|
|
2826
|
+
|
|
2827
|
+
case 5:
|
|
2828
|
+
// must wait at least once
|
|
2829
|
+
waitTime = 1000 / 60;
|
|
2830
|
+
|
|
2831
|
+
case 6:
|
|
2832
|
+
if (!checkFn(gl, program)) {
|
|
2833
|
+
_context.next = 3;
|
|
2834
|
+
break;
|
|
2835
|
+
}
|
|
2836
|
+
|
|
2837
|
+
case 7:
|
|
2838
|
+
errMsgs = [];
|
|
2839
|
+
success = checkProgramStatus(gl, program, function (msg) {
|
|
2840
|
+
return errMsgs.push(msg);
|
|
2841
|
+
});
|
|
2842
|
+
err = success ? undefined : errMsgs.join('\n');
|
|
2843
|
+
|
|
2844
|
+
if (!success) {
|
|
2845
|
+
errFn = progOptions.errorCallback || error;
|
|
2846
|
+
errFn(err);
|
|
2847
|
+
gl.deleteProgram(program); // TODO: delete shaders, but only shaders that were created newly for this
|
|
2848
|
+
// program
|
|
2849
|
+
|
|
2850
|
+
program = null;
|
|
2851
|
+
}
|
|
2852
|
+
|
|
2853
|
+
progOptions.callback(err, program);
|
|
2854
|
+
|
|
2855
|
+
case 12:
|
|
2856
|
+
case "end":
|
|
2857
|
+
return _context.stop();
|
|
2858
|
+
}
|
|
2859
|
+
}
|
|
2860
|
+
}, _callee);
|
|
2861
|
+
}));
|
|
2862
|
+
return _checkForProgramLinkCompletionAsync.apply(this, arguments);
|
|
2863
|
+
}
|
|
2864
|
+
|
|
2865
|
+
function checkProgramStatus(gl, program, errFn) {
|
|
2866
|
+
errFn = errFn || error; // Check the link status
|
|
2624
2867
|
|
|
2625
2868
|
var linked = gl.getProgramParameter(program, LINK_STATUS);
|
|
2626
2869
|
|
|
2627
2870
|
if (!linked) {
|
|
2628
2871
|
// something went wrong with the link
|
|
2629
2872
|
var lastError = gl.getProgramInfoLog(program);
|
|
2630
|
-
|
|
2631
|
-
var
|
|
2873
|
+
errFn("".concat(gl.getAttachedShaders(program).map(function (shader) {
|
|
2874
|
+
var _prepShaderSource2 = prepShaderSource(gl.getShaderSource(shader)),
|
|
2875
|
+
lineOffset = _prepShaderSource2.lineOffset,
|
|
2876
|
+
shaderSource = _prepShaderSource2.shaderSource;
|
|
2877
|
+
|
|
2878
|
+
var shaderError = gl.getShaderInfoLog(shader);
|
|
2879
|
+
var src = addLineNumbersWithError(shaderSource, shaderError, lineOffset);
|
|
2632
2880
|
var type = gl.getShaderParameter(shader, gl.SHADER_TYPE);
|
|
2633
2881
|
return "".concat(utils.glEnumToString(gl, type), "\n").concat(src, "}");
|
|
2634
2882
|
}).join('\n'), "\nError in program linking: ").concat(lastError));
|
|
2635
|
-
gl.deleteProgram(program);
|
|
2636
|
-
deleteShaders(gl, newShaders);
|
|
2637
|
-
return null;
|
|
2638
2883
|
}
|
|
2639
2884
|
|
|
2640
|
-
return
|
|
2885
|
+
return linked;
|
|
2641
2886
|
}
|
|
2642
2887
|
/**
|
|
2643
2888
|
* Loads a shader from a script tag.
|
|
@@ -3346,6 +3591,7 @@ function setUniformBlock(gl, programInfo, uniformBlockInfo) {
|
|
|
3346
3591
|
* struct Light {
|
|
3347
3592
|
* float intensity;
|
|
3348
3593
|
* vec4 color;
|
|
3594
|
+
* float nearFar[2];
|
|
3349
3595
|
* };
|
|
3350
3596
|
* uniform Lights {
|
|
3351
3597
|
* Light lights[2];
|
|
@@ -3355,28 +3601,35 @@ function setUniformBlock(gl, programInfo, uniformBlockInfo) {
|
|
|
3355
3601
|
*
|
|
3356
3602
|
* twgl.setBlockUniforms(someBlockInfo, {
|
|
3357
3603
|
* lights: [
|
|
3358
|
-
* { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
3359
|
-
* { intensity: 2.0, color: [0, 0, 1, 1] },
|
|
3604
|
+
* { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
|
|
3605
|
+
* { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
|
|
3360
3606
|
* ],
|
|
3361
3607
|
* });
|
|
3362
3608
|
*
|
|
3363
|
-
*
|
|
3609
|
+
* or the more traditional way
|
|
3364
3610
|
*
|
|
3365
3611
|
* twgl.setBlockUniforms(someBlockInfo, {
|
|
3366
3612
|
* "lights[0].intensity": 5.0,
|
|
3367
3613
|
* "lights[0].color": [1, 0, 0, 1],
|
|
3614
|
+
* "lights[0].nearFar": [0.1, 10],
|
|
3368
3615
|
* "lights[1].intensity": 2.0,
|
|
3369
3616
|
* "lights[1].color": [0, 0, 1, 1],
|
|
3617
|
+
* "lights[1].nearFar": [0.2, 15],
|
|
3370
3618
|
* });
|
|
3371
3619
|
*
|
|
3372
3620
|
* You can also specify partial paths
|
|
3373
3621
|
*
|
|
3374
3622
|
* twgl.setBlockUniforms(someBlockInfo, {
|
|
3375
|
-
* 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
3623
|
+
* 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
|
|
3376
3624
|
* });
|
|
3377
3625
|
*
|
|
3378
3626
|
* But you can not specify leaf array indices.
|
|
3379
3627
|
*
|
|
3628
|
+
* twgl.setBlockUniforms(someBlockInfo, {
|
|
3629
|
+
* 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
|
|
3630
|
+
* 'lights[1].nearFar': [0.2, 15], // GOOD
|
|
3631
|
+
* });
|
|
3632
|
+
*
|
|
3380
3633
|
* **IMPORTANT!**, packing in a UniformBlock is unintuitive.
|
|
3381
3634
|
* For example the actual layout of `someVec3Array` above in memory
|
|
3382
3635
|
* is `1, 2, 3, unused, 4, 5, 6, unused`. twgl takes in 6 values
|
|
@@ -3536,6 +3789,7 @@ function setUniformTree(tree, values) {
|
|
|
3536
3789
|
* struct Light {
|
|
3537
3790
|
* float intensity;
|
|
3538
3791
|
* vec4 color;
|
|
3792
|
+
* float nearFar[2];
|
|
3539
3793
|
* };
|
|
3540
3794
|
* uniform Light lights[2];
|
|
3541
3795
|
*
|
|
@@ -3543,8 +3797,8 @@ function setUniformTree(tree, values) {
|
|
|
3543
3797
|
*
|
|
3544
3798
|
* twgl.setUniforms(programInfo, {
|
|
3545
3799
|
* lights: [
|
|
3546
|
-
* { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
3547
|
-
* { intensity: 2.0, color: [0, 0, 1, 1] },
|
|
3800
|
+
* { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
|
|
3801
|
+
* { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
|
|
3548
3802
|
* ],
|
|
3549
3803
|
* });
|
|
3550
3804
|
*
|
|
@@ -3553,18 +3807,25 @@ function setUniformTree(tree, values) {
|
|
|
3553
3807
|
* twgl.setUniforms(programInfo, {
|
|
3554
3808
|
* "lights[0].intensity": 5.0,
|
|
3555
3809
|
* "lights[0].color": [1, 0, 0, 1],
|
|
3810
|
+
* "lights[0].nearFar": [0.1, 10],
|
|
3556
3811
|
* "lights[1].intensity": 2.0,
|
|
3557
3812
|
* "lights[1].color": [0, 0, 1, 1],
|
|
3813
|
+
* "lights[1].nearFar": [0.2, 15],
|
|
3558
3814
|
* });
|
|
3559
3815
|
*
|
|
3560
3816
|
* You can also specify partial paths
|
|
3561
3817
|
*
|
|
3562
3818
|
* twgl.setUniforms(programInfo, {
|
|
3563
|
-
* 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
3819
|
+
* 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
|
|
3564
3820
|
* });
|
|
3565
3821
|
*
|
|
3566
3822
|
* But you can not specify leaf array indices
|
|
3567
3823
|
*
|
|
3824
|
+
* twgl.setUniforms(programInfo, {
|
|
3825
|
+
* 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
|
|
3826
|
+
* 'lights[1].nearFar': [0.2, 15], // GOOD
|
|
3827
|
+
* });
|
|
3828
|
+
*
|
|
3568
3829
|
* @memberOf module:twgl/programs
|
|
3569
3830
|
*/
|
|
3570
3831
|
|
|
@@ -3798,6 +4059,8 @@ function createProgramInfoFromProgram(gl, program) {
|
|
|
3798
4059
|
|
|
3799
4060
|
return programInfo;
|
|
3800
4061
|
}
|
|
4062
|
+
|
|
4063
|
+
var notIdRE = /\s|{|}|;/;
|
|
3801
4064
|
/**
|
|
3802
4065
|
* Creates a ProgramInfo from 2 sources.
|
|
3803
4066
|
*
|
|
@@ -3829,18 +4092,18 @@ function createProgramInfoFromProgram(gl, program) {
|
|
|
3829
4092
|
* @memberOf module:twgl/programs
|
|
3830
4093
|
*/
|
|
3831
4094
|
|
|
3832
|
-
|
|
3833
4095
|
function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
|
|
3834
4096
|
var progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
|
|
3835
|
-
var
|
|
4097
|
+
var errors = [];
|
|
3836
4098
|
shaderSources = shaderSources.map(function (source) {
|
|
3837
4099
|
// Lets assume if there is no \n it's an id
|
|
3838
|
-
if (
|
|
4100
|
+
if (!notIdRE.test(source)) {
|
|
3839
4101
|
var script = getElementById(source);
|
|
3840
4102
|
|
|
3841
4103
|
if (!script) {
|
|
3842
|
-
|
|
3843
|
-
|
|
4104
|
+
var err = "no element with id: ".concat(source);
|
|
4105
|
+
progOptions.errorCallback(err);
|
|
4106
|
+
errors.push(err);
|
|
3844
4107
|
} else {
|
|
3845
4108
|
source = script.text;
|
|
3846
4109
|
}
|
|
@@ -3849,13 +4112,32 @@ function createProgramInfo(gl, shaderSources, opt_attribs, opt_locations, opt_er
|
|
|
3849
4112
|
return source;
|
|
3850
4113
|
});
|
|
3851
4114
|
|
|
3852
|
-
if (
|
|
4115
|
+
if (errors.length) {
|
|
4116
|
+
if (progOptions.callback) {
|
|
4117
|
+
progOptions.callback(errors.join('\n'));
|
|
4118
|
+
}
|
|
4119
|
+
|
|
3853
4120
|
return null;
|
|
3854
4121
|
}
|
|
3855
4122
|
|
|
4123
|
+
var origCallback = progOptions.callback;
|
|
4124
|
+
|
|
4125
|
+
if (origCallback) {
|
|
4126
|
+
progOptions.callback = function (err, program) {
|
|
4127
|
+
var programInfo;
|
|
4128
|
+
|
|
4129
|
+
if (!err) {
|
|
4130
|
+
programInfo = createProgramInfoFromProgram(gl, program);
|
|
4131
|
+
}
|
|
4132
|
+
|
|
4133
|
+
origCallback(err, programInfo);
|
|
4134
|
+
};
|
|
4135
|
+
}
|
|
4136
|
+
|
|
3856
4137
|
var program = createProgramFromSources(gl, shaderSources, progOptions);
|
|
3857
4138
|
|
|
3858
4139
|
if (!program) {
|
|
4140
|
+
// we should never get if if we have a callback
|
|
3859
4141
|
return null;
|
|
3860
4142
|
}
|
|
3861
4143
|
|
|
@@ -7197,6 +7479,9 @@ function createVertexArrayInfo(gl, programInfos, bufferInfo) {
|
|
|
7197
7479
|
* @param {Object.<string, function>} setters Attribute setters as returned from createAttributeSetters
|
|
7198
7480
|
* @param {Object.<string, module:twgl.AttribInfo>} attribs AttribInfos mapped by attribute name.
|
|
7199
7481
|
* @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
|
|
7482
|
+
*
|
|
7483
|
+
* @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
|
|
7484
|
+
*
|
|
7200
7485
|
* @memberOf module:twgl/vertexArrays
|
|
7201
7486
|
*/
|
|
7202
7487
|
|
|
@@ -7224,6 +7509,9 @@ function createVAOAndSetAttributes(gl, setters, attribs, indices) {
|
|
|
7224
7509
|
* @param {Object.<string, function>| module:twgl.ProgramInfo} programInfo as returned from createProgramInfo or Attribute setters as returned from createAttributeSetters
|
|
7225
7510
|
* @param {module:twgl.BufferInfo} bufferInfo BufferInfo as returned from createBufferInfoFromArrays etc...
|
|
7226
7511
|
* @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
|
|
7512
|
+
*
|
|
7513
|
+
* @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
|
|
7514
|
+
*
|
|
7227
7515
|
* @memberOf module:twgl/vertexArrays
|
|
7228
7516
|
*/
|
|
7229
7517
|
|
package/package.json
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "twgl.js",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.23.0",
|
|
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",
|
|
7
7
|
"module": "dist/4.x/twgl-full.module.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"check-ci": "npm run lint && npm run build && npm test",
|
|
10
|
-
"test": "npm run
|
|
11
|
-
"testjs": "node node_modules/mocha/bin/mocha --recursive 'test/src/js/*-harness.js'",
|
|
10
|
+
"test": "npm run testts && npm run testbrowser",
|
|
12
11
|
"testts": "tsc --project test/src/ts/",
|
|
13
12
|
"testbrowser": "node test/puppeteer.js",
|
|
14
13
|
"pre-push": "grunt eslint",
|
|
@@ -73,13 +72,11 @@
|
|
|
73
72
|
"load-grunt-tasks": "^5.1.0",
|
|
74
73
|
"marked": "^1.1.1",
|
|
75
74
|
"minami": "^1.2.3",
|
|
76
|
-
"mocha": "^8.1.3",
|
|
77
75
|
"puppeteer": "^10.4.0",
|
|
78
76
|
"requirejs": "^2.3.5",
|
|
79
77
|
"rollup": "^2.27.1",
|
|
80
78
|
"semver": "^7.3.2",
|
|
81
79
|
"servez": "^1.10.0",
|
|
82
|
-
"should": "^13.2.0",
|
|
83
80
|
"terser-webpack-plugin": "^4.2.2",
|
|
84
81
|
"tsd-jsdoc": "^2.1.2",
|
|
85
82
|
"typescript": "^3.9.7",
|