8000 metallic roughness workflow support specular by hhhhkrx · Pull Request #2756 · galacean/engine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

metallic roughness workflow support specular #2756

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

Open
wants to merge 6 commits into
base: dev/1.6
Choose a base branch
from

Conversation

hhhhkrx
Copy link
Contributor
@hhhhkrx hhhhkrx commented Jun 27, 2025

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

    • Added support for specular reflection properties in materials, including controls for specular strength, color, and related textures.
    • Enhanced shader editor with new specular parameters for improved material realism.
    • Introduced support for the "KHR_materials_specular" GLTF extension to correctly parse and apply specular properties during material loading.
  • Chores

    • Removed legacy support for the "KHR_materials_pbrSpecularGlossiness" extension and related tests, streamlining GLTF extension handling.
    • Removed the deprecated PBRSpecularMaterial class and its exports.

@hhhhkrx hhhhkrx added this to the 1.6 milestone Jun 27, 2025
@hhhhkrx hhhhkrx self-assigned this Jun 27, 2025
@hhhhkrx hhhhkrx added the rendering Rendering related functions label Jun 27, 2025
@github-project-automation github-project-automation bot moved this to In progress in Shader Jun 27, 2025
@github-project-automation github-project-automation bot moved this to In progress in PBR material Jun 27, 2025
Copy link
coderabbitai bot commented Jun 27, 2025

"""

Walkthrough

This update removes support for the "KHR_materials_pbrSpecularGlossiness" GLTF extension and its related material class, schema, and loader logic. It introduces new support for the "KHR_materials_specular" extension, updating the PBR material and shader system to handle specular reflection properties, textures, and color. Test cases and texture parsing logic are adjusted accordingly.

Changes

File(s) Summary
packages/core/src/material/PBRMaterial.ts Extended PBRMaterial with specular reflection properties, textures, color, and dynamic shader macro management.
packages/core/src/material/PBRSpecularMaterial.ts Removed the PBRSpecularMaterial class and all its properties and methods.
packages/loader/src/gltf/extensions/GLTFExtensionSchema.ts Removed IKHRMaterialsPbrSpecularGlossiness, updated IKHRMaterialsSpecular, and modified GLTFExtensionSchema union.
packages/loader/src/gltf/extensions/KHR_materials_pbrSpecularGlossiness.ts Removed parser for KHR_materials_pbrSpecularGlossiness extension.
packages/loader/src/gltf/extensions/KHR_materials_specular.ts Added parser for KHR_materials_specular extension, handling specular factors and textures.
packages/loader/src/gltf/extensions/index.ts Swapped import: removed KHR_materials_pbrSpecularGlossiness, added KHR_materials_specular.
packages/loader/src/gltf/parser/GLTFTextureParser.ts Removed checks for specularGlossiness textures in _isSRGBColorSpace.
packages/shader-shaderlab/src/shaders/PBR.gs Added specular section to shader properties; removed IS_METALLIC_WORKFLOW define from Forward Pass.
tests/src/loader/GLTFLoader.test.ts Removed all references and tests for KHR_materials_pbrSpecularGlossiness from GLTF loader tests.
packages/core/src/material/index.ts Removed export of PBRSpecularMaterial.
packages/loader/src/gltf/parser/GLTFMaterialParser.ts Removed import and usage of PBRSpecularMaterial; narrowed material type in _parseStandardProperty method.
tests/src/core/material/PBRSpecularMaterial.test.ts Removed tests for PBRSpecularMaterial class.

Sequence Diagram(s)

sequenceDiagram
    participant GLTFLoader
    participant KHR_materials_specular
    participant PBRMaterial
    participant TextureParser

    GLTFLoader->>KHR_materials_specular: Parse KHR_materials_specular extension
    KHR_materials_specular->>PBRMaterial: Set specularFactor, specularColorFactor
    KHR_materials_specular->>TextureParser: Get specularTexture
    TextureParser-->>KHR_materials_specular: Return Texture2D
    KHR_materials_specular->>PBRMaterial: Set specularTexture
    KHR_materials_specular->>TextureParser: Get specularColorTexture
    TextureParser-->>KHR_materials_specular: Return Texture2D
    KHR_materials_specular->>PBRMaterial: Set specularColorTexture
Loading

Suggested labels

enhancement, shader, glTF

Suggested reviewers

  • zhuxudong
  • GuoLei1990

Poem

In the warren of code, a shimmer anew,
Specular bunnies hop with a radiant hue.
Old glossiness gone, new reflections appear,
With textures and colors, the render is clear.
From shader to loader, the sparkle will spread—
A hop, a shine, and forward we tread!
🐇✨
"""

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

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
@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: 3

