Plugins: Add Assimp plugin (WIP)
Currently load only static meshes Former-commit-id: 61dee8f53a423d6d170bd3bfe72e71daa63e8c5b
This commit is contained in:
250
plugins/Assimp/Plugin.cpp
Normal file
250
plugins/Assimp/Plugin.cpp
Normal file
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
Nazara Engine - Assimp Plugin
|
||||
|
||||
Copyright (C) 2015 Jérôme "Lynix" Leclercq (lynix680@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <CustomStream.hpp>
|
||||
#include <Nazara/Core/String.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/IndexIterator.hpp>
|
||||
#include <Nazara/Utility/IndexMapper.hpp>
|
||||
#include <Nazara/Utility/StaticMesh.hpp>
|
||||
#include <assimp/cfileio.h>
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/config.h>
|
||||
#include <assimp/mesh.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <set>
|
||||
|
||||
using namespace Nz;
|
||||
|
||||
void ProcessJoints(aiNode* node, Skeleton* skeleton, const std::set<Nz::String>& joints)
|
||||
{
|
||||
Nz::String jointName(node->mName.data, node->mName.length);
|
||||
if (joints.count(jointName))
|
||||
{
|
||||
Joint* joint = skeleton->GetJoint(jointName);
|
||||
if (joint)
|
||||
{
|
||||
if (node->mParent)
|
||||
joint->SetParent(skeleton->GetJoint(node->mParent->mName.C_Str()));
|
||||
|
||||
Matrix4f transformMatrix(node->mTransformation.a1, node->mTransformation.a2, node->mTransformation.a3, node->mTransformation.a4,
|
||||
node->mTransformation.b1, node->mTransformation.b2, node->mTransformation.b3, node->mTransformation.b4,
|
||||
node->mTransformation.c1, node->mTransformation.c2, node->mTransformation.c3, node->mTransformation.c4,
|
||||
node->mTransformation.d1, node->mTransformation.d2, node->mTransformation.d3, node->mTransformation.d4);
|
||||
|
||||
transformMatrix.InverseAffine();
|
||||
|
||||
joint->SetInverseBindMatrix(transformMatrix);
|
||||
}
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < node->mNumChildren; ++i)
|
||||
ProcessJoints(node->mChildren[i], skeleton, joints);
|
||||
}
|
||||
|
||||
bool IsSupported(const String& extension)
|
||||
{
|
||||
String dotExt = '.' + extension;
|
||||
return (aiIsExtensionSupported(dotExt.GetConstBuffer()) == AI_TRUE);
|
||||
}
|
||||
|
||||
Ternary Check(Stream& stream, const MeshParams& parameters)
|
||||
{
|
||||
bool skip;
|
||||
if (parameters.custom.GetBooleanParameter("SkipAssimpLoader", &skip) && skip)
|
||||
return Ternary_False;
|
||||
|
||||
return Ternary_Unknown;
|
||||
}
|
||||
|
||||
bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters)
|
||||
{
|
||||
Nz::String streamPath = stream.GetPath();
|
||||
|
||||
FileIOUserdata userdata;
|
||||
userdata.originalFilePath = (!streamPath.IsEmpty()) ? streamPath.GetConstBuffer() : StreamPath;
|
||||
userdata.originalStream = &stream;
|
||||
|
||||
aiFileIO fileIO;
|
||||
fileIO.CloseProc = StreamCloser;
|
||||
fileIO.OpenProc = StreamOpener;
|
||||
fileIO.UserData = reinterpret_cast<char*>(&userdata);
|
||||
|
||||
unsigned int postProcess = aiProcess_CalcTangentSpace | aiProcess_JoinIdenticalVertices
|
||||
| aiProcess_MakeLeftHanded | aiProcess_Triangulate
|
||||
| aiProcess_RemoveComponent | aiProcess_GenSmoothNormals
|
||||
| aiProcess_SplitLargeMeshes | aiProcess_LimitBoneWeights
|
||||
| aiProcess_ImproveCacheLocality | aiProcess_RemoveRedundantMaterials
|
||||
| aiProcess_FixInfacingNormals | aiProcess_SortByPType
|
||||
| aiProcess_FindInvalidData | aiProcess_GenUVCoords
|
||||
| aiProcess_TransformUVCoords | aiProcess_OptimizeMeshes
|
||||
| aiProcess_OptimizeGraph | aiProcess_FlipWindingOrder
|
||||
| aiProcess_Debone;
|
||||
|
||||
if (!parameters.flipUVs)
|
||||
postProcess |= aiProcess_FlipUVs;
|
||||
|
||||
if (parameters.optimizeIndexBuffers)
|
||||
postProcess |= aiProcess_ImproveCacheLocality;
|
||||
|
||||
float smoothingAngle = 80.f;
|
||||
parameters.custom.GetFloatParameter("AssimpLoader_SmoothingAngle", &smoothingAngle);
|
||||
|
||||
int triangleLimit = 1'000'000;
|
||||
parameters.custom.GetIntegerParameter("AssimpLoader_TriangleLimit", &triangleLimit);
|
||||
|
||||
int vertexLimit = 1'000'000;
|
||||
parameters.custom.GetIntegerParameter("AssimpLoader_VertexLimit", &vertexLimit);
|
||||
|
||||
aiPropertyStore* properties = aiCreatePropertyStore();
|
||||
aiSetImportPropertyFloat(properties, AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE, smoothingAngle);
|
||||
aiSetImportPropertyInteger(properties, AI_CONFIG_PP_LBW_MAX_WEIGHTS, 4);
|
||||
aiSetImportPropertyInteger(properties, AI_CONFIG_PP_SBP_REMOVE, ~aiPrimitiveType_TRIANGLE); //< We only want triangles
|
||||
aiSetImportPropertyInteger(properties, AI_CONFIG_PP_SLM_TRIANGLE_LIMIT, triangleLimit);
|
||||
aiSetImportPropertyInteger(properties, AI_CONFIG_PP_SLM_VERTEX_LIMIT, vertexLimit);
|
||||
aiSetImportPropertyInteger(properties, AI_CONFIG_PP_RVC_FLAGS, aiComponent_COLORS);
|
||||
|
||||
const aiScene* scene = aiImportFileExWithProperties(userdata.originalFilePath, postProcess, &fileIO, properties);
|
||||
aiReleasePropertyStore(properties);
|
||||
|
||||
std::set<Nz::String> joints;
|
||||
|
||||
bool animatedMesh = false;
|
||||
if (parameters.animated)
|
||||
{
|
||||
for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
|
||||
{
|
||||
aiMesh* mesh = scene->mMeshes[i];
|
||||
if (mesh->HasBones()) // Inline functions can be safely called
|
||||
{
|
||||
animatedMesh = true;
|
||||
for (unsigned int j = 0; j < mesh->mNumBones; ++j)
|
||||
joints.insert(mesh->mBones[j]->mName.C_Str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (animatedMesh)
|
||||
{
|
||||
mesh->CreateSkeletal(joints.size());
|
||||
|
||||
Skeleton* skeleton = mesh->GetSkeleton();
|
||||
|
||||
// First, assign names
|
||||
unsigned int jointIndex = 0;
|
||||
for (const Nz::String& jointName : joints)
|
||||
skeleton->GetJoint(jointIndex++)->SetName(jointName);
|
||||
|
||||
ProcessJoints(scene->mRootNode, skeleton, joints);
|
||||
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
mesh->CreateStatic();
|
||||
|
||||
for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
|
||||
{
|
||||
aiMesh* iMesh = scene->mMeshes[i];
|
||||
if (!iMesh->HasBones()) // Don't process skeletal meshs
|
||||
{
|
||||
unsigned int indexCount = iMesh->mNumFaces * 3;
|
||||
unsigned int vertexCount = iMesh->mNumVertices;
|
||||
|
||||
// Index buffer
|
||||
bool largeIndices = (vertexCount > std::numeric_limits<UInt16>::max());
|
||||
|
||||
IndexBufferRef indexBuffer = IndexBuffer::New(largeIndices, indexCount, parameters.storage);
|
||||
|
||||
IndexMapper indexMapper(indexBuffer, BufferAccess_DiscardAndWrite);
|
||||
IndexIterator index = indexMapper.begin();
|
||||
|
||||
for (unsigned int j = 0; j < iMesh->mNumFaces; ++j)
|
||||
{
|
||||
aiFace& face = iMesh->mFaces[j];
|
||||
if (face.mNumIndices != 3)
|
||||
NazaraWarning("Assimp plugin: This face is not a triangle!");
|
||||
|
||||
*index++ = face.mIndices[0];
|
||||
*index++ = face.mIndices[1];
|
||||
*index++ = face.mIndices[2];
|
||||
}
|
||||
indexMapper.Unmap();
|
||||
|
||||
// Vertex buffer
|
||||
VertexBufferRef vertexBuffer = VertexBuffer::New(VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent), vertexCount, parameters.storage);
|
||||
BufferMapper<VertexBuffer> vertexMapper(vertexBuffer, BufferAccess_WriteOnly);
|
||||
|
||||
MeshVertex* vertex = static_cast<MeshVertex*>(vertexMapper.GetPointer());
|
||||
for (unsigned int j = 0; j < vertexCount; ++j)
|
||||
{
|
||||
aiVector3D position = iMesh->mVertices[j];
|
||||
aiVector3D normal = iMesh->mNormals[j];
|
||||
aiVector3D tangent = iMesh->mTangents[j];
|
||||
aiVector3D uv = iMesh->mTextureCoords[0][j];
|
||||
|
||||
vertex->position = parameters.scale * Vector3f(position.x, position.y, position.z);
|
||||
vertex->normal.Set(normal.x, normal.y, normal.z);
|
||||
vertex->tangent.Set(tangent.x, tangent.y, tangent.z);
|
||||
vertex->uv.Set(uv.x, uv.y);
|
||||
vertex++;
|
||||
}
|
||||
|
||||
vertexMapper.Unmap();
|
||||
|
||||
// Submesh
|
||||
StaticMeshRef subMesh = StaticMesh::New(mesh);
|
||||
subMesh->Create(vertexBuffer);
|
||||
|
||||
subMesh->SetIndexBuffer(indexBuffer);
|
||||
subMesh->GenerateAABB();
|
||||
subMesh->SetMaterialIndex(iMesh->mMaterialIndex);
|
||||
|
||||
mesh->AddSubMesh(subMesh);
|
||||
}
|
||||
}
|
||||
|
||||
if (parameters.center)
|
||||
mesh->Recenter();
|
||||
}
|
||||
|
||||
aiReleaseImport(scene);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
NAZARA_EXPORT int PluginLoad()
|
||||
{
|
||||
Nz::MeshLoader::RegisterLoader(IsSupported, Check, Load);
|
||||
return 1;
|
||||
}
|
||||
|
||||
NAZARA_EXPORT void PluginUnload()
|
||||
{
|
||||
Nz::MeshLoader::UnregisterLoader(IsSupported, Check, Load);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user