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(render): z actually orders sprites within a layer

esenginee67773f46a8e
diff --git a/examples/scene-flow/assets/scenes/main.esscene b/examples/scene-flow/assets/scenes/main.esscene
index 7e04e6fd8..1bee22aef 100644
--- a/examples/scene-flow/assets/scenes/main.esscene
+++ b/examples/scene-flow/assets/scenes/main.esscene
@@ -11,7 +11,11 @@
{
"type": "Transform",
"data": {
- "position": { "x": 0, "y": 0, "z": 10 }
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 10
+ }
}
},
{
@@ -34,13 +38,28 @@
"components": [
{
"type": "Transform",
- "data": { "position": { "x": 0, "y": 0, "z": -5 } }
+ "data": {
+ "position": {
+ "x": 0,
+ "y": 0,
+ "z": 0
+ }
+ }
},
{
"type": "Sprite",
"data": {
- "size": { "x": 820, "y": 620 },
- "color": { "r": 0.07, "g": 0.08, "b": 0.1, "a": 1 }
+ "size": {
+ "x": 820,
+ "y": 620
+ },
+ "color": {
+ "r": 0.07,
+ "g": 0.08,
+ "b": 0.1,
+ "a": 1
+ },
+ "layer": -10
}
}
],
@@ -61,7 +80,10 @@
{
"type": "Canvas",
"data": {
- "designResolution": { "x": 800, "y": 600 },
+ "designResolution": {
+ "x": 800,
+ "y": 600
+ },
"scaleMode": 2
}
},
@@ -78,15 +100,30 @@
"parent": 2,
"children": [],
"components": [
- { "type": "Transform", "data": {} },
+ {
+ "type": "Transform",
+ "data": {}
+ },
{
"type": "UINode",
"data": {
"position": 1,
- "insetLeft": { "value": 0, "unit": 0 },
- "insetRight": { "value": 0, "unit": 0 },
- "insetBottom": { "value": 10, "unit": 0 },
- "height": { "value": 16, "unit": 0 }
+ "insetLeft": {
+ "value": 0,
+ "unit": 0
+ },
+ "insetRight": {
+ "value": 0,
+ "unit": 0
+ },
+ "insetBottom": {
+ "value": 10,
+ "unit": 0
+ },
+ "height": {
+ "value": 16,
+ "unit": 0
+ }
}
},
{
@@ -94,7 +131,12 @@
"data": {
"content": "Shell scene — camera, backdrop and this footer persist across every scene swap",
"fontSize": 11,
- "color": { "r": 0.55, "g": 0.58, "b": 0.64, "a": 1 },
+ "color": {
+ "r": 0.55,
+ "g": 0.58,
+ "b": 0.64,
+ "a": 1
+ },
"align": 1,
"verticalAlign": 1
}
diff --git a/src/esengine/renderer/DrawCommand.hpp b/src/esengine/renderer/DrawCommand.hpp
index adc1be379..409e2dd68 100644
--- a/src/esengine/renderer/DrawCommand.hpp
+++ b/src/esengine/renderer/DrawCommand.hpp
@@ -79,13 +79,17 @@ struct DrawCommand {
u64 flagsKey = static_cast<u64>(stateFlags & 0x03) << 31;
u64 materialKey = (static_cast<u64>(materialId) & 0x1FFFF) << 14;
+ // Order-preserving float mapping — monotonic over the FULL float range.
+ // The old [-1,1] normalize-and-truncate silently wrapped any real-world z
+ // (e.g. a backdrop at z=-5 sorted as if nearest), and had the painter's
+ // order inverted for the blended stages. Camera looks down -z, so larger
+ // z = nearer: transparent/overlay draw back-to-front (far first, near
+ // lands on top); opaque draws front-to-back (early-z friendly).
u32 depthBits;
if (stage == RenderStage::Transparent || stage == RenderStage::Overlay) {
- f32 invDepth = 1.0f - (depth * 0.5f + 0.5f);
- depthBits = static_cast<u32>(invDepth * 16383.0f);
+ depthBits = orderedFloatBits(depth) >> 18;
} else {
- f32 normDepth = depth * 0.5f + 0.5f;
- depthBits = static_cast<u32>(normDepth * 16383.0f);
+ depthBits = (~orderedFloatBits(depth)) >> 18;
}
u64 depthKey = static_cast<u64>(depthBits & 0x3FFF);