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

Support WebP plugin #2310

Merged
merged 4 commits into from
Aug 7, 2024
Merged

Conversation

zhuxudong
Copy link
Member

@zhuxudong zhuxudong commented Aug 6, 2024

Please check if the PR fulfills these requirements

Support #2309

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

What is the current behavior? (You can also link to an open issue here)

What is the new behavior (if this is a feature change)?

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

Other information:

Summary by CodeRabbit

  • New Features

    • Introduced support for WebP textures in GLTF, enhancing texture loading efficiency and reducing memory usage.
    • Added an optional source property in the texture interface for increased flexibility in texture definitions.
  • Bug Fixes

    • Improved texture parsing logic for better organization and error handling.
  • Refactor

    • Streamlined texture loading process through modularization of parsing methods for improved maintenance.

@zhuxudong zhuxudong added the enhancement New feature or request label Aug 6, 2024
@zhuxudong zhuxudong requested a review from GuoLei1990 August 6, 2024 10:25
Copy link

coderabbitai bot commented Aug 6, 2024

Walkthrough

Recent modifications enhance the GLTF framework by introducing optional properties for texture definitions and adding support for WebP textures. The ITexture interface now allows the omission of the source property, increasing flexibility. Additionally, a new module for WebP texture handling has been integrated, improving loading efficiency. The restructuring of the GLTFTextureParser enhances code maintainability and clarity, streamlining the texture loading process.

Changes

Files Change Summary
.../gltf/GLTFSchema.ts, .../gltf/extensions/EXT_texture_webp.ts, .../gltf/extensions/index.ts Modified ITexture to make source optional, introduced EXT_texture_webp for WebP support, and imported the new extension in the index file.
.../gltf/parser/GLTFTextureParser.ts Refactored GLTFTextureParser to use a new static method _parseTexture, enhancing the structure and readability of texture loading logic.

Sequence Diagram(s)

sequenceDiagram
    participant A as GLTFParser
    participant B as GLTFTextureParser
    participant C as EXT_texture_webp
    participant D as TextureLoader

    A->>B: parse(textureInfo)
    B->>D: _parseTexture(imageIndex, textureIndex)
    D-->>B: Return Texture
    B-->>A: Return Parsed Texture
Loading

🐇 In the world of GLTF, textures anew,
With WebP support, we're dancing in view!
Optional sources, oh what a delight,
Parsing with ease, everything feels right.
Hopping through changes, we cheer and we play,
Here's to our textures, brightening the day! ✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f01c996 and 8cbd497.

Files selected for processing (4)
  • packages/loader/src/gltf/GLTFSchema.ts (1 hunks)
  • packages/loader/src/gltf/extensions/EXT_texture_webp.ts (1 hunks)
  • packages/loader/src/gltf/extensions/index.ts (1 hunks)
  • packages/loader/src/gltf/parser/GLTFTextureParser.ts (2 hunks)
Additional comments not posted (8)
packages/loader/src/gltf/extensions/index.ts (1)

18-18: Importing the WebP extension looks good.

The import statement for the EXT_texture_webp module is correctly added, enhancing the module's capabilities by including WebP texture handling functionality.

packages/loader/src/gltf/extensions/EXT_texture_webp.ts (3)

1-10: Imports and interface definition look good.

The imports from the relevant modules and the interface definition for EXTWebPSchema are appropriate and correctly implemented.


12-23: Constructor implementation looks good.

The constructor correctly checks for WebP support using a canvas element and handles exceptions appropriately.


25-42: createAndParse method implementation looks good.

The method correctly handles the creation and parsing of WebP textures, utilizing the _parseTexture method from GLTFTextureParser.

packages/loader/src/gltf/parser/GLTFTextureParser.ts (3)

Line range hint 3-16:
Imports and static property definition look good.

The imports from the relevant modules and the static property _wrapMap are appropriate and correctly implemented.


18-77: _parseTexture method implementation looks good.

The method correctly handles the parsing and loading of textures, improving modularity and maintainability.


79-91: parse method implementation looks good.

The method correctly utilizes the _parseTexture method, enhancing code organization and readability.

packages/loader/src/gltf/GLTFSchema.ts (1)

766-766: LGTM! But verify the impact on the codebase.

The change to make the source property optional in the ITexture interface increases flexibility. However, ensure that all parts of the codebase that use this interface handle the source property being potentially undefined.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Outside diff range, codebase verification and nitpick comments (1)
packages/loader/src/gltf/parser/GLTFTextureParser.ts (1)

Line range hint 90-95:
Ensure promise rejection is handled.

Ensure that any errors during texture parsing are properly handled and rejected.

