- Home /
Shader - Don't render parts at certain distance
Hey all! I've written a shader, which uses quite a few passes to achieve the right effect. Obviously, this is very expensive, and the issue is that some of these passes are only really necessary close up. So I'm wondering whether it's possible to skip the rendering of certain pixels if they are too far away from the camera, or to skip out entire passes depending on the distance from the camera. I could even make do with a custom fallback, meaning it uses a different shader at a certain distance. Is there any way I can achieve this? I won't say I'm an expert at CG or ShaderLab, but I do understand the basic concept of how shaders work, and it seems like one or all of the above should be possible...
I have an idea: Click on your camera in the hierarchy. In the Inspector panel find Camera > Clipping Planes > Far and reduce it.
Yeah, no. That will stop rendering ANYTHING. That's not what I'm asking for, did you even read my question? I want to stop rendering PARTS of a SINGLE SHADER at certain distances, essentially reducing the detail in the shader, not to remove it entirely.
Sorry for the misunderstanding. Calm down I wanted to help you out $$anonymous$$an.
Sorry, your post came across as a little condescending, and it somewhat annoyed me.
Never $$anonymous$$d, no problem. :) I hope you will have the proper answer for your question soon.
Answer by DarkPixel · Oct 03, 2013 at 12:21 AM
That's a really good question. I think the only way to do that is to render the mesh 2 times. Condition in shader should be avoided. But I'm not 100% sure it's the best way, maybe benchmark multiple solution will be a good idea.
I would make a low quality shader to render only after a certain distance from the camera with a fade value. So pixel too near the camera will be ignore with an alpha of 0. After I render the same mesh with the high quality shader only before the start fading distance of the low quality shader. It would create a nice blend with the low quality/high quality transition.
Combining this answer (or something like it) with shortened clip-planes and different cameras for the high- and low-quality passes could indeed improve performance in some cases. The camera/render setup would be a little bit of a pain--I'd only go that far if you're actually seeing a lot of time being spent rendering with this shader.
Okay. I had thought about this sort of method, but I wondered if there was another way. It seems this is the best way to do it, though. $$anonymous$$y shaders are pretty expensive (they can have up to 40 passes in them), but by missing out some of the passes, they become almost indistinguishable from the higher-quality ones at a distance. Because of this, I shouldn't actually need the fading (I may still, but there's a good chance I won't). I may be able to just alter the shader in the material, rather than drawing two at once (which could push the passes up from 40 to 60), though I'm not sure what the performance overhead of switching the shader in a material (and not shared$$anonymous$$aterial) is.