2018-09-14 09:45:05 +00:00
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include "riggenerator.h"
|
|
|
|
#include "autorigger.h"
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
RigGenerator::RigGenerator(const Outcome &outcome) :
|
|
|
|
m_outcome(new Outcome(outcome))
|
2018-09-14 09:45:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RigGenerator::~RigGenerator()
|
|
|
|
{
|
2018-10-25 00:19:38 +00:00
|
|
|
delete m_outcome;
|
2018-09-14 09:45:05 +00:00
|
|
|
delete m_resultMesh;
|
|
|
|
delete m_autoRigger;
|
|
|
|
delete m_resultBones;
|
|
|
|
delete m_resultWeights;
|
|
|
|
}
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
Outcome *RigGenerator::takeMeshResultContext()
|
2018-09-18 03:17:35 +00:00
|
|
|
{
|
2018-10-25 00:19:38 +00:00
|
|
|
Outcome *outcome = m_outcome;
|
|
|
|
m_outcome = nullptr;
|
|
|
|
return outcome;
|
2018-09-18 03:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 09:45:05 +00:00
|
|
|
std::vector<AutoRiggerBone> *RigGenerator::takeResultBones()
|
|
|
|
{
|
|
|
|
std::vector<AutoRiggerBone> *resultBones = m_resultBones;
|
|
|
|
m_resultBones = nullptr;
|
|
|
|
return resultBones;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<int, AutoRiggerVertexWeights> *RigGenerator::takeResultWeights()
|
|
|
|
{
|
|
|
|
std::map<int, AutoRiggerVertexWeights> *resultWeights = m_resultWeights;
|
|
|
|
m_resultWeights = nullptr;
|
|
|
|
return resultWeights;
|
|
|
|
}
|
|
|
|
|
|
|
|
MeshLoader *RigGenerator::takeResultMesh()
|
|
|
|
{
|
|
|
|
MeshLoader *resultMesh = m_resultMesh;
|
|
|
|
m_resultMesh = nullptr;
|
|
|
|
return resultMesh;
|
|
|
|
}
|
|
|
|
|
2018-10-02 04:59:30 +00:00
|
|
|
bool RigGenerator::isSucceed()
|
|
|
|
{
|
|
|
|
return m_isSucceed;
|
|
|
|
}
|
|
|
|
|
2018-09-14 09:45:05 +00:00
|
|
|
const std::vector<QString> &RigGenerator::missingMarkNames()
|
|
|
|
{
|
|
|
|
return m_missingMarkNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<QString> &RigGenerator::errorMarkNames()
|
|
|
|
{
|
|
|
|
return m_errorMarkNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RigGenerator::process()
|
|
|
|
{
|
|
|
|
QElapsedTimer countTimeConsumed;
|
|
|
|
countTimeConsumed.start();
|
|
|
|
|
|
|
|
std::vector<QVector3D> inputVerticesPositions;
|
|
|
|
std::set<MeshSplitterTriangle> inputTriangles;
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
for (const auto &vertex: m_outcome->vertices) {
|
2018-09-14 09:45:05 +00:00
|
|
|
inputVerticesPositions.push_back(vertex.position);
|
|
|
|
}
|
2018-10-25 00:19:38 +00:00
|
|
|
std::map<std::pair<BoneMark, SkeletonSide>, std::tuple<QVector3D, int, std::set<MeshSplitterTriangle>>> marksMap;
|
|
|
|
for (size_t triangleIndex = 0; triangleIndex < m_outcome->triangles.size(); triangleIndex++) {
|
|
|
|
const auto &sourceTriangle = m_outcome->triangles[triangleIndex];
|
2018-09-14 09:45:05 +00:00
|
|
|
MeshSplitterTriangle newTriangle;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
newTriangle.indicies[i] = sourceTriangle.indicies[i];
|
2018-10-25 00:19:38 +00:00
|
|
|
auto findBmeshNodeResult = m_outcome->bmeshNodeMap().find(m_outcome->triangleSourceNodes()[triangleIndex]);
|
|
|
|
if (findBmeshNodeResult != m_outcome->bmeshNodeMap().end()) {
|
2018-09-14 09:45:05 +00:00
|
|
|
const auto &bmeshNode = *findBmeshNodeResult->second;
|
2018-10-25 00:19:38 +00:00
|
|
|
if (bmeshNode.boneMark != BoneMark::None) {
|
2018-09-14 09:45:05 +00:00
|
|
|
SkeletonSide boneSide = SkeletonSide::None;
|
2018-10-25 00:19:38 +00:00
|
|
|
if (BoneMarkHasSide(bmeshNode.boneMark)) {
|
2018-09-14 09:45:05 +00:00
|
|
|
boneSide = bmeshNode.origin.x() > 0 ? SkeletonSide::Left : SkeletonSide::Right;
|
|
|
|
}
|
|
|
|
auto &marks = marksMap[std::make_pair(bmeshNode.boneMark, boneSide)];
|
|
|
|
std::get<0>(marks) += bmeshNode.origin;
|
|
|
|
std::get<1>(marks) += 1;
|
|
|
|
std::get<2>(marks).insert(newTriangle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inputTriangles.insert(newTriangle);
|
|
|
|
}
|
|
|
|
m_autoRigger = new AutoRigger(inputVerticesPositions, inputTriangles);
|
|
|
|
for (const auto &marks: marksMap) {
|
|
|
|
m_autoRigger->addMarkGroup(marks.first.first, marks.first.second,
|
|
|
|
std::get<0>(marks.second) / std::get<1>(marks.second),
|
|
|
|
std::get<2>(marks.second));
|
|
|
|
}
|
2018-10-02 04:59:30 +00:00
|
|
|
m_isSucceed = m_autoRigger->rig();
|
2018-09-14 09:45:05 +00:00
|
|
|
|
2018-10-02 04:59:30 +00:00
|
|
|
if (m_isSucceed) {
|
2018-09-14 09:45:05 +00:00
|
|
|
qDebug() << "Rig succeed";
|
|
|
|
} else {
|
|
|
|
qDebug() << "Rig failed";
|
|
|
|
m_missingMarkNames = m_autoRigger->missingMarkNames();
|
|
|
|
m_errorMarkNames = m_autoRigger->errorMarkNames();
|
|
|
|
for (const auto &message: m_autoRigger->messages()) {
|
|
|
|
qDebug() << "errorType:" << message.first << "Message:" << message.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Blend vertices colors according to bone weights
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
std::vector<QColor> inputVerticesColors(m_outcome->vertices.size());
|
2018-10-02 04:59:30 +00:00
|
|
|
if (m_isSucceed) {
|
2018-09-14 09:45:05 +00:00
|
|
|
const auto &resultWeights = m_autoRigger->resultWeights();
|
|
|
|
const auto &resultBones = m_autoRigger->resultBones();
|
|
|
|
|
|
|
|
m_resultWeights = new std::map<int, AutoRiggerVertexWeights>;
|
|
|
|
*m_resultWeights = resultWeights;
|
|
|
|
|
|
|
|
m_resultBones = new std::vector<AutoRiggerBone>;
|
|
|
|
*m_resultBones = resultBones;
|
|
|
|
|
2018-09-18 03:17:35 +00:00
|
|
|
for (const auto &weightItem: resultWeights) {
|
|
|
|
size_t vertexIndex = weightItem.first;
|
|
|
|
const auto &weight = weightItem.second;
|
2018-09-14 09:45:05 +00:00
|
|
|
int blendR = 0, blendG = 0, blendB = 0;
|
2018-09-18 03:17:35 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
int boneIndex = weight.boneIndicies[i];
|
|
|
|
if (boneIndex > 0) {
|
|
|
|
const auto &bone = resultBones[boneIndex];
|
|
|
|
blendR += bone.color.red() * weight.boneWeights[i];
|
|
|
|
blendG += bone.color.green() * weight.boneWeights[i];
|
|
|
|
blendB += bone.color.blue() * weight.boneWeights[i];
|
2018-09-14 09:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
QColor blendColor = QColor(blendR, blendG, blendB, 255);
|
|
|
|
inputVerticesColors[vertexIndex] = blendColor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Smooth normals
|
|
|
|
|
2018-09-18 03:17:35 +00:00
|
|
|
std::vector<QVector3D> vertexNormalMap;
|
|
|
|
vertexNormalMap.resize(inputVerticesPositions.size());
|
2018-10-25 00:19:38 +00:00
|
|
|
for (size_t triangleIndex = 0; triangleIndex < m_outcome->triangles.size(); triangleIndex++) {
|
|
|
|
const auto &sourceTriangle = m_outcome->triangles[triangleIndex];
|
2018-09-14 09:45:05 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
vertexNormalMap[sourceTriangle.indicies[i]] += sourceTriangle.normal;
|
|
|
|
}
|
|
|
|
for (auto &item: vertexNormalMap)
|
2018-09-18 03:17:35 +00:00
|
|
|
item.normalize();
|
2018-09-14 09:45:05 +00:00
|
|
|
|
|
|
|
// Create mesh for demo
|
|
|
|
|
2018-10-25 00:19:38 +00:00
|
|
|
Vertex *triangleVertices = new Vertex[m_outcome->triangles.size() * 3];
|
2018-09-14 09:45:05 +00:00
|
|
|
int triangleVerticesNum = 0;
|
2018-10-25 00:19:38 +00:00
|
|
|
for (size_t triangleIndex = 0; triangleIndex < m_outcome->triangles.size(); triangleIndex++) {
|
|
|
|
const auto &sourceTriangle = m_outcome->triangles[triangleIndex];
|
2018-09-14 09:45:05 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
Vertex ¤tVertex = triangleVertices[triangleVerticesNum++];
|
|
|
|
const auto &sourcePosition = inputVerticesPositions[sourceTriangle.indicies[i]];
|
|
|
|
const auto &sourceColor = inputVerticesColors[sourceTriangle.indicies[i]];
|
|
|
|
const auto &sourceNormal = vertexNormalMap[sourceTriangle.indicies[i]];
|
|
|
|
currentVertex.posX = sourcePosition.x();
|
|
|
|
currentVertex.posY = sourcePosition.y();
|
|
|
|
currentVertex.posZ = sourcePosition.z();
|
|
|
|
currentVertex.texU = 0;
|
|
|
|
currentVertex.texV = 0;
|
|
|
|
currentVertex.colorR = sourceColor.redF();
|
|
|
|
currentVertex.colorG = sourceColor.greenF();
|
|
|
|
currentVertex.colorB = sourceColor.blueF();
|
|
|
|
currentVertex.normX = sourceNormal.x();
|
|
|
|
currentVertex.normY = sourceNormal.y();
|
|
|
|
currentVertex.normZ = sourceNormal.z();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_resultMesh = new MeshLoader(triangleVertices, triangleVerticesNum);
|
|
|
|
|
|
|
|
qDebug() << "The rig generation took" << countTimeConsumed.elapsed() << "milliseconds";
|
|
|
|
|
|
|
|
this->moveToThread(QGuiApplication::instance()->thread());
|
|
|
|
emit finished();
|
|
|
|
}
|