Geometries for 3D rendering, including normals, UVs and cell indices (faces). Perfect if you want to supercharge your dependency folder... with 30KB of geometries.
npm install primitive-geometry- Common API: options object in, simplicial complex out
- Outputs TypedArray (
Float32Arrayfor geometry data andUint8Array|Uint16Array|Uint32Arrayfor cells) - Zero dependency
- Same parameters naming: radius (or rx/ry/rz), scale (or height/sx/sy/sz), segments (or nx/ny/nz) and a few specific parameters for icosphere/cylinder/cone/torus.
See difference with v1 here.
See the example and its source.
import Primitives from "primitive-geometry";
const quadGeometry = Primitives.quad();
const planeGeometry = Primitives.plane();
const cubeGeometry = Primitives.cube();
const roundedCubeGeometry = Primitives.roundedCube();
const cylinderGeometry = Primitives.cylinder();
const coneGeometry = Primitives.cone();
const capsuleGeometry = Primitives.capsule();
const sphereGeometry = Primitives.sphere();
const icosphereGeometry = Primitives.icosphere();
const ellipsoidGeometry = Primitives.ellipsoid();
const torusGeometry = Primitives.torus();
// without normals/uvs
const boxGeometry = Primitives.box();
const circleGeometry = Primitives.circle();
console.log(quadGeometry);
// {
// positions: [ [x, y, z], [x, y, z], ... ],
// cells: [ [a, b, c], [a, b, c], ... ],
// uvs: [ [u, v], [u, v], ... ],
// normals: [ [x, y, z], [x, y, z], ... ]
// }- index
Re-export all geometries and utils.
- box
- capsule
- circle
- cone
- cube
- cylinder
- ellipsoid
- icosphere
- plane
- quad
- rounded-cube
- sphere
- torus
- utils
- BasicSimplicialComplex :
Object Geometry definition without normals and UVs.
- SimplicialComplex :
Object Geometry definition.
Re-export all geometries and utils.
- box
- box([options]) ⇒
BasicSimplicialComplex⏏- ~BoxOptions :
Object
- ~BoxOptions :
- box([options]) ⇒
box([options]) ⇒ BasicSimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | BoxOptions |
{} |
Kind: inner typedef of box
Properties
| Name | Type | Default |
|---|---|---|
| [sx] | number |
1 |
| [sy] | number |
sx |
| [sz] | number |
sx |
- capsule
- capsule([options]) ⇒
SimplicialComplex⏏- ~CapsuleOptions :
Object
- ~CapsuleOptions :
- capsule([options]) ⇒
capsule([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | CapsuleOptions |
{} |
Kind: inner typedef of capsule
Properties
| Name | Type | Default |
|---|---|---|
| [height] | number |
1 |
| [radius] | number |
0.25 |
| [nx] | number |
16 |
| [ny] | number |
32 |
- circle
- circle([options]) ⇒
BasicSimplicialComplex⏏- ~BoxOptions :
Object
- ~BoxOptions :
- circle([options]) ⇒
circle([options]) ⇒ BasicSimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | BoxOptions |
{} |
Kind: inner typedef of circle
Properties
| Name | Type | Default |
|---|---|---|
| [radius] | number |
0.5 |
| [segments] | number |
32 |
- cone
- cone([options]) ⇒
SimplicialComplex⏏- ~ConeOptions :
Object
- ~ConeOptions :
- cone([options]) ⇒
cone([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | ConeOptions |
{} |
Kind: inner typedef of cone
Properties
| Name | Type | Default |
|---|---|---|
| [height] | number |
1 |
| [radius] | number |
0.25 |
| [nx] | number |
16 |
| [ny] | number |
1 |
| [capSegments] | number |
1 |
| [capBase] | boolean |
true |
- cube
- cube([options]) ⇒
SimplicialComplex⏏- ~CubeOptions :
Object
- ~CubeOptions :
- cube([options]) ⇒
cube([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | CubeOptions |
{} |
Kind: inner typedef of cube
Properties
| Name | Type | Default |
|---|---|---|
| [sx] | number |
1 |
| [sy] | number |
sx |
| [sz] | number |
sx |
| [nx] | number |
1 |
| [ny] | number |
nx |
| [nz] | number |
nx |
- cylinder
- cylinder([options]) ⇒
SimplicialComplex⏏- ~CylinderOptions :
Object
- ~CylinderOptions :
- cylinder([options]) ⇒
cylinder([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | CylinderOptions |
{} |
Kind: inner typedef of cylinder
Properties
| Name | Type | Default |
|---|---|---|
| [height] | number |
1 |
| [radius] | number |
0.25 |
| [nx] | number |
16 |
| [ny] | number |
1 |
| [radiusApex] | number |
radius |
| [capSegments] | number |
1 |
| [capApex] | boolean |
true |
| [capBase] | boolean |
true |
- ellipsoid
- ellipsoid([options]) ⇒
SimplicialComplex⏏- ~EllipsoidOptions :
Object
- ~EllipsoidOptions :
- ellipsoid([options]) ⇒
ellipsoid([options]) ⇒ SimplicialComplex ⏏
Default to an oblate spheroid.
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | EllipsoidOptions |
{} |
Kind: inner typedef of ellipsoid
Properties
| Name | Type | Default |
|---|---|---|
| [radius] | number |
0.5 |
| [nx] | number |
32 |
| [ny] | number |
16 |
| [rx] | number |
1 |
| [rx] | number |
0.5 |
| [rz] | number |
ry |
- icosphere
- icosphere([options]) ⇒
SimplicialComplex⏏- ~IcosphereOptions :
Object
- ~IcosphereOptions :
- icosphere([options]) ⇒
icosphere([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | IcosphereOptions |
{} |
Kind: inner typedef of icosphere
Properties
| Name | Type | Default |
|---|---|---|
| [radius] | number |
0.5 |
| [subdivisions] | number |
2 |
- plane
- plane([options]) ⇒
SimplicialComplex⏏- ~PlaneOptions :
Object
- ~PlaneOptions :
- plane([options]) ⇒
plane([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | PlaneOptions |
{} |
Kind: inner typedef of plane
Properties
| Name | Type | Default |
|---|---|---|
| [sx] | number |
1 |
| [sy] | number |
sx |
| [nx] | number |
1 |
| [ny] | number |
nx |
- quad
- quad([options]) ⇒
SimplicialComplex⏏- ~QuadOptions :
Object
- ~QuadOptions :
- quad([options]) ⇒
quad([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | QuadOptions |
{} |
Kind: inner typedef of quad
Properties
| Name | Type | Default |
|---|---|---|
| [scale] | number |
0.5 |
roundedCube([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | module:rounded~RoundedCubeOptions |
{} |
Kind: inner typedef of roundedCube
Properties
| Name | Type | Default |
|---|---|---|
| [sx] | number |
1 |
| [sy] | number |
sx |
| [sz] | number |
sx |
| [nx] | number |
16 |
| [ny] | number |
nx |
| [nz] | number |
nx |
| [radius] | number |
sx * 0.25 |
- sphere
- sphere([options]) ⇒
SimplicialComplex⏏- ~SphereOptions :
Object
- ~SphereOptions :
- sphere([options]) ⇒
sphere([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | SphereOptions |
{} |
Kind: inner typedef of sphere
Properties
| Name | Type | Default |
|---|---|---|
| [radius] | number |
0.5 |
| [nx] | number |
32 |
| [ny] | number |
16 |
- torus
- torus([options]) ⇒
SimplicialComplex⏏- ~TorusOptions :
Object
- ~TorusOptions :
- torus([options]) ⇒
torus([options]) ⇒ SimplicialComplex ⏏
Kind: Exported function
| Param | Type | Default |
|---|---|---|
| [options] | TorusOptions |
{} |
Kind: inner typedef of torus
Properties
| Name | Type | Default |
|---|---|---|
| [radius] | number |
0.5 |
| [segments] | number |
64 |
| [minorRadius] | number |
0.1 |
| [minorSegments] | number |
16 |
| [arc] | number |
2 * Math.PI |
- utils
- .TAU :
number - .getCellsTypedArray ⇒
Uint8Array|Uint16Array|Uint32Array - .normalize(v) ⇒
Array.<number> - .setTypedArrayType(type)
- .TAU :
Two times PI.
Kind: static constant of utils
Select cells typed array from a size determined by amount of vertices.
Kind: static constant of utils
See: MDN TypedArray objects
| Param | Type | Description |
|---|---|---|
| size | number |
The max value expected |
Normalize a vector 3.
Kind: static method of utils
Returns: Array.<number> - Normalized vector
| Param | Type | Description |
|---|---|---|
| v | Array.<number> |
Vector 3 array |
Enforce a typed array constructor for cells
Kind: static method of utils
| Param | Type |
|---|---|
| type | Class.<Uint8Array> | Class.<Uint16Array> | Class.<Uint32Array> |
Geometry definition without normals and UVs.
Kind: global typedef
Properties
| Name | Type |
|---|---|
| positions | Float32Array |
| cells | Uint8Array | Uint16Array | Uint32Array |
Geometry definition.
Kind: global typedef
Properties
| Name | Type |
|---|---|
| positions | Float32Array |
| normals | Float32Array |
| uvs | Float32Array |
| cells | Uint8Array | Uint16Array | Uint32Array |
See original packages used in v1:
- primitive-quad
- primitive-plane
- primitive-cube
- primitive-rounded-cube
- primitive-capsule
- primitive-sphere
- primitive-icosphere
- primitive-ellipsoid
- primitive-torus
- primitive-cylinder
- primitive-box
- primitive-circle
Differences with v1:
- fix cylinder orientation and uvs
- fix icosphere uvs (based on: https://github.com/mourner/icomesh)
- uniformise api and internal names
- use options object
- remove gl-matrix/pex-math and icosphere dependencies
- use only trigonometric operation, no matrix transformation
- base sphere on ellispsoid
- add cone based on cylinder
- use flat typed arrays
- add jsdoc, prettier, eslint via snowdev
MIT. See license file.
