Skip to content

Commit

Permalink
EXT_texture_avif implementation (BabylonJS#13370)
Browse files Browse the repository at this point in the history
* EXT_texture_avif implementation

* added avif to places where I forgot it
  • Loading branch information
leon authored Apr 2, 2024
1 parent cb2f20f commit f09a970
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ export class GLTFComponent extends React.Component<IGLTFComponentProps> {
isSelected={() => extensionStates["EXT_texture_webp"].enabled}
onSelect={(value) => (extensionStates["EXT_texture_webp"].enabled = value)}
/>
<CheckBoxLineComponent
label="EXT_texture_avif"
isSelected={() => extensionStates["EXT_texture_avif"].enabled}
onSelect={(value) => (extensionStates["EXT_texture_avif"].enabled = value)}
/>
<CheckBoxLineComponent
label="KHR_draco_mesh_compression"
isSelected={() => extensionStates["KHR_draco_mesh_compression"].enabled}
Expand Down
1 change: 1 addition & 0 deletions packages/dev/inspector/src/components/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export class GlobalState {
EXT_lights_image_based: { enabled: true },
EXT_mesh_gpu_instancing: { enabled: true },
EXT_texture_webp: { enabled: true },
EXT_texture_avif: { enabled: true },
};

public glTFLoaderDefaults: { [key: string]: any } = {
Expand Down
58 changes: 58 additions & 0 deletions packages/dev/loaders/src/glTF/2.0/Extensions/EXT_texture_avif.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import type { IGLTFLoaderExtension } from "../glTFLoaderExtension";
import { GLTFLoader, ArrayItem } from "../glTFLoader";
import type { ITexture } from "../glTFLoaderInterfaces";
import type { BaseTexture } from "core/Materials/Textures/baseTexture";
import type { Nullable } from "core/types";
import type { IEXTTextureAVIF } from "babylonjs-gltf2interface";

const NAME = "EXT_texture_avif";

/**
* [glTF PR](https://github.com/KhronosGroup/glTF/pull/2235)
* [Specification](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Vendor/EXT_texture_avif/README.md)
*/
// eslint-disable-next-line @typescript-eslint/naming-convention
export class EXT_texture_avif implements IGLTFLoaderExtension {
/** The name of this extension. */
public readonly name = NAME;

/** Defines whether this extension is enabled. */
public enabled: boolean;

private _loader: GLTFLoader;

/**
* @internal
*/
constructor(loader: GLTFLoader) {
this._loader = loader;
this.enabled = loader.isExtensionUsed(NAME);
}

/** @internal */
public dispose() {
(this._loader as any) = null;
}

/**
* @internal
*/
public _loadTextureAsync(context: string, texture: ITexture, assign: (babylonTexture: BaseTexture) => void): Nullable<Promise<BaseTexture>> {
return GLTFLoader.LoadExtensionAsync<IEXTTextureAVIF, BaseTexture>(context, texture, this.name, (extensionContext, extension) => {
const sampler = texture.sampler == undefined ? GLTFLoader.DefaultSampler : ArrayItem.Get(`${context}/sampler`, this._loader.gltf.samplers, texture.sampler);
const image = ArrayItem.Get(`${extensionContext}/source`, this._loader.gltf.images, extension.source);
return this._loader._createTextureAsync(
context,
sampler,
image,
(babylonTexture) => {
assign(babylonTexture);
},
undefined,
!texture._textureInfo.nonColorData
);
});
}
}

GLTFLoader.RegisterExtension(NAME, (loader) => new EXT_texture_avif(loader));
1 change: 1 addition & 0 deletions packages/dev/loaders/src/glTF/2.0/Extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./EXT_lights_image_based";
export * from "./EXT_mesh_gpu_instancing";
export * from "./EXT_meshopt_compression";
export * from "./EXT_texture_webp";
export * from "./EXT_texture_avif";
export * from "./KHR_draco_mesh_compression";
export * from "./KHR_lights_punctual";
export * from "./KHR_materials_pbrSpecularGlossiness";
Expand Down
2 changes: 2 additions & 0 deletions packages/dev/serializers/src/glTF/2.0/glTFMaterialExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ function getFileExtensionFromMimeType(mimeType: ImageMimeType): string {
return ".png";
case ImageMimeType.WEBP:
return ".webp";
case ImageMimeType.AVIF:
return ".avif";
}
}

Expand Down
13 changes: 13 additions & 0 deletions packages/public/glTF2Interface/babylon.glTF2Interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ declare module BABYLON.GLTF2 {
* WEBP Mime-type
*/
WEBP = "image/webp",
/**
* AVIF Mime-type
*/
AVIF = "image/avif",
}

/**
Expand Down Expand Up @@ -1204,6 +1208,15 @@ declare module BABYLON.GLTF2 {
source: number;
}

/**
* Interfaces from the EXT_texture_avif extension
*/

/** @internal */
interface IEXTTextureAVIF {
source: number;
}

/**
* Interfaces from the KHR_texture_transform extension
*/
Expand Down

0 comments on commit f09a970

Please sign in to comment.