correct glsl affine texture mapping

后端 未结 6 1337
一整个雨季
一整个雨季 2020-12-19 10:43

i\'m trying to code correct 2D affine texture mapping in GLSL.

Explanation:

\"\"

...NONE of thi

6条回答
  •  天涯浪人
    2020-12-19 11:24

    This works well as long as you have a trapezoid, and its parallel edges are aligned with one of the local axes. I recommend playing around with my Unity package.

    GLSL:

    varying vec2 shiftedPosition, width_height;
    
    #ifdef VERTEX
    void main() {
        gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        shiftedPosition = gl_MultiTexCoord0.xy; // left and bottom edges zeroed.
        width_height = gl_MultiTexCoord1.xy;
    }
    #endif
    
    #ifdef FRAGMENT
    uniform sampler2D _MainTex;
    void main() {
        gl_FragColor = texture2D(_MainTex, shiftedPosition / width_height);
    }
    #endif
    

    C#:

    // Zero out the left and bottom edges, 
    // leaving a right trapezoid with two sides on the axes and a vertex at the origin.
    var shiftedPositions = new Vector2[] {
        Vector2.zero,
        new Vector2(0, vertices[1].y - vertices[0].y),
        new Vector2(vertices[2].x - vertices[1].x, vertices[2].y - vertices[3].y),
        new Vector2(vertices[3].x - vertices[0].x, 0)
    };
    mesh.uv = shiftedPositions;
    
    var widths_heights = new Vector2[4];
    widths_heights[0].x = widths_heights[3].x = shiftedPositions[3].x;
    widths_heights[1].x = widths_heights[2].x = shiftedPositions[2].x;
    widths_heights[0].y = widths_heights[1].y = shiftedPositions[1].y;
    widths_heights[2].y = widths_heights[3].y = shiftedPositions[2].y;
    mesh.uv2 = widths_heights;
    

提交回复
热议问题