Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JonnyHilly · May 17, 2019 at 03:01 AM · shadersrender texture

How to render a 3D object with various alpha into a render texture, such that the alpha is maintained

I have setup a render camera with alpha 0 clear setting. The render texture target is 32 bit RGBA I have a 3D object with a 32bit alpha texture ( think of a holey object, where the holes are alpha 0, not geometry holes ) I render the 3D object into the texture... if I use a standard opaque shader like Legacy/Diffuse, the result is as expected... the render texture has alpha edges from the camera clear, with my object in the middle, but the alpha holes don't show through onto the render texture. So the render texture has both alpha 1 and alpha 0 areas. So far so good.

If I render my with an alpha shader, then it renders into the render texture, using the alpha channel does indeed cut holes, and whatever color the camera clear's color is set to.... shows through the holes.

BUT. it dies not "RENDER" the alpha from my 3D object's texture into the render texture. So I cannot use that texture in the scene like a sprite, or like just rendering the 3D object would look.

Summary.
1) rendering diffuse/non transparent shader into the render texture, writes alpha 1 into it (regardless of the alpha in the texture or shader's output) 2) rendering with alpha transparent shader into the render texture, uses the alpha to control how visible the texture is rendered into the render texture, but does not render the Alpha into the texture's A channel. The A channel is just 100% zero.

How do I make the shader write the RGB and "A" into the render texture please? (not from the camera clear, but from the shader render) This should be possible. I've tried various shader blend and op modes... with no luck. Help appreciated. Thanks in advance

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SunnyChow · May 17, 2019 at 07:22 AM

the problem is most of the shaders don't concern alpha channel. some just ignore the alpha channel (in the source code, some of them includes code "ColorMask RGB", which ignore alpha channel). other render on alpha with insuitable blending.

I think the ultimate solution is to use Camera.RenderWithShader() with a custom shader that only renders alpha channel

Comment
Add comment · Show 1 · 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 JonPQ · May 17, 2019 at 03:56 PM 0
Share

thanks for the reply, appreciated. How do you render into the Alpha channel though ? The camera clear does it, but how to make a shader do it ? $$anonymous$$y shader does not nclude Color$$anonymous$$askRBG...unless "Lambert" lighting choice does it somehow.

I am writing my own shaders, but I'm trying to do this all in 1 render for performance.

I "could" make a separate render, that just renders alpha, into say an R channel of a second texture, then use another shader to combine the 2 textures, using that R as alpha channel... but it would make things a lot less performant. Ins$$anonymous$$d of 1 render to get texture. I'd need 2 renders, plus a more complex combining shader to render it. Still trying to figure out how to do it in one render.

$$anonymous$$aybe I need to write the source color and alpha blend modes separately.. any idea how ?

avatar image
0

Answer by JonPQ · May 17, 2019 at 05:56 PM

ok found the solution... saw many other previous searches so hopefully this will help some others also. The Unity Lambert lighting overrides what the Blends modes say to do, and wipes out the alpha assuming that you don't want it. ( you can look at the compiled shader code to see this ) If you want to write your alpha to the render texture, then add the magic keyword 'keepalpha'

      #pragma surface surf Lambert keepalpha    

It doesn't hurt to specify alphamask rgba either

       ColorMask RGBA

can also remind user to use RGBA in the shader's texture field....

 _MainTex("Texture (RGBA)", 2D) = "white" {}




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

116 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 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 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 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 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 avatar image

Related Questions

Shader Help? Need an Alpha Test, then a "set pixel" sort of command 1 Answer

creating portal effect using a render texture and Shadergraph with screen position node in VR 0 Answers

DrawMesh / Stencil / RendureTexture topic 0 Answers

How do I create a reflection render texture and assign it to be used as part of a shader? 0 Answers

Why CYAN is default object color (with default materials & shaders) 0 Answers


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