3d_in_2d.gd 778 B

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