Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Millerah · Mar 29, 2019 at 08:43 PM · 2dspritetexture2d

Strange edge on texture after hiding pixels

Hi,

I'm working on a project that changes a texture's alpha to define edges. However, when I set a texture, I'm getting a strange extra line on the bottom. alt text I've concluded its some kind of wrapping from the other edge, as when Delete the part of the top, the corresponding part of the extra bottom line disappears.

I made a new scene that just makes a 100x50 blank texture on an empty object with a sprite renderer and a script with this code in the start function:

 void Start()
 {
     //setup texture and sprite.
     int width = 100;
     int height = 50;
     int pixelsPerUnit = 10;
     var textureCopy = new Texture2D(width, height, TextureFormat.RGBA32, false, true);
     GetComponent<SpriteRenderer>().sprite = Sprite.Create(textureCopy, new Rect(0, 0, width, height), Vector2.zero, pixelsPerUnit);
     
     //Hide the bottom half of the texture
     var colors = GetComponent<SpriteRenderer>().sprite.texture.GetPixels32();
     for (int i = 0; i < colors.Length / 2; i++)
         colors[i].a = 0;

     //Set and redraw.
     GetComponent<SpriteRenderer>().sprite.texture.SetPixels32(colors, 0);
     GetComponent<SpriteRenderer>().sprite.texture.Apply();
 }

Here's another picture of a blank texture with some edges erased. Its as if a pixel wraps around. alt text

I've tried changing various settings, but I haven't managed to get those edges to disapear.

strangeline.png (7.9 kB)
moreerasedstuff.png (13.8 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by digipaul · Mar 29, 2019 at 09:42 PM

Yes textures can wrap around. This behavior is controlled in the import settings of the texture, via wrap mode. You probably want to set it to clamp.

It happens because when a texture is read it does a sort of average of the the pixel specified in the UV coordinates with adjacent pixels. When the pixel is at an edge, some of the adjacent pixels will be beyond the range of the texture so you have to tell unity what to do in the situation. Clamp means force the pixel to the nearest point in the texture while wrap means go to the opposite side.

Another thing to try is changing the filter mode. If you change it to point, then the adjacent pixels have no effect. If the filter mode is bi linear, it does this averaging behavior above while trilinear also includes the different mip map textures in the average.

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
1

Answer by Millerah · Mar 29, 2019 at 09:57 PM

It was indeed the wrap mode. When making a new texture it was defaulting to Repeat. After setting it to clamp it works perfectly. I had fiddled with wrap settings on the texture I was importing on another script, but it was leaving the new texture generated by the script unchanged.

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 StartStart · Oct 01, 2020 at 10:13 AM

Thank you @digipaul , @Millerah !

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

223 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 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 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 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 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 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 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 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 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 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 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 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

Unity UpdateExternalTexture from MovieTexture windows crash 0 Answers

Is it impossible to swap the Texture2D of a Sprite Renderer at runtime? 1 Answer

Drawing onto the Texture2D of a sprite 1 Answer

Can't create a 2D sprite with Sprite.Create when creating texture in code 1 Answer

How to Address Texture2D Elements from a Sprite with Sprite Mode: Multiple, in Code? 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