Пример #1
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);
 }
            public override IEnumerator OnCoroutine(Action <float> onProgress = null)
            {
                // No nodes
                if (nodes == null)
                {
                    if (onProgress != null)
                    {
                        onProgress.Invoke(1f);
                    }
                    IsCompleted = true;
                    yield break;
                }

                Result = new ImportResult[nodes.Count];

                // Initialize transforms
                for (int i = 0; i < Result.Length; i++)
                {
                    Result[i]                = new GLTFNode.ImportResult();
                    Result[i].transform      = new GameObject().transform;
                    Result[i].transform.name = nodes[i].name;
                }
                // Set up hierarchy
                for (int i = 0; i < Result.Length; i++)
                {
                    if (nodes[i].children != null)
                    {
                        int[] children = nodes[i].children;
                        Result[i].children = children;
                        for (int k = 0; k < children.Length; k++)
                        {
                            int childIndex = children[k];
                            Result[childIndex].parent           = i;
                            Result[childIndex].transform.parent = Result[i].transform;
                        }
                    }
                }
                // Apply TRS
                for (int i = 0; i < Result.Length; i++)
                {
                    nodes[i].ApplyTRS(Result[i].transform);
                }
                // Setup components
                for (int i = 0; i < Result.Length; i++)
                {
                    // Setup mesh
                    if (nodes[i].mesh.HasValue)
                    {
                        GLTFMesh.ImportResult meshResult = meshTask.Result[nodes[i].mesh.Value];
                        if (meshResult == null)
                        {
                            continue;
                        }

                        Mesh     mesh = meshResult.mesh;
                        Renderer renderer;
                        if (nodes[i].skin.HasValue)
                        {
                            GLTFSkin.ImportResult skin = skinTask.Result[nodes[i].skin.Value];
                            renderer = skin.SetupSkinnedRenderer(Result[i].transform.gameObject, mesh, Result);
                        }
                        else if (mesh.blendShapeCount > 0)
                        {
                            // Blend shapes require skinned mesh renderer
                            SkinnedMeshRenderer mr = Result[i].transform.gameObject.AddComponent <SkinnedMeshRenderer>();
                            mr.sharedMesh = mesh;
                            renderer      = mr;
                        }
                        else
                        {
                            MeshRenderer mr = Result[i].transform.gameObject.AddComponent <MeshRenderer>();
                            MeshFilter   mf = Result[i].transform.gameObject.AddComponent <MeshFilter>();
                            renderer      = mr;
                            mf.sharedMesh = mesh;
                        }
                        //Materials
                        renderer.materials = meshResult.materials;
                        if (string.IsNullOrEmpty(Result[i].transform.name))
                        {
                            Result[i].transform.name = "node" + i;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(Result[i].transform.name))
                        {
                            Result[i].transform.name = "node" + i;
                        }
                    }

                    // Setup camera
                    if (nodes[i].camera.HasValue)
                    {
                        GLTFCamera cameraData = cameras[nodes[i].camera.Value];
                        Camera     camera     = Result[i].transform.gameObject.AddComponent <Camera>();
                        if (cameraData.type == CameraType.orthographic)
                        {
                            camera.orthographic     = true;
                            camera.nearClipPlane    = cameraData.orthographic.znear;
                            camera.farClipPlane     = cameraData.orthographic.zfar;
                            camera.orthographicSize = cameraData.orthographic.ymag;
                        }
                        else
                        {
                            camera.orthographic  = false;
                            camera.nearClipPlane = cameraData.perspective.znear;
                            if (cameraData.perspective.zfar.HasValue)
                            {
                                camera.farClipPlane = cameraData.perspective.zfar.Value;
                            }
                            if (cameraData.perspective.aspectRatio.HasValue)
                            {
                                camera.aspect = cameraData.perspective.aspectRatio.Value;
                            }
                            camera.fieldOfView = Mathf.Rad2Deg * cameraData.perspective.yfov;
                        }
                    }
                }
                IsCompleted = true;
            }
