Skip to content

Commit

Permalink
Fix exporter logic that getting node.mesh index #140
Browse files Browse the repository at this point in the history
  • Loading branch information
ousttrue committed Jan 26, 2019
1 parent dc1e1da commit 1ccef78
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 12 additions & 2 deletions Assets/VRM/UniGLTF/Editor/UniGLTFTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ public void SameMeshButDifferentMaterialExport()
{
cubeA.transform.SetParent(go.transform);
var material = new Material(shader);
material.name = "red";
material.color = Color.red;
cubeA.GetComponent<Renderer>().sharedMaterial = material;
}
Expand All @@ -596,6 +597,7 @@ public void SameMeshButDifferentMaterialExport()
cubeB.transform.SetParent(go.transform);
var material = new Material(shader);
material.color = Color.blue;
material.name = "blue";
cubeB.GetComponent<Renderer>().sharedMaterial = material;

Assert.AreEqual(cubeB.GetComponent<MeshFilter>().sharedMesh, cubeA.GetComponent<MeshFilter>().sharedMesh);
Expand All @@ -620,17 +622,25 @@ public void SameMeshButDifferentMaterialExport()
var blue = gltf.materials[gltf.meshes[1].primitives[0].material];
Assert.AreEqual(new float[] { 0, 0, 1, 1 }, blue.pbrMetallicRoughness.baseColorFactor);

Assert.AreEqual(2, gltf.nodes.Count);

Assert.AreNotEqual(gltf.nodes[0].mesh, gltf.nodes[1].mesh);

// import
var context = new ImporterContext();
context.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
//Debug.LogFormat("{0}", context.Json);
context.Load();

var importedRed = context.Root.transform.GetChild(0);
Assert.AreEqual(Color.red, importedRed.GetComponent<Renderer>().sharedMaterial.color);
var importedRedMaterial = importedRed.GetComponent<Renderer>().sharedMaterial;
Assert.AreEqual("red", importedRedMaterial.name);
Assert.AreEqual(Color.red, importedRedMaterial.color);

var importedBlue = context.Root.transform.GetChild(1);
Assert.AreEqual(Color.blue, importedBlue.GetComponent<Renderer>().sharedMaterial.color);
var importedBlueMaterial = importedBlue.GetComponent<Renderer>().sharedMaterial;
Assert.AreEqual("blue", importedBlueMaterial.name);
Assert.AreEqual(Color.blue, importedBlueMaterial.color);
}
finally
{
Expand Down
14 changes: 7 additions & 7 deletions Assets/VRM/UniGLTF/Scripts/IO/gltfExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void Dispose()
}

#region Export
static glTFNode ExportNode(Transform x, List<Transform> nodes, List<Mesh> meshes, List<SkinnedMeshRenderer> skins)
static glTFNode ExportNode(Transform x, List<Transform> nodes, List<Renderer> renderers, List<SkinnedMeshRenderer> skins)
{
var node = new glTFNode
{
Expand All @@ -164,16 +164,16 @@ static glTFNode ExportNode(Transform x, List<Transform> nodes, List<Mesh> meshes

if (x.gameObject.activeInHierarchy)
{
var meshFilter = x.GetComponent<MeshFilter>();
if (meshFilter != null)
var meshRenderer = x.GetComponent<MeshRenderer>();
if (meshRenderer != null)
{
node.mesh = meshes.IndexOf(meshFilter.sharedMesh);
node.mesh = renderers.IndexOf(meshRenderer);
}

var skinnredMeshRenderer = x.GetComponent<SkinnedMeshRenderer>();
if (skinnredMeshRenderer != null)
{
node.mesh = meshes.IndexOf(skinnredMeshRenderer.sharedMesh);
node.mesh = renderers.IndexOf(skinnredMeshRenderer);
node.skin = skins.IndexOf(skinnredMeshRenderer);
}
}
Expand Down Expand Up @@ -244,14 +244,14 @@ void FromGameObject(glTF gltf, GameObject go, bool useSparseAccessorForMorphTarg
Meshes = unityMeshes.Select(x => x.Mesh).ToList();
#endregion

#region Skins
#region Nodes and Skins
var unitySkins = Nodes
.Select(x => x.GetComponent<SkinnedMeshRenderer>()).Where(x =>
x != null
&& x.bones != null
&& x.bones.Length > 0)
.ToList();
gltf.nodes = Nodes.Select(x => ExportNode(x, Nodes, unityMeshes.Select(y => y.Mesh).ToList(), unitySkins)).ToList();
gltf.nodes = Nodes.Select(x => ExportNode(x, Nodes, unityMeshes.Select(y => y.Rendererer).ToList(), unitySkins)).ToList();
gltf.scenes = new List<gltfScene>
{
new gltfScene
Expand Down

0 comments on commit 1ccef78

Please sign in to comment.