Merge pull request #16444 from sabrina-shanman/instancing_slimmesh

Instancing: Calculate shape extents using existing extents in simple mesh
This commit is contained in:
Sam Gateau 2019-11-01 16:50:21 -07:00 committed by GitHub
commit e19a46f14b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 32 deletions

View file

@ -1600,12 +1600,10 @@ HFMModel* FBXSerializer::extractHFMModel(const hifi::VariantHash& mapping, const
cluster.jointIndex = transformIndex; cluster.jointIndex = transformIndex;
clusters.push_back(cluster); clusters.push_back(cluster);
std::vector<hfm::SkinCluster> skinClusters;
// Skinned mesh instances have an hfm::SkinDeformer // Skinned mesh instances have an hfm::SkinDeformer
skinDeformer.skinClusterIndices.reserve(clusterIDs.size()); std::vector<hfm::SkinCluster> skinClusters;
for (const auto& clusterID : clusterIDs) { for (const auto& clusterID : clusterIDs) {
const Cluster& fbxCluster = fbxClusters[clusterID]; const Cluster& fbxCluster = fbxClusters[clusterID];
skinDeformer.skinClusterIndices.emplace_back();
skinClusters.emplace_back(); skinClusters.emplace_back();
hfm::SkinCluster& skinCluster = skinClusters.back(); hfm::SkinCluster& skinCluster = skinClusters.back();
size_t indexWeightPairs = (size_t)std::min(fbxCluster.indices.size(), fbxCluster.weights.size()); size_t indexWeightPairs = (size_t)std::min(fbxCluster.indices.size(), fbxCluster.weights.size());

View file

@ -235,6 +235,7 @@ struct TriangleListMesh {
std::vector<glm::vec3> vertices; std::vector<glm::vec3> vertices;
std::vector<uint32_t> indices; std::vector<uint32_t> indices;
std::vector<glm::ivec2> parts; // Offset in the indices, Number of indices std::vector<glm::ivec2> parts; // Offset in the indices, Number of indices
std::vector<Extents> partExtents; // Extents of each part with no transform applied. Same length as parts.
}; };
/// A single mesh (with optional blendshapes). /// A single mesh (with optional blendshapes).
@ -250,7 +251,6 @@ public:
QVector<glm::vec2> texCoords; QVector<glm::vec2> texCoords;
QVector<glm::vec2> texCoords1; QVector<glm::vec2> texCoords1;
QVector<Cluster> clusters; // DEPRECATED (see hfm::Shape::dynamicTransform, hfm::DynamicTransform::clusters)
Extents meshExtents; // DEPRECATED (see hfm::Shape::transformedExtents) Extents meshExtents; // DEPRECATED (see hfm::Shape::transformedExtents)
glm::mat4 modelTransform; // DEPRECATED (see hfm::Joint::globalTransform, hfm::Shape::transform, hfm::Model::joints) glm::mat4 modelTransform; // DEPRECATED (see hfm::Joint::globalTransform, hfm::Shape::transform, hfm::Model::joints)
@ -316,7 +316,6 @@ public:
class SkinDeformer { class SkinDeformer {
public: public:
std::vector<uint16_t> skinClusterIndices; // DEPRECATED (see hfm::Mesh.clusterIndices, hfm::Mesh.clusterWeights)
std::vector<Cluster> clusters; std::vector<Cluster> clusters;
}; };

View file

@ -40,22 +40,30 @@ void thickenFlatExtents(Extents& extents) {
extents.maximum += glm::vec3(EPSILON, EPSILON, EPSILON); extents.maximum += glm::vec3(EPSILON, EPSILON, EPSILON);
} }
void calculateExtentsForShape(hfm::Shape& shape, const std::vector<hfm::Mesh>& meshes, const std::vector<hfm::Joint> joints) { void calculateExtentsForTriangleListMesh(TriangleListMesh& triangleListMesh) {
triangleListMesh.partExtents.resize(triangleListMesh.parts.size());
for (size_t partIndex = 0; partIndex < triangleListMesh.parts.size(); ++partIndex) {
const auto& part = triangleListMesh.parts[partIndex];
auto& extents = triangleListMesh.partExtents[partIndex];
int partEnd = part.x + part.y;
for (int i = part.x; i < partEnd; ++i) {
auto index = triangleListMesh.indices[i];
const auto& position = triangleListMesh.vertices[index];
extents.addPoint(position);
}
}
}
void calculateExtentsForShape(hfm::Shape& shape, const std::vector<hfm::TriangleListMesh>& triangleListMeshes, const std::vector<hfm::Joint>& joints) {
auto& shapeExtents = shape.transformedExtents; auto& shapeExtents = shape.transformedExtents;
shapeExtents.reset(); shapeExtents.reset();
const auto& mesh = meshes[shape.mesh]; const auto& triangleListMesh = triangleListMeshes[shape.mesh];
const auto& meshPart = mesh.parts[shape.meshPart]; const auto& partExtent = triangleListMesh.partExtents[shape.meshPart];
glm::mat4 transform = joints[shape.joint].transform; const glm::mat4& transform = joints[shape.joint].transform;
forEachIndex(meshPart, [&](int32_t idx){ shapeExtents = partExtent;
if (mesh.vertices.size() <= idx) { shapeExtents.transform(transform);
return;
}
const glm::vec3& vertex = mesh.vertices[idx];
const glm::vec3 transformedVertex = glm::vec3(transform * glm::vec4(vertex, 1.0f));
shapeExtents.addPoint(transformedVertex);
});
thickenFlatExtents(shapeExtents); thickenFlatExtents(shapeExtents);
} }
@ -196,6 +204,8 @@ const TriangleListMesh generateTriangleListMesh(const std::vector<glm::vec3>& sr
} }
} }
calculateExtentsForTriangleListMesh(dest);
return dest; return dest;
} }

