OBJ Importer: Vertex Colors Lose Alignment In At Least 2 Situations #117367

Open
opened 2024-01-20 19:16:43 +01:00 by L.Spiro · 6 comments

System Information
Operating system: Windows-10-10.0.19045-SP0 64 Bits
Graphics card: GeForce RTX 3050 Ti Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 462.62

Blender Version
Broken: version: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: 9be62e85b727
Worked: (newest version of Blender that worked as expected)

Short description of error
I have found that Blender likes vertex colors to simply be an extension of the v line in .OBJ files, so I insert vertex colors into my .OBJ file by making a 6-element v instead of a 3- or 4- element v.
IE:

    v -291.437500 19.875000 -420.062500 0.749020 0.874510 0.121569
    v -305.187500 16.000000 -362.187500 0.749020 0.874510 0.121569
    v -247.937500 18.000000 -362.187500 1.000000 1.000000 1.000000
…

By definition this means that vertex colors are strongly associated with vertices. IE it is not possible to have the correct vertices but incorrect vertex colors. And yet there are at least 2 ways this happens in Blender 4.0: Referencing only a section of the v/vt/vn tables and by using o.

This image shows the correct result on the left and the misaligned vertex colors on the right:
image

In both files, the v/vt/vn tables are exactly the same, and all of the f (face) values are exactly the same, but in the right image there is an o at the very start of the face data (which, alone, does not cause the issue) and a 2nd o object starting at the first transparency-enabled face at the end of the file (I have put all the non-opaque faces at the end of the face data in both cases, it’s just that in the 2nd example they are in their own o object).

When the files are identical except for the addition of 2 o [name] lines, there should be no reason for vertex colors to suddenly become misaligned.

The 2nd way that this exact same error can occur is by simply chopping off faces from the end of the file.
In this case, I produce the erroneous result by going to roughly the 3,270th face and then simply removing all of the following faces in a text editor. The file remains valid, and no other data has been modified, it’s just that there are fewer faces, thus also some of the v/vt/vn values are no longer referenced.

By doing this, I get the following result, which is basically just a section of the incorrect right result above:
image
The fact that some of the v/vt/vn values have become unreferenced should not cause a problem for the loader, and it is even more baffling because the vertex colors are defined on the same lines as their associated vertex positions, making it normally impossible for the vertex positions to be correct but the vertex colors to be wrong.

Exact steps for others to reproduce the error
1: Create a reasonably complex object in Blender with colored vertices. In my above example, my model has 1051 v (with 6 floating-point values each to include vertex colors), 1261 vt, 959 vn, and 1216 f.
2: Export to .OBJ.
3: Load it again to confirm it looks fine.
4: Edit the .OBJ file in a text editor. Pick a face anywhere from maybe 30% down the file or later and simply delete all faces after that. In my example above, I started getting bad vertex colors when I started deleting faces that indexed into the 240-270 range in my tables, though I have created other examples that look correct for many more vertices than this before going AWOL.
5: Save and load and observe incorrect vertex coloring.

L. Spiro

