• Stars
    star
    179
  • Rank 214,039 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A small and fast library for extruding 2D polygons and polylines to 3D meshes

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]],
    // Hole
    [[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 same with coordinates of MultiPolygon type geometry in GeoJSON
    // See http://wiki.geojson.org/GeoJSON_draft_version_6#MultiPolygon
    polygons: GeoJSONMultiPolygonGeometry,
    // Options of extrude
    opts: {
        // Can be a constant value, or a function.
        // Default to be 1.
        depth?: ((idx: number) => number) | number,
        // Size of bevel, default to be 0, which is no bevel.
        bevelSize?: number,
        // Segments of bevel, default to be 2. Larger value will lead to smoother bevel.
        bevelSegments?: number,
        // Polygon or polyline simplification tolerance. Default to be 0.
        // Use https://www.npmjs.com/package/simplify-js to do the simplification. Same with the tolerance parameter in it. The unit is same with depth and bevelSize
        simplify?: number,
        // If has smooth side, default to be false.
        smoothSide?: boolean,
        // If has smooth bevel, default to be false.
        smoothBevel?: boolean,
        // If exclude bottom faces, default to be false.
        // Usefull when bottom side can't be seen.
        excludeBottom?: boolean,
        // Transform the polygon to fit this rect.
        // Will keep polygon aspect if only width or height is given.
        fitRect?: {x?: number, y?: number, width?: number: height?: number},
        // Translate the polygon. Default to be [0, 0]
        // Will be ignored if fitRect is given.
        translate?: ArrayLike<number>,
        // Scale the polygon. Default to be [1, 1]
        // Will be ignored if fitRect is given.
        scale?: ArrayLike<number>
    }
) => {
    indices: Uint16Array|Uint32Array,
    position: Float32Array,
    normal: Float32Array,
    uv: Float32Array,
    boundingRect: {x: number, y: number, width: number, height: number}
}

extrudePolyline

extrudePolyline(
    // polylines same with coordinates of MultiLineString type geometry in GeoJSON
    // See http://wiki.geojson.org/GeoJSON_draft_version_6#MultiLineString
    polylines: GeoJSONMultiLineStringGeometry,
    // Options of extrude
    opts: {
        ////// Extended from opts in extrudePolygon

        // Thickness of line, default to be 1
        lineWidth?: number,
        // default to be 2
        miterLimit?: number
    }
) => {
    indices: Uint16Array|Uint32Array,
    position: Float32Array,
    normal: Float32Array,
    uv: Float32Array,
    boundingRect: {x: number, y: number, width: number, height: number}
}

extrudeGeoJSON

extrudeGeoJSON(
    // Extrude geojson with Polygon/LineString/MultiPolygon/MultiLineString geometries.
    geojson: GeoJSON,
    // Options of extrude
    opts: {
        ////// Extended from opts in extrudePolygon

        // Can be a constant value, or a function with parameter of each feature in geojson.
        // Default to be 1.
        depth?: ((feature: GeoJSONFeature) => number) | number
        // Thickness of line, default to be 1
        lineWidth?: number,
        // default to be 2
        miterLimit?: number
    }
) => {
    // Same result with extrudePolygon
    polygon: Object,
    // Same result with extrudePolyline
    polyline: Object
}

More Repositories

1

claygl

A WebGL graphic library for building scalable Web3D applications
JavaScript
2,788
star
2

little-big-city

JavaScript
809
star
3

clay-viewer

3D model viewer with high quality rendering and glTF2.0/GLB export
JavaScript
767
star
4

dota2hero

Online Dota2 hero viewer based on WebGL
JavaScript
447
star
5

oidn-web

Open Image Denoise on the Web
TypeScript
215
star
6

starbucks

Starbucks locations represented by echarts map
HTML
182
star
7

paper-quilling-art

https://pissang.github.io/paper-quilling-art/
JavaScript
173
star
8

voxelize-image

JavaScript
126
star
9

papercut-box-art

Generate a paper cut art in few clicks!
JavaScript
112
star
10

emage

WebGL based image processing library
JavaScript
91
star
11

echarts-x

Extension pack of ECharts providing 3d plots and globe visualization
JavaScript
52
star
12

dfatool

JavaScript Data Flow Analyze Tool
JavaScript
47
star
13

claygl-advanced-renderer

Advanced rendering pipeline for ClayGL
JavaScript
45
star
14

worldcup-intro

World Cup 2014 intro movie
JavaScript
31
star
15

echarts-factory

TypeScript
25
star
16

lottie-parser

Parsing lottie files and display it in the Apache ECharts.
TypeScript
22
star
17

geometry-extrude-example-threejs

JavaScript
21
star
18

echarts-www-landing-animation

TypeScript
18
star
19

qtek-bootcamp

Bootcamp demo of qtek
JavaScript
17
star
20

undersea

Undersea rendering with WebGL
JavaScript
14
star
21

slides

JavaScript
13
star
22

claygl-examples

Examples of ClayGL
JavaScript
13
star
23

lottery

Lottery
JavaScript
12
star
24

bkgraph

人物图谱前端樑板
JavaScript
12
star
25

echarts-next

JavaScript
11
star
26

liquidfun-demo

JavaScript
11
star
27

qtek-physics

Physics compoment of qtek
JavaScript
11
star
28

echarts-worker

JavaScript
10
star
29

qtek-2d

2d rendering component of qtek
JavaScript
9
star
30

curlnoise-2d

2D Curlnoise Example of ClayGL
JavaScript
6
star
31

colorful-stream

JavaScript
5
star
32

lowpoly

Realtime lowpolify
JavaScript
5
star
33

oidn-web-example

Example of oidn-web
JavaScript
5
star
34

pissang.github.io

HTML
4
star
35

parametric-surface-fun

JavaScript
4
star
36

qtek-app

3D application framework based on qtek
JavaScript
4
star
37

moodymusic

Music Classification By Mood
JavaScript
4
star
38

qtek-editor-DEPRECATED

A scene editor for three.js
JavaScript
4
star
39

epage

Web based page editor
JavaScript
3
star
40

webgl-library-benchmark

JavaScript
2
star
41

hierarchy

JavaScript
2
star
42

squares

JavaScript
2
star
43

grapher.js

Simple 3d plots library
JavaScript
2
star
44

seajs-plugin-aop

AOP plugin for SeaJS
JavaScript
1
star
45

echarts-doc-public

Deploying experimental echarts docs
HTML
1
star
46

claygl-docs

Documentation of ClayGL
HTML
1
star
47

geometry-extrude-example-regl

JavaScript
1
star
48

claygl.xyz

ClayGL official website
JavaScript
1
star
49

echarts-www-spa-boilerplate

Boilerplate for creating a SPA tool page that can be integrated in Apache ECharts website.
HTML
1
star
50

doubi

JavaScript
1
star