README
primitive-geometry
Geometries for 3D rendering, including normals, UVs and cell indices (faces). Perfect if you want to supercharge your dependency folder... with 30KB of geometries.
Installation
npm install primitive-geometry
Features
- Common API: options object in, simplicial complex out
- Outputs TypedArray (
Float32Array
for geometry data andUint8Array|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.
See difference with v1 here.
Usage
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], ... ]
// }
API
Modules
- index
Re-export all geometries and utils.
- box
- capsule
- circle
- cone
- cube
- cylinder
- ellipsoid
- icosphere
- plane
- quad
- rounded-cube
- sphere
- torus
- utils
Typedefs
- BasicSimplicialComplex :
Object
Geometry definition without normals and UVs.
- SimplicialComplex :
Object
Geometry definition.
index
Re-export all geometries and utils.
box
- box
- box([options]) ⇒
BasicSimplicialComplex
⏏- ~BoxOptions :
Object
- ~BoxOptions :
- box([options]) ⇒
BasicSimplicialComplex
⏏
box([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | BoxOptions |
{} |
Object
box~BoxOptions : Kind: inner typedef of box
Properties
Name | Type | Default |
---|---|---|
[sx] | number |
1 |
[sy] | number |
sx |
[sz] | number |
sx |
capsule
- capsule
- capsule([options]) ⇒
SimplicialComplex
⏏- ~CapsuleOptions :
Object
- ~CapsuleOptions :
- capsule([options]) ⇒
SimplicialComplex
⏏
capsule([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | CapsuleOptions |
{} |
Object
capsule~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
- circle([options]) ⇒
BasicSimplicialComplex
⏏- ~BoxOptions :
Object
- ~BoxOptions :
- circle([options]) ⇒
BasicSimplicialComplex
⏏
circle([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | BoxOptions |
{} |
Object
circle~BoxOptions : Kind: inner typedef of circle
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[segments] | number |
32 |
cone
- cone
- cone([options]) ⇒
SimplicialComplex
⏏- ~ConeOptions :
Object
- ~ConeOptions :
- cone([options]) ⇒
SimplicialComplex
⏏
cone([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | ConeOptions |
{} |
Object
cone~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
- cube([options]) ⇒
SimplicialComplex
⏏- ~CubeOptions :
Object
- ~CubeOptions :
- cube([options]) ⇒
SimplicialComplex
⏏
cube([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | CubeOptions |
{} |
Object
cube~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
- cylinder([options]) ⇒
SimplicialComplex
⏏- ~CylinderOptions :
Object
- ~CylinderOptions :
- cylinder([options]) ⇒
SimplicialComplex
⏏
cylinder([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | CylinderOptions |
{} |
Object
cylinder~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
- ellipsoid([options]) ⇒
SimplicialComplex
⏏- ~EllipsoidOptions :
Object
- ~EllipsoidOptions :
- ellipsoid([options]) ⇒
SimplicialComplex
⏏
ellipsoid([options]) ⇒ Default to an oblate spheroid.
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | EllipsoidOptions |
{} |
Object
ellipsoid~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
- icosphere([options]) ⇒
SimplicialComplex
⏏- ~IcosphereOptions :
Object
- ~IcosphereOptions :
- icosphere([options]) ⇒
SimplicialComplex
⏏
icosphere([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | IcosphereOptions |
{} |
Object
icosphere~IcosphereOptions : Kind: inner typedef of icosphere
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[subdivisions] | number |
2 |
plane
- plane
- plane([options]) ⇒
SimplicialComplex
⏏- ~PlaneOptions :
Object
- ~PlaneOptions :
- plane([options]) ⇒
SimplicialComplex
⏏
plane([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | PlaneOptions |
{} |
Object
plane~PlaneOptions : Kind: inner typedef of plane
Properties
Name | Type | Default |
---|---|---|
[sx] | number |
1 |
[sy] | number |
sx |
[nx] | number |
1 |
[ny] | number |
nx |
quad
- quad
- quad([options]) ⇒
SimplicialComplex
⏏- ~QuadOptions :
Object
- ~QuadOptions :
- quad([options]) ⇒
SimplicialComplex
⏏
quad([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | QuadOptions |
{} |
Object
quad~QuadOptions : Kind: inner typedef of quad
Properties
Name | Type | Default |
---|---|---|
[scale] | number |
0.5 |
rounded-cube
SimplicialComplex
⏏
roundedCube([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | module:rounded~RoundedCubeOptions |
{} |
Object
roundedCube~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
- sphere([options]) ⇒
SimplicialComplex
⏏- ~SphereOptions :
Object
- ~SphereOptions :
- sphere([options]) ⇒
SimplicialComplex
⏏
sphere([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | SphereOptions |
{} |
Object
sphere~SphereOptions : Kind: inner typedef of sphere
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[nx] | number |
32 |
[ny] | number |
16 |
torus
- torus
- torus([options]) ⇒
SimplicialComplex
⏏- ~TorusOptions :
Object
- ~TorusOptions :
- torus([options]) ⇒
SimplicialComplex
⏏
torus([options]) ⇒ Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | TorusOptions |
{} |
Object
torus~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
- utils
- .TAU :
number
- .getCellsTypedArray ⇒
Uint8Array
|Uint16Array
|Uint32Array
- .normalize(v) ⇒
Array.<number>
- .checkArguments(...args)
- .setTypedArrayType(type)
- .TAU :
number
utils.TAU : Two times PI.
Kind: static constant of utils
Uint8Array
| Uint16Array
| Uint32Array
utils.getCellsTypedArray ⇒ 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 |
Array.<number>
utils.normalize(v) ⇒ Normalize a vector 3.
Kind: static method of utils
Returns: Array.<number>
- Normalized vector
Param | Type | Description |
---|---|---|
v | Array.<number> |
Vector 3 array |
utils.checkArguments(...args)
Ensure first argument passed to the primitive functions is an object
Kind: static method of utils
Param | Type |
---|---|
...args | * |
utils.setTypedArrayType(type)
Enforce a typed array constructor for cells
Kind: static method of utils
Param | Type |
---|---|
type | Class.<Uint8Array> | Class.<Uint16Array> | Class.<Uint32Array> |
Object
BasicSimplicialComplex : Geometry definition without normals and UVs.
Kind: global typedef
Properties
Name | Type |
---|---|
positions | Float32Array |
cells | Uint8Array | Uint16Array | Uint32Array |
Object
SimplicialComplex : Geometry definition.
Kind: global typedef
Properties
Name | Type |
---|---|
positions | Float32Array |
normals | Float32Array |
uvs | Float32Array |
cells | Uint8Array | Uint16Array | Uint32Array |
License
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.