Search Unity

Passing parameters to Cg shader through appdata_full

Discussion in 'Shaders' started by Heisenbug, Feb 5, 2013.

  1. Heisenbug

    Heisenbug

    Joined:
    Aug 31, 2012
    Posts:
    40
    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:

    Code (csharp):
    1. struct appdata_full {
    2.     float4 vertex : POSITION;
    3.     float4 tangent : TANGENT;
    4.     float3 normal : NORMAL;
    5.     float4 texcoord : TEXCOORD0;
    6.     float4 texcoord1 : TEXCOORD1;
    7.     fixed4 color : COLOR;
    8. #if defined(SHADER_API_XBOX360)
    9.     half4 texcoord2 : TEXCOORD2;
    10.     half4 texcoord3 : TEXCOORD3;
    11.     half4 texcoord4 : TEXCOORD4;
    12.     half4 texcoord5 : TEXCOORD5;
    13. #endif
    14. };
    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:

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

    thanks in advance
     
  2. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    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.
     
  3. Heisenbug

    Heisenbug

    Joined:
    Aug 31, 2012
    Posts:
    40
    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.
     
  4. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    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.
     
  5. Heisenbug

    Heisenbug

    Joined:
    Aug 31, 2012
    Posts:
    40
    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.
     
  6. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    Perhaps you can use the vertex colors instead?
     
  7. Heisenbug

    Heisenbug

    Joined:
    Aug 31, 2012
    Posts:
    40
    Yes I do. But I still need to send some more infos to the shader. I neet at least 12 floats.
     
  8. runonthespot

    runonthespot

    Joined:
    Sep 29, 2010
    Posts:
    305
    There are also normals and tangents (depending on what your shader is using).
     
  9. andSol

    andSol

    Joined:
    May 8, 2016
    Posts:
    22
    @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.
     
  10. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    109
    You can use
    Code (CSharp):
    1. mesh.SetUVs(channel , vector4List);
    if I understand the question correctly.