**System Information** Operating system: Windows-10-10.0.19045-SP0 64 Bits Graphics card: GeForce RTX 3050 Ti Laptop GPU/PCIe/SSE2 NVIDIA Corporation 4.6.0 NVIDIA 462.62 **Blender Version** Broken: version: 4.0.2, branch: blender-v4.0-release, commit date: 2023-12-05 07:41, hash: `9be62e85b727` Worked: (newest version of Blender that worked as expected) **Short description of error** I have found that Blender likes vertex colors to simply be an extension of the `v` line in .OBJ files, so I insert vertex colors into my .OBJ file by making a 6-element `v` instead of a 3- or 4- element `v`. IE: ``` v -291.437500 19.875000 -420.062500 0.749020 0.874510 0.121569 v -305.187500 16.000000 -362.187500 0.749020 0.874510 0.121569 v -247.937500 18.000000 -362.187500 1.000000 1.000000 1.000000 … ``` By definition this means that vertex colors are strongly associated with vertices. IE it is not possible to have the correct vertices but incorrect vertex colors. And yet there are at least 2 ways this happens in Blender 4.0: Referencing only a section of the v/vt/vn tables and by using `o`. This image shows the correct result on the left and the misaligned vertex colors on the right: ![image](/attachments/ddb9f32f-cc64-4345-b747-92de5688d4c2) In both files, the `v/vt/vn` tables are exactly the same, and all of the `f` (face) values are exactly the same, but in the right image there is an `o` at the very start of the face data (which, alone, does not cause the issue) and a 2nd `o` object starting at the first transparency-enabled face at the end of the file (I have put all the non-opaque faces at the end of the face data in both cases, it’s just that in the 2nd example they are in their own `o` object). When the files are identical except for the addition of 2 `o [name]` lines, there should be no reason for vertex colors to suddenly become misaligned. The 2nd way that this exact same error can occur is by simply chopping off faces from the end of the file. In this case, I produce the erroneous result by going to roughly the 3,270th face and then simply removing all of the following faces in a text editor. The file remains valid, and no other data has been modified, it’s just that there are fewer faces, thus also some of the `v/vt/vn` values are no longer referenced. By doing this, I get the following result, which is basically just a section of the incorrect right result above: ![image](/attachments/542767cb-b663-4682-9edc-c7018c53ae82) The fact that some of the `v/vt/vn` values have become unreferenced should not cause a problem for the loader, and it is even more baffling because the vertex colors are defined on the same lines as their associated vertex positions, making it normally impossible for the vertex positions to be correct but the vertex colors to be wrong. **Exact steps for others to reproduce the error** 1: Create a reasonably complex object in Blender with colored vertices. In my above example, my model has 1051 `v` (with 6 floating-point values each to include vertex colors), 1261 `vt`, 959 `vn`, and 1216 `f`. 2: Export to .OBJ. 3: Load it again to confirm it looks fine. 4: Edit the .OBJ file in a text editor. Pick a face anywhere from maybe 30% down the file or later and simply delete all faces after that. In my example above, I started getting bad vertex colors when I started deleting faces that indexed into the 240-270 range in my tables, though I have created other examples that look correct for many more vertices than this before going AWOL. 5: Save and load and observe incorrect vertex coloring. L. Spiro
470 KiB
109 KiB
L.Spiro added the
Priority
Normal
Status
Needs Triage
Type
Report
labels 2024-01-20 19:16:44 +01:00
Contributor

Can you attach an OBJ file for testing?

Can you attach an OBJ file for testing?
Contributor

Nevermind, I managed to repro the case of inserting an o line. I've attached a Test.obj. If o obj is inserted on line 796, the vertex colors are broken. If it is inserted on line 797 instead though, it doesn't break.

Iss117367.zip

     Test.obj        Test-Line796-Broken.obj  Test-Line797-Working.obj
     -------------   -----------------------  ------------------------
794  f 397 399 400   f 397 399 400            f 397 399 400
795  f 401 402 403   f 401 402 403            f 401 402 403
796  f 401 403 404   o Test2                  f 401 403 404
797  f 405 406 407   f 401 403 404            o Test2
798  f 405 407 408   f 405 406 407            f 405 406 407

ObjVertexColorIssue.png

Nevermind, I managed to repro the case of inserting an `o` line. I've attached a Test.obj. If `o obj` is inserted on line 796, the vertex colors are broken. If it is inserted on line 797 instead though, it doesn't break. [Iss117367.zip](/attachments/c498f313-82c8-4dc8-b348-d018ec072521) ``` Test.obj Test-Line796-Broken.obj Test-Line797-Working.obj ------------- ----------------------- ------------------------ 794 f 397 399 400 f 397 399 400 f 397 399 400 795 f 401 402 403 f 401 402 403 f 401 402 403 796 f 401 403 404 o Test2 f 401 403 404 797 f 405 406 407 f 401 403 404 o Test2 798 f 405 407 408 f 405 406 407 f 405 406 407 ``` ![ObjVertexColorIssue.png](/attachments/1be9520e-bcb8-409a-b7c8-f0d7bd1564e1)
Contributor

