示例#1
0
        public FragmentInput VS(AnimatedVertex input)
        {
            Matrix4x4 boneTransformation = Bones.BonesTransformations[input.BoneIndices.X] * input.BoneWeights.X;

            boneTransformation += Bones.BonesTransformations[input.BoneIndices.Y] * input.BoneWeights.Y;
            boneTransformation += Bones.BonesTransformations[input.BoneIndices.Z] * input.BoneWeights.Z;
            boneTransformation += Bones.BonesTransformations[input.BoneIndices.W] * input.BoneWeights.W;

            FragmentInput output;

            output.Position = Vector4.Transform(
                Vector4.Transform(
                    Vector4.Transform(
                        Vector4.Transform(
                            new Vector4(input.Position, 1),
                            boneTransformation),
                        World),
                    View),
                Projection);
            output.UV = input.UV;
            return(output);
        }
示例#2
0
    // Use this for initialization
    void Start()
    {
        mutableMesh = new MutableMesh();
        var grid = new SquareGrid(10, 10, mutableMesh);

        grid.grid[0, 0].Init(SquarePoint.BottomLeft, SquareBaseShape.Triangle);

        SquareNode node = grid.grid[0, 0].GetNode(SquarePoint.BottomLeft);

        v1 = new AnimatedVertex(node.Vertex, 0.5f, () => {
            direction *= -1;
            v1.SetTarget(v1.initialPosition - new Vector3(direction * 0.2f, 0, 0));
        });
        v1.SetTarget(v1.initialPosition + new Vector3(0.2f, 0, 0));

        var meshFilter = gameObject.GetComponent <MeshFilter> ();
        var mesh       = new Mesh()
        {
            vertices  = mutableMesh.GetVertexes().ToArray(),
            triangles = mutableMesh.GetTriangles().ToArray(),
        };

        meshFilter.sharedMesh = mesh;
    }
