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 davedev · Apr 25, 2011 at 02:23 PM · alphasetpixel

SetPixels Not Transparent?

I am trying to use SetPixel with a brush image that has a round brush shape with a transparent background. The code is below. When I run this, the brush shape displays but with a white background. The output values for the first pixel are listed below, which should be transparent(?) since a=0.0, but it appears white when rendered. Or am I misunderstanding something?

function testAlpha(){ var tex=Instantiate(renderer.material.mainTexture); var pixels=brushTexture.GetPixels(0,0,brushTexture.width, brushTexture.height,0); var outputString=""; for (var y : int = 0; y < brushTexture.height; ++y) { for (var x : int = 0; x < brushTexture.width; ++x) { var brushColor=brushTexture.GetPixel(x,y); if(x==0){outputString+="brush color: "+brushColor+",";} if(x==0){outputString+="old text color: "+tex.GetPixel(x, y)+",";} tex.SetPixel (x, y, brushColor); if(x==0){outputString+="new tex color: "+tex.GetPixel(x, y)+"|";} } } Debug.Log(outputString); renderer.material.mainTexture=tex; tex.Apply(); }

brush color: RGBA(1.000, 1.000, 1.000, 0.000),old text color: RGBA(0.227, 0.459, 0.773, 1.000),new tex color: RGBA(1.000, 1.000, 1.000, 0.000)

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
Best Answer

Answer by Kourosh · Apr 25, 2011 at 03:10 PM

Check this answer:

http://answers.unity3d.com/questions/43888/painting-stencil-on-a-surface

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
4

Answer by davedev · Apr 25, 2011 at 05:53 PM

Here's the code I came up with that does this:

function compositeAlphaImage(baseImage, compositeImage, cx, cy){
    // composite image with alpha channel onto base image
    // cx and cy define the location of composite image relative to base image
    var newImage=Instantiate(baseImage);
    for (var y : int = 0; y < compositeImage.height; ++y) {
        for (var x : int = 0; x < compositeImage.width; ++x) {
           var compositeColor=compositeImage.GetPixel(x,y);
           var baseColor=baseImage.GetPixel(cx+x, cy+y);
           var alpha=compositeColor.a;
           var newRed=(1-alpha)*baseColor.r+alpha*compositeColor.r;
           var newGreen=(1-alpha)*baseColor.g+alpha*compositeColor.g;
           var newBlue=(1-alpha)*baseColor.b+alpha*compositeColor.b;
           var newAlpha=1.0;
           var newColor=Color(newRed,newGreen,newBlue,newAlpha);
           newImage.SetPixel (cx+x, cx+y, newColor);
        }
    }
    return newImage;
}
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 brainoidgames · Jul 28, 2016 at 03:43 PM 0
Share

I think there is a mistake here:

 newImage.SetPixel (cx+x, cx+y, newColor);

it has to be

 newImage.SetPixel (cx+x, cy+y, newColor);

avatar image
1

Answer by Mike 3 · Apr 25, 2011 at 02:51 PM

Are you using a transparent shader to render with? Otherwise it'll disregard your texture's alpha.

Comment
Add comment · Show 2 · 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 davedev · Apr 25, 2011 at 03:01 PM 0
Share

I've tried both. Non-transparent shader results in a red circle with a white background (my brush) on a blue texture (my canvas). A transparent shader results in a red circle with transparency around the circle in the brush image area which shows transparency work. However, I'm trying to get a red circle on a blue background where the transparency applies when setting the pixel but not on the final texture.

avatar image Mike 3 · Apr 25, 2011 at 03:13 PM 0
Share

Aah, I see, then $$anonymous$$ourosh's answer is probably what you want (blending between the colours), unless you just render an opaque blue plane behind it

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

1 Person is following this question.

avatar image

Related Questions

Paint a transparent object at runtime 0 Answers

Alpha Channel images. 2 Answers

simple water alpha 1 Answer

changes to colour alpha affect RGB values, but not in the same way 1 Answer

Having issues with terrain splat 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