SkinningBake

This commit is contained in:
ousttrue
2021-04-22 20:50:17 +09:00
parent 5beba7c647
commit 81a4b1ffea
5 changed files with 102 additions and 10 deletions

View File

@@ -11,7 +11,7 @@ namespace UniVRM10
{
const string MENU_KEY = VRMVersion.MENU + "/SpringBone Window";
[MenuItem(MENU_KEY, false, 0)]
[MenuItem(MENU_KEY, false, 1)]
private static void ExportFromMenu()
{
var window = (VRM10SpringSelectorWindow)GetWindow(typeof(VRM10SpringSelectorWindow));

View File

@@ -14,7 +14,7 @@ namespace UniVRM10
{
const string CONVERT_HUMANOID_KEY = VRMVersion.MENU + "/Export VRM-1.0";
[MenuItem(CONVERT_HUMANOID_KEY, false, 1)]
[MenuItem(CONVERT_HUMANOID_KEY, false, 0)]
private static void ExportFromMenu()
{
var window = (VRM10ExportDialog)GetWindow(typeof(VRM10ExportDialog));
@@ -284,13 +284,13 @@ namespace UniVRM10
var converter = new UniVRM10.RuntimeVrmConverter();
var model = converter.ToModelFrom10(root);
// if (MeshUtility.Validators.HumanoidValidator.HasRotationOrScale(root))
// {
// // 正規化
// m_logLabel += $"normalize...\n";
// var modifier = new ModelModifier(model);
// modifier.SkinningBake();
// }
if (HumanoidValidator.HasRotationOrScale(root))
{
// 正規化
m_logLabel += $"normalize...\n";
var modifier = new ModelModifier(model);
modifier.SkinningBake();
}
// 右手系に変換
m_logLabel += $"convert to right handed coordinate...\n";

View File

@@ -72,6 +72,6 @@ namespace VrmLib
CheckIndex(model.Skins, nameof(model.Skins));
CheckIndex(model.MeshGroups, nameof(model.MeshGroups));
CheckIndex(model.Animations, nameof(model.Animations));
}
}
}
}

View File

@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace VrmLib
{
public static class ModelModifierExtensions
{
public static string SkinningBake(this ModelModifier modifier)
{
foreach (var node in modifier.Model.Nodes)
{
var meshGroup = node.MeshGroup;
if (meshGroup == null)
{
continue;
}
if (meshGroup.Skin != null)
{
// 正規化されていれば1つしかない
// されていないと Primitive の数だけある
foreach (var mesh in meshGroup.Meshes)
{
{
// Skinningの出力先を自身にすることでBakeする
meshGroup.Skin.Skinning(mesh.VertexBuffer);
}
// morphのPositionは相対値が入っているはずなので、手を加えない正規化されていない場合、二重に補正が掛かる
/*
foreach (var morph in mesh.MorphTargets)
{
if (morph.VertexBuffer.Positions != null)
{
meshGroup.Skin.Skinning(morph.VertexBuffer);
}
}
*/
}
meshGroup.Skin.Root = null;
meshGroup.Skin.InverseMatrices = null;
}
else
{
foreach (var mesh in meshGroup.Meshes)
{
// nodeに対して疑似的にSkinningする
// 回転と拡縮を適用し位置は適用しない
mesh.ApplyRotationAndScaling(node.Matrix);
}
}
}
// 回転・拡縮を除去する
modifier.Model.ApplyRotationAndScale();
// inverse matrix の再計算
foreach (var node in modifier.Model.Nodes)
{
var meshGroup = node.MeshGroup;
if (meshGroup == null)
{
continue;
}
foreach (var mesh in meshGroup.Meshes)
{
if (meshGroup.Skin != null)
{
meshGroup.Skin.CalcInverseMatrices();
}
}
}
return "SkinningBake";
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 54fd566dc84b51643886ce8858a671db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: