@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.mjs 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
  /******************************************************************************
@@ -149,7 +149,7 @@ var TextureSourceType;
149
149
  TextureSourceType[TextureSourceType["mipmaps"] = 7] = "mipmaps";
150
150
  })(TextureSourceType || (TextureSourceType = {}));
151
151
 
152
- var _a$6;
152
+ var _a$6, _b$2;
153
153
  // @ts-expect-error safe to assign
154
154
  var constants = {};
155
155
  {
@@ -188,6 +188,14 @@ function getBytesPerElementByGLType(type) {
188
188
  var _a;
189
189
  return ((_a = map[type]) === null || _a === void 0 ? void 0 : _a.BYTES_PER_ELEMENT) || 0;
190
190
  }
191
+ var MatAttrLocPairMap = (_b$2 = {},
192
+ _b$2[constants.FLOAT_MAT2] = [2, 2],
193
+ _b$2[constants.FLOAT_MAT3] = [3, 3],
194
+ _b$2[constants.FLOAT_MAT4] = [4, 4],
195
+ _b$2);
196
+ function getMatAttrLocPair(type) {
197
+ return MatAttrLocPairMap[type] || [1, 1];
198
+ }
191
199
 
192
200
  var ShaderCompileResultStatus;
193
201
  (function (ShaderCompileResultStatus) {
@@ -1840,7 +1848,7 @@ var GLProgram = /** @class */ (function () {
1840
1848
  var uniformInfo = DATA_DICT[info.type];
1841
1849
  var subInfos = info.subInfos;
1842
1850
  if (subInfos.length > 0 && Array.isArray(value) && Array.isArray(value[0])) {
1843
- for (var i = 0; i < subInfos.length; ++i) {
1851
+ for (var i = 0; i < subInfos.length && i < value.length; ++i) {
1844
1852
  var subInfo = subInfos[i];
1845
1853
  uniformInfo.uniform(gl, subInfo, value[i], this.renderer);
1846
1854
  }
@@ -1852,6 +1860,7 @@ var GLProgram = /** @class */ (function () {
1852
1860
  var _a;
1853
1861
  var gl = this.renderer.gl;
1854
1862
  var vao = geometry.createVao(this.id);
1863
+ var gpu = this.renderer.gpu;
1855
1864
  if (vao) {
1856
1865
  vao.bind();
1857
1866
  if (vao.ready) {
@@ -1869,8 +1878,26 @@ var GLProgram = /** @class */ (function () {
1869
1878
  throw Error("no buffer named ".concat(attribute.dataSource || name));
1870
1879
  }
1871
1880
  buffer.bind();
1872
- gl.enableVertexAttribArray(attrInfo.loc);
1873
- gl.vertexAttribPointer(attrInfo.loc, attribute.size, attribute.type, attribute.normalize, attribute.stride || 0, attribute.offset || 0);
1881
+ if (attribute.size > 4) {
1882
+ var _a = getMatAttrLocPair(attrInfo.type), row = _a[0], col = _a[1];
1883
+ var bytesPerRow = getBytesPerElementByGLType(attribute.type) * col;
1884
+ var stride = row * bytesPerRow;
1885
+ for (var i = 0; i < row; i++) {
1886
+ var loc = attrInfo.loc + i;
1887
+ gl.enableVertexAttribArray(loc);
1888
+ gl.vertexAttribPointer(loc, col, attribute.type, attribute.normalize, stride, i * bytesPerRow);
1889
+ if (attribute.instanceDivisor && gpu.capability.instanceDraw) {
1890
+ gpu.vertexAttribDivisor(gl, loc, attribute.instanceDivisor);
1891
+ }
1892
+ }
1893
+ }
1894
+ else {
1895
+ gl.enableVertexAttribArray(attrInfo.loc);
1896
+ gl.vertexAttribPointer(attrInfo.loc, attribute.size, attribute.type, attribute.normalize, attribute.stride || 0, attribute.offset || 0);
1897
+ if (attribute.instanceDivisor && gpu.capability.instanceDraw) {
1898
+ gpu.vertexAttribDivisor(gl, attrInfo.loc, attribute.instanceDivisor);
1899
+ }
1900
+ }
1874
1901
  }
1875
1902
  });
1876
1903
  (_a = geometry._indexBuffer) === null || _a === void 0 ? void 0 : _a.bind();
@@ -2223,6 +2250,7 @@ var GLGPUCapability = /** @class */ (function () {
2223
2250
  }
2224
2251
  this.internalFormatDepth16 = level2 ? gl.DEPTH_COMPONENT16 : gl.DEPTH_COMPONENT;
2225
2252
  this.internalFormatDepth24_stencil8 = level2 ? gl.DEPTH24_STENCIL8 : gl.DEPTH_STENCIL;
2253
+ this.instanceDrawExt = gl.getExtension('ANGLE_instanced_arrays');
2226
2254
  var floatTexture = (level2 || gl.getExtension('OES_texture_float')) ? gl.FLOAT : 0;
2227
2255
  var halfFloatTexture = level2 ? WebGL2RenderingContext.HALF_FLOAT : (((_a = gl.getExtension('OES_texture_half_float')) === null || _a === void 0 ? void 0 : _a.HALF_FLOAT_OES) || 0);
2228
2256
  var detail = {
@@ -2242,7 +2270,7 @@ var GLGPUCapability = /** @class */ (function () {
2242
2270
  floatLinear: floatLinear,
2243
2271
  maxTextureAnisotropy: textureAnisotropicExt ? gl.getParameter(textureAnisotropicExt.MAX_TEXTURE_MAX_ANISOTROPY_EXT) : 0,
2244
2272
  shaderTextureLod: level2 || !!gl.getExtension('EXT_shader_texture_lod'),
2245
- instanceDraw: level2 || !!gl.getExtension('ANGLE_instanced_arrays'),
2273
+ instanceDraw: level2 || !!this.instanceDrawExt,
2246
2274
  drawBuffers: level2 || !!this.drawBufferExtension,
2247
2275
  asyncShaderCompile: !!gl.getExtension('KHR_parallel_shader_compile'),
2248
2276
  intIndexElementBuffer: !!gl.getExtension('OES_element_index_uint'),
@@ -2292,6 +2320,39 @@ var GLGPUCapability = /** @class */ (function () {
2292
2320
  consoleError('invalid color attachment index:' + index);
2293
2321
  }
2294
2322
  };
2323
+ GLGPUCapability.prototype.vertexAttribDivisor = function (gl, loc, divisor) {
2324
+ if (this.level === 2) {
2325
+ gl.vertexAttribDivisor(loc, divisor);
2326
+ }
2327
+ else if (this.instanceDrawExt) {
2328
+ this.instanceDrawExt.vertexAttribDivisorANGLE(loc, divisor);
2329
+ }
2330
+ else {
2331
+ consoleWarn('instance draw not support');
2332
+ }
2333
+ };
2334
+ GLGPUCapability.prototype.drawElementsInstanced = function (gl, mode, count, type, offset, instanceCount) {
2335
+ if (this.level === 2) {
2336
+ gl.drawElementsInstanced(mode, count, type, offset, instanceCount);
2337
+ }
2338
+ else if (this.instanceDrawExt) {
2339
+ this.instanceDrawExt.drawElementsInstancedANGLE(mode, count, type, offset, instanceCount);
2340
+ }
2341
+ else {
2342
+ consoleWarn('instance draw not support');
2343
+ }
2344
+ };
2345
+ GLGPUCapability.prototype.drawArraysInstanced = function (gl, mode, first, count, instanceCount) {
2346
+ if (this.level === 2) {
2347
+ gl.drawArraysInstanced(mode, first, count, instanceCount);
2348
+ }
2349
+ else if (this.instanceDrawExt) {
2350
+ this.instanceDrawExt.drawArraysInstancedANGLE(mode, first, count, instanceCount);
2351
+ }
2352
+ else {
2353
+ consoleWarn('instance draw not support');
2354
+ }
2355
+ };
2295
2356
  GLGPUCapability.prototype.drawBuffers = function (gl, bufferStates) {
2296
2357
  var ext = this.drawBufferExtension;
2297
2358
  if (this.level === 1 && !ext) {
@@ -2769,9 +2830,6 @@ var GLGPURenderer = /** @class */ (function () {
2769
2830
  GLGPURenderer.prototype.createBuffer = function (options) {
2770
2831
  return new GLGPUBuffer(options, this);
2771
2832
  };
2772
- GLGPURenderer.prototype.parseSceneSchema = function (schema) {
2773
- throw new Error('Method not implemented.');
2774
- };
2775
2833
  GLGPURenderer.prototype.resize = function (width, height) {
2776
2834
  var gl = this.gl;
2777
2835
  if (gl && gl.drawingBufferWidth !== width || gl.drawingBufferHeight !== height) {
@@ -4734,10 +4792,12 @@ var GLGeometry = /** @class */ (function () {
4734
4792
  return null;
4735
4793
  };
4736
4794
  GLGeometry.prototype.draw = function (state) {
4737
- var _a;
4795
+ var _a, _b;
4738
4796
  var index = this._indexBuffer;
4739
4797
  var gl = (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.gl;
4740
- if (gl) {
4798
+ var gpu = (_b = this.renderer) === null || _b === void 0 ? void 0 : _b.gpu;
4799
+ var useInstancedDraw = (gpu === null || gpu === void 0 ? void 0 : gpu.capability.instanceDraw) && state.currentMesh.instanceCount > 0;
4800
+ if (gl && gpu) {
4741
4801
  var drawCount = this.drawCount;
4742
4802
  if (index) {
4743
4803
  var type = index.type;
@@ -4745,11 +4805,21 @@ var GLGeometry = /** @class */ (function () {
4745
4805
  //this._indexBuffer?.bind();
4746
4806
  var dc = isNaN(drawCount) ? index.elementCount : drawCount;
4747
4807
  if (dc > 0) {
4748
- gl.drawElements(this.mode, dc, type, this.drawStart || 0);
4808
+ if (useInstancedDraw) {
4809
+ gpu.drawElementsInstanced(gl, this.mode, dc, type, this.drawStart || 0, state.currentMesh.instanceCount);
4810
+ }
4811
+ else {
4812
+ gl.drawElements(this.mode, dc, type, this.drawStart || 0);
4813
+ }
4749
4814
  }
4750
4815
  }
4751
4816
  else if (drawCount > 0) {
4752
- gl.drawArrays(this.mode, this.drawStart, this.drawCount);
4817
+ if (useInstancedDraw) {
4818
+ gpu.drawArraysInstanced(gl, this.mode, this.drawStart, this.drawCount, state.currentMesh.instanceCount);
4819
+ }
4820
+ else {
4821
+ gl.drawArrays(this.mode, this.drawStart, this.drawCount);
4822
+ }
4753
4823
  }
4754
4824
  }
4755
4825
  };
@@ -4840,6 +4910,7 @@ var MarsGeometry = /** @class */ (function () {
4840
4910
  type: type || gltype,
4841
4911
  normalize: !!attr.normalize,
4842
4912
  dataSource: (name),
4913
+ instanceDivisor: attr.instanceDivisor,
4843
4914
  };
4844
4915
  attributesReleasable[name] = releasable || false;
4845
4916
  dirtyFlags[name] = {
@@ -5170,7 +5241,7 @@ var MarsGeometry = /** @class */ (function () {
5170
5241
 
5171
5242
  var seed = 1;
5172
5243
  var MarsMesh = /** @class */ (function () {
5173
- function MarsMesh(options) {
5244
+ function MarsMesh(options, renderer) {
5174
5245
  this._isDestroyed = false;
5175
5246
  this.material = options.material instanceof MarsMaterial ? options.material : new MarsMaterial(options.material);
5176
5247
  var geo = options.geometry;
@@ -5187,6 +5258,9 @@ var MarsMesh = /** @class */ (function () {
5187
5258
  this.priority = options.priority || 0;
5188
5259
  this.hide = false;
5189
5260
  this.worldMatrix = options.worldMatrix || [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
5261
+ if (renderer) {
5262
+ this.assignRenderer(renderer);
5263
+ }
5190
5264
  }
5191
5265
  Object.defineProperty(MarsMesh.prototype, "isDestroyed", {
5192
5266
  get: function () {
@@ -5257,6 +5331,15 @@ var MarsMesh = /** @class */ (function () {
5257
5331
  };
5258
5332
  return MarsMesh;
5259
5333
  }());
5334
+ var MarsInstancedMesh = /** @class */ (function (_super) {
5335
+ __extends(MarsInstancedMesh, _super);
5336
+ function MarsInstancedMesh(options, renderer) {
5337
+ var _this = _super.call(this, options, renderer) || this;
5338
+ _this.instanceCount = options.instanceCount;
5339
+ return _this;
5340
+ }
5341
+ return MarsInstancedMesh;
5342
+ }(MarsMesh));
5260
5343
 
5261
5344
  var copyShaderId = '$mri-internal-copy';
5262
5345
  var MarsExtWrap = /** @class */ (function () {
@@ -5520,7 +5603,7 @@ var MarsSharedGeometry = /** @class */ (function (_super) {
5520
5603
  return MarsSharedGeometry;
5521
5604
  }(MarsGeometry));
5522
5605
 
5523
- consoleLog('version: ' + "0.1.8");
5606
+ consoleLog('version: ' + "0.1.10");
5524
5607
 
5525
- export { DestroyOptions, MarsGeometry as Geometry, MarsTextureFactory, MarsMaterial as Material, MarsMaterialDataBlock as MaterialDataBlock, MarsMesh as Mesh, MarsRenderFrame as RenderFrame, MarsRenderPass as RenderPass, RenderPassAttachmentStorageType, RenderPassDestroyAttachmentType, RenderPassMeshOrder, RenderPassPriorityNormal, RenderPassPriorityPostprocess, RenderPassPriorityPrepare, MarsRenderer as Renderer, ShaderCompileResultStatus, ShaderLibraryEmpty, MarsSharedGeometry as SharedGeometry, MarsTexture as Texture, TextureLoadAction, TextureSourceType, TextureStoreAction, constants, getDefaultGPUCapability, getDefaultTextureFactory, setDefaultTextureFactory };
5608
+ export { DestroyOptions, MarsGeometry as Geometry, MarsInstancedMesh as InstancedMesh, MarsTextureFactory, MarsMaterial as Material, MarsMaterialDataBlock as MaterialDataBlock, MarsMesh as Mesh, MarsRenderFrame as RenderFrame, MarsRenderPass as RenderPass, RenderPassAttachmentStorageType, RenderPassDestroyAttachmentType, RenderPassMeshOrder, RenderPassPriorityNormal, RenderPassPriorityPostprocess, RenderPassPriorityPrepare, MarsRenderer as Renderer, ShaderCompileResultStatus, ShaderLibraryEmpty, MarsSharedGeometry as SharedGeometry, MarsTexture as Texture, TextureLoadAction, TextureSourceType, TextureStoreAction, constants, getDefaultGPUCapability, getDefaultTextureFactory, setDefaultTextureFactory };
5526
5609
  //# sourceMappingURL=index.mjs.map