2.10.0 • Published 3 months ago

primitive-geometry v2.10.0

Weekly downloads
22
License
MIT
Repository
github
Last release
3 months ago

primitive-geometry

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Geometries for 3D rendering, including normals, UVs and cell indices (faces). Perfect if you want to supercharge your dependency folder... with 30KB of geometries.

paypal coinbase twitter

npm.io

Installation

npm install primitive-geometry

Features

  • Common API: options object in, simplicial complex out
  • Outputs TypedArray (Float32Array for geometry data and Uint8Array|Uint16Array|Uint32Array for 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.
  • Different Elliptical mappings: see the comparison images and the demo.

See difference with v1 here.

Usage

See the example and its source.

import Primitives from "primitive-geometry";

const quadGeometry = Primitives.quad({
  scale: 0.5,
});
console.log(quadGeometry);
// {
//   positions: Float32Array [x, y, z, x, y, z,  ...],
//   normals: Float32Array [x, y, z, x, y, z, ...]
//   uvs: Float32Array [u, v, u, v, ...],
//   cells: Uint8/16/32/Array [a, b, c, a, b, c, ...],
// }
const planeGeometry = Primitives.plane({
  sx: 1,
  sy: 1,
  nx: 1,
  ny: 1,
  direction: "z",
  quads: false,
});
const roundedRectangleGeometry = Primitives.roundedRectangle({
  sx: 1,
  sy: 1,
  nx: 1,
  ny: 1,
  radius: 0.25,
  roundSegments: 8,
  edgeSegments: 1,
});
const stadiumGeometry = Primitives.stadium({
  sx: 1,
  sy: 0.5,
  nx: 1,
  ny: 1,
  roundSegments: 8,
  edgeSegments: 1,
});

const ellipseGeometry = Primitives.ellipse({
  sx: 1,
  sy: 0.5,
  radius: 0.5,
  segments: 32,
  innerSegments: 16,
  theta: Math.PI * 2,
  thetaOffset: 0,
  mapping: mappings.elliptical,
});
const disc = Primitives.disc({
  radius: 0.5,
  segments: 32,
  innerSegments: 16,
  theta: Math.PI * 2,
  thetaOffset: 0,
  mapping: mappings.concentric,
});
const superellipse = Primitives.superellipse({
  sx: 1,
  sy: 0.5,
  radius: 0.5,
  segments: 32,
  innerSegments: 16,
  theta: Math.PI * 2,
  thetaOffset: 0,
  mapping: mappings.lamé,
  m: 2,
  n: 2,
});
const squircle = Primitives.squircle({
  sx: 1,
  sy: 1,
  radius: 0.5,
  segments: 128,
  innerSegments: 16,
  theta: Math.PI * 2,
  thetaOffset: 0,
  mapping: mappings.fgSquircular,
  squareness: 0.95,
});
const annulus = Primitives.annulus({
  radius: 0.5,
  segments: 32,
  innerSegments: 16,
  theta: Math.PI * 2,
  thetaOffset: 0,
  innerRadius: 0.25,
  mapping: mappings.concentric,
});
const reuleux = Primitives.reuleux({
  radius: 0.5,
  segments: 32,
  innerSegments: 16,
  theta: Math.PI * 2,
  thetaOffset: 0,
  mapping: mappings.concentric,
  n: 3,
});

const cubeGeometry = Primitives.cube({
  sx: 1,
  sy: 1,
  sz: 1,
  nx: 1,
  ny: 1,
  nz: 1,
});
const roundedCubeGeometry = Primitives.roundedCube({
  sx: 1,
  sy: 1,
  sz: 1,
  nx: 1,
  ny: 1,
  nz: 1,
  radius: 0.25,
  roundSegments: 8,
  edgeSegments: 1,
});

const sphereGeometry = Primitives.sphere({
  radius: 0.5,
  nx: 32,
  ny: 16,
  theta: Math.PI,
  thetaOffset: 0,
  phi: Math.PI * 2,
  phiOffset: 0,
});
const icosphereGeometry = Primitives.icosphere({
  radius: 0.5,
  subdivisions: 2,
});
const ellipsoidGeometry = Primitives.ellipsoid({
  radius: 1,
  nx: 32,
  ny: 16,
  rx: 0.5,
  ry: 0.25,
  rz: 0.25,
  theta: Math.PI,
  thetaOffset: 0,
  phi: Math.PI * 2,
  phiOffset: 0,
});

const cylinderGeometry = Primitives.cylinder({
  height: 1,
  radius: 0.25,
  nx: 16,
  ny: 1,
  radiusApex: 0.25,
  capSegments: 1,
  capApex: true,
  capBase: true,
  capBaseSegments: 1,
  phi: Math.PI * 2,
});
const coneGeometry = Primitives.cone({
  height: 1,
  radius: 0.25,
  nx: 16,
  ny: 1,
  capSegments: 1,
  capBase: true,
  theta: Math.PI * 2,
});
const capsuleGeometry = Primitives.capsule({
  height: 0.5,
  radius: 0.25,
  nx: 16,
  ny: 1,
  roundSegments: 16,
  theta: Math.PI * 2,
});
const torusGeometry = Primitives.torus({
  radius: 0.4,
  segments: 64,
  minorRadius: 0.1,
  minorSegments: 32,
  theta: Math.PI * 2,
  thetaOffset: 0,
  phi: Math.PI * 2,
  phiOffset: 0,
});

const tetrahedron = Primitives.tetrahedron({
  radius: 0.5,
});
const icosahedron = Primitives.icosahedron({
  radius: 0.5,
});

// without normals/uvs
const boxGeometry = Primitives.box({
  sx: 1,
  sy: 1,
  sz: 1,
});
const circleGeometry = Primitives.circle({
  radius: 0.5,
  segments: 32,
  closed: false,
  theta: Math.PI * 2,
  thetaOffset: 0,
});

API

Modules

Typedefs

index

Re-export all geometries, UV mappings functions and utils.

annulus

annulus(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsAnnulusOptions{}

annulus~AnnulusOptions : object

Kind: inner typedef of annulus Properties

NameTypeDefault
radiusnumber0.5
segmentsnumber32
innerSegmentsnumber16
thetanumberTAU
thetaOffsetnumber0
innerRadiusnumberradius * 0.5
mappingfunctionmappings.concentric

box

box(options) ⇒ BasicSimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsBoxOptions{}

box~BoxOptions : object

Kind: inner typedef of box Properties

NameTypeDefault
sxnumber1
synumbersx
sznumbersx

capsule

capsule(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsCapsuleOptions{}

capsule~CapsuleOptions : object

Kind: inner typedef of capsule Properties

NameTypeDefault
heightnumber0.5
radiusnumber0.25
nxnumber16
nynumber1
roundSegmentsnumber32
phinumberTAU

circle

circle(options) ⇒ BasicSimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsCircleOptions{}

circle~CircleOptions : object

Kind: inner typedef of circle Properties

NameTypeDefault
radiusnumber0.5
segmentsnumber32
thetanumberTAU
thetaOffsetnumber0
closedbooleanfalse

cone

cone(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsConeOptions{}

cone~ConeOptions : object

Kind: inner typedef of cone Properties

NameTypeDefault
heightnumber1
radiusnumber0.25
nxnumber16
nynumber1
capSegmentsnumber1
capBasebooleantrue
phinumberTAU

cube

cube(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsCubeOptions{}

cube~CubeOptions : object

Kind: inner typedef of cube Properties

NameTypeDefault
sxnumber1
synumbersx
sznumbersx
nxnumber1
nynumbernx
nznumbernx

cylinder

cylinder(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsCylinderOptions{}

cylinder~CylinderOptions : object

Kind: inner typedef of cylinder Properties

NameTypeDefault
heightnumber1
radiusnumber0.25
nxnumber16
nynumber1
radiusApexnumberradius
capSegmentsnumber1
capApexbooleantrue
capBasebooleantrue
phinumberTAU

disc

disc(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsDiscOptions{}

disc~DiscOptions : object

Kind: inner typedef of disc Properties

NameTypeDefault
radiusnumber0.5
segmentsnumber32
innerSegmentsnumber16
thetanumberTAU
thetaOffsetnumber0
mappingfunctionmappings.concentric

ellipse

ellipse(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsEllipseOptions{}

ellipse~EllipseOptions : object

Kind: inner typedef of ellipse Properties

NameTypeDefault
sxnumber1
synumber0.5
radiusnumber0.5
segmentsnumber32
innerSegmentsnumber16
thetanumberTAU
thetaOffsetnumber0
mappingfunctionmappings.elliptical

ellipsoid

ellipsoid(options) ⇒ SimplicialComplex

Default to an oblate spheroid.

Kind: Exported function

ParamTypeDefault
optionsEllipsoidOptions{}

ellipsoid~EllipsoidOptions : object

Kind: inner typedef of ellipsoid Properties

NameTypeDefault
radiusnumber0.5
nxnumber32
nynumber16
rxnumber1
rynumber0.5
rznumberry
thetanumberMath.PI
thetaOffsetnumber0
phinumberTAU
phiOffsetnumber0

icosahedron

icosahedron(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsIcosahedronOptions{}

icosahedron~IcosahedronOptions : object

Kind: inner typedef of icosahedron Properties

NameTypeDefault
radiusnumber0.5

icosphere

icosphere(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsIcosphereOptions{}

icosphere~IcosphereOptions : object

Kind: inner typedef of icosphere Properties

NameTypeDefault
radiusnumber0.5
subdivisionsnumber2

mappings

plane

plane(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsPlaneOptions{}

plane~PlaneOptions : object

Kind: inner typedef of plane Properties

NameTypeDefault
sxnumber1
synumbersx
nxnumber1
nynumbernx
directionPlaneDirection"z"
quadsbooleanfalse

plane~PlaneDirection : "x" | "-x" | "y" | "-y" | "z" | "-z"

Kind: inner typedef of plane

quad

quad(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsQuadOptions{}

quad~QuadOptions : object

Kind: inner typedef of quad Properties

NameTypeDefault
scalenumber0.5

reuleux

reuleux(options) ⇒ SimplicialComplex

Kind: Exported function See: Parametric equations for regular and Reuleaux polygons

ParamTypeDefault
optionsReuleuxOptions{}

reuleux~ReuleuxOptions : object

Kind: inner typedef of reuleux Properties

NameTypeDefault
radiusnumber0.5
segmentsnumber32
innerSegmentsnumber16
thetanumberTAU
thetaOffsetnumber0
mappingfunctionmappings.concentric
nnumber3

roundedCube

roundedCube(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsRoundedCubeOptions{}

roundedCube~RoundedCubeOptions : object

Kind: inner typedef of roundedCube Properties

NameTypeDefault
sxnumber1
synumbersx
sznumbersx
nxnumber1
nynumbernx
nznumbernx
radiusnumbersx * 0.25
roundSegmentsnumber8
edgeSegmentsnumber1

roundedRectangle

roundedRectangle(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsRoundedCubeOptions{}

roundedRectangle~RoundedCubeOptions : object

Kind: inner typedef of roundedRectangle Properties

NameTypeDefault
sxnumber1
synumbersx
nxnumber1
nynumbernx
radiusnumbersx * 0.25
roundSegmentsnumber8
edgeSegmentsnumber1

sphere

sphere(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsSphereOptions{}

sphere~SphereOptions : object

Kind: inner typedef of sphere Properties

NameTypeDefault
radiusnumber0.5
nxnumber32
nynumber16
thetanumberMath.PI
thetaOffsetnumber0
phinumberTAU
phiOffsetnumber0

squircle

squircle(options) ⇒ SimplicialComplex

Fernández-Guasti squircle

Kind: Exported function See: Squircular Calculations – Chamberlain Fong

ParamTypeDefault
optionsSquircleOptions{}

squircle~SquircleOptions : object

Kind: inner typedef of squircle Properties

NameTypeDefaultDescription
sxnumber1
synumber1
radiusnumber0.5
segmentsnumber128
innerSegmentsnumber16
thetanumberTAU
thetaOffsetnumber0
mappingfunctionmappings.fgSquircular
squarenessnumber0.95Squareness (0 < s <= 1)

stadium

stadium(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsStadiumOptions{}

stadium~StadiumOptions : object

Kind: inner typedef of stadium Properties

NameTypeDefault
sxnumber1
synumbersx
nxnumber1
nynumbernx
roundSegmentsnumber8
edgeSegmentsnumber1

superellipse

superellipse(options) ⇒ SimplicialComplex

Lamé curve See elliptical-mapping example for a few special cases

Kind: Exported function See

ParamTypeDefault
optionsSuperellipseOptions{}

superellipse~SuperellipseOptions : object

Kind: inner typedef of superellipse Properties

NameTypeDefault
sxnumber1
synumber0.5
radiusnumber0.5
segmentsnumber32
innerSegmentsnumber16
thetanumberTAU
thetaOffsetnumber0
mappingfunctionmappings.lamé
mnumber2
nnumberm

tetrahedron

tetrahedron(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsTetrahedronOptions{}

tetrahedron~TetrahedronOptions : object

Kind: inner typedef of tetrahedron Properties

NameTypeDefault
radiusnumber0.5

torus

torus(options) ⇒ SimplicialComplex

Kind: Exported function

ParamTypeDefault
optionsTorusOptions{}

torus~TorusOptions : object

Kind: inner typedef of torus Properties

NameTypeDefault
radiusnumber0.4
segmentsnumber64
minorRadiusnumber0.1
minorSegmentsnumber32
thetanumberTAU
thetaOffsetnumber0
phinumberTAU
phiOffsetnumber0

utils

utils.TAU : number

Two times PI.

Kind: static constant of utils

utils.HALF_PI : number

Two times PI.

Kind: static constant of utils

utils.SQRT2 : number

Square root of 2.

Kind: static constant of utils

utils.getCellsTypedArray ⇒ Uint8Array | Uint16Array | Uint32Array

Select cells typed array from a size determined by amount of vertices.

Kind: static constant of utils See: MDN TypedArray objects

ParamTypeDescription
sizenumberThe max value expected

utils.normalize(v) ⇒ Array.<number>

Normalize a vector 3.

Kind: static method of utils Returns: Array.<number> - Normalized vector

ParamTypeDescription
vArray.<number>Vector 3 array

utils.checkArguments(...args)

Ensure first argument passed to the primitive functions is an object

Kind: static method of utils

ParamType
...args*

utils.setTypedArrayType(type)

Enforce a typed array constructor for cells

Kind: static method of utils

ParamType
typeClass.<Uint8Array> | Class.<Uint16Array> | Class.<Uint32Array>

BasicSimplicialComplex : object

Geometry definition without normals and UVs.

Kind: global typedef Properties

NameType
positionsFloat32Array
cellsUint8Array | Uint16Array | Uint32Array

SimplicialComplex : object

Geometry definition.

Kind: global typedef Properties

NameType
positionsFloat32Array
normalsFloat32Array
uvsFloat32Array
cellsUint8Array | Uint16Array | Uint32Array

License

See original packages used in v1:

Differences with v1:

  • use 3D positions for circle
  • base disc on ellispse and add inner segments
  • fix cylinder orientation and uvs
  • fix icosphere uvs (based on: https://github.com/mourner/icomesh)
  • fix quad normal to +z
  • fix subdivision for rounded geometries (rounded-cube and capsule)
  • 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
  • defaults produce geometries contained in a unit bbox
  • add jsdoc, prettier, eslint via snowdev

MIT. See license file.