Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 /
This question was closed Apr 26, 2021 at 07:21 PM by elingranath01 for the following reason:

solved it

avatar image
0
Question by elingranath01 · Apr 26, 2021 at 04:12 PM · shadertextureshaderscolortexture2d

Combining 2 black-white textures

Trying to combine 2 black and white images so the white parts overlay. I've tried just simply adding the pixel colors together which SHOULD keep the black parts black (0+0=0) and the white parts white (0+1=1) but for some reason the background turns into a light gray? (first pic is 1/2 images being combined, second is the result, I couldn't fit all 3 images, but both of them have black backgrounds) alt text alt text

 public Texture2D CombineTex(List<Texture2D> input)
     {        
         int width = input[0].width;
         int height = input[0].height;
         Texture2D newTex = new Texture2D(width, height, TextureFormat.RGB24, false);
         foreach (Texture2D tex in input)
         {
             for (int x = 0; x < width; x++)
             {
                 for (int y = 0; y < height; y++)
                 {
                     Color c = newTex.GetPixel(x, y) + tex.GetPixel(x, y);
                     newTex.SetPixel(x, y, c);
                 }
             }
             newTex.name += tex.name;
         }
         newTex.Apply();
         byte[] bytes = newTex.EncodeToPNG();
         File.WriteAllBytes("Assets/Textures/CatTex/WhiteMarks/" + newTex.name + ".png", bytes);
         return newTex;
     }
whitemark1.png (41.0 kB)
whitemark1whitemark2.png (38.0 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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by elingranath01 · Apr 26, 2021 at 07:21 PM

nvm I'm dumb. I forgot to set the new texture to be equal to the first texture to add together. I guess the "texture base color" was light gray or something so even if I added onto it the bg was gray ^^;

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 Bunny83 · Apr 26, 2021 at 05:59 PM

Well, there are a gazillion ways how to combine two textures. Just adding them is possible, but could result in some strange results depending on how the result is treated. Keep in mind that adding full brightness to another full brightness would result in double that value so with normalized colors a 1 and a 1 results in a 2 which probably would simply clip back to 1. If that's what you want, great, that's what your code does. However there may be an issue if your color also have an alpha channel as this will be combined the same way. By looking at your provided image, the top image seems to have an actual background color of black (0,0,0) and the features are actually white (1,1,1). However your resulting image has an 80% white background.


I can see several possible issues here. First of all you should clip all your rgba values and keep them in the range of 0 to 1, especially the alpha value. Second, Unity always was a bit dodgy when iit comes to gamma correction with png files. So we don't know what colorspace your target image lives in. You should check that. I've once created those generic PNGTools to read and disect a png file into it's "chunks" in order to inspect them or manipulate them. I originally created them for this answer but it was about the dpi size of the image (specifically the pHYs chunk). Though I used them in this question which also was about color issues due to the gamma information stored in the png file.


When creating a Texture2D object in Unity we can specify if we want a linear color space or the gamma color space. So if your original image (which seems to be your target image) uses a certain color space, that would be the resulting colorspace as well. You may want to create a completely new Texture as the target texture so you can specify the color space. AFAIK there's no way to change the colorspace of an existing Texture2D.

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 elingranath01 · Apr 26, 2021 at 06:57 PM 0
Share

I think there's something wrong with the way I'm adding them together? It's not impossible to get black in the resulting texture, so I don't get why the color space would be messing it up :S I tried clamping all the values and it made no difference

Follow this Question

Answers Answers and Comments

216 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

Related Questions

Retrieving information from texture not working in shader graph 0 Answers

Changing Eye Colour (Colour only non-white parts of a texture?) 2 Answers

Shaders - offset texture coordinates by a single pixel 2 Answers

Drawing a Texture on top of another Texture 2 Answers

Double Layer Material 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