TextureTransform: set material transform only when the extension is provided.

pull/2764/head
Kim Kulling 2019-11-16 08:08:57 +01:00
parent 4ad2368116
commit 2eed8b1820
3 changed files with 1001 additions and 1039 deletions

View File

@ -686,9 +686,12 @@ namespace glTF2
unsigned int index; unsigned int index;
unsigned int texCoord = 0; unsigned int texCoord = 0;
bool textureTransformSupported = false;
struct TextureTransformExt {
float offset[2]; float offset[2];
float rotation; float rotation;
float scale[2]; float scale[2];
} TextureTransformExt_t;
}; };
struct NormalTextureInfo : TextureInfo struct NormalTextureInfo : TextureInfo

View File

@ -803,25 +803,26 @@ namespace {
inline void SetTextureProperties(Asset& r, Value* prop, TextureInfo& out) { inline void SetTextureProperties(Asset& r, Value* prop, TextureInfo& out) {
if (r.extensionsUsed.KHR_texture_transform) { if (r.extensionsUsed.KHR_texture_transform) {
if (Value *extensions = FindObject(*prop, "extensions")) { if (Value *extensions = FindObject(*prop, "extensions")) {
out.textureTransformSupported = true;
if (Value *pKHR_texture_transform = FindObject(*extensions, "KHR_texture_transform")) { if (Value *pKHR_texture_transform = FindObject(*extensions, "KHR_texture_transform")) {
if (Value *array = FindArray(*pKHR_texture_transform, "offset")) { if (Value *array = FindArray(*pKHR_texture_transform, "offset")) {
out.offset[0] = (*array)[0].GetFloat(); out.TextureTransformExt_t.offset[0] = (*array)[0].GetFloat();
out.offset[1] = (*array)[1].GetFloat(); out.TextureTransformExt_t.offset[1] = (*array)[1].GetFloat();
} else { } else {
out.offset[0] = 0; out.TextureTransformExt_t.offset[0] = 0;
out.offset[1] = 0; out.TextureTransformExt_t.offset[1] = 0;
} }
if (!ReadMember(*pKHR_texture_transform, "rotation", out.rotation)) { if (!ReadMember(*pKHR_texture_transform, "rotation", out.TextureTransformExt_t.rotation)) {
out.rotation = 0; out.TextureTransformExt_t.rotation = 0;
} }
if (Value *array = FindArray(*pKHR_texture_transform, "scale")) { if (Value *array = FindArray(*pKHR_texture_transform, "scale")) {
out.scale[0] = (*array)[0].GetFloat(); out.TextureTransformExt_t.scale[0] = (*array)[0].GetFloat();
out.scale[1] = (*array)[1].GetFloat(); out.TextureTransformExt_t.scale[1] = (*array)[1].GetFloat();
} else { } else {
out.scale[0] = 1; out.TextureTransformExt_t.scale[0] = 1;
out.scale[1] = 1; out.TextureTransformExt_t.scale[1] = 1;
} }
} }
} }

View File

@ -43,18 +43,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef ASSIMP_BUILD_NO_GLTF_IMPORTER #ifndef ASSIMP_BUILD_NO_GLTF_IMPORTER
#include "glTF2/glTF2Importer.h" #include "glTF2/glTF2Importer.h"
#include "PostProcessing/MakeVerboseFormat.h"
#include "glTF2/glTF2Asset.h" #include "glTF2/glTF2Asset.h"
#include "glTF2/glTF2AssetWriter.h" #include "glTF2/glTF2AssetWriter.h"
#include "PostProcessing/MakeVerboseFormat.h"
#include <assimp/CreateAnimMesh.h>
#include <assimp/StringComparison.h> #include <assimp/StringComparison.h>
#include <assimp/StringUtils.h> #include <assimp/StringUtils.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/ai_assert.h> #include <assimp/ai_assert.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/importerdesc.h> #include <assimp/importerdesc.h>
#include <assimp/CreateAnimMesh.h> #include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/Importer.hpp>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
@ -67,11 +67,11 @@ using namespace glTF2;
using namespace glTFCommon; using namespace glTFCommon;
namespace { namespace {
// generate bi-tangents from normals and tangents according to spec // generate bi-tangents from normals and tangents according to spec
struct Tangent { struct Tangent {
aiVector3D xyz; aiVector3D xyz;
ai_real w; ai_real w;
}; };
} // namespace } // namespace
// //
@ -91,11 +91,11 @@ static const aiImporterDesc desc = {
"gltf glb" "gltf glb"
}; };
glTF2Importer::glTF2Importer() glTF2Importer::glTF2Importer() :
: BaseImporter() BaseImporter(),
, meshOffsets() meshOffsets(),
, embeddedTexIdxs() embeddedTexIdxs(),
, mScene( NULL ) { mScene(NULL) {
// empty // empty
} }
@ -103,13 +103,11 @@ glTF2Importer::~glTF2Importer() {
// empty // empty
} }
const aiImporterDesc* glTF2Importer::GetInfo() const const aiImporterDesc *glTF2Importer::GetInfo() const {
{
return &desc; return &desc;
} }
bool glTF2Importer::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool /* checkSig */) const bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const {
{
const std::string &extension = GetExtension(pFile); const std::string &extension = GetExtension(pFile);
if (extension != "gltf" && extension != "glb") if (extension != "gltf" && extension != "glb")
@ -125,8 +123,7 @@ bool glTF2Importer::CanRead(const std::string& pFile, IOSystem* pIOHandler, bool
return false; return false;
} }
static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) {
{
switch (gltfWrapMode) { switch (gltfWrapMode) {
case SamplerWrap::Mirrored_Repeat: case SamplerWrap::Mirrored_Repeat:
return aiTextureMapMode_Mirror; return aiTextureMapMode_Mirror;
@ -180,22 +177,19 @@ static void CopyValue(const glTF2::vec4& v, aiQuaternion& out)
o.a4 = v[12]; o.b4 = v[13]; o.c4 = v[14]; o.d4 = v[15]; o.a4 = v[12]; o.b4 = v[13]; o.c4 = v[14]; o.d4 = v[15];
}*/ }*/
inline void SetMaterialColorProperty(Asset& /*r*/, vec4& prop, aiMaterial* mat, const char* pKey, unsigned int type, unsigned int idx) inline void SetMaterialColorProperty(Asset & /*r*/, vec4 &prop, aiMaterial *mat, const char *pKey, unsigned int type, unsigned int idx) {
{
aiColor4D col; aiColor4D col;
CopyValue(prop, col); CopyValue(prop, col);
mat->AddProperty(&col, 1, pKey, type, idx); mat->AddProperty(&col, 1, pKey, type, idx);
} }
inline void SetMaterialColorProperty(Asset& /*r*/, vec3& prop, aiMaterial* mat, const char* pKey, unsigned int type, unsigned int idx) inline void SetMaterialColorProperty(Asset & /*r*/, vec3 &prop, aiMaterial *mat, const char *pKey, unsigned int type, unsigned int idx) {
{
aiColor4D col; aiColor4D col;
glTFCommon::CopyValue(prop, col); glTFCommon::CopyValue(prop, col);
mat->AddProperty(&col, 1, pKey, type, idx); mat->AddProperty(&col, 1, pKey, type, idx);
} }
inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& /*r*/, glTF2::TextureInfo prop, aiMaterial* mat, aiTextureType texType, unsigned int texSlot = 0) inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset & /*r*/, glTF2::TextureInfo prop, aiMaterial *mat, aiTextureType texType, unsigned int texSlot = 0) {
{
if (prop.texture && prop.texture->source) { if (prop.texture && prop.texture->source) {
aiString uri(prop.texture->source->uri); aiString uri(prop.texture->source->uri);
@ -206,13 +200,15 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
uri.length = 1 + ASSIMP_itoa10(uri.data + 1, MAXLEN - 1, texIdx); uri.length = 1 + ASSIMP_itoa10(uri.data + 1, MAXLEN - 1, texIdx);
} }
if (prop.textureTransformSupported) {
aiUVTransform transform; aiUVTransform transform;
transform.mTranslation.x = prop.offset[0]; transform.mTranslation.x = prop.TextureTransformExt_t.offset[0];
transform.mTranslation.y = prop.offset[0]; transform.mTranslation.y = prop.TextureTransformExt_t.offset[0];
transform.mRotation = prop.rotation; transform.mRotation = prop.TextureTransformExt_t.rotation;
transform.mScaling.x = prop.scale[0]; transform.mScaling.x = prop.TextureTransformExt_t.scale[0];
transform.mScaling.y = prop.scale[1]; transform.mScaling.y = prop.TextureTransformExt_t.scale[1];
mat->AddProperty(&transform, 1, _AI_MATKEY_UVTRANSFORM_BASE, texType, texSlot); mat->AddProperty(&transform, 1, _AI_MATKEY_UVTRANSFORM_BASE, texType, texSlot);
}
mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType, texSlot)); mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType, texSlot));
mat->AddProperty(&prop.texCoord, 1, _AI_MATKEY_GLTF_TEXTURE_TEXCOORD_BASE, texType, texSlot); mat->AddProperty(&prop.texCoord, 1, _AI_MATKEY_GLTF_TEXTURE_TEXCOORD_BASE, texType, texSlot);
@ -242,27 +238,24 @@ inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset&
} }
} }
inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF2::NormalTextureInfo& prop, aiMaterial* mat, aiTextureType texType, unsigned int texSlot = 0) inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset &r, glTF2::NormalTextureInfo &prop, aiMaterial *mat, aiTextureType texType, unsigned int texSlot = 0) {
{ SetMaterialTextureProperty(embeddedTexIdxs, r, (glTF2::TextureInfo)prop, mat, texType, texSlot);
SetMaterialTextureProperty( embeddedTexIdxs, r, (glTF2::TextureInfo) prop, mat, texType, texSlot );
if (prop.texture && prop.texture->source) { if (prop.texture && prop.texture->source) {
mat->AddProperty(&prop.scale, 1, AI_MATKEY_GLTF_TEXTURE_SCALE(texType, texSlot)); mat->AddProperty(&prop.scale, 1, AI_MATKEY_GLTF_TEXTURE_SCALE(texType, texSlot));
} }
} }
inline void SetMaterialTextureProperty(std::vector<int>& embeddedTexIdxs, Asset& r, glTF2::OcclusionTextureInfo& prop, aiMaterial* mat, aiTextureType texType, unsigned int texSlot = 0) inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset &r, glTF2::OcclusionTextureInfo &prop, aiMaterial *mat, aiTextureType texType, unsigned int texSlot = 0) {
{ SetMaterialTextureProperty(embeddedTexIdxs, r, (glTF2::TextureInfo)prop, mat, texType, texSlot);
SetMaterialTextureProperty( embeddedTexIdxs, r, (glTF2::TextureInfo) prop, mat, texType, texSlot );
if (prop.texture && prop.texture->source) { if (prop.texture && prop.texture->source) {
mat->AddProperty(&prop.strength, 1, AI_MATKEY_GLTF_TEXTURE_STRENGTH(texType, texSlot)); mat->AddProperty(&prop.strength, 1, AI_MATKEY_GLTF_TEXTURE_STRENGTH(texType, texSlot));
} }
} }
static aiMaterial* ImportMaterial(std::vector<int>& embeddedTexIdxs, Asset& r, Material& mat) static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, Material &mat) {
{ aiMaterial *aimat = new aiMaterial();
aiMaterial* aimat = new aiMaterial();
if (!mat.name.empty()) { if (!mat.name.empty()) {
aiString str(mat.name); aiString str(mat.name);
@ -319,13 +312,12 @@ static aiMaterial* ImportMaterial(std::vector<int>& embeddedTexIdxs, Asset& r, M
return aimat; return aimat;
} }
void glTF2Importer::ImportMaterials(glTF2::Asset& r) void glTF2Importer::ImportMaterials(glTF2::Asset &r) {
{
const unsigned int numImportedMaterials = unsigned(r.materials.Size()); const unsigned int numImportedMaterials = unsigned(r.materials.Size());
Material defaultMaterial; Material defaultMaterial;
mScene->mNumMaterials = numImportedMaterials + 1; mScene->mNumMaterials = numImportedMaterials + 1;
mScene->mMaterials = new aiMaterial*[mScene->mNumMaterials]; mScene->mMaterials = new aiMaterial *[mScene->mNumMaterials];
mScene->mMaterials[numImportedMaterials] = ImportMaterial(embeddedTexIdxs, r, defaultMaterial); mScene->mMaterials[numImportedMaterials] = ImportMaterial(embeddedTexIdxs, r, defaultMaterial);
for (unsigned int i = 0; i < numImportedMaterials; ++i) { for (unsigned int i = 0; i < numImportedMaterials; ++i) {
@ -333,24 +325,20 @@ void glTF2Importer::ImportMaterials(glTF2::Asset& r)
} }
} }
static inline void SetFace(aiFace &face, int a) {
static inline void SetFace(aiFace& face, int a)
{
face.mNumIndices = 1; face.mNumIndices = 1;
face.mIndices = new unsigned int[1]; face.mIndices = new unsigned int[1];
face.mIndices[0] = a; face.mIndices[0] = a;
} }
static inline void SetFace(aiFace& face, int a, int b) static inline void SetFace(aiFace &face, int a, int b) {
{
face.mNumIndices = 2; face.mNumIndices = 2;
face.mIndices = new unsigned int[2]; face.mIndices = new unsigned int[2];
face.mIndices[0] = a; face.mIndices[0] = a;
face.mIndices[1] = b; face.mIndices[1] = b;
} }
static inline void SetFace(aiFace& face, int a, int b, int c) static inline void SetFace(aiFace &face, int a, int b, int c) {
{
face.mNumIndices = 3; face.mNumIndices = 3;
face.mIndices = new unsigned int[3]; face.mIndices = new unsigned int[3];
face.mIndices[0] = a; face.mIndices[0] = a;
@ -359,8 +347,7 @@ static inline void SetFace(aiFace& face, int a, int b, int c)
} }
#ifdef ASSIMP_BUILD_DEBUG #ifdef ASSIMP_BUILD_DEBUG
static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsigned nVerts) static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsigned nVerts) {
{
for (unsigned i = 0; i < nFaces; ++i) { for (unsigned i = 0; i < nFaces; ++i) {
for (unsigned j = 0; j < faces[i].mNumIndices; ++j) { for (unsigned j = 0; j < faces[i].mNumIndices; ++j) {
unsigned idx = faces[i].mIndices[j]; unsigned idx = faces[i].mIndices[j];
@ -372,28 +359,27 @@ static inline bool CheckValidFacesIndices(aiFace* faces, unsigned nFaces, unsign
} }
#endif // ASSIMP_BUILD_DEBUG #endif // ASSIMP_BUILD_DEBUG
void glTF2Importer::ImportMeshes(glTF2::Asset& r) void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
{ std::vector<aiMesh *> meshes;
std::vector<aiMesh*> meshes;
unsigned int k = 0; unsigned int k = 0;
for (unsigned int m = 0; m < r.meshes.Size(); ++m) { for (unsigned int m = 0; m < r.meshes.Size(); ++m) {
Mesh& mesh = r.meshes[m]; Mesh &mesh = r.meshes[m];
meshOffsets.push_back(k); meshOffsets.push_back(k);
k += unsigned(mesh.primitives.size()); k += unsigned(mesh.primitives.size());
for (unsigned int p = 0; p < mesh.primitives.size(); ++p) { for (unsigned int p = 0; p < mesh.primitives.size(); ++p) {
Mesh::Primitive& prim = mesh.primitives[p]; Mesh::Primitive &prim = mesh.primitives[p];
aiMesh* aim = new aiMesh(); aiMesh *aim = new aiMesh();
meshes.push_back(aim); meshes.push_back(aim);
aim->mName = mesh.name.empty() ? mesh.id : mesh.name; aim->mName = mesh.name.empty() ? mesh.id : mesh.name;
if (mesh.primitives.size() > 1) { if (mesh.primitives.size() > 1) {
ai_uint32& len = aim->mName.length; ai_uint32 &len = aim->mName.length;
aim->mName.data[len] = '-'; aim->mName.data[len] = '-';
len += 1 + ASSIMP_itoa10(aim->mName.data + len + 1, unsigned(MAXLEN - len - 1), p); len += 1 + ASSIMP_itoa10(aim->mName.data + len + 1, unsigned(MAXLEN - len - 1), p);
} }
@ -414,10 +400,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
case PrimitiveMode_TRIANGLE_FAN: case PrimitiveMode_TRIANGLE_FAN:
aim->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE; aim->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
break; break;
} }
Mesh::Primitive::Attributes& attr = prim.attributes; Mesh::Primitive::Attributes &attr = prim.attributes;
if (attr.position.size() > 0 && attr.position[0]) { if (attr.position.size() > 0 && attr.position[0]) {
aim->mNumVertices = static_cast<unsigned int>(attr.position[0]->count); aim->mNumVertices = static_cast<unsigned int>(attr.position[0]->count);
@ -442,7 +427,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
aim->mBitangents[i] = (aim->mNormals[i] ^ tangents[i].xyz) * tangents[i].w; aim->mBitangents[i] = (aim->mNormals[i] ^ tangents[i].xyz) * tangents[i].w;
} }
delete [] tangents; delete[] tangents;
} }
} }
@ -464,36 +449,36 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
attr.texcoord[tc]->ExtractData(aim->mTextureCoords[tc]); attr.texcoord[tc]->ExtractData(aim->mTextureCoords[tc]);
aim->mNumUVComponents[tc] = attr.texcoord[tc]->GetNumComponents(); aim->mNumUVComponents[tc] = attr.texcoord[tc]->GetNumComponents();
aiVector3D* values = aim->mTextureCoords[tc]; aiVector3D *values = aim->mTextureCoords[tc];
for (unsigned int i = 0; i < aim->mNumVertices; ++i) { for (unsigned int i = 0; i < aim->mNumVertices; ++i) {
values[i].y = 1 - values[i].y; // Flip Y coords values[i].y = 1 - values[i].y; // Flip Y coords
} }
} }
std::vector<Mesh::Primitive::Target>& targets = prim.targets; std::vector<Mesh::Primitive::Target> &targets = prim.targets;
if (targets.size() > 0) { if (targets.size() > 0) {
aim->mNumAnimMeshes = (unsigned int)targets.size(); aim->mNumAnimMeshes = (unsigned int)targets.size();
aim->mAnimMeshes = new aiAnimMesh*[aim->mNumAnimMeshes]; aim->mAnimMeshes = new aiAnimMesh *[aim->mNumAnimMeshes];
for (size_t i = 0; i < targets.size(); i++) { for (size_t i = 0; i < targets.size(); i++) {
aim->mAnimMeshes[i] = aiCreateAnimMesh(aim); aim->mAnimMeshes[i] = aiCreateAnimMesh(aim);
aiAnimMesh& aiAnimMesh = *(aim->mAnimMeshes[i]); aiAnimMesh &aiAnimMesh = *(aim->mAnimMeshes[i]);
Mesh::Primitive::Target& target = targets[i]; Mesh::Primitive::Target &target = targets[i];
if (target.position.size() > 0) { if (target.position.size() > 0) {
aiVector3D *positionDiff = nullptr; aiVector3D *positionDiff = nullptr;
target.position[0]->ExtractData(positionDiff); target.position[0]->ExtractData(positionDiff);
for(unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) { for (unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) {
aiAnimMesh.mVertices[vertexId] += positionDiff[vertexId]; aiAnimMesh.mVertices[vertexId] += positionDiff[vertexId];
} }
delete [] positionDiff; delete[] positionDiff;
} }
if (target.normal.size() > 0) { if (target.normal.size() > 0) {
aiVector3D *normalDiff = nullptr; aiVector3D *normalDiff = nullptr;
target.normal[0]->ExtractData(normalDiff); target.normal[0]->ExtractData(normalDiff);
for(unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) { for (unsigned int vertexId = 0; vertexId < aim->mNumVertices; vertexId++) {
aiAnimMesh.mNormals[vertexId] += normalDiff[vertexId]; aiAnimMesh.mNormals[vertexId] += normalDiff[vertexId];
} }
delete [] normalDiff; delete[] normalDiff;
} }
if (target.tangent.size() > 0) { if (target.tangent.size() > 0) {
Tangent *tangent = nullptr; Tangent *tangent = nullptr;
@ -507,8 +492,8 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
aiAnimMesh.mTangents[vertexId] = tangent[vertexId].xyz; aiAnimMesh.mTangents[vertexId] = tangent[vertexId].xyz;
aiAnimMesh.mBitangents[vertexId] = (aiAnimMesh.mNormals[vertexId] ^ tangent[vertexId].xyz) * tangent[vertexId].w; aiAnimMesh.mBitangents[vertexId] = (aiAnimMesh.mNormals[vertexId] ^ tangent[vertexId].xyz) * tangent[vertexId].w;
} }
delete [] tangent; delete[] tangent;
delete [] tangentDiff; delete[] tangentDiff;
} }
if (mesh.weights.size() > i) { if (mesh.weights.size() > i) {
aiAnimMesh.mWeight = mesh.weights[i]; aiAnimMesh.mWeight = mesh.weights[i];
@ -516,8 +501,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
} }
} }
aiFace *faces = 0;
aiFace* faces = 0;
size_t nFaces = 0; size_t nFaces = 0;
if (prim.indices) { if (prim.indices) {
@ -580,13 +564,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
faces = new aiFace[nFaces]; faces = new aiFace[nFaces];
for (unsigned int i = 0; i < nFaces; ++i) { for (unsigned int i = 0; i < nFaces; ++i) {
//The ordering is to ensure that the triangles are all drawn with the same orientation //The ordering is to ensure that the triangles are all drawn with the same orientation
if ((i + 1) % 2 == 0) if ((i + 1) % 2 == 0) {
{
//For even n, vertices n + 1, n, and n + 2 define triangle n //For even n, vertices n + 1, n, and n + 2 define triangle n
SetFace(faces[i], data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2)); SetFace(faces[i], data.GetUInt(i + 1), data.GetUInt(i), data.GetUInt(i + 2));
} } else {
else
{
//For odd n, vertices n, n+1, and n+2 define triangle n //For odd n, vertices n, n+1, and n+2 define triangle n
SetFace(faces[i], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2)); SetFace(faces[i], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
} }
@ -602,8 +583,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
} }
break; break;
} }
} } else { // no indices provided so directly generate from counts
else { // no indices provided so directly generate from counts
// use the already determined count as it includes checks // use the already determined count as it includes checks
unsigned int count = aim->mNumVertices; unsigned int count = aim->mNumVertices;
@ -622,7 +602,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
nFaces = count / 2; nFaces = count / 2;
if (nFaces * 2 != count) { if (nFaces * 2 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped."); ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
count = (unsigned int) nFaces * 2; count = (unsigned int)nFaces * 2;
} }
faces = new aiFace[nFaces]; faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 2) { for (unsigned int i = 0; i < count; i += 2) {
@ -649,7 +629,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
nFaces = count / 3; nFaces = count / 3;
if (nFaces * 3 != count) { if (nFaces * 3 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped."); ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
count = (unsigned int) nFaces * 3; count = (unsigned int)nFaces * 3;
} }
faces = new aiFace[nFaces]; faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 3) { for (unsigned int i = 0; i < count; i += 3) {
@ -662,15 +642,12 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
faces = new aiFace[nFaces]; faces = new aiFace[nFaces];
for (unsigned int i = 0; i < nFaces; ++i) { for (unsigned int i = 0; i < nFaces; ++i) {
//The ordering is to ensure that the triangles are all drawn with the same orientation //The ordering is to ensure that the triangles are all drawn with the same orientation
if ((i+1) % 2 == 0) if ((i + 1) % 2 == 0) {
{
//For even n, vertices n + 1, n, and n + 2 define triangle n //For even n, vertices n + 1, n, and n + 2 define triangle n
SetFace(faces[i], i+1, i, i+2); SetFace(faces[i], i + 1, i, i + 2);
} } else {
else
{
//For odd n, vertices n, n+1, and n+2 define triangle n //For odd n, vertices n, n+1, and n+2 define triangle n
SetFace(faces[i], i, i+1, i+2); SetFace(faces[i], i, i + 1, i + 2);
} }
} }
break; break;
@ -694,11 +671,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
if (prim.material) { if (prim.material) {
aim->mMaterialIndex = prim.material.GetIndex(); aim->mMaterialIndex = prim.material.GetIndex();
} } else {
else {
aim->mMaterialIndex = mScene->mNumMaterials - 1; aim->mMaterialIndex = mScene->mNumMaterials - 1;
} }
} }
} }
@ -707,20 +682,19 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)
CopyVector(meshes, mScene->mMeshes, mScene->mNumMeshes); CopyVector(meshes, mScene->mMeshes, mScene->mNumMeshes);
} }
void glTF2Importer::ImportCameras(glTF2::Asset& r) void glTF2Importer::ImportCameras(glTF2::Asset &r) {
{
if (!r.cameras.Size()) return; if (!r.cameras.Size()) return;
mScene->mNumCameras = r.cameras.Size(); mScene->mNumCameras = r.cameras.Size();
mScene->mCameras = new aiCamera*[r.cameras.Size()]; mScene->mCameras = new aiCamera *[r.cameras.Size()];
for (size_t i = 0; i < r.cameras.Size(); ++i) { for (size_t i = 0; i < r.cameras.Size(); ++i) {
Camera& cam = r.cameras[i]; Camera &cam = r.cameras[i];
aiCamera* aicam = mScene->mCameras[i] = new aiCamera(); aiCamera *aicam = mScene->mCameras[i] = new aiCamera();
// cameras point in -Z by default, rest is specified in node transform // cameras point in -Z by default, rest is specified in node transform
aicam->mLookAt = aiVector3D(0.f,0.f,-1.f); aicam->mLookAt = aiVector3D(0.f, 0.f, -1.f);
if (cam.type == Camera::Perspective) { if (cam.type == Camera::Perspective) {
@ -733,38 +707,38 @@ void glTF2Importer::ImportCameras(glTF2::Asset& r)
aicam->mClipPlaneNear = cam.cameraProperties.ortographic.znear; aicam->mClipPlaneNear = cam.cameraProperties.ortographic.znear;
aicam->mHorizontalFOV = 0.0; aicam->mHorizontalFOV = 0.0;
aicam->mAspect = 1.0f; aicam->mAspect = 1.0f;
if (0.f != cam.cameraProperties.ortographic.ymag ) { if (0.f != cam.cameraProperties.ortographic.ymag) {
aicam->mAspect = cam.cameraProperties.ortographic.xmag / cam.cameraProperties.ortographic.ymag; aicam->mAspect = cam.cameraProperties.ortographic.xmag / cam.cameraProperties.ortographic.ymag;
} }
} }
} }
} }
void glTF2Importer::ImportLights(glTF2::Asset& r) void glTF2Importer::ImportLights(glTF2::Asset &r) {
{
if (!r.lights.Size()) if (!r.lights.Size())
return; return;
mScene->mNumLights = r.lights.Size(); mScene->mNumLights = r.lights.Size();
mScene->mLights = new aiLight*[r.lights.Size()]; mScene->mLights = new aiLight *[r.lights.Size()];
for (size_t i = 0; i < r.lights.Size(); ++i) { for (size_t i = 0; i < r.lights.Size(); ++i) {
Light& light = r.lights[i]; Light &light = r.lights[i];
aiLight* ail = mScene->mLights[i] = new aiLight(); aiLight *ail = mScene->mLights[i] = new aiLight();
switch (light.type) switch (light.type) {
{
case Light::Directional: case Light::Directional:
ail->mType = aiLightSource_DIRECTIONAL; break; ail->mType = aiLightSource_DIRECTIONAL;
break;
case Light::Point: case Light::Point:
ail->mType = aiLightSource_POINT; break; ail->mType = aiLightSource_POINT;
break;
case Light::Spot: case Light::Spot:
ail->mType = aiLightSource_SPOT; break; ail->mType = aiLightSource_SPOT;
break;
} }
if (ail->mType != aiLightSource_POINT) if (ail->mType != aiLightSource_POINT) {
{
ail->mDirection = aiVector3D(0.0f, 0.0f, -1.0f); ail->mDirection = aiVector3D(0.0f, 0.0f, -1.0f);
ail->mUp = aiVector3D(0.0f, 1.0f, 0.0f); ail->mUp = aiVector3D(0.0f, 1.0f, 0.0f);
} }
@ -774,14 +748,11 @@ void glTF2Importer::ImportLights(glTF2::Asset& r)
CopyValue(colorWithIntensity, ail->mColorDiffuse); CopyValue(colorWithIntensity, ail->mColorDiffuse);
CopyValue(colorWithIntensity, ail->mColorSpecular); CopyValue(colorWithIntensity, ail->mColorSpecular);
if (ail->mType == aiLightSource_DIRECTIONAL) if (ail->mType == aiLightSource_DIRECTIONAL) {
{
ail->mAttenuationConstant = 1.0; ail->mAttenuationConstant = 1.0;
ail->mAttenuationLinear = 0.0; ail->mAttenuationLinear = 0.0;
ail->mAttenuationQuadratic = 0.0; ail->mAttenuationQuadratic = 0.0;
} } else {
else
{
//in PBR attenuation is calculated using inverse square law which can be expressed //in PBR attenuation is calculated using inverse square law which can be expressed
//using assimps equation: 1/(att0 + att1 * d + att2 * d*d) with the following parameters //using assimps equation: 1/(att0 + att1 * d + att2 * d*d) with the following parameters
//this is correct equation for the case when range (see //this is correct equation for the case when range (see
@ -795,19 +766,17 @@ void glTF2Importer::ImportLights(glTF2::Asset& r)
ail->mAttenuationQuadratic = 1.0; ail->mAttenuationQuadratic = 1.0;
} }
if (ail->mType == aiLightSource_SPOT) if (ail->mType == aiLightSource_SPOT) {
{
ail->mAngleInnerCone = light.innerConeAngle; ail->mAngleInnerCone = light.innerConeAngle;
ail->mAngleOuterCone = light.outerConeAngle; ail->mAngleOuterCone = light.outerConeAngle;
} }
} }
} }
static void GetNodeTransform(aiMatrix4x4& matrix, const glTF2::Node& node) { static void GetNodeTransform(aiMatrix4x4 &matrix, const glTF2::Node &node) {
if (node.matrix.isPresent) { if (node.matrix.isPresent) {
CopyValue(node.matrix.value, matrix); CopyValue(node.matrix.value, matrix);
} } else {
else {
if (node.translation.isPresent) { if (node.translation.isPresent) {
aiVector3D trans; aiVector3D trans;
CopyValue(node.translation.value, trans); CopyValue(node.translation.value, trans);
@ -832,9 +801,8 @@ static void GetNodeTransform(aiMatrix4x4& matrix, const glTF2::Node& node) {
} }
} }
static void BuildVertexWeightMapping(Mesh::Primitive& primitive, std::vector<std::vector<aiVertexWeight>>& map) static void BuildVertexWeightMapping(Mesh::Primitive &primitive, std::vector<std::vector<aiVertexWeight>> &map) {
{ Mesh::Primitive::Attributes &attr = primitive.attributes;
Mesh::Primitive::Attributes& attr = primitive.attributes;
if (attr.weight.empty() || attr.joint.empty()) { if (attr.weight.empty() || attr.joint.empty()) {
return; return;
} }
@ -844,17 +812,23 @@ static void BuildVertexWeightMapping(Mesh::Primitive& primitive, std::vector<std
size_t num_vertices = attr.weight[0]->count; size_t num_vertices = attr.weight[0]->count;
struct Weights { float values[4]; }; struct Weights {
Weights* weights = nullptr; float values[4];
};
Weights *weights = nullptr;
attr.weight[0]->ExtractData(weights); attr.weight[0]->ExtractData(weights);
struct Indices8 { uint8_t values[4]; }; struct Indices8 {
struct Indices16 { uint16_t values[4]; }; uint8_t values[4];
Indices8* indices8 = nullptr; };
Indices16* indices16 = nullptr; struct Indices16 {
uint16_t values[4];
};
Indices8 *indices8 = nullptr;
Indices16 *indices16 = nullptr;
if (attr.joint[0]->GetElementSize() == 4) { if (attr.joint[0]->GetElementSize() == 4) {
attr.joint[0]->ExtractData(indices8); attr.joint[0]->ExtractData(indices8);
}else { } else {
attr.joint[0]->ExtractData(indices16); attr.joint[0]->ExtractData(indices16);
} }
// //
@ -866,7 +840,7 @@ static void BuildVertexWeightMapping(Mesh::Primitive& primitive, std::vector<std
for (size_t i = 0; i < num_vertices; ++i) { for (size_t i = 0; i < num_vertices; ++i) {
for (int j = 0; j < 4; ++j) { for (int j = 0; j < 4; ++j) {
const unsigned int bone = (indices8!=nullptr) ? indices8[i].values[j] : indices16[i].values[j]; const unsigned int bone = (indices8 != nullptr) ? indices8[i].values[j] : indices16[i].values[j];
const float weight = weights[i].values[j]; const float weight = weights[i].values[j];
if (weight > 0 && bone < map.size()) { if (weight > 0 && bone < map.size()) {
map[bone].reserve(8); map[bone].reserve(8);
@ -880,23 +854,21 @@ static void BuildVertexWeightMapping(Mesh::Primitive& primitive, std::vector<std
delete[] indices16; delete[] indices16;
} }
static std::string GetNodeName(const Node& node) static std::string GetNodeName(const Node &node) {
{
return node.name.empty() ? node.id : node.name; return node.name.empty() ? node.id : node.name;
} }
aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>& meshOffsets, glTF2::Ref<glTF2::Node>& ptr) aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &meshOffsets, glTF2::Ref<glTF2::Node> &ptr) {
{ Node &node = *ptr;
Node& node = *ptr;
aiNode* ainode = new aiNode(GetNodeName(node)); aiNode *ainode = new aiNode(GetNodeName(node));
if (!node.children.empty()) { if (!node.children.empty()) {
ainode->mNumChildren = unsigned(node.children.size()); ainode->mNumChildren = unsigned(node.children.size());
ainode->mChildren = new aiNode*[ainode->mNumChildren]; ainode->mChildren = new aiNode *[ainode->mNumChildren];
for (unsigned int i = 0; i < ainode->mNumChildren; ++i) { for (unsigned int i = 0; i < ainode->mNumChildren; ++i) {
aiNode* child = ImportNode(pScene, r, meshOffsets, node.children[i]); aiNode *child = ImportNode(pScene, r, meshOffsets, node.children[i]);
child->mParent = ainode; child->mParent = ainode;
ainode->mChildren[i] = child; ainode->mChildren[i] = child;
} }
@ -915,9 +887,9 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
if (node.skin) { if (node.skin) {
for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) { for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) {
aiMesh* mesh = pScene->mMeshes[meshOffsets[mesh_idx]+primitiveNo]; aiMesh *mesh = pScene->mMeshes[meshOffsets[mesh_idx] + primitiveNo];
mesh->mNumBones = static_cast<unsigned int>(node.skin->jointNames.size()); mesh->mNumBones = static_cast<unsigned int>(node.skin->jointNames.size());
mesh->mBones = new aiBone*[mesh->mNumBones]; mesh->mBones = new aiBone *[mesh->mNumBones];
// GLTF and Assimp choose to store bone weights differently. // GLTF and Assimp choose to store bone weights differently.
// GLTF has each vertex specify which bones influence the vertex. // GLTF has each vertex specify which bones influence the vertex.
@ -931,11 +903,11 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
std::vector<std::vector<aiVertexWeight>> weighting(mesh->mNumBones); std::vector<std::vector<aiVertexWeight>> weighting(mesh->mNumBones);
BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting); BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting);
mat4* pbindMatrices = nullptr; mat4 *pbindMatrices = nullptr;
node.skin->inverseBindMatrices->ExtractData(pbindMatrices); node.skin->inverseBindMatrices->ExtractData(pbindMatrices);
for (uint32_t i = 0; i < mesh->mNumBones; ++i) { for (uint32_t i = 0; i < mesh->mNumBones; ++i) {
aiBone* bone = new aiBone(); aiBone *bone = new aiBone();
Ref<Node> joint = node.skin->jointNames[i]; Ref<Node> joint = node.skin->jointNames[i];
if (!joint->name.empty()) { if (!joint->name.empty()) {
@ -943,7 +915,7 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
} else { } else {
// Assimp expects each bone to have a unique name. // Assimp expects each bone to have a unique name.
static const std::string kDefaultName = "bone_"; static const std::string kDefaultName = "bone_";
char postfix[10] = {0}; char postfix[10] = { 0 };
ASSIMP_itoa10(postfix, i); ASSIMP_itoa10(postfix, i);
bone->mName = (kDefaultName + postfix); bone->mName = (kDefaultName + postfix);
} }
@ -951,7 +923,7 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
CopyValue(pbindMatrices[i], bone->mOffsetMatrix); CopyValue(pbindMatrices[i], bone->mOffsetMatrix);
std::vector<aiVertexWeight>& weights = weighting[i]; std::vector<aiVertexWeight> &weights = weighting[i];
bone->mNumWeights = static_cast<uint32_t>(weights.size()); bone->mNumWeights = static_cast<uint32_t>(weights.size());
if (bone->mNumWeights > 0) { if (bone->mNumWeights > 0) {
@ -988,8 +960,7 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
//range is optional - see https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual //range is optional - see https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
//it is added to meta data of parent node, because there is no other place to put it //it is added to meta data of parent node, because there is no other place to put it
if (node.light->range.isPresent) if (node.light->range.isPresent) {
{
ainode->mMetaData = aiMetadata::Alloc(1); ainode->mMetaData = aiMetadata::Alloc(1);
ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value); ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value);
} }
@ -998,59 +969,52 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector<unsigned int>&
return ainode; return ainode;
} }
void glTF2Importer::ImportNodes(glTF2::Asset& r) void glTF2Importer::ImportNodes(glTF2::Asset &r) {
{
if (!r.scene) return; if (!r.scene) return;
std::vector< Ref<Node> > rootNodes = r.scene->nodes; std::vector<Ref<Node>> rootNodes = r.scene->nodes;
// The root nodes // The root nodes
unsigned int numRootNodes = unsigned(rootNodes.size()); unsigned int numRootNodes = unsigned(rootNodes.size());
if (numRootNodes == 1) { // a single root node: use it if (numRootNodes == 1) { // a single root node: use it
mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]); mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]);
} } else if (numRootNodes > 1) { // more than one root node: create a fake root
else if (numRootNodes > 1) { // more than one root node: create a fake root aiNode *root = new aiNode("ROOT");
aiNode* root = new aiNode("ROOT"); root->mChildren = new aiNode *[numRootNodes];
root->mChildren = new aiNode*[numRootNodes];
for (unsigned int i = 0; i < numRootNodes; ++i) { for (unsigned int i = 0; i < numRootNodes; ++i) {
aiNode* node = ImportNode(mScene, r, meshOffsets, rootNodes[i]); aiNode *node = ImportNode(mScene, r, meshOffsets, rootNodes[i]);
node->mParent = root; node->mParent = root;
root->mChildren[root->mNumChildren++] = node; root->mChildren[root->mNumChildren++] = node;
} }
mScene->mRootNode = root; mScene->mRootNode = root;
} }
//if (!mScene->mRootNode) {
// mScene->mRootNode = new aiNode("EMPTY");
//}
} }
struct AnimationSamplers { struct AnimationSamplers {
AnimationSamplers() AnimationSamplers() :
: translation(nullptr) translation(nullptr),
, rotation(nullptr) rotation(nullptr),
, scale(nullptr) scale(nullptr),
, weight(nullptr) { weight(nullptr) {
// empty // empty
} }
Animation::Sampler* translation; Animation::Sampler *translation;
Animation::Sampler* rotation; Animation::Sampler *rotation;
Animation::Sampler* scale; Animation::Sampler *scale;
Animation::Sampler* weight; Animation::Sampler *weight;
}; };
aiNodeAnim* CreateNodeAnim(glTF2::Asset& r, Node& node, AnimationSamplers& samplers) aiNodeAnim *CreateNodeAnim(glTF2::Asset &r, Node &node, AnimationSamplers &samplers) {
{ aiNodeAnim *anim = new aiNodeAnim();
aiNodeAnim* anim = new aiNodeAnim();
anim->mNodeName = GetNodeName(node); anim->mNodeName = GetNodeName(node);
static const float kMillisecondsFromSeconds = 1000.f; static const float kMillisecondsFromSeconds = 1000.f;
if (samplers.translation) { if (samplers.translation) {
float* times = nullptr; float *times = nullptr;
samplers.translation->input->ExtractData(times); samplers.translation->input->ExtractData(times);
aiVector3D* values = nullptr; aiVector3D *values = nullptr;
samplers.translation->output->ExtractData(values); samplers.translation->output->ExtractData(values);
anim->mNumPositionKeys = static_cast<uint32_t>(samplers.translation->input->count); anim->mNumPositionKeys = static_cast<uint32_t>(samplers.translation->input->count);
anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys]; anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
@ -1070,9 +1034,9 @@ aiNodeAnim* CreateNodeAnim(glTF2::Asset& r, Node& node, AnimationSamplers& sampl
} }
if (samplers.rotation) { if (samplers.rotation) {
float* times = nullptr; float *times = nullptr;
samplers.rotation->input->ExtractData(times); samplers.rotation->input->ExtractData(times);
aiQuaternion* values = nullptr; aiQuaternion *values = nullptr;
samplers.rotation->output->ExtractData(values); samplers.rotation->output->ExtractData(values);
anim->mNumRotationKeys = static_cast<uint32_t>(samplers.rotation->input->count); anim->mNumRotationKeys = static_cast<uint32_t>(samplers.rotation->input->count);
anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys]; anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys];
@ -1096,9 +1060,9 @@ aiNodeAnim* CreateNodeAnim(glTF2::Asset& r, Node& node, AnimationSamplers& sampl
} }
if (samplers.scale) { if (samplers.scale) {
float* times = nullptr; float *times = nullptr;
samplers.scale->input->ExtractData(times); samplers.scale->input->ExtractData(times);
aiVector3D* values = nullptr; aiVector3D *values = nullptr;
samplers.scale->output->ExtractData(values); samplers.scale->output->ExtractData(values);
anim->mNumScalingKeys = static_cast<uint32_t>(samplers.scale->input->count); anim->mNumScalingKeys = static_cast<uint32_t>(samplers.scale->input->count);
anim->mScalingKeys = new aiVectorKey[anim->mNumScalingKeys]; anim->mScalingKeys = new aiVectorKey[anim->mNumScalingKeys];
@ -1120,17 +1084,16 @@ aiNodeAnim* CreateNodeAnim(glTF2::Asset& r, Node& node, AnimationSamplers& sampl
return anim; return anim;
} }
aiMeshMorphAnim* CreateMeshMorphAnim(glTF2::Asset& r, Node& node, AnimationSamplers& samplers) aiMeshMorphAnim *CreateMeshMorphAnim(glTF2::Asset &r, Node &node, AnimationSamplers &samplers) {
{ aiMeshMorphAnim *anim = new aiMeshMorphAnim();
aiMeshMorphAnim* anim = new aiMeshMorphAnim();
anim->mName = GetNodeName(node); anim->mName = GetNodeName(node);
static const float kMillisecondsFromSeconds = 1000.f; static const float kMillisecondsFromSeconds = 1000.f;
if (nullptr != samplers.weight) { if (nullptr != samplers.weight) {
float* times = nullptr; float *times = nullptr;
samplers.weight->input->ExtractData(times); samplers.weight->input->ExtractData(times);
float* values = nullptr; float *values = nullptr;
samplers.weight->output->ExtractData(values); samplers.weight->output->ExtractData(values);
anim->mNumKeys = static_cast<uint32_t>(samplers.weight->input->count); anim->mNumKeys = static_cast<uint32_t>(samplers.weight->input->count);
@ -1146,7 +1109,7 @@ aiMeshMorphAnim* CreateMeshMorphAnim(glTF2::Asset& r, Node& node, AnimationSampl
for (unsigned int j = 0u; j < numMorphs; ++j, ++k) { for (unsigned int j = 0u; j < numMorphs; ++j, ++k) {
anim->mKeys[i].mValues[j] = j; anim->mKeys[i].mValues[j] = j;
anim->mKeys[i].mWeights[j] = ( 0.f > values[k] ) ? 0.f : values[k]; anim->mKeys[i].mWeights[j] = (0.f > values[k]) ? 0.f : values[k];
} }
} }
@ -1157,18 +1120,17 @@ aiMeshMorphAnim* CreateMeshMorphAnim(glTF2::Asset& r, Node& node, AnimationSampl
return anim; return anim;
} }
std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation& anim) std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation &anim) {
{
std::unordered_map<unsigned int, AnimationSamplers> samplers; std::unordered_map<unsigned int, AnimationSamplers> samplers;
for (unsigned int c = 0; c < anim.channels.size(); ++c) { for (unsigned int c = 0; c < anim.channels.size(); ++c) {
Animation::Channel& channel = anim.channels[c]; Animation::Channel &channel = anim.channels[c];
if (channel.sampler >= static_cast<int>(anim.samplers.size())) { if (channel.sampler >= static_cast<int>(anim.samplers.size())) {
continue; continue;
} }
const unsigned int node_index = channel.target.node.GetIndex(); const unsigned int node_index = channel.target.node.GetIndex();
AnimationSamplers& sampler = samplers[node_index]; AnimationSamplers &sampler = samplers[node_index];
if (channel.target.path == AnimationPath_TRANSLATION) { if (channel.target.path == AnimationPath_TRANSLATION) {
sampler.translation = &anim.samplers[channel.sampler]; sampler.translation = &anim.samplers[channel.sampler];
} else if (channel.target.path == AnimationPath_ROTATION) { } else if (channel.target.path == AnimationPath_ROTATION) {
@ -1183,8 +1145,7 @@ std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation& an
return samplers; return samplers;
} }
void glTF2Importer::ImportAnimations(glTF2::Asset& r) void glTF2Importer::ImportAnimations(glTF2::Asset &r) {
{
if (!r.scene) return; if (!r.scene) return;
mScene->mNumAnimations = r.animations.Size(); mScene->mNumAnimations = r.animations.Size();
@ -1192,11 +1153,11 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
return; return;
} }
mScene->mAnimations = new aiAnimation*[mScene->mNumAnimations]; mScene->mAnimations = new aiAnimation *[mScene->mNumAnimations];
for (unsigned int i = 0; i < r.animations.Size(); ++i) { for (unsigned int i = 0; i < r.animations.Size(); ++i) {
Animation& anim = r.animations[i]; Animation &anim = r.animations[i];
aiAnimation* ai_anim = new aiAnimation(); aiAnimation *ai_anim = new aiAnimation();
ai_anim->mName = anim.name; ai_anim->mName = anim.name;
ai_anim->mDuration = 0; ai_anim->mDuration = 0;
ai_anim->mTicksPerSecond = 0; ai_anim->mTicksPerSecond = 0;
@ -1206,7 +1167,7 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
uint32_t numChannels = 0u; uint32_t numChannels = 0u;
uint32_t numMorphMeshChannels = 0u; uint32_t numMorphMeshChannels = 0u;
for (auto& iter : samplers) { for (auto &iter : samplers) {
if ((nullptr != iter.second.rotation) || (nullptr != iter.second.scale) || (nullptr != iter.second.translation)) { if ((nullptr != iter.second.rotation) || (nullptr != iter.second.scale) || (nullptr != iter.second.translation)) {
++numChannels; ++numChannels;
} }
@ -1217,9 +1178,9 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
ai_anim->mNumChannels = numChannels; ai_anim->mNumChannels = numChannels;
if (ai_anim->mNumChannels > 0) { if (ai_anim->mNumChannels > 0) {
ai_anim->mChannels = new aiNodeAnim*[ai_anim->mNumChannels]; ai_anim->mChannels = new aiNodeAnim *[ai_anim->mNumChannels];
int j = 0; int j = 0;
for (auto& iter : samplers) { for (auto &iter : samplers) {
if ((nullptr != iter.second.rotation) || (nullptr != iter.second.scale) || (nullptr != iter.second.translation)) { if ((nullptr != iter.second.rotation) || (nullptr != iter.second.scale) || (nullptr != iter.second.translation)) {
ai_anim->mChannels[j] = CreateNodeAnim(r, r.nodes[iter.first], iter.second); ai_anim->mChannels[j] = CreateNodeAnim(r, r.nodes[iter.first], iter.second);
++j; ++j;
@ -1229,9 +1190,9 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
ai_anim->mNumMorphMeshChannels = numMorphMeshChannels; ai_anim->mNumMorphMeshChannels = numMorphMeshChannels;
if (ai_anim->mNumMorphMeshChannels > 0) { if (ai_anim->mNumMorphMeshChannels > 0) {
ai_anim->mMorphMeshChannels = new aiMeshMorphAnim*[ai_anim->mNumMorphMeshChannels]; ai_anim->mMorphMeshChannels = new aiMeshMorphAnim *[ai_anim->mNumMorphMeshChannels];
int j = 0; int j = 0;
for (auto& iter : samplers) { for (auto &iter : samplers) {
if (nullptr != iter.second.weight) { if (nullptr != iter.second.weight) {
ai_anim->mMorphMeshChannels[j] = CreateMeshMorphAnim(r, r.nodes[iter.first], iter.second); ai_anim->mMorphMeshChannels[j] = CreateMeshMorphAnim(r, r.nodes[iter.first], iter.second);
++j; ++j;
@ -1268,10 +1229,10 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
} }
for (unsigned int j = 0; j < ai_anim->mNumMorphMeshChannels; ++j) { for (unsigned int j = 0; j < ai_anim->mNumMorphMeshChannels; ++j) {
const auto* const chan = ai_anim->mMorphMeshChannels[j]; const auto *const chan = ai_anim->mMorphMeshChannels[j];
if (0u != chan->mNumKeys) { if (0u != chan->mNumKeys) {
const auto& lastKey = chan->mKeys[chan->mNumKeys - 1u]; const auto &lastKey = chan->mKeys[chan->mNumKeys - 1u];
if (lastKey.mTime > maxDuration) { if (lastKey.mTime > maxDuration) {
maxDuration = lastKey.mTime; maxDuration = lastKey.mTime;
} }
@ -1286,8 +1247,7 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
} }
} }
void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r) void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
{
embeddedTexIdxs.resize(r.images.Size(), -1); embeddedTexIdxs.resize(r.images.Size(), -1);
int numEmbeddedTexs = 0; int numEmbeddedTexs = 0;
@ -1299,7 +1259,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
if (numEmbeddedTexs == 0) if (numEmbeddedTexs == 0)
return; return;
mScene->mTextures = new aiTexture*[numEmbeddedTexs]; mScene->mTextures = new aiTexture *[numEmbeddedTexs];
// Add the embedded textures // Add the embedded textures
for (size_t i = 0; i < r.images.Size(); ++i) { for (size_t i = 0; i < r.images.Size(); ++i) {
@ -1309,17 +1269,17 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
int idx = mScene->mNumTextures++; int idx = mScene->mNumTextures++;
embeddedTexIdxs[i] = idx; embeddedTexIdxs[i] = idx;
aiTexture* tex = mScene->mTextures[idx] = new aiTexture(); aiTexture *tex = mScene->mTextures[idx] = new aiTexture();
size_t length = img.GetDataLength(); size_t length = img.GetDataLength();
void* data = img.StealData(); void *data = img.StealData();
tex->mWidth = static_cast<unsigned int>(length); tex->mWidth = static_cast<unsigned int>(length);
tex->mHeight = 0; tex->mHeight = 0;
tex->pcData = reinterpret_cast<aiTexel*>(data); tex->pcData = reinterpret_cast<aiTexel *>(data);
if (!img.mimeType.empty()) { if (!img.mimeType.empty()) {
const char* ext = strchr(img.mimeType.c_str(), '/') + 1; const char *ext = strchr(img.mimeType.c_str(), '/') + 1;
if (ext) { if (ext) {
if (strcmp(ext, "jpeg") == 0) ext = "jpg"; if (strcmp(ext, "jpeg") == 0) ext = "jpg";
@ -1332,8 +1292,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset& r)
} }
} }
void glTF2Importer::InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) void glTF2Importer::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
{
// clean all member arrays // clean all member arrays
meshOffsets.clear(); meshOffsets.clear();
embeddedTexIdxs.clear(); embeddedTexIdxs.clear();
@ -1366,4 +1325,3 @@ void glTF2Importer::InternReadFile(const std::string& pFile, aiScene* pScene, IO
} }
#endif // ASSIMP_BUILD_NO_GLTF_IMPORTER #endif // ASSIMP_BUILD_NO_GLTF_IMPORTER