• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Kylotan · Jan 23, 2013 at 08:10 PM · texturesprite

How can I get Unity to stop resampling my sprites?

I am rendering sprites as a single texture on a quad. Unfortunately, Unity resamples the texture to the nearest power of two which affects the artwork when it is a different size (eg. 24x24). I can zoom in during play and see that the texture has become a 32x32 one. The texture is set to Point mode so bilinear filtering is off.

I have tried setting the asset to the 'GUI' texture mode, but it is still being resampled. Similarly if I go to 'Advanced' texture mode and leave the nearestPowerOfTwo mode on 'None', it is resampled then, as well.

Anybody have any suggestions?

Example image, Scene view on left, original asset on right: alt text

And a close-up of how the texture appears in the preview after importing, to show it was imported correctly: alt text

texture_sampling_issue.png (10.6 kB)
texture_sampling_issue_2.png (4.1 kB)
Comment
Add comment · Show 20
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Jan 23, 2013 at 08:45 PM 0
Share

How do you render into that texture? Do you use RenderTextures or regular textures? Do you create textures by script or are you exclusively using project assets? When you hit play and zoom in, hit pause, then select the object you suspect to have 32x32 in the scene view and find the texture in the inspector. What resolution does it give in the preview? Are any other settings off?

We need more information.

avatar image Kylotan · Jan 23, 2013 at 09:22 PM 0
Share

It is just a plain project asset, a PNG. The image in the preview is the original resolution on disk (24x24), but in Scene and Game view it's 32x32 with the resampling artifacts.

avatar image Wolfram · Jan 23, 2013 at 09:27 PM 0
Share

So you counted the pixels? Are you certain it's not just interpolation artefacts from bilinear sampling? While zoomed in in Play mode, does the texture change if you modify the texture asset, for example limiting it to 16x16, setting Filter mode to "Point", etc.?

avatar image Kylotan · Jan 23, 2013 at 09:29 PM 1
Share

Yes, I counted the pixels. I posted the image so you can too. :) There is no bilinear sampling; it's set to Point mode. If I use a 32x32 asset it renders perfectly.

avatar image Wolfram · Jan 23, 2013 at 09:36 PM 0
Share

Hm, this shouldn't happen ^^

Try a reimport of the texture. Also, try my 16x16 suggestion. Does the scene view react to the change?

Another idea: any platform dependent texture settings? What are you building for (i.e., what's your project set to), Win/$$anonymous$$ac, iOS, Android?

avatar image Kylotan · Jan 23, 2013 at 09:43 PM 0
Share

Limiting to 16x16 doesn't appear to be an option; the $$anonymous$$ax Size option in the Inspector doesn't go lower than 32 for me. Changing to bilinear blurs the image, as expected. No platform specific settings, build mode set to PC/$$anonymous$$ac Standalone.

avatar image Wolfram · Jan 23, 2013 at 09:44 PM 0
Share

What shader are you using for that object?

avatar image liszto · Jan 23, 2013 at 09:47 PM 0
Share

In advanced and check there is no mipmaps activated, nearestPowerOfTwo to none and point mode ?

avatar image Wolfram · Jan 23, 2013 at 09:51 PM 0
Share

Hm, come to think of it, I seem to remember there has been some bug for ages where small(?) textures are resampled despite being set to non-power-of-two. Unfortunately I don't have my Unity here, so I can only confirm that tomorrow.

avatar image Kylotan · Jan 23, 2013 at 10:06 PM 0
Share

@liszto: It makes no difference. $$anonymous$$y screenshot was with the texture in GUI mode but if I switch to Advanced with those settings it looks identical. @Wolfram: currently using the Unlit/Transparent shader. The same effect appears with any shaders I try though.

avatar image Wolfram · Jan 30, 2013 at 08:00 PM 1
Share

Sorry I didn't get back to you earlier. The answer is "yes, Unity resamples your textures, and no, you can't prevent it". See my answer for a more detailed explanation.

avatar image Wolfram · Jan 30, 2013 at 08:40 PM 0
Share

Additional note: if you look along the rim of your resampled texture you'll notice several pixels interpolated towards white, although the original texture has no white pixels there. This is a problem of how you generate/prepare your alpha textures, and can later bite you in the, umh, bum, even if you use true POT textures. See @Eric5h5's answer in this question, where Ben $$anonymous$$t explains the approach to prevent these errors.

avatar image Kylotan · Jan 30, 2013 at 08:48 PM 0
Share

That should only apply if you bilinearly sample them. With point sampling it should always be exactly on a texel with no blending. Ideally, if I was going to be bilinearly sampling textures with alpha, I'd use premultiplied alpha with additive blending which doesn't have these problems.

avatar image Wolfram · Jan 30, 2013 at 08:56 PM 0
Share

As Eric explains, it is independent of the filtering method and mipmapping, so you will get this problem even with point sampling (see his snapshot).

However, it is not a problem if you always display your textures "pixel perfect" (=1 texel displayed exactly in 1 pixel). Which I suppose you do?

Once you start to zoom in or zoom out, the problem will become visible.

I am not sure how anti-aliasing influence/intensify this behaviour.

avatar image Wolfram · Jan 31, 2013 at 12:41 PM 0
Share

UPDATE: O$$anonymous$$, I did some analysis of that.

If you exclusively use either 0% or 100% alpha pixels, you are correct that setting the (POT!-)texture to "Point" is enough to prevent the white rims, independent of mipmapping - if the textures are displayed pixel-perfect or enlarged. Artifacts will appear even for "Point" if a) mipmapping is enabled, and b) the texture is displayed at 70% (=1/sqrt(2)) or smaller. Or if you have any half-transparent pixels in your textures (or use NPOT textures).

