- Home /
Reuse vertices modified in a previous shader pass
I've recently started to learn shaders and I'm trying to write a shader that :
- Animates a mesh to simulate water waves ;
- Reuses the standard Unity shader so that I don't have to write all the complex lighting stuff myself.
The shader modifies the vertices in the first pass and that pass works as intended. The problem is that, when the subsequent passes are performed, i.e the Unity standard shader passes, these passes do not reuse the modified vertex coordinates !
As you can see on the picture, my first pass properly updates vertices, but the following passes ignore the updated values.
My question is, how can I make the following passes reuse the updated vertexes ? Do I have to modify every single pass of the unity standard shader to also update the vertices or is there some kind of magic command that I have not learned yet ?
Thanks !
Answer by D43DB33F · Feb 11, 2018 at 12:24 AM
After a lot of pain in my ass, I finally managed to create GPU simulated water with proper normals.
The solution in a nutshell is : don't try to save the computed vertices from a pass to another in order to use it for normal computation. Instead, compute fake "neighbor" vertices (assuming that it is possible to recompute them with the available data, which in my case is possible) and use them to recompute the normals. Basically this means 5 times the work for every vertex if you take 4 neighbor vertices to compute an averaged normal based on two cross products.
There are more things to do beyond that in order to use the computed normals. In a nutshell again, it requires to write a surface shader that also has a vertex function. The vert function receives an appdata_full structure as "inout" and assigns both the vertex and the normal into it.
See the following youtube videos : - https://www.youtube.com/watch?v=1G37-Yav2ZM - https://www.youtube.com/watch?v=UfX9dzhBhg0 - https://www.youtube.com/watch?v=cz5mt_0bxqQ
The performance gain of doing this using the GPU instead of the CPU using the Unity API is ridiculous. I can animate about 1 million triangles à 65 FPS using an AMD R9 280 3 Go. I would probably get something like 0.001 FPS if I was doing this with the CPU.
Your answer
Follow this Question
Related Questions
Vertex Shader problem with _WorldSpaceCameraPos 1 Answer
Create a Wireframe Shader with Shader Forge 1 Answer
[Shader, sprite] frag shade entire sprite image? 1 Answer
Adding transparency to vertex shader? 0 Answers
Fisheye effect vertex shader fail - asking for help - video and code included 1 Answer