ViewportTexture loses track of its Viewport all the time. What am I doing wrong?

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

Whenever I try to use a ViewportTexture (to get a 2D UI on a texture used in 3D), the texture kind of just works once, clears and then stops updating. I’ve checked out the “gui_in_3D” demo project which seems to work, but moving things around a bit and saving that eventually broke as well and I couldn’t get it back to working.

An example of what I’m trying to do:

  1. Create an empty scene with a Spatial as root
  2. Create a Viewport as child, set size to 100, 100
  3. Create a Label with a the text “TEST” as child to the Viewport
  4. Do “Save Branch as Scene” on the Label to make a scene of it, let’s call it “UI”
  5. Create a MeshInstance with a QuadMesh and a new SpatialMaterial
  6. Set the Albedo texture of the new material to a new ViewportTexture, and pick our Viewport
    You should now see “TEST” (upside down) on the quad.
  7. Switch to the UI scene and just save it (ctrl-S)
  8. Switch back to the original scene, the quad will now be black

Inspecting the viewport texture shows it’s 0x0, and in the output window it throws an error:

scene\main\viewport.cpp:106 - Condition ' !vp ' is true. returned: Size2()

Basically it seems like the ViewportTexture totally loses track of the Viewport, and at this point there’s no going back except re-creating the ViewportTexture and the new one gets lost just as easily.

When in the “broken” state, if I clear the Viewport Path of the texture, click Assign and pick my Viewport again, I get this path:

"/root/EditorNode/@@5/@@6/@@14/@@16/@@20/@@24/@@25/@@26/@@42/@@43/@@50/@@51/@@6456/@@6322/@@6323/@@6324/@@6325/@@6326/@@6327/Spatial/Viewport"

Which… doesn’t look very reasonable (and also obviously only works within the editor and, again, only until the UI scene is edited.)

I think every node is already a viewport, so adding a viewport node creates two.

MysteryGM | 2018-10-18 18:00

Not sure exactly what you mean, but I do want two viewports in the sense that one renders an UI to a texture, and the other (implicit) one renders the 3D scene to screen.

Anders Stenberg | 2018-10-18 19:09

:bust_in_silhouette: Reply From: CathodeRayBlues

Go to the viewport path settings and check on “Local To Scene”. That should fix the error.

I was trying to do something similar with rendering a UI progress bar to a Sprite3D texture, and checking that managed to fix it.

For some reason, though, the compiler will report a different set of errors, but still works completely normally and the viewport works correctly.