Yasen Bagalev
Yasen Bagalev

Reputation: 51

LibGDX scaling 2d graphics

I have problem with stretching sprites in LibGDX.

I have one png file for the sky in the game (1x1000 px) and i want to do it 1000x1000px

sprite.setSize(pixelWidth,pixelHeight);

Edit: Here is the image that I want to stretch - sky . It has little gradient from the top to the bottom as you can see. I want to make it 1000px in width. I expect image that has color-change in the width.

But I get this strange result: Image Image

You can see that there is something like gradient in width, even with lines, but with no reason, because I am scaling 1px image...

The strange thing is that everything looks fine on other PC's and mobile phones... But I don't think that the problem is in my PC!?

I don't know what to try... Any ideas?

Sorry if I didn't explained it very well.. and sorry for my bad english

Final: It was from the filtering. I changed the filter from linear to nearest and everything was fine

Upvotes: 5

Views: 3127

Answers (2)

TheWhiteLlama
TheWhiteLlama

Reputation: 1286

I think this is just happening because of interpolation. When your sprite and your grabbed region of the texture has a width of 1 px and you're scaling the sprite to the screen width, the GPU will interpolate some color-values and it will use colors of pixel in the neighborhood in the texture.

you could try to change your gradient to a 3px wide texture and grab your 1px wide sprite out from the middle of the 3 px. The GPU will still interpolate the colors and use pixelcolors of the neighborhood, but as they'll be the same as, it will look okay.

Here an image how your textureregion probably looks like at the moment and its grabbing area, and following the textureregion how it should look like and its grabbing area:

enter image description here

Upvotes: 0

sandeep kundliya
sandeep kundliya

Reputation: 961

i dont understand what your question realy is but a good way to scale a sprite is like this

sprite.setSize(scrw * 0.16f, scrw * 0.16f);

here scrw is your viewport's width

Upvotes: 2

Related Questions