Blurring based on distance to camera

Greetings UnityDevs!

I am trying to wrap my brain around some blurring techniques. What I have are two cameras: 1 rendering a background and a 2nd camera rendering some floating objects. The objects are placed around a circle ( root transform ). What I am trying to do is rotate the ring to specific points so the objects at that point come into focus, and rest are blurred.

I tried using a Depth of Field shader and for the most part it somewhat works, but it is a post screen blur and therefore blurs my background. I was curious if anyone out there has managed to blur specific objects from camera X…

Normally I would make a shader that passes an attribute: distanceToCamera and apply a blur on the fragment shader. Though I know little of how Unity’s shader system works…

Thanks in advance

You can attach a script to the object you want to check against, work out the distanceToCam and then pass this to the shader:

renderer.sharedMaterial.SetFloat( "_distToCam", distanceToCam );

Another way would be to test each fragment in the shader with the camera position:

#include "UnityCG.cginc"
float3 _WorldSpaceCameraPos

if the camera has been tagged as MainCamera. And then use this position to base your calculations on.