Skip to content

Commit e759e60

Browse files
authored
Merge pull request #1066 from zeux/gltf-fix
gltfpack: Fix corner cases in texture coordinate processing
2 parents 441ff30 + f5c3dc2 commit e759e60

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

gltf/gltfpack.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,23 @@ struct hash<std::pair<uint64_t, uint64_t> >
350350
};
351351
} // namespace std
352352

353+
struct PrimitiveCacheEntry
354+
{
355+
size_t offset;
356+
size_t size;
357+
QuantizationTexture qt;
358+
};
359+
360+
static bool sameQuantization(const QuantizationTexture& lhs, const QuantizationTexture& rhs, const Settings& settings)
361+
{
362+
if (!settings.quantize || settings.tex_float)
363+
return true;
364+
365+
return lhs.offset[0] == rhs.offset[0] && lhs.offset[1] == rhs.offset[1] &&
366+
lhs.scale[0] == rhs.scale[0] && lhs.scale[1] == rhs.scale[1] &&
367+
lhs.bits == rhs.bits && lhs.normalized == rhs.normalized;
368+
}
369+
353370
static size_t process(cgltf_data* data, const char* input_path, const char* output_path, const char* report_path, std::vector<Mesh>& meshes, std::vector<Animation>& animations, const Settings& settings, std::string& json, std::string& bin, std::string& fallback, size_t& fallback_size, const char* meshopt_ext)
354371
{
355372
if (settings.verbose)
@@ -582,7 +599,7 @@ static size_t process(cgltf_data* data, const char* input_path, const char* outp
582599
ext_texture_transform = ext_texture_transform || mi.uses_texture_transform;
583600
}
584601

585-
std::unordered_map<std::pair<uint64_t, uint64_t>, std::pair<size_t, size_t> > primitive_cache;
602+
std::unordered_map<std::pair<uint64_t, uint64_t>, PrimitiveCacheEntry> primitive_cache;
586603

587604
for (size_t i = 0; i < meshes.size(); ++i)
588605
{
@@ -614,18 +631,19 @@ static size_t process(cgltf_data* data, const char* input_path, const char* outp
614631

615632
if (prim.geometry_duplicate)
616633
{
617-
std::pair<size_t, size_t>& primitive_json = primitive_cache[std::make_pair(prim.geometry_hash[0], prim.geometry_hash[1])];
634+
PrimitiveCacheEntry& entry = primitive_cache[std::make_pair(prim.geometry_hash[0], prim.geometry_hash[1])];
618635

619-
if (primitive_json.second)
636+
if (entry.size && sameQuantization(entry.qt, qt, settings))
620637
{
621638
// reuse previously written accessors
622-
json_meshes.append(json_meshes, primitive_json.first, primitive_json.second);
639+
json_meshes.append(json_meshes, entry.offset, entry.size);
623640
}
624641
else
625642
{
626-
primitive_json.first = json_meshes.size();
643+
entry.offset = json_meshes.size();
627644
writeMeshGeometry(json_meshes, views, json_accessors, accr_offset, prim, qp, qt, settings);
628-
primitive_json.second = json_meshes.size() - primitive_json.first;
645+
entry.size = json_meshes.size() - entry.offset;
646+
entry.qt = qt;
629647
}
630648
}
631649
else

gltf/write.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "gltfpack.h"
33

44
#include <float.h>
5+
#include <math.h>
56
#include <stdio.h>
67
#include <stdlib.h>
78
#include <string.h>
@@ -195,8 +196,11 @@ static void writeTextureInfo(std::string& json, const cgltf_data* data, const cg
195196

196197
if (qt)
197198
{
198-
transform.offset[0] += qt->offset[0];
199-
transform.offset[1] += qt->offset[1];
199+
float rcos = cosf(transform.rotation), rsin = sinf(transform.rotation);
200+
float ox = transform.scale[0] * qt->offset[0], oy = transform.scale[1] * qt->offset[1];
201+
202+
transform.offset[0] += rcos * ox + rsin * oy;
203+
transform.offset[1] += -rsin * ox + rcos * oy;
200204
transform.scale[0] *= qt->scale[0] / float((1 << qt->bits) - 1) * (qt->normalized ? 65535.f : 1.f);
201205
transform.scale[1] *= qt->scale[1] / float((1 << qt->bits) - 1) * (qt->normalized ? 65535.f : 1.f);
202206
needs_transform = true;

0 commit comments

Comments
 (0)