Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

fbx import messes up vertex colors

Discussion in 'Scripting' started by schmid, Dec 1, 2008.

  1. schmid

    schmid

    Joined:
    May 22, 2008
    Posts:
    4
    I'm trying to use vertex coloring as input to a shader.

    To test, I created a plane (4 vertices) in Blender, and exported an FBX file. The revelant parts of the FBX is here:

    Code (csharp):
    1.  
    2.     Vertices: 1.000000,1.000000,0.000000,1.000000,-1.000000,0.000000,-1.000000,-1.000000,0.000000,-1.000000,1.000000,0.000000
    3.     PolygonVertexIndex: 0,3,2,-2
    4.  
    Code (csharp):
    1.  
    2.     LayerElementColor: 0 {
    3.         Version: 101
    4.         Name: "Col"
    5.         MappingInformationType: "ByPolygonVertex"
    6.         ReferenceInformationType: "IndexToDirect"
    7.         Colors: 28,252,72,37,18,252,252,0,0,0,0,0
    8.         ColorIndex: 0,1,2,3
    9.     }
    10.  
    Then the following code dumps the colors:

    Code (csharp):
    1.  
    2.     Mesh mesh = (GetComponent(typeof(MeshFilter)) as MeshFilter).mesh;
    3.     foreach(Color color in mesh.colors)
    4.         print(color);
    5.  
    Resulting in this output:

    Code (csharp):
    1.  
    2.     RGBA(1.0, 1.0, 1.0, 1.0)
    3.     RGBA(0.0, 0.0, 0.0, 0.0)
    4.     RGBA(0.0, 0.0, 0.0, 0.0)
    5.     RGBA(1.0, 1.0, 1.0, 0.0)
    6.  
    Is this a bug in Unity, the Blender FBX exporter, or did I do something wrong?
     
  2. Joe ByDesign

    Joe ByDesign

    Joined:
    Oct 13, 2005
    Posts:
    841
    Odd... and are you seeing the colors using your shader?

    Might be down to the import, somehow stripping the colors, or if you're seeing the colors in Unity with your shader, might be a bug in Mesh.colors.

    I'd say submit a Bug Report in any case.
     
  3. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,325
    I believe this is a problem with your mesh from Blender and Unity not properly reading the vertex colors. The following script works perfectly fine on a vertex colored plane from Maya:

    Code (csharp):
    1. function Start ()
    2. {
    3.     var filter : MeshFilter = gameObject.GetComponent(MeshFilter);
    4.     var mesh : Mesh = filter.mesh;
    5.     var colors : Color[] = mesh.colors;
    6.  
    7.     for (var i=0;i<colors.Length;i++)
    8.     {
    9.         print (colors[i]);
    10.     }
    11. }
    Looking at the bug you reported right now (a bit late I know) ;)
    Ethan
     
  4. amirebrahimi

    amirebrahimi

    Joined:
    Jan 27, 2008
    Posts:
    171
    So, this is a bug in the Blender FBX exporter. I have submitted a patch to the maintainer over at Blender. There are two issues. The first is that the values are incorrect and should be floating point values between 0 and 1. A simply division by 255 will get us there. The second part is that the exporter doesn't write alpha channels, which are required. This has already been fixed in the Blender source code repository and will be in the next release.

    You can either massage the data by hand or with a script or come up with some other solution in the meantime.
     
  5. amirebrahimi

    amirebrahimi

    Joined:
    Jan 27, 2008
    Posts:
    171
    By the way, you can pick up new Blender builds from:
    http://www.graphicall.org/builds/index.php

    If you want the change I submitted, then you should look for a Blender 2.5 Trunk build that was built after 2/11/09 or SVN Revision 18934.
     
  6. Hummin

    Hummin

    Joined:
    Feb 20, 2009
    Posts:
    9
    I'm trying to import vertex colors for the vertex color shader I found in the wiki and I get strange rainbow color effects here and there, and not the correct colors. I'm using the .blend format. Is vertex colors supported for .blend files?

    I'm also using unity iphone.

    Best Regards, Linus
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,334
    Read the thread above.

    --Eric
     
  8. fuzzy3d

    fuzzy3d

    Joined:
    Jun 17, 2009
    Posts:
    189
    Hi,
    i am new in unity and my english is very poor, but i think i understand Hummin. I tested blender and i cannot export vertex colors. Now i have blender 2.49a (and python 2.6.2) on vista and i can export vertex colors thru collada 1.4 into unity 2.5.0f5.
     

    Attached Files:

  9. amirebrahimi

    amirebrahimi

    Joined:
    Jan 27, 2008
    Posts:
    171
    You're saying that vertex colors don't export properly if you are saving a .blend file directly?
     
  10. amirebrahimi

    amirebrahimi

    Joined:
    Jan 27, 2008
    Posts:
    171
    So, I hadn't had a chance to check out 2.49a yet. I installed it and compared it with how it was exporting vertex colors before. It seems they have integrated the patch I sent in this version. I've included a comparison image.

    Additionally, I have attached a debug shader file, so you can view your vertex colors.
     

    Attached Files:

  11. fuzzy3d

    fuzzy3d

    Joined:
    Jun 17, 2009
    Posts:
    189
    When i save *.blend into my assets directory, i cannot import mesh in UNITY drag-and-drop to scene like COLLADA file *.dae.
    I cannot find FBX exporter in Blender (see pic.).
    COLLADA works perfect.

    I am working now on Windows Vista 64. Not tested on my MAC yet.

    Thanks for shader.
     

    Attached Files:

  12. amirebrahimi

    amirebrahimi

    Joined:
    Jan 27, 2008
    Posts:
    171
    You'll have to check your Blender installation. Looking at the release page it seems that Blender 2.49 64-bit is still in a development stage and not feature complete. I suggest installing the 32-bit version and a 32-bit version of Python to match. You may need to uninstall the 64-bit versions of Blender/Python if it doesn't work immediately. Additionally, make sure that you can properly export FBX from within Blender before dropping a .blend file in your Unity folder.
     
  13. fuzzy3d

    fuzzy3d

    Joined:
    Jun 17, 2009
    Posts:
    189
    I am working on 32bits version Python and Blender becouse compatibility with MAC (Leopard is 32bit system).
    COLLADA is no problem for me now. Seems to work.
     

    Attached Files:

  14. fuzzy3d

    fuzzy3d

    Joined:
    Jun 17, 2009
    Posts:
    189
    Testing Unity iPhone on MAC Leopard OS. File .blend with vertex colors working excellent.
    (sorry, snapshot is not from MAC, becouse i am new on Leopard and PrintScreen key not working)
     

    Attached Files:

  15. CrystalVisions

    CrystalVisions

    Joined:
    Sep 11, 2007
    Posts:
    61
    I too have Blender 2.49 32 bit on windows xp and do not see an option to export to fbx.

    When I place a .blend file in my assets folder in Unity I get the error:

    Blender could not convert the .blend file to FBX file.
    You need to use Blender 2.45 or higher for direct Blender import to work.

    Any ideas where to get the export script if it is not already in blender?

    oops - should have looked a little longer through
    the posts to find this link:

    http://www.blender.org/forum/viewtopic.php?p=73670&sid=e4f97ea3d374601c310fedb7459e55e0

    and now it works!
     
  16. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    What a load of trash. I spent several hours learning Python and Blender's API, just to find out that the alpha channel of the vertex colors

    1. does nothing in Blender
    2. doesn't get exported

    On top of the fact that only the highest-layered vertex color layer gets exported, and you can't rearrange the order of layers, I'm pretty unhappy. :evil:

    Feel free to test - I had no luck with 2.49b, and there is no documentation on how to script for 2.5 now, so I'm not going to bother at present.

    The idea is, you have the top layer be called RGBA, and have another layer called Alpha somewhere below it. Paint some RGB colors into Alpha and then run this.

    Code (csharp):
    1. import Blender
    2. import bpy
    3.  
    4. scene = bpy.data.scenes.active
    5. selection = scene.objects.active
    6. mesh = selection.getData(mesh=1)
    7.  
    8. # Grab the colors from the vertex color layer named "Alpha".
    9. mesh.activeColorLayer = ('Alpha')
    10. alphaColorList = []
    11. for face in mesh.faces:
    12.     alphaColorList.append(face.col)
    13.  
    14. # Convert them to grayscale and use that as the A channel for the "RGB" layer.
    15. mesh.activeColorLayer = ('RGBA')
    16. for rgbaFace, alphaColors in zip(mesh.faces, alphaColorList):
    17.     for rgbaColor, alphaColor in zip(rgbaFace.col, alphaColors):
    18.         rgbaColor.a = (alphaColor.r + alphaColor.g + alphaColor.b) / 3
    19.        
    20.         # just to test that it's sort of working
    21.         rgbaColor.r = rgbaColor.g = rgbaColor.b = rgbaColor.a
    22.        
    23. mesh.update()
     
  17. fuzzy3d

    fuzzy3d

    Joined:
    Jun 17, 2009
    Posts:
    189
    Thank you very much. Can i really use vertex alpha value in unity for iphone? It is magic.
     
unityunity