mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-07-06 07:15:12 +08:00
Serializer skips null values
This commit is contained in:
parent
ae751c4882
commit
faa2722f45
162
tiny_gltf.h
162
tiny_gltf.h
@ -3298,8 +3298,9 @@ static void SerializeNumberArrayProperty(const std::string &key,
|
|||||||
for (unsigned int i = 0; i < value.size(); ++i) {
|
for (unsigned int i = 0; i < value.size(); ++i) {
|
||||||
vals.push_back(static_cast<double>(value[i]));
|
vals.push_back(static_cast<double>(value[i]));
|
||||||
}
|
}
|
||||||
|
if (!vals.is_null()) {
|
||||||
obj[key] = vals;
|
obj[key] = vals;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SerializeStringProperty(const std::string &key,
|
static void SerializeStringProperty(const std::string &key,
|
||||||
@ -3477,10 +3478,20 @@ static void SerializeGltfBuffer(Buffer &buffer, json &o,
|
|||||||
static void SerializeGltfBufferView(BufferView &bufferView, json &o) {
|
static void SerializeGltfBufferView(BufferView &bufferView, json &o) {
|
||||||
SerializeNumberProperty("buffer", bufferView.buffer, o);
|
SerializeNumberProperty("buffer", bufferView.buffer, o);
|
||||||
SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o);
|
SerializeNumberProperty<size_t>("byteLength", bufferView.byteLength, o);
|
||||||
SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o);
|
|
||||||
SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o);
|
|
||||||
SerializeNumberProperty("target", bufferView.target, o);
|
|
||||||
|
|
||||||
|
// byteStride is optional, minimum allowed is 4
|
||||||
|
if (bufferView.byteStride >= 4) {
|
||||||
|
SerializeNumberProperty<size_t>("byteStride", bufferView.byteStride, o);
|
||||||
|
}
|
||||||
|
// byteOffset is optional, default is 0
|
||||||
|
if (bufferView.byteOffset > 0) {
|
||||||
|
SerializeNumberProperty<size_t>("byteOffset", bufferView.byteOffset, o);
|
||||||
|
}
|
||||||
|
// Target is optional, check if it contains a valid value
|
||||||
|
if (bufferView.target == TINYGLTF_TARGET_ARRAY_BUFFER ||
|
||||||
|
bufferView.target == TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER) {
|
||||||
|
SerializeNumberProperty("target", bufferView.target, o);
|
||||||
|
}
|
||||||
if (bufferView.name.size()) {
|
if (bufferView.name.size()) {
|
||||||
SerializeStringProperty("name", bufferView.name, o);
|
SerializeStringProperty("name", bufferView.name, o);
|
||||||
}
|
}
|
||||||
@ -3532,8 +3543,15 @@ static void SerializeGltfMesh(Mesh &mesh, json &o) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
primitive["attributes"] = attributes;
|
primitive["attributes"] = attributes;
|
||||||
SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive);
|
|
||||||
SerializeNumberProperty<int>("material", gltfPrimitive.material, primitive);
|
// Indicies is optional
|
||||||
|
if (gltfPrimitive.indices > -1) {
|
||||||
|
SerializeNumberProperty<int>("indices", gltfPrimitive.indices, primitive);
|
||||||
|
}
|
||||||
|
// Material is optional
|
||||||
|
if (gltfPrimitive.material > -1) {
|
||||||
|
SerializeNumberProperty<int>("material", gltfPrimitive.material, primitive);
|
||||||
|
}
|
||||||
SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive);
|
SerializeNumberProperty<int>("mode", gltfPrimitive.mode, primitive);
|
||||||
|
|
||||||
// Morph targets
|
// Morph targets
|
||||||
@ -3773,52 +3791,64 @@ bool TinyGLTF::WriteGltfSceneToFile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IMAGES
|
// IMAGES
|
||||||
json images;
|
if (model->images.size()) {
|
||||||
for (unsigned int i = 0; i < model->images.size(); ++i) {
|
json images;
|
||||||
json image;
|
for (unsigned int i = 0; i < model->images.size(); ++i) {
|
||||||
SerializeGltfImage(model->images[i], image);
|
json image;
|
||||||
images.push_back(image);
|
SerializeGltfImage(model->images[i], image);
|
||||||
|
images.push_back(image);
|
||||||
|
}
|
||||||
|
output["images"] = images;
|
||||||
}
|
}
|
||||||
output["images"] = images;
|
|
||||||
|
|
||||||
// MATERIALS
|
// MATERIALS
|
||||||
json materials;
|
if (model->materials.size()) {
|
||||||
for (unsigned int i = 0; i < model->materials.size(); ++i) {
|
json materials;
|
||||||
json material;
|
for (unsigned int i = 0; i < model->materials.size(); ++i) {
|
||||||
SerializeGltfMaterial(model->materials[i], material);
|
json material;
|
||||||
materials.push_back(material);
|
SerializeGltfMaterial(model->materials[i], material);
|
||||||
|
materials.push_back(material);
|
||||||
|
}
|
||||||
|
output["materials"] = materials;
|
||||||
}
|
}
|
||||||
output["materials"] = materials;
|
|
||||||
|
|
||||||
// MESHES
|
// MESHES
|
||||||
json meshes;
|
if (model->meshes.size()) {
|
||||||
for (unsigned int i = 0; i < model->meshes.size(); ++i) {
|
json meshes;
|
||||||
json mesh;
|
for (unsigned int i = 0; i < model->meshes.size(); ++i) {
|
||||||
SerializeGltfMesh(model->meshes[i], mesh);
|
json mesh;
|
||||||
meshes.push_back(mesh);
|
SerializeGltfMesh(model->meshes[i], mesh);
|
||||||
|
meshes.push_back(mesh);
|
||||||
|
}
|
||||||
|
output["meshes"] = meshes;
|
||||||
}
|
}
|
||||||
output["meshes"] = meshes;
|
|
||||||
|
|
||||||
// NODES
|
// NODES
|
||||||
json nodes;
|
if (model->nodes.size()) {
|
||||||
for (unsigned int i = 0; i < model->nodes.size(); ++i) {
|
json nodes;
|
||||||
json node;
|
for (unsigned int i = 0; i < model->nodes.size(); ++i) {
|
||||||
SerializeGltfNode(model->nodes[i], node);
|
json node;
|
||||||
nodes.push_back(node);
|
SerializeGltfNode(model->nodes[i], node);
|
||||||
|
nodes.push_back(node);
|
||||||
|
}
|
||||||
|
output["nodes"] = nodes;
|
||||||
}
|
}
|
||||||
output["nodes"] = nodes;
|
|
||||||
|
|
||||||
// SCENE
|
// SCENE
|
||||||
SerializeNumberProperty<int>("scene", model->defaultScene, output);
|
if (model->defaultScene > -1) {
|
||||||
|
SerializeNumberProperty<int>("scene", model->defaultScene, output);
|
||||||
|
}
|
||||||
|
|
||||||
// SCENES
|
// SCENES
|
||||||
json scenes;
|
if (model->scenes.size()) {
|
||||||
for (unsigned int i = 0; i < model->scenes.size(); ++i) {
|
json scenes;
|
||||||
json currentScene;
|
for (unsigned int i = 0; i < model->scenes.size(); ++i) {
|
||||||
SerializeGltfScene(model->scenes[i], currentScene);
|
json currentScene;
|
||||||
scenes.push_back(currentScene);
|
SerializeGltfScene(model->scenes[i], currentScene);
|
||||||
|
scenes.push_back(currentScene);
|
||||||
|
}
|
||||||
|
output["scenes"] = scenes;
|
||||||
}
|
}
|
||||||
output["scenes"] = scenes;
|
|
||||||
|
|
||||||
// SKINS
|
// SKINS
|
||||||
if (model->skins.size()) {
|
if (model->skins.size()) {
|
||||||
@ -3832,40 +3862,48 @@ bool TinyGLTF::WriteGltfSceneToFile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TEXTURES
|
// TEXTURES
|
||||||
json textures;
|
if (model->textures.size()) {
|
||||||
for (unsigned int i = 0; i < model->textures.size(); ++i) {
|
json textures;
|
||||||
json texture;
|
for (unsigned int i = 0; i < model->textures.size(); ++i) {
|
||||||
SerializeGltfTexture(model->textures[i], texture);
|
json texture;
|
||||||
textures.push_back(texture);
|
SerializeGltfTexture(model->textures[i], texture);
|
||||||
|
textures.push_back(texture);
|
||||||
|
}
|
||||||
|
output["textures"] = textures;
|
||||||
}
|
}
|
||||||
output["textures"] = textures;
|
|
||||||
|
|
||||||
// SAMPLERS
|
// SAMPLERS
|
||||||
json samplers;
|
if (model->samplers.size()) {
|
||||||
for (unsigned int i = 0; i < model->samplers.size(); ++i) {
|
json samplers;
|
||||||
json sampler;
|
for (unsigned int i = 0; i < model->samplers.size(); ++i) {
|
||||||
SerializeGltfSampler(model->samplers[i], sampler);
|
json sampler;
|
||||||
samplers.push_back(sampler);
|
SerializeGltfSampler(model->samplers[i], sampler);
|
||||||
|
samplers.push_back(sampler);
|
||||||
|
}
|
||||||
|
output["samplers"] = samplers;
|
||||||
}
|
}
|
||||||
output["samplers"] = samplers;
|
|
||||||
|
|
||||||
// CAMERAS
|
// CAMERAS
|
||||||
json cameras;
|
if (model->cameras.size()) {
|
||||||
for (unsigned int i = 0; i < model->cameras.size(); ++i) {
|
json cameras;
|
||||||
json camera;
|
for (unsigned int i = 0; i < model->cameras.size(); ++i) {
|
||||||
SerializeGltfCamera(model->cameras[i], camera);
|
json camera;
|
||||||
cameras.push_back(camera);
|
SerializeGltfCamera(model->cameras[i], camera);
|
||||||
|
cameras.push_back(camera);
|
||||||
|
}
|
||||||
|
output["cameras"] = cameras;
|
||||||
}
|
}
|
||||||
output["cameras"] = cameras;
|
|
||||||
|
|
||||||
// LIGHTS
|
// LIGHTS
|
||||||
json lights;
|
if (model->lights.size()) {
|
||||||
for (unsigned int i = 0; i < model->lights.size(); ++i) {
|
json lights;
|
||||||
json light;
|
for (unsigned int i = 0; i < model->lights.size(); ++i) {
|
||||||
SerializeGltfLight(model->lights[i], light);
|
json light;
|
||||||
lights.push_back(light);
|
SerializeGltfLight(model->lights[i], light);
|
||||||
|
lights.push_back(light);
|
||||||
|
}
|
||||||
|
output["lights"] = lights;
|
||||||
}
|
}
|
||||||
output["lights"] = lights;
|
|
||||||
|
|
||||||
WriteGltfFile(filename, output.dump());
|
WriteGltfFile(filename, output.dump());
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user