-    return Promise.resolve(texture).then((texture) => {
+    return Promise.resolve(texture)
+      .then((texture) => {
+        GLTFParser.executeExtensionsAdditiveAndParse(extensions, context, texture, textureInfo);
+        // @ts-ignore
+        texture._associationSuperResource(glTFResource);
+        return texture;
+      })
+      .catch((error) => {
+        console.error(`Failed to parse texture: ${textureName}`, error);
+        throw error;
+      });
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8cbd497 and ca20789.

Files selected for processing (1)
  • packages/loader/src/gltf/parser/GLTFTextureParser.ts (2 hunks)
Additional comments not posted (2)
packages/loader/src/gltf/parser/GLTFTextureParser.ts (2)

18-24: Good modularization with _parseTexture.

The introduction of _parseTexture improves code organization by encapsulating the texture loading logic.


79-79: Improved readability with _parseTexture delegation.

The delegation to _parseTexture improves readability and maintainability.

const { glTFResource, glTF } = context;
const { engine, url } = glTFResource;
const { sampler, source = 0, name: textureName, extensions } = textureInfo;
const { uri, bufferView: bufferViewIndex, mimeType, name: imageName } = glTF.images[source];
const { uri, bufferView: bufferViewIndex, mimeType, name: imageName } = glTF.images[imageIndex];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure imageIndex is within bounds.

Before accessing glTF.images[imageIndex], ensure imageIndex is within the bounds of glTF.images.

+    if (imageIndex < 0 || imageIndex >= glTF.images.length) {
+      throw new Error(`Invalid imageIndex: ${imageIndex}`);
+    }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { uri, bufferView: bufferViewIndex, mimeType, name: imageName } = glTF.images[imageIndex];
if (imageIndex < 0 || imageIndex >= glTF.images.length) {
throw new Error(`Invalid imageIndex: ${imageIndex}`);
}
const { uri, bufferView: bufferViewIndex, mimeType, name: imageName } = glTF.images[imageIndex];

Comment on lines +33 to +36
if (uri) {
const extIndex = uri.lastIndexOf(".");
const ext = uri.substring(extIndex + 1);
const type = ext.startsWith("ktx") ? AssetType.KTX : AssetType.Texture2D;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider supporting additional texture formats.

Currently, the code supports KTX and Texture2D. Consider extending support for other formats like WebP.

-      const type = ext.startsWith("ktx") ? AssetType.KTX : AssetType.Texture2D;
+      let type;
+      if (ext.startsWith("ktx")) {
+        type = AssetType.KTX;
+      } else if (ext === "webp") {
+        type = AssetType.WebP;
+      } else {
+        type = AssetType.Texture2D;
+      }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (uri) {
const extIndex = uri.lastIndexOf(".");
const ext = uri.substring(extIndex + 1);
const type = ext.startsWith("ktx") ? AssetType.KTX : AssetType.Texture2D;
if (uri) {
const extIndex = uri.lastIndexOf(".");
const ext = uri.substring(extIndex + 1);
let type;
if (ext.startsWith("ktx")) {
type = AssetType.KTX;
} else if (ext === "webp") {
type = AssetType.WebP;
} else {
type = AssetType.Texture2D;
}

Comment on lines +56 to +61
texture = context.get<ArrayBuffer>(GLTFParserType.Buffer).then((buffers) => {
const buffer = buffers[bufferView.buffer];
const imageBuffer = new Uint8Array(buffer, bufferView.byteOffset, bufferView.byteLength);

return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then((image) => {
const texture = new Texture2D(engine, image.width, image.height, undefined, samplerInfo?.mipmap);
texture.setImageSource(image);
texture.generateMipmaps();
return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then((image) => {
const texture = new Texture2D(engine, image.width, image.height, undefined, samplerInfo?.mipmap);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle potential errors in buffer view processing.

Ensure proper error handling when processing buffer views and loading image buffers.

+      if (!bufferView) {
+        throw new Error(`Buffer view not found for index: ${bufferViewIndex}`);
+      }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
texture = context.get<ArrayBuffer>(GLTFParserType.Buffer).then((buffers) => {
const buffer = buffers[bufferView.buffer];
const imageBuffer = new Uint8Array(buffer, bufferView.byteOffset, bufferView.byteLength);
return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then((image) => {
const texture = new Texture2D(engine, image.width, image.height, undefined, samplerInfo?.mipmap);
texture.setImageSource(image);
texture.generateMipmaps();
return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then((image) => {
const texture = new Texture2D(engine, image.width, image.height, undefined, samplerInfo?.mipmap);
if (!bufferView) {
throw new Error(`Buffer view not found for index: ${bufferViewIndex}`);
}
texture = context.get<ArrayBuffer>(GLTFParserType.Buffer).then((buffers) => {
const buffer = buffers[bufferView.buffer];
const imageBuffer = new Uint8Array(buffer, bufferView.byteOffset, bufferView.byteLength);
return GLTFUtils.loadImageBuffer(imageBuffer, mimeType).then((image) => {
const texture = new Texture2D(engine, image.width, image.height, undefined, samplerInfo?.mipmap);

Comment on lines +68 to +69
const bufferTextureRestoreInfo = new BufferTextureRestoreInfo(texture, bufferView, mimeType);
context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure bufferView and mimeType are valid.

Before creating BufferTextureRestoreInfo, ensure bufferView and mimeType are valid.

+      if (!bufferView || !mimeType) {
+        throw new Error(`Invalid bufferView or mimeType for texture: ${textureName}`);
+      }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const bufferTextureRestoreInfo = new BufferTextureRestoreInfo(texture, bufferView, mimeType);
context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);
if (!bufferView || !mimeType) {
throw new Error(`Invalid bufferView or mimeType for texture: ${textureName}`);
}
const bufferTextureRestoreInfo = new BufferTextureRestoreInfo(texture, bufferView, mimeType);
context.contentRestorer.bufferTextures.push(bufferTextureRestoreInfo);

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ca20789 and c317ccb.

Files selected for processing (1)
  • packages/loader/src/gltf/extensions/EXT_texture_webp.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/gltf/extensions/EXT_texture_webp.ts

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c317ccb and 4c9eb12.

Files selected for processing (1)
  • packages/loader/src/gltf/extensions/EXT_texture_webp.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/gltf/extensions/EXT_texture_webp.ts

this._supportWebP = document.createElement("canvas").toDataURL("image/webp").indexOf("data:image/webp") == 0;
} catch (e) {
this._supportWebP = false;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use try catch

@GuoLei1990 GuoLei1990 merged commit e1a8afe into galacean:main Aug 7, 2024
6 of 7 checks passed
@GuoLei1990 GuoLei1990 added the resource Resource-related functions label Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request resource Resource-related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants