Skip to content

Commit 7bf172c

Browse files
committed
add vertex color support
1 parent 89ed221 commit 7bf172c

File tree

1 file changed

+55
-25
lines changed

1 file changed

+55
-25
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,30 +81,6 @@ public void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unm
8181
fbxLayer.SetNormals (fbxLayerElement);
8282
}
8383

84-
using (var fbxLayerElement = FbxLayerElementUV.Create (fbxMesh, "UVSet"))
85-
{
86-
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByPolygonVertex);
87-
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eIndexToDirect);
88-
89-
// set texture coordinates per vertex
90-
FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray ();
91-
92-
// TODO: only copy unique UVs into this array, and index appropriately
93-
for (int n = 0; n < mesh.UV.Length; n++) {
94-
fbxElementArray.Add (new FbxVector2 (mesh.UV [n] [0],
95-
mesh.UV [n] [1]));
96-
}
97-
98-
// For each face index, point to a texture uv
99-
FbxLayerElementArray fbxIndexArray = fbxLayerElement.GetIndexArray ();
100-
fbxIndexArray.SetCount (unmergedTriangles.Length);
101-
102-
for(int i = 0; i < unmergedTriangles.Length; i++){
103-
fbxIndexArray.SetAt (i, unmergedTriangles [i]);
104-
}
105-
fbxLayer.SetUVs (fbxLayerElement, FbxLayerElement.EType.eTextureDiffuse);
106-
}
107-
10884
/// Set the binormals on Layer 0.
10985
using (var fbxLayerElement = FbxLayerElementBinormal.Create (fbxMesh, "Binormals"))
11086
{
@@ -140,6 +116,60 @@ public void ExportComponentAttributes (MeshInfo mesh, FbxMesh fbxMesh, int[] unm
140116
}
141117
fbxLayer.SetTangents (fbxLayerElement);
142118
}
119+
120+
using (var fbxLayerElement = FbxLayerElementUV.Create (fbxMesh, "UVSet"))
121+
{
122+
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByPolygonVertex);
123+
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eIndexToDirect);
124+
125+
// set texture coordinates per vertex
126+
FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray ();
127+
128+
// TODO: only copy unique UVs into this array, and index appropriately
129+
for (int n = 0; n < mesh.UV.Length; n++) {
130+
fbxElementArray.Add (new FbxVector2 (mesh.UV [n] [0],
131+
mesh.UV [n] [1]));
132+
}
133+
134+
// For each face index, point to a texture uv
135+
FbxLayerElementArray fbxIndexArray = fbxLayerElement.GetIndexArray ();
136+
fbxIndexArray.SetCount (unmergedTriangles.Length);
137+
138+
for(int i = 0; i < unmergedTriangles.Length; i++){
139+
fbxIndexArray.SetAt (i, unmergedTriangles [i]);
140+
}
141+
fbxLayer.SetUVs (fbxLayerElement, FbxLayerElement.EType.eTextureDiffuse);
142+
}
143+
144+
using (var fbxLayerElement = FbxLayerElementVertexColor.Create (fbxMesh, "VertexColors"))
145+
{
146+
fbxLayerElement.SetMappingMode (FbxLayerElement.EMappingMode.eByPolygonVertex);
147+
fbxLayerElement.SetReferenceMode (FbxLayerElement.EReferenceMode.eIndexToDirect);
148+
149+
// set texture coordinates per vertex
150+
FbxLayerElementArray fbxElementArray = fbxLayerElement.GetDirectArray ();
151+
152+
// TODO: only copy unique UVs into this array, and index appropriately
153+
for (int n = 0; n < mesh.VertexColors.Length; n++) {
154+
// Converting to Color from Color32, as Color32 stores the colors
155+
// as ints between 0-255, while FbxColor and Color
156+
// use doubles between 0-1
157+
Color color = mesh.VertexColors [n];
158+
fbxElementArray.Add (new FbxColor (color.r,
159+
color.g,
160+
color.b,
161+
color.a));
162+
}
163+
164+
// For each face index, point to a texture uv
165+
FbxLayerElementArray fbxIndexArray = fbxLayerElement.GetIndexArray ();
166+
fbxIndexArray.SetCount (unmergedTriangles.Length);
167+
168+
for(int i = 0; i < unmergedTriangles.Length; i++){
169+
fbxIndexArray.SetAt (i, unmergedTriangles [i]);
170+
}
171+
fbxLayer.SetVertexColors (fbxLayerElement);
172+
}
143173
}
144174

145175
/// <summary>
@@ -652,7 +682,7 @@ public Vector3 [] Binormals
652682
/// TODO: Gets the tangents for the vertices.
653683
/// </summary>
654684
/// <value>The tangents.</value>
655-
public Color [] VertexColors { get { return mesh.colors; } }
685+
public Color32 [] VertexColors { get { return mesh.colors32; } }
656686

657687
/// <summary>
658688
/// Gets the uvs.

0 commit comments

Comments
 (0)