First draft of the makeMaterials command

This commit is contained in:
Robert Beckebans 2024-05-18 16:16:41 +02:00
parent 5561411f65
commit 133c63f7ec
2 changed files with 80 additions and 1 deletions

View file

@ -263,7 +263,7 @@ ID_INLINE void idCmdSystem::ArgCompletion_SoundName( const idCmdArgs& args, void
ID_INLINE void idCmdSystem::ArgCompletion_ImageName( const idCmdArgs& args, void( *callback )( const char* s ) )
{
cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".dds", ".jpg", ".pcx", NULL );
cmdSystem->ArgCompletion_FolderExtension( args, callback, "/", false, ".tga", ".png", ".jpg", ".exr", NULL );
}
ID_INLINE void idCmdSystem::ArgCompletion_VideoName( const idCmdArgs& args, void( *callback )( const char* s ) )

View file

@ -3863,4 +3863,83 @@ void idMaterial::ExportJSON( idFile* file, bool lastEntry ) const
file->Printf( "\t\t},\n" );
}
}
// RB: completely rewritten from IcedTech1 and adjusted to generate PBR materials for typical asset store conventions
// this also supports file suffices used by Blender's Node Wranger addon
CONSOLE_COMMAND_SHIP( makeMaterials, "Make .mtr file from a models or textures folder using PBR conventions", idCmdSystem::ArgCompletion_ImageName )
{
if( args.Argc() < 2 )
{
common->Warning( "Usage: makeMaterials <folder>\n" );
return;
}
idStr folderName = args.Argv( 1 );
idFileList* files = fileSystem->ListFilesTree( folderName, ".png|.tga|.jpg|.exr" );
idStr mtrBuffer;
mtrBuffer += va( "// generated by %s\n\n", ENGINE_VERSION );
idStrList list = files->GetList();
for( int i = 0; i < files->GetNumFiles(); i++ )
{
idStr imageName = list[i];
if( idStr::FindText( imageName, "_orig", false ) != -1 )
{
continue;
}
if( idStr::FindText( imageName, "_BaseColor", false ) != -1 || idStr::FindText( imageName, "_Color", false ) != -1 )
{
imageName = imageName.StripFileExtension();
idStr baseName = imageName;
baseName.StripTrailing( "_BaseColor" );
baseName.StripTrailing( "_Color" );
//mtrBuffer += va( "%s/%s\n", folderName, imageName.c_str() );
mtrBuffer += baseName.c_str();
mtrBuffer += "\n{\n";
//mtrBuffer += va( "\tqer_editorimage %s/%s_d.%s\n", folderName, args.Argv( 2 ), imagepath.c_str() );
// test opacity / transparency map
ID_TIME_T timestamp;
idStr opacityName = baseName + "_Opacity";
R_LoadImage( opacityName, NULL, NULL, NULL, &timestamp, true, NULL );
if( timestamp != FILE_NOT_FOUND_TIMESTAMP )
{
// TODO load opacity map and store values in the alpha channel of the base color image
mtrBuffer += "\t{\n";
mtrBuffer += "\t\tblend basecolormap\n";
mtrBuffer += va( "\t\tmap %s\n", imageName.c_str() );
mtrBuffer += "\t\talphaTest 0.5\n";
mtrBuffer += "\t}\n";
}
else
{
mtrBuffer += va( "\tbasecolormap %s\n", imageName.c_str() );
}
//mtrBuffer += va( "\tbumpmap addnormals ( textures/%s/%s_local.%s, heightmap ( textures/%s/%s_h.%s, 4 ) )\n", args.Argv( 1 ), args.Argv( 2 ), imagepath.c_str() );
//mtrBuffer += va( "\tspecularmap textures/%s/%s_spec.%s\n", args.Argv( 1 ), args.Argv( 2 ), imagepath.c_str() );
mtrBuffer += va( "}\n\n" );
}
}
fileSystem->FreeFileList( files );
folderName.ReplaceChar( '/', '_' );
idStr mtrName = "materials/";
mtrName += folderName;
mtrName.StripTrailing( '_' );
mtrName.DefaultFileExtension( ".mtr" );
fileSystem->WriteFile( mtrName.c_str(), mtrBuffer.c_str(), mtrBuffer.Length(), "fs_basepath" );
}
// RB end