载入中...
搜索中...
未找到
TileMap3D.cpp
浏览该文件的文档.
1#include "hd2d/Hd2d.h"
2
3#include "common/Exception.h"
4#include "graphics/Graphics.h"
5#include "graphics/Mesh.h"
7#include "graphics/Texture.h"
8#include "map/TileLayer.h"
9
10#include <algorithm>
11#include <cmath>
12#include <cstdint>
13#include <glm/glm.hpp>
14#include <vector>
15
16namespace eve::hd2d {
17namespace {
18
19constexpr float kPi = 3.14159265358979323846f;
20
26void atlasUvForGid(const map::TileLayer::Tileset &set, uint32_t gid, float &u0, float &v0,
27 float &u1, float &v1) {
28 const int cols = std::max(1, set.columns);
29 const int tileW = std::max(1, set.tileW);
30 const int tileH = std::max(1, set.tileH);
31 const int margin = std::max(0, set.margin);
32 const int spacing = std::max(0, set.spacing);
33 const int cell = int(gid) - set.firstGid;
34 if (cell < 0) {
35 u0 = 0.f;
36 v0 = 0.f;
37 u1 = 1.f;
38 v1 = 1.f;
39 return;
40 }
41 const int cx = cell % cols;
42 const int cy = cell / cols;
43 const float texW = set.texture ? float(std::max(1, set.texture->getWidth())) : float(tileW);
44 const float texH = set.texture ? float(std::max(1, set.texture->getHeight())) : float(tileH);
45 const float px0 = float(margin + cx * (tileW + spacing));
46 const float py0 = float(margin + cy * (tileH + spacing));
47 u0 = px0 / texW;
48 v0 = py0 / texH;
49 u1 = (px0 + float(tileW)) / texW;
50 v1 = (py0 + float(tileH)) / texH;
51}
52
53} // namespace
54
55// ---------------------------------------------------------------------------
56// TileMap3D
57// ---------------------------------------------------------------------------
58
60 if (depth < 0.f) throw eve::Exception("TileMap3D.setSideDepth: depth must be >= 0");
61 sideDepth_ = depth;
62}
63float TileMap3D::getSideDepth() const { return sideDepth_; }
65 if (scale < 0.f) throw eve::Exception("TileMap3D.setHeightScale: scale must be >= 0");
66 heightScale_ = scale;
67}
68float TileMap3D::getHeightScale() const { return heightScale_; }
69void TileMap3D::setWallUV(float u0, float v0, float u1, float v1) {
70 wallU0_ = u0;
71 wallV0_ = v0;
72 wallU1_ = u1;
73 wallV1_ = v1;
74}
75void TileMap3D::setTint(float r, float g, float b, float a) {
76 tintR_ = std::max(0.f, std::min(1.f, r));
77 tintG_ = std::max(0.f, std::min(1.f, g));
78 tintB_ = std::max(0.f, std::min(1.f, b));
79 tintA_ = std::max(0.f, std::min(1.f, a));
80}
81
83 if (!gfx) throw eve::Exception("TileMap3D.buildMesh: null graphics");
84 if (!layer) throw eve::Exception("TileMap3D.buildMesh: null tile layer");
85
86 const int mapW = layer->getMapWidth();
87 const int mapH = layer->getMapHeight();
88 if (mapW <= 0 || mapH <= 0) throw eve::Exception("TileMap3D.buildMesh: empty tile layer");
89
90 const float tileW = std::max(1.f, layer->getTileWidth());
91 const float tileH = std::max(1.f, layer->getTileHeight());
92 const float hx = tileW * 0.5f;
93 const float hz = tileH * 0.5f;
94
95 auto set = layer->tileset(); // ecs::ComponentRef<Tileset>; always valid
96 const bool haveSet = set->texture != nullptr && set->columns > 0;
97
98 std::vector<float> pos;
99 std::vector<float> nrm;
100 std::vector<float> uv;
101 std::vector<uint32_t> idx;
102
103 // Unit-cube face topology (RH Y-up, outward CCW): faces = -Z,+Z,-X,+X,-Y,+Y.
104 // Face 5 (+Y) is the textured top; faces 0..4 are extruded side walls.
105 static const int kQuads[6][4] = {
106 {0, 1, 2, 3}, {5, 4, 7, 6}, {4, 0, 3, 7},
107 {1, 5, 6, 2}, {4, 5, 1, 0}, {3, 2, 6, 7},
108 };
109 static const glm::vec3 kFaces[6] = {
110 {0.f, 0.f, -1.f}, {0.f, 0.f, 1.f}, {-1.f, 0.f, 0.f},
111 {1.f, 0.f, 0.f}, {0.f, -1.f, 0.f}, {0.f, 1.f, 0.f},
112 };
113
114 const auto pushBoxTile = [&](float cx, float cz, float topY, float botY, float tu0, float tv0, float tu1, float tv1,
115 uint32_t flags) {
116 // Corners (index matches kQuads[kFace]).
117 const glm::vec3 corners[8] = {
118 {cx - hx, botY, cz - hz}, {cx + hx, botY, cz - hz}, {cx + hx, topY, cz - hz},
119 {cx - hx, topY, cz - hz}, {cx - hx, botY, cz + hz}, {cx + hx, botY, cz + hz},
120 {cx + hx, topY, cz + hz}, {cx - hx, topY, cz + hz},
121 };
122 const float sideU0 = wallU0_, sideV0 = wallV0_, sideU1 = wallU1_, sideV1 = wallV1_;
123 for (int f = 0; f < 6; ++f) {
124 if (sideDepth_ == 0.f && f != 5) continue;
125 const uint32_t base = uint32_t(pos.size() / 3);
126 const bool top = (f == 5);
127 const float su0 = top ? tu0 : sideU0;
128 const float sv0 = top ? tv0 : sideV0;
129 const float su1 = top ? tu1 : sideU1;
130 const float sv1 = top ? tv1 : sideV1;
131 const float us[4] = {su0, su1, su1, su0};
132 const float vs[4] = {sv0, sv0, sv1, sv1};
133 const glm::vec3 &n = kFaces[f];
134 for (int k = 0; k < 4; ++k) {
135 const glm::vec3 &p = corners[kQuads[f][k]];
136 pos.insert(pos.end(), {p.x, p.y, p.z});
137 nrm.insert(nrm.end(), {n.x, n.y, n.z});
138 float u = us[k], v = vs[k];
139 if (top) {
140 // Invert Tiled's diagonal-then-horizontal/vertical transform
141 // to map a destination corner back into the source atlas.
142 float s = (k == 1 || k == 2) ? 1.f : 0.f;
143 float t = k >= 2 ? 1.f : 0.f;
144 if (flags & 0x80000000u) s = 1.f - s;
145 if (flags & 0x40000000u) t = 1.f - t;
146 if (flags & 0x20000000u) std::swap(s, t);
147 u = tu0 + s * (tu1 - tu0);
148 v = tv0 + t * (tv1 - tv0);
149 }
150 uv.insert(uv.end(), {u, v});
151 }
152 idx.insert(idx.end(), {base, base + 2, base + 1, base, base + 3, base + 2});
153 }
154 };
155
156 tileCount_ = 0;
157
158 for (int ty = 0; ty < mapH; ++ty) {
159 for (int tx = 0; tx < mapW; ++tx) {
160 const uint32_t raw = uint32_t(layer->getTile(tx, ty));
161 const uint32_t gid = eve::map::tileGid(raw);
162 if (gid == 0) continue;
163
164 // World X/Z from the layer's 2D projection; elevation from metadata.
165 const float wx = layer->tileToWorldX(tx, ty) + hx;
166 const float wz = layer->tileToWorldY(tx, ty) + hz;
167 const float elev = layer->getTileDataNumber(int(gid), "height") * heightScale_;
168 const float topY = elev;
169 const float botY = elev - sideDepth_;
170
171 float tu0 = 0.f, tv0 = 0.f, tu1 = 1.f, tv1 = 1.f;
172 if (haveSet) atlasUvForGid(*set, gid, tu0, tv0, tu1, tv1);
173
174 pushBoxTile(wx, wz, topY, botY, tu0, tv0, tu1, tv1, raw);
175 ++tileCount_;
176 }
177 }
178
179 if (pos.empty()) return nullptr;
180 return gfx->newMeshFromArrays(pos.data(), nrm.data(), uv.data(), int(pos.size() / 3),
181 idx.data(), int(idx.size()));
182}
183
186 if (!gfx) throw eve::Exception("TileMap3D.buildRenderable: null graphics");
187 if (!layer) throw eve::Exception("TileMap3D.buildRenderable: null tile layer");
188
190 if (!mesh) throw eve::Exception("TileMap3D.buildRenderable: no non-empty tiles");
191
192 auto *renderable = graphics::Renderable3D::create();
193 if (!renderable) throw eve::Exception("TileMap3D.buildRenderable: Renderable3D create failed");
194 renderable->setMesh(mesh);
195 if (layer->tileset()->texture) renderable->setTexture(layer->tileset()->texture);
196 renderable->setTint(tintR_, tintG_, tintB_, tintA_);
197 return renderable;
198}
199
200} // namespace eve::hd2d
const std::string & s
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
float uv
glm::vec4 p[6]
double raw
int cols
tensor::Graph g
Definition GpuGraph.cpp:7
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
double r
float v
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanMeshSettings mesh
TileLayer * layer
int idx
float f
float t
Value::Array corners
Cell cell
int spacing
int tileW
int margin
int tileH
std::uint32_t depth
uint32_t flags
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
virtual Mesh * newMeshFromArrays(const float *posXYZ, const float *nrmXYZ, const float *uvST, int vertexCount, const uint32_t *indices, int indexCount)=0
Upload a triangle mesh from packed CPU arrays. Owned by Graphics. posXYZ required (vertexCount*3)....
GPU mesh handle (+ optional CPU morph targets).
Definition Mesh.h:26
EVENGINE_API_BACKENDS public API.
void setWallUV(float u0, float v0, float u1, float v1)
UV sub-rectangle of the atlas used for extruded side walls.
Definition TileMap3D.cpp:69
graphics::Mesh * buildMesh(graphics::Graphics *gfx, map::TileLayer *layer)
Build and upload the terrain mesh from a tile layer.
Definition TileMap3D.cpp:82
void setTint(float r, float g, float b, float a=1.f)
Tint multiplier applied to the whole terrain.
Definition TileMap3D.cpp:75
float getSideDepth() const
Returns the side depth.
Definition TileMap3D.cpp:63
void setHeightScale(float scale)
World units per unit of tile "height" metadata (default 1).
Definition TileMap3D.cpp:64
float getHeightScale() const
Returns the height scale.
Definition TileMap3D.cpp:68
void setSideDepth(float depth)
Downward side-wall depth in world units (default 6).
Definition TileMap3D.cpp:59
graphics::Renderable3D * buildRenderable(graphics::Graphics *gfx, map::TileLayer *layer)
Build a scene-ready Renderable3D carrying the extruded terrain. The returned entity is already regist...
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
Definition TileLayer.h:33
SQInteger top
Definition Snapshot.cpp:115
uint32_t tileGid(uint32_t raw)
Strip Tiled flip / rotate flags; keep low 28 bits.
Definition TileLayer.h:392