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

PBR support sheen #2448

Merged
merged 5 commits into from
Dec 12, 2024
Merged

PBR support sheen #2448

merged 5 commits into from
Dec 12, 2024

Conversation

hhhhkrx
Copy link
Contributor

@hhhhkrx hhhhkrx commented Dec 3, 2024

Please check if the PR fulfills these requirements

  • 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 sheen properties to materials, enhancing visual effects with new settings for sheen color, roughness, and textures.
    • Added support for the "KHR_materials_sheen" GLTF extension, allowing for better parsing of sheen-related material properties.
  • Bug Fixes

    • Improved texture validation for sheen properties to ensure proper rendering.

@hhhhkrx hhhhkrx added Rendering Rendering related functions shader Shader related functions glTF material labels Dec 3, 2024
Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

The pull request introduces enhancements to the PBRMaterial class by adding properties and methods for managing sheen effects, including color and roughness. Additionally, a new class, KHR_materials_sheen, is created to handle the parsing of sheen-related properties from GLTF materials. This includes logic for extracting sheen color and roughness factors, as well as managing associated textures. These changes collectively improve the material rendering capabilities in the graphics framework.

Changes

File Path Change Summary
packages/core/src/material/PBRMaterial.ts - Added private boolean property _sheenEnabled and four static properties for sheen: _sheenColorProp, _sheenRoughnessProp, _sheenTextureProp, _sheenRoughnessTextureProp.
- Implemented getters and setters for sheen properties: sheenColor, sheenRoughness, sheenColorTexture, sheenRoughnessTexture.
- Updated constructor to initialize _sheenColorProp to black (new Color(0, 0, 0)), with event handling for sheen color changes.
- Setters for sheenColorTexture and sheenRoughnessTexture include macro management logic.
packages/loader/src/gltf/extensions/KHR_materials_sheen.ts - Added KHR_materials_sheen class extending GLTFExtensionParser.
- Implemented additiveParse method for parsing sheen properties from GLTF materials, handling sheen color, roughness, and associated textures.

Possibly related PRs

  • PBR shader support Iridescence #2425: The changes in this PR enhance the PBRMaterial class by adding support for iridescence properties, which is closely related to the sheen properties introduced in the main PR, as both involve modifications to the same class and similar material rendering capabilities.

Suggested reviewers

  • zhuxudong
  • GuoLei1990

Poem

🐇 In the meadow where sheen does gleam,
New properties dance, like a dream.
With colors that shimmer and roughness so fine,
Our materials now sparkle, truly divine!
Hopping along, with textures that play,
Rendering magic in a bright, joyful way! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5604310 and affd513.

📒 Files selected for processing (2)
  • packages/core/src/material/PBRMaterial.ts (5 hunks)
  • packages/loader/src/gltf/extensions/KHR_materials_sheen.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/loader/src/gltf/extensions/KHR_materials_sheen.ts
  • packages/core/src/material/PBRMaterial.ts

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

codecov bot commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 46.77419% with 66 lines in your changes missing coverage. Please review.

Project coverage is 67.89%. Comparing base (785455c) to head (affd513).
Report is 128 commits behind head on dev/1.4.

Files with missing lines Patch % Lines
packages/core/src/material/PBRMaterial.ts 55.42% 37 Missing ⚠️
.../loader/src/gltf/extensions/KHR_materials_sheen.ts 29.26% 29 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           dev/1.4    #2448      +/-   ##
===========================================
+ Coverage    67.12%   67.89%   +0.77%     
===========================================
  Files          897      908      +11     
  Lines        93218    94498    +1280     
  Branches      7581     7926     +345     
