public static void Colorize(Mesh mesh, Vector4 color) { if (mesh.Textured) for (int i = 0; i < mesh.VertexCount; ++i) mesh.texturedVerticies[i].Color = color; else for (int i = 0; i < mesh.VertexCount; ++i) mesh.verticies[i].Color = color; }
public Mesh CreatePatch(Vector3[] points, int tesselation) { var r = new Mesh(); r.indicies = CreatePatchIndices(tesselation, false); r.verticies = CreatePatchVertices(points, tesselation, false); return r; }
public static Mesh MorphToSplineCopy( Mesh mesh, Vector3 axis, Gem.Math.Spline spline) { var result = Copy(mesh); MorphToSpline(result, axis, spline); return result; }
public static Mesh AlignToSplineCopy( Mesh mesh, Vector3 axis, Vector3 splineRotationAxis, Gem.Math.Spline spline, float distance) { var result = Copy(mesh); AlignToSpline(result, axis, splineRotationAxis, spline, distance); return result; }
/// <summary> /// Scale mesh part so that it fits - and fills - the supplied bounding box. /// </summary> /// <param name="box"></param> /// <returns></returns> public static void FitToBox(Mesh mesh, BoundingBox box) { var partBox = Gen.CalculateBoundingBox(mesh); var partDims = partBox.Max - partBox.Min; var boxDims = box.Max - box.Min; Morph(mesh, (v) => { var rel = v - partBox.Min; return box.Min + ((rel / partDims) * boxDims); }); }
public static Mesh CreateQuad() { var result = new Mesh(); result.verticies = new Vertex[4]; result.verticies[0].Position = new Vector3(0, 0, 0); result.verticies[1].Position = new Vector3(1, 0, 0); result.verticies[2].Position = new Vector3(1, 1, 0); result.verticies[3].Position = new Vector3(0, 1, 0); result.indicies = new short[] { 0, 1, 2, 3, 0, 2 }; return result; }
/// <summary> /// Align the object with the spline. Transforms object so that its origin is on the spline. /// </summary> /// <returns></returns> public static void AlignToSpline( Mesh mesh, Vector3 axis, Vector3 splineRotationAxis, Gem.Math.Spline spline, float distance) { Morph(mesh, (v) => { var splinePoint = spline.Point(distance); var splineTangent = spline.Tangent(distance); var m = Matrix.CreateFromAxisAngle(Vector3.Normalize(splineRotationAxis), Gem.Math.Vector.AngleBetweenVectors(axis, splineTangent)); return splinePoint + Vector3.Transform(v, m); }); }
public static Mesh CreateLine(Vector3 v0, Vector3 v1, Vector3 normal, float width) { var lineV = v1 - v0; lineV.Normalize(); var lineP = Vector3.Cross(lineV, normal); var result = new Mesh(); result.verticies = new Vertex[4]; result.verticies[0].Position = v0 + (lineP * (width / 2.0f)); result.verticies[1].Position = v0 - (lineP * (width / 2.0f)); result.verticies[2].Position = v1 - (lineP * (width / 2.0f)); result.verticies[3].Position = v1 + (lineP * (width / 2.0f)); result.indicies = new short[] { 0, 1, 2, 3, 0, 2 }; return result; }
public static BoundingBox CalculateBoundingBox(Mesh mesh) { Vector3 minimum = mesh.verticies[0].Position; Vector3 maximum = mesh.verticies[0].Position; foreach (var vert in mesh.verticies) { if (vert.Position.X < minimum.X) minimum.X = vert.Position.X; if (vert.Position.Y < minimum.Y) minimum.Y = vert.Position.Y; if (vert.Position.Z < minimum.Z) minimum.Z = vert.Position.Z; if (vert.Position.X > maximum.X) maximum.X = vert.Position.X; if (vert.Position.Y > maximum.Y) maximum.Y = vert.Position.Y; if (vert.Position.Z > maximum.Z) maximum.Z = vert.Position.Z; } return new BoundingBox(minimum, maximum); }
/// <summary> /// Morph the object onto a spline. Expects the object's origin to already be aligned with the spline. /// </summary> /// <returns></returns> public static void MorphToSpline( Mesh mesh, Vector3 axis, Gem.Math.Spline spline) { Morph(mesh, (v) => { var distance = Gem.Math.Vector.ProjectAOntoB(v, axis).Length() / axis.Length(); var splinePoint = spline.Point(distance); var splineTangent = spline.Tangent(distance); var rel = (axis * distance) - v; var m = Matrix.CreateFromAxisAngle(Vector3.Normalize(spline.RotationAxis),//splineRotationAxis), Gem.Math.Vector.AngleBetweenVectors(axis, splineTangent)); return splinePoint + Vector3.Transform(rel, m); }); }
public static Mesh Copy(Mesh mesh) { var result = new Mesh(); if (mesh.texturedVerticies != null) { result.texturedVerticies = new TexturedVertex[mesh.texturedVerticies.Length]; for (int i = 0; i < mesh.texturedVerticies.Length; ++i) result.texturedVerticies[i] = mesh.texturedVerticies[i]; } if (mesh.verticies != null) { result.verticies = new Vertex[mesh.verticies.Length]; for (int i = 0; i < mesh.verticies.Length; ++i) result.verticies[i] = mesh.verticies[i]; } result.Textured = mesh.Textured; result.indicies = new short[mesh.indicies.Length]; CopyIndicies(result.indicies, 0, mesh.indicies); return result; }
public static Mesh CreateTexturedQuad() { var result = new Mesh(); result.Textured = true; result.texturedVerticies = new TexturedVertex[4]; result.texturedVerticies[0].Position = new Vector3(-0.5f, -0.5f, 0); result.texturedVerticies[1].Position = new Vector3( 0.5f, -0.5f, 0); result.texturedVerticies[2].Position = new Vector3( 0.5f, 0.5f, 0); result.texturedVerticies[3].Position = new Vector3(-0.5f, 0.5f, 0); for (int i = 0; i < 4; ++i) result.texturedVerticies[i].Texcoord = new Vector2(result.texturedVerticies[i].Position.X + 0.5f, result.texturedVerticies[i].Position.Y + 0.5f); result.indicies = new short[] { 0, 1, 2, 3, 0, 2 }; return result; }
//Explode mesh into unique triangles for facetted look. public static Mesh FacetCopy(Mesh m) { var result = new Mesh(); result.Textured = m.Textured; if (m.Textured) { result.texturedVerticies = new TexturedVertex[m.indicies.Length]; result.indicies = new short[m.indicies.Length]; for (short i = 0; i < m.indicies.Length; ++i) { result.texturedVerticies[i] = m.texturedVerticies[m.indicies[i]]; result.indicies[i] = i; } for (short i = 0; i < result.texturedVerticies.Length; i += 3) { var normal = -Gen.CalculateNormal(result, i, i + 1, i + 2); for (int j = 0; j < 3; ++j) result.texturedVerticies[i + j].Normal = normal; } } else { result.verticies = new Vertex[m.indicies.Length]; result.indicies = new short[m.indicies.Length]; for (short i = 0; i < m.indicies.Length; ++i) { result.verticies[i] = m.verticies[m.indicies[i]]; result.indicies[i] = i; } for (short i = 0; i < result.verticies.Length; i += 3) { var normal = -Gen.CalculateNormal(result, i, i + 1, i + 2); for (int j = 0; j < 3; ++j) result.verticies[i + j].Normal = normal; } } return result; }
public static Mesh CreateUnitPolygon(int sides) { var result = new Mesh(); result.verticies = new Vertex[sides + 1]; result.verticies[0].Position = new Vector3(0, 0, 0); for (int i = 0; i < sides; ++i) { var matrix = Matrix.CreateRotationZ(MathHelper.ToRadians(-(360.0f/sides) * i)); result.verticies[i + 1].Position = Vector3.Transform(new Vector3(0, 1, 0.0f), matrix); } result.indicies = new short[3 * sides]; for (int i = 0; i < sides; ++i) { result.indicies[i * 3] = 0; result.indicies[(i * 3)+2] = (short)(i + 1); result.indicies[(i * 3)+1] = (short)(i == (sides - 1) ? 1 : (i + 2)); } return result; }
public static Mesh CreateCube() { var result = new Mesh(); result.verticies = new Vertex[8]; result.verticies[0].Position = new Vector3( -0.5f, -0.5f, -0.5f); result.verticies[1].Position = new Vector3( 0.5f, -0.5f, -0.5f); result.verticies[2].Position = new Vector3( 0.5f, 0.5f, -0.5f); result.verticies[3].Position = new Vector3( -0.5f, 0.5f, -0.5f); result.verticies[4].Position = new Vector3( -0.5f, -0.5f, 0.5f); result.verticies[5].Position = new Vector3( 0.5f, -0.5f, 0.5f); result.verticies[6].Position = new Vector3( 0.5f, 0.5f, 0.5f); result.verticies[7].Position = new Vector3( -0.5f, 0.5f, 0.5f); result.indicies = new short[] { 0, 2, 1, 3, 2, 0, 0, 1, 4, 4, 1, 5, 1, 2, 5, 5, 2, 6, 2, 3, 6, 6, 3, 7, 3, 0, 7, 7, 0, 4, 4, 5, 6, 7, 4, 6 }; return result; }
public static void Morph(Mesh mesh, Func<Vector3, Vector3> func) { for (int i = 0; i < mesh.verticies.Length; ++i) mesh.verticies[i].Position = func(mesh.verticies[i].Position); }
public static Vertex[] GetVerticies(Mesh mesh, int startIndex, int Length) { var r = new Vertex[Length]; for (int i = 0; i < Length; ++i) r[i] = mesh.verticies[i + startIndex]; return r; }
public static Mesh MorphCopy(Mesh mesh, Func<Vector3, Vector3> func) { var result = Copy(mesh); Morph(result, func); return result; }
public static Mesh FitToBoxCopy(Mesh mesh, BoundingBox box) { var result = Copy(mesh); FitToBox(result, box); return result; }
public static void Transform(Mesh mesh, Matrix m) { Transform(mesh, m, 0, mesh.verticies.Length); }
public static void Transform(Mesh mesh, Matrix m, int start, int count) { if (start < 0) start = mesh.verticies.Length - start; for (int i = start; i < start + count; ++i) mesh.verticies[i].Position = Vector3.Transform(mesh.verticies[i].Position, m); Vector3 scale; Vector3 trans; Quaternion rot; m.Decompose(out scale, out rot, out trans); for (int i = start; i < start + count; ++i) mesh.verticies[i].Normal = Vector3.Transform(mesh.verticies[i].Normal, rot); }
public static Mesh TransformCopy(Mesh mesh, Matrix m) { return TransformCopy(mesh, m, 0, mesh.verticies.Length); }
public static Mesh TransformCopy(Mesh mesh, Matrix m, int start, int count) { var result = Copy(mesh); Transform(result, m, start, count); return result; }
public static Vector3 CalculateNormal(Mesh part, int a, int b, int c) { return Vector3.Normalize(Vector3.Cross(part.GetVertex(b).Position - part.GetVertex(a).Position, part.GetVertex(c).Position - part.GetVertex(a).Position)); }
public static Mesh ColorizeCopy(Mesh mesh, Vector4 color) { var result = Copy(mesh); Colorize(result, color); return result; }
public void AddPart(Mesh part) { parts.Add(part); }
public RawModel(Mesh part) { AddPart(part); }
public static Mesh Merge(params Mesh[] parts) { var result = new Mesh(); result.verticies = new Vertex[parts.Sum((p) => p.verticies.Length)]; result.indicies = new short[parts.Sum((p) => p.indicies.Length)]; int vCount = 0; int iCount = 0; foreach (var part in parts) { for (int i = 0; i < part.verticies.Length; ++i) result.verticies[i + vCount] = part.verticies[i]; for (int i = 0; i < part.indicies.Length; ++i) result.indicies[i + iCount] = (short)(part.indicies[i] + vCount); vCount += part.verticies.Length; iCount += part.indicies.Length; } return result; }