Geometry Extrude
A small and fast JavaScript library for extruding 2D polygons and polylines to 3D meshes. It depends on earcut to do triangulation.
Features
-
Extrude polygons with holes.
-
Extrude polylines with specific line thickness.
-
Generate position
/ uv
/ normal
/ indices
TypedArray.
-
Support bevel style.
Basic Usage
Install with npm
npm i geometry-extrude
Extrude a simple square with hole
import {extrudePolygon} from 'geometry-extrude';
const squareWithHole = [
[[0, 0], [10, 0], [10, 10], [0, 10]],
[[2, 2], [8, 2], [8, 8], [2, 8]]
];
const {indices, position, uv, normal} = extrudePolygon([squareWithHole], {
depth: 2
});
Use with ClayGL
const {indices, position, uv, normal} = extrudePolygon(squareWithHole);
const geometry = new clay.Geometry();
geometry.attributes.position.value = position;
geometry.attributes.texcoord0.value = uv;
geometry.attributes.normal.value = normal;
geometry.indices = indices;
Use with ThreeJS
const {indices, position, uv, normal} = extrudePolygon(squareWithHole);
const geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.Float32BufferAttribute(position, 3));
geometry.addAttribute('normal', new THREE.Float32BufferAttribute(normal, 3));
geometry.setIndex(new THREE.Uint16BufferAttribute(indices, 1));
Example
Use with regl
const {indices, position, uv, normal} = extrudePolygon(squareWithHole);
const draw = regl({
frag: `...`,
vert: `...`,
attributes: {
position: position,
uv: uv,
normal: norma
},
elements: indices
});
Example
Full API List
extrudePolygon
extrudePolygon(
polygons: GeoJSONMultiPolygonGeometry,
opts: {
depth?: ((idx: number) => number) | number,
bevelSize?: number,
bevelSegments?: number,
simplify?: number,
smoothSide?: boolean,
smoothBevel?: boolean,
excludeBottom?: boolean,
fitRect?: {x?: number, y?: number, width?: number: height?: number},
translate?: ArrayLike<number>,
scale?: ArrayLike<number>
}
) => {
indices: Uint16Array|Uint32Array,
position: Float32Array,
normal: Float32Array,
uv: Float32Array,
boundingRect: {x: number, y: number, width: number, height: number}
}
extrudePolyline
extrudePolyline(
polylines: GeoJSONMultiLineStringGeometry,
opts: {
lineWidth?: number,
miterLimit?: number
}
) => {
indices: Uint16Array|Uint32Array,
position: Float32Array,
normal: Float32Array,
uv: Float32Array,
boundingRect: {x: number, y: number, width: number, height: number}
}
extrudeGeoJSON
extrudeGeoJSON(
geojson: GeoJSON,
opts: {
depth?: ((feature: GeoJSONFeature) => number) | number
lineWidth?: number,
miterLimit?: number
}
) => {
polygon: Object,
polyline: Object
}