Random SubUV material for instanced meshes?

Hey guys!

I am attempting to make a material that chooses a random SubUV image from a sprite sheet. It will not be used in a particle system, however, which means I’ve run into a wall - how do I set it up to choose a random sub-image per instance? I’ve attached a shot of what I’ve been experimenting with, but which (probably obviously) doesn’t work.

PerInstanceRandom works only on Instanced Meshes, which is why it won’t work. There’s no way currently to randomize like this for just static meshes, so one of the things you could do is to have a Blueprint that generates the number for every one of it’s copies and passes it into the material via parameter. If you wan’t to keep it in the material, you can use pseudo-random UV offset - that will offset you object’s UVs in random directions using it’s world position. Here’s how to do it for objects without an atlas:

And for atlases you put the output of the previous node into a function containing this:

I find it decent enough, though at has some trouble with variance. I mean, sometimes you get multiple of the same result close to each other. Better than nothing though.

That’s awesome, thank you very very much for explanation and examples. Shall try it out immediately. :smiley:

Hmmm, turns out it’s not quite what I am after, sorry. Basically, what I am dealing with is a kind of homebrew vegetation system, so it does need to be per instance - I want to spawn a load of instanced meshes, with randomized textures based on a subUV atlas, sort of like you would with grass (the system we’re using has some expanded functionality that basic vegetation doesn’t offer, hence we’re making it ourselves). Basically, I want exactly the kind of random subUV thing I can do in Cascade where a random sub-image is chosen for a given particle, except I want to do it inside the material. In a nutshell, I want a material that says “My atlas has two rows, two columns. Choose and display a random image from that.”

As shown here, the thing I originally did ALMOST kinda sorta works, but it’s not quite there.

Problem is that it sort of blends the images together AND only shows the first two, instead of all four. But it DOES randomize them, which is what I want.

Success! I managed to splice together something based on your setup and some hints and some dumb beginner’s luck that actually does something close to what I want. Case closed, for now. :smiley: Suggestions for improvements are very welcome though - it does look like it favors the first frames compared the last ones, but it’ll do for now.

2 Likes

actually, your first method was closer. the issue was you needed to multiply it by 4, because the perinstancerandom goes between 0-1 and you have 4 frames, so that’s that. and it was blending between the frames because you hadn’t floored or ceiled it.
glad you figured out a way though :slight_smile:

Ahhh, it seems so obvious now that you say it. It works perfectly. Thanks alot, man. :slight_smile: