Good day! After publishing an article about rendering a quadratic tree (Quad-tree) , I was asked to write an article showing the work of the shader, which translates the image into a “sweater”.
So, let's consider this technique.
Implementation
To implement the algorithm, you will need a texture with sweater stitches. It looks like this. ')
The texture consists of 30 stitches of the same width of 30 pixels. As a result, we must display this texture on the screen, make me stitch color under the desired color on the screen, and shuffle the stitches themselves on the screen so that the image does not seem artificial.
Initially, you need to prepare the parameters for the shader, the language described in the article will be C # and GLSL, respectively.
The function is called when the scaling factor is updated, it calculates the number of textures with stitches.
Amount is the number of textures that will fit on the screen;
itemWidth - the width of a single tie;
The variable amountH is divided by 2, this is done in order to divide one stitch by sex and change the color of the half stitch.
Let us turn to the implementation of the shader. A shorter image from the camera will be transferred to the shader in order to split the image into blocks, which will be converted into stitches next.
Initially, we need to determine in which block we are now. The blocks are formed by the fact that our thumbnail image is then stretched to full size, and each pixel forms a block of pixels of the same color.
Next, take the color of the block, the color of the stitch and the color of the previous block. The color of the previous block must be taken, as the stitches go beyond the limits of their blocks.
In the image, the current block is shown in red, and the next one is green, but a part of the stitch painted in green should have a red color, for this the color search of the previous block is used.
Initially, we mix the color of the stitch with the color of the block, after which we find the stitch, which should be drawn over the red part and then we mix the two parts of the stitch.
As a result, we get a rather smart shader, which, if desired, can be made to work well on old Android (on iOS it will already work fine). You can use different textures to change the effect, say cross-stitch.