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 crappygraphix · Apr 04, 2013 at 10:54 PM · iostexture2dencodetopng

How do I prevent Texture2D.EncodeToPNG from creating artifacts on iOS?

When I run the code block below using a standalone build or from within the editor the resulting PNG images look perfect. When executed on an iPad or iPhone the resulting PNG has artifacts as if it was being compressed. I'm at a loss.

 ...
 
 Texture2D image;
 for(int i=0; i<Grid.UptPixelList.Count; i++){
   image = new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32, false);
   image.filterMode = FilterMode.Point;
   pixelList = Grid.UptPixelList[i];
   origin = Grid.OriginList[i];
   foreach(UptPixel pixel in pixelList){
     if(Mathf.FloorToInt(pixel.position.z - origin.z) < 0) continue; // Skip, we've gone underground.
     image.SetPixel(Mathf.FloorToInt(pixel.position.x - origin.x + halfWidth), Mathf.FloorToInt(pixel.position.z - origin.z), pixel.color);
   }
   image.Apply();
                         
   File.WriteAllBytes(filename + Path.DirectorySeparatorChar + dirname + layerCounter + ".png", image.EncodeToPNG());
                         
   Destroy(image);
   layerCounter++;
 }
 
 ...
Comment
Add comment · Show 2
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 Statement · Apr 04, 2013 at 11:12 PM 0
Share

If you would display the image using GUI.DrawTexture etc, does it also have artefacts? Could you show us the images you generate?

avatar image crappygraphix · Apr 05, 2013 at 01:29 AM 0
Share

I won't be able to do the GUI Texture test until $$anonymous$$onday but for now here is the standalone and ios export of a simple red square for comparison.

alt text alt text

ios.png (4.8 kB)

1 Reply

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

Answer by crappygraphix · Apr 08, 2013 at 05:50 AM

So the problem was a simple one to solve. Basically the foreach loop going through "pixelList" in the code shown in the question above only writes to a subset of the pixels in the texture. I needed to initialize all pixels of the texture to clear before writing the pixels in the list.

The new code looks like this:

 ...
     
 Texture2D image = new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32, false);
   image.filterMode = FilterMode.Point;
   for(int i=0; i<Grid.UptPixelList.Count; i++){
     ClearImage(image, imageWidth, imageHeight);
     pixelList = Grid.UptPixelList[i];
     origin = Grid.OriginList[i];
       foreach(UptPixel pixel in pixelList){
         if(Mathf.FloorToInt(pixel.position.z - origin.z) < 0) continue; // Skip, we've gone underground.
         image.SetPixel(Mathf.FloorToInt(pixel.position.x - origin.x + halfWidth), Mathf.FloorToInt(pixel.position.z - origin.z), pixel.color);
       }
     image.Apply();
     
     File.WriteAllBytes(filename + Path.DirectorySeparatorChar + dirname + layerCounter + ".png", image.EncodeToPNG());
     
     layerCounter++;
 }
 Destroy(image);
     
 ...

Where ClearImage is:

   void ClearImage(Texture2D image, int width, int height){
     for(int x=0; x<width; x++){
       for(int y=0; y<height; y++){
         image.SetPixel(x,y,Color.clear);
       }
     }
     image.Apply();
   }
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 hydrix · Oct 04, 2016 at 11:48 PM 0
Share

I had the same thing but personally I found I had to initialize each pixel as a new Color(1, 1, 0.005f) because otherwise it would just overwrite the png with blank and do the same thing. It was doing this weird stretching thing where a png with a few non-transparent pixels would create this nightmarish texture

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

12 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

Related Questions

Texture2D.EncodeToPNG not working on iOs on Unity 3.5.5f3 1 Answer

Bug - Texture "corrupted" after sending on server with MultipartformDataSection, only on iOS 1 Answer

EncodeToPng that works more smoothly on mobile 1 Answer

iOS Sprites in PVRTC? 1 Answer

Only RGBA32bit works when setting Texture2D pixels on iOS? 0 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