vshape

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2022 License: BSD-3-Clause Imports: 3 Imported by: 2

README

vshape: Compositional 3D shape library

vshape provides a library of 3D shapes, built from indexed triangle meshes, which can be added together in ShapeGroup lists. Each Shape can report the number of points and indexes based on configured parameters, and keeps track of its offset within an overall mat32.ArrayF32 allocated based on total numbers. In this way, separate Allocate then Configure phases are supported, as required by the vgpu Memory allocation system.

It only has a dependency on the mat32 package and could be used for anything.

Basic building blocks (e.g., Plane, SphereSector) have standalone methods, in addition to Shape elements.

Here are the shapes:

  • Plane
  • Box
  • Sphere (including various partial segments thereof)
  • Cylinder, Cone (including different size top and bottom radii, e.g., Cones)
  • Capsule: a cylinder with half-sphere caps -- good for simple body segments

Documentation

Overview

vshape provides a library of 3D shapes, built from indexed triangle meshes, which can be added together in `ShapeGroup` lists. Each `Shape` can report the number of points and indexes based on configured parameters, and keeps track of its offset within an overall `mat32.ArrayF32` allocated based on total numbers. In this way, separate Allocate then Configure phases are supported, as required by the vgpu Memory allocation system.

Basic building blocks (e.g., Plane, SphereSector) have standalone methods, in addition to Shape elements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoxN

func BoxN(segs mat32.Vec3i) (nVtx, nIdx int)

PlaneN returns the N's for a single plane's worth of vertex and index data with given number of segments. Note: In *vertex* units, not float units (i.e., x3 to get actual float offset in Vtx array).

func CylinderSectorN

func CylinderSectorN(radialSegs, heightSegs int, top, bottom bool) (nVtx, nIdx int)

CylinderSectorN returns the N's for the cylinder (truncated cone) sector vertex and index data with given parameters

func DiskSectorN

func DiskSectorN(segs int) (nVtx, nIdx int)

DiskSectorN returns the N's for a disk sector's vertex and index data with given number of segments. Note: In *vertex* units, not float units (i.e., x3 to get actual float offset in Vtx array).

func PlaneN

func PlaneN(wsegs, hsegs int) (nVtx, nIdx int)

PlaneN returns the N's for a single plane's worth of vertex and index data with given number of segments. Note: In *vertex* units, not float units (i.e., x3 to get actual float offset in Vtx array). nVtx = (wsegs + 1) * (hsegs + 1) nIdx = wsegs * hsegs * 6

func SetBox

func SetBox(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, size mat32.Vec3, segs mat32.Vec3i, pos mat32.Vec3) mat32.Vec3

SetBox sets box vertex, norm, tex, index data at given starting *vertex* index (i.e., multiply this *3 to get actual float offset in Vtx array), and starting Idx index. for given 3D size, and given number of segments per side. finely subdividing a plane allows for higher-quality lighting and texture rendering (minimum of 1 will be enforced). pos is a 3D position offset. returns 3D size of plane.

func SetCone

func SetCone(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, height, radius float32, radialSegs, heightSegs int, bottom bool, pos mat32.Vec3) mat32.Box3

SetCone creates a cone mesh with the specified base radius, height, vertex, norm, tex, index data at given starting *vertex* index (i.e., multiply this *3 to get actual float offset in Vtx array), number of radial segments, number of height segments, and presence of a bottom cap. Height is along the Y axis. pos is an arbitrary offset (for composing shapes).

func SetCylinderSector

func SetCylinderSector(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, height, topRad, botRad float32, radialSegs, heightSegs int, angStart, angLen float32, top, bottom bool, pos mat32.Vec3) mat32.Box3