🔭 Outside diff range comments (2)
tests/src/loader/GLTFLoader.test.ts (2)

7-7: Critical: Remove unused PBRSpecularMaterial import

The test file still imports PBRSpecularMaterial which was removed according to the PR changes. This will cause compilation errors.

-  PBRSpecularMaterial,

376-398: Critical: Update test assertions to reflect material class changes

The test code still references PBRSpecularMaterial and its properties (specularColor, glossiness, specularGlossinessTexture) which were removed. These assertions will fail since the class no longer exists.

The test needs to be updated to reflect the new specular workflow integrated into PBRMaterial. Consider updating the test data and assertions to use the new specular extension properties.

🧹 Nitpick comments (1)
packages/loader/src/gltf/extensions/KHR_materials_specular.ts (1)

16-16: Clarify specular color alpha component handling

Passing undefined as the alpha component seems intentional but unclear. Consider using a default value or adding a comment to explain this behavior.

-      material.specularColor.set(specularColorFactor[0], specularColorFactor[1], specularColorFactor[2], undefined);
+      material.specularColor.set(specularColorFactor[0], specularColorFactor[1], specularColorFactor[2], 1.0);

Or add a comment explaining why undefined is used for the alpha component.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 95135f4 and 2d6ec37.

⛔ Files ignored due to path filters (4)
  • packages/shader-shaderlab/src/shaders/shadingPBR/BSDF.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/FragmentPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/LightIndirectPBR.glsl is excluded by !**/*.glsl
  • packages/shader-shaderlab/src/shaders/shadingPBR/ReflectionLobe.glsl is excluded by !**/*.glsl
📒 Files selected for processing (9)
  • packages/core/src/material/PBRMaterial.ts (5 hunks)
  • packages/core/src/material/PBRSpecularMaterial.ts (0 hunks)
  • packages/loader/src/gltf/extensions/GLTFExtensionSchema.ts (1 hunks)
  • packages/loader/src/gltf/extensions/KHR_materials_pbrSpecularGlossiness.ts (0 hunks)
  • packages/loader/src/gltf/extensions/KHR_materials_specular.ts (1 hunks)
  • packages/loader/src/gltf/extensions/index.ts (1 hunks)
  • packages/loader/src/gltf/parser/GLTFTextureParser.ts (0 hunks)
  • packages/shader-shaderlab/src/shaders/PBR.gs (1 hunks)
  • tests/src/loader/GLTFLoader.test.ts (1 hunks)
💤 Files with no reviewable changes (3)
  • packages/loader/src/gltf/parser/GLTFTextureParser.ts
  • packages/core/src/material/PBRSpecularMaterial.ts
  • packages/loader/src/gltf/extensions/KHR_materials_pbrSpecularGlossiness.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/loader/src/gltf/extensions/GLTFExtensionSchema.ts (2)
packages/loader/src/gltf/GLTFSchema.ts (1)
  • ITextureInfo (772-781)
packages/loader/src/gltf/index.ts (1)
  • ITextureInfo (9-9)
🔇 Additional comments (12)
packages/loader/src/gltf/extensions/index.ts (1)

18-18: LGTM - Clean extension replacement

The import statement correctly replaces the deprecated KHR_materials_pbrSpecularGlossiness extension with the new KHR_materials_specular extension.

tests/src/loader/GLTFLoader.test.ts (1)

119-119: LGTM - Correctly removed deprecated extension

The removal of KHR_materials_pbrSpecularGlossiness from the extensionsRequired array aligns with the deprecation of this extension.

packages/shader-shaderlab/src/shaders/PBR.gs (1)

15-21: LGTM - Well-structured specular shader properties

The new "Specular" header section correctly defines the necessary shader properties for the specular workflow:

  • material_Specular: Specular factor with appropriate range
  • material_SpecularColor: Specular color property
  • material_SpecularTexture and material_SpecularColorTexture: Texture properties

The naming conventions and property definitions are consistent with existing shader properties.

packages/loader/src/gltf/extensions/GLTFExtensionSchema.ts (1)

62-66: LGTM - Correct schema interface definition

The IKHRMaterialsSpecular interface properly defines all specular-related properties as optional, which aligns with GLTF extension conventions. The property types (number, number[], ITextureInfo) are appropriate for their respective purposes.

packages/loader/src/gltf/extensions/KHR_materials_specular.ts (1)

19-43: LGTM - Robust texture loading implementation

The texture loading logic correctly:

  • Validates texture transforms before loading
  • Uses async context.get() for texture retrieval
  • Implements proper error handling with logging
  • Follows the established pattern used in other extension parsers

