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

Fix material clone name #2576

Merged
merged 4 commits into from
Mar 3, 2025

Conversation

GuoLei1990
Copy link
Member

@GuoLei1990 GuoLei1990 commented Mar 3, 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

    • Updated default naming for new entities for a cleaner and more intuitive label.
    • Improved naming for duplicated objects, which now include a distinguishing suffix (e.g. "(Clone)" or "(Instance)").
  • Bug Fixes

    • Resolved inconsistencies in instance naming to ensure accurate and predictable displays.
  • Tests

    • Enhanced test coverage to validate the improved naming behavior for objects and materials.

@GuoLei1990 GuoLei1990 added the bug Something isn't working label Mar 3, 2025
@GuoLei1990 GuoLei1990 linked an issue Mar 3, 2025 that may be closed by this pull request
Copy link

codecov bot commented Mar 3, 2025

Codecov Report

Attention: Patch coverage is 76.47059% with 4 lines in your changes missing coverage. Please review.

Project coverage is 68.95%. Comparing base (2cd5b85) to head (95cfd49).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
packages/core/src/material/BaseMaterial.ts 0.00% 1 Missing ⚠️
packages/core/src/particle/ParticleMaterial.ts 0.00% 1 Missing ⚠️
packages/core/src/sky/SkyBoxMaterial.ts 0.00% 1 Missing ⚠️
packages/core/src/sky/SkyProceduralMaterial.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2576      +/-   ##
==========================================
- Coverage   68.95%   68.95%   -0.01%     
==========================================
  Files         961      961              
  Lines      100298   100304       +6     
  Branches     8667     8665       -2     
==========================================
+ Hits        69163    69165       +2     
- Misses      30875    30879       +4     
  Partials      260      260              
Flag Coverage Δ
unittests 68.95% <76.47%> (-0.01%) ⬇️

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 bot commented Mar 3, 2025

Walkthrough

The pull request updates several core components by modifying naming conventions and cloning logic. In the Entity class, the default value for the name property is changed from "New Entity" to "Entity". The Renderer now correctly assigns the derived instance material name and its position in the materials array. Multiple material classes have their cloning methods updated to use a new _cloneToAndModifyName method, with the Material class additionally assigning its name based on the shader and appending "(Clone)" during cloning. Test cases are updated accordingly to validate these changes.

Changes

File(s) Summary
packages/core/src/Entity.ts Modified default name in the Entity class constructor from "New Entity" to "Entity".
packages/core/src/Renderer.ts Updated logic in _createInstanceMaterial to derive the instance material name correctly from the original material and assign it in the _materials array.
packages/core/src/material/BaseMaterial.ts,
packages/core/src/material/BlinnPhongMaterial.ts,
packages/core/src/material/PBRMaterial.ts,
packages/core/src/material/PBRSpecularMaterial.ts,
packages/core/src/material/UnlitMaterial.ts,
packages/core/src/particle/ParticleMaterial.ts,
packages/core/src/sky/SkyBoxMaterial.ts,
packages/core/src/sky/SkyProceduralMaterial.ts
Updated the clone methods to replace cloneTo with _cloneToAndModifyName, incorporating logic to modify the name of the cloned instance.
packages/core/src/material/Material.ts Added a new property assignment (this.name = shader.name;) in the constructor and updated the clone method to call _cloneToAndModifyName, which appends "(Clone)" to the material's name. Also introduced the new protected method _cloneToAndModifyName to encapsulate this logic.
tests/src/core/MeshRenderer.test.ts Adjusted expected names for materials (e.g., from "undefined(Instance)" to "unlit(Instance)" and "pbr(Instance)") and added a console.log to aid debugging of material names.
tests/src/core/material/Material.test.ts Added a new test case for the clone method verifying that cloning produces a material with the name formatted as "blinn-phong(Clone)".

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Material
    Client->>Material: clone()
    Material->>Material: _cloneToAndModifyName(target)
    Note right of Material: Internally calls cloneTo() and appends "(Clone)" to the original name.
    Material-->>Client: Returns cloned Material with modified name
Loading

Possibly related PRs

Suggested labels

material, enhancement

Suggested reviewers

  • zhuxudong
  • Sway007

Poem

I'm a little bunny, hopping through the code,
Tweaking names and clones to lighten every load.
From "New Entity" to a simpler "Entity" I hop,
While materials gain a twist—"(Clone)" on top!
With whiskers twitching at each change in sight,
I celebrate with CodeRabbit, coding day and night!
🐇💻


📜 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 fd9fa2d and 95cfd49.

📒 Files selected for processing (1)
  • tests/src/core/MeshRenderer.test.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/src/core/MeshRenderer.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: codecov
  • GitHub Check: e2e (22.x)

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.
  • @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: 1

🔭 Outside diff range comments (1)
packages/core/src/Renderer.ts (1)

487-494: ⚠️ Potential issue

Important fix for material instance naming

The change correctly sets the instance material name to be based on the original material's name rather than potentially using undefined or incorrect naming. This ensures consistent naming of instance materials in the engine.