I'll update Eric's answer with additional snapshots and more info.

avatar image Kylotan · Jan 31, 2013 at 02:52 PM 0
Share

$$anonymous$$ipmapping causes problems because the extra mip levels are essentially filtered downsamples. Without that, point sampling should always be fine by definition, because it never blends texels. If Unity is doing something different then that is likely to be a bug.

avatar image Wolfram · Jan 31, 2013 at 03:37 PM 0
Share

Unity generates the mip levels always via bilinear texel interpolation, independent of the point/bilinear/trilinear filter mode setting. The filter mode is merely an output setting, which does nto modify the texture data. This is also the default behaviour for both OpenGL and DirectX. AFAI$$anonymous$$ DirectX in theory allows point sampling for mipmap generation, while OpenGL does not, but I could be wrong.

avatar image Wolfram · Jan 31, 2013 at 06:13 PM 1
Share

Summary of the white-alpha-pixel-problem: http://forum.unity3d.com/threads/168532-White-rims-around-transparent-textures-an-analysis

avatar image Kylotan · Jan 31, 2013 at 06:52 PM 0
Share

I know, there's no safe way to use mipmaps if you want to avoid texture bleeding unless you manually generate the mip levels yourself. I was trying to say that if you have no mip mapping then point sampling should always work. The only exception would be anti-aliased diagonals, but I don't understand why it would be sampling the white pixels. I guess that's a multisampling issue.

avatar image Wolfram · Oct 16, 2013 at 02:13 PM 0
Share

For reference: the whole internal-rescaling-to-POT-for-NPOT-textures issue is fixed in Unity 4 (tested 4.2.0)!

6 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Wolfram · Jan 23, 2013 at 10:05 PM

UPDATE: For reference: the whole internal-rescaling-to-POT-for-NPOT-textures issue is fixed in Unity 4 (tested 4.2.0)! The following discussion/answer is about older versions, such as Unity 3.5:

Assuming it really is a bug, I can at least suggest a workaround. It is ugly, tedious, and hackish, but it will resolve your problem:

  • pad your textures manually to be 32x32

  • either adapt your object UV to the new mapping scale (and maybe origin), or adapt the scale/offset settings in the material using the texture.

For example, extend your texture to the right and the bottom, to keep the "correct" texture segment in the upper left corner. Then you can set the texture scaling to 0.75/0.75 instead of 1/1 in the material.

If you keep your sprite centered while padding on all four sides, you'd have to adjust the offset, too.

EDIT:

I checked this again with an old testscene I did about this problem, and unfortunately it is still present (latest version tested: 3.5.7. The issue is actually fixed in 4.x (tested 4.2.0). Problem exists at least since 2.6.

====> Any non-power-of-two-textures will be resampled to the nearest(?) power-of-two, even if NPOT-scaling is explicitly set to "None". An exception are GUI textures, i.e. if you use such a texture for OnGUI() or in GUITexture objects. But any such texture applied to a "regular" GameObject will no longer be pixel-perfect compared to the original texture. See also Aras' answer here (although he does not explicitly mention that the scaling also happens if you set NPOT-scaling to "None").

I guess it is debatable whether this is a bug or a feature - I consider it a bug, since if I explicitly set the texture scaling to "leave-the-texture-alone-and-dont-use-NPOT-scaling", I expect the system to do exactly that. If it is unable to do that due to hardware limitations (or whatever other reasons) it at the very least needs to explain this drawback in the documentation.

EDIT2: Woops, as @Bunny83 pointed out, it is documented, somewhere in the fine print ;-)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 30, 2013 at 08:24 PM 0
Share

Yes, it's not really documented in a way that it's easy to find.

Btw, aras clearly says that non power of two textures, if used for anything but GUI, uses a second version of the texture which is scaled to the next higher power of two texture.

avatar image Kylotan · Jan 30, 2013 at 08:28 PM 1
Share

What isn't so clear in Aras's message is that you can't get to that padded texture yourself. Doesn't this make the whole dropdown box for how to treat NPOT textures pointless?

