Skip to content

Commit

Permalink
Merge pull request #428 from PoChang007/OptionForVertexColorRemoval
Browse files Browse the repository at this point in the history
Add an option to remove vertex color in export menu
  • Loading branch information
ousttrue authored Jun 9, 2020
2 parents 287ccf3 + 6cbe468 commit df0436d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 9 deletions.
13 changes: 9 additions & 4 deletions Assets/VRM/UniGLTF/Scripts/IO/MeshExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public static class MeshExporter
static glTFMesh ExportPrimitives(glTF gltf, int bufferIndex,
string rendererName,
Mesh mesh, Material[] materials,
List<Material> unityMaterials)
List<Material> unityMaterials,
bool removeVertexColor)
{
var positions = mesh.vertices.Select(y => y.ReverseZ()).ToArray();
var positionAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, positions, glBufferTarget.ARRAY_BUFFER);
Expand All @@ -30,7 +31,10 @@ static glTFMesh ExportPrimitives(glTF gltf, int bufferIndex,
var tangentAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.tangents.Select(y => y.ReverseZ()).ToArray(), glBufferTarget.ARRAY_BUFFER);
#endif
var uvAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.uv.Select(y => y.ReverseUV()).ToArray(), glBufferTarget.ARRAY_BUFFER);
var colorAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.colors, glBufferTarget.ARRAY_BUFFER);

var colorAccessorIndex = -1;
if (!removeVertexColor)
colorAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, mesh.colors, glBufferTarget.ARRAY_BUFFER);

var boneweights = mesh.boneWeights;
var weightAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, boneweights.Select(y => new Vector4(y.weight0, y.weight1, y.weight2, y.weight3)).ToArray(), glBufferTarget.ARRAY_BUFFER);
Expand Down Expand Up @@ -231,7 +235,8 @@ static gltfMorphTarget ExportMorphTarget(glTF gltf, int bufferIndex,
public static void ExportMeshes(glTF gltf, int bufferIndex,
List<MeshWithRenderer> unityMeshes, List<Material> unityMaterials,
bool useSparseAccessorForMorphTarget,
bool exportOnlyBlendShapePosition)
bool exportOnlyBlendShapePosition,
bool removeVertexColor)
{
for (int i = 0; i < unityMeshes.Count; ++i)
{
Expand All @@ -241,7 +246,7 @@ public static void ExportMeshes(glTF gltf, int bufferIndex,

var gltfMesh = ExportPrimitives(gltf, bufferIndex,
x.Renderer.name,
mesh, materials, unityMaterials);
mesh, materials, unityMaterials, removeVertexColor);

for (int j = 0; j < mesh.blendShapeCount; ++j)
{
Expand Down
14 changes: 11 additions & 3 deletions Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public bool ExportOnlyBlendShapePosition
set;
}

public bool RemoveVertexColor
{
get;
set;
}

public GameObject Copy
{
get;
Expand Down Expand Up @@ -142,7 +148,7 @@ public virtual void Prepare(GameObject go)

public void Export()
{
FromGameObject(glTF, Copy, UseSparseAccessorForBlendShape);
FromGameObject(glTF, Copy, UseSparseAccessorForBlendShape, RemoveVertexColor);
}

public void Dispose()
Expand Down Expand Up @@ -188,7 +194,8 @@ static glTFNode ExportNode(Transform x, List<Transform> nodes, List<Renderer> re
return node;
}

void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarget = false)
void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarget = false,
bool removeVertexColor = false)
{
var bytesBuffer = new ArrayByteBuffer(new byte[50 * 1024 * 1024]);
var bufferIndex = gltf.AddBuffer(bytesBuffer);
Expand Down Expand Up @@ -247,7 +254,8 @@ void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarg
return true;
})
.ToList();
MeshExporter.ExportMeshes(gltf, bufferIndex, unityMeshes, Materials, useSparseAccessorForMorphTarget, ExportOnlyBlendShapePosition);
MeshExporter.ExportMeshes(gltf, bufferIndex, unityMeshes, Materials, useSparseAccessorForMorphTarget,
ExportOnlyBlendShapePosition, removeVertexColor);
Meshes = unityMeshes.Select(x => x.Mesh).ToList();
#endregion

Expand Down
3 changes: 2 additions & 1 deletion Assets/VRM/UniVRM/Editor/Format/VRMEditorExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ static void Export(string path, VRMExportSettings settings, List<GameObject> des
var vrm = VRMExporter.Export(target, new VRMExporterConfiguration
{
UseSparseAccessorForBlendShape = settings.UseSparseAccessor,
ExportOnlyBlendShapePosition = settings.OnlyBlendshapePosition
ExportOnlyBlendShapePosition = settings.OnlyBlendshapePosition,
RemoveVertexColor = settings.RemoveVertexColor
});
vrm.extensions.VRM.meta.title = settings.Title;
vrm.extensions.VRM.meta.version = settings.Version;
Expand Down
12 changes: 12 additions & 0 deletions Assets/VRM/UniVRM/Editor/Format/VRMExportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public class VRMExportSettings
/// </summary>
[Tooltip("Remove blendShapeClip that preset is Unknown")]
public bool ReduceBlendshapeClip = false;

/// <summary>
/// 頂点カラーを削除する
/// </summary>
[Tooltip("Remove vertex color")]
public bool RemoveVertexColor = false;
#endregion

public struct Validation
Expand Down Expand Up @@ -186,6 +192,12 @@ public IEnumerable<Validation> Validate()
{
yield return Validation.Error("ReduceBlendshapeSize is need VRMBlendShapeProxy, you need to convert to VRM once.");
}

var vertexColor = Source.GetComponentsInChildren<SkinnedMeshRenderer>().Any(x => x.sharedMesh.colors.Length > 0);
if (vertexColor)
{
yield return Validation.Warning("This model contains vertex color");
}

var renderers = Source.GetComponentsInChildren<Renderer>();
if (renderers.All(x => !x.gameObject.activeInHierarchy))
Expand Down
3 changes: 2 additions & 1 deletion Assets/VRM/UniVRM/Scripts/Format/VRMExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public static glTF Export(GameObject go, VRMExporterConfiguration configuration)
using (var exporter = new VRMExporter(gltf)
{
UseSparseAccessorForBlendShape = configuration.UseSparseAccessorForBlendShape,
ExportOnlyBlendShapePosition = configuration.ExportOnlyBlendShapePosition
ExportOnlyBlendShapePosition = configuration.ExportOnlyBlendShapePosition,
RemoveVertexColor = configuration.RemoveVertexColor,
})
{
_Export(gltf, exporter, go);
Expand Down
2 changes: 2 additions & 0 deletions Assets/VRM/UniVRM/Scripts/Format/VRMExporterConfiguation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ public struct VRMExporterConfiguration
{
public bool UseSparseAccessorForBlendShape;
public bool ExportOnlyBlendShapePosition;
public bool RemoveVertexColor;

public static VRMExporterConfiguration Default => new VRMExporterConfiguration
{
UseSparseAccessorForBlendShape = true,
ExportOnlyBlendShapePosition = false,
RemoveVertexColor = false,
};
}
}

0 comments on commit df0436d

Please sign in to comment.