I needed a set of shaders that curve based on the distance from camera. Basically, I took some of the default HLSL shaders Unity provides and changed the code in the vertex shader to adjust the position of the vertices.
It works great, but I would like to know if there is a way to centralize the code in the vertex shader, as it is the same in all cases, and just pass that code throughout all shaders I need, because only the fragment shader is different.
This is because if I need to change anything in the vertex shader function, I need to change it in 6 different places.
They’re called include files. In fact, by default all surface shaders in Unity use include files and you’ll find most vert/frag shaders do too. Basically, create an empty text file and call it whatever you want. Then, change the file suffix/type to ‘.cginc’. Then, inside this file, you don’t have to add any special information, you can literally just type the lines you want to be included. For example, if you wanted to reuse just a vertex function, you could simply have the following by itself in an include file (let’s call it ‘Resources.cginc’);
Then, in each of your shaders, at the beginning you just need to have the following;
...
#pragma vertex vert
#include "Resources.cginc"
...
And by default, the shader will use the vertex program from the include file we created, and therefore you don’t even need to type it in this shader (so long as you initialise the vertex function pragma with the same name).