Garry's Mod

Garry's Mod

Tidak cukup rating
Converting brush (map) materials for Material tool use.
Oleh 80Hg
Teaches you how to convert materials meant for map terrain use to ones that can be used with the material tool!
   
Penghargaan
Favorit
Difavoritkan
Batalkan favorit
Introduction and requirements
Hello! Today I'm gonna teach you how to convert materials meant for use on map terrain into materials that you can use on models with the material tool.

It's a fairly simple process, so let's get started.

You will need GCFScape to get the materials and VTFEdit to edit .VMTs so go get them here:
Valve Developer Wiki - GCFScape
Valve Developer Wiki - VTFEdit
Step 1: Folder Layout
First of all, let's get the addon layout out of the way.
The folder of your addon should look like this:
lua -autorun --addmaterials_*somethingunique*.lua materials -*a unique name for the converted custom materials folder* --all your converted materials go here

I am not going to cover uploading the addon to the workshop. For that, go here: GMod Wiki - Workshop Addon Creation[wiki.facepunch.com]
Step 2: Converting materials
Option 1: Manual conversion
Now it's time to get to the tedious part. (Well, tedious if you want to convert a lot of materials)

Find a material that you like, either in-game with an asset browser addon or outside of the game with GCFScape and VTFEdit.
For this tutorial, I'm gonna use the toxicslime002a material from HL2. For clarity's sake, this is the material:

Now that you've got the material you want. Find the VPK that contains it with GCFScape. In my case, the appropriate VPK was in steamapps/common/Half-Life 2/hl2/hl2_misc_dir.vpk

Keep in mind this distinction: VTF is the actual image texture file, VMT is a text file which states how the material will be put together. We need the VMT.

Once you've got your VMT file, it should look something like this when you open it with VTFEdit:

See that line I've outlined with a red rectangle? That is the only part that we need to change. Do not touch anything else unless you know what you're doing.

The reason why map materials do not work with the material tool by default is that they use a shader which is meant only for map brushes and displacements. (terrain)

To make most materials work with the tool, we have to change "LightmappedGeneric" (or whatever the VMT starts with) to "VertexLitGeneric" which is the shader used by models.

Note that some materials may require more changes. For example this slime material has a specific entry for when the detected DirectX level is 7 or lower, which is when it will try to use the LightmappedGeneric_DX6 shader instead. Probably a good idea to change this to VertexLitGeneric_DX6 just to be sure.

Also, some materials like water materials will not work by simply changing the shader to LightmappedGeneric, and require more work. I may cover this in the future.


Once you've done that, simply press Ctrl+S to save the new VMT, then move it over to the converted custom materials folder that we made in the 1st step.

Option 2: Automatic Conversion
Wanna save yourself lots of time and frustration? What if I told you that you can automatically convert thousands of materials in just a second? Sounds neat, right?

Well you can, you just need to use a simple Python script that I made.

This script will convert all LightmappedGeneric, UnlitGeneric and WorldVertexTransition materials to VertexLitGeneric. For anything else, you're gonna have to do it manually or edit the script.

Blend materials will only use the first material.

First of all you're gonna need Python[www.python.org] installed. so go to their website and get it.

Next, go to the folder where you put your converted materials in. Copy in all the unconverted materials that you want.

After that, make a new text file called convertmaterials

Open it with your favorite text editor and paste this in:
import os import re directory = os.listdir(os.getcwd()) for file in directory: if not file.endswith(".vmt"): continue open_file = open(file, "r") read_file = [open_file.read(), 0] num_subs = 0 replacers = ['LightmappedGeneric', 'UnLitGeneric', 'WorldVertexTransition'] for replacer in replacers: read_file = re.subn(replacer.lower(), 'VertexLitGeneric', read_file[0].lower()) num_subs += read_file[1] modified = read_file[0] if num_subs == 0: print(f"Nothing to change in {file}") continue write_file = open(file, "w") write_file.write(modified) print(f'Converted {num_subs} shader names to VertexLitGeneric in {file}.')

Save the text file and change the extension to .py

You can also straight up download the script from here if you're having trouble with pasting the code: Download Link[drive.google.com]

After that, click the empty part on the right of your address bar at the top (this thing)
Remove everything and type cmd, then press enter
This should open up a new cmd window with the directory set to the material folder.
Type in this:
py convertmaterials.py
Then press enter

The script should now run and convert all the materials in the folder really fast.
The window should look something like this. It will print whether the material was changed or if there was nothing that needed to be changed.

And voila! Thousands of files converted in seconds.
Step 3: Adding the material to the material tool list (Optional)
At this point, you can already use the material with the tool, but it will not show up in the Material Tool list yet, as we need a script for that. I have created a simple script, which will find all VMTs in a folder and add them to the tool's materials list.

MAKE SURE THAT THE LUA FILE YOU CREATED HAS AN UNIQUE NAME TO AVOID ADDON CONFLICTS! E.G. IF YOU NAME IT "addmaterials.lua" IT WILL CONFLICT WITH MY CONVERTED HL2 MATERIALS ADDON. GIVE IT A UNIQUE NAME OR PREFIX/SUFFIX!

Open up the lua file that you created in the lua/autorun folder with your favorite text editor and paste this inside:
local folderName = "gigaconvertedmats" -- Replace this with your converted materials folder's name local fileTbl = file.Find("materials/"..folderName.."/*.vmt", "GAME") -- Finds all VMTs in the folder. for _, fileName in pairs(fileTbl) do -- Loop over each file name that we have received. local stripped = string.StripExtension(fileName) -- Remove the .vmt extension of the file. list.Add("OverrideMaterials", folderName.."/"..stripped) -- Add mats to tool list end

The code is commented for learning purposes. You can safely remove the comments from the script.

Be sure to change the folder name in the quotes on the first line of the code to your folder name!
Step 4: Profit
With this, you should be all done and your materials should show up in the Material Tool list.

If you find any errors with the code, please write the error you get in the comments so I can take a look.

For an example of a completed addon, look here:
https://steamcommunity.com/sharedfiles/filedetails/?id=2810344748

A pack of most HL2 map materials converted with the Python script here.

Hope this was helpful!
2 Komentar
80Hg  [pembuat] 25 Jun 2022 @ 4:35am 
To use them on props and with the tool gun. The default map materials do not work on props.
Spicyzombie005 24 Jun 2022 @ 9:04pm 
Umm? is this just meant to be able to use the new materials with the tool gun or is this meant to make the materials look better when on a prop