- Home /
is it possible to make 2d real time coloring with masks (paint app)
Hello everyone,
So it comes up that i need to make a make-up game for girls, and one of the thing that am really stacked with is how to color a certain part of an object without effecting other objects, before explaining a little bit more, please watch 10 seconds of this video from the given time, as you can see when he is applying the blue color to the eyelashes, it appears like he is coloring it "step by step" for now the only solution that i can think about in unity is to break the eyelashes to more than one object and each time a user touch an object it get colored. But i want to know if there is a better solution for this ? if i have to make this in flash for example, i would add a mask in the eyelash layer that only make the eyelash shape visible and simply draw over it, any chance to make such a thing in unity ? thank you
Answer by robertbu · Jan 12, 2014 at 05:58 PM
You could use Texture2D.GetPixels32() and Texture2D.SetPixels32() to get and set pixels of the texture. You could also use some sort of a second texture to define one or more masks for the setting of color. Note that modifying the pixels of a texture requires the texture to be marked as read/write which doubles the amount of texture memory required for a specific texture. In addition, the operation to set pixels requires that you copy all the pixels for the texture making the operation slower than native texture setting methods. I think you would be okay for most platforms, but you'll have to do a performance test on your target platform. Note there are various examples of painting using Unity around, so I'd Google a bit and take a look at some example code.
thank you for revealing get and set Pixels32(), i hope they are not over-complicated to use otherwise i'll stick to my first approach since am not making a real paint application, about that, i looked for unity paint examples and unfortunately i wasn't lucky enough to find a good example, would you please post a link for that ? thank you
Here is one project I took a look at the when I was researching painting:
http://www.arongranberg.com/unity/unitypaint/
This is an older project, so I don't know if it will compile for the latest version of Unity. Figuring out Get and Set pixels will take a bit of work. You have a few challenges.
Figuring out what pixels to change. You can use the Raycast.textureCoord to figure out the center of where the person is dragging and clicking, but you will still have figure out which pixels beyond the center get changed. This could be based on distance and/or based on the mask.
The array's returned by Get/Set Pixels32 are 1D, so you have to calculate the 2D coordinates yourself.
Yes i found that project and i thought it's a little bit old too, i'll give it a try anyway,thanx.
And the pixels manipulation seems more expensive than expected (in term of performance, and coding) specially for this project so i'll keep to working with my first method hoping that i have a decent "drawing" effect. thanx again.
hey @robertbu i think it comes down to use one of setPixel solution, cause as i explained it here the masking approach seems to be a very bad solution, my question to you is, is it possible to draw with texture ?? for example, suppose you have 2 exact same size circles that have the exact same size texture but with different motifs, is it possible to draw on the first texture using pixel with the same coordinate from the 2nd texture ? if yes, can you please explain it more and show some examples (in c#) i will REALLY appreciate that, thank you
I don't have any special knowledge here, but I could work it out. It looks straight forward. Unfortunately I won't be back at my desk for a week. You can either open this as a new question, or you bump this question in a week and I'll take a look.