5
\$\begingroup\$

I'm working in Unity, using the 2D features to recreate the match-3 style of 10000000 (Ten Million).

I have a parent object (green square) with several children objects (blue rectangles) on top of it, as pictured in the first image. I want to only render the parts of the child objects that are over top of the parent object, as shown in the second image.

I can figure out the size and position of everything just fine, what I'm having trouble with is figuring out how to mask off the parts of the child objects that don't overlap with the parent.

Non-masked Masked

\$\endgroup\$
  • \$\begingroup\$ Did you try posting on the Unity Q&A site? \$\endgroup\$ – ashes999 Mar 14 '14 at 22:03
1
\$\begingroup\$

Use a shader with an alpha mask. The Unify Community Wiki has an excellent alpha mask shader, and it's remarkably simple to use. I've done a few tricks with stuff like this (such as making a "night" scene with lighting by putting a semi-transparent overlay over the scene, and using alpha-masking to "cut out" the overlay, where there was light).

\$\endgroup\$
  • \$\begingroup\$ This looks like exactly what I need, but I'm having trouble getting it to work with a SpriteRenderer. How would you adapt the technique for use with the native 2D tools? \$\endgroup\$ – NobodyNothing Apr 7 '14 at 15:39
  • \$\begingroup\$ You should be able to just change the material on the sprite renderer to a custom material that uses the proper shader. I don't think you should even need to do anything special because it's going to be for a sprite. It should just work, as far as I can tell. \$\endgroup\$ – Gurgadurgen Apr 8 '14 at 14:01
  • \$\begingroup\$ Apparently the SpriteRenderer doesn't play nice with fixed function shaders: "Material uses fixed function shader. It is not compatible with SpriteRenderer." Porting even a simple shader like the mask to Cg is beyond me at the moment though. \$\endgroup\$ – NobodyNothing Apr 8 '14 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.