3d_in_2d.gd 760 B

1234567891011121314151617181920
  1. extends Node2D
  2. var viewport_initial_size = Vector2()
  3. @onready var viewport = $SubViewport
  4. @onready var viewport_sprite = $ViewportSprite
  5. func _ready():
  6. $AnimatedSprite2D.play()
  7. get_viewport().size_changed.connect(self._root_viewport_size_changed)
  8. viewport_initial_size = viewport.size
  9. # Called when the root's viewport size changes (i.e. when the window is resized).
  10. # This is done to handle multiple resolutions without losing quality.
  11. func _root_viewport_size_changed():
  12. # The viewport is resized depending on the window height.
  13. # To compensate for the larger resolution, the viewport sprite is scaled down.
  14. viewport.size = Vector2.ONE * get_viewport().size.y
  15. viewport_sprite.scale = Vector2.ONE * viewport_initial_size.y / get_viewport().size.y