载入中...
搜索中...
未找到
HexSphereTopology.cpp
浏览该文件的文档.
29[[nodiscard]] constexpr float dot(Vec a, Vec b) noexcept { return a.x * b.x + a.y * b.y + a.z * b.z; }
54 {-1.f, kGoldenRatio, 0.f}, {1.f, kGoldenRatio, 0.f}, {-1.f, -kGoldenRatio, 0.f}, {1.f, -kGoldenRatio, 0.f},
55 {0.f, -1.f, kGoldenRatio}, {0.f, 1.f, kGoldenRatio}, {0.f, -1.f, -kGoldenRatio}, {0.f, 1.f, -kGoldenRatio},
56 {kGoldenRatio, 0.f, -1.f}, {kGoldenRatio, 0.f, 1.f}, {-kGoldenRatio, 0.f, -1.f}, {-kGoldenRatio, 0.f, 1.f},
112 for (const auto& vertex : kIcosahedronVertices) vertices.push_back(normalize(Vec{vertex[0], vertex[1], vertex[2]}));
116 for (const auto& face : kIcosahedronFaces) triangles.push_back(Triangle{face[0], face[1], face[2]});
119 cross(vertices[triangle.b] - vertices[triangle.a], vertices[triangle.c] - vertices[triangle.a]);
165 cornerDirections[i] = normalize(topology.directions_[triangle.a] + topology.directions_[triangle.b] +
197 const std::int32_t pairs[3][2] = {{triangle.a, triangle.b}, {triangle.b, triangle.c}, {triangle.c, triangle.a}};
239 if (degree < 5 || degree > 6) return sphereFailure("hex sphere cell has an unsupported edge count");
311HexSphereCell HexSphereTopology::neighbor(HexSphereCell cell, std::int32_t direction) const noexcept {
316std::int32_t HexSphereTopology::directionOf(HexSphereCell cell, HexSphereCell other) const noexcept {
326std::int32_t HexSphereTopology::oppositeDirection(HexSphereCell cell, std::int32_t direction) const noexcept {
337std::int32_t HexSphereTopology::cornerCountOf(HexSphereCell cell) const noexcept { return neighborCount(cell); }
424[[nodiscard]] Result<std::vector<HexCellData>> unpackCells(std::string_view packed, std::size_t expected) {
444 if (field->isInt64()) return Result<std::int32_t>::success(static_cast<std::int32_t>(field->asInt()));
510 if (kindField == nullptr || !kindField->isString()) return invalidBake("hex terrain bake is missing string 'kind'");
557 Diagnostic::error(DiagnosticCode::InvalidArgument, "applyHexTerrain requires a planar bake", "hexmap"));
560 Diagnostic::error(DiagnosticCode::InvalidArgument, "planar bake size must be positive", "hexmap"));
563 DiagnosticCode::InvalidArgument, "planar bake cell count does not match width and height", "hexmap"));
582 DiagnosticCode::InvalidArgument, "applyHexSphereTerrain requires an existing sphere topology", "hexmap"));
585 Diagnostic::error(DiagnosticCode::InvalidArgument, "sphere bake cell count is inconsistent", "hexmap"));
622 const HexVec3 normal{ab.y * ac.z - ab.z * ac.y, ab.z * ac.x - ab.x * ac.z, ab.x * ac.y - ab.y * ac.x};
636void emitTriangle(HexMeshData& out, const SurfaceVertex& a, const SurfaceVertex& b, const SurfaceVertex& c,
642 out.addTriangle(out.addVertex(pa, a.u, a.v), out.addVertex(pb, b.u, b.v), out.addVertex(pc, c.u, c.v));
653void emitQuad(HexMeshData& out, const SurfaceVertex& r0, const SurfaceVertex& r1, const SurfaceVertex& r2,
660[[nodiscard]] float waterHeightOf(const HexMap& map, HexCoordinates coordinates, float offGridHeight) noexcept {
661 return map.contains(coordinates) ? HexMetrics::waterSurfaceY(map.waterLevel(coordinates)) : offGridHeight;
689void appendOpenWater(const HexMap& map, HexCoordinates coordinates, HexDirection direction, HexVec3 centre,
712 if (!map.getNeighbor(coordinates, next(direction), nextNeighbour) || !isWater(map, nextNeighbour)) return;
716 HexVec3 corner = map.cellGroundPosition(nextNeighbour) + HexMetrics::firstWaterCorner(previous(direction));
742void appendWaterShore(const HexMap& map, HexCoordinates coordinates, HexDirection direction, HexVec3 centre,
747 const SurfaceVertex nearSamples[5] = {SurfaceVertex{near.v1, 0.f, 0.f}, SurfaceVertex{near.v2, 0.f, 0.f},
763 const SurfaceVertex farSamples[5] = {SurfaceVertex{far.v1, 1.f, 0.f}, SurfaceVertex{far.v2, 1.f, 0.f},
800 const HexVec3 centre{ground.x, HexMetrics::waterSurfaceY(map.waterLevel(coordinates)), ground.z};
Stable, structured diagnostics shared by engine modules.
Per-chunk mesh generation for every hex map surface.
CPU vertex/index container shared by the hex surface builders.
Pointy-top hex metrics, directions and vertex helpers.
Deterministic asset-free noise for hex perturbation and generation.
Icosahedral hex topology: twelve pentagons and hexagons on a sphere.
Terrain bake hexmap applies; procgen produces the payload.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
const std::string & asString() const
Return the string payload; the caller must have checked the kind.
Definition Value.cpp:207
bool isNumeric() const noexcept
Return whether this value is an integer or finite number.
Definition Value.h:188
double asDouble() const
Return the Double payload; the caller must have checked the kind.
Definition Value.cpp:195
void set(const std::string &key, Value value)
Insert or replace an object member.
Definition Value.cpp:238
std::int64_t asInt() const
Return the Int64 payload; the caller must have checked the kind.
Definition Value.cpp:189
Value * find(const std::string &key) noexcept
Return an object member, or nullptr when absent.
Definition Value.cpp:244
std::int32_t waterLevel(HexCoordinates c) const noexcept
Definition HexMap.cpp:304
HexCoordinates coordinatesAt(std::int32_t index) const noexcept
Coordinates of a linear cell index; out-of-range indices return (0, 0).
Definition HexMap.cpp:69
Result< void > setCellState(HexCoordinates c, HexValues values, HexFlags flags)
Writes a complete cell record verbatim.
Definition HexMap.cpp:505
const HexNoise & noise() const noexcept
Deterministic noise field shared by this map's mesh builders.
Definition HexMap.h:110
bool getNeighbor(HexCoordinates coordinates, HexDirection direction, HexCoordinates &out) const noexcept
Neighbour of coordinates in direction; false when it is off-grid.
Definition HexMap.cpp:87
bool isUnderwater(HexCoordinates c) const noexcept
Definition HexMap.cpp:328
Result< void > reset(std::int32_t cellCountX, std::int32_t cellCountZ, std::uint32_t seed)
Replaces the grid with a new cellCountX * cellCountZ map.
Definition HexMap.cpp:37
const HexCellData * cell(HexCoordinates coordinates) const noexcept
Cell record, or null when outside the grid. @ownership Borrowed; the map owns the record....
Definition HexMap.cpp:79
const HexCellData * cellAt(std::int32_t index) const noexcept
Cell record by linear index, or null when out of range. @ownership Borrowed; the map owns the record....
Definition HexMap.cpp:74
HexVec3 cellGroundPosition(HexCoordinates coordinates) const noexcept
Cell centre at y = 0 (no elevation, no perturbation).
Definition HexMap.h:198
HexCoordinates chunkCell(std::int32_t chunkIndex, std::int32_t column, std::int32_t row) const noexcept
Coordinates of the cell at (column, row) inside a chunk.
Definition HexMap.cpp:115
CPU-side triangle soup for one hex chunk surface.
Definition HexMeshData.h:24
static HexVec3 firstSolidCorner(HexDirection d) noexcept
First solid corner of direction.
Definition HexMetrics.h:202
static HexVec3 secondSolidCorner(HexDirection d) noexcept
Second solid corner of direction.
Definition HexMetrics.h:204
static HexVec3 secondWaterCorner(HexDirection d) noexcept
Second water corner of direction.
Definition HexMetrics.h:212
static float waterSurfaceY(int waterLevel) noexcept
Y coordinate of a water surface at waterLevel.
Definition HexMetrics.h:273
static HexVec3 firstWaterCorner(HexDirection d) noexcept
First water corner of direction.
Definition HexMetrics.h:210
static HexVec3 waterBridge(HexDirection d) noexcept
Bridge vector from the water edge of direction to the neighbour.
Definition HexMetrics.h:218
Deterministic four-channel value noise over the XZ plane.
Definition HexNoise.h:23
std::int32_t subdivision() const noexcept
The subdivision level this map was built at.
Definition HexSphereMap.h:87
const HexCellData * cellAt(HexSphereCell cell) const noexcept
Cell record by id, or null when out of range. @ownership Borrowed; the map owns the record....
Definition HexSphereMap.cpp:114
Result< void > setCellState(HexSphereCell c, HexValues values, HexFlags flags)
Writes a complete cell record verbatim.
Definition HexSphereMap.cpp:501
Icosahedral (Goldberg) hex topology for a spherical hex map.
Definition HexSphereTopology.h:54
std::int32_t subdivision() const noexcept
The subdivision level this topology was built at.
Definition HexSphereTopology.h:76
static Result< HexSphereTopology > build(std::int32_t subdivision)
Builds the topology of one subdivision level.
Definition HexSphereTopology.cpp:100
bool isInt64() const
True when the JSON number was an integer literal that fits Int64.
Definition Json.cpp:329
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
Definition BuildingSnapshot.cpp:96
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
Definition HexCell.h:9
HexTerrainBake snapshotHexSphereTerrain(const HexSphereMap &map)
Snapshot every cell of a spherical map into a bake.
Definition HexSphereTopology.cpp:475
constexpr HexDirection opposite(HexDirection d) noexcept
The direction opposite to d.
Definition HexMetrics.h:65
Result< void > applyHexTerrain(HexMap &map, const HexTerrainBake &bake)
Replace a planar map's cells with a bake.
Definition HexSphereTopology.cpp:554
Result< HexTerrainBake > hexTerrainBakeFromValue(const Value &value)
Parse a script bake payload produced by hexTerrainBakeToValue.
Definition HexSphereTopology.cpp:507
HexTerrainBake snapshotHexTerrain(const HexMap &map)
Snapshot every cell of a planar map into a bake.
Definition HexSphereTopology.cpp:460
constexpr std::int32_t kHexDirectionCount
Number of hex edges / facing directions.
Definition HexMetrics.h:62
constexpr std::int32_t kMaxHexSphereSubdivision
Largest supported subdivision level; 10 * 4^7 + 2 = 163842 cells.
Definition HexSphereTopology.h:22
EVENGINE_API_WORLD void buildWaterMesh(const HexMap &map, std::int32_t chunkIndex, HexMeshData &out)
Builds the water surface and shore band of one chunk.
Definition HexSphereTopology.cpp:785
constexpr HexSphereCell kNoHexSphereCell
Returned by a spherical cell query that has no answer.
Definition HexSphereTopology.h:19
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
Result< void > applyHexSphereTerrain(HexSphereMap &map, const HexTerrainBake &bake)
Replace a spherical map's cells with a bake.
Definition HexSphereTopology.cpp:576
std::int32_t HexSphereCell
Dense identifier of one cell of a spherical hex topology.
Definition HexSphereTopology.h:16
Value hexTerrainBakeToValue(const HexTerrainBake &bake)
Project a bake into the script Result payload.
Definition HexSphereTopology.cpp:490
@ InvalidArgument
One cell of the hex map: packed values plus packed flags.
Definition HexCell.h:244
Packed terrain produced by a generator and applied onto a hex map.
Definition HexTerrainBake.h:28
Minimal 3-component float vector used by the hex mesh builders.
Definition HexMetrics.h:18