Skip to content

Commit a2ceb2c

Browse files
committed
model import: embedded image will use full file data hash as resource name to avoid collisions between multiple models
1 parent c012833 commit a2ceb2c

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

Editor/ModelImporter_FBX.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,7 @@ void ImportModel_FBX(const std::string& filename, wi::scene::Scene& scene)
112112
if (filename.empty())
113113
{
114114
// Force some image resource name:
115-
do {
116-
filename.clear();
117-
filename += "fbximport_" + std::to_string(wi::random::GetRandom(std::numeric_limits<uint32_t>::max())) + ".png";
118-
} while (wi::resourcemanager::Contains(filename)); // this is to avoid overwriting an existing imported image
115+
filename = "fbximport_" + std::to_string(wi::helper::HashByteData((const uint8_t*)texture->content.data, texture->content.size)) + ".png";
119116
}
120117

121118
auto resource = wi::resourcemanager::Load(

Editor/ModelImporter_GLTF.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ namespace tinygltf
9090
if (image->uri.empty())
9191
{
9292
// Force some image resource name:
93-
std::string ss;
94-
do {
95-
ss.clear();
96-
ss += "gltfimport_" + std::to_string(wi::random::GetRandom(std::numeric_limits<uint32_t>::max())) + ".png";
97-
} while (wi::resourcemanager::Contains(ss)); // this is to avoid overwriting an existing imported image
98-
image->uri = ss;
93+
image->uri = "gltfimport_" + std::to_string(wi::helper::HashByteData(bytes, size)) + ".png";
9994
}
10095

10196
auto resource = wi::resourcemanager::Load(

WickedEngine/wiHelper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,4 +1612,14 @@ namespace wi::helper
16121612
res = ZSTD_decompress(dst_data.data(), dst_data.size(), src_data, src_size);
16131613
return ZSTD_isError(res) == 0;
16141614
}
1615+
1616+
size_t HashByteData(const uint8_t* data, size_t size)
1617+
{
1618+
size_t hash = 0;
1619+
for (size_t i = 0; i < size; ++i)
1620+
{
1621+
hash_combine(hash, data[i]);
1622+
}
1623+
return hash;
1624+
}
16151625
}

WickedEngine/wiHelper.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,7 @@ namespace wi::helper
190190

191191
// Lossless decompression of byte array that was compressed with wi::helper::Compress()
192192
bool Decompress(const uint8_t* src_data, size_t src_size, wi::vector<uint8_t>& dst_data);
193+
194+
// Hash the contents of a file:
195+
size_t HashByteData(const uint8_t* data, size_t size);
193196
};

0 commit comments

Comments
 (0)