Skip to content

Commit 4037c63

Browse files
authored
Merge pull request #1321 from melonjs/feature/tiled-blend-modes
Support Tiled 1.12+ layer and object group blend modes
2 parents 8807641 + a6311a5 commit 4037c63

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

packages/melonjs/src/level/tiled/TMXTileMap.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,14 @@ export default class TMXTileMap {
596596
// propagate group blend mode to children (if not default)
597597
if (group.blendMode !== "normal" && obj.blendMode === "normal") {
598598
obj.blendMode = group.blendMode;
599+
// also propagate to child renderables (e.g. Entity wrappers)
600+
if (
601+
typeof obj.renderable !== "undefined" &&
602+
obj.renderable.isRenderable === true &&
603+
obj.renderable.blendMode === "normal"
604+
) {
605+
obj.renderable.blendMode = group.blendMode;
606+
}
599607
}
600608
obj.setOpacity(obj.getOpacity() * group.opacity);
601609
// and to child renderables if any
@@ -611,6 +619,22 @@ export default class TMXTileMap {
611619
// directly add the obj into the objects array
612620
objects.push(obj);
613621
} /* false*/ else {
622+
// propagate group blend mode to children in non-flattened mode,
623+
// since container blendMode is overridden by each child's preDraw()
624+
if (
625+
group.blendMode !== "normal" &&
626+
obj.isRenderable === true &&
627+
obj.blendMode === "normal"
628+
) {
629+
obj.blendMode = group.blendMode;
630+
if (
631+
typeof obj.renderable !== "undefined" &&
632+
obj.renderable.isRenderable === true &&
633+
obj.renderable.blendMode === "normal"
634+
) {
635+
obj.renderable.blendMode = group.blendMode;
636+
}
637+
}
614638
// add it to the new container
615639
targetContainer.addChild(obj);
616640
}

packages/melonjs/tests/tmxtilemap.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,23 @@ describe("TMXTileMap", () => {
129129
});
130130
expect(container.getOpacity()).toBeCloseTo(0.8);
131131
});
132+
133+
it("should propagate blend mode to children in non-flattened mode", () => {
134+
const map = new TMXTileMap("test", minimalMap);
135+
const objects = map.getObjects(false);
136+
137+
const container = objects.find((obj) => {
138+
return obj.name === "TestGroup";
139+
});
140+
expect(container).toBeDefined();
141+
expect(container.children.length).toBeGreaterThan(0);
142+
143+
// children should inherit the group blend mode
144+
for (const child of container.children) {
145+
if (child.isRenderable === true) {
146+
expect(child.blendMode).toEqual("multiply");
147+
}
148+
}
149+
});
132150
});
133151
});

0 commit comments

Comments
 (0)