Search code examples
opengl3dtexturestexture-mappinguv-mapping

How to transfer colors from one UV unfolding to another UV unfolding programmatically?


As seen from the figure, assuming a model has two UV unfolding ways, i.e., UV-1 and UV-1. Then I ask an artist to paint the model based on UV-1 and get the texture map 1. How can I transfer colors from UV-1 to UV-2 programmatically (e.g., python)? One method I know is mapping the texture map 1 into vertex colors and then rendering the vertex colors to UV-2. But this method would lose some color details. So how can I do it?

enter image description here


Solution

  • Render your model on Texture Map 2 using UV-2 coordinates for vertex positions and UV-1 coordinates interpolated across the triangles. In the fragment shader use the interpolated UV-1 coordinates to sample Texture Map 1. This way you're limited only by the resolution of the texture maps, not by the resolution of the model.

    EDIT: Vertex shader:

    in vec2 UV1;
    in vec2 UV2;
    out vec2 fUV1;
    void main() {
        gl_Position = vec4(UV2, 0, 1);
        fUV1 = UV1;
    }
    

    Fragment shader:

    in vec2 fUV1;
    uniform sampler2D TEX1;
    out vec4 OUT;
    void main() {
        OUT = texture(TEX1, fUV1);
    }