• 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 /
  • Help Room /
avatar image
Question by Doidel · Jan 10, 2018 at 07:55 AM · rendertexturedepth buffer

DepthNormals texture into custom global variable

I have a camera...

 _camera = GetComponent<Camera>();
 _camera.depthTextureMode = DepthTextureMode.DepthNormals;

...whose DepthNormals texture I use in a shader

 sampler2D _CameraDepthNormalsTexture;

Works perfectly!

But when I try to put the DepthNormalsTexture into my own global variable...

 _camera.targetTexture = new RenderTexture(_camera.pixelWidth, _camera.pixelHeight, 16, RenderTextureFormat.ARGB32);
 _camera.targetTexture.filterMode = FilterMode.Bilinear;
 Shader.SetGlobalTexture(_globalTextureName, _camera.targetTexture);

... it doesn't work. It's not the same. And I am unable to make it work in this RenderTexture + SetGlobalTexture way.

What I've tried so far:

  • Various sorts of RenderTexture formats

  • Assign the camera's RenderTexture directly to the shaders using them. This also behaved weirdly, that's why my guess is that my RenderTexture has different contents from _CameraDepthNormalsTexture.

    Any help is greatly appreciated!

Comment

People who like this

0 Show 0
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

Answer by StarkeyBoy · Apr 12, 2018 at 08:05 AM

@Doidel

When the camera renders to a texture, it renders what it can see (based on the applied culling mask filters) NOT the depth normals. You access the depth and normal data from within the shader using the inbuilt _CameraDepthNormalsTexture variable (which gives access to the GBuffer from the GPU side).

The only time that I've seen the shader variable being accessed outside the GPU is to pass them into a compute shader. If you are using compute shaders you have to pass this into the shader ComputeShader.SetTextureFromGlobal(CSMainkernalID, "_DepthNormalsTexture", "_CameraDepthNormalsTexture");

Hope this helps.

Comment

People who like this

0 Show 0 · 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

Answer by Doidel · Apr 13, 2018 at 08:03 AM

Thanks for your explanation @StarkeyBoy!
I am not using a compute shader. Instead, in my forward rendering setup, I have a surface shader which accesses the _CameraDepthNormalsTexture of a camera A with depth -1. There is also a camera B with depth 0 around, but like this, the surface shader uses camera A's _CameraDepthNormalsTexture.

Now if I switch the forward rendering setup to a deferred one, the surface shader doesn't work out of the box anymore. This didn't come as a surprise to me, and I thought the easiest way to circumvent this is to simply draw the camera's rendered depth and normal data (GBuffer) to a texture and then just reference that texture in my surface shader. After all, since I can do that with an albedo texture, why not with the depthNormals texture.

Should I also use something like MySurfaceShader.SetTextureFromGlobal(SSMainkernalID, "_DepthNormalsTexture", "_CameraDepthNormalsTexture"); or do you have a suggestion how I can make sure that my surface shader has access to camera A's GBuffer (which btw indeed has a specific culling mask)?

Thanks again for your effort, much appreciated!

Comment

People who like this

0 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 StarkeyBoy · Apr 15, 2018 at 04:59 PM 0
Share

Hi @Doidel,

I have not tried this but you may want to take a look at the 'Shader.SetGlobalTexture'. This will make the texture available for all shaders, but sound like it will do what you require. Take a look at the Unity API manual on this topic. Remember you will still need to decode the _CameraDepthNormalsTexture, in each shader where this is used, using the DecodeDepthNormal function contained within the UnityCG.cginc. Unless you decode this to a colour texture first and set that into a global texture.

I have used a compute shader in the past to output onto a render texture. I created a colour texture from the decoded normals and output this from the compute shader. This worked fine and allowed me visually see the normal map. Really I only did this as an experiment to see if could be done. In reality I was able to use the _CameraDepthNormalsTexture within the compute shader. If your working on after effects then take a look at compute shaders and use the compute shader.SetTextureFromGlobal function.

However I think from what you've described the Shader.SetGlobalTexture option looks the best for you. Good luck, I hope this does what you need.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Proper SetTargetBuffers usage with Deferred rendering in unity 5.5 (possible problem?) 1 Answer

Set Render Texture size without cropping? 0 Answers

how can I send the render texture to a new independent window of the main window of unity? 0 Answers

FPS y-axis mouse look not working on a render texture 0 Answers

9 sliced on 3d mesh 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