Ejemplo n.º 1
0
 public ImportTask(List <GLTFAccessor> accessors, GLTFBufferView.ImportTask bufferViewTask) : base(bufferViewTask)
 {
     task = new Task(() => {
         Result = new ImportResult[accessors.Count];
         for (int i = 0; i < Result.Length; i++)
         {
             Result[i] = accessors[i].Import(bufferViewTask.Result);
         }
     });
 }
Ejemplo n.º 2
0
        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)
            {
                ProcessExtrasInternal(gameObject, gltfObject, importSettings, animations);
            }
            return(gameObject);
        }
Ejemplo n.º 3
0
            public ImportTask(List <GLTFImage> images, string directoryRoot, GLTFBufferView.ImportTask bufferViewTask) : base(bufferViewTask)
            {
                task = new Task(() => {
                    // No images
                    if (images == null)
                    {
                        return;
                    }

                    Result = new ImportResult[images.Count];
                    for (int i = 0; i < images.Count; i++)
                    {
                        string fullUri = directoryRoot + images[i].uri;
                        if (!string.IsNullOrEmpty(images[i].uri))
                        {
                            if (File.Exists(fullUri))
                            {
                                // If the file is found at fullUri, read it
                                byte[] bytes = File.ReadAllBytes(fullUri);
                                Result[i]    = new ImportResult(bytes, fullUri);
                            }
                            else if (images[i].uri.StartsWith("data:"))
                            {
                                // If the image is embedded, find its Base64 content and save as byte array
                                string content    = images[i].uri.Split(',').Last();
                                byte[] imageBytes = Convert.FromBase64String(content);
                                Result[i]         = new ImportResult(imageBytes);
                            }
                        }
                        else if (images[i].bufferView.HasValue && !string.IsNullOrEmpty(images[i].mimeType))
                        {
                            GLTFBufferView.ImportResult view = bufferViewTask.Result[images[i].bufferView.Value];
                            byte[] bytes         = new byte[view.byteLength];
                            view.stream.Position = view.byteOffset;
                            view.stream.Read(bytes, 0, view.byteLength);
                            Result[i] = new ImportResult(bytes);
                        }
                        else
                        {
                            Debug.Log("Couldn't find texture at " + fullUri);
                        }
                    }
                });
            }
Ejemplo n.º 4
0
        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 (importSettings.extrasProcessor != null)
            {
                ProcessExtrasInternal(root, gltfObject, importSettings, animations);
            }
            if (onFinished != null)
            {
                onFinished(nodeTask.Result.GetRoot(), animations);
            }

            // Close file streams
            foreach (var item in bufferTask.Result)
            {
                item.Dispose();
            }
        }
Ejemplo n.º 5
0
            public ImportTask(List <GLTFMesh> meshes, GLTFAccessor.ImportTask accessorTask, GLTFBufferView.ImportTask bufferViewTask, GLTFMaterial.ImportTask materialTask, ImportSettings importSettings) : base(accessorTask, materialTask)
            {
                this.meshes       = meshes;
                this.materialTask = materialTask;

                task = new Task(() => {
                    if (meshes == null)
                    {
                        return;
                    }

                    meshData = new MeshData[meshes.Count];
                    for (int i = 0; i < meshData.Length; i++)
                    {
                        meshData[i] = new MeshData(meshes[i], accessorTask.Result, bufferViewTask.Result);
                    }
                });
            }