Пример #1
0
            public override IEnumerator OnCoroutine(Action <float> onProgress = null)
            {
                // No mesh
                if (meshData == null)
                {
                    if (onProgress != null)
                    {
                        onProgress.Invoke(1f);
                    }
                    IsCompleted = true;
                    yield break;
                }

                Result = new ImportResult[meshData.Length];
                for (int i = 0; i < meshData.Length; i++)
                {
                    if (meshData[i] == null)
                    {
                        Debug.LogWarning("Mesh " + i + " import error");
                        continue;
                    }

                    Result[i]           = new ImportResult();
                    Result[i].mesh      = meshData[i].ToMesh();
                    Result[i].materials = new Material[meshes[i].primitives.Count];
                    for (int k = 0; k < meshes[i].primitives.Count; k++)
                    {
                        int?matIndex = meshes[i].primitives[k].material;
                        if (matIndex.HasValue && materialTask.Result != null && materialTask.Result.Count() > matIndex.Value)
                        {
                            GLTFMaterial.ImportResult matImport = materialTask.Result[matIndex.Value];
                            if (matImport != null)
                            {
                                Result[i].materials[k] = matImport.material;
                            }
                            else
                            {
                                Debug.LogWarning("Mesh[" + i + "].matIndex points to null material (index " + matIndex.Value + ")");
                                Result[i].materials[k] = GLTFMaterial.defaultMaterial;
                            }
                        }
                        else
                        {
                            Result[i].materials[k] = GLTFMaterial.defaultMaterial;
                        }
                    }
                    if (string.IsNullOrEmpty(Result[i].mesh.name))
                    {
                        Result[i].mesh.name = "mesh" + i;
                    }
                    if (onProgress != null)
                    {
                        onProgress.Invoke((float)(i + 1) / (float)meshData.Length);
                    }
                    yield return(null);
                }
                IsCompleted = true;
            }
Пример #2
0
        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());
        }
Пример #3
0
        public static GLTFMaterial.ImportResult Import(this List <GLTFMaterial> materials, GLTFTexture.ImportResult[] textures, ImportSettings importSettings)
        {
            if (!importSettings.materials)
            {
                return(null);
            }

            GLTFMaterial.ImportResult result = new GLTFMaterial.ImportResult();
            result.materials = new Material[materials.Count];
            for (int i = 0; i < materials.Count; i++)
            {
                result.materials[i] = materials[i].CreateMaterial(textures, importSettings.shaders);
                if (materials[i].name == null)
                {
                    materials[i].name = "material" + i;
                }
            }
            return(result);
        }
Пример #4
0
            protected override void OnMainThreadFinalize()
            {
                Result = new ImportResult[meshData.Length];
                for (int i = 0; i < meshData.Length; i++)
                {
                    if (meshData[i] == null)
                    {
                        Debug.LogWarning("Draco mesh not supported");
                        continue;
                    }

                    Result[i]           = new ImportResult();
                    Result[i].mesh      = meshData[i].ToMesh();
                    Result[i].materials = new Material[meshes[i].primitives.Count];
                    for (int k = 0; k < meshes[i].primitives.Count; k++)
                    {
                        int?matIndex = meshes[i].primitives[k].material;
                        if (matIndex.HasValue && materialTask.Result != null && materialTask.Result.Count() > matIndex.Value)
                        {
                            GLTFMaterial.ImportResult matImport = materialTask.Result[matIndex.Value];
                            if (matImport != null)
                            {
                                Result[i].materials[k] = matImport.material;
                            }
                            else
                            {
                                Debug.LogWarning("Mesh[" + i + "].matIndex points to null material (index " + matIndex.Value + ")");
                                Result[i].materials[k] = GLTFMaterial.defaultMaterial;
                            }
                        }
                        else
                        {
                            Result[i].materials[k] = GLTFMaterial.defaultMaterial;
                        }
                    }
                    if (string.IsNullOrEmpty(Result[i].mesh.name))
                    {
                        Result[i].mesh.name = "mesh" + i;
                    }
                }
            }
Пример #5
0
 public static GLTFMesh.ImportResult[] Import(this List <GLTFMesh> meshes, GLTFAccessor.ImportResult[] accessors, GLTFMaterial.ImportResult materials, ImportSettings importSettings)
 {
     GLTFMesh.ImportResult[] results = new GLTFMesh.ImportResult[meshes.Count];
     for (int i = 0; i < results.Length; i++)
     {
         results[i]           = new GLTFMesh.ImportResult();
         results[i].mesh      = meshes[i].CreateMesh(accessors);
         results[i].materials = new Material[meshes[i].primitives.Count];
         for (int k = 0; k < meshes[i].primitives.Count; k++)
         {
             int?matIndex = meshes[i].primitives[k].material;
             if (matIndex.HasValue && materials != null)
             {
                 results[i].materials[k] = materials.materials[matIndex.Value];
             }
             else
             {
                 results[i].materials[k] = GLTFMaterial.defaultMaterial;
             }
         }
         if (meshes[i].name == null)
         {
             meshes[i].name = "mesh" + i;
         }
     }
     return(results);
 }