Fix #117367: OBJ: fix vertex colors imported scrambled up #126065

Merged
Aras Pranckevicius merged 2 commits from scurest/blender:fix-117367 into main 2024-08-09 08:54:08 +02:00
Contributor

Lift certain incorrect assumptions about the order of vertices in
an OBJ file when processing vertex colors, which could lead to
missing or randomly permuted colors.

Replaces the list-of-blocks representation, attuned to assumptions
that the verts in an object form a contiguous subrange, with a
flat array better suited for random-access.


Recall that an OBJ has a global list of vertices (v), each of which may or may not have a vertex color. Each object (o) references a subset of vertices in this global list. For each referenced vert, the importer creates one Blender vert in the imported mesh. The vertex index in the global OBJ list is the global index, and the index in the Blender mesh's list of verts is the local index.

Specifically, the incorrect assumption in the importer appears to be that an object always references a contiguous range of vertices in the global list (no gaps) between the first and last referenced verts, and therefore (1) whether it has vertex colors just depends on if that range lies in a range that has vertex colors, and (2) the local index for a vert is just the distance to the start of the range in the global list.

(Note that vertex positions are stored the same way as colors, but the code for them does not make this assumption, so it's okay.)

So this does the following:

  • Replaces the list-of-blocks representation with a flat vertex color array. -1 is used to mark verts that did not have color. As an optimization, the array can omit trailing elements if they are all missing colors (so no memory cost for files without vertex colors). This is less sparse, but simpler for random-access.
  • Does a full scan of all referenced vertices to determine if they all have colors. As opposed to the coarse-but-fast range check.
  • Does a second scan to actually fill out the colors via color_attr_data[local_index] = global_vertex_colors[global_index]. Reusing the global-to-local mapping from create_vertices. This is the part that actually fixes the misaligned color problem.

In terms of performance, there should be no cost to importing files without vertex colors. Others are expected to slow down.

Fixes all the files posted in #117367 in my tests.

Lift certain incorrect assumptions about the order of vertices in an OBJ file when processing vertex colors, which could lead to missing or randomly permuted colors. Replaces the list-of-blocks representation, attuned to assumptions that the verts in an object form a contiguous subrange, with a flat array better suited for random-access. ------- Recall that an OBJ has a global list of vertices (`v`), each of which may or may not have a vertex color. Each object (`o`) references a subset of vertices in this global list. For each referenced vert, the importer creates one Blender vert in the imported mesh. The vertex index in the global OBJ list is the _global index_, and the index in the Blender mesh's list of verts is the _local index_. Specifically, the incorrect assumption in the importer appears to be that an object always references a contiguous range of vertices in the global list (no gaps) between the first and last referenced verts, and therefore (1) whether it has vertex colors just depends on if that range lies in a range that has vertex colors, and (2) the local index for a vert is just the distance to the start of the range in the global list. (Note that vertex positions are stored the same way as colors, but the code for them does not make this assumption, so it's okay.) So this does the following: * **Replaces the list-of-blocks representation with a flat vertex color array**. -1 is used to mark verts that did not have color. As an optimization, the array can omit trailing elements if they are all missing colors (so no memory cost for files without vertex colors). This is less sparse, but simpler for random-access. * **Does a full scan of all referenced vertices to determine if they all have colors**. As opposed to the coarse-but-fast range check. * **Does a second scan to actually fill out the colors via `color_attr_data[local_index] = global_vertex_colors[global_index]`**. Reusing the global-to-local mapping from `create_vertices`. This is the part that actually fixes the misaligned color problem. In terms of performance, there should be no cost to importing files without vertex colors. Others are expected to slow down. Fixes all the files posted in #117367 in my tests.
Iliya Katushenock added this to the Pipeline, Assets & IO project 2024-08-08 00:49:05 +02:00

@blender-bot build

