载入中...
搜索中...
未找到
HeightmapTargetModule.cpp
浏览该文件的文档.
2
10
11#include <simplesquirrel/simplesquirrel.hpp>
12
13#include <cstdint>
14#include <functional>
15#include <utility>
16
18
20
21std::unique_ptr<procgen_editing::HeightmapTarget> HeightmapTargetModule::newTarget(
22 std::string id, procgen::Heightmap* heightmap) const {
23 return procgen_editing::createHeightmapTarget(std::move(id), heightmap);
24}
25
29
30int HeightmapTargetModule::applyBrush(procgen::Heightmap* heightmap, float centerX, float centerY, float radius,
31 float strength) const {
32 auto result = procgen_editing::applyHeightmapBrush(heightmap, centerX, centerY, radius, strength);
33 return result.ok() ? result.value() : 0;
34}
35
36graphics::Mesh* HeightmapTargetModule::newMesh(procgen::Heightmap* heightmap, float cellSize, float heightScale) const {
37 auto result = procgen_graphics_editing::createHeightmapMesh(heightmap, cellSize, heightScale, false);
38 return result.ok() ? result.value() : nullptr;
39}
40
42 procgen::Heightmap* heightmap, float cellSize, float heightScale) const {
43 return procgen_graphics_editing::updateHeightmapMesh(mesh, graphics, heightmap, cellSize, heightScale, false).ok();
44}
45
47 float heightScale) const {
48 auto result = procgen_graphics_editing::createHeightmapMesh(heightmap, cellSize, heightScale, true);
49 return result.ok() ? result.value() : nullptr;
50}
51
53 procgen::Heightmap* heightmap, float cellSize, float heightScale) const {
54 return procgen_graphics_editing::updateHeightmapMesh(mesh, graphics, heightmap, cellSize, heightScale, true).ok();
55}
56
58 float spacingZ) const {
59 if (!heightmap)
61 "terrain document requires a heightmap", {}, {},
62 "heightmap-target.document"));
63 auto encoded = encodeTerrainDocument(*heightmap, spacingX, spacingZ);
64 if (!encoded) return Result<std::string>::failure(encoded.status());
65 return Result<std::string>::success(editor::editorValueToJson(encoded.value()));
66}
67
69 if (!heightmap)
71 "terrain document requires a destination heightmap", {}, {},
72 "heightmap-target.document"));
73 auto parsed = editor::editorValueFromJson(std::string(json));
74 if (!parsed) return Result<void>::failure(parsed.status());
75 auto decoded = decodeTerrainDocument(parsed.value());
76 if (!decoded) return Result<void>::failure(decoded.status());
77 *heightmap = std::move(decoded).takeValue().heightmap;
78 return Result<void>::success();
79}
80
81void HeightmapTargetModule::expose(ssq::Table& table) {
82 auto target = table.addClass<procgen_editing::HeightmapTarget>(
83 "HeightmapTarget",
84 std::function<procgen_editing::HeightmapTarget*()>(
85 []() -> procgen_editing::HeightmapTarget* { return nullptr; }),
86 true);
87 target.addFunc("getTargetId", [](procgen_editing::HeightmapTarget* self) {
88 return self ? self->targetId().value() : std::string{};
89 });
90 target.addFunc("getRevision", [](procgen_editing::HeightmapTarget* self) {
91 return self ? static_cast<int64_t>(self->revision()) : int64_t{0};
92 });
99
100 auto module = table.addClass(name, HeightmapTargetModule::create, false);
101 expose(module);
102}
103
104void HeightmapTargetModule::expose(ssq::Class& cls) {
105 cls.addFunc("create", [](HeightmapTargetModule* self, const std::string& id, procgen::Heightmap* heightmap) {
106 return self->newTarget(id, heightmap).release();
107 });
108 cls.addFunc("bind", &HeightmapTargetModule::bind);
109 cls.addFunc("applyBrush", &HeightmapTargetModule::applyBrush);
110 cls.addFunc("newMesh", &HeightmapTargetModule::newMesh);
111 cls.addFunc("updateMesh", &HeightmapTargetModule::updateMesh);
112 cls.addFunc("newSmoothMesh", &HeightmapTargetModule::newSmoothMesh);
113 cls.addFunc("updateSmoothMesh", &HeightmapTargetModule::updateSmoothMesh);
114 cls.addFunc("encodeDocument", [vm = cls.getHandle()](HeightmapTargetModule* self, procgen::Heightmap* heightmap,
115 float spacingX, float spacingZ) {
116 if (!self)
117 return eve::script::projectResult(
118 vm,
119 Result<std::string>::failure(
120 Diagnostic::error(DiagnosticCode::InvalidArgument, "heightmap target module is unavailable")),
121 [](std::string json) { return eve::Value(std::move(json)); });
122 return eve::script::projectResult(vm, self->encodeDocument(heightmap, spacingX, spacingZ),
123 [](std::string json) { return eve::Value(std::move(json)); });
124 });
125 cls.addFunc("decodeDocument", [vm = cls.getHandle()](HeightmapTargetModule* self, const std::string& json,
126 procgen::Heightmap* heightmap) {
127 if (!self)
130 Diagnostic::error(DiagnosticCode::InvalidArgument, "heightmap target module is unavailable")));
131 return eve::script::projectResult(vm, self->decodeDocument(json, heightmap));
132 });
133}
134
135} // namespace eve::heightmap_target
LogicalId target
double value
HSQUIRRELVM vm
Definition ECS.cpp:20
HSQOBJECT cls
Definition ECS.cpp:21
#define Module_IMPL(ModuleName, newExpr)
Definition Module.h:26
float radius
std::function< eve::Result< void >()> release
Definition Procgen.cpp:86
Mesh * mesh
std::string string
The single Squirrel projection for common Result, Status and Value.
std::optional< InteractionSession > session
Definition Tactics.cpp:39
Versioned editor persistence for editable heightfields.
double spacingZ
double spacingX
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={}, DiagnosticDetails details={}, std::string source={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.h:125
Move-only operation result carrying either a value or Status.
Definition Result.h:155
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:164
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:175
Hosts interchangeable editor tools and routes their lifecycle/input.
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:25
Script-facing composition module for live heightmap editing targets.
int applyBrush(procgen::Heightmap *heightmap, float centerX, float centerY, float radius, float strength) const
Compatibility projection of the canonical structured brush result for scripts.
std::unique_ptr< procgen_editing::HeightmapTarget > newTarget(std::string id, procgen::Heightmap *heightmap) const
Create an independently owned adapter over a borrowed heightmap.
Result< std::string > encodeDocument(const procgen::Heightmap *heightmap, float spacingX, float spacingZ) const
Encode an editable heightmap as the versioned terrain editor JSON document.
void bind(editor::EditorSession *session, procgen_editing::HeightmapTarget *target) const
Bind borrowed target and session objects without taking ownership. @thread Owner-thread only.
Result< void > decodeDocument(std::string_view json, procgen::Heightmap *heightmap) const
Atomically replace a heightmap from a validated terrain editor JSON document.
bool updateSmoothMesh(graphics::Mesh *mesh, graphics::Graphics *graphics, procgen::Heightmap *heightmap, float cellSize, float heightScale) const
Compatibility projection that updates a smooth-normal preview mesh. @thread Render-thread only.
graphics::Mesh * newSmoothMesh(procgen::Heightmap *heightmap, float cellSize, float heightScale) const
Compatibility projection that creates a smooth-normal preview mesh. @thread Render-thread only.
graphics::Mesh * newMesh(procgen::Heightmap *heightmap, float cellSize, float heightScale) const
Compatibility projection that creates a flat-shaded preview mesh. @thread Render-thread only.
bool updateMesh(graphics::Mesh *mesh, graphics::Graphics *graphics, procgen::Heightmap *heightmap, float cellSize, float heightScale) const
Compatibility projection that updates a flat-shaded preview mesh. @thread Render-thread only.
In-memory terrain heightmap: a dense float grid (row-major, index = y * width + x) materialized from ...
Definition Heightmap.h:21
Non-owning scalar-field adapter for a live procedural heightmap.
float readScalar(int x, int y) const override
Reads scalar.
int height() const override
Height.
void clearDirtyRegion() override
Clears dirty region.
editing::TargetId targetId() const override
Target id.
editing::FieldWriteStatus writeScalar(int x, int y, float value) override
Writes scalar.
float sampleScalar(float x, float y) const override
Sample scalar.
editor::Result< TerrainDocumentData > decodeTerrainDocument(const editor::EditorValue &value, const TerrainDocumentLimits &limits)
Validate and atomically decode one terrain editor snapshot.
editor::Result< editor::EditorValue > encodeTerrainDocument(const procgen::Heightmap &heightmap, float spacingX, float spacingZ, const TerrainDocumentLimits &limits)
Encode the current authoritative heightmap as eve.terrain-editor-document/1.
EVENGINE_API_ORCHESTRATION editing::Result< int > applyHeightmapBrush(procgen::Heightmap *heightmap, float centerX, float centerY, float radius, float strength)
Apply the legacy circular brush through the procgen authoring satellite.
std::unique_ptr< HeightmapTarget > createHeightmapTarget(std::string id, procgen::Heightmap *heightmap)
Create a heightmap adapter.
editing::Result< graphics::Mesh * > createHeightmapMesh(procgen::Heightmap *heightmap, float cellSize, float heightScale, bool smoothNormals)
Build a heightmap preview mesh through the linked Graphics module.
editing::Result< void > updateHeightmapMesh(graphics::Mesh *mesh, graphics::Graphics *graphics, procgen::Heightmap *heightmap, float cellSize, float heightScale, bool smoothNormals)
Update preview vertices without replacing the mesh identity.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.