primitive-geometry
primitive-geometry copied to clipboard
Geometries for 3D rendering, including normals, UVs and cell indices (faces). Perfect if you want to supercharge your dependency folder... with 30KB of geometries.
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({
scale: 0.5,
});
const planeGeometry = Primitives.plane({
sx: 1,
sy: 1,
nx: 1,
ny: 1,
});
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 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,
roundedSegments: 16,
theta: Math.PI * 2,
});
const sphereGeometry = Primitives.sphere({
radius: 0.5,
nx: 32,
ny: 16,
theta: Math.PI,
phi: Math.PI * 2,
});
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,
phi: Math.PI * 2,
});
const torusGeometry = Primitives.torus({
radius: 0.4,
segments: 64,
minorRadius: 0.1,
minorSegments: 32,
theta: Math.PI * 2,
phi: Math.PI * 2,
});
const tetrahedron = Primitives.tetrahedron({
radius: 0.5,
});
const icosahedron = Primitives.icosahedron({
radius: 0.5,
});
const disc = Primitives.disc({
radius: 0.5,
segments: 32,
theta: Math.PI * 2,
});
const annulus = Primitives.annulus({
radius: 0.5,
segments: 32,
theta: Math.PI * 2,
innerRadius: radius * 0.5,
innerSegments: 1,
});
// 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,
});
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.
- annulus
- box
- capsule
- circle
- cone
- cube
- cylinder
- disc
- ellipsoid
- icosahedron
- icosphere
- plane
- quad
- rounded-cube
- sphere
- tetrahedron
- torus
- utils
Typedefs
- BasicSimplicialComplex :
Object
Geometry definition without normals and UVs.
- SimplicialComplex :
Object
Geometry definition.
index
Re-export all geometries and utils.
annulus
-
annulus
-
annulus([options]) ⇒
BasicSimplicialComplex
⏏-
~AnnulusOptions :
Object
-
~AnnulusOptions :
-
annulus([options]) ⇒
annulus([options]) ⇒ BasicSimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | AnnulusOptions |
{} |
annulus~AnnulusOptions : Object
Kind: inner typedef of annulus
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[segments] | number |
32 |
[theta] | number |
TAU |
[innerRadius] | number |
radius * 0.5 |
[innerSegments] | number |
1 |
box
-
box
-
box([options]) ⇒
BasicSimplicialComplex
⏏-
~BoxOptions :
Object
-
~BoxOptions :
-
box([options]) ⇒
box([options]) ⇒ BasicSimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | BoxOptions |
{} |
box~BoxOptions : Object
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]) ⇒
capsule([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | CapsuleOptions |
{} |
capsule~CapsuleOptions : Object
Kind: inner typedef of capsule
Properties
Name | Type | Default |
---|---|---|
[height] | number |
0.5 |
[radius] | number |
0.25 |
[nx] | number |
16 |
[ny] | number |
1 |
[roundSegments] | number |
32 |
[phi] | number |
TAU |
circle
-
circle
-
circle([options]) ⇒
BasicSimplicialComplex
⏏-
~CircleOptions :
Object
-
~CircleOptions :
-
circle([options]) ⇒
circle([options]) ⇒ BasicSimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | CircleOptions |
{} |
circle~CircleOptions : Object
Kind: inner typedef of circle
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[segments] | number |
32 |
[theta] | number |
TAU |
[closed] | boolean |
false |
cone
-
cone
-
cone([options]) ⇒
SimplicialComplex
⏏-
~ConeOptions :
Object
-
~ConeOptions :
-
cone([options]) ⇒
cone([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | ConeOptions |
{} |
cone~ConeOptions : Object
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 |
[phi] | number |
TAU |
cube
-
cube
-
cube([options]) ⇒
SimplicialComplex
⏏-
~CubeOptions :
Object
-
~CubeOptions :
-
cube([options]) ⇒
cube([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | CubeOptions |
{} |
cube~CubeOptions : Object
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]) ⇒
cylinder([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | CylinderOptions |
{} |
cylinder~CylinderOptions : Object
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 |
[phi] | number |
TAU |
disc
-
disc
-
disc([options]) ⇒
BasicSimplicialComplex
⏏-
~DiscOptions :
Object
-
~DiscOptions :
-
disc([options]) ⇒
disc([options]) ⇒ BasicSimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | DiscOptions |
{} |
disc~DiscOptions : Object
Kind: inner typedef of disc
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[segments] | number |
32 |
[theta] | number |
TAU |
ellipsoid
-
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 |
{} |
ellipsoid~EllipsoidOptions : Object
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 |
[theta] | number |
Math.PI |
[phi] | number |
TAU |
icosahedron
-
icosahedron
-
icosahedron([options]) ⇒
SimplicialComplex
⏏-
~IcosahedronOptions :
Object
-
~IcosahedronOptions :
-
icosahedron([options]) ⇒
icosahedron([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | IcosahedronOptions |
{} |
icosahedron~IcosahedronOptions : Object
Kind: inner typedef of icosahedron
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
icosphere
-
icosphere
-
icosphere([options]) ⇒
SimplicialComplex
⏏-
~IcosphereOptions :
Object
-
~IcosphereOptions :
-
icosphere([options]) ⇒
icosphere([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | IcosphereOptions |
{} |
icosphere~IcosphereOptions : Object
Kind: inner typedef of icosphere
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[subdivisions] | number |
2 |
plane
-
plane
-
plane([options]) ⇒
SimplicialComplex
⏏-
~PlaneOptions :
Object
-
~PlaneDirection :
"x"
|"-x"
|"y"
|"-y"
|"z"
|"-z"
-
~PlaneOptions :
-
plane([options]) ⇒
plane([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | PlaneOptions |
{} |
plane~PlaneOptions : Object
Kind: inner typedef of plane
Properties
Name | Type | Default |
---|---|---|
[sx] | number |
1 |
[sy] | number |
sx |
[nx] | number |
1 |
[ny] | number |
nx |
[direction] | PlaneDirection |
"z" |
[quads] | boolean |
false |
plane~PlaneDirection : "x"
| "-x"
| "y"
| "-y"
| "z"
| "-z"
Kind: inner typedef of plane
quad
-
quad
-
quad([options]) ⇒
SimplicialComplex
⏏-
~QuadOptions :
Object
-
~QuadOptions :
-
quad([options]) ⇒
quad([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | QuadOptions |
{} |
quad~QuadOptions : Object
Kind: inner typedef of quad
Properties
Name | Type | Default |
---|---|---|
[scale] | number |
0.5 |
rounded-cube
-
rounded-cube
-
roundedCube([options]) ⇒
SimplicialComplex
⏏-
~RoundedCubeOptions :
Object
-
~RoundedCubeOptions :
-
roundedCube([options]) ⇒
roundedCube([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | module:rounded~RoundedCubeOptions |
{} |
roundedCube~RoundedCubeOptions : Object
Kind: inner typedef of roundedCube
Properties
Name | Type | Default |
---|---|---|
[sx] | number |
1 |
[sy] | number |
sx |
[sz] | number |
sx |
[nx] | number |
1 |
[ny] | number |
nx |
[nz] | number |
nx |
[radius] | number |
sx * 0.25 |
[roundSegments] | number |
8 |
[edgeSegments] | number |
1 |
sphere
-
sphere
-
sphere([options]) ⇒
SimplicialComplex
⏏-
~SphereOptions :
Object
-
~SphereOptions :
-
sphere([options]) ⇒
sphere([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | SphereOptions |
{} |
sphere~SphereOptions : Object
Kind: inner typedef of sphere
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
[nx] | number |
32 |
[ny] | number |
16 |
[theta] | number |
Math.PI |
[phi] | number |
TAU |
tetrahedron
-
tetrahedron
-
tetrahedron([options]) ⇒
SimplicialComplex
⏏-
~TetrahedronOptions :
Object
-
~TetrahedronOptions :
-
tetrahedron([options]) ⇒
tetrahedron([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | TetrahedronOptions |
{} |
tetrahedron~TetrahedronOptions : Object
Kind: inner typedef of tetrahedron
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.5 |
torus
-
torus
-
torus([options]) ⇒
SimplicialComplex
⏏-
~TorusOptions :
Object
-
~TorusOptions :
-
torus([options]) ⇒
torus([options]) ⇒ SimplicialComplex
⏏
Kind: Exported function
Param | Type | Default |
---|---|---|
[options] | TorusOptions |
{} |
torus~TorusOptions : Object
Kind: inner typedef of torus
Properties
Name | Type | Default |
---|---|---|
[radius] | number |
0.4 |
[segments] | number |
64 |
[minorRadius] | number |
0.1 |
[minorSegments] | number |
32 |
[theta] | number |
TAU |
[phi] | number |
TAU |
utils
-
utils
-
.TAU :
number
-
.getCellsTypedArray ⇒
Uint8Array
|Uint16Array
|Uint32Array
-
.normalize(v) ⇒
Array.<number>
- .checkArguments(...args)
- .setTypedArrayType(type)
-
.TAU :
utils.TAU : number
Two times PI.
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
Param | Type | Description |
---|---|---|
size | number |
The max value expected |
utils.normalize(v) ⇒ Array.<number>
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> |
BasicSimplicialComplex : Object
Geometry definition without normals and UVs.
Kind: global typedef Properties
Name | Type |
---|---|
positions | Float32Array |
cells | Uint8Array | Uint16Array | Uint32Array |
SimplicialComplex : Object
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:
- [x] fix cylinder orientation and uvs
- [x] fix icosphere uvs (based on: https://github.com/mourner/icomesh)
- [x] fix quad normal to +z
- [x] fix subdivision for rounded geometries (rounded-cube and capsule)
- [x] uniformise api and internal names
- [x] use options object
- [x] remove gl-matrix/pex-math and icosphere dependencies
- [x] use only trigonometric operation, no matrix transformation
- [x] base sphere on ellispsoid
- [x] add cone based on cylinder
- [x] use flat typed arrays
- [x] defaults produce geometries contained in a unit bbox
- [x] add jsdoc, prettier, eslint via snowdev
MIT. See license file.