- Home /
Shader question: interaction with world objects?
Hi,
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)
Answer by wibble82 · Dec 21, 2015 at 03:58 PM
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! :)
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.
-Chris
Hi Chris,
A related question..
Are demos like this created just for fun ? This scenery would mean nothing when loaded in an unity / any other game engine ?
Yes - that scene only exists on the graphics card - created just before it is rendered. It cannot interact with any objects in your scene, user input, physics etc.
While the shader can't read the scene it certainly can be fed information, like the position of the balls, and the vertices of the geometry adjusted in the vertex shader accordingly. Shaders are not simple programs at all, in fact they are in many ways more powerful than programs run on CPUs, especially when computing and calculating tasks in parallel. The task in question is definitely feasible and could probably be done with $$anonymous$$imal branching to boot.
Yes - it's relatively easy to make a shader react. $$anonymous$$uch harder to make it interact.
Your answer
Follow this Question
Related Questions
Mobile performance of splat map shader with distance blending 0 Answers
How to extend a Material Shader so that it also maps the Normal Maps in WorldSpace? 1 Answer
vertex and geometry shader change with distance 0 Answers
Shader to turn materials transparent based on y axis 0 Answers
unity shader custom vertex stream - What does TEXCOORD0.w|x mean? 1 Answer