Skip to content
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

Fix GLTFExporter when exporting in GLTF with OffscreenCanvas #24031

Merged
merged 7 commits into from
May 11, 2022
Merged
Changes from 5 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
80 changes: 50 additions & 30 deletions examples/jsm/exporters/GLTFExporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,37 @@ function getCanvas() {

}

function getToBlobPromise( canvas ) {

if ( canvas.toBlob !== undefined ) {

return new Promise( ( resolve ) => canvas.toBlob( resolve, mimeType ) );
LeviPesin marked this conversation as resolved.
Show resolved Hide resolved

}

let quality;

// Blink's implementation of convertToBlob seems to default to a quality level of 100%
// Use the Blink default quality levels of toBlob instead so that file sizes are comparable.
if ( mimeType === 'image/jpeg' ) {

quality = 0.92;

} else if ( mimeType === 'image/webp' ) {

quality = 0.8;

}

return canvas.convertToBlob( {

type: mimeType,
quality: quality

} );

}

/**
* Writer
*/
Expand Down Expand Up @@ -1104,50 +1135,39 @@ class GLTFWriter {

if ( options.binary === true ) {

let toBlobPromise;
pending.push(

if ( canvas.toBlob !== undefined ) {
getToBlobPromise( canvas )
.then( blob => writer.processBufferViewImage( blob ) )
.then( bufferViewIndex => {

toBlobPromise = new Promise( ( resolve ) => canvas.toBlob( resolve, mimeType ) );

} else {

let quality;

// Blink's implementation of convertToBlob seems to default to a quality level of 100%
// Use the Blink default quality levels of toBlob instead so that file sizes are comparable.
if ( mimeType === 'image/jpeg' ) {

quality = 0.92;

} else if ( mimeType === 'image/webp' ) {

quality = 0.8;
imageDef.bufferView = bufferViewIndex;

}
} )

toBlobPromise = canvas.convertToBlob( {
);

type: mimeType,
quality: quality
} else {

} );
if ( canvas.toDataURL !== undefined ) {

}
imageDef.uri = canvas.toDataURL( mimeType );

pending.push( toBlobPromise.then( blob =>
} else {

writer.processBufferViewImage( blob ).then( bufferViewIndex => {
pending.push(

imageDef.bufferView = bufferViewIndex;
getToBlobPromise( canvas )
.then( blob => new FileReader().readAsDataURL( blob ) )
.then( dataURL => {

} )
imageDef.uri = dataURL;

) );
} )

} else {
);

imageDef.uri = canvas.toDataURL( mimeType );
}

}

Expand Down