Skip to content

Expose PLY load progress #7259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/framework/asset/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,21 @@ class Asset extends EventHandler {
*/
static EVENT_CHANGE = 'change';

/**
* Fired when the asset's stream download progresses.
*
* Please note:
* - only gsplat assets current emit this event
* - totalBytes may not be reliable as it is based on the content-length header of the response
*
* @event
* @example
* asset.on('progress', (receivedBytes, totalBytes) => {
* console.log(`Asset ${asset.name} progress ${readBytes / totalBytes}`);
* });
*/
static EVENT_PROGRESS = 'progress';

/**
* Fired when we add a new localized asset id to the asset.
*
Expand Down
25 changes: 21 additions & 4 deletions src/framework/parsers/ply.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const dataTypeMap = new Map([
class StreamBuf {
reader;

progressFunc;

data;

view;
Expand All @@ -54,8 +56,9 @@ class StreamBuf {

tail = 0;

constructor(reader) {
constructor(reader, progressFunc) {
this.reader = reader;
this.progressFunc = progressFunc;
}

// read the next chunk of data
Expand All @@ -67,6 +70,7 @@ class StreamBuf {
}

this.push(value);
this.progressFunc?.(value.byteLength);
}

// append data to the buffer
Expand Down Expand Up @@ -395,9 +399,10 @@ const readGeneralPly = async (streamBuf, elements) => {
*
* @param {ReadableStreamDefaultReader<Uint8Array>} reader - The reader.
* @param {Function|null} propertyFilter - Function to filter properties with.
* @param {Function|null} progressFunc - Function to call with progress updates.
* @returns {Promise<{ data: GSplatData | GSplatCompressedData, comments: string[] }>} The ply file data.
*/
const readPly = async (reader, propertyFilter = null) => {
const readPly = async (reader, propertyFilter = null, progressFunc = null) => {
/**
* Searches for the first occurrence of a sequence within a buffer.
* @example
Expand Down Expand Up @@ -444,7 +449,7 @@ const readPly = async (reader, propertyFilter = null) => {
return true;
};

const streamBuf = new StreamBuf(reader);
const streamBuf = new StreamBuf(reader, progressFunc);
let headerLength;

while (true) {
Expand Down Expand Up @@ -553,7 +558,19 @@ class PlyParser {
if (!response || !response.body) {
callback('Error loading resource', null);
} else {
const { data, comments } = await readPly(response.body.getReader(), asset.data.elementFilter ?? defaultElementFilter);
const totalLength = parseInt(response.headers.get('content-length') ?? '0', 10);
let totalReceived = 0;

const { data, comments } = await readPly(
response.body.getReader(),
asset.data.elementFilter ?? defaultElementFilter,
(bytes) => {
totalReceived += bytes;
if (asset) {
asset.fire('progress', totalReceived, totalLength);
}
}
);

// reorder data
if (!data.isCompressed) {
Expand Down
Loading