USD export: Fix incorrect texture alpha mapping. #107022

Merged
Michael Kowalski merged 8 commits from makowalski/blender:usd_preview_surface_alpha_bug into main 2023-05-03 16:13:00 +02:00

This addresses bug #105621.

Fixed bug where texture alpha was incorrectly remapped to red when exporting material shaders.

Now connecting the UsdPreviewSurface opacity input to the UsdUVTexture a output if the Blender texture image node source socket identifier is Alpha.

Specific changes include the following:

  • The InputSpec::source_name struct member was removed, as the connection source name is now determined at runtime based on the Blender node input socket identifier.
  • The traverse_channel() utility function now returns a bNodeLink*, so that the source socket identifier can be queried from the link.
  • In create_usd_preview_surface_materia(), the code for creating and connecting a UsdUVTexture shader was reorganized to group it within one conditional block, to make it easier to follow. Also replaced a call to a deprecated version of UsdShadeInput::ConnectToSource().

To test, open the attached test_usd_preview_surface_alpha.blend file, export to USD and open the exported file in the usdview application. The attached two images show the displayed texture before and after the fix, respectively. Note the incorrect opacities in the first image demonstrating the bug, where regions that should be blue are transparent.

This addresses bug #105621. Fixed bug where texture alpha was incorrectly remapped to red when exporting material shaders. Now connecting the `UsdPreviewSurface` `opacity` input to the `UsdUVTexture` `a` output if the Blender texture image node source socket identifier is Alpha. Specific changes include the following: - The `InputSpec::source_name` struct member was removed, as the connection source name is now determined at runtime based on the Blender node input socket identifier. - The `traverse_channel()` utility function now returns a `bNodeLink*`, so that the source socket identifier can be queried from the link. - In `create_usd_preview_surface_materia()`, the code for creating and connecting a `UsdUVTexture` shader was reorganized to group it within one conditional block, to make it easier to follow. Also replaced a call to a deprecated version of `UsdShadeInput::ConnectToSource()`. To test, open the attached `test_usd_preview_surface_alpha.blend` file, export to USD and open the exported file in the `usdview` application. The attached two images show the displayed texture before and after the fix, respectively. Note the incorrect opacities in the first image demonstrating the bug, where regions that should be blue are transparent.
Michael Kowalski added 1 commit 2023-04-17 03:31:24 +02:00
0c2b8df986 USD export: Fix incorrect texture alpha mapping.
This addresses bug #105621.

Fixed bug where texture alpha was incorrectly remapped to red.
Now connecting the UsdPreviewSurface "opacity" input to the
UsdUVTexture "a" output.
Michael Kowalski added the
Interest
USD
Interest
Pipeline, Assets & IO
labels 2023-04-17 03:32:36 +02:00
Michael Kowalski requested review from Jesse Yurkovich 2023-04-17 16:08:31 +02:00
Michael Kowalski added this to the USD project 2023-04-17 18:20:52 +02:00
Member

I wonder if something more nuanced is needed here. Using the alpha channel for opacity generally makes sense, but I have also encountered materials that use a single-channel texture for opacity, in which case I see the red channel is used. Perhaps the important thing here is to preserve however the material/shader was authored instead of making an assumption about the correct channel?

I wonder if something more nuanced is needed here. Using the alpha channel for opacity generally makes sense, but I have also encountered materials that use a single-channel texture for opacity, in which case I see the red channel is used. Perhaps the important thing here is to preserve however the material/shader was authored instead of making an assumption about the correct channel?

Hmm, without full material graph translation between USD <--> Blender, it will not be possible to be correct in all situations here. Only the following nodes are supported, which isn't enough to accurately describe most general graphs: https://docs.blender.org/manual/en/3.6/files/import_export/usd.html#materials

This patch here is probably ok as a small step forward. But, outside of the exact scenario listed in the bug, things will still be problematic until we can land a design/implementation for things like MaterialX and make decisions on what to do about Blender nodes that aren't compatible with that ecosystem and vice versa.

E.g. Similar to the scenario that Matt describes above:

Original (native scene) After USD Export and Import (this patch)
usd-setup.png usd-result.png
Hmm, without full material graph translation between USD <--> Blender, it will not be possible to be correct in all situations here. Only the following nodes are supported, which isn't enough to accurately describe most general graphs: https://docs.blender.org/manual/en/3.6/files/import_export/usd.html#materials This patch here is probably ok as a small step forward. But, outside of the exact scenario listed in the bug, things will still be problematic until we can land a design/implementation for things like MaterialX and make decisions on what to do about Blender nodes that aren't compatible with that ecosystem and vice versa. E.g. Similar to the scenario that Matt describes above: | Original (native scene) | After USD Export and Import (this patch) | | -- | -- | | ![usd-setup.png](/attachments/71220514-b2ce-4606-818b-48302b9ff92d) | ![usd-result.png](/attachments/fe7ad542-b77d-4872-aa38-9e691ba79c9e) |
Author
Member

I wonder if something more nuanced is needed here. Using the alpha channel for opacity generally makes sense, but I have also encountered materials that use a single-channel texture for opacity, in which case I see the red channel is used. Perhaps the important thing here is to preserve however the material/shader was authored instead of making an assumption about the correct channel?

That's a good point. I am reworking the logic to take the source connection into account.

