Display Half a Texture/Object

I want to have a texture/object only be rendered as it appears within a certain bounding box. I have attached an image of what i’m trying to accomplish. Lets say I have a cube with a happy face texture on it. It is moving downward. I have a bounding box in my scene and in the view of the camera. What I want: The texture on the cube does not show until it enters the bounding box. Once it touches the bounding box, the only part of the texture that is shown is the part in the bounding box. As the cube moves more into the bounding box, more of the texture is shown. And when it leaves the box, the part of the cube that is now out of the box is now not visible again.

Perhaps this is not possible?

Maybe Projector (Component->Rendering->Projector) with Ortographic Size equal to your bounding box size…
Projected texture should have Wrap Mode = Clamp
and shader should be similar to this one:
iPhone Standard Assets\Additive-Projector\Additive.shader

Ultimately, it’s a scripting question, and you’d likely get more responses in the scripting forum.

But I can think of 3 ways to pull this off:

  1. If you have pro, have a camera pan by the smilely face material and render the results to another texture.
  2. Put the smiley face in the middle of a large, mostly empty texture and use texture offset/scale to scroll a portion of the smiley face.
  3. Use the Texture2D functions GetPixels() and SetPixels() to create a new texture, but those may be too expensive for the iPhone unless your texture is small.

If it is 2d then simply clip your rectangle and uv coordinates.

If it is 3d then you could try a set of transparent sprites drawn around it making sure that their Z value is lower than that of the box you are trying to clip.

warmi what do you mean clip the rectangle and uv coordinates. How do I do that?

I think it’s possible to “mask” objects with the right shader. Check this thread out: http://forum.unity3d.com/threads/26945-Diablo-style-health-orb

If this is for GUI then create a group for the bounding box and the use GUI.DrawTexture to display the texture within the group. GUI elements drawn inside a group use coordinates that are relative to the group rectangle so you can use negative coordinates to make the image start outside the group rectangle (the image gets clipped to this rectangle).

If you are not doing GUI then none of this advice will help at all :wink:

This actually did exactly what I was looking for:

Anyone know where I can learn more about culling in unity?