@blender-bot build
Aras Pranckevicius reviewed 2024-08-08 09:39:52 +02:00
@ -440,0 +423,4 @@
/* First pass to determine if we need to create a color attribute.
* Only when all vertices used specify a vertex color. */
for (int vi : mesh_geometry_.vertices_) {
const float3 color = global_vertices_.get_vertex_color(vi);

Could this check be made a bit faster by having something like bool has_vertex_color(vi) that simply returns true or false? Right now this creates a whole "dummy" float3 only to check the validity.

And then seemingly whole get_vertex_color is not even actually needed elsewhere

Could this check be made a bit faster by having something like `bool has_vertex_color(vi)` that simply returns true or false? Right now this creates a whole "dummy" float3 only to check the validity. And then seemingly whole `get_vertex_color` is not even actually needed elsewhere
Author
Contributor

How's that?

How's that?

Good! Does your latest push fix the obj tests by chance? They were all crashing on the buildbot this morning.

Good! Does your latest push fix the obj tests by chance? They were all crashing on the buildbot this morning.
Author
Contributor

If by crashing you mean the tests failed, no that is not fixed.

If by crashing you mean the tests failed, no that is not fixed.

Since you force-pushed it's hard to see the buildbot failures on the previous commit. But IIRC the output of the failing test simply said "segfault", which I assume means something within the test crashed. Which makes them fail, yes.

Since you force-pushed it's hard to see the buildbot failures on the previous commit. But IIRC the output of the failing test simply said "segfault", which I assume means something within the test crashed. Which makes them fail, yes.
aras_p marked this conversation as resolved
Scurest force-pushed fix-117367 from 599ef78a25 to 8c5148042b 2024-08-08 13:51:16 +02:00 Compare
Author
Contributor

So I have broken #MRGB colors. AFAICT they work like this

v ... # v1
v ... # v2
#MRGB ffff0000  # sets color for v1 (-2)
#MRGB ffff0000  # sets color for v2 (-1)
v ... # v3
v ... # v4
#MRGB ffff0000  # sets color for v4 (-1)

This is easier to handle with the list-of-blocks representation (perhaps that's why it was used?), since you don't know when you process each #MRGB line what vertex index it affects until you have seen the rest of the #MRGB lines up to the next v (or EOF).

My current idea is to buffer one Vector<float3> mrgb_block in GlobalVertices, accumulate into it on every #MRGB line, and flush it out the to the vertex_colors list at each v or EOF. How does that sound?

So I have broken #MRGB colors. AFAICT they work like this ``` v ... # v1 v ... # v2 #MRGB ffff0000 # sets color for v1 (-2) #MRGB ffff0000 # sets color for v2 (-1) v ... # v3 v ... # v4 #MRGB ffff0000 # sets color for v4 (-1) ``` This is easier to handle with the list-of-blocks representation (perhaps that's why it was used?), since you don't know when you process each #MRGB line what vertex index it affects until you have seen the rest of the #MRGB lines up to the next `v` (or EOF). My current idea is to buffer one `Vector<float3> mrgb_block` in `GlobalVertices`, accumulate into it on every #MRGB line, and flush it out the to the `vertex_colors` list at each `v` or EOF. How does that sound?

This is easier to handle with the list-of-blocks representation (perhaps that's why it was used?)

Quite probably. I forget which order I implemented those in, but possibly I first tried to make MRGB work, and that is indeed very much "block based", and quite poorly defined (as in, it is not clear at all what should happen when multiple objects are present, etc.). And as you figured out, I did some poorly thought out decisions in handling various xyzrgb cases.

My current idea <...> How does that sound?

Sounds sensible to me.

> This is easier to handle with the list-of-blocks representation (perhaps that's why it was used?) Quite probably. I forget which order I implemented those in, but possibly I first tried to make MRGB work, and that is indeed very much "block based", and quite poorly defined (as in, it is not clear at all what *should happen* when multiple objects are present, etc.). And as you figured out, I did some poorly thought out decisions in handling various xyzrgb cases. > My current idea <...> How does that sound? Sounds sensible to me.
Scurest added 1 commit 2024-08-08 21:38:45 +02:00
Fix: OBJ: fix importing of #MRGB vertex colors
All checks were successful
buildbot/vexp-code-patch-lint Build done.
buildbot/vexp-code-patch-linux-x86_64 Build done.
buildbot/vexp-code-patch-darwin-x86_64 Build done.
buildbot/vexp-code-patch-darwin-arm64 Build done.
buildbot/vexp-code-patch-windows-amd64 Build done.
buildbot/vexp-code-patch-coordinator Build done.
b3f618f0af
Fix non-trivial OBJs with #MRGB colors by buffering a block of
MRGB lines and flushing them to the main vertex_colors array.
Author
Contributor

Okay, the mrgb_block is implemented. Tests are passing locally.

Okay, the `mrgb_block` is implemented. Tests are passing locally.

@blender-bot build

@blender-bot build
Aras Pranckevicius approved these changes 2024-08-09 08:51:51 +02:00
Aras Pranckevicius merged commit 86e7668b11 into main 2024-08-09 08:54:08 +02:00

Excellent, thanks! Merged to main for 4.3, also added an item for backporting into 4.2.2.

Excellent, thanks! Merged to main for 4.3, also added an item for backporting into 4.2.2.
Scurest deleted branch fix-117367 2024-08-09 17:08:34 +02:00
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
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
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
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
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
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
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
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 Assignees
2 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#126065
No description provided.