示例#3
0
        MAnimatedMesh processMesh(Mesh mesh, Scene scene)
        {
            Console.WriteLine("bones: ", mesh.BoneCount, " vertices: ", mesh.VertexCount);

            List <AnimatedVertex> vertices = new List <AnimatedVertex>();
            List <int>            indices  = new List <int>();

            //List<MTexture> textures = new List<MTexture>();
            VertexBoneData[] bones_id_weights_for_each_vertex = new VertexBoneData[mesh.VertexCount];
            for (int i = 0; i < bones_id_weights_for_each_vertex.Length; i++)
            {
                bones_id_weights_for_each_vertex[i].InitVertexBoneData();
            }

            //vertices
            for (int i = 0; i < mesh.VertexCount; i++)
            {
                AnimatedVertex vertex = new AnimatedVertex();
                Vector3        vector;
                vector.X         = mesh.Vertices[i].X;
                vector.Y         = mesh.Vertices[i].Y;
                vector.Z         = mesh.Vertices[i].Z;
                vertex._position = vector;

                if (mesh.Normals != null)
                {
                    vector.X       = mesh.Normals[i].X;
                    vector.Y       = mesh.Normals[i].Y;
                    vector.Z       = mesh.Normals[i].Z;
                    vertex._normal = vector;
                }
                else
                {
                    vertex._normal = Vector3.Zero;
                }

                // in assimp model can have 8 different texture coordinates
                // we only care about the first set of texture coordinates
                if (mesh.HasTextureCoords(0))
                {
                    Vector2 vec = new Vector2(0, 0);
                    vec.X = mesh.TextureCoordinateChannels[0][i].X;
                    vec.Y = mesh.TextureCoordinateChannels[0][i].Y;
                    vertex._textureCoordinate = vec;
                }
                else
                {
                    vertex._textureCoordinate = new Vector2(0.0f, 0.0f);
                }
                vertices.Add(vertex);
            }

            //int[] indices = mesh.GetIndices();

            //indices
            for (int i = 0; i < mesh.FaceCount; i++)
            {
                Face face = mesh.Faces[i];
                // retrieve all indices of the face and store them in the indices vector
                for (int j = 0; j < face.IndexCount; j++)
                {
                    indices.Add(face.Indices[j]);
                }
            }

            //Indices = indices.ToArray();


            //material

            /*
             * if (mesh.MaterialIndex >= 0)
             * {
             * //all pointers created in assimp will be deleted automaticaly when we call import.FreeScene();
             * Material material = scene.Materials[mesh.MaterialIndex];
             * List<MTexture> diffuse_maps = LoadMaterialTexture(material, aiTextureType_DIFFUSE, "texture_diffuse");
             * bool exist = false;
             * for (int i = 0; (i < textures.size()) && (diffuse_maps.size() != 0); i++)
             * {
             *  if (textures[i].path == diffuse_maps[0].path) // ������ ���� �������� 1 �������� ������� � 1 ������� � ����� ����
             *  {
             *    exist = true;
             *  }
             * }
             * if (!exist && diffuse_maps.size() != 0) textures.push_back(diffuse_maps[0]); //������ �������� �� 1 �������� !!!
             *                                                                             //textures.insert(textures.end(), diffuse_maps.begin(), diffuse_maps.end());
             *
             * vector<Texture> specular_maps = LoadMaterialTexture(material, aiTextureType_SPECULAR, "texture_specular");
             * exist = false;
             * for (int i = 0; (i < textures.size()) && (specular_maps.size() != 0); i++)
             * {
             *  if (textures[i].path == specular_maps[0].path) // ������ ���� �������� 1 �������� ������� � 1 ������� � ����� ����
             *  {
             *    exist = true;
             *  }
             * }
             * if (!exist && specular_maps.size() != 0) textures.push_back(specular_maps[0]); //������ �������� �� 1 �������� !!!
             *                                                                               //textures.insert(textures.end(), specular_maps.begin(), specular_maps.end());
             *
             * }
             */

            // Dictionary<string, uint>  m_bone_mapping = new Dictionary<string, uint>();

            // load bones

            for (int i = 0; i < mesh.BoneCount; i++)
            {
                uint   bone_index = 0;
                string bone_name  = mesh.Bones[i].Name;

                //cout << mesh->mBones[i]->mName.data << endl;

                if (!m_bone_mapping.ContainsKey(bone_name)) // ��������� ��� �� � ������� ��������
                {
                    // Allocate an index for a new bone
                    bone_index = (uint)m_num_bones;
                    m_num_bones++;
                    //BoneMatrix bi = new BoneMatrix();
                    //m_bone_matrices.Add(bi);

                    BoneMatrix bbi = new BoneMatrix();
                    //m_bone_matrices[bone_index];
                    bbi.offset_matrix = mesh.Bones[i].OffsetMatrix;
                    // bbi.offset_matrix.Inverse();
                    m_bone_matrices.Add(bbi);

                    //m_bone_matrices[bone_index] = bbi;
                    m_bone_mapping[bone_name] = (uint)bone_index;

                    //cout << "bone_name: " << bone_name << "			 bone_index: " << bone_index << endl;
                }
                else
                {
                    bone_index = (uint)m_bone_mapping[bone_name];
                }

                //bones_id_weights_for_each_vertex = new List<VertexBoneData>(mesh.Bones[i].VertexWeightCount);

                for (int j = 0; j < mesh.Bones[i].VertexWeightCount; j++)
                {
                    int   vertex_id = mesh.Bones[i].VertexWeights[j].VertexID;                   // �� ������� �� ������ ����� �������� �����
                    float weight    = mesh.Bones[i].VertexWeights[j].Weight;
                    bones_id_weights_for_each_vertex[vertex_id].addBoneData(bone_index, weight); // � ������ ������� ����� ����� � �� ���

                    // ������ ������� vertex_id �� ������ ����� � �������� bone_index  ����� ��� weight
                    //cout << " vertex_id: " << vertex_id << "	bone_index: " << bone_index << "		weight: " << weight << endl;
                }
            }


            return(new MAnimatedMesh(vertices, indices.ToArray(), bones_id_weights_for_each_vertex));
        }