SetCylinderSector creates a generalized cylinder (truncated cone) sector vertex, norm, tex, index data at given starting *vertex* index (i.e., multiply this *3 to get actual float offset in Vtx array), with the specified top and bottom radii, height, number of radial segments, number of height segments, sector start angle in degrees (start = -1,0,0) sector size angle in degrees, and presence of a top and/or bottom cap. Height is along the Y axis. pos is an arbitrary offset (for composing shapes).

func SetDiskSector

func SetDiskSector(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, radius float32, segs int, angStart, angLen float32, pos mat32.Vec3) mat32.Box3

SetDiskSector sets a disk sector vertex, norm, tex, index data at given starting *vertex* index (i.e., multiply this *3 to get actual float offset in Vtx array), and starting Idx index, with the specified radius, number of radial segments (minimum 3), sector start angle and angle length in degrees. The center of the disk is at the origin, and angle runs counter-clockwise on the XY plane, starting at (x,y,z)=(1,0,0). pos is an arbitrary offset (for composing shapes), returns bounding box.

func SetPlane

func SetPlane(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, waxis, haxis mat32.Dims, wdir, hdir int, width, height, woff, hoff, zoff float32, wsegs, hsegs int, pos mat32.Vec3)

SetPlane sets plane vertex, norm, tex, index data at given starting *vertex* index (i.e., multiply this *3 to get actual float offset in Vtx array), and starting Idx index. waxis, haxis = width, height axis, wdir, hdir are the directions for width and height dimensions. wsegs, hsegs = number of segments to create in each dimension -- more finely subdividing a plane allows for higher-quality lighting and texture rendering (minimum of 1 will be enforced). offset is the distance to place the plane along the orthogonal axis. pos is a 3D position offset.

func SetPlaneAxisSize

func SetPlaneAxisSize(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, normAxis mat32.Dims, normNeg bool, size mat32.Vec2, segs mat32.Vec2i, offset float32, pos mat32.Vec3) mat32.Vec3

SetPlaneAxisSize sets plane vertex, norm, tex, index data at given starting *vertex* index (i.e., multiply this *3 to get actual float offset in Vtx array), and starting Idx index. using Norm Axis, offset, and size params. wsegs, hsegs = number of segments to create in each dimension -- more finely subdividing a plane allows for higher-quality lighting and texture rendering (minimum of 1 will be enforced). offset is the distance to place the plane along the orthogonal axis. pos is a 3D position offset. returns 3D size of plane.

func SetSphereSector

func SetSphereSector(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32, vtxOff, idxOff int, radius float32, widthSegs, heightSegs int, angStart, angLen, elevStart, elevLen float32, pos mat32.Vec3) mat32.Box3

SetSphereSector sets a sphere sector vertex, norm, tex, index data at given starting *vertex* index (i.e., multiply this *3 to get actual float offset in Vtx array), and starting Idx index, with the specified radius, number of radial segments in each dimension (min 3), radial sector start angle and length in degrees (0 - 360), start = -1,0,0, elevation start angle and length in degrees (0 - 180), top = 0, bot = 180. pos is an arbitrary offset (for composing shapes), returns bounding box.

func SphereSectorN

func SphereSectorN(widthSegs, heightSegs int, elevStart, elevLen float32) (nVtx, nIdx int)

SphereSectorN returns the N's for a sphere sector's vertex and index data with given number of segments. Note: In *vertex* units, not float units (i.e., x3 to get actual float offset in Vtx array).

Types

type Box

type Box struct {
	ShapeBase
	Size mat32.Vec3  `desc:"size along each dimension"`
	Segs mat32.Vec3i `` /* 137-byte string literal not displayed */
}

Box is a rectangular-shaped solid (cuboid)

func NewBox

func NewBox(width, height, depth float32) *Box

NewBox returns a Box shape with given size

func (*Box) Defaults

func (bx *Box) Defaults()

func (*Box) N

func (bx *Box) N() (nVtx, nIdx int)

func (*Box) Set

func (bx *Box) Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32)

SetBox sets points in given allocated arrays

type Capsule

