载入中...
搜索中...
未找到
GpuAgentsPreview.cpp
浏览该文件的文档.
2
6
7#include <algorithm>
8#include <cmath>
9#include <memory>
10
11namespace eve::gpuagents_editor {
12
14 const GpuAgentsPreviewRequest& request) const {
16 out.documentRevision = target.describe().revision;
17 out.diagnostics = target.validate();
18 if (std::any_of(out.diagnostics.begin(), out.diagnostics.end(),
19 [](const auto& d) { return d.severity() == gpuagents_editing::DiagnosticSeverity::Error; })) {
20 out.status = EditorStatus::Failed;
21 return out;
22 }
23 if (request.agentCount <= 0 || request.seconds < 0.0 || request.fixedStep <= 0.0 || request.maximumSteps == 0) {
24 out.status = EditorStatus::Failed;
26 eve::DiagnosticCode::InvalidArgument, gpuagents_editing::RuleId("editor.gpuagents.preview.request"),
27 gpuagents_editing::DiagnosticSeverity::Error, "Invalid GPU Agents preview request"));
28 return out;
29 }
30
32 auto backend = std::make_unique<gpuagents::EffectBackend>();
34 auto applied = applier.apply(target, backend.get(), &world);
35 if (!applied.ok()) {
36 out.status = EditorStatus::Failed;
38 eve::DiagnosticCode::Failed, gpuagents_editing::RuleId("editor.gpuagents.preview.apply"),
39 gpuagents_editing::DiagnosticSeverity::Error, "Failed to apply GPU Agents document to preview runtime"));
40 return out;
41 }
42
43 const int count = std::min(request.agentCount, backend->simulation().capacity());
44 auto spawned = backend->simulation().initialize(count, [&](int i) {
46 const float a = static_cast<float>(i) * 0.6180339f;
47 const float r = static_cast<float>(request.spawnSpread) * (0.2f + 0.8f * std::fmod(a, 1.f));
48 s.position = {static_cast<float>(request.centerX) + std::cos(a * 6.2831853f) * r,
49 static_cast<float>(request.centerY),
50 static_cast<float>(request.centerZ) + std::sin(a * 6.2831853f) * r};
51 s.velocity = {std::cos(a), 0.f, std::sin(a)};
52 if (target.settings().kind == "Petal") {
53 s.velocity = {0.f, -1.f, 0.f};
54 s.customData[1] = 1.f;
55 s.position.y += 2.f;
56 }
57 if (target.settings().kind == "LifeNetwork") s.position.y = static_cast<float>(request.centerY);
58 return s;
59 });
60 if (!spawned.ok()) {
61 out.status = EditorStatus::Failed;
62 return out;
63 }
64
65 auto registered = world.registerBackend("preview", backend.get());
66 if (!registered.ok()) {
67 out.status = EditorStatus::Failed;
68 return out;
69 }
70 const double step = request.fixedStep;
71 std::uint32_t steps = 0;
72 double t = 0.0;
73 while (t + 1e-12 < request.seconds && steps < request.maximumSteps) {
74 auto stepped = world.stepAll(static_cast<float>(step));
75 if (!stepped.ok()) {
76 out.status = EditorStatus::Failed;
77 return out;
78 }
79 t += step;
80 ++steps;
81 }
82 out.simulatedSeconds = t;
83 for (const auto& s : backend->simulation().states()) {
84 if (!s.alive) continue;
86 a.x = s.position.x;
87 a.y = s.position.y;
88 a.z = s.position.z;
89 a.vx = s.velocity.x;
90 a.vy = s.velocity.y;
91 a.vz = s.velocity.z;
92 a.age = s.age;
93 a.custom0 = s.customData[0];
94 out.agents.push_back(a);
95 }
96 if (target.settings().kind == "LifeNetwork") {
97 for (const auto& c : world.surface().lifeField) out.lifeTrailEnergy += c.r;
98 }
99 out.status = EditorStatus::Applied;
100 return out;
101}
102
103} // namespace eve::gpuagents_editor
LogicalId target
const std::string & s
const GltfImportRequest & request
std::uint32_t spawned
double r
std::int32_t c
MeleePoint3 a
Definition MeleeHit.cpp:40
World3D * world
float d
int steps
float t
std::uint32_t count
float step
Definition TreeMesh.cpp:314
UUID-backed identifier adapter for legacy textual boundaries.
Definition Identity.h:314
World-level registry for GPU agent simulations and environment semantics.
Reversible document for GPU Agents effect authoring.
Optional bridge projecting a validated document into live World/Backend. @ownership Borrowed world/ba...
Result< void > apply(const GpuAgentsDocumentTarget &target, gpuagents::EffectBackend *backend, gpuagents::GpuAgentWorld *world) const
Configure backend from document and optionally re-init the world surface domain.
GpuAgentsPreviewSnapshot build(const GpuAgentsDocumentTarget &target, const GpuAgentsPreviewRequest &request) const
Validate, spawn, step and capture a renderer-neutral preview frame.
Diagnostic ruleDiagnostic(eve::DiagnosticCode code, RuleId rule, DiagnosticSeverity severity, std::string message)
Build a common diagnostic carrying an open editing rule identity.
Standardized per-agent simulation output shared by all effect kinds.
Definition AgentState.h:12
One agent sample in a preview snapshot.
Deterministic scrub request for editor preview.
Revision-bound preview snapshot with optional life-field energy.
std::vector< EditorDiagnostic > diagnostics
std::vector< GpuAgentsPreviewAgent > agents