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 · Jan 11, 2014 at 01:25 PM · shadercgmatrix

Depth map from a second camera at a light position

I'm looking into material translucency and I'm struggling to follow an example from GPU Gems (http://http.developer.nvidia.com/GPUGems/gpugems_ch16.html) at Example 16-4.

I'm currently using a camera attached to a light to generate a depth map which I then reference in my shader. Most of the example I can find of generating and using a depth map rely on doing so from the main camera, which I'm not doing, so using ComputeScreenPos() etc isn't any good for me.

So, I'm passing in the camera projection matrix from the light instead and referencing the light matrix using _Object2Light0[0]. I'm hoping this is the correct way to get the light space matrix but I find the documentation pretty sketchy (http://docs.unity3d.com/Documentation/Components/SL-BuiltinValues.html), and not everything seems to be available according to update documentation.

On top of this Unity is now compiling my shader and commenting out the SubShader because it says it's using Unity 2.x per-pixel lighting. I have no idea what I'm using that's causing it to do so, and again, documentation appears to be scarce. I can't play around with my shader anymore until it compiles OK and doesn't comment out my code.

Any ideas?

 Shader "Custom/depthTest" 
 {
         Properties
         {
             _ScatterTex ("Scatter Texture", 2D) = "white" {}    
         }
         #warning Upgrade NOTE: SubShader commented out; uses Unity 2.x per-pixel lighting. You should rewrite shader into a Surface Shader.
     /*SubShader 
         {
             Tags { "RenderType"="Opaque" } 
             Pass
             {            
                 CGPROGRAM
                 #pragma vertex vert
                 #pragma fragment frag
                 #include "UnityCG.cginc"
                 
                 sampler2D _ScatterTex;
                 sampler2D _CameraDepthTexture;
                 float4x4 _LightProjectionMatrix;
                 
                 struct v2f
                 {
                     float4 pos                : SV_POSITION;
                     float4 fragPos            : TEXCOORD0;
                 };
                 
                 v2f vert( appdata_full v )
                 {
                     v2f output;
                 
                     output.pos = mul( UNITY_MATRIX_MVP, v.vertex );
                     output.fragPos = v.vertex;
     
                     return output;
                 }
                 
                 half4 frag( v2f input ) : COLOR
                 {
                     // translate the point into light space
                     float4 PointInLightSpace = mul( _Object2Light0[0], input.fragPos );
                     // translate the point into the projection space of the light/depth map
                     float4 texCoord = mul( _LightProjectionMatrix, PointInLightSpace );
                     // get the depth and set it into the range 0-1
                     float d_i = Linear01Depth( tex2Dproj( _CameraDepthTexture, UNITY_PROJ_COORD( texCoord.xyw ) ).r );
                     // translate the point into light space
                     float Plight = mul( _Object2Light0[0], float4( input.fragPos.xyz, 1.0 ) );
                     
                     float d_o = length( Plight );
                     
                     float distance = d_o - d_i;
                     
                     return tex2D( _ScatterTex, float2( distance, 0 ) );
                 }
                 
                 ENDCG
             }     
         }*/
         FallBack "Diffuse"
     }
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 phatboy225 · Jan 12, 2014 at 05:16 PM

It was _Object2Light0[0] causing the problem.

http://en.wikibooks.org/wiki/Cg_Programming/Unity/Cookies references _LightMatrix0 and you need to include Autolight.cginc to access it.

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

18 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

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

Efficient code for shortest distance inside a fragment shader 1 Answer

UnityObjectToClipPos is inverted? 0 Answers

Shader programming question for Cg expert : Scaling textures 5 Answers

shader inverse(float4x4) function 2 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