type Capsule struct {
	ShapeBase
	Height     float32 `desc:"height of the cylinder portion"`
	TopRad     float32 `desc:"radius of the top -- set to 0 to omit top cap"`
	BotRad     float32 `desc:"radius of the bottom -- set to 0 to omit bottom cap"`
	RadialSegs int     `min:"1" desc:"number of radial segments (32 is a reasonable default for full circle)"`
	HeightSegs int     `desc:"number of height segments"`
	CapSegs    int     `desc:"number of segments in the hemisphere cap ends (16 is a reasonable default)"`
	AngStart   float32 `min:"0" max:"360" step:"5" desc:"starting angle in degrees, relative to -1,0,0 left side starting point"`
	AngLen     float32 `min:"0" max:"360" step:"5" desc:"total angle to generate in degrees (max 360)"`
}

Capsule is a generalized capsule shape: a cylinder with hemisphere end caps. Supports different radii on each end. Height is along the Y axis -- total height is Height + TopRad + BotRad.

func NewCapsule

func NewCapsule(height, radius float32, segs, heightSegs int) *Capsule

NewCapsule returns a Capsule shape with given radius, height, number of radial segments, number of height segments, and presence of a top and/or bottom cap. Height is along the Y axis.

func (*Capsule) Defaults

func (cp *Capsule) Defaults()

func (*Capsule) N

func (cp *Capsule) N() (nVtx, nIdx int)

func (*Capsule) Set

func (cp *Capsule) Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32)

SetCapsuleSector sets points in given allocated arrays

type Cylinder

type Cylinder struct {
	ShapeBase
	Height     float32 `desc:"height of the cylinder"`
	TopRad     float32 `desc:"radius of the top -- set to 0 for a cone"`
	BotRad     float32 `desc:"radius of the bottom"`
	RadialSegs int     `min:"1" desc:"number of radial segments (32 is a reasonable default for full circle)"`
	HeightSegs int     `desc:"number of height segments"`
	Top        bool    `desc:"render the top disc"`
	Bottom     bool    `desc:"render the bottom disc"`
	AngStart   float32 `min:"0" max:"360" step:"5" desc:"starting angle in degrees, relative to -1,0,0 left side starting point"`
	AngLen     float32 `min:"0" max:"360" step:"5" desc:"total angle to generate in degrees (max 360)"`
}

Cylinder is a generalized cylinder shape, including a cone or truncated cone by having different size circles at either end. Height is up along the Y axis.

func NewCone

func NewCone(height, radius float32, radialSegs, heightSegs int, bottom bool) *Cylinder

NewCone returns a cone shape with the specified base radius, height, number of radial segments, number of height segments, and presence of a bottom cap. Height is along the Y axis.

func NewCylinder

func NewCylinder(height, radius float32, radialSegs, heightSegs int, top, bottom bool) *Cylinder

NewCylinder returns a Cylinder shape with given radius, height, number of radial segments, number of height segments, and presence of a top and/or bottom cap. Height is along the Y axis.

func (*Cylinder) Defaults

func (cy *Cylinder) Defaults()

func (*Cylinder) N

func (cy *Cylinder) N() (nVtx, nIdx int)

func (*Cylinder) Set

func (cy *Cylinder) Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32)

SetCylinderSector sets points in given allocated arrays

type Plane

type Plane struct {
	ShapeBase
	NormAxis mat32.Dims  `` /* 235-byte string literal not displayed */
	NormNeg  bool        `` /* 135-byte string literal not displayed */
	Size     mat32.Vec2  `desc:"2D size of plane"`
	Segs     mat32.Vec2i `` /* 132-byte string literal not displayed */
	Offset   float32     `desc:"offset from origin along direction of normal to the plane"`
}

Plane is a flat 2D plane, which can be oriented along any axis facing either positive or negative

func NewPlane

func NewPlane(axis mat32.Dims, width, height float32) *Plane

