Endless non-repeating textures with mosaic Van



    There are always some problems with textures! It turns out that you can not take any picture and stick on the model. Then at the junction of the texture there are seams that you are tormented by smoothing. It seems that everything has already been blotted out, but the eye, a sort of prankster, still notices the repeating patterns and destroys the illusion.

    You can make the texture larger so that the repeating pieces are farther apart and not so noticeable. You can even make it very huge, a couple of hundred thousand pixels, so that it covers the entire scene without seams and repetitions. A similar technique is called megatexture. But megatextures and virtual textures close to them complicate the work with memory, special tools are required to work with them, and in general this is still a young technology.

    How to be? There is one trick - non-periodic mosaics. They lack the problem of repeatability and are quite simple to implement. One of these mosaics was invented by the Chinese mathematician Wang Hao in 1961. Elements of this mosaic can be represented as rectangles with colorful faces. But in order to understand the principle of its operation, one must first understand the classical method of filling squares with textures.


    And the classic method is this: we take the picture we like, we trace a small area for the test with it and immediately see all the bad border places. After that, we redo the picture so that the right side coincides with the left, and the top with the bottom. Done, the texture can be used.



    In other words, it is necessary to ensure that the horizontal and vertical edges of adjacent textures smoothly flow into each other. It turns out a kind of container for the picture, which fits well next to other containers.



    We develop the thought further. Container filling can be anything. You can make many different options for the same texture and bridge the area with random tiles from the set. But whatever one may say, even with this approach, a repeating grid of tile faces remains. Therefore, you need to come up with a way to mix faces.



    In fact, the right and left sides of the same tiles do not have to be combined with each other, the main thing is that they are combined with adjacent tiles. You can make several texture options with different edges and pack them in an atlas. Then, while laying down the textures, just grab a random tile with an edge that matches the adjacent laid tiles.

    Unfortunately, the size of the atlas for a complete set of tiles grows very quickly with the number of facet variations. The calculation is carried out according to the following formula: N 2 * M 2 , where N is the number of horizontal faces, M- the number of vertical faces. For example, for two options, sixteen different tiles go to the horizontal and vertical. If you just need a guaranteed opportunity to continue the mosaic, the formula is simpler: 2 of N * M * .



    Atlas tiles can bridge an arbitrarily large space. One random tile is taken, on the side a second tile with a suitable face is applied to it, a third tile to the second tile, and so on until the end of the row. When choosing tiles in the second row, neighbors from the first row are additionally taken into account. The image above shows the atlas on the left and the mosaic on the right.



    How to create an atlas of tiles? You can take one large picture and lay a grid of colored faces on top of it. Then choose a suitable seam for each color and propagate it along the grid. Or go from the opposite: first make a grid, then fill in the empty spaces. Below are links to more detailed instructions.



    In the illustration above, on the left is a simple repetition of the created atlas, on the right is Van's mosaic, assembled from it. The resulting mosaic has no noticeable repetitions, it looks random and fluid.

    Of course, Van’s mosaic is not perfect, it has a problem with the corners of the tiles. Mosaic solves the problem of horizontals and verticals, but there are still diagonals, due to which artifacts may appear at the junction of four tiles. But this mosaic is simple and can be used not only for texturing, but also for the procedural generation of floor plans or dungeons.



    I sketched a simple C # code for Unity3d to work with Wang's mosaic, you can look at it using the links below.

    Unity Web Player | Windows | Linux | Mac | Sources on GitHub

    Space - a new random texture, 1, 2, 3 - atlas selection, Esc - exit.
    For Linux users: Make the WangTiles file executable with “chmod + x WangTiles” and run it.

    Useful links on the topic:
    Instructions for creating atlases
    Procedural generation of dungeons
    More about creating atlases of different sizes
    Semi-automatic atlas generator

    At the request of workers a small addition about creating atlases:

    Take the large picture you like, put a grid on top for convenience in Photoshop or where else.



    For each grid color, you need to select a segment of the picture to create a new grid. Just multiply each segment and put the copies in columns and columns.



    The grid is initially very curved and with a bunch of ugly seams. We apply all the available magic of Photoshop and make the seams invisible. In the example below, I just anointed the grid with a little eraser. It turns out atlas - a new picture that can be cut into pieces that will be combined with each other.



    You can’t do without basic programming, you can assemble everything by hand, but it will take a lot of time. Use the created atlas in the code, select pieces from it and make a mosaic. Elementary. The hardest thing to make is a beautiful satin, but with textures it is always like that.

    In fact, I just retold the instructions from here . Read, everything is well-painted there.


    Also popular now: