@lingbi/studio 0.9.0 → 0.9.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.
Files changed (2) hide show
  1. package/index.js +104 -19
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -32221,6 +32221,85 @@ class OBJLoader extends Loader {
32221
32221
  return container;
32222
32222
  }
32223
32223
  }
32224
+ class StreamingResourceReader {
32225
+ async readText(url, signal, progress) {
32226
+ const response = await this.fetchOk(url, signal);
32227
+ const streamedBody = this.getStreamedBody(response, progress);
32228
+ if (streamedBody === null) {
32229
+ return response.text();
32230
+ }
32231
+ return new TextDecoder().decode(
32232
+ await this.readStreamingBody(
32233
+ streamedBody.stream,
32234
+ streamedBody.contentLength,
32235
+ streamedBody.progress
32236
+ )
32237
+ );
32238
+ }
32239
+ async readArrayBuffer(url, signal, progress) {
32240
+ const response = await this.fetchOk(url, signal);
32241
+ const streamedBody = this.getStreamedBody(response, progress);
32242
+ if (streamedBody === null) {
32243
+ return response.arrayBuffer();
32244
+ }
32245
+ const bytes = await this.readStreamingBody(
32246
+ streamedBody.stream,
32247
+ streamedBody.contentLength,
32248
+ streamedBody.progress
32249
+ );
32250
+ return bytes.buffer;
32251
+ }
32252
+ async fetchOk(url, signal) {
32253
+ const response = await fetch(url, { signal });
32254
+ if (!response.ok) {
32255
+ throw new Error(`Unable to load resource: ${url}`);
32256
+ }
32257
+ return response;
32258
+ }
32259
+ getStreamedBody(response, progress) {
32260
+ if (progress === void 0) {
32261
+ return null;
32262
+ }
32263
+ const contentLength = this.parseContentLength(response);
32264
+ if (contentLength === void 0) {
32265
+ return null;
32266
+ }
32267
+ const stream = response.body;
32268
+ return stream === null ? null : { contentLength, progress, stream };
32269
+ }
32270
+ async readStreamingBody(stream, contentLength, progress) {
32271
+ const reader = stream.getReader();
32272
+ const chunks = [];
32273
+ let loaded = 0;
32274
+ for (; ; ) {
32275
+ const next = await reader.read();
32276
+ if (next.done) {
32277
+ break;
32278
+ }
32279
+ chunks.push(next.value);
32280
+ loaded += next.value.byteLength;
32281
+ progress(Math.min(1, loaded / contentLength));
32282
+ }
32283
+ return this.mergeChunks(chunks, loaded);
32284
+ }
32285
+ mergeChunks(chunks, total) {
32286
+ const merged = new Uint8Array(total);
32287
+ let offset = 0;
32288
+ for (const chunk of chunks) {
32289
+ merged.set(chunk, offset);
32290
+ offset += chunk.byteLength;
32291
+ }
32292
+ return merged;
32293
+ }
32294
+ parseContentLength(response) {
32295
+ const raw = response.headers.get("Content-Length");
32296
+ if (raw === null) {
32297
+ return void 0;
32298
+ }
32299
+ const parsed = Number(raw);
32300
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : void 0;
32301
+ }
32302
+ }
32224
32303
  const DEFAULT_MODEL_COLOR$1 = 10265519;
32225
32304
  const DEFAULT_POINT_SIZE$1 = 3;
32226
32305
  class TextureLoadMonitor {
@@ -32321,6 +32400,7 @@ class TextureLoadMonitor {
32321
32400
  }
32322
32401
  class ObjLoader {
32323
32402
  parser;
32403
+ resourceReader = new StreamingResourceReader();
32324
32404
  constructor(parser = new OBJLoader()) {
32325
32405
  this.parser = parser;
32326
32406
  }
@@ -32345,7 +32425,7 @@ class ObjLoader {
32345
32425
  let warnings = [];
32346
32426
  try {
32347
32427
  const objFetchStage = performanceTrace.startStage("obj-fetch", role);
32348
- const objSource = await this.loadText(resources.objUrl, signal);
32428
+ const objSource = await this.loadText(resources.objUrl, signal, progress);
32349
32429
  performanceTrace.endStage(objFetchStage, "success");
32350
32430
  progress?.markModelFetched();
32351
32431
  this.assertNotCancelled(signal);
@@ -32777,14 +32857,12 @@ class ObjLoader {
32777
32857
  }
32778
32858
  return outcome;
32779
32859
  }
32780
- async loadText(url, signal) {
32781
- const response = await fetch(url, {
32782
- signal
32783
- });
32784
- if (!response.ok) {
32785
- throw new Error(`Unable to load OBJ resource: ${url}`);
32786
- }
32787
- return response.text();
32860
+ async loadText(url, signal, progress) {
32861
+ return this.resourceReader.readText(
32862
+ url,
32863
+ signal,
32864
+ progress === void 0 ? void 0 : progress.markModelFetchProgress.bind(progress)
32865
+ );
32788
32866
  }
32789
32867
  getImportPerformanceFailureStatus(signal) {
32790
32868
  return signal.aborted ? "cancelled" : "failure";
@@ -33677,6 +33755,7 @@ const DEFAULT_POINT_SIZE = 3;
33677
33755
  const MAX_HEADER_BYTES = 65536;
33678
33756
  class PlyLoader {
33679
33757
  parser;
33758
+ resourceReader = new StreamingResourceReader();
33680
33759
  textureLoader;
33681
33760
  constructor(parser = new PLYLoader(), textureLoader = new PlyTextureLoader()) {
33682
33761
  this.parser = parser;
@@ -33689,7 +33768,7 @@ class PlyLoader {
33689
33768
  success: false
33690
33769
  };
33691
33770
  }
33692
- const source = await this.fetchSource(resources.plyUrl, signal);
33771
+ const source = await this.fetchSource(resources.plyUrl, signal, progress);
33693
33772
  if (!(source instanceof ArrayBuffer)) {
33694
33773
  return source;
33695
33774
  }
@@ -33815,16 +33894,13 @@ class PlyLoader {
33815
33894
  warnings: []
33816
33895
  };
33817
33896
  }
33818
- async fetchSource(url, signal) {
33897
+ async fetchSource(url, signal, progress) {
33819
33898
  try {
33820
- const response = await fetch(url, { signal });
33821
- if (!response.ok) {
33822
- return {
33823
- code: ObjImportFailureCode$1.InvalidSource,
33824
- success: false
33825
- };
33826
- }
33827
- return await response.arrayBuffer();
33899
+ return await this.resourceReader.readArrayBuffer(
33900
+ url,
33901
+ signal,
33902
+ progress === void 0 ? void 0 : progress.markModelFetchProgress.bind(progress)
33903
+ );
33828
33904
  } catch {
33829
33905
  return {
33830
33906
  code: signal.aborted ? ObjImportFailureCode$1.Cancelled : ObjImportFailureCode$1.InvalidSource,
@@ -33887,6 +33963,12 @@ class RoleImportProgressReporter {
33887
33963
  this.tracker.setRolePosition(this.role, this.profile.materialsPosition);
33888
33964
  this.reportTexturePosition();
33889
33965
  }
33966
+ markModelFetchProgress(fraction) {
33967
+ this.tracker.setRolePosition(
33968
+ this.role,
33969
+ this.profile.fetchedPosition * this.clampFraction(fraction)
33970
+ );
33971
+ }
33890
33972
  markModelFetched() {
33891
33973
  this.tracker.setRolePosition(this.role, this.profile.fetchedPosition);
33892
33974
  }
@@ -33909,6 +33991,9 @@ class RoleImportProgressReporter {
33909
33991
  this.profile.parsedPosition + textureSpan * settledShare
33910
33992
  );
33911
33993
  }
33994
+ clampFraction(fraction) {
33995
+ return Math.max(0, Math.min(1, fraction));
33996
+ }
33912
33997
  }
33913
33998
  class SimulationSnapshotFactory {
33914
33999
  constructor(scheduler) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingbi/studio",
3
- "version": "0.9.0",
3
+ "version": "0.9.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/genewanggang-sudo/lingbi-shitou.git"