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.
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.”
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?
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
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
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
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 ===========================
for all those who like to use vertex alpha with Blender 2.74 with FBX binary export and Unity, you’ll find a complete package of all file from this thread and added vertex alpha for FBX binary export.
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:
# Write VertexColor Layers. [modified 18.03.2015 by ByteRockers' Games]
vcolnumber = 0
for collayer in me.vertex_colors:
if collayer.name.endswith('_ALPHA'):
continue
vcolnumber += 1
if vcolnumber:
def _coltuples_gen(raw_cols, in_alpha_cols):
print(raw_cols)
return zip(*(iter(raw_cols),) * 3 + (iter(in_alpha_cols),)*1)
t_lc = array.array(data_types.ARRAY_FLOAT64, (0.0,)) * len(me.loops) * 3
colindex = -1
for collayer in me.vertex_colors:
if collayer.name.endswith('_ALPHA'):
continue
colindex += 1
alphalayer_alpha = [1.0] * len(collayer.data)
if collayer.name+'_ALPHA' in me.vertex_colors.keys():
collayer_alpha = me.vertex_colors[collayer.name+'_ALPHA']
for idx,colordata in enumerate(collayer_alpha.data):
alphaValue = ( (colordata.color.r + colordata.color.g + colordata.color.b) / 3.0)
alphalayer_alpha[idx] = alphaValue
collayer.data.foreach_get("color", t_lc)
lay_vcol = elem_data_single_int32(geom, b"LayerElementColor", colindex)
elem_data_single_int32(lay_vcol, b"Version", FBX_GEOMETRY_VCOLOR_VERSION)
elem_data_single_string_unicode(lay_vcol, b"Name", collayer.name)
elem_data_single_string(lay_vcol, b"MappingInformationType", b"ByPolygonVertex")
elem_data_single_string(lay_vcol, b"ReferenceInformationType", b"IndexToDirect")
col2idx = tuple(set(_coltuples_gen(t_lc,alphalayer_alpha)))
elem_data_single_float64_array(lay_vcol, b"Colors", chain(*col2idx)) # Flatten again...
col2idx = {col: idx for idx, col in enumerate(col2idx)}
elem_data_single_int32_array(lay_vcol, b"ColorIndex", (col2idx[c] for c in _coltuples_gen(t_lc,alphalayer_alpha)))
del col2idx
del t_lc
del _coltuples_gen
# end of modification
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.
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:
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.