载入中...
搜索中...
未找到
GraphicsFoliage.cpp
浏览该文件的文档.
1#include <array>
2#include <cstdint>
3#include <cstdio>
4#include <unordered_map>
6#include "graphics/Graphics.h"
7#include "graphics/Material.h"
8#include "image/ImageData.h"
9
10// GraphicsFoliage.cpp
11namespace eve::graphics {
12Result<void> Graphics::configureFoliageWind(Material* material, float directionX, float directionZ, float strength,
13 float bending, float bendingSpeed, float branch, float branchSpeed,
14 float flutter, float flutterSpeed, float fadeDistance, float bendFactor) {
15 if (!material)
16 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "null foliage material", {}, {},
17 "graphics.foliage.wind"));
19 std::array<uint16_t, 9 * 4> neutral{};
20 for (size_t layer = 0; layer < 9; ++layer) {
21 neutral[layer * 4] = 0x3c00; // 1.0
22 neutral[layer * 4 + 2] = 0x3800; // 0.5
23 }
24 auto uploaded = newTextureArrayRgba16f(1, 1, 9, neutral);
25 if (!uploaded) return Result<void>::failure(uploaded.status());
26 foliageWindMotionTexture_ = uploaded.value();
27 }
29 image::ImageData noise(4, 4, "RGBA8");
30 for (int y = 0; y < 4; ++y) {
31 for (int x = 0; x < 4; ++x) {
32 const float value = float((x * 73 + y * 151 + x * y * 29 + 37) & 255) / 255.f;
33 noise.setPixel(x, y, image::ImageData::Colorf{value, 1.f - value, .5f, 1.f});
34 }
35 }
38 return Result<void>::failure(Diagnostic::error(DiagnosticCode::Failed, "foliage noise upload failed", {},
39 {}, "graphics.foliage.wind"));
40 }
41 return material->setFoliageWind(foliageWindMotionTexture_, foliageWindNoiseTexture_, directionX, directionZ,
42 strength, bending, bendingSpeed, branch, branchSpeed, flutter, flutterSpeed,
43 fadeDistance, bendFactor);
44}
45} // namespace eve::graphics
46
47// GraphicsLifetime.cpp
48namespace eve::graphics {
49namespace {
50struct Lifetime {
52 bool retired = false;
53};
54bool registryAlive = false;
55struct Registry {
56 std::unordered_map<const Graphics*, std::shared_ptr<Lifetime>> tokens;
57 Registry() { registryAlive = true; }
58 ~Registry() { registryAlive = false; }
59};
60auto& lifetimes() {
61 static Registry registry;
62 return registry.tokens;
63}
64auto lifetime(const Graphics* graphics) {
65 auto& value = lifetimes()[graphics];
66 if (!value) value = std::make_shared<Lifetime>();
67 return value;
68}
69} // namespace
70std::weak_ptr<const void> Graphics::resourceLifetime() const {
71 auto value = lifetime(this);
72 return value->retired ? std::weak_ptr<const void>{} : value;
73}
75 auto value = lifetime(this);
76 if (value->retired)
78 Diagnostic::error(DiagnosticCode::Conflict, "Graphics resources are retiring"));
79 return Result<eve::Subscription>::success(value->retiring.subscribe(std::move(callback)));
80}
82 if (!registryAlive) return;
83 auto it = lifetimes().find(this);
84 if (it == lifetimes().end() || it->second->retired) return;
85 auto value = it->second;
86 value->retired = true;
87 const auto failures = value->retiring.notifyChecked([]() noexcept {});
88 if (failures) std::fprintf(stderr, "Graphics resource retirement: %zu callback failures\n", failures);
89 lifetimes().erase(this);
90}
91} // namespace eve::graphics
double y
double x
std::vector< std::unique_ptr< ScriptClimbingEventSink > > retired
float strength
eve::Observer retiring
std::weak_ptr< const void > lifetime
std::array< uint32_t, 16 > material
TileLayer * layer
const eve::script::BindingContractRegistry & registry
Object::Ptr value
Graphics * graphics
size_t end
std::vector< Token > tokens
ViewPreparation callback
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Thread-affine callback registry with mutation-safe dispatch.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Result< void > configureFoliageWind(Material *material, float directionX, float directionZ, float strength, float bending, float bendingSpeed, float branch, float branchSpeed, float flutter, float flutterSpeed, float fadeDistance, float bendFactor)
Apply a source-derived foliage wind profile using graphics-owned shared field textures....
std::weak_ptr< const void > resourceLifetime() const
Observe this provider's resource lifetime without extending it.
Texture * foliageWindNoiseTexture_
Definition Graphics.h:2365
void retireResourceLifetime() const
Retire resource lifetime.
Texture * newTextureFromImageData(image::ImageData *data, bool repeatU=false, bool repeatV=false)
Creates a texture from image data. @ownership Caller deletes unless documented otherwise.
Result< eve::Subscription > onResourcesRetiring(std::function< void()> callback) const
Register cleanup before this provider destroys its GPU device/resources.
Texture * foliageWindMotionTexture_
Definition Graphics.h:2364
virtual Result< Texture * > newTextureArrayRgba16f(uint32_t width, uint32_t height, uint32_t layers, std::span< const uint16_t > rgbaHalf)
Upload a sampled 2D array of tightly packed linear RGBA16F texels.
Packages shading method + surface parameters into one attachable asset.
Definition Material.h:38
Represents raw pixel data.
Definition ImageData.h:38
void setPixel(int x, int y, const Colorf &p)
Sets the pixel at location (x,y).
medialoader::Colorf Colorf
Definition ImageData.h:41
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25