public void RecalculateNormalsFlatShaded() {
      var verticesCopy = new List<Vector3>(Vertices);
      var uvsCopy = new List<Vector3>(TexCoords);
      Vertices.Clear();
      Normals.Clear();
      TexCoords.Clear();

      for (int i = 0; i < Faces.Count; i++) {
        int index = Vertices.Count;
        Vertices.Add(verticesCopy[Faces[i].A]);
        Vertices.Add(verticesCopy[Faces[i].B]);
        Vertices.Add(verticesCopy[Faces[i].C]);

        TexCoords.Add(uvsCopy[Faces[i].A]);
        TexCoords.Add(uvsCopy[Faces[i].B]);
        TexCoords.Add(uvsCopy[Faces[i].C]);

        var face = new GenericFace(index, index + 1, index + 2, Faces[i].Material, Faces[i].Section);
        Faces[i] = face;

        var normal = face.CalculateNormal(Vertices);
        Normals.Add(normal);
        Normals.Add(normal);
        Normals.Add(normal);
      }
    }
    public void AddMesh(GenericMesh other, Matrix4x4 transform) {
      int offset = Vertices.Count;

      var newVerts = other.Vertices.Select(vertex => Vector3.Transform(vertex, transform));
      var newNormals = other.Normals.Select(normal => Vector3.TransformNormal(normal, transform));
      var newFaces = other.Faces.Select(face => GenericFace.OffsetIndices(face, offset));

      Vertices.AddRange(newVerts);
      Normals.AddRange(newNormals);
      TexCoords.AddRange(other.TexCoords);
      Faces.AddRange(newFaces);
    }
    public void ApplyExportOptions(MeshExportOptions options) {
      if (!exportOptionsApplied || !options.Equals(ExportOptions)) {
        // If these settings haven't already been applied

        if (options.NormalExportMode == MeshNormalExportMode.CalculateNormalsFlatShaded) {
          RecalculateNormalsFlatShaded();
        } else if (options.NormalExportMode == MeshNormalExportMode.CalculateNormalsSmoothShaded) {
          RecalculateNormalsSmoothShaded();
        } else {
          // Use already set up normals
        }

        Vertices = Vertices.Select(vertex => Vector3.Transform(vertex, options.Matrix)).ToList();
        Normals = Normals
                  .Select(normal => Vector3.Normalize(Vector3.TransformNormal(normal, options.Matrix) *
                                                      (options.InvertNormals ? -1.0f : 1.0f)))
                  .ToList();
        if (options.ReverseFaceWinding) {
          Faces = Faces.Select(face => GenericFace.ReverseWinding(face)).ToList();
        }

        exportOptionsApplied = true;
      }
    }
 public static GenericFace ReverseWinding(GenericFace initialValue) {
   return new GenericFace(initialValue.A, initialValue.C, initialValue.B, initialValue.Material,
                          initialValue.Section);
 }
 public static GenericFace OffsetIndices(GenericFace initialValue, int offset) {
   return new GenericFace(initialValue.A + offset, initialValue.B + offset, initialValue.C + offset,
                          initialValue.Material, initialValue.Section);
 }