godot: ViewportTexture set for Sprite2D in editor doesn't work

Godot version

4.0-alpha6

System information

Ubuntu 20.04.3 LTS, Vulkan, NVIDIA GTX 1060 6GB, nvidia-driver-470 (proprietary, tested)

Issue description

ViewportTextures set in the editor for Sprite2Ds don’t work. Instead, they result in this error:

0:00:00:0411      setup_local_to_scene: Condition "proxy.is_valid()" is true.
  <C++ Source>    scene/main/viewport.cpp:87 @ setup_local_to_scene()

As a workaround, setting the ViewportTexture programmatically works, albeit the resulting image is inverted on the Y axis and the transparent_bg property of the SubViewport making the background black if on, or gray if off:

var view_texture = viewport.get_viewport().get_texture()
viewport_sprite.texture = view_texture

image

This given the following structure, using the Godot 3 demo https://godotengine.org/asset-library/asset/128 as base. image

Using a SubViewportContainer as a workaround is also possible, though this also results in the transparent_bg property of the SubViewport making the background black if on, or gray if off: image image

Steps to reproduce

The minimal reproduction project presents the issue using SubViewport1, as well as the workarounds using SubViewport2 and SubViewport3.

On running the project two more errors undocumented above are pushed:

0:00:00:0526      get_path: Cannot get path of node as it is not in a scene tree.
  <C++ Error>     Condition "!is_inside_tree()" is true. Returning: NodePath()
  <C++ Source>    scene/main/node.cpp:1617 @ get_path()
0:00:00:0526      get_node: Node not found: "SubViewport1" (relative to "").
  <C++ Error>     Method/function failed. Returning: nullptr
  <C++ Source>    scene/main/node.cpp:1335 @ get_node()

At the time of writing I cannot be certain whether these were pushed because of a glitch or because of me making a mistake.

Minimal reproduction project

3d_in_2d.zip

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 22 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Trying in Godot 4.0 stable release today, I just ran into this same issue on Linux. Varying the render_target_update_mode did not help for me. I can still run successfully if I set the viewport texture in code rather than in the UI.

same problem in 4.0 release

Still occurs in 4.0.beta2 in case it needed confirmation.

The strange thing is that I get this error in the debugger yet the texture is shown fine when running from the editor. But when exporting the game (release or debug) the viewport texture is black. I was able to remove all the errors and warnings by getting the texture through code, but the result is the same, just no errors. The only thing that solved it for me was to use material_override, setting the viewport texture through code and not using local to scene:

extends MeshInstance3D


func _ready():
	var vpt := (%SubViewport as SubViewport).get_texture()
	material_override.texture_albedo = vpt

Note: doing the same from the editor results in the error and black texture when exported

The strange thing is that I get this error in the debugger yet the texture is shown fine when running from the editor. But when exporting the game (release or debug) the viewport texture is black.

This is probably a separable issue, but the SubViewport’s render_target_update_mode seems to be the trick here. For some reason, when run from the editor, the sub viewport gets drawn as long as the update mode is at least “When Visible”. But when exported, that no longer triggers draws, only “When Parent Visible” or “Always” does.

For extra fun, if your viewport changes via a @tool script so it should vary directly in the editor without running - only “When Visible” or “Always” (not “When Parent Visible”) cause it to be redrawn live.

Here’s a fresh (4.0rc5) MRP with toggles to mess with the refresh mode, and otherwise as a working example of 2d in 3d (despite the proxy.is_valid() error logs) : 2d3d.zip

Thank you so much. Using the Godot 4.0 release, changing ‘render_target_update_mode’ to “Always” solved the issue, however, the issue without changing that attribute remains unsolved on my behalf.

Having this issue as well with using a viewport containing a control node in a 3D environment.

Is anybody having this issue on Windows? Or is it just Mac and Linux?

albeit the resulting image is inverted on the Y axis

Note that unlike Godot 3.x, you don’t need to enable flip_y anymore to get the image with the right orientation.