MigrateExpression

This commit is contained in:
ousttrue 2021-01-26 18:18:07 +09:00
parent b2fd1dc8ce
commit 24e16077b5
3 changed files with 185 additions and 3 deletions

View File

@ -28,6 +28,19 @@ namespace UniJSON
return self.Children.Count() / 2;
}
public static string GetObjectValueOrDefault<T>(this ListTreeNode<T> self, String key, string defualtValue)
where T : IListTreeItem, IValue<T>
{
try
{
return self[key].GetString();
}
catch (KeyNotFoundException)
{
return defualtValue;
}
}
public static ListTreeNode<T> GetObjectItem<T>(this ListTreeNode<T> self, String key)
where T : IListTreeItem, IValue<T>
{

View File

@ -39,8 +39,14 @@ namespace UniVRM10
AvatarPermission = ToAvaterPermission(self),
RedistributionLicense = ToRedistributionLicense(self),
};
meta.References.AddRange(self.References);
meta.Authors.AddRange(self.Authors);
if (self.References != null)
{
meta.References.AddRange(self.References);
}
if (self.Authors != null)
{
meta.Authors.AddRange(self.Authors);
}
if (self.ThumbnailImage.HasValue)
{
var texture = textures[self.ThumbnailImage.Value] as ImageTexture;

View File

@ -377,9 +377,10 @@ namespace UniVRM10
// },
foreach (var y in x["bones"].ArrayItems())
{
var comment = x.GetObjectValueOrDefault("comment", "");
var spring = new UniGLTF.Extensions.VRMC_springBone.Spring
{
Name = x["comment"].GetString(),
Name = comment,
Colliders = x["colliderGroups"].ArrayItems().Select(z => colliderNodes[z.GetInt32()]).ToArray(),
Joints = new List<UniGLTF.Extensions.VRMC_springBone.SpringBoneJoint>(),
};
@ -404,6 +405,167 @@ namespace UniVRM10
return springBone;
}
static UniGLTF.Extensions.VRMC_vrm.ExpressionPreset ToPreset(ListTreeNode<JsonValue> json)
{
switch (json.GetString().ToLower())
{
case "unknown": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.custom;
// https://github.com/vrm-c/vrm-specification/issues/185
case "neutral": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.neutral;
case "a": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.aa;
case "i": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.ih;
case "u": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.ou;
case "e": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.ee;
case "o": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.oh;
case "blink": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.blink;
case "blink_l": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.blinkLeft;
case "blink_r": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.blinkRight;
// https://github.com/vrm-c/vrm-specification/issues/163
case "joy": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.happy;
case "angry": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.angry;
case "sorrow": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.sad;
case "fun": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.relaxed;
case "lookup": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.lookUp;
case "lookdown": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.lookDown;
case "lookleft": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.lookLeft;
case "lookright": return UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.lookRight;
}
throw new NotImplementedException();
}
static IEnumerable<UniGLTF.Extensions.VRMC_vrm.MorphTargetBind> ToMorphTargetBinds(UniGLTF.glTF gltf, ListTreeNode<JsonValue> json)
{
foreach (var x in json.ArrayItems())
{
var meshIndex = x["mesh"].GetInt32();
var morphTargetIndex = x["index"].GetInt32();
var weight = x["weight"].GetInt32() * 0.01f;
var bind = new UniGLTF.Extensions.VRMC_vrm.MorphTargetBind();
// https://github.com/vrm-c/vrm-specification/pull/106
// https://github.com/vrm-c/vrm-specification/pull/153
bind.Node = gltf.nodes.IndexOf(gltf.nodes.First(y => y.mesh == meshIndex));
bind.Index = morphTargetIndex;
// https://github.com/vrm-c/vrm-specification/issues/209
bind.Weight = weight * 0.01f;
yield return bind;
}
}
public const string COLOR_PROPERTY = "_Color";
public const string EMISSION_COLOR_PROPERTY = "_EmissionColor";
public const string RIM_COLOR_PROPERTY = "_RimColor";
public const string OUTLINE_COLOR_PROPERTY = "_OutlineColor";
public const string SHADE_COLOR_PROPERTY = "_ShadeColor";
static UniGLTF.Extensions.VRMC_vrm.MaterialColorType ToMaterialType(string src)
{
switch (src)
{
case COLOR_PROPERTY:
return UniGLTF.Extensions.VRMC_vrm.MaterialColorType.color;
case EMISSION_COLOR_PROPERTY:
return UniGLTF.Extensions.VRMC_vrm.MaterialColorType.emissionColor;
case RIM_COLOR_PROPERTY:
return UniGLTF.Extensions.VRMC_vrm.MaterialColorType.rimColor;
case SHADE_COLOR_PROPERTY:
return UniGLTF.Extensions.VRMC_vrm.MaterialColorType.shadeColor;
case OUTLINE_COLOR_PROPERTY:
return UniGLTF.Extensions.VRMC_vrm.MaterialColorType.outlineColor;
}
throw new NotImplementedException();
}
/// <summary>
/// MaterialValue の仕様変更
///
/// * MaterialColorBind
/// * TextureTransformBind
///
/// の2種類になった。
///
/// </summary>
/// <param name="gltf"></param>
/// <param name="json"></param>
/// <param name="expression"></param>
static void ToMaterialColorBinds(UniGLTF.glTF gltf, ListTreeNode<JsonValue> json, UniGLTF.Extensions.VRMC_vrm.Expression expression)
{
foreach (var x in json.ArrayItems())
{
var materialName = x["materialName"].GetString();
var materialIndex = gltf.materials.IndexOf(gltf.materials.First(y => y.name == materialName));
var propertyName = x["propertyName"].GetString();
var targetValue = x["targetValue"].ArrayItems().Select(y => y.GetSingle()).ToArray();
if (propertyName.EndsWith("_ST"))
{
expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
{
Material = materialIndex,
Scaling = new float[] { targetValue[0], targetValue[1] },
Offset = new float[] { targetValue[2], targetValue[3] }
});
}
else if (propertyName.EndsWith("_ST_S"))
{
expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
{
Material = materialIndex,
Scaling = new float[] { targetValue[0], 1 },
Offset = new float[] { targetValue[2], 0 }
});
}
else if (propertyName.EndsWith("_ST_T"))
{
expression.TextureTransformBinds.Add(new UniGLTF.Extensions.VRMC_vrm.TextureTransformBind
{
Material = materialIndex,
Scaling = new float[] { 1, targetValue[1] },
Offset = new float[] { 0, targetValue[3] }
});
}
else
{
// color
expression.MaterialColorBinds.Add(new UniGLTF.Extensions.VRMC_vrm.MaterialColorBind
{
Material = materialIndex,
Type = ToMaterialType(propertyName),
TargetValue = targetValue,
});
}
}
}
static IEnumerable<UniGLTF.Extensions.VRMC_vrm.Expression> MigrateExpression(UniGLTF.glTF gltf, ListTreeNode<JsonValue> json)
{
foreach (var blendShapeClip in json["blendShapeGroups"].ArrayItems())
{
var name = blendShapeClip["name"].GetString();
var expression = new UniGLTF.Extensions.VRMC_vrm.Expression
{
Name = name,
Preset = ToPreset(blendShapeClip["presetName"]),
IsBinary = blendShapeClip["isBinary"].GetBoolean(),
};
expression.MorphTargetBinds = ToMorphTargetBinds(gltf, blendShapeClip["binds"]).ToList();
ToMaterialColorBinds(gltf, blendShapeClip["materialValues"], expression);
yield return expression;
}
}
public static byte[] Migrate(byte[] src)
{
var glb = UniGLTF.Glb.Parse(src);
@ -419,6 +581,7 @@ namespace UniVRM10
var vrm1 = new UniGLTF.Extensions.VRMC_vrm.VRMC_vrm();
vrm1.Meta = MigrateMeta(vrm0["meta"]);
vrm1.Humanoid = MigrateHumanoid(vrm0["humanoid"]);
vrm1.Expressions = MigrateExpression(gltf, vrm0["blendShapeMaster"]).ToList();
var f = new JsonFormatter();
UniGLTF.Extensions.VRMC_vrm.GltfSerializer.Serialize(f, vrm1);