Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Shader JSON export
Browse files Browse the repository at this point in the history
* Shaders export as JSON
* Shader format modified for ease of use
  • Loading branch information
TiredHobgoblin authored Jun 25, 2021
1 parent a8b1ee8 commit b9aa184
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ std::unordered_map<uint32_t, std::string> channelNames =

std::vector<std::string> dataNames =
{
"Detail Diffuse Transform",
"Detail Normal Transform",
"\"detail_diffuse_transform\"",
"\"detail_normal_transform\"",
"\"spec_aa_transform\"",
"Primary Color",
"\"primary_albedo_tint\"",
"\"primary_emissive_tint_color_and_intensity_bias\"",
"\"primary_material_params\"",
"\"primary_material_advanced_params\"",
"Primary Roughness Remap",
"Primary Wear Color",
"Primary Wear Remap",
"Primary Worn Roughness Remap",
"\"primary_roughness_remap\"",
"\"primary_worn_albedo_tint\"",
"\"primary_wear_remap\"",
"\"primary_worn_roughness_remap\"",
"\"primary_worn_material_parameters\"",
"Secondary Color",
"\"secondary_albedo_tint\"",
"\"secondary_emissive_tint_color_and_intensity_bias\"",
"\"secondary_material_params\"",
"\"secondary_material_advanced_params\"",
"Secondary Roughness Remap",
"Secondary Wear Color",
"Secondary Wear Remap",
"Secondary Worn Roughness Remap",
"\"secondary_roughness_remap\"",
"\"secondary_worn_albedo_tint\"",
"\"secondary_wear_remap\"",
"\"secondary_worn_roughness_remap\"",
"\"secondary_worn_material_parameters\"",
};

Expand Down Expand Up @@ -180,6 +180,7 @@ void getAPIShader(uint32_t apiHash, std::string outputPath, std::string packages
std::string dyeFileHash;
File* finalDyeFile = nullptr;
std::string channelName;
std::string channelNameHash;
// For each pair, find the channel hash to pair it with a name + find the dye file

std::unordered_map<std::string, std::unordered_map<std::string, std::vector<float>>> defaultDyes;
Expand All @@ -200,6 +201,7 @@ void getAPIShader(uint32_t apiHash, std::string outputPath, std::string packages
// Get channel name
memcpy((char*)&channelHash, channelTable->data + 0x30 + 4 * it.first, 4);
channelName = channelNames[channelHash];
channelNameHash = std::to_string(channelHash);

// Get dye file
memcpy((char*)&dyeManifestHash, dyeManifestTable->data + 0x30 + 8 * it.second + 4, 4);
Expand Down Expand Up @@ -292,17 +294,17 @@ void getAPIShader(uint32_t apiHash, std::string outputPath, std::string packages

if (q == 0)
{
defaultDyes[channelName] = dyeData;
defaultDyes[channelNameHash] = dyeData;
texData["Diffuse"] = diffuseName;
texData["Normal"] = normalName;
defaultTextures[channelName] = texData;
defaultTextures[channelNameHash] = texData;
}
else
{
customDyes[channelName] = dyeData;
customDyes[channelNameHash] = dyeData;
texData["Diffuse"] = diffuseName;
texData["Normal"] = normalName;
customTextures[channelName] = texData;
customTextures[channelNameHash] = texData;
}
}
}
Expand All @@ -316,29 +318,38 @@ void getAPIShader(uint32_t apiHash, std::string outputPath, std::string packages

void writeShader(std::unordered_map<std::string, std::unordered_map<std::string, std::vector<float>>> dyes, std::unordered_map<std::string, std::unordered_map<std::string, std::string>> textures, bool bCustom, std::string outputPath)
{
std::string stringFactoryShader = "";
if (!bCustom) stringFactoryShader += "Default dyes:\n";
else stringFactoryShader += "Custom dyes:\n";
std::string stringFactoryShader = "{\n";
if (!bCustom) stringFactoryShader += " \"custom_dyes\": [],\n \"locked_dyes\": [],\n \"default_dyes\": [\n";
else stringFactoryShader += " \"default_dyes\": [],\n \"locked_dyes\": [],\n \"custom_dyes\": [\n";
std::string propertiesString = "";
for (auto& it : dyes)
{
stringFactoryShader += " " + it.first + ":\n";
if (it.first.find("Cloth", 0) != std::string::npos) stringFactoryShader += " Is cloth: True\n";
else stringFactoryShader += " Is cloth: False\n";
stringFactoryShader += " Properties:\n";
//propertiesString += " " + it.first + ":\n";
propertiesString += " {\n";
propertiesString += " \"investment_hash\": " + it.first + ",\n";
if (it.first.find("Cloth", 0) != std::string::npos) propertiesString += " \"cloth\": true,\n";
else propertiesString += " \"cloth\": false,\n";
propertiesString += " \"material_properties\": {\n";
std::string valuesString = "";
for (auto& it2 : it.second)
{
if (it.first.find("Diffuse", 0) != std::string::npos) break;
if (it.first.find("diffuse", 0) != std::string::npos) break;
std::string floatString = "[";
for (auto& flt : it2.second) floatString += std::to_string(flt) + ", ";
stringFactoryShader += " " + it2.first + ": " + floatString.substr(0, floatString.size()-2) + "]\n";
//stringFactoryShader += " " + it2.first + ": " + floatString.substr(0, floatString.size()-2) + "],\n";
valuesString += " " + it2.first + ": " + floatString.substr(0, floatString.size()-2) + "],\n";
}
stringFactoryShader += " Diffuse: " + textures[it.first]["Diffuse"] + "\n";
stringFactoryShader += " Normal: " + textures[it.first]["Normal"] + "\n";
propertiesString += valuesString.substr(0, valuesString.size()-2) + "\n },\n";
propertiesString += " \"textures\": {\n";
propertiesString += " \"diffuse\": {\n \"name\": \"" + textures[it.first]["Diffuse"] + "\"\n },\n";
propertiesString += " \"normal\": {\n \"name\": \"" + textures[it.first]["Normal"] + "\"\n }\n";
propertiesString += " }\n },\n";
}
stringFactoryShader += "\n";
stringFactoryShader += propertiesString.substr(0, propertiesString.size()-2) + "\n ]\n}";
//stringFactoryShader += "\n";

FILE* shaderFile;
std::string path = outputPath + "/shader.txt";
std::string path = outputPath + "/shader.json";
fopen_s(&shaderFile, path.c_str(), "w");
fwrite(stringFactoryShader.c_str(), stringFactoryShader.size(), 1, shaderFile);
fclose(shaderFile);
Expand Down

0 comments on commit b9aa184

Please sign in to comment.