How do individual Fog for instances in shader?

I have a shader that handles instances of bushes waving in the wind very nicely, but the fog applied to the bushes is the same for every instance, no matter how far away they are.

I have tried setting fogDepth in the vertex shader to the z of the individual position, but still all instances are ‘fogged’ the same.

Here Palms (on a Retrowave road) I use instances of palms with fog. Though I just modified a default material THREE.MeshBasicMaterial().

Nice demo.

I think modifying an existent material using onBeforeVompile works out different than taking the shaders from THREE.ShaderLib and modifying them and send them to ShaderMaterial… but how?

You could provide a working example with your code, so people on the forum would provide better help :slightly_smiling_face:

Sure, here is an example:

use the mouse wheel to scroll in and out and notice that all the boxes are ‘grayed’ by the fog in exactly the same way, no matter their distance to the viewer.

fog_vertex chunk is implemented utilizing mvPosition.z to calculate the fogDepth.

Since you are only applying the vertex offset to calculate gl_Position and not mvPosition itself, all vertices get the same fog falloff.

Adjusting your code to modify mvPosition accordingly fixes this problem CodePen


Thanks, that does the trick…