Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question What is "v.vertex.yyyy" mean

Discussion in 'Shaders' started by WayneJP, Dec 1, 2022.

  1. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    I am new to shader, I found following code when I learn a blur shader:
    Code (CSharp):
    1.  
    2. v2f vert(appdata_full v)
    3. {
    4.     v2f o;
    5.     float4 tmp0;
    6.     tmp0 = v.vertex.yyyy * unity_ObjectToWorld._m01_m11_m21_m31;
    7.     tmp0 = unity_ObjectToWorld._m02_m12_m22_m32 * v.vertex.zzzz + tmp0;
    8.     tmp0 = tmp0 + unity_ObjectToWorld._m03_m13_m23_m33;
    9.    
    10.     ...
    11.  
    12.     o.position = unity_MatrixVP._m03_m13_m23_m33 * tmp0.wwww;
    13.     o.texcoord.xy = v.texcoord.xy;
    14.     return o;
    15. }
    16.  
    There is little information about it on google, I only found something related on https://developer.download.nvidia.com/cg/Cg_language.html :

    Vector swizzle operator: (.)
    a = b.xxyz; // A swizzle operator example
    • At least one swizzle character must follow the operator.
    • There are three sets of swizzle characters and they may not be mixed: Set one is xyzw = 0123, set two is rgba = 0123, and set three is stpq = 0123.
    • The vector swizzle operator may only be applied to vectors or to scalars.
    • Applying the vector swizzle operator to a scalar gives the same result as applying the operator to a vector of length one. Thus, myscalar.xxx and float3(myscalar, myscalar, myscalar) yield the same value.
    • If only one swizzle character is specified, the result is a scalar not a vector of length one. Therefore, the expression b.y returns a scalar.
    • Care is required when swizzling a constant scalar because of ambiguity in the use of the decimal point character. For example, to create a three-vector from a scalar, use one of the following: (1).xxx or 1..xxx or 1.0.xxx or 1.0f.xxx
    • The size of the returned vector is determined by the number of swizzle characters. Therefore, the size of the result may be larger or smaller than the size of the original vector. For example, float2(0,1).xxyy and float4(0,0,1,1) yields the same result.
    Question:
    I am not sure what v.vertex.yyyy or tmp0.wwww is.
    If v.vertex equals (1, 2 ,3 ,4), does v.vertex.yyyy equals (2, 2, 2, 2) ?
    And is
    v.vertex.yyyy * unity_ObjectToWorld._m01_m11_m21_m31;

    equivalent to
    mul(unity_ObjectToWorld, float4(y, y, y, y);
    which means convert this float4 to world space?
     
    Last edited: Dec 3, 2022
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Yes.


    No. It’s equivalent to:
    mul(unity_ObjectToWorld, float4(0,v.vertex.y,0,0));


    Also, a minor word of warning. Regardless of what you may have seen said elsewhere, Unity does not use Cg. Hasn’t for many, many years. It’s purely HLSL now. That said Cg and DirectX 9 HLSL are very similar, so most of what’s on that site can still be helpful. Just know sometimes it won’t be correct because it’s not documentation for HLSL. Also there’s a lot of stuff in DirectX 11 HLSL that changed significantly and won’t match Cg at all.
     
    CiroContns likes this.
  3. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Ok, thanks !
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    Minor correction, but .y would be the second component, so it would be (2,2,2,2) in that case.
     
    bgolus likes this.
  5. WayneJP

    WayneJP

    Joined:
    Jun 28, 2019
    Posts:
    44
    Oh , yes yes, I will correct that. Thanks for reminding !
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    whoops, I derped that one