How to hide node in viewport/camera?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By babyblueboxers

I have multiple viewport containers, viewports, and cameras all referencing the same world_2d. I need a way for some nodes to not render in certain viewports (for compositing reasons). Is this achievable?

I understand 3D Cameras have some kind of culling layers, but this is a 2D world.

I’m guessing you want multiple players or else this would be a trivial matter of just setting things visible = false. Y-sorting is sort of the culling of 2D but it doesn’t sound like that’s what you’re going after. Are you going for like a multiplayer Project Zomboid style view? Where items are only visible if they’re in front of a player but they wouldn’t be visible to the other player unless they are also looking towards the object?

The VisibilityNotifier2D node is mostly likely going to be a part of your solution. it gives you the viewport_entered(viewport: Viewport) signal that is emitted when the VisibilityNotifier2D enters a Viewport’s view.

timothybrentwood | 2021-05-01 16:11

So details regarding the project:

What I’m trying to achieve is seamless 2D world-wrapping. You scroll to the far right edge of the screen and you end up back on the far left side of the map, but there’s no hard edge or boundary you run into. As you approach the right edge, you need to be able to see what lies back on the far left side. Teleporting objects back and forth which cross the boundary is easy, and I can achieve the illusion of the world-wrapping rather effortlessly by moving around some viewports, cameras, etc. But the problem is that objects get clipped by the background at the world’s edge. So if an object is “hanging” over the left or right edge, then when you approach one side of the map the object’s sprite is clipped by the background of the world since viewports don’t respect the Z-Levels of the objects they’re viewing.

What I need to do is be able to set up a special camera that doesn’t render the background sprite, and only renders other objects in the world, so that the viewport doesn’t clip the object sprites with the background sprite.

I came up with 3 solutions, but only one seems viable:

  1. Separate the background from the foreground objects. - Doable, but hacky and not preferable. Would make switching worlds more tedious than I’d prefer.
  2. Set up a special camera that ignores the background. - Doesn’t seem possible with Camera2D.
  3. Get the viewports to respect the Z-Levels of the objects they’re viewing. - Also doesn’t seem possible. Viewports seem to just “screenshot” the world and paste it like an image.

babyblueboxers | 2021-05-01 16:41