Overview
ThePolygon class creates 2D polygonal shapes on the XY plane. It supports complex polygons with holes, automatic triangulation, and outline rendering. Polygons are defined by counter-clockwise (CCW) vertices, with holes specified in clockwise (CW) order.
Constructor
IPolygonOptions
Unique identifier for the polygon. Auto-generated if not provided.
Array of vertices defining the polygon boundary. Must be in counter-clockwise order when viewed from above (positive Y-axis).
Hexadecimal color value for the polygon fill.
Methods
setConfig()
Updates the polygon configuration and regenerates geometry.addVertices()
Replaces all vertices with a new set and regenerates the polygon.addHole()
Adds a hole to the polygon. Hole vertices must be in clockwise order.getBrepData()
Returns the B-Rep (Boundary Representation) data as a JSON string.generateGeometry()
Regenerates the THREE.js geometry from the current polygon configuration. Automatically called after configuration changes.dispose()
Cleans up geometry and material resources.Properties
Unique identifier for the polygon instance.
Current polygon configuration including vertices and color.
Transformation matrix for the polygon geometry.
Get or set the polygon fill color.
Enable or disable outline rendering.
Get or set the outline color (default: 0x000000 - black).
Usage Examples
Basic Rectangle
Polygon with Hole
Complex Shape
Implementation Details
Triangulation
The polygon is automatically triangulated using thetriangulate_polygon_with_holes function from the Rust kernel. This ensures proper rendering of complex shapes with holes.
Source: /workspace/source/main/opengeometry/src/shapes/polygon.ts:155
Geometry Generation
Geometry is stored as a THREE.BufferGeometry with double-sided material to ensure visibility from both sides:/workspace/source/main/opengeometry/src/shapes/polygon.ts:176-182
Outline Rendering
Outlines are rendered as THREE.LineSegments, positioned as a child of the polygon mesh to maintain transformation hierarchy. Source:/workspace/source/main/opengeometry/src/shapes/polygon.ts:345-379
Best Practices
Performance: For polygons with many vertices or multiple holes, consider enabling outline rendering only when necessary, as it adds additional geometry.
Live Demo
Polygon Demo
Try the Polygon shape in the browser
See Also
Cuboid
Rectangular 3D boxes
Cylinder
Circular extrusions
Sweep
Extrude profiles along paths

