Make *full* object transparent based based on *some* pixels

I’ve made myself a shader that dithers objects based on camera distance, with the goal of ensuring that if they’re occluding something the camera is trying to look at, they go translucent. Pretty easy/standard stuff. However, I’d like it if the full object was dithered in this case, not just the pixels in question. Is there a way to do this?

Some of my thoughts:

  • I don’t want to use the object’s position, because the pivot may be in a totally unrelated spot
  • I tried using the screen position, but it’s basically the same problem – only the pixels in the given spot are dithered, which makes sense.
  • I’ve thought about trying to pass around globals, but didn’t really see a good way to do it.
  • I can obviously do this differently – raycast and adjust the alpha – but I don’t currently have colliders on many of these things (tree leaves and whatnot) and, more importantly, I’m just curious if there is a way to do this.

Using shader graph / URP

I don’t think this can be done in shader graph, unfortunately. Or through shaders at all, assuming your objects aren’t extremely simple geometry like spheres.

So I’d say go with plain ol’ raycasting. If targets are detected between the camera and the target object, set a shader property to 1 to enable dithering. You can use the MaterialPropertyBlock class to set shader properties dynamically: Unity - Scripting API: MaterialPropertyBlock