Skip to content

Commit

Permalink
gltfpack: Introduce optional floating-point normal quantization
Browse files Browse the repository at this point in the history
Usually normalized integer storage is optimal for normals; however,
morph target deltas require a [-2, 2] storage range. During quantization
we clamp them to [-1, 1] which ends up usually working fine, however
there are some use cases where this doesn't work well when the normal
deformation is extreme.

This can be solved by scaling normals by 0.5, so we'll use [-0.5, 0.5]
range for base normal and [-1, 1] range for normal deltas. This usually
works fine with any glTF renderers as they normalize the normal (which
is more or less required for handling non-uniform scale transforms).

However, the validator treats files like this as invalid. Validator
doesn't process files with -c/cc so technically we could fix this when
compression is enabled, but that would result in an odd mismatch where
compression *improves* quality instead of degrading it...

For now we introduce an option to use floating point normals, quantized
using reduced mantissa and stored, when compression is enabled, using
exponential filter. Similarly to -vtf, this isn't normally the
recommended mode but there might be cases where it's a desireable
tradeoff.
  • Loading branch information
zeux committed Nov 16, 2023
1 parent d57567f commit e3aab8b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,10 @@ int main(int argc, char** argv)
{
settings.tex_float = true;
}
else if (strcmp(arg, "-vnf") == 0)
{
settings.nrm_float = true;
}
else if (strcmp(arg, "-at") == 0 && i + 1 < argc && isdigit(argv[i + 1][0]))
{
settings.trn_bits = clamp(atoi(argv[++i]), 1, 24);
Expand Down Expand Up @@ -1581,9 +1585,9 @@ int main(int argc, char** argv)
return 1;
}

if (settings.fallback && (settings.pos_float || settings.tex_float))
if (settings.fallback && (settings.pos_float || settings.tex_float || settings.nrm_float))
{
fprintf(stderr, "Option -cf can not be used together with -vpf or -tpf\n");
fprintf(stderr, "Option -cf can not be used together with -vpf, -vtf or -vnf\n");
return 1;
}

Expand Down
1 change: 1 addition & 0 deletions gltf/gltfpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ struct Settings
bool pos_normalized;
bool pos_float;
bool tex_float;
bool nrm_float;

int trn_bits;
int rot_bits;
Expand Down
4 changes: 4 additions & 0 deletions gltf/stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ StreamFormat writeVertexStream(std::string& bin, const Stream& stream, const Qua
if (!settings.quantize)
return writeVertexStreamRaw(bin, stream, cgltf_type_vec3, 3);

// expand the encoded range to ensure it covers [0..1) interval
if (settings.nrm_float)
return writeVertexStreamFloat(bin, stream, cgltf_type_vec3, 3, settings, settings.nrm_bits, /* min_exp= */ 0);

bool oct = settings.compressmore && stream.target == 0;
int bits = settings.nrm_bits;

Expand Down

0 comments on commit e3aab8b

Please sign in to comment.