载入中...
搜索中...
未找到
Snow.cpp
浏览该文件的文档.
1#include "weather/Snow.h"
2
3#include "graphics/Graphics.h"
5
6#include <simplesquirrel/simplesquirrel.hpp>
7
8#include <cstdint>
9#include <functional>
10#include <vector>
11
12namespace eve::weather {
13
15
16Snow::Snow() = default;
17
19 procgen::Heightmap &out, float heightScale) {
20 const int w = terrain.getWidth();
21 const int h = terrain.getHeight();
22 out.resize(w, h);
23 for (int y = 0; y < h; ++y) {
24 for (int x = 0; x < w; ++x) {
25 const float s = field.inBounds(x, y) ? field.height(x, y) : 0.f;
26 out.setHeight(x, y, terrain.height(x, y) + s * heightScale);
27 }
28 }
29}
30
32 return new SnowField(width, height);
33}
34
36 procgen::Heightmap *out, float heightScale) {
37 if (!field || !terrain || !out) return false;
38 applySnowToHeightmap(*field, *terrain, *out, heightScale);
39 return true;
40}
41
42namespace {
43
44bool snowPixels(const SnowField &field, const std::string &kind, std::vector<uint8_t> &rgba) {
45 if (kind == "height") {
46 rgba = field.toHeightRGBA();
47 return true;
48 }
49 if (kind == "albedo") {
50 rgba = field.toAlbedoRGBA();
51 return true;
52 }
53 if (kind == "normal") {
54 rgba = field.toNormalRGBA();
55 return true;
56 }
57 return false;
58}
59
60} // namespace
61
63 const std::string &kind) {
64 if (!field || !gfx) return nullptr;
65 std::vector<uint8_t> rgba;
66 if (!snowPixels(*field, kind, rgba) || rgba.empty()) return nullptr;
67 return gfx->newTexture(field->getWidth(), field->getHeight(), rgba.data(), false, false);
68}
69
71 graphics::Graphics *gfx, const std::string &kind) {
72 if (!field || !texture || !gfx) return false;
73 std::vector<uint8_t> rgba;
74 if (!snowPixels(*field, kind, rgba) || rgba.empty()) return false;
75 return gfx->updateTexture(texture, field->getWidth(), field->getHeight(), rgba.data());
76}
77
78// ---------------------------------------------------------------------------
79// Script binding
80// ---------------------------------------------------------------------------
81
82void Snow::expose(ssq::Table &table) {
83 auto cls = table.addClass(name, Snow::create, false);
84 expose(cls);
85
86 auto field = table.addClass<SnowField>(
87 "SnowField", std::function<SnowField *()>([]() -> SnowField * { return nullptr; }),
88 true);
89 field.addFunc("resize", &SnowField::resize);
90 field.addFunc("getWidth", &SnowField::getWidth);
91 field.addFunc("getHeight", &SnowField::getHeight);
92 field.addFunc("fill", &SnowField::fill);
93 field.addFunc("setHeight", &SnowField::setHeight);
94 field.addFunc("height", &SnowField::height);
95 field.addFunc("stampFootprint", &SnowField::stampFootprint);
96 field.addFunc("stampImpact", &SnowField::stampImpact);
97 field.addFunc("addSnowfall", &SnowField::addSnowfall);
98 field.addFunc("isDirty", &SnowField::isDirty);
99 field.addFunc("clearDirty", &SnowField::clearDirty);
100}
101
102void Snow::expose(ssq::Class &cls) {
103 cls.addFunc("newField", &Snow::newField);
104 cls.addFunc("applyToHeightmap", &Snow::applyToHeightmap);
105 cls.addFunc("uploadTexture", &Snow::uploadTexture);
106 cls.addFunc("updateTexture", &Snow::updateTexture);
107}
108
109} // namespace eve::weather
float w
Definition AnimClip.cpp:738
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
std::string terrain
const std::string & s
HSQOBJECT cls
Definition ECS.cpp:21
int h
std::uint32_t height
std::uint32_t width
TokenKind kind
std::string name
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
virtual bool updateTexture(Texture *texture, int width, int height, const uint8_t *rgba)=0
Replace an existing texture's pixels in place (pointer stays stable).
virtual Texture * newTexture(int width, int height, const uint8_t *rgba, bool repeatU=false, bool repeatV=false)=0
Creates a texture. @ownership Caller deletes unless documented otherwise.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:18
In-memory terrain heightmap: a dense float grid (row-major, index = y * width + x) materialized from ...
Definition Heightmap.h:21
void resize(int width, int height)
Resize.
Definition Heightmap.cpp:10
void setHeight(int x, int y, float h)
Sets the height.
Definition Heightmap.cpp:23
Interactive snow depth field: a dense [0,1] float grid.
Definition SnowField.h:21
bool isDirty() const
True when any cell changed since the last clearDirty().
Definition SnowField.h:45
void clearDirty()
Clears dirty.
Definition SnowField.h:47
int getWidth() const
Returns the width.
Definition SnowField.h:31
int getHeight() const
Returns the height.
Definition SnowField.h:33
void stampFootprint(float cx, float cz, float dirX, float dirZ, float radius, float depth)
Press a footprint into the snow: an ellipse along (dirX, dirZ) with a soft bowl depression and a rais...
Definition SnowField.cpp:56
void resize(int width, int height)
Resize.
Definition SnowField.cpp:28
float height(int x, int y) const
Cell value in [0,1]; out-of-bounds reads 0.
Definition SnowField.cpp:51
void addSnowfall(float amount)
Snowfall recovery: raise every cell toward 1 by amount. Call with dt * snowfallRate each frame; clamp...
void stampImpact(float cx, float cz, float radius, float depth)
Carve an impact crater: a parabolic bowl (deep center, shallow edge) plus a raised snow rim just outs...
void setHeight(int x, int y, float h)
Set one cell (clamped to [0,1]); out-of-bounds is a no-op.
Definition SnowField.cpp:45
void fill(float v)
Set every cell to v (clamped to [0,1]).
Definition SnowField.cpp:39
Interactive snow service owned by the weather module.
Definition Snow.h:40
bool updateTexture(SnowField *field, graphics::Texture *texture, graphics::Graphics *gfx, const std::string &kind)
Replace an uploaded snow texture's pixels in place.
Definition Snow.cpp:70
Snow()
Constructs a Snow.
SnowField * newField(int width, int height)
New empty snow field. @ownership Ownership transfers to the script/native caller. @lifetime Valid unt...
Definition Snow.cpp:31
bool applyToHeightmap(SnowField *field, procgen::Heightmap *terrain, procgen::Heightmap *out, float heightScale)
Compatibility script wrapper around applySnowToHeightmap(). @ownership All pointers are borrowed; no ...
Definition Snow.cpp:35
graphics::Texture * uploadTexture(SnowField *field, graphics::Graphics *gfx, const std::string &kind)
Upload the field as an RGBA8 texture.
Definition Snow.cpp:62
void applySnowToHeightmap(const SnowField &field, const procgen::Heightmap &terrain, procgen::Heightmap &out, float heightScale)
out(x, y) = terrain(x, y) + field(x, y) * heightScale.
Definition Snow.cpp:18