Skip to main content

Overview

The Polygon 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

ogid
string
Unique identifier for the polygon. Auto-generated if not provided.
vertices
Vector3[]
required
Array of vertices defining the polygon boundary. Must be in counter-clockwise order when viewed from above (positive Y-axis).
color
number
required
Hexadecimal color value for the polygon fill.

Methods

setConfig()

Updates the polygon configuration and regenerates geometry.
Example:

addVertices()

Replaces all vertices with a new set and regenerates the polygon.
Example:

addHole()

Adds a hole to the polygon. Hole vertices must be in clockwise order.
Example:

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

ogid
string
Unique identifier for the polygon instance.
options
IPolygonOptions
Current polygon configuration including vertices and color.
transformationMatrix
THREE.Matrix4
Transformation matrix for the polygon geometry.
color
number
Get or set the polygon fill color.
outline
boolean
Enable or disable outline rendering.
outlineColor
number
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 the triangulate_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:
Source: /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

Vertex Order: Always define outer polygon vertices in counter-clockwise (CCW) order and hole vertices in clockwise (CW) order when viewed from above.
Minimum Vertices: Polygons require at least 3 vertices. The geometry generation will fail silently if fewer vertices are provided.
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
Last modified on March 14, 2026