示例#1
0
        private void InitSkinInfo(AiMesh mesh, AssimpSceneContainer container)
        {
            var          boneIDs     = new uvec4[mesh.Vertexes.Length];
            var          boneWeights = new vec4[mesh.Vertexes.Length];
            AllBoneInfos allBones    = container.GetAllBoneInfos();
            Dictionary <string, uint> nameIndexDict = allBones.nameIndexDict;

            for (int i = 0; i < mesh.BoneCount; i++)
            {
                AiBone bone      = mesh.Bones[i]; // bones that influence this mesh.
                uint   boneIndex = nameIndexDict[bone.Name];

                for (int j = 0; j < bone.VertexWeightCount; j++)
                {
                    AiVertexWeight vertexWeight = bone.VertexWeights[j];
                    uint           vertexID     = vertexWeight.VertexID;
                    for (int t = 0; t < 4; t++)
                    {
                        if (boneWeights[vertexID][t] == 0.0f) // fill in x y z w.
                        {
                            boneIDs[vertexID][t]     = boneIndex;
                            boneWeights[vertexID][t] = vertexWeight.Weight;
                            break;
                        }
                    }
                }
            }
            this.boneIDs     = boneIDs;
            this.boneWeights = boneWeights;
        }
        private AllBoneInfos InitBonesInfo(AiScene aiScene)
        {
            List <BoneInfo> boneInfos     = new List <BoneInfo>();
            var             nameIndexDict = new Dictionary <string, uint>();

            for (int i = 0; i < aiScene.MeshCount; i++)
            {
                AiMesh mesh = aiScene.Meshes[i];
                for (int j = 0; j < mesh.BoneCount; j++)
                {
                    AiBone bone     = mesh.Bones[j];
                    string boneName = bone.Name;
                    if (!nameIndexDict.ContainsKey(boneName))
                    {
                        var boneInfo = new BoneInfo(bone);
                        boneInfos.Add(boneInfo);
                        nameIndexDict.Add(boneName, (uint)(boneInfos.Count - 1));
                    }
                }
            }

            return(new AllBoneInfos(boneInfos.ToArray(), nameIndexDict));
        }
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{MeshAnimationAttachment, AiAnimMesh}.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.MarshalStructure <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
                IntPtr[] colors = aiMesh.Colors;

                if (colors != null)
                {
                    for (int i = 0; i < colors.Length; i++)
                    {
                        IntPtr colorPtr = colors[i];

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

                //Texture coordinate channels
                IntPtr[] texCoords = aiMesh.TextureCoords;

                if (texCoords != null)
                {
                    for (int i = 0; i < texCoords.Length; i++)
                    {
                        IntPtr texCoordsPtr = texCoords[i];

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

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }
示例#4
0
 public AnimationModel(AiMesh mesh, AssimpSceneContainer container)
 {
     this.mesh      = mesh;
     this.container = container;
     InitSkinInfo(mesh, container);
 }