public static UniGLTF.Extensions.VRMC_vrm.Expression ToGltf(this VrmLib.Expression x, List <VrmLib.Node> nodes, List <VrmLib.Material> materials) { var g = new UniGLTF.Extensions.VRMC_vrm.Expression { Preset = (UniGLTF.Extensions.VRMC_vrm.ExpressionPreset)x.Preset, Name = x.Name, IsBinary = x.IsBinary, IgnoreBlink = x.IgnoreBlink, IgnoreLookAt = x.IgnoreLookAt, IgnoreMouth = x.IgnoreMouth, }; foreach (var blendShapeBind in x.MorphTargetBinds) { g.MorphTargetBinds.Add(blendShapeBind.ToGltf(nodes)); } foreach (var materialColorBind in x.MaterialColorBinds) { g.MaterialColorBinds.Add(materialColorBind.ToGltf(materials)); } foreach (var materialUVBind in x.TextureTransformBinds) { g.TextureTransformBinds.Add(materialUVBind.ToGltf(materials)); } return(g); }
public static UniGLTF.Extensions.VRMC_vrm.Expression ToGltf(this VrmLib.Expression x, List <VrmLib.Node> nodes, List <VrmLib.Material> materials) { var g = new UniGLTF.Extensions.VRMC_vrm.Expression { Preset = x.Preset.ToGltfFormat(), Name = x.Name, IsBinary = x.IsBinary, OverrideBlink = EnumUtil.Cast <UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType>(x.OverrideBlink), OverrideLookAt = EnumUtil.Cast <UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType>(x.OverrideLookAt), OverrideMouth = EnumUtil.Cast <UniGLTF.Extensions.VRMC_vrm.ExpressionOverrideType>(x.OverrideMouth), }; g.MorphTargetBinds = new List <UniGLTF.Extensions.VRMC_vrm.MorphTargetBind>(); foreach (var blendShapeBind in x.MorphTargetBinds) { g.MorphTargetBinds.Add(blendShapeBind.ToGltf(nodes)); } g.MaterialColorBinds = new List <UniGLTF.Extensions.VRMC_vrm.MaterialColorBind>(); foreach (var materialColorBind in x.MaterialColorBinds) { g.MaterialColorBinds.Add(materialColorBind.ToGltf(materials)); } g.TextureTransformBinds = new List <UniGLTF.Extensions.VRMC_vrm.TextureTransformBind>(); foreach (var materialUVBind in x.TextureTransformBinds) { g.TextureTransformBinds.Add(materialUVBind.ToGltf(materials)); } return(g); }
public static VrmLib.Expression FromGltf(this UniGLTF.Extensions.VRMC_vrm.Expression x, List <VrmLib.Node> nodes, List <VrmLib.Material> materials) { var expression = new VrmLib.Expression((VrmLib.ExpressionPreset)x.Preset, x.Name, x.IsBinary.HasValue && x.IsBinary.Value) { IgnoreBlink = x.IgnoreBlink.GetValueOrDefault(), IgnoreLookAt = x.IgnoreLookAt.GetValueOrDefault(), IgnoreMouth = x.IgnoreMouth.GetValueOrDefault(), }; if (x.MorphTargetBinds != null) { foreach (var y in x.MorphTargetBinds) { var node = nodes[y.Node.Value]; var blendShapeName = node.Mesh.MorphTargets[y.Index.Value].Name; var blendShapeBind = new MorphTargetBind(node, blendShapeName, y.Weight.Value); expression.MorphTargetBinds.Add(blendShapeBind); } } if (x.MaterialColorBinds != null) { foreach (var y in x.MaterialColorBinds) { var material = materials[y.Material.Value]; var materialColorBind = new MaterialColorBind(material, EnumUtil.Cast <MaterialBindType>(y.Type), y.TargetValue.ToVector4(Vector4.Zero)); expression.MaterialColorBinds.Add(materialColorBind); } } if (x.TextureTransformBinds != null) { foreach (var y in x.TextureTransformBinds) { var material = materials[y.Material.Value]; var materialUVBind = new TextureTransformBind(material, y.Scaling.ToVector2(Vector2.One), y.Offset.ToVector2(Vector2.Zero)); expression.TextureTransformBinds.Add(materialUVBind); } } return(expression); }
/// <summary> /// VRM-0.X の MaterialBindValue を VRM-1.0 仕様に変換する /// /// * Property名 => enum MaterialBindType /// * 特に _MainTex_ST の場合、MaterialBindType.UvScale + MaterialBindType.UvScale 2つになりうる /// /// </summary> VrmLib.Expression ToVrmLib(VRM10Expression clip, GameObject root) { var expression = new VrmLib.Expression(clip.Preset, clip.ExpressionName, clip.IsBinary); expression.OverrideBlink = EnumUtil.Cast <VrmLib.ExpressionOverrideType>(clip.OverrideBlink); expression.OverrideLookAt = EnumUtil.Cast <VrmLib.ExpressionOverrideType>(clip.OverrideLookAt); expression.OverrideMouth = EnumUtil.Cast <VrmLib.ExpressionOverrideType>(clip.OverrideMouth); foreach (var binding in clip.MorphTargetBindings) { var transform = GetTransformFromRelativePath(root.transform, binding.RelativePath); if (transform == null) { continue; } var renderer = transform.gameObject.GetComponent <SkinnedMeshRenderer>(); if (renderer == null) { continue; } var mesh = renderer.sharedMesh; if (mesh == null) { continue; } var names = new List <string>(); for (int i = 0; i < mesh.blendShapeCount; ++i) { names.Add(mesh.GetBlendShapeName(i)); } var node = Nodes[transform.gameObject]; var blendShapeValue = new VrmLib.MorphTargetBind( node, names[binding.Index], // Unity Range [0-100] to VRM-1.0 Range [0-1.0] binding.Weight * 0.01f ); expression.MorphTargetBinds.Add(blendShapeValue); } foreach (var binding in clip.MaterialColorBindings) { var materialPair = Materials.FirstOrDefault(x => x.Key.name == binding.MaterialName); if (materialPair.Value != null) { var bind = new VrmLib.MaterialColorBind( materialPair.Value, binding.BindType, binding.TargetValue.ToNumericsVector4() ); expression.MaterialColorBinds.Add(bind); } } foreach (var binding in clip.MaterialUVBindings) { var materialPair = Materials.FirstOrDefault(x => x.Key.name == binding.MaterialName); if (materialPair.Value != null) { var bind = new VrmLib.TextureTransformBind( materialPair.Value, binding.Scaling.ToNumericsVector2(), binding.Offset.ToNumericsVector2() ); expression.TextureTransformBinds.Add(bind); } } return(expression); }