Пример #1
0
        public static string SingleMesh(this ModelModifier modifier, string name)
        {
            var count  = modifier.Model.MeshGroups.Sum(x => x.Meshes.Count);
            var meshes = modifier.Model.Root.Traverse()
                         .Select(x => x.MeshGroup)
                         .Where(x => x != null)
                         .Select(x => $"[{x.Name}]")
                         .ToArray();

            if (meshes.Length == 0)
            {
                return("SingleMesh: no mesh. do nothing");
            }
            if (meshes.Length <= 1)
            {
                return("SingleMesh: one mesh. do nothing");
            }

            var mesh     = modifier.Model.CreateSingleMesh(name);
            var meshNode = new Node(mesh.Name)
            {
                MeshGroup = mesh,
            };

            mesh.Skin.Root = meshNode;

            // fix bone weight (0, x, 0, 0) => (x, 0, 0, 0)
            // mesh.Meshes[0].VertexBuffer.FixBoneWeight();

            // replace morphAnimation reference
            ReplaceMorphTargetAnimationNode(modifier.Model.Animations, meshNode);

            // update Model
            foreach (var x in modifier.Model.MeshGroups.ToArray())
            {
                modifier.MeshReplace(x, mesh);
            }
            modifier.NodeMeshClear();
            modifier.NodeReplace(null, meshNode);

            var names = string.Join("", meshes);

            // return $"SingleMesh: {names}";
            return($"SingleMesh: {count} => {modifier.Model.MeshGroups.Sum(x => x.Meshes.Count)}");
        }