Search Unity

A vertex position is a float4. What does the 4th component represent?

Discussion in 'Shaders' started by lifeformed, Sep 28, 2012.

  1. lifeformed

    lifeformed

    Joined:
    Aug 1, 2012
    Posts:
    51
    In the vertex input data structure to vertex shaders, you specify a position as float4:

    Code (csharp):
    1.          struct vertexInput {
    2.             float4 vertex : POSITION;
    3.          };
    I understand x, y, and z correspond to the vertex's xyz coordinates. What about w?
     
  2. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    Here is an explanation why the fourth coordinate should always be 1:
    http://en.wikibooks.org/wiki/Cg_Programming/Vertex_Transformations#Structure_of_the_Model_Matrix

    (For pedantic geeks: If you know about the homogeneous division you might think that you can use any other nonzero value if you scale the other coordinates with it. This is only true if you do it with all(!) vertices of your mesh, otherwise the perspective interpolation is messed up because the specification of perspective interpolation in OpenGL assumes that all fourth coordinates of the vertices are the same.)

    EDIT: Oops, I forgot: yes, the fourth component should be 1 only for points and it needs to be 0 for directions as mentioned by Farfarer.
     
    Last edited: Sep 28, 2012
  3. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    If I'm right, matrix transforming a 4 component value with the fourth component being 0, it'll rotate but not move the value. If it's filled with 1, it'll rotate and move the value.

    So the first one would be for vectors (only rotation is needed) the second would be for positions (where you need both rotation and translation).

    Which is why the vector transforms appear as mul((float3x3)matrix, float3) as there's no need to calculate then ignore the fourth component.
     
    sorialexandre likes this.