- Home /
Wavey Shaders, Where to start?
Hi,
One part of a game I'm working on requires a high vertex plane to animate in waves; sort of like an ocean. Originally, I achieved this effect by randomly moving each vertex in the plane and using smooth shading. This created the desired effect, but was computationally heavy.
Here's what it looks like from the side..
I want to turn this into a surface shader, but I'm having a bit of trouble and I don't have access to Unity's ShaderGraph.
I also tried just moving the verts along their normals randomly in the surface shader, but that doesn't create the soft shadow effect shown in the first link (I think this is the limitation of ShaderForge too).
I'm only familiar with Vert and Frag shaders and not familiar enough with surface Shaders to know how to achieve this affect. What's the best direction to go in at the moment? I feel like learning surface shaders is unecessary. Is this just not possible to do for me right now and is it better to just buy a pre-made shader on the asset store?
Thanks for any feedback in advance.
Answer by Pangamini · Oct 21, 2019 at 06:31 PM
Surface shader is just a shader.... well, technically it generates a lot of fragment shaders for different parts of the pipeline, and unity shader framework calculates a lot for you. But you can move vertices in a vertex shader just the same, then use the result in the surface shader. The only tricky part is to calculate the normals, but if you are generating the waves (eg. from a sin or a texture) you could just generate the normals in the vertex shader
That's the bit I need. I need to understand how to regenerate the normals after I've moved the vertices. Do you know where I can find information about how to do this? Or a practical example?
That's closely bound to how you generate the wave displacement. If you generate the displacement by sampling a heightmap of the wave, then the easiest solution would be to generate the normals the same way - by sampling a normalmap of the wave.
If you are generating some arbitrary displacement, you might need to get into geometry shaders that can see the whole triangle and more... and things get complicated pretty quickly. So unless you want to do a bit of research here, stick to the first option
Or, you can just ignore vertex normals (set them to all point upwards) and sample the animated normal map in the pixel shader. Same as above, you should have a matching normal function to your height function (whether it's a texture or some sine wave composite)
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Shadergraph Custom Function undeclared identifier 4 Answers
Do TEXCOORDS need to be in sequence? 1 Answer
Is it possible to read light value per vertex with a shader? 0 Answers
doubles and compute shaders 1 Answer