@predy-js/render-interface 0.1.61-beta.20 → 0.1.61-beta.23

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.61-beta.20
5
+ * Version: v0.1.61-beta.23
6
6
  */
7
7
 
8
8
  /******************************************************************************
@@ -149,7 +149,9 @@ var TextureSourceType;
149
149
  TextureSourceType[TextureSourceType["mipmaps"] = 7] = "mipmaps";
150
150
  })(TextureSourceType || (TextureSourceType = {}));
151
151
 
152
- var _a$6, _b$2;
152
+ var GPUBufferOptionsMemoryShared = 1 << 1;
153
+
154
+ var _a$6, _b$3;
153
155
  // @ts-expect-error safe to assign
154
156
  var constants = {};
155
157
  {
@@ -188,11 +190,11 @@ function getBytesPerElementByGLType(type) {
188
190
  var _a;
189
191
  return ((_a = map[type]) === null || _a === void 0 ? void 0 : _a.BYTES_PER_ELEMENT) || 0;
190
192
  }
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);
193
+ var MatAttrLocPairMap = (_b$3 = {},
194
+ _b$3[constants.FLOAT_MAT2] = [2, 2],
195
+ _b$3[constants.FLOAT_MAT3] = [3, 3],
196
+ _b$3[constants.FLOAT_MAT4] = [4, 4],
197
+ _b$3);
196
198
  function getMatAttrLocPair(type) {
197
199
  return MatAttrLocPairMap[type] || [1, 1];
198
200
  }
@@ -674,19 +676,17 @@ var GPUCapabilityEmpty = /** @class */ (function () {
674
676
  var GLGPUBuffer = /** @class */ (function () {
675
677
  function GLGPUBuffer(options, renderer) {
676
678
  this._isDestroyed = false;
677
- this.target = options.target || constants.ARRAY_BUFFER;
678
- this.type = options.type || constants.FLOAT;
679
679
  this.renderer = renderer;
680
680
  this.glBuffer = renderer.createGLBuffer(this, options.name);
681
681
  this.usage = options.usage || constants.STATIC_DRAW;
682
- this.bytesPerElement = getBytesPerElementByGLType(this.type);
683
682
  this._byteLength = 0;
684
683
  this.name = options.name || '';
684
+ this.target = options.target || constants.ARRAY_BUFFER;
685
685
  if (options.data) {
686
686
  this.bufferData(options.data);
687
687
  }
688
- else if (options.elementCount) {
689
- this.bufferData(getBytesPerElementByGLType(this.type) * options.elementCount);
688
+ else if (options.byteLength) {
689
+ this.bufferData(options.byteLength);
690
690
  }
691
691
  }
692
692
  Object.defineProperty(GLGPUBuffer.prototype, "byteLength", {
@@ -703,13 +703,6 @@ var GLGPUBuffer = /** @class */ (function () {
703
703
  enumerable: false,
704
704
  configurable: true
705
705
  });
706
- Object.defineProperty(GLGPUBuffer.prototype, "elementCount", {
707
- get: function () {
708
- return (this.byteLength / this.bytesPerElement);
709
- },
710
- enumerable: false,
711
- configurable: true
712
- });
713
706
  Object.defineProperty(GLGPUBuffer.prototype, "level", {
714
707
  get: function () {
715
708
  var _a;
@@ -747,6 +740,7 @@ var GLGPUBuffer = /** @class */ (function () {
747
740
  gl.bufferSubData(target, 0, typedArray);
748
741
  }
749
742
  }
743
+ gl.bindBuffer(target, null);
750
744
  }
751
745
  else {
752
746
  this._byteLength = 0;
@@ -756,7 +750,7 @@ var GLGPUBuffer = /** @class */ (function () {
756
750
  if (this.renderer) {
757
751
  var gl = this.renderer.gl;
758
752
  var target = this.target;
759
- var byteOffset = elementOffset * this.bytesPerElement;
753
+ var byteOffset = elementOffset * typedArray.BYTES_PER_ELEMENT;
760
754
  gl.bindBuffer(target, this.glBuffer);
761
755
  var byteLength = byteOffset + typedArray.byteLength;
762
756
  if (byteLength > this._byteLength) {
@@ -764,6 +758,7 @@ var GLGPUBuffer = /** @class */ (function () {
764
758
  gl.bufferData(target, byteLength, this.usage);
765
759
  }
766
760
  gl.bufferSubData(target, byteOffset, typedArray);
761
+ gl.bindBuffer(target, null);
767
762
  }
768
763
  else {
769
764
  this._byteLength = 0;
@@ -780,13 +775,19 @@ var GLGPUBuffer = /** @class */ (function () {
780
775
  var _a;
781
776
  if (this.level === 2) {
782
777
  var gl = (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.gl;
778
+ var target = this.target;
783
779
  if (gl) {
784
- gl.getBufferSubData(this.target, elementOffset * this.bytesPerElement, typedArray);
780
+ gl.bindBuffer(target, this.glBuffer);
781
+ gl.getBufferSubData(target, elementOffset * typedArray.BYTES_PER_ELEMENT, typedArray);
782
+ gl.bindBuffer(target, null);
785
783
  return true;
786
784
  }
787
785
  }
788
786
  return false;
789
787
  };
788
+ GLGPUBuffer.prototype.getElementCount = function (bytePerElement) {
789
+ return this._byteLength / bytePerElement;
790
+ };
790
791
  GLGPUBuffer.prototype.assignRenderer = function (renderer) {
791
792
  if (!this.renderer) {
792
793
  // @ts-expect-error safe to assign
@@ -1448,7 +1449,7 @@ function assignTextureLoc(gl, info, value, renderer, index) {
1448
1449
  }
1449
1450
  }
1450
1451
 
1451
- var _a$4, _b$1;
1452
+ var _a$4, _b$2;
1452
1453
  var BlockUniformInfoOffset = 1;
1453
1454
  var BlockUniformInfoByteLength = 8;
1454
1455
  var BlockUniformInfoType = 0;
@@ -1590,43 +1591,42 @@ var MemorySetter = (_a$4 = {},
1590
1591
  _a$4[constants.BOOL_VEC3] = setUInt8Array,
1591
1592
  _a$4[constants.BOOL_VEC4] = setUInt8Array,
1592
1593
  _a$4);
1593
- var ItemPerValueMap = (_b$1 = {},
1594
- _b$1[constants.FLOAT] = 1,
1595
- _b$1[constants.INT] = 1,
1596
- _b$1[constants.UNSIGNED_INT] = 1,
1597
- _b$1[constants.SHORT] = 1,
1598
- _b$1[constants.BOOL] = 1,
1599
- _b$1[constants.UNSIGNED_SHORT] = 1,
1600
- _b$1[constants.FLOAT_VEC2] = 2,
1601
- _b$1[constants.FLOAT_VEC3] = 3,
1602
- _b$1[constants.FLOAT_VEC4] = 4,
1603
- _b$1[constants.FLOAT_MAT2] = 4,
1604
- _b$1[constants.FLOAT_MAT3] = 9,
1605
- _b$1[constants.FLOAT_MAT4] = 16,
1606
- _b$1[constants.FLOAT_MAT2x3] = 6,
1607
- _b$1[constants.FLOAT_MAT2x4] = 8,
1608
- _b$1[constants.FLOAT_MAT4x3] = 12,
1609
- _b$1[constants.FLOAT_MAT4x2] = 8,
1610
- _b$1[constants.FLOAT_MAT3x4] = 12,
1611
- _b$1[constants.FLOAT_MAT3x2] = 6,
1612
- _b$1[constants.INT_VEC2] = 2,
1613
- _b$1[constants.INT_VEC3] = 3,
1614
- _b$1[constants.INT_VEC4] = 4,
1615
- _b$1[constants.UNSIGNED_INT_VEC2] = 2,
1616
- _b$1[constants.UNSIGNED_INT_VEC3] = 3,
1617
- _b$1[constants.UNSIGNED_INT_VEC4] = 4,
1618
- _b$1[constants.BOOL_VEC2] = 2,
1619
- _b$1[constants.BOOL_VEC3] = 3,
1620
- _b$1[constants.BOOL_VEC4] = 4,
1621
- _b$1);
1594
+ var ItemPerValueMap = (_b$2 = {},
1595
+ _b$2[constants.FLOAT] = 1,
1596
+ _b$2[constants.INT] = 1,
1597
+ _b$2[constants.UNSIGNED_INT] = 1,
1598
+ _b$2[constants.SHORT] = 1,
1599
+ _b$2[constants.BOOL] = 1,
1600
+ _b$2[constants.UNSIGNED_SHORT] = 1,
1601
+ _b$2[constants.FLOAT_VEC2] = 2,
1602
+ _b$2[constants.FLOAT_VEC3] = 3,
1603
+ _b$2[constants.FLOAT_VEC4] = 4,
1604
+ _b$2[constants.FLOAT_MAT2] = 4,
1605
+ _b$2[constants.FLOAT_MAT3] = 9,
1606
+ _b$2[constants.FLOAT_MAT4] = 16,
1607
+ _b$2[constants.FLOAT_MAT2x3] = 6,
1608
+ _b$2[constants.FLOAT_MAT2x4] = 8,
1609
+ _b$2[constants.FLOAT_MAT4x3] = 12,
1610
+ _b$2[constants.FLOAT_MAT4x2] = 8,
1611
+ _b$2[constants.FLOAT_MAT3x4] = 12,
1612
+ _b$2[constants.FLOAT_MAT3x2] = 6,
1613
+ _b$2[constants.INT_VEC2] = 2,
1614
+ _b$2[constants.INT_VEC3] = 3,
1615
+ _b$2[constants.INT_VEC4] = 4,
1616
+ _b$2[constants.UNSIGNED_INT_VEC2] = 2,
1617
+ _b$2[constants.UNSIGNED_INT_VEC3] = 3,
1618
+ _b$2[constants.UNSIGNED_INT_VEC4] = 4,
1619
+ _b$2[constants.BOOL_VEC2] = 2,
1620
+ _b$2[constants.BOOL_VEC3] = 3,
1621
+ _b$2[constants.BOOL_VEC4] = 4,
1622
+ _b$2);
1622
1623
  var fullRange = [0, 0];
1623
1624
  var UniformBlockBuffer = /** @class */ (function () {
1624
1625
  function UniformBlockBuffer(renderer, info) {
1625
1626
  this.buffer = new GLGPUBuffer({
1626
1627
  target: constants.UNIFORM_BUFFER,
1627
1628
  name: info.name,
1628
- type: constants.BYTE,
1629
- elementCount: info.size,
1629
+ byteLength: info.size,
1630
1630
  }, renderer);
1631
1631
  this.dirtyFlags = {};
1632
1632
  this.info = info;
@@ -2430,7 +2430,7 @@ function registerCompressedTexture(gl) {
2430
2430
  return 0;
2431
2431
  }
2432
2432
 
2433
- var _a$2, _b;
2433
+ var _a$2, _b$1;
2434
2434
  var flipCanvas;
2435
2435
  var imageBitMapAvailable$1 = typeof ImageBitmap === 'function' && typeof createImageBitmap == 'function';
2436
2436
  var GLTexture = /** @class */ (function () {
@@ -2761,14 +2761,14 @@ var FORMAT_HALF_FLOAT = (_a$2 = {},
2761
2761
  _a$2[constants.LUMINANCE_ALPHA] = 33327,
2762
2762
  _a$2[constants.LUMINANCE] = 33325,
2763
2763
  _a$2);
2764
- var FORMAT_FLOAT = (_b = {},
2765
- _b[constants.RGBA] = 34836,
2766
- _b[constants.RGB] = 34837,
2767
- _b[constants.ALPHA] = 33326,
2768
- _b[constants.RED] = 33326,
2769
- _b[constants.LUMINANCE_ALPHA] = 33328,
2770
- _b[constants.LUMINANCE] = 33326,
2771
- _b);
2764
+ var FORMAT_FLOAT = (_b$1 = {},
2765
+ _b$1[constants.RGBA] = 34836,
2766
+ _b$1[constants.RGB] = 34837,
2767
+ _b$1[constants.ALPHA] = 33326,
2768
+ _b$1[constants.RED] = 33326,
2769
+ _b$1[constants.LUMINANCE_ALPHA] = 33328,
2770
+ _b$1[constants.LUMINANCE] = 33326,
2771
+ _b$1);
2772
2772
 
2773
2773
  var GL_LOST_EVENT = 'webglcontextlost';
2774
2774
  var GLGPURenderer = /** @class */ (function () {
@@ -2814,6 +2814,12 @@ var GLGPURenderer = /** @class */ (function () {
2814
2814
  gl.canvas.addEventListener(GL_LOST_EVENT, this.onContextLose);
2815
2815
  }
2816
2816
  }
2817
+ GLGPURenderer.prototype.requestAnimationFrame = function (cb) {
2818
+ return window.requestAnimationFrame(cb);
2819
+ };
2820
+ GLGPURenderer.prototype.cancelAnimationFrame = function (id) {
2821
+ return window.cancelAnimationFrame(id);
2822
+ };
2817
2823
  Object.defineProperty(GLGPURenderer.prototype, "isDestroyed", {
2818
2824
  get: function () {
2819
2825
  return this._isDestroyed;
@@ -4884,12 +4890,15 @@ var GLVertexArrayObject = /** @class */ (function () {
4884
4890
  return GLVertexArrayObject;
4885
4891
  }());
4886
4892
 
4887
- var _a$1;
4893
+ var _a$1, _b;
4888
4894
  var INDEX_TYPE_MAP = (_a$1 = {},
4889
- _a$1[Uint8Array.BYTES_PER_ELEMENT] = constants.UNSIGNED_BYTE,
4890
4895
  _a$1[Uint16Array.BYTES_PER_ELEMENT] = constants.UNSIGNED_SHORT,
4891
4896
  _a$1[Uint32Array.BYTES_PER_ELEMENT] = constants.UNSIGNED_INT,
4892
4897
  _a$1);
4898
+ var TYPE_BYTE_MAP = (_b = {},
4899
+ _b[constants.UNSIGNED_SHORT] = Uint16Array.BYTES_PER_ELEMENT,
4900
+ _b[constants.UNSIGNED_INT] = Uint32Array.BYTES_PER_ELEMENT,
4901
+ _b);
4893
4902
  /**
4894
4903
  * GLGeometry对象,保存GLGPUBuffer对象。
4895
4904
  *
@@ -4915,7 +4924,8 @@ var GLGeometry = /** @class */ (function () {
4915
4924
  buffers[name] = new GLGPUBuffer(opt, renderer);
4916
4925
  });
4917
4926
  if (option.index && option.index.data) {
4918
- this._indexBuffer = this.createIndexBuffer(option.index.data, renderer);
4927
+ var ret = this.createIndexBuffer(option.index.data, renderer);
4928
+ this.setIndexBuffer(ret[0], ret[1]);
4919
4929
  }
4920
4930
  }
4921
4931
  this._buffersMap = buffers;
@@ -4937,18 +4947,18 @@ var GLGeometry = /** @class */ (function () {
4937
4947
  (_a = this.vaoMap[name]) === null || _a === void 0 ? void 0 : _a.destroy();
4938
4948
  this.vaoMap[name] = undefined;
4939
4949
  };
4940
- GLGeometry.prototype.setIndexBuffer = function (buffer) {
4950
+ GLGeometry.prototype.setIndexBuffer = function (buffer, indexBufferType) {
4941
4951
  this._indexBuffer = buffer;
4952
+ this.indexBufferType = indexBufferType;
4942
4953
  };
4943
4954
  GLGeometry.prototype.createIndexBuffer = function (data, renderer) {
4944
4955
  var type = INDEX_TYPE_MAP[data.BYTES_PER_ELEMENT];
4945
4956
  var indexOption = {
4946
4957
  data: data,
4947
4958
  target: constants.ELEMENT_ARRAY_BUFFER,
4948
- type: type,
4949
4959
  name: "".concat(this.name, "##index"),
4950
4960
  };
4951
- return new GLGPUBuffer(indexOption, renderer);
4961
+ return [new GLGPUBuffer(indexOption, renderer), type];
4952
4962
  };
4953
4963
  GLGeometry.prototype.getGPUBuffer = function (name) {
4954
4964
  return this._buffersMap[name];
@@ -4990,10 +5000,10 @@ var GLGeometry = /** @class */ (function () {
4990
5000
  if (gl && gpu) {
4991
5001
  var drawCount = this.drawCount;
4992
5002
  if (index) {
4993
- var type = index.type;
5003
+ var type = this.indexBufferType;
4994
5004
  //bind by vao
4995
5005
  //this._indexBuffer?.bind();
4996
- var dc = isNaN(drawCount) ? index.elementCount : drawCount;
5006
+ var dc = isNaN(drawCount) ? index.getElementCount(TYPE_BYTE_MAP[type]) : drawCount;
4997
5007
  if (dc > 0) {
4998
5008
  if (useInstancedDraw) {
4999
5009
  gpu.drawElementsInstanced(gl, this.mode, dc, type, this.drawStart || 0, state.currentMesh.instanceCount);
@@ -5089,7 +5099,6 @@ var MarsGeometry = /** @class */ (function () {
5089
5099
  buffers[name] = {
5090
5100
  data: data,
5091
5101
  usage: usage,
5092
- target: constants.ARRAY_BUFFER,
5093
5102
  name: name,
5094
5103
  };
5095
5104
  var gltype = attributeData2GLType(data);
@@ -5129,6 +5138,9 @@ var MarsGeometry = /** @class */ (function () {
5129
5138
  });
5130
5139
  // 顶点索引
5131
5140
  this._index = (_a = option.index) === null || _a === void 0 ? void 0 : _a.data;
5141
+ if (this._index instanceof Uint8Array) {
5142
+ throw Error('Uint8 Index not support');
5143
+ }
5132
5144
  dirtyFlags['index'] = {
5133
5145
  dirty: true,
5134
5146
  discard: true,
@@ -5243,8 +5255,6 @@ var MarsGeometry = /** @class */ (function () {
5243
5255
  var option = {
5244
5256
  data: data,
5245
5257
  usage: gpuBufferOption.usage,
5246
- target: gpuBufferOption.target,
5247
- elementCount: data.length,
5248
5258
  };
5249
5259
  buffers[gpuBufferName] = option;
5250
5260
  this._dirtyFlags[gpuBufferName].discard = true;
@@ -5344,19 +5354,21 @@ var MarsGeometry = /** @class */ (function () {
5344
5354
  return this._attributesName;
5345
5355
  };
5346
5356
  MarsGeometry.prototype.assignRenderer = function (renderer) {
5347
- var _a;
5348
5357
  if (!this.internal) {
5349
5358
  // 索引缓冲区参数
5350
5359
  var indexBufferOption = undefined;
5351
5360
  if (this._index) {
5352
- var type = INDEX_TYPE_MAP[(_a = this._index) === null || _a === void 0 ? void 0 : _a.BYTES_PER_ELEMENT];
5353
5361
  var data = this._index;
5354
5362
  indexBufferOption = {
5355
- target: constants.ELEMENT_ARRAY_BUFFER,
5356
- type: type,
5357
5363
  data: data,
5358
5364
  usage: constants.STATIC_DRAW,
5359
5365
  };
5366
+ if (data instanceof Uint16Array) {
5367
+ this.indexDataType = constants.SHORT;
5368
+ }
5369
+ else if (data instanceof Uint32Array) {
5370
+ this.indexDataType = constants.INT;
5371
+ }
5360
5372
  }
5361
5373
  // 顶点属性参数
5362
5374
  var options = {
@@ -5367,6 +5379,7 @@ var MarsGeometry = /** @class */ (function () {
5367
5379
  buffers: this._buffers,
5368
5380
  mode: this.mode,
5369
5381
  index: indexBufferOption,
5382
+ indexDataType: this.indexDataType,
5370
5383
  name: this.name,
5371
5384
  };
5372
5385
  //@ts-expect-error safe to set
@@ -5775,7 +5788,8 @@ var MarsSharedGeometry = /** @class */ (function (_super) {
5775
5788
  glgeo.drawStart = this.drawStart;
5776
5789
  glgeo.mode = this.mode;
5777
5790
  if (this._index) {
5778
- glgeo._indexBuffer = glgeo.createIndexBuffer(this._index, renderer.internal);
5791
+ var ret = glgeo.createIndexBuffer(this._index, renderer.internal);
5792
+ glgeo.setIndexBuffer(ret[0], ret[1]);
5779
5793
  }
5780
5794
  //@ts-expect-error safe to assign
5781
5795
  this.internal = glgeo;
@@ -5806,7 +5820,7 @@ var MarsSharedGeometry = /** @class */ (function (_super) {
5806
5820
  return MarsSharedGeometry;
5807
5821
  }(MarsGeometry));
5808
5822
 
5809
- consoleLog('version: ' + "0.1.61-beta.20");
5823
+ consoleLog('version: ' + "0.1.61-beta.23");
5810
5824
 
5811
- 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 };
5825
+ export { DestroyOptions, GPUBufferOptionsMemoryShared, 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 };
5812
5826
  //# sourceMappingURL=index.mjs.map