Пример #3
0
            protected override void OnMainThreadFinalize()
            {
                if (nodes == null)
                {
                    return;
                }

                Result = new ImportResult[nodes.Count];

                // Initialize transforms
                for (int i = 0; i < Result.Length; i++)
                {
                    Result[i]                = new GLTFNode.ImportResult();
                    Result[i].transform      = new GameObject().transform;
                    Result[i].transform.name = nodes[i].name;
                }
                // Set up hierarchy
                for (int i = 0; i < Result.Length; i++)
                {
                    if (nodes[i].children != null)
                    {
                        int[] children = nodes[i].children;
                        Result[i].children = children;
                        for (int k = 0; k < children.Length; k++)
                        {
                            int childIndex = children[k];
                            Result[childIndex].parent           = i;
                            Result[childIndex].transform.parent = Result[i].transform;
                        }
                    }
                }
                // Apply TRS
                for (int i = 0; i < Result.Length; i++)
                {
                    nodes[i].ApplyTRS(Result[i].transform);
                }
                // Setup components
                for (int i = 0; i < Result.Length; i++)
                {
                    if (nodes[i].mesh.HasValue)
                    {
                        GLTFMesh.ImportResult meshResult = meshTask.Result[nodes[i].mesh.Value];
                        if (meshResult == null)
                        {
                            continue;
                        }

                        Mesh     mesh = meshResult.mesh;
                        Renderer renderer;
                        if (nodes[i].skin.HasValue)
                        {
                            GLTFSkin.ImportResult skin = skinTask.Result[nodes[i].skin.Value];
                            renderer = skin.SetupSkinnedRenderer(Result[i].transform.gameObject, mesh, Result);
                        }
                        else
                        {
                            MeshRenderer mr = Result[i].transform.gameObject.AddComponent <MeshRenderer>();
                            MeshFilter   mf = Result[i].transform.gameObject.AddComponent <MeshFilter>();
                            renderer      = mr;
                            mf.sharedMesh = mesh;
                        }
                        //Materials
                        renderer.materials = meshResult.materials;
                        if (string.IsNullOrEmpty(Result[i].transform.name))
                        {
                            Result[i].transform.name = "node" + i;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(Result[i].transform.name))
                        {
                            Result[i].transform.name = "node" + i;
                        }
                    }
                }
            }
