Changing the color of a texture fragment using shaders

The declarative programming language QML allows you to associate elements, their properties with universal variables in the description of shader programs. For these purposes, for example, there are elements such as ShaderEffectItem . Here, for example, we define a specific image image.jpg . And in the source property, we establish that this image will change in the image, and this property is already set in the description of the fragment shader program fragment of the Shader as a single source sampler2D. qt_TexCoord0 defines the initial coordinate of the texture. We get there a gray color for all the textures of the scalar product lowp float gray = dot (textureColor, vec4 (0.299, 0.587, 0.114, 0.0)); we also set the color of the fragment in the output variable gl_FragColor.

 .... Item { id: main Image { id: img source: "images/image.jpg" } ShaderEffectItem { id: effect property real ratio: 1.0 property variant source: ShaderEffectSource { sourceItem: img; hideSource: true } fragmentShader: " varying highp vec2 qt_TexCoord0; uniform sampler2D source; uniform highp float ratio; void main(void) { lowp vec4 textureColor = texture2D(source, qt_TexCoord0.st); lowp float gray = dot(textureColor, vec4(0.299, 0.587, 0.114, 0.0)); gl_FragColor = vec4(gray * ratio + textureColor.r * (1.0 - ratio), gray * ratio + textureColor.g * (1.0 - ratio), gray * ratio + textureColor.b * (1.0 - ratio), textureColor.a); } " } SequentialAnimation on ratio { id: ratioAnimation running: true loops: Animation.Infinite NumberAnimation { easing.type: Easing.Linear to: 0.0 duration: 500 } PauseAnimation { duration: 500 } NumberAnimation { easing.type: Easing.Linear to: 1.0 duration: 500 } PauseAnimation { duration: 500 } } } 

After the color of the texture (image) gradually gradually turns gray, and then vice versa, it runs in an endless loop. Now:

enter image description here

The question is actually what it is: and I can somehow change the color of some certainty of a part of the texture (my images are images / image.jpg), that is, any specific sections. For example, I define two variables xColor, yColor in QML, we pass them in the same way as the shader description, and then, for example, the texture changes only in the square with the coordinates of the upper left corner [xColor - 10, yColor - 10] and [xColor + 10 , yColor + 10] is the lower right corner. I want to:

enter image description here

I know that it can be implemented. How to do this optimally? In which direction should I think? Are there any similar examples? It would be nice to add a mixed color (from red to gray in this section).

+4
source share
1 answer

For a rectangle, you need to check the texture coordinates:

  • Convert coordinates of input textures to pixels:

    highp vec2 pix_coords = qt_TexCoord0.st * image_size; // you need to pass the image_size variable to the shader

  • Check the borders of your rectangle:

    if ((pix_coords.x> rect_bottom_left.x) && (pix_coords.y> rect_bottom_left.y) && & && & & &&&& && & && & && & && & & && & & && & && & && & &; & rect_top_right.y))

    {// grayscale}

    yet

    {// standard color search}

As for optimization, it would be better to convert the rectangle into a texture space and check the unconverted coordinates of the texture.

+3
source

Source: https://habr.com/ru/post/1384646/


All Articles