5#include <glm/common.hpp>
6#include <glm/geometric.hpp>
13constexpr char kMagic[4] = {
'E',
'V',
'S',
'V'};
14constexpr std::uint32_t kVersion = 1;
17bool writeValue(std::ofstream &stream,
const T &
value) {
18 stream.write(
reinterpret_cast<const char *
>(&
value),
sizeof(T));
23bool readValue(std::ifstream &stream, T &
value) {
24 stream.read(
reinterpret_cast<char *
>(&
value),
sizeof(T));
28bool isVacuum(
const FogFroxel &
f) {
29 return f.extinction == 0.f &&
f.scattering == glm::vec3(0.f) &&
f.emissive == glm::vec3(0.f);
35 width_ = std::max(
width, 1);
36 height_ = std::max(
height, 1);
37 depth_ = std::max(
depth, 1);
38 brickSize_ = std::clamp(brickSize, 2, 32);
45 const std::size_t perBrick = std::size_t(brickSize_) * std::size_t(brickSize_) * std::size_t(brickSize_);
46 return bricks_.size() * perBrick;
49bool SparseVolumeTexture::inBounds(
int x,
int y,
int z)
const {
50 return x >= 0 &&
y >= 0 &&
z >= 0 &&
x < width_ &&
y < height_ &&
z < depth_;
53std::uint64_t SparseVolumeTexture::brickKey(
int bx,
int by,
int bz)
const {
54 return (std::uint64_t(std::uint32_t(bx)) << 42) | (std::uint64_t(std::uint32_t(by)) << 21) |
55 std::uint64_t(std::uint32_t(bz));
58std::size_t SparseVolumeTexture::localIndex(
int x,
int y,
int z)
const {
59 const int lx =
x % brickSize_;
60 const int ly =
y % brickSize_;
61 const int lz =
z % brickSize_;
62 return (std::size_t(lz) * std::size_t(brickSize_) + std::size_t(ly)) * std::size_t(brickSize_) + std::size_t(lx);
66 if (!inBounds(
x,
y,
z))
return;
67 const std::uint64_t
key = brickKey(
x / brickSize_,
y / brickSize_,
z / brickSize_);
69 if (
found == bricks_.end()) {
70 if (isVacuum(voxel))
return;
72 brick.voxels.resize(std::size_t(brickSize_) * std::size_t(brickSize_) * std::size_t(brickSize_));
73 found = bricks_.emplace(
key, std::move(brick)).first;
75 found->second.voxels[localIndex(
x,
y,
z)] = voxel;
79 if (!inBounds(
x,
y,
z))
return {};
80 const auto found = bricks_.find(brickKey(
x / brickSize_,
y / brickSize_,
z / brickSize_));
85 return getVoxel(std::clamp(
int(
u *
float(width_)), 0, width_ - 1),
86 std::clamp(
int(
v *
float(height_)), 0, height_ - 1),
87 std::clamp(
int(
w *
float(depth_)), 0, depth_ - 1));
91 float extinctionScale,
const glm::vec3 &
albedo,
float emptyThreshold,
float time) {
93 const glm::vec3 omega = glm::clamp(
albedo, glm::vec3(0.f), glm::vec3(1.f));
94 for (
int z = 0;
z < depth_; ++
z) {
95 for (
int y = 0;
y < height_; ++
y) {
96 for (
int x = 0;
x < width_; ++
x) {
97 const glm::vec3 uvw((
float(
x) + 0.5f) /
float(width_), (
float(
y) + 0.5f) /
float(height_),
98 (
float(
z) + 0.5f) /
float(depth_));
100 std::max(0.f,
graph.evaluate(worldMin + (worldMax - worldMin) * uvw,
time) * extinctionScale);
112 std::ofstream stream(
path, std::ios::binary | std::ios::trunc);
113 if (!stream)
return false;
114 stream.write(kMagic,
sizeof(kMagic));
115 const std::uint32_t
width = std::uint32_t(width_),
height = std::uint32_t(height_);
116 const std::uint32_t
depth = std::uint32_t(depth_), brickSize = std::uint32_t(brickSize_);
117 const std::uint64_t brickCount = std::uint64_t(bricks_.size());
118 if (!writeValue(stream, kVersion) || !writeValue(stream,
width) || !writeValue(stream,
height) ||
119 !writeValue(stream,
depth) || !writeValue(stream, brickSize) || !writeValue(stream, brickCount))
121 std::vector<std::uint64_t> keys;
122 keys.reserve(bricks_.size());
123 for (
const auto &entry : bricks_) keys.push_back(entry.first);
124 std::sort(keys.begin(), keys.end());
125 for (std::uint64_t
key : keys) {
126 if (!writeValue(stream,
key))
return false;
127 const Brick &brick = bricks_.at(
key);
129 const float values[9] = {
f.scattering.x,
f.scattering.y,
f.scattering.z,
f.extinction,
f.emissive.x,
130 f.emissive.y,
f.emissive.z,
f.anisotropy,
f.lightVisibility};
131 stream.write(
reinterpret_cast<const char *
>(
values),
sizeof(
values));
132 if (!stream)
return false;
139 std::ifstream stream(
path, std::ios::binary);
141 stream.read(magic,
sizeof(magic));
143 std::uint64_t brickCount = 0;
144 if (!stream || !std::equal(std::begin(magic), std::end(magic), std::begin(kMagic)) || !readValue(stream,
version) ||
146 !readValue(stream, brickSize) || !readValue(stream, brickCount) ||
width == 0 ||
height == 0 ||
depth == 0 ||
147 brickSize < 2 || brickSize > 32)
151 const std::size_t voxelsPerBrick = std::size_t(brickSize) * std::size_t(brickSize) * std::size_t(brickSize);
152 for (std::uint64_t i = 0; i < brickCount; ++i) {
153 std::uint64_t
key = 0;
154 if (!readValue(stream,
key))
return false;
156 brick.voxels.resize(voxelsPerBrick);
159 stream.read(
reinterpret_cast<char *
>(
values),
sizeof(
values));
160 if (!stream)
return false;
167 loaded.bricks_.emplace(
key, std::move(brick));
169 *
this = std::move(
loaded);
179float hash3(
const glm::ivec3 &
p, std::uint32_t
seed) {
180 std::uint32_t
h =
seed ^ (std::uint32_t(
p.x) * 0x8da6b343u) ^ (std::uint32_t(
p.y) * 0xd8163841u) ^
181 (std::uint32_t(
p.z) * 0xcb1ab31fu);
185 return float(
h & 0x00ffffffu) / float(0x01000000u);
196 const int next = int(nodes_.size());
199 node.inputA = inputA >= 0 && inputA < next ? inputA : -1;
200 node.inputB = inputB >= 0 && inputB < next ? inputB : -1;
202 nodes_.push_back(
node);
211float VolumeDensityGraph::valueNoise(
const glm::vec3 &
p, std::uint32_t
seed) {
212 const glm::ivec3
cell = glm::ivec3(glm::floor(
p));
213 const glm::vec3
f = glm::fract(
p);
214 const glm::vec3
u =
f *
f * (3.f - 2.f *
f);
216 for (
int z = 0;
z < 2; ++
z)
217 for (
int y = 0;
y < 2; ++
y)
219 const float z0 = glm::mix(glm::mix(
corners[0][0][0],
corners[0][0][1],
u.x),
221 const float z1 = glm::mix(glm::mix(
corners[1][0][0],
corners[1][0][1],
u.x),
223 return glm::mix(z0, z1,
u.z);
227 if (nodes_.empty())
return 0.f;
228 std::vector<float>
values(nodes_.size(), 0.f);
229 for (std::size_t i = 0; i < nodes_.size(); ++i) {
230 const Node &
n = nodes_[i];
231 const float a =
n.inputA >= 0 ?
values[std::size_t(
n.inputA)] : 0.f;
232 const float b =
n.inputB >= 0 ?
values[std::size_t(
n.inputB)] : 0.f;
236 values[i] = std::exp(-std::max(
n.params.y, 0.f) * std::max(worldPosition.y -
n.params.x, 0.f));
239 const float radius = std::max(
n.params.w, 1e-4f);
240 values[i] = std::clamp(1.f - glm::length(worldPosition - glm::vec3(
n.params)) /
radius, 0.f, 1.f);
244 const glm::vec3
q = glm::abs(worldPosition - glm::vec3(
n.params));
245 const float extent = std::max(
n.params.w, 1e-4f);
246 values[i] = std::clamp(1.f - std::max(std::max(
q.x,
q.y),
q.z) / extent, 0.f, 1.f);
250 const float scale = std::max(std::fabs(
n.params.x), 1e-4f);
251 const glm::vec3 wind(
n.params.y, 0.f,
n.params.z);
252 values[i] = valueNoise((worldPosition + wind *
time) /
scale, std::uint32_t(std::max(
n.params.w, 0.f)));
261 const int selected = output_ >= 0 ? output_ : int(nodes_.size()) - 1;
graphics::Texture * albedo
std::array< double, 10 > q
std::array< float, 3 > scale
std::map< std::string, std::vector< std::string > > graph
std::unordered_set< std::string > loaded
std::set< std::string > values
Brick-sparse, pre-bakeable participating-media texture.
bool save(const std::string &path) const
Save the deterministic EVSV v1 binary format.
FogFroxel sample(float u, float v, float w) const
Nearest sample using normalized coordinates.
std::size_t getAllocatedVoxelCount() const
Returns the allocated voxel count.
void resize(int width, int height, int depth, int brickSize=8)
Allocate a logical volume; bricks are created only for non-empty voxels.
void bake(const VolumeDensityGraph &graph, const glm::vec3 &worldMin, const glm::vec3 &worldMax, float extinctionScale, const glm::vec3 &albedo, float emptyThreshold=1e-5f, float time=0.f)
Pre-bake a procedural graph into sparse bricks.
FogFroxel getVoxel(int x, int y, int z) const
Read one voxel; missing bricks return vacuum.
void setVoxel(int x, int y, int z, const FogFroxel &voxel)
Store one voxel; all-zero voxels do not allocate a new brick.
bool load(const std::string &path)
Load the deterministic EVSV v1 binary format, replacing current data.
Compact acyclic program that evaluates procedural volume density.
void clear()
Remove every node and reset the output.
float evaluate(const glm::vec3 &worldPosition, float time=0.f) const
Evaluate signed density at a world position and time.
void setOutput(int nodeIndex)
Select the node returned by evaluate; invalid indices select the last node.
int addNode(Op op, int inputA=-1, int inputB=-1, const glm::vec4 ¶ms=glm::vec4(0.f))
Append a node and return its stable index. Inputs must precede the node.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Participating-media coefficients stored in one froxel.