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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @license twgl.js 4.
|
|
1
|
+
/* @license twgl.js 4.23.0 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
|
|
2
2
|
Available via the MIT license.
|
|
3
3
|
see: http://github.com/greggman/twgl.js for details */
|
|
4
4
|
/*
|
|
@@ -548,6 +548,15 @@ function negate$1(m, dst) {
|
|
|
548
548
|
return dst;
|
|
549
549
|
}
|
|
550
550
|
|
|
551
|
+
/**
|
|
552
|
+
* Creates a matrix.
|
|
553
|
+
* @return {module:twgl/m4.Mat4} A new matrix.
|
|
554
|
+
* @memberOf module:twgl/m4
|
|
555
|
+
*/
|
|
556
|
+
function create$1() {
|
|
557
|
+
return new MatType(16).fill(0);
|
|
558
|
+
}
|
|
559
|
+
|
|
551
560
|
/**
|
|
552
561
|
* Copies a matrix.
|
|
553
562
|
* @param {module:twgl/m4.Mat4} m The matrix.
|
|
@@ -1717,6 +1726,7 @@ var m4 = /*#__PURE__*/Object.freeze({
|
|
|
1717
1726
|
axisRotate: axisRotate,
|
|
1718
1727
|
axisRotation: axisRotation,
|
|
1719
1728
|
copy: copy$1,
|
|
1729
|
+
create: create$1,
|
|
1720
1730
|
frustum: frustum,
|
|
1721
1731
|
getAxis: getAxis,
|
|
1722
1732
|
getTranslation: getTranslation,
|
|
@@ -7243,56 +7253,79 @@ function addLineNumbersWithError(src, log = '', lineOffset = 0) {
|
|
|
7243
7253
|
* @memberOf module:twgl
|
|
7244
7254
|
*/
|
|
7245
7255
|
|
|
7256
|
+
/**
|
|
7257
|
+
* Program Callback
|
|
7258
|
+
* @callback ProgramCallback
|
|
7259
|
+
* @param {string} [err] error message, falsy if no error
|
|
7260
|
+
* @param {WebGLProgram|module:twgl.ProgramInfo} [result] the program or programInfo
|
|
7261
|
+
*/
|
|
7262
|
+
|
|
7246
7263
|
const spaceRE = /^[ \t]*\n/;
|
|
7247
7264
|
|
|
7265
|
+
/**
|
|
7266
|
+
* Remove the first end of line because WebGL 2.0 requires
|
|
7267
|
+
* #version 300 es
|
|
7268
|
+
* as the first line. No whitespace allowed before that line
|
|
7269
|
+
* so
|
|
7270
|
+
*
|
|
7271
|
+
* <script>
|
|
7272
|
+
* #version 300 es
|
|
7273
|
+
* </script>
|
|
7274
|
+
*
|
|
7275
|
+
* Has one line before it which is invalid according to GLSL ES 3.00
|
|
7276
|
+
*
|
|
7277
|
+
* @param {string} shaderSource The source of the shader
|
|
7278
|
+
* @returns {{shaderSource: string, lineOffset: number}}
|
|
7279
|
+
*/
|
|
7280
|
+
function prepShaderSource(shaderSource) {
|
|
7281
|
+
let lineOffset = 0;
|
|
7282
|
+
if (spaceRE.test(shaderSource)) {
|
|
7283
|
+
lineOffset = 1;
|
|
7284
|
+
shaderSource = shaderSource.replace(spaceRE, '');
|
|
7285
|
+
}
|
|
7286
|
+
return {lineOffset, shaderSource};
|
|
7287
|
+
}
|
|
7288
|
+
|
|
7248
7289
|
/**
|
|
7249
7290
|
* Loads a shader.
|
|
7250
7291
|
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
7251
7292
|
* @param {string} shaderSource The shader source.
|
|
7252
7293
|
* @param {number} shaderType The type of shader.
|
|
7253
|
-
* @param {module:twgl.ErrorCallback} opt_errorCallback callback for errors.
|
|
7254
7294
|
* @return {WebGLShader} The created shader.
|
|
7255
7295
|
* @private
|
|
7256
7296
|
*/
|
|
7257
|
-
function loadShader(gl, shaderSource, shaderType
|
|
7258
|
-
const errFn = opt_errorCallback || error$1;
|
|
7297
|
+
function loadShader(gl, shaderSource, shaderType) {
|
|
7259
7298
|
// Create the shader object
|
|
7260
7299
|
const shader = gl.createShader(shaderType);
|
|
7261
7300
|
|
|
7262
|
-
// Remove the first end of line because WebGL 2.0 requires
|
|
7263
|
-
// #version 300 es
|
|
7264
|
-
// as the first line. No whitespace allowed before that line
|
|
7265
|
-
// so
|
|
7266
|
-
//
|
|
7267
|
-
// <script>
|
|
7268
|
-
// #version 300 es
|
|
7269
|
-
// </script>
|
|
7270
|
-
//
|
|
7271
|
-
// Has one line before it which is invalid according to GLSL ES 3.00
|
|
7272
|
-
//
|
|
7273
|
-
let lineOffset = 0;
|
|
7274
|
-
if (spaceRE.test(shaderSource)) {
|
|
7275
|
-
lineOffset = 1;
|
|
7276
|
-
shaderSource = shaderSource.replace(spaceRE, '');
|
|
7277
|
-
}
|
|
7278
|
-
|
|
7279
7301
|
// Load the shader source
|
|
7280
|
-
gl.shaderSource(shader, shaderSource);
|
|
7302
|
+
gl.shaderSource(shader, prepShaderSource(shaderSource).shaderSource);
|
|
7281
7303
|
|
|
7282
7304
|
// Compile the shader
|
|
7283
7305
|
gl.compileShader(shader);
|
|
7284
7306
|
|
|
7307
|
+
return shader;
|
|
7308
|
+
}
|
|
7309
|
+
|
|
7310
|
+
/**
|
|
7311
|
+
* Check Shader status
|
|
7312
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
7313
|
+
* @param {WebGLShader} shader The shader
|
|
7314
|
+
* @param {ErrorCallback} [errFn] function to receive error message.
|
|
7315
|
+
* @returns {bool} true if shader is ok.
|
|
7316
|
+
*/
|
|
7317
|
+
function checkShaderStatus(gl, shaderType, shader, errFn) {
|
|
7318
|
+
errFn = errFn || error$1;
|
|
7285
7319
|
// Check the compile status
|
|
7286
7320
|
const compiled = gl.getShaderParameter(shader, COMPILE_STATUS);
|
|
7287
7321
|
if (!compiled) {
|
|
7288
7322
|
// Something went wrong during compilation; get the error
|
|
7289
7323
|
const lastError = gl.getShaderInfoLog(shader);
|
|
7324
|
+
const shaderType = gl.getShaderParameter(shader, gl.SHADER_TYPE);
|
|
7325
|
+
const {lineOffset, shaderSource} = prepShaderSource(gl.getShaderSource(shader));
|
|
7290
7326
|
errFn(`${addLineNumbersWithError(shaderSource, lastError, lineOffset)}\nError compiling ${glEnumToString(gl, shaderType)}: ${lastError}`);
|
|
7291
|
-
gl.deleteShader(shader);
|
|
7292
|
-
return null;
|
|
7293
7327
|
}
|
|
7294
|
-
|
|
7295
|
-
return shader;
|
|
7328
|
+
return compiled;
|
|
7296
7329
|
}
|
|
7297
7330
|
|
|
7298
7331
|
/**
|
|
@@ -7303,6 +7336,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
|
|
|
7303
7336
|
* a BufferInfo will use the attribs names inside. If passed an object of AttribInfos will use the names from that object. Otherwise
|
|
7304
7337
|
* you can pass an array of names.
|
|
7305
7338
|
* @property {number} [transformFeedbackMode] the mode to pass `gl.transformFeedbackVaryings`. Defaults to `SEPARATE_ATTRIBS`.
|
|
7339
|
+
* @property {ProgramCallback} [callback] callback for async program compilation.
|
|
7306
7340
|
* @memberOf module:twgl
|
|
7307
7341
|
*/
|
|
7308
7342
|
|
|
@@ -7318,6 +7352,7 @@ function loadShader(gl, shaderSource, shaderType, opt_errorCallback) {
|
|
|
7318
7352
|
function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
|
|
7319
7353
|
let transformFeedbackVaryings;
|
|
7320
7354
|
let transformFeedbackMode;
|
|
7355
|
+
let callback;
|
|
7321
7356
|
if (typeof opt_locations === 'function') {
|
|
7322
7357
|
opt_errorCallback = opt_locations;
|
|
7323
7358
|
opt_locations = undefined;
|
|
@@ -7336,12 +7371,14 @@ function getProgramOptions(opt_attribs, opt_locations, opt_errorCallback) {
|
|
|
7336
7371
|
opt_attribs = opt.attribLocations;
|
|
7337
7372
|
transformFeedbackVaryings = opt.transformFeedbackVaryings;
|
|
7338
7373
|
transformFeedbackMode = opt.transformFeedbackMode;
|
|
7374
|
+
callback = opt.callback;
|
|
7339
7375
|
}
|
|
7340
7376
|
|
|
7341
7377
|
const options = {
|
|
7342
7378
|
errorCallback: opt_errorCallback || error$1,
|
|
7343
|
-
transformFeedbackVaryings
|
|
7344
|
-
transformFeedbackMode
|
|
7379
|
+
transformFeedbackVaryings,
|
|
7380
|
+
transformFeedbackMode,
|
|
7381
|
+
callback,
|
|
7345
7382
|
};
|
|
7346
7383
|
|
|
7347
7384
|
if (opt_attribs) {
|
|
@@ -7379,6 +7416,8 @@ function deleteShaders(gl, shaders) {
|
|
|
7379
7416
|
});
|
|
7380
7417
|
}
|
|
7381
7418
|
|
|
7419
|
+
const wait = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));
|
|
7420
|
+
|
|
7382
7421
|
/**
|
|
7383
7422
|
* Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
|
|
7384
7423
|
* program and calls useProgram.
|
|
@@ -7401,6 +7440,8 @@ function deleteShaders(gl, shaders) {
|
|
|
7401
7440
|
*/
|
|
7402
7441
|
function createProgram(
|
|
7403
7442
|
gl, shaders, opt_attribs, opt_locations, opt_errorCallback) {
|
|
7443
|
+
// This code is really convoluted, because it may or may not be async
|
|
7444
|
+
// Maybe it would be better to have a separate function
|
|
7404
7445
|
const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
|
|
7405
7446
|
const realShaders = [];
|
|
7406
7447
|
const newShaders = [];
|
|
@@ -7413,7 +7454,11 @@ function createProgram(
|
|
|
7413
7454
|
if (elem && elem.type) {
|
|
7414
7455
|
type = getShaderTypeFromScriptType(gl, elem.type) || type;
|
|
7415
7456
|
}
|
|
7416
|
-
shader = loadShader(gl, src, type
|
|
7457
|
+
shader = loadShader(gl, src, type);
|
|
7458
|
+
if (!progOptions.callback && !checkShaderStatus(gl, shader, opt_errorCallback)) {
|
|
7459
|
+
gl.deleteShader(shader);
|
|
7460
|
+
shader = null;
|
|
7461
|
+
}
|
|
7417
7462
|
newShaders.push(shader);
|
|
7418
7463
|
}
|
|
7419
7464
|
if (isShader(gl, shader)) {
|
|
@@ -7446,26 +7491,142 @@ function createProgram(
|
|
|
7446
7491
|
}
|
|
7447
7492
|
gl.transformFeedbackVaryings(program, varyings, progOptions.transformFeedbackMode || SEPARATE_ATTRIBS);
|
|
7448
7493
|
}
|
|
7494
|
+
|
|
7449
7495
|
gl.linkProgram(program);
|
|
7496
|
+
if (progOptions.callback) {
|
|
7497
|
+
checkForProgramLinkCompletionAsync(gl, program, progOptions);
|
|
7498
|
+
return null;
|
|
7499
|
+
} else {
|
|
7500
|
+
if (!checkProgramStatus(gl, program, progOptions.errorCallback)) {
|
|
7501
|
+
gl.deleteProgram(program);
|
|
7502
|
+
deleteShaders(gl, newShaders);
|
|
7503
|
+
return null;
|
|
7504
|
+
}
|
|
7505
|
+
return program;
|
|
7506
|
+
}
|
|
7507
|
+
}
|
|
7450
7508
|
|
|
7509
|
+
/**
|
|
7510
|
+
* Same as createProgram but returns a promise
|
|
7511
|
+
*
|
|
7512
|
+
* NOTE: There are 4 signatures for this function
|
|
7513
|
+
*
|
|
7514
|
+
* twgl.createProgramAsync(gl, [vs, fs], options);
|
|
7515
|
+
* twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
|
|
7516
|
+
* twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
|
|
7517
|
+
* twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
|
|
7518
|
+
*
|
|
7519
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
7520
|
+
* @param {WebGLShader[]|string[]} shaders The shaders to attach, or element ids for their source, or strings that contain their source
|
|
7521
|
+
* @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
|
|
7522
|
+
* @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.
|
|
7523
|
+
* @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
|
|
7524
|
+
* on error. If you want something else pass an callback. It's passed an error message.
|
|
7525
|
+
* @return {Promise<WebGLProgram>} The created program
|
|
7526
|
+
* @memberOf module:twgl/programs
|
|
7527
|
+
*/
|
|
7528
|
+
function createProgramAsync(gl, shaders, ...args) {
|
|
7529
|
+
return new Promise((resolve, reject) => {
|
|
7530
|
+
const programOptions = getProgramOptions(...args);
|
|
7531
|
+
programOptions.callback = (err, program) => {
|
|
7532
|
+
if (err) {
|
|
7533
|
+
reject(err);
|
|
7534
|
+
} else {
|
|
7535
|
+
resolve(program);
|
|
7536
|
+
}
|
|
7537
|
+
};
|
|
7538
|
+
twgl.createProgram(gl, shaders, programOptions);
|
|
7539
|
+
});
|
|
7540
|
+
}
|
|
7541
|
+
|
|
7542
|
+
/**
|
|
7543
|
+
* Same as createProgramInfo but returns a promise
|
|
7544
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext
|
|
7545
|
+
* to use.
|
|
7546
|
+
* @param {string[]} shaderSources Array of sources for the
|
|
7547
|
+
* shaders or ids. The first is assumed to be the vertex shader,
|
|
7548
|
+
* the second the fragment shader.
|
|
7549
|
+
* @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
|
|
7550
|
+
* @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.
|
|
7551
|
+
* @param {module:twgl.ErrorCallback} [opt_errorCallback] callback for errors. By default it just prints an error to the console
|
|
7552
|
+
* on error. If you want something else pass an callback. It's passed an error message.
|
|
7553
|
+
* @return {Promise<module:twgl.ProgramInfo>} The created ProgramInfo
|
|
7554
|
+
* @memberOf module:twgl/programs
|
|
7555
|
+
*/
|
|
7556
|
+
function createProgramInfoAsync(gl, shaders, ...args) {
|
|
7557
|
+
return new Promise((resolve, reject) => {
|
|
7558
|
+
const programOptions = getProgramOptions(...args);
|
|
7559
|
+
programOptions.callback = (err, programInfo) => {
|
|
7560
|
+
if (err) {
|
|
7561
|
+
reject(err);
|
|
7562
|
+
} else {
|
|
7563
|
+
resolve(programInfo);
|
|
7564
|
+
}
|
|
7565
|
+
};
|
|
7566
|
+
twgl.createProgramInfo(gl, shaders, programOptions);
|
|
7567
|
+
});
|
|
7568
|
+
}
|
|
7569
|
+
|
|
7570
|
+
|
|
7571
|
+
/**
|
|
7572
|
+
* Asynchronously wait for program to link.
|
|
7573
|
+
* Note: if 'KHR_parallel_shader_compile' extension does not
|
|
7574
|
+
* exist then compilation will not be truly async.
|
|
7575
|
+
* @param {WebGLRenderingContext} gl The context
|
|
7576
|
+
* @param {WebGLProgram} program The program
|
|
7577
|
+
* @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
|
|
7578
|
+
*/
|
|
7579
|
+
async function checkForProgramLinkCompletionAsync(gl, program, progOptions) {
|
|
7580
|
+
const ext = gl.getExtension('KHR_parallel_shader_compile');
|
|
7581
|
+
const checkFn = ext
|
|
7582
|
+
? (gl, program) => gl.getProgramParameter(program, ext.COMPLETION_STATUS_KHR)
|
|
7583
|
+
: () => true;
|
|
7584
|
+
|
|
7585
|
+
let waitTime = 0;
|
|
7586
|
+
do {
|
|
7587
|
+
await wait(waitTime); // must wait at least once
|
|
7588
|
+
waitTime = 1000 / 60;
|
|
7589
|
+
} while (!checkFn(gl, program));
|
|
7590
|
+
|
|
7591
|
+
const errMsgs = [];
|
|
7592
|
+
const success = checkProgramStatus(gl, program, (msg) => errMsgs.push(msg));
|
|
7593
|
+
const err = success ? undefined : errMsgs.join('\n');
|
|
7594
|
+
if (!success) {
|
|
7595
|
+
const errFn = progOptions.errorCallback || error$1;
|
|
7596
|
+
errFn(err);
|
|
7597
|
+
gl.deleteProgram(program);
|
|
7598
|
+
// TODO: delete shaders, but only shaders that were created newly for this
|
|
7599
|
+
// program
|
|
7600
|
+
program = null;
|
|
7601
|
+
}
|
|
7602
|
+
progOptions.callback(err, program);
|
|
7603
|
+
}
|
|
7604
|
+
|
|
7605
|
+
/**
|
|
7606
|
+
* Check a program's link status
|
|
7607
|
+
* @param {WebGLRenderingContext} gl The WebGLRenderingContext to use.
|
|
7608
|
+
* @param {WebGLProgram} program Program to check
|
|
7609
|
+
* @param {ErrorCallback} [errFn] func for errors
|
|
7610
|
+
* @return {bool} true if program is ok
|
|
7611
|
+
*/
|
|
7612
|
+
function checkProgramStatus(gl, program, errFn) {
|
|
7613
|
+
errFn = errFn || error$1;
|
|
7451
7614
|
// Check the link status
|
|
7452
7615
|
const linked = gl.getProgramParameter(program, LINK_STATUS);
|
|
7453
7616
|
if (!linked) {
|
|
7454
7617
|
// something went wrong with the link
|
|
7455
7618
|
const lastError = gl.getProgramInfoLog(program);
|
|
7456
|
-
|
|
7457
|
-
|
|
7458
|
-
const
|
|
7619
|
+
errFn(`${
|
|
7620
|
+
gl.getAttachedShaders(program).map(shader => {
|
|
7621
|
+
const {lineOffset, shaderSource} = prepShaderSource(gl.getShaderSource(shader));
|
|
7622
|
+
const shaderError = gl.getShaderInfoLog(shader);
|
|
7623
|
+
const src = addLineNumbersWithError(shaderSource, shaderError, lineOffset);
|
|
7459
7624
|
const type = gl.getShaderParameter(shader, gl.SHADER_TYPE);
|
|
7460
7625
|
return `${glEnumToString(gl, type)}\n${src}}`;
|
|
7461
7626
|
}).join('\n')
|
|
7462
7627
|
}\nError in program linking: ${lastError}`);
|
|
7463
|
-
|
|
7464
|
-
gl.deleteProgram(program);
|
|
7465
|
-
deleteShaders(gl, newShaders);
|
|
7466
|
-
return null;
|
|
7467
7628
|
}
|
|
7468
|
-
return
|
|
7629
|
+
return linked;
|
|
7469
7630
|
}
|
|
7470
7631
|
|
|
7471
7632
|
/**
|
|
@@ -7492,7 +7653,7 @@ function createShaderFromScript(
|
|
|
7492
7653
|
throw new Error('unknown shader type');
|
|
7493
7654
|
}
|
|
7494
7655
|
|
|
7495
|
-
return loadShader(gl, shaderSource, shaderType
|
|
7656
|
+
return loadShader(gl, shaderSource, shaderType);
|
|
7496
7657
|
}
|
|
7497
7658
|
|
|
7498
7659
|
/**
|
|
@@ -8125,6 +8286,7 @@ function setUniformBlock(gl, programInfo, uniformBlockInfo) {
|
|
|
8125
8286
|
* struct Light {
|
|
8126
8287
|
* float intensity;
|
|
8127
8288
|
* vec4 color;
|
|
8289
|
+
* float nearFar[2];
|
|
8128
8290
|
* };
|
|
8129
8291
|
* uniform Lights {
|
|
8130
8292
|
* Light lights[2];
|
|
@@ -8134,28 +8296,35 @@ function setUniformBlock(gl, programInfo, uniformBlockInfo) {
|
|
|
8134
8296
|
*
|
|
8135
8297
|
* twgl.setBlockUniforms(someBlockInfo, {
|
|
8136
8298
|
* lights: [
|
|
8137
|
-
* { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
8138
|
-
* { intensity: 2.0, color: [0, 0, 1, 1] },
|
|
8299
|
+
* { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
|
|
8300
|
+
* { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
|
|
8139
8301
|
* ],
|
|
8140
8302
|
* });
|
|
8141
8303
|
*
|
|
8142
|
-
*
|
|
8304
|
+
* or the more traditional way
|
|
8143
8305
|
*
|
|
8144
8306
|
* twgl.setBlockUniforms(someBlockInfo, {
|
|
8145
8307
|
* "lights[0].intensity": 5.0,
|
|
8146
8308
|
* "lights[0].color": [1, 0, 0, 1],
|
|
8309
|
+
* "lights[0].nearFar": [0.1, 10],
|
|
8147
8310
|
* "lights[1].intensity": 2.0,
|
|
8148
8311
|
* "lights[1].color": [0, 0, 1, 1],
|
|
8312
|
+
* "lights[1].nearFar": [0.2, 15],
|
|
8149
8313
|
* });
|
|
8150
8314
|
*
|
|
8151
8315
|
* You can also specify partial paths
|
|
8152
8316
|
*
|
|
8153
8317
|
* twgl.setBlockUniforms(someBlockInfo, {
|
|
8154
|
-
* 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
8318
|
+
* 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
|
|
8155
8319
|
* });
|
|
8156
8320
|
*
|
|
8157
8321
|
* But you can not specify leaf array indices.
|
|
8158
8322
|
*
|
|
8323
|
+
* twgl.setBlockUniforms(someBlockInfo, {
|
|
8324
|
+
* 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
|
|
8325
|
+
* 'lights[1].nearFar': [0.2, 15], // GOOD
|
|
8326
|
+
* });
|
|
8327
|
+
*
|
|
8159
8328
|
* **IMPORTANT!**, packing in a UniformBlock is unintuitive.
|
|
8160
8329
|
* For example the actual layout of `someVec3Array` above in memory
|
|
8161
8330
|
* is `1, 2, 3, unused, 4, 5, 6, unused`. twgl takes in 6 values
|
|
@@ -8311,6 +8480,7 @@ function setUniformTree(tree, values) {
|
|
|
8311
8480
|
* struct Light {
|
|
8312
8481
|
* float intensity;
|
|
8313
8482
|
* vec4 color;
|
|
8483
|
+
* float nearFar[2];
|
|
8314
8484
|
* };
|
|
8315
8485
|
* uniform Light lights[2];
|
|
8316
8486
|
*
|
|
@@ -8318,8 +8488,8 @@ function setUniformTree(tree, values) {
|
|
|
8318
8488
|
*
|
|
8319
8489
|
* twgl.setUniforms(programInfo, {
|
|
8320
8490
|
* lights: [
|
|
8321
|
-
* { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
8322
|
-
* { intensity: 2.0, color: [0, 0, 1, 1] },
|
|
8491
|
+
* { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.1, 10] },
|
|
8492
|
+
* { intensity: 2.0, color: [0, 0, 1, 1], nearFar[0.2, 15] },
|
|
8323
8493
|
* ],
|
|
8324
8494
|
* });
|
|
8325
8495
|
*
|
|
@@ -8328,18 +8498,25 @@ function setUniformTree(tree, values) {
|
|
|
8328
8498
|
* twgl.setUniforms(programInfo, {
|
|
8329
8499
|
* "lights[0].intensity": 5.0,
|
|
8330
8500
|
* "lights[0].color": [1, 0, 0, 1],
|
|
8501
|
+
* "lights[0].nearFar": [0.1, 10],
|
|
8331
8502
|
* "lights[1].intensity": 2.0,
|
|
8332
8503
|
* "lights[1].color": [0, 0, 1, 1],
|
|
8504
|
+
* "lights[1].nearFar": [0.2, 15],
|
|
8333
8505
|
* });
|
|
8334
8506
|
*
|
|
8335
8507
|
* You can also specify partial paths
|
|
8336
8508
|
*
|
|
8337
8509
|
* twgl.setUniforms(programInfo, {
|
|
8338
|
-
* 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
|
|
8510
|
+
* 'lights[1]': { intensity: 5.0, color: [1, 0, 0, 1], nearFar[0.2, 15] },
|
|
8339
8511
|
* });
|
|
8340
8512
|
*
|
|
8341
8513
|
* But you can not specify leaf array indices
|
|
8342
8514
|
*
|
|
8515
|
+
* twgl.setUniforms(programInfo, {
|
|
8516
|
+
* 'lights[1].nearFar[1]': 15, // BAD! nearFar is a leaf
|
|
8517
|
+
* 'lights[1].nearFar': [0.2, 15], // GOOD
|
|
8518
|
+
* });
|
|
8519
|
+
*
|
|
8343
8520
|
* @memberOf module:twgl/programs
|
|
8344
8521
|
*/
|
|
8345
8522
|
function setUniforms(setters, ...args) { // eslint-disable-line
|
|
@@ -8558,6 +8735,8 @@ function createProgramInfoFromProgram(gl, program) {
|
|
|
8558
8735
|
return programInfo;
|
|
8559
8736
|
}
|
|
8560
8737
|
|
|
8738
|
+
const notIdRE = /\s|{|}|;/;
|
|
8739
|
+
|
|
8561
8740
|
/**
|
|
8562
8741
|
* Creates a ProgramInfo from 2 sources.
|
|
8563
8742
|
*
|
|
@@ -8591,25 +8770,40 @@ function createProgramInfoFromProgram(gl, program) {
|
|
|
8591
8770
|
function createProgramInfo(
|
|
8592
8771
|
gl, shaderSources, opt_attribs, opt_locations, opt_errorCallback) {
|
|
8593
8772
|
const progOptions = getProgramOptions(opt_attribs, opt_locations, opt_errorCallback);
|
|
8594
|
-
|
|
8773
|
+
const errors = [];
|
|
8595
8774
|
shaderSources = shaderSources.map(function(source) {
|
|
8596
8775
|
// Lets assume if there is no \n it's an id
|
|
8597
|
-
if (
|
|
8776
|
+
if (!notIdRE.test(source)) {
|
|
8598
8777
|
const script = getElementById(source);
|
|
8599
8778
|
if (!script) {
|
|
8600
|
-
|
|
8601
|
-
|
|
8779
|
+
const err = `no element with id: ${source}`;
|
|
8780
|
+
progOptions.errorCallback(err);
|
|
8781
|
+
errors.push(err);
|
|
8602
8782
|
} else {
|
|
8603
8783
|
source = script.text;
|
|
8604
8784
|
}
|
|
8605
8785
|
}
|
|
8606
8786
|
return source;
|
|
8607
8787
|
});
|
|
8608
|
-
if (
|
|
8788
|
+
if (errors.length) {
|
|
8789
|
+
if (progOptions.callback) {
|
|
8790
|
+
progOptions.callback(errors.join('\n'));
|
|
8791
|
+
}
|
|
8609
8792
|
return null;
|
|
8610
8793
|
}
|
|
8794
|
+
const origCallback = progOptions.callback;
|
|
8795
|
+
if (origCallback) {
|
|
8796
|
+
progOptions.callback = (err, program) => {
|
|
8797
|
+
let programInfo;
|
|
8798
|
+
if (!err) {
|
|
8799
|
+
programInfo = createProgramInfoFromProgram(gl, program);
|
|
8800
|
+
}
|
|
8801
|
+
origCallback(err, programInfo);
|
|
8802
|
+
};
|
|
8803
|
+
}
|
|
8611
8804
|
const program = createProgramFromSources(gl, shaderSources, progOptions);
|
|
8612
8805
|
if (!program) {
|
|
8806
|
+
// we should never get if if we have a callback
|
|
8613
8807
|
return null;
|
|
8614
8808
|
}
|
|
8615
8809
|
return createProgramInfoFromProgram(gl, program);
|
|
@@ -8619,9 +8813,11 @@ var programs = /*#__PURE__*/Object.freeze({
|
|
|
8619
8813
|
__proto__: null,
|
|
8620
8814
|
createAttributeSetters: createAttributeSetters,
|
|
8621
8815
|
createProgram: createProgram,
|
|
8816
|
+
createProgramAsync: createProgramAsync,
|
|
8622
8817
|
createProgramFromScripts: createProgramFromScripts,
|
|
8623
8818
|
createProgramFromSources: createProgramFromSources,
|
|
8624
8819
|
createProgramInfo: createProgramInfo,
|
|
8820
|
+
createProgramInfoAsync: createProgramInfoAsync,
|
|
8625
8821
|
createProgramInfoFromProgram: createProgramInfoFromProgram,
|
|
8626
8822
|
createUniformSetters: createUniformSetters,
|
|
8627
8823
|
createUniformBlockSpecFromProgram: createUniformBlockSpecFromProgram,
|
|
@@ -8882,6 +9078,7 @@ const LINEAR$1 = 0x2601;
|
|
|
8882
9078
|
* @property {number} [type] The type. Used for texture. Default = `gl.UNSIGNED_BYTE`.
|
|
8883
9079
|
* @property {number} [target] The texture target for `gl.framebufferTexture2D`.
|
|
8884
9080
|
* Defaults to `gl.TEXTURE_2D`. Set to appropriate face for cube maps.
|
|
9081
|
+
* @property {number} [samples] The number of samples. Default = 1
|
|
8885
9082
|
* @property {number} [level] level for `gl.framebufferTexture2D`. Defaults to 0.
|
|
8886
9083
|
* @property {number} [layer] layer for `gl.framebufferTextureLayer`. Defaults to undefined.
|
|
8887
9084
|
* If set then `gl.framebufferTextureLayer` is called, if not then `gl.framebufferTexture2D`
|
|
@@ -8986,16 +9183,21 @@ function createFramebufferInfo(gl, attachments, width, height) {
|
|
|
8986
9183
|
};
|
|
8987
9184
|
attachments.forEach(function(attachmentOptions) {
|
|
8988
9185
|
let attachment = attachmentOptions.attachment;
|
|
9186
|
+
const samples = attachmentOptions.samples;
|
|
8989
9187
|
const format = attachmentOptions.format;
|
|
8990
9188
|
let attachmentPoint = attachmentOptions.attachmentPoint || getAttachmentPointForFormat(format, attachmentOptions.internalFormat);
|
|
8991
9189
|
if (!attachmentPoint) {
|
|
8992
9190
|
attachmentPoint = COLOR_ATTACHMENT0 + colorAttachmentCount++;
|
|
8993
9191
|
}
|
|
8994
9192
|
if (!attachment) {
|
|
8995
|
-
if (isRenderbufferFormat(format)) {
|
|
9193
|
+
if (samples !== undefined || isRenderbufferFormat(format)) {
|
|
8996
9194
|
attachment = gl.createRenderbuffer();
|
|
8997
9195
|
gl.bindRenderbuffer(RENDERBUFFER, attachment);
|
|
8998
|
-
|
|
9196
|
+
if (samples > 1) {
|
|
9197
|
+
gl.renderbufferStorageMultisample(RENDERBUFFER, samples, format, width, height);
|
|
9198
|
+
} else {
|
|
9199
|
+
gl.renderbufferStorage(RENDERBUFFER, format, width, height);
|
|
9200
|
+
}
|
|
8999
9201
|
} else {
|
|
9000
9202
|
const textureOptions = Object.assign({}, attachmentOptions);
|
|
9001
9203
|
textureOptions.width = width;
|
|
@@ -9088,9 +9290,14 @@ function resizeFramebufferInfo(gl, framebufferInfo, attachments, width, height)
|
|
|
9088
9290
|
attachments.forEach(function(attachmentOptions, ndx) {
|
|
9089
9291
|
const attachment = framebufferInfo.attachments[ndx];
|
|
9090
9292
|
const format = attachmentOptions.format;
|
|
9091
|
-
|
|
9293
|
+
const samples = attachmentOptions.samples;
|
|
9294
|
+
if (samples !== undefined || isRenderbuffer(gl, attachment)) {
|
|
9092
9295
|
gl.bindRenderbuffer(RENDERBUFFER, attachment);
|
|
9093
|
-
|
|
9296
|
+
if (samples > 1) {
|
|
9297
|
+
gl.renderbufferStorageMultisample(RENDERBUFFER, samples, format, width, height);
|
|
9298
|
+
} else {
|
|
9299
|
+
gl.renderbufferStorage(RENDERBUFFER, format, width, height);
|
|
9300
|
+
}
|
|
9094
9301
|
} else if (isTexture(gl, attachment)) {
|
|
9095
9302
|
resizeTexture(gl, attachment, attachmentOptions, width, height);
|
|
9096
9303
|
} else {
|
|
@@ -9244,6 +9451,9 @@ function createVertexArrayInfo(gl, programInfos, bufferInfo) {
|
|
|
9244
9451
|
* @param {Object.<string, function>} setters Attribute setters as returned from createAttributeSetters
|
|
9245
9452
|
* @param {Object.<string, module:twgl.AttribInfo>} attribs AttribInfos mapped by attribute name.
|
|
9246
9453
|
* @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
|
|
9454
|
+
*
|
|
9455
|
+
* @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
|
|
9456
|
+
*
|
|
9247
9457
|
* @memberOf module:twgl/vertexArrays
|
|
9248
9458
|
*/
|
|
9249
9459
|
function createVAOAndSetAttributes(gl, setters, attribs, indices) {
|
|
@@ -9268,6 +9478,9 @@ function createVAOAndSetAttributes(gl, setters, attribs, indices) {
|
|
|
9268
9478
|
* @param {Object.<string, function>| module:twgl.ProgramInfo} programInfo as returned from createProgramInfo or Attribute setters as returned from createAttributeSetters
|
|
9269
9479
|
* @param {module:twgl.BufferInfo} bufferInfo BufferInfo as returned from createBufferInfoFromArrays etc...
|
|
9270
9480
|
* @param {WebGLBuffer} [indices] an optional ELEMENT_ARRAY_BUFFER of indices
|
|
9481
|
+
*
|
|
9482
|
+
* @return {WebGLVertexArrayObject|null} The created WebGLVertexArrayObject
|
|
9483
|
+
*
|
|
9271
9484
|
* @memberOf module:twgl/vertexArrays
|
|
9272
9485
|
*/
|
|
9273
9486
|
function createVAOFromBufferInfo(gl, programInfo, bufferInfo) {
|
|
@@ -9629,4 +9842,4 @@ function resizeCanvasToDisplaySize(canvas, multiplier) {
|
|
|
9629
9842
|
return false;
|
|
9630
9843
|
}
|
|
9631
9844
|
|
|
9632
|
-
export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoFromProgram, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults$2 as setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };
|
|
9845
|
+
export { addExtensionsToContext, attributes, bindFramebufferInfo, bindTransformFeedbackInfo, bindUniformBlock, canFilter, canGenerateMipmap, createAttribsFromArrays, createAttributeSetters, createBufferFromArray, createBufferFromTypedArray, createBufferInfoFromArrays, createBuffersFromArrays, createFramebufferInfo, createProgram, createProgramAsync, createProgramFromScripts, createProgramFromSources, createProgramInfo, createProgramInfoAsync, createProgramInfoFromProgram, createSampler, createSamplers, createTexture, createTextures, createTransformFeedback, createTransformFeedbackInfo, createUniformBlockInfo, createUniformBlockInfoFromProgram, createUniformBlockSpecFromProgram, createUniformSetters, createVAOAndSetAttributes, createVAOFromBufferInfo, createVertexArrayInfo, draw, drawBufferInfo, drawObjectList, framebuffers, getArray as getArray_, getBytesPerElementForInternalFormat, getContext, getFormatAndTypeForInternalFormat, getGLTypeForTypedArray, getGLTypeForTypedArrayType, getNumComponentsForFormat, getNumComponents as getNumComponents_, getTypedArrayTypeForGLType, getWebGLContext, glEnumToString, isArrayBuffer, isWebGL1, isWebGL2, loadTextureFromUrl, m4, primitives, programs, resizeCanvasToDisplaySize, resizeFramebufferInfo, resizeTexture, setAttribInfoBufferFromArray, setDefaults as setAttributeDefaults_, setAttributePrefix, setAttributes, setBlockUniforms, setBuffersAndAttributes, setDefaultTextureColor, setDefaults$2 as setDefaults, setEmptyTexture, setSamplerParameters, setDefaults$1 as setTextureDefaults_, setTextureFilteringForSize, setTextureFromArray, setTextureFromElement, setTextureParameters, setUniformBlock, setUniforms, setUniformsAndBindTextures, textures, typedarrays, utils, v3, vertexArrays };
|
package/dist/4.x/twgl.d.ts
CHANGED
|
@@ -68,6 +68,7 @@ export type AttachmentOptions = TextureOptions & {
|
|
|
68
68
|
format?: number;
|
|
69
69
|
type?: number;
|
|
70
70
|
target?: number;
|
|
71
|
+
samples?: number;
|
|
71
72
|
level?: number;
|
|
72
73
|
layer?: number;
|
|
73
74
|
attachment?: WebGLRenderbuffer | WebGLTexture;
|
|
@@ -88,6 +89,7 @@ export type ProgramOptions = {
|
|
|
88
89
|
[key: string]: AttribInfo;
|
|
89
90
|
} | string[];
|
|
90
91
|
transformFeedbackMode?: number;
|
|
92
|
+
callback?: ProgramCallback;
|
|
91
93
|
};
|
|
92
94
|
export type TransformFeedbackInfo = {
|
|
93
95
|
index: number;
|
|
@@ -255,7 +257,14 @@ export function bindFramebufferInfo(gl: WebGLRenderingContext, framebufferInfo?:
|
|
|
255
257
|
|
|
256
258
|
|
|
257
259
|
export function getBindPointForSamplerType(): void;
|
|
260
|
+
export type ProgramCallback = (err?: string, result?: WebGLProgram | ProgramInfo) => void;
|
|
261
|
+
export function prepShaderSource(shaderSource: string): any;
|
|
262
|
+
export function checkShaderStatus(gl: WebGLRenderingContext, shader: WebGLShader, errFn?: ErrorCallback): boolean;
|
|
258
263
|
export function createProgram(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
|
|
264
|
+
export function createProgramAsync(gl: WebGLRenderingContext, shaders: WebGLShader[] | string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<WebGLProgram>;
|
|
265
|
+
export function createProgramInfoAsync(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): Promise<ProgramInfo>;
|
|
266
|
+
export function checkForProgramLinkCompletionAsync(gl: WebGLRenderingContext, program: WebGLProgram, progOptions: ProgramOptions): void;
|
|
267
|
+
export function checkProgramStatus(gl: WebGLRenderingContext, program: WebGLProgram, errFn?: ErrorCallback): boolean;
|
|
259
268
|
export function createProgramFromScripts(gl: WebGLRenderingContext, shaderScriptIds: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
|
|
260
269
|
export function createProgramFromSources(gl: WebGLRenderingContext, shaderSources: string[], opt_attribs?: ProgramOptions | string[] | ErrorCallback, opt_errorCallback?: ErrorCallback): WebGLProgram;
|
|
261
270
|
export function createUniformSetters(gl: WebGLRenderingContext, program: WebGLProgram): {
|
|
@@ -333,7 +342,7 @@ export function createVAOAndSetAttributes(gl: WebGLRenderingContext, setters: {
|
|
|
333
342
|
[key: string]: (...params: any[]) => any;
|
|
334
343
|
}, attribs: {
|
|
335
344
|
[key: string]: AttribInfo;
|
|
336
|
-
}, indices?: WebGLBuffer):
|
|
345
|
+
}, indices?: WebGLBuffer): WebGLVertexArrayObject | null;
|
|
337
346
|
export function createVAOFromBufferInfo(gl: WebGLRenderingContext, programInfo: {
|
|
338
347
|
[key: string]: (...params: any[]) => any;
|
|
339
|
-
} | ProgramInfo, bufferInfo: BufferInfo, indices?: WebGLBuffer):
|
|
348
|
+
} | ProgramInfo, bufferInfo: BufferInfo, indices?: WebGLBuffer): WebGLVertexArrayObject | null;
|