Viewport not updating its size (Rendering 2D elements in 3D)

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

I have this Spatial node called Life where I have the viewport containing a TextureRect and a ColorRect to construct the lifebar. Finally, a Sprite3D to render the 2D image into the 3D world.

structure

In the life script I have the following code to update the size of the lifebar according to the unit’s max health:

func calculate_size(value):
	return Vector2(32 * value / 50.0, 32)

func set_max_life(value):
	total_life = max(0, value)
	current_life = total_life
	var size = calculate_size(total_life)
	$Viewport.size = size
	$Viewport/LifeBox.rect_size = size
	$Viewport/BarBox.rect_size = size

Then I have another 3D scene called Unit, that has Life as a child. Unit has the following script:

func _ready():
	var max_life = 100
	$Life.set_max_life(max_life)

Then, in my main scene I have one unit instantiated in the 3D editor, and another one through the following GDScript:

func _ready():
    var nav = get_parent().get_parent()  # Navigation Node
    var unit_scene = preload("res://Unit.tscn")
    for index in range(3):
    	var unit = unit_scene.instance()
    	var position = -(index + 1)
    	unit.translate(Vector3(position, .2, position))
    	unit.rotate_y(135 * PI / 180)
    	nav.call_deferred("add_child", unit)

But then, only the unit instantiated through the editor seems to have the lifebar size updated:

error

What am I doing wrong?

:bust_in_silhouette: Reply From: rossunger

I think you’re going about this the wrong way… my understanding is that having more viewports is not great for performance…

You might be better of have a quad that’s draw at the location, a rotated to face the camera (aka a billboard). is there a reason you’re not using 3d Sprite or something like that?

They talk about billboards here:
https://forum.godotengine.org/30238/how-to-make-a-3d-sprite-always-look-at-the-player

I think it’s easier to make health bars using 2D nodes. Using only 3D sprites would demand a lot of coding (I suppose).

Every tutorial I saw about making 2D health bars in a 3D environment used viewports, which is the only reason I used it. But I guess you are right, they look expensive.

I’ll try moving on to using only 3D sprites then. Thanks for the reply!

flaae | 2022-02-03 02:47

I might be wrong about this though! if it’s working for you and it’s not using an unexpected amount of resources, then maybe viewports is the right way to go… i hadn’t come across it until now!

rossunger | 2022-02-03 02:50

they are being harder to use than I expected, i’ll give a solo sprite3d a try anyways :slight_smile:
thanks again!

flaae | 2022-02-03 02:53