- Home /
The question is answered, right answer was accepted
Shader preprocessor macros - define custom vector?
Hey so I've seen lines like this in shaders, where you can define basically compilation constants:
#define MY_CUSTOM_CONSTANT 2.940386
I was wondering if I could do the same, but with a vector? Maybe something like...
#define MY_CUSTOM_FLOAT4 float4(0.2, 0.4, 0.6, 1)
Answer by ModLunar · Feb 16, 2018 at 11:14 PM
Ah silly me! I was right -- I tested it and it actually worked! There's just not a lot of documentation on some shader topics, so hopefully this helps someone else too. My example from above works:
#define MY_CUSTOM_FLOAT4 float4(0.2, 0.4, 0.6, 1)
And also here are some other examples that also worked -- it's good with basically any simple Cg/HLSL data type like booleans, integers, and matrices! (I didn't need to try defining any samplers/textures cause those would usually not be constant, or I would want to be able to set from a material via C#/inspector anyway)
#define DEFAULT_NORMAL float3(0, 0, 1)
#define DEFAULT_NORMAL_COLOR float4(0.5, 0.5, 1, 1)
#define TEST_BOOLEANS bool2(true, true)
#define SOME_UNSIGNED_INTEGER uint(49)
#define JUST_A_SYMBOL
Cg / HLSL uses pretty much the standard C perprocessor directives. The documentation even mentions slight differences in the HLSL compiler because HLSL has of course different language tokens. Just look at the examples.
Also keep in $$anonymous$$d that Unity provides many include files for their own shaders and your shader code. Just look at your install folder of Unity. On windows you'll find the cg include folder at
C:\Program Files\Unity\Editor\Data\CGIncludes\
unless you have installed Unity elsewhere. Unity uses a lot of built-in macros which makes it actually quite difficult to follow what a line of code actually means / does ^^.
Ah hey Bunny83, great thanks for the help :) I'll take a look at that.
Haha yeah right it's like a wild goose chase sometimes! I wonder if Visual Studio will be able to support intellisense/better syntax highlighting for Unity shaders and Cg/HLSL to make it easier for us to interpret when we're writing shaders :o