public override int AddFace(int v1, int v2, int v3, int v4) { MeshFace face = new MeshFace(_Mesh.Vertices[v1], _Mesh.Vertices[v2], _Mesh.Vertices[v3], _Mesh.Vertices[v4]); _Mesh.Faces.Add(face); return(_Mesh.Faces.Count - 1); }
public bool IsCombinable(MeshFace addition) { return(position == addition.position && length == addition.length && vertexData == addition.vertexData && isSingleSided == addition.isSingleSided); }
public Mesh MeshFromPoints(List <Point3d> P, List <Color> C, int U, int V) { //, C As list(Of Color) Mesh M = new Mesh(); M.Vertices.AddVertices(P); if ((C != null)) { M.VertexColors.AppendColors(C.ToArray()); } for (int j = 0; j <= V - 2; j++) { for (int i = 0; i <= U - 2; i++) { int n0 = 0; int n1 = 0; int n2 = 0; int n3 = 0; n0 = j * U + i; n1 = n0 + 1; n2 = n0 + U; n3 = n1 + U; MeshFace face = new MeshFace(n0, n1, n3, n2); M.Faces.AddFace(face); } } return(M); }
public double v2FaceWVal(Mesh M, List <double> Vv, int i) { double fV = 0, fDist = 0, dists = 0; MeshFace f = M.Faces.GetFace(i); if (f.IsValid(M.Vertices.Count)) { Point3d fc = getFaceCenter(M, i); fDist = fc.DistanceTo(M.Vertices[f.A]); dists += fDist; fV += Vv[f.A] * fDist; fDist = fc.DistanceTo(M.Vertices[f.B]); dists += fDist; fV += Vv[f.B] * fDist; fDist = fc.DistanceTo(M.Vertices[f.C]); dists += fDist; fV += Vv[f.C] * fDist; if (f.IsQuad) { fDist = fc.DistanceTo(M.Vertices[f.D]); dists += fDist; fV += Vv[f.D] * fDist; } fV /= dists; } return(fV); }
/***************************************************/ public static List <MeshFace> MeshFaces(BH.oM.Geometry.Mesh mesh, ISurfaceProperty property = null, string name = null) { List <MeshFace> meshFaces = new List <MeshFace>(); foreach (Face face in mesh.Faces) { List <Node> nodes = new List <Node>(); nodes.Add(Create.Node(mesh.Vertices[face.A])); nodes.Add(Create.Node(mesh.Vertices[face.B])); nodes.Add(Create.Node(mesh.Vertices[face.C])); if (BH.Engine.Geometry.Query.IsQuad(face)) { nodes.Add(Create.Node(mesh.Vertices[face.D])); } MeshFace mf = new MeshFace() { Property = property, Nodes = nodes }; if (name != null) { mf.Name = name; } meshFaces.Add(mf); } return(meshFaces); }
public static Mesh Create(double r_ = 1) { Mesh mesh = new Mesh(); float length = (float)r_; float width = (float)r_; float height = (float)r_; Point3f p0 = new Point3f(length * .5f, width * .5f, -height * .5f); // Point3f p1 = new Point3f(-length * .5f, -width * .5f, -height * .5f); // Point3f p2 = new Point3f(-length * .5f, width * .5f, height * .5f); Point3f p3 = new Point3f(length * .5f, -width * .5f, height * .5f); Point3f[] vertices = new Point3f[] { p0, p1, p2, p3 }; MeshFace[] mf = new MeshFace[] { new MeshFace(1, 0, 3), new MeshFace(0, 1, 2), new MeshFace(2, 3, 0), new MeshFace(3, 2, 1), }; mesh.Faces.AddFaces(mf); mesh.Vertices.AddVertices(vertices); mesh.RebuildNormals(); mesh.Clean(); return(mesh); }
/// <summary>Gets the current intensity glow intensity, using the glow attenuation factor</summary> /// <param name="Vertices">The verticies to which the glow is to be applied</param> /// <param name="Face">The face which these vertices make up</param> /// <param name="GlowAttenuationData">The current glow attenuation</param> /// <param name="CameraX">The X-position of the camera</param> /// <param name="CameraY">The Y-position of the camera</param> /// <param name="CameraZ">The Z-position of the camera</param> /// <returns></returns> private static double GetDistanceFactor(VertexTemplate[] Vertices, ref MeshFace Face, ushort GlowAttenuationData, double CameraX, double CameraY, double CameraZ) { if (Face.Vertices.Length == 0) { return(1.0); } GlowAttenuationMode mode; double halfdistance; Glow.SplitAttenuationData(GlowAttenuationData, out mode, out halfdistance); int i = (int)Face.Vertices[0].Index; double dx = Vertices[i].Coordinates.X - CameraX; double dy = Vertices[i].Coordinates.Y - CameraY; double dz = Vertices[i].Coordinates.Z - CameraZ; switch (mode) { case GlowAttenuationMode.DivisionExponent2: { double t = dx * dx + dy * dy + dz * dz; return(t / (t + halfdistance * halfdistance)); } case GlowAttenuationMode.DivisionExponent4: { double t = dx * dx + dy * dy + dz * dz; t *= t; halfdistance *= halfdistance; return(t / (t + halfdistance * halfdistance)); } default: return(1.0); } }
public Mesh GenereateDeformedMesh(double t) { Mesh mesh = new Mesh(); List <Vector3D> dispVecs = new List <Vector3D>(); foreach (Node node in nodes) { Vector3D dispVec = new Vector3D(a[node.dofX], a[node.dofY], a[node.dofZ]); dispVec = dispVec * t; dispVecs.Add(dispVec); mesh.Vertices.Add(node.x + dispVec.X, node.y + dispVec.Y, node.z + dispVec.Z); } double max = dispVecs.Max(x => x.Length); Grasshopper.GUI.Gradient.GH_Gradient grad = StaticFunctions.GetStandardGradient(); foreach (Vector3D vec in dispVecs) { double normVal = vec.Length / max; System.Drawing.Color color = grad.ColourAt(normVal); mesh.VertexColors.Add(color.R, color.G, color.B); } foreach (ShellElement elem in this.elements) { MeshFace face = new MeshFace(elem.Nodes[0].Id, elem.Nodes[1].Id, elem.Nodes[2].Id); mesh.Faces.AddFace(face); } return(mesh); }
public MeshFace[] TriangulateClosedPolyline() { if (!IsClosed) { return(null); } int triangle_count = (Count - 3) * 3; if (triangle_count < 1) { return(null); } int[] triangles = new int[triangle_count]; if (!UnsafeNativeMethods.TLC_Triangulate3dPolygon(Count, ToArray(), triangle_count, triangles)) { return(null); } int face_count = triangle_count / 3; MeshFace[] rc = new MeshFace[face_count]; for (int i = 0; i < face_count; i++) { rc[i] = new MeshFace(triangles[i * 3], triangles[i * 3 + 1], triangles[i * 3 + 2]); } return(rc); }
public static Polyline FaceBoundary(this Mesh mesh, MeshFace face) { var boundary = new Polyline(mesh.GetFaceVertices(face)); boundary.Close(); return(boundary); }
public bool IsExtendable(MeshFace extension) { return(position + length + 1 == extension.position && height == extension.height && vertexData == extension.vertexData && isSingleSided == extension.isSingleSided); }
public void processLine(string line, ref List <Point3d> points3D, ref List <Point3d> points2D, ref List <MeshFace> faces3D, ref List <MeshFace> faces2D) { string[] lineData = line.Split(' '); string type = lineData[0]; switch (type) { case ("v"): //3d point Point3d p3D = new Point3d(Convert.ToDouble(lineData[1]), Convert.ToDouble(lineData[2]), Convert.ToDouble(lineData[3])); points3D.Add(p3D); break; case ("vt"): //2d point Point3d p2D = new Point3d(Convert.ToDouble(lineData[1]), Convert.ToDouble(lineData[2]), 0); points2D.Add(p2D); break; case ("f"): string[] v0 = lineData[1].Split('/'); string[] v1 = lineData[2].Split('/'); string[] v2 = lineData[3].Split('/'); MeshFace mf3D = new MeshFace(Convert.ToInt32(v0[0]) - 1, Convert.ToInt32(v1[0]) - 1, Convert.ToInt32(v2[0]) - 1); MeshFace mf2D = new MeshFace(Convert.ToInt32(v0[1]) - 1, Convert.ToInt32(v1[1]) - 1, Convert.ToInt32(v2[1]) - 1); faces3D.Add(mf3D); faces2D.Add(mf2D); break; } }
public static Mesh ToRhino(DB.Mesh mesh) { if (mesh.NumTriangles < 1) { return(null); } var result = new Mesh(); result.Vertices.AddVertices(mesh.Vertices.Select(x => ToRhino(x))); for (int t = 0; t < mesh.NumTriangles; ++t) { var triangle = mesh.get_Triangle(t); var meshFace = new MeshFace ( (int)triangle.get_Index(0), (int)triangle.get_Index(1), (int)triangle.get_Index(2) ); result.Faces.AddFace(meshFace); } return(result); }
/// <summary> /// /// </summary> /// <param name="triangles"></param> /// <param name="vertices"></param> /// <returns></returns> public static List <MeshFace> GetFaces(int[] triangles, Vector3[] vertices) { List <MeshFace> faces = new List <MeshFace>(); // Find all the connected faces of the mesh for (int i = 0; i < triangles.Length;) { int[] triangleFace = { triangles[i], triangles[i + 1], triangles[i + 2] }; MeshFace face = GetFace(triangleFace, triangles, vertices); if (face.Face.Count > 0) { faces.Add(face); i += 3 * face.Face.Count; } else { i += 3; } } return(faces); }
private Mesh meshFromFace(Mesh M, List <int> fInd) { Mesh mOut = new Mesh(); for (int i = 0; i < fInd.Count; i++) { Mesh M1 = new Mesh(); MeshFace f = M.Faces.GetFace(fInd[i]); if (f.IsValid(M.Vertices.Count)) { M1.Vertices.Add(M.Vertices[f.A]); M1.Vertices.Add(M.Vertices[f.B]); M1.Vertices.Add(M.Vertices[f.C]); if (f.IsTriangle) { M1.Faces.AddFace(0, 1, 2); } else if (f.IsQuad) { M1.Vertices.Add(M.Vertices[f.D]); M1.Faces.AddFace(0, 1, 2, 3); } } mOut.Append(M1); } return(mOut); }
/// <summary> /// Gets the color of a mesh at a specified point /// </summary> /// <param name="sp">The sample point</param> /// <param name="coloredMesh">The colored mesh to sample</param> /// <returns>The color at that location</returns> public static Color GetColor(Point3d sp, Mesh coloredMesh) { coloredMesh.Faces.ConvertQuadsToTriangles(); MeshPoint mp = coloredMesh.ClosestMeshPoint(sp, 0); if (mp != null) { MeshFace face = coloredMesh.Faces[mp.FaceIndex]; Color colorA = coloredMesh.VertexColors[face.A]; Color colorB = coloredMesh.VertexColors[face.B]; Color colorC = coloredMesh.VertexColors[face.C]; double colorSampleA = colorA.A * mp.T[0] + colorB.A * mp.T[1] + colorC.A * mp.T[2]; double colorSampleR = colorA.R * mp.T[0] + colorB.R * mp.T[1] + colorC.R * mp.T[2]; double colorSampleG = colorA.G * mp.T[0] + colorB.G * mp.T[1] + colorC.G * mp.T[2]; double colorSampleB = colorA.B * mp.T[0] + colorB.B * mp.T[1] + colorC.B * mp.T[2]; int Alpha = (int)colorSampleA; int Red = (int)colorSampleR; int Green = (int)colorSampleG; int Blue = (int)colorSampleB; Color colour = Color.FromArgb(Alpha, Red, Green, Blue); return(colour); } else { return(new Color()); } }
public void Export() { for (int i = 0; i < mesh.Length; i++) { if (mesh[i].vertices == null) { mesh[i].vertices = new List <MeshVert>(); } if (mesh[i].faces == null) { mesh[i].faces = new List <MeshFace>(); } mesh[i].vertices.Clear(); mesh[i].faces.Clear(); for (int j = 0; j < mesh[i].Vertices.Length / 15; j++) { MeshVert m = new MeshVert(); m.pos = new Vector3(mesh[i].Vertices[j, 0], mesh[i].Vertices[j, 2], mesh[i].Vertices[j, 1]); m.normal = new Vector3(mesh[i].Vertices[j, 4], mesh[i].Vertices[j, 5], mesh[i].Vertices[j, 6]); m.color = new Color(mesh[i].Vertices[j, 8], mesh[i].Vertices[j, 9], mesh[i].Vertices[j, 10], mesh[i].Vertices[j, 11]); m.uv = new Vector2(mesh[i].Vertices[j, 13], mesh[i].Vertices[j, 14]); mesh[i].vertices.Add(m); } for (int j = 0; j < mesh[i].Faces.Length / 7; j++) { MeshFace mf = new MeshFace(); mf.material = (int)mesh[i].Faces[j, 0]; mf.triangle = new List <int> { (int)mesh[i].Faces[j, 1], (int)mesh[i].Faces[j, 3], (int)mesh[i].Faces[j, 2] }; mesh[i].faces.Add(mf); } } }
MeshFace[] ParseMeshFaces(XmlReader xml) { List <MeshFace> data = new List <MeshFace>(); int depth = xml.Depth; while (xml.Read() && xml.Depth > depth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { string matId = xml.GetAttribute("MaterialID"); string[] val = xml.ReadString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); val[0] = val[0].Substring(1); val[2] = val[2].Substring(0, val[2].Length - 1); MeshFace face = new MeshFace(int.Parse(val[0]), int.Parse(val[1]), int.Parse(val[2])); face.MaterialIndex = int.Parse(matId); data.Add(face); } } } return(data.ToArray()); }
public override bool CastTo <Q>(out Q target) { if (typeof(Q).IsAssignableFrom(typeof(GH_Mesh))) { var mesh = new Mesh(); foreach (var curve in Curves) { if (curve.SegmentCount == 4) { var face = new MeshFace ( mesh.Vertices.Add(curve.SegmentCurve(0).PointAtStart), mesh.Vertices.Add(curve.SegmentCurve(1).PointAtStart), mesh.Vertices.Add(curve.SegmentCurve(2).PointAtStart), mesh.Vertices.Add(curve.SegmentCurve(3).PointAtStart) ); mesh.Faces.AddFace(face); } } target = (Q)(object)new GH_Mesh(mesh); return(true); } target = default; return(false); }
private Mesh BuildMeshFromSlicePoints(Plane currentPlane, List <Point3d> slicePoints) { Mesh m = new Mesh(); int vertexCount = 0; foreach (Point3d pt in slicePoints) { double halfRes = resolution / 2; Point3d v0 = pt + (currentPlane.XAxis * halfRes) + (currentPlane.YAxis * halfRes); Point3d v1 = pt + (currentPlane.XAxis * halfRes) - (currentPlane.YAxis * halfRes); Point3d v2 = pt - (currentPlane.XAxis * halfRes) + (currentPlane.YAxis * halfRes); Point3d v3 = pt - (currentPlane.XAxis * halfRes) - (currentPlane.YAxis * halfRes); m.Vertices.Add(v0); vertexCount++; m.Vertices.Add(v1); vertexCount++; m.Vertices.Add(v3); vertexCount++; m.Vertices.Add(v2); vertexCount++; MeshFace mf = new MeshFace(vertexCount - 4, vertexCount - 3, vertexCount - 2, vertexCount - 1); m.Faces.AddFace(mf); } return(m); }
public BeatInstant(float newTime, MeshFace newMeshes) { time = newTime; meshes = new List<MeshFace?> (); if (newMeshes != null) { meshes.Add (newMeshes); } }
public CopyLayerInstance(MeshVert _parent, MeshFace _element, int copyLayerId = -1) { parent = _parent; element = _element; offsetPosition = Vector3.zero; copyLayer = CopyLayerManager.GetLayer(copyLayerId); copyLayer.Add(this); }
public void LoftGearFromCurve() { bevelMeshes = new List <Mesh>(); for (int i = 0; i < bevelProfiles.Count; i++) { Mesh gear = new Mesh(); for (int j = 0; j < bevelProfiles[i][0].Points.Count; j++) //each mesh vertices { Point3d thisP = bevelProfiles[i][0].Points[j].Location; //S gear.Vertices.Add(thisP); for (int k = 0; k < coneCircles[i].Count; k++) { thisP = coneCircles[i][k].ClosestPoint(thisP); gear.Vertices.Add(thisP); } gear.Vertices.Add(bevelProfiles[i][1].Points[j].Location); //E } //Debug.WriteLine("============================debug============================="); //Debug.WriteLine(bevelProfiles[i][0].Points.Count.ToString()); int V = 4 + 2; List <MeshFace> gearFaces = new List <MeshFace>(); for (int k = 0; k < bevelProfiles[i][0].Points.Count - 1; k++) { if (k < bevelProfiles[i][0].Points.Count - 1) { int _this = -1; for (int j = 0; j < V - 1; j++) { _this = k * V + j; MeshFace thisF = new MeshFace(_this, _this + V, _this + V + 1, _this + 1); gearFaces.Add(thisF); } _this = k * V + 5; gearFaces.Add(new MeshFace(_this, _this + V, _this + 1, _this - V + 1)); } else { //Debug.WriteLine(k.ToString()); for (int n = 0; n < V - 1; n++) { int _this = k * V + n; MeshFace thisF = new MeshFace(_this, n, n + 1, _this + 1); gearFaces.Add(thisF); } gearFaces.Add(new MeshFace(k * V + V - 1, V - 1, 0, k + V)); } } gear.Faces.AddFaces(gearFaces); bevelMeshes.Add(gear); } }
/// <summary> /// Initializes a new instance of the <see cref="MeshPoint" /> class. /// </summary> /// <param name="point">3D Point.</param> /// <param name="face">Mesh face.</param> public MeshPoint(MeshFace face, Point3d point) { var adj = face.AdjacentVertices(); var bary = Convert.Point3dToBarycentric(point, adj[0], adj[1], adj[2]); this.U = bary[0]; this.V = bary[1]; this.W = bary[2]; }
public void GetValleyFacePairBasicInfo(CMesh cm) { Mesh m = cm.mesh; List <Tuple <double, double> > face_heignt_pairs = new List <Tuple <double, double> >(); List <double> edge_length_between_face_pairs = new List <double>(); m.FaceNormals.ComputeFaceNormals(); MeshVertexList vert = m.Vertices; for (int e_ind = 0; e_ind < cm.valley_edges.Count; e_ind++) { // Register indices IndexPair edge_ind = cm.valley_edges[e_ind]; int u = edge_ind.I; int v = edge_ind.J; IndexPair face_ind = cm.valley_face_pairs[e_ind]; int P = face_ind.I; int Q = face_ind.J; MeshFace face_P = m.Faces[P]; MeshFace face_Q = m.Faces[Q]; int p = 0; int q = 0; for (int i = 0; i < 3; i++) { if (!edge_ind.Contains(face_P[i])) { p = face_P[i]; } if (!edge_ind.Contains(face_Q[i])) { q = face_Q[i]; } } /// Compute h_P & cot_Pu Vector3d vec_up = vert[p] - vert[u]; Vector3d vec_uv = vert[v] - vert[u]; double sin_Pu = (Vector3d.CrossProduct(vec_up, vec_uv) / (vec_up.Length * vec_uv.Length)).Length; double len_up = (vec_up - vec_uv).Length; double h_P = len_up * sin_Pu; /// Compute h_Q & cot_Qu Vector3d vec_uq = vert[q] - vert[u]; double sin_Qu = (Vector3d.CrossProduct(vec_uq, vec_uv) / (vec_uq.Length * vec_uv.Length)).Length; double len_uq = (vec_uq - vec_uv).Length; double h_Q = len_uq * sin_Qu; // Compute len_uv double len_uv = vec_uv.Length; // Set Tuple<h_P, h_Q> Tuple <double, double> face_height_pair = new Tuple <double, double>(h_P, h_Q); face_heignt_pairs.Add(face_height_pair); edge_length_between_face_pairs.Add(len_uv); } cm.valley_face_height_pairs = face_heignt_pairs; cm.length_of_valley_diagonal_edges = edge_length_between_face_pairs; }
public static Mesh BitmapFromVertexColors(Mesh mesh, string file) { var path = Path.GetDirectoryName(file); if (!Directory.Exists(path)) { throw new DirectoryNotFoundException($" Directory \"{path}\" not found."); } mesh.Unweld(0, false); mesh.TextureCoordinates.Clear(); for (int i = 0; i < mesh.Vertices.Count; i++) { mesh.TextureCoordinates.Add(0, 0); } int size = (int)Math.Ceiling(Math.Sqrt(mesh.Faces.Count)); float fSize = (float)size * 2; Bitmap bitmap = new Bitmap(size * 2, size * 2); for (int i = 0; i < mesh.Faces.Count; i++) { MeshFace face = mesh.Faces[i]; int x = (i % size) * 2; int y = (i / size) * 2; float fx = (float)x; float fy = (float)y; mesh.TextureCoordinates[face.A] = new Point2f((fx + 0.5) / fSize, (fy + 0.5) / fSize); mesh.TextureCoordinates[face.B] = new Point2f((fx + 1.5) / fSize, (fy + 0.5) / fSize); mesh.TextureCoordinates[face.C] = new Point2f((fx + 1.5) / fSize, (fy + 1.5) / fSize); mesh.TextureCoordinates[face.D] = new Point2f((fx + 0.5) / fSize, (fy + 1.5) / fSize); Color colorA = mesh.VertexColors[face.A]; colorA = Color.FromArgb(255, colorA.R, colorA.G, colorA.B); Color colorB = mesh.VertexColors[face.B]; colorB = Color.FromArgb(255, colorB.R, colorB.G, colorB.B); Color colorC = mesh.VertexColors[face.C]; colorC = Color.FromArgb(255, colorC.R, colorC.G, colorC.B); Color colorD = mesh.VertexColors[face.D]; colorD = Color.FromArgb(255, colorD.R, colorD.G, colorD.B); y = size * 2 - 2 - y; bitmap.SetPixel(x + 0, y + 1, colorA); bitmap.SetPixel(x + 1, y + 1, colorB); bitmap.SetPixel(x + 1, y + 0, colorC); bitmap.SetPixel(x + 0, y + 0, colorD); } bitmap.Save(file, System.Drawing.Imaging.ImageFormat.Png); mesh.VertexColors.Clear(); return(mesh); }
/// <summary> /// Get the local coordinate system of a point on the mesh /// </summary> /// <param name="i">The face index</param> /// <param name="u"></param> /// <param name="v"></param> /// <param name="orientation"></param> /// <param name="xLimit"></param> /// <returns></returns> public override CartesianCoordinateSystem LocalCoordinateSystem(int i, double u, double v, Angle orientation, Angle xLimit) { if (Faces != null && Faces.Count > i) { MeshFace face = Faces[i]; return(face.GetPlane()); } return(null); }
private int FaceSortComparer(MeshFace x, MeshFace y) { int rc = x.FromNode.Index.CompareTo(y.FromNode.Index); if (rc == 0) { rc = x.ToNode.Index.CompareTo(y.ToNode.Index); } return(rc); }
private static List <Point3d> getFacePoints(MeshFace f, Mesh m) { List <Point3d> pts = new List <Point3d>(); pts.Add(m.Vertices[f.A]); pts.Add(m.Vertices[f.B]); pts.Add(m.Vertices[f.C]); pts.Add(m.Vertices[f.D]); return(pts); }
public void CreateIteration(MeshFace startingFace) { Iterations.Clear(); CopyLayerManager.GotoNextLayer(); var iterator = new MeshIteration(); iterator.Create(startingFace); Iterations.Add(iterator); AddIteration(iterator); }
public FaceIterationElement(MeshFace _element, FaceIterationElement _parentIterationElement, int iteratorIndex) { element = _element; element.IteratorIndex = iteratorIndex; parentIterationElement = _parentIterationElement; element.ChangeVertCopyLayer(); anchorVerts = new List <MeshVert>(); floatingVerts = new List <FloatingMeshVert>(); }
/** * Change the color of a given face * @param face - the face in which we would like to change the color * @param newColor - the new color in which we want to paint the face * @param mesh - the mesh in which the face belongs to, so we can update the verticie colors * @side-effect: mesh - mesh.colors is updated as per the color & face **/ public static void ColorFace(MeshFace? face, Color newColor, Mesh mesh) { if (face == null) { return; } Color[] newColors = mesh.colors; foreach (int y in face.Value.verticies) { newColors [y] = newColor; } mesh.colors = newColors; }
public MeshFace? FaceClickTest(Vector3 mouseClickPos) { RaycastHit hit; Ray rc = Camera.main.ScreenPointToRay (Input.mousePosition); try{ if (Physics.Raycast (rc, out hit)) { Vector3 V1 = tmesh.vertices [tmesh.triangles [hit.triangleIndex * 3]]; Vector3 V2 = tmesh.vertices [tmesh.triangles [hit.triangleIndex * 3 + 1]]; Vector3 V3 = tmesh.vertices [tmesh.triangles [hit.triangleIndex * 3 + 2]]; Vector3 normal = GeometryUtility.GetNormal (V1, V2, V3); MeshFace? newFace = new MeshFace (meshFacesMap [normal], normal); return newFace.Value; } }catch(Exception e) { } return null; }
public MeshSimplify_Face(MeshFace f) { this.face = f; }
internal static extern bool ON_Mesh_GetFace(IntPtr pConstMesh, int face_index, ref MeshFace face);
/** * Used in tracking the currently visible faces **/ private List<MeshFace?> CalculateFaces(Vector3 originPoint, Vector3 originDirection) { float leftXBound = -1 * (width / 4); float rightXBound = (width / 4); float topYBound = height / 2; float bottomYBound = -1 * (height / 2); List<Vector3> intersectedNormals = new List<Vector3> (); List<MeshFace?> returnVerticies = new List<MeshFace?> (); for (float i = leftXBound; i <= rightXBound; i+=RAYCAST_INTERVAL) { for (float y= topYBound; y>= bottomYBound; y-=RAYCAST_INTERVAL) { Vector3 ray = Vector3.forward; Vector3 ray2 = new Vector3 (i, y, 100); Quaternion q = Quaternion.FromToRotation(ray, ray2); Vector3 newVector = q * originDirection ; RaycastHit hit; Debug.DrawRay (originPoint, newVector * 100, Color.red, 1.0f); int bitmast = 1 << 8; // @TODO Really need to understand why raycast is using the length of the direction :/ if (Physics.Raycast (originPoint, newVector, out hit, 100000, bitmast)) { Vector3 V1 = tmesh.vertices [tmesh.triangles [hit.triangleIndex * 3]]; Vector3 V2 = tmesh.vertices [tmesh.triangles [hit.triangleIndex * 3 + 1]]; Vector3 V3 = tmesh.vertices [tmesh.triangles [hit.triangleIndex * 3 + 2]]; Vector3 normal = GeometryUtility.GetNormal (V1, V2, V3); if (!intersectedNormals.Contains (normal)) { intersectedNormals.Add (normal); MeshFace newFace = new MeshFace (meshFacesMap [normal], normal); //We should probably stash these in some custom struct so we don't have to keep calculating information again returnVerticies.Add (newFace); } } } } return returnVerticies; }
public void MeshClean(ref Mesh mesh, double tolerance) { try { List<int> mapping = new List<int>(); List<Point3d> ps = new List<Point3d>(); List<double> MapCount = new List<double>(); ps.Add(mesh.Vertices[0]); mapping.Add(0); MapCount.Add(1); for (int j = 1; j < mesh.Vertices.Count; j++) { double min = double.MaxValue; int sign = 0; for (int i = 0; i < ps.Count; i++) { double tempt = ps[i].DistanceTo((Point3d)mesh.Vertices[j]); if (tempt < min) { min = tempt; sign = i; } } if (min < tolerance) { mapping.Add(sign); MapCount[sign]++; ps[sign] *= (MapCount[sign] - 1) / MapCount[sign]; Point3d tempp = (Point3d)(mesh.Vertices[j]); tempp *= (1 / MapCount[sign]); ps[sign] += tempp; } else { mapping.Add(ps.Count); ps.Add(mesh.Vertices[j]); MapCount.Add(1); } } Mesh mesh2 = new Mesh(); for (int i = 0; i < ps.Count; i++) { mesh2.Vertices.Add(ps[i]); } for (int i = 0; i < mesh.Faces.Count; i++) { MeshFace f_temp = MeshFace.Unset; bool FaceSign = false; if (mesh.Faces[i].IsQuad) { int p1 = mapping[mesh.Faces[i].A]; int p2 = mapping[mesh.Faces[i].B]; int p3 = mapping[mesh.Faces[i].C]; int p4 = mapping[mesh.Faces[i].D]; if (noRepeat(p1, p2, p3, p4)) { f_temp = new MeshFace(p1, p2, p3, p4); FaceSign = true; } } else if (mesh.Faces[i].IsTriangle) { int p1 = mapping[mesh.Faces[i].A]; int p2 = mapping[mesh.Faces[i].B]; int p3 = mapping[mesh.Faces[i].C]; if (noRepeat(p1, p2, p3)) { f_temp = new MeshFace(p1, p2, p3); FaceSign = true; } } if (mesh2.Faces.Count > 0) { for (int j = 0; j < mesh2.Faces.Count; j++) { if (!noRepeat(mesh2.Faces[j], f_temp)) FaceSign = false; } } if (FaceSign) mesh2.Faces.AddFace(f_temp); } mesh2.Compact(); mesh2.UnifyNormals(); mesh = mesh2; } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString()); } }
//Is this the most effecient way? private IEnumerator MarkFace(MeshFace? mf) { int delay = Mathf.FloorToInt(GameplayConfiguration.QUARTER_BAR / GameplayConfiguration.ITERATIONS); float saturationAmount = (1f-((HSLColor)initColor).s) / GameplayConfiguration.ITERATIONS ; float lightnessAmount = (1f-((HSLColor)initColor).l) / GameplayConfiguration.ITERATIONS ; Color incrementalColor = initColor; for (int i=0; i< GameplayConfiguration.ITERATIONS; i++) { incrementalColor = ColorUtilities.IncreaseBrightness (incrementalColor, lightnessAmount, saturationAmount); GeometryUtility.ColorFace(mf, incrementalColor , tmesh); yield return new WaitForSeconds (delay*2); } GeometryUtility.ColorFace (mf, Color.white, tmesh); yield return new WaitForSeconds (GameplayConfiguration.BEAT_WINDOW_TOLERANCE); GeometryUtility.ColorFace(mf, initColor , tmesh); yield return null; }
byte[] BuildVertexPBNT1Data(Vector3[] positions, Vector3[] normals, Vector2[] texVtx, Index3i[] texIdx, VertexWeight[] vtxWeights, MeshFace[] faces) { Dictionary<string, int> table = new Dictionary<string, int>(faces.Length * 3); FastList<VertexPBNT1> vertices = new FastList<VertexPBNT1>(faces.Length * 3); float[] blendBuf = new float[4]; byte[] blendBuf2 = new byte[4]; for (int i = 0; i < faces.Length; i++) { int index; VertexPBNT1 vtx; int vtxIdx = faces[i].IndexA; vtx.pos = positions[vtxIdx]; vtx.n = normals[vtxIdx]; vtx.u = texVtx[texIdx[i].A].X; vtx.v = texVtx[texIdx[i].A].Y; for (int j = 0; j < 4; j++) { if (j < vtxWeights[vtxIdx].BoneID.Length) { blendBuf[j] = vtxWeights[vtxIdx].Weight[j]; blendBuf2[j] = (byte)vtxWeights[vtxIdx].BoneID[j]; } else { blendBuf2[j] = 0; blendBuf[j] = 0; } } vtx.blend = new Vector4(blendBuf[0], blendBuf[1], blendBuf[2], blendBuf[3]); vtx.boneId1 = blendBuf2[0]; vtx.boneId2 = blendBuf2[1]; vtx.boneId3 = blendBuf2[2]; vtx.boneId4 = blendBuf2[3]; string desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexA = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexA = index; } // ========================================= vtxIdx = faces[i].IndexB; vtx.pos = positions[vtxIdx]; vtx.n = normals[vtxIdx]; vtx.u = texVtx[texIdx[i].B].X; vtx.v = texVtx[texIdx[i].B].Y; for (int j = 0; j < 4; j++) { if (j < vtxWeights[vtxIdx].BoneID.Length) { blendBuf[j] = vtxWeights[vtxIdx].Weight[j]; blendBuf2[j] = (byte)vtxWeights[vtxIdx].BoneID[j]; } else { blendBuf2[j] = 0; blendBuf[j] = 0; } } vtx.blend = new Vector4(blendBuf[0], blendBuf[1], blendBuf[2], blendBuf[3]); vtx.boneId1 = blendBuf2[0]; vtx.boneId2 = blendBuf2[1]; vtx.boneId3 = blendBuf2[2]; vtx.boneId4 = blendBuf2[3]; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexB = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexB = index; } // ========================================= vtx.pos = positions[faces[i].IndexC]; vtx.n = normals[faces[i].IndexC]; vtx.u = texVtx[texIdx[i].C].X; vtx.v = texVtx[texIdx[i].C].Y; for (int j = 0; j < 4; j++) { if (j < vtxWeights[vtxIdx].BoneID.Length) { blendBuf[j] = vtxWeights[vtxIdx].Weight[j]; blendBuf2[j] = (byte)vtxWeights[vtxIdx].BoneID[j]; } else { blendBuf2[j] = 0; blendBuf[j] = 0; } } vtx.blend = new Vector4(blendBuf[0], blendBuf[1], blendBuf[2], blendBuf[3]); vtx.boneId1 = blendBuf2[0]; vtx.boneId2 = blendBuf2[1]; vtx.boneId3 = blendBuf2[2]; vtx.boneId4 = blendBuf2[3]; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexC = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexC = index; } } byte[] buffer = new byte[vertices.Count * sizeof(VertexPBNT1)]; fixed (byte* dst = &buffer[0]) { fixed (VertexPBNT1* src = &vertices.Elements[0]) { Memory.Copy(src, dst, buffer.Length); } } return buffer; }
/// <summary> /// Attempts to create a list of triangles which represent a /// triangulation of a closed polyline /// </summary> /// <returns></returns> public MeshFace[] TriangulateClosedPolyline() { if (!IsClosed) return null; int triangle_count = (Count - 3) * 3; if (triangle_count < 1) return null; int[] triangles = new int[triangle_count]; if (!UnsafeNativeMethods.TLC_Triangulate3dPolygon(Count, ToArray(), triangle_count, triangles)) return null; int face_count = triangle_count / 3; MeshFace[] rc = new MeshFace[face_count]; for (int i = 0; i < face_count; i++) { rc[i] = new MeshFace(triangles[i * 3], triangles[i * 3 + 1], triangles[i * 3 + 2]); } return rc; }
public bool ProcessInput(MeshFace playerInput) { BeatInstant inputInstant = new BeatInstant (audioSource.time, playerInput); BeatInstant comparisonInstant = bTracker.Peek (); bool hit = CheckMatch (comparisonInstant, inputInstant); if (hit) { bTracker.Dequeue (); ComboCount++; } else { ComboCount = 0; } return hit; }
private bool noRepeat(MeshFace f1, MeshFace f2) { List<int> p1 = new List<int>(); List<int> p2 = new List<int>(); if (f1.IsQuad && f2.IsTriangle) { return false; } else if (f1.IsTriangle && f2.IsQuad) { return false; } else if (f1.IsTriangle && f2.IsTriangle) { p1.Add(f1.A); p1.Add(f1.B); p1.Add(f1.C); p2.Add(f2.A); p2.Add(f2.B); p2.Add(f2.C); p1.Sort(); p2.Sort(); if (p1[0] == p2[0] && p1[1] == p2[1] && p1[2] == p2[2]) { return false; } } else if (f1.IsQuad && f2.IsQuad) { p1.Add(f1.A); p1.Add(f1.B); p1.Add(f1.C); p1.Add(f1.D); p2.Add(f2.A); p2.Add(f2.B); p2.Add(f2.C); p2.Add(f2.D); p1.Sort(); p2.Sort(); if (p1[0] == p2[0] && p1[1] == p2[1] && p1[2] == p2[2] && p1[3] == p2[3]) { return false; } } return true; }
MeshFace[] ParseMeshFaces(XmlReader xml) { List<MeshFace> data = new List<MeshFace>(); int depth = xml.Depth; while (xml.Read() && xml.Depth > depth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { Index3i idx = ParseIndex3(xml.ReadString()); MeshFace face = new MeshFace(idx.A, idx.B, idx.C); face.MaterialIndex = int.Parse(xml.GetAttribute("MaterialID")); data.Add(face); } } } return data.ToArray(); }
MeshFace[] ParseMeshFaces(XmlReader xml) { List<MeshFace> data = new List<MeshFace>(); int depth = xml.Depth; while (xml.Read() && xml.Depth > depth) { if (xml.IsStartElement() && !xml.IsEmptyElement) { if (xml.Name == "Value") { string matId = xml.GetAttribute("MaterialID"); string[] val = xml.ReadString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); val[0] = val[0].Substring(1); val[2] = val[2].Substring(0, val[2].Length - 1); MeshFace face = new MeshFace(int.Parse(val[0]), int.Parse(val[1]), int.Parse(val[2])); face.MaterialIndex = int.Parse(matId); data.Add(face); } } } return data.ToArray(); }
byte[] BuildVertexPNData(Vector3[] positions, Vector3[] normals, MeshFace[] faces) { Dictionary<string, int> table = new Dictionary<string, int>(faces.Length * 3); FastList<VertexPN> vertices = new FastList<VertexPN>(faces.Length * 3); for (int i = 0; i < faces.Length; i++) { int index; VertexPN vtx; int vtxIdx = faces[i].IndexA; vtx.pos = positions[vtxIdx]; vtx.n = normals[vtxIdx]; string desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexA = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexA = index; } // ========================================= vtxIdx = faces[i].IndexB; vtx.pos = positions[vtxIdx]; vtx.n = normals[vtxIdx]; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexB = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexB = index; } // ========================================= vtx.pos = positions[faces[i].IndexC]; vtx.n = normals[faces[i].IndexC]; desc = vtx.ToString(); if (!table.TryGetValue(desc, out index)) { table.Add(desc, vertices.Count); faces[i].IndexC = vertices.Count; vertices.Add(ref vtx); } else { faces[i].IndexC = index; } } byte[] buffer = new byte[vertices.Count * sizeof(VertexPN)]; fixed (byte* dst = &buffer[0]) { fixed (VertexPN* src = &vertices.Elements[0]) { Memory.Copy(src, dst, buffer.Length); } } return buffer; }
private IEnumerator MarkFace1(MeshFace? mf) { yield return new WaitForSeconds (GameplayConfiguration.QUARTER_BAR/4); GeometryUtility.ColorFace (mf, initColor , tmesh); }