Class PolygonMesh
Namespace: ValveResourceFormat.IO.ContentFormats.HalfEdgeMesh
Assembly: ValveResourceFormat.dll
An editable polygon mesh: a topology carrying the data of a Hammer mesh, positions per vertex, corner data per half edge and a material per face, with the editing operations that keep that data right. Taken from Sbox
public sealed class PolygonMeshInheritance
Constructors
PolygonMesh()
Initializes a new instance of the class.
public PolygonMesh()Fields
DefaultTextureScale
Units one texel spans when a face's texture is projected at the default scale, Hammer's default when aligning a texture to the grid, in most s2 games hammer has this set to 0.125, Sbox uses 0.25 here
public const float DefaultTextureScale = 0.125Field Value
Properties
FaceHandles
Faces of the mesh.
public IEnumerable<FaceHandle> FaceHandles { get; }Property Value
HalfEdgeHandles
Half edges of the mesh.
public IEnumerable<HalfEdgeHandle> HalfEdgeHandles { get; }Property Value
MaterialIndex
Index into per face, -1 for no material.
public FaceData<int> MaterialIndex { get; }Property Value
Materials
Materials used by the faces, in order.
public IReadOnlyList<string> Materials { get; }Property Value
Normals
Normal per corner.
public HalfEdgeData<Vector3> Normals { get; }Property Value
Positions
Position per vertex.
public VertexData<Vector3> Positions { get; }Property Value
Tangents
Tangent per corner.
public HalfEdgeData<Vector4> Tangents { get; }Property Value
TextureCoords
Texture coordinates per corner.
public HalfEdgeData<Vector2> TextureCoords { get; }Property Value
TextureCoords1
Second texture coordinates per corner.
public HalfEdgeData<Vector2> TextureCoords1 { get; }Property Value
TextureOffset
Texture projection offset per face, in texels.
public FaceData<Vector2> TextureOffset { get; }Property Value
TextureScale
Texture projection scale per face, world units per texel.
public FaceData<Vector2> TextureScale { get; }Property Value
TextureUAxis
Texture projection U axis per face, see .
public FaceData<Vector3> TextureUAxis { get; }Property Value
TextureVAxis
Texture projection V axis per face.
public FaceData<Vector3> TextureVAxis { get; }Property Value
Topology
The half edge topology.
public HalfEdgeMesh Topology { get; }Property Value
VertexHandles
Vertices of the mesh.
public IEnumerable<VertexHandle> VertexHandles { get; }Property Value
VertexPaintBlendParams
Vertex paint blend parameters per corner.
public HalfEdgeData<Vector4> VertexPaintBlendParams { get; }Property Value
VertexPaintTintColor
Vertex paint tint per corner.
public HalfEdgeData<Vector4> VertexPaintTintColor { get; }Property Value
Methods
AddFace(params VertexHandle[])
Connect these vertices to make a face
public FaceHandle AddFace(params VertexHandle[] hVertices)Parameters
hVertices VertexHandle[]
Returns
AddMaterial(string?)
Returns the index of a material, adding it when it is new.
public int AddMaterial(string? material)Parameters
material string?
Returns
AddVertex(Vector3)
Add a vertex to the topology
public VertexHandle AddVertex(Vector3 position)Parameters
position Vector3
Returns
AddVertexToEdge(VertexHandle, VertexHandle, float, out VertexHandle)
Splits the edge between two vertices by inserting a new vertex at the given parameter along it, interpolating the vertex and corner data.
public bool AddVertexToEdge(VertexHandle hVertexA, VertexHandle hVertexB, float flParam, out VertexHandle pOutNewVertex)Parameters
hVertexA VertexHandle
hVertexB VertexHandle
flParam float
pOutNewVertex VertexHandle
Returns
AddVertices(ReadOnlySpan<Vector3>)
Add multiple vertices to the topology
public VertexHandle[] AddVertices(ReadOnlySpan<Vector3> positions)Parameters
positions ReadOnlySpan<Vector3>
Returns
AreEdgesCoLinear(HalfEdgeHandle, HalfEdgeHandle, float)
Whether two edges run along the same line within an angle.
public bool AreEdgesCoLinear(HalfEdgeHandle hEdgeA, HalfEdgeHandle hEdgeB, float flAngleToleranceInDegrees)Parameters
hEdgeA HalfEdgeHandle
hEdgeB HalfEdgeHandle
flAngleToleranceInDegrees float
Returns
CombineFaces(IReadOnlyList<FaceHandle>)
Combines a set of faces into one by dissolving the edges they share, removing the vertices left on straight runs.
public void CombineFaces(IReadOnlyList<FaceHandle> faces)Parameters
faces IReadOnlyList<FaceHandle>
ComputeFaceNormal(FaceHandle, out Vector3)
Computes the normal of a face.
public void ComputeFaceNormal(FaceHandle hFace, out Vector3 pOutNormal)Parameters
hFace FaceHandle
pOutNormal Vector3
ComputeFaceTextureCoordinatesFromParameters(FaceHandle, Vector2, float)
Computes the texture coordinates of a face's corners from its texture projection parameters.
public void ComputeFaceTextureCoordinatesFromParameters(FaceHandle hFace, Vector2 textureSize, float defaultScale = 0.125)Parameters
hFace FaceHandle
Face to compute.
textureSize Vector2
Size of the face's texture in texels.
defaultScale float
Scale used where the face has none.
ComputeFaceTextureParametersFromCoordinates(FaceHandle, Vector2)
Computes the texture projection parameters of a face from its texture coordinates: the world axes, scale and offset that reproduce them through , fitted through the three corners that span the face best.
public void ComputeFaceTextureParametersFromCoordinates(FaceHandle hFace, Vector2 textureSize)Parameters
hFace FaceHandle
Face to compute.
textureSize Vector2
Size of the face's texture in texels, the parameters are in texels.
ComputeFaceVertexTangent(HalfEdgeHandle, Vector3, out Vector4)
Computes the tangent of a face corner from the face's texture mapping around it, as a direction with the handedness in W, the way a compiled mesh carries it.
public void ComputeFaceVertexTangent(HalfEdgeHandle hFaceVertex, Vector3 normal, out Vector4 tangent)Parameters
hFaceVertex HalfEdgeHandle
Half edge ending at the corner.
normal Vector3
Normal at the corner.
tangent Vector4
The tangent, built from the normal alone when the mapping gives none.
DissolveEdge(HalfEdgeHandle)
Merges the two faces of an edge by removing it.
public void DissolveEdge(HalfEdgeHandle edge)Parameters
edge HalfEdgeHandle
DissolveEdges(IReadOnlyList<HalfEdgeHandle>, bool, DissolveRemoveVertexCondition)
Merges the faces on either side of each edge by removing the edges, optionally only where the faces are coplanar, and removes the end vertices that are left with only two edges under the given condition.
public void DissolveEdges(IReadOnlyList<HalfEdgeHandle> edges, bool bFaceMustBePlanar, PolygonMesh.DissolveRemoveVertexCondition removeCondition)Parameters
edges IReadOnlyList<HalfEdgeHandle>
bFaceMustBePlanar bool
removeCondition PolygonMesh.DissolveRemoveVertexCondition
FindEdgeConnectingVertices(VertexHandle, VertexHandle)
Finds the full edge connecting two vertices.
public HalfEdgeHandle FindEdgeConnectingVertices(VertexHandle hVertexA, VertexHandle hVertexB)Parameters
hVertexA VertexHandle
hVertexB VertexHandle
Returns
FindFaceIslands(IReadOnlyList<FaceHandle>, out List<List<FaceHandle>>)
Groups faces into islands connected through shared edges.
public static void FindFaceIslands(IReadOnlyList<FaceHandle> faces, out List<List<FaceHandle>> outFaces)Parameters
faces IReadOnlyList<FaceHandle>
outFaces List<List<FaceHandle>>
FindIslands()
Groups the faces into islands connected through shared edges. Faces that only touch at a vertex stay apart, so a bowtie vertex doesn't glue two separate objects together. Faces without any neighbour (their vertices were duplicated, or they stand alone) are grouped through coinciding vertex positions instead, so the pieces of one object stay together without collecting every stray face in one lump.
public List<List<FaceHandle>> FindIslands()Returns
GetEdgeVertexPositions(HalfEdgeHandle, out Vector3, out Vector3)
Get start and end points of an edge
public void GetEdgeVertexPositions(HalfEdgeHandle hEdge, out Vector3 pOutVertexA, out Vector3 pOutVertexB)Parameters
hEdge HalfEdgeHandle
pOutVertexA Vector3
pOutVertexB Vector3
GetFaceMaterial(FaceHandle)
Gets the material of a face, null when it has none.
public string? GetFaceMaterial(FaceHandle hFace)Parameters
hFace FaceHandle
Returns
GetFaceVertices(FaceHandle)
Gets the vertices of a face, in loop order.
public static VertexHandle[] GetFaceVertices(FaceHandle hFace)Parameters
hFace FaceHandle
Returns
GetVertexPosition(VertexHandle)
Get the position of a vertex
public Vector3 GetVertexPosition(VertexHandle hVertex)Parameters
hVertex VertexHandle
Returns
MergeCoincidentOpenEdges(float)
Merges every pair of open edges that lie on top of each other, running in opposite directions, the way two sheets meet along a seam. Merging the seams edge by edge joins the sheets without creating bowtie vertices, which merging vertex by vertex does when two faces that only touch at a point get merged first. Use before
.public int MergeCoincidentOpenEdges(float maxDistance)Parameters
maxDistance float
Largest distance between the edge end points that still counts as coinciding.
Returns
Number of edges merged.
MergeEdges(HalfEdgeHandle, HalfEdgeHandle, out HalfEdgeHandle)
Merges two edges into one, merging their end vertices pairwise.
public bool MergeEdges(HalfEdgeHandle hEdgeA, HalfEdgeHandle hEdgeB, out HalfEdgeHandle hOutNewEdge)Parameters
hEdgeA HalfEdgeHandle
hEdgeB HalfEdgeHandle
hOutNewEdge HalfEdgeHandle
Returns
MergeMesh(PolygonMesh, out Dictionary<VertexHandle, VertexHandle>, out Dictionary<HalfEdgeHandle, HalfEdgeHandle>, out Dictionary<FaceHandle, FaceHandle>)
Copies another mesh into this one, reporting the handle each source component landed on.
public void MergeMesh(PolygonMesh sourceMesh, out Dictionary<VertexHandle, VertexHandle> newVertices, out Dictionary<HalfEdgeHandle, HalfEdgeHandle> newHalfEdges, out Dictionary<FaceHandle, FaceHandle> newFaces)Parameters
sourceMesh PolygonMesh
newVertices Dictionary<VertexHandle, VertexHandle>
newHalfEdges Dictionary<HalfEdgeHandle, HalfEdgeHandle>
newFaces Dictionary<FaceHandle, FaceHandle>
MergeMesh(PolygonMesh, IReadOnlyCollection<FaceHandle>, out Dictionary<VertexHandle, VertexHandle>, out Dictionary<HalfEdgeHandle, HalfEdgeHandle>, out Dictionary<FaceHandle, FaceHandle>)
Copies a set of faces of another mesh into this one, with their edges and vertices, reporting the handle each source component landed on. The faces should form whole islands connected through edges.
public void MergeMesh(PolygonMesh sourceMesh, IReadOnlyCollection<FaceHandle> faces, out Dictionary<VertexHandle, VertexHandle> newVertices, out Dictionary<HalfEdgeHandle, HalfEdgeHandle> newHalfEdges, out Dictionary<FaceHandle, FaceHandle> newFaces)Parameters
sourceMesh PolygonMesh
faces IReadOnlyCollection<FaceHandle>
newVertices Dictionary<VertexHandle, VertexHandle>
newHalfEdges Dictionary<HalfEdgeHandle, HalfEdgeHandle>
newFaces Dictionary<FaceHandle, FaceHandle>
MergeVertices(VertexHandle, VertexHandle, float, out VertexHandle)
Merges two vertices into one where the topology allows it, interpolating the position by param (0 keeps the first vertex, 1 the second). Connected vertices collapse their edge.
public bool MergeVertices(VertexHandle hVertexA, VertexHandle hVertexB, float param, out VertexHandle hOutNewVertex)Parameters
hVertexA VertexHandle
hVertexB VertexHandle
param float
hOutNewVertex VertexHandle
Returns
MergeVerticesWithinDistance(float)
Merges every vertex of the mesh that lies within maxDistance of another one, the Hammer "merge vertices by distance" operation. Use after all faces were added: the input vertex indices the builder handed out no longer apply afterwards.
public int MergeVerticesWithinDistance(float maxDistance)Parameters
maxDistance float
Largest distance between two vertices that still get merged.
Returns
Number of vertices merged away.
MergeVerticesWithinDistance(IReadOnlyList<VertexHandle>, float, bool, out List<VertexHandle>)
Merges the given vertices into groups of vertices lying within maxDistance of each other
public int MergeVerticesWithinDistance(IReadOnlyList<VertexHandle> originalVertices, float maxDistance, bool averagePositions, out List<VertexHandle> finalVertices)Parameters
originalVertices IReadOnlyList<VertexHandle>
Vertices to consider for merging.
maxDistance float
Largest distance between two vertices that still get merged, negative merges all of them into one.
averagePositions bool
Move each merged vertex to the average of the positions it merged, otherwise the first vertex of a group keeps its position.
finalVertices List<VertexHandle>
The vertices left over after merging.
Returns
Number of vertices merged away.
RemoveColinearVertex(VertexHandle, float)
Removes a vertex connected to exactly two edges that continue each other, merging them into one edge.
public bool RemoveColinearVertex(VertexHandle hVertex, float flColinearAngleTolerance = 5)Parameters
hVertex VertexHandle
flColinearAngleTolerance float
Returns
RemoveEdges(IEnumerable<HalfEdgeHandle>)
Remove these edges
public void RemoveEdges(IEnumerable<HalfEdgeHandle> hEdges)Parameters
hEdges IEnumerable<HalfEdgeHandle>
RemoveFaces(IEnumerable<FaceHandle>)
Remove these faces
public void RemoveFaces(IEnumerable<FaceHandle> hFaces)Parameters
hFaces IEnumerable<FaceHandle>
RemoveVertices(IEnumerable<VertexHandle>)
Remove these vertices
public void RemoveVertices(IEnumerable<VertexHandle> hVertices)Parameters
hVertices IEnumerable<VertexHandle>
SetFaceMaterial(FaceHandle, string?)
Sets the material of a face.
public void SetFaceMaterial(FaceHandle hFace, string? material)Parameters
hFace FaceHandle
material string?
SetVertexPosition(VertexHandle, Vector3)
Set the position of a vertex
public void SetVertexPosition(VertexHandle hVertex, Vector3 position)Parameters
hVertex VertexHandle
position Vector3
TextureAlignToGrid(FaceHandle, Vector2)
Aligns the texture projection of a face to the world axes closest to its plane, Hammer's default alignment, and computes its texture coordinates from that.
public void TextureAlignToGrid(FaceHandle hFace, Vector2 textureSize)Parameters
hFace FaceHandle
Face to align.
textureSize Vector2
Size of the face's texture in texels, the projection is in texels.
Untriangulate(float)
Joins pairs of coplanar triangles into quads where they use the same material and agree on their corner data along the shared edge, so no texture seam, hard edge or vertex paint break gets stretched over a quad.
public int Untriangulate(float maxFaceAngleDegrees = 40)Parameters
maxFaceAngleDegrees float
Returns
Number of triangle pairs joined.

