Toast Engine
Loading...
Searching...
No Matches
toast::VoxelPiece Class Referenceabstract

#include <voxel_piece.hpp>

Inheritance diagram for toast::VoxelPiece:
Collaboration diagram for toast::VoxelPiece:

Classes

struct  Grid

Public Types

using GridRef = std::shared_ptr<const Grid>

Public Member Functions

auto getSize () -> glm::vec3
auto getVoxel (glm::vec3 pos) -> int
void setVoxel (glm::vec3 pos, int id)
void removeVoxel (glm::vec3 pos)
void fillBox (glm::vec3 min, glm::vec3 max, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void fillRoundBox (glm::vec3 min, glm::vec3 max, float radius, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void fillSphere (glm::vec3 center, float radius, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void fillEllipsoid (glm::vec3 min, glm::vec3 max, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void fillCylinder (glm::vec3 a, glm::vec3 b, float radius, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void fillCapsule (glm::vec3 a, glm::vec3 b, float radius, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void carveBox (glm::vec3 min, glm::vec3 max)
void carveSphere (glm::vec3 center, float radius)
void paintBox (glm::vec3 min, glm::vec3 max, int id)
void stamp (const assets::Handle< assets::VoxelModel > &asset, glm::vec3 pos, glm::quat rotation=glm::quat(1.0f, 0.0f, 0.0f, 0.0f), voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void tile (const assets::Handle< assets::VoxelModel > &asset, int fit=0, glm::vec3 anchor=glm::vec3(0.0f), glm::vec3 offset=glm::vec3(0.0f), glm::vec3 mirror=glm::vec3(0.0f), voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
void redraw ()
auto getShapeScript () const -> const assets::Handle< assets::Script > &
void setShapeScript (assets::Handle< assets::Script > script)
auto getClipPlanes () const -> std::vector< glm::vec4 >
void setClipPlanes (std::vector< glm::vec4 > planes)
void addClipPlane (glm::vec4 plane)
void clearClipPlanes ()
auto getShapeBounds () -> std::vector< glm::vec3 >
auto getShape () -> Box< Node >
auto isMisaligned () const noexcept -> bool
virtual auto kind () const noexcept -> PieceKind
virtual auto writeMode () const noexcept -> voxel::WriteMode
virtual auto matchId () const noexcept -> uint8_t
virtual auto sourcePaletteUid () const -> uint64_t
auto grid () -> const voxel::Volume *
auto gridRef () -> GridRef
void requestGrid ()
auto collectGrid () -> bool
auto readyGrid () -> GridRef
auto gridBuilding () const noexcept -> bool
auto gridStale () const noexcept -> bool
void waitForGrid ()
auto inputRevision () const noexcept -> uint32_t
auto buildCount () const noexcept -> uint32_t
auto clipPlanes () const noexcept -> const std::vector< glm::vec4 > &
auto nominalSize () -> glm::ivec3
virtual auto resizable () const noexcept -> bool
void releaseGrid ()
void setMisaligned (bool misaligned)
Public Member Functions inherited from toast::Node3D
auto raycast (glm::vec3 dir, glm::vec3 offset=glm::vec3(0.0f), float max_distance=-1.0f, bool ignore_self=true, const std::vector< int > &layers=std::vector< int > {}) const -> std::optional< physics::RayHit >
 The nearest thing a ray from this node hits.
auto raymulticast (glm::vec3 dir, glm::vec3 offset=glm::vec3(0.0f), float max_distance=-1.0f, int max_hits=-1, bool ignore_self=true, const std::vector< int > &layers=std::vector< int > {}) const -> std::vector< physics::RayHit >
 Up to max_hits things collisions.
auto linecast (glm::vec3 from, glm::vec3 to, int max_hits=-1, bool ignore_self=true, const std::vector< int > &layers=std::vector< int > {}) const -> std::vector< physics::RayHit >
 Everything on the line between two points, nearest to from first.
void lookAt (glm::vec3 target, glm::vec3 up={0.0f, 0.0f, 1.0f})
 Orients the node so its forward axis points at a world-space target.
void lookAtZ (glm::vec3 target)
 lookAt() constrained to the XY plane; the node faces the target with no roll
auto up () const -> glm::vec3
auto forward () const -> glm::vec3
void syncTransform () const
void syncWorldTransform () const
void refreshTransformParent ()
auto getTransform () const noexcept -> const glm::mat4 &
auto getWorldTransform () const noexcept -> const glm::mat4 &
virtual void onEditorTransformChanged ()
Public Member Functions inherited from toast::Node
auto uid () const noexcept -> const UID &
 Stable unique identifier, assigned at construction or deserialization. Never changes.
auto rid () const noexcept -> std::uintptr_t
 Runtime identifier for this node, guaranteed to be unique but changes every time.
auto name () const noexcept -> std::string_view
 Display name. Siblings may share one - this is not an identifier.
void name (std::string_view name) noexcept
 Sets the display name.
auto enabled () const noexcept -> bool
 Whether this node is currently active.
auto participatesIn (NodeOwnerParticipation use) const noexcept -> bool
auto owner () const noexcept -> INodeOwner *
 The World or Workspace this node belongs to.
void enabled (bool value) noexcept
 Sets the local enabled flag and propagates the change to all children.
auto box () const noexcept -> Box< Node >
 Returns an owning handle to this node.
auto info () const -> const NodeInfo *
 Reflection metadata registered by the code generator.
auto sourcePrefab () const noexcept -> const assets::Handle< assets::Prefab > &
 The prefab this node was instantiated from.
auto isInstanceRoot () const noexcept -> bool
 Whether this node is the root of a prefab instance.
auto root () const noexcept -> Box< Node >
 The topmost ancestor that is not the world root.
auto state () const noexcept -> NodeState
auto type () const noexcept -> NodeType
auto listener () noexcept -> event::Listener &
 The event Listener owned by this node. Lazy-allocated, freed only with the node.
auto parent () noexcept -> Box< Node >
 The immediate parent of this node.
auto children () const noexcept -> const std::vector< Box< Node > > &
 The ordered list of direct children.
auto childrenSnapshot () const -> std::vector< Box< Node > >
auto create (std::string_view type="toast::Node") noexcept -> Box< Node >
 Creates a new child node of the given type and attaches it to this node.
auto spawn (UID uid) noexcept -> Box< Node >
 Asynchronously instantiates a prefab as a child of this node.
auto spawn (std::string_view uri) noexcept -> Box< Node >
 Asynchronously instantiates a prefab as a child of this node.
auto find (std::string_view query) -> Box< Node >
 Depth-first search for a single descendant.
auto find (const UID &uid) -> Box< Node >
 Looks up a node by UID within this node's local root.
auto search (std::string_view query) -> std::vector< Box< Node > >
 Depth-first search for all matching descendants.
auto childrenOfType (const NodeInfo *type) const -> std::vector< Box< Node > >
auto searchType (const NodeInfo *type) const -> std::vector< Box< Node > >
void addDependsOn (Node &other)
void removeDependsOn (Node &other)
void interactsWith (Node &other)
 Declares that the scripts of this node and of other call each other.
auto scriptRuntime () noexcept -> scripting::ScriptRuntime *
auto hasTickFunction (TickFunctionList mask) const noexcept -> bool
auto scriptVm (TickFunctionList func) const noexcept -> std::optional< size_t >
 The interpreter this node runs Lua on while it executes func, empty when it runs none.
auto scriptVmAny () const noexcept -> std::optional< size_t >
 The interpreter the scripts of this node live on whatever phase they define.
auto hasCallable (std::string_view name) const noexcept -> bool
void reloadScripts () noexcept
 Rebuilds the script runtime after a script asset changed (hot reload or attach).
auto scripts () const noexcept -> const std::vector< assets::Handle< assets::Script > > &
void setScripts (std::vector< assets::Handle< assets::Script > > scripts) noexcept
void addScript (const assets::Handle< assets::Script > &script) noexcept
template<typename R = void, typename... Args>
auto call (std::string_view name, Args &&... args) -> R
 Invokes all C++ reflected implementations of name (base→derived) and all same-named Lua functions across every attached script, forwarding args.
template<typename T>
auto get (std::string_view name) const -> T
 Reads a named value: reflected C++ field first, then the first matching Lua instance variable.
template<typename T>
void set (std::string_view name, const T &value)
 Writes a named value: reflected C++ field if present and every Lua instance variable of that name that already exists.
auto isRecursivePrefab () const noexcept -> bool

Public Attributes

signals::Signal drawn
 Sent after editShape drew the grid again.
Public Attributes inherited from toast::Node3D
glm::vec3 position = glm::vec3(0.0f)
glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f)
glm::vec3 scale = glm::vec3(1.0f)
glm::vec3 world_position = glm::vec3(0.0f)
glm::quat world_rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f)
glm::vec3 world_scale = glm::vec3(1.0f)
Public Attributes inherited from toast::Node
signals::Signal< Box< Node > > on_enable
signals::Signal< Box< Node > > on_disable
signals::Signal< Box< Node > > on_begin
signals::Signal< Box< Node > > on_end

Protected Member Functions

virtual auto pieceSize () -> glm::ivec3
virtual auto prepareGrid () -> std::unique_ptr< voxel::Volume >=0
virtual void finishGrid (voxel::Volume &)
void markGridDirty () noexcept
void markLandingDirty () noexcept
void onReflectedFieldChanged (std::string_view field_name) override
void onScriptsReloading () override
void onScriptsReloaded () override
void onScriptVarChanged (std::string_view path) override
void updateInspectorMessages () override
template<typename Kernel>
void draw (std::string_view operation, Kernel &&kernel)
auto drawing () const noexcept -> voxel::Volume *
void init ()
void destroy ()
Protected Member Functions inherited from toast::Node3D
void init ()
 X axis.
Protected Member Functions inherited from toast::Node
virtual auto luaAffinity () const noexcept -> std::optional< size_t >
void addInspectorMessage (const NodeMessage &message)
void removeInspectorMessage (const NodeMessage &message)

Protected Attributes

assets::Handle< assets::Script > m_shape_script
std::vector< glm::vec4 > m_clip_planes
Protected Attributes inherited from toast::Node
INodeOwner * m_owner = nullptr

Additional Inherited Members

Static Public Attributes inherited from toast::Node3D
static const glm::vec3 world_up = {0.0f, 0.0f, 1.0f}
static const glm::vec3 world_forward = {0.0f, 1.0f, 0.0f}
 Z axis.
static const glm::vec3 world_right = {1.0f, 0.0f, 0.0f}
 Y axis.

Detailed Description

Base of everything a ProceduralVoxel builds itself from

Every piece draws into its own grid with its first script's editShape The grid is cached so moving, rotating or reordering pieces never runs a script again

Positions are in voxels from the piece corner and each voxel is 10 cm The drawing functions only work while editShape runs

Member Function Documentation

◆ addClipPlane()

void toast::VoxelPiece::addClipPlane ( glm::vec4 plane)

Keeps only the voxels on the side the normal in xyz points to

◆ buildCount()

auto toast::VoxelPiece::buildCount ( ) const -> uint32_t
inlinenodiscardnoexcept

How many times the script grid was built, tests use it to know a move did not redraw

◆ collectGrid()

auto toast::VoxelPiece::collectGrid ( ) -> bool

Takes the grid a worker finished

◆ drawing()

auto toast::VoxelPiece::drawing ( ) const -> voxel::Volume *
nodiscardprotectednoexcept

The grid being drawn when the calling thread is the one drawing it

◆ fillEllipsoid()

void toast::VoxelPiece::fillEllipsoid ( glm::vec3 min,
glm::vec3 max,
int id,
voxel::WriteMode mode = voxel::WriteMode::replace,
int match_id = 0 )

The ellipsoid that touches every face of the box from min to max

◆ fillRoundBox()

void toast::VoxelPiece::fillRoundBox ( glm::vec3 min,
glm::vec3 max,
float radius,
int id,
voxel::WriteMode mode = voxel::WriteMode::replace,
int match_id = 0 )

A box with its edges rounded by radius voxels

◆ finishGrid()

virtual void toast::VoxelPiece::finishGrid ( voxel::Volume & )
inlineprotectedvirtual

Runs after editShape

Reimplemented in toast::VoxelVolume.

◆ getClipPlanes()

auto toast::VoxelPiece::getClipPlanes ( ) const -> std::vector< glm::vec4 >
nodiscard

Clip planes in piece voxels, a voxel is kept when dot(xyz, centre) + w >= 0

◆ getShape()

auto toast::VoxelPiece::getShape ( ) -> Box< Node >
nodiscard
Returns
the ProceduralVoxel the piece is built into, through any groups

◆ getShapeBounds()

auto toast::VoxelPiece::getShapeBounds ( ) -> std::vector< glm::vec3 >
nodiscard

The box the piece covers on its ProceduralVoxel, in shape voxels

Returns
min then max or an empty list when it is not in a ProceduralVoxel

◆ getShapeScript()

auto toast::VoxelPiece::getShapeScript ( ) const -> const assets::Handle< assets::Script > &
nodiscard

The script that draws the piece, the first of its scripts

◆ getSize()

auto toast::VoxelPiece::getSize ( ) -> glm::vec3
nodiscard
Returns
the size of the grid in voxels
Here is the caller graph for this function:

◆ getVoxel()

auto toast::VoxelPiece::getVoxel ( glm::vec3 pos) -> int
nodiscard
Returns
the palette ID at pos in the grid being drawn

◆ grid()

auto toast::VoxelPiece::grid ( ) -> const voxel::Volume *
nodiscard

The grid after the clip planes, rebuilt first when an input changed

Returns
null when there is nothing to draw
Note
Waits for a grid still drawing on another thread
Here is the caller graph for this function:

◆ gridBuilding()

auto toast::VoxelPiece::gridBuilding ( ) const -> bool
inlinenodiscardnoexcept
Returns
true while a worker is drawing the grid

◆ gridRef()

auto toast::VoxelPiece::gridRef ( ) -> GridRef
nodiscard

Like grid() but it keeps the grid alive while someone else reads it

Here is the caller graph for this function:

◆ gridStale()

auto toast::VoxelPiece::gridStale ( ) const -> bool
inlinenodiscardnoexcept
Returns
true when the grid is out of date, drawing or not

◆ inputRevision()

auto toast::VoxelPiece::inputRevision ( ) const -> uint32_t
inlinenodiscardnoexcept

Bumped whenever something that changes the grid or how it lands changes

◆ isMisaligned()

auto toast::VoxelPiece::isMisaligned ( ) const -> bool
inlinenodiscardnoexcept
Returns
true when the transform had to be snapped to the closest voxel and right angle

◆ markGridDirty()

void toast::VoxelPiece::markGridDirty ( )
protectednoexcept

The grid needs its script to run again

Here is the caller graph for this function:

◆ markLandingDirty()

void toast::VoxelPiece::markLandingDirty ( )
inlineprotectednoexcept

Only how the grid lands changed

Here is the caller graph for this function:

◆ nominalSize()

auto toast::VoxelPiece::nominalSize ( ) -> glm::ivec3
inlinenodiscard

The size in voxels the piece covers before any rotation

◆ onReflectedFieldChanged()

void toast::VoxelPiece::onReflectedFieldChanged ( std::string_view field_name)
overrideprotectedvirtual

Reimplemented from toast::Node.

◆ onScriptsReloaded()

void toast::VoxelPiece::onScriptsReloaded ( )
overrideprotectedvirtual

Reimplemented from toast::Node.

◆ onScriptsReloading()

void toast::VoxelPiece::onScriptsReloading ( )
overrideprotectedvirtual

Runs right before the script runtime is torn down and built again

Reimplemented from toast::Node.

◆ onScriptVarChanged()

void toast::VoxelPiece::onScriptVarChanged ( std::string_view path)
overrideprotectedvirtual

A script variable was edited from outside the script, path is "<instance>:<group/name>"

Reimplemented from toast::Node.

◆ pieceSize()

auto toast::VoxelPiece::pieceSize ( ) -> glm::ivec3
nodiscardprotectedvirtual

The size scripts draw into, the grid itself is rounded up to whole bricks

Reimplemented in toast::VoxelMesh, and toast::VoxelVolume.

Here is the caller graph for this function:

◆ prepareGrid()

virtual auto toast::VoxelPiece::prepareGrid ( ) -> std::unique_ptr< voxel::Volume >
nodiscardprotectedpure virtual

A fresh grid for editShape to draw into

Implemented in toast::VoxelMesh, and toast::VoxelVolume.

◆ readyGrid()

auto toast::VoxelPiece::readyGrid ( ) -> GridRef
nodiscard

The newest grid there is without drawing or waiting, it can be out of date or null

◆ redraw()

void toast::VoxelPiece::redraw ( )

Runs editShape again on the next rebuild

Note
Only needed when the script reads something the piece cannot see change

◆ releaseGrid()

void toast::VoxelPiece::releaseGrid ( )

Frees the grids, the next grid() call rebuilds them

◆ requestGrid()

void toast::VoxelPiece::requestGrid ( )

Starts drawing the grid on a worker

◆ resizable()

virtual auto toast::VoxelPiece::resizable ( ) const -> bool
inlinenodiscardvirtualnoexcept

Volumes resize, meshes keep the size of their model

Reimplemented in toast::VoxelVolume.

◆ setMisaligned()

void toast::VoxelPiece::setMisaligned ( bool misaligned)

Set by the ProceduralVoxel when the transform had to be snapped to the voxel lattice

◆ setShapeScript()

void toast::VoxelPiece::setShapeScript ( assets::Handle< assets::Script > script)

Swaps the script that draws the piece and redraws it

◆ sourcePaletteUid()

virtual auto toast::VoxelPiece::sourcePaletteUid ( ) const -> uint64_t
inlinenodiscardvirtual

The palette the grid ids come from, 0 when they already are ProceduralVoxel ids

Reimplemented in toast::VoxelMesh.

◆ stamp()

void toast::VoxelPiece::stamp ( const assets::Handle< assets::VoxelModel > & asset,
glm::vec3 pos,
glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f),
voxel::WriteMode mode = voxel::WriteMode::replace,
int match_id = 0 )

Drops a voxel model into the grid at pos

Note
Colors get matched to the ProceduralVoxel palette

◆ tile()

void toast::VoxelPiece::tile ( const assets::Handle< assets::VoxelModel > & asset,
int fit = 0,
glm::vec3 anchor = glm::vec3(0.0f),
glm::vec3 offset = glm::vec3(0.0f),
glm::vec3 mirror = glm::vec3(0.0f),
voxel::WriteMode mode = voxel::WriteMode::replace,
int match_id = 0 )

Repeats the solid part of a voxel model across the whole grid

Parameters
fit0 crop, 1 stretch, 2 center
anchorPer axis 0 min, 1 center, 2 max
offsetShifts the tiles by this many voxels
mirrorPer axis, non zero flips every other tile

◆ updateInspectorMessages()

void toast::VoxelPiece::updateInspectorMessages ( )
overrideprotectedvirtual

Reimplemented from toast::Node.

◆ waitForGrid()

void toast::VoxelPiece::waitForGrid ( )

Blocks until the worker drawing the grid is done and takes its grid

Here is the caller graph for this function:

Member Data Documentation

◆ m_clip_planes

std::vector<glm::vec4> toast::VoxelPiece::m_clip_planes
protected

Clip planes in piece voxels, a voxel is kept when dot(xyz, centre) + w >= 0

◆ m_shape_script

assets::Handle<assets::Script> toast::VoxelPiece::m_shape_script
protected

The script that draws the piece, it mirrors the first entry of the node scripts


The documentation for this class was generated from the following files:
  • engine/src/toast/voxel/nodes/voxel_piece.hpp
  • engine/src/toast/voxel/nodes/voxel_piece.cpp