public void AddTextures(AssetImportContext ctx, GLTFObject gltfObject) { for (int i = 0; i < gltfObject.images.Count; i++) { // Dont add asset textures if (gltfObject.images[i].imageIsAsset) { continue; } Texture2D tex = gltfObject.images[i].GetTexture(); if (tex == null) { continue; } if (string.IsNullOrEmpty(tex.name)) { tex.name = "texture" + i.ToString(); } #if UNITY_2018_2_OR_NEWER ctx.AddObjectToAsset(i.ToString(), tex); #else ctx.AddSubAsset(i.ToString(), glbObject.images[i].GetTexture()); #endif } }
private static GameObject LoadInternal(this GLTFObject gltfObject, string filepath, ImportSettings importSettings, out GLTFAnimation.ImportResult[] animations) { // directory root is sometimes used for loading buffers from containing file, or local images string directoryRoot = Directory.GetParent(filepath).ToString() + "/"; // Import tasks synchronously GLTFBuffer.ImportTask bufferTask = new GLTFBuffer.ImportTask(gltfObject.buffers, filepath); bufferTask.RunSynchronously(); GLTFBufferView.ImportTask bufferViewTask = new GLTFBufferView.ImportTask(gltfObject.bufferViews, bufferTask); bufferViewTask.RunSynchronously(); GLTFAccessor.ImportTask accessorTask = new GLTFAccessor.ImportTask(gltfObject.accessors, bufferViewTask); accessorTask.RunSynchronously(); GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images, directoryRoot, bufferViewTask); imageTask.RunSynchronously(); GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures, imageTask); textureTask.RunSynchronously(); GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials, textureTask, importSettings); materialTask.RunSynchronously(); GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, materialTask, importSettings); meshTask.RunSynchronously(); GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask); skinTask.RunSynchronously(); GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras); nodeTask.RunSynchronously(); animations = gltfObject.animations.Import(accessorTask.Result, nodeTask.Result); return(nodeTask.Result.GetRoot()); }
private static GameObject LoadInternal(this GLTFObject gltfObject, string filepath, byte[] bytefile, long binChunkStart, ImportSettings importSettings, out GLTFAnimation.ImportResult[] animations) { // directory root is sometimes used for loading buffers from containing file, or local images string directoryRoot = filepath != null?Directory.GetParent(filepath).ToString() + "/" : null; importSettings.shaderOverrides.CacheDefaultShaders(); // Import tasks synchronously GLTFBuffer.ImportTask bufferTask = new GLTFBuffer.ImportTask(gltfObject.buffers, filepath, bytefile, binChunkStart); bufferTask.RunSynchronously(); GLTFBufferView.ImportTask bufferViewTask = new GLTFBufferView.ImportTask(gltfObject.bufferViews, bufferTask); bufferViewTask.RunSynchronously(); GLTFAccessor.ImportTask accessorTask = new GLTFAccessor.ImportTask(gltfObject.accessors, bufferViewTask); accessorTask.RunSynchronously(); GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images, directoryRoot, bufferViewTask); imageTask.RunSynchronously(); GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures, imageTask); textureTask.RunSynchronously(); GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials, textureTask, importSettings); materialTask.RunSynchronously(); GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, materialTask, importSettings); meshTask.RunSynchronously(); GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask); skinTask.RunSynchronously(); GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras); nodeTask.RunSynchronously(); animations = gltfObject.animations.Import(accessorTask.Result, nodeTask.Result, importSettings); foreach (var item in bufferTask.Result) { item.Dispose(); } return(nodeTask.Result.GetRoot()); }
private static IEnumerator LoadAsync(string json, string filepath, ImportSettings importSettings, Action <GameObject> onFinished) { // Threaded deserialization Task <GLTFObject> deserializeTask = new Task <GLTFObject>(() => JsonConvert.DeserializeObject <GLTFObject>(json)); deserializeTask.Start(); while (!deserializeTask.IsCompleted) { yield return(null); } GLTFObject gltfObject = deserializeTask.Result; // directory root is sometimes used for loading buffers from containing file, or local images string directoryRoot = Directory.GetParent(filepath).ToString() + "/"; importSettings.shaders.CacheDefaultShaders(); // Setup import tasks List <ImportTask> importTasks = new List <ImportTask>(); GLTFBuffer.ImportTask bufferTask = new GLTFBuffer.ImportTask(gltfObject.buffers, filepath); importTasks.Add(bufferTask); GLTFBufferView.ImportTask bufferViewTask = new GLTFBufferView.ImportTask(gltfObject.bufferViews, bufferTask); importTasks.Add(bufferViewTask); GLTFAccessor.ImportTask accessorTask = new GLTFAccessor.ImportTask(gltfObject.accessors, bufferViewTask); importTasks.Add(accessorTask); GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images, directoryRoot, bufferViewTask); importTasks.Add(imageTask); GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures, imageTask); importTasks.Add(textureTask); GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials, textureTask, importSettings); importTasks.Add(materialTask); GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, materialTask, importSettings); importTasks.Add(meshTask); GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask); importTasks.Add(skinTask); GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask); importTasks.Add(nodeTask); // Ignite for (int i = 0; i < importTasks.Count; i++) { TaskSupervisor(importTasks[i]).RunCoroutine(); } // Fire onFinished when all tasks have completed if (onFinished != null) { // Wait for all tasks to finish while (!importTasks.All(x => x.IsCompleted)) { yield return(null); } GameObject root = nodeTask.Result.GetRoot(); onFinished(root); } }
public static void ExportGLB(GameObject root) { GLTFObject gltfObject = CreateGLTFObject(root.transform); Debug.Log(JsonConvert.SerializeObject(gltfObject, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore })); }
private static void ImportGLB(byte[] bytes, ImportSettings importSettings, Action <GameObject, AnimationClip[]> onFinished) { Stream stream = new MemoryStream(bytes); long binChunkStart; string json = GetGLBJson(stream, out binChunkStart); GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); StaticCoroutine.Start(gltfObject.LoadInternal(null, bytes, binChunkStart, importSettings, onFinished)); }
private static void ImportGLB(string filepath, ImportSettings importSettings, Action <GameObject, AnimationClip[]> onFinished) { FileStream stream = File.OpenRead(filepath); long binChunkStart; string json = GetGLBJson(stream, out binChunkStart); GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); StaticCoroutine.Start(gltfObject.LoadInternal(filepath, null, binChunkStart, importSettings, onFinished)); }
private static void ImportGLTF(string filepath, ImportSettings importSettings, Action <GameObject, AnimationClip[]> onFinished) { string json = File.ReadAllText(filepath); // Parse json GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); StaticCoroutine.Start(gltfObject.LoadInternal(filepath, null, 0, importSettings, onFinished)); }
private static GameObject ImportGLTF(string filepath, ImportSettings importSettings, out GLTFAnimation.ImportResult[] animations) { string json = File.ReadAllText(filepath); // Parse json GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); return(gltfObject.LoadInternal(filepath, importSettings, out animations)); }
public bool Load(GLTFObject glTFObject) { this.glTFObject = glTFObject; if (OnLoad()) { isLoaded = true; } return(isLoaded); }
private static GameObject ImportGLB(string filepath, ImportSettings importSettings, out AnimationClip[] animations) { FileStream stream = File.OpenRead(filepath); long binChunkStart; string json = GetGLBJson(stream, out binChunkStart); GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); return(gltfObject.LoadInternal(filepath, null, binChunkStart, importSettings, out animations)); }
private static GameObject ImportGLB(byte[] bytes, ImportSettings importSettings, out AnimationClip[] animations) { Stream stream = new MemoryStream(bytes); long binChunkStart; string json = GetGLBJson(stream, out binChunkStart); GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); return(gltfObject.LoadInternal(null, bytes, binChunkStart, importSettings, out animations)); }
/// <summary> Convenience method. Load multiple GLTFProperties with null check </summary> public static void Load <T>(GLTFObject glTFObject, IList <T> properties) where T : GLTFProperty { for (int i = 0; i < properties.Count; i++) { if (properties[i] != null) { properties[i].Load(glTFObject); } } }
/// <summary> Convenience method. Load multiple GLTFProperties with null check </summary> public static void Load(GLTFObject glTFObject, params GLTFProperty[] properties) { for (int i = 0; i < properties.Length; i++) { if (properties[i] != null) { properties[i].Load(glTFObject); } } }
// hkyoo private static GLTFObject ImportGLB(string filepath, out long binChunk) { FileStream stream = File.OpenRead(filepath); string json = GetGLBJson(stream, out binChunk); GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); CheckExtensions(gltfObject); return(gltfObject); }
private static IEnumerator LoadInternal(this GLTFObject gltfObject, string filepath, byte[] bytefile, long binChunkStart, ImportSettings importSettings, Action <GameObject, AnimationClip[]> onFinished) { CheckExtensions(gltfObject); // directory root is sometimes used for loading buffers from containing file, or local images string directoryRoot = filepath != null?Directory.GetParent(filepath).ToString() + "/" : null; importSettings.shaderOverrides.CacheDefaultShaders(); // Import tasks synchronously GLTFBuffer.ImportTask bufferTask = new GLTFBuffer.ImportTask(gltfObject.buffers, filepath, bytefile, binChunkStart); yield return(StaticCoroutine.Start(bufferTask.RunSynchronously())); GLTFBufferView.ImportTask bufferViewTask = new GLTFBufferView.ImportTask(gltfObject.bufferViews, bufferTask); yield return(StaticCoroutine.Start(bufferViewTask.RunSynchronously())); GLTFAccessor.ImportTask accessorTask = new GLTFAccessor.ImportTask(gltfObject.accessors, bufferViewTask); yield return(StaticCoroutine.Start(accessorTask.RunSynchronously())); GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images, directoryRoot, bufferViewTask); yield return(StaticCoroutine.Start(imageTask.RunSynchronously())); GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures, imageTask); yield return(StaticCoroutine.Start(textureTask.RunSynchronously())); GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials, textureTask, importSettings); yield return(StaticCoroutine.Start(materialTask.RunSynchronously())); GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, bufferViewTask, materialTask, importSettings); yield return(StaticCoroutine.Start(meshTask.RunSynchronously())); GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask); yield return(StaticCoroutine.Start(skinTask.RunSynchronously())); GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras); yield return(StaticCoroutine.Start(nodeTask.RunSynchronously())); GLTFAnimation.ImportResult[] animationResult = gltfObject.animations.Import(accessorTask.Result, nodeTask.Result, importSettings); AnimationClip[] animations = null; if (animationResult != null) { animations = animationResult.Select(x => x.clip).ToArray(); } else { animations = new AnimationClip[0]; } foreach (var item in bufferTask.Result) { item.Dispose(); } onFinished?.Invoke(nodeTask.Result.GetRoot(), animations); }
public void AddAnimations(AssetImportContext ctx, GLTFObject gltfObject) { for (int i = 0; i < gltfObject.animations.Count; i++) { AnimationClip clip = gltfObject.animations[i].Clip; #if UNITY_2018_2_OR_NEWER ctx.AddObjectToAsset(clip.name, clip); #else ctx.AddSubAsset(clip.name, clip); #endif } }
private static GameObject ImportGLB(byte[] bytes, ImportSettings importSettings, out AnimationClip[] animations, Action <GameObject, Dictionary <string, object> > handleExtensions = null) { Stream stream = new MemoryStream(bytes); long binChunkStart; string json = GetGLBJson(stream, out binChunkStart); GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); var go = gltfObject.LoadInternal(null, bytes, binChunkStart, importSettings, out animations); if (gltfObject.extras != null) { handleExtensions(go, gltfObject.extras); } return(go); }
public override void OnImportAsset(AssetImportContext ctx) { // Load asset GLTFObject gltfObject = new GLTFObject(ctx.assetPath); // Create gameobject structure GameObject[] roots = gltfObject.Create(); ApplyDefaultMaterial(roots); SaveToAsset(ctx, roots); AddMeshes(ctx, gltfObject); AddMaterials(ctx, gltfObject); AddTextures(ctx, gltfObject); AddAnimations(ctx, gltfObject); }
public void AddMaterials(AssetImportContext ctx, GLTFObject gltfObject) { for (int i = 0; i < gltfObject.materials.Count; i++) { Material mat = gltfObject.materials[i].GetMaterial(); if (string.IsNullOrEmpty(mat.name)) { mat.name = "material" + i.ToString(); } #if UNITY_2018_2_OR_NEWER ctx.AddObjectToAsset(mat.name, mat); #else ctx.AddSubAsset(mat.name, mat); #endif } }
private static GameObject LoadInternal(this GLTFObject gltfObject, string filepath, ImportSettings importSettings, out GLTFAnimation.ImportResult[] animations) { string directoryRoot = Directory.GetParent(filepath).ToString() + "/"; GLTFBuffer.ImportResult[] buffers = gltfObject.buffers.Select(x => x.Import(filepath)).ToArray(); GLTFBufferView.ImportResult[] bufferViews = gltfObject.bufferViews.Select(x => x.Import(buffers)).ToArray(); GLTFAccessor.ImportResult[] accessors = gltfObject.accessors.Select(x => x.Import(bufferViews)).ToArray(); GLTFImage.ImportResult[] images = gltfObject.images.Import(directoryRoot, bufferViews); GLTFTexture.ImportResult[] textures = gltfObject.textures.Import(images); GLTFMaterial.ImportResult materials = gltfObject.materials.Import(textures, importSettings); GLTFMesh.ImportResult[] meshes = gltfObject.meshes.Import(accessors, materials, importSettings); GLTFSkin.ImportResult[] skins = gltfObject.skins.Import(accessors); GLTFNode.ImportResult[] nodes = gltfObject.nodes.Import(meshes, skins); animations = gltfObject.animations.Import(accessors, nodes); return(nodes.GetRoot()); }
public override void OnImportAsset(AssetImportContext ctx) { byte[] bytes = File.ReadAllBytes(ctx.assetPath); // 12 byte header // 0-4 - magic = "glTF" // 4-8 - version = 2 // 8-12 - length = total length of glb, including Header and all Chunks, in bytes. string magic = Encoding.ASCII.GetString(bytes.SubArray(0, 4)); if (magic != "glTF") { return; } uint version = System.BitConverter.ToUInt32(bytes, 4); if (version != 2) { Debug.LogWarning("Importer does not support gltf version " + version); return; } uint length = System.BitConverter.ToUInt32(bytes, 8); // Chunk 0 (json) uint chunkLength = System.BitConverter.ToUInt32(bytes, 12); string chunkType = Encoding.ASCII.GetString(bytes.SubArray(16, 4)); string json = Encoding.ASCII.GetString(bytes.SubArray(20, (int)chunkLength)); // Load file and get directory GLTFObject gltfObject = JsonUtility.FromJson <GLTFObject>(json); string directoryRoot = Directory.GetParent(ctx.assetPath).ToString() + "/"; string mainFile = Path.GetFileName(ctx.assetPath); gltfObject.Load(directoryRoot, mainFile); // Create gameobject structure GameObject[] roots = gltfObject.Create(); ApplyDefaultMaterial(roots); SaveToAsset(ctx, roots); AddMeshes(ctx, gltfObject); AddMaterials(ctx, gltfObject); AddTextures(ctx, gltfObject); AddAnimations(ctx, gltfObject); }
private static void CheckExtensions(GLTFObject gLTFObject) { if (gLTFObject.extensionsRequired != null) { for (int i = 0; i < gLTFObject.extensionsRequired.Count; i++) { switch (gLTFObject.extensionsRequired[i]) { case "KHR_materials_pbrSpecularGlossiness": break; default: Debug.LogWarning($"GLTFUtility: Required extension '{gLTFObject.extensionsRequired[i]}' not supported. Import process will proceed but results may vary."); break; } } } }
public void AddMeshes(AssetImportContext ctx, GLTFObject gltfObject) { for (int i = 0; i < gltfObject.meshes.Count; i++) { Mesh mesh = gltfObject.meshes[i].GetMesh(); if (mesh == null) { Debug.LogWarning("Mesh at index " + i + " was null"); continue; } #if UNITY_2018_2_OR_NEWER ctx.AddObjectToAsset(gltfObject.meshes[i].name, gltfObject.meshes[i].GetCachedMesh()); #else ctx.AddSubAsset(glbObject.meshes[i].name, glbObject.meshes[i].GetCachedMesh()); #endif } }
public static GLTFObject CreateGLTFObject(Transform root) { GLTFObject gltfObject = new GLTFObject(); GLTFAsset asset = new GLTFAsset() { generator = "GLTFUtility by Siccity https: //github.com/Siccity/GLTFUtility", version = "2.0" }; //List<GLTFAccessor.ExportResult> accessors List <GLTFNode.ExportResult> nodes = GLTFNode.Export(root); List <GLTFMesh.ExportResult> meshes = GLTFMesh.Export(nodes); gltfObject.scene = 0; gltfObject.asset = asset; gltfObject.nodes = nodes.Cast <GLTFNode>().ToList(); gltfObject.meshes = meshes.Cast <GLTFMesh>().ToList(); return(gltfObject); }
public override void OnImportAsset(AssetImportContext ctx) { // Load file and get directory GLTFObject gltfObject = JsonUtility.FromJson <GLTFObject>(File.ReadAllText(ctx.assetPath)); string directoryRoot = Directory.GetParent(ctx.assetPath).ToString() + "/"; string mainFile = Path.GetFileName(ctx.assetPath); gltfObject.Load(directoryRoot, mainFile); // Create gameobject structure GameObject[] roots = gltfObject.Create(); ApplyDefaultMaterial(roots); SaveToAsset(ctx, roots); AddMeshes(ctx, gltfObject); AddMaterials(ctx, gltfObject); AddTextures(ctx, gltfObject); AddAnimations(ctx, gltfObject); }
private static GameObject ImportGLB(string filepath, ImportSettings importSettings, out GLTFAnimation.ImportResult[] animations) { byte[] bytes = File.ReadAllBytes(filepath); animations = null; // 12 byte header // 0-4 - magic = "glTF" // 4-8 - version = 2 // 8-12 - length = total length of glb, including Header and all Chunks, in bytes. string magic = Encoding.ASCII.GetString(bytes.SubArray(0, 4)); if (magic != "glTF") { Debug.LogWarning("File at " + filepath + " does not look like a .glb file"); return(null); } uint version = System.BitConverter.ToUInt32(bytes, 4); if (version != 2) { Debug.LogWarning("Importer does not support gltf version " + version); return(null); } // What do we even need the length for. //uint length = System.BitConverter.ToUInt32(bytes, 8); // Chunk 0 (json) uint chunkLength = System.BitConverter.ToUInt32(bytes, 12); // This prints out JSON. So predictable. //string chunkType = Encoding.ASCII.GetString(bytes.SubArray(16, 4)); string json = Encoding.ASCII.GetString(bytes.SubArray(20, (int)chunkLength)); // Parse json GLTFObject gltfObject = JsonConvert.DeserializeObject <GLTFObject>(json); return(gltfObject.LoadInternal(filepath, importSettings, out animations)); }
private static IEnumerator LoadAsync(string json, string filepath, byte[] bytefile, long binChunkStart, ImportSettings importSettings, Action <GameObject, AnimationClip[]> onFinished, Action <float> onProgress = null) { // Threaded deserialization Task <GLTFObject> deserializeTask = new Task <GLTFObject>(() => JsonConvert.DeserializeObject <GLTFObject>(json)); deserializeTask.Start(); while (!deserializeTask.IsCompleted) { yield return(null); } GLTFObject gltfObject = deserializeTask.Result; CheckExtensions(gltfObject); // directory root is sometimes used for loading buffers from containing file, or local images string directoryRoot = filepath != null?Directory.GetParent(filepath).ToString() + "/" : null; importSettings.shaderOverrides.CacheDefaultShaders(); // Setup import tasks List <ImportTask> importTasks = new List <ImportTask>(); GLTFBuffer.ImportTask bufferTask = new GLTFBuffer.ImportTask(gltfObject.buffers, filepath, bytefile, binChunkStart); importTasks.Add(bufferTask); GLTFBufferView.ImportTask bufferViewTask = new GLTFBufferView.ImportTask(gltfObject.bufferViews, bufferTask); importTasks.Add(bufferViewTask); GLTFAccessor.ImportTask accessorTask = new GLTFAccessor.ImportTask(gltfObject.accessors, bufferViewTask); importTasks.Add(accessorTask); GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images, directoryRoot, bufferViewTask); importTasks.Add(imageTask); GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures, imageTask); importTasks.Add(textureTask); GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials, textureTask, importSettings); importTasks.Add(materialTask); GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, bufferViewTask, materialTask, importSettings); importTasks.Add(meshTask); GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask); importTasks.Add(skinTask); GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras); importTasks.Add(nodeTask); // Ignite for (int i = 0; i < importTasks.Count; i++) { TaskSupervisor(importTasks[i], onProgress).RunCoroutine(); } // Wait for all tasks to finish while (!importTasks.All(x => x.IsCompleted)) { yield return(null); } // Fire onFinished when all tasks have completed GameObject root = nodeTask.Result.GetRoot(); GLTFAnimation.ImportResult[] animationResult = gltfObject.animations.Import(accessorTask.Result, nodeTask.Result, importSettings); AnimationClip[] animations = new AnimationClip[0]; if (animationResult != null) { animations = animationResult.Select(x => x.clip).ToArray(); } if (onFinished != null) { onFinished(nodeTask.Result.GetRoot(), animations); } // Close file streams foreach (var item in bufferTask.Result) { item.Dispose(); } }
private static GameObject LoadInternal(this GLTFObject gltfObject, string filepath, byte[] bytefile, long binChunkStart, ImportSettings importSettings, out AnimationClip[] animations) { CheckExtensions(gltfObject); // directory root is sometimes used for loading buffers from containing file, or local images string directoryRoot = filepath != null?Directory.GetParent(filepath).ToString() + "/" : null; importSettings.shaderOverrides.CacheDefaultShaders(); // Import tasks synchronously GLTFBuffer.ImportTask bufferTask = new GLTFBuffer.ImportTask(gltfObject.buffers, filepath, bytefile, binChunkStart); bufferTask.RunSynchronously(); GLTFBufferView.ImportTask bufferViewTask = new GLTFBufferView.ImportTask(gltfObject.bufferViews, bufferTask); bufferViewTask.RunSynchronously(); GLTFAccessor.ImportTask accessorTask = new GLTFAccessor.ImportTask(gltfObject.accessors, bufferViewTask); accessorTask.RunSynchronously(); GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images, directoryRoot, bufferViewTask); imageTask.RunSynchronously(); GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures, imageTask); textureTask.RunSynchronously(); GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials, textureTask, importSettings); materialTask.RunSynchronously(); GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, bufferViewTask, materialTask, importSettings); meshTask.RunSynchronously(); GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask); skinTask.RunSynchronously(); GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras); nodeTask.RunSynchronously(); GLTFAnimation.ImportResult[] animationResult = gltfObject.animations.Import(accessorTask.Result, nodeTask.Result, importSettings); if (animationResult != null) { animations = animationResult.Select(x => x.clip).ToArray(); } else { animations = new AnimationClip[0]; } foreach (var item in bufferTask.Result) { item.Dispose(); } GameObject gameObject = nodeTask.Result.GetRoot(); if (importSettings.extrasProcessor != null) { if (gltfObject.extras == null) { gltfObject.extras = new JObject(); } if (gltfObject.materials != null) { JArray materialExtras = new JArray(); bool hasMaterialExtraData = false; foreach (GLTFMaterial material in gltfObject.materials) { if (material.extras != null) { materialExtras.Add(material.extras); hasMaterialExtraData = true; } else { materialExtras.Add(new JObject()); } } if (hasMaterialExtraData) { gltfObject.extras.Add("material", materialExtras); } } if (gltfObject.animations != null) { JArray animationExtras = new JArray(); bool hasAnimationExtraData = false; foreach (GLTFAnimation animation in gltfObject.animations) { if (animation.extras != null) { hasAnimationExtraData = true; animationExtras.Add(animation.extras); } else { animationExtras.Add(new JObject()); } } if (hasAnimationExtraData) { gltfObject.extras.Add("animation", animationExtras); } } importSettings.extrasProcessor.ProcessExtras(gameObject, animations, gltfObject.extras); } return(gameObject); }
// hkyoo public static GameObject LoadInternal(this GLTFObject gltfObject, string filepath, int index, byte[] bytefile, long binChunkStart, ImportSettings importSettings, out AnimationClip[] animations) { // directory root is sometimes used for loading buffers from containing file, or local images string directoryRoot = filepath != null?Directory.GetParent(filepath).ToString() + "/" : null; importSettings.shaderOverrides.CacheDefaultShaders(); //// for debug //Debug.Log(gltfObject.nodes.ToString() + " " + gltfObject.nodes.Count); // 151 //Debug.Log(gltfObject.meshes.ToString() + " " + gltfObject.meshes.Count); // 151 ////Debug.Log(gltfObject.animations.ToString() + " " + gltfObject.animations.Count); // null //Debug.Log(gltfObject.buffers.ToString() + " " + gltfObject.buffers.Count); // 1 //Debug.Log(gltfObject.bufferViews.ToString() + " " + gltfObject.bufferViews.Count); // 755 //Debug.Log(gltfObject.accessors.ToString() + " " + gltfObject.accessors.Count); // 604 ////Debug.Log(gltfObject.skins.ToString() + " " + gltfObject.skins.Count); // null //Debug.Log(gltfObject.textures.ToString() + " " + gltfObject.textures.Count); // 151 //Debug.Log(gltfObject.images.ToString() + " " + gltfObject.images.Count); // 151 //Debug.Log(gltfObject.materials.ToString() + " " + gltfObject.materials.Count); // 151 ////Debug.Log(gltfObject.cameras.ToString() + " " + gltfObject.cameras.Count); // null /// Stopwatch sw = new Stopwatch(); // image/material/mesh sw.Start(); // Import tasks synchronously GLTFBuffer.ImportTask bufferTask = new GLTFBuffer.ImportTask(gltfObject.buffers, filepath, bytefile, binChunkStart); bufferTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("bufferTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); // TODO? GLTFBufferView.ImportTask bufferViewTask = new GLTFBufferView.ImportTask(gltfObject.bufferViews, bufferTask); bufferViewTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("bufferViewTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); GLTFAccessor.ImportTask accessorTask = new GLTFAccessor.ImportTask(gltfObject.accessors, bufferViewTask); accessorTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("accessorTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images[index], directoryRoot, bufferViewTask); //GLTFImage.ImportTask imageTask = new GLTFImage.ImportTask(gltfObject.images, directoryRoot, bufferViewTask); imageTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("imageTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures[index], imageTask); //GLTFTexture.ImportTask textureTask = new GLTFTexture.ImportTask(gltfObject.textures, imageTask); textureTask.RunSynchronously(); UnityEngine.Debug.Log("textureTask.Result[0] " + textureTask.Result.Length + " " + textureTask.Result[0]); sw.Stop(); UnityEngine.Debug.Log("textureTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); // TODO : shader setting GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials[index], textureTask, importSettings); //GLTFMaterial.ImportTask materialTask = new GLTFMaterial.ImportTask(gltfObject.materials, textureTask, importSettings); materialTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("materialTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); // TODO : GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes[index], accessorTask, bufferViewTask, materialTask, importSettings); //GLTFMesh.ImportTask meshTask = new GLTFMesh.ImportTask(gltfObject.meshes, accessorTask, bufferViewTask, materialTask, importSettings); meshTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("meshTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); GLTFSkin.ImportTask skinTask = new GLTFSkin.ImportTask(gltfObject.skins, accessorTask); skinTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("skinTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); sw.Start(); GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes[index], meshTask, skinTask, gltfObject.cameras); //GLTFNode.ImportTask nodeTask = new GLTFNode.ImportTask(gltfObject.nodes, meshTask, skinTask, gltfObject.cameras); nodeTask.RunSynchronously(); sw.Stop(); UnityEngine.Debug.Log("nodeTask : " + sw.ElapsedMilliseconds.ToString() + "ms"); sw.Reset(); GLTFAnimation.ImportResult[] animationResult = gltfObject.animations.Import(accessorTask.Result, nodeTask.Result, importSettings); if (animationResult != null) { animations = animationResult.Select(x => x.clip).ToArray(); } else { animations = new AnimationClip[0]; } foreach (var item in bufferTask.Result) { item.Dispose(); } return(nodeTask.Result.GetRoot()); }