Search Unity

Vertex RGBA Blender 2.5x

Discussion in 'Asset Importing & Exporting' started by Zenchuck, Jun 27, 2014.

  1. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    If you have ever tried bringing vertex colors in from Blender - you will quickly run into the issue of no vertex alpha - until now!

    This is a two part solution with a custom fbx export script as well as a collection of tools inside Blender for quickly and easily editing vertex colors.


    • DOWNLOADS
    - fbx export script export_fbx.py

    -vertexRGBA Add-On vertexRGBA.py
    • INSTALLATION
    - replace export_fbx.py in your ..scripts/addons/io_scene_fbx

    - place vertexRGBA.py in the../scripts/addons folder. You will need to enable the addon inside Blender "User Preferences."
    • INSTRUCTIONS
    - Blender defines vertex color with 3 variables (x,y,z). The basic idea of these scripts is to add a 4th variable (w) because most shaders make use of (x,y,z,w). In Blender a second set of data is added so there are 6 variables. The data from the first color layer is packed in the fbx as usual and the data from the second color layer is combined and added to a 4th. The second vertex color layer name must always end in "_ALPHA". This second layer is created automatically with the provided addon. You could paint color into the alpha layer but it will always be exported as a single channel.

    In Blender you must be in Vertex Paint Mode to paint vertex color. The addon is visible in the 3D viewport tool properties panel.

    Save the .blend file to your Unity project and you will now have a mesh with RGBA vertex color.


    • VERTEX SHADERS
    Vertex colors are great for everything from animating vegetation, adding inexpensive AO and alpha, to splatting textures on terrain.

    I am currently working on some Unity shaders that support vertex color. I have a shader that can splat 8 textures in RGB and add AO in the alpha channel in a single pass. I will be writing all shaders for "shader model 2.0."
     
    Last edited: Jul 1, 2014
  2. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    AWESOME!!!! I've been looking for a similar tool for some time now. Is this compatible with Blender v2.71?
     
  3. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    Yes, it should be compatible. I am using 2.70a currently.
     
    Last edited: Jul 14, 2014
  4. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    Thats great news (for me at least). This is the most thorough support of RGBA vCols I've seen yet. Thanks for your work on this toolset :)
     
  5. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    Have you explored the ability to export these RGB A!!! values with the new FBX exporter in blender? Or modded the current exporter to include these A values in RGBA floats for the verts?
     
  6. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    The rgba values are exported with the provided export_fbx.py. I know that some work is currently being done on the .fbx import/export scripts for Blender but I am not aware of any new functionality that would improve the current exporter. The current export script handles animation and blend shapes as well as all important vertex data - now including rgba :)
     
  7. bansheesoft

    bansheesoft

    Joined:
    Oct 3, 2014
    Posts:
    62
    First thanks for the great tool!

    To get it working in 2.72b I had to cut and paste the color section from the old script into the new. Problem is I can SAVE the blend file and it converts BUT if I use the Autodesk exporter it does not I guess they are two separate scripts?

    The commented section is not commented in the normal exporter and it is commented here.

    Any idea on the exporter not working exporting to FBX???

    USE AT YOUR OWN RISK AND BACKUP ALL FILES YOU ALTER =)...

    #======================== START ORIGINAL CODE ===========================
    # Write VertexColor Layers
    #collayers = []
    #if len(me.vertex_colors):
    # collayers = me.vertex_colors
    # t_lc = [None] * len(me.loops) * 3
    # col2idx = None
    # _nchunk = 4 # Number of colors per line
    # _nchunk_idx = 64 # Number of color indices per line
    # for colindex, collayer in enumerate(collayers):
    # collayer.data.foreach_get("color", t_lc)
    # lc = tuple(zip(*[iter(t_lc)] * 3))
    # fw('\n\t\tLayerElementColor: %i {'
    # '\n\t\t\tVersion: 101'
    # '\n\t\t\tName: "%s"'
    # '\n\t\t\tMappingInformationType: "ByPolygonVertex"'
    # '\n\t\t\tReferenceInformationType: "IndexToDirect"'
    # '\n\t\t\tColors: ' % (colindex, collayer.name))

    # col2idx = tuple(set(lc))
    # fw(',\n\t\t\t '.join(','.join('%.6f,%.6f,%.6f,1' % c for c in chunk)
    # for chunk in grouper_exact(col2idx, _nchunk)))

    # fw('\n\t\t\tColorIndex: ')
    # col2idx = {col: idx for idx, col in enumerate(col2idx)}
    # fw(',\n\t\t\t '
    # ''.join(','.join('%d' % col2idx[c] for c in chunk) for chunk in grouper_exact(lc, _nchunk_idx)))
    # fw('\n\t\t}')
    # del t_lc
    #======================== END ORIGINAL CODE ===========================
    # Write VertexColor Layers
    #======================== Vertex Color Alpha Modification Begin ===========================
    ALPHA_SUFFIX = '_ALPHA'
    collayers = []
    if len(me.vertex_colors):
    alphalayers = {}
    for name,layer in me.vertex_colors.items():
    if name.endswith(ALPHA_SUFFIX):
    refname = name[:-len(ALPHA_SUFFIX)]
    alphalayers[refname]=layer
    else:
    collayers.append(layer)

    t_lc = [None] * len(me.loops) * 3
    if alphalayers:
    t_lca = [None] * len(me.loops)*3
    col2idx = None
    _nchunk = 4 # Number of colors per line
    _nchunk_idx = 64 # Number of color indices per line
    for colindex, collayer in enumerate(collayers):
    collayer.data.foreach_get("color", t_lc)
    iter_tlc = iter(t_lc)
    if collayer.name in alphalayers:
    alphalayers[collayer.name].data.foreach_get("color", t_lca)
    iter_tlca = (sum(c)/3.0 for c in zip(*[iter(t_lca)] * 3))
    lc = tuple(zip(*[iter_tlc,iter_tlc,iter_tlc,iter_tlca]))
    else:
    from itertools import repeat
    lc = tuple(zip(*[iter_tlc,iter_tlc,iter_tlc,repeat(1.0)]))

    fw('\n\t\tLayerElementColor: %i {'
    '\n\t\t\tVersion: 101'
    '\n\t\t\tName: "%s"'
    '\n\t\t\tMappingInformationType: "ByPolygonVertex"'
    '\n\t\t\tReferenceInformationType: "IndexToDirect"'
    '\n\t\t\tColors: ' % (colindex, collayer.name))

    col2idx = tuple(set(lc))
    fw(',\n\t\t\t '.join(','.join('%.6f,%.6f,%.6f,%.6f' % c for c in chunk)
    for chunk in grouper_exact(col2idx, _nchunk)))

    fw('\n\t\t\tColorIndex: ')
    col2idx = {col: idx for idx, col in enumerate(col2idx)}
    fw(',\n\t\t\t '
    ''.join(','.join('%d' % col2idx[c] for c in chunk) for chunk in grouper_exact(lc, _nchunk_idx)))
    fw('\n\t\t}')
    del t_lc
    if alphalayers:
    del t_lca
    #======================== Vertex Color Alpha Modification End ===========================
     

    Attached Files:

  8. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    I too would appreciate the ability to use this tool in 2.72b. I havent gotten it to work again as it used to though :-|
     
  9. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    I would like to get something like this (or better in trunk). Not sure why Blender Foundation has ignored this for so long... This is primary to 3d art.
     
  10. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    +1, this really needs to be in trunk. RGBA for game development is a core necessity.
     
  11. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    any updates for this AWESOME tool/plugin with the newest 2.74 builds?
     
  12. Project-Mysh

    Project-Mysh

    Joined:
    Nov 3, 2013
    Posts:
    223
    Someone noticed that exporting FBX with tangent and binormals in blender is again broken with unity? There is no way to import tangent again....
     
  13. ByteRockersGames

    ByteRockersGames

    Joined:
    Dec 20, 2012
    Posts:
    5
    Zenchuck and Project-Mysh like this.
  14. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    Way to go Martin. I'll test it out.
     
  15. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    Looks like something changed again for 2.74.5:
    Code (csharp):
    1.  
    2.  
    3. Traceback (most recent call last):
    4.   File "/Users/andrejivanis/Dropbox/blender library/scripts/addons/io_scene_fbx/__init__.py", line 509, in execute
    5.     return export_fbx_bin.save(self, context, **keywords)
    6.   File "/Users/andrejivanis/Dropbox/blender library/scripts/addons/io_scene_fbx/export_fbx_bin.py", line 2930, in save
    7.     ret = save_single(operator, context.scene, filepath, **kwargs_mod)
    8.   File "/Users/andrejivanis/Dropbox/blender library/scripts/addons/io_scene_fbx/export_fbx_bin.py", line 2816, in save_single
    9.     False, media_settings, use_custom_props,
    10. TypeError: __new__() missing 1 required positional argument: 'use_custom_props'
    11.  
    12. location: <unknown location>:-1
    13.  
     
  16. aniv

    aniv

    Joined:
    Jan 19, 2011
    Posts:
    135
    As they are changing fbx export often, it is the best to open export_fbx.bin.py (and export_fbx.py if you need) that came with Blender and search for VertexColors, then delete old and paste modified code between VertexColors and UV layers comments.
    modified code:
    Code (csharp):
    1.  
    2.     # Write VertexColor Layers. [modified 18.03.2015 by ByteRockers' Games]
    3.     vcolnumber = 0
    4.     for collayer in me.vertex_colors:
    5.         if collayer.name.endswith('_ALPHA'):
    6.                 continue
    7.         vcolnumber += 1
    8.  
    9.     if vcolnumber:
    10.  
    11.         def _coltuples_gen(raw_cols, in_alpha_cols):
    12.             print(raw_cols)
    13.             return zip(*(iter(raw_cols),) * 3 + (iter(in_alpha_cols),)*1)
    14.  
    15.         t_lc = array.array(data_types.ARRAY_FLOAT64, (0.0,)) * len(me.loops) * 3
    16.         colindex = -1
    17.  
    18.         for collayer in me.vertex_colors:
    19.             if collayer.name.endswith('_ALPHA'):
    20.                 continue
    21.  
    22.             colindex += 1
    23.             alphalayer_alpha = [1.0] * len(collayer.data)
    24.             if collayer.name+'_ALPHA' in me.vertex_colors.keys():
    25.                 collayer_alpha = me.vertex_colors[collayer.name+'_ALPHA']
    26.                 for idx,colordata in enumerate(collayer_alpha.data):
    27.                     alphaValue = ( (colordata.color.r + colordata.color.g + colordata.color.b) / 3.0)
    28.                     alphalayer_alpha[idx] = alphaValue
    29.  
    30.             collayer.data.foreach_get("color", t_lc)
    31.             lay_vcol = elem_data_single_int32(geom, b"LayerElementColor", colindex)
    32.             elem_data_single_int32(lay_vcol, b"Version", FBX_GEOMETRY_VCOLOR_VERSION)
    33.             elem_data_single_string_unicode(lay_vcol, b"Name", collayer.name)
    34.             elem_data_single_string(lay_vcol, b"MappingInformationType", b"ByPolygonVertex")
    35.             elem_data_single_string(lay_vcol, b"ReferenceInformationType", b"IndexToDirect")
    36.  
    37.             col2idx = tuple(set(_coltuples_gen(t_lc,alphalayer_alpha)))
    38.             elem_data_single_float64_array(lay_vcol, b"Colors", chain(*col2idx))  # Flatten again...
    39.  
    40.             col2idx = {col: idx for idx, col in enumerate(col2idx)}
    41.             elem_data_single_int32_array(lay_vcol, b"ColorIndex", (col2idx[c] for c in _coltuples_gen(t_lc,alphalayer_alpha)))
    42.             del col2idx
    43.         del t_lc
    44.         del _coltuples_gen
    45.  
    46.         # end of modification
    47.  
    48.  
     
    dyupa, ByteRockersGames and Karearea like this.
  17. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    Thanks @aniv for sharing. I am happy for the community support to keep this fix going. I am looking forward to the day when BF will find time to address this issue and make it part of a permanent solution.
     
  18. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    Thanks for sharing your fix aniv! :)
     
  19. Project-Mysh

    Project-Mysh

    Joined:
    Nov 3, 2013
    Posts:
    223
    Hi there Blender users!
    Since this post is helping ppl with more advanced vertex coloring in blender (adding alpha) I think I should share this script I found on a website:

    https://github.com/selfsame/vcol-compositor

    This lets you edit vertex colors in separate layers and combine them. This is important while you are working in vertex bending for vegetation (working separately RGB and then combining into 1 layer). Also this script can bake AO and lightmaps into separate Vertex Color Layers, and then combine them as you like (with multiply, add etc...) with other Vcol Layers.

    Hope this helps you.
     
  20. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    That's indeed very cool, I'm really in need of this;) Works fine besides a meaningless error popping up every time when adding a layer...
     
  21. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    284
    Hey folks - I'm in the middle of building some special effects that require vertex alpha control (scrolling cloud textures on geo that fades out at one end). Eg -


    This is an incredibly cool addon from Zenchuck - just what I need - but I've run into a roadblock (using Blender v2.75).

    I've installed the vertex alpha paint addon in Blender and it seems to be working - I've made the vertex colours visible in on the material of my test cube, then switched to Paint on Alpha, filled the cube with white and then painted the bottom four vertexes of the cube with 100% black.

    My expected behaviour in Unity 5 is that the cube should fade from full alpha at the top of the cube to zero alpha (fully transparent) at the bottom.

    For exporting, I've taken the code isolated by Aniv and using Notepad++ I've overwritten the suggested sections in both my export_fbx_bin.py and export_fbx.py files in the Blender io_scene_fbx folder. I've overwritten Unity-BlenderToFBX.py in my Unity 5 editor/Data/Tools folder.

    I normally export as .fbx from Blender to Unity (usually get a little more control that way, overall less confused), but in this instance I've both exported separate .fbxs AND saved a .blend of my cube in a Models folder in my Unity VertextTestProject.

    End result in Unity 5: A perfectly serviceable cube, minus any kind of alpha vertex shading - it is totally opaque, without any sign of the expected alpha gradient.

    Does anybody have any clues on how I should take this further?

    By the way, serious kudos to Zenchuck - was seriously annoyed to find that Blender didn't support alpha vertex paint - your work has given me hope! =P

    --Rev
    noVertexAlphaGradient01001Rev.png
     
  22. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Standard shader does not handle Vertex colors by default, especially not the alphas. You'll need to modify it yourself to support that or take a look at this one:)
     
  23. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    284
    Thank you! That did the trick...!

    --Rev
     
  24. ionside

    ionside

    Joined:
    Apr 7, 2011
    Posts:
    43
    This is. Amazing. Thank you.
     
  25. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    Here is an additional fbx export script that you might find useful. This one works by defining 4 vcol layers in Blender with the suffixes _R,_G,_B,_A.

    or,


    The reason for this is that it is sometimes difficult to visualize RGB in one layer. In this scenario you would paint "0-1" "black-white" in each channel. Enjoy.

    In the export options you can select.


    Link:
    https://dl.orangedox.com/3ZaiHHNNe7cgc3ushC
     
    Last edited: Mar 18, 2016
  26. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    284
    Hey, folks - dumb question. I love using my vector colour tools - much appreciation for Zenchuck's work! - but is this something we should really be handling through shaders and masking textures? Eg. http://forum.unity3d.com/threads/alpha-mask-shader-help.181605/

    I'm really asking from a point of ignorance here - what are the advantages to doing this with vector alphas? Many thanks!

    --Rev
     
  27. ByteRockersGames

    ByteRockersGames

    Joined:
    Dec 20, 2012
    Posts:
    5
    Hi,

    It depends on what you are doing. If you like, you can use it as transparence or for something else. RGBA are just four values what are used als red, green, blue and alpha in default shaders. But they are just four values from 0-255 and you can use them for more than colors.
    You don't need a high density alpha texture if a simple fade from one vertex to an other is enough.

    Modern and easy techniques aren't always the best. They take a lot of hardware resources because they are easy to you.
    There are a lot of people that think: "we are working on PC so we have endless resources (RAM, CPU/GPU-Power)."

    Remember and learn old techniques, they can improve performance. ;-)
     
  28. Reverend-Speed

    Reverend-Speed

    Joined:
    Mar 28, 2011
    Posts:
    284
    Oh, absolutely, am very sympathetic to this point of view - and it's the basis for a lot of my own work. I'm just increasingly aware that my own fascination with older methodologies might be holding me back with newer tech, especially as even the 'weakest' platforms gain more 'grunt' as time goes on...

    Thanks for your answer, Chef, that's very helpful!

    --Rev
     
  29. MrEastwood

    MrEastwood

    Joined:
    Dec 9, 2013
    Posts:
    19
    3 years later,.... still no vertex color alpha support in Blender. I don't know if it will help, but we could leave some tokens on this issue: https://developer.blender.org/D911 But Zenchuck's method still works! However, I wanted to be able to read fbx alpha colors too, and so I decided to expand his method. And while I was at it I put it into the binary fbx exporter as well :) Attached are modified fbx scripts that are based on the versions that install with Blender 2.78c.

    Download the attached zip and put the files in ..scripts/addons/io_scene_fbx just like before. These will take care of fbx import and export.

    To edit the colors use the vertexRGBA.py from Zenchuck's original post.
     

    Attached Files:

    ktk-kumamoto and Zenchuck like this.
  30. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    Many thanks for this, I'll be trying it out. That .py script I wrote could use a good overhaul and upgrade as well. One thing that is broken is the bake AO. You can get a much better result from the bake panel in Blender ("Bake AO to Vertex color").
     
  31. Zenchuck

    Zenchuck

    Joined:
    Jun 2, 2010
    Posts:
    297
    Vertex colors are a great way to assign unique data to a material without breaking batching or instancing a material.
     
  32. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    is this addon work with blender 2.79? it baffled me why BF doesn't add this kind of basic requirement :/
     
    Flurgle likes this.
  33. Atomike

    Atomike

    Joined:
    Mar 25, 2014
    Posts:
    15
    I can confirm it partially works with Blender 2.79 using Mr. Eastwood's FBX exporters. Only problem is that you can only export ASCII FBX files. Trying to export Binary FBX models gives this error:

     
    ktk-kumamoto likes this.
  34. csiro_rmc3

    csiro_rmc3

    Joined:
    Jun 8, 2014
    Posts:
    14
    Likely the fbx exporter had been updated since MrEastwood worked on it so the other parts of the file are probably mismatched.

    I had the same error and copy/pasted the section between the vertex color modification comments into the exporter for 2.79, then hit another issue with it deleting iter_tlca before being initialised (could be an edge case for my data). I added an empty initialiser for it further up and it works for me in ASCII or Binary in 2.79 now.

    Here are the replacement files with the modified export_fbx_bin.py (line 17 after the insertion comment added ... 'iter_tlca = {}').
     
    Last edited: Mar 3, 2018
  35. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    I think i read somewhere in blender forum that they added support for vertex alpha channel in the nightly build. But not sure if it's added into the fbx io plugins
     
  36. Bonfi_96

    Bonfi_96

    Joined:
    Oct 2, 2013
    Posts:
    35
    Let me know if I get this wrong, does your script require one RGB + Alpha layer called xxxx_ALPHA?
     
    Last edited: Mar 9, 2018
  37. Bonfi_96

    Bonfi_96

    Joined:
    Oct 2, 2013
    Posts:
    35
    I'll answer myself: it does exectly what I asked, turns out I messed up something else in blender and the export was going wrong. Thanks a lot for the scripts, they are really amazing to take advantage of that extra channel! Expecially for ambient occlusion using a simple RGB*A shader!
     
  38. M0rrigan

    M0rrigan

    Joined:
    May 29, 2015
    Posts:
    29
    Hello people, I'm in the need of paint my own vertex alpha channel. I'm using Blender 2.76 - yes I know it's an old build, but I don't use Blender everyday. In this thread I read that the scripts you are using have different version. Do they work with Blender 2.76 or do I need to download the latest version?
     
  39. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    392
    Are there possibly any other free modelling packages that do support alpha painting and exporting?
    I don't really want to overwrite blender files with custom scripts.

    How are people supposed to assign which parts of vegetation should animate if they don't have maya or whatnot?
    Goddamn the unity terrain system sucks so hard ><
     
  40. Teosis

    Teosis

    Joined:
    Jan 24, 2018
    Posts:
    22
    There is also this addon for Blender which handles the vertex colour alpha but it requires a specific Blender Build to be able to export it in FBX (namely, 2.79 buildbot version)
     
  41. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    392
    Ok so another free option is to just do it in unreal and export it back...
    Unreal has a vertex paint tool with alpha, and an option to export your object back to fbx.

    Create your model in blender, rotate x axis to -90, export, import in unreal, paint vertex values and export to fbx in unreal.

    Some nice publicity for unreal if unity doesn't fix this S***..
     
  42. Atomike

    Atomike

    Joined:
    Mar 25, 2014
    Posts:
    15
    First of all, Unreal has enough publicity as it is. Second of all, this is a Blender issue, not a Unity issue. Unity supports Vertex Alpha from the get-go, but Blender does not.
     
    Seneral likes this.
  43. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    well you can paint and assign vertex color with probuilder/probrush now in 2018.2 if you want.
     
    Seneral likes this.
  44. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    392
    Yes and blender is probably the most popular modelling tool out there.
    What's your point exactly, to buy maya or 3ds max solely for the vertex alpha functionality?

    Unity can read the values, but there is no way to assign the values which is used by an incredibly important feature concering vegetation....
    Aside from that, it's not documented ANYWHERE that this functionality exists.

    Seems like a unity issue to me.
     
  45. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Assigning vertex colors is traditionally a part of the 3d modeling pipeline, which a game engine is not responsible for. As Reanimate_L said, it has been offered before through assets for quite some time (even free) and is now included with probuilder if you need it. Considering that, unity does have extensive support for 3d-modeling, if that's what you want, that's what the asset store is for.
     
  46. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    392
    Blender 2.8 has working vertex alpha values now, you can add correctly animated vegetation to your terrain now.