Random Numbers (0, 0.2, 0.4 etc) in material

Hi,

is there any way to create random numbers in a material? The numbers should be 0, 0.2, 0.4, 0.6 and 0.8. I need these numbers to calc the uv tiling of a material at runtime

Normally, noise functions are REALLY problematic in a shader. The normal thing people do its to use a random texture, just a noise texture, and read that to get “random” values. In this case, you can create a random texture in photoshop wich has 4 shades of grey, only one channel, and read that from your material.

1 Like

Hi,

thank you for your quick reply. Could you please elaborate a little bit further? Say I have an image with four grey shaded squares in the red channel (rgb values of 128, 64, 32 and 0 respectively). Now to access the top left square I would use a texCoord of U .5 and V .5. If I plug the red channel of this pseudo-random tex into the uvs of my texture sheet that I would like to acces with a random number I get a colorized version of my sheet but not one specific part of it (like I would get if I had used directly specific texCords). So I certainly misunderstood something here :frowning:

Also is a solution something along these lines possible: every object has (quite natural) a unique world position. How can I (mis)use that position to generate a unique random number? I tried this one but obviously don’t get the desired result:

In broken English: first I add the x and z (or y and z) of the world pos. The result is divided by an arbitrary number, e.g. 13. With Frac I get a nice random-looking float equal to or greater than 0 and below 1 (at least I believe that - is there a way to visualize the result with a material node?). To cut off unwanted numbers the result is multiplied with 10 and rounded which leaves me with an int between 0 and 9. With Fmod I test if the result is even or odd. If even the result is divided by 10 to get a number smaller than 1, plugged into an appendVector and then added to a texCoord. If odd, 0.5 is added to make it even. Yes, there is a ton of math involved only to get a simple random value and its probably useless in a real production environment. But I still want to know how to do it.

And last not least: why do I have to plug in 3 comparisons into the if-node although I do need only 2 (A == B and A < B)? The official example at the Epic documentation has only 2 but in that case I get an error (using UE 4.92).

At least this does work (although not really random but position dependent):

2 Likes

And finally more flexible and as master mat:

The material works a little bit like a flipbook, but shows only one part of a texture depending on the position of the material in the world. I am using it to randomize of some of my assets.

2 Likes

Is there a way to get any randomness when used in UMG? Seems like the object position is the same for all instances.

You could try sampling a 256x256 tiling noise texture stretched across the screen via WPO.
That’s about it, you don’t have access to per instance random or other stuff that requires instancing within UMG.

At the same time, compared to 5plus years ago you may be able to just use the noise nodes. GPU processing power being a lot more than it was back then…

How about creating a dynamic material instance and and use a random float from the actor blueprint instead?

yep, simpler to implement