Skip to content

Commit

Permalink
Merge pull request #2449 from ousttrue/fix/export_index_of
Browse files Browse the repository at this point in the history
Fix/export index of
  • Loading branch information
ousttrue authored Sep 26, 2024
2 parents a3aeafe + 90aa903 commit 0e9020a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 22 deletions.
13 changes: 8 additions & 5 deletions Assets/VRM/Runtime/IO/VRMExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,14 @@ public override void ExportExtensions(ITextureSerializer textureSerializer)
{
VRM.firstPerson.firstPersonBone = Nodes.IndexOf(firstPerson.FirstPersonBone);
VRM.firstPerson.firstPersonBoneOffset = firstPerson.FirstPersonOffset;
VRM.firstPerson.meshAnnotations = firstPerson.Renderers.Select(x => new glTF_VRM_MeshAnnotation
{
mesh = Meshes.IndexOf(x.SharedMesh),
firstPersonFlag = x.FirstPersonFlag.ToString(),
}).ToList();
VRM.firstPerson.meshAnnotations = firstPerson.Renderers
.Select(x => new glTF_VRM_MeshAnnotation
{
mesh = Meshes.IndexOf(x.SharedMesh),
firstPersonFlag = x.FirstPersonFlag.ToString(),
})
.Where(x => x.mesh != -1)
.ToList();
}

// lookAt
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/Runtime/IO/VRMImporterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ async Task<BlendShapeClip> LoadBlendShapeBind(glTF_VRM_BlendShapeGroup group, Di
// fallback
asset.Preset = CachedEnum.ParseOrDefault<BlendShapePreset>(group.name, true);
}
asset.Values = group.binds.Select(x =>
asset.Values = group.binds.Where(x => x.mesh >= 0 && x.mesh < Meshes.Count).Select(x =>
{
var mesh = Meshes[x.mesh].Mesh;
var node = transformMeshTable[mesh];
Expand Down
22 changes: 16 additions & 6 deletions Assets/VRM10/Runtime/IO/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,23 @@ namespace UniVRM10
{
public static class ExpressionExtensions
{
public static UniVRM10.MorphTargetBinding Build10(this MorphTargetBind bind, GameObject root, Vrm10Importer.ModelMap loader, VrmLib.Model model)
public static MorphTargetBinding? Build10(this MorphTargetBind bind, GameObject root, Vrm10Importer.ModelMap loader, VrmLib.Model model)
{
var libNode = model.Nodes[bind.Node.Value];
var node = loader.Nodes[libNode].transform;
var mesh = loader.Meshes[libNode.MeshGroup];
var relativePath = node.RelativePathFrom(root.transform);
return new UniVRM10.MorphTargetBinding(relativePath, bind.Index.Value, bind.Weight.Value);
if (bind.Node.TryGetValidIndex(model.Nodes.Count, out var nodeIndex))
{
var libNode = model.Nodes[nodeIndex];
if (libNode.MeshGroup == null)
{
return default;
}
var node = loader.Nodes[libNode].transform;
var relativePath = node.RelativePathFrom(root.transform);
return new MorphTargetBinding(relativePath, bind.Index.Value, bind.Weight.Value);
}
else
{
return default;
}
}

public static UniVRM10.MaterialColorBinding? Build10(this MaterialColorBind bind, IReadOnlyList<MaterialFactory.MaterialLoadInfo> materials)
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM10/Runtime/IO/Model/ModelExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ VrmLib.Model _Export(INativeArrayManager arrayManager, GameObject root)
}

// material and textures
var rendererComponents = root.GetComponentsInChildren<Renderer>();
var rendererComponents = root.GetComponentsInChildren<Renderer>().Where(x => x.gameObject.activeInHierarchy && x.enabled).ToArray();
{
foreach (var renderer in rendererComponents)
{
Expand Down
26 changes: 24 additions & 2 deletions Assets/VRM10/Runtime/IO/Vrm10Exporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,13 @@ void ExportFirstPerson(UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm, Vrm10Instance v
};
foreach (var f in firstPerson.Renderers)
{
vrm.FirstPerson.MeshAnnotations.Add(ExportMeshAnnotation(f, vrmController.transform, getIndex));
var annotation = ExportMeshAnnotation(f, vrmController.transform, getIndex);
if (annotation.Node < 0)
{
// maybe disabled Rendeer
continue;
}
vrm.FirstPerson.MeshAnnotations.Add(annotation);
}
}

Expand Down Expand Up @@ -718,6 +724,15 @@ static UniGLTF.Extensions.VRMC_vrm.Expression ExportExpression(VRM10Expression e
Func<string, int> getIndexFromRelativePath = relativePath =>
{
var rendererNode = vrmController.transform.GetFromPath(relativePath);
var renderer = rendererNode.GetComponent<Renderer>();
if (renderer == null)
{
return -1;
}
if (!renderer.enabled)
{
return -1;
}
var node = converter.Nodes[rendererNode.gameObject];
return model.Nodes.IndexOf(node);
};
Expand Down Expand Up @@ -751,7 +766,14 @@ static UniGLTF.Extensions.VRMC_vrm.Expression ExportExpression(VRM10Expression e
{
try
{
vrmExpression.MorphTargetBinds.Add(ExportMorphTargetBinding(b, getIndexFromRelativePath));
var binding = ExportMorphTargetBinding(b, getIndexFromRelativePath);
if (binding.Node < 0)
{
// node もしくは renderer が存在しない
continue;
}

vrmExpression.MorphTargetBinds.Add(binding);
}
catch (Exception ex)
{
Expand Down
20 changes: 13 additions & 7 deletions Assets/VRM10/Runtime/IO/Vrm10Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ VRM10Expression GetOrLoadExpression(in SubAssetKey key, ExpressionPreset preset,

if (expression.MorphTargetBinds != null)
{
clip.MorphTargetBindings = expression.MorphTargetBinds?.Select(x => x.Build10(Root, m_map, m_model))
clip.MorphTargetBindings = expression.MorphTargetBinds?
.Select(x => x.Build10(Root, m_map, m_model))
.Where(x => x.HasValue)
.Select(x => x.Value)
.ToArray();
}
else
Expand Down Expand Up @@ -492,13 +495,16 @@ async Task<VRM10Object> LoadVrmAsync(IAwaitCaller awaitCaller, UniGLTF.Extension
var fp = vrmExtension.FirstPerson;
foreach (var x in fp.MeshAnnotations)
{
var node = Nodes[x.Node.Value];
var relative = node.RelativePathFrom(Root.transform);
vrm.FirstPerson.Renderers.Add(new RendererFirstPersonFlags
if (x.Node.TryGetValidIndex(Nodes.Count, out var index))
{
FirstPersonFlag = x.Type,
Renderer = relative,
});
var node = Nodes[x.Node.Value];
var relative = node.RelativePathFrom(Root.transform);
vrm.FirstPerson.Renderers.Add(new RendererFirstPersonFlags
{
FirstPersonFlag = x.Type,
Renderer = relative,
});
}
}
}
else
Expand Down

0 comments on commit 0e9020a

Please sign in to comment.