Shader question: interaction with world objects?


Simple but perhaps a silly question, but is it possible for a shader to interact directly with objects in the scene?

For example: You have a field of grass with a custom shader on it. You then roll a ball with a collider and a rigidbody through the grass, the shader should then deform the verts of the grass away from the ball. (it doesn’t need to be 100% physically correct, just a slight deformation for juice purposes)

I think what you’re asking is ‘can the shader read my scene and deform the verts away from the ball’. The simple answer to this is no! :slight_smile:

A shader is a very simple program, that runs on the GPU and operates on a few specific blocks of data that you (or unity) give it. These for a vertex shader are typically vertex buffers and constant buffers, and for a pixel shader are the vertices output by the vertex shader, along with textures and constant buffers. Unity exposes this to you in the form of textures, meshes and materials.

Modern GPUs are more flexible, though unity doesn’t provide access to much of this functionality as it is a cross platform system.

That all said, it wouldn’t be unusual to try and feed the shader some degree of information about collisions to utilize the GPU’s processing abilities. For example, if there was only 1 ball in the scene you could feed your vertex shader its position, and have the shader calculate the distance of its vertices from the ball and deform them accordingly.