Random tile generation




Do you want a ultra detailed skybox full of stars but you cant afford 99K huge textures? Do you want a huge area covered with very small detailed textures but you cant afford the memory budget? Come take a look ;^)

Random tile distribution


Lets start by creating a grid with white dots, each dot center will be at (0.5,0.5) and will have a radius of 0.5 so we can see clearly how the 0-1 UV space is distributed.


Lets add some position variation, to do this lets add a random -0.5 to 0.5 value to each cell. I will use the vector noise node, but keep in mind that you can save some instructions by just sampling a small noise texture ( this is a very cheap and usefull way of getting random values, as you can use any radom distribution you like just by changing the texture). I also added the offset color to the background to see the random value in each cell.
By the way, you can see how many instructions each random function need and the output range in the tooltip of the noise function selector.

*You will need to use a ceil or a floor before using other than cell noise in the noise node (and remember to be sure the output still in -0.5 to 0.5 range)

Now that we have a random position on each cell lets change the size so we allways see the 0-1 UV space. To do this just apply a scale relative to the distance to the cell border, wich is gonna be the one minus the offset vector length. We need to apply the scale as if the center was the (0.5,0.5).

Why not adding a rotation variation and some controls to do the distribution at will?

Now you can control global size, random size, rotation and offset!



What if you plug time to the rotation input?^^




Texture sampling


Now its time to sample a texture, but before plug the coords lets make sure of some things. First the texture tiling methiod need to be clamped to ensure "one texture" per cell.



Lets now divide the area we want to splatter into a number of 0-1 spaces using a frac and add a ceil so we work with a integer number of instances.

Oh shit! What are those weird ass lines?

Right now you may notice some weird artifacts. This happen because of how the graphics card renders the texture. This is the second thing we have to take care when using modified UVs.

The engine creates by default what is called MIP maps and use one or another depending on how big the texture is seen on screen. As we are modifying the UVs, we got 0-1 value cicles on each axis. A pixel UV can have a value of 1 and the adjacent one a value of 0.
Without getting too technical, the calculation to determine wich mip to use is done in a 2x2 pixel group and the final pixels are interpolated. This calculation results are the derivatives DDX and DDY.
So in extreme UV values such as our cell case, this interpolation is wrong and the GPU render a hole 0-1 texture where it shoudnt(this are the white lines).

To fix this we have to feed the correct derivative, in this case lets use the pre frac UV values to calculate it:


Awesome, we got a nice tiling non tiling texture


Now we can apply the function to the tex and see the result!





We now can package all into a function and use it to create a sky full of detailed stars or a forest full of leaves. You can use this for all kind of materials!





Comments