===========================================
+ Hits         62572    64163    +1591     
+ Misses       30401    30085     -316     
- Partials       245      250       +5     
Flag Coverage Δ
unittests 67.89% <46.77%> (+0.77%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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: 4

🧹 Outside diff range and nitpick comments (2)
packages/loader/src/gltf/extensions/KHR_materials_sheen.ts (1)

12-40: Unit tests are missing for the new KHR_materials_sheen extension

The newly added KHR_materials_sheen class and its parsing logic are not covered by tests. Adding unit tests will help ensure the correctness and stability of the sheen extension parsing, especially for different schema variations.

Would you like assistance in creating unit tests for this new extension?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 12-40: packages/loader/src/gltf/extensions/KHR_materials_sheen.ts#L12-L40
Added lines #L12 - L40 were not covered by tests

packages/core/src/material/PBRMaterial.ts (1)

236-307: Unit tests are missing for the new sheen properties

The added properties and methods related to sheen are not covered by unit tests. Testing these will help prevent regressions and ensure they work as expected under various conditions.

Would you like help in creating unit tests for these new properties?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 236-237: packages/core/src/material/PBRMaterial.ts#L236-L237
Added lines #L236 - L237 were not covered by tests


[warning] 240-248: packages/core/src/material/PBRMaterial.ts#L240-L248
Added lines #L240 - L248 were not covered by tests


[warning] 255-256: packages/core/src/material/PBRMaterial.ts#L255-L256
Added lines #L255 - L256 were not covered by tests


[warning] 259-263: packages/core/src/material/PBRMaterial.ts#L259-L263
Added lines #L259 - L263 were not covered by tests


[warning] 270-271: packages/core/src/material/PBRMaterial.ts#L270-L271
Added lines #L270 - L271 were not covered by tests


[warning] 274-275: packages/core/src/material/PBRMaterial.ts#L274-L275
Added lines #L274 - L275 were not covered by tests


[warning] 281-282: packages/core/src/material/PBRMaterial.ts#L281-L282
Added lines #L281 - L282 were not covered by tests


[warning] 285-291: packages/core/src/material/PBRMaterial.ts#L285-L291
Added lines #L285 - L291 were not covered by tests


[warning] 297-298: packages/core/src/material/PBRMaterial.ts#L297-L298
Added lines #L297 - L298 were not covered by tests


[warning] 301-307: packages/core/src/material/PBRMaterial.ts#L301-L307
Added lines #L301 - L307 were not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 785455c and 06a4242.

📒 Files selected for processing (2)
  • packages/core/src/material/PBRMaterial.ts (4 hunks)
  • packages/loader/src/gltf/extensions/KHR_materials_sheen.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
packages/loader/src/gltf/extensions/KHR_materials_sheen.ts

[warning] 12-40: packages/loader/src/gltf/extensions/KHR_materials_sheen.ts#L12-L40
Added lines #L12 - L40 were not covered by tests

packages/core/src/material/PBRMaterial.ts

[warning] 236-237: packages/core/src/material/PBRMaterial.ts#L236-L237
Added lines #L236 - L237 were not covered by tests


[warning] 240-248: packages/core/src/material/PBRMaterial.ts#L240-L248
Added lines #L240 - L248 were not covered by tests


[warning] 255-256: packages/core/src/material/PBRMaterial.ts#L255-L256
Added lines #L255 - L256 were not covered by tests


[warning] 259-263: packages/core/src/material/PBRMaterial.ts#L259-L263
Added lines #L259 - L263 were not covered by tests


[warning] 270-271: packages/core/src/material/PBRMaterial.ts#L270-L271
Added lines #L270 - L271 were not covered by tests


[warning] 274-275: packages/core/src/material/PBRMaterial.ts#L274-L275
Added lines #L274 - L275 were not covered by tests


[warning] 281-282: packages/core/src/material/PBRMaterial.ts#L281-L282
Added lines #L281 - L282 were not covered by tests


[warning] 285-291: packages/core/src/material/PBRMaterial.ts#L285-L291
Added lines #L285 - L291 were not covered by tests


[warning] 297-298: packages/core/src/material/PBRMaterial.ts#L297-L298
Added lines #L297 - L298 were not covered by tests


[warning] 301-307: packages/core/src/material/PBRMaterial.ts#L301-L307
Added lines #L301 - L307 were not covered by tests

packages/core/src/material/PBRMaterial.ts Show resolved Hide resolved
packages/core/src/material/PBRMaterial.ts Show resolved Hide resolved
packages/core/src/material/PBRMaterial.ts Outdated Show resolved Hide resolved
packages/core/src/material/PBRMaterial.ts Outdated Show resolved Hide resolved
packages/core/src/material/PBRMaterial.ts Outdated Show resolved Hide resolved
packages/core/src/material/PBRMaterial.ts Show resolved Hide resolved
packages/core/src/material/PBRMaterial.ts Outdated Show resolved Hide resolved
packages/loader/src/gltf/extensions/KHR_materials_sheen.ts Outdated Show resolved Hide resolved
packages/core/src/material/PBRMaterial.ts Outdated Show resolved Hide resolved
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: 1

🧹 Outside diff range and nitpick comments (3)
packages/core/src/material/PBRMaterial.ts (3)

274-276: Improve documentation for sheenRoughnessTexture

The documentation is incomplete. Add details about the texture format and usage.

Update the documentation:

  /**
+  * Sheen roughness texture
   * @remarks Use alpha channel, and multiply 'sheenRoughness'.
+  * @remarks The alpha channel of this texture is multiplied with the sheenRoughness property to get the final roughness value.
   */

265-272: Add null checks in texture setters

The texture setters should handle null values explicitly for better type safety.

Apply this pattern to both texture setters:

  set sheenColorTexture(value: Texture2D) {
+   if (value === null || value === undefined) {
+     this.shaderData.setTexture(PBRMaterial._sheenTextureProp, null);
+     this.shaderData.disableMacro("MATERIAL_HAS_SHEEN_TEXTURE");
+     return;
+   }
    this.shaderData.setTexture(PBRMaterial._sheenTextureProp, value);
-   if (value) {
    this.shaderData.enableMacro("MATERIAL_HAS_SHEEN_TEXTURE");
-   } else {
-     this.shaderData.disableMacro("MATERIAL_HAS_SHEEN_TEXTURE");
-   }
  }

Also applies to: 281-288


303-324: Refine sheen color change handler implementation

The implementation could be improved for better maintainability and documentation.

Consider these improvements:

+ /** Threshold for enabling sheen effect based on color intensity */
+ private static readonly SHEEN_INTENSITY_THRESHOLD = 0;

  constructor(engine: Engine) {
    // ... existing code ...
    shaderData.setColor(PBRMaterial._sheenColorProp, new Color(0, 0, 0, 0));
    
    // @ts-ignore
    this.sheenColor._onValueChanged = () => {
      const { r, g, b, a } = this.sheenColor;
-     /**
-      * The sheen layer switch.
-      */
-     const enableSheen = (r + g + b) * a > 0;
+     const colorIntensity = (r + g + b) * a;
+     const enableSheen = colorIntensity > PBRMaterial.SHEEN_INTENSITY_THRESHOLD;
      
      if (enableSheen !== this._sheen) {
        this._sheen = enableSheen;
        if (enableSheen) {
          this.shaderData.enableMacro("MATERIAL_ENABLE_SHEEN");
        } else {
          this.shaderData.disableMacro("MATERIAL_ENABLE_SHEEN");
        }
      }
    };
  }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 06a4242 and d1dda0c.

📒 Files selected for processing (2)
  • packages/core/src/material/PBRMaterial.ts (4 hunks)
  • packages/loader/src/gltf/extensions/KHR_materials_sheen.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/loader/src/gltf/extensions/KHR_materials_sheen.ts
🔇 Additional comments (2)
packages/core/src/material/PBRMaterial.ts (2)

28-32: LGTM! Property declarations follow established patterns

The static shader properties and private state tracking are well-structured and consistent with the existing codebase.


254-256: Clamp sheenRoughness value to the range [0.0, 1.0]

The sheenRoughness property should be within the range of 0.0 to 1.0. Without clamping, assigning values outside this range could cause rendering issues.

packages/core/src/material/PBRMaterial.ts Outdated Show resolved Hide resolved
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: 1

🧹 Outside diff range and nitpick comments (2)
packages/core/src/material/PBRMaterial.ts (2)

306-308: Avoid using @ts-ignore; ensure proper typing for _onValueChanged

Using @ts-ignore bypasses TypeScript's type checking and may conceal underlying issues. It's better to properly define the type or adjust the code to resolve the type error.

Consider defining _onValueChanged in the Color class or extending the class to include this property.


317-323: Add unit tests for the sheen feature

The logic enabling and disabling the sheen effect based on sheenColor changes is not covered by unit tests. Adding tests will ensure that the sheen effect behaves as expected when sheenColor is modified.

Would you like assistance in creating unit tests for the sheen functionality?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 317-323: packages/core/src/material/PBRMaterial.ts#L317-L323
Added lines #L317 - L323 were not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d1dda0c and 5604310.

📒 Files selected for processing (2)
  • packages/core/src/material/PBRMaterial.ts (5 hunks)
  • packages/loader/src/gltf/extensions/KHR_materials_sheen.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
packages/core/src/material/PBRMaterial.ts

[warning] 240-244: packages/core/src/material/PBRMaterial.ts#L240-L244
Added lines #L240 - L244 were not covered by tests


[warning] 251-252: packages/core/src/material/PBRMaterial.ts#L251-L252
Added lines #L251 - L252 were not covered by tests


[warning] 255-256: packages/core/src/material/PBRMaterial.ts#L255-L256
Added lines #L255 - L256 were not covered by tests


[warning] 262-263: packages/core/src/material/PBRMaterial.ts#L262-L263
Added lines #L262 - L263 were not covered by tests


[warning] 266-272: packages/core/src/material/PBRMaterial.ts#L266-L272
Added lines #L266 - L272 were not covered by tests


[warning] 279-280: packages/core/src/material/PBRMaterial.ts#L279-L280
Added lines #L279 - L280 were not covered by tests


[warning] 283-289: packages/core/src/material/PBRMaterial.ts#L283-L289
Added lines #L283 - L289 were not covered by tests


[warning] 317-323: packages/core/src/material/PBRMaterial.ts#L317-L323
Added lines #L317 - L323 were not covered by tests

packages/loader/src/gltf/extensions/KHR_materials_sheen.ts

[warning] 12-40: packages/loader/src/gltf/extensions/KHR_materials_sheen.ts#L12-L40
Added lines #L12 - L40 were not covered by tests

🔇 Additional comments (5)
packages/core/src/material/PBRMaterial.ts (2)

239-244: Implement validation for sheenColor values

The sheenColor setter does not validate the input color values. To prevent unexpected behavior, consider clamping each color component to ensure they are within the valid range of [0, 1].

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 240-244: packages/core/src/material/PBRMaterial.ts#L240-L244
Added lines #L240 - L244 were not covered by tests


255-256: Clamp sheenRoughness value to the range [0.0, 1.0]

The sheenRoughness property should be within the range of 0.0 to 1.0. Adding validation will prevent invalid values from causing rendering issues.

Apply this diff to clamp the sheenRoughness value:

      set sheenRoughness(value: number) {
+       value = Math.max(0, Math.min(1, value));
        this.shaderData.setFloat(PBRMaterial._sheenRoughnessProp, value);
      }
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 255-256: packages/core/src/material/PBRMaterial.ts#L255-L256
Added lines #L255 - L256 were not covered by tests

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

19-19: Set alpha channel when assigning sheenColor

The alpha channel represents sheen intensity. It's currently set to undefined, which may cause issues. Consider setting it to 1.0 or an appropriate value.


23-23: Check if sheenRoughnessFactor is defined before assignment

Assigning undefined to material.sheenRoughness could lead to unintended behavior. Add a check to ensure it is defined.

Apply this diff:

+       if (sheenRoughnessFactor !== undefined) {
          material.sheenRoughness = sheenRoughnessFactor;
+       }

28-30: Ensure asynchronous texture loading does not cause race conditions

When setting sheenColorTexture, the asynchronous loading might result in textures being applied after material initialization. Consider handling potential race conditions.

Run the following script to check for other instances where asynchronous texture loading is handled:

✅ Verification successful

Let me analyze the context further to understand how texture loading is handled in the codebase.


Let me check one more aspect to see how the material loading is coordinated.


Race conditions in texture loading are properly handled

The codebase shows a well-structured approach to texture loading coordination:

  1. The MaterialLoader class uses Promise.all(texturePromises) to ensure all textures are loaded before completing material initialization.
  2. Texture assignments are handled through a uniform system that coordinates texture uploads during the render pipeline.
  3. The asynchronous texture loading pattern is consistently used across all material types (base, sheen, clearcoat, etc.), indicating a standardized approach.

The current implementation in KHR_materials_sheen.ts follows the established pattern and doesn't introduce race conditions, as texture assignments are part of the material's initialization promise chain.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for asynchronous texture assignments in material parsers.

# Find occurrences of '.then(' after 'context.get<Texture2D>'
rg 'context\.get<Texture2D>.*\.then\(' -A 5

Length of output: 9057


Script:

#!/bin/bash
# Check if there's a Promise.all or similar mechanism to handle multiple texture loads
rg -B 5 -A 5 'Promise\.all.*texture' 

# Check the GLTFParser class implementation for texture loading coordination
ast-grep --pattern 'class GLTFParser {
  $$$
}'

