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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…