- Home /
Culling part of a sprite in Sprite Manager 2
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.
Your answer
Follow this Question
Related Questions
UI masking in a jet fighter HUD 0 Answers
Gradient mapping in shader graph 1 Answer
How to resize shader area? 0 Answers
Shader - Hide self overlapping mesh 1 Answer
Pixel snap for material made with Unlit Shader Graph? 1 Answer