Skip to content

feat(graphic): support local and file-based textures in material setup#435

Merged
Miou-zora merged 7 commits into
mainfrom
add-a-way-to-use-custom-materials
Jan 24, 2026
Merged

feat(graphic): support local and file-based textures in material setup#435
Miou-zora merged 7 commits into
mainfrom
add-a-way-to-use-custom-materials

Conversation

@Miou-zora
Copy link
Copy Markdown
Contributor

@Miou-zora Miou-zora commented Jan 24, 2026

This pull request enhances the material and texture handling logic in both the example usage and the GPU material creation system. The changes improve support for custom materials created from files, locally generated textures, and materials without textures. The most important changes are grouped below:

Material and Texture Creation Logic Improvements

  • The example in main.cpp now demonstrates three types of material usage: materials loaded from file textures, materials created from locally generated textures, and materials without textures. It adds logic to generate a local texture programmatically and register it with the texture manager, and updates entity transforms for clarity.

GPU Material System Robustness

  • The GPU material creation system in OnMaterialCreation.cpp now first checks if the texture file exists and loads it if available. If the file does not exist but the texture is already present in the texture container (e.g., from a locally generated texture), it uses the existing texture instead of logging a warning or failing. This allows for more flexible material definitions and avoids unnecessary warnings.

Summary by CodeRabbit

  • New Features

    • Added a local texture workflow with inline pixel data and dynamic texture registration; example now shows file-textured, locally-textured, and untextured objects with adjusted placement.
  • Bug Fixes

    • Improved texture resolution to reuse already-loaded textures when files are missing and reduced spurious warnings.
  • Breaking Changes

    • Material uniform layout updated (emission → ambient), affecting material appearance and shader uniform bindings.

✏️ Tip: You can customize this high-level summary in your review settings.

Miou-zora and others added 2 commits January 24, 2026 15:52
Updated material creation logic to allow using textures from local resources as well as files. The example now demonstrates creating materials from a file, a local texture, and without a texture. OnMaterialCreation now checks for both file existence and local texture container before assigning the texture.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 24, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Introduces local texture registration in the example, reorders cube setup and adds an untextured cube, changes G-buffer material uniform from emission to ambient, and updates GPU material creation to prefer loading a texture file, otherwise reuse a texture from the TextureContainer or emit a warning if missing.

Changes

Cohort / File(s) Summary
Example material workflow
examples/graphic_material_usage/src/main.cpp
Adds creation/registration of a local texture (inline pixel data) and materialFromLocalTexture for cube2, moves cube2 to (0.0f,2.0f,0.0), reintroduces an untextured cube (cube3), retains file-based texture for cube1.
Texture loading logic
src/plugin/default-pipeline/src/system/GPUComponentManagement/OnMaterialCreation.cpp
Changes ambient texture lookup: if file exists → load & register and assign textureId; else if textureId exists in TextureContainer → reuse and assign; else if ambient name non-empty → emit warning. Sampler and bind-group setup unchanged.
G-buffer uniform/layout
src/plugin/default-pipeline/src/resource/pass/GBuffer.hpp
Renames shader material field from emission to ambient and updates fragment output to multiply sampled texture by material.ambient (uniform layout change).

Sequence Diagram(s)

sequenceDiagram
    participant MaterialCreator as Material Creation
    participant FS as File System
    participant TC as TextureContainer
    participant GPU as GPU/BindGroup

    MaterialCreator->>FS: check ambient texture file exists?
    alt file exists
        FS->>TC: load texture & register (new textureId)
        TC-->>MaterialCreator: textureId
        MaterialCreator->>GPU: set GPUMaterial.texture = textureId
    else file missing
        MaterialCreator->>TC: query textureId by name
        alt texture found in TC
            TC-->>MaterialCreator: existing textureId
            MaterialCreator->>GPU: set GPUMaterial.texture = textureId
        else not found
            MaterialCreator->>MaterialCreator: if ambient name non-empty → warn "texture not found"
        end
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • ripel2

Poem

🐰 I stitched a tiny pixel bright,
I registered it in morning light.
Cubes now wear and cubes go bare,
Old textures found when files aren't there.
Hooray — a hop, a render, a spare delight! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding support for both local and file-based textures in material setup, which is demonstrated in both the example and GPU material creation logic.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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

Comment @coderabbitai help to get the list of available commands and usage tips.

@Miou-zora Miou-zora self-assigned this Jan 24, 2026
@Miou-zora Miou-zora requested a review from a team January 24, 2026 16:13
@Miou-zora Miou-zora marked this pull request as ready for review January 24, 2026 16:13
@Miou-zora
Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 24, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown
Contributor

@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

🤖 Fix all issues with AI agents
In
`@src/plugin/default-pipeline/src/system/GPUComponentManagement/OnMaterialCreation.cpp`:
- Around line 30-40: The code path in OnMaterialCreation.cpp that checks
material.ambientTexName and textureContainer.Contains(textureId) can leave
GPUMaterial.texture uninitialized when the file doesn't exist and the texture
isn't pre-registered; update the logic to set a safe fallback: when
std::filesystem::exists(material.ambientTexName) is false and
textureContainer.Contains(textureId) is false (or when ambientTexName is empty),
assign GPUMaterial.texture to a known defaultTextureId (or a sentinel) and/or
register a default placeholder texture via
textureContainer.Add(defaultTextureId, ...) and ensure the bind group creation
that references textureId uses that defaultTextureId (or conditionally omits the
texture) and emits a warning via logging to aid debugging.
🧹 Nitpick comments (1)
examples/graphic_material_usage/src/main.cpp (1)

47-58: Minor naming inconsistency between texture's internal name and registration key.

The texture is created with internal name "LocalTexture" (line 52) but registered under key "LocalTextureName" (line 54), which matches the material's ambientTexName (line 49). While functionally correct, aligning these names would improve clarity for readers of this example.

Suggested alignment
     Object::Component::Material materialFromLocalTexture;
     materialFromLocalTexture.ambientTexName = "LocalTextureName";
     auto &textureManager = core.GetResource<Graphic::Resource::TextureContainer>();
     auto texture =
-        Graphic::Resource::Texture(core.GetResource<Graphic::Resource::Context>(), "LocalTexture", glm::uvec2(255, 255),
+        Graphic::Resource::Texture(core.GetResource<Graphic::Resource::Context>(), "LocalTextureName", glm::uvec2(255, 255),
                                    [](glm::uvec2 pos) { return glm::u8vec4(pos.x, pos.y, 0, 255); });
     textureManager.Add("LocalTextureName", std::move(texture));

Copy link
Copy Markdown
Contributor

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/plugin/default-pipeline/src/system/GPUComponentManagement/OnMaterialCreation.cpp (1)

62-70: Bind group unconditionally includes texture entry regardless of whether texture was loaded.

The bind group creation at line 68 always includes textureId as a texture asset. If the texture loading failed (file not found and not in container) or ambientTexName was empty, textureId won't exist in the container, potentially causing a runtime error when the bind group is used.

Consider either:

  1. Ensuring a default texture is always registered, or
  2. Conditionally including the texture entry based on whether GPUMaterial.texture was assigned.

@Miou-zora Miou-zora merged commit 78d8f97 into main Jan 24, 2026
3 of 4 checks passed
@Miou-zora Miou-zora deleted the add-a-way-to-use-custom-materials branch January 24, 2026 17:22
@Miou-zora Miou-zora restored the add-a-way-to-use-custom-materials branch January 24, 2026 17:22
@sonarqubecloud
Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant