public static VertexColorTexture Convert( MeshVertex meshVertex ) { VertexColorTexture v; v.Position = meshVertex.Position; v.Color = meshVertex.Color0; v.TexCoord = meshVertex.TexCoord0; return v; }
void AddSpan(MeshConstructor mc, MeshVertex[,] mx, int latitudeSpanIndex, int longitudeSpanIndex, int submesh = 0) { MeshVertex topLeft = mx[latitudeSpanIndex + 1, longitudeSpanIndex]; MeshVertex topRight = mx[latitudeSpanIndex + 1, longitudeSpanIndex + 1]; MeshVertex bottomLeft = mx[latitudeSpanIndex, longitudeSpanIndex]; MeshVertex bottomRight = mx[latitudeSpanIndex, longitudeSpanIndex + 1]; mc.AddTriangle(topLeft, topRight, bottomLeft, submesh); mc.AddTriangle(bottomRight, bottomLeft, topRight, submesh); }
public void AddTriangle(MeshVertex a, MeshVertex b, MeshVertex c, int submeshIndex = 0) { AddVertex(a); AddVertex(b); AddVertex(c); triangles[submeshIndex].Add(a.index); triangles[submeshIndex].Add(b.index); triangles[submeshIndex].Add(c.index); }
public void AddVertex(MeshVertex v) { if (v.index != -1) { return; } vertices.Add(v.position); uv.Add(v.uv); v.index = vertices.Count - 1; normals.Add(v.normal); }
public static VertexColorTextureTBN Convert( MeshVertex meshVertex ) { VertexColorTextureTBN v; v.Position = meshVertex.Position; v.Tangent = MathUtil.ToHalf4( meshVertex.Tangent, 0 ); v.Binormal = MathUtil.ToHalf4( meshVertex.Binormal, 0 ); v.Normal = MathUtil.ToHalf4( meshVertex.Normal, 0 ); v.Color = meshVertex.Color0; v.TexCoord = meshVertex.TexCoord0; return v; }
public static VertexColorTextureTBNSkinned Convert ( MeshVertex meshVertex ) { VertexColorTextureTBNSkinned v; v.Position = meshVertex.Position; v.Tangent = MathUtil.ToHalf4( meshVertex.Tangent, 0 ); v.Binormal = MathUtil.ToHalf4( meshVertex.Binormal, 0 ); v.Normal = MathUtil.ToHalf4( meshVertex.Normal, 0 ); v.Color = meshVertex.Color0; v.TexCoord = meshVertex.TexCoord0; v.SkinIndices = meshVertex.SkinIndices; v.SkinWeights = meshVertex.SkinWeights; return v; }
private void Init(IEnumerable <MeshVertex> vertices) { IEnumerator <MeshVertex> enumerator = vertices.GetEnumerator(); enumerator.Reset(); if (enumerator.MoveNext()) { this.First = enumerator.Current; } if (enumerator.MoveNext()) { this.Second = enumerator.Current; } if (!enumerator.MoveNext()) { throw new ArgumentException("Not enough vertices to define a triangle."); } this.Third = enumerator.Current; }
public void AddVertex(Vector3 pos, Vector3 normal, Vector2 textureUv, Vector2 lightmapUv) { pos += Offset; var meshVert = new MeshVertex(pos, normal, textureUv, lightmapUv); int vertIndex; if (!_vertDict.TryGetValue(meshVert, out vertIndex)) { vertIndex = _verts.Count; _verts.Add(pos); _normals.Add(normal); _textureUvs.Add(textureUv); _lightmapUvs.Add(lightmapUv); _vertDict.Add(meshVert, vertIndex); } _faceIndices.Add(vertIndex); }
private static MeshVertex[] CreateVertices( W3dMesh w3dMesh, bool isSkinned) { var numVertices = (uint)w3dMesh.Vertices.Length; var vertices = new MeshVertex[numVertices]; for (var i = 0; i < numVertices; i++) { vertices[i] = new MeshVertex { Position = w3dMesh.Vertices[i], Normal = w3dMesh.Normals[i], BoneIndex = isSkinned ? w3dMesh.Influences[i].BoneIndex : 0u }; } return(vertices); }
public void UpdatePosition(MeshVertex instigatingVertex) { //get nearby vert for (int i = 0; i < Mainmesh.MeshVerts.Length; i++) { float dist = Vector3.Distance(transform.position, Mainmesh.MeshVerts[i].transform.position); //if a distance from this vertex is closer than the minimum distance if (dist < Mainmesh.MinimumDistance) { if (instigatingVertex == Mainmesh.MeshVerts[i]) { continue; } if (instigatingVertex == this) { continue; } //push the vertex away from this vertex Vector3 newPos = Mainmesh.MeshVerts[i].transform.position - transform.position; newPos *= 0.2f; Mainmesh.MeshVerts[i].transform.position += newPos; } } }
public Meshold() { Edges = new List<HalfEdge>(); Faces = new List<HalfEdgeFace>(); Vertices = new List<MeshVertex>(); double size = 1.0; var v0 = new MeshVertex(new Vect3(size / 2.0, -size / 2.0, size / 2.0)); var v1 = new MeshVertex(new Vect3(-size / 2.0, -size / 2.0, size / 2.0)); var v2 = new MeshVertex(new Vect3(-size / 2.0, -size / 2.0, -size / 2.0)); var v3 = new MeshVertex(new Vect3(size / 2.0, -size / 2.0, -size / 2.0)); var v4 = new MeshVertex(new Vect3(size / 2.0, size / 2.0, size / 2.0)); var v5 = new MeshVertex(new Vect3(-size / 2.0, size / 2.0, size / 2.0)); var v6 = new MeshVertex(new Vect3(-size / 2.0, size / 2.0, -size / 2.0)); var v7 = new MeshVertex( new Vect3(0, 0, 0)); var face1 = new HalfEdgeFace(); var face2 = new HalfEdgeFace(); var face3 = new HalfEdgeFace(); var face4 = new HalfEdgeFace(); var face5 = new HalfEdgeFace(); var face6 = new HalfEdgeFace(); Faces.AddRange(new[]{face1,face2,face3,face4,face5,face6}); var he1 = new HalfEdge { ToVertex = v1, HalfEdgeFace = face1 }; var he2 = new HalfEdge { ToVertex = v2, HalfEdgeFace = face1 }; var he3 = new HalfEdge { ToVertex = v3, HalfEdgeFace = face1 }; var he4 = new HalfEdge { ToVertex = v0, HalfEdgeFace = face1 }; he1.Next = he2; he2.Next = he3; he3.Next = he4; he4.Next = he1; face1.HalfEdge = v0.Edge = he1; var he5 = new HalfEdge { ToVertex = v1, HalfEdgeFace = face2 }; var he6 = new HalfEdge { ToVertex = v5, HalfEdgeFace = face2 }; var he7 = new HalfEdge { ToVertex = v6, HalfEdgeFace = face2 }; var he8 = new HalfEdge { ToVertex = v2, HalfEdgeFace = face2 }; he5.Next = he6; he6.Next = he7; he7.Next = he8; he8.Next = he5; face2.HalfEdge = v2.Edge = he5; var he9 = new HalfEdge { ToVertex = v5, HalfEdgeFace = face3 }; var he10 = new HalfEdge { ToVertex = v4, HalfEdgeFace = face3 }; var he11 = new HalfEdge { ToVertex = v7, HalfEdgeFace = face3 }; var he12 = new HalfEdge { ToVertex = v6, HalfEdgeFace = face3 }; he9.Next = he10; he10.Next = he11; he11.Next = he12; he12.Next = he9; face3.HalfEdge = v6.Edge = he9; var he13 = new HalfEdge { ToVertex = v4, HalfEdgeFace = face4 }; var he14 = new HalfEdge { ToVertex = v0, HalfEdgeFace = face4 }; var he15 = new HalfEdge { ToVertex = v3, HalfEdgeFace = face4 }; var he16 = new HalfEdge { ToVertex = v7, HalfEdgeFace = face4 }; he13.Next = he14; he14.Next = he15; he15.Next = he16; he16.Next = he13; face4.HalfEdge = v7.Edge = he13; var he17 = new HalfEdge { ToVertex = v2, HalfEdgeFace = face5 }; var he18 = new HalfEdge { ToVertex = v6, HalfEdgeFace = face5 }; var he19 = new HalfEdge { ToVertex = v7, HalfEdgeFace = face5 }; var he20 = new HalfEdge { ToVertex = v3, HalfEdgeFace = face5 }; he17.Next = he18; he18.Next = he19; he19.Next = he20; he20.Next = he17; face5.HalfEdge = v3.Edge = he17; var he21 = new HalfEdge { ToVertex = v1, HalfEdgeFace = face6 }; var he22 = new HalfEdge { ToVertex = v5, HalfEdgeFace = face6 }; var he23 = new HalfEdge { ToVertex = v4, HalfEdgeFace = face6 }; var he24 = new HalfEdge { ToVertex = v0, HalfEdgeFace = face6 }; he21.Next = he22; he22.Next = he23; he23.Next = he24; he24.Next = he21; face6.HalfEdge = v0.Edge = he21; MapPair(he1, he21); MapPair(he2, he5); MapPair(he3, he17); MapPair(he4, he15); //MapPair(he5, he2); MapPair(he6, he24); MapPair(he7, he9); MapPair(he8, he18); //MapPair(he9, he7); MapPair(he10, he23); MapPair(he11, he13); MapPair(he12, he19); //MapPair(he13, he11); MapPair(he14, he22); //MapPair(he15, he4); MapPair(he16, he20); //MapPair(he17, he3); //MapPair(he18, he8); //MapPair(he19, he12); //MapPair(he20, he16); //MapPair(he21, he1); //MapPair(he22, he14); //MapPair(he23, he10); //MapPair(he24, he6); }
Mesh GenerateMesh() { var mc = new MeshConstructor(); mc.mesh.name = name; mc.SetSubmeshCount(2); latitudes = latitudeSpans + 1; longitudes = longitudeSpans + 1; pictureLatitudeSpans = 2 * halfLatitudePictureSpans; pictureLongitudeSpans = 2 * halfLongitudePictureSpans; pictureLatitudes = pictureLatitudeSpans + 1; pictureLongitudes = pictureLongitudeSpans + 1; pictureSpans = new bool[latitudeSpans, longitudeSpans]; baseMatrix = new MeshVertex[latitudes, longitudes]; pictureMatrix = new MeshVertex[latitudes, longitudes]; for (int latitudePictureSpanIndex = 0; latitudePictureSpanIndex < pictureLatitudeSpans; latitudePictureSpanIndex++) { for (int longitudePictureSpanIndex = 0; longitudePictureSpanIndex < pictureLongitudeSpans; longitudePictureSpanIndex++) { int latitudeSpanIndex = latitudes/2-halfLatitudePictureSpans+latitudePictureSpanIndex; int longitudeSpanIndex = Extensions.Modulo(-halfLongitudePictureSpans+longitudePictureSpanIndex, longitudeSpans); pictureSpans[latitudeSpanIndex, longitudeSpanIndex] = true; } } for (int latitudeIndex = 0; latitudeIndex <= latitudeSpans; latitudeIndex++) { for (int longitudeIndex = 0; longitudeIndex <= longitudeSpans; longitudeIndex++) { baseMatrix[latitudeIndex, longitudeIndex] = new MeshVertex( position: getSpherePoint(latitudeIndex, longitudeIndex), uv: new Vector2(1f * longitudeIndex / longitudeSpans, 1f * latitudeIndex / latitudeSpans), normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized ); } } for (int pictureLatitudeIndex = 0; pictureLatitudeIndex <= 2 * halfLatitudePictureSpans; pictureLatitudeIndex++) { for (int pictureLongitudeIndex = 0; pictureLongitudeIndex <= 2 * halfLongitudePictureSpans; pictureLongitudeIndex++) { int latitudeIndex = latitudes/2 -halfLatitudePictureSpans + pictureLatitudeIndex; int longitudeIndex = Extensions.Modulo(-halfLongitudePictureSpans + pictureLongitudeIndex, longitudeSpans); //Debug.LogFormat("Adding picture vertex {0}, {1}", latitudeIndex, longitudeIndex); pictureMatrix[latitudeIndex, longitudeIndex] = new MeshVertex( position: getSpherePoint(latitudeIndex, longitudeIndex), uv: new Vector2(1f * pictureLongitudeIndex / pictureLongitudeSpans, 1f * pictureLatitudeIndex / pictureLatitudeSpans), normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized ); if (longitudeIndex == 0) { longitudeIndex = longitudes - 1; //Debug.LogFormat("Adding picture vertex {0}, {1}", latitudeIndex, longitudeIndex); pictureMatrix[latitudeIndex, longitudeIndex] = new MeshVertex( position: getSpherePoint(latitudeIndex, longitudeIndex), uv: new Vector2(1f * pictureLongitudeIndex / pictureLongitudeSpans, 1f * pictureLatitudeIndex / pictureLatitudeSpans), normal: getSpherePoint(latitudeIndex, longitudeIndex).normalized ); } } } for (int latitudeSpanIndex = 0; latitudeSpanIndex < latitudeSpans; latitudeSpanIndex++) { for (int longitudeSpanIndex = 0; longitudeSpanIndex < longitudeSpans; longitudeSpanIndex++) { if (isPictureSpan(latitudeSpanIndex, longitudeSpanIndex)) { AddSpan(mc, pictureMatrix, latitudeSpanIndex, longitudeSpanIndex, 1); } AddSpan(mc, baseMatrix, latitudeSpanIndex, longitudeSpanIndex); } } return mc.Done(); }
/// <summary> /// Splits this triangle with given plane. /// </summary> /// <param name="splitter"> <see cref="Plane"/> that is used for splitting.</param> /// <param name="frontCoplanarElements"> /// An optional collection for this triangle if it's located on this plane and faces the /// same way. /// </param> /// <param name="backCoplanarElements"> /// An optional collection for this triangle if it's located on this plane and faces the /// opposite way. /// </param> /// <param name="frontElements"> /// An optional collection for parts of this triangle that are located in front of this plane. /// </param> /// <param name="backElements"> /// An optional collection for parts of this triangle that are located behind this plane. /// </param> /// <param name="customData"> Not used.</param> public void Split(Plane splitter, ICollection <SplittableTriangle> frontCoplanarElements, ICollection <SplittableTriangle> backCoplanarElements, ICollection <SplittableTriangle> frontElements, ICollection <SplittableTriangle> backElements, object customData = null) { PlanePosition triangleType = 0; PlanePosition[] positions = new PlanePosition[3]; // Determine position of the triangle relative to the plane. splitter.PointPosition(this.First.Position, out positions[0], ref triangleType); splitter.PointPosition(this.Second.Position, out positions[1], ref triangleType); splitter.PointPosition(this.Third.Position, out positions[2], ref triangleType); // Process this triangle's data based on its position. switch (triangleType) { case PlanePosition.Coplanar: // See where this triangle is looking and it to corresponding list. if (this.Normal * splitter.Normal > 0) { if (frontCoplanarElements != null) { frontCoplanarElements.Add(this); } } else { if (backCoplanarElements != null) { backCoplanarElements.Add(this); } } break; case PlanePosition.Front: if (frontElements != null) { frontElements.Add(this); } break; case PlanePosition.Back: if (backElements != null) { backElements.Add(this); } break; case PlanePosition.Spanning: if (frontElements == null && backElements == null) { return; // Any calculations won't be saved anywhere. } // // Prepare to create a split of this triangle. // // Cash vertices into an array, so we can loop through it. MeshVertex[] vertices = this.Vertices; // Create lists for vertices on the front and back. List <MeshVertex> fvs = new List <MeshVertex>(4); List <MeshVertex> bvs = new List <MeshVertex>(4); // // Process edges. // // We go through the polygon edge by edge with i being index of the start of the // edge, and j - end. for (int i = 0, j = 1; i < 3; i++, j = (j + 1) % 3) { // If edge doesn't begin behind the plane, add starting vertex to front vertices. if (positions[i] != PlanePosition.Back) { fvs.Add(vertices[i]); } // Else put the starting vertex to the back vertices. else { bvs.Add(vertices[i]); } // If this edge intersects the plane, split it. if ((positions[i] | positions[j]) == PlanePosition.Spanning) { // Calculate fraction that describes position of splitting vertex along // the line between start and end of the edge. float positionParameter = (splitter.D - splitter.Normal * vertices[i].Position) / (splitter.Normal * (vertices[j].Position - vertices[i].Position)); // Linearly interpolate the vertex that splits the edge. MeshVertex splittingVertex = (MeshVertex)vertices[i].CreateLinearInterpolation(vertices[j], positionParameter); // Add splitting vertex to both lists. fvs.Add(splittingVertex); bvs.Add(splittingVertex); } // Create front and back triangle(s) from vertices from corresponding lists. if (frontElements != null) { SplittableTriangle.TriangulateLinearly(fvs, false, frontElements); } if (backElements != null) { SplittableTriangle.TriangulateLinearly(bvs, false, backElements); } } break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// This is the method that actually does the work. /// </summary> /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param> protected override void SolveInstance(IGH_DataAccess DA) { MeshGhData hE_MeshData = new MeshGhData(); if (!DA.GetData(0, ref hE_MeshData)) { return; } Paramdigma.Core.HalfEdgeMesh.Mesh hE_Mesh = hE_MeshData.Value; List <Point3d> vertices = new List <Point3d>(); List <Line> edges = new List <Line>(); List <Rhino.Geometry.Mesh> faces = new List <Rhino.Geometry.Mesh>(); foreach (MeshVertex v in hE_Mesh.Vertices) { vertices.Add(new Point3d(v.X, v.Y, v.Z)); } foreach (MeshEdge e in hE_Mesh.Edges) { MeshVertex v1 = e.HalfEdge.Vertex; MeshVertex v2 = e.HalfEdge.Twin.Vertex; edges.Add(new Line(new Point3d(v1.X, v1.Y, v1.Z), new Point3d(v2.X, v2.Y, v2.Z))); } foreach (Paramdigma.Core.HalfEdgeMesh.MeshFace f in hE_Mesh.Faces) { List <MeshVertex> vs = f.AdjacentVertices(); List <int> faceVs = new List <int>(); List <Point3d> facePoints = new List <Point3d>(); int vi = 0; foreach (MeshVertex v in vs) { facePoints.Add(new Point3d(v.X, v.Y, v.Z)); faceVs.Add(vi); vi++; } Rhino.Geometry.Mesh m = new Rhino.Geometry.Mesh(); m.Vertices.AddVertices(facePoints); if (vs.Count == 3) { m.Faces.AddFace(0, 1, 2); } else if (vs.Count == 4) { m.Faces.AddFace(0, 1, 2, 3); } faces.Add(m); } DA.SetDataList(0, vertices); DA.SetDataList(1, edges); DA.SetDataList(2, faces); }
/// <summary> /// Convenience method to update all three vertices at once. /// </summary> /// <param name="a">The first vertex in clockwise order.</param> /// <param name="b">The second vertex in clockwise order.</param> /// <param name="c">The third vertex in clockwise order.</param> public void Update(MeshVertex a, MeshVertex b, MeshVertex c) { MeshVertices[0] = a; MeshVertices[1] = b; MeshVertices[2] = c; }
/// <summary> /// Creates a new triangle. /// </summary> /// <param name="a">The first vertex in clockwise order.</param> /// <param name="b">The second vertex in clockwise order.</param> /// <param name="c">The third vertex in clockwise order.</param> public MeshTriangle(MeshVertex a, MeshVertex b, MeshVertex c) { MeshVertices = new[] { a, b, c }; }
private void ParserASCII(PlyObjectHeader header, Stream sr, ref MeshModel mesh) { MeshVertex[] vertices = new MeshVertex[header.Elements["vertex"].Count]; int[] indices = new int[header.Elements["face"].Count * 3]; NumberFormatInfo nfi = new NumberFormatInfo(); nfi.NumberDecimalSeparator = "."; nfi.NumberGroupSeparator = ","; //Initialize bound box calculation String[] str; //for number of vertices readed in header do... BoundBox boundBox = new BoundBox(); for (int i = 0; i < vertices.Length; i++) { str = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); vertices[i].Position = new Point3D(float.Parse(str[0], nfi), float.Parse(str[1], nfi), float.Parse(str[2], nfi)); //Adjusting BoundBox... boundBox.Include(vertices[i].Position); //Reporting progress int percent = (int)(((float)i / vertices.Length) * 100.0f); if ((percent % 20) == 0) { this.OnElementLoaded(percent, ElementMesh.Vertex); } } //MeshModel mesh = new MeshModel(header.Elements["face"].Count); mesh.Triangles = new MeshTriangle[header.Elements["face"].Count]; mesh.BoundBox = boundBox; for (int i = 0, ptr = 0; i < mesh.Triangles.Length; i++) { str = sr.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); indices[ptr++] = Int32.Parse(str[1], nfi); mesh.Triangles[i].Vertex1 = vertices[indices[ptr - 1]]; indices[ptr++] = Int32.Parse(str[2], nfi); mesh.Triangles[i].Vertex2 = vertices[indices[ptr - 1]]; indices[ptr++] = Int32.Parse(str[3], nfi); mesh.Triangles[i].Vertex3 = vertices[indices[ptr - 1]]; int percent = (int)(((float)i / indices.Length) * 100.0f); if ((percent % 20) == 0) { this.OnElementLoaded(percent, ElementMesh.VextexIndice); } } int verticesCount = vertices.Length; vertices = null; GC.Collect(); GC.WaitForPendingFinalizers(); ProcessNormalsPerVertex(indices, ref mesh, verticesCount); indices = null; GC.Collect(); GC.WaitForPendingFinalizers(); }
public override VertexColorTextureNormal Convert(MeshVertex vertex) { return(VertexColorTextureNormal.Convert(vertex)); }
public override VertexColorSkin Convert(MeshVertex vertex) { return(VertexColorSkin.Convert(vertex)); }