Here's a very simple shader,
float4 vert (float4 vertex :POSITION, out PositionHolder o) :SV_POSITION
{
UNITY_INITIALIZE_OUTPUT(PositionHolder,o);
o.localPos = vertex;
return UnityObjectToClipPos(vertex);
}
fixed4 frag (PositionHolder IN) :SV_Target
{
if (IN.localPos.y > -.2)
return _UpperColor;
else
return _LowerColor;
}
(So, if on a quad it just paints the top 70% in one color and the bottom stripe in another color.)
Notice that the only thing vert
does is pass along the local on the mesh vertex position.
Here's the struct to do so
struct PositionHolder
{
float3 localPos;
};
However, (in Unity, anyway) it will want a semantic for that float3.
Reviewing Unity's doco, they don't have any of course, but there's a link to
https://learn.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-semantics
(Aside - which, as I understand it, is specifically for D3D, but, I guess that's the syntax/semantic to go with anyway?)
My reading of the situation,
suggests you should use say,
struct PositionHolder
{
float3 localPos :POSITION1;
};
Or really, position-any-number you're not already using as a resource.
What about,
struct PositionHolder
{
float3 localPos :POSITION8;
};
for luck.
(Confusingly, they seem to run only from blank to 9, I don't know if that reflects a hardware reality, or is just something shoddy somewhere - or what. But anyway, basically "any number 1 to 9 you're not already using elsewhere.)
Note that anyway, POSITION
is a float4, really is that ok for my float3?? Is there something better than POSITION for a general-purpose float3? (I've noticed if you just use, say, a COLOR, it also works fine. Does it matter?)
Is the "1 on the end" system actually correct? So, POSITION1 is good to go? I'm not melting the gpu or some such?
Hmm, I've noticed you can just type ANYTHING in there (say, "abcd1"). But surely then it's not actually a hardware register thingy???
Let say your development platform is iOS.
iOS has its own shader language - metal shading language.
In order for your shaders to work on iOS, unity compiles them from HLSL to Metal. So if we were to compile the following shader part:
HLSL:
float4 localPos: POSITION1;
this is what the compilation result be:
Metal:
float4 POSITION1 [[ user(POSITION1) ]];
According to Metal Shading Language Specification
The attribute [[user(name)]] syntax can also be used to specify an attribute name for any user-defined variables.
Unity3d translates your HLSL semantic field "name" to Metal Language attribute [user(name)]. So, it doesn't matter if you write: [POSITION1 or HelloWorld or Fattie], as long as it's unique and doesn't use reserved keywords. Hence, after compiling the following shader
HLSL:
float4 localPos: Fattie;
this would be the result:
Metal:
float4 Fattie0 [[ user(Fattie0) ]];
As for Vector Data Types, vector types [float, float2, float3, float4, etc] are not specific to your custom semantics name. Compiling this shader with float3
HLSL:
float3 localPos: Fattie;
gives the following result:
Metal:
float3 Fattie0 [[ user(Fattie0) ]];