From 5b7d418a3485af7aa09054a6b4294ea9de9c6622 Mon Sep 17 00:00:00 2001 From: dververidis Date: Fri, 25 Nov 2022 12:34:45 +0200 Subject: [PATCH 1/2] Comments about accessor format when serializing big models with more than 65536 vertices. Added support for ACCESSOR_COLOR_1, ACCESSOR_COLOR_2, and ACCESSOR_COLOR_3 --- GLTFSDK.Samples/Serialize/Source/main.cpp | 11 ++++++++++- GLTFSDK/Inc/GLTFSDK/Constants.h | 3 +++ GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h | 3 +++ GLTFSDK/Source/MeshPrimitiveUtils.cpp | 19 +++++++++++++++++++ GLTFSDK/Source/SchemaValidation.cpp | 2 +- GLTFSDK/Source/Validation.cpp | 3 +++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/GLTFSDK.Samples/Serialize/Source/main.cpp b/GLTFSDK.Samples/Serialize/Source/main.cpp index ac87c10..310be5b 100644 --- a/GLTFSDK.Samples/Serialize/Source/main.cpp +++ b/GLTFSDK.Samples/Serialize/Source/main.cpp @@ -89,8 +89,17 @@ namespace 0U, 1U, 2U }; + // Warning!: When your model has more than 65536 vertices then you must store in uint32_t as follows + // std::vector indices = { + // 0U, 1U, 2U .... your indices here + // }; + + // Copy the Accessor's id - subsequent calls to AddAccessor may invalidate the returned reference - accessorIdIndices = bufferBuilder.AddAccessor(indices, { TYPE_SCALAR, COMPONENT_UNSIGNED_SHORT }).id; + accessorIdIndices = bufferBuilder.AddAccessor(indices, { TYPE_SCALAR, COMPONENT_UNSIGNED_SHORT }).id; + + // Warning ! : When your model has more than 65536 vertices then this number should be INT but not SHORT + // accessorIdIndices = bufferBuilder.AddAccessor(indices, { Microsoft::glTF::TYPE_SCALAR, Microsoft::glTF::COMPONENT_UNSIGNED_INT}).id; // Create a BufferView with target ARRAY_BUFFER (as it will reference vertex attribute data) bufferBuilder.AddBufferView(BufferViewTarget::ARRAY_BUFFER); diff --git a/GLTFSDK/Inc/GLTFSDK/Constants.h b/GLTFSDK/Inc/GLTFSDK/Constants.h index dcced8a..fa0c4dc 100644 --- a/GLTFSDK/Inc/GLTFSDK/Constants.h +++ b/GLTFSDK/Inc/GLTFSDK/Constants.h @@ -50,6 +50,9 @@ namespace Microsoft constexpr const char* ACCESSOR_TEXCOORD_0 = "TEXCOORD_0"; constexpr const char* ACCESSOR_TEXCOORD_1 = "TEXCOORD_1"; constexpr const char* ACCESSOR_COLOR_0 = "COLOR_0"; + constexpr const char* ACCESSOR_COLOR_1 = "COLOR_1"; + constexpr const char* ACCESSOR_COLOR_2 = "COLOR_2"; + constexpr const char* ACCESSOR_COLOR_3 = "COLOR_3"; constexpr const char* ACCESSOR_JOINTS_0 = "JOINTS_0"; constexpr const char* ACCESSOR_WEIGHTS_0 = "WEIGHTS_0"; diff --git a/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h b/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h index 7b518b8..ab9766f 100644 --- a/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h +++ b/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h @@ -47,6 +47,9 @@ namespace Microsoft std::vector GetColors(const Document& doc, const GLTFResourceReader& reader, const Accessor& accessor); std::vector GetColors_0(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); + std::vector GetColors_1(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); + std::vector GetColors_2(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); + std::vector GetColors_3(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); std::vector GetJointIndices32(const Document& doc, const GLTFResourceReader& reader, const Accessor& accessor); std::vector GetJointIndices32_0(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); diff --git a/GLTFSDK/Source/MeshPrimitiveUtils.cpp b/GLTFSDK/Source/MeshPrimitiveUtils.cpp index 1ebc123..9fbac6f 100644 --- a/GLTFSDK/Source/MeshPrimitiveUtils.cpp +++ b/GLTFSDK/Source/MeshPrimitiveUtils.cpp @@ -758,6 +758,25 @@ std::vector MeshPrimitiveUtils::GetColors_0(const Document& doc, const return GetColors(doc, reader, accessor); } +std::vector MeshPrimitiveUtils::GetColors_1(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive) +{ + const auto& accessor = doc.accessors.Get(meshPrimitive.GetAttributeAccessorId(ACCESSOR_COLOR_1)); + return GetColors(doc, reader, accessor); +} + +std::vector MeshPrimitiveUtils::GetColors_2(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive) +{ + const auto& accessor = doc.accessors.Get(meshPrimitive.GetAttributeAccessorId(ACCESSOR_COLOR_2)); + return GetColors(doc, reader, accessor); +} + +std::vector MeshPrimitiveUtils::GetColors_3(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive) +{ + const auto& accessor = doc.accessors.Get(meshPrimitive.GetAttributeAccessorId(ACCESSOR_COLOR_3)); + return GetColors(doc, reader, accessor); +} + + // Joints std::vector MeshPrimitiveUtils::GetJointIndices32(const Document& doc, const GLTFResourceReader& reader, const Accessor& jointsAccessor) { diff --git a/GLTFSDK/Source/SchemaValidation.cpp b/GLTFSDK/Source/SchemaValidation.cpp index b606f9a..9e243a6 100644 --- a/GLTFSDK/Source/SchemaValidation.cpp +++ b/GLTFSDK/Source/SchemaValidation.cpp @@ -34,7 +34,7 @@ namespace throw GLTFException("Schema document at " + uri + " is not valid JSON"); } - auto result = schemaDocuments.emplace(uri, rapidjson::SchemaDocument(document, nullptr, 0, this)); + auto result = schemaDocuments.emplace(uri, rapidjson::SchemaDocument(document)); // , nullptr, 0, this)); assert(result.second); auto resultSchemaDoc = &(result.first->second); assert(resultSchemaDoc); diff --git a/GLTFSDK/Source/Validation.cpp b/GLTFSDK/Source/Validation.cpp index f081fac..c4ad55a 100644 --- a/GLTFSDK/Source/Validation.cpp +++ b/GLTFSDK/Source/Validation.cpp @@ -215,6 +215,9 @@ void Validation::ValidateMeshPrimitiveAttributeAccessors( { ACCESSOR_TEXCOORD_0, { { TYPE_VEC2 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, { ACCESSOR_TEXCOORD_1, { { TYPE_VEC2 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, { ACCESSOR_COLOR_0, { { TYPE_VEC3, TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, + { ACCESSOR_COLOR_1, { { TYPE_VEC3, TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, + { ACCESSOR_COLOR_2, { { TYPE_VEC3, TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, + { ACCESSOR_COLOR_3, { { TYPE_VEC3, TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, { ACCESSOR_JOINTS_0, { { TYPE_VEC4 }, { COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, { ACCESSOR_WEIGHTS_0, { { TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } } }; From c09342333843af6f2ae2434212e38a048eb7b4ad Mon Sep 17 00:00:00 2001 From: dververidis Date: Wed, 4 Jan 2023 10:36:49 +0200 Subject: [PATCH 2/2] Color and Attribute Stable --- .../Deserialize/Deserialize.vcxproj | 2 +- GLTFSDK.Samples/Serialize/Serialize.vcxproj | 2 +- GLTFSDK.Test/GLTFSDK.Test.vcxproj | 2 +- GLTFSDK/GLTFSDK.vcxproj | 2 +- GLTFSDK/Inc/GLTFSDK/Constants.h | 5 ++ GLTFSDK/Inc/GLTFSDK/GLTF.h | 19 ++++++- GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h | 4 ++ GLTFSDK/Source/MeshPrimitiveUtils.cpp | 56 +++++++++++++++++++ GLTFSDK/Source/Serialize.cpp | 6 ++ GLTFSDK/Source/Validation.cpp | 1 + 10 files changed, 93 insertions(+), 6 deletions(-) diff --git a/GLTFSDK.Samples/Deserialize/Deserialize.vcxproj b/GLTFSDK.Samples/Deserialize/Deserialize.vcxproj index 26d1540..4bd21e5 100644 --- a/GLTFSDK.Samples/Deserialize/Deserialize.vcxproj +++ b/GLTFSDK.Samples/Deserialize/Deserialize.vcxproj @@ -39,7 +39,7 @@ {DE6A7757-2DF3-4705-829B-96A77BABF0B2} Win32Proj Deserialize - 10.0.16299.0 + 10.0.19041.0 diff --git a/GLTFSDK.Samples/Serialize/Serialize.vcxproj b/GLTFSDK.Samples/Serialize/Serialize.vcxproj index 65907fd..19e21a6 100644 --- a/GLTFSDK.Samples/Serialize/Serialize.vcxproj +++ b/GLTFSDK.Samples/Serialize/Serialize.vcxproj @@ -39,7 +39,7 @@ {DE6A7757-2DE3-4705-829B-96A77BABF0B2} Win32Proj Serialize - 10.0.16299.0 + 10.0.19041.0 diff --git a/GLTFSDK.Test/GLTFSDK.Test.vcxproj b/GLTFSDK.Test/GLTFSDK.Test.vcxproj index 6d5146b..a044d1f 100644 --- a/GLTFSDK.Test/GLTFSDK.Test.vcxproj +++ b/GLTFSDK.Test/GLTFSDK.Test.vcxproj @@ -30,7 +30,7 @@ {F170B140-6AB9-4014-97D9-D897E0493CEC} Win32Proj GLTFSDKTest - 10.0.10586.0 + 10.0.19041.0 GLTFSDK.Test $(Platform) diff --git a/GLTFSDK/GLTFSDK.vcxproj b/GLTFSDK/GLTFSDK.vcxproj index 8a2cb84..9903c22 100644 --- a/GLTFSDK/GLTFSDK.vcxproj +++ b/GLTFSDK/GLTFSDK.vcxproj @@ -37,7 +37,7 @@ {F656C078-7F2A-4753-9B92-5E959AF80E26} GLTFSDK - 10.0.10586.0 + 10.0.19041.0 diff --git a/GLTFSDK/Inc/GLTFSDK/Constants.h b/GLTFSDK/Inc/GLTFSDK/Constants.h index fa0c4dc..c79963a 100644 --- a/GLTFSDK/Inc/GLTFSDK/Constants.h +++ b/GLTFSDK/Inc/GLTFSDK/Constants.h @@ -53,6 +53,9 @@ namespace Microsoft constexpr const char* ACCESSOR_COLOR_1 = "COLOR_1"; constexpr const char* ACCESSOR_COLOR_2 = "COLOR_2"; constexpr const char* ACCESSOR_COLOR_3 = "COLOR_3"; + constexpr const char* ACCESSOR_ATTRIBUTE_0 = "ATTRIBUTE_0"; + constexpr const char* ACCESSOR_ATTRIBUTE_1 = "ATTRIBUTE_1"; + constexpr const char* ACCESSOR_ATTRIBUTE_2 = "ATTRIBUTE_2"; constexpr const char* ACCESSOR_JOINTS_0 = "JOINTS_0"; constexpr const char* ACCESSOR_WEIGHTS_0 = "WEIGHTS_0"; @@ -79,6 +82,8 @@ namespace Microsoft constexpr const char* TARGETPATH_NAME_ROTATION = "rotation"; constexpr const char* TARGETPATH_NAME_SCALE = "scale"; constexpr const char* TARGETPATH_NAME_WEIGHTS = "weights"; + constexpr const char* TARGETPATH_NAME_COLOR_0 = "color0"; + constexpr const char* TARGETPATH_NAME_ATTRIBUTE_0 = "attribute0"; constexpr const char* INTERPOLATIONTYPE_NAME_LINEAR = "LINEAR"; constexpr const char* INTERPOLATIONTYPE_NAME_STEP = "STEP"; diff --git a/GLTFSDK/Inc/GLTFSDK/GLTF.h b/GLTFSDK/Inc/GLTFSDK/GLTF.h index 0bc4b57..8dac5ce 100644 --- a/GLTFSDK/Inc/GLTFSDK/GLTF.h +++ b/GLTFSDK/Inc/GLTFSDK/GLTF.h @@ -90,7 +90,9 @@ namespace Microsoft TARGET_TRANSLATION, TARGET_ROTATION, TARGET_SCALE, - TARGET_WEIGHTS + TARGET_WEIGHTS, + TARGET_COLOR_0, + TARGET_ATTRIBUTE_0 }; enum InterpolationType @@ -147,6 +149,14 @@ namespace Microsoft { return TARGET_WEIGHTS; } + if (targetPath == TARGETPATH_NAME_COLOR_0) + { + return TARGET_COLOR_0; + } + if (targetPath == TARGETPATH_NAME_ATTRIBUTE_0) + { + return TARGET_ATTRIBUTE_0; + } return TARGET_UNKNOWN; } @@ -544,11 +554,16 @@ namespace Microsoft std::string positionsAccessorId; std::string normalsAccessorId; std::string tangentsAccessorId; + std::string color0AccessorId; + std::string attribute0AccessorId; + bool operator==(const MorphTarget& rhs) const { return this->positionsAccessorId == rhs.positionsAccessorId && this->normalsAccessorId == rhs.normalsAccessorId - && this->tangentsAccessorId == rhs.tangentsAccessorId; + && this->tangentsAccessorId == rhs.tangentsAccessorId + && this->color0AccessorId == rhs.color0AccessorId + && this->attribute0AccessorId == rhs.attribute0AccessorId; } bool operator!=(const MorphTarget& rhs) const diff --git a/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h b/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h index ab9766f..7ae9184 100644 --- a/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h +++ b/GLTFSDK/Inc/GLTFSDK/MeshPrimitiveUtils.h @@ -51,6 +51,10 @@ namespace Microsoft std::vector GetColors_2(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); std::vector GetColors_3(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); + std::vector GetFloatAttributes(const Document& doc, const GLTFResourceReader& reader, const Accessor& accessor); + std::vector GetFloatAttributes_0(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); + + std::vector GetJointIndices32(const Document& doc, const GLTFResourceReader& reader, const Accessor& accessor); std::vector GetJointIndices32_0(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive); diff --git a/GLTFSDK/Source/MeshPrimitiveUtils.cpp b/GLTFSDK/Source/MeshPrimitiveUtils.cpp index 9fbac6f..8b4ee3b 100644 --- a/GLTFSDK/Source/MeshPrimitiveUtils.cpp +++ b/GLTFSDK/Source/MeshPrimitiveUtils.cpp @@ -752,6 +752,52 @@ std::vector MeshPrimitiveUtils::GetColors(const Document& doc, const G } } + +// Custom Attributes +std::vector MeshPrimitiveUtils::GetFloatAttributes(const Document& doc, const GLTFResourceReader& reader, const Accessor& Accessor) +{ + // Vertex Attributes as Floats + // if (Accessor.componentType == COMPONENT_FLOAT) + // { + std::vector data = reader.ReadFloatData(doc, Accessor); + + + return data; + // if (Accessor.type == TYPE_VEC4) + // return PackColorsRGBA(colorData); + // else + // return PackColorsRGB(colorData); + + // throw GLTFException("Not implemented yet for Float Vertex Attributes " + Accessor.id); + // } + + + // // 3. 3Bytes = color + // if (Accessor.componentType == COMPONENT_UNSIGNED_BYTE ) + // { + // std::vector data = reader.ReadBinaryData(doc, Accessor); + // + // if (Accessor.type == TYPE_VEC4) + // return PackColorsRGBA(data); + // else + // return PackColorsRGB(data); + // + // //throw GLTFException("Not implemented yet for Byte Vertex Attributes " + colorsAccessor.id); + // } + // + // + // // 4. Unsigned Short + // if (Accessor.componentType != COMPONENT_UNSIGNED_SHORT) + // { + // throw GLTFException("Not implement yet for Unsigned Shorts " + Accessor.id); + // } + // + + + +} + + std::vector MeshPrimitiveUtils::GetColors_0(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive) { const auto& accessor = doc.accessors.Get(meshPrimitive.GetAttributeAccessorId(ACCESSOR_COLOR_0)); @@ -777,6 +823,16 @@ std::vector MeshPrimitiveUtils::GetColors_3(const Document& doc, const } +std::vector MeshPrimitiveUtils::GetFloatAttributes_0(const Document& doc, const GLTFResourceReader& reader, const MeshPrimitive& meshPrimitive) +{ + const auto& accessor = doc.accessors.Get(meshPrimitive.GetAttributeAccessorId(ACCESSOR_ATTRIBUTE_0)); + return GetFloatAttributes(doc, reader, accessor); +} + + + + + // Joints std::vector MeshPrimitiveUtils::GetJointIndices32(const Document& doc, const GLTFResourceReader& reader, const Accessor& jointsAccessor) { diff --git a/GLTFSDK/Source/Serialize.cpp b/GLTFSDK/Source/Serialize.cpp index abc3aa8..cf2b54e 100644 --- a/GLTFSDK/Source/Serialize.cpp +++ b/GLTFSDK/Source/Serialize.cpp @@ -47,6 +47,10 @@ namespace return TARGETPATH_NAME_SCALE; case TARGET_WEIGHTS: return TARGETPATH_NAME_WEIGHTS; + case TARGET_COLOR_0: + return TARGETPATH_NAME_COLOR_0; + case TARGET_ATTRIBUTE_0: + return TARGETPATH_NAME_ATTRIBUTE_0; default: return ""; } @@ -503,6 +507,8 @@ namespace RapidJsonUtils::AddOptionalMemberIndex(ACCESSOR_POSITION, targetValue, target.positionsAccessorId, gltfDocument.accessors, a); RapidJsonUtils::AddOptionalMemberIndex(ACCESSOR_NORMAL, targetValue, target.normalsAccessorId, gltfDocument.accessors, a); RapidJsonUtils::AddOptionalMemberIndex(ACCESSOR_TANGENT, targetValue, target.tangentsAccessorId, gltfDocument.accessors, a); + RapidJsonUtils::AddOptionalMemberIndex(ACCESSOR_ATTRIBUTE_0, targetValue, target.attribute0AccessorId, gltfDocument.accessors, a); + RapidJsonUtils::AddOptionalMemberIndex(ACCESSOR_COLOR_0, targetValue, target.color0AccessorId, gltfDocument.accessors, a); return targetValue; } diff --git a/GLTFSDK/Source/Validation.cpp b/GLTFSDK/Source/Validation.cpp index c4ad55a..f6c815c 100644 --- a/GLTFSDK/Source/Validation.cpp +++ b/GLTFSDK/Source/Validation.cpp @@ -218,6 +218,7 @@ void Validation::ValidateMeshPrimitiveAttributeAccessors( { ACCESSOR_COLOR_1, { { TYPE_VEC3, TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, { ACCESSOR_COLOR_2, { { TYPE_VEC3, TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, { ACCESSOR_COLOR_3, { { TYPE_VEC3, TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, + { ACCESSOR_ATTRIBUTE_0, { { TYPE_SCALAR }, { COMPONENT_FLOAT } } }, { ACCESSOR_JOINTS_0, { { TYPE_VEC4 }, { COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } }, { ACCESSOR_WEIGHTS_0, { { TYPE_VEC4 }, { COMPONENT_FLOAT, COMPONENT_UNSIGNED_BYTE, COMPONENT_UNSIGNED_SHORT } } } };