Search Unity

Passing arbitrary float values to vertex shader in texcoord - should they lie in 0-1 range?

Discussion in 'Shaders' started by tanoshimi, Nov 23, 2014.

  1. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
    Hi,

    I need to pass two floating point values for use in calculations in a vertex shader and, since it doesn't require texture coordinates for anything else, I'm currently encoding these values in texcoord.u and texccord.v.

    This works great.

    However, my only concern is that these values are somewhat arbitrary and may lie anywhere from -2,500 to +2,500, say. A shader presumeably expects texture coordinates only to take values in the range 0 - 1, so am I running risk of causing any "issues" by doing this? (I haven't noticed any precision or performance issues so far on any devices I have to hand). Is there a better structure I could use? (I figured using normal would have exactly the same issue...)

    I could always apply a scale factor to reduce the values down to lie within the 0-1 range before sending them to the shader, and then rescaling them up again to use in calculations in the vertex program, but wasn't sure if that was an unnecessary step seeing everything seems to be working? (Am I just lucky - perhaps there are GPUs that will have a hissy fit when sent a texture coord of (-415.234, 738.23849)?!)

    Many thanks in advance for any insight!
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Texture coordinates should go as high/low as float values can go (with the given precision).

    I think the only thing that's clamped are vertex colours (in Unity, vertex colours are clamped to 0-1 and stored with 8bit precision, rather than full float precision).
     
    Last edited: Nov 24, 2014
  3. tanoshimi

    tanoshimi

    Joined:
    May 21, 2013
    Posts:
    297
    Thanks for the reply - much appreciated! I'll carry on using my current method then (at least, until I find some weird Android hardware somewhere that just doesn't like negative texture coordinates :)