Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Rednalreden · Apr 02, 2015 at 02:41 PM · texturetexture2druntime

Converting a NPOT Texture2D at runtime with SetPixel problem

Hello,

Some time ago I asked a question about converting normal maps at runtime (click) which got anwsered by Aras. It seemed to work correctly but recently we received some textures which are higher than they are wide. When this occurres the texture gets deformed in a weird way.(see below)

The method I'm using for this conversion to a normal map is the same as referenced in the above question:


 private Texture2D NormalMap(Texture2D source)
     {
         Texture2D normalTexture = new Texture2D(source.width, source.height, TextureFormat.ARGB32, true);
         //normalTexture.filterMode = FilterMode.Trilinear;
         Color theColour = new Color();
 
         for (int y = 0; y < source.height; y++)
         {
             for (int x = 0; x < source.width; x++)
             {
                 Color newColor = source.GetPixel(x, y);
                 theColour.r = 0;
                 theColour.g = newColor.g;
                 theColour.b = 0;
                 theColour.a = newColor.r;
                 normalTexture.SetPixel(x, y, theColour);
             }
         }
 
         normalTexture.Apply();
         return normalTexture;
     }



Case

Now lets say my source image is the following dimensions: width(2027px) height(2048px). alt text

The image gets loaded from disk and put through the method to be converted and this is the result: (deformed) alt text

Now when we just rotate the image using any Imagine viewer/tool and upload it again this is the result: (correct) alt text

I've tried it with multiple textures and if the texture is wider (or equally wide) than it's high the result is correct. If the texture is higher than it's wide the deformation is detected. When I read the pixel values after we've applied the colors to the texture and I compare them to the original the values are exactly the same even though the texture is deformed. The problem seems to be located in the SetPixel from a NPOT texture that is higher then it's wide. Changing the direction in which the pixels are read and set did not work (as expected since I run through all of them)

Does anybody know a solution for this problem? It's important that the textures keep the same orientation for the application so turning them (even automatic) is not an option.

*Note: I am using Unity 4.6.2f1 for this project


Update 03-04-2015:

Checked it using

Color[] colors = source.GetPixels(); normalTexture.SetPixels(colors); normalTexture.Apply();

This gives the same deformation as with the SetPixel one. Also this does not occur with POT textures even if they are higher than they are wide (I have confirmed this).

It seems to be going wrong in the SetPixel methods in NPOT textures, Unity bug?

Comment
Add comment
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

1 Reply

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

Answer by Rednalreden · Apr 03, 2015 at 09:33 AM

I found the solution, it's not a Unity bug but an oversight on my part. When I loaded the texture in through the WWW class I first made a placeholder texture of 4x4px in which I loaded the texture using www.LoadImageIntoTexture(). The mistake was trying to keep down memory usage using DXT compression. This obviously does not work with NPOT textures I was using. After loading a texture with the above method everything looks fine. However when you try and read the pixels it will read it from the compressed type and it becomes displaced.

When I changed the placeholder TextureFormat to ARGB32 the problem was solved.

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

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.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image

Related Questions

Raycast project to texture at runtime 1 Answer

Make a Texture2D gradient during run-time 1 Answer

Difficulty loading alpha textures at runtime 0 Answers

Create a custom texture from code? 1 Answer

Changing Part Of Texture/Material 1 Answer


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