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

GLTFLoader: Add EXT_texture_avif support. #25173

Merged
merged 6 commits into from
Jan 31, 2023
Merged
Changes from 1 commit
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
84 changes: 84 additions & 0 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ const EXTENSIONS = {
KHR_MESH_QUANTIZATION: 'KHR_mesh_quantization',
KHR_MATERIALS_EMISSIVE_STRENGTH: 'KHR_materials_emissive_strength',
EXT_TEXTURE_WEBP: 'EXT_texture_webp',
EXT_TEXTURE_AVIF: 'EXT_texture_avif',
EXT_MESHOPT_COMPRESSION: 'EXT_meshopt_compression',
EXT_MESH_GPU_INSTANCING: 'EXT_mesh_gpu_instancing'
};
Expand Down Expand Up @@ -1314,6 +1315,89 @@ class GLTFTextureWebPExtension {

}

/**
* AVIF Texture Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_texture_avif
*/
class GLTFTextureAVIFExtension {
leon marked this conversation as resolved.
Show resolved Hide resolved
Fixed Show fixed Hide fixed

constructor( parser ) {

this.parser = parser;
this.name = EXTENSIONS.EXT_TEXTURE_AVIF;
this.isSupported = null;

}

loadTexture( textureIndex ) {

const name = this.name;
const parser = this.parser;
const json = parser.json;

const textureDef = json.textures[ textureIndex ];

if ( ! textureDef.extensions || ! textureDef.extensions[ name ] ) {

return null;

}

const extension = textureDef.extensions[ name ];
const source = json.images[ extension.source ];

let loader = parser.textureLoader;
if ( source.uri ) {

const handler = parser.options.manager.getHandler( source.uri );
if ( handler !== null ) loader = handler;

}

return this.detectSupport().then( function ( isSupported ) {

if ( isSupported ) return parser.loadTextureImage( textureIndex, extension.source, loader );

if ( json.extensionsRequired && json.extensionsRequired.indexOf( name ) >= 0 ) {

throw new Error( 'THREE.GLTFLoader: AVIF required by asset but unsupported.' );

}

// Fall back to PNG or JPEG.
return parser.loadTexture( textureIndex );

} );

}

detectSupport() {

if ( ! this.isSupported ) {

this.isSupported = new Promise( function ( resolve ) {

const image = new Image();

// Lossy test image.
image.src = 'data:image/avif;base64,AAAAIGZ0eXBhdmlmAAAAAGF2aWZtaWYxbWlhZk1BMUIAAADybWV0YQAAAAAAAAAoaGRscgAAAAAAAAAAcGljdAAAAAAAAAAAAAAAAGxpYmF2aWYAAAAADnBpdG0AAAAAAAEAAAAeaWxvYwAAAABEAAABAAEAAAABAAABGgAAABcAAAAoaWluZgAAAAAAAQAAABppbmZlAgAAAAABAABhdjAxQ29sb3IAAAAAamlwcnAAAABLaXBjbwAAABRpc3BlAAAAAAAAAAEAAAABAAAAEHBpeGkAAAAAAwgICAAAAAxhdjFDgQAMAAAAABNjb2xybmNseAACAAIABoAAAAAXaXBtYQAAAAAAAAABAAEEAQKDBAAAAB9tZGF0EgAKCBgABogQEDQgMgkQAAAAB8dSLfI=';
image.onload = image.onerror = function () {

resolve( image.height === 1 );

};

} );

}

return this.isSupported;

}

}

/**
* meshopt BufferView Compression Extension
*
Expand Down