Esempio n. 1
0
            //-------------------------------------------------------------------------------
            internal static void Face(int[] unity, MeshTopology topology, Interface.Array <aiFace> assimp)
            {
                uint nb_faces = 0;

                switch (topology)
                {
                case MeshTopology.Points:
                    nb_faces = 1;
                    break;

                case MeshTopology.Lines:
                    nb_faces = 2;
                    break;

                case MeshTopology.Triangles:
                    nb_faces = 3;
                    break;

                case MeshTopology.Quads:
                    nb_faces = 4;
                    break;

                default:
                    Debug.LogErrorFormat("Unsupported topology '{0}' in assimp export.", topology);
                    break;
                }

                assimp.Clear();

                if (nb_faces > 0)
                {
                    long size = unity.Length / nb_faces;

                    uint i = 0;
                    for (; i < size; i++)
                    {
                        using (aiFace face = new aiFace())
                        {
                            using (aiUIntArray indices = face.Indices)
                            {
                                for (uint j = 0; j < nb_faces; j++)
                                {
                                    indices.Set(j, (uint)unity[i * nb_faces + j]);
                                }

                                assimp.Set(i, face.Unmanaged());
                            }
                        }
                    }

                    if (i * nb_faces != unity.Length)
                    {
                        Debug.LogError("Invalid number of vertices to compose the faces.");
                    }
                }
            }
Esempio n. 2
0
            //-------------------------------------------------------------------------------
            internal static int[] Face(Interface.DynamicArray <aiFace> assimp, out MeshTopology topology)
            {
                uint size = assimp.Size();

                topology = MeshTopology.Triangles;
                uint nb_faces = 3;

                if (size > 0)
                {
                    // Get the topology from the first face (because option aiProcess_SortByPType is mandatory, all faces have the same topology)
                    using (aiFace face = assimp.Get(0))
                    {
                        using (aiUIntArray indices = face.Indices)
                        {
                            switch (indices.Size())
                            {
                            case 1:
                                topology = MeshTopology.Points;
                                nb_faces = 1;
                                break;

                            case 2:
                                topology = MeshTopology.Lines;
                                nb_faces = 2;
                                break;

                            default:
                                // Because option aiProcess_Triangulate is mandatory
                                topology = MeshTopology.Triangles;
                                nb_faces = 3;
                                break;
                            }
                        }
                    }
                }

                int[] unity = new int[size * nb_faces];

                for (uint i = 0; i < size; i++)
                {
                    using (aiFace face = assimp.Get(i))
                    {
                        using (aiUIntArray indices = face.Indices)
                        {
                            if (indices.Size() >= nb_faces)
                            {
                                for (uint j = 0; j < nb_faces; j++)
                                {
                                    unity[i * nb_faces + j] = (int)indices.Get(j);
                                }

                                if (indices.Size() > nb_faces)
                                {
                                    Debug.LogError("Too many vertices to compose a face. Some data is lost.");
                                }
                            }
                            else
                            {
                                Debug.LogError("Not enough vertices to compose a face.");
                            }
                        }
                    }
                }

                return(unity);
            }
Esempio n. 3
0
        public static Node FromAssimp(Module.Import.Assimp.Context context, aiScene scene, aiNode assimp_node)
        {
            // Create new node object
            Node node = new Node();

            // Get node ID
            node.id = context.id++;

            // Get node name
            node.name = Assimp.Convert.Name(assimp_node.mName, "node");

            // Get node metadata
            using (aiMetadata meta = assimp_node.mMetaData)
            {
                UnityComponent metadata = UnityComponent.FromAssimpMetadata(meta);

                if (metadata != null)
                {
                    node.components = new UnityComponent[] { metadata };
                }
            }

            // Parse children recursively
            using (aiNodeArray children = assimp_node.Children)
            {
                uint children_size = children.Size();

                if (children_size > 0)
                {
                    node.children = new Node[children_size];

                    for (uint i = 0; i < children_size; i++)
                    {
                        aiNode child = children.Get(i);

                        // ParseNode must dispose of the given node afterward
                        // We must use a proxy method for saving the result into the array because the index i is captured by the lambda otherwise and it's value is indefinite across multiple threads.
                        context.threads.ExecAndSaveToArray(node.children, (int)i, () => FromAssimp(context, scene, child));
                    }
                }
            }

            // Parse meshes associated to this node
            using (aiUIntArray meshes = assimp_node.Meshes)
            {
                uint meshes_size = meshes.Size();

                if (meshes_size > 0)
                {
                    int global_meshes_size = (int)scene.Meshes.Size();

                    node.meshes = new GraphicMesh[meshes_size];

                    for (uint j = 0; j < meshes_size; j++)
                    {
                        node.meshes[j] = new GraphicMesh();

                        if (j < global_meshes_size)
                        {
                            uint mesh_index = meshes.Get(j);

                            node.meshes[j].meshIndex = (int)mesh_index;
                        }
                    }
                }
            }

            // Get the transform of this node
            using (aiVector3D position = new aiVector3D())
            {
                using (aiVector3D scaling = new aiVector3D())
                {
                    using (aiQuaternion rotation = new aiQuaternion())
                    {
                        assimp_node.mTransformation.Decompose(scaling, rotation, position);

                        node.position = Assimp.Convert.AssimpToUnity.Vector3(position);
                        node.rotation = Assimp.Convert.AssimpToUnity.Quaternion(rotation);
                        node.scale    = Assimp.Convert.AssimpToUnity.Vector3(scaling);
                    }
                }
            }

            // We must dispose of the given parameter to avoid memory leaks
            assimp_node.Dispose();

            context.progress.Update(ASSIMP_PROGRESS_FACTOR);

            return(node);
        }
