Hello, I have no idea how to solve the following problem:
I need to cut off a part of a sprite, so the first half is rendered properly, and the second is not rendered at all (totally translucent). I thought I'd have to do a shader for that but I couldn't really write anything that made sense. I need to adjust how much/what part of a sprite is cut off on the go. By "what part" I mean that I need to be able to not only cut off (for example) right half of the sprite, but also the bottom and along any axis (not really simultaneously).
If you know how to solve this problem (even partially), please post an answer! Many thanks!
Answer by Brady
Apr 28, 2011 at 07:50 PM
There are two ways to do this:
To cut off just one side of a sprite, use one of the Truncate* methods. For example, TruncateRight(0.5f) will truncate the right side of the sprite so that half of it is still showing. TruncateBottom(0.25f) will only leave the top 25% showing (cuts off the bottom).
These methods only cut off one side at a time, however. To cut off multiple sides, use a clipping rect. To do that, decide the rectangular area, in world space, you want to confine the sprite to, and outside of which, the sprite will get cut off. Think of this sort of like framing the sprite.
Create a Rect that describes this rectangular area, we'll assume you call this "clip2D". Then, do this:
Rect3D clip3D = new Rect(clip2D);
Now you can apply the clipping to your sprite:
mySprite.ClippingRect = clip3D;
That's all there is to it.
Thanks, I tested both methods and they work really well. It doesn't completely solve the problem, but certainly is much faster and more elegant than the solution I came up with.
I needed this for 2D portals (those from "Portal"). I know that I could settle for depth tricks, but I wanted two sides of the portal to be available to go through, so for example, two object can use each side of one portal simultaneously.
I wrote a shader that cuts off a part of a texture in a material along any axis. I managed to use it and get any rotated sprite work with a portal at any angle, though it's a mess.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users post good questions and use the site effectively we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
double-sided plane with texture?
UI masking in a jet fighter HUD
Dynamic Shadows in Texture Space Diffusion
2D - draw sprite only on one background color
Shader wants texture coordinates, but the mesh doesn't have them