Passing parameters to Cg shader through appdata_full

Hi all,
I’m using some mesh parameters to forward some values to a CG shader. I need to use as much parameters as possible since this way I can avoid the use of an indirection texture.

Now, I’m looking at appdata_full definition in the file: UnityCG.cginc:

struct appdata_full {
    float4 vertex : POSITION;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    float4 texcoord1 : TEXCOORD1;
    fixed4 color : COLOR;
#if defined(SHADER_API_XBOX360)
	half4 texcoord2 : TEXCOORD2;
	half4 texcoord3 : TEXCOORD3;
	half4 texcoord4 : TEXCOORD4;
	half4 texcoord5 : TEXCOORD5;
#endif
};

texcoord is defined as float4, so I should be able to pass 4 32-bit float to the shader through this parameter, am I right?

The question is: is possible to do that?AFAIK I can set this parameter using Mesh.uv propertyhttp://docs.unity3d.com/Documentation/ScriptReference/Mesh-uv.html. But mesh uv is just a Vector2.

So here’s 2 questions:

  • If mesh.uv is a Vector2, why appdata_full defines texcoord as a float4?
  • Since appdata_full is actually a float4, is there anyway I can set all the 4 floats from script?

thanks in advance

Because the struct appdata_full has more to do with the graphics libraries that are being used than with Unity itself. It is mainly used to guide data between the various shader stages (for example: between vertex and fragment shader).

(I think it’s a float4 in appdata_full because that maps directly to the semantics defined by DirectX, and I’m guessing it has to do with what the rasterization hardware supports.)

Unity probably only lets you set Vector2 texture coordinates, because you don’t need to set more, to be able to use 2D Textures.

Thanks for the answer. I’d search another way to go. If it would be possible to fill all the float4 texcoord vector I’d be really happy.

You might be able to use setVector. I don’t know if you can directly set the input texture coordinates, but you can at least set a custom float4.

Unfortunately I’m using static batching over my meshes. All of them needs to share a unique material. If I use setVector I’ll force Unity to instantiate a new material. I need to encode the parameters as vertices’s parameters in order to use static batching and support different parameters values for each GameObject.

Perhaps you can use the vertex colors instead?

Yes I do. But I still need to send some more infos to the shader. I neet at least 12 floats.

There are also normals and tangents (depending on what your shader is using).

@Heisenbug I would love to know if the OP eventually found out a way of passing the 4 floats to a float4 texture, or if someone else has any ideas - so I avoid asking a duplicate of the exact same question.

You can use mesh.SetUVs(channel , vector4List); if I understand the question correctly.