Esempio n. 1
0
        static public ModelAsset ToUnityAsset(VrmLib.Model model, string assetPath, IExternalUnityObject scriptedImporter)
        {
            var modelAsset = new ModelAsset();

            CreateTextureAsset(model, modelAsset, scriptedImporter);
            CreateMaterialAsset(model, modelAsset, scriptedImporter);
            CreateMeshAsset(model, modelAsset);

            // node
            RuntimeUnityBuilder.CreateNodes(model.Root, null, modelAsset.Map.Nodes);
            modelAsset.Root = modelAsset.Map.Nodes[model.Root];

            // renderer
            var map = modelAsset.Map;

            foreach (var(node, go) in map.Nodes)
            {
                if (node.MeshGroup is null)
                {
                    continue;
                }

                if (node.MeshGroup.Meshes.Count > 1)
                {
                    throw new NotImplementedException("invalid isolated vertexbuffer");
                }

                var renderer = RuntimeUnityBuilder.CreateRenderer(node, go, map);
                map.Renderers.Add(node, renderer);
                modelAsset.Renderers.Add(renderer);
            }

            // humanoid
            var boneMap = modelAsset.Map.Nodes
                          .Where(x => x.Key.HumanoidBone.GetValueOrDefault() != VrmLib.HumanoidBones.unknown)
                          .Select(x => (x.Value.transform, x.Key.HumanoidBone.Value))
                          .ToDictionary(x => x.transform, x => x.Item2);

            if (boneMap.Any())
            {
                modelAsset.HumanoidAvatar = HumanoidLoader.LoadHumanoidAvatar(modelAsset.Root.transform, boneMap);
                if (modelAsset.HumanoidAvatar != null)
                {
                    modelAsset.HumanoidAvatar.name = "VRM";
                }
            }

            var animator = modelAsset.Root.AddComponent <Animator>();

            animator.avatar = modelAsset.HumanoidAvatar;

            return(modelAsset);
        }
Esempio n. 2
0
        public ModelAsset ToUnityAsset(VrmLib.Model model, string assetPath, IExternalUnityObject scriptedImporter)
        {
            var modelAsset = new ModelAsset();

            CreateTextureAsset(model, modelAsset, scriptedImporter);
            CreateMaterialAsset(model, modelAsset, scriptedImporter);
            RuntimeUnityBuilder.CreateMeshAsset(model, modelAsset, Meshes);

            // node
            RuntimeUnityBuilder.CreateNodes(model.Root, null, Nodes);
            modelAsset.Root = Nodes[model.Root];
            Root            = modelAsset.Root;

            // renderer
            foreach (var(mesh, renderer) in RuntimeUnityBuilder.CreateRendererAsset(Nodes, Meshes, Materials))
            {
                Renderers.Add(mesh, renderer);
            }

            // humanoid
            var boneMap = Nodes
                          .Where(x => x.Key.HumanoidBone.GetValueOrDefault() != VrmLib.HumanoidBones.unknown)
                          .Select(x => (x.Value.transform, x.Key.HumanoidBone.Value))
                          .ToDictionary(x => x.transform, x => x.Item2);

            if (boneMap.Any())
            {
                modelAsset.HumanoidAvatar = HumanoidLoader.LoadHumanoidAvatar(modelAsset.Root.transform, boneMap);
                if (modelAsset.HumanoidAvatar != null)
                {
                    modelAsset.HumanoidAvatar.name = "VRM";
                }
            }

            var animator = modelAsset.Root.AddComponent <Animator>();

            animator.avatar = modelAsset.HumanoidAvatar;

            modelAsset.Map = new ModelMap
            {
                Nodes     = Nodes,
                Textures  = Textures,
                Materials = Materials,
                Meshes    = Meshes,
                Renderers = Renderers,
            };

            return(modelAsset);
        }
Esempio n. 3
0
        /// <summary>
        /// モデル(Transform + Renderer)を構築する。
        /// <summary>
        public static ModelAsset ToUnityAsset(VrmLib.Model model, bool showMesh = true)
        {
            var modelAsset = new ModelAsset();

            // texture
            for (int i = 0; i < model.Textures.Count; ++i)
            {
                var src  = model.Textures[i];
                var name = !string.IsNullOrEmpty(src.Name)
                    ? src.Name
                    : string.Format("{0}_img{1}", model.Root.Name, i);
                if (src is VrmLib.ImageTexture imageTexture)
                {
                    var texture = CreateTexture(imageTexture);
                    texture.name = name;
                    modelAsset.Map.Textures.Add(src, texture);
                    modelAsset.Textures.Add(texture);
                }
                else
                {
                    Debug.LogWarning($"{name} not ImageTexture");
                }
            }

            // material
            foreach (var src in model.Materials)
            {
                // TODO: material has VertexColor
                var material = RuntimeUnityMaterialBuilder.CreateMaterialAsset(src, hasVertexColor: false, modelAsset.Map.Textures);
                material.name = src.Name;
                modelAsset.Map.Materials.Add(src, material);
                modelAsset.Materials.Add(material);
            }

            // mesh
            for (int i = 0; i < model.MeshGroups.Count; ++i)
            {
                var src = model.MeshGroups[i];
                if (src.Meshes.Count == 1)
                {
                    // submesh 方式
                    var mesh = new Mesh();
                    mesh.name = src.Name;
                    mesh.LoadMesh(src.Meshes[0], src.Skin);
                    modelAsset.Map.Meshes.Add(src, mesh);
                    modelAsset.Meshes.Add(mesh);
                }
                else
                {
                    // 頂点バッファの連結が必用
                    throw new NotImplementedException();
                }
            }

            // node: recursive
            CreateNodes(model.Root, null, modelAsset.Map.Nodes);
            modelAsset.Root = modelAsset.Map.Nodes[model.Root];

            // renderer
            var map = modelAsset.Map;

            foreach (var(node, go) in map.Nodes)
            {
                if (node.MeshGroup is null)
                {
                    continue;
                }

                if (node.MeshGroup.Meshes.Count > 1)
                {
                    throw new NotImplementedException("invalid isolated vertexbuffer");
                }

                var renderer = CreateRenderer(node, go, map);
                if (!showMesh)
                {
                    renderer.enabled = false;
                }
                map.Renderers.Add(node, renderer);
                modelAsset.Renderers.Add(renderer);
            }

            // humanoid
            var boneMap = map.Nodes
                          .Where(x => x.Key.HumanoidBone.GetValueOrDefault() != VrmLib.HumanoidBones.unknown)
                          .Select(x => (x.Value.transform, x.Key.HumanoidBone.Value))
                          .ToDictionary(x => x.transform, x => x.Item2);

            modelAsset.HumanoidAvatar      = HumanoidLoader.LoadHumanoidAvatar(modelAsset.Root.transform, boneMap);
            modelAsset.HumanoidAvatar.name = "VRM";

            var animator = modelAsset.Root.AddComponent <Animator>();

            animator.avatar = modelAsset.HumanoidAvatar;

            return(modelAsset);
        }