Skip to content

Commit

Permalink
model import: embedded image will use full file data hash as resource…
Browse files Browse the repository at this point in the history
… name to avoid collisions between multiple models
  • Loading branch information
turanszkij committed Feb 2, 2025
1 parent c012833 commit a2ceb2c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
5 changes: 1 addition & 4 deletions Editor/ModelImporter_FBX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,7 @@ void ImportModel_FBX(const std::string& filename, wi::scene::Scene& scene)
if (filename.empty())
{
// Force some image resource name:
do {
filename.clear();
filename += "fbximport_" + std::to_string(wi::random::GetRandom(std::numeric_limits<uint32_t>::max())) + ".png";
} while (wi::resourcemanager::Contains(filename)); // this is to avoid overwriting an existing imported image
filename = "fbximport_" + std::to_string(wi::helper::HashByteData((const uint8_t*)texture->content.data, texture->content.size)) + ".png";
}

auto resource = wi::resourcemanager::Load(
Expand Down
7 changes: 1 addition & 6 deletions Editor/ModelImporter_GLTF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,7 @@ namespace tinygltf
if (image->uri.empty())
{
// Force some image resource name:
std::string ss;
do {
ss.clear();
ss += "gltfimport_" + std::to_string(wi::random::GetRandom(std::numeric_limits<uint32_t>::max())) + ".png";
} while (wi::resourcemanager::Contains(ss)); // this is to avoid overwriting an existing imported image
image->uri = ss;
image->uri = "gltfimport_" + std::to_string(wi::helper::HashByteData(bytes, size)) + ".png";
}

auto resource = wi::resourcemanager::Load(
Expand Down
10 changes: 10 additions & 0 deletions WickedEngine/wiHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1612,4 +1612,14 @@ namespace wi::helper
res = ZSTD_decompress(dst_data.data(), dst_data.size(), src_data, src_size);
return ZSTD_isError(res) == 0;
}

size_t HashByteData(const uint8_t* data, size_t size)
{
size_t hash = 0;
for (size_t i = 0; i < size; ++i)
{
hash_combine(hash, data[i]);
}
return hash;
}
}
3 changes: 3 additions & 0 deletions WickedEngine/wiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,7 @@ namespace wi::helper

// Lossless decompression of byte array that was compressed with wi::helper::Compress()
bool Decompress(const uint8_t* src_data, size_t src_size, wi::vector<uint8_t>& dst_data);

// Hash the contents of a file:
size_t HashByteData(const uint8_t* data, size_t size);
};

0 comments on commit a2ceb2c

Please sign in to comment.