简体   繁体   中英

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

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

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

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:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM