123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209 |
- /*
- Open Asset Import Library (assimp)
- ----------------------------------------------------------------------
- Copyright (c) 2006-2020, assimp team
- All rights reserved.
- Redistribution and use of this software in source and binary forms,
- with or without modification, are permitted provided that the
- following conditions are met:
- * Redistributions of source code must retain the above
- copyright notice, this list of conditions and the
- following disclaimer.
- * Redistributions in binary form must reproduce the above
- copyright notice, this list of conditions and the
- following disclaimer in the documentation and/or other
- materials provided with the distribution.
- * Neither the name of the assimp team, nor the names of its
- contributors may be used to endorse or promote products
- derived from this software without specific prior
- written permission of the assimp team.
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ----------------------------------------------------------------------
- */
- #ifndef ASSIMP_BUILD_NO_EXPORT
- #ifndef ASSIMP_BUILD_NO_GLTF_EXPORTER
- #include "AssetLib/glTF2/glTF2Exporter.h"
- #include "AssetLib/glTF2/glTF2AssetWriter.h"
- #include "PostProcessing/SplitLargeMeshes.h"
- #include <assimp/commonMetaData.h>
- #include <assimp/Exceptional.h>
- #include <assimp/StringComparison.h>
- #include <assimp/ByteSwapper.h>
- #include <assimp/SceneCombiner.h>
- #include <assimp/version.h>
- #include <assimp/IOSystem.hpp>
- #include <assimp/Exporter.hpp>
- #include <assimp/material.h>
- #include <assimp/scene.h>
- // Header files, standard library.
- #include <memory>
- #include <limits>
- #include <inttypes.h>
- using namespace rapidjson;
- using namespace Assimp;
- using namespace glTF2;
- namespace Assimp {
- // ------------------------------------------------------------------------------------------------
- // Worker function for exporting a scene to GLTF. Prototyped and registered in Exporter.cpp
- void ExportSceneGLTF2(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
- {
- // invoke the exporter
- glTF2Exporter exporter(pFile, pIOSystem, pScene, pProperties, false);
- }
- // ------------------------------------------------------------------------------------------------
- // Worker function for exporting a scene to GLB. Prototyped and registered in Exporter.cpp
- void ExportSceneGLB2(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
- {
- // invoke the exporter
- glTF2Exporter exporter(pFile, pIOSystem, pScene, pProperties, true);
- }
- } // end of namespace Assimp
- glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const aiScene* pScene,
- const ExportProperties* pProperties, bool isBinary)
- : mFilename(filename)
- , mIOSystem(pIOSystem)
- , mProperties(pProperties)
- {
- mScene = pScene;
- mAsset.reset( new Asset( pIOSystem ) );
- if (isBinary) {
- mAsset->SetAsBinary();
- }
- ExportMetadata();
- ExportMaterials();
- if (mScene->mRootNode) {
- ExportNodeHierarchy(mScene->mRootNode);
- }
- ExportMeshes();
- MergeMeshes();
- ExportScene();
- ExportAnimations();
- AssetWriter writer(*mAsset);
- if (isBinary) {
- writer.WriteGLBFile(filename);
- } else {
- writer.WriteFile(filename);
- }
- }
- glTF2Exporter::~glTF2Exporter() {
- // empty
- }
- /*
- * Copy a 4x4 matrix from struct aiMatrix to typedef mat4.
- * Also converts from row-major to column-major storage.
- */
- static void CopyValue(const aiMatrix4x4& v, mat4& o) {
- o[ 0] = v.a1; o[ 1] = v.b1; o[ 2] = v.c1; o[ 3] = v.d1;
- o[ 4] = v.a2; o[ 5] = v.b2; o[ 6] = v.c2; o[ 7] = v.d2;
- o[ 8] = v.a3; o[ 9] = v.b3; o[10] = v.c3; o[11] = v.d3;
- o[12] = v.a4; o[13] = v.b4; o[14] = v.c4; o[15] = v.d4;
- }
- static void CopyValue(const aiMatrix4x4& v, aiMatrix4x4& o) {
- memcpy(&o, &v, sizeof(aiMatrix4x4));
- }
- static void IdentityMatrix4(mat4& o) {
- o[ 0] = 1; o[ 1] = 0; o[ 2] = 0; o[ 3] = 0;
- o[ 4] = 0; o[ 5] = 1; o[ 6] = 0; o[ 7] = 0;
- o[ 8] = 0; o[ 9] = 0; o[10] = 1; o[11] = 0;
- o[12] = 0; o[13] = 0; o[14] = 0; o[15] = 1;
- }
- template<typename T>
- void SetAccessorRange(Ref<Accessor> acc, void* data, size_t count,
- unsigned int numCompsIn, unsigned int numCompsOut)
- {
- ai_assert(numCompsOut <= numCompsIn);
- // Allocate and initialize with large values.
- for (unsigned int i = 0 ; i < numCompsOut ; i++) {
- acc->min.push_back( std::numeric_limits<double>::max());
- acc->max.push_back(-std::numeric_limits<double>::max());
- }
- size_t totalComps = count * numCompsIn;
- T* buffer_ptr = static_cast<T*>(data);
- T* buffer_end = buffer_ptr + totalComps;
- // Search and set extreme values.
- for (; buffer_ptr < buffer_end ; buffer_ptr += numCompsIn) {
- for (unsigned int j = 0 ; j < numCompsOut ; j++) {
- double valueTmp = buffer_ptr[j];
- // Gracefully tolerate rogue NaN's in buffer data
- // Any NaNs/Infs introduced in accessor bounds will end up in
- // document and prevent rapidjson from writing out valid JSON
- if (!std::isfinite(valueTmp)) {
- continue;
- }
- if (valueTmp < acc->min[j]) {
- acc->min[j] = valueTmp;
- }
- if (valueTmp > acc->max[j]) {
- acc->max[j] = valueTmp;
- }
- }
- }
- }
- inline void SetAccessorRange(ComponentType compType, Ref<Accessor> acc, void* data,
- size_t count, unsigned int numCompsIn, unsigned int numCompsOut)
- {
- switch (compType) {
- case ComponentType_SHORT:
- SetAccessorRange<short>(acc, data, count, numCompsIn, numCompsOut);
- return;
- case ComponentType_UNSIGNED_SHORT:
- SetAccessorRange<unsigned short>(acc, data, count, numCompsIn, numCompsOut);
- return;
- case ComponentType_UNSIGNED_INT:
- SetAccessorRange<unsigned int>(acc, data, count, numCompsIn, numCompsOut);
- return;
- case ComponentType_FLOAT:
- SetAccessorRange<float>(acc, data, count, numCompsIn, numCompsOut);
- return;
- case ComponentType_BYTE:
- SetAccessorRange<int8_t>(acc, data, count, numCompsIn, numCompsOut);
- return;
- case ComponentType_UNSIGNED_BYTE:
- SetAccessorRange<uint8_t>(acc, data, count, numCompsIn, numCompsOut);
- return;
- }
- }
- inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& buffer,
- size_t count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, BufferViewTarget target = BufferViewTarget_NONE)
- {
- if (!count || !data) {
- return Ref<Accessor>();
- }
- unsigned int numCompsIn = AttribType::GetNumComponents(typeIn);
- unsigned int numCompsOut = AttribType::GetNumComponents(typeOut);
- unsigned int bytesPerComp = ComponentTypeSize(compType);
- size_t offset = buffer->byteLength;
- // make sure offset is correctly byte-aligned, as required by spec
- size_t padding = offset % bytesPerComp;
- offset += padding;
- size_t length = count * numCompsOut * bytesPerComp;
- buffer->Grow(length + padding);
- // bufferView
- Ref<BufferView> bv = a.bufferViews.Create(a.FindUniqueID(meshName, "view"));
- bv->buffer = buffer;
- bv->byteOffset = offset;
- bv->byteLength = length; //! The target that the WebGL buffer should be bound to.
- bv->byteStride = 0;
- bv->target = target;
- // accessor
- Ref<Accessor> acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor"));
- acc->bufferView = bv;
- acc->byteOffset = 0;
- acc->componentType = compType;
- acc->count = count;
- acc->type = typeOut;
- // calculate min and max values
- SetAccessorRange(compType, acc, data, count, numCompsIn, numCompsOut);
- // copy the data
- acc->WriteData(count, data, numCompsIn*bytesPerComp);
- return acc;
- }
- inline void SetSamplerWrap(SamplerWrap& wrap, aiTextureMapMode map)
- {
- switch (map) {
- case aiTextureMapMode_Clamp:
- wrap = SamplerWrap::Clamp_To_Edge;
- break;
- case aiTextureMapMode_Mirror:
- wrap = SamplerWrap::Mirrored_Repeat;
- break;
- case aiTextureMapMode_Wrap:
- case aiTextureMapMode_Decal:
- default:
- wrap = SamplerWrap::Repeat;
- break;
- };
- }
- void glTF2Exporter::GetTexSampler(const aiMaterial* mat, Ref<Texture> texture, aiTextureType tt, unsigned int slot)
- {
- aiString aId;
- std::string id;
- if (aiGetMaterialString(mat, AI_MATKEY_GLTF_MAPPINGID(tt, slot), &aId) == AI_SUCCESS) {
- id = aId.C_Str();
- }
- if (Ref<Sampler> ref = mAsset->samplers.Get(id.c_str())) {
- texture->sampler = ref;
- } else {
- id = mAsset->FindUniqueID(id, "sampler");
- texture->sampler = mAsset->samplers.Create(id.c_str());
- aiTextureMapMode mapU, mapV;
- SamplerMagFilter filterMag;
- SamplerMinFilter filterMin;
- if (aiGetMaterialInteger(mat, AI_MATKEY_MAPPINGMODE_U(tt, slot), (int*)&mapU) == AI_SUCCESS) {
- SetSamplerWrap(texture->sampler->wrapS, mapU);
- }
- if (aiGetMaterialInteger(mat, AI_MATKEY_MAPPINGMODE_V(tt, slot), (int*)&mapV) == AI_SUCCESS) {
- SetSamplerWrap(texture->sampler->wrapT, mapV);
- }
- if (aiGetMaterialInteger(mat, AI_MATKEY_GLTF_MAPPINGFILTER_MAG(tt, slot), (int*)&filterMag) == AI_SUCCESS) {
- texture->sampler->magFilter = filterMag;
- }
- if (aiGetMaterialInteger(mat, AI_MATKEY_GLTF_MAPPINGFILTER_MIN(tt, slot), (int*)&filterMin) == AI_SUCCESS) {
- texture->sampler->minFilter = filterMin;
- }
- aiString name;
- if (aiGetMaterialString(mat, AI_MATKEY_GLTF_MAPPINGNAME(tt, slot), &name) == AI_SUCCESS) {
- texture->sampler->name = name.C_Str();
- }
- }
- }
- void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int slot)
- {
- std::string textureKey = std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName;
- mat->Get(textureKey.c_str(), tt, slot, prop);
- }
- void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, float& prop, const char* propName, aiTextureType tt, unsigned int slot)
- {
- std::string textureKey = std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName;
- mat->Get(textureKey.c_str(), tt, slot, prop);
- }
- void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTextureType tt, unsigned int slot = 0)
- {
- if (mat->GetTextureCount(tt) > 0) {
- aiString tex;
- if (mat->Get(AI_MATKEY_TEXTURE(tt, slot), tex) == AI_SUCCESS) {
- std::string path = tex.C_Str();
- if (path.size() > 0) {
- std::map<std::string, unsigned int>::iterator it = mTexturesByPath.find(path);
- if (it != mTexturesByPath.end()) {
- texture = mAsset->textures.Get(it->second);
- }
- if (!texture) {
- std::string texId = mAsset->FindUniqueID("", "texture");
- texture = mAsset->textures.Create(texId);
- mTexturesByPath[path] = texture.GetIndex();
- std::string imgId = mAsset->FindUniqueID("", "image");
- texture->source = mAsset->images.Create(imgId);
- if (path[0] == '*') { // embedded
- aiTexture* curTex = mScene->mTextures[atoi(&path[1])];
- texture->source->name = curTex->mFilename.C_Str();
- // The asset has its own buffer, see Image::SetData
- texture->source->SetData(reinterpret_cast<uint8_t *>(curTex->pcData), curTex->mWidth, *mAsset);
- if (curTex->achFormatHint[0]) {
- std::string mimeType = "image/";
- mimeType += (memcmp(curTex->achFormatHint, "jpg", 3) == 0) ? "jpeg" : curTex->achFormatHint;
- texture->source->mimeType = mimeType;
- }
- }
- else {
- texture->source->uri = path;
- }
- GetTexSampler(mat, texture, tt, slot);
- }
- }
- }
- }
- }
- void glTF2Exporter::GetMatTex(const aiMaterial* mat, TextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
- {
- Ref<Texture>& texture = prop.texture;
- GetMatTex(mat, texture, tt, slot);
- if (texture) {
- GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
- }
- }
- void glTF2Exporter::GetMatTex(const aiMaterial* mat, NormalTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
- {
- Ref<Texture>& texture = prop.texture;
- GetMatTex(mat, texture, tt, slot);
- if (texture) {
- GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
- GetMatTexProp(mat, prop.scale, "scale", tt, slot);
- }
- }
- void glTF2Exporter::GetMatTex(const aiMaterial* mat, OcclusionTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
- {
- Ref<Texture>& texture = prop.texture;
- GetMatTex(mat, texture, tt, slot);
- if (texture) {
- GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
- GetMatTexProp(mat, prop.strength, "strength", tt, slot);
- }
- }
- aiReturn glTF2Exporter::GetMatColor(const aiMaterial* mat, vec4& prop, const char* propName, int type, int idx)
- {
- aiColor4D col;
- aiReturn result = mat->Get(propName, type, idx, col);
- if (result == AI_SUCCESS) {
- prop[0] = col.r; prop[1] = col.g; prop[2] = col.b; prop[3] = col.a;
- }
- return result;
- }
- aiReturn glTF2Exporter::GetMatColor(const aiMaterial* mat, vec3& prop, const char* propName, int type, int idx)
- {
- aiColor3D col;
- aiReturn result = mat->Get(propName, type, idx, col);
- if (result == AI_SUCCESS) {
- prop[0] = col.r; prop[1] = col.g; prop[2] = col.b;
- }
- return result;
- }
- void glTF2Exporter::ExportMaterials()
- {
- aiString aiName;
- for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
- const aiMaterial* mat = mScene->mMaterials[i];
- std::string id = "material_" + to_string(i);
- Ref<Material> m = mAsset->materials.Create(id);
- std::string name;
- if (mat->Get(AI_MATKEY_NAME, aiName) == AI_SUCCESS) {
- name = aiName.C_Str();
- }
- name = mAsset->FindUniqueID(name, "material");
- m->name = name;
- GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE);
- if (!m->pbrMetallicRoughness.baseColorTexture.texture) {
- //if there wasn't a baseColorTexture defined in the source, fallback to any diffuse texture
- GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, aiTextureType_DIFFUSE);
- }
- GetMatTex(mat, m->pbrMetallicRoughness.metallicRoughnessTexture, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE);
- if (GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_FACTOR) != AI_SUCCESS) {
- // if baseColorFactor wasn't defined, then the source is likely not a metallic roughness material.
- //a fallback to any diffuse color should be used instead
- GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_COLOR_DIFFUSE);
- }
- if (mat->Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR, m->pbrMetallicRoughness.metallicFactor) != AI_SUCCESS) {
- //if metallicFactor wasn't defined, then the source is likely not a PBR file, and the metallicFactor should be 0
- m->pbrMetallicRoughness.metallicFactor = 0;
- }
- // get roughness if source is gltf2 file
- if (mat->Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR, m->pbrMetallicRoughness.roughnessFactor) != AI_SUCCESS) {
- // otherwise, try to derive and convert from specular + shininess values
- aiColor4D specularColor;
- ai_real shininess;
- if (
- mat->Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS &&
- mat->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS
- ) {
- // convert specular color to luminance
- float specularIntensity = specularColor[0] * 0.2125f + specularColor[1] * 0.7154f + specularColor[2] * 0.0721f;
- //normalize shininess (assuming max is 1000) with an inverse exponentional curve
- float normalizedShininess = std::sqrt(shininess / 1000);
- //clamp the shininess value between 0 and 1
- normalizedShininess = std::min(std::max(normalizedShininess, 0.0f), 1.0f);
- // low specular intensity values should produce a rough material even if shininess is high.
- normalizedShininess = normalizedShininess * specularIntensity;
- m->pbrMetallicRoughness.roughnessFactor = 1 - normalizedShininess;
- }
- }
- GetMatTex(mat, m->normalTexture, aiTextureType_NORMALS);
- GetMatTex(mat, m->occlusionTexture, aiTextureType_LIGHTMAP);
- GetMatTex(mat, m->emissiveTexture, aiTextureType_EMISSIVE);
- GetMatColor(mat, m->emissiveFactor, AI_MATKEY_COLOR_EMISSIVE);
- mat->Get(AI_MATKEY_TWOSIDED, m->doubleSided);
- mat->Get(AI_MATKEY_GLTF_ALPHACUTOFF, m->alphaCutoff);
- aiString alphaMode;
- if (mat->Get(AI_MATKEY_GLTF_ALPHAMODE, alphaMode) == AI_SUCCESS) {
- m->alphaMode = alphaMode.C_Str();
- } else {
- float opacity;
- if (mat->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) {
- if (opacity < 1) {
- m->alphaMode = "BLEND";
- m->pbrMetallicRoughness.baseColorFactor[3] *= opacity;
- }
- }
- }
- bool hasPbrSpecularGlossiness = false;
- mat->Get(AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS, hasPbrSpecularGlossiness);
- if (hasPbrSpecularGlossiness) {
- if (!mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
- mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness = true;
- }
- PbrSpecularGlossiness pbrSG;
- GetMatColor(mat, pbrSG.diffuseFactor, AI_MATKEY_COLOR_DIFFUSE);
- GetMatColor(mat, pbrSG.specularFactor, AI_MATKEY_COLOR_SPECULAR);
- if (mat->Get(AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS_GLOSSINESS_FACTOR, pbrSG.glossinessFactor) != AI_SUCCESS) {
- float shininess;
- if (mat->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) {
- pbrSG.glossinessFactor = shininess / 1000;
- }
- }
- GetMatTex(mat, pbrSG.diffuseTexture, aiTextureType_DIFFUSE);
- GetMatTex(mat, pbrSG.specularGlossinessTexture, aiTextureType_SPECULAR);
- m->pbrSpecularGlossiness = Nullable<PbrSpecularGlossiness>(pbrSG);
- }
- bool unlit;
- if (mat->Get(AI_MATKEY_GLTF_UNLIT, unlit) == AI_SUCCESS && unlit) {
- mAsset->extensionsUsed.KHR_materials_unlit = true;
- m->unlit = true;
- }
- }
- }
- /*
- * Search through node hierarchy and find the node containing the given meshID.
- * Returns true on success, and false otherwise.
- */
- bool FindMeshNode(Ref<Node>& nodeIn, Ref<Node>& meshNode, std::string meshID)
- {
- for (unsigned int i = 0; i < nodeIn->meshes.size(); ++i) {
- if (meshID.compare(nodeIn->meshes[i]->id) == 0) {
- meshNode = nodeIn;
- return true;
- }
- }
- for (unsigned int i = 0; i < nodeIn->children.size(); ++i) {
- if(FindMeshNode(nodeIn->children[i], meshNode, meshID)) {
- return true;
- }
- }
- return false;
- }
- /*
- * Find the root joint of the skeleton.
- * Starts will any joint node and traces up the tree,
- * until a parent is found that does not have a jointName.
- * Returns the first parent Ref<Node> found that does not have a jointName.
- */
- Ref<Node> FindSkeletonRootJoint(Ref<Skin>& skinRef)
- {
- Ref<Node> startNodeRef;
- Ref<Node> parentNodeRef;
- // Arbitrarily use the first joint to start the search.
- startNodeRef = skinRef->jointNames[0];
- parentNodeRef = skinRef->jointNames[0];
- do {
- startNodeRef = parentNodeRef;
- parentNodeRef = startNodeRef->parent;
- } while (!parentNodeRef->jointName.empty());
- return parentNodeRef;
- }
- void ExportSkin(Asset& mAsset, const aiMesh* aimesh, Ref<Mesh>& meshRef, Ref<Buffer>& bufferRef, Ref<Skin>& skinRef, std::vector<aiMatrix4x4>& inverseBindMatricesData)
- {
- if (aimesh->mNumBones < 1) {
- return;
- }
- // Store the vertex joint and weight data.
- const size_t NumVerts( aimesh->mNumVertices );
- vec4* vertexJointData = new vec4[ NumVerts ];
- vec4* vertexWeightData = new vec4[ NumVerts ];
- int* jointsPerVertex = new int[ NumVerts ];
- for (size_t i = 0; i < NumVerts; ++i) {
- jointsPerVertex[i] = 0;
- for (size_t j = 0; j < 4; ++j) {
- vertexJointData[i][j] = 0;
- vertexWeightData[i][j] = 0;
- }
- }
- for (unsigned int idx_bone = 0; idx_bone < aimesh->mNumBones; ++idx_bone) {
- const aiBone* aib = aimesh->mBones[idx_bone];
- // aib->mName =====> skinRef->jointNames
- // Find the node with id = mName.
- Ref<Node> nodeRef = mAsset.nodes.Get(aib->mName.C_Str());
- nodeRef->jointName = nodeRef->name;
- unsigned int jointNamesIndex = 0;
- bool addJointToJointNames = true;
- for ( unsigned int idx_joint = 0; idx_joint < skinRef->jointNames.size(); ++idx_joint) {
- if (skinRef->jointNames[idx_joint]->jointName.compare(nodeRef->jointName) == 0) {
- addJointToJointNames = false;
- jointNamesIndex = idx_joint;
- }
- }
- if (addJointToJointNames) {
- skinRef->jointNames.push_back(nodeRef);
- // aib->mOffsetMatrix =====> skinRef->inverseBindMatrices
- aiMatrix4x4 tmpMatrix4;
- CopyValue(aib->mOffsetMatrix, tmpMatrix4);
- inverseBindMatricesData.push_back(tmpMatrix4);
- jointNamesIndex = static_cast<unsigned int>(inverseBindMatricesData.size() - 1);
- }
- // aib->mWeights =====> vertexWeightData
- for (unsigned int idx_weights = 0; idx_weights < aib->mNumWeights; ++idx_weights) {
- unsigned int vertexId = aib->mWeights[idx_weights].mVertexId;
- float vertWeight = aib->mWeights[idx_weights].mWeight;
- // A vertex can only have at most four joint weights. Ignore all others.
- if (jointsPerVertex[vertexId] > 3) {
- continue;
- }
- vertexJointData[vertexId][jointsPerVertex[vertexId]] = static_cast<float>(jointNamesIndex);
- vertexWeightData[vertexId][jointsPerVertex[vertexId]] = vertWeight;
- jointsPerVertex[vertexId] += 1;
- }
- } // End: for-loop mNumMeshes
- Mesh::Primitive& p = meshRef->primitives.back();
- Ref<Accessor> vertexJointAccessor = ExportData(mAsset, skinRef->id, bufferRef, aimesh->mNumVertices, vertexJointData, AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT);
- if ( vertexJointAccessor ) {
- size_t offset = vertexJointAccessor->bufferView->byteOffset;
- size_t bytesLen = vertexJointAccessor->bufferView->byteLength;
- unsigned int s_bytesPerComp= ComponentTypeSize(ComponentType_UNSIGNED_SHORT);
- unsigned int bytesPerComp = ComponentTypeSize(vertexJointAccessor->componentType);
- size_t s_bytesLen = bytesLen * s_bytesPerComp / bytesPerComp;
- Ref<Buffer> buf = vertexJointAccessor->bufferView->buffer;
- uint8_t* arrys = new uint8_t[bytesLen];
- unsigned int i = 0;
- for ( unsigned int j = 0; j <= bytesLen; j += bytesPerComp ){
- size_t len_p = offset + j;
- float f_value = *(float *)&buf->GetPointer()[len_p];
- unsigned short c = static_cast<unsigned short>(f_value);
- memcpy(&arrys[i*s_bytesPerComp], &c, s_bytesPerComp);
- ++i;
- }
- buf->ReplaceData_joint(offset, bytesLen, arrys, bytesLen);
- vertexJointAccessor->componentType = ComponentType_UNSIGNED_SHORT;
- vertexJointAccessor->bufferView->byteLength = s_bytesLen;
- p.attributes.joint.push_back( vertexJointAccessor );
- delete[] arrys;
- }
- Ref<Accessor> vertexWeightAccessor = ExportData(mAsset, skinRef->id, bufferRef, aimesh->mNumVertices,
- vertexWeightData, AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT);
- if ( vertexWeightAccessor ) {
- p.attributes.weight.push_back( vertexWeightAccessor );
- }
- delete[] jointsPerVertex;
- delete[] vertexWeightData;
- delete[] vertexJointData;
- }
- void glTF2Exporter::ExportMeshes()
- {
- typedef decltype(aiFace::mNumIndices) IndicesType;
- std::string fname = std::string(mFilename);
- std::string bufferIdPrefix = fname.substr(0, fname.rfind(".gltf"));
- std::string bufferId = mAsset->FindUniqueID("", bufferIdPrefix.c_str());
- Ref<Buffer> b = mAsset->GetBodyBuffer();
- if (!b) {
- b = mAsset->buffers.Create(bufferId);
- }
- //----------------------------------------
- // Initialize variables for the skin
- bool createSkin = false;
- for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) {
- const aiMesh* aim = mScene->mMeshes[idx_mesh];
- if(aim->HasBones()) {
- createSkin = true;
- break;
- }
- }
- Ref<Skin> skinRef;
- std::string skinName = mAsset->FindUniqueID("skin", "skin");
- std::vector<aiMatrix4x4> inverseBindMatricesData;
- if(createSkin) {
- skinRef = mAsset->skins.Create(skinName);
- skinRef->name = skinName;
- }
- //----------------------------------------
- for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) {
- const aiMesh* aim = mScene->mMeshes[idx_mesh];
- std::string name = aim->mName.C_Str();
- std::string meshId = mAsset->FindUniqueID(name, "mesh");
- Ref<Mesh> m = mAsset->meshes.Create(meshId);
- m->primitives.resize(1);
- Mesh::Primitive& p = m->primitives.back();
- m->name = name;
- p.material = mAsset->materials.Get(aim->mMaterialIndex);
- /******************* Vertices ********************/
- Ref<Accessor> v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
- if (v) p.attributes.position.push_back(v);
- /******************** Normals ********************/
- // Normalize all normals as the validator can emit a warning otherwise
- if ( nullptr != aim->mNormals) {
- for ( auto i = 0u; i < aim->mNumVertices; ++i ) {
- aim->mNormals[ i ].NormalizeSafe();
- }
- }
- Ref<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
- if (n) p.attributes.normal.push_back(n);
- /************** Texture coordinates **************/
- for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
- if (!aim->HasTextureCoords(i))
- continue;
- // Flip UV y coords
- if (aim -> mNumUVComponents[i] > 1) {
- for (unsigned int j = 0; j < aim->mNumVertices; ++j) {
- aim->mTextureCoords[i][j].y = 1 - aim->mTextureCoords[i][j].y;
- }
- }
- if (aim->mNumUVComponents[i] > 0) {
- AttribType::Value type = (aim->mNumUVComponents[i] == 2) ? AttribType::VEC2 : AttribType::VEC3;
- Ref<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
- if (tc) p.attributes.texcoord.push_back(tc);
- }
- }
- /*************** Vertex colors ****************/
- for (unsigned int indexColorChannel = 0; indexColorChannel < aim->GetNumColorChannels(); ++indexColorChannel) {
- Ref<Accessor> c = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mColors[indexColorChannel], AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
- if (c)
- p.attributes.color.push_back(c);
- }
- /*************** Vertices indices ****************/
- if (aim->mNumFaces > 0) {
- std::vector<IndicesType> indices;
- unsigned int nIndicesPerFace = aim->mFaces[0].mNumIndices;
- indices.resize(aim->mNumFaces * nIndicesPerFace);
- for (size_t i = 0; i < aim->mNumFaces; ++i) {
- for (size_t j = 0; j < nIndicesPerFace; ++j) {
- indices[i*nIndicesPerFace + j] = IndicesType(aim->mFaces[i].mIndices[j]);
- }
- }
- p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_INT, BufferViewTarget_ELEMENT_ARRAY_BUFFER);
- }
- switch (aim->mPrimitiveTypes) {
- case aiPrimitiveType_POLYGON:
- p.mode = PrimitiveMode_TRIANGLES; break; // TODO implement this
- case aiPrimitiveType_LINE:
- p.mode = PrimitiveMode_LINES; break;
- case aiPrimitiveType_POINT:
- p.mode = PrimitiveMode_POINTS; break;
- default: // aiPrimitiveType_TRIANGLE
- p.mode = PrimitiveMode_TRIANGLES;
- }
- /*************** Skins ****************/
- if(aim->HasBones()) {
- ExportSkin(*mAsset, aim, m, b, skinRef, inverseBindMatricesData);
- }
- /*************** Targets for blendshapes ****************/
- if (aim->mNumAnimMeshes > 0) {
- bool bExportTargetNames = this->mProperties->HasPropertyBool("GLTF2_TARGETNAMES_EXP") &&
- this->mProperties->GetPropertyBool("GLTF2_TARGETNAMES_EXP");
- p.targets.resize(aim->mNumAnimMeshes);
- for (unsigned int am = 0; am < aim->mNumAnimMeshes; ++am) {
- aiAnimMesh *pAnimMesh = aim->mAnimMeshes[am];
- if (bExportTargetNames)
- m->targetNames.push_back(pAnimMesh->mName.data);
- // position
- if (pAnimMesh->HasPositions()) {
- // NOTE: in gltf it is the diff stored
- aiVector3D *pPositionDiff = new aiVector3D[pAnimMesh->mNumVertices];
- for (unsigned int vt = 0; vt < pAnimMesh->mNumVertices; ++vt) {
- pPositionDiff[vt] = pAnimMesh->mVertices[vt] - aim->mVertices[vt];
- }
- Ref<Accessor> vec = ExportData(*mAsset, meshId, b,
- pAnimMesh->mNumVertices, pPositionDiff,
- AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
- if (vec) {
- p.targets[am].position.push_back(vec);
- }
- delete[] pPositionDiff;
- }
- // normal
- if (pAnimMesh->HasNormals()) {
- aiVector3D *pNormalDiff = new aiVector3D[pAnimMesh->mNumVertices];
- for (unsigned int vt = 0; vt < pAnimMesh->mNumVertices; ++vt) {
- pNormalDiff[vt] = pAnimMesh->mNormals[vt] - aim->mNormals[vt];
- }
- Ref<Accessor> vec = ExportData(*mAsset, meshId, b,
- pAnimMesh->mNumVertices, pNormalDiff,
- AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
- if (vec) {
- p.targets[am].normal.push_back(vec);
- }
- delete[] pNormalDiff;
- }
- // tangent?
- }
- }
- }
- //----------------------------------------
- // Finish the skin
- // Create the Accessor for skinRef->inverseBindMatrices
- if (createSkin) {
- mat4* invBindMatrixData = new mat4[inverseBindMatricesData.size()];
- for ( unsigned int idx_joint = 0; idx_joint < inverseBindMatricesData.size(); ++idx_joint) {
- CopyValue(inverseBindMatricesData[idx_joint], invBindMatrixData[idx_joint]);
- }
- Ref<Accessor> invBindMatrixAccessor = ExportData(*mAsset, skinName, b,
- static_cast<unsigned int>(inverseBindMatricesData.size()),
- invBindMatrixData, AttribType::MAT4, AttribType::MAT4, ComponentType_FLOAT);
- if (invBindMatrixAccessor) {
- skinRef->inverseBindMatrices = invBindMatrixAccessor;
- }
- // Identity Matrix =====> skinRef->bindShapeMatrix
- // Temporary. Hard-coded identity matrix here
- skinRef->bindShapeMatrix.isPresent = true;
- IdentityMatrix4(skinRef->bindShapeMatrix.value);
- // Find nodes that contain a mesh with bones and add "skeletons" and "skin" attributes to those nodes.
- Ref<Node> rootNode = mAsset->nodes.Get(unsigned(0));
- Ref<Node> meshNode;
- for (unsigned int meshIndex = 0; meshIndex < mAsset->meshes.Size(); ++meshIndex) {
- Ref<Mesh> mesh = mAsset->meshes.Get(meshIndex);
- bool hasBones = false;
- for (unsigned int i = 0; i < mesh->primitives.size(); ++i) {
- if (!mesh->primitives[i].attributes.weight.empty()) {
- hasBones = true;
- break;
- }
- }
- if (!hasBones) {
- continue;
- }
- std::string meshID = mesh->id;
- FindMeshNode(rootNode, meshNode, meshID);
- Ref<Node> rootJoint = FindSkeletonRootJoint(skinRef);
- meshNode->skeletons.push_back(rootJoint);
- meshNode->skin = skinRef;
- }
- delete[] invBindMatrixData;
- }
- }
- // Merges a node's multiple meshes (with one primitive each) into one mesh with multiple primitives
- void glTF2Exporter::MergeMeshes()
- {
- for (unsigned int n = 0; n < mAsset->nodes.Size(); ++n) {
- Ref<Node> node = mAsset->nodes.Get(n);
- unsigned int nMeshes = static_cast<unsigned int>(node->meshes.size());
- //skip if it's 1 or less meshes per node
- if (nMeshes > 1) {
- Ref<Mesh> firstMesh = node->meshes.at(0);
- //loop backwards to allow easy removal of a mesh from a node once it's merged
- for (unsigned int m = nMeshes - 1; m >= 1; --m) {
- Ref<Mesh> mesh = node->meshes.at(m);
- //append this mesh's primitives to the first mesh's primitives
- firstMesh->primitives.insert(
- firstMesh->primitives.end(),
- mesh->primitives.begin(),
- mesh->primitives.end()
- );
- //remove the mesh from the list of meshes
- unsigned int removedIndex = mAsset->meshes.Remove(mesh->id.c_str());
- //find the presence of the removed mesh in other nodes
- for (unsigned int nn = 0; nn < mAsset->nodes.Size(); ++nn) {
- Ref<Node> curNode = mAsset->nodes.Get(nn);
- for (unsigned int mm = 0; mm < curNode->meshes.size(); ++mm) {
- Ref<Mesh> &meshRef = curNode->meshes.at(mm);
- unsigned int meshIndex = meshRef.GetIndex();
- if (meshIndex == removedIndex) {
- node->meshes.erase(curNode->meshes.begin() + mm);
- } else if (meshIndex > removedIndex) {
- Ref<Mesh> newMeshRef = mAsset->meshes.Get(meshIndex - 1);
- meshRef = newMeshRef;
- }
- }
- }
- }
- //since we were looping backwards, reverse the order of merged primitives to their original order
- std::reverse(firstMesh->primitives.begin() + 1, firstMesh->primitives.end());
- }
- }
- }
- /*
- * Export the root node of the node hierarchy.
- * Calls ExportNode for all children.
- */
- unsigned int glTF2Exporter::ExportNodeHierarchy(const aiNode* n)
- {
- Ref<Node> node = mAsset->nodes.Create(mAsset->FindUniqueID(n->mName.C_Str(), "node"));
- node->name = n->mName.C_Str();
- if (!n->mTransformation.IsIdentity()) {
- node->matrix.isPresent = true;
- CopyValue(n->mTransformation, node->matrix.value);
- }
- for (unsigned int i = 0; i < n->mNumMeshes; ++i) {
- node->meshes.push_back(mAsset->meshes.Get(n->mMeshes[i]));
- }
- for (unsigned int i = 0; i < n->mNumChildren; ++i) {
- unsigned int idx = ExportNode(n->mChildren[i], node);
- node->children.push_back(mAsset->nodes.Get(idx));
- }
- return node.GetIndex();
- }
- /*
- * Export node and recursively calls ExportNode for all children.
- * Since these nodes are not the root node, we also export the parent Ref<Node>
- */
- unsigned int glTF2Exporter::ExportNode(const aiNode* n, Ref<Node>& parent)
- {
- std::string name = mAsset->FindUniqueID(n->mName.C_Str(), "node");
- Ref<Node> node = mAsset->nodes.Create(name);
- node->parent = parent;
- node->name = name;
- if (!n->mTransformation.IsIdentity()) {
- if (mScene->mNumAnimations > 0) {
- aiQuaternion quaternion;
- n->mTransformation.Decompose(*reinterpret_cast<aiVector3D *>(&node->scale.value), quaternion, *reinterpret_cast<aiVector3D *>(&node->translation.value));
- aiVector3D vector(static_cast<ai_real>(1.0f), static_cast<ai_real>(1.0f), static_cast<ai_real>(1.0f));
- if (!reinterpret_cast<aiVector3D *>(&node->scale.value)->Equal(vector)) {
- node->scale.isPresent = true;
- }
- if (!reinterpret_cast<aiVector3D *>(&node->translation.value)->Equal(vector)) {
- node->translation.isPresent = true;
- }
- node->rotation.isPresent = true;
- node->rotation.value[0] = quaternion.x;
- node->rotation.value[1] = quaternion.y;
- node->rotation.value[2] = quaternion.z;
- node->rotation.value[3] = quaternion.w;
- node->matrix.isPresent = false;
- } else {
- node->matrix.isPresent = true;
- CopyValue(n->mTransformation, node->matrix.value);
- }
- }
- for (unsigned int i = 0; i < n->mNumMeshes; ++i) {
- node->meshes.push_back(mAsset->meshes.Get(n->mMeshes[i]));
- }
- for (unsigned int i = 0; i < n->mNumChildren; ++i) {
- unsigned int idx = ExportNode(n->mChildren[i], node);
- node->children.push_back(mAsset->nodes.Get(idx));
- }
- return node.GetIndex();
- }
- void glTF2Exporter::ExportScene()
- {
- const char* sceneName = "defaultScene";
- Ref<Scene> scene = mAsset->scenes.Create(sceneName);
- // root node will be the first one exported (idx 0)
- if (mAsset->nodes.Size() > 0) {
- scene->nodes.push_back(mAsset->nodes.Get(0u));
- }
- // set as the default scene
- mAsset->scene = scene;
- }
- void glTF2Exporter::ExportMetadata()
- {
- AssetMetadata& asset = mAsset->asset;
- asset.version = "2.0";
- char buffer[256];
- ai_snprintf(buffer, 256, "Open Asset Import Library (assimp v%d.%d.%x)",
- aiGetVersionMajor(), aiGetVersionMinor(), aiGetVersionRevision());
- asset.generator = buffer;
- // Copyright
- aiString copyright_str;
- if (mScene->mMetaData != nullptr && mScene->mMetaData->Get(AI_METADATA_SOURCE_COPYRIGHT, copyright_str)) {
- asset.copyright = copyright_str.C_Str();
- }
- }
- inline Ref<Accessor> GetSamplerInputRef(Asset& asset, std::string& animId, Ref<Buffer>& buffer, std::vector<float>& times)
- {
- return ExportData(asset, animId, buffer, (unsigned int)times.size(), ×[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_FLOAT);
- }
- inline void ExtractTranslationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
- {
- const unsigned int numKeyframes = nodeChannel->mNumPositionKeys;
- if (numKeyframes == 0) {
- return;
- }
- std::vector<float> times(numKeyframes);
- std::vector<float> values(numKeyframes * 3);
- for (unsigned int i = 0; i < numKeyframes; ++i) {
- const aiVectorKey& key = nodeChannel->mPositionKeys[i];
- // mTime is measured in ticks, but GLTF time is measured in seconds, so convert.
- times[i] = static_cast<float>(key.mTime / ticksPerSecond);
- values[(i * 3) + 0] = key.mValue.x;
- values[(i * 3) + 1] = key.mValue.y;
- values[(i * 3) + 2] = key.mValue.z;
- }
- sampler.input = GetSamplerInputRef(asset, animId, buffer, times);
- sampler.output = ExportData(asset, animId, buffer, numKeyframes, &values[0], AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
- sampler.interpolation = Interpolation_LINEAR;
- }
- inline void ExtractScaleSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
- {
- const unsigned int numKeyframes = nodeChannel->mNumScalingKeys;
- if (numKeyframes == 0) {
- return;
- }
- std::vector<float> times(numKeyframes);
- std::vector<float> values(numKeyframes * 3);
- for (unsigned int i = 0; i < numKeyframes; ++i) {
- const aiVectorKey& key = nodeChannel->mScalingKeys[i];
- // mTime is measured in ticks, but GLTF time is measured in seconds, so convert.
- times[i] = static_cast<float>(key.mTime / ticksPerSecond);
- values[(i * 3) + 0] = key.mValue.x;
- values[(i * 3) + 1] = key.mValue.y;
- values[(i * 3) + 2] = key.mValue.z;
- }
- sampler.input = GetSamplerInputRef(asset, animId, buffer, times);
- sampler.output = ExportData(asset, animId, buffer, numKeyframes, &values[0], AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
- sampler.interpolation = Interpolation_LINEAR;
- }
- inline void ExtractRotationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
- {
- const unsigned int numKeyframes = nodeChannel->mNumRotationKeys;
- if (numKeyframes == 0) {
- return;
- }
- std::vector<float> times(numKeyframes);
- std::vector<float> values(numKeyframes * 4);
- for (unsigned int i = 0; i < numKeyframes; ++i) {
- const aiQuatKey& key = nodeChannel->mRotationKeys[i];
- // mTime is measured in ticks, but GLTF time is measured in seconds, so convert.
- times[i] = static_cast<float>(key.mTime / ticksPerSecond);
- values[(i * 4) + 0] = key.mValue.x;
- values[(i * 4) + 1] = key.mValue.y;
- values[(i * 4) + 2] = key.mValue.z;
- values[(i * 4) + 3] = key.mValue.w;
- }
- sampler.input = GetSamplerInputRef(asset, animId, buffer, times);
- sampler.output = ExportData(asset, animId, buffer, numKeyframes, &values[0], AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT);
- sampler.interpolation = Interpolation_LINEAR;
- }
- static void AddSampler(Ref<Animation>& animRef, Ref<Node>& nodeRef, Animation::Sampler& sampler, AnimationPath path)
- {
- Animation::Channel channel;
- channel.sampler = static_cast<int>(animRef->samplers.size());
- channel.target.path = path;
- channel.target.node = nodeRef;
- animRef->channels.push_back(channel);
- animRef->samplers.push_back(sampler);
- }
- void glTF2Exporter::ExportAnimations()
- {
- Ref<Buffer> bufferRef = mAsset->buffers.Get(unsigned (0));
- for (unsigned int i = 0; i < mScene->mNumAnimations; ++i) {
- const aiAnimation* anim = mScene->mAnimations[i];
- const float ticksPerSecond = static_cast<float>(anim->mTicksPerSecond);
- std::string nameAnim = "anim";
- if (anim->mName.length > 0) {
- nameAnim = anim->mName.C_Str();
- }
- for (unsigned int channelIndex = 0; channelIndex < anim->mNumChannels; ++channelIndex) {
- const aiNodeAnim* nodeChannel = anim->mChannels[channelIndex];
- // It appears that assimp stores this type of animation as multiple animations.
- // where each aiNodeAnim in mChannels animates a specific node.
- std::string name = nameAnim + "_" + to_string(channelIndex);
- name = mAsset->FindUniqueID(name, "animation");
- Ref<Animation> animRef = mAsset->animations.Create(name);
- Ref<Node> animNode = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str());
- Animation::Sampler translationSampler;
- ExtractTranslationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, translationSampler);
- AddSampler(animRef, animNode, translationSampler, AnimationPath_TRANSLATION);
- Animation::Sampler rotationSampler;
- ExtractRotationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, rotationSampler);
- AddSampler(animRef, animNode, rotationSampler, AnimationPath_ROTATION);
- Animation::Sampler scaleSampler;
- ExtractScaleSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, scaleSampler);
- AddSampler(animRef, animNode, scaleSampler, AnimationPath_SCALE);
- }
- // Assimp documentation staes this is not used (not implemented)
- // for (unsigned int channelIndex = 0; channelIndex < anim->mNumMeshChannels; ++channelIndex) {
- // const aiMeshAnim* meshChannel = anim->mMeshChannels[channelIndex];
- // }
- } // End: for-loop mNumAnimations
- }
- #endif // ASSIMP_BUILD_NO_GLTF_EXPORTER
- #endif // ASSIMP_BUILD_NO_EXPORT
|