Before this fix, instance materials might have had names like "undefined(Instance)" if the original material's name wasn't properly set, leading to confusing debugging experiences.

🧹 Nitpick comments (2)
tests/src/core/MeshRenderer.test.ts (1)

199-200: Remove debug log statement

The console.log statement should be removed before merging as it's likely just for debugging purposes during development.

-console.log(material.name);
packages/core/src/material/Material.ts (1)

116-119: Consider handling multiple clone operations more elegantly.

While the current implementation works well for single cloning operations, cloning an already-cloned material will result in names like "Material(Clone)(Clone)". This could become unwieldy with multiple levels of cloning.

  protected _cloneToAndModifyName(target: Material): void {
    this.cloneTo(target);
-   target.name = this.name + "(Clone)";
+   // Avoid appending multiple "(Clone)" suffixes
+   target.name = this.name.endsWith("(Clone)") 
+     ? this.name 
+     : this.name + "(Clone)";
  }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2cd5b85 and fd9fa2d.

📒 Files selected for processing (13)
  • packages/core/src/Entity.ts (1 hunks)
  • packages/core/src/Renderer.ts (1 hunks)
  • packages/core/src/material/BaseMaterial.ts (1 hunks)
  • packages/core/src/material/BlinnPhongMaterial.ts (1 hunks)
  • packages/core/src/material/Material.ts (2 hunks)
  • packages/core/src/material/PBRMaterial.ts (1 hunks)
  • packages/core/src/material/PBRSpecularMaterial.ts (1 hunks)
  • packages/core/src/material/UnlitMaterial.ts (1 hunks)
  • packages/core/src/particle/ParticleMaterial.ts (1 hunks)
  • packages/core/src/sky/SkyBoxMaterial.ts (1 hunks)
  • packages/core/src/sky/SkyProceduralMaterial.ts (1 hunks)
  • tests/src/core/MeshRenderer.test.ts (2 hunks)
  • tests/src/core/material/Material.test.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
packages/core/src/sky/SkyBoxMaterial.ts

[warning] 99-99: packages/core/src/sky/SkyBoxMaterial.ts#L99
Added line #L99 was not covered by tests

packages/core/src/particle/ParticleMaterial.ts

[warning] 59-59: packages/core/src/particle/ParticleMaterial.ts#L59
Added line #L59 was not covered by tests

packages/core/src/sky/SkyProceduralMaterial.ts

[warning] 152-152: packages/core/src/sky/SkyProceduralMaterial.ts#L152
Added line #L152 was not covered by tests

packages/core/src/material/BaseMaterial.ts

[warning] 223-223: packages/core/src/material/BaseMaterial.ts#L223
Added line #L223 was not covered by tests

🔇 Additional comments (14)
tests/src/core/material/Material.test.ts (1)

95-101: Test coverage added for material clone naming. Great!

This test ensures that material clones follow the expected naming convention, where "(Clone)" is appended to the original material name. This helps validate the changes made to the cloning mechanism across material types.

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

487-487: Consistent cloning mechanism adopted.

Changed from using cloneTo to _cloneToAndModifyName which ensures that cloned materials have a standardized naming convention that includes "(Clone)" suffix, making it easier to identify cloned materials.

packages/core/src/Entity.ts (1)

254-254: Simplified default entity naming.

The default name for entities has been simplified from "New Entity" to just "Entity", which is more concise and aligns better with common naming conventions.

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

76-76: Consistent cloning mechanism applied.

Changed from cloneTo to _cloneToAndModifyName to maintain consistency with the clone naming convention across all material types. This ensures that cloned UnlitMaterial instances will have the "(Clone)" suffix in their names.

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

76-79: Good improvement on material clone naming!

The change from cloneTo to _cloneToAndModifyName is a positive improvement that ensures cloned materials have a meaningful name based on the original material. This promotes better debug experiences and makes it easier to identify cloned materials in the scene hierarchy.

packages/core/src/particle/ParticleMaterial.ts (1)

57-60:

❓ Verification inconclusive

Good improvement on material clone naming!

The change from cloneTo to _cloneToAndModifyName ensures cloned particle materials have a meaningful name based on the original material, which is consistent with the naming changes in other material types.

However, I notice that this line is flagged as not covered by tests according to the static analysis:


🏁 Script executed:

#!/bin/bash
# Verify if there are tests for the ParticleMaterial clone method

# Search for any tests targeting ParticleMaterial clone functionality
rg -A 5 "ParticleMaterial.*clone" --glob "tests/**/*.test.ts"

Length of output: 63


Attention: Verify Test Coverage for ParticleMaterial.clone()

Good improvement on the material clone naming! Changing from cloneTo to _cloneToAndModifyName ensures that cloned particle materials automatically receive a meaningful name based on the original material, keeping the behavior consistent with other material types.

