Fix #106326: Add scale-bias processing for UsdUVTexture #115224

Merged
Jesse Yurkovich merged 9 commits from deadpin/blender:fix106326-scalebias into main 2024-02-02 23:23:38 +01:00

This adds the processing required to import and export, simple, material
graphs utilizing the UsdUVTexture Scale and Bias inputs.

Since Blender does not have equivalent inputs on its Image node, a
Multiply-Add node is used instead. This matches the calculation as per
the UsdPreviewSurface spec[1]

A complicating factor here is when these two inputs are used for normal
map textures. The Scale and Bias inputs are authored in such a way to
take the [0, 1] image data and expand into the [-1, 1] tangent space
range as per the spec. However, the Blender Normal Map node expects to
do this transformation itself. The processing in this patch needs to
account for this. For the case of normal maps it will:

  • Apply the Scale-Bias calculation directly as authored
  • Apply an additional transform to move from [-1, 1] back into [0, 1]
  • Feeds this into the Normal Map node

This processing extends to Export as well. During material graph
traversal we need to "skip" the middle adjustment transform and only
export the "real" Scale-Bias data. Traversing the graph like this can be
error prone but is probably the best we can do without having a native
Scale-Bias concept on Blender's Image node.

[1] https://openusd.org/release/spec_usdpreviewsurface.html#texture-reader


Testing performed

  • The test case in #106326 will load correctly and can be round-tripped as well (both EEVEE and Cycles seem ok)
  • Spot checked that some ALab and Intel Moore Lane assets still load correctly

Here is what the 3 graphs from the above issue look like. Notice that the "easy"/"default" case of Scale=(2, 2, 2), Bias=(-1, -1, -1) will yield no additional nodes:
example.png

This adds the processing required to import and export, simple, material graphs utilizing the UsdUVTexture Scale and Bias inputs. Since Blender does not have equivalent inputs on its Image node, a Multiply-Add node is used instead. This matches the calculation as per the UsdPreviewSurface spec[1] A complicating factor here is when these two inputs are used for normal map textures. The Scale and Bias inputs are authored in such a way to take the [0, 1] image data and expand into the [-1, 1] tangent space range as per the spec. However, the Blender Normal Map node expects to do this transformation itself. The processing in this patch needs to account for this. For the case of normal maps it will: - Apply the Scale-Bias calculation directly as authored - Apply an additional transform to move from [-1, 1] back into [0, 1] - Feeds this into the Normal Map node This processing extends to Export as well. During material graph traversal we need to "skip" the middle adjustment transform and only export the "real" Scale-Bias data. Traversing the graph like this can be error prone but is probably the best we can do without having a native Scale-Bias concept on Blender's Image node. [1] https://openusd.org/release/spec_usdpreviewsurface.html#texture-reader ---- Testing performed - The test case in #106326 will load correctly and can be round-tripped as well (both EEVEE and Cycles seem ok) - Spot checked that some ALab and Intel Moore Lane assets still load correctly Here is what the 3 graphs from the above issue look like. Notice that the "easy"/"default" case of Scale=(2, 2, 2), Bias=(-1, -1, -1) will yield no additional nodes: ![example.png](/attachments/b3f76251-7609-4289-a47d-d1ddc4965e51)
Jesse Yurkovich added 1 commit 2023-11-21 08:14:44 +01:00
Jesse Yurkovich requested review from Charles Wardlaw 2023-11-21 08:16:19 +01:00
Jesse Yurkovich requested review from Michael Kowalski 2023-11-21 08:16:19 +01:00
Jesse Yurkovich added the
Module
Pipeline, Assets & IO
Interest
USD
labels 2023-11-21 21:24:40 +01:00

This is great! Thank you for implementing this, @deadpin! I'm out this week for the holiday, but will review when I get back.

This is great! Thank you for implementing this, @deadpin! I'm out this week for the holiday, but will review when I get back.
Jesse Yurkovich changed title from WIP: Fix #106326: Add scale-bias processing for UsdUVTexture to Fix #106326: Add scale-bias processing for UsdUVTexture 2023-11-27 10:18:33 +01:00
Michael Kowalski requested changes 2023-11-27 21:24:18 +01:00
@ -586,0 +591,4 @@
const char *sock_output_name;
};
IntermediateNode add_normal_map(const pxr::UsdShadeShader &usd_shader,

Should this function be declared static, as it's local to the file?

Should this function be declared `static`, as it's local to the file?
deadpin marked this conversation as resolved
@ -586,0 +663,4 @@
return scale_bias;
}
IntermediateNode add_scale_bias_adjust(bNodeTree *ntree, int column, NodePlacementContext *r_ctx)

As above, should the function be declared static?

As above, should the function be declared static?
deadpin marked this conversation as resolved
Jesse Yurkovich added 3 commits 2023-11-27 22:56:54 +01:00
Michael Kowalski reviewed 2023-11-28 02:35:38 +01:00
@ -236,0 +262,4 @@
if (!scale_attr) {
scale_attr = usd_shader.CreateInput(usdtokens::scale, pxr::SdfValueTypeNames->Float4);
}
scale_attr.Set(pxr::VtValue(scale));