The implementation is consistent and handles both specular and specular color textures appropriately.

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

44-52: LGTM!

The static property declarations for specular workflow follow the established pattern and are properly typed.


57-58: LGTM!

The private fields for tracking specular state are properly declared.


68-71: LGTM!

Correctly updates reflectance when IOR changes, as reflectance calculation depends on the index of refraction.


490-536: LGTM!

The specular color and texture properties are correctly implemented, following the established patterns for color and texture properties with proper shader macro management.


559-563: LGTM!

The specular properties are initialized with appropriate default values that match typical PBR material behavior.


568-569: LGTM!

The specular color change handler is correctly implemented, following the same pattern as the sheen color handler.

Also applies to: 600-611


613-623: Fix the reflectance calculation logic.

The current implementation has several issues:

  1. Line 615 treats specular as a boolean when it's a number
  2. The logic appears inverted - reflectance should be calculated from IOR when specular is NOT being used explicitly
  3. Line 616 gets reflectance but doesn't use it

Apply this diff to fix the logic:

  private _updateReflectance(): void {
-    let reflectance: number;
-    if (this.specular) {
-      reflectance = this.shaderData.getFloat(PBRMaterial._reflectanceProp);
+    if (this._specularEnabled) {
+      // When specular is enabled, reflectance is controlled by specular value
+      // The shader will handle the calculation
+      return;
    } else {
+      // When specular is disabled, calculate reflectance from IOR
      const ior = this.ior;
      const f0 = (ior - 1.0) / (ior + 1.0);
-      reflectance = Math.sqrt((f0 * f0) / 0.16);
+      const reflectance = Math.sqrt((f0 * f0) / 0.16);
+      this.shaderData.setFloat(PBRMaterial._reflectanceProp, reflectance);
    }
-    this.shaderData.setFloat(PBRMaterial._reflectanceProp, reflectance);
  }

Likely an incorrect or invalid review comment.

override additiveParse(context: GLTFParserContext, material: PBRMaterial, schema: IKHRMaterialsSpecular): void {
const { specularFactor, specularTexture, specularColorFactor, specularColorTexture } = schema;

material.specular = specularFactor;
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add null check for specularFactor

The specularFactor could be undefined since it's optional in the schema. Add a null check before assignment.

-    material.specular = specularFactor;
+    if (specularFactor !== undefined) {
+      material.specular = specularFactor;
+    }
📝 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
material.specular = specularFactor;
if (specularFactor !== undefined) {
material.specular = specularFactor;
}
🤖 Prompt for AI Agents
In packages/loader/src/gltf/extensions/KHR_materials_specular.ts at line 13, the
assignment of specularFactor to material.specular lacks a null check. Add a
condition to verify that specularFactor is not n
8000
ull or undefined before
assigning it to material.specular to prevent potential runtime errors.

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

🧹 Nitpick comments (1)
packages/loader/src/gltf/parser/GLTFMaterialParser.ts (1)

89-89: Consider using instanceof for type checking instead of constructor comparison.

While the logic is correct for checking PBR material types, using instanceof would be more idiomatic and type-safe in TypeScript.

-    if (material.constructor === PBRMaterial) {
+    if (material instanceof PBRMaterial) {

Note: The same pattern is used on line 72, which should also be updated for consistency.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 3494f6a and 89990c2.

📒 Files selected for processing (2)
  • packages/loader/src/gltf/parser/GLTFMaterialParser.ts (2 hunks)
  • tests/src/core/material/PBRSpecularMaterial.test.ts (0 hunks)
💤 Files with no reviewable changes (1)
  • tests/src/core/material/PBRSpecularMaterial.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: e2e (22.x)
  • GitHub Check: codecov
🔇 Additional comments (2)
packages/loader/src/gltf/parser/GLTFMaterialParser.ts (2)

34-34: Parameter type narrowing looks correct.

The removal of PBRSpecularMaterial from the union type aligns with the deprecation of the specular-glossiness workflow. The type is now properly constrained to the supported material types.


72-86: Verify PBRMaterial property existence and types

I couldn’t locate a local definition of PBRMaterial in this package (it’s likely imported from the engine core), so please confirm that the external class exposes the following with the correct types:

  • metallic: number
  • roughness: number
  • roughnessMetallicTexture: Texture2D | null

File to check:
• packages/loader/src/gltf/parser/GLTFMaterialParser.ts (lines 72–86)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
material rendering Rendering related functions
Projects
Status: In progress
Status: In progress
Development

Successfully merging this pull request may close these issues.

1 participant
0