Game Development Community

How often can I call Material::reload()?

by Daniel Buckmaster · in Torque 3D Beginner · 11/26/2013 (7:07 pm) · 12 replies

I want to do some material-colour-changing effects in a game, which I now I can easily do in scripts by changing the diffuse colour of a Material and then calling reload(). My question is, how expensive is the process? I don't want to be doing it every tick if it's going to significantly stuff up rendering speed.

I'm looking into other solutions like using a big coloured light instead of a material change (all my base materials will be white, so the effect will be similar), but this approach seems elegant, and I'm interested to see if I can use it for other effects as well that won't be so well-simulated by a light.

About the author

Studying mechatronic engineering and computer science at the University of Sydney. Game development is probably my most time-consuming hobby!


#1
11/26/2013 (11:56 pm)
personally I would do colour changing effects with shaders. I think it would be a lot less taxing to have one shader scan materials for alpha layers and then apply a colour to the alpha layer of the materials. Team colours are sometimes done this way, especially if the player has the option to choose their team colour.
#2
11/27/2013 (3:09 am)
I agree, that's probably a more correct way to do it... I just don't know the first thing about shaders and wanted to know whether my quick-fix solution would be acceptable :P.

I actually did a really simple test using Charlie Patterson's Twillex library, and didn't see any frame hit using it on a single material. So that's not too bad. And it was a really cool effect!
#3
11/27/2013 (3:21 am)
Well i could write a shader for you but only when i get back to my pc later on, not unless someone on here beats me to the punch lol

I would test a scene with at very least 100 cube objects in it then change their colour by executing the change colour code in console and take a note of what happens to the frame rate and then do the same test with a shader programmed to do the same thing. whichever one has the least frame drop will obviously be the best option.
#4
11/27/2013 (3:33 am)
Will the cost incurred by reload() depend on the number of objects using the material? Fair enough. I might test it out on my trees instead of my terrain - there's 50 of them, with a couple dozen polys each.

...hah, well that's creepy. If anything the fps seemed to increase :P. Which is nonsense and must have been measurement noise. But no visible drop, which is good.

I'd appreciate seeing a shader solution, I do need to get my feet wet with them at some point!
#5
11/27/2013 (3:41 am)
i actually didnt know whether it would or not lol but its nice to see that it dosnt its now obvious that it is down to how many materials use the call at any one time, what i actually should have said was 100 cube objects with different materials assigned to them lol
#6
11/27/2013 (10:45 am)
gameclay.com/documentation/per-shape-shader-values-t3d
Mostly still accurate. Currently using something similar to alpha-fade a detail map based on an object's health, for instance.
#7
11/27/2013 (12:56 pm)
Daniel, sounds interesting and possible but, have you thought about maybe just adding an image sequence to the material you are try to get to change? Basically a series of colors that switch over time or per-triggered event.

Another possibility, perhaps use the TOD object to trigger a texture change? I experimented with this technique for various texture changes in my trees (spring, summer, fall, winter) it works pretty well.

Kinda rolls into my 'simple is better', just a suggestion.

Ron
#8
11/27/2013 (2:08 pm)
Thomas - yeah, that'd be a better test. On the other hand I'm not interested in chancing a hundred materials at a time, just a handful.

Azaezel - thanks for that! I'd read it before but was under the impression it was mostly out of date. I'll have a re-read and see if I can use it.

Ron - I should have gone into a bit more detail on the problem itself. I intend to start some precipitation, and gradually have the terrain change colour to match. Like, over thirty seconds or a minute. Unless I had a list of 500 images of varying shades (for each colour!) I think my one-line* solution wins ;).

*Using Twillex, of course. But I had that installed anyway! If anyone's interested, my code is basically:
Twillex.toOnce(5000, GroundMat, "diffuseColor[0]: 1 0 0 1", "onChange: reload");
I had to change the way Twillex calls onChange: it wants to pass one argument (the tween object), but reload() takes no arguments, so I just changed that. Might be nice to have a more robust and flexible solution, but this works for me.
#9
11/27/2013 (2:18 pm)
Daniel,

That explains things a bit and yeah.... both my idea's are not a good choice. However, take a look at the way wet depth works in the water block. I think that might lead to a different way of looking at your issue. Basically, the when wet the material gets darker. The wetness shader does a similar thing. Again, just an idea.

Ron
#10
11/28/2013 (5:40 am)
O ok yea teamcolor shader would have been useless here. Just out of curiosity what colours are u going to be changing to and from? is it just like if its snowing change to white if its raining change to a dark grey?
#11
11/28/2013 (6:36 pm)
Yep, basically. Pretty much all my materials are flat white with toon outlines. The idea is to have different colours of rain, and the terrain should change colour to match.

Of course, this could be done with a light in the sky above the player, and would make things like shadows where rain couldn't get anyway. But I'm kind of interested in trying out other uses for the effect, like displaying damage levels or something.

Wetness is an interesting idea, I never have had a look at how that happens. I'll keep it in mind for when I do start learning shaders :P.
#12
11/28/2013 (6:58 pm)
Its ideas like that that make me wish polygon limits were a thing of the past, imagine doing that with decals :D where the red rain hit a red pool would form would be really cool but polygon counts would soar =/