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
1
Question by TheSorm · Jun 22, 2016 at 09:40 PM · pixelsetpixelsgetpixelssetpixel

Use SetPixels, but dont set unvisible Pixels

I have a little Solution to put togehter multiple Sprites in one Sprite, but i have a problem, when i use SetPixels Method on the new Texture2D, SetPixel sets also unvisible Pixels with Alpha = 0.(i Use GetPixels to get pixels from Sprite) So the function delet whats behinde the Sprite but should be vissible because its only behinde a alpha = 0 pixel. The Only Solution i see is to use SetPixel and check for alpha = 0, but thats realy bad work, because i have some offset and setting every single pixel that would be bad.

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

Answer by Bunny83 · Jun 22, 2016 at 11:41 PM

A texture is always a rectangle and SetPixels can only set all pixels at once. You would need to combine the pixel data manually. I've once written this simple alpha blend method which does exactly that. The method i've posted over there only works on images with equal size as i simply iterate over both pixel arrays. If you want to use offsets or source / target rectangles it's a bit more complicated but not too bad.

I've just written a little helper class that allows you to write a portion of a Texture2D into another Color array based on a blend function. By default it uses a src alpha blend function.

 public static class Texture2DExtension
 {
     public delegate Color BlendFunc(Color src, Color dest);
 
     public static void WriteTo(this Texture2D aTexture, Color[] aTarget, int aTargetWidth, Rect aSourceRect, Vector2 aTargetPos, BlendFunc blend = null)
     {
         if (blend == null)
             blend = AlphaBlend;
         int w = aTexture.width;
         int h = aTexture.height;
         int tw = aTargetWidth;
         int th = aTarget.Length / tw;
         Color32[] colors = aTexture.GetPixels32();
         int sx = (int)aSourceRect.x;
         int sy = (int)aSourceRect.y;
         int sw = (int)aSourceRect.width;
         int sh = (int)aSourceRect.height;
         int tx = (int)aTargetPos.x;
         int ty = (int)aTargetPos.y;
         // clip rectangles
         if (sx < 0) { sw -= sx; sx = 0; }
         if (sy < 0) { sh -= sy; sy = 0; }
         if (sx + sw > w) { sw -= sx + sw - w; }
         if (sy + sh > h) { sh -= sy + sh - h; }
         if (tx < 0) { sx -= tx; sw += tx; }
         if (ty < 0) { sy -= ty; sh += ty; }
         if (tx + sw > tw) { sw -= tx + sw - tw; }
         if (ty + sh > th) { sh -= ty + sh - th; }
 
         for (int y = 0; y < sh; y++)
         {
             for (int x = 0; x < sw; x++)
             {
                 int srcIdx = x + sx + (y + sy) * w;
                 int destIdx = x + tx + (y + ty) * tw;
                 Color src = colors[srcIdx];                
                 Color dest = aTarget[destIdx];
                 aTarget[destIdx] = blend(src, dest);
             }
         }
     }
     public static void WriteTo(this Texture2D aTexture, Color[] aTarget, int aTargetWidth, Vector2 aTargetPos, BlendFunc blend = null)
     {
         Rect srcRect = new Rect(0, 0, aTexture.width, aTexture.height);
         WriteTo(aTexture, aTarget, aTargetWidth, srcRect, aTargetPos, blend);
     }
 
     public static BlendFunc AlphaBlend = _AlphaBlend;
     private static Color _AlphaBlend(Color src, Color dest)
     {
         return Color.Lerp(src, dest, src.a);
     }
 }

With that class inside your project you can simply do this:

 Texture2D someTex;
 Color[] targetPixels;
 
 someTex.WriteTo(targetPixels, targetWidth, new Vector2(10,10));

targetWidth is the width of the target texture so we can calculate the index into the targetPixels array. The vector defines where inside the target area you want to write that texture. Note that there's an overload that takes an additional "source rect" which allows you to just write a portion of "someTex" into the target area. The function clips all rectangles according to the image sizes.

Finally you can pass a custom blend function as last parameter so you can freely decide how the destination color and the source color should be combined.

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

44 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

Related Questions

SetPixels PROBLEM (trying to put decals) 0 Answers

getPixel/setPixels or stencil eraser brush 2 Answers

How can I show my SetPixels() texture changes without having to zoom my orthographic camera? 1 Answer

Resetting a Material's Texture After GetPixels/SetPixels 1 Answer

Proper way to strew lots of dead in Napoleonic RTS? 3 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