载入中...
搜索中...
未找到
FogVolume.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6
7#include <glm/common.hpp>
8#include <glm/geometric.hpp>
9#include <glm/vec2.hpp>
10
11namespace eve::graphics {
12namespace {
13
14float hashNoise(int x, int y, int z, int seed) {
15 uint32_t value = uint32_t(x) * 0x8da6b343u ^ uint32_t(y) * 0xd8163841u ^
16 uint32_t(z) * 0xcb1ab31fu ^ uint32_t(seed) * 0x165667b1u;
17 value ^= value >> 16u;
18 value *= 0x7feb352du;
19 value ^= value >> 15u;
20 return float(value & 0x00ffffffu) / float(0x01000000u);
21}
22
23float smoothNoise(const glm::vec3 &p, int seed) {
24 const glm::ivec3 cell = glm::ivec3(glm::floor(p));
25 const glm::vec3 fraction = glm::fract(p);
26 const glm::vec3 blend = fraction * fraction * (glm::vec3(3.f) - 2.f * fraction);
27 float corners[2][2][2];
28 for (int z = 0; z < 2; ++z)
29 for (int y = 0; y < 2; ++y)
30 for (int x = 0; x < 2; ++x)
31 corners[x][y][z] = hashNoise(cell.x + x, cell.y + y, cell.z + z, seed);
32 const float z0 = glm::mix(glm::mix(corners[0][0][0], corners[1][0][0], blend.x),
33 glm::mix(corners[0][1][0], corners[1][1][0], blend.x), blend.y);
34 const float z1 = glm::mix(glm::mix(corners[0][0][1], corners[1][0][1], blend.x),
35 glm::mix(corners[0][1][1], corners[1][1][1], blend.x), blend.y);
36 return glm::mix(z0, z1, blend.z);
37}
38
39} // namespace
40
41void FogVolume::setShape(const std::string &shape) {
42 if (shape == "sphere")
43 shape_ = Shape::sphere;
44 else if (shape == "cylinder")
45 shape_ = Shape::cylinder;
46 else
47 shape_ = Shape::box;
48}
49
50std::string FogVolume::getShapeName() const {
51 if (shape_ == Shape::sphere) return "sphere";
52 if (shape_ == Shape::cylinder) return "cylinder";
53 return "box";
54}
55
56void FogVolume::setSize(float x, float y, float z) {
57 size_ = glm::max(glm::vec3(x, y, z), glm::vec3(1e-3f));
58}
59
60void FogVolume::setAlbedo(float r, float g, float b) {
61 albedo_ = glm::clamp(glm::vec3(r, g, b), glm::vec3(0.f), glm::vec3(1.f));
62}
63
64void FogVolume::setEmissive(float r, float g, float b) {
65 emissive_ = glm::max(glm::vec3(r, g, b), glm::vec3(0.f));
66}
67
68void FogVolume::setAnisotropy(float anisotropy) {
69 anisotropy_ = std::clamp(anisotropy, -0.99f, 0.99f);
70}
71
72void FogVolume::setEdgeFalloff(float falloff) {
73 edgeFalloff_ = std::clamp(falloff, 0.f, 1.f);
74}
75
76void FogVolume::setNoise(float amount, float scale, int seed) {
77 noiseAmount_ = std::clamp(amount, 0.f, 1.f);
78 noiseScale_ = std::max(scale, 1e-3f);
79 noiseSeed_ = seed;
80}
81
82float FogVolume::sampleExtinction(const glm::vec3 &worldPosition) const {
83 const glm::vec3 p = (worldPosition - position_) / (size_ * 0.5f);
84 float radius = 0.f;
85 if (shape_ == Shape::sphere) {
86 radius = glm::length(p);
87 } else if (shape_ == Shape::cylinder) {
88 radius = std::max(glm::length(glm::vec2(p.x, p.z)), std::fabs(p.y));
89 } else {
90 radius = std::max(std::max(std::fabs(p.x), std::fabs(p.y)), std::fabs(p.z));
91 }
92 if (radius >= 1.f) return 0.f;
93 const float inner = 1.f - edgeFalloff_;
94 const float coverage = edgeFalloff_ <= 1e-6f || radius <= inner
95 ? 1.f
96 : 1.f - glm::smoothstep(inner, 1.f, radius);
97 const float noise = smoothNoise(worldPosition * noiseScale_, noiseSeed_);
98 const float densityVariation = glm::mix(1.f, 0.18f + 1.32f * noise, noiseAmount_);
99 return extinction_ * coverage * densityVariation;
100}
101
102} // namespace eve::graphics
double value
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
glm::vec4 p[6]
tensor::Graph g
Definition GpuGraph.cpp:7
ShaderImageInput shape
double r
float blend
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
float radius
std::uint32_t seed
Definition PointSet.cpp:807
Cell cell
float sampleExtinction(const glm::vec3 &worldPosition) const
Normalized analytic density at a world position, including signed extinction.
Definition FogVolume.cpp:82
void setAnisotropy(float anisotropy)
HG anisotropy in [-0.99,0.99].
Definition FogVolume.cpp:68
std::string getShapeName() const
Definition FogVolume.cpp:50
void setShape(const std::string &shape)
Set shape using "sphere", "box" or "cylinder"; unknown selects box.
Definition FogVolume.cpp:41
void setAlbedo(float r, float g, float b)
Single-scattering albedo, clamped to [0,1].
Definition FogVolume.cpp:60
void setEmissive(float r, float g, float b)
Unlit radiance emitted per unit density.
Definition FogVolume.cpp:64
void setEdgeFalloff(float falloff)
Fraction of normalized radius used for a smooth edge, in [0,1].
Definition FogVolume.cpp:72
void setSize(float x, float y, float z)
Set full world-space dimensions; values are clamped positive.
Definition FogVolume.cpp:56
void setNoise(float amount, float scale, int seed)
Configure deterministic world-space density variation.
Definition FogVolume.cpp:76
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25