Skip to content

Commit

Permalink
Proper render order for map elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Jul 27, 2024
1 parent 34af9aa commit c90dd88
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,27 @@ class Map extends Node<Map> {
rules = rulesIni as RulesFile

addChild(new MapBackground())
addChild(new MapPack())
addChild(new OverlayPack())
addChild(new Terrain())
addChild(new Structures())
addChild(new Units())
addChild(new Infantry())
addChild(new MapPack().tap {
setPosition(0, 0, 0.1f)
})
addChild(new OverlayPack().tap {
setPosition(0, 0, 0.2f)
})
addChild(new Terrain().tap {
setPosition(0, 0, 0.2f)
})
addChild(new Structures().tap {
setPosition(0, 0, 0.2f)
})
addChild(new Units().tap {
setPosition(0, 0, 0.2f)
})
addChild(new Infantry().tap {
setPosition(0, 0, 0.2f)
})

addChild(new MapLines().tap {
transform { ->
translate(0f, 0f, 0.1f)
}
setPosition(0, 0, 0.4f)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,10 @@ class RenderPipeline implements AutoCloseable {
private final FrustumIntersection frustumIntersection = new FrustumIntersection()
private final List<Node> queryResults = []
private final List<RenderCommand> drawCommands = []
private final Comparator<Node> renderOrderComparator = new Comparator<Node>() {
@Override
int compare(Node node1, Node node2) {
return node1.globalPosition.z() <=> node2.globalPosition.z()
}
}

// Sorting seems to be more performant with separate sort functions
private final Comparator<Node> orderByDistance = Comparator.comparing { Node node -> node.globalPosition.z() }
private final Comparator<Node> orderByHighest = Comparator.comparing { Node node -> -node.globalPosition.y() }

Scene scene

Expand All @@ -214,11 +212,11 @@ class RenderPipeline implements AutoCloseable {
average('Gathering', 1f, logger) { ->
queryResults.clear()
drawCommands.clear()

drawCommands << scene.camera.renderCommand()
frustumIntersection.set(enlargedViewProjection.scaling(0.9f, 0.9f, 1f).mul(scene.camera.viewProjection), false)
scene.query(frustumIntersection, queryResults)
.sort(true, renderOrderComparator) // Furthest-away objects first
.sort(true, orderByHighest)
.sort(true, orderByDistance)
.each { node ->
if (node instanceof GraphicsElement) {
drawCommands << node.renderCommand()
Expand Down

0 comments on commit c90dd88

Please sign in to comment.