- Home /
Dynamically building sprite by using masks in unity
There is this tutorial in imagemagick
http://www.imagemagick.org/Usage/masking/#masks
I was wondering if there was some way to mimic the behavior (like cutting the image up based on a black image mask that turns image parts transparent... )
and then trim that image in game...
trying to hack around with the webcam feature and reproduce some of the imagemagick opencv stuff in it in Unity but I am sadly unequipped with masks, shaders etc in unity skill/knowledge. Not even sure where to start.
Answer by robertbu · Jul 22, 2014 at 05:45 PM
I was wondering if there was some way to mimic the behavior (like cutting the image up based on a black image mask that turns image parts transparent... )
Typically this is done with a shader, but it can be done procedurally. Here is one shader to get you started:
Ahhh, I Actually rolled some code using the texture set/get pixels and got it mostly "working", though I realized I think I am in trouble with this approach not being variant resolution friendly.
$$anonymous$$ostly it is to template a face with a picture you provide, then save the cut out face as a texture to use all over on planes again. Getting the picture you provide to match the face template requires image scaling and that, that where im running into trouble. Scaling a texture to line things up is not trivial it seems. If I could just smack the textures onto a plane and scale those that would be awesome but then I cannot build up the final cutout face texture that way (As scaling the plane with a texture does not scale the texture that I am cutting the pixels from).
I am still researching all this, I have to seriously dive into shaders too...the above code while readable Id have no idea how to implement with little to no shader experience.
Shane
Your answer
![](https://koobas.hobune.stream/wayback/20220613154719im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sprite Mask Render Issue 1 Answer
(2D) Rendering only the parts of a sprite that are within specific bounds 3 Answers
Sprite pixels problem on Android with mask 0 Answers
Mask a sprite mask?,Is there anyway to use a sprite mask under a rect mask? 0 Answers
How to mask sprites/animations using alpha channel 0 Answers