From 8af0229e0d257f7443b8f254fe2c75339680da57 Mon Sep 17 00:00:00 2001 From: Max Vollmer Date: Wed, 5 Feb 2020 14:40:35 +0000 Subject: [PATCH 1/4] In "ByVertice" case tempData.size() needs to be mapping_offsets.size(), not vertex_count --- code/FBX/FBXMeshGeometry.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/code/FBX/FBXMeshGeometry.cpp b/code/FBX/FBXMeshGeometry.cpp index 2f2782182..4a3de9f99 100644 --- a/code/FBX/FBXMeshGeometry.cpp +++ b/code/FBX/FBXMeshGeometry.cpp @@ -446,20 +446,19 @@ void ResolveVertexDataArray(std::vector& data_out, const Scope& source, return; } std::vector tempData; - ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName)); + ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName)); - if (tempData.size() != vertex_count) { + if (tempData.size() != mapping_offsets.size()) { FBXImporter::LogError(Formatter::format("length of input data unexpected for ByVertice mapping: ") - << tempData.size() << ", expected " << vertex_count); + << tempData.size() << ", expected " << mapping_offsets.size()); return; } data_out.resize(vertex_count); - for (size_t i = 0, e = tempData.size(); i < e; ++i) { - + for (size_t i = 0, e = tempData.size(); i < e; ++i) { const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i]; for (unsigned int j = istart; j < iend; ++j) { - data_out[mappings[j]] = tempData[i]; + data_out[mappings[j]] = tempData[i]; } } } From 18c01a023cd876ba328478a5cd4c6ff389904789 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Wed, 5 Feb 2020 21:24:54 +0000 Subject: [PATCH 2/4] Use the translation matrix in gltf2 cameras for aiCamera.mPosition --- code/glTF2/glTF2Importer.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/code/glTF2/glTF2Importer.cpp b/code/glTF2/glTF2Importer.cpp index e5052d4d6..154fef313 100644 --- a/code/glTF2/glTF2Importer.cpp +++ b/code/glTF2/glTF2Importer.cpp @@ -925,6 +925,11 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector & if (node.camera) { pScene->mCameras[node.camera.GetIndex()]->mName = ainode->mName; + if (node.translation.isPresent) { + aiVector3D trans; + CopyValue(node.translation.value, trans); + pScene->mCameras[node.camera.GetIndex()]->mPosition = trans; + } } if (node.light) { From 50334086cfb693f720d18d1664d177e63d200f0d Mon Sep 17 00:00:00 2001 From: Marc-Antoine Lortie Date: Tue, 11 Feb 2020 11:42:13 -0500 Subject: [PATCH 3/4] Added missing texture types when searching for invalid textures. --- code/PostProcessing/ValidateDataStructure.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/code/PostProcessing/ValidateDataStructure.cpp b/code/PostProcessing/ValidateDataStructure.cpp index a812efb0b..6212bfb69 100644 --- a/code/PostProcessing/ValidateDataStructure.cpp +++ b/code/PostProcessing/ValidateDataStructure.cpp @@ -777,6 +777,12 @@ void ValidateDSProcess::Validate( const aiMaterial* pMaterial) SearchForInvalidTextures(pMaterial,aiTextureType_DISPLACEMENT); SearchForInvalidTextures(pMaterial,aiTextureType_LIGHTMAP); SearchForInvalidTextures(pMaterial,aiTextureType_REFLECTION); + SearchForInvalidTextures(pMaterial,aiTextureType_BASE_COLOR); + SearchForInvalidTextures(pMaterial,aiTextureType_NORMAL_CAMERA); + SearchForInvalidTextures(pMaterial,aiTextureType_EMISSION_COLOR); + SearchForInvalidTextures(pMaterial,aiTextureType_METALNESS); + SearchForInvalidTextures(pMaterial,aiTextureType_DIFFUSE_ROUGHNESS); + SearchForInvalidTextures(pMaterial,aiTextureType_AMBIENT_OCCLUSION); } // ------------------------------------------------------------------------------------------------ From b62bd38c71293f0425a1ac5f163c746ecb7c6c5b Mon Sep 17 00:00:00 2001 From: "Hui.Du" Date: Mon, 10 Feb 2020 02:03:26 +0000 Subject: [PATCH 4/4] Fix: GLTF animation works on RTS not matrix; fix matrix related bug. --- code/glTF/glTFAsset.h | 1 + code/glTF/glTFAssetWriter.inl | 4 +++- code/glTF/glTFExporter.cpp | 20 +++++++--------- code/glTF2/glTF2Asset.h | 1 + code/glTF2/glTF2AssetWriter.inl | 8 +++---- code/glTF2/glTF2Exporter.cpp | 42 +++++++++++++++++++++++---------- 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/code/glTF/glTFAsset.h b/code/glTF/glTFAsset.h index d0b72703e..b918d456b 100644 --- a/code/glTF/glTFAsset.h +++ b/code/glTF/glTFAsset.h @@ -191,6 +191,7 @@ namespace glTF //! Values for the BufferView::target field enum BufferViewTarget { + BufferViewTarget_NONE = 0, BufferViewTarget_ARRAY_BUFFER = 34962, BufferViewTarget_ELEMENT_ARRAY_BUFFER = 34963 }; diff --git a/code/glTF/glTFAssetWriter.inl b/code/glTF/glTFAssetWriter.inl index 784264488..6abe7e078 100644 --- a/code/glTF/glTFAssetWriter.inl +++ b/code/glTF/glTFAssetWriter.inl @@ -203,7 +203,9 @@ namespace glTF { obj.AddMember("buffer", Value(bv.buffer->id, w.mAl).Move(), w.mAl); obj.AddMember("byteOffset", static_cast(bv.byteOffset), w.mAl); obj.AddMember("byteLength", static_cast(bv.byteLength), w.mAl); - obj.AddMember("target", int(bv.target), w.mAl); + if (bv.target != BufferViewTarget_NONE) { + obj.AddMember("target", int(bv.target), w.mAl); + } } inline void Write(Value& /*obj*/, Camera& /*c*/, AssetWriter& /*w*/) diff --git a/code/glTF/glTFExporter.cpp b/code/glTF/glTFExporter.cpp index 072234891..7c21b738b 100644 --- a/code/glTF/glTFExporter.cpp +++ b/code/glTF/glTFExporter.cpp @@ -160,10 +160,7 @@ static void CopyValue(const aiMatrix4x4& v, glTF::mat4& o) static void CopyValue(const aiMatrix4x4& v, aiMatrix4x4& o) { - o.a1 = v.a1; o.a2 = v.a2; o.a3 = v.a3; o.a4 = v.a4; - o.b1 = v.b1; o.b2 = v.b2; o.b3 = v.b3; o.b4 = v.b4; - o.c1 = v.c1; o.c2 = v.c2; o.c3 = v.c3; o.c4 = v.c4; - o.d1 = v.d1; o.d2 = v.d2; o.d3 = v.d3; o.d4 = v.d4; + memcpy(&o, &v, sizeof(aiMatrix4x4)); } static void IdentityMatrix4(glTF::mat4& o) @@ -230,9 +227,8 @@ inline void SetAccessorRange(ComponentType compType, Ref acc, void* da } } -inline Ref ExportData(Asset& a, std::string& meshName, Ref& buffer, - unsigned int count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false) -{ +inline Ref ExportData(Asset &a, std::string &meshName, Ref &buffer, + unsigned int count, void *data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, BufferViewTarget target = BufferViewTarget_NONE) { if (!count || !data) return Ref(); unsigned int numCompsIn = AttribType::GetNumComponents(typeIn); @@ -251,7 +247,7 @@ inline Ref ExportData(Asset& a, std::string& meshName, Ref& bu bv->buffer = buffer; bv->byteOffset = unsigned(offset); bv->byteLength = length; //! The target that the WebGL buffer should be bound to. - bv->target = isIndices ? BufferViewTarget_ELEMENT_ARRAY_BUFFER : BufferViewTarget_ARRAY_BUFFER; + bv->target = target; // accessor Ref acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor")); @@ -616,13 +612,13 @@ void glTFExporter::ExportMeshes() // If compression is used then you need parameters of uncompressed region: begin and size. At this step "begin" is stored. if(comp_allow) idx_srcdata_begin = b->byteLength; - Ref v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); + Ref v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER); if (v) p.attributes.position.push_back(v); /******************** Normals ********************/ if(comp_allow && (aim->mNormals != 0)) idx_srcdata_normal = b->byteLength;// Store index of normals array. - Ref n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); + Ref n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER); if (n) p.attributes.normal.push_back(n); /************** Texture coordinates **************/ @@ -639,7 +635,7 @@ void glTFExporter::ExportMeshes() if(comp_allow) idx_srcdata_tc.push_back(b->byteLength);// Store index of texture coordinates array. - Ref tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, false); + Ref tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER); if (tc) p.attributes.texcoord.push_back(tc); } } @@ -657,7 +653,7 @@ void glTFExporter::ExportMeshes() } } - p.indices = ExportData(*mAsset, meshId, b, unsigned(indices.size()), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_SHORT, true); + p.indices = ExportData(*mAsset, meshId, b, unsigned(indices.size()), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_SHORT, BufferViewTarget_ELEMENT_ARRAY_BUFFER); } switch (aim->mPrimitiveTypes) { diff --git a/code/glTF2/glTF2Asset.h b/code/glTF2/glTF2Asset.h index 53774de7a..d3c1654d0 100644 --- a/code/glTF2/glTF2Asset.h +++ b/code/glTF2/glTF2Asset.h @@ -198,6 +198,7 @@ namespace glTF2 //! Values for the BufferView::target field enum BufferViewTarget { + BufferViewTarget_NONE = 0, BufferViewTarget_ARRAY_BUFFER = 34962, BufferViewTarget_ELEMENT_ARRAY_BUFFER = 34963 }; diff --git a/code/glTF2/glTF2AssetWriter.inl b/code/glTF2/glTF2AssetWriter.inl index 02c14980d..8adc20404 100644 --- a/code/glTF2/glTF2AssetWriter.inl +++ b/code/glTF2/glTF2AssetWriter.inl @@ -176,13 +176,13 @@ namespace glTF2 { valSampler.AddMember("input", s.input->index, w.mAl); switch (s.interpolation) { case Interpolation_LINEAR: - valSampler.AddMember("path", "LINEAR", w.mAl); + valSampler.AddMember("interpolation", "LINEAR", w.mAl); break; case Interpolation_STEP: - valSampler.AddMember("path", "STEP", w.mAl); + valSampler.AddMember("interpolation", "STEP", w.mAl); break; case Interpolation_CUBICSPLINE: - valSampler.AddMember("path", "CUBICSPLINE", w.mAl); + valSampler.AddMember("interpolation", "CUBICSPLINE", w.mAl); break; } valSampler.AddMember("output", s.output->index, w.mAl); @@ -209,7 +209,7 @@ namespace glTF2 { if (bv.byteStride != 0) { obj.AddMember("byteStride", bv.byteStride, w.mAl); } - if (bv.target != 0) { + if (bv.target != BufferViewTarget_NONE) { obj.AddMember("target", int(bv.target), w.mAl); } } diff --git a/code/glTF2/glTF2Exporter.cpp b/code/glTF2/glTF2Exporter.cpp index 1bfee4491..35dac57e6 100644 --- a/code/glTF2/glTF2Exporter.cpp +++ b/code/glTF2/glTF2Exporter.cpp @@ -141,10 +141,7 @@ static void CopyValue(const aiMatrix4x4& v, mat4& o) { } static void CopyValue(const aiMatrix4x4& v, aiMatrix4x4& o) { - o.a1 = v.a1; o.a2 = v.a2; o.a3 = v.a3; o.a4 = v.a4; - o.b1 = v.b1; o.b2 = v.b2; o.b3 = v.b3; o.b4 = v.b4; - o.c1 = v.c1; o.c2 = v.c2; o.c3 = v.c3; o.c4 = v.c4; - o.d1 = v.d1; o.d2 = v.d2; o.d3 = v.d3; o.d4 = v.d4; + memcpy(&o, &v, sizeof(aiMatrix4x4)); } static void IdentityMatrix4(mat4& o) { @@ -211,7 +208,7 @@ inline void SetAccessorRange(ComponentType compType, Ref acc, void* da } inline Ref ExportData(Asset& a, std::string& meshName, Ref& buffer, - size_t count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false) + size_t count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, BufferViewTarget target = BufferViewTarget_NONE) { if (!count || !data) { return Ref(); @@ -234,7 +231,7 @@ inline Ref ExportData(Asset& a, std::string& meshName, Ref& bu bv->byteOffset = offset; bv->byteLength = length; //! The target that the WebGL buffer should be bound to. bv->byteStride = 0; - bv->target = isIndices ? BufferViewTarget_ELEMENT_ARRAY_BUFFER : BufferViewTarget_ARRAY_BUFFER; + bv->target = target; // accessor Ref acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor")); @@ -747,7 +744,7 @@ void glTF2Exporter::ExportMeshes() p.material = mAsset->materials.Get(aim->mMaterialIndex); /******************* Vertices ********************/ - Ref v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); + Ref v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER); if (v) p.attributes.position.push_back(v); /******************** Normals ********************/ @@ -758,7 +755,7 @@ void glTF2Exporter::ExportMeshes() } } - Ref n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); + Ref n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER); if (n) p.attributes.normal.push_back(n); /************** Texture coordinates **************/ @@ -776,14 +773,14 @@ void glTF2Exporter::ExportMeshes() if (aim->mNumUVComponents[i] > 0) { AttribType::Value type = (aim->mNumUVComponents[i] == 2) ? AttribType::VEC2 : AttribType::VEC3; - Ref tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, false); + Ref tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER); if (tc) p.attributes.texcoord.push_back(tc); } } /*************** Vertex colors ****************/ for (unsigned int indexColorChannel = 0; indexColorChannel < aim->GetNumColorChannels(); ++indexColorChannel) { - Ref c = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mColors[indexColorChannel], AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT, false); + Ref c = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mColors[indexColorChannel], AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER); if (c) p.attributes.color.push_back(c); } @@ -799,7 +796,7 @@ void glTF2Exporter::ExportMeshes() } } - p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_INT, true); + p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_INT, BufferViewTarget_ELEMENT_ARRAY_BUFFER); } switch (aim->mPrimitiveTypes) { @@ -956,8 +953,27 @@ unsigned int glTF2Exporter::ExportNode(const aiNode* n, Ref& parent) node->name = name; if (!n->mTransformation.IsIdentity()) { - node->matrix.isPresent = true; - CopyValue(n->mTransformation, node->matrix.value); + if (mScene->mNumAnimations > 0) { + aiQuaternion quaternion; + n->mTransformation.Decompose(*reinterpret_cast(&node->scale.value), quaternion, *reinterpret_cast(&node->translation.value)); + + aiVector3D vector(static_cast(1.0f), static_cast(1.0f), static_cast(1.0f)); + if (!reinterpret_cast(&node->scale.value)->Equal(vector)) { + node->scale.isPresent = true; + } + if (!reinterpret_cast(&node->translation.value)->Equal(vector)) { + node->translation.isPresent = true; + } + node->rotation.isPresent = true; + node->rotation.value[0] = quaternion.x; + node->rotation.value[1] = quaternion.y; + node->rotation.value[2] = quaternion.z; + node->rotation.value[3] = quaternion.w; + node->matrix.isPresent = false; + } else { + node->matrix.isPresent = true; + CopyValue(n->mTransformation, node->matrix.value); + } } for (unsigned int i = 0; i < n->mNumMeshes; ++i) {