ModelAsset BuildGameObject(VrmLib.Model model, bool showMesh) { var assets = RuntimeUnityBuilder.ToUnityAsset(model, showMesh); UniVRM10.ComponentBuilder.Build10(model, assets); return(assets); }
private ModelAsset ToUnity(Model model) { // Model => Unity var assets = RuntimeUnityBuilder.ToUnityAsset(model); UniVRM10.ComponentBuilder.Build10(model, assets); return(assets); }
private (GameObject, IReadOnlyList <VRMShaders.MaterialFactory.MaterialLoadInfo>) ToUnity(GltfParser parser) { // Model => Unity using (var loader = new RuntimeUnityBuilder(parser)) { loader.Load(); loader.DisposeOnGameObjectDestroyed(); return(loader.Root, loader.MaterialFactory.Materials); } }
GameObject BuildGameObject(GltfParser parser, bool showMesh) { using (var loader = new RuntimeUnityBuilder(parser)) { loader.Load(); if (showMesh) { loader.ShowMeshes(); } loader.EnableUpdateWhenOffscreen(); return(loader.DisposeOnGameObjectDestroyed().gameObject); } }
static GameObject Import(byte[] bytes, FileInfo path) { var parser = new GltfParser(); parser.Parse(path.FullName, bytes); using (var loader = new RuntimeUnityBuilder(parser)) { loader.Load(); loader.ShowMeshes(); return(loader.DisposeOnGameObjectDestroyed().gameObject); } }
public static IEnumerator ToUnityAssetAsync(VrmLib.Model model, ModelAsset asset) { // 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 = RuntimeUnityBuilder.CreateTexture(imageTexture); texture.name = name; asset.Map.Textures.Add(src, texture); asset.Textures.Add(texture); } else { Debug.LogWarning($"{name} not ImageTexture"); } // next frame yield return(null); } // material foreach (var src in model.Materials) { // TODO: material has VertexColor var material = RuntimeUnityMaterialBuilder.CreateMaterialAsset(src, hasVertexColor: false, asset.Map.Textures); material.name = src.Name; asset.Map.Materials.Add(src, material); asset.Materials.Add(material); // next frame yield return(null); } // 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); asset.Map.Meshes.Add(src, mesh); asset.Meshes.Add(mesh); } else { // 頂点バッファの連結が必用 throw new NotImplementedException(); } // next frame yield return(null); } // node: recursive yield return(CreateNodesAsync(model.Root, null, asset.Map.Nodes)); asset.Root = asset.Map.Nodes[model.Root]; // next frame yield return(null); // renderer var map = asset.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); renderer.enabled = false; map.Renderers.Add(node, renderer); asset.Renderers.Add(renderer); // next frame yield return(null); } // humanoid var boneMap = map.Nodes .Where(x => x.Key.HumanoidBone.GetValueOrDefault() != VrmLib.HumanoidBones.unknown) .Select(x => (x.Value.transform, x.Key.HumanoidBone.Value)).AsEnumerable(); // next frame yield return(null); asset.HumanoidAvatar = HumanoidLoader.LoadHumanoidAvatar(asset.Root.transform, boneMap); asset.HumanoidAvatar.name = "VRM"; // next frame yield return(null); var animator = asset.Root.AddComponent <Animator>(); animator.avatar = asset.HumanoidAvatar; }
void LoadModel(string path) { if (!File.Exists(path)) { return; } Debug.LogFormat("{0}", path); var ext = Path.GetExtension(path).ToLower(); switch (ext) { case ".vrm": { var parser = new UniGLTF.GltfParser(); parser.ParsePath(path); using (var loader = new RuntimeUnityBuilder(parser)) { loader.Load(); loader.ShowMeshes(); loader.EnableUpdateWhenOffscreen(); var destroyer = loader.DisposeOnGameObjectDestroyed(); SetModel(destroyer.gameObject); } break; } case ".glb": { var file = File.ReadAllBytes(path); var parser = new GltfParser(); parser.ParseGlb(file); using (var loader = new UniGLTF.ImporterContext(parser)) { loader.Load(); loader.ShowMeshes(); loader.EnableUpdateWhenOffscreen(); loader.ShowMeshes(); var destroyer = loader.DisposeOnGameObjectDestroyed(); SetModel(destroyer.gameObject); } break; } case ".gltf": case ".zip": { var parser = new GltfParser(); parser.ParsePath(path); using (var loader = new UniGLTF.ImporterContext(parser)) { loader.Load(); loader.ShowMeshes(); loader.EnableUpdateWhenOffscreen(); loader.ShowMeshes(); var destroyer = loader.DisposeOnGameObjectDestroyed(); SetModel(destroyer.gameObject); } break; } default: Debug.LogWarningFormat("unknown file type: {0}", path); break; } }