<commit_before>﻿using CSharpGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FirstSightOfAssimpNet
{
    class AnimationModel : IBufferSource
    {
        public readonly Assimp.Mesh mesh;
        public readonly AssimpSceneContainer container;

        private uvec4[] boneIDs;
        private vec4[] boneWeights;

        public AnimationModel(Assimp.Mesh mesh, AssimpSceneContainer container)
        {
            this.mesh = mesh;
            this.container = container;
            InitBoneInfo(mesh, container);
        }

        private void InitBoneInfo(Assimp.Mesh mesh, AssimpSceneContainer container)
        {
            var boneIDs = new uvec4[mesh.VertexCount];
            var boneWeights = new vec4[mesh.VertexCount];
            List<BoneInfo> bones = new List<BoneInfo>();
            AllBones allBones = container.GetAllBones();
            Dictionary<string, uint> nameIndexDict = allBones.nameIndexDict;
            for (int i = 0; i < mesh.BoneCount; i++)
            {
                Assimp.Bone bone = mesh.Bones[i];
                uint boneIndex = nameIndexDict[bone.Name];

                for (int j = 0; j < bone.VertexWeightCount; j++)
                {
                    Assimp.VertexWeight vertexWeight = bone.VertexWeights[j];
                    uint vertexID = vertexWeight.VertexID;
                    for (int t = 0; t < 4; t++)
                    {
                        if (boneWeights[vertexID][t] == 0.0f)
                        {
                            boneIDs[vertexID][t] = boneIndex;
                            boneWeights[vertexID][t] = vertexWeight.Weight;
                            break;
                        }
                    }
                }
            }
            this.boneIDs = boneIDs;
            this.boneWeights = boneWeights;
        }

        public Texture Texture
        {
            get
            {
                int index = this.mesh.MaterialIndex;
                var texture = this.container.textures[index];
                return texture;
            }
        }

        public int BoneCount
        {
            get { return this.container.GetAllBones().boneInfos.Length; }
        }

        public const string strPosition = "position";
        private VertexBuffer positionBuffer;
        public const string strNormal = "normal";
        private VertexBuffer normalBuffer;
        public const string strTexCoord = "texCoord";
        private VertexBuffer texCoordBuffer;
        public const string strBoneIDs = "boneIDs";
        private VertexBuffer boneIDsBuffer;
        public const string strWeights = "weights";
        private VertexBuffer weightsBuffer;

        private IDrawCommand drawCommand;

        #region IBufferSource 成员

        public IEnumerable<VertexBuffer> GetVertexAttribute(string bufferName)
        {
            if (strPosition == bufferName)
            {
                if (this.positionBuffer == null)
                {
                    this.positionBuffer = this.mesh.Vertices.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw);
                }

                yield return this.positionBuffer;
            }
            else if (strNormal == bufferName)
            {
                if (this.normalBuffer == null)
                {
                    this.normalBuffer = this.mesh.Normals.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw);
                }

                yield return this.normalBuffer;
            }
            else if (strTexCoord == bufferName)
            {
                if (this.texCoordBuffer == null)
                {
                    Assimp.Vector3D[] texCoords = this.mesh.GetTextureCoords(0);
                    if (texCoords == null)
                    {
                        texCoords = new Assimp.Vector3D[this.mesh.VertexCount];
                        for (int i = 0; i < texCoords.Length; i++)
                        {
                            texCoords[i] = new Assimp.Vector3D(-1, -1, -1);
                        }
                    }
                    this.texCoordBuffer = texCoords.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw);
                }

                yield return this.texCoordBuffer;
            }
            else if (strBoneIDs == bufferName)
            {
                if (this.boneIDsBuffer == null)
                {
                    this.boneIDsBuffer = this.boneIDs.GenVertexBuffer(VBOConfig.UVec4, BufferUsage.StaticDraw);
                }

                yield return this.boneIDsBuffer;
            }
            else if (strWeights == bufferName)
            {
                if (this.weightsBuffer == null)
                {
                    this.weightsBuffer = this.boneWeights.GenVertexBuffer(VBOConfig.Vec4, BufferUsage.StaticDraw);
                }

                yield return this.weightsBuffer;
            }
            else
            {
                throw new ArgumentException();
            }
        }

        public IEnumerable<IDrawCommand> GetDrawCommand()
        {
            if (this.drawCommand == null)
            {
                int faceCount = this.mesh.FaceCount;
                var indexes = new uint[faceCount * 3];
                for (int i = 0; i < faceCount; i++)
                {
                    for (int t = 0; t < 3; t++)
                    {
                        indexes[i * 3 + t] = this.mesh.Faces[i].Indices[t];
                    }
                }
                IndexBuffer indexBuffer = indexes.GenIndexBuffer(BufferUsage.StaticDraw);
                this.drawCommand = new DrawElementsCmd(indexBuffer, DrawMode.Triangles);
            }

            yield return this.drawCommand;
        }

        #endregion
    }
}
<commit_msg>InitSkinInfo<commit_after>﻿using CSharpGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FirstSightOfAssimpNet
{
    class AnimationModel : IBufferSource
    {
        public readonly Assimp.Mesh mesh;
        public readonly AssimpSceneContainer container;

        private uvec4[] boneIDs;
        private vec4[] boneWeights;

        public AnimationModel(Assimp.Mesh mesh, AssimpSceneContainer container)
        {
            this.mesh = mesh;
            this.container = container;
            InitSkinInfo(mesh, container);
        }

        private void InitSkinInfo(Assimp.Mesh mesh, AssimpSceneContainer container)
        {
            var boneIDs = new uvec4[mesh.VertexCount];
            var boneWeights = new vec4[mesh.VertexCount];
            AllBones allBones = container.GetAllBones();
            Dictionary<string, uint> nameIndexDict = allBones.nameIndexDict;
            for (int i = 0; i < mesh.BoneCount; i++)
            {
                Assimp.Bone bone = mesh.Bones[i]; // bones that influence this mesh.
                uint boneIndex = nameIndexDict[bone.Name];

                for (int j = 0; j < bone.VertexWeightCount; j++)
                {
                    Assimp.VertexWeight 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;
        }

        public Texture Texture
        {
            get
            {
                int index = this.mesh.MaterialIndex;
                var texture = this.container.textures[index];
                return texture;
            }
        }

        public int BoneCount
        {
            get { return this.container.GetAllBones().boneInfos.Length; }
        }

        public const string strPosition = "position";
        private VertexBuffer positionBuffer;
        public const string strNormal = "normal";
        private VertexBuffer normalBuffer;
        public const string strTexCoord = "texCoord";
        private VertexBuffer texCoordBuffer;
        public const string strBoneIDs = "boneIDs";
        private VertexBuffer boneIDsBuffer;
        public const string strWeights = "weights";
        private VertexBuffer weightsBuffer;

        private IDrawCommand drawCommand;

        #region IBufferSource 成员

        public IEnumerable<VertexBuffer> GetVertexAttribute(string bufferName)
        {
            if (strPosition == bufferName)
            {
                if (this.positionBuffer == null)
                {
                    this.positionBuffer = this.mesh.Vertices.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw);
                }

                yield return this.positionBuffer;
            }
            else if (strNormal == bufferName)
            {
                if (this.normalBuffer == null)
                {
                    this.normalBuffer = this.mesh.Normals.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw);
                }

                yield return this.normalBuffer;
            }
            else if (strTexCoord == bufferName)
            {
                if (this.texCoordBuffer == null)
                {
                    Assimp.Vector3D[] texCoords = this.mesh.GetTextureCoords(0);
                    if (texCoords == null)
                    {
                        texCoords = new Assimp.Vector3D[this.mesh.VertexCount];
                        for (int i = 0; i < texCoords.Length; i++)
                        {
                            texCoords[i] = new Assimp.Vector3D(-1, -1, -1);
                        }
                    }
                    this.texCoordBuffer = texCoords.GenVertexBuffer(VBOConfig.Vec3, BufferUsage.StaticDraw);
                }

                yield return this.texCoordBuffer;
            }
            else if (strBoneIDs == bufferName)
            {
                if (this.boneIDsBuffer == null)
                {
                    this.boneIDsBuffer = this.boneIDs.GenVertexBuffer(VBOConfig.UVec4, BufferUsage.StaticDraw);
                }

                yield return this.boneIDsBuffer;
            }
            else if (strWeights == bufferName)
            {
                if (this.weightsBuffer == null)
                {
                    this.weightsBuffer = this.boneWeights.GenVertexBuffer(VBOConfig.Vec4, BufferUsage.StaticDraw);
                }

                yield return this.weightsBuffer;
            }
            else
            {
                throw new ArgumentException();
            }
        }

        public IEnumerable<IDrawCommand> GetDrawCommand()
        {
            if (this.drawCommand == null)
            {
                int faceCount = this.mesh.FaceCount;
                var indexes = new uint[faceCount * 3];
                for (int i = 0; i < faceCount; i++)
                {
                    for (int t = 0; t < 3; t++)
                    {
                        indexes[i * 3 + t] = this.mesh.Faces[i].Indices[t];
                    }
                }
                IndexBuffer indexBuffer = indexes.GenIndexBuffer(BufferUsage.StaticDraw);
                this.drawCommand = new DrawElementsCmd(indexBuffer, DrawMode.Triangles);
            }

            yield return this.drawCommand;
        }

        #endregion
    }
}

{'fraction_non_alphanumeric': 0.07995196019559063, 'fraction_numerical': 0.004803980440936776, 'mean_word_length': 1.3080578103345872, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6029235', 'n_tokens_mistral': 3012, 'n_tokens_neox': 2827, 'n_words': 627}