NewPlane returns a Plane shape with given size

func (*Plane) Defaults

func (pl *Plane) Defaults()

func (*Plane) N

func (pl *Plane) N() (nVtx, nIdx int)

N returns number of vertex, index points in this shape element

func (*Plane) Set

func (pl *Plane) Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32)

Set sets points in given allocated arrays

type Shape

type Shape interface {
	// N returns number of vertex, index points in this shape element
	N() (nVtx, nIdx int)

	// Offs returns starting offset for verticies, indexes in full shape array,
	// in terms of points, not floats
	Offs() (vtxOff, idxOff int)

	// SetOffs sets starting offset for verticies, indexes in full shape array,
	// in terms of points, not floats
	SetOffs(vtxOff, idxOff int)

	// Set sets points in given allocated arrays
	Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32)

	// BBox returns the bounding box for the shape, typically centered around 0
	// This is only valid after Set has been called.
	BBox() mat32.Box3
}

Shape is an interface for all shape-constructing elements

type ShapeBase

type ShapeBase struct {
	VtxOff int        `desc:"vertex offset, in points"`
	IdxOff int        `desc:"index offset, in points"`
	CBBox  mat32.Box3 `desc:"cubic bounding box in local coords"`
	Pos    mat32.Vec3 `desc:"all shapes take a 3D position offset to enable composition"`
}

ShapeBase is the base shape element

func (*ShapeBase) BBox

func (sb *ShapeBase) BBox() mat32.Box3

BBox returns the bounding box for the shape, typically centered around 0 This is only valid after Set has been called.

func (*ShapeBase) Offs

func (sb *ShapeBase) Offs() (vtxOff, idxOff int)

Offs returns starting offset for verticies, indexes in full shape array, in terms of points, not floats

func (*ShapeBase) SetOffs

func (sb *ShapeBase) SetOffs(vtxOff, idxOff int)

SetOffs sets starting offsets for verticies, indexes in full shape array

type ShapeGroup

type ShapeGroup struct {
	ShapeBase
	Shapes []Shape `desc:"list of shapes in group"`
}

ShapeGroup is a group of shapes -- returns summary data for shape elements

func (*ShapeGroup) N

func (sb *ShapeGroup) N() (nVtx, nIdx int)

N returns number of vertex, index points in this shape element.

func (*ShapeGroup) Set

func (sb *ShapeGroup) Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32)

Set sets points in given allocated arrays, also updates offsets

type Sphere

type Sphere struct {
	ShapeBase
	Radius     float32 `desc:"radius of the sphere"`
	WidthSegs  int     `min:"3" desc:"number of segments around the width of the sphere (32 is reasonable default for full circle)"`
	HeightSegs int     `min:"3" desc:"number of height segments (32 is reasonable default for full height)"`
	AngStart   float32 `min:"0" max:"360" step:"5" desc:"starting radial angle in degrees, relative to -1,0,0 left side starting point"`
	AngLen     float32 `min:"0" max:"360" step:"5" desc:"total radial angle to generate in degrees (max = 360)"`
	ElevStart  float32 `min:"0" max:"180" step:"5" desc:"starting elevation (height) angle in degrees - 0 = top of sphere, and Pi is bottom"`
	ElevLen    float32 `min:"0" max:"180" step:"5" desc:"total angle to generate in degrees (max = 180)"`
}

Sphere is a sphere shape (can be a partial sphere too)

func NewSphere

func NewSphere(radius float32, segs int) *Sphere

NewSphere returns a Sphere shape with given size

func (*Sphere) Defaults

func (sp *Sphere) Defaults()

func (*Sphere) N

func (sp *Sphere) N() (nVtx, nIdx int)

func (*Sphere) Set

func (sp *Sphere) Set(vtxAry, normAry, texAry mat32.ArrayF32, idxAry mat32.ArrayU32)

SetSphereSector sets points in given allocated arrays

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL