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.

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.

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!