载入中...
搜索中...
未找到
SparseVolumeTexture.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <fstream>
5#include <glm/common.hpp>
6#include <glm/geometric.hpp>
8
9// SparseVolumeTexture.cpp
10namespace eve::graphics {
11namespace {
12
13constexpr char kMagic[4] = {'E', 'V', 'S', 'V'};
14constexpr std::uint32_t kVersion = 1;
15
16template <typename T>
17bool writeValue(std::ofstream &stream, const T &value) {
18 stream.write(reinterpret_cast<const char *>(&value), sizeof(T));
19 return bool(stream);
20}
21
22template <typename T>
23bool readValue(std::ifstream &stream, T &value) {
24 stream.read(reinterpret_cast<char *>(&value), sizeof(T));
25 return bool(stream);
26}
27
28bool isVacuum(const FogFroxel &f) {
29 return f.extinction == 0.f && f.scattering == glm::vec3(0.f) && f.emissive == glm::vec3(0.f);
30}
31
32} // namespace
33
34void SparseVolumeTexture::resize(int width, int height, int depth, int brickSize) {
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);
39 bricks_.clear();
40}
41
42void SparseVolumeTexture::clear() { bricks_.clear(); }
43
45 const std::size_t perBrick = std::size_t(brickSize_) * std::size_t(brickSize_) * std::size_t(brickSize_);
46 return bricks_.size() * perBrick;
47}
48
49bool SparseVolumeTexture::inBounds(int x, int y, int z) const {
50 return x >= 0 && y >= 0 && z >= 0 && x < width_ && y < height_ && z < depth_;
51}
52
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));
56}
57
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);
63}
64
65void SparseVolumeTexture::setVoxel(int x, int y, int z, const FogFroxel &voxel) {
66 if (!inBounds(x, y, z)) return;
67 const std::uint64_t key = brickKey(x / brickSize_, y / brickSize_, z / brickSize_);
68 auto found = bricks_.find(key);
69 if (found == bricks_.end()) {
70 if (isVacuum(voxel)) return;
71 Brick brick;
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;
74 }
75 found->second.voxels[localIndex(x, y, z)] = voxel;
76}
77
79 if (!inBounds(x, y, z)) return {};
80 const auto found = bricks_.find(brickKey(x / brickSize_, y / brickSize_, z / brickSize_));
81 return found == bricks_.end() ? FogFroxel{} : found->second.voxels[localIndex(x, y, z)];
82}
83
84FogFroxel SparseVolumeTexture::sample(float u, float v, float w) const {
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));
88}
89
90void SparseVolumeTexture::bake(const VolumeDensityGraph &graph, const glm::vec3 &worldMin, const glm::vec3 &worldMax,
91 float extinctionScale, const glm::vec3 &albedo, float emptyThreshold, float time) {
92 clear();
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_));
99 const float extinction =
100 std::max(0.f, graph.evaluate(worldMin + (worldMax - worldMin) * uvw, time) * extinctionScale);
101 if (extinction <= emptyThreshold) continue;
102 FogFroxel voxel;
103 voxel.extinction = extinction;
104 voxel.scattering = omega * extinction;
105 setVoxel(x, y, z, voxel);
106 }
107 }
108 }
109}
110
111bool SparseVolumeTexture::save(const std::string &path) const {
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))
120 return false;
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);
128 for (const FogFroxel &f : brick.voxels) {
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;
133 }
134 }
135 return true;
136}
137
138bool SparseVolumeTexture::load(const std::string &path) {
139 std::ifstream stream(path, std::ios::binary);
140 char magic[4]{};
141 stream.read(magic, sizeof(magic));
142 std::uint32_t version = 0, width = 0, height = 0, depth = 0, brickSize = 0;
143 std::uint64_t brickCount = 0;
144 if (!stream || !std::equal(std::begin(magic), std::end(magic), std::begin(kMagic)) || !readValue(stream, version) ||
145 version != kVersion || !readValue(stream, width) || !readValue(stream, height) || !readValue(stream, depth) ||
146 !readValue(stream, brickSize) || !readValue(stream, brickCount) || width == 0 || height == 0 || depth == 0 ||
147 brickSize < 2 || brickSize > 32)
148 return false;
150 loaded.resize(int(width), int(height), int(depth), int(brickSize));
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;
155 Brick brick;
156 brick.voxels.resize(voxelsPerBrick);
157 for (FogFroxel &f : brick.voxels) {
158 float values[9]{};
159 stream.read(reinterpret_cast<char *>(values), sizeof(values));
160 if (!stream) return false;
161 f.scattering = {values[0], values[1], values[2]};
162 f.extinction = values[3];
163 f.emissive = {values[4], values[5], values[6]};
164 f.anisotropy = values[7];
165 f.lightVisibility = values[8];
166 }
167 loaded.bricks_.emplace(key, std::move(brick));
168 }
169 *this = std::move(loaded);
170 return true;
171}
172
173} // namespace eve::graphics
174
175// VolumeDensityGraph.cpp
176namespace eve::graphics {
177namespace {
178
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);
182 h ^= h >> 16;
183 h *= 0x7feb352du;
184 h ^= h >> 15;
185 return float(h & 0x00ffffffu) / float(0x01000000u);
186}
187
188} // namespace
189
191 nodes_.clear();
192 output_ = -1;
193}
194
195int VolumeDensityGraph::addNode(Op op, int inputA, int inputB, const glm::vec4 &params) {
196 const int next = int(nodes_.size());
197 Node node;
198 node.op = op;
199 node.inputA = inputA >= 0 && inputA < next ? inputA : -1;
200 node.inputB = inputB >= 0 && inputB < next ? inputB : -1;
201 node.params = params;
202 nodes_.push_back(node);
203 output_ = next;
204 return next;
205}
206
208 output_ = nodeIndex >= 0 && nodeIndex < int(nodes_.size()) ? nodeIndex : int(nodes_.size()) - 1;
209}
210
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);
215 float corners[2][2][2];
216 for (int z = 0; z < 2; ++z)
217 for (int y = 0; y < 2; ++y)
218 for (int x = 0; x < 2; ++x) corners[z][y][x] = hash3(cell + glm::ivec3(x, y, z), seed);
219 const float z0 = glm::mix(glm::mix(corners[0][0][0], corners[0][0][1], u.x),
220 glm::mix(corners[0][1][0], corners[0][1][1], u.x), u.y);
221 const float z1 = glm::mix(glm::mix(corners[1][0][0], corners[1][0][1], u.x),
222 glm::mix(corners[1][1][0], corners[1][1][1], u.x), u.y);
223 return glm::mix(z0, z1, u.z);
224}
225
226float VolumeDensityGraph::evaluate(const glm::vec3 &worldPosition, float time) const {
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;
233 switch (n.op) {
234 case Op::constant: values[i] = n.params.x; break;
235 case Op::height:
236 values[i] = std::exp(-std::max(n.params.y, 0.f) * std::max(worldPosition.y - n.params.x, 0.f));
237 break;
238 case Op::sphere: {
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);
241 break;
242 }
243 case Op::box: {
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);
247 break;
248 }
249 case Op::noise: {
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)));
253 break;
254 }
255 case Op::add: values[i] = a + b; break;
256 case Op::multiply: values[i] = a * b; break;
257 case Op::subtract: values[i] = a - b; break;
258 case Op::clamp: values[i] = std::clamp(a, n.params.x, n.params.y); break;
259 }
260 }
261 const int selected = output_ >= 0 ? output_ : int(nodes_.size()) - 1;
262 return values[std::size_t(selected)];
263}
264
265} // namespace eve::graphics
double z
double y
double x
float w
Definition AnimClip.cpp:738
std::string selected
graphics::Texture * albedo
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
float v
int h
std::uint32_t height
std::uint32_t width
std::array< float, 3 > scale
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
std::map< std::string, std::vector< std::string > > graph
Definition Package.cpp:173
float f
std::string path
Definition PlayHost.cpp:111
std::unordered_set< std::string > loaded
Definition Plugins.cpp:24
std::set< std::string > values
Object::Ptr value
const RoadNode * node
bool found
Value::Array corners
Vec extinction
Cell cell
uint32_t seed
Definition TreeMesh.cpp:276
std::uint32_t depth
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 &params=glm::vec4(0.f))
Append a node and return its stable index. Inputs must precede the node.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
Participating-media coefficients stored in one froxel.