In a shader, I would like to know for a given projected 2D pixel if the related 3D point is visible by camera (not especially the main camera, the projection and view matrix are provided to the shader as parameters).
By visible, I mean I can shoot a ray from that 3D point to the camera position without hitting any object.
The result should be similar to the camera being a light source and test would fail where there is shadows.
I think one possible solution would be to first render scene from camera view into a texture (eg : render texture) and then use than information later in the shader for the main rendering.
EDIT : I forget to mention what I want in the end is Projective texture mapping. I have tried to implement what Stormwind describe (I did not used a Surf but a render texture) It almost works, however shadows are not aligned correctly (like they are moved by a given offset).
Here is the shader that I wrote :
float4x4 _CameraMatrix;
sampler2D _MainTex;
sampler2D _DepthTex;
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
float4 cameraPos = mul(mul(_CameraMatrix, UNITY_MATRIX_M), v.vertex);
o.scrPos = ComputeScreenPos(cameraPos);
return o;
}
half4 frag (v2f i) : COLOR
{
float4 cameraPos = i.scrPos;
cameraPos /= cameraPos.w;
if(cameraPos.x > 0.0f && cameraPos.x < 1.0f
&& cameraPos.y > 0.0f && cameraPos.y < 1.0f)
{
float depth = tex2D(_DepthTex, float2(cameraPos.x,cameraPos.y)).r;
float cameraDepth = cameraPos.z;
if(depth > cameraDepth)
{
return tex2D(_MainTex, cameraPos);
}
else
{
return float4(0.0f ,0.0f ,0.0f ,1.0f);
}
}
else
{
return float4(0.0f ,0.0f ,0.0f ,1.0f);
}
}
Shadow mapping
is the term I was looking for. Do you have any link or resource to know how to implement my own ? Or can I use Unity built in shadows feature to do that ? \$\endgroup\$