Esempio n. 4
0
        public aiNode ToAssimp(Module.Export.Assimp.Context context, Scene scene, aiNode parent)
        {
            uint index = 0;

            aiNode node_object = new aiNode(name);

            // Set parent
            node_object.mParent = parent;

            // Set transform
            using (aiVector3D assimp_scale = Assimp.Convert.UnityToAssimp.Vector3(scale))
            {
                using (aiQuaternion assimp_rotation = Assimp.Convert.UnityToAssimp.Quaternion(rotation))
                {
                    using (aiVector3D assimp_position = Assimp.Convert.UnityToAssimp.Vector3(position))
                    {
                        using (aiMatrix4x4 matrix = new aiMatrix4x4(assimp_scale, assimp_rotation, assimp_position))
                        {
                            node_object.mTransformation = matrix.Unmanaged();
                        }
                    }
                }
            }

            // Parse the children nodes
            if (children != null && children.Length > 0)
            {
                using (aiNodeArray assimp_children = node_object.Children)
                {
                    assimp_children.Reserve((uint)children.Length, true);

                    index = 0;

                    foreach (Node child in children)
                    {
                        using (aiNode assimp_child = child.ToAssimp(context, scene, node_object))
                        {
                            if (assimp_child != null)
                            {
                                assimp_children.Set(index++, assimp_child.Unmanaged());
                            }
                        }
                    }
                }
            }

            // Parse the mesh objects
            if (meshes != null && meshes.Length > 0)
            {
                using (aiUIntArray assimp_meshes = node_object.Meshes)
                {
                    assimp_meshes.Reserve((uint)meshes.Length, true);

                    index = 0;

                    foreach (GraphicMesh graphic_mesh in meshes)
                    {
                        Mesh mesh = scene.meshes[graphic_mesh.meshIndex];

                        int nb_materials = (graphic_mesh.materialsIndexes != null ? graphic_mesh.materialsIndexes.Length : 0);

                        // Handle unity submeshes by creating new meshes for each submesh
                        for (int i = 0; i < mesh.SubMeshesCount; i++)
                        {
                            // Assimp meshes can only have one material. Therefore, mutliple instances of one mesh
                            // using different materials must be detected and replaced by different output meshes.

                            uint assimp_mesh_index;

                            int mat_index = (i < nb_materials ? graphic_mesh.materialsIndexes[i] : 0 /*Assimp default*/);

                            Module.Export.Assimp.Mesh key = new Module.Export.Assimp.Mesh(graphic_mesh.meshIndex, i, mat_index);

                            if (!context.meshes.TryGetValue(key, out assimp_mesh_index))
                            {
                                assimp_mesh_index = (uint)context.meshes.Count;

                                context.meshes.Add(key, assimp_mesh_index);
                            }

                            assimp_meshes.Set(index++, assimp_mesh_index);
                        }
                    }
                }
            }

            // Parse the node metadata
            if (components != null)
            {
                foreach (UnityComponent component in components)
                {
                    aiMetadata assimp_meta = component.ToAssimpMetadata();

                    if (assimp_meta != null)
                    {
                        node_object.mMetaData = assimp_meta.Unmanaged();

                        break;
                    }
                }
            }

            context.progress.Update(ASSIMP_PROGRESS_FACTOR);

            return(node_object);
        }
Esempio n. 5
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(aiUIntArray obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }