@predy-js/render-interface 0.1.8 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Name: @predy-js/render-interface
3
3
  * Description: undefined
4
4
  * Author: undefined
5
- * Version: v0.1.8
5
+ * Version: v0.1.10
6
6
  */
7
7
 
8
8
  'use strict';
@@ -153,7 +153,7 @@ exports.TextureSourceType = void 0;
153
153
  TextureSourceType[TextureSourceType["mipmaps"] = 7] = "mipmaps";
154
154
  })(exports.TextureSourceType || (exports.TextureSourceType = {}));
155
155
 
156
- var _a$6;
156
+ var _a$6, _b$2;
157
157
  // @ts-expect-error safe to assign
158
158
  var constants = {};
159
159
  {
@@ -192,6 +192,14 @@ function getBytesPerElementByGLType(type) {
192
192
  var _a;
193
193
  return ((_a = map[type]) === null || _a === void 0 ? void 0 : _a.BYTES_PER_ELEMENT) || 0;
194
194
  }
195
+ var MatAttrLocPairMap = (_b$2 = {},
196
+ _b$2[constants.FLOAT_MAT2] = [2, 2],
197
+ _b$2[constants.FLOAT_MAT3] = [3, 3],
198
+ _b$2[constants.FLOAT_MAT4] = [4, 4],
199
+ _b$2);
200
+ function getMatAttrLocPair(type) {
201
+ return MatAttrLocPairMap[type] || [1, 1];
202
+ }
195
203
 
196
204
  exports.ShaderCompileResultStatus = void 0;
197
205
  (function (ShaderCompileResultStatus) {
@@ -1844,7 +1852,7 @@ var GLProgram = /** @class */ (function () {
1844
1852
  var uniformInfo = DATA_DICT[info.type];
1845
1853
  var subInfos = info.subInfos;
1846
1854
  if (subInfos.length > 0 && Array.isArray(value) && Array.isArray(value[0])) {
1847
- for (var i = 0; i < subInfos.length; ++i) {
1855
+ for (var i = 0; i < subInfos.length && i < value.length; ++i) {
1848
1856
  var subInfo = subInfos[i];
1849
1857
  uniformInfo.uniform(gl, subInfo, value[i], this.renderer);
1850
1858
  }
@@ -1856,6 +1864,7 @@ var GLProgram = /** @class */ (function () {
1856
1864
  var _a;
1857
1865
  var gl = this.renderer.gl;
1858
1866
  var vao = geometry.createVao(this.id);
1867
+ var gpu = this.renderer.gpu;
1859
1868
  if (vao) {
1860
1869
  vao.bind();
1861
1870
  if (vao.ready) {
@@ -1873,8 +1882,26 @@ var GLProgram = /** @class */ (function () {
1873
1882
  throw Error("no buffer named ".concat(attribute.dataSource || name));
1874
1883
  }
1875
1884
  buffer.bind();
1876
- gl.enableVertexAttribArray(attrInfo.loc);
1877
- gl.vertexAttribPointer(attrInfo.loc, attribute.size, attribute.type, attribute.normalize, attribute.stride || 0, attribute.offset || 0);
1885
+ if (attribute.size > 4) {
1886
+ var _a = getMatAttrLocPair(attrInfo.type), row = _a[0], col = _a[1];
1887
+ var bytesPerRow = getBytesPerElementByGLType(attribute.type) * col;
1888
+ var stride = row * bytesPerRow;
1889
+ for (var i = 0; i < row; i++) {
1890
+ var loc = attrInfo.loc + i;
1891
+ gl.enableVertexAttribArray(loc);
1892
+ gl.vertexAttribPointer(loc, col, attribute.type, attribute.normalize, stride, i * bytesPerRow);
1893
+ if (attribute.instanceDivisor && gpu.capability.instanceDraw) {
1894
+ gpu.vertexAttribDivisor(gl, loc, attribute.instanceDivisor);
1895
+ }
1896
+ }
1897
+ }
1898
+ else {
1899
+ gl.enableVertexAttribArray(attrInfo.loc);
1900
+ gl.vertexAttribPointer(attrInfo.loc, attribute.size, attribute.type, attribute.normalize, attribute.stride || 0, attribute.offset || 0);
1901
+ if (attribute.instanceDivisor && gpu.capability.instanceDraw) {
1902
+ gpu.vertexAttribDivisor(gl, attrInfo.loc, attribute.instanceDivisor);
1903
+ }
1904
+ }
1878
1905
  }
1879
1906
  });
1880
1907
  (_a = geometry._indexBuffer) === null || _a === void 0 ? void 0 : _a.bind();
@@ -2227,6 +2254,7 @@ var GLGPUCapability = /** @class */ (function () {
2227
2254
  }
2228
2255
  this.internalFormatDepth16 = level2 ? gl.DEPTH_COMPONENT16 : gl.DEPTH_COMPONENT;
2229
2256
  this.internalFormatDepth24_stencil8 = level2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL;
2257
+ this.instanceDrawExt = gl.getExtension('ANGLE_instanced_arrays');
2230
2258
  var floatTexture = (level2 || gl.getExtension('OES_texture_float')) ? gl.FLOAT : 0;
2231
2259
  var halfFloatTexture = level2 ? WebGL2RenderingContext.HALF_FLOAT : (((_a = gl.getExtension('OES_texture_half_float')) === null || _a === void 0 ? void 0 : _a.HALF_FLOAT_OES) || 0);
2232
2260
  var detail = {
@@ -2246,7 +2274,7 @@ var GLGPUCapability = /** @class */ (function () {
2246
2274
  floatLinear: floatLinear,
2247
2275
  maxTextureAnisotropy: textureAnisotropicExt ? gl.getParameter(textureAnisotropicExt.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 0,
2248
2276
  shaderTextureLod: level2 || !!gl.getExtension('EXT_shader_texture_lod'),
2249
- instanceDraw: level2 || !!gl.getExtension('ANGLE_instanced_arrays'),
2277
+ instanceDraw: level2 || !!this.instanceDrawExt,
2250
2278
  drawBuffers: level2 || !!this.drawBufferExtension,
2251
2279
  asyncShaderCompile: !!gl.getExtension('KHR_parallel_shader_compile'),
2252
2280
  intIndexElementBuffer: !!gl.getExtension('OES_element_index_uint'),
@@ -2296,6 +2324,39 @@ var GLGPUCapability = /** @class */ (function () {
2296
2324
  consoleError('invalid color attachment index:' + index);
2297
2325
  }
2298
2326
  };
2327
+ GLGPUCapability.prototype.vertexAttribDivisor = function (gl, loc, divisor) {
2328
+ if (this.level === 2) {
2329
+ gl.vertexAttribDivisor(loc, divisor);
2330
+ }
2331
+ else if (this.instanceDrawExt) {
2332
+ this.instanceDrawExt.vertexAttribDivisorANGLE(loc, divisor);
2333
+ }
2334
+ else {
2335
+ consoleWarn('instance draw not support');
2336
+ }
2337
+ };
2338
+ GLGPUCapability.prototype.drawElementsInstanced = function (gl, mode, count, type, offset, instanceCount) {
2339
+ if (this.level === 2) {
2340
+ gl.drawElementsInstanced(mode, count, type, offset, instanceCount);
2341
+ }
2342
+ else if (this.instanceDrawExt) {
2343
+ this.instanceDrawExt.drawElementsInstancedANGLE(mode, count, type, offset, instanceCount);
2344
+ }
2345
+ else {
2346
+ consoleWarn('instance draw not support');
2347
+ }
2348
+ };
2349
+ GLGPUCapability.prototype.drawArraysInstanced = function (gl, mode, first, count, instanceCount) {
2350
+ if (this.level === 2) {
2351
+ gl.drawArraysInstanced(mode, first, count, instanceCount);
2352
+ }
2353
+ else if (this.instanceDrawExt) {
2354
+ this.instanceDrawExt.drawArraysInstancedANGLE(mode, first, count, instanceCount);
2355
+ }
2356
+ else {
2357
+ consoleWarn('instance draw not support');
2358
+ }
2359
+ };
2299
2360
  GLGPUCapability.prototype.drawBuffers = function (gl, bufferStates) {
2300
2361
  var ext = this.drawBufferExtension;
2301
2362
  if (this.level === 1 && !ext) {
@@ -2773,9 +2834,6 @@ var GLGPURenderer = /** @class */ (function () {
2773
2834
  GLGPURenderer.prototype.createBuffer = function (options) {
2774
2835
  return new GLGPUBuffer(options, this);
2775
2836
  };
2776
- GLGPURenderer.prototype.parseSceneSchema = function (schema) {
2777
- throw new Error('Method not implemented.');
2778
- };
2779
2837
  GLGPURenderer.prototype.resize = function (width, height) {
2780
2838
  var gl = this.gl;
2781
2839
  if (gl && gl.drawingBufferWidth !== width || gl.drawingBufferHeight !== height) {
@@ -4738,10 +4796,12 @@ var GLGeometry = /** @class */ (function () {
4738
4796
  return null;
4739
4797
  };
4740
4798
  GLGeometry.prototype.draw = function (state) {
4741
- var _a;
4799
+ var _a, _b;
4742
4800
  var index = this._indexBuffer;
4743
4801
  var gl = (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.gl;
4744
- if (gl) {
4802
+ var gpu = (_b = this.renderer) === null || _b === void 0 ? void 0 : _b.gpu;
4803
+ var useInstancedDraw = (gpu === null || gpu === void 0 ? void 0 : gpu.capability.instanceDraw) && state.currentMesh.instanceCount > 0;
4804
+ if (gl && gpu) {
4745
4805
  var drawCount = this.drawCount;
4746
4806
  if (index) {
4747
4807
  var type = index.type;
@@ -4749,11 +4809,21 @@ var GLGeometry = /** @class */ (function () {
4749
4809
  //this._indexBuffer?.bind();
4750
4810
  var dc = isNaN(drawCount) ? index.elementCount : drawCount;
4751
4811
  if (dc > 0) {
4752
- gl.drawElements(this.mode, dc, type, this.drawStart || 0);
4812
+ if (useInstancedDraw) {
4813
+ gpu.drawElementsInstanced(gl, this.mode, dc, type, this.drawStart || 0, state.currentMesh.instanceCount);
4814
+ }
4815
+ else {
4816
+ gl.drawElements(this.mode, dc, type, this.drawStart || 0);
4817
+ }
4753
4818
  }
4754
4819
  }
4755
4820
  else if (drawCount > 0) {
4756
- gl.drawArrays(this.mode, this.drawStart, this.drawCount);
4821
+ if (useInstancedDraw) {
4822
+ gpu.drawArraysInstanced(gl, this.mode, this.drawStart, this.drawCount, state.currentMesh.instanceCount);
4823
+ }
4824
+ else {
4825
+ gl.drawArrays(this.mode, this.drawStart, this.drawCount);
4826
+ }
4757
4827
  }
4758
4828
  }
4759
4829
  };
@@ -4844,6 +4914,7 @@ var MarsGeometry = /** @class */ (function () {
4844
4914
  type: type || gltype,
4845
4915
  normalize: !!attr.normalize,
4846
4916
  dataSource: (name),
4917
+ instanceDivisor: attr.instanceDivisor,
4847
4918
  };
4848
4919
  attributesReleasable[name] = releasable || false;
4849
4920
  dirtyFlags[name] = {
@@ -5174,7 +5245,7 @@ var MarsGeometry = /** @class */ (function () {
5174
5245
 
5175
5246
  var seed = 1;
5176
5247
  var MarsMesh = /** @class */ (function () {
5177
- function MarsMesh(options) {
5248
+ function MarsMesh(options, renderer) {
5178
5249
  this._isDestroyed = false;
5179
5250
  this.material = options.material instanceof MarsMaterial ? options.material : new MarsMaterial(options.material);
5180
5251
  var geo = options.geometry;
@@ -5191,6 +5262,9 @@ var MarsMesh = /** @class */ (function () {
5191
5262
  this.priority = options.priority || 0;
5192
5263
  this.hide = false;
5193
5264
  this.worldMatrix = options.worldMatrix || [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
5265
+ if (renderer) {
5266
+ this.assignRenderer(renderer);
5267
+ }
5194
5268
  }
5195
5269
  Object.defineProperty(MarsMesh.prototype, "isDestroyed", {
5196
5270
  get: function () {
@@ -5261,6 +5335,15 @@ var MarsMesh = /** @class */ (function () {
5261
5335
  };
5262
5336
  return MarsMesh;
5263
5337
  }());
5338
+ var MarsInstancedMesh = /** @class */ (function (_super) {
5339
+ __extends(MarsInstancedMesh, _super);
5340
+ function MarsInstancedMesh(options, renderer) {
5341
+ var _this = _super.call(this, options, renderer) || this;
5342
+ _this.instanceCount = options.instanceCount;
5343
+ return _this;
5344
+ }
5345
+ return MarsInstancedMesh;
5346
+ }(MarsMesh));
5264
5347
 
5265
5348
  var copyShaderId = '$mri-internal-copy';
5266
5349
  var MarsExtWrap = /** @class */ (function () {
@@ -5524,9 +5607,10 @@ var MarsSharedGeometry = /** @class */ (function (_super) {
5524
5607
  return MarsSharedGeometry;
5525
5608
  }(MarsGeometry));
5526
5609
 
5527
- consoleLog('version: ' + "0.1.8");
5610
+ consoleLog('version: ' + "0.1.10");
5528
5611
 
5529
5612
  exports.Geometry = MarsGeometry;
5613
+ exports.InstancedMesh = MarsInstancedMesh;
5530
5614
  exports.MarsTextureFactory = MarsTextureFactory;
5531
5615
  exports.Material = MarsMaterial;
5532
5616
  exports.MaterialDataBlock = MarsMaterialDataBlock;