public static void CenterPivot(ref AMT_MODEL model) { List <Vector3> vertices = new List <Vector3>(); for (int i = 0; i < model.Vertices.Count; i++) { vertices.Add(model.Vertices[i].Position); } BoundingBox bb = BoundingBox.CreateFromPoints(vertices); Vector3 positionAjust = -(bb.Max + bb.Min) / 2; Matrix translation = Matrix.CreateTranslation(positionAjust); //redimensiona os vertices for (int i = 0; i < model.Vertices.Count; i++) { model.Vertices[i].Position = Vector3.Transform(model.Vertices[i].Position, translation); } //reposiciona tb todos os bones e os keyframes if (model.Joints != null) { for (int i = 0; i < model.Joints.Count; i++) { AMT_JOINT v = model.Joints[i]; //como ele mexeu na posicao entao mexe so no raiz if (v.Name == "Root") { v.BindMatrix *= translation; } if (model.Joints[i].ParentID != -1) { v.MatrixAbsolute = model.Joints[i].BindMatrix * model.Joints[model.Joints[i].ParentID].MatrixAbsolute; } else { v.MatrixAbsolute = model.Joints[i].BindMatrix; } v.InverseBindMatrix = Matrix.Invert(translation) * v.InverseBindMatrix; //mexe so no raiz if (v.Name == "Root") { for (int j = 0; j < v.KFData.Count; j++) { AMT_KF p = v.KFData[j]; p.BindMatrix *= translation; v.KFData[j] = p; } } model.Joints[i] = v; } } }
public static void SetYUp(ref AMT_MODEL model, Vector3 upVector) { if (upVector == Vector3.UnitY) { return; } //aplica a rotacao Matrix rotation; if (upVector == Vector3.UnitX) { rotation = Matrix.CreateRotationZ(-MathHelper.PiOver2); } else { rotation = Matrix.CreateRotationX(-MathHelper.PiOver2); } List <Vector3> vertices = new List <Vector3>(); for (int i = 0; i < model.Vertices.Count; i++) { AMT_VERTEX v = model.Vertices[i]; Vector3.Transform(ref v.Position, ref rotation, out v.Position); Vector3.TransformNormal(ref v.Normal, ref rotation, out v.Normal); } if (model.Joints != null) { for (int i = 0; i < model.Joints.Count; i++) { AMT_JOINT v = model.Joints[i]; //como ele mexeu na posicao entao mexe so no raiz if (v.Name == "Root") { v.BindMatrix *= rotation; } if (model.Joints[i].ParentID != -1) { v.MatrixAbsolute = model.Joints[i].BindMatrix * model.Joints[model.Joints[i].ParentID].MatrixAbsolute; } else { v.MatrixAbsolute = model.Joints[i].BindMatrix; } v.InverseBindMatrix = Matrix.Invert(rotation) * v.InverseBindMatrix; //mexe so no raiz if (v.Name == "Root") { for (int j = 0; j < v.KFData.Count; j++) { AMT_KF p = v.KFData[j]; p.BindMatrix *= rotation; v.KFData[j] = p; } } model.Joints[i] = v; } } }
//deixa todos os modelos com tamanho 1 para serem redimensionados no editor public static void UniformScale(ref AMT_MODEL model) { List <Vector3> vertices = new List <Vector3>(); for (int i = 0; i < model.Vertices.Count; i++) { vertices.Add(model.Vertices[i].Position); } BoundingSphere sphere = BoundingSphere.CreateFromPoints(vertices); //redimensiona os vertices float scalefactor = 1.0f / sphere.Radius; //redimensiona tb todos os bones e os keyframes Matrix scale = Matrix.CreateScale(scalefactor); for (int i = 0; i < model.Vertices.Count; i++) { model.Vertices[i].Position = Vector3.Transform(model.Vertices[i].Position, scale); model.Vertices[i].Normal = Vector3.TransformNormal(model.Vertices[i].Normal, scale); } if (model.Joints != null) { for (int i = 0; i < model.Joints.Count; i++) { AMT_JOINT v = model.Joints[i]; //como ele mexeu na posicao entao mexe so no raiz if (v.Name == "Root") { v.BindMatrix *= scale; } if (model.Joints[i].ParentID != -1) { v.MatrixAbsolute = model.Joints[i].BindMatrix * model.Joints[model.Joints[i].ParentID].MatrixAbsolute; } else { v.MatrixAbsolute = model.Joints[i].BindMatrix; } v.InverseBindMatrix = Matrix.Invert(scale) * v.InverseBindMatrix; if (v.Name == "Root") { for (int j = 0; j < v.KFData.Count; j++) { AMT_KF p = v.KFData[j]; p.BindMatrix *= scale; v.KFData[j] = p; } } model.Joints[i] = v; } } }