Jump to content

Find textures from an InternalPart?


Recommended Posts

EDIT original post:

Hello everyone!

I'm currently looking for a way to find the name of a bunch of textures at runtime (from editor and flight scene, in this case), starting from part's informations.

Right now, I've found how to access the part's materials and retrieve the main texture and normals, but in some case the part use an emissive texture (mostly used for cockpit lights and reactor heating effects). Can't find this one anywhere.

Also, I'm looking for a way to find textures in an IVA space, if there's one attached to a part.

It seems like the emissive texture is part of an animation, but I've no idea where to look...

I'm currently messing with a plugin forking DDSLoader and ATM, resizing textures to a ridiculous size to reload them at runtime. The first results are interesting enough to keep working on it, but if those textures are not reloaded, it does not looks right...

Thanks in advance!

I've finally figured how to get the emissive texture (I should have look for documentation here instead of Unity's website...), turns out "material.GetTexture("_Emissive")" is just what I needed.

I'm still looking for a way to get InternalParts materials, though.

Edit 2: modified the title for visibility. Still didn't find anything that could be useful regarding InternalParts.

The only way I have to access InternalPart infos are a "foreach (InternalPart ip in Resources.FindObjectsOfType(typeof(InternalPart)){}", but in this case all objects are considered as enabled, and I can't find a way to determine if this particular part is needed by the current vessel.

Edited by Lilleman
Link to comment
Share on other sites

In fact, just being able to access the "INTERNAL" node of a part would be a great help. At this point, I'm thinking of re-parsing all cfg files with a home-made function to acces it... There's probably a better way do to that?

Link to comment
Share on other sites

In fact, just being able to access the "INTERNAL" node of a part would be a great help.

Not much to that:

All parts

PartLoader.LoadedPartsList.ForEach(ap =>
{
if (!ap.internalConfig.HasData) return;

_log.Normal("{0} has the following INTERNAL:", ap.name);
_log.Normal("{0}", ap.internalConfig.ToString());
});

Live parts from vessel:

FlightGlobals.ActiveVessel.parts
.Where(p => p.internalModel != null)
.ToList()
.ForEach(
p =>
print(string.Format("{0} has INTERNAL of:\n{1}", p.partInfo.name,
p.internalModel.internalConfig.ToString())));

If you're looking for a list of all textures needed for InternalModels on the current vessel, that should be easy enough as well:

var internals = new HashSet<InternalModel>();

FlightGlobals.ActiveVessel.parts.ForEach(part =>
{
if (part.internalModel != null)
internals.Add(part.internalModel);
});

internals.ToList().ForEach(imodel =>
{
_log.Normal("Listing internals for " + imodel.internalName);

imodel.FindModelComponents<Renderer>()
.Where(r => r.sharedMaterial.mainTexture != null)
.Select(r => r.sharedMaterial.mainTexture.name)
.Distinct()
.OrderBy(url => url)
.ToList().ForEach(textureUrl =>
_log.Normal(" Texture: " + textureUrl));
});

If you want to access InternalModels which don't exist or outside of flight, you'll want to get them with PartLoader.GetInternalPart and looking at the name key in the INTERNAL ConfigNode from above

Edited by xEvilReeperx
Link to comment
Share on other sites

I finally found a workaround, but this is a way better way to proceed, thanks a lot!

Until now my solution was to add an internal module to each internal part, who send the name of the textures currently in use to an external object, on each OnAwake and OnDestroy method.

This is gonna make things much cleaner.

This is taking shape, for now the plugin seems to work with every part converted to DDS. Next step is to make it compatible with most RAM-consuming mods (I have to find a way to determine if EVE need high-res textures in a given SOI), but that's another story.

Of course, I didn't mention it, but if someone is interested in such a plugin, I can PM the sources. It looks a lot like LoadOnDemand, the main benefit being that DDS loading is fast enough to keep a smooth framerate.

Edited by Lilleman
Link to comment
Share on other sites

Yep, firsts results are quite good, it saves about 600MB of RAM while using OpenGL (about 1.2GB with DX9), while using full-res textures (on an install hitting +3GB of RAM usage with DX9). This is only for parts, though, I don't expect textures from other mods to be unloaded that easily.

Also, the memory consumption is slowly raising every time when reverting a flight, and I can't figure if it's a "natural KSP memory leak" or just textures not destroyed correctly...

Edited by Lilleman
Link to comment
Share on other sites

It seems to do the trick, thanks!

The memory is still raising from 15-20MB each launch,though, I'll have to test without mods to see if the game do it naturally.

Update (no need to up the post, it isn't that important at this point): The memory leak happen with Kerbal X on a stock install: it will raise the RAM usage each launch. The stock shuttle (I forgot his name) isn't affected by this...

It leads me to the conclusion the memory leak is related to parts, and not something from assets. This might need more investigations, though. At least, now I know my code destroy unused objects correctly.

Edited by Lilleman
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...