Fix: GLTF animation works on RTS not matrix; fix matrix related bug.

pull/2995/head
Hui.Du 2020-02-10 02:03:26 +00:00
parent a4868a9530
commit b62bd38c71
6 changed files with 46 additions and 30 deletions

View File

@ -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
};

View File

@ -203,7 +203,9 @@ namespace glTF {
obj.AddMember("buffer", Value(bv.buffer->id, w.mAl).Move(), w.mAl);
obj.AddMember("byteOffset", static_cast<uint64_t>(bv.byteOffset), w.mAl);
obj.AddMember("byteLength", static_cast<uint64_t>(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*/)

View File

@ -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<Accessor> acc, void* da
}
}
inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& buffer,
unsigned int count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false)
{
inline Ref<Accessor> ExportData(Asset &a, std::string &meshName, Ref<Buffer> &buffer,
unsigned int count, void *data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, BufferViewTarget target = BufferViewTarget_NONE) {
if (!count || !data) return Ref<Accessor>();
unsigned int numCompsIn = AttribType::GetNumComponents(typeIn);
@ -251,7 +247,7 @@ inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& 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<Accessor> 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<Accessor> v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
Ref<Accessor> 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<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
Ref<Accessor> 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<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, false);
Ref<Accessor> 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) {

View File

@ -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
};

View File

@ -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);
}
}

View File

@ -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<Accessor> acc, void* da
}
inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& 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<Accessor>();
@ -234,7 +231,7 @@ inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& 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<Accessor> acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor"));
@ -747,7 +744,7 @@ void glTF2Exporter::ExportMeshes()
p.material = mAsset->materials.Get(aim->mMaterialIndex);
/******************* Vertices ********************/
Ref<Accessor> v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
Ref<Accessor> 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<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
Ref<Accessor> 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<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, false);
Ref<Accessor> 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<Accessor> c = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mColors[indexColorChannel], AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT, false);
Ref<Accessor> 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<Node>& 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<aiVector3D *>(&node->scale.value), quaternion, *reinterpret_cast<aiVector3D *>(&node->translation.value));
aiVector3D vector(static_cast<ai_real>(1.0f), static_cast<ai_real>(1.0f), static_cast<ai_real>(1.0f));
if (!reinterpret_cast<aiVector3D *>(&node->scale.value)->Equal(vector)) {
node->scale.isPresent = true;
}
if (!reinterpret_cast<aiVector3D *>(&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) {