Пример #4
0
        public static GLTFNode.ImportResult[] Import(this List <GLTFNode> nodes, GLTFMesh.ImportResult[] meshes, GLTFSkin.ImportResult[] skins)
        {
            GLTFNode.ImportResult[] results = new GLTFNode.ImportResult[nodes.Count];

            // Initialize transforms
            for (int i = 0; i < results.Length; i++)
            {
                results[i]                = new GLTFNode.ImportResult();
                results[i].transform      = new GameObject().transform;
                results[i].transform.name = nodes[i].name;
            }
            // Set up hierarchy
            for (int i = 0; i < results.Length; i++)
            {
                if (nodes[i].children != null)
                {
                    int[] children = nodes[i].children;
                    results[i].children = children;
                    for (int k = 0; k < children.Length; k++)
                    {
                        int childIndex = children[k];
                        results[childIndex].parent           = i;
                        results[childIndex].transform.parent = results[i].transform;
                    }
                }
            }
            // Apply TRS
            for (int i = 0; i < results.Length; i++)
            {
                nodes[i].ApplyTRS(results[i].transform);
            }
            // Setup components
            for (int i = 0; i < results.Length; i++)
            {
                if (nodes[i].mesh.HasValue)
                {
                    GLTFMesh.ImportResult meshResult = meshes[nodes[i].mesh.Value];
                    Mesh     mesh = meshResult.mesh;
                    Renderer renderer;
                    if (nodes[i].skin.HasValue)
                    {
                        GLTFSkin.ImportResult skin = skins[nodes[i].skin.Value];
                        renderer = skin.SetupSkinnedRenderer(results[i].transform.gameObject, mesh, results);
                    }
                    else
                    {
                        MeshRenderer mr = results[i].transform.gameObject.AddComponent <MeshRenderer>();
                        MeshFilter   mf = results[i].transform.gameObject.AddComponent <MeshFilter>();
                        renderer      = mr;
                        mf.sharedMesh = mesh;
                    }
                    //Materials
                    renderer.materials = meshResult.materials;
                    if (string.IsNullOrEmpty(results[i].transform.name))
                    {
                        results[i].transform.name = "node" + i;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(results[i].transform.name))
                    {
                        results[i].transform.name = "node" + i;
                    }
                }
            }

            return(results);
        }
            protected override void OnMainThreadFinalize()
            {
                if (nodes == null)
                {
                    return;
                }

                Result = new ImportResult[nodes.Count];

                // Initialize transforms
                for (int i = 0; i < Result.Length; i++)
                {
                    Result[i]                = new GLTFNode.ImportResult();
                    Result[i].transform      = new GameObject().transform;
                    Result[i].transform.name = nodes[i].name;
                }
                // Set up hierarchy
                for (int i = 0; i < Result.Length; i++)
                {
                    if (nodes[i].children != null)
                    {
                        int[] children = nodes[i].children;
                        Result[i].children = children;
                        for (int k = 0; k < children.Length; k++)
                        {
                            int childIndex = children[k];
                            Result[childIndex].parent           = i;
                            Result[childIndex].transform.parent = Result[i].transform;
                        }
                    }
                }
                // Apply TRS
                for (int i = 0; i < Result.Length; i++)
                {
                    nodes[i].ApplyTRS(Result[i].transform);
                }
                // Setup components
                for (int i = 0; i < Result.Length; i++)
                {
                    // Setup mesh
                    if (nodes[i].mesh.HasValue)
                    {
                        GLTFMesh.ImportResult meshResult = meshTask.Result[nodes[i].mesh.Value];
                        if (meshResult == null)
                        {
                            continue;
                        }

                        Mesh     mesh = meshResult.mesh;
                        Renderer renderer;
                        if (nodes[i].skin.HasValue)
                        {
                            GLTFSkin.ImportResult skin = skinTask.Result[nodes[i].skin.Value];
                            renderer = skin.SetupSkinnedRenderer(Result[i].transform.gameObject, mesh, Result);
                        }
                        else
                        {
                            MeshRenderer mr = Result[i].transform.gameObject.AddComponent <MeshRenderer>();
                            MeshFilter   mf = Result[i].transform.gameObject.AddComponent <MeshFilter>();
                            renderer      = mr;
                            mf.sharedMesh = mesh;
                        }
                        //Materials
                        renderer.materials = meshResult.materials;
                        if (string.IsNullOrEmpty(Result[i].transform.name))
                        {
                            Result[i].transform.name = "node" + i;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(Result[i].transform.name))
                        {
                            Result[i].transform.name = "node" + i;
                        }
                    }

                    // Setup camera
                    if (nodes[i].camera.HasValue)
                    {
                        GLTFCamera cameraData = cameras[nodes[i].camera.Value];
                        Camera     camera     = Result[i].transform.gameObject.AddComponent <Camera>();
                        if (cameraData.type == CameraType.orthographic)
                        {
                            camera.orthographic     = true;
                            camera.nearClipPlane    = cameraData.orthographic.znear;
                            camera.farClipPlane     = cameraData.orthographic.zfar;
                            camera.orthographicSize = cameraData.orthographic.ymag;
                        }
                        else
                        {
                            camera.orthographic  = false;
                            camera.nearClipPlane = cameraData.perspective.znear;
                            if (cameraData.perspective.zfar.HasValue)
                            {
                                camera.farClipPlane = cameraData.perspective.zfar.Value;
                            }
                            if (cameraData.perspective.aspectRatio.HasValue)
                            {
                                camera.aspect = cameraData.perspective.aspectRatio.Value;
                            }
                            camera.fieldOfView = Mathf.Rad2Deg * cameraData.perspective.yfov;
                        }
                    }
                }
            }