17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-17 15:39:20 +02:00
Files
maszyna/scene/prefab.cppm
maj00r 2660d14565 Stream the scenery: a baked terrain heightfield, a logic container, and prefabs
Today's work on the binary scene format plan (binary-scene-format-plan.md), from the
first measurement to a terrain that bakes itself on first run and streams from a
thread. The step-by-step history is kept on backup/scene-streaming-format-presquash-0916.

Measuring first. -loadprofile times a scenery load by stage and by node kind and reports
node counts and resident memory. On l204 terrain is 95% of the bytes and 99.8% of the
nodes; on quarkmce2007 vehicles, not models, dominate what is left. The terrain turned
out to be a heightfield in all but name, a ribbon covering 12% of its bounding box.

The heightfield. scene/heightfieldformat.h is the .ehf layout: Morton-ordered tiles
of 128 samples with one shared edge sample, a point-sampled mip chain, zstd per tile,
a nodata sentinel, a material palette with a measured tiling per material, and a stamp
of the rules that decided what counted as terrain. scene/heightfieldreader.h reads it
for the tool, the renderer and the loader alike, and scene/terraincooker.h writes it,
shared by the terraincook tool and the engine so a first-run bake is the file the tool
would have made. The cooker rasterises every triangle where it is drawn (through
scene::place_in_world, the placement the region applies too), keeps an upper surface
laid within half a metre over another and the lower of two further apart, and then
asks each triangle whether the heightfield shows it: laid, reaching a sample, keeping
nearly all it claimed, and with the ground around it drawn at every level out to 2 km.

Baking on first run. simulation/terrainbake bakes <scenario>.ehf the first time a
scenery is opened, choosing the grid step from the first 200,000 vertices, and on later
loads leaves out the triangles its node table says the heightfield shows. Everything
else - trees, grass tufts, gantries, bridge decks - is drawn as written, so a bad bake
can at worst draw something twice, and the heightfield sits 2 cm under the triangles so
the original wins there. Nodes are keyed by file, line, include parameters and
placement; an edited file changes its keys and those nodes are drawn again. A bake under
older rules, or without its table, is thrown away and redone. A launcher copy named
$scenario shares the scenario's files. -nobaketerrain shows the triangles as written.
A scenery can still name hand-cooked fields, several at different steps.

Drawing and streaming. rendering/terrainclipmap draws resident tiles at the mip level
their distance earns, deriving the grid from gl_VertexID and texturing in world space
with the scenery's own materials. rendering/terraintileloader reads and decodes tiles on
a thread of its own; the renderer rescans what is in range only after moving a quarter
of a tile, asks for what is missing nearest first, uploads at most 32 tiles a frame,
drops arrivals the camera has moved past, and keeps drawing a tile at the level it has
until the right one arrives. The terrain has its own range, terrainrange in the ini and
a slider in the debug panel, 20 km by default and independent of basedrawrange; the far
plane of an ordinary view is pushed out to it. The debug panel shows the range, tiles in
view, on the gpu, wanted and queued, and how many sit at each level.

Events. The event queue moved out of the events into a binary heap with a deterministic
tie-break, and an event is handed its activator instead of storing it, so basic_event
is a definition and nothing else.

The logic container. scene/logicformat.h and scene/logiccontainer write a scenery's
events, their targets and sibling chains as flat, deterministic arrays with a schema
hash that refuses a mismatched build. simulation/logiccook cooks it from the loaded
state whenever it is missing, stale or unreadable, reads it straight back to check it,
and resolves each target to a position in the registry its event kind searches.
-cooklogic forces a cook, -verifylogic only checks. Nothing reads it at load yet.

Prefabs. scene/prefab describes a signal by the aspects it can show - lights, speeds,
animation channels, a sound - instead of pasting an .inc per variant, and expands an
instance to ordinary scenery text. Tried on a light signal and a shape signal.

Screenshots without a person present: -shot x,y,z[,yaw[,pitch]], -shotsettle, -shotstay.
2026-09-16 23:18:09 +02:00

119 lines
4.5 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
module;
#include <string>
#include <unordered_map>
#include <vector>
export module eu07.scene.prefab;
export {
// Shared archetypes. A prefab is a model plus the events, memcell, animation channels and
// sounds that give it behaviour, described once and instanced many times. It exists because
// the .inc templates it replaces are not templates at all: cParser pastes text and
// substitutes (pN), so a signal that differs only by which aspects it can display needs a
// whole new file. The scenery directory holds over twelve hundred of them.
//
// What makes one prefab enough for the whole signal family is that its parameter is a list,
// not a number. Every signal template is the same shape - a state event, a light pattern and
// a speed the driver reads, for each aspect it can display, wrapped in a fixed core of
// _sem_info, _uszk and the dark state. Give the prefab the aspects and the rest follows. A
// shape signal adds animation channels to each aspect and a sound to the transition, which
// is more per aspect but not a different shape.
//
// Instances expand to scenery text and go through the ordinary node and event parsing. The
// binary format will compile them directly; until then this keeps one description of the
// signal family instead of one per variant.
namespace scene {
// one submodel driven by an aspect. kind is the word the animation event takes, translate or
// rotate; target is where the submodel goes, in metres or degrees; speed is how fast
struct prefab_animation {
std::string kind;
std::string submodel;
std::string x{ "0" }, y{ "0" }, z{ "0" };
std::string speed{ "0" };
};
// one thing the signal can show. lights is the pattern the model's light channels take, one
// entry per channel, -1 leaving a channel alone; velocity is what the driver reads from the
// memcell, -1 meaning no limit
struct prefab_aspect {
std::string name;
std::vector<int> lights;
std::string lightdelay{ "0.0" };
std::string velocity{ "-1" };
std::string velocitynext{ "-1" };
std::vector<prefab_animation> animations;
};
// a second model hung on the instance: the lattice mast a shape signal stands on, its
// distant lod, the plate carrying the signal's symbol
struct prefab_attachment {
std::string rangemax{ "50" };
std::string rangemin{ "0" };
std::string model;
// the instance's own texture when "instance", otherwise a literal, "none" included
std::string texture{ "none" };
};
struct prefab_definition {
std::string name;
std::string model;
std::string modelangle{ "0" };
std::vector<prefab_attachment> attachments;
// how many light channels the model has, which is how many values an aspect's pattern holds
std::size_t chambers{ 0 };
std::vector<prefab_aspect> aspects;
// aspect the signal shows before anything sets it, by name; empty lights every channel off
std::string initial;
// the node's own starting pattern, when it is not simply the initial aspect: a shape
// signal starts with channel zero on automatic, which is not a thing any aspect says
std::vector<int> initiallights;
// played when the signal changes aspect. shape signals have one, light signals do not
std::string sound;
// emit the _dzienny / _nocny / _auto events which switch channel zero between day,
// night and automatic lighting. only models with that channel want them
bool lightmodes{ false };
};
// arguments one instance supplies
struct prefab_placement {
std::string name;
double x{ 0.0 }, y{ 0.0 }, z{ 0.0 };
double yaw{ 0.0 };
std::string texture;
};
class prefab_registry {
public:
// looks the definition up, loading <Name>.pfb from the scenery directory on first use.
// returns nullptr and logs if there is no such prefab
prefab_definition const * definition( std::string const &Name );
// scenery text realising the instance: the model node, its attachments, memcell, sound
// and events. empty if the definition is missing
std::string expand( std::string const &Name, prefab_placement const &Placement );
private:
bool load( std::string const &Name );
std::unordered_map<std::string, prefab_definition> m_definitions;
// names which failed to load, so a scenery full of them complains once each
std::unordered_map<std::string, bool> m_missing;
};
} // namespace scene
inline scene::prefab_registry Prefabs;
}