create a texture with shader code? ( without using a texture )

Hi, is there a way to procedurally generate a texture in shader code which is different each time you run the game?

Or for simpler question - how can i create a texture with shader code? ( without using a texture )

Of course you can “create” / sample a texture procedurally in a shader. However that limits you heavily what those textures actually look like. Also too complex shaders will lower your performance on the GPU. I think one of the best collection of procedural texturing is The Art of Code channel. For example the voronoi example or this smiley. Note that none of them will improve your performance and you are quite limited on what you can do. Also doing such things in a shader requires a completely different approach than creating procedural textures on the CPU.

Depending on what your texture actually should look like it might be better to create a texture on the CPU. Some things can be created in a shader more easily, however for example fractals like the mandelbrot set quickly get out of control in terms of performance. Note that you can’t store any state inside a shader. For example the voronoi sample I’ve linked. He has to generate all random points he wants to use for every fragment / pixel that is rendered. That’s why he later splits up space into a grid to reduce the number of points he has to check. Though this limits the movement of the random points.

In the end you have to ask yourself if you really need this in a shader. Creating textures in a shader is mainly useful for animated patterns. If you just want to generate a pattern once and want it to stay like this for this game it makes not much sense to recalculate everything each frame.