However, static analysis flagged that the clone method in packages/core/src/particle/ParticleMaterial.ts (lines 57–60) is not covered by tests. The recent search did not return any test cases for this functionality. Please manually verify whether tests for the clone functionality exist or need to be added. If missing, it would be beneficial to include a test case to safeguard against regressions.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 59-59: packages/core/src/particle/ParticleMaterial.ts#L59
Added line #L59 was not covered by tests

tests/src/core/MeshRenderer.test.ts (2)

197-208: Test update correctly validates material instance naming

The test expectations have been updated to match the new material instance naming convention, which is good. Now the test properly expects "unlit(Instance)" and "pbr(Instance)" instead of potentially incorrect names like "undefined(Instance)".


223-226: Test update correctly validates material instance naming in arrays

The updated expectations for instance material names in arrays correctly validate the new naming convention. This ensures the consistency of naming across all instance materials, whether accessed individually or as part of a collection.

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

222-224:

✅ Verification successful

Consistent cloning approach implemented

The switch from cloneTo to _cloneToAndModifyName ensures consistent handling of material names during cloning, which addresses the primary goal of this PR to fix material clone naming.


🏁 Script executed:

#!/bin/bash
# Check the implementation of the _cloneToAndModifyName method in the Material class
rg -A 6 "_cloneToAndModifyName" --type ts packages/core/src/material/Material.ts

Length of output: 374


Consistent Cloning Implementation Verified
The changes in packages/core/src/material/BaseMaterial.ts correctly instantiate a new BaseMaterial and use _cloneToAndModifyName, which calls cloneTo and appends "(Clone)" to the material’s name. This confirms that material clone naming is now handled as intended.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 223-223: packages/core/src/material/BaseMaterial.ts#L223
Added line #L223 was not covered by tests

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

179-181: Consistent naming convention for material clones

The implementation now uses the centralized _cloneToAndModifyName method which ensures consistent naming of cloned materials across different material types.

packages/core/src/sky/SkyProceduralMaterial.ts (1)

151-153: Cloning consistency improved with shared method

Using _cloneToAndModifyName aligns this material's cloning behavior with the rest of the material system, ensuring proper naming of cloned instances.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 152-152: packages/core/src/sky/SkyProceduralMaterial.ts#L152
Added line #L152 was not covered by tests

packages/core/src/material/Material.ts (3)

87-87: Improved material naming with shader reference.

Initializing the material name based on the shader name provides better default naming semantics and consistency throughout the application.


93-96: Better naming convention for cloned materials.

Using the new method to create a unique name for cloned materials improves clarity when working with cloned instances.


87-87:

✅ Verification successful

Test the correct cloning behavior across different material types.

Since this change affects how materials are named during cloning, ensure that this behavior is consistent across all material types, especially if some materials might have custom naming logic.

Also applies to: 116-119


🏁 Script executed:

#!/bin/bash
# Check all material classes to verify consistent implementation of the _cloneToAndModifyName method

echo "Checking for material classes with clone methods:"
grep -r "clone()" --include="*.ts" packages/core/src/material/

echo "\nChecking for _cloneToAndModifyName implementations:"
grep -r "_cloneToAndModifyName" --include="*.ts" packages/core/src/material/

echo "\nChecking for material test cases that verify clone naming:"
grep -r "clone.*name.*Clone" --include="*.test.ts" tests/

Length of output: 1725


Ensure Consistent Clone Naming Across All Material Types

The tests confirm that every material class calls _cloneToAndModifyName in their clone() methods and that the naming convention (e.g., "blinn-phong(Clone)") is enforced through dedicated tests. Please make sure that the changes in Material.ts (lines 87 and 116–119) continue to work seamlessly with custom clone logic in other material types.

Comment on lines 98 to 100
const dest = new SkyBoxMaterial(this._engine);
this.cloneTo(dest);
this._cloneToAndModifyName(dest);
return dest;
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Add test coverage for cloning functionality

The new cloning approach ensures consistent naming, but according to static analysis, this change lacks test coverage.


🏁 Script executed:

#!/bin/bash
# Check if there are any material cloning tests
rg -A 3 "clone\(\)" --type ts tests/src/core/material

Length of output: 2728


Action: Add dedicated test for SkyBoxMaterial cloning functionality

The new cloning approach in packages/core/src/sky/SkyBoxMaterial.ts (lines 98–100) appears to be working as intended. However, while tests exist for other material clone methods (e.g., in tests/src/core/material/Material.test.ts and similar files), there isn’t any direct test covering the cloning behavior of SkyBoxMaterial. Please add a test specifically for this class to ensure that the helper _cloneToAndModifyName correctly updates the cloned instance (such as validating its naming convention and any other critical properties).

  • Consider placing the new test in a suitable location, for example in a new file under tests/src/core/sky or in an existing material tests file if appropriate.
  • Verify that the cloned instance’s properties (especially the name) conform to the consistent naming convention established by the cloning functionality.
🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 99-99: packages/core/src/sky/SkyBoxMaterial.ts#L99
Added line #L99 was not covered by tests

@GuoLei1990 GuoLei1990 merged commit 6185bfb into galacean:main Mar 3, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The name of the cloned Material is not copied.
3 participants