View file

@ -20,8 +20,10 @@ void forEachIndex(const hfm::MeshPart& meshPart, std::function<void(uint32_t)> f
void initializeExtents(Extents& extents); void initializeExtents(Extents& extents);
void calculateExtentsForTriangleListMesh(TriangleListMesh& triangleListMesh);
// This can't be moved to model-baker until // This can't be moved to model-baker until
void calculateExtentsForShape(hfm::Shape& shape, const std::vector<hfm::Mesh>& meshes, const std::vector<hfm::Joint> joints); void calculateExtentsForShape(hfm::Shape& shape, const std::vector<hfm::TriangleListMesh>& triangleListMeshes, const std::vector<hfm::Joint>& joints);
void calculateExtentsForModel(Extents& modelExtents, const std::vector<hfm::Shape>& shapes); void calculateExtentsForModel(Extents& modelExtents, const std::vector<hfm::Shape>& shapes);

View file

@ -20,7 +20,7 @@
#include "CalculateBlendshapeNormalsTask.h" #include "CalculateBlendshapeNormalsTask.h"
#include "CalculateBlendshapeTangentsTask.h" #include "CalculateBlendshapeTangentsTask.h"
#include "PrepareJointsTask.h" #include "PrepareJointsTask.h"
#include "CalculateExtentsTask.h" #include "CalculateTransformedExtentsTask.h"
#include "BuildDracoMeshTask.h" #include "BuildDracoMeshTask.h"
#include "ParseFlowDataTask.h" #include "ParseFlowDataTask.h"
#include <hfm/HFMModelMath.h> #include <hfm/HFMModelMath.h>
@ -200,10 +200,10 @@ namespace baker {
const auto jointIndices = jointInfoOut.getN<PrepareJointsTask::Output>(2); const auto jointIndices = jointInfoOut.getN<PrepareJointsTask::Output>(2);
// Use transform information to compute extents // Use transform information to compute extents
const auto calculateExtentsInputs = CalculateExtentsTask::Input(modelExtentsIn, meshesIn, shapesIn, jointsOut).asVarying(); const auto calculateExtentsInputs = CalculateTransformedExtentsTask::Input(modelExtentsIn, triangleListMeshes, shapesIn, jointsOut).asVarying();
const auto calculateExtentsOutputs = model.addJob<CalculateExtentsTask>("CalculateExtents", calculateExtentsInputs); const auto calculateExtentsOutputs = model.addJob<CalculateTransformedExtentsTask>("CalculateExtents", calculateExtentsInputs);
const auto modelExtentsOut = calculateExtentsOutputs.getN<CalculateExtentsTask::Output>(0); const auto modelExtentsOut = calculateExtentsOutputs.getN<CalculateTransformedExtentsTask::Output>(0);
const auto shapesOut = calculateExtentsOutputs.getN<CalculateExtentsTask::Output>(1); const auto shapesOut = calculateExtentsOutputs.getN<CalculateTransformedExtentsTask::Output>(1);
// Parse material mapping // Parse material mapping
const auto parseMaterialMappingInputs = ParseMaterialMappingTask::Input(mapping, materialMappingBaseURL).asVarying(); const auto parseMaterialMappingInputs = ParseMaterialMappingTask::Input(mapping, materialMappingBaseURL).asVarying();

View file

@ -1,5 +1,5 @@
// //
// CalculateExtentsTask.cpp // CalculateTransformedExtentsTask.cpp
// model-baker/src/model-baker // model-baker/src/model-baker
// //
// Created by Sabrina Shanman on 2019/10/04. // Created by Sabrina Shanman on 2019/10/04.
@ -9,13 +9,13 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
// //
#include "CalculateExtentsTask.h" #include "CalculateTransformedExtentsTask.h"
#include "hfm/HFMModelMath.h" #include "hfm/HFMModelMath.h"
void CalculateExtentsTask::run(const baker::BakeContextPointer& context, const Input& input, Output& output) { void CalculateTransformedExtentsTask::run(const baker::BakeContextPointer& context, const Input& input, Output& output) {
const auto& modelExtentsIn = input.get0(); const auto& modelExtentsIn = input.get0();
const auto& meshes = input.get1(); const auto& triangleListMeshes = input.get1();
const auto& shapesIn = input.get2(); const auto& shapesIn = input.get2();
const auto& joints = input.get3(); const auto& joints = input.get3();
auto& modelExtentsOut = output.edit0(); auto& modelExtentsOut = output.edit0();
@ -31,7 +31,7 @@ void CalculateExtentsTask::run(const baker::BakeContextPointer& context, const I
continue; continue;
} }
hfm::calculateExtentsForShape(shapeOut, meshes, joints); hfm::calculateExtentsForShape(shapeOut, triangleListMeshes, joints);
} }
modelExtentsOut = modelExtentsIn; modelExtentsOut = modelExtentsIn;

View file

@ -1,5 +1,5 @@
// //
// CalculateExtentsTask.h // CalculateTransformedExtentsTask.h
// model-baker/src/model-baker // model-baker/src/model-baker
// //
// Created by Sabrina Shanman on 2019/10/04. // Created by Sabrina Shanman on 2019/10/04.
@ -17,11 +17,11 @@
// Calculates any undefined extents in the shapes and the model. Precalculated extents will be left alone. // Calculates any undefined extents in the shapes and the model. Precalculated extents will be left alone.
// Bind extents will currently not be calculated // Bind extents will currently not be calculated
class CalculateExtentsTask { class CalculateTransformedExtentsTask {
public: public:
using Input = baker::VaryingSet4<Extents, std::vector<hfm::Mesh>, std::vector<hfm::Shape>, std::vector<hfm::Joint>>; using Input = baker::VaryingSet4<Extents, std::vector<hfm::TriangleListMesh>, std::vector<hfm::Shape>, std::vector<hfm::Joint>>;
using Output = baker::VaryingSet2<Extents, std::vector<hfm::Shape>>; using Output = baker::VaryingSet2<Extents, std::vector<hfm::Shape>>;
using JobModel = baker::Job::ModelIO<CalculateExtentsTask, Input, Output>; using JobModel = baker::Job::ModelIO<CalculateTransformedExtentsTask, Input, Output>;
void run(const baker::BakeContextPointer& context, const Input& input, Output& output); void run(const baker::BakeContextPointer& context, const Input& input, Output& output);
}; };