1
\$\begingroup\$

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 saddly unequipped with masks, shaders etc in unity skill/knowledge. Not even sure where to start.

\$\endgroup\$
1
  • \$\begingroup\$ Please accept your answer, the website will keep bringing your question to the frontpage periodically if you don't (which is why I saw this question nearly a year after it was asked), thank you \$\endgroup\$ Feb 18, 2015 at 9:36

1 Answer 1

0
\$\begingroup\$

Ended up just taking the picture the mask and copying the pixels across if not a masked pixel:

Texture2D BuildMask(Texture2D tex)
{
    int x =0;
    int y=0;
    int width = Mathf.FloorToInt(maskTex.width);
    int height = Mathf.FloorToInt(maskTex.height);


    Texture2D tmpTexture = new Texture2D(maskTex.width, maskTex.height);
    //set all the tmpTexture to alpha

    int farthestLeft=0;
    int farthestRight=0;

    for(int yv=y; yv<height; yv++)
    {
        for(int xv=x; xv<width; xv++)
        {

            if(maskTex.GetPixel(xv,yv).a == 1)
            {
                if(farthestLeft <= xv)
                    farthestLeft = xv;

                if(xv>=farthestRight)
                    farthestRight =xv;


                tmpTexture.SetPixel(xv, yv, tex.GetPixel(xv,yv));
            }
            else
            {

                tmpTexture.SetPixel(xv, yv, new Color(0,0,0,0)); //0,0 bottom left.
            }
        }
    }

    tmpTexture.Apply();
    return tmpTexture;
  }
\$\endgroup\$
1
  • \$\begingroup\$ That's a solution. However I'd suggest you to use GetPixels/SetPixels instead of SetPixel: way faster and optimized when you have to work with big textures. You just have to work with an array of Color instead of working directly with your texture colors. \$\endgroup\$
    – lvictorino
    Sep 21, 2014 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .