Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CanvasLayer Child Controls Z-Index Layering Issue #81257

Closed
mikuYongh opened this issue Sep 2, 2023 · 9 comments · Fixed by #81270
Closed

CanvasLayer Child Controls Z-Index Layering Issue #81257

mikuYongh opened this issue Sep 2, 2023 · 9 comments · Fixed by #81270

Comments

@mikuYongh
Copy link

Godot version

4.0.3

System information

Windows -Godot 4.0.3

Issue description

I need to change the rendering order of controls within the same Layer, but when I use two CanvasLayer nodes with the same Layer and place controls inside them, changing the z-index of child components does not affect their rendering order. I believe this may be incorrect; within the same layer, I should be able to change a control's z-index to alter its rendering order.

Steps to reproduce

_% D 8 DH}{`I5I$)7WTMPG
%W0R{~VLQZBKB508}EL4A T

Minimal reproduction project

demo.zip

@jonathandw743
Copy link

I'm not sure but this might be intended behaviour because the ordering works as you expect if using a Control or Node2D instead of a canvas layer.

@jonathandw743
Copy link

Screenshot 2023-09-02 201827

@mikuYongh
Copy link
Author

Screenshot 2023-09-02 201827
Yes, if it's just Node2D or Control, the z-index behaves correctly in terms of drawing order. However, my game has a complex hierarchy, and I now need multiple CanvasLayers, and the zIndex of CanvasLayer's child controls needs to be arranged according to their drawing order. I believe that in the case of consistent CanvasLayers, they should be arranged based on the Layer hierarchy according to the zIndex.

@Sauermann
Copy link
Contributor

Please have a look at the description of CanvasLayer.
https://docs.godotengine.org/en/latest/classes/class_canvaslayer.html

It explicitly states, that CanvasItems of that layer are drawn independently.
So what happens here in your demo-project is

  • The first Sprite2D of your demo project is drawn on CanvasLayer based on their z-index.
  • The second Sprite2D of your demo project is drawn on CanvasLayer2 based on their z-index.
  • CanvasLayer and CanvasLayer2 are drawn on the viewport based on the order of their layer-number.

If I understand you correctly, you want to intermix the drawing hierarchy of CanvasItems in different CanvasLayers.
This is currently not supported and would likely need to be considered compatibility-breaking, since other projects depend on the current behavior.

As a solution, it would be best to describe the current behavior better in the docs:
https://docs.godotengine.org/en/latest/classes/class_canvasitem.html#class-canvasitem-property-z-index

@jonathandw743
Copy link

Is there a specific reason or piece of functionality that means you have to use CanvasLayer and not a Node2D or Control node?

@mikuYongh
Copy link
Author

是否有特定原因或功能意味着您必须使用 CanvasLayer 而不是 Node2D 或 Control 节点?

This is because the maximum z-index is 4096, and in my game, each map has 8 layers. For example, some tiles need to be placed in each layer, and there may be thousands of textures in each layer. So, if I use the formula layer*1000 + texture z-index, it might not be enough, as I have a large number of map textures. Therefore, I adopted the implementation of CanvasLayer. However, it seems that Godot may lack the ability to handle hierarchical rendering order among multiple layers, and it cannot achieve this functionality. This could be an area that needs improvement because there might be a demand for arranging the display order of CanvasLayer child controls within multiple identical layers. From the documentation, it appears that the current Godot implementation is lacking in this aspect.

@mikuYongh
Copy link
Author

请看一下 的描述CanvasLayerhttps://docs.godotengine.org/en/latest/classes/class_canvaslayer.html

它明确指出,CanvasItem该层的 s 是独立绘制的。 那么在您的演示项目中发生的事情是

  • 您的第一个演示项目是根据其 z-indexSprite2D绘制的。CanvasLayer
  • 您的演示项目的第二个是根据其 z-indexSprite2D绘制的。CanvasLayer2
  • CanvasLayerCanvasLayer2根据其图层编号的顺序绘制在视口上。

如果我理解正确的话,您想混合不同 CanvasLayers 中 CanvasItems 的绘图层次结构。 目前不支持此功能,并且可能需要被视为破坏兼容性,因为其他项目依赖于当前的行为。

作为解决方案,最好在文档中更好地描述当前行为: https://docs.godotengine.org/en/latest/classes/class_canvasitem.html#class-canvasitem-property-z-index

I agree, I believe that Godot currently lacks the ability to handle the hierarchical rendering order of CanvasItems across different CanvasLayers. I think Godot needs to enhance this functionality within CanvasLayers. While this usage scenario may be relatively less common, there is still a need for managing the drawing order of CanvasItems across mixed CanvasLayers. If this functionality cannot be achieved, it should indeed be documented to describe the current behavior.

@jonathandw743
Copy link

So it seems that the point of a canvas layer is to completely separate rendering so one layer is behind another no matter their children's z indices. If this were not the case, and you could use the z index to "mix" between the layers, then you that would defeat the purpose of the CanvasLayer, as they would share the same 4096 z indices.

It seems that your problem is that you don't have enough z indices.

To fix this you could:

Put some images on the same z index but just order them using their order in the tree.

Use negative z indices.

Render some parts that don't change as an image and use that is your game.

Godot could:

Add more z indices (propbably not necessary).

Add some named sorting layers, but this would be the same as using canvas layers.

@Sauermann
Copy link
Contributor

I have created a PR for the documentation change in #81270.
If you need additional functionality like

  • more canvas layer indices or
  • rendering order change for CanvasItems in different CanvasLayers,

it would probably be better, to discuss these in the feature proposal repository https://github.com/godotengine/godot-proposals/

@akien-mga akien-mga added this to the 4.2 milestone Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants