UE4 - Randomizing textures in Shaders

tldr: I have an idea of a randomized cartoony night sky with stylized stars shader. Trying to logic out how to create it in UE4.


So in my head I’m thinking about creating a night sky with randomized cartoony stars over a pretty galaxy background. I can sort of already create something like this by faking randomization through warping the UVs with a texture. But I am trying to think of a way to really randomize those stars, sort of like the noise texture but with the stars masks.

My current idea:

  1. I am thinking that you could create a mask of a white star, and a black mask.

  2. Then you could randomly transform the stars within the bounds of parameters, and THEN you could randomize how many black masks go in between the star mask. OR, just also randomize the black mask’s parameters.

  3. Then if you layer two of those you would have a very pretty cartoon night sky. Maybe you would bake the resulting textures onto your sky sphere, OR you could move the layers very slowly at different speeds if you wanted something like showing the passage of time, or to make it look like you were on a moving spaceship.

Is there a better way to do this? I don’t want the stars partly faded out from two mashed together masks, which is why I’m trying to think of this other method. I also do not know HLSL, but have been wanting to learn it, so if there is a possible method using it please let me know. :star: I am really just sharing my idea for brainstorming and for fun.

Here’s a way to think about it: basically, you’d divide the final rendered surface into a regular grid of cells (which is basically multiplying the uv’s by some number of repeats). Within each UV cell you have a little UV space by taking only the fractional part. If you just fed that one texture, you’d get a regular grid of that texture.

You can offset a bit within that by scaling the UVs down a bit around (0.5, 0.5) and then adding a bit of random offset. Keeping things inside cell boundaries makes it easier, but will be less fluid. By randomizing the downscale and the offset you can get a somewhat randomized field (you’ll want to use the floor of the UVs to generate your random offsets, since they will have to be consistent within a given cell.

You could also rotate within a cell, again with a random rotation applied before the offset and based on the integer cell coordinates.

Finally, if your source texture was a regular grid of star pictures, you could use the same technique to grab a random subset of the source texture instead of the whole thing. That would let you take, say, a 4x4 grid of 16 different star images and fill a sky with them at different sized and rotations.

For stars in particular you could do two passes to give a less gridded look, although you might start to see overlaps. Our it might be enough to offset alternate rows by half a cell to avoid too many right angles.

1 Like

I love this idea, thank you for your input! I will try to play around and see if I can get anything like that working. Creating a true randomized grid would be so cool.

I want to avoid having too many textures for the sake of optimization, but I really wonder if there’s a way to combine ideas. There has to be.

Any updates of progress will be posted here. I think starting with creating the grid, and seeing if I can effect each cell individually would be a good place to begin.

One thing to keep in mind, people have devoted entire careers to creating randomization algorithms. Sometimes trying to get your head around them can be a real nightmare! Something that has worked for me in the past, is simply generating a bunch of random numbers via python or something similar and saving them as an include file that you can import in unreal. That way you have true random numbers and you dont even need to worry about an algorithm :muscle:

1 Like

I have been generally looking more into HLSL as well, and this was a great start. https://www.youtube.com/watch?v=JUnjDu33Nq8&t=15s

It also seems like a great way to create a grid feeling of randomization using a noise texture. Nice!

I’m going to keep using HLSL combined with nodes and eventually come back to this night sky, and maybe make a night sphere.