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 crushingcups · Mar 06, 2016 at 09:02 PM · camerarendertexturedepthrender texture

How can I take depth into account when rendering to texture?

Okay so basically I have an object with particles around it. I wanna apply a special image effect only on my particles (metaballs), so I put the particles on a separate layer, and render to texture from a separate camera that only sees this layer. I create a material that uses this render texture, and I apply it to a plane that rotates with my main camera. Everything works as expected, except for the fact that since I'm rendering to a plane, and the plane is closer to my camera than the actual scene, it gets drawn over my scene. How can I take into account the depth when I render to my texture so that my geometry is drawn over the particles? Or is my setup wrong? Any ideas?

What my scene looks like: http://i.imgur.com/mRCSC3U.png

What the game view looks like: http://i.imgur.com/McojxnE.png

Main camera settings: http://i.imgur.com/SEX5KtU.png

Blob camera settings: http://i.imgur.com/m36YL1D.png

Comment
Add comment · Show 1
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 crushingcups · Mar 07, 2016 at 02:05 PM 0
Share

I'm doing it with OnRenderImage now, ins$$anonymous$$d of the plane. The problem I have now is that the cameras have different depth, so either I just get the blobs, or I just get the object with the particles. If I set the blob camera to "Don't Clear" I get this http://imgur.com/dB$$anonymous$$r$$anonymous$$XS which is correct except for the fact that I just want the postprocessing effect on the particles but not the object.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Naphier · Mar 06, 2016 at 09:10 PM

You might need a third camera that is the same as your main camera. Have the new camera be the one that renders the render texture for the blobs and alter the depth of this new camera accordingly. You might be able to do this with the blob camera if the effect isn't fullscreen.

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 crushingcups · Mar 07, 2016 at 10:55 AM 0
Share

As suggested I'm not rendering to texture anymore and I'm just using OnRenderImage to post process the particles into blobs. The problem is that since the depth of the two cameras is different I either get this http://i.imgur.com/Jk$$anonymous$$ftem.png or this http://i.imgur.com/Unc9qVc.png depending on the relative depth. Would a third camera help in this case?

avatar image
0

Answer by tanoshimi · Mar 06, 2016 at 09:14 PM

Why do you draw the rendertexture to a plane in the scene? If the particles are on a separate layer with a separate camera, it would probably make more sense to apply the metaball material as an image effect in the OnRenderImage of that camera:

 void OnRenderImage (RenderTexture source, RenderTexture destination)
     {
         Graphics.Blit (source, destination, material);
     }

Comment
Add comment · Show 7 · 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 crushingcups · Mar 07, 2016 at 10:49 AM 0
Share

You're right, I'm doing it with OnRenderImage now. I still have a problem with the depth though: with my main camera's depth is set to 0, and the blob camera is set to -1 I see this http://i.imgur.com/Jk$$anonymous$$ftem.png so no effect is visible, and when I change the blob camera's depth to something higher than zero I get this http://i.imgur.com/Unc9qVc.png so the effect is the only thing I see. I don't know how to blend the two.

avatar image tanoshimi crushingcups · Mar 07, 2016 at 02:08 PM 0
Share

You should draw the effect on top (i.e. your metaball camera should have a higher depth), but it looks like the blend mode on your metaball material is wrong. Are you using alpha blending and, if so, are you setting transparent pixels to all parts of the screen not covered by the metaball particles?

avatar image crushingcups tanoshimi · Mar 07, 2016 at 02:19 PM 0
Share
 Shader "Custom/Blobs" 
 {
     Properties
     {
         _$$anonymous$$ainTex("Texture", 2D) = "white" { }
         _Threshold1("Threshold 1", Range(0.001,1.0)) = 0.05
         _Threshold2("Threshold 2", Range(0.001,1.0)) = 0.5
         _Threshold3("Threshold 3", Range(0.001,1.0)) = 0.3
     }
     SubShader
     {
         Pass
         {
             Blend SrcAlpha One$$anonymous$$inusSrcAlpha
             CGPROGRA$$anonymous$$
             #pragma vertex vert
             #pragma fragment frag    
             #include "UnityCG.cginc"    
             float4 _Color;
             sampler2D _$$anonymous$$ainTex;
 
             struct v2f
             {
                 float4  pos : SV_POSITION;
                 float2  uv : TEXCOORD0;
             };
             float4 _$$anonymous$$ainTex_ST;
 
             v2f vert(appdata_base v)
             {
                 v2f o;
                 o.pos = mul(UNITY_$$anonymous$$ATRIX_$$anonymous$$VP, v.vertex);
                 o.uv = TRANSFOR$$anonymous$$_TEX(v.texcoord, _$$anonymous$$ainTex);
                 return o;
             }
 
             float _Threshold1;
             float _Threshold2;
             float _Threshold3;
 
             half4 frag(v2f i) : COLOR
             {
                 half4 texcol = tex2D(_$$anonymous$$ainTex, i.uv);
                 half4 finalColor = texcol;
                 if(texcol.r > _Threshold3)
                 {
                     finalColor = half4(texcol.r, texcol.g, texcol.b, texcol.a);
                     finalColor = floor(finalColor / _Threshold1)*_Threshold2;
                 }
                 return finalColor;
             }
             ENDCG
         }
     }
     Fallback "VertexLit"
 }

This is what my shader looks like, you mean change the line with SrcAlpha?

Show more comments
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Fake crystal using render textures 1 Answer

camera for rendertexture doesn't render in urp+vr,The camera for rendertexture doesn't render in vr + urp. 0 Answers

Render texture for map doesn't render all objects. When given a Cinemachine 2d camera controller it renders the missing objects at the perspective of the main camera 0 Answers

RenderTexture behaviour inconsistent to Cameras, Rendering two times to a RenderTexture respecting depth, Rendering object visibility 0 Answers

"Motion vectors" and "depth with normals" from camera's target texture 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