104 struct InternalRecord {
107 glm::vec3 position {};
108 float intensity { 0.0F };
110 float query_radius { 0.0F };
111 std::optional<glm::vec3> color;
112 std::optional<float> size;
113 std::string influence_fn_name;
114 std::string perception_fn_name;
116 float entity_type_norm { 0.0F };
117 float trigger_kind { 0.0F };
118 float time_kind { 0.0F };
120 uint32_t sink_type { 0 };
121 uint32_t first_audio_channel { 0 };
124 std::vector<InternalRecord> records;
126 for (uint32_t
id : fabric.
all_ids()) {
127 const auto k = fabric.
kind(
id);
131 if (!e || !e->position()) {
134 if (e->fn_name().empty()) {
136 "StateEncoder: Emitter {} has no fn_name",
id);
138 auto& rec = records.emplace_back(InternalRecord {
141 .position = *e->position(),
142 .intensity = e->intensity(),
143 .radius = e->radius(),
146 .influence_fn_name = e->fn_name(),
147 .entity_type_norm = 0.0F,
149 fill_wiring_pixels(fabric,
id, rec.trigger_kind, rec.time_kind);
150 rec.sink_type = (e->audio_sinks().empty() ? 0U : 1U)
151 | (e->render_sinks().empty() ? 0U : 2U);
152 if (!e->audio_sinks().empty())
153 rec.first_audio_channel = e->audio_sinks().front().channel;
158 if (!s || !s->position()) {
161 if (s->fn_name().empty()) {
163 "StateEncoder: Sensor {} has no fn_name",
id);
165 auto& rec = records.emplace_back(InternalRecord {
168 .position = *s->position(),
169 .query_radius = s->query_radius(),
170 .perception_fn_name = s->fn_name(),
171 .entity_type_norm = 0.333F,
173 fill_wiring_pixels(fabric,
id, rec.trigger_kind, rec.time_kind);
178 if (!
a || !
a->position()) {
181 if (
a->perception_fn_name().empty()) {
183 "StateEncoder: Agent {} has no perception_fn_name",
id);
185 if (
a->influence_fn_name().empty()) {
187 "StateEncoder: Agent {} has no influence_fn_name",
id);
189 auto& rec = records.emplace_back(InternalRecord {
192 .position = *
a->position(),
193 .intensity =
a->intensity(),
194 .radius =
a->radius(),
195 .query_radius =
a->query_radius(),
198 .influence_fn_name =
a->influence_fn_name(),
199 .perception_fn_name =
a->perception_fn_name(),
200 .entity_type_norm = 0.667F,
202 fill_wiring_pixels(fabric,
id, rec.trigger_kind, rec.time_kind);
203 rec.sink_type = (
a->audio_sinks().empty() ? 0U : 1U)
204 | (
a->render_sinks().empty() ? 0U : 2U);
205 if (!
a->audio_sinks().empty())
206 rec.first_audio_channel =
a->audio_sinks().front().channel;
212 if (records.empty()) {
222 bool init_pos_x =
false, init_pos_y =
false, init_pos_z =
false;
223 bool init_intensity =
false, init_radius =
false, init_query_radius =
false;
224 bool init_color_r =
false, init_color_g =
false, init_color_b =
false;
225 bool init_size =
false;
227 for (
const auto& rec : records) {
228 expand_range(rs.
pos_x, rec.position.x, init_pos_x);
229 expand_range(rs.
pos_y, rec.position.y, init_pos_y);
230 expand_range(rs.
pos_z, rec.position.z, init_pos_z);
233 expand_range(rs.
intensity, rec.intensity, init_intensity);
234 expand_range(rs.
radius, rec.radius, init_radius);
236 expand_range(rs.
color_r, rec.color->r, init_color_r);
237 expand_range(rs.
color_g, rec.color->g, init_color_g);
238 expand_range(rs.
color_b, rec.color->b, init_color_b);
241 expand_range(rs.
size, *rec.size, init_size);
245 expand_range(rs.
query_radius, rec.query_radius, init_query_radius);
252 const auto width =
static_cast<uint32_t
>(records.size());
262 for (
size_t i = 0; i < records.size(); ++i) {
263 const auto& rec = records[i];
286 pixels[row3 + 0] =
static_cast<float>(rec.id);
287 pixels[row3 + 1] = rec.entity_type_norm;
288 pixels[row3 + 2] = rec.trigger_kind;
289 pixels[row3 + 3] = rec.time_kind;
292 pixels[row4 + 0] =
static_cast<float>(rec.sink_type);
293 pixels[row4 + 1] =
static_cast<float>(rec.first_audio_channel);
301 const std::string exr_path = base_path +
".exr";
312 if (!writer->write(exr_path,
image, options)) {
313 m_last_error =
"EXR write failed: " + writer->get_last_error();
325 schema.
entities.reserve(records.size());
327 for (
const auto& rec : records) {
335 ent.
color = rec.color;
339 ent.
wiring = build_wiring(fabric, rec.
id);
343 for (
const auto& s : e->audio_sinks())
344 ent.
audio_sinks.push_back({ .channel = s.channel, .fn_name = s.fn_name });
345 for (
const auto& s : e->render_sinks())
348 auto a = fabric.get_agent(rec.id);
349 for (
const auto& s :
a->audio_sinks())
350 ent.audio_sinks.push_back({ .channel = s.channel, .fn_name = s.fn_name });
352 for (
const auto& s :
a->render_sinks())
353 ent.render_sinks.push_back({ .fn_name = s.fn_name });
355 if (
auto locus = std::dynamic_pointer_cast<Locus>(
a)) {
356 ent.subkind =
"locus";
357 const auto& nav = locus->nav();
358 ent.locus_nav = State::LocusNavRecord {
360 .target = nav.eye + glm::vec3 { std::cos(nav.pitch) * std::sin(nav.yaw), std::sin(nav.pitch), std::cos(nav.pitch) * std::cos(nav.yaw) },
361 .up = { 0.0F, 1.0F, 0.0F },
362 .fov = nav.fov_radians,
363 .near_plane = nav.near_plane,
364 .far_plane = nav.far_plane,
365 .speed = nav.move_speed,
367 }
else if (
auto presence = std::dynamic_pointer_cast<Presence>(
a)) {
368 ent.subkind =
"presence";
369 ent.radiate_fn_name = presence->radiate_fn_name();
370 ent.falloff_radius = presence->falloff_radius() != presence->query_radius()
371 ? std::optional<float>(presence->falloff_radius())
373 if (
auto fc = presence->falloff_curve())
378 schema.entities.push_back(std::move(ent));
381 for (uint32_t xid : fabric.all_expanse_ids()) {
382 const auto x = fabric.get_expanse(xid);
385 if (x->fn_name().empty()) {
387 "StateEncoder: Expanse {} has no fn_name, skipping", xid);
390 schema.expanses.push_back(State::ExpanseRecord {
392 .fn_name = x->fn_name(),
393 .on_enter_fn_name = x->on_enter_fn_name(),
394 .on_exit_fn_name = x->on_exit_fn_name(),
398 IO::JSONSerializer ser;
399 const std::string json_path = base_path +
".json";
400 if (!ser.write(json_path, schema)) {
401 m_last_error =
"Failed to write schema: " + ser.last_error();
407 "StateEncoder: wrote {} entities to {} + {}",
408 records.size(), exr_path, json_path);
413bool StateEncoder::encode(
const Tapestry& tapestry,
const std::string& base_dir, nlohmann::json user_state)
415 m_last_error.clear();
419 for (
const auto& fabric : tapestry.
all_fabrics()) {
420 const std::string fabric_id = fabric->name().empty()
421 ? std::to_string(fabric->id())
424 const std::string base_path = base_dir +
"/" + fabric_id;
426 if (!encode(*fabric, base_path)) {
432 .base_path = base_path,
436 for (
const auto& [xname, xptr] : tapestry.
all_expanses()) {
439 .fn_name = xptr->fn_name(),
440 .on_enter_fn_name = xptr->on_enter_fn_name(),
441 .on_exit_fn_name = xptr->on_exit_fn_name(),
443 for (
const auto& fabric : tapestry.
all_fabrics()) {
444 for (uint32_t xid : fabric->all_expanse_ids()) {
445 if (fabric->get_expanse(xid) == xptr) {
446 const std::string fname = fabric->name().empty()
447 ? std::to_string(fabric->id())
449 xrec.fabric_names.push_back(fname);
454 schema.
expanses.push_back(std::move(xrec));
460 const std::string tapestry_path = base_dir +
"/tapestry.json";
461 if (!ser.
write(tapestry_path, schema)) {
462 m_last_error =
"Failed to write tapestry schema: " + ser.
last_error();
468 "StateEncoder: wrote {} fabrics to {}", schema.
fabrics.size(), tapestry_path);