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 Aladine · Jan 29, 2014 at 06:33 AM · logicpaintingbrushsoft

[Logic] Paint with soft brush

Hello everyone,

During the last few days i was coding a painting behavior for a game am working on, and am currently in a very advanced phase, i can say that i have 90% of the work done and working perfectly, now what i need to do is being able to draw with a "soft brush" cause for now it's like am painting with "pixel style" and that was totally expected cause that's what i wrote, my current goal consist of using this solution :

  1. import a brush texture, this : ![

  2. create an array that contain all The alpha values of that texture

  3. When drawing use the array elements in order to define the new pixels alpha

And this is my code to do that (it's not very long, there is too much comments)

 //The main painting method
         //theObject     = the object to be painted
         //tmpTexture    = the object current texture
         //targetTexture = the new texture 
         void paint (GameObject theObject, Texture2D tmpTexture, Texture2D targetTexture)
         {
                 //x and y are 2 floats from another class
                 //they store the coordinates of the pixel 
                 //that get hit by the RayCast
                 int x = (int)(coordinates.pixelPos.x);
                 int y = (int)(coordinates.pixelPos.y);
                 
                 //iterate through a block of pixels that goes fro
                 //Y and X and go #brushHeight Pixels up
                 // and #brushWeight Pixels right
                 for (int tmpY = y; tmpY<y+brushHeight; tmpY++) {
                         for (int tmpX = x; tmpX<x+brushWidth; tmpX++) {
                                 //check if the current pixel is different from the target pixel
                                 if (tmpTexture.GetPixel (tmpX, tmpY) != targetTexture.GetPixel (tmpX, tmpY)) {
                                         //create a temporary color from the target pixel at the given coordinates
                                         Color tmpCol = targetTexture.GetPixel (tmpX, tmpY);
                                         //change the alpha of that pixel based on the brush alpha
                                         //myBrushAlpha is a 2 Dimensional array that contain
                                         //the different Alpha values of the brush
                                         //the substractions are to keep the index in range
                                         if (myBrushAlpha [tmpY - y, tmpX - x].a > 0) {
                                             tmpCol.a = myBrushAlpha [tmpY - y, tmpX - x].a;
                                         }
                                         //set the new pixel to the current texture
                                         tmpTexture.SetPixel (tmpX, tmpY, tmpCol);
                                 } 
                         }
                 }
                 //Apply 
                 tmpTexture.Apply ();
                 //change the object main texture 
                 theObject.renderer.material.mainTexture = tmpTexture;
         }
     

Now the fun (and bad) part is the code did exactly what i asked for, but there is something that i didn't think of and i couldn't solve after spend the whole night trying, the thing is that by asking to draw anytime with the brush alpha i found myself create a very weird effect which is decreasing the alpha value of an "old" pixel, so i tried to fix that by adding an if statement that check if the current alpha of the pixel is less than the equivalent brush alpha pixel, if it is, then augment the alpha to be equal to the brush, and if the pixel alpha is bigger, then keep adding the brush alpha value to it in order to have that "soft brushing" effect, and in code it become this :

                                                 if (myBrushAlpha [tmpY - y, tmpX - x].a > tmpCol.a) {
                                                         tmpCol.a = myBrushAlpha [tmpY - y, tmpX - x].a;
                                                 } else {
                                                         tmpCol.a += myBrushAlpha [tmpY - y, tmpX - x].a;
                                                 }
                                         

But after i've done that, i got the "pixelized brush" effect back, am not sure but i think maybe it's because am making these conditions inside a for loop so everything is executed before the end of the current frame so i don't see the effect, could it be that ? Am really lost here and hope that you can put me in the right direction,

Thank you very much and have a great day

Comment
Add comment · Show 2
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 Aladine · Jan 29, 2014 at 06:52 AM 0
Share

I also tried to break the conditions but still didn't get the willing effect

avatar image Aladine · Jan 29, 2014 at 03:30 PM 0
Share

@robertbu i don't know if answers.unity3D send notifications after user tagging, but if it did, i hope you have time to go through this and help me find a solution, thnx

1 Reply

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

Answer by melkior · Jan 29, 2014 at 07:14 PM

I suspect your using alpha differently than I would expect it to work as an artist in some of your calculations.

Let me explain alpha/opacity from an end user perspective ; and then you can see if your code is doing this? As from reading it it doesn't seem to me like it is?

Brush Opacity : How much of the pixels information should be imprinted when the brush is applied.

Brush Alpha: For any one pixel on the brush a pixel might have an individual opacity (alpha) that differs from the overall painting opacity.

Example 1:

Brush Opacity 100% Brush Alpha pixel 1: 100% Brush Alpha pixel 2: 50%

I am painting BLUE pixels on a YELLOW canvas.

For pixel 1 : Opacity 100% and Pixel Alpha 100% the underlying YELLOW pixel is replaced with a BLUE pixel

For pixel 2: Opacity 100% and Pixel Alpha is 50% the underlying YELLOW pixel is mixed with 50% BLUE and becomes GREEN.

Example 2:

Brush Opacity 50% Brush Alpha pixel 1: 100% Brush Alpha pixel 2: 50%

I am still painting BLUE pixels on a YELLOW canvas.

For pixel 1 : Opacity 50% and Pixel Alpha 100% the underlying YELLOW pixel is mixed with a BLUE pixel and becomes GREEN.

For pixel 2: Opacity 50% and Pixel Alpha is 50% the underlying YELLOW pixel is mixed with 25% BLUE and becomes a light YELLOW-GREEN.

The canvas level yellow pixels alpha doesn't come in to consideration most of the time. Its hue and saturation level would though.

The times when the canvas alpha might be important is in a document with multiple layers. When multiple layers are present often much of each layer is transparency (alpha) level lets pixels below it show through. If you are using multi-layer painting documents and find the layer you are paining on is fully transparent you have to make a choice to either treat it as a neutral canvas (no mixing of color needed) or to go down layers iteratively until it finds some color value to sample to use for color mixing modes.

Also in play might be what type of brush/painting you are trying to emulate? Airbrush? Oil paints? Acrylics? Etc.

Also in play most paint programs offer a range of brush tool options like 'screen', 'burn', 'dodge', 'multiply' etc. These affect the output also.

It sounds like to me when you say pixel art you mean close to how most art programs emulate a 'pencil' brush. It overwrites whatever is beneath it most of the time; the opacity rules above might apply though. Color mixing in pencil brushes is usually more of an 'overlay' rather than the 'mix' I applied.

Its actually more difficult to mix colors than you might realize on a canvas. In real life something like airbrush does this easiest. Oil paints on a canvas you usually pre-mix the paint on your pallet before applying it to the canvas otherwise its going to be streaky and not evenly mixed... although that can be a desired effect some times too :)

Its all in what you desire the intended end result to look & feel like.

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 Aladine · Jan 29, 2014 at 09:44 PM 0
Share

thank you !! all what you said helped me figuring out something very important, i was replacing the current pixel alpha by the brush alpha which is not correct, in fact what i need is mixing the current pixel color with the target pixel color based on the brush alpha

avatar image melkior · Jan 31, 2014 at 12:50 AM 0
Share

Your welcome I'm glad that was what you were looking for!

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

19 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

Related Questions

Painting using a brush for iOS 1 Answer

Terran painting brush resolution. 2 Answers

When I attempt to paint my texture on an already existing texture, the texture doesn't change. Please help! 0 Answers

Lines to form? 0 Answers

fix brush size of the terrain 1 Answer


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