Skip to content

Update glTF2Importer.cpp to support vrm extension #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion code/AssetLib/Assbin/AssbinLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ void AssbinImporter::InternReadFile(const std::string &pFile, aiScene *pScene, I
stream->Seek(128, aiOrigin_CUR); // options
stream->Seek(64, aiOrigin_CUR); // padding

/*
if (compressed) {
uLongf uncompressedSize = Read<uint32_t>(stream);
uLongf compressedSize = static_cast<uLongf>(stream->FileSize() - stream->Tell());
Expand All @@ -725,8 +726,9 @@ void AssbinImporter::InternReadFile(const std::string &pFile, aiScene *pScene, I
delete[] uncompressedData;
delete[] compressedData;
} else {
*/
ReadBinaryScene(stream, pScene);
}
//}

pIOHandler->Close(stream);
}
Expand Down
101 changes: 100 additions & 1 deletion code/AssetLib/MMD/MMDImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/ai_assert.h>
#include <assimp/scene.h>
#include <assimp/Importer.hpp>
#include <assimp/CreateAnimMesh.h>


#include <streambuf>
#include <iomanip>
#include <memory>
#include <sstream>
Expand All @@ -68,6 +70,15 @@ static const aiImporterDesc desc = { "MMD Importer",
0,
"pmx" };

namespace {
struct membuf : std::streambuf
{
membuf(char* begin, char* end) {
this->setg(begin, begin, end);
}
};
}

namespace Assimp {

using namespace std;
Expand Down Expand Up @@ -167,6 +178,85 @@ void MMDImporter::CreateDataFromImport(const pmx::PmxModel *pModel,
indexStart += indexCount;
}

{
int morphTotal = 0;
for (int morphNo = 0; morphNo < pModel->morph_count; morphNo++) {
const auto& morph = pModel->morphs[morphNo];
if (morph.morph_type != pmx::MorphType::Vertex) {
continue;
}
if (morph.morph_name.length() <= 0) {
continue;
}
morphTotal++;
}

int indexStart = 0;
int indexNext = 0;
for (unsigned int i = 0; i < pScene->mNumMeshes; i++) {
const unsigned int indexCount = pModel->materials[i].index_count;
indexStart = indexNext;
indexNext += indexCount;

auto* aim = pScene->mMeshes[i];
if (aim->mNumAnimMeshes == 0) {
aim->mNumAnimMeshes = (unsigned int)morphTotal;
aim->mAnimMeshes = new aiAnimMesh * [aim->mNumAnimMeshes];
}

int currentMorph = 0;
for (int morphNo = 0; morphNo < pModel->morph_count; morphNo++) {

const auto& morph = pModel->morphs[morphNo];
if (morph.morph_type != pmx::MorphType::Vertex) {
continue;
}
if (morph.morph_name.length() <= 0) {
continue;
}

int c = currentMorph;
currentMorph++;

aim->mAnimMeshes[c] = Assimp::aiCreateAnimMesh(aim);
aiAnimMesh& aiAnimMesh = *(aim->mAnimMeshes[c]);
for (unsigned int v = 0; v < aiAnimMesh.mNumVertices; ++v) {
//aiAnimMesh.mVertices[v] = aim->mVertices[v];
aiAnimMesh.mVertices[v].Set(0, 0, 0);
}

//vrm
aiAnimMesh.mName = morph.morph_name;

if (morph.vertex_offsets.get() != nullptr) {
for (int vertexId = 0; vertexId < morph.offset_count; vertexId++) {
const auto& ver = morph.vertex_offsets[vertexId];
const int targetIndex = ver.vertex_index;

int bFoundCount = false;
for (unsigned int uu = 0; uu < indexCount; ++uu) {
if (pModel->indices[indexStart + uu] == targetIndex) {
++bFoundCount;
//break;
if (uu < aiAnimMesh.mNumVertices) {
aiAnimMesh.mVertices[uu].Set(
-morph.vertex_offsets[vertexId].position_offset[0],
morph.vertex_offsets[vertexId].position_offset[1],
-morph.vertex_offsets[vertexId].position_offset[2]);
}
}
}
if (bFoundCount == 0) {
continue;
}

aiAnimMesh.mWeight = 1.f;
}
}
}
}
} // morph end

// create node hierarchy for bone position
std::unique_ptr<aiNode *[]> ppNode(new aiNode *[pModel->bone_count]);
for (auto i = 0; i < pModel->bone_count; i++) {
Expand All @@ -178,6 +268,11 @@ void MMDImporter::CreateDataFromImport(const pmx::PmxModel *pModel,

if (bone.parent_index < 0) {
pScene->mRootNode->addChildren(1, ppNode.get() + i);
aiVector3D v3 = aiVector3D(
bone.position[0],
bone.position[1],
bone.position[2]);
aiMatrix4x4::Translation(v3, ppNode[i]->mTransformation);
} else {
ppNode[bone.parent_index]->addChildren(1, ppNode.get() + i);

Expand Down Expand Up @@ -326,7 +421,11 @@ aiMesh *MMDImporter::CreateMesh(const pmx::PmxModel *pModel,
aiMaterial *MMDImporter::CreateMaterial(const pmx::PmxMaterial *pMat,
const pmx::PmxModel *pModel) {
aiMaterial *mat = new aiMaterial();
aiString name(pMat->material_english_name);
//aiString name(pMat->material_english_name);
aiString name(pMat->material_name);
if (pMat->material_name.size() == 0) {
name = pMat->material_english_name;
}
mat->AddProperty(&name, AI_MATKEY_NAME);

aiColor3D diffuse(pMat->diffuse[0], pMat->diffuse[1], pMat->diffuse[2]);
Expand Down
27 changes: 27 additions & 0 deletions code/AssetLib/glTF2/glTF2Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# define ai_assert
#endif

#include "assimp/vrm/vrmmeta.h"

#if _MSC_VER > 1500 || (defined __GNUC___)
# define ASSIMP_GLTF_USE_UNORDERED_MULTIMAP
#else
Expand Down Expand Up @@ -803,6 +805,8 @@ struct MaterialIOR {

//! The material appearance of a primitive.
struct Material : public Object {
std::string shaderName;

//PBR metallic roughness properties
PbrMetallicRoughness pbrMetallicRoughness;

Expand Down Expand Up @@ -862,6 +866,7 @@ struct Mesh : public Object {
Ref<Material> material;

struct Target {
std::string name;
AccessorList position, normal, tangent;
};
std::vector<Target> targets;
Expand Down Expand Up @@ -1062,6 +1067,25 @@ class LazyDict : public LazyDictBase {
inline T &operator[](size_t i) { return *mObjs[i]; }
};


struct GLTF2VRMMetadata
{
VRM::VRMMetadata* vrmdata;

std::map <std::string, std::string> materialShaderName;

void Read(Document& doc, Asset& r);

GLTF2VRMMetadata() : vrmdata(nullptr) {}

~GLTF2VRMMetadata() {
if (vrmdata) delete vrmdata;
vrmdata = nullptr;
}
};



struct AssetMetadata {
std::string copyright; //!< A copyright message suitable for display to credit the content creator.
std::string generator; //!< Tool that generated this glTF model.Useful for debugging.
Expand Down Expand Up @@ -1140,6 +1164,9 @@ class Asset {
AssetMetadata asset;
Value *extras;

GLTF2VRMMetadata vrmdata;


// Dictionaries for each type of object

LazyDict<Accessor> accessors;
Expand Down
Loading