Simpler example: Take this OBJ file

v -0.004936 0.000000 1.013093 1.0000 0.4235 0.0000
v 1.995063 0.000000 1.013093 1.0000 0.8470 0.0000
v -0.004936 0.000000 -0.986907 1.0000 0.4235 0.0000
v 1.995063 0.000000 -0.986907 1.0000 0.8470 0.0000
v -1.947056 0.000000 1.013093 1.0000 0.0039 0.2118
v -1.947056 0.000000 -0.986907 1.0000 0.0000 0.2118
s 0
o Plane
f 1 2 4 3
f 1 3 6 5

If you insert o Plane2 between the f lines, it breaks.

VertexColorIssue2.png

Simpler example: Take this OBJ file ``` v -0.004936 0.000000 1.013093 1.0000 0.4235 0.0000 v 1.995063 0.000000 1.013093 1.0000 0.8470 0.0000 v -0.004936 0.000000 -0.986907 1.0000 0.4235 0.0000 v 1.995063 0.000000 -0.986907 1.0000 0.8470 0.0000 v -1.947056 0.000000 1.013093 1.0000 0.0039 0.2118 v -1.947056 0.000000 -0.986907 1.0000 0.0000 0.2118 s 0 o Plane f 1 2 4 3 f 1 3 6 5 ``` If you insert `o Plane2` between the f lines, it breaks. ![VertexColorIssue2.png](/attachments/e0fcebc8-31ad-4f20-884f-a91b1d888c23)
Contributor

Just looking at MeshFromGeometry::create_colors, I think it assumes the Blender object vertex index (local vertex index) corresponds to a contiguous range of vertices in the OBJ file (global vertex index), which isn't necessarily true. It seems like it should probably do something like

for each global_vi in mesh_geometry_.vertices_ {
   let local_vi = mesh_geometry_.global_to_local_vertices_[global_vi]
   colors[local_vi] = get_color_at_global_vertex_index(global_vi)
}

(It also apparently currently requires that the whole range of OBJ vertices from the first used one to the last used one have vertex colors (ie everything sits within a single VertexColorsBlock), even if some of the vertices in that range are never referenced by the object. But that's a different issue.)

@aras_p

Just looking at [MeshFromGeometry::create_colors](https://projects.blender.org/blender/blender/src/commit/6a9d2212e43ee0a49bbf32c32bb5531a40a8f7ee/source/blender/io/wavefront_obj/importer/obj_import_mesh.cc#L394-L420), I think it assumes the Blender object vertex index (local vertex index) corresponds to a contiguous range of vertices in the OBJ file (global vertex index), which isn't necessarily true. It seems like it should probably do something like ``` for each global_vi in mesh_geometry_.vertices_ { let local_vi = mesh_geometry_.global_to_local_vertices_[global_vi] colors[local_vi] = get_color_at_global_vertex_index(global_vi) } ``` (It also apparently currently requires that the whole range of OBJ vertices from the first used one to the last used one have vertex colors (ie everything sits within a single VertexColorsBlock), even if some of the vertices in that range are never referenced by the object. But that's a different issue.) @aras_p
Member

Thanks for the report. Can confirm

Thanks for the report. Can confirm
Author

If this is going to be updated, may I request that vertex-color alpha channels be added?
If we have 6 v elements, it is a vertex + RGB.
If 7 v elements, a vertex + RGBA. Or if that is ambiguous with XYZW + RGB, then 8 elements = XYZW + RGBA.

L. Spiro

If this is going to be updated, may I request that vertex-color alpha channels be added? If we have 6 `v` elements, it is a vertex + RGB. If 7 `v` elements, a vertex + RGBA. Or if that is ambiguous with XYZW + RGB, then 8 elements = XYZW + RGBA. L. Spiro
Aras Pranckevicius self-assigned this 2024-01-22 16:02:16 +01:00
Aras Pranckevicius added
Type
Bug
and removed
Type
Report
labels 2024-01-22 16:02:36 +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
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#117367
No description provided.