When setting the scale and bias attribute values here and below, I'm not sure if it's necessary to construct a VtValue and I believe it should be safe to set the GvVec4f values directly, i.e., by calling scale_attr.Set(scale) and bias_attr.Set(bias). But I don't feel strongly about making this change.

When setting the scale and bias attribute values here and below, I'm not sure if it's necessary to construct a `VtValue` and I believe it should be safe to set the `GvVec4f` values directly, i.e., by calling `scale_attr.Set(scale)` and `bias_attr.Set(bias)`. But I don't feel strongly about making this change.
deadpin marked this conversation as resolved
Michael Kowalski approved these changes 2023-11-28 02:47:46 +01:00
Michael Kowalski left a comment
Member

These changes look very good to me and work nicely in my tests.

You solved some tricky issues adjusting the scale and bias where needed for normal maps. I also like how you abstracted the IntermediateNode data, which makes the code easier to follow.

Thank you!

These changes look very good to me and work nicely in my tests. You solved some tricky issues adjusting the scale and bias where needed for normal maps. I also like how you abstracted the `IntermediateNode` data, which makes the code easier to follow. Thank you!
Jesse Yurkovich added 1 commit 2023-11-28 08:54:10 +01:00

Hey Jesse,

Thanks again for doing this!

I'm having a look and the 0.5 multiply seems off-- if you compare it to the same asset viewed from usdview the normals are softer. In this image the left side is your patch and the right side is me connecting just the main multiply/add into the normal:

image

The right side is more in line with usdview:

image

Note the lack of sharpness in the original import.

I'm also confused on why the normal map node is still required, since under the hood all it does is the same calculation as the multiply add.

Lastly, when comparing against usdview the "RNormals" object looks incorrect (again, too soft). Original import (RNormals is on the far right):

image

After applying the typical *2 - 1 calculation:

image

usdview (RNormals is selected):

image

Notice the sharpness matches better to the version where I added the extra multiply add.

Hey Jesse, Thanks again for doing this! I'm having a look and the 0.5 multiply seems off-- if you compare it to the same asset viewed from usdview the normals are softer. In this image the left side is your patch and the right side is me connecting just the main multiply/add into the normal: ![image](/attachments/6189d29e-e723-4ec7-8982-a1445fa96c57) The right side is more in line with usdview: ![image](/attachments/eb4a98a7-ee0a-4742-8fc1-45bec4c98300) Note the lack of sharpness in the original import. I'm also confused on why the normal map node is still required, since under the hood all it does is the same calculation as the multiply add. Lastly, when comparing against usdview the "RNormals" object looks incorrect (again, too soft). Original import (RNormals is on the far right): ![image](/attachments/b83114d9-60b2-48b5-a25e-0e72098a8da3) After applying the typical *2 - 1 calculation: ![image](/attachments/69114821-21a0-4df7-98d7-180ac8466622) usdview (RNormals is selected): ![image](/attachments/84828855-2146-4c19-810a-26f298edd1ef) Notice the sharpness matches better to the version where I added the extra multiply add.
Author
Member

The Normal Map node is still needed because it does more than just expand to the [-1, 1] space. It needs to actually apply the normal map using the tangents and bi-tangents based on the geometry. Otherwise you'll get bad shading depending on orientation (code; EEVEE has similar problems)
ex0.png

And since the node is needed for that, we can't let it do it's own multiply-add. Doing that will take the [-1, 1] data we're now feeding it and it will become [-3, 1]. You can see this if you visualize the output of the nodes directly:
ex2.png
ex1.png

The Normal Map node is still needed because it does more than just expand to the [-1, 1] space. It needs to actually apply the normal map using the tangents and bi-tangents based on the geometry. Otherwise you'll get bad shading depending on orientation ([code](https://projects.blender.org/blender/blender/src/branch/main/intern/cycles/kernel/svm/tex_coord.h#L266); EEVEE has similar problems) ![ex0.png](/attachments/a64f9fcf-0ebd-4dee-8481-6c2ce52658c3) And since the node is needed for that, we can't let it do it's own multiply-add. Doing that will take the [-1, 1] data we're now feeding it and it will become [-3, 1]. You can see this if you visualize the output of the nodes directly: ![ex2.png](/attachments/17504676-5e24-455c-875d-3c2c20526ff0) ![ex1.png](/attachments/2dae1ef2-406b-44c3-aa7b-0d5cebd3b619)
712 KiB
718 KiB
460 KiB
Jesse Yurkovich added 1 commit 2023-12-07 01:38:40 +01:00
Jesse Yurkovich added 2 commits 2024-01-03 23:51:21 +01:00
Jesse Yurkovich added 1 commit 2024-02-02 21:50:06 +01:00
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
0e1c28e797
Merge remote-tracking branch 'origin/main' into fix106326-scalebias
Author
Member

@blender-bot build

@blender-bot build
Jesse Yurkovich merged commit f355b20262 into main 2024-02-02 23:23:38 +01:00
Jesse Yurkovich deleted branch fix106326-scalebias 2024-02-02 23:23:40 +01:00
Sign in to join this conversation.
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#115224
No description provided.