Toast Engine
Loading...
Searching...
No Matches
voxel_piece.hpp
Go to the documentation of this file.
1
7
8#pragma once
9#include "voxel.hpp"
10
11#include <array>
12#include <cstdint>
13#include <future>
14#include <memory>
17#include <vector>
18
19namespace assets {
20class VoxelModel;
21}
22
23namespace toast {
24
26enum class PieceKind : uint8_t {
30};
31
41class [[ToastNode, Hidden, Interface, Icon("Shape"), Color("Magenta")]] TOAST_API VoxelPiece : public Voxel {
42public:
43 // Drawing
44
46 [[nodiscard, Reflect]]
47 auto getSize() -> glm::vec3;
48
50 [[nodiscard, Reflect]]
51 auto getVoxel(glm::vec3 pos) -> int;
52
53 [[Reflect]]
54 void setVoxel(glm::vec3 pos, int id);
55
56 [[Reflect]]
57 void removeVoxel(glm::vec3 pos);
58
59 [[Reflect]]
60 void fillBox(glm::vec3 min, glm::vec3 max, int id, voxel::WriteMode mode = voxel::WriteMode::replace, int match_id = 0);
61
63 [[Reflect]]
64 void fillRoundBox(
65 glm::vec3 min, glm::vec3 max, float radius, int id, voxel::WriteMode mode = voxel::WriteMode::replace, int match_id = 0
66 );
67
68 [[Reflect]]
69 void fillSphere(glm::vec3 center, float radius, int id, voxel::WriteMode mode = voxel::WriteMode::replace, int match_id = 0);
70
72 [[Reflect]]
73 void fillEllipsoid(glm::vec3 min, glm::vec3 max, int id, voxel::WriteMode mode = voxel::WriteMode::replace, int match_id = 0);
74
75 [[Reflect]]
76 void fillCylinder(
77 glm::vec3 a, glm::vec3 b, float radius, int id, voxel::WriteMode mode = voxel::WriteMode::replace, int match_id = 0
78 );
79
80 [[Reflect]]
81 void fillCapsule(
82 glm::vec3 a, glm::vec3 b, float radius, int id, voxel::WriteMode mode = voxel::WriteMode::replace, int match_id = 0
83 );
84
85 [[Reflect]]
86 void carveBox(glm::vec3 min, glm::vec3 max);
87
88 [[Reflect]]
89 void carveSphere(glm::vec3 center, float radius);
90
91 [[Reflect]]
92 void paintBox(glm::vec3 min, glm::vec3 max, int id);
93
98 [[Reflect]]
99 void stamp(
100 const assets::Handle<assets::VoxelModel>& asset, glm::vec3 pos, glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f),
101 voxel::WriteMode mode = voxel::WriteMode::replace, int match_id = 0
102 );
103
111 [[Reflect]]
112 void tile(
113 const assets::Handle<assets::VoxelModel>& asset, int fit = 0, glm::vec3 anchor = glm::vec3(0.0f),
114 glm::vec3 offset = glm::vec3(0.0f), glm::vec3 mirror = glm::vec3(0.0f), voxel::WriteMode mode = voxel::WriteMode::replace,
115 int match_id = 0
116 );
117
118 // Piece
119
124 [[Reflect]]
125 void redraw();
126
128 [[nodiscard, Reflect]]
129 auto getShapeScript() const -> const assets::Handle<assets::Script>&;
130
132 [[Reflect]]
134
136 [[nodiscard, Reflect]]
137 auto getClipPlanes() const -> std::vector<glm::vec4>;
138
139 [[Reflect]]
140 void setClipPlanes(std::vector<glm::vec4> planes);
141
143 [[Reflect]]
144 void addClipPlane(glm::vec4 plane);
145
146 [[Reflect]]
147 void clearClipPlanes();
148
153 [[nodiscard, Reflect]]
154 auto getShapeBounds() -> std::vector<glm::vec3>;
155
157 [[nodiscard, Reflect]]
158 auto getShape() -> Box<Node>;
159
161 [[nodiscard, Reflect]]
162 auto isMisaligned() const noexcept -> bool {
163 return m_misaligned;
164 }
165
166 signals::Signal<> drawn;
167
168 // Backend
169
170 [[nodiscard]]
171 virtual auto kind() const noexcept -> PieceKind {
172 return PieceKind::fill;
173 }
174
175 [[nodiscard]]
176 virtual auto writeMode() const noexcept -> voxel::WriteMode {
178 }
179
180 [[nodiscard]]
181 virtual auto matchId() const noexcept -> uint8_t {
182 return 0;
183 }
184
186 [[nodiscard]]
187 virtual auto sourcePaletteUid() const -> uint64_t {
188 return 0;
189 }
190
191 struct Grid {
192 std::shared_ptr<const Grid> base;
193 std::unique_ptr<voxel::Volume> volume;
194 std::array<uint32_t, voxel::k_palette_size> histogram {};
195 };
196
197 using GridRef = std::shared_ptr<const Grid>;
198
204 [[nodiscard]]
205 auto grid() -> const voxel::Volume*;
206
208 [[nodiscard]]
209 auto gridRef() -> GridRef;
210
211 // Drawing on other threads
212 // for the editor ONLY (dario im looking at you)
213
215 void requestGrid();
216
218 auto collectGrid() -> bool;
219
221 [[nodiscard]]
222 auto readyGrid() -> GridRef;
223
225 [[nodiscard]]
226 auto gridBuilding() const noexcept -> bool {
227 return m_job.valid();
228 }
229
231 [[nodiscard]]
232 auto gridStale() const noexcept -> bool {
233 return m_grid_dirty || m_job.valid();
234 }
235
237 void waitForGrid();
238
240 [[nodiscard]]
241 auto inputRevision() const noexcept -> uint32_t {
242 return m_input_revision;
243 }
244
246 [[nodiscard]]
247 auto buildCount() const noexcept -> uint32_t {
248 return m_build_count;
249 }
250
251 [[nodiscard]]
252 auto clipPlanes() const noexcept -> const std::vector<glm::vec4>& {
253 return m_clip_planes;
254 }
255
257 [[nodiscard]]
258 auto nominalSize() -> glm::ivec3 {
259 return pieceSize();
260 }
261
263 [[nodiscard]]
264 virtual auto resizable() const noexcept -> bool {
265 return false;
266 }
267
269 void releaseGrid();
270
272 void setMisaligned(bool misaligned);
273
274protected:
276 [[nodiscard]]
277 virtual auto pieceSize() -> glm::ivec3;
278
280 [[nodiscard]]
281 virtual auto prepareGrid() -> std::unique_ptr<voxel::Volume> = 0;
282
284 virtual void finishGrid(voxel::Volume& /*grid*/) { }
285
287 void markGridDirty() noexcept;
288
290 void markLandingDirty() noexcept { ++m_input_revision; }
291
292 void onReflectedFieldChanged(std::string_view field_name) override;
293 void onScriptsReloading() override;
294 void onScriptsReloaded() override;
295 void onScriptVarChanged(std::string_view path) override;
296 void updateInspectorMessages() override;
297
298 template<typename Kernel>
299 void draw(std::string_view operation, Kernel&& kernel);
300
302 [[nodiscard]]
303 auto drawing() const noexcept -> voxel::Volume*;
304
305 void init();
306 void destroy();
307
309 [[Reflect, Name("Script")]]
310 assets::Handle<assets::Script> m_shape_script;
311
313 [[Reflect, Name("Clip Planes"), Group("Clip")]]
314 std::vector<glm::vec4> m_clip_planes;
315
316private:
318 [[nodiscard]]
319 auto drawGrid() -> GridRef;
320
322 void landGrid(GridRef fresh);
323
325 void applyClip();
326
328 [[nodiscard]]
329 auto latestGrid() -> const voxel::Volume*;
330
331 GridRef m_drawn;
332 GridRef m_grid;
333 std::future<GridRef> m_job;
334 bool m_grid_dirty = true;
335 bool m_clip_dirty = true;
336 bool m_misaligned = false;
337 bool m_warned_outside_edit = false;
338 uint32_t m_input_revision = 1;
339 uint32_t m_build_count = 0;
340};
341
342}
Type-safe smart pointer for assets.
Definition core_types.hpp:149
Definition voxel_model.hpp:43
Intrusive reference-counted handle to a Node.
Definition box.hpp:35
Definition reflect.hpp:244
Definition volume.hpp:27
Definition voxel_piece.hpp:41
void requestGrid()
Definition voxel_piece.cpp:259
void addClipPlane(glm::vec4 plane)
Definition voxel_piece.cpp:325
auto collectGrid() -> bool
Definition voxel_piece.cpp:276
virtual void finishGrid(voxel::Volume &)
Definition voxel_piece.hpp:284
virtual auto pieceSize() -> glm::ivec3
Definition voxel_piece.cpp:69
void fillRoundBox(glm::vec3 min, glm::vec3 max, float radius, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
Definition voxel_piece.cpp:100
auto getSize() -> glm::vec3
Definition voxel_piece.cpp:65
auto gridRef() -> GridRef
Definition voxel_piece.cpp:244
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)
Definition voxel_piece.cpp:160
auto getClipPlanes() const -> std::vector< glm::vec4 >
Definition voxel_piece.cpp:315
std::vector< glm::vec4 > m_clip_planes
Definition voxel_piece.hpp:314
virtual auto sourcePaletteUid() const -> uint64_t
Definition voxel_piece.hpp:187
auto getShapeScript() const -> const assets::Handle< assets::Script > &
Definition voxel_piece.cpp:311
signals::Signal drawn
Sent after editShape drew the grid again.
Definition voxel_piece.hpp:166
auto getVoxel(glm::vec3 pos) -> int
Definition voxel_piece.cpp:74
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)
Definition voxel_piece.cpp:140
virtual auto prepareGrid() -> std::unique_ptr< voxel::Volume >=0
auto gridBuilding() const noexcept -> bool
Definition voxel_piece.hpp:226
virtual auto resizable() const noexcept -> bool
Definition voxel_piece.hpp:264
auto readyGrid() -> GridRef
Definition voxel_piece.cpp:290
auto getShapeBounds() -> std::vector< glm::vec3 >
Definition voxel_piece.cpp:337
void fillEllipsoid(glm::vec3 min, glm::vec3 max, int id, voxel::WriteMode mode=voxel::WriteMode::replace, int match_id=0)
Definition voxel_piece.cpp:110
void setShapeScript(assets::Handle< assets::Script > script)
Definition voxel_piece.cpp:369
auto inputRevision() const noexcept -> uint32_t
Definition voxel_piece.hpp:241
void markLandingDirty() noexcept
Definition voxel_piece.hpp:290
auto gridStale() const noexcept -> bool
Definition voxel_piece.hpp:232
auto getShape() -> Box< Node >
Definition voxel_piece.cpp:345
assets::Handle< assets::Script > m_shape_script
Definition voxel_piece.hpp:310
auto grid() -> const voxel::Volume *
Definition voxel_piece.cpp:254
auto isMisaligned() const noexcept -> bool
Definition voxel_piece.hpp:162
auto buildCount() const noexcept -> uint32_t
Definition voxel_piece.hpp:247
auto nominalSize() -> glm::ivec3
Definition voxel_piece.hpp:258
void redraw()
Definition voxel_piece.cpp:307
Definition voxel.hpp:14
Definition voxel_volume.hpp:21
Definition voxel_piece.hpp:191
Concrete asset types for the Toast engine.
Common base of every voxel node.
Functions that allow the editing of Voxel Volumes.
WriteMode
Which voxels we should override.
Definition voxel_edit.hpp:26
@ replace
Everything.
Definition voxel_edit.hpp:27
PieceKind
Definition voxel_piece.hpp:26
@ carve
Empties every voxel it has.
Definition voxel_piece.hpp:28
@ fill
Adds its voxels with its write mode.
Definition voxel_piece.hpp:27
@ paint
Recolors voxels that are already solid.
Definition voxel_piece.hpp:29