Post by Son of Persia on Sept 21, 2014 5:04:53 GMT 10
Resident Evil 4 Ultimate HD Model Importer/Exporter Toolset
By Son of Persia
September 2014
Hello dear fiends. The waiting is over and now it's time to present you the complete model importer/exporter toolset of Resident Evil 4 Ultimate HD .
This project is a continuation(or actually the completion) of RE4 GC/Wii model importer/exporter project I started back in 2012. Thank god, it's now finished after two years and is ready to make you happy .
Let me thank you for being patient while I was developing/polishing this toolset and making a good tutorial for using it.
In this tutorial I assume you are a complete newbie and explain every thing from ground up. I know that many of you who are reading this may be advanced/pro modders but I have no choice but to expalin every thing in detail so that the toolset can be used by anyone who is interested.
So pro guys, please forgive me for being very detailed on this.
Why I used a batch tool instead of a script? The answer is very simple
1- Batch processing is much faster, more optimizable and suitable for this kind of offline tasks compared to scripts.
2- I know that many modders do not(can not afford to) use 3DS Max in their projects and preferably use free softwares such as MilkShape3D OR Blender. So there is no point in forcing the people to use what I like to use by writing the toolset in maxscript(phyton).
Things you must know before start importing custom models into RE4 UHD
1 - Model Import Limitations
As all of you already know, this game is a direct port of GC/Wii versions of RE4. Thus, the constraints of that old versions(that were imposed by the technology constraints of those years) applies to this port too.
The most important and limiting ones for importing custom models are:
a- The maximum number of model vertices/polygons
b- The maximum number of vertex bone/weight palette entries
What is vertex bone/weight palette?
GC/Wii versions of RE4 used to store vertex data in separate blocks for most compactness due to the lack of disk storage at that time (and maybe some hardware specific issues of that consoles). Therefore, vertex position, normal and texture coordinates were separated.
The other important separated block was vertex bone/weight info. They used a very efficient and compact method for storing vertex bone/weight data. At first they detected and stored unique N-tuples of up to 3 vertex bones and weights in a table and then stored the index of corresponding table entry in vertex/normal structures(instead of repeatedly using and wasting up to 8 bytes for storing bone/weight data of each vertex/normal).
Oh yeah, japanese taste. Brilliant solution for the most compact storage of model data.
But they limited the maximum number of unique vertex bone/weight entries that can be stored in the table to 255.
That's it. More than that will make the game crash. And strangely, after 9 years this old constraint still exists and is ported to this version too.
Long story short, you CAN have a 20000 poly model BUT it CAN NOT have more than 255 unique vertex bone/weight entries. Really disappointing and limiting factor for importing fully custom models.
But come on guys, we can still import something if not anything. Right?
And about the maximum number of model vertices/polygons, I personally have not tested it yet but the game already works just fine with 20000 vertex/poly models. You can give it a try and figure it out by yourself.
Just beware of the maximum number of vertex bone/weight entries and DO NOT use complex rigging/skinning on your models.
2 - Usage of new kind of TPL files
In GC/Wii versions of RE4, TPL files were used to store compressed version of model textures due to lack of available storage space. Thank god(and also Qloc), This limitation is finally removed but TPL files usage still exist for dumb consistency with old versions.
This time TPL files are used as texture index tables. They hold textures info(resolution, type, etc...) in addition to the number of model texture package and the index of textures of that package used by the model. It's very important to use this sensitive files carefully unless that ugly "Button Textures" will start to appear on your model.
But don't worry, the toolset will automatically do it for you as long as you give it correct info.
What is correct info?
As you all know, at this port, each model has an associated texture package number. And this number is stored and frequently used in TPL files. Model texture packages are usually stored in *.pack.yz2 or *.pack formats. You can easily extract and view model textures using sectus's YZ2Tool. By default all TPL files inside a UDAS archive use an strict order for accessing model textures because textures are stored sequentially in texture packages.
Thus if you want to change the number and sequence of textures inside a package, you must also edit/update ALL texture indices in ALL TPL files. For example assume that we want to replace leon with chris and leon had 10 textures(from 0000 to 0009) but chris has 14 textures(from 0000 to 0013).
Also let's assume that leon's default texture package already contains 20 textures(extra textures are used for the items that leon carries/uses such as grenades, rocket launcher, bullet shells, radio, chicken eggs, etc...).
If we want to simply rename and copy chris textures to the texture package, we must also change/shift the other texture indices/numbers and here is where the main problem arise - changing the number/index of textures without updating their indices in the TPL files that use/index them.
The toolset automatically generates a valid TPL file for chris But what about the other TPL files?
You must manually open and edit/update ALL of them in a hex editor if you want chris carry items with correct textures in game. A whole pain in the ass .
But "don't worry. I'll take good care of it" - ada wong.
The solution is to add new textures to the end of current texture package instead of adding them to the beginning of texture package. Said, instead of using 0000 as first number/index for chris textures use the default number of textures in the package as first number/index for adding chris textures.
For example if the default package already contains 20 textures and we want to add 14 more for chris model, we simply use indices 0020-0033(instead of using 0000-0013) for new chris textures and add them to the end of current texture package.
Persian genius .
This way you will be able to easily add new textures to the default texture package without having to modify/update ALL other TPL files. After renaming and adding chris textures to default texture package, you must use the default number of textures in the package(as first index of chris textures) as one of the inputs to Bin Generator tool for generating chris's BIN and TPL files.
Wow. Really long and boring subject. But trust me guys, it's worth it .
All right guys, after this really SHORT intro, you know the basics of importing custom models into this brilliant, state of art port of RE4. But Before proceeding to model import/export topic, let's mention some necessary preparations.
1- To edit a model, first you need to access it's UDAS file that contains the actual model BIN and TPL files. At this game, all model UDAS and texture package files are compressed in .lfs format.
Therefore, first you need to extract LFs archives. To do so, first select the model file you want to edit from "Em" folder of RE4 UHD and extract it's lfs archive using LFS script(thanks to dear Thief1987) as following :
a- Double click on quickbms tool(Thanks to dear aluigi from xentax) included in the package
b- Select RE4_UHD_LFS_Extract.bms and click open.
c- Select model's *.UDAS.lfs archive you want to extract and click open.
d- Select where you want to store extracted UDAS file and then click save.
2- After extracting lfs archive, remember to put extracted UDAS file in the same folder as RE4_UHD_UDAS_Tool.exe and simply double click on UDAS_Extract.bat to extract UDAS archive file(s). After extracting the contents of UDAS archive, you'll see a folder(with the same name as UDAS archive file) that contains a bunch of model files.
A Brief on Textures
1- All diffuse textures must be in DXT1 DDS format without alpha(must save your textures in DXT5 and then compress/convert them to DXT1 using a tool such as AMD compressonator).
2- All specular/bump textures must be in 32-bit TGA format with alpha.
3- Other remaining opacity(transparency) textures must be in 32-bit TGA format with alpha(actually alpha and RGB channels must be the same).
So, before start importing a character, please prepare/convert the textures according to the above formats. It's incredibly important.
Extracting and Repacking Texture Packages
First of all you need to know the number/name of texture package of the model you want to replace. It's very simple to find out. After extracting original UDAS archive, open any of the extracted TPL files in a hex editor and look at it's end.
You must be able to see a repeated sequence of 8 bytes with a fixed 4 byte number and another increasing 4 byte index. That fixed number is the number/name of model texture package.
Note that this 4 byte number is stored in reverse order(little endian order) and to use it you must reverse it's bytes to get the actual usable texture package number.
For example if you find texture package number of "00 00 00 10" in TPL file, the actual texture package number would be "10 00 00 00".
You must use the actual texture package number as another input to model generator tool.
I know, modding this game is a real ... in the ...
After detecting model's texture package number/name, you need to extract and/or repack it's content.
First of all copy the found texture package(package_number.pack.yz2.lfs/package_number.pack.lfs) from "ImagePack"/"ImagePackHD" folders of RE4 UHD to a separate folder.
Then use quick bms and LFS script to extract it's compressed lfs archive. Then simply use dear sectus's YZ2tool to extract and view model textures. It's very easy to use.
Extracting Textures
After copying extracted texture package(that after using LFS script is now in *.pack.yz2/*.pack format) to the same folder as YZ2tool.exe tool, just double click on unpack-all.bat file.
The tool creates a folder with the same name as original texture package and exports all textures to it.
Repacking Textures
After renaming the textures of your custom model and converting their names to sequentially increasing numbers(in order to be added to the end of original texture pack), copy them to the folder of extracted (original) texture pack.
Make sure no original texture is replaced. And then simply double click on repack-all.bat file.
The tool will then repack all textures in image pack folder and generates a new *.pack.yz2/*.pack file that can be directly used in game.
Another important(and the last) input to the model generator tool would be the default number of model textures.
I mean the number of textures you see right after extracting the original model texture package BEFORE adding new textures to it's end.
All right, preparations done. First let's deal with exporting models.
Exporting Models
As I mentioned before, models are stored in BIN files. TPL files contain indices of model textures.
Each texture index includes the number of model texture package plus individual texture index/number inside texture package.
In order to extract a BIN model file with correct textures, you must also specify the name of it's associated TPL file.
Usually TPL files come right after or before a model BIN file but it's not always the case.
In order to identify the exact TPL file of a model, you have to open BIN file in a hex editor, then look at offset 0x24 for the number of model textures(total number of diffuse, opacity and bump/specular map textures).
Then you have to examine extracted TPL files one by one(first examine nearest TPL files) and look for the one with the same number of textures as model BIN file.
To do so, just open TPL files in a hex editor and look for offset 0x4(total number of texture indices stored in TPL file).
After finding a match, just open BIN_Extract.bat file in a text editor and change the name of default(chris) *.bin and *.tpl files inside it and save. Do not forget to put both model BIN and TPL files in the same folder as RE4_UHD_Bin_Extractor.exe.
Double click on BIN_Extract.bat and the model will be exported to SMD and OBJ/MTL formats.
Now it can be easily viewed and/or modified using any 3D application.
For example the contents of BIN_Extract.bat file can be:
RE4_UHD_Bin_Extractor.exe pl00_000.bin pl00_0001.tpl -o
Importing a Custom Model
1- Follow above instructions to extract the model you want to replace.
2- Find and extract model's texture package(according to the above instructions) and remember the default number of textures in that package.
3- Convert all textures of your desired model according to the rules mentioned in "A brief on textures" section.
4- Open extracted model in your favorite 3D application and adjust the size of your desired model(the one you want to import into RE4 UHD) to match the size of original model, rig/skin your custom model and then export it to .SMD format.
Since SMD format can not support bump/specular/opacity(transparency) maps, I used an extra MTL file for that type of textures. Thus if you like to have any kind of specular/bump and/or opacity(transparency) maps, first assign ALL of them to your model and then export the model to OBJ/MTL format in order to get the required material MTL file.
5- Copy exported SMD and MTL files in addition to all model textures to the same folder as RE4_UHD_Bin_Generator.exe tool.
6- Open BIN_Generate.bat file in a text editor and type/change the following command in it :
RE4_UHD_BIN_Generator.exe exported_model_smd_file_name.smd exported_model_mtl_file_name.mtl texture_package_number texture_package_default_texture_count
For example you can use this command : RE4_UHD_BIN_Generator.exe chris.smd chris.mtl 01000008 20
Then save and double click on BIN_Generate.bat. After that, the tool automatically generates 3 output files for you.
Two of them are model's BIN and TPL files that must be copied to UDAS archive folder and replaced with their counterparts.
The third output is a TCS file that contains the name and actual number/index of model textures in texture pack.
You must change the name of model textures to the numbers given in front of their names.
For example if output TCS file contains such a statement "file 26 = pl00Harnes_BM.dds" Then you have to change the name of pl00Harnes_BM.dds texture to 0026.dds and so on.
EDIT :
After generating output BIN file, to make sure the number of vertex bone/weight palette entries used by the model does not exceed the maximum allowable limit, open output BIN file in a hex editor and look for a single byte at offset 0x18 and a word at offset 0x2A.
These two numbers show the number of vertex bone/weight palette entries used by the model. After all these years I still can not figure out why they used another 2 byte copy of some thing that can not exceed a single byte.
Anyway, if these two numbers were not the same(model used more than 255 vertex bone/weight palette entries), unfortunately the game will crash. In this situation you have no choice but to re rig/skin your model to a fewer number of vertex bone/weight palette entries and re check it again and again...
7- After renaming all model texture according to the output TCS file, copy all of the renamed textures to the original texture package's folder and use YZ2tool.exe to repack a new texture pack.
To do so, just double click on repack-all.bat file and you're done.
8- Rename output BIN and TPL files to their counterparts you want to replace. Copy the two renamed files to UDAS archive's folder and replace them with their original counterparts.
For example you can rename chris.bin to pl08_000.bin and chris.mtl to pl08_001.tpl and then copy them to UDAS archive's folder and replace them with their counterparts.
9- It's time to repack a new UDAS archive file. To do so, simply double click on UDAS_Generate.bat file.
10- You're almost done. Just make a backup of your original image pack and UDAS files and then copy their new counterparts to their corresponding folders.
Said, copy new UDAS archive file to "Em" folder and the new image pack file to "ImagePack"/"ImagePackHD" folders of RE4 UHD.
11- Congratulations . you're done importing your first custom model into RE4 UHD. Play as your first custom character and enjoy .
Just do not forget to give me credits whenever you used this toolset and please let me know if you intend to re upload it somewhere else.
Best wishes my dear friends and Enjoy the outcome of my hard work .
IMPORTANT EDIT : If you experienced some crashes or got corrupted files then run the tools in compatibility mode with windows XP service pack 3.
UPDATE : You can get the latest version of EVD Tool HERE