Esempio n. 1
0
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{Mesh, AiMesh}.ToNative"/>.
        /// </summary>
        /// <param name="nativeValue">Native value to free</param>
        /// <param name="freeNative">True if the unmanaged memory should be freed, false otherwise.</param>
        public static void FreeNative(IntPtr nativeValue, bool freeNative)
        {
            if (nativeValue == IntPtr.Zero)
            {
                return;
            }

            AiMesh aiMesh = MemoryHelper.Read <AiMesh>(nativeValue);

            if (aiMesh.NumVertices > 0)
            {
                if (aiMesh.Vertices != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Vertices);
                }

                if (aiMesh.Normals != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Normals);
                }

                if (aiMesh.Tangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Tangents);
                }

                if (aiMesh.BiTangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.BiTangents);
                }

                //Vertex Color channels
                for (int i = 0; i < aiMesh.Colors.Length; i++)
                {
                    IntPtr colorPtr = aiMesh.Colors[i];

                    if (colorPtr != IntPtr.Zero)
                    {
                        MemoryHelper.FreeMemory(colorPtr);
                    }
                }

                //Texture coordinate channels
                for (int i = 0; i < aiMesh.TextureCoords.Length; i++)
                {
                    IntPtr texCoordsPtr = aiMesh.TextureCoords[i];

                    if (texCoordsPtr != IntPtr.Zero)
                    {
                        MemoryHelper.FreeMemory(texCoordsPtr);
                    }
                }
            }

            //Faces
            if (aiMesh.NumFaces > 0 && aiMesh.Faces != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiFace>(aiMesh.Faces, (int)aiMesh.NumFaces, Face.FreeNative);
            }

            //Bones
            if (aiMesh.NumBones > 0 && aiMesh.Bones != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiBone>(aiMesh.Bones, (int)aiMesh.NumBones, Bone.FreeNative, true);
            }

            //Attachment meshes
            if (aiMesh.NumAnimMeshes > 0 && aiMesh.AnimMeshes != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiAnimMesh>(aiMesh.AnimMeshes, (int)aiMesh.NumAnimMeshes, MeshAnimationAttachment.FreeNative, true);
            }

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }