Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 GR00G0 · Oct 04, 2018 at 09:32 AM · 2dgraphicstexture2drendertexture

How can I draw a part of a texture on a another texture with transparency?

I am trying to draw a certain part of a Texture2D on a different Texture2D. I have tried using Graphics.CopyTexture, but the problem is I hav some transparent pixels, which won’t just get overlaid, but replace the pixels below entirely. I have also tried using a RenderTexture and Graphics.DrawTexture, but that hasn’t worked for me aswell, and the documentation for DrawTexture is really confusing. Can someone help me with this problem? Thank you!

Comment
Add comment · Show 1
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 nitzanwilnai · Oct 04, 2018 at 12:56 PM 0
Share

Have you considered using a shader to do it?

Is this a one time copy?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Oct 04, 2018 at 01:24 PM

Well, Unity doesn't have a built in method to blend two textures on the CPU. I've written this simple alpha blend implementation. However it only works if the textures are the same size. Though it could be changed to allow drawing just a section of an image and specifing a location.


However using the GPU you would have more options. Here you can use Graphics.DrawTexture and using a RenderTexture as target. Now you can use any shader you want to draw to the render texture. However RenderTextures live in the GPU memory. There are ways to extract the result into a normal Texture2D but it's kinda slow. Though depending on your needs this may still be worth it.


It's generally easier to provide specific answers if you ask specific questions. That includes what's the actual purpose of the drawing? How should the result be used? Some context?

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 GR00G0 · Oct 04, 2018 at 04:05 PM 0
Share

Basically I wanted to create a tilemap system and draw the chunk as one texture, and for that I first draw all tiles of the chunk on this specific chunk texture.

avatar image
1

Answer by toddisarockstar · Oct 04, 2018 at 04:57 PM

you have to change the import settings of the textures to do this sort of thing. under texture type, you would need to select "advanced so you can make the textures readable. and maybe change the format setting.

then just do a little coding. here is a very basic example of changing pixels to put one image on top the other:

     public Texture2D stamp;
     public Texture2D background;
     public Texture2D output;
 
     //location to stamp
     public int locationx = 0;
     public int locationy = 0;
     void Start () {
         int x, y;
 
         //make a copy of your background
         output = new Texture2D (background.width, background.height);
         x = output.width;y = output.height;
         while (x>0) {x--;
             y = output.height;
             while (y>0) {y--;
                 output.SetPixel(x,y,background.GetPixel(x,y));}}
         
 
         // page through all your pixels of stamp
         x = stamp.width;y = stamp.height;
         while (x>0) {x--;
         y = stamp.height;
         while (y>0) {y--;
                 if(x+locationx<background.width&&y+locationy<background.height){
                 Color cs = stamp.GetPixel(x,y);
                 Color cb = background.GetPixel(x+locationx,y+locationy);
                 float a = cs.a;//<---alph of the stamp pixel;
                     // mix colors based on alpha channel
                 float r = (cs.r*a)+(cb.r*(1-a));
                 float g = (cs.g*a)+(cb.g*(1-a));
                 float b = (cs.b*a)+(cb.b*(1-a));
                       
                 output.SetPixel(x+locationx,y+locationy,new Color(r,g,b,1));
 
                 }
                         }}
         output.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 GR00G0 · Oct 04, 2018 at 06:55 PM 0
Share

Something like this has already crossed my $$anonymous$$d, but I’am afraid doing everything on the CPU will be to expensive.

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

206 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

Related Questions

Alternative for Graphics.CopyTexture() ? 2 Answers

All pixels drawn on my RenderTexture with a Sprite Diffuse material are shown as black instead of the intended texture colour 1 Answer

Background Image in a 2D game: problems of size/quality importing. What's the right workflow? 1 Answer

Layered Billboard Sprite 0 Answers

2D Shadows? 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