Toast Engine
Loading...
Searching...
No Matches
fracture_profile.hpp
Go to the documentation of this file.
1
7
8#pragma once
9
10#include "material_defaults.hpp"
11
12#include <algorithm>
13#include <cstdint>
14
15namespace physics {
16
18 float debris_share = material_defaults::k_debris_share;
19 float fragment_share = material_defaults::k_fragment_share;
20
21 float fragment_threshold = material_defaults::k_fragment_threshold;
22 uint32_t fragment_max_parts = material_defaults::k_fragment_max_parts;
23
24 float shatter_threshold = material_defaults::k_shatter_threshold;
25 float shatter_cell_size = material_defaults::k_shatter_cell_size;
26 float shatter_crack_cost = material_defaults::k_shatter_crack_cost;
27 float shatter_release = material_defaults::k_shatter_release;
28 float shatter_stretch = material_defaults::k_shatter_stretch;
29 float shatter_crumble = material_defaults::k_shatter_crumble;
30
31 float edge_noise = material_defaults::k_edge_noise;
32 float edge_lobes = material_defaults::k_edge_lobes;
33 float edge_noise_scale = material_defaults::k_edge_noise_scale;
34};
35
36namespace detail {
37
38[[nodiscard]]
39constexpr auto quantize(float value, float steps_per_unit, float max_steps) -> float {
40 if (not(value > 0.0f)) {
41 return 0.0f;
42 }
43 const float steps = (value * steps_per_unit) + 0.5f;
44 return steps >= max_steps ? max_steps : static_cast<float>(static_cast<uint32_t>(steps));
45}
46
47}
48
50 uint16_t fragment_threshold = 0;
51 uint16_t shatter_threshold = 0;
52 uint8_t debris_share = 0;
53 uint8_t fragment_share = 0;
54 uint8_t edge_noise = 0;
55 uint8_t edge_lobes = 0;
56 uint8_t shatter_release = 0;
57 uint8_t shatter_crumble = 0;
58 uint8_t edge_noise_scale = 0;
59 uint8_t shatter_crack_cost = 0;
60 uint8_t shatter_stretch = 0;
61 uint8_t shatter_cell_size = 0;
62 uint8_t fragment_max_parts = 0;
63
64 [[nodiscard]]
65 static constexpr auto pack(const FractureValues& v) -> PackedFractureProfile {
67 .fragment_threshold = static_cast<uint16_t>(detail::quantize(v.fragment_threshold, 1000.0f, 65535.0f)),
68 .shatter_threshold = static_cast<uint16_t>(detail::quantize(v.shatter_threshold, 1000.0f, 65535.0f)),
69 .debris_share = static_cast<uint8_t>(detail::quantize(v.debris_share, 255.0f, 255.0f)),
70 .fragment_share = static_cast<uint8_t>(detail::quantize(v.fragment_share, 255.0f, 255.0f)),
71 .edge_noise = static_cast<uint8_t>(detail::quantize(v.edge_noise, 255.0f, 255.0f)),
72 .edge_lobes = static_cast<uint8_t>(detail::quantize(v.edge_lobes, 255.0f, 255.0f)),
73 .shatter_release = static_cast<uint8_t>(detail::quantize(v.shatter_release, 255.0f, 255.0f)),
74 .shatter_crumble = static_cast<uint8_t>(detail::quantize(v.shatter_crumble, 255.0f, 255.0f)),
75 .edge_noise_scale = static_cast<uint8_t>(detail::quantize(v.edge_noise_scale, 4.0f, 255.0f)),
76 .shatter_crack_cost = static_cast<uint8_t>(detail::quantize(v.shatter_crack_cost, 64.0f, 255.0f)),
77 .shatter_stretch = static_cast<uint8_t>(detail::quantize(v.shatter_stretch, 16.0f, 255.0f)),
78 .shatter_cell_size = static_cast<uint8_t>(detail::quantize(v.shatter_cell_size, 100.0f, 255.0f)),
79 .fragment_max_parts = static_cast<uint8_t>(std::min<uint32_t>(v.fragment_max_parts, 255u)),
80 };
81 }
82
83 [[nodiscard]]
84 constexpr auto unpack() const -> FractureValues {
85 return FractureValues {
86 .debris_share = static_cast<float>(debris_share) / 255.0f,
87 .fragment_share = static_cast<float>(fragment_share) / 255.0f,
88 .fragment_threshold = static_cast<float>(fragment_threshold) / 1000.0f,
89 .fragment_max_parts = fragment_max_parts,
90 .shatter_threshold = static_cast<float>(shatter_threshold) / 1000.0f,
91 .shatter_cell_size = static_cast<float>(shatter_cell_size) / 100.0f,
92 .shatter_crack_cost = static_cast<float>(shatter_crack_cost) / 64.0f,
93 .shatter_release = static_cast<float>(shatter_release) / 255.0f,
94 .shatter_stretch = static_cast<float>(shatter_stretch) / 16.0f,
95 .shatter_crumble = static_cast<float>(shatter_crumble) / 255.0f,
96 .edge_noise = static_cast<float>(edge_noise) / 255.0f,
97 .edge_lobes = static_cast<float>(edge_lobes) / 255.0f,
98 .edge_noise_scale = static_cast<float>(edge_noise_scale) / 4.0f,
99 };
100 }
101};
102
103namespace material_defaults {
104inline constexpr PackedFractureProfile k_fracture_profile = PackedFractureProfile::pack(FractureValues {});
105}
106
107}
The values a material has when nothing sets them.
Definition fracture_profile.hpp:17
Definition fracture_profile.hpp:49