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 phatboy225 · Mar 03, 2014 at 06:01 PM · depth bufferrenderwithshaderrender to texture

Depth maps and reconstructing the MVP matrix from a light

In order to try and simulate translucency I'm attempting to read values from a depth map created from the perspective of a light.

In my script I'm collecting the view and projection matrices from a temporary camera attached to the light and passing them into my shader to calculate the MVP from light space.

 Shader.SetGlobalMatrix( "_LightViewMatrix", tempCamera.worldToCameraMatrix );
 Shader.SetGlobalMatrix( "_LightProjectionMatrix", lightProjectionMatrix );

(lightProjectionMatrix is the camera projection matrix corrected as per http://answers.unity3d.com/questions/12713/how-do-i-reproduce-the-mvp-matrix.html)

I then multiply these together with the _Object2World matrix in the vertex shader to create my custom MVP. I then multiply the vertex model space position by this matrix and use these coordinates to gather depth values from my depth texture.

 float4x4 MVP_MATRIX        = _LightProjectionMatrix * _LightViewMatrix * modelMatrix;
                 
 output.lightTexCoord    = mul( MVP_MATRIX, float4( v.vertex.xyz, 1.0 ) );
 output.lightTexCoord    = COMPUTE_POS( output.lightTexCoord );

 // doing this in the fragment shader
 // unconvinced this is right but I can't check it properly
 float depth = tex2Dproj( _CameraDepthBuffer, UNITY_PROJ_COORD(output.lightTexCoord) );

However, I can't tell if this is working because my depth texture seems to be off. I'm testing this by overlaying a GUITexture over the screen with the contents of the render texture, so that it's the same size to check against distortion. However, as I move the light close to the target the depth buffer image arrives from the right (see image below), rather than directly in front as I'd expect. It moves across the screen from right to left as I get closer to the model.

Render texture code:

 m_depthRenderTexture = RenderTexture.GetTemporary( Screen.width, Screen.height, 24, RenderTextureFormat.ARGB32 );
 tempCamera.targetTexture = m_depthRenderTexture;
 tempCamera.RenderWithShader( m_shader, null );

m_shader simply uses a depth example from the docs

 v2f vert( appdata_base v )
 {
     v2f o;
     o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
     o.depth = ComputeScreenPos( o.pos );
                 
     return o;
 }
             
 half4 frag(v2f i) : COLOR 
 {
     // UNITY_OUTPUT_DEPTH(i.depth);
                 
     //Grab the depth value from the depth texture
     //Linear01Depth restricts this value to [0, 1]
     float depth = Linear01Depth( tex2Dproj( _CameraDepthTexture, UNITY_PROJ_COORD(i.depth)).r);
  
     half4 c;
     c.r = depth;
     c.g = depth;
     c.b = depth;
     c.a = 1;
  
     return c;
 }

alt text

Any ideas? This has been frustrating me for a long time.

head.png (121.4 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Replacing and stacking shaders for some objects 1 Answer

Weird Material clashing in LWRP with the CustomForwardRenderData 0 Answers

Depth of field with multiple cameras 0 Answers

Question about Lists [C#] 0 Answers

Get RenderTexture 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