Preload Materials
by Matthew Hoesterey · in Torque X 2D · 11/09/2009 (2:29 am) · 62 replies
I have a question about preloading materials in torque. I'm trying to improve load times and was wondering. When preload materials is set to true does torque only load the materials that are used in the .txscene or all the materials in all levels?
Thanks for any info.
Thanks for any info.
#22
I'm not lucky enough to have any non coders other then my artists who don't integrate or work with variables. :)
thanks again for the help.
11/12/2009 (1:56 pm)
Interesting. I may try cutting that code once I have my char data scene stay loaded. I'm not lucky enough to have any non coders other then my artists who don't integrate or work with variables. :)
thanks again for the help.
#23
12/12/2009 (4:18 pm)
I wrote a program to automatically rip out unused materials from a scene. I'll post it once I get around to publishing it. Till then if anyone needs it just let me know.
#24
I was just about to write it myself, but if you have it done already there's no need to have any double around: are you going to put it on TDN?
12/14/2009 (6:30 am)
Hi Matthew,I was just about to write it myself, but if you have it done already there's no need to have any double around: are you going to put it on TDN?
#25
12/14/2009 (9:45 am)
Yes I'll post it here and on the TDN. Give me a few days and I'll have it up :).
#26
12/14/2009 (10:34 am)
Great, thank you ;)
#27
12/14/2009 (1:02 pm)
Nice work Matt - looking forward to having a look at it.
#28
Here is the tool source
http://www.mhoesterey.com/TorqueX/TorqueXLevelMaterialRipper.rar
All you need to do is:
1)Open the Levels.txt file and change the paths found in it to point to your levels. You can add any number of levels. just start at line one and only have 1 level per line (no skipped lines)
load the .csproj file into c#
Build and run it.
It will update all your levels
I've tested this and it works but BACKUP YOUR LEVELS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I have not tried this with other peoples files and would hate you to lose work, there could be bugs I don't know about. Use at your own risk :)
If this works for you guys let me know and I'll have this code stickied and added to the TDN.
Thanks, hope this code helps.
12/16/2009 (9:50 pm)
So I had trouble getting it to publish correctly out of C# Express. (I have never published a tool with an external config file (in this case just a text file) and might be doing something wrong. Usually my config file is built into the gui) This tool was built quickly (about 3 hours) so please forgive the Hacky feel.Here is the tool source
http://www.mhoesterey.com/TorqueX/TorqueXLevelMaterialRipper.rar
All you need to do is:
1)Open the Levels.txt file and change the paths found in it to point to your levels. You can add any number of levels. just start at line one and only have 1 level per line (no skipped lines)
load the .csproj file into c#
Build and run it.
It will update all your levels
I've tested this and it works but BACKUP YOUR LEVELS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I have not tried this with other peoples files and would hate you to lose work, there could be bugs I don't know about. Use at your own risk :)
If this works for you guys let me know and I'll have this code stickied and added to the TDN.
Thanks, hope this code helps.
#29
12/16/2009 (10:13 pm)
Have downloaded it. Looking forward to checking it out tomorrow to confirm that it works (after backing up first, of course ;-)
#30
(1) when an unused animation is removed from the scene file, the material the animation uses is left behind - even if the material isn't used by anything else.
(2) materials referenced with a simple "nameref=" are removed even though they are being referenced in the scene file. (Materials referenced by "Material nameRef=" remain in the file, but components can also reference materials and animations via properties in the form "MyProperty nameRef=" - materials referenced in this way are currently removed)
12/17/2009 (4:53 pm)
Seems to work okay. Just a couple of points where it needs tweaking.(1) when an unused animation is removed from the scene file, the material the animation uses is left behind - even if the material isn't used by anything else.
(2) materials referenced with a simple "nameref=" are removed even though they are being referenced in the scene file. (Materials referenced by "Material nameRef=" remain in the file, but components can also reference materials and animations via properties in the form "MyProperty nameRef=" - materials referenced in this way are currently removed)
#31
Can you give me an example of a component that references a material in the way you describe?
I'm guessing I don't have any components that reference materials in that way. I can add one to a scene for testing and update the tool to take that into account though.
12/18/2009 (11:06 am)
Hey Duncan, Thanks for finding that stuff. I'll take a look this weekend. I'll make sure the program does another loop to rip out mats after the anims.Can you give me an example of a component that references a material in the way you describe?
I'm guessing I don't have any components that reference materials in that way. I can add one to a scene for testing and update the tool to take that into account though.
#32
If you put that in a component and then load up the scene editor you will see it gives you a list of materials to select for the property.
In the scene file it will then show as "MyMaterialProperty nameRef=" and the name of the material being referenced.
12/18/2009 (12:40 pm)
I don't recall a TX component that does it, but the platformer kit does and I use it a lot too. In c# the code for a property referencing a material is:public RenderMaterial MyMaterialProperty
{
....
....
....
}If you put that in a component and then load up the scene editor you will see it gives you a list of materials to select for the property.
In the scene file it will then show as "MyMaterialProperty nameRef=" and the name of the material being referenced.
#33
Should fix all issues. It looks for materials referenced in components and rips out unused animations. I tested it but missed things before so let me know if I missed anything else.
The new tool also finds Refraction mats along with simple materials.
Still really Hacky but should work a bit better. :)
12/19/2009 (1:25 am)
Re-uploaded the file. Same name, same directory, same bat time... arg.Should fix all issues. It looks for materials referenced in components and rips out unused animations. I tested it but missed things before so let me know if I missed anything else.
The new tool also finds Refraction mats along with simple materials.
Still really Hacky but should work a bit better. :)
#34
just downloaded. I let you know if there are issues. Thanks a lot ;)
Pino
12/21/2009 (6:10 pm)
Hi Matt,just downloaded. I let you know if there are issues. Thanks a lot ;)
Pino
#35
With that in mind the following may be useful for scenes that require additional materials not directly referenced in the scene itself - a component that references a pair of arbitrary lists of materials and animations so that the material culling tool will leave the materials/animations in the scene:
First, add the following two classes to your project:
Then create a new component class and add the following two lines:
Assign the component to an object in your scene and then add all the extra material/animation references that you need.
TXB didn't seem to like List<>, etc, of materials or animations, hence the XML**** classes that just force TXB to let us create a list of stuff.
12/23/2009 (10:07 am)
@matt: I've run it on one of my more complex scenes and it seems to be working fine now - haven't *actually* loaded the scene as the scene itself needs additional materials not referenced in the scene directly so would crash on load. However, I have given the xml file a visual inspection and everything looks in order.With that in mind the following may be useful for scenes that require additional materials not directly referenced in the scene itself - a component that references a pair of arbitrary lists of materials and animations so that the material culling tool will leave the materials/animations in the scene:
First, add the following two classes to your project:
/// <summary>
/// Facilitates creating a list of material references in a component in the scene editor
/// </summary>
public class XMLRenderMaterial
{
public RenderMaterial Material
{
get { return _material; }
set { _material = value; }
}
RenderMaterial _material;
}
/// <summary>
/// Facilitates creating a list of animation references in a component in the scene editor
/// </summary>
public class XMLAnimationData
{
public T2DAnimationData Animation
{
get { return _animation; }
set { _animation = value; }
}
T2DAnimationData _animation;
}Then create a new component class and add the following two lines:
public List<XMLRenderMaterial> MaterialsList; public List<XMLAnimationData> AnimationsList;
Assign the component to an object in your scene and then add all the extra material/animation references that you need.
TXB didn't seem to like List<>, etc, of materials or animations, hence the XML**** classes that just force TXB to let us create a list of stuff.
#36
12/23/2009 (5:56 pm)
Awesome Idea Duncan with the class. I'll send this info to GG to sticky
#37
Did you mean to include some extra brackets?
12/24/2009 (6:26 pm)
In the code there are a few lines like:if (s != null && s.Contains(simpleMatTag) && !_skipTillAnimClose || s != null && s.Contains(refractionMatTag) && !_skipTillAnimClose)
{
...
...
}Did you mean to include some extra brackets?
if ((s != null && s.Contains(simpleMatTag) && !_skipTillAnimClose) || (s != null && s.Contains(refractionMatTag) && !_skipTillAnimClose))
{
...
...
}
#38
How would the extra brackets make the code read differently? Wouldn't the if statement simply short circuit and then test the other side of the or operator if a test returned false? It seams to work like that. Is this unoptimized?
I'm a self taught coder so I may not know something that I'd love to learn. :)
12/28/2009 (11:31 am)
Heya Duncan, How would the extra brackets make the code read differently? Wouldn't the if statement simply short circuit and then test the other side of the or operator if a test returned false? It seams to work like that. Is this unoptimized?
I'm a self taught coder so I may not know something that I'd love to learn. :)
#39
Well it's not quite about optimizing code. But, if you're looking to improve your coding in general, you should consider writing for human readability. After all, it's people that have to modify and maintain code and fix bugs. The computer knows what you mean, but for human readers it makes it clearer if you bracket the two halves of the if() - the order of precendence of conditional operators isn't something many people commit to memory so it's good to be clear in your code :-)
You've obviously come a long way teaching yourself, which is a huge accomplishment. If you're interested to kick things up to the next level then I can't recommend the book "Code Complete" highly enough.
12/28/2009 (12:42 pm)
Yep, I checked and you're right the if() would evaluate the same. Because && is a higher priority operator than ||. So no need to change it for functional reasons.Quote:
I'm a self taught coder so I may not know something that I'd love to learn. :)
Well it's not quite about optimizing code. But, if you're looking to improve your coding in general, you should consider writing for human readability. After all, it's people that have to modify and maintain code and fix bugs. The computer knows what you mean, but for human readers it makes it clearer if you bracket the two halves of the if() - the order of precendence of conditional operators isn't something many people commit to memory so it's good to be clear in your code :-)
You've obviously come a long way teaching yourself, which is a huge accomplishment. If you're interested to kick things up to the next level then I can't recommend the book "Code Complete" highly enough.
#40
(I've actually been yelled at by a few of the coders I work with at my real job who tell me my code is unreadable lol. So I know I need to improve in that area. Its harder as no one usually needs to read my code.)
12/28/2009 (12:50 pm)
Awesome Duncan, I ordered the book on amazon just now. Thanks for the advice. :)(I've actually been yelled at by a few of the coders I work with at my real job who tell me my code is unreadable lol. So I know I need to improve in that area. Its harder as no one usually needs to read my code.)
Torque Owner Duncan Colvin
The Unload scene thing? The thread you linked to is an old one on TX2. I haven't had any issues with scenes not unloading properly I have to say. Maybe that's because I load a 'globalvars' scene before any of the others and that scene remains loaded for the entire game? (the thread mentions something about maybe loading a persistent scene can help). I always do that as it is a useful place to put components that non-programmers can use to tweak key global parameters for the game (instead of having to keep asking me to do it directly in the code).