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 Kourosh · Mar 01, 2011 at 06:47 AM · texture2dalphastencilsetpixelsgetpixels

Painting stencil on a surface.

Hi, I've been trying and experimenting on GetPixel/SetPixels to read color data of a 64 x 64 pixels psd file and paste the retrieved data into a Texture2D. The stencil (PSD file) comes with a single layer without background. The Texture2d that is created inside the code is formatted as ARGB32 and the PSD is also imported as ARGB32. In the test link you can see that each star overlaps and removes the previously painted one.

How is it possible to solve this problem so that stars overlap but don't remove eachother?

WebPlayer test: http://jovialart.com/WebPlayer/WebPlayer.html

Thoughts appreciated.

here is script:

var stencil:Texture2D; var tagFilter:String; var paintMaterial:Material;

private var tex : Texture2D; private var stencilUV:Color[]; private var i:int; private var pixelUV;

function Start(){ stencilUV = new Color[stencil.width * stencil.height]; tex = new Texture2D (1024,1024, TextureFormat.ARGB32, false); //ReadStencilTexture(); }

function Update () { if(DetectPaintable(tagFilter)){ CreateStencil(pixelUV.x,pixelUV.y,stencil); } }

function DetectPaintable(tagFilter:String):boolean{ // Only if we hit something, do we continue var hit : RaycastHit; if(Physics.Raycast (camera.ScreenPointToRay(Input.mousePosition), hit)){ if (hit.transform.gameObject.tag == tagFilter){ // Only when we press the mouse if (!Input.GetMouseButton (0)) return; // Just in case, also make sure the collider also has a renderer // material and texture. Also we should ignore primitive colliders. var renderer : Renderer = hit.collider.renderer; var meshCollider = hit.collider as MeshCollider; if (renderer == null || renderer.sharedMaterial == null || renderer.sharedMaterial.mainTexture == null || meshCollider == null) return;

 pixelUV= hit.textureCoord;
 pixelUV.x *= tex.width;
 pixelUV.y *= tex.height;
 return true;
 }
 }

}

function CreateStencil(x:int,y:int, texture:Texture2D){ paintMaterial.mainTexture = tex; for (var xPix =0; xPix<texture.width; xPix++){ for (var yPix=0;yPix<texture.height; yPix++){ stencilUV[i] = texture.GetPixel (xPix,yPix) texture.GetPixel (xPix,yPix).a + tex.GetPixel((x -texture.width/2) +xPix, (y -texture.height/2)+yPix) (1-texture.GetPixel(xPix,yPix).a); // <----- i++; } } i=0; tex.SetPixels(x -texture.width/2, y-texture.height/2,texture.width,texture.height,stencilUV); tex.Apply(); }

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

6 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by taoa · Mar 01, 2011 at 09:20 AM

You should do what a graphics card does when you ask it to perform alpha blending (alpha blending is what you're trying to do here):

final rgb colour = sourceColour*sourceAlpha + destinationColour*oneMinusSourceAlpha

or in your case:

final rgb colour = (rgb colour of the texture you're applying alpha colour of the texture you're applying) + (rgb colour of the texture into which you are rendering (1 - alpha colour of the texture you're applying))

That way you won't just overwrite the alpha values of your 64x64, you'll blend them properly :)

Comment
Add comment · Show 6 · 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 Kourosh · Mar 02, 2011 at 01:04 AM 0
Share

Thanks taoa, i did what you mentioned, and also included the webplayer test. still having issues... In the "CreateStencil" function inside the loops there is a Color array called "stencilUV" where i implemented what you commented. is this what you meant?

avatar image taoa · Mar 02, 2011 at 10:52 AM 0
Share

Holy crap. Javasript. Ouch. Give me some time then :) BTW: ins$$anonymous$$d of perfor$$anonymous$$g a raycast every frame and only using it if you click the mouse, do the other way around, only do your raycast if the mouse is clicked. A mouse click test is about thirty seven billions times quicker than a ray cast test. Roughly.

avatar image taoa · Mar 02, 2011 at 11:26 AM 0
Share

O$$anonymous$$, I believe your code is generally correct, but your main issue here is that you use the alpha value of your source image (the one you apply) to multiply in both your source texture and your destination texture all colours components... including the alpha values! You should only multiply the rgb components, and for the alpha component of the final colour, choose the highest value between the source and the destination. Also, you're calling three (3! yes, 3!) times the function texture.GetPixel (xPix,yPix). Please, Call it once (1) and store it into a Color variable :p

avatar image Kourosh · Mar 02, 2011 at 04:17 PM 0
Share

HoOoray!! Success!! thanks taoa! you rock :)

avatar image taoa · Mar 02, 2011 at 06:25 PM 0
Share

Glad I helped! ^_^

Show more comments
avatar image
0

Answer by s.papian · Nov 18, 2011 at 12:27 PM

Hi Kourosh, would you be so kind to share your corrected script, i'm having a hard time multiplying my rgb components with the alpha channels.. Thx!

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 leonardoebs · Mar 20, 2012 at 03:22 PM

I'm trying to implement this scrip, but with no luck so far... Could you post the correct script? It would be an awesome help.

Thanks in advance

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 dbravo · Apr 05, 2012 at 12:39 PM

Hi, did anyone manage to run this code on iPhone? I need exactly this but it is running too slow.. is there another workaround?

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 KrishnaMV · Sep 17, 2013 at 01:07 PM 0
Share

yaa...too slow..cant use for mobile devices...:(

avatar image
0

Answer by dbravo · Apr 05, 2012 at 12:39 PM

Hi, did anyone manage to run this code on iPhone? I need exactly this but it is running too slow.. is there another workaround?

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
  • 1
  • 2
  • ›

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

getPixel/setPixels or stencil eraser brush 2 Answers

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

Merging Textures At Runtime - Not Correct? 1 Answer

Changing color of Texture2d with transparency 1 Answer

Setting pixels on a texture? 2 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