From 733e5343c86cf4c5604616c6070881232934346c Mon Sep 17 00:00:00 2001 From: Yingying Wang Date: Thu, 12 Mar 2020 12:14:17 -0700 Subject: [PATCH] export blendshape positions/normals in gltf2/glb2 --- code/glTF2/glTF2AssetWriter.inl | 18 +++++++++++++++ code/glTF2/glTF2Exporter.cpp | 41 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/code/glTF2/glTF2AssetWriter.inl b/code/glTF2/glTF2AssetWriter.inl index 8adc20404..8f99ed008 100644 --- a/code/glTF2/glTF2AssetWriter.inl +++ b/code/glTF2/glTF2AssetWriter.inl @@ -446,6 +446,24 @@ namespace glTF2 { WriteAttrs(w, attrs, p.attributes.weight, "WEIGHTS", true); } prim.AddMember("attributes", attrs, w.mAl); + + // targets for blendshapes + if (p.targets.size() > 0) { + Value tjs; + tjs.SetArray(); + tjs.Reserve(unsigned(p.targets.size()), w.mAl); + for (unsigned int t = 0; t < p.targets.size(); ++t) { + Value tj; + tj.SetObject(); + { + WriteAttrs(w, tj, p.targets[t].position, "POSITION"); + WriteAttrs(w, tj, p.targets[t].normal, "NORMAL"); + WriteAttrs(w, tj, p.targets[t].tangent, "TANGENT"); + } + tjs.PushBack(tj, w.mAl); + } + prim.AddMember("targets", tjs, w.mAl); + } } primitives.PushBack(prim, w.mAl); } diff --git a/code/glTF2/glTF2Exporter.cpp b/code/glTF2/glTF2Exporter.cpp index 35dac57e6..7894f8adb 100644 --- a/code/glTF2/glTF2Exporter.cpp +++ b/code/glTF2/glTF2Exporter.cpp @@ -814,6 +814,47 @@ void glTF2Exporter::ExportMeshes() if(aim->HasBones()) { ExportSkin(*mAsset, aim, m, b, skinRef, inverseBindMatricesData); } + + /*************** Targets for blendshapes ****************/ + if (aim->mNumAnimMeshes > 0) { + p.targets.resize(aim->mNumAnimMeshes); + for (unsigned int am = 0; am < aim->mNumAnimMeshes; ++am) { + aiAnimMesh *pAnimMesh = aim->mAnimMeshes[am]; + + // position + if (pAnimMesh->HasPositions()) { + // NOTE: in gltf it is the diff stored + aiVector3D *pPositionDiff = new aiVector3D[pAnimMesh->mNumVertices]; + for (unsigned int vt = 0; vt < pAnimMesh->mNumVertices; ++vt) { + pPositionDiff[vt] = pAnimMesh->mVertices[vt] - aim->mVertices[vt]; + } + Ref v = ExportData(*mAsset, meshId, b, + pAnimMesh->mNumVertices, pPositionDiff, + AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); + if (v) { + p.targets[am].position.push_back(v); + } + delete[] pPositionDiff; + } + + // normal + if (pAnimMesh->HasNormals()) { + aiVector3D *pNormalDiff = new aiVector3D[pAnimMesh->mNumVertices]; + for (unsigned int vt = 0; vt < pAnimMesh->mNumVertices; ++vt) { + pNormalDiff[vt] = pAnimMesh->mNormals[vt] - aim->mNormals[vt]; + } + Ref v = ExportData(*mAsset, meshId, b, + pAnimMesh->mNumVertices, pNormalDiff, + AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT); + if (v) { + p.targets[am].normal.push_back(v); + } + delete[] pNormalDiff; + } + + // tangent? + } + } } //----------------------------------------