> I wonder if something more nuanced is needed here. Using the alpha channel for opacity generally makes sense, but I have also encountered materials that use a single-channel texture for opacity, in which case I see the red channel is used. Perhaps the important thing here is to preserve however the material/shader was authored instead of making an assumption about the correct channel? That's a good point. I am reworking the logic to take the source connection into account.
Author
Member

Hmm, without full material graph translation between USD <--> Blender, it will not be possible to be correct in all situations here. Only the following nodes are supported, which isn't enough to accurately describe most general graphs: https://docs.blender.org/manual/en/3.6/files/import_export/usd.html#materials

This is very true, @deadpin. I appreciate that observation. The current material conversion code doesn't aim to cover all possible graphs, obviously. The intent was to try to handle the case where complex shaders have been baked to textures. Adding logic to handle the Separate XYZ node could help in this case, and I'm looking into implementing this as well.

> Hmm, without full material graph translation between USD <--> Blender, it will not be possible to be correct in all situations here. Only the following nodes are supported, which isn't enough to accurately describe most general graphs: https://docs.blender.org/manual/en/3.6/files/import_export/usd.html#materials This is very true, @deadpin. I appreciate that observation. The current material conversion code doesn't aim to cover all possible graphs, obviously. The intent was to try to handle the case where complex shaders have been baked to textures. Adding logic to handle the `Separate XYZ` node could help in this case, and I'm looking into implementing this as well.
Author
Member

@deadpin I will also be addressing the issue of the duplicated nodes on re-import in a separate patch.

@deadpin I will also be addressing the issue of the duplicated nodes on re-import in a separate patch.
Michael Kowalski added 4 commits 2023-04-25 21:35:56 +02:00
Michael Kowalski added 1 commit 2023-04-25 22:18:43 +02:00
Author
Member

@Matt-McLin I updated the logic for connecting texture inputs to take the Blender node source socket identifier into account when determining the UsdUVTexture source name.

@deadpin With the latest changes, the code no longer uses hard coded UsdUVTexture source names. This logic can be extended in the future to set the appropriate source names when converting Blender Separate XYZ shaders. However, I believe handling Separate XYZ shaders should be implemented in a separate pull request.

@Matt-McLin I updated the logic for connecting texture inputs to take the Blender node source socket identifier into account when determining the `UsdUVTexture` source name. @deadpin With the latest changes, the code no longer uses hard coded `UsdUVTexture` source names. This logic can be extended in the future to set the appropriate source names when converting Blender `Separate XYZ` shaders. However, I believe handling `Separate XYZ` shaders should be implemented in a separate pull request.
Jesse Yurkovich approved these changes 2023-04-26 07:10:04 +02:00
Jesse Yurkovich left a comment
Member

Seems ok to me. I was only able to validate that round-tripping the provided .blend file works. usdview is still giving me grief on my machine so I can't validate there, but the exported .usda file seems correct.

Seems ok to me. I was only able to validate that round-tripping the provided .blend file works. usdview is still giving me grief on my machine so I can't validate there, but the exported .usda file seems correct.
Author
Member

Thanks for the reviews, @deadpin and @Matt-McLin!

@Matt-McLin Just FYI, I'll hold off on committing this pull request until after pull

#107149

is finished and committed, since that pull touches the same code.

Thanks for the reviews, @deadpin and @Matt-McLin! @Matt-McLin Just FYI, I'll hold off on committing this pull request until after pull https://projects.blender.org/blender/blender/pulls/107149 is finished and committed, since that pull touches the same code.
Member

Thanks, Michael, this looks good to me. Sorry about wait on #107149, have been traveling all week, planning to get the change up tomorrow.

Thanks, Michael, this looks good to me. Sorry about wait on #107149, have been traveling all week, planning to get the change up tomorrow.
Author
Member

Thanks, Michael, this looks good to me. Sorry about wait on #107149, have been traveling all week, planning to get the change up tomorrow.

Thanks for the update, Matt. No worries about any delays.

> Thanks, Michael, this looks good to me. Sorry about wait on #107149, have been traveling all week, planning to get the change up tomorrow. Thanks for the update, Matt. No worries about any delays.
Michael Kowalski added 2 commits 2023-05-02 18:49:36 +02:00
Author
Member

@Matt-McLin I merged in the recently committed logic to set opacityThreshold from

#107149

Note that I refactored that logic somewhat to consolidate the code in one conditional block, since this is the approach I'm taking in the current patch. I verified that the opacityThreshold test still passes, but please let me know if this change is okay.

@Matt-McLin I merged in the recently committed logic to set `opacityThreshold` from https://projects.blender.org/blender/blender/pulls/107149 Note that I refactored that logic somewhat to consolidate the code in one conditional block, since this is the approach I'm taking in the current patch. I verified that the `opacityThreshold` test still passes, but please let me know if this change is okay.
Member

Thanks, Michael, yes, the refactor for opacityThreshold looks good to me.

Thanks, Michael, yes, the refactor for `opacityThreshold` looks good to me.
Author
Member

@blender-bot build

@blender-bot build
Michael Kowalski merged commit 109c1b92cd into main 2023-05-03 16:13:00 +02:00
Michael Kowalski deleted branch usd_preview_surface_alpha_bug 2023-05-03 16:13:01 +02:00
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:42 +02:00
Bastien Montagne removed this from the USD project 2023-08-25 23:52:28 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#107022
No description provided.