public AxisRenderer() { Mesh myMesh = new Mesh(); float smallthingsize = 0.05f; // X Axis MeshUtils.CreateLine(ref myMesh, Vector3.Zero, Vector3.UnitX, Vector3.UnitX); MeshUtils.CreateLine(ref myMesh, Vector3.Zero, Vector3.UnitY, Vector3.UnitY); MeshUtils.CreateLine(ref myMesh, Vector3.Zero, Vector3.UnitZ, Vector3.UnitZ); MeshUtils.CreateArrowCap(ref myMesh, Vector3.UnitX, Vector3.UnitY, Vector3.UnitX, 0.025f, 0.1f, Vector3.UnitX); MeshUtils.CreateArrowCap(ref myMesh, Vector3.UnitZ, Vector3.UnitY, Vector3.UnitZ, 0.025f, 0.1f, Vector3.UnitZ); MeshUtils.CreateArrowCap(ref myMesh, Vector3.UnitY, Vector3.UnitX, Vector3.UnitY, 0.025f, 0.1f, Vector3.UnitY); MeshUtils.CreateLine(ref myMesh, Vector3.UnitX * smallthingsize, Vector3.UnitY * smallthingsize, Vector3.One); MeshUtils.CreateLine(ref myMesh, Vector3.UnitX * smallthingsize, Vector3.UnitZ * smallthingsize, Vector3.One); MeshUtils.CreateLine(ref myMesh, Vector3.UnitY * smallthingsize, Vector3.UnitZ * smallthingsize, Vector3.One); MeshUtils.CreateLine(ref myMesh, Vector3.Zero, Vector3.UnitX * smallthingsize, Vector3.One); MeshUtils.CreateLine(ref myMesh, Vector3.Zero, Vector3.UnitZ * smallthingsize, Vector3.One); MeshUtils.CreateLine(ref myMesh, Vector3.Zero, Vector3.UnitY * smallthingsize, Vector3.One); Mesh = myMesh; Mesh.GenerateVAO(); }
public void GenerateQuad(float w, float h) { if (Mesh == null) { Mesh = new Mesh(); } else { Mesh.Clear(); } Vertex v1 = new Vertex(new Vector3(0, 0, 0), Vector3.One, Vector3.UnitZ, new Vector2(0, 0)); Vertex v2 = new Vertex(new Vector3(w, 0, 0), Vector3.One, Vector3.UnitZ, new Vector2(1, 0)); Vertex v3 = new Vertex(new Vector3(w, h, 0), Vector3.One, Vector3.UnitZ, new Vector2(1, 1)); Vertex v4 = new Vertex(new Vector3(0, h, 0), Vector3.One, Vector3.UnitZ, new Vector2(0, 1)); Mesh.Vertices.Add(v1); Mesh.Vertices.Add(v2); Mesh.Vertices.Add(v3); Mesh.Vertices.Add(v4); Mesh.Indices = new int[] { 0, 1, 2, 0, 2, 3 }; Mesh.GenerateVAO(); }
public void GenerateCircle(float radius, Vector3 color, Vector3 up, int segments = 100) { if (Mesh == null) { Mesh = new Mesh(); } else { Mesh.Clear(); } Vector3 fwd = Vector3.Cross(up, Vector3.UnitY); if (fwd == Vector3.Zero) { fwd = Vector3.Cross(up, Vector3.UnitX); } Vector3 right = Vector3.Cross(fwd, up); Matrix3 m = new Matrix3(right.Normalized(), up.Normalized(), fwd.Normalized()); float angleStep = (float)Math.PI * 2.0f / segments; for (int i = 0; i <= segments; ++i) { float x = (float)Math.Cos(angleStep * i) * radius; float y = (float)Math.Sin(angleStep * i) * radius; Mesh.Vertices.Add(new Vertex(new Vector3(x, 0, y) * m, color)); } Mesh.GenerateVAO(); }
public void GenerateBox(Bounds box) { if (Mesh != null) { Mesh.Clear(); } Mesh myMesh = new Mesh(); /* * MeshUtils.CreateLine(ref myMesh, box.Min, box.Min + Vector3.UnitX, Vector3.UnitX); * MeshUtils.CreateLine(ref myMesh, box.Min, box.Min + Vector3.UnitY, Vector3.UnitX); * MeshUtils.CreateLine(ref myMesh, box.Min, box.Min + Vector3.UnitZ, Vector3.UnitX); * * MeshUtils.CreateLine(ref myMesh, box.Max, box.Max - Vector3.UnitX, Vector3.UnitY); * MeshUtils.CreateLine(ref myMesh, box.Max, box.Max - Vector3.UnitY, Vector3.UnitY); * MeshUtils.CreateLine(ref myMesh, box.Max, box.Max - Vector3.UnitZ, Vector3.UnitY); */ Vector3 frontTL = new Vector3(box.Min.X, box.Min.Y, box.Max.Z); Vector3 frontBL = new Vector3(box.Min.X, box.Max.Y, box.Max.Z); Vector3 frontTR = new Vector3(box.Max.X, box.Min.Y, box.Max.Z); Vector3 frontBR = new Vector3(box.Max.X, box.Max.Y, box.Max.Z); Vector3 backTL = new Vector3(box.Min.X, box.Min.Y, box.Min.Z); Vector3 backBL = new Vector3(box.Min.X, box.Max.Y, box.Min.Z); Vector3 backTR = new Vector3(box.Max.X, box.Min.Y, box.Min.Z); Vector3 backBR = new Vector3(box.Max.X, box.Max.Y, box.Min.Z); // Front rect MeshUtils.CreateLine(ref myMesh, frontTL, frontTR, Vector3.One); MeshUtils.CreateLine(ref myMesh, frontTR, frontBR, Vector3.One); MeshUtils.CreateLine(ref myMesh, frontBR, frontBL, Vector3.One); MeshUtils.CreateLine(ref myMesh, frontBL, frontTL, Vector3.One); MeshUtils.CreateLine(ref myMesh, frontTL, backTL, Vector3.One); MeshUtils.CreateLine(ref myMesh, frontTR, backTR, Vector3.One); MeshUtils.CreateLine(ref myMesh, frontBR, backBR, Vector3.One); MeshUtils.CreateLine(ref myMesh, frontBL, backBL, Vector3.One); MeshUtils.CreateLine(ref myMesh, backTL, backTR, Vector3.One); MeshUtils.CreateLine(ref myMesh, backTR, backBR, Vector3.One); MeshUtils.CreateLine(ref myMesh, backBR, backBL, Vector3.One); MeshUtils.CreateLine(ref myMesh, backBL, backTL, Vector3.One); Mesh = myMesh; Mesh.GenerateVAO(); }
public void GenerateLine(Vector3 from, Vector3 to, Vector3 col) { if (Mesh == null) { Mesh = new Mesh(); } else { Mesh.Clear(); } Mesh.Vertices.Add(new Vertex(from, col)); Mesh.Vertices.Add(new Vertex(to, col)); Mesh.GenerateVAO(); }
public void GenerateBox(Vector3 position, Vector3 size, Vector3 color) { if (Mesh != null) { Mesh.Clear(); } Mesh myMesh = new Mesh(); Vector3 frontTL = new Vector3(position.X - size.X / 2.0f, position.Y - size.Y / 2.0f, position.Z - size.Z / 2.0f); Vector3 frontBL = new Vector3(position.X - size.X / 2.0f, position.Y + size.Y / 2.0f, position.Z - size.Z / 2.0f); Vector3 frontTR = new Vector3(position.X + size.X / 2.0f, position.Y - size.Y / 2.0f, position.Z - size.Z / 2.0f); Vector3 frontBR = new Vector3(position.X + size.X / 2.0f, position.Y + size.Y / 2.0f, position.Z - size.Z / 2.0f); Vector3 backTL = new Vector3(position.X - size.X / 2.0f, position.Y - size.Y / 2.0f, -position.Z + size.Z / 2.0f); Vector3 backBL = new Vector3(position.X - size.X / 2.0f, position.Y + size.Y / 2.0f, -position.Z + size.Z / 2.0f); Vector3 backTR = new Vector3(position.X + size.X / 2.0f, position.Y - size.Y / 2.0f, -position.Z + size.Z / 2.0f); Vector3 backBR = new Vector3(position.X + size.X / 2.0f, position.Y + size.Y / 2.0f, -position.Z + size.Z / 2.0f); // Front rect Vector3 normal = new Vector3(0, 0, 1); MeshUtils.CreateLine(ref myMesh, frontTL, frontTR, color); MeshUtils.CreateLine(ref myMesh, frontTR, frontBR, color); MeshUtils.CreateLine(ref myMesh, frontBR, frontBL, color); MeshUtils.CreateLine(ref myMesh, frontBL, frontTL, color); MeshUtils.CreateLine(ref myMesh, frontTL, backTL, color); MeshUtils.CreateLine(ref myMesh, frontTR, backTR, color); MeshUtils.CreateLine(ref myMesh, frontBR, backBR, color); MeshUtils.CreateLine(ref myMesh, frontBL, backBL, color); MeshUtils.CreateLine(ref myMesh, backTL, backTR, color); MeshUtils.CreateLine(ref myMesh, backTR, backBR, color); MeshUtils.CreateLine(ref myMesh, backBR, backBL, color); MeshUtils.CreateLine(ref myMesh, backBL, backTL, color); Mesh = myMesh; Mesh.GenerateVAO(); }
public void GenerateGridMesh(int cellsX, int cellsY, float cellW, float cellH) { Mesh = new Mesh(); Vector3 offset = new Vector3(-cellsX * cellW / 2.0f, 0, -cellsY * cellH / 2.0f); for (int i = 0; i <= cellsX; i++) { Mesh.Vertices.Add(new Vertex(new Vector3(i * cellW, 0, 0) + offset)); Mesh.Vertices.Add(new Vertex(new Vector3(i * cellW, 0, cellsY * cellH) + offset)); } for (int i = 0; i <= cellsY; i++) { Mesh.Vertices.Add(new Vertex(new Vector3(0, 0, i * cellH) + offset)); Mesh.Vertices.Add(new Vertex(new Vector3(cellsX * cellW, 0, i * cellH) + offset)); } Mesh.GenerateVAO(); }
public static Mesh Load(string path) { if (!File.Exists(path)) { throw new FileNotFoundException("Unable to open \"" + path + "\", does not exist."); } List <Vector4> vertices = new List <Vector4>(); List <Vector3> textureVertices = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); List <FaceIndices> indices = new List <FaceIndices>(); using (StreamReader streamReader = new StreamReader(path)) { while (!streamReader.EndOfStream) { List <string> words = new List <string>(streamReader.ReadLine().ToLower().Split(' ')); words.RemoveAll(s => s == string.Empty); if (words.Count == 0) { continue; } string type = words[0]; words.RemoveAt(0); switch (type) { // vertex case "v": vertices.Add(new Vector4(float.Parse(words[0]), float.Parse(words[1]), float.Parse(words[2]), words.Count < 4 ? 1 : float.Parse(words[3]))); break; case "vt": textureVertices.Add(new Vector3(float.Parse(words[0]), float.Parse(words[1]), words.Count < 3 ? 0 : float.Parse(words[2]))); break; case "vn": normals.Add(new Vector3(float.Parse(words[0]), float.Parse(words[1]), float.Parse(words[2]))); break; // face case "f": foreach (string w in words) { if (w.Length == 0) { continue; } string[] comps = w.Split('/'); FaceIndices fi = new FaceIndices(); fi.VertexId = w; // subtract 1: indices start from 1, not 0 fi.VertexIndex = int.Parse(comps[0]) - 1; if (comps.Length > 1 && comps[1].Length != 0) { fi.TextureCoordIndex = int.Parse(comps[1]) - 1; } if (comps.Length > 2) { fi.NormalIndex = int.Parse(comps[2]) - 1; } indices.Add(fi); } break; default: break; } } } List <uint> meshIndices = new List <uint>(); Mesh m = new Mesh(); Dictionary <String, uint> vertmap = new Dictionary <string, uint>(); uint index = 0; foreach (FaceIndices fi in indices) { Vertex v; if (vertmap.ContainsKey(fi.VertexId)) { meshIndices.Add(vertmap[fi.VertexId]); } else { v = new Vertex(vertices[fi.VertexIndex].Xyz); if (fi.NormalIndex >= 0) { v.Normal = normals[fi.NormalIndex]; } if (fi.TextureCoordIndex >= 0) { v.TexCoord = textureVertices[fi.TextureCoordIndex].Xy; } m.Vertices.Add(v); vertmap.Add(fi.VertexId, index); meshIndices.Add(index); index++; } } m.Indices = meshIndices.ToArray(); m.GenerateVAO(); return(m); }
public static Mesh Load(string path) { if (!File.Exists(path)) { throw new FileNotFoundException("Unable to open \"" + path + "\", does not exist."); } List <Vector4> vertices = new List <Vector4>(); List <Vector3> textureVertices = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); //List<FaceIndices> indices = new List<FaceIndices>(); List <Face> faces = new List <Face>(); using (StreamReader streamReader = new StreamReader(path)) { while (!streamReader.EndOfStream) { List <string> words = new List <string>(streamReader.ReadLine().ToLower().Split(' ')); words.RemoveAll(s => s == string.Empty); if (words.Count == 0) { continue; } string type = words[0]; words.RemoveAt(0); switch (type) { // vertex case "v": vertices.Add(new Vector4(float.Parse(words[0]), float.Parse(words[1]), float.Parse(words[2]), words.Count < 4 ? 1 : float.Parse(words[3]))); break; case "vt": textureVertices.Add(new Vector3(float.Parse(words[0]), float.Parse(words[1]), words.Count < 3 ? 0 : float.Parse(words[2]))); break; case "vn": normals.Add(new Vector3(float.Parse(words[0]), float.Parse(words[1]), float.Parse(words[2]))); break; // face case "f": Face face = new Face(); int i = 0; foreach (string w in words) { if (w.Length == 0) { continue; } string[] comps = w.Split('/'); FaceIndices fi = new FaceIndices(); fi.VertexId = w; // subtract 1: indices start from 1, not 0 fi.VertexIndex = int.Parse(comps[0]) - 1; if (comps.Length > 1 && comps[1].Length != 0) { fi.TextureCoordIndex = int.Parse(comps[1]) - 1; } if (comps.Length > 2) { fi.NormalIndex = int.Parse(comps[2]) - 1; } face.Indices[i] = fi; i++; } // Calculate face Tangent/Bitangent Vector4 pos1 = vertices[face.Indices[0].VertexIndex]; Vector4 pos2 = vertices[face.Indices[1].VertexIndex]; Vector4 pos3 = vertices[face.Indices[2].VertexIndex]; Vector3 uv1 = textureVertices[face.Indices[0].TextureCoordIndex]; Vector3 uv2 = textureVertices[face.Indices[1].TextureCoordIndex]; Vector3 uv3 = textureVertices[face.Indices[2].TextureCoordIndex]; Vector4 edge1 = pos2 - pos1; Vector4 edge2 = pos3 - pos1; Vector3 deltaUV1 = uv2 - uv1; Vector3 deltaUV2 = uv3 - uv1; float f = 1.0f / (deltaUV1.X * deltaUV2.Y - deltaUV2.X * deltaUV1.Y); Vector3 tangent = new Vector3(); tangent.X = f * (deltaUV2.Y * edge1.X - deltaUV1.Y * edge2.X); tangent.Y = f * (deltaUV2.Y * edge1.Y - deltaUV1.Y * edge2.Y); tangent.Z = f * (deltaUV2.Y * edge1.Z - deltaUV1.Y * edge2.Z); tangent.Normalize(); Vector3 bitangent = new Vector3(); bitangent.X = f * (-deltaUV2.X * edge1.X + deltaUV1.X * edge2.X); bitangent.Y = f * (-deltaUV2.X * edge1.Y + deltaUV1.X * edge2.Y); bitangent.Z = f * (-deltaUV2.X * edge1.Z + deltaUV1.X * edge2.Z); bitangent.Normalize(); face.Tangent = tangent; face.Bitangent = bitangent; faces.Add(face); break; default: break; } } } List <int> meshIndices = new List <int>(); Mesh m = new Mesh(); Dictionary <String, int> vertmap = new Dictionary <string, int>(); int index = 0; foreach (Face face in faces) { foreach (FaceIndices fi in face.Indices) { Vertex v; if (vertmap.ContainsKey(fi.VertexId)) { int vindex = vertmap[fi.VertexId]; v = m.Vertices[vindex]; v.Tangent += face.Tangent; v.Bitangent += face.Bitangent; v.Tangent = v.Tangent.Normalized(); v.Bitangent = v.Bitangent.Normalized(); m.Vertices[vindex] = v; meshIndices.Add(vindex); } else { v = new Vertex(vertices[fi.VertexIndex].Xyz); v.Tangent = face.Tangent; v.Bitangent = face.Bitangent; if (fi.NormalIndex >= 0) { v.Normal = normals[fi.NormalIndex]; } if (fi.TextureCoordIndex >= 0) { v.TexCoord = textureVertices[fi.TextureCoordIndex].Xy; } m.Vertices.Add(v); vertmap.Add(fi.VertexId, index); meshIndices.Add(index); index++; } } } m.Indices = meshIndices.ToArray(); m.GenerateVAO(); return(m); }