- Home /
Render a GameObject/mesh to a -transparent- texture at run-time
I've searched about and tried almost everything I can think of, but I am having extreme difficult achieving the following:
Render a GameObject to a transparent texture.
Have a Plane use the texture, so that the plane is transparent except from where the object is rendered.
This effectively achieved the effect of just rendering the model.
Thank you.
Presumably you're using RenderToTexture, in which case can't you set the alpha of the camera background colour?
Hi, I'm using RenderTextures yes, but setting the camera's background color alpha to 0 will still render the solid color as the background in the final texture (using ReadPixels) to convert the RenderTexture to a Texture. It's most bizarre.
Answer by Paximillian · May 08, 2014 at 12:42 PM
Well, I can't immediately think of a way to achieve this with conventional means, but you might be able to do it in a brute force approach, and I'm not even sure it would work well since that depends on your use for this heavily.
Unity has a function for taking screenshots of your game Application.CaptureScreenshot(). The idea is to use that to screencap only the GameObject you want through the use of layers, and then call on an outside program to remove the Camera background's color and color it to alpha, and then load it after it's finished back as a new texture into the game.
The problems with this are many, it's slow, it's unreliable, you would need another program that can actually do the alpha manipulation, and even then I'm pretty sure it would only work on PC.
Another really big issue is that CaptureScreenshot is based on Application and not Camera, so you would need to do some of the sort of changing the active camera to one that only views the desired layer used by the GameObject, take the screenshot, and then change the camera back, resulting in a 1 frame stutter at the best case scenario.
Like I said, I don't think there's a simpler way. There're similiar things you can do using Unity Pro to render a camera's view to a texture using Render Textures, and then using a projector to show the result on a plane, but I can't really think of a good solution for the free version.
Hi, thanks for the reply. I'm using Pro. I've managed to get everything working to the point where I can render to a texture the object exactly how I want it, but I cannot for the life of me have the background transparent. I have tried everything I can think of. It seems Unity is actually limited here.
Well, it might actually be limited in that regard, but maybe there's something else that can be done. What do you need this for? A mirror? A character creation system? You can try rendering this along with the actual background you want behind it.
That's true, I had thought about that, this is basically an Avatar image that renders a player's avatar for use in scoreboards and things like that.
Then you should certainly know what the background is supposed to be =)
Even better, you can carry around a plane behind the character with your wanted background the whole time, and just tell the main camera to ignore its' layer, that way you can just capture smoothly from the secondary camera while not affecting your actual game.
Answer by MMOERPG · May 26, 2014 at 08:02 PM
var tex = new Texture2D(imageWidth, imageHeight, TextureFormat.ARGB32, false );
*// Read screen contents into the texture*
tex.ReadPixels(new Rect(0, 0, imageWidth, imageHeight), 0, 0 );
tex.Apply();
int x;
int y;
for(y = 0; y < imageHeight; y++)
{
for(x = 0; x < imageWidth; x++)
{
Color c = tex.GetPixel(x, y);
c.a = 1.0f;
tex.SetPixel(x, y, c);
}
}
tex.Apply();
*//turn all pixels == background-color to transparent*
Color bCol = cam.backgroundColor;
bCol.a = 0.0f;
for(y = 0; y < imageHeight; y++)
{
for(x = 0; x < imageWidth; x++)
{
Color c2 = tex.GetPixel(x, y);
if (c2.r == bCol.r && c2.g == bCol.g && c2.b == bCol.b)
{
tex.SetPixel(x, y, bCol);
}
}
}
tex.Apply();
This script reads all pixels on the screen, turns all of their alpha channels to 1.0f (visible,) then compares each pixel to the background color. If a pixel equals the background color, the alpha channel is changed to 0.0f (transparent.) (The camera must only be rendering the image and the background color for this to work.)
In the editor, once you have your texture, be sure to check the box "Alpha is Transparent" and click apply.
In the editor, once you've applied your texture to a material, change the shader to transparent/diffuse.
In the editor, apply the material to your plane mesh and your done!