How can I add a random float for a Material Instance Constant?

I have a constant material that I want to use for the sake of performance. This will be a mobile app so I don’t want to spawn a dynamic material for every tile. However, I want my tiles to pulse like a star would. It is easy enough to add a time element and cosine it doing a lerp between the dim/bright colors that make up the pulse. My problem is they all pulse exactly the same. I wanted to add a random float value to the time parameter so they all pulse at different times. Is there a way to achieve this without having to create a dynamic material instance for every tile??

Thanks so much!

  • Jeff

You can turn that time multiplier into a Collection Parameter, create a Material Parameter Collection in the content browser and use it in the blueprint to change the parameter for all instances.

I’m trying to find documentation on this. I’ve added the material collection in the browser and dragged/dropped it onto the material so I can access the “RandomOffset” value that I created. Now I’m trying to figure out how I can send this value via Blueprint. I’m not sure you can do this for a material instance constant… Wouldn’t there only be one value per constant instance rather than per spawned actor I’m generating??

I found the documentation and I don’t think you understand what I’m trying to do. I have 50+ spawned actors all using a single material (constant). However, I want each actor to be responsible for updating the material with a different random value at the start of the game.

I know you can do this if you created 50 dynamic material instances but I feel like that is a waste… The Material Parameter Collection is used to pass in global values that all materials may want to use such as Player Location, etc.

You could incorporate the ObjectPositionWS node into your material and use that as a means to create an offset. It outputs the world space coordinate for your object’s location. You could do something crazy like average R, G, and B together and use the output of that to drive an offset. I’d probably clamp it in some way to reach your goals.

But I just tested this network on some objects placed around my level and they all flicker differently.

Oh, and I didn’t need a MatInst Constant for this. Just FYI. You could, though. Just keep in mind that when you want different instances to have different values of ANYTHING, you need a dynamic material instance.

I guess I’ll have to make instances then. My tiles are aligned in a 2x2 grid so it won’t appear too random using the position. I also need to figure out if it will be a performance problem for mobile devices to have an instance per tile. Thanks!

For those coming here again (like me), if you’re using InstancedStaticMesh or HISM, there’s a node in the materials called PerInstanceRandom. It outputs a random float for each instance that doesn’t change along that instance’s lifespan. That way you can have random stuff unique for each instance, independent of their location.

1 Like

I get an error using the Object Position in my Light Function. It says [SM5] /Engine/Generated/Material.ush(2136,21-54): error X3004: undeclared identifier ‘GetObjectWorldPosition’