This commit is contained in:
owilliamailliwo 2019-07-19 05:04:46 +00:00 committed by GitHub
commit de47c743fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 340 additions and 52 deletions

View File

@ -185,12 +185,27 @@ int main(int argc, char* argv[]) {
->type_size(-1) ->type_size(-1)
->type_name("(position|normal|tangent|binormial|color|uv0|uv1|auto)"); ->type_name("(position|normal|tangent|binormial|color|uv0|uv1|auto)");
app.add_flag( app.add_option(
"-d,--draco", gltfOptions.draco.enabled, "Apply Draco mesh compression to geometries.") "-d,--draco",
->group("Draco"); [&](std::vector<std::string> choices) -> bool {
for (const std::string choice : choices) {
if (choice == "mesh") {
gltfOptions.draco.enabledMesh = true;
} else if (choice == "animation") {
gltfOptions.draco.enabledAnimation = true;
} else {
fmt::printf("Unknown -d,--draco: %s\n", choice);
throw CLI::RuntimeError(1);
}
}
return true;
},
"Apply Draco mesh|animation compression to geometries|animation data.")
->type_size(-1)
->type_name("(mesh|animation)");
app.add_option( app.add_option(
"--draco-compression-level", "--draco-mesh-compression-level",
gltfOptions.draco.compressionLevel, gltfOptions.draco.compressionLevel,
"The compression level to tune Draco to.", "The compression level to tune Draco to.",
true) true)
@ -202,7 +217,7 @@ int main(int argc, char* argv[]) {
gltfOptions.draco.quantBitsPosition, gltfOptions.draco.quantBitsPosition,
"How many bits to quantize position to.", "How many bits to quantize position to.",
true) true)
->check(CLI::Range(1, 32)) ->check(CLI::Range(1, 30))
->group("Draco"); ->group("Draco");
app.add_option( app.add_option(
@ -210,7 +225,7 @@ int main(int argc, char* argv[]) {
gltfOptions.draco.quantBitsTexCoord, gltfOptions.draco.quantBitsTexCoord,
"How many bits to quantize UV coordinates to.", "How many bits to quantize UV coordinates to.",
true) true)
->check(CLI::Range(1, 32)) ->check(CLI::Range(1, 30))
->group("Draco"); ->group("Draco");
app.add_option( app.add_option(
@ -218,7 +233,7 @@ int main(int argc, char* argv[]) {
gltfOptions.draco.quantBitsNormal, gltfOptions.draco.quantBitsNormal,
"How many bits to quantize nornals to.", "How many bits to quantize nornals to.",
true) true)
->check(CLI::Range(1, 32)) ->check(CLI::Range(1, 30))
->group("Draco"); ->group("Draco");
app.add_option( app.add_option(
@ -226,7 +241,7 @@ int main(int argc, char* argv[]) {
gltfOptions.draco.quantBitsColor, gltfOptions.draco.quantBitsColor,
"How many bits to quantize colors to.", "How many bits to quantize colors to.",
true) true)
->check(CLI::Range(1, 32)) ->check(CLI::Range(1, 30))
->group("Draco"); ->group("Draco");
app.add_option( app.add_option(
@ -234,9 +249,33 @@ int main(int argc, char* argv[]) {
gltfOptions.draco.quantBitsGeneric, gltfOptions.draco.quantBitsGeneric,
"How many bits to quantize all other vertex attributes to.", "How many bits to quantize all other vertex attributes to.",
true) true)
->check(CLI::Range(1, 32)) ->check(CLI::Range(1, 30))
->group("Draco"); ->group("Draco");
app.add_option(
"--draco-animation-compression-level",
gltfOptions.draco.animationCompressionLevel,
"The animation compression level to tune Draco to.",
true)
->check(CLI::Range(0, 10))
->group("Draco");
app.add_option(
"--draco-bits-for-timestamp",
gltfOptions.draco.quantBitsTimestamp,
"How many bits to quantize timestamp to.",
true)
->check(CLI::Range(1, 30))
->group("Draco");
app.add_option(
"--draco-bits-for-keyframe",
gltfOptions.draco.quantBitsKeyframe,
"How many bits to quantize keyframe to.",
true)
->check(CLI::Range(1, 30))
->group("Draco");
CLI11_PARSE(app, argc, argv); CLI11_PARSE(app, argc, argv);
bool do_flip_u = false; bool do_flip_u = false;
@ -249,11 +288,11 @@ int main(int argc, char* argv[]) {
std::vector<std::function<Vec2f(Vec2f)>> texturesTransforms; std::vector<std::function<Vec2f(Vec2f)>> texturesTransforms;
if (do_flip_u || do_flip_v) { if (do_flip_u || do_flip_v) {
if (do_flip_u && do_flip_v) { if (do_flip_u && do_flip_v) {
texturesTransforms.emplace_back([](Vec2f uv) { return Vec2f(1.0 - uv[0], 1.0 - uv[1]); }); texturesTransforms.emplace_back([](Vec2f uv) { return Vec2f(1.0f - uv[0], 1.0f - uv[1]); });
} else if (do_flip_u) { } else if (do_flip_u) {
texturesTransforms.emplace_back([](Vec2f uv) { return Vec2f(1.0 - uv[0], uv[1]); }); texturesTransforms.emplace_back([](Vec2f uv) { return Vec2f(1.0f - uv[0], uv[1]); });
} else { } else {
texturesTransforms.emplace_back([](Vec2f uv) { return Vec2f(uv[0], 1.0 - uv[1]); }); texturesTransforms.emplace_back([](Vec2f uv) { return Vec2f(uv[0], 1.0f - uv[1]); });
} }
} }
if (verboseOutput) { if (verboseOutput) {
@ -366,7 +405,7 @@ int main(int argc, char* argv[]) {
if (data_render_model->binary->empty() == false) { if (data_render_model->binary->empty() == false) {
const unsigned char* binaryData = &(*data_render_model->binary)[0]; const unsigned char* binaryData = &(*data_render_model->binary)[0];
unsigned long binarySize = data_render_model->binary->size(); size_t binarySize = data_render_model->binary->size();
if (fwrite(binaryData, binarySize, 1, fp) != 1) { if (fwrite(binaryData, binarySize, 1, fp) != 1) {
fmt::fprintf( fmt::fprintf(
stderr, "ERROR: Failed to write %lu bytes to file '%s'.\n", binarySize, binaryPath); stderr, "ERROR: Failed to write %lu bytes to file '%s'.\n", binarySize, binaryPath);

View File

@ -90,15 +90,26 @@ struct GltfOptions {
/** If non-binary, whether to inline all resources, for a single (large) .glTF file. */ /** If non-binary, whether to inline all resources, for a single (large) .glTF file. */
bool embedResources{false}; bool embedResources{false};
/** Whether and how to use KHR_draco_mesh_compression to minimize static geometry size. */ /** Whether and how to use KHR_draco_mesh_compression & Draco_animation_compression to minimize static geometry size. */
struct { struct {
bool enabled = false; bool enabledMesh = false;
int compressionLevel = 7; int compressionLevel = 7;
int quantBitsPosition = 14; int quantBitsPosition = 14;
int quantBitsTexCoord = 10; int quantBitsTexCoord = 10;
int quantBitsNormal = 10; int quantBitsNormal = 10;
int quantBitsColor = 8; int quantBitsColor = 8;
int quantBitsGeneric = 8; int quantBitsGeneric = 8;
// int compressionLevel = -1; // 7
// int quantBitsPosition = -1; // 14
// int quantBitsTexCoord = -1; // 10
// int quantBitsNormal = -1; // 10
// int quantBitsColor = -1; // 8
// int quantBitsGeneric = -1; // 8
bool enabledAnimation = false;
int animationCompressionLevel = -1; // 5
int quantBitsTimestamp = -1;
int quantBitsKeyframe = -1;
} draco; } draco;
/** Whether to include FBX User Properties as 'extras' metadata in glTF nodes. */ /** Whether to include FBX User Properties as 'extras' metadata in glTF nodes. */

View File

@ -122,6 +122,47 @@ class GltfModel {
return accessor; return accessor;
}; };
template <class T>
std::shared_ptr<AccessorData> AddTimestampsToAnimation(
BufferData& buffer,
AnimationData& animationData,
const std::vector<T>& timestamps,
const GLType& glType,
const draco::DataType dracoComponentType) {
std::shared_ptr<AccessorData> accessor;
if (dracoComponentType != draco::DT_INVALID && animationData.dracoKeyframeAnimation != nullptr) {
accessor = accessors.hold(new AccessorData(glType));
accessor->count = to_uint32(timestamps.size());
animationData.AddDracoTimestamps(*accessor, timestamps);
} else {
accessor = AddAccessorAndView(buffer, glType, timestamps);
animationData.AddTimestamps(*accessor);
}
return accessor;
};
template <class T>
std::shared_ptr<AccessorData> AddChannelToAnimation(
BufferData& buffer,
AnimationData& animationData,
const NodeData& nDat,
const ChannelDefinition<T>& channelDef) {
std::shared_ptr<AccessorData> accessor;
if (channelDef.dracoComponentType != draco::DT_INVALID && animationData.dracoKeyframeAnimation != nullptr) {
accessor = accessors.hold(new AccessorData(channelDef.glType));
accessor->count = to_uint32(channelDef.channelData.size());
animationData.AddDracoNodeChannel(
nDat,
*accessor,
channelDef.path,
channelDef);
} else {
accessor = AddAccessorAndView(buffer, channelDef.glType, channelDef.channelData);
animationData.AddNodeChannel(nDat, *accessor, channelDef.path);
}
return accessor;
};
template <class T> template <class T>
void serializeHolder(json& glTFJson, std::string key, const Holder<T> holder) { void serializeHolder(json& glTFJson, std::string key, const Holder<T> holder) {
if (!holder.ptrs.empty()) { if (!holder.ptrs.empty()) {

View File

@ -149,6 +149,8 @@ ModelData* Raw2Gltf(
nodesById.insert(std::make_pair(node.id, nodeData)); nodesById.insert(std::make_pair(node.id, nodeData));
} }
std::vector<std::shared_ptr<AccessorData>> accessors;
// //
// animations // animations
// //
@ -162,11 +164,22 @@ ModelData* Raw2Gltf(
continue; continue;
} }
auto accessor = gltf->AddAccessorAndView(buffer, GLT_FLOAT, animation.times); AnimationData* animationData = nullptr;
accessor->min = {*std::min_element(std::begin(animation.times), std::end(animation.times))}; if (options.draco.enabledAnimation) {
accessor->max = {*std::max_element(std::begin(animation.times), std::end(animation.times))}; // create Draco KeyframeAnimation
auto dracoKeyframeAnimation(std::make_shared<draco::KeyframeAnimation>());
animationData = new AnimationData(animation.name, dracoKeyframeAnimation);
} else {
animationData = new AnimationData(animation.name);
}
AnimationData& aDat = *gltf->animations.hold(animationData);
const auto timestampsAccessor =
gltf->AddTimestampsToAnimation<float>(buffer, aDat, animation.times, GLT_FLOAT, draco::DT_FLOAT32);
timestampsAccessor->min = { *std::min_element(std::begin(animation.times), std::end(animation.times)) };
timestampsAccessor->max = { *std::max_element(std::begin(animation.times), std::end(animation.times)) };
accessors.emplace_back(timestampsAccessor);
AnimationData& aDat = *gltf->animations.hold(new AnimationData(animation.name, *accessor));
if (verboseOutput) { if (verboseOutput) {
fmt::printf( fmt::printf(
"Animation '%s' has %lu channels:\n", "Animation '%s' has %lu channels:\n",
@ -191,26 +204,82 @@ ModelData* Raw2Gltf(
NodeData& nDat = require(nodesById, node.id); NodeData& nDat = require(nodesById, node.id);
if (!channel.translations.empty()) { if (!channel.translations.empty()) {
aDat.AddNodeChannel( const ChannelDefinition<Vec3f> CHANNEL_TRANSLATIONS(
nDat, "translation",
*gltf->AddAccessorAndView(buffer, GLT_VEC3F, channel.translations), channel.translations,
"translation"); GLT_VEC3F,
draco::DT_FLOAT32);
const auto _ =
gltf->AddChannelToAnimation<Vec3f>(buffer, aDat, nDat, CHANNEL_TRANSLATIONS);
accessors.emplace_back(_);
} }
if (!channel.rotations.empty()) { if (!channel.rotations.empty()) {
aDat.AddNodeChannel( const ChannelDefinition<Quatf> CHANNEL_ROTATIONS(
nDat, *gltf->AddAccessorAndView(buffer, GLT_QUATF, channel.rotations), "rotation"); "rotation",
channel.rotations,
GLT_QUATF,
draco::DT_FLOAT32);
const auto _ =
gltf->AddChannelToAnimation<Quatf>(buffer, aDat, nDat, CHANNEL_ROTATIONS);
accessors.emplace_back(_);
} }
if (!channel.scales.empty()) { if (!channel.scales.empty()) {
aDat.AddNodeChannel( const ChannelDefinition<Vec3f> CHANNEL_SCALES(
nDat, *gltf->AddAccessorAndView(buffer, GLT_VEC3F, channel.scales), "scale"); "scale",
channel.scales,
GLT_VEC3F,
draco::DT_FLOAT32);
const auto _ =
gltf->AddChannelToAnimation<Vec3f>(buffer, aDat, nDat, CHANNEL_SCALES);
accessors.emplace_back(_);
} }
if (!channel.weights.empty()) { if (!channel.weights.empty()) {
aDat.AddNodeChannel( const ChannelDefinition<float> CHANNEL_WEIGHTS(
nDat, "weights",
*gltf->AddAccessorAndView(buffer, {CT_FLOAT, 1, "SCALAR"}, channel.weights), channel.weights,
"weights"); { CT_FLOAT, 1, "SCALAR" },
draco::DT_FLOAT32);
const auto _ =
gltf->AddChannelToAnimation<float>(buffer, aDat, nDat, CHANNEL_WEIGHTS);
accessors.emplace_back(_);
} }
} }
if (options.draco.enabledAnimation) {
draco::EncoderOptions encodeOptions = draco::EncoderOptions::CreateDefaultOptions();
if (options.draco.animationCompressionLevel != -1) {
int dracoSpeed = 10 - options.draco.animationCompressionLevel;
int en = dracoSpeed;
int de = dracoSpeed;
encodeOptions.SetSpeed(en, de);
}
if (-1 != options.draco.quantBitsTimestamp) {
// set quantization for timestamps.
encodeOptions.SetAttributeInt(0, "quantization_bits", options.draco.quantBitsTimestamp);
}
if (-1 != options.draco.quantBitsKeyframe) {
// set quantization for keyframes.
for (int i = 1; i <= aDat.dracoKeyframeAnimation->num_animations(); ++i) {
encodeOptions.SetAttributeInt(i, "quantization_bits", options.draco.quantBitsKeyframe);
}
}
draco::EncoderBuffer dracoBuffer;
draco::KeyframeAnimationEncoder encoder;
draco::Status status = encoder.EncodeKeyframeAnimation(*(aDat.dracoKeyframeAnimation), encodeOptions, &dracoBuffer);
assert(status.code() == draco::Status::OK);
auto view = gltf->AddRawBufferView(buffer, dracoBuffer.data(), to_uint32(dracoBuffer.size()));
dracoBuffer.Clear();
for (auto accessor : accessors)
{
accessor->bufferView = view->ix;
accessor->byteOffset = -1;
}
}
accessors.clear();
} }
// //
@ -438,7 +507,7 @@ ModelData* Raw2Gltf(
surfaceModel.GetVertexCount() > 65535); surfaceModel.GetVertexCount() > 65535);
std::shared_ptr<PrimitiveData> primitive; std::shared_ptr<PrimitiveData> primitive;
if (options.draco.enabled) { if (options.draco.enabledMesh) {
size_t triangleCount = surfaceModel.GetTriangleCount(); size_t triangleCount = surfaceModel.GetTriangleCount();
// initialize Draco mesh with vertex index information // initialize Draco mesh with vertex index information
@ -454,10 +523,12 @@ ModelData* Raw2Gltf(
dracoMesh->SetFace(draco::FaceIndex(ii), face); dracoMesh->SetFace(draco::FaceIndex(ii), face);
} }
AccessorData& indexes = std::shared_ptr<AccessorData> indexes =
*gltf->accessors.hold(new AccessorData(useLongIndices ? GLT_UINT : GLT_USHORT)); gltf->accessors.hold(new AccessorData(useLongIndices ? GLT_UINT : GLT_USHORT));
indexes.count = to_uint32(3 * triangleCount); indexes->count = to_uint32(3 * triangleCount);
primitive.reset(new PrimitiveData(indexes, mData, dracoMesh)); primitive.reset(new PrimitiveData(*indexes, mData, dracoMesh));
accessors.emplace_back(indexes);
} else { } else {
const AccessorData& indexes = *gltf->AddAccessorWithView( const AccessorData& indexes = *gltf->AddAccessorWithView(
*gltf->GetAlignedBufferView(buffer, BufferViewData::GL_ELEMENT_ARRAY_BUFFER), *gltf->GetAlignedBufferView(buffer, BufferViewData::GL_ELEMENT_ARRAY_BUFFER),
@ -483,6 +554,7 @@ ModelData* Raw2Gltf(
accessor->min = toStdVec(rawSurface.bounds.min); accessor->min = toStdVec(rawSurface.bounds.min);
accessor->max = toStdVec(rawSurface.bounds.max); accessor->max = toStdVec(rawSurface.bounds.max);
accessors.emplace_back(accessor);
} }
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_NORMAL) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_NORMAL) != 0) {
const AttributeDefinition<Vec3f> ATTR_NORMAL( const AttributeDefinition<Vec3f> ATTR_NORMAL(
@ -492,12 +564,13 @@ ModelData* Raw2Gltf(
draco::GeometryAttribute::NORMAL, draco::GeometryAttribute::NORMAL,
draco::DT_FLOAT32); draco::DT_FLOAT32);
const auto _ = const auto _ =
gltf->AddAttributeToPrimitive<Vec3f>(buffer, surfaceModel, *primitive, ATTR_NORMAL); gltf->AddAttributeToPrimitive<Vec3f>(buffer, surfaceModel, *primitive, ATTR_NORMAL);
} accessors.emplace_back(_);
}
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_TANGENT) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_TANGENT) != 0) {
const AttributeDefinition<Vec4f> ATTR_TANGENT("TANGENT", &RawVertex::tangent, GLT_VEC4F); const AttributeDefinition<Vec4f> ATTR_TANGENT("TANGENT", &RawVertex::tangent, GLT_VEC4F);
const auto _ = gltf->AddAttributeToPrimitive<Vec4f>( const auto _ = gltf->AddAttributeToPrimitive<Vec4f>(buffer, surfaceModel, *primitive, ATTR_TANGENT);
buffer, surfaceModel, *primitive, ATTR_TANGENT); accessors.emplace_back(_);
} }
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_COLOR) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_COLOR) != 0) {
const AttributeDefinition<Vec4f> ATTR_COLOR( const AttributeDefinition<Vec4f> ATTR_COLOR(
@ -508,6 +581,7 @@ ModelData* Raw2Gltf(
draco::DT_FLOAT32); draco::DT_FLOAT32);
const auto _ = const auto _ =
gltf->AddAttributeToPrimitive<Vec4f>(buffer, surfaceModel, *primitive, ATTR_COLOR); gltf->AddAttributeToPrimitive<Vec4f>(buffer, surfaceModel, *primitive, ATTR_COLOR);
accessors.emplace_back(_);
} }
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_UV0) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_UV0) != 0) {
const AttributeDefinition<Vec2f> ATTR_TEXCOORD_0( const AttributeDefinition<Vec2f> ATTR_TEXCOORD_0(
@ -518,6 +592,7 @@ ModelData* Raw2Gltf(
draco::DT_FLOAT32); draco::DT_FLOAT32);
const auto _ = gltf->AddAttributeToPrimitive<Vec2f>( const auto _ = gltf->AddAttributeToPrimitive<Vec2f>(
buffer, surfaceModel, *primitive, ATTR_TEXCOORD_0); buffer, surfaceModel, *primitive, ATTR_TEXCOORD_0);
accessors.emplace_back(_);
} }
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_UV1) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_UV1) != 0) {
const AttributeDefinition<Vec2f> ATTR_TEXCOORD_1( const AttributeDefinition<Vec2f> ATTR_TEXCOORD_1(
@ -528,6 +603,7 @@ ModelData* Raw2Gltf(
draco::DT_FLOAT32); draco::DT_FLOAT32);
const auto _ = gltf->AddAttributeToPrimitive<Vec2f>( const auto _ = gltf->AddAttributeToPrimitive<Vec2f>(
buffer, surfaceModel, *primitive, ATTR_TEXCOORD_1); buffer, surfaceModel, *primitive, ATTR_TEXCOORD_1);
accessors.emplace_back(_);
} }
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_JOINT_INDICES) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_JOINT_INDICES) != 0) {
const AttributeDefinition<Vec4i> ATTR_JOINTS( const AttributeDefinition<Vec4i> ATTR_JOINTS(
@ -538,6 +614,7 @@ ModelData* Raw2Gltf(
draco::DT_UINT16); draco::DT_UINT16);
const auto _ = const auto _ =
gltf->AddAttributeToPrimitive<Vec4i>(buffer, surfaceModel, *primitive, ATTR_JOINTS); gltf->AddAttributeToPrimitive<Vec4i>(buffer, surfaceModel, *primitive, ATTR_JOINTS);
accessors.emplace_back(_);
} }
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_JOINT_WEIGHTS) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_JOINT_WEIGHTS) != 0) {
const AttributeDefinition<Vec4f> ATTR_WEIGHTS( const AttributeDefinition<Vec4f> ATTR_WEIGHTS(
@ -548,6 +625,7 @@ ModelData* Raw2Gltf(
draco::DT_FLOAT32); draco::DT_FLOAT32);
const auto _ = const auto _ =
gltf->AddAttributeToPrimitive<Vec4f>(buffer, surfaceModel, *primitive, ATTR_WEIGHTS); gltf->AddAttributeToPrimitive<Vec4f>(buffer, surfaceModel, *primitive, ATTR_WEIGHTS);
accessors.emplace_back(_);
} }
// each channel present in the mesh always ends up a target in the primitive // each channel present in the mesh always ends up a target in the primitive
@ -599,7 +677,7 @@ ModelData* Raw2Gltf(
primitive->AddTarget(pAcc.get(), nAcc.get(), tAcc.get()); primitive->AddTarget(pAcc.get(), nAcc.get(), tAcc.get());
} }
} }
if (options.draco.enabled) { if (options.draco.enabledMesh) {
// Set up the encoder. // Set up the encoder.
draco::Encoder encoder; draco::Encoder encoder;
@ -634,7 +712,15 @@ ModelData* Raw2Gltf(
auto view = gltf->AddRawBufferView(buffer, dracoBuffer.data(), to_uint32(dracoBuffer.size())); auto view = gltf->AddRawBufferView(buffer, dracoBuffer.data(), to_uint32(dracoBuffer.size()));
primitive->NoteDracoBuffer(*view); primitive->NoteDracoBuffer(*view);
dracoBuffer.Clear();
for (auto accessor : accessors)
{
accessor->bufferView = view->ix;
accessor->byteOffset = -1;
}
} }
accessors.clear();
mesh->AddPrimitive(primitive); mesh->AddPrimitive(primitive);
} }
@ -800,10 +886,14 @@ ModelData* Raw2Gltf(
if (!gltf->lights.ptrs.empty()) { if (!gltf->lights.ptrs.empty()) {
extensionsUsed.push_back(KHR_LIGHTS_PUNCTUAL); extensionsUsed.push_back(KHR_LIGHTS_PUNCTUAL);
} }
if (options.draco.enabled) { if (options.draco.enabledMesh) {
extensionsUsed.push_back(KHR_DRACO_MESH_COMPRESSION); extensionsUsed.push_back(KHR_DRACO_MESH_COMPRESSION);
extensionsRequired.push_back(KHR_DRACO_MESH_COMPRESSION); extensionsRequired.push_back(KHR_DRACO_MESH_COMPRESSION);
} }
if (options.draco.enabledAnimation) {
extensionsUsed.push_back(DRACO_ANIMATION_COMPRESSION);
extensionsRequired.push_back(DRACO_ANIMATION_COMPRESSION);
}
json glTFJson{{"asset", {{"generator", "FBX2glTF v" + FBX2GLTF_VERSION}, {"version", "2.0"}}}, json glTFJson{{"asset", {{"generator", "FBX2glTF v" + FBX2GLTF_VERSION}, {"version", "2.0"}}},
{"scene", rootScene.ix}}; {"scene", rootScene.ix}};

View File

@ -14,6 +14,8 @@
// This can be a macro under Windows, confusing Draco // This can be a macro under Windows, confusing Draco
#undef ERROR #undef ERROR
#include <draco/compression/encode.h> #include <draco/compression/encode.h>
#include <draco/animation/keyframe_animation.h>
#include <draco/animation/keyframe_animation_encoder.h>
#include "FBX2glTF.h" #include "FBX2glTF.h"
#include "raw/RawModel.hpp" #include "raw/RawModel.hpp"
@ -21,6 +23,7 @@
const std::string KHR_DRACO_MESH_COMPRESSION = "KHR_draco_mesh_compression"; const std::string KHR_DRACO_MESH_COMPRESSION = "KHR_draco_mesh_compression";
const std::string KHR_MATERIALS_CMN_UNLIT = "KHR_materials_unlit"; const std::string KHR_MATERIALS_CMN_UNLIT = "KHR_materials_unlit";
const std::string KHR_LIGHTS_PUNCTUAL = "KHR_lights_punctual"; const std::string KHR_LIGHTS_PUNCTUAL = "KHR_lights_punctual";
const std::string DRACO_ANIMATION_COMPRESSION = "Draco_animation_compression";
const std::string extBufferFilename = "buffer.bin"; const std::string extBufferFilename = "buffer.bin";
@ -98,6 +101,41 @@ struct GLType {
((T*)buf)[3] = quaternion.scalar(); ((T*)buf)[3] = quaternion.scalar();
} }
template <class T>
const std::vector<T>& toStdVec(const std::vector<T>& scalars) const {
return scalars;
}
template <class T, int d>
std::vector<T> toStdVec(const std::vector<mathfu::Vector<T, d>>& vectors) const {
std::vector<T> vec(vectors.size() * d);
std::vector<uint8_t> component(sizeof(T) * d);
for (uint32_t ii = 0; ii < vectors.size(); ii++) {
uint8_t* ptr = &component[0];
this->write(ptr, vectors[ii]);
const T* typePtr = (const T*)ptr;
for (uint32_t jj = 0; jj < d; ++jj) {
vec[ii * d + jj] = *(typePtr + jj);
}
}
return vec;
}
template <class T>
std::vector<T> toStdVec(const std::vector<mathfu::Quaternion<T>>& quaternions) const {
std::vector<T> vec(quaternions.size() * 4);
std::vector<uint8_t> component(sizeof(T) * 4);
for (uint32_t ii = 0; ii < quaternions.size(); ii++) {
uint8_t* ptr = &component[0];
this->write(ptr, quaternions[ii]);
const T* typePtr = (const T*)ptr;
for (uint32_t jj = 0; jj < 4; ++jj) {
vec[ii * 4 + jj] = *(typePtr + jj);
}
}
return vec;
}
const ComponentType componentType; const ComponentType componentType;
const uint8_t count; const uint8_t count;
const std::string dataType; const std::string dataType;
@ -138,7 +176,7 @@ struct AttributeDefinition {
const GLType& _glType, const GLType& _glType,
const draco::GeometryAttribute::Type dracoAttribute, const draco::GeometryAttribute::Type dracoAttribute,
const draco::DataType dracoComponentType) const draco::DataType dracoComponentType)
: gltfName(gltfName), : gltfName(std::move(gltfName)),
rawAttributeIx(rawAttributeIx), rawAttributeIx(rawAttributeIx),
glType(_glType), glType(_glType),
dracoAttribute(dracoAttribute), dracoAttribute(dracoAttribute),
@ -148,13 +186,40 @@ struct AttributeDefinition {
const std::string gltfName, const std::string gltfName,
const T RawVertex::*rawAttributeIx, const T RawVertex::*rawAttributeIx,
const GLType& _glType) const GLType& _glType)
: gltfName(gltfName), : gltfName(std::move(gltfName)),
rawAttributeIx(rawAttributeIx), rawAttributeIx(rawAttributeIx),
glType(_glType), glType(_glType),
dracoAttribute(draco::GeometryAttribute::INVALID), dracoAttribute(draco::GeometryAttribute::INVALID),
dracoComponentType(draco::DataType::DT_INVALID) {} dracoComponentType(draco::DataType::DT_INVALID) {}
}; };
template <class T>
struct ChannelDefinition {
const std::string path;
const std::vector<T>& channelData;
const GLType glType;
const draco::DataType dracoComponentType;
ChannelDefinition(
const std::string path,
const std::vector<T>& channelData,
const GLType& glType,
const draco::DataType dracoComponentType)
: path(std::move(path)),
channelData(channelData),
glType(glType),
dracoComponentType(dracoComponentType) {}
ChannelDefinition(
const std::string path,
const std::vector<T>& channelData,
const GLType& glType)
: path(std::move(path)),
channelData(channelData),
glType(glType),
dracoComponentType(draco::DataType::DT_INVALID) {}
};
struct AccessorData; struct AccessorData;
struct AnimationData; struct AnimationData;
struct BufferData; struct BufferData;

