Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by conna · May 08, 2014 at 11:43 AM · camera3dtransparencyrendertexturerender

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Graham-Dunnett ♦♦ · May 08, 2014 at 11:45 AM 0
Share

Presumably you're using RenderToTexture, in which case can't you set the alpha of the camera background colour?

avatar image conna · May 08, 2014 at 01:53 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image conna · May 08, 2014 at 01:51 PM 0
Share

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.

avatar image Paximillian · May 08, 2014 at 01:59 PM 0
Share

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.

avatar image conna · May 08, 2014 at 02:01 PM 0
Share

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.

avatar image Paximillian · May 08, 2014 at 02:19 PM 0
Share

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.

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

why does my temp camera only renders transparent objects ? 1 Answer

Howto render camera into texture while still rendering into scene view? 1 Answer

Render Camera View Help :( 1 Answer

Masking camera from rectangular to arbitrary shape 0 Answers

what does Camera.targetTexture do? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges