Updated UniGLTF

This commit is contained in:
ousttrue
2018-07-23 14:39:40 +09:00
parent 90bc21a320
commit dc2ebb369f
5 changed files with 29 additions and 82 deletions

View File

@@ -16,49 +16,27 @@ namespace VRM
var ext = Path.GetExtension(path).ToLower();
if (ext == ".vrm")
{
var context = new VRMImporterContext(path);
context.ParseGlb(File.ReadAllBytes(context.Path));
//
// https://answers.unity.com/questions/647615/how-to-update-import-settings-for-newly-created-as.html
//
for (int i = 0; i < context.GLTF.textures.Count; ++i)
{
var x = context.GLTF.textures[i];
var image = context.GLTF.images[x.source];
if (string.IsNullOrEmpty(image.uri))
{
// glb buffer
var folder = context.GetAssetFolder(".Textures").AssetPathToFullPath();
if (!Directory.Exists(folder))
{
UnityEditor.AssetDatabase.CreateFolder(context.GLTF.baseDir, Path.GetFileNameWithoutExtension(context.Path) + ".Textures");
//Directory.CreateDirectory(folder);
}
var textureName = !string.IsNullOrEmpty(image.name) ? image.name: string.Format("buffer#{0:00}", i);
var png = Path.Combine(folder, textureName + ".png");
var byteSegment = context.GLTF.GetViewBytes(image.bufferView);
File.WriteAllBytes(png, byteSegment.ToArray());
var assetPath = png.ToUnityRelativePath();
//Debug.LogFormat("import asset {0}", assetPath);
UnityEditor.AssetDatabase.ImportAsset(assetPath, UnityEditor.ImportAssetOptions.ForceUpdate);
UnityEditor.AssetDatabase.Refresh();
image.uri = assetPath.Substring(context.GLTF.baseDir.Length + 1);
}
}
EditorApplication.delayCall += () =>
{
// delay and can import png texture
VRMImporter.LoadFromBytes(context);
context.SaveAsAsset();
context.Destroy(false);
};
ImportVrm(path);
}
}
}
static void ImportVrm(string path)
{
var context = new VRMImporterContext(path);
context.ParseGlb(File.ReadAllBytes(path));
var prefabPath = (Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(path) + ".prefab").Replace("\\", "/");
context.SaveTexturesAsPng(prefabPath);
EditorApplication.delayCall += () =>
{
// delay and can import png texture
VRMImporter.LoadFromBytes(context);
context.SaveAsAsset(prefabPath);
context.Destroy(false);
};
}
}
#endif
}

View File

@@ -17,7 +17,7 @@ namespace UniGLTF
}
}
public partial class glTF_extensions : ExtensionBase
public partial class glTF_extensions : ExtensionsBase<glTF_extensions>
{
public VRM.glTF_VRM_extensions VRM = new VRM.glTF_VRM_extensions();

View File

@@ -28,38 +28,17 @@ namespace VRM
public static GameObject LoadFromBytes(Byte[] bytes)
{
var context = new VRMImporterContext();
var context = new VRMImporterContext(null);
context.ParseGlb(bytes);
LoadFromBytes(context);
return context.Root;
}
public static void LoadFromPath(VRMImporterContext context)
{
context.ParseGlb(File.ReadAllBytes(context.Path));
LoadFromBytes(context);
}
public static void LoadFromBytes(VRMImporterContext context)
{
context.CreateMaterial = VRMImporter.GetMaterialFunc(glTF_VRM_Material.Parse(context.Json));
gltfImporter.Load(context);
if (string.IsNullOrEmpty(context.Path))
{
if (string.IsNullOrEmpty(context.GLTF.extensions.VRM.meta.title))
{
context.Root.name = "VRM_LOADED";
}
else
{
context.Root.name = context.GLTF.extensions.VRM.meta.title;
}
}
else
{
context.Root.name = Path.GetFileNameWithoutExtension(context.Path);
}
OnLoadModel(context);
@@ -368,7 +347,7 @@ namespace VRM
};
context.HumanoidAvatar = AvatarBuilder.BuildHumanAvatar(context.Root, description);
context.HumanoidAvatar.name = Path.GetFileNameWithoutExtension(context.Path);
context.HumanoidAvatar.name = "VrmAvatar";
context.AvatarDescription = UniHumanoid.AvatarDescription.CreateFrom(description);
context.AvatarDescription.name = "AvatarDescription";
@@ -389,7 +368,7 @@ namespace VRM
context.AvatarDescription = context.GLTF.extensions.VRM.humanoid.ToDescription(context.Nodes);
context.AvatarDescription.name = "AvatarDescription";
context.HumanoidAvatar = context.AvatarDescription.CreateAvatar(context.Root.transform);
context.HumanoidAvatar.name = Path.GetFileNameWithoutExtension(context.Path);
context.HumanoidAvatar.name = "VrmAvatar";
var humanoid = context.Root.AddComponent<VRMHumanoidDescription>();
humanoid.Avatar = context.HumanoidAvatar;
@@ -409,7 +388,7 @@ namespace VRM
{
for (int i = 0; i < context.GLTF.textures.Count; ++i)
{
var x = new TextureItem(context.GLTF, i);
var x = new TextureItem(context.GLTF, i, null);
x.Process(context.GLTF, storage);
context.Textures.Add(x);
yield return null;
@@ -518,7 +497,7 @@ namespace VRM
public static void LoadVrmAsync(Byte[] bytes, Action<GameObject> onLoaded, Action<Exception> onError = null, bool show = true)
{
var context = new VRMImporterContext();
var context = new VRMImporterContext(null);
context.ParseGlb(bytes);
LoadVrmAsync(context, onLoaded, onError, show);
}
@@ -539,11 +518,6 @@ namespace VRM
return schedulable
.AddTask(Scheduler.ThreadPool, () =>
{
ctx.GLTF.baseDir = Path.GetDirectoryName(ctx.Path);
return Unit.Default;
})
.ContinueWith(Scheduler.CurrentThread, _ =>
{
return glTF_VRM_Material.Parse(ctx.Json);
})
@@ -561,7 +535,7 @@ namespace VRM
parent.AddTask(Scheduler.MainThread,
() =>
{
var texture = new TextureItem(ctx.GLTF, index);
var texture = new TextureItem(ctx.GLTF, index, null);
texture.Process(ctx.GLTF, ctx.Storage);
return texture;
})
@@ -588,7 +562,7 @@ namespace VRM
.ContinueWith(Scheduler.CurrentThread,
_ =>
{
ctx.Root.name = Path.GetFileNameWithoutExtension(ctx.Path);
ctx.Root.name = "VRM";
if (show)
{

View File

@@ -7,13 +7,8 @@ namespace VRM
{
public class VRMImporterContext : ImporterContext
{
public VRMImporterContext()
public VRMImporterContext(string path):base(path)
{
}
public VRMImporterContext(string path)
{
Path = path;
}
public UniHumanoid.AvatarDescription AvatarDescription;
@@ -44,7 +39,7 @@ namespace VRM
// 作成する(先行ロード用)
if(gltfMeta.texture >= 0 && gltfMeta.texture < GLTF.textures.Count)
{
var t = new TextureItem(GLTF, gltfMeta.texture);
var t = new TextureItem(GLTF, gltfMeta.texture, null);
t.Process(GLTF, Storage);
meta.Thumbnail=t.Texture;
}

Submodule UniGLTF updated: 7c5646a081...d65954fa54