# Render points only if visible from another camera

I am trying to find a way to render only parts of the objects and scene that are visible from another camera. As an example, imagine I have two cameras pointing towards an object, the second making, say, a 45° angle with the first one. I want to render the scene from the first camera, and then render on the second a mask with only points that are visible from the first camera (like a visibility mask).

I can do this either using Nodes or Python (or something else). What I can think of now is :

• something similar to the Node Light Path output Is Camera Ray, used as a factor of a Mix Shader in Cycles. However, I would like to use another camera than the one being used for the render.
• In Python, I can render a depth map from the second camera, and then for each pixel, use the camera matrix and the depth information to backproject-it to a 3D point, and evaluate the distance of the first camera to that 3D point. If that distance is greater than the depth of the pixel on the first camera, I know that this pixel is occluded. So I would render it on the mask.

Is there any simpler way to do so ? Thanks in advance for your time !

## 1 Answer

All you need - projection mapping. Just put additional UV layer on objects which you want to control, add UV project modifier, and add new logic to material. Second camera projects normalized UV coordinates [0;1][0;1], but we can calculate if some point is outside and apply different material to it. Emissive like on first screenshot, or Transparent like on second one.

OK, lets get really freaky!

Of course, you have to render Z-channel to some EXR file (or sequence if you have an animation). Then add more to material: drivers to get camera position, math nodes to calculate distance for sampling point, and little more to avoid another reincarnation of z-fighting problem.

Just one fix to previous: that UV projecting produces "square", so you have to correct aspect (based on projecting camera render), it was 1080/1920=0.5625 to ScaleY.

I love Cycles magic!

• Thanks a lot. This is a great way to use the UV map, I haven't thought about it. However, this is only doing truncation of parts of the objects that do not project on the screen of the first camera. This does not hide in the second render portions of objects that are hidden because behind objects closer to the camera. Thanks again for your help Jul 15, 2015 at 13:01
• @DanilRomanov That's awesome ! You just converted the words to nodes :) And as I've read your cycles setup, it should work exactly as I want it to. Thanks really. Jul 16, 2015 at 8:48
• Hi, I've finally had time to test it for real. The problem is that I have trouble manipulting the UV Projector modifier. I've done everything as I should (with the simple first case for now testing UV against 0-1), and I can see the test does not work as expected - it seems like the projection is not working as expected, see image imgur.com/DTzjuLH and blend file : <img src="http://blend-exchange.giantcowfilms.com/embedImage.png?bid=531" /> (cannot upload EXR z image, but not useful for the simple setup). Can you find what's the cause ? Sep 30, 2015 at 9:04
• @DanilRomanov And I was using a square camera to avoid the scale effect in the scaleY of the projector. And thanks again for your help ! Sep 30, 2015 at 9:08
• Sorry for the several comments, but the updated link for a more simple version of the image and blend files are here : imgur.com/9xHATtP and blend-exchange.giantcowfilms.com/b/532 And the weird thing is that the same UV projector works with an orthographic camera. May be a bug, can someone tell me where to fill a bug request ? Sep 30, 2015 at 9:28