View File

@ -25,6 +25,8 @@ json AccessorData::serialize() const {
{"componentType", type.componentType.glType}, {"type", type.dataType}, {"count", count}}; {"componentType", type.componentType.glType}, {"type", type.dataType}, {"count", count}};
if (bufferView >= 0) { if (bufferView >= 0) {
result["bufferView"] = bufferView; result["bufferView"] = bufferView;
}
if (byteOffset >= 0) {
result["byteOffset"] = byteOffset; result["byteOffset"] = byteOffset;
} }
if (!min.empty()) { if (!min.empty()) {

View File

@ -34,10 +34,10 @@ struct AccessorData : Holdable {
return type.byteStride() * count; return type.byteStride() * count;
} }
const int bufferView; /*const*/ int bufferView;
const GLType type; const GLType type;
unsigned int byteOffset; /*unsigned*/ int byteOffset;
unsigned int count; unsigned int count;
std::vector<float> min; std::vector<float> min;
std::vector<float> max; std::vector<float> max;

View File

@ -10,11 +10,20 @@
#include <utility> #include <utility>
#include "AccessorData.hpp"
#include "NodeData.hpp" #include "NodeData.hpp"
AnimationData::AnimationData(std::string name, const AccessorData& timeAccessor) AnimationData::AnimationData(std::string name)
: Holdable(), name(std::move(name)), timeAccessor(timeAccessor.ix) {} : Holdable(),
name(std::move(name)) {}
AnimationData::AnimationData(std::string name, std::shared_ptr<draco::KeyframeAnimation> dracoKeyframeAnimation)
: Holdable(),
name(std::move(name)),
dracoKeyframeAnimation(dracoKeyframeAnimation) {}
void AnimationData::AddTimestamps(const AccessorData& timeAccessor) {
this->timeAccessor = timeAccessor.ix;
}
// assumption: 1-to-1 relationship between channels and samplers; this is a simplification on what // assumption: 1-to-1 relationship between channels and samplers; this is a simplification on what
// glTF can express, but it means we can rely on samplerIx == channelIx throughout an animation // glTF can express, but it means we can rely on samplerIx == channelIx throughout an animation

View File

@ -9,14 +9,44 @@
#pragma once #pragma once
#include "gltf/Raw2Gltf.hpp" #include "gltf/Raw2Gltf.hpp"
#include "AccessorData.hpp"
struct AnimationData : Holdable { struct AnimationData : Holdable {
AnimationData(std::string name, const AccessorData& timeAccessor); AnimationData(std::string name);
AnimationData(std::string name, std::shared_ptr<draco::KeyframeAnimation> dracoKeyframeAnimation);
void AddTimestamps(const AccessorData& timeAccessor);
template <class T>
void AddDracoTimestamps(const AccessorData& timeAccessor, const std::vector<T>& timestamps) {
this->timeAccessor = timeAccessor.ix;
std::vector<draco::KeyframeAnimation::TimestampType> dracoTimestamps(timestamps.begin(), timestamps.end());
dracoKeyframeAnimation->SetTimestamps(dracoTimestamps);
}
// assumption: 1-to-1 relationship between channels and samplers; this is a simplification on what // assumption: 1-to-1 relationship between channels and samplers; this is a simplification on what
// glTF can express, but it means we can rely on samplerIx == channelIx throughout an animation // glTF can express, but it means we can rely on samplerIx == channelIx throughout an animation
void AddNodeChannel(const NodeData& node, const AccessorData& accessor, std::string path); void AddNodeChannel(const NodeData& node, const AccessorData& accessor, std::string path);
template <class T>
void AddDracoNodeChannel(
const NodeData& node,
const AccessorData& accessor,
const std::string& path,
const ChannelDefinition<T>& keyframe) {
assert(channels.size() == samplers.size());
uint32_t ix = to_uint32(channels.size());
channels.emplace_back(channel_t(ix, node, std::move(path)));
samplers.emplace_back(sampler_t(timeAccessor, accessor.ix));
dracoKeyframeAnimation->AddKeyframes(
keyframe.dracoComponentType,
keyframe.glType.count,
keyframe.glType.toStdVec(keyframe.channelData));
}
json serialize() const override; json serialize() const override;
struct channel_t { struct channel_t {
@ -35,9 +65,10 @@ struct AnimationData : Holdable {
}; };
const std::string name; const std::string name;
const uint32_t timeAccessor; uint32_t timeAccessor;
std::vector<channel_t> channels; std::vector<channel_t> channels;
std::vector<sampler_t> samplers; std::vector<sampler_t> samplers;
std::shared_ptr<draco::KeyframeAnimation> dracoKeyframeAnimation;
}; };
void to_json(json& j, const AnimationData::channel_t& data); void to_json(json& j, const AnimationData::channel_t& data);

View File

@ -36,7 +36,7 @@ struct PrimitiveData {
const AccessorData* tangents); const AccessorData* tangents);
template <class T> template <class T>
void AddDracoAttrib(const AttributeDefinition<T> attribute, const std::vector<T>& attribArr) { void AddDracoAttrib(const AttributeDefinition<T>& attribute, const std::vector<T>& attribArr) {
draco::PointAttribute att; draco::PointAttribute att;
int8_t componentCount = attribute.glType.count; int8_t componentCount = attribute.glType.count;
att.Init( att.Init(