"Painting" transparency

Imagine in your game you have a wall like cube object with a texture. It is opaque, ie, you can not see what is behind the cube/wall.

How can we use mouse to “paint” transparency on an opaque surface/cube/wall?
Wherever mouse cursor moves on this opaque surface become transparent and we will see through those regions. Any idea how to achiew this effect?

I’m not sure how good your scripting skills are, but there are a couple of functions that would let you do this.

You want to shoot a ray where the mouse is, and find the UV coordinate of the texture.
RaycastHit.textureCoord Unity - Scripting API: RaycastHit.textureCoord

Then set the alpha channel at that x,y coordinate so it’s transparent.
Texture2D.SetPixel Unity - Scripting API: Texture2D.SetPixel

However that will just do one pixel, tough part is a “brush” shape. In your picture that would be a box orientated in the direction of the mouse.

callahan.44 knows what they’re talking about. On top of that you could look at masking the opaque object with shaders by using a grabtexture mapped shader (Unity - Manual: ShaderLab command: GrabPass) on an object that renders over the top of the opaque object, or screen-space image manipulation of multiple cameras where the opaque object to be rendered transparent is rendered separately, or applying alpha values to areas of the texture based on the mouse input using RaycastHit.textureCoord and using a greyscale texturemap for the alpha “brush” which modifies the map.

All of this requires a lot of shader manipulation. Look at Strumpy’s free shader editor for an easy way of achieving this :slight_smile:

http://forum.unity3d.com/threads/56180-Strumpy-Shader-Editor-Beta-3.1-released-(performance-usability-upgrade)

1 Like