How to correctly draw other texture in fragment shader?

benifest

How to correctly calculate uv coordinates for my sampler2D uniform?

E.g. i have main texture which has 1920x1080 resolution, i attached a shader to this texture, then passed other texture as sampler2D uniform which has 200x200 resolution.

Also, as i understand, i have to pass to a shader texture resolution and its normalized position. But i don't understand how to calculate uv coordinates for second texture.

uniform sampler2D u_render_texture;
uniform vec2 u_render_texture_pos;
uniform vec2 u_render_texture_size;

In main:

void main() {
   vec4 mainTextureCol = texture2D(u_texture, v_texCoord);
   vec4 renderTextureCol = texture2D(u_render_texture, what is here?);
   gl_FragColor = mainTextureCol + renderTextureCol;
}
Rabbid76

You need to scale the texture coordinates according to the ratio of the texture sizes. If the unit of u_render_texture_pos is pixels and since the texture coordinates are in the range [0.0, 1.0], the offset must also be scaled.
Skip the color you got from u_render_texture if one of the coordinates is not in the range [0.0, 1.0]:

uniform vec2 u_render_texture_pos;
uniform vec2 u_render_texture_size;
uniform vec2 u_texture_size;

void main()
{
    vec2 uv = v_texCoord * u_render_texture_size/u_texture_size 
              + u_render_texture_pos/u_texture_size; 

    vec4 mainTextureCol = texture2D(u_texture, v_texCoord);
    vec4 renderTextureCol = texture2D(u_render_texture, uv);

    vec4 finalColor = mainTextureCol;
    if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0)
        finalColor += renderTextureCol;

    gl_FragColor = finalColor;
}

If you are using GLSL 1.30 or higher, you can use textureSize to determine the size of a texture:

vec2 texture_size = textureSize(u_texture, 0); 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Fragment shader and coloring a texture

Texture Reading in Fragment Shader

How to store an array into a texture and sampler the texture in vertex shader correctly?

How I can draw a tile from a texture tilemap in an OpenGL shader?

Fragment shader ignores texture coordinates

OpenGL texture black in fragment shader

PyOpenGL fragment shader texture sampling

in RealityKit Custom Shader API, how do I create a texture sampler object in fragment shader?

How many times does the glsl fragment shader execute for one draw?

Draw a line segment in a fragment shader

Edge/outline detection from texture in fragment shader

Slow texture fetch in fragment shader using Vulkan

glsl fragment shader calculate texture position

Reading custom texture format in a fragment shader

fragment shader: texture2D() and texelFetch()

OpenGL Fragment Shader Return Fixed Color for Texture

Multiple texture output data in fragment shader

Texture function in fragment shader giving error

glsl fragment shader - how to sample a GL_NEAREST texture as GL_LINEAR

How do I properly pass a texture2d_array<float> to the fragment shader?

Is there a way to draw a circle with the fragment shader at the position of a point from the vertex shader?

How the vertex shader is passing to the fragment shader

Creating texture in Vertex shader and passing to Fragment to achieve smudging brush with Metal?

passing a float array as a 3D Texture to GLSL fragment shader

Why some pixels sample wrong texture in fragment shader?

Use 3D Texture for Fragment shader on Android

OpenGL ES - Rotating texture in fragment shader without distortion

Changing the color of a texture in OpenGL in the fragment shader isn't quite working

Proplem reading generated texture from fragment shader using ThreeJS