Reskinning on-the-fly problem

(Noticed too late that this thread belongs in the question subforum. My apologies. Can’t find a button to move or delete this thread, so would be grateful if a mod could move it for me)

Right. Long story short: I’m trying to reskin player models ingame, using existing materials. I’ve run into problems however.

I’m using the following script:
local mat = Material(“models/humans/male/group01/citizen_sheet”)
mat:SetMaterialTexture("$basetexture", Material(“models/humans/male/group01/citizen_sheet”):GetMaterialTexture("$basetexture"))

// Breen: "models/breen/breen_sheet"
// Citizen01: "models/humans/male/group01/citizen_sheet"
//citCloth = Material("models/bloocobalt/citizens/citizen_sheet")
local citCloth = Material("models/base_clothing/citizens/citizen_sheet_torso")

//local citCloth = Material("models/humans/male/group01/citizen_sheet")
local changed_Textures = {}

hook.Add("PrePlayerDraw", "clothtest", function(ply)
    if ply.Cloth then
        table.insert(changed_Textures, {citCloth, citCloth:GetMaterialTexture("$basetexture")})
        citCloth:SetMaterialTexture("$basetexture", ply.Cloth)

hook.Add("PostPlayerDraw", "clothtest2", function(ply)
    for k, v in pairs(changed_Textures) do
        v[1]:SetMaterialTexture("$basetexture", v[2])
        changed_Textures[k] = nil
        table.remove(changed_Textures, k)

concommand.Add("changeclothes", function(ply, cmd, args) 
    if not args[1] or not args[2] then
        print("I poop on your chest")
    local targ = Entity(args[1])
    if targ and targ:IsPlayer() then
        targ.Cloth = Material(args[2]):GetMaterialTexture("$basetexture")
        print(Material(args[2]), Material(args[2]):GetName(), targ.Cloth, targ.Cloth:GetName())

(excuse the clutter, this is a test script I run off the /lua/ folder to quickly see changes in code)

I’m using this on a modified version of Bloocobalt’s “Enhanced citizen” pack. If you are unfamiliar with it, its essentially the hl2 characters, with bodygroups split up for hands, torso, legs, and so forth.
I’ve made it so torso relies on models/base_clothing/citizens/citizen_sheet_torso.vmt, and same for legs.

Now, using the above script, I can reskin both torso and legs just fine. However, certain skins/materials that I have is projected entirely wrong, even if the same textures work just fine using this hack on the base hl2 character.
The base hl2 character(s) have the exact same projection, and uses the exact same skin as my modified model.
The .vmt’s of the original skin, and the skin I replace with are identical (except, obviously, the contents of $basetexture).

I tried overwriting the file contents of models/base_clothing/citizens/citizen_sheet_torso.vmt with one of the skins I’m trying to replace it with ingame, and that loaded just fine. I tried to use the script above to change that to the original skin I overwrote the file with: It got messed up again.

Here’s a couple of observations:

  • The modified model I am using have the same problem, even before I decompiled it or touched it. The standard hl2 character does not, however, and can use all of the skin samples I have just fine.
  • Actually changing the contents of the texture file out-of-game worked fine. Trying to swap to that texture ingame using this script failed however. see picture below.
  • Some skins do work, roughly ~10% of the test skins I have available. Despite this, all of them should be compatible. They are all exactly the same in terms of texture placement, size, and whatnot.
  • The .wmt’s are also identical, except the obvious difference in contents within $basetexture.

At this point, I have exactly no idea what I’m doing wrong, or what to try next.

Here is some debug data I’ve collected:

] changeclothes 2 models/humans/male/group02/MaleSurvivors"
Player [2][Bot01]
Material    models/humans/male/group02/malesurvivors    ITexture: 476D1B68    models/humans/male/group02/malesurvivors

*Original (models/base_clothing/citizens/citizen_sheet_torso.vtf overwritten with the contents from models/humans/male/group02/malesurvivors)

*Skin changed with script above, replacing $basetexture with models/humans/male/group02/malesurvivors (same skin, different file). Notice the stripe on the back.

*If things were working correctly, the two images above should be identical - I’m replacing a skin with an identical skin. Can anyone help me understand what separates them?

Note, I can provide the .vmt, .vtf of all the relevant files. Hell, I can even throw in the .smd and .qc files of the modified model I am using, if that helps.

Is the VTF of the modified one “Clamp T” or “Clamp S” applied to them?

By the way, I tried to do something similar to this before
I dont have the code at hand right now, but its a crashy bitch to get to work properly, unless garry fixed it

Indeed they were. This fixed my problem.
You have no idea how happy that made me :smiley:

Thanks for the advice, I’ll pay close attention to the performance.

Are you going to release this script? It seems pretty good.

He just did. check op.