- Home /
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"
}
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.