Length of output: 631


Script:

#!/bin/bash
# Check the material parser implementation for texture loading coordination
rg -B 5 -A 10 'class GLTFMaterialParser' 

# Check if there's a texture loading queue or coordination mechanism
rg -B 3 -A 3 'texturePromises|loadTexture'

Length of output: 10501

*/
get sheenRoughness(): number {
return this.shaderData.getFloat(PBRMaterial._sheenRoughnessProp);
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe should add value = Math.max(0, Math.min(1, value)); if you want clamp the range like comments

@GuoLei1990 GuoLei1990 merged commit afc9087 into galacean:dev/1.4 Dec 12, 2024
7 of 9 checks passed
@GuoLei1990 GuoLei1990 mentioned this pull request Dec 12, 2024
3 tasks
@coderabbitai coderabbitai bot mentioned this pull request Dec 17, 2024
@GuoLei1990 GuoLei1990 added the enhancement New feature or request label Dec 17, 2024
@coderabbitai coderabbitai bot mentioned this pull request Dec 18, 2024
3 tasks
@hhhhkrx hhhhkrx linked an issue Dec 19, 2024 that may be closed by this pull request
@coderabbitai coderabbitai bot mentioned this pull request Dec 25, 2024
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request glTF material Rendering Rendering related functions shader Shader related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sheen
3 participants