Different Resources/assets for different screen size

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

Hello,
I have just migrated from libgdx and trying to learn godot. One thing which I am not able to do or did not find any resources online is how to use different assets source. For eg, in libgdx i had three different file of same texture but with different size. That way i would not need to scale up or down (results in smooth graphics overall) and just render it to viewport. I first queried screen size,and point the asset loader to the specific location while loading, some thing like this

ResolutionFileResolver.Resolution resolutions = {
new ResolutionFileResolver.Resolution(720, 1280, “1280X720”),
new ResolutionFileResolver.Resolution(1080, 1920, “1920X1080”),
new ResolutionFileResolver.Resolution(1440,2560, “2560X1440”)
};

    ResolutionFileResolver resolver = new ResolutionFileResolver(new InternalFileHandleResolver(),resolutions);
    assetManager = new AssetManager();
    assetManager.setLoader(TextureAtlas.class,new TextureAtlasLoader(resolver));

Is it possible to simulate it in godot? Basically I want the sprite to pick up texture source based on the screen size.

In Godot use mip maps for the same effect, but only when using the Display->Window->Stretch->Mode = 2D option under Project Settings.

Godot’s 2D stretch mode is what you are use to from libgdx
When clicking on a image, go to it’s import settings and accept the Mipmap flag, then re-import.
Mip maps are made by the computer from your largest image, it calculates all the smaller versions for you and you don’t have to make them yourself.

Godot also has a Viewport mode, this will scale the image for you at realtime. You won’t need smaller version of the image to do this.

MysteryGM | 2018-09-26 01:07

:bust_in_silhouette: Reply From: SIsilicon

You could probably do something like this.

var texture_path = "res://examples/texture-"
var texture
var screen_size = get_viewport.size
if screen_size.y >= 2160: #4K
    texture = preload(texture_path+"-4K.png")
elif screen_size.y >= 1080: #FullHD
    texture = preload(texture_path+"-FullHD.png")
elif screen_size.y >= 720: #HD(720p)
    texture = preload(texture_path+"-HD.png")
else: #480p or less. The lowest resolution
    texture = preload(texture_path+".png")

Just be sure to name your textures properly like-

  • texture-4k.png
  • texture-fullHD.png
  • texture-HD.png
  • texture.png

I think this approach is just a hack. It becomes tedious when project size increases. I was looking for something which automatically switches asset location. The above approach also forces you to build the ui from code rather than from editor.
Thanks for suggestion

kakidon | 2018-09-25 04:05