Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(tilemap): a layer's origin offset moves its tiles

esenginea8bc3abbaefb
diff --git a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
index b0a143c8b..ec9f31b37 100644
--- a/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
+++ b/src/esengine/renderer/plugins/TilemapRenderPlugin.cpp
@@ -171,6 +171,9 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
glm::vec2 parallax;
resource::TextureHandle singleTileset; // single-tileset fallback source
u32 singleColumns;
+ // Where this layer's grid starts. A layer whose tiles are drawn from a
+ // LayerData has no component to author it on, so it starts at the origin.
+ glm::vec2 originOffset{0.0f, 0.0f};
if (comp) {
visible = comp->visible;
sortLayer = comp->renderLayer;
@@ -180,6 +183,7 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
parallax = comp->parallaxFactor;
singleTileset = comp->tileset;
singleColumns = static_cast<u32>(comp->tilesetColumns);
+ originOffset = comp->originOffset;
} else {
visible = layer.visible;
sortLayer = layer.sort_layer;
@@ -247,8 +251,11 @@ void TilemapRenderPlugin::collect(RenderCollectContext& collect_ctx) {
f32 parallaxOffsetX = cam.center.x * (1.0f - parallax.x);
f32 parallaxOffsetY = cam.center.y * (1.0f - parallax.y);
- f32 adjOriginX = originX + parallaxOffsetX;
- f32 adjOriginY = originY + parallaxOffsetY;
+ // The layer's own offset rides here rather than in the chunk vertices:
+ // those are cached, and this is read every frame, so moving the grid is
+ // live in the editor and costs no rebuild.
+ f32 adjOriginX = originX + parallaxOffsetX + originOffset.x;
+ f32 adjOriginY = originY + parallaxOffsetY + originOffset.y;
// Visible chunk window. A chunk's screen position is only cx*CHUNK*tw /
// cy*CHUNK*th for an orthogonal grid; iso couples cx and cy, and
diff --git a/tools/check-component-fields.mjs b/tools/check-component-fields.mjs
index bad96a05b..d4bf73c4e 100644
--- a/tools/check-component-fields.mjs
+++ b/tools/check-component-fields.mjs
@@ -27,8 +27,8 @@ const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
* says which, so the list reads as work rather than as permission.
*/
const DECLARED_GAPS = {
- 'TilemapLayer.originOffset': "Tiled's per-tileset tileoffset, which the loader does not read either",
- 'TilemapLayer.tilesetRows': 'written by the Tiled loader; nothing bounds-checks a tile id against it yet',
+ 'TilemapLayer.tilesetRows': 'written by the Tiled loader; a bounds check against it would read'
+ + ' its default of 1 as a one-row tileset and cut every layer that never set it',
};
const components = JSON.parse(
diff --git a/tools/renderScenes.mjs b/tools/renderScenes.mjs
index 067eb2113..4dbeb0d7b 100644
--- a/tools/renderScenes.mjs
+++ b/tools/renderScenes.mjs
@@ -40,6 +40,10 @@ export const SCENES = [
{ id: "mesh2d", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/mesh2d.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/mesh2d.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.30,\"y\":0.556,\"rgb\":[255,0,0],\"tol\":40},{\"x\":0.70,\"y\":0.556,\"rgb\":[0,255,0],\"tol\":40},{\"x\":0.30,\"y\":0.40,\"rgb\":[255,0,0],\"tol\":40}]" } },
{ id: "parallax-shape", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/parallax-shape.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/parallax-shape.textures.json", ESTELLA_VERIFY_STEPS: "4", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.5,\"y\":0.5,\"rgb\":[0,255,0],\"tol\":40},{\"x\":0.25,\"y\":0.5,\"rgb\":[255,0,0],\"tol\":40}]" } },
{ id: "tilemap-flip", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/tilemap-flip.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/tilemap-flip.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.40,\"y\":0.36,\"rgb\":[255,0,0]},{\"x\":0.60,\"y\":0.36,\"rgb\":[0,255,0]},{\"x\":0.40,\"y\":0.58,\"rgb\":[0,0,255]},{\"x\":0.60,\"y\":0.58,\"rgb\":[255,255,0]}]" } },
+ // A layer's own offset, set through the inspector's door: 16 world units is one
+ // cell here, so the whole grid steps right by one and the left column empties.
+ // It rides the per-frame origin, not the cached chunk vertices.
+ { id: "tilemap-origin", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SET_FIELD: "{\"entity\":1,\"component\":\"TilemapLayer\",\"key\":\"originOffset\",\"value\":[16,0]}", ESTELLA_VERIFY_SCENE: "/scenes/tilemap-flip.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/tilemap-flip.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.60,\"y\":0.36,\"rgb\":[255,0,0]},{\"x\":0.80,\"y\":0.36,\"rgb\":[0,255,0]},{\"x\":0.60,\"y\":0.58,\"rgb\":[0,0,255]},{\"x\":0.40,\"y\":0.36,\"rgb\":[0,0,0],\"tol\":20}]" } },
// Parallax on a cached-chunk tilemap: the camera moves while the layer model is untouched, so only the per-frame origin/parallax merge (not a revision-bump rebuild) can place the tiles — guards against baking camera-dependent offsets into the chunk cache.
{ id: "tilemap-parallax", tier: "pr", webgpu: true, env: { ESTELLA_VERIFY_SCENE: "/scenes/tilemap-parallax.esscene", ESTELLA_VERIFY_MANIFEST: "/scenes/tilemap-flip.textures.json", ESTELLA_VERIFY_W: "256", ESTELLA_VERIFY_H: "256", ESTELLA_VERIFY_STEPS: "2", ESTELLA_VERIFY_MOVE: "{\"component\":\"Camera\",\"to\":[48,-16],\"steps\":3}", ESTELLA_VERIFY_EXPECT: "[{\"x\":0.30,\"y\":0.40,\"rgb\":[255,0,0]},{\"x\":0.10,\"y\":0.40,\"rgb\":[0,0,0],\"tol\":20},{\"x\":0.50,\"y\":0.40,\"rgb\":[0,255,0]}]" } },
// BitmapText's first pixel coverage: a 2-glyph synthetic .fnt atlas (red A, green B) pins glyph layout, atlas UV orientation and the one-DrawCommand-per-text path.