twgl.js 4.19.4 → 4.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -376,6 +376,61 @@ gl.renderbufferStorage(gl.RENDERBUFFER, gl.DEPTH_STENCIL, gl.drawingBufferWidth,
376
376
  gl.framebufferRenderbuffer(gl.FRAMEBUFFER, gl.DEPTH_STENCIL_ATTACHMENT, gl.RENDERBUFFER, rb);
377
377
  ```
378
378
 
379
+ ### Setting uniform and uniformblock structures and arrays
380
+
381
+ Given an array of GLSL structures like this
382
+
383
+ ```glsl
384
+ struct Light {
385
+ float intensity;
386
+ float shininess;
387
+ vec4 color;
388
+ }
389
+ uniform Light lights[2];
390
+ ```
391
+
392
+ TWGL
393
+
394
+ ```javascript
395
+ const progInfo = twgl.createProgramInfo(gl, [vs, fs]);
396
+ ...
397
+ twgl.setUniforms(progInfo, {
398
+ lights: [
399
+ { intensity: 5.0, shininess: 100, color: [1, 0, 0, 1] },
400
+ { intensity: 2.0, shininess: 50, color: [0, 0, 1, 1] },
401
+ ],
402
+ });
403
+ ```
404
+
405
+ WebGL
406
+
407
+ ```javascript
408
+ // assuming we already compiled and linked the program
409
+ const light0IntensityLoc = gl.getUniformLocation('lights[0].intensity');
410
+ const light0ShininessLoc = gl.getUniformLocation('lights[0].shininess');
411
+ const light0ColorLoc = gl.getUniformLocation('lights[0].color');
412
+ const light1IntensityLoc = gl.getUniformLocation('lights[1].intensity');
413
+ const light1ShininessLoc = gl.getUniformLocation('lights[1].shininess');
414
+ const light1ColorLoc = gl.getUniformLocation('lights[1].color');
415
+ ...
416
+ gl.uniform1f(light0IntensityLoc, 5.0);
417
+ gl.uniform1f(light0ShininessLoc, 100);
418
+ gl.uniform4fv(light0ColorLoc, [1, 0, 0, 1]);
419
+ gl.uniform1f(light1IntensityLoc, 2.0);
420
+ gl.uniform1f(light1ShininessLoc, 50);
421
+ gl.uniform4fv(light1ColorLoc, [0, 0, 1, 1]);
422
+ ```
423
+
424
+ If you just want to set the 2nd light in TWGL you can do this
425
+
426
+ ```javascript
427
+ const progInfo = twgl.createProgramInfo(gl, [vs, fs]);
428
+ ...
429
+ twgl.setUniforms(progInfo, {
430
+ lights[1]: { intensity: 5.0, shininess: 100, color: [1, 0, 0, 1] },
431
+ });
432
+ ```
433
+
379
434
  ### Compare
380
435
 
381
436
  [TWGL example](http://twgljs.org/examples/twgl-cube.html) vs [WebGL example](http://twgljs.org/examples/webgl-cube.html)
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 4.19.4 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 4.21.1 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
3
3
  * Available via the MIT license.
4
4
  * see: http://github.com/greggman/twgl.js for details
5
5
  */
@@ -5849,6 +5849,52 @@ function isBuiltIn(info) {
5849
5849
  var name = info.name;
5850
5850
  return name.startsWith("gl_") || name.startsWith("webgl_");
5851
5851
  }
5852
+
5853
+ var tokenRE = /(?=[.[\]])|(?<=[.[\]])/g;
5854
+
5855
+ var isDigit = function isDigit(s) {
5856
+ return s >= '0' && s <= '9';
5857
+ };
5858
+
5859
+ function addSetterToUniformTree(fullPath, setter, node, uniformSetters) {
5860
+ var tokens = fullPath.split(tokenRE);
5861
+ var tokenNdx = 0;
5862
+ var path = '';
5863
+
5864
+ for (;;) {
5865
+ var token = tokens[tokenNdx++]; // has to be name or number
5866
+
5867
+ path += token;
5868
+ var isArrayIndex = isDigit(token[0]);
5869
+ var accessor = isArrayIndex ? parseInt(token) : token;
5870
+
5871
+ if (isArrayIndex) {
5872
+ path += tokens[tokenNdx++]; // skip ']'
5873
+ }
5874
+
5875
+ var isLastToken = tokenNdx === tokens.length;
5876
+
5877
+ if (isLastToken) {
5878
+ node[accessor] = setter;
5879
+ break;
5880
+ } else {
5881
+ var _token = tokens[tokenNdx++]; // has to be . or [
5882
+
5883
+ var isArray = _token === '[';
5884
+ var child = node[accessor] || (isArray ? [] : {});
5885
+ node[accessor] = child;
5886
+ node = child;
5887
+
5888
+ uniformSetters[path] = uniformSetters[path] || function (node) {
5889
+ return function (value) {
5890
+ setUniformTree(node, value);
5891
+ };
5892
+ }(child);
5893
+
5894
+ path += _token;
5895
+ }
5896
+ }
5897
+ }
5852
5898
  /**
5853
5899
  * Creates setter functions for all uniforms of a shader
5854
5900
  * program.
@@ -5906,6 +5952,7 @@ function createUniformSetters(gl, program) {
5906
5952
  }
5907
5953
 
5908
5954
  var uniformSetters = {};
5955
+ var uniformTree = {};
5909
5956
  var numUniforms = gl.getProgramParameter(program, ACTIVE_UNIFORMS);
5910
5957
 
5911
5958
  for (var ii = 0; ii < numUniforms; ++ii) {
@@ -5924,7 +5971,9 @@ function createUniformSetters(gl, program) {
5924
5971
  var location = gl.getUniformLocation(program, uniformInfo.name); // the uniform will have no location if it's in a uniform block
5925
5972
 
5926
5973
  if (location) {
5927
- uniformSetters[name] = createUniformSetter(program, uniformInfo, location);
5974
+ var setter = createUniformSetter(program, uniformInfo, location);
5975
+ uniformSetters[name] = setter;
5976
+ addSetterToUniformTree(name, setter, uniformTree, uniformSetters);
5928
5977
  }
5929
5978
  }
5930
5979
 
@@ -6069,11 +6118,6 @@ function createUniformBlockSpecFromProgram(gl, program) {
6069
6118
  uniformIndices.push(ii);
6070
6119
  uniformData.push({});
6071
6120
  var uniformInfo = gl.getActiveUniform(program, ii);
6072
-
6073
- if (isBuiltIn(uniformInfo)) {
6074
- break;
6075
- }
6076
-
6077
6121
  uniformData[ii].name = uniformInfo.name;
6078
6122
  }
6079
6123
 
@@ -6212,6 +6256,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
6212
6256
 
6213
6257
  var uniforms = {};
6214
6258
  var setters = {};
6259
+ var setterTree = {};
6215
6260
  blockSpec.uniformIndices.forEach(function (uniformNdx) {
6216
6261
  var data = uniformData[uniformNdx];
6217
6262
  var name = data.name;
@@ -6230,8 +6275,35 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
6230
6275
  var Type = typeInfo.Type;
6231
6276
  var byteLength = isArray ? pad(typeInfo.size, 16) * data.size : typeInfo.size * data.size;
6232
6277
  var uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
6233
- uniforms[name] = uniformView;
6234
- setters[name] = createUniformBlockUniformSetter(uniformView, isArray, typeInfo.rows, typeInfo.cols);
6278
+ uniforms[name] = uniformView; // Note: I'm not sure what to do here. The original
6279
+ // idea was to create TypedArray views into each part
6280
+ // of the block. This is useful, for example if you have
6281
+ // a block with { mat4: model; mat4 view; mat4 projection; }
6282
+ // you'll get a Float32Array for each one suitable for
6283
+ // passing to most JS math libraries including twgl's and glMatrix.js.
6284
+ //
6285
+ // But, if you have a an array of structures, especially if that
6286
+ // array is large, you get a whole bunch of TypedArray views.
6287
+ // Every one of them has overhead and switching between them all
6288
+ // is probably a cache miss. In that case it would really be better
6289
+ // to just have one view (asFloat) and have all the setters
6290
+ // just reference the correct portion. But, then you can't easily
6291
+ // treat a matrix, or a vec4, as a standalone thing like you can
6292
+ // with all the views.
6293
+ //
6294
+ // Another problem with the views is they are not shared. With
6295
+ // uniforms you have one set of setters. With UniformBlockInfo
6296
+ // you have a set of setters *pre block instance*. That's because
6297
+ // TypedArray views can't be mapped to different buffers.
6298
+ //
6299
+ // My gut right now is if you really want the speed and compactness
6300
+ // then you should probably roll your own solution. TWGL's goal
6301
+ // here is ease of use as AFAICT there is no simple generic efficient
6302
+ // solution.
6303
+
6304
+ var setter = createUniformBlockUniformSetter(uniformView, isArray, typeInfo.rows, typeInfo.cols);
6305
+ setters[name] = setter;
6306
+ addSetterToUniformTree(name, setter, setterTree, setters);
6235
6307
  });
6236
6308
  return {
6237
6309
  name: blockName,
@@ -6343,6 +6415,44 @@ function setUniformBlock(gl, programInfo, uniformBlockInfo) {
6343
6415
  *
6344
6416
  * Arrays can be JavaScript arrays or typed arrays
6345
6417
  *
6418
+ * You can also fill out structure and array values either via
6419
+ * shortcut. Example
6420
+ *
6421
+ * // -- in shader --
6422
+ * struct Light {
6423
+ * float intensity;
6424
+ * vec4 color;
6425
+ * };
6426
+ * uniform Lights {
6427
+ * Light lights[2];
6428
+ * };
6429
+ *
6430
+ * // in JavaScript
6431
+ *
6432
+ * twgl.setBlockUniforms(someBlockInfo, {
6433
+ * lights: [
6434
+ * { intensity: 5.0, color: [1, 0, 0, 1] },
6435
+ * { intensity: 2.0, color: [0, 0, 1, 1] },
6436
+ * ],
6437
+ * });
6438
+ *
6439
+ * // or the more traditional way
6440
+ *
6441
+ * twgl.setBlockUniforms(someBlockInfo, {
6442
+ * "lights[0].intensity": 5.0,
6443
+ * "lights[0].color": [1, 0, 0, 1],
6444
+ * "lights[1].intensity": 2.0,
6445
+ * "lights[1].color": [0, 0, 1, 1],
6446
+ * });
6447
+ *
6448
+ * You can also specify partial paths
6449
+ *
6450
+ * twgl.setBlockUniforms(someBlockInfo, {
6451
+ * 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
6452
+ * });
6453
+ *
6454
+ * But you can not specify leaf array indices.
6455
+ *
6346
6456
  * **IMPORTANT!**, packing in a UniformBlock is unintuitive.
6347
6457
  * For example the actual layout of `someVec3Array` above in memory
6348
6458
  * is `1, 2, 3, unused, 4, 5, 6, unused`. twgl takes in 6 values
@@ -6371,6 +6481,18 @@ function setBlockUniforms(uniformBlockInfo, values) {
6371
6481
  }
6372
6482
  }
6373
6483
  }
6484
+
6485
+ function setUniformTree(tree, values) {
6486
+ for (var name in values) {
6487
+ var prop = tree[name];
6488
+
6489
+ if (typeof prop === 'function') {
6490
+ prop(values[name]);
6491
+ } else {
6492
+ setUniformTree(tree[name], values[name]);
6493
+ }
6494
+ }
6495
+ }
6374
6496
  /**
6375
6497
  * Set uniforms and binds related textures.
6376
6498
  *
@@ -6483,30 +6605,66 @@ function setBlockUniforms(uniformBlockInfo, values) {
6483
6605
  * twgl.setUniforms(programInfo, sharedUniforms);
6484
6606
  * twgl.setUniforms(programInfo, localUniforms};
6485
6607
  *
6608
+ * You can also fill out structure and array values either via
6609
+ * shortcut. Example
6610
+ *
6611
+ * // -- in shader --
6612
+ * struct Light {
6613
+ * float intensity;
6614
+ * vec4 color;
6615
+ * };
6616
+ * uniform Light lights[2];
6617
+ *
6618
+ * // in JavaScript
6619
+ *
6620
+ * twgl.setUniforms(programInfo, {
6621
+ * lights: [
6622
+ * { intensity: 5.0, color: [1, 0, 0, 1] },
6623
+ * { intensity: 2.0, color: [0, 0, 1, 1] },
6624
+ * ],
6625
+ * });
6626
+ *
6627
+ * or the more traditional way
6628
+ *
6629
+ * twgl.setUniforms(programInfo, {
6630
+ * "lights[0].intensity": 5.0,
6631
+ * "lights[0].color": [1, 0, 0, 1],
6632
+ * "lights[1].intensity": 2.0,
6633
+ * "lights[1].color": [0, 0, 1, 1],
6634
+ * });
6635
+ *
6636
+ * You can also specify partial paths
6637
+ *
6638
+ * twgl.setUniforms(programInfo, {
6639
+ * 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
6640
+ * });
6641
+ *
6642
+ * But you can not specify leaf array indices
6643
+ *
6486
6644
  * @memberOf module:twgl/programs
6487
6645
  */
6488
6646
 
6489
6647
 
6490
- function setUniforms(setters, values) {
6648
+ function setUniforms(setters) {
6491
6649
  // eslint-disable-line
6492
6650
  var actualSetters = setters.uniformSetters || setters;
6493
- var numArgs = arguments.length;
6651
+ var numArgs = arguments.length <= 1 ? 0 : arguments.length - 1;
6494
6652
 
6495
- for (var aNdx = 1; aNdx < numArgs; ++aNdx) {
6496
- var _values = arguments[aNdx];
6653
+ for (var aNdx = 0; aNdx < numArgs; ++aNdx) {
6654
+ var values = aNdx + 1 < 1 || arguments.length <= aNdx + 1 ? undefined : arguments[aNdx + 1];
6497
6655
 
6498
- if (Array.isArray(_values)) {
6499
- var numValues = _values.length;
6656
+ if (Array.isArray(values)) {
6657
+ var numValues = values.length;
6500
6658
 
6501
6659
  for (var ii = 0; ii < numValues; ++ii) {
6502
- setUniforms(actualSetters, _values[ii]);
6660
+ setUniforms(actualSetters, values[ii]);
6503
6661
  }
6504
6662
  } else {
6505
- for (var name in _values) {
6663
+ for (var name in values) {
6506
6664
  var setter = actualSetters[name];
6507
6665
 
6508
6666
  if (setter) {
6509
- setter(_values[name]);
6667
+ setter(values[name]);
6510
6668
  }
6511
6669
  }
6512
6670
  }
@@ -1,4 +1,4 @@
1
- /* @license twgl.js 4.19.4 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
1
+ /* @license twgl.js 4.21.1 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
  /*
@@ -7591,6 +7591,43 @@ function isBuiltIn(info) {
7591
7591
  return name.startsWith("gl_") || name.startsWith("webgl_");
7592
7592
  }
7593
7593
 
7594
+ const tokenRE = /(?=[.[\]])|(?<=[.[\]])/g;
7595
+ const isDigit = s => s >= '0' && s <= '9';
7596
+ function addSetterToUniformTree(fullPath, setter, node, uniformSetters) {
7597
+ const tokens = fullPath.split(tokenRE);
7598
+ let tokenNdx = 0;
7599
+ let path = '';
7600
+
7601
+ for (;;) {
7602
+ const token = tokens[tokenNdx++]; // has to be name or number
7603
+ path += token;
7604
+ const isArrayIndex = isDigit(token[0]);
7605
+ const accessor = isArrayIndex
7606
+ ? parseInt(token)
7607
+ : token;
7608
+ if (isArrayIndex) {
7609
+ path += tokens[tokenNdx++]; // skip ']'
7610
+ }
7611
+ const isLastToken = tokenNdx === tokens.length;
7612
+ if (isLastToken) {
7613
+ node[accessor] = setter;
7614
+ break;
7615
+ } else {
7616
+ const token = tokens[tokenNdx++]; // has to be . or [
7617
+ const isArray = token === '[';
7618
+ const child = node[accessor] || (isArray ? [] : {});
7619
+ node[accessor] = child;
7620
+ node = child;
7621
+ uniformSetters[path] = uniformSetters[path] || function(node) {
7622
+ return function(value) {
7623
+ setUniformTree(node, value);
7624
+ };
7625
+ }(child);
7626
+ path += token;
7627
+ }
7628
+ }
7629
+ }
7630
+
7594
7631
  /**
7595
7632
  * Creates setter functions for all uniforms of a shader
7596
7633
  * program.
@@ -7640,13 +7677,14 @@ function createUniformSetters(gl, program) {
7640
7677
  return setter;
7641
7678
  }
7642
7679
 
7643
- const uniformSetters = { };
7680
+ const uniformSetters = {};
7681
+ const uniformTree = {};
7644
7682
  const numUniforms = gl.getProgramParameter(program, ACTIVE_UNIFORMS);
7645
7683
 
7646
7684
  for (let ii = 0; ii < numUniforms; ++ii) {
7647
7685
  const uniformInfo = gl.getActiveUniform(program, ii);
7648
7686
  if (isBuiltIn(uniformInfo)) {
7649
- continue;
7687
+ continue;
7650
7688
  }
7651
7689
  let name = uniformInfo.name;
7652
7690
  // remove the array suffix.
@@ -7656,9 +7694,12 @@ function createUniformSetters(gl, program) {
7656
7694
  const location = gl.getUniformLocation(program, uniformInfo.name);
7657
7695
  // the uniform will have no location if it's in a uniform block
7658
7696
  if (location) {
7659
- uniformSetters[name] = createUniformSetter(program, uniformInfo, location);
7697
+ const setter = createUniformSetter(program, uniformInfo, location);
7698
+ uniformSetters[name] = setter;
7699
+ addSetterToUniformTree(name, setter, uniformTree, uniformSetters);
7660
7700
  }
7661
7701
  }
7702
+
7662
7703
  return uniformSetters;
7663
7704
  }
7664
7705
 
@@ -7790,9 +7831,6 @@ function createUniformBlockSpecFromProgram(gl, program) {
7790
7831
  uniformIndices.push(ii);
7791
7832
  uniformData.push({});
7792
7833
  const uniformInfo = gl.getActiveUniform(program, ii);
7793
- if (isBuiltIn(uniformInfo)) {
7794
- break;
7795
- }
7796
7834
  uniformData[ii].name = uniformInfo.name;
7797
7835
  }
7798
7836
 
@@ -7928,6 +7966,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
7928
7966
  }
7929
7967
  const uniforms = {};
7930
7968
  const setters = {};
7969
+ const setterTree = {};
7931
7970
  blockSpec.uniformIndices.forEach(function(uniformNdx) {
7932
7971
  const data = uniformData[uniformNdx];
7933
7972
  let name = data.name;
@@ -7945,7 +7984,34 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
7945
7984
  : typeInfo.size * data.size;
7946
7985
  const uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
7947
7986
  uniforms[name] = uniformView;
7948
- setters[name] = createUniformBlockUniformSetter(uniformView, isArray, typeInfo.rows, typeInfo.cols);
7987
+ // Note: I'm not sure what to do here. The original
7988
+ // idea was to create TypedArray views into each part
7989
+ // of the block. This is useful, for example if you have
7990
+ // a block with { mat4: model; mat4 view; mat4 projection; }
7991
+ // you'll get a Float32Array for each one suitable for
7992
+ // passing to most JS math libraries including twgl's and glMatrix.js.
7993
+ //
7994
+ // But, if you have a an array of structures, especially if that
7995
+ // array is large, you get a whole bunch of TypedArray views.
7996
+ // Every one of them has overhead and switching between them all
7997
+ // is probably a cache miss. In that case it would really be better
7998
+ // to just have one view (asFloat) and have all the setters
7999
+ // just reference the correct portion. But, then you can't easily
8000
+ // treat a matrix, or a vec4, as a standalone thing like you can
8001
+ // with all the views.
8002
+ //
8003
+ // Another problem with the views is they are not shared. With
8004
+ // uniforms you have one set of setters. With UniformBlockInfo
8005
+ // you have a set of setters *pre block instance*. That's because
8006
+ // TypedArray views can't be mapped to different buffers.
8007
+ //
8008
+ // My gut right now is if you really want the speed and compactness
8009
+ // then you should probably roll your own solution. TWGL's goal
8010
+ // here is ease of use as AFAICT there is no simple generic efficient
8011
+ // solution.
8012
+ const setter = createUniformBlockUniformSetter(uniformView, isArray, typeInfo.rows, typeInfo.cols);
8013
+ setters[name] = setter;
8014
+ addSetterToUniformTree(name, setter, setterTree, setters);
7949
8015
  });
7950
8016
  return {
7951
8017
  name: blockName,
@@ -8052,6 +8118,44 @@ function setUniformBlock(gl, programInfo, uniformBlockInfo) {
8052
8118
  *
8053
8119
  * Arrays can be JavaScript arrays or typed arrays
8054
8120
  *
8121
+ * You can also fill out structure and array values either via
8122
+ * shortcut. Example
8123
+ *
8124
+ * // -- in shader --
8125
+ * struct Light {
8126
+ * float intensity;
8127
+ * vec4 color;
8128
+ * };
8129
+ * uniform Lights {
8130
+ * Light lights[2];
8131
+ * };
8132
+ *
8133
+ * // in JavaScript
8134
+ *
8135
+ * twgl.setBlockUniforms(someBlockInfo, {
8136
+ * lights: [
8137
+ * { intensity: 5.0, color: [1, 0, 0, 1] },
8138
+ * { intensity: 2.0, color: [0, 0, 1, 1] },
8139
+ * ],
8140
+ * });
8141
+ *
8142
+ * // or the more traditional way
8143
+ *
8144
+ * twgl.setBlockUniforms(someBlockInfo, {
8145
+ * "lights[0].intensity": 5.0,
8146
+ * "lights[0].color": [1, 0, 0, 1],
8147
+ * "lights[1].intensity": 2.0,
8148
+ * "lights[1].color": [0, 0, 1, 1],
8149
+ * });
8150
+ *
8151
+ * You can also specify partial paths
8152
+ *
8153
+ * twgl.setBlockUniforms(someBlockInfo, {
8154
+ * 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
8155
+ * });
8156
+ *
8157
+ * But you can not specify leaf array indices.
8158
+ *
8055
8159
  * **IMPORTANT!**, packing in a UniformBlock is unintuitive.
8056
8160
  * For example the actual layout of `someVec3Array` above in memory
8057
8161
  * is `1, 2, 3, unused, 4, 5, 6, unused`. twgl takes in 6 values
@@ -8077,6 +8181,17 @@ function setBlockUniforms(uniformBlockInfo, values) {
8077
8181
  }
8078
8182
  }
8079
8183
 
8184
+ function setUniformTree(tree, values) {
8185
+ for (const name in values) {
8186
+ const prop = tree[name];
8187
+ if (typeof prop === 'function') {
8188
+ prop(values[name]);
8189
+ } else {
8190
+ setUniformTree(tree[name], values[name]);
8191
+ }
8192
+ }
8193
+ }
8194
+
8080
8195
  /**
8081
8196
  * Set uniforms and binds related textures.
8082
8197
  *
@@ -8189,13 +8304,49 @@ function setBlockUniforms(uniformBlockInfo, values) {
8189
8304
  * twgl.setUniforms(programInfo, sharedUniforms);
8190
8305
  * twgl.setUniforms(programInfo, localUniforms};
8191
8306
  *
8307
+ * You can also fill out structure and array values either via
8308
+ * shortcut. Example
8309
+ *
8310
+ * // -- in shader --
8311
+ * struct Light {
8312
+ * float intensity;
8313
+ * vec4 color;
8314
+ * };
8315
+ * uniform Light lights[2];
8316
+ *
8317
+ * // in JavaScript
8318
+ *
8319
+ * twgl.setUniforms(programInfo, {
8320
+ * lights: [
8321
+ * { intensity: 5.0, color: [1, 0, 0, 1] },
8322
+ * { intensity: 2.0, color: [0, 0, 1, 1] },
8323
+ * ],
8324
+ * });
8325
+ *
8326
+ * or the more traditional way
8327
+ *
8328
+ * twgl.setUniforms(programInfo, {
8329
+ * "lights[0].intensity": 5.0,
8330
+ * "lights[0].color": [1, 0, 0, 1],
8331
+ * "lights[1].intensity": 2.0,
8332
+ * "lights[1].color": [0, 0, 1, 1],
8333
+ * });
8334
+ *
8335
+ * You can also specify partial paths
8336
+ *
8337
+ * twgl.setUniforms(programInfo, {
8338
+ * 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
8339
+ * });
8340
+ *
8341
+ * But you can not specify leaf array indices
8342
+ *
8192
8343
  * @memberOf module:twgl/programs
8193
8344
  */
8194
- function setUniforms(setters, values) { // eslint-disable-line
8345
+ function setUniforms(setters, ...args) { // eslint-disable-line
8195
8346
  const actualSetters = setters.uniformSetters || setters;
8196
- const numArgs = arguments.length;
8197
- for (let aNdx = 1; aNdx < numArgs; ++aNdx) {
8198
- const values = arguments[aNdx];
8347
+ const numArgs = args.length;
8348
+ for (let aNdx = 0; aNdx < numArgs; ++aNdx) {
8349
+ const values = args[aNdx];
8199
8350
  if (Array.isArray(values)) {
8200
8351
  const numValues = values.length;
8201
8352
  for (let ii = 0; ii < numValues; ++ii) {
@@ -8240,7 +8391,7 @@ function createAttributeSetters(gl, program) {
8240
8391
  for (let ii = 0; ii < numAttribs; ++ii) {
8241
8392
  const attribInfo = gl.getActiveAttrib(program, ii);
8242
8393
  if (isBuiltIn(attribInfo)) {
8243
- continue;
8394
+ continue;
8244
8395
  }
8245
8396
  const index = gl.getAttribLocation(program, attribInfo.name);
8246
8397
  const typeInfo = attrTypeMap[attribInfo.type];
@@ -8394,9 +8545,9 @@ function createProgramInfoFromProgram(gl, program) {
8394
8545
  const uniformSetters = createUniformSetters(gl, program);
8395
8546
  const attribSetters = createAttributeSetters(gl, program);
8396
8547
  const programInfo = {
8397
- program: program,
8398
- uniformSetters: uniformSetters,
8399
- attribSetters: attribSetters,
8548
+ program,
8549
+ uniformSetters,
8550
+ attribSetters,
8400
8551
  };
8401
8552
 
8402
8553
  if (isWebGL2(gl)) {
package/dist/4.x/twgl.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * @license twgl.js 4.19.4 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
2
+ * @license twgl.js 4.21.1 Copyright (c) 2015, Gregg Tavares All Rights Reserved.
3
3
  * Available via the MIT license.
4
4
  * see: http://github.com/greggman/twgl.js for details
5
5
  */
@@ -2771,6 +2771,52 @@ function isBuiltIn(info) {
2771
2771
  var name = info.name;
2772
2772
  return name.startsWith("gl_") || name.startsWith("webgl_");
2773
2773
  }
2774
+
2775
+ var tokenRE = /(?=[.[\]])|(?<=[.[\]])/g;
2776
+
2777
+ var isDigit = function isDigit(s) {
2778
+ return s >= '0' && s <= '9';
2779
+ };
2780
+
2781
+ function addSetterToUniformTree(fullPath, setter, node, uniformSetters) {
2782
+ var tokens = fullPath.split(tokenRE);
2783
+ var tokenNdx = 0;
2784
+ var path = '';
2785
+
2786
+ for (;;) {
2787
+ var token = tokens[tokenNdx++]; // has to be name or number
2788
+
2789
+ path += token;
2790
+ var isArrayIndex = isDigit(token[0]);
2791
+ var accessor = isArrayIndex ? parseInt(token) : token;
2792
+
2793
+ if (isArrayIndex) {
2794
+ path += tokens[tokenNdx++]; // skip ']'
2795
+ }
2796
+
2797
+ var isLastToken = tokenNdx === tokens.length;
2798
+
2799
+ if (isLastToken) {
2800
+ node[accessor] = setter;
2801
+ break;
2802
+ } else {
2803
+ var _token = tokens[tokenNdx++]; // has to be . or [
2804
+
2805
+ var isArray = _token === '[';
2806
+ var child = node[accessor] || (isArray ? [] : {});
2807
+ node[accessor] = child;
2808
+ node = child;
2809
+
2810
+ uniformSetters[path] = uniformSetters[path] || function (node) {
2811
+ return function (value) {
2812
+ setUniformTree(node, value);
2813
+ };
2814
+ }(child);
2815
+
2816
+ path += _token;
2817
+ }
2818
+ }
2819
+ }
2774
2820
  /**
2775
2821
  * Creates setter functions for all uniforms of a shader
2776
2822
  * program.
@@ -2828,6 +2874,7 @@ function createUniformSetters(gl, program) {
2828
2874
  }
2829
2875
 
2830
2876
  var uniformSetters = {};
2877
+ var uniformTree = {};
2831
2878
  var numUniforms = gl.getProgramParameter(program, ACTIVE_UNIFORMS);
2832
2879
 
2833
2880
  for (var ii = 0; ii < numUniforms; ++ii) {
@@ -2846,7 +2893,9 @@ function createUniformSetters(gl, program) {
2846
2893
  var location = gl.getUniformLocation(program, uniformInfo.name); // the uniform will have no location if it's in a uniform block
2847
2894
 
2848
2895
  if (location) {
2849
- uniformSetters[name] = createUniformSetter(program, uniformInfo, location);
2896
+ var setter = createUniformSetter(program, uniformInfo, location);
2897
+ uniformSetters[name] = setter;
2898
+ addSetterToUniformTree(name, setter, uniformTree, uniformSetters);
2850
2899
  }
2851
2900
  }
2852
2901
 
@@ -2991,11 +3040,6 @@ function createUniformBlockSpecFromProgram(gl, program) {
2991
3040
  uniformIndices.push(ii);
2992
3041
  uniformData.push({});
2993
3042
  var uniformInfo = gl.getActiveUniform(program, ii);
2994
-
2995
- if (isBuiltIn(uniformInfo)) {
2996
- break;
2997
- }
2998
-
2999
3043
  uniformData[ii].name = uniformInfo.name;
3000
3044
  }
3001
3045
 
@@ -3134,6 +3178,7 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3134
3178
 
3135
3179
  var uniforms = {};
3136
3180
  var setters = {};
3181
+ var setterTree = {};
3137
3182
  blockSpec.uniformIndices.forEach(function (uniformNdx) {
3138
3183
  var data = uniformData[uniformNdx];
3139
3184
  var name = data.name;
@@ -3152,8 +3197,35 @@ function createUniformBlockInfoFromProgram(gl, program, uniformBlockSpec, blockN
3152
3197
  var Type = typeInfo.Type;
3153
3198
  var byteLength = isArray ? pad(typeInfo.size, 16) * data.size : typeInfo.size * data.size;
3154
3199
  var uniformView = new Type(array, data.offset, byteLength / Type.BYTES_PER_ELEMENT);
3155
- uniforms[name] = uniformView;
3156
- setters[name] = createUniformBlockUniformSetter(uniformView, isArray, typeInfo.rows, typeInfo.cols);
3200
+ uniforms[name] = uniformView; // Note: I'm not sure what to do here. The original
3201
+ // idea was to create TypedArray views into each part
3202
+ // of the block. This is useful, for example if you have
3203
+ // a block with { mat4: model; mat4 view; mat4 projection; }
3204
+ // you'll get a Float32Array for each one suitable for
3205
+ // passing to most JS math libraries including twgl's and glMatrix.js.
3206
+ //
3207
+ // But, if you have a an array of structures, especially if that
3208
+ // array is large, you get a whole bunch of TypedArray views.
3209
+ // Every one of them has overhead and switching between them all
3210
+ // is probably a cache miss. In that case it would really be better
3211
+ // to just have one view (asFloat) and have all the setters
3212
+ // just reference the correct portion. But, then you can't easily
3213
+ // treat a matrix, or a vec4, as a standalone thing like you can
3214
+ // with all the views.
3215
+ //
3216
+ // Another problem with the views is they are not shared. With
3217
+ // uniforms you have one set of setters. With UniformBlockInfo
3218
+ // you have a set of setters *pre block instance*. That's because
3219
+ // TypedArray views can't be mapped to different buffers.
3220
+ //
3221
+ // My gut right now is if you really want the speed and compactness
3222
+ // then you should probably roll your own solution. TWGL's goal
3223
+ // here is ease of use as AFAICT there is no simple generic efficient
3224
+ // solution.
3225
+
3226
+ var setter = createUniformBlockUniformSetter(uniformView, isArray, typeInfo.rows, typeInfo.cols);
3227
+ setters[name] = setter;
3228
+ addSetterToUniformTree(name, setter, setterTree, setters);
3157
3229
  });
3158
3230
  return {
3159
3231
  name: blockName,
@@ -3265,6 +3337,44 @@ function setUniformBlock(gl, programInfo, uniformBlockInfo) {
3265
3337
  *
3266
3338
  * Arrays can be JavaScript arrays or typed arrays
3267
3339
  *
3340
+ * You can also fill out structure and array values either via
3341
+ * shortcut. Example
3342
+ *
3343
+ * // -- in shader --
3344
+ * struct Light {
3345
+ * float intensity;
3346
+ * vec4 color;
3347
+ * };
3348
+ * uniform Lights {
3349
+ * Light lights[2];
3350
+ * };
3351
+ *
3352
+ * // in JavaScript
3353
+ *
3354
+ * twgl.setBlockUniforms(someBlockInfo, {
3355
+ * lights: [
3356
+ * { intensity: 5.0, color: [1, 0, 0, 1] },
3357
+ * { intensity: 2.0, color: [0, 0, 1, 1] },
3358
+ * ],
3359
+ * });
3360
+ *
3361
+ * // or the more traditional way
3362
+ *
3363
+ * twgl.setBlockUniforms(someBlockInfo, {
3364
+ * "lights[0].intensity": 5.0,
3365
+ * "lights[0].color": [1, 0, 0, 1],
3366
+ * "lights[1].intensity": 2.0,
3367
+ * "lights[1].color": [0, 0, 1, 1],
3368
+ * });
3369
+ *
3370
+ * You can also specify partial paths
3371
+ *
3372
+ * twgl.setBlockUniforms(someBlockInfo, {
3373
+ * 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
3374
+ * });
3375
+ *
3376
+ * But you can not specify leaf array indices.
3377
+ *
3268
3378
  * **IMPORTANT!**, packing in a UniformBlock is unintuitive.
3269
3379
  * For example the actual layout of `someVec3Array` above in memory
3270
3380
  * is `1, 2, 3, unused, 4, 5, 6, unused`. twgl takes in 6 values
@@ -3293,6 +3403,18 @@ function setBlockUniforms(uniformBlockInfo, values) {
3293
3403
  }
3294
3404
  }
3295
3405
  }
3406
+
3407
+ function setUniformTree(tree, values) {
3408
+ for (var name in values) {
3409
+ var prop = tree[name];
3410
+
3411
+ if (typeof prop === 'function') {
3412
+ prop(values[name]);
3413
+ } else {
3414
+ setUniformTree(tree[name], values[name]);
3415
+ }
3416
+ }
3417
+ }
3296
3418
  /**
3297
3419
  * Set uniforms and binds related textures.
3298
3420
  *
@@ -3405,30 +3527,66 @@ function setBlockUniforms(uniformBlockInfo, values) {
3405
3527
  * twgl.setUniforms(programInfo, sharedUniforms);
3406
3528
  * twgl.setUniforms(programInfo, localUniforms};
3407
3529
  *
3530
+ * You can also fill out structure and array values either via
3531
+ * shortcut. Example
3532
+ *
3533
+ * // -- in shader --
3534
+ * struct Light {
3535
+ * float intensity;
3536
+ * vec4 color;
3537
+ * };
3538
+ * uniform Light lights[2];
3539
+ *
3540
+ * // in JavaScript
3541
+ *
3542
+ * twgl.setUniforms(programInfo, {
3543
+ * lights: [
3544
+ * { intensity: 5.0, color: [1, 0, 0, 1] },
3545
+ * { intensity: 2.0, color: [0, 0, 1, 1] },
3546
+ * ],
3547
+ * });
3548
+ *
3549
+ * or the more traditional way
3550
+ *
3551
+ * twgl.setUniforms(programInfo, {
3552
+ * "lights[0].intensity": 5.0,
3553
+ * "lights[0].color": [1, 0, 0, 1],
3554
+ * "lights[1].intensity": 2.0,
3555
+ * "lights[1].color": [0, 0, 1, 1],
3556
+ * });
3557
+ *
3558
+ * You can also specify partial paths
3559
+ *
3560
+ * twgl.setUniforms(programInfo, {
3561
+ * 'lights[1]: { intensity: 5.0, color: [1, 0, 0, 1] },
3562
+ * });
3563
+ *
3564
+ * But you can not specify leaf array indices
3565
+ *
3408
3566
  * @memberOf module:twgl/programs
3409
3567
  */
3410
3568
 
3411
3569
 
3412
- function setUniforms(setters, values) {
3570
+ function setUniforms(setters) {
3413
3571
  // eslint-disable-line
3414
3572
  var actualSetters = setters.uniformSetters || setters;
3415
- var numArgs = arguments.length;
3573
+ var numArgs = arguments.length <= 1 ? 0 : arguments.length - 1;
3416
3574
 
3417
- for (var aNdx = 1; aNdx < numArgs; ++aNdx) {
3418
- var _values = arguments[aNdx];
3575
+ for (var aNdx = 0; aNdx < numArgs; ++aNdx) {
3576
+ var values = aNdx + 1 < 1 || arguments.length <= aNdx + 1 ? undefined : arguments[aNdx + 1];
3419
3577
 
3420
- if (Array.isArray(_values)) {
3421
- var numValues = _values.length;
3578
+ if (Array.isArray(values)) {
3579
+ var numValues = values.length;
3422
3580
 
3423
3581
  for (var ii = 0; ii < numValues; ++ii) {
3424
- setUniforms(actualSetters, _values[ii]);
3582
+ setUniforms(actualSetters, values[ii]);
3425
3583
  }
3426
3584
  } else {
3427
- for (var name in _values) {
3585
+ for (var name in values) {
3428
3586
  var setter = actualSetters[name];
3429
3587
 
3430
3588
  if (setter) {
3431
- setter(_values[name]);
3589
+ setter(values[name]);
3432
3590
  }
3433
3591
  }
3434
3592
  }
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "twgl.js",
3
- "version": "4.19.4",
3
+ "version": "4.21.1",
4
4
  "description": "A Tiny WebGL helper library",
5
5
  "main": "dist/4.x/twgl-full.js",
6
6
  "types": "dist/4.x/twgl-full.d.ts",
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 testjs && npm run testts",
10
+ "test": "npm run testjs && npm run testts && npm run testbrowser",
11
11
  "testjs": "node node_modules/mocha/bin/mocha --recursive 'test/src/js/*-harness.js'",
12
12
  "testts": "tsc --project test/src/ts/",
13
13
  "testbrowser": "node test/puppeteer.js",