Much progress with morph targets.

Everything's in place to drop the progressive morph calculations into
readAnimations(). Shouldn't be hard.

Normals are proving trickier. I'm not convinced they make it into the
FBX at all. The SDK reports the existence of normals in the primary
layer, but they seems to consist exclusively of zeroes.

We may have to compute them.
This commit is contained in:
Par Winzell 2017-10-29 22:25:14 -07:00
parent 77470e236e
commit 91a783dc2d
6 changed files with 260 additions and 96 deletions

View File

@ -449,89 +449,172 @@ private:
std::vector<Vec4f> vertexJointWeights; std::vector<Vec4f> vertexJointWeights;
}; };
/**
* At the FBX level, each Mesh can have a set of FbxBlendShape deformers; organisational units that contain no data
* of their own. The actual deformation is determined by one or more FbxBlendShapeChannels, whose influences are all
* additively applied to the mesh. In a simpler world, each such channel would extend each base vertex with alternate
* position, and optionally normal and tangent.
*
* It's not quite so simple, though. We also have progressive morphing, where one logical morph actually consists of
* several concrete ones, each applied in sequence. For us, this means each channel contains a sequence of FbxShapes
* (aka target shape); these are the actual data-holding entities that provide the alternate vertex attributes. As such
* a channel is given more weight, it moves from one target shape to another.
*
* The total number of alternate sets of attributes, then, is the total number of target shapes across all the channels
* of all the blend shapes of the mesh.
*
* Each animation in the scene stack can yield one or zero FbxAnimCurves per channel (not target shape). We evaluate
* these curves to get the weight of the channel: this weight is further introspected on to figure out which target
* shapes we're currently interpolation between.
*/
class FbxBlendShapesAccess class FbxBlendShapesAccess
{ {
public: public:
struct Shape { struct BlendChannel {
explicit Shape(const std::vector<FbxVector4> &offsets, FbxFloat defaultDeform, const std::vector<FbxAnimCurve *>animations) : explicit BlendChannel(FbxDouble defaultDeform) :
offsets(offsets), defaultDeform(defaultDeform)
defaultDeform(defaultDeform),
animations(animations)
{} {}
const std::vector<FbxVector4> offsets; struct TargetShape {
const FbxDouble defaultDeform; TargetShape(
const std::vector<FbxAnimCurve *> animations; double fullWeight,
const std::vector<FbxVector4> &positions,
const std::vector<FbxVector4> &normals,
const std::vector<FbxVector4> &tangents
) : fullWeight(fullWeight),
positions(positions),
normals(normals),
tangents(tangents)
{}
const double fullWeight;
const std::vector<FbxVector4> positions;
const std::vector<FbxVector4> normals;
const std::vector<FbxVector4> tangents;
};
const FbxDouble defaultDeform;
// one for each FbxShape
std::vector<const TargetShape> targetShapes {};
// always the size of the scene's animation stack; can and will contain nulls
std::vector<FbxAnimCurve *> animations {};
}; };
explicit FbxBlendShapesAccess(const FbxScene *scene, FbxMesh *mesh) : explicit FbxBlendShapesAccess(const FbxScene *scene, FbxMesh *mesh) :
shapes(extractShapes(scene, mesh)) channels(extractChannels(scene, mesh))
{ } { }
size_t GetShapeCount() const { return shapes.size(); } size_t GetShapeCount() const { return channels.size(); }
FbxDouble GetDefaultDeform(size_t shapeIx) const { FbxDouble GetDefaultDeform(size_t channelIx) const {
return shapes.at(shapeIx).defaultDeform; return channels.at(channelIx).defaultDeform;
} }
FbxVector4 GetPosition(size_t shapeIx, size_t vertexIx) const { size_t GetTargetShapeCount(size_t channelIx) const { return channels[channelIx].targetShapes.size(); }
return shapes.at(shapeIx).offsets[vertexIx]; const BlendChannel::TargetShape &GetTargetShape(size_t channelIx, size_t targetShapeIx) const {
return channels.at(channelIx).targetShapes[targetShapeIx];
} }
size_t GetAnimCount(size_t shapeIx) const { return shapes.at(shapeIx).animations.size(); } size_t GetAnimCount(size_t channelIx) const { return channels.at(channelIx).animations.size(); }
FbxAnimCurve * GetAnimation(size_t shapeIx, size_t animIx) const { FbxAnimCurve *GetAnimation(size_t channelIx, size_t animIx) const {
return shapes.at(shapeIx).animations[animIx]; return channels.at(channelIx).animations[animIx];
} }
private: private:
std::vector<Shape> extractShapes(const FbxScene *scene, FbxMesh *mesh) const { std::vector<BlendChannel> extractChannels(const FbxScene *scene, FbxMesh *mesh) const {
std::vector<Shape> results;
// acquire the regular control points from the mesh // acquire the regular control points from the mesh
const int controlPointsCount = mesh->GetControlPointsCount();
const FbxVector4 *meshPoints = mesh->GetControlPoints(); const FbxVector4 *meshPoints = mesh->GetControlPoints();
// acquire the normals, if they're present & make sure they're well-formed
FbxLayerElementArrayTemplate<FbxVector4>* meshNormals = nullptr;
if (!mesh->GetNormals(&meshNormals) || meshNormals->GetCount() != controlPointsCount) {
meshNormals = nullptr;
}
// same, but tangents
FbxLayerElementArrayTemplate<FbxVector4>* meshTangents = nullptr;
if (!mesh->GetTangents(&meshTangents) || meshTangents->GetCount() != controlPointsCount) {
meshTangents = nullptr;
}
std::vector<BlendChannel> results;
for (int shapeIx = 0; shapeIx < mesh->GetDeformerCount(FbxDeformer::eBlendShape); shapeIx++) { for (int shapeIx = 0; shapeIx < mesh->GetDeformerCount(FbxDeformer::eBlendShape); shapeIx++) {
auto *blendShape = dynamic_cast<FbxBlendShape *>(mesh->GetDeformer(shapeIx, FbxDeformer::eBlendShape)); auto *fbxBlendShape = dynamic_cast<FbxBlendShape *>(mesh->GetDeformer(shapeIx, FbxDeformer::eBlendShape));
if (blendShape == nullptr) { if (fbxBlendShape == nullptr) {
continue; continue;
} }
for (int channelIx = 0; channelIx < blendShape->GetBlendShapeChannelCount(); ++channelIx) { for (int channelIx = 0; channelIx < fbxBlendShape->GetBlendShapeChannelCount(); ++channelIx) {
FbxBlendShapeChannel *channel = blendShape->GetBlendShapeChannel(channelIx); FbxBlendShapeChannel *channel = fbxBlendShape->GetBlendShapeChannel(channelIx);
unsigned int targetShapeCount = static_cast<unsigned int>(channel->GetTargetShapeCount());
if (channel->GetTargetShapeCount() != 1) { if (targetShapeCount < 1) {
if (channel->GetTargetShapeCount() > 1) {
fmt::print("Warning: Blend Shape %s is a progressive morph; this is not supported.\n", channel->GetName());
}
continue; continue;
} }
FbxShape *fbxShape = channel->GetTargetShape(0); BlendChannel shape(channel->DeformPercent * 0.01f);
assert(fbxShape->GetControlPointsCount() == mesh->GetControlPointsCount());
// glTF morph target positions are *mutation vectors* to be added (by weight) to the regular mesh positions. std::vector<std::pair<double, FbxShape *>> targetShapes (targetShapeCount);
// FBX blend shape control points, on the other hand are final positions. double *fullWeights = channel->GetTargetShapeFullWeights();
// So we must do a little subtracting. for (int targetShapeIx = 0; targetShapeIx < targetShapes.size(); targetShapeIx++) {
std::vector<FbxVector4> offsets; FbxShape *fbxShape = channel->GetTargetShape(targetShapeIx);
auto shapePoints = fbxShape->GetControlPoints(); assert(fbxShape->GetControlPointsCount() == controlPointsCount);
for (int pointIx = 0; pointIx < mesh->GetControlPointsCount(); pointIx ++) {
offsets.push_back(shapePoints[pointIx] - meshPoints[pointIx]); // glTF morph target positions are *mutation vectors* to be added (by weight) to the regular mesh positions.
// FBX blend shape control points, on the other hand are final positions.
// So we must do a little subtracting.
std::vector<FbxVector4> positions, normals, tangents;
// fetch positions
const FbxVector4 *shapePoints = fbxShape->GetControlPoints();
for (int pointIx = 0; pointIx < controlPointsCount; pointIx ++) {
positions.push_back(shapePoints[pointIx] - meshPoints[pointIx]);
}
// maybe fetch normals
if (meshNormals) {
FbxLayerElementArrayTemplate<FbxVector4>* fbxNormals = nullptr;
if (fbxShape->GetNormals(&fbxNormals)) {
for (int pointIx = 0; pointIx < controlPointsCount; pointIx ++) {
normals.push_back(fbxNormals->GetAt(pointIx) - meshNormals->GetAt(pointIx));
}
}
}
// maybe fetch tangents
if (meshTangents) {
FbxLayerElementArrayTemplate<FbxVector4>* fbxTangents = nullptr;
if (fbxShape->GetTangents(&fbxTangents)) {
for (int pointIx = 0; pointIx < controlPointsCount; pointIx ++) {
tangents.push_back(fbxTangents->GetAt(pointIx) - meshTangents->GetAt(pointIx));
}
}
}
// finally combine all this into a TargetShape and add it to our work-in-progress BlendChannel
shape.targetShapes.push_back(
BlendChannel::TargetShape(fullWeights[targetShapeIx], positions, normals, tangents)
);
} }
// go through all the animations in the scene and figure out their relevance to this mesh
size_t animationCount = static_cast<size_t>(scene->GetSrcObjectCount<FbxAnimStack>()); size_t animationCount = static_cast<size_t>(scene->GetSrcObjectCount<FbxAnimStack>());
std::vector<FbxAnimCurve *>animations(animationCount); std::vector<FbxAnimCurve *>animations(animationCount);
for (int j = 0; j < animationCount; j++) { for (int animIx = 0; animIx < animationCount; animIx++) {
auto *pAnimStack = scene->GetSrcObject<FbxAnimStack>(j); auto *pAnimStack = scene->GetSrcObject<FbxAnimStack>(animIx);
auto *layer = pAnimStack->GetMember<FbxAnimLayer>(0); auto *layer = pAnimStack->GetMember<FbxAnimLayer>(0);
if (pAnimStack->GetMemberCount() > 1) { if (pAnimStack->GetMemberCount() > 1) {
fmt::print("Warning: ignoring animation layers 1+ in stack %s", pAnimStack->GetName()); fmt::print("Warning: ignoring animation layers 1+ in stack %s", pAnimStack->GetName());
} }
FbxAnimCurve *curve = mesh->GetShapeChannel(shapeIx, channelIx, layer, true); // note that some of these will be null here, which is fine; the critical part is that the
// note that curve can be null here, which is fine // indices maintain parity with the scene-wide animation stack
animations[j] = curve; shape.animations.push_back(mesh->GetShapeChannel(shapeIx, channelIx, layer, true));
} }
results.push_back(Shape(offsets, channel->DeformPercent * 0.01f, animations)); results.push_back(shape);
} }
} }
return results; return results;
} }
const std::vector<Shape> shapes; const std::vector<BlendChannel> channels;
}; };
static bool TriangleTexturePolarity(const Vec2f &uv0, const Vec2f &uv1, const Vec2f &uv2) static bool TriangleTexturePolarity(const Vec2f &uv0, const Vec2f &uv1, const Vec2f &uv2)
@ -627,9 +710,17 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std:
rawSurface.jointGeometryMaxs.emplace_back(-FLT_MAX, -FLT_MAX, -FLT_MAX); rawSurface.jointGeometryMaxs.emplace_back(-FLT_MAX, -FLT_MAX, -FLT_MAX);
} }
rawSurface.defaultShapeDeforms.clear(); rawSurface.blendChannels.clear();
for (int shapeIx = 0; shapeIx < blendShapes.GetShapeCount(); shapeIx ++) { for (size_t shapeIx = 0; shapeIx < blendShapes.GetShapeCount(); shapeIx ++) {
rawSurface.defaultShapeDeforms.push_back(blendShapes.GetDefaultDeform(shapeIx)); for (size_t targetIx = 0; targetIx < blendShapes.GetTargetShapeCount(shapeIx); targetIx ++) {
const auto &shape = blendShapes.GetTargetShape(shapeIx, targetIx);
float defaultDeform = static_cast<float>(blendShapes.GetDefaultDeform(shapeIx));
rawSurface.blendChannels.push_back(RawBlendChannel {
defaultDeform,
!shape.normals.empty(),
!shape.tangents.empty()
});
}
} }
int polygonVertexIndex = 0; int polygonVertexIndex = 0;
@ -732,10 +823,21 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std:
rawSurface.bounds.AddPoint(vertex.position); rawSurface.bounds.AddPoint(vertex.position);
for (int shapeIx = 0; shapeIx < blendShapes.GetShapeCount(); shapeIx ++) { for (size_t shapeIx = 0; shapeIx < blendShapes.GetShapeCount(); shapeIx ++) {
// extract this vertex' position in this blend shape, transform it as per above, and add it to the vector for (size_t targetIx = 0; targetIx < blendShapes.GetTargetShapeCount(shapeIx); targetIx ++) {
const Vec3f rawPos = toVec3(transform.MultNormalize(blendShapes.GetPosition(shapeIx, controlPointIndex))); const auto &shape = blendShapes.GetTargetShape(shapeIx, targetIx);
vertex.shapePositions.push_back(rawPos);
RawBlendVertex blendVertex;
// the morph target positions must be transformed just as with the vertex positions above
blendVertex.position = toVec3(transform.MultNormalize(shape.positions[controlPointIndex]));
if (!shape.normals.empty()) {
blendVertex.normal = toVec3(shape.normals[controlPointIndex]);
}
if (!shape.tangents.empty()) {
blendVertex.tangent = toVec4(shape.tangents[controlPointIndex]);
}
vertex.blends.push_back(blendVertex);
}
} }
if (skinning.IsSkinned()) { if (skinning.IsSkinned()) {
@ -943,12 +1045,12 @@ static void ReadAnimations(RawModel &raw, FbxScene *pScene)
{ {
FbxTime::EMode eMode = FbxTime::eFrames24; FbxTime::EMode eMode = FbxTime::eFrames24;
const int animationCount = pScene->GetSrcObjectCount<FbxAnimStack>(); const int animationCount = pScene->GetSrcObjectCount<FbxAnimStack>();
for (int animIx = 0; animIx < animationCount; animIx++) { for (size_t animIx = 0; animIx < animationCount; animIx++) {
FbxAnimStack *pAnimStack = pScene->GetSrcObject<FbxAnimStack>(animIx); FbxAnimStack *pAnimStack = pScene->GetSrcObject<FbxAnimStack>(animIx);
FbxString animStackName = pAnimStack->GetName(); FbxString animStackName = pAnimStack->GetName();
if (verboseOutput) { if (verboseOutput) {
fmt::printf("animation %d: %s (%d%%)", animIx, (const char *) animStackName, 0); fmt::printf("animation %zu: %s (%d%%)", animIx, (const char *) animStackName, 0);
} }
pScene->SetCurrentAnimationStack(pAnimStack); pScene->SetCurrentAnimationStack(pAnimStack);
@ -991,8 +1093,8 @@ static void ReadAnimations(RawModel &raw, FbxScene *pScene)
if (nodeAttr != nullptr && nodeAttr->GetAttributeType() == FbxNodeAttribute::EType::eMesh) { if (nodeAttr != nullptr && nodeAttr->GetAttributeType() == FbxNodeAttribute::EType::eMesh) {
// it's inelegant to recreate this same access class multiple times, but it's also dirt cheap... // it's inelegant to recreate this same access class multiple times, but it's also dirt cheap...
FbxBlendShapesAccess blendShapes(pScene, dynamic_cast<FbxMesh *>(nodeAttr)); FbxBlendShapesAccess blendShapes(pScene, dynamic_cast<FbxMesh *>(nodeAttr));
for (size_t shapeIx = 0; shapeIx < blendShapes.GetShapeCount(); shapeIx ++) { for (size_t channelIx = 0; channelIx < blendShapes.GetShapeCount(); channelIx ++) {
shapeAnimCurves.push_back(blendShapes.GetAnimation(shapeIx, animIx)); shapeAnimCurves.push_back(blendShapes.GetAnimation(channelIx, animIx));
} }
} }
for (FbxLongLong frameIndex = firstFrameIndex; frameIndex <= lastFrameIndex; frameIndex++) { for (FbxLongLong frameIndex = firstFrameIndex; frameIndex <= lastFrameIndex; frameIndex++) {
@ -1024,8 +1126,11 @@ static void ReadAnimations(RawModel &raw, FbxScene *pScene)
channel.scales.push_back(toVec3f(localScale)); channel.scales.push_back(toVec3f(localScale));
for (auto curve : shapeAnimCurves) { for (auto curve : shapeAnimCurves) {
float weight = curve->Evaluate(pTime) * 0.01; // [0, 100] in FBX, [0, 1] in glTF float weight = 0f;
hasMorphs |= (fabs(weight) > epsilon); if (curve) {
weight = curve->Evaluate(pTime) * 0.01f; // [0, 100] in FBX, [0, 1] in glTF
hasMorphs |= (fabs(weight) > epsilon);
}
channel.weights.push_back(weight); channel.weights.push_back(weight);
} }
} }

View File

@ -499,7 +499,11 @@ ModelData *Raw2Gltf(
mesh = meshIter->second.get(); mesh = meshIter->second.get();
} else { } else {
auto meshPtr = gltf->meshes.hold(new MeshData(rawSurface.name, rawSurface.defaultShapeDeforms)); std::vector<float> defaultDeforms;
for (const auto &channel : rawSurface.blendChannels) {
defaultDeforms.push_back(channel.defaultDeform);
}
auto meshPtr = gltf->meshes.hold(new MeshData(rawSurface.name, defaultDeforms));
meshByNodeName[nodeName] = meshPtr; meshByNodeName[nodeName] = meshPtr;
meshNode.SetMesh(meshPtr->ix); meshNode.SetMesh(meshPtr->ix);
mesh = meshPtr.get(); mesh = meshPtr.get();
@ -568,21 +572,6 @@ ModelData *Raw2Gltf(
accessor->min = toStdVec(rawSurface.bounds.min); accessor->min = toStdVec(rawSurface.bounds.min);
accessor->max = toStdVec(rawSurface.bounds.max); accessor->max = toStdVec(rawSurface.bounds.max);
for (int ii = 0; ii < rawSurface.defaultShapeDeforms.size(); ii ++) {
auto bufferView = gltf->GetAlignedBufferView(buffer, BufferViewData::GL_ARRAY_BUFFER);
std::vector<Vec3f> result;
Bounds<float, 3> shapeBounds;
for (int jj = 0; jj < surfaceModel.GetVertexCount(); jj ++) {
const Vec3f &position = surfaceModel.GetVertex(jj).shapePositions[ii];
shapeBounds.AddPoint(position);
result.push_back(position);
}
accessor = gltf->AddAccessorWithView(*bufferView, GLT_VEC3F, result);
accessor->min = toStdVec(shapeBounds.min);
accessor->max = toStdVec(shapeBounds.max);
primitive->AddTarget(*accessor);
}
} }
if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_NORMAL) != 0) { if ((surfaceModel.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_NORMAL) != 0) {
const AttributeDefinition<Vec3f> ATTR_NORMAL("NORMAL", &RawVertex::normal, const AttributeDefinition<Vec3f> ATTR_NORMAL("NORMAL", &RawVertex::normal,
@ -618,6 +607,49 @@ ModelData *Raw2Gltf(
GLT_VEC4F, draco::GeometryAttribute::GENERIC, draco::DT_FLOAT32); GLT_VEC4F, draco::GeometryAttribute::GENERIC, draco::DT_FLOAT32);
gltf->AddAttributeToPrimitive<Vec4f>(buffer, surfaceModel, *primitive, ATTR_WEIGHTS); gltf->AddAttributeToPrimitive<Vec4f>(buffer, surfaceModel, *primitive, ATTR_WEIGHTS);
} }
// each channel present in the mesh always ends up a target in the primitive
for (int channelIx = 0; channelIx < rawSurface.blendChannels.size(); channelIx ++) {
const auto &channel = rawSurface.blendChannels[channelIx];
// track the bounds of each shape channel
Bounds<float, 3> shapeBounds;
std::vector<Vec3f> positions, normals;
std::vector<Vec4f> tangents;
for (int jj = 0; jj < surfaceModel.GetVertexCount(); jj ++) {
auto blendVertex = surfaceModel.GetVertex(jj).blends[channelIx];
shapeBounds.AddPoint(blendVertex.position);
positions.push_back(blendVertex.position);
if (channel.hasNormals) {
normals.push_back(blendVertex.normal);
}
if (channel.hasTangents) {
tangents.push_back(blendVertex.tangent);
}
}
std::shared_ptr<AccessorData> pAcc = gltf->AddAccessorWithView(
*gltf->GetAlignedBufferView(buffer, BufferViewData::GL_ARRAY_BUFFER),
GLT_VEC3F, positions);
pAcc->min = toStdVec(shapeBounds.min);
pAcc->max = toStdVec(shapeBounds.max);
std::shared_ptr<AccessorData> nAcc;
if (channel.hasNormals) {
nAcc = gltf->AddAccessorWithView(
*gltf->GetAlignedBufferView(buffer, BufferViewData::GL_ARRAY_BUFFER),
GLT_VEC3F, normals);
}
std::shared_ptr<AccessorData> tAcc;
if (channel.hasTangents) {
nAcc = gltf->AddAccessorWithView(
*gltf->GetAlignedBufferView(buffer, BufferViewData::GL_ARRAY_BUFFER),
GLT_VEC4F, tangents);
}
primitive->AddTarget(pAcc.get(), nAcc.get(), tAcc.get());
}
} }
if (options.useDraco) { if (options.useDraco) {
// Set up the encoder. // Set up the encoder.

View File

@ -38,7 +38,7 @@ bool RawVertex::operator==(const RawVertex &other) const
(jointIndices == other.jointIndices) && (jointIndices == other.jointIndices) &&
(jointWeights == other.jointWeights) && (jointWeights == other.jointWeights) &&
(polarityUv0 == other.polarityUv0) && (polarityUv0 == other.polarityUv0) &&
(shapePositions == other.shapePositions); (blends == other.blends);
} }
size_t RawVertex::Difference(const RawVertex &other) const size_t RawVertex::Difference(const RawVertex &other) const

View File

@ -28,6 +28,19 @@ enum RawVertexAttribute
RAW_VERTEX_ATTRIBUTE_AUTO = 1 << 31 RAW_VERTEX_ATTRIBUTE_AUTO = 1 << 31
}; };
struct RawBlendVertex
{
Vec3f position {};
Vec3f normal {};
Vec4f tangent {};
bool operator==(const RawBlendVertex &other) const {
return position == other.position &&
normal == other.normal &&
tangent == other.tangent;
}
};
struct RawVertex struct RawVertex
{ {
RawVertex() : RawVertex() :
@ -46,7 +59,9 @@ struct RawVertex
Vec4i jointIndices { 0, 0, 0, 0 }; Vec4i jointIndices { 0, 0, 0, 0 };
Vec4f jointWeights { 0.0f }; Vec4f jointWeights { 0.0f };
std::vector<Vec3f> shapePositions { }; // each vertex can have many alternate positions, normals and tangents -- one set per blend shape target.
// the size of this vector is always identical to the size of RawSurface.blendChannels
std::vector<RawBlendVertex> blends { };
bool polarityUv0; bool polarityUv0;
bool pad1; bool pad1;
@ -158,24 +173,30 @@ struct RawMaterial
int textures[RAW_TEXTURE_USAGE_MAX]; int textures[RAW_TEXTURE_USAGE_MAX];
}; };
struct RawBlendChannel
{
float defaultDeform;
bool hasNormals;
bool hasTangents;
};
struct RawSurface struct RawSurface
{ {
std::string name; // The name of this surface std::string name; // The name of this surface
std::string nodeName; // The node that links to this surface. std::string nodeName; // The node that links to this surface.
std::string skeletonRootName; // The name of the root of the skeleton. std::string skeletonRootName; // The name of the root of the skeleton.
Bounds<float, 3> bounds; Bounds<float, 3> bounds;
std::vector<std::string> jointNames; std::vector<std::string> jointNames;
std::vector<Vec3f> jointGeometryMins; std::vector<Vec3f> jointGeometryMins;
std::vector<Vec3f> jointGeometryMaxs; std::vector<Vec3f> jointGeometryMaxs;
std::vector<Mat4f> inverseBindMatrices; std::vector<Mat4f> inverseBindMatrices;
std::vector<float> defaultShapeDeforms; std::vector<RawBlendChannel> blendChannels;
bool discrete; bool discrete;
bool skinRigid;
}; };
struct RawChannel struct RawChannel
{ {
int nodeIndex; int nodeIndex;
std::vector<Vec3f> translations; std::vector<Vec3f> translations;
std::vector<Quatf> rotations; std::vector<Quatf> rotations;
std::vector<Vec3f> scales; std::vector<Vec3f> scales;

View File

@ -39,9 +39,13 @@ void PrimitiveData::NoteDracoBuffer(const BufferViewData &data)
dracoBufferView = data.ix; dracoBufferView = data.ix;
} }
void PrimitiveData::AddTarget(const AccessorData &positions) void PrimitiveData::AddTarget(const AccessorData *positions, const AccessorData *normals, const AccessorData *tangents)
{ {
targetPositionAccessors.push_back(positions.ix); targetAccessors.push_back({
positions->ix,
normals ? normals->ix : -1,
tangents ? tangents ->ix : -1
});
} }
void to_json(json &j, const PrimitiveData &d) { void to_json(json &j, const PrimitiveData &d) {
@ -53,12 +57,14 @@ void to_json(json &j, const PrimitiveData &d) {
if (d.indices >= 0) { if (d.indices >= 0) {
j["indices"] = d.indices; j["indices"] = d.indices;
} }
if (!d.targetPositionAccessors.empty()) { if (!d.targetAccessors.empty()) {
json targets {}; json targets {};
for (int ii = 0; ii < d.targetPositionAccessors.size(); ii ++) { int pIx, nIx, tIx;
targets.push_back({ for (auto accessor : d.targetAccessors) {
{ "POSITION", d.targetPositionAccessors[ii] } std::tie(pIx, nIx, tIx) = accessor;
}); if (pIx >= 0) { targets.push_back({{ "POSITION", pIx }}); }
if (nIx >= 0) { targets.push_back({{ "NORMAL", nIx }}); }
if (tIx >= 0) { targets.push_back({{ "TANGENT", tIx }}); }
} }
j["targets"] = targets; j["targets"] = targets;
} }

View File

@ -31,7 +31,7 @@ struct PrimitiveData
void AddAttrib(std::string name, const AccessorData &accessor); void AddAttrib(std::string name, const AccessorData &accessor);
void AddTarget(const AccessorData &positions); void AddTarget(const AccessorData *positions, const AccessorData *normals, const AccessorData *tangents);
template<class T> template<class T>
void AddDracoAttrib(const AttributeDefinition<T> attribute, const std::vector<T> &attribArr) void AddDracoAttrib(const AttributeDefinition<T> attribute, const std::vector<T> &attribArr)
@ -61,7 +61,7 @@ struct PrimitiveData
const unsigned int material; const unsigned int material;
const MeshMode mode; const MeshMode mode;
std::vector<int> targetPositionAccessors { }; std::vector<std::tuple<int, int, int>> targetAccessors {};
std::map<std::string, int> attributes; std::map<std::string, int> attributes;
std::map<std::string, int> dracoAttributes; std::map<std::string, int> dracoAttributes;