avatar image Wolfram · Jan 30, 2013 at 08:30 PM 0
Share

@Bunny83: Yes, but when I was first reading that I was hoping for an addendum such as "Yes, this even is true if you explicitly disable NPOT-scaling". The way he wrote it it is unclear, since it also describes the default settings of the import behaviour: scaling to the next (well, ok: nearest...) power of two.

avatar image Wolfram · Jan 30, 2013 at 08:34 PM 0
Share

@$$anonymous$$ylotan: Indeed it does, except for GUITextures and GUI.DrawTexture.

avatar image Bunny83 · Jan 31, 2013 at 02:44 PM 0
Share

Btw, nearest is not the same as next (it that'S really what aras ment). Next would be like the setting "ToLarger" so the next greater power of two size. Nearest could be the next larger or the next smaller.

avatar image
2

Answer by Bunny83 · Jan 30, 2013 at 08:07 PM

Read the section about Texture Sizes. As you can see when NPOT textures are used as "normal" textures they are converted internally to POT textures. I'm not sure why they do that, but it seems you can't disable this behaviour. It might be for compatibility with graphics cards which don't support NPOT textures.

However the easiest and best fix is to resize your texture to 32x32 (add padding). NPOT textures are even slower. The graphics card memory is optimised for POT sizes. Even NPOT textures will use the next higher POT size alignment in graphics memory.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 30, 2013 at 08:12 PM 0
Share

Btw, your princess fits into 16 pixels horizontally, so when using a 16x32 texture it's even smaller than a 24x24 texture ;)

But maybe in some cases it might be larger (sprite animations?). The best way is of course to use an atlas like @$$anonymous$$alikDrako said, but it requires a bit more UV fiddling

avatar image Kylotan · Jan 30, 2013 at 08:13 PM 0
Share

The link to the Texture Sizes part of the docs is a bit contradictory - one part says the texture will be padded, another part says it will be stretched. Hopefully they can fix this in the future.

avatar image Kylotan · Jan 30, 2013 at 08:14 PM 0
Share

As for texture atlases, they are a complication that most simple sprite systems will want to avoid. Ideally Unity would perform atlasing for you, as it does for fonts.

avatar image Wolfram · Jan 30, 2013 at 08:18 PM 0
Share

@$$anonymous$$ylotan: Yes, the docs page is incomplete/misleading. In the important section (the explanation of the parameter), they only mention GUITextures, and I guess for them it will be padded (so even they don't use the NPOT-version internally). However, further below in the "fine print", it mentions the rescaling (neglecting to mention that this does not apply to GUITextures...)

avatar image
1

Answer by bopie · Feb 19, 2014 at 05:34 AM

Do you set the color format to TrueColor? Its default value is 'Compressed' and will be blurred.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image K0ndor · May 20, 2016 at 02:57 PM 0
Share

Yup, I had similiar problem with my 8x8 textures. Changing from "compressed" to "16bits" fixed it.

avatar image
0

Answer by mescalin · Jan 30, 2013 at 07:20 PM

i am not sure that is really possible in unity, i think my approach would be to make them textures a much higher resolution.

actually i have some 2d art left over from my construct classic obsession, i have all this scaled up 4x from 16x16 to sharpen the aliasing. I imagine in unity I'd scale up even more still and maybe customize the actual edges depending how the alpha goes down.

basically you're gonna just have to use a tonne of more memory and get down and dirty with it but in a systematic way preferably, I'd suggest investigating programmable batch type image manipulation tool like imagemagic so you can mass process stuff

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MalikDrako · Jan 30, 2013 at 07:38 PM

That is happening because you are using a texture with dimensions that are not a power of 2. You can stop Unity from resizing the texture by either using the GUI texture type or Advanced with "Non Power of 2" set to "None," but I'm not sure how much of an impact this will have on performance

From the Texture documentation:

It is possible to use other (non power of two) texture sizes with Unity. Non power of two texture sizes work best when used on GUI Textures, however if used on anything else they will be converted to an uncompressed RGBA 32 bit format. That means they will take up more video memory (compared to PVRT(iOS)/DXT(Desktop) compressed textures), will be slower to load and slower to render (if you are on iOS mode). In general you'll use non power of two sizes only for GUI purposes.

You could also combine multiple sprites onto a single larger texture and change the UVs of the quad to get just the sprite you want. This is called a texture atlas

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Jan 30, 2013 at 07:49 PM 0
Share

This is not correct - read on in the document right after the paragraph you quoted. See my answer, as well as the discussion in the question.

Your last sentence is valid.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to create a GUI like Zombieville? 1 Answer

How to stop gradient banding? 2 Answers

Texture input: changing format 1 Answer

Assigning UV Map to model at runtime 0 Answers

How change sprite animation texture?? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges