• 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
1
Question by MichaI · Sep 22, 2019 at 09:04 PM · camerashaderrenderingdepthrender texture

"Motion vectors" and "depth with normals" from camera's target texture

Is there a way to get motion vectors and depth with normals from camera by it's target texture, or in some other way? I cannot use global shader variables _CameraMotionVectorsTexture and _CameraDepthNormalsTexture because I use two cameras. I want to access only data from my second camera and not from the main camera. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MichaI · Sep 23, 2019 at 07:42 PM

Ok, I figured it out, I used OnRenderImage method and called inside Graphics.Blit with material that using my shader, inside which I am getting this textures through _CameraMotionVectorsTexture, and _CameraDepthNormalsTexture samplers (in my case _CameraDepthNormalsTexture didn't worked so I used _LastCameraDepthNormalsTexture). Inside fragment shader I combine them and return to target texture. But motion vector texture gives me weird results and I don't know exactly how to use it. At least I managed to get to work normals with depth. I post code and shader in case someone needs it.

Code:

 public Camera Cam
 {
    get
    {
         if(_cam == null)
         {
             _cam = GetComponent<Camera>();
         }
         return _cam;
    }
    set
    {
         _cam = value;
     }
 }
 private Camera _cam;
     
 protected void Awake()
 {
     Cam.depthTextureMode |= DepthTextureMode.MotionVectors | DepthTextureMode.DepthNormals;
 }
     
 private Material _onRenderMaterial;
 protected Material OnRenderMaterial
 {
     get
     {
         if(null == _onRenderMaterial)
         {
             _onRenderMaterial = new Material(Shader.Find("Hidden/GrassPhysics/OnRenderCamera"));
         }
         return _onRenderMaterial;
     }
 }
     
 private void OnRenderImage(RenderTexture source, RenderTexture destination)
 {
     Matrix4x4 viewToWorld = Cam.cameraToWorldMatrix;
     OnRenderMaterial.SetMatrix("_viewToWorld", viewToWorld);
     Graphics.Blit(source, destination, OnRenderMaterial);
 }

Shader:

 Shader "Hidden/GrassPhysics/OnRenderCamera" {
 
     Properties
     {
     }
     SubShader
     {
         Cull Off
         ZWrite Off
         ZTest Always
 
         Pass
         {
             CGPROGRAM
             #pragma vertex vertexShader
             #pragma fragment fragmentShader
 
             #include "UnityCG.cginc"
 
             struct vertexInput
             {
             float4 vertex : POSITION;
             float2 uv : TEXCOORD0;
             };
 
             struct vertexOutput
             {
             float2 uv : TEXCOORD0;
             float4 position : SV_POSITION;
             };
 
             vertexOutput vertexShader(vertexInput i)
             {
             vertexOutput o;
             o.position = UnityObjectToClipPos(i.vertex);
             o.uv = i.uv;
             return o;
             }
 
             uniform sampler2D _CameraMotionVectorsTexture;
             uniform sampler2D _LastCameraDepthNormalsTexture;
             float4x4 _viewToWorld;
 
             float4 fragmentShader(vertexOutput i) : SV_TARGET
             {
                 float4 col = tex2D(_LastCameraDepthNormalsTexture, i.uv);
 
                 //decode depthnormal
                 float3 normal;
                 float depth;
                 DecodeDepthNormal(col, depth, normal);
 #ifdef UNITY_REVERSED_Z
                 depth = 1 - depth;
 #endif
                 normal = mul((float3x3)_viewToWorld, normal);
                 return float4(depth,normal.rb,1);
             }
             ENDCG
         }
     }
     Fallback Off
 }

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.

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

239 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 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

Rendering different shader when camera get near object 1 Answer

How do I do custom camera render between opaque and transparent objects? 0 Answers

Second camera has higher depth, but terrain still occluding camera image. 1 Answer

Render inside of mesh as black 2 Answers

How to get depth texture and render texture from one camera 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