2017-07-19 20:21:43 +00:00
|
|
|
|
/*
|
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2006-2017, 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
|
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
#include "glTF2Exporter.h"
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
#include "Exceptional.h"
|
|
|
|
|
#include "StringComparison.h"
|
|
|
|
|
#include "ByteSwapper.h"
|
|
|
|
|
|
|
|
|
|
#include "SplitLargeMeshes.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, standart library.
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
#include "glTF2AssetWriter.h"
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
using namespace rapidjson;
|
|
|
|
|
|
|
|
|
|
using namespace Assimp;
|
2017-07-20 17:59:21 +00:00
|
|
|
|
using namespace glTF2;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
// Worker function for exporting a scene to GLTF. Prototyped and registered in Exporter.cpp
|
2017-07-20 17:59:21 +00:00
|
|
|
|
void ExportSceneGLTF2(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
// invoke the exporter
|
2017-07-20 17:59:21 +00:00
|
|
|
|
glTF2Exporter exporter(pFile, pIOSystem, pScene, pProperties, false);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // end of namespace Assimp
|
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const aiScene* pScene,
|
2017-07-19 20:21:43 +00:00
|
|
|
|
const ExportProperties* pProperties, bool isBinary)
|
|
|
|
|
: mFilename(filename)
|
|
|
|
|
, mIOSystem(pIOSystem)
|
|
|
|
|
, mProperties(pProperties)
|
|
|
|
|
{
|
|
|
|
|
aiScene* sceneCopy_tmp;
|
|
|
|
|
SceneCombiner::CopyScene(&sceneCopy_tmp, pScene);
|
|
|
|
|
std::unique_ptr<aiScene> sceneCopy(sceneCopy_tmp);
|
|
|
|
|
|
|
|
|
|
SplitLargeMeshesProcess_Triangle tri_splitter;
|
|
|
|
|
tri_splitter.SetLimit(0xffff);
|
|
|
|
|
tri_splitter.Execute(sceneCopy.get());
|
|
|
|
|
|
|
|
|
|
SplitLargeMeshesProcess_Vertex vert_splitter;
|
|
|
|
|
vert_splitter.SetLimit(0xffff);
|
|
|
|
|
vert_splitter.Execute(sceneCopy.get());
|
|
|
|
|
|
|
|
|
|
mScene = sceneCopy.get();
|
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
mAsset.reset( new Asset( pIOSystem ) );
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
if (isBinary) {
|
|
|
|
|
mAsset->SetAsBinary();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExportMetadata();
|
|
|
|
|
|
|
|
|
|
ExportMaterials();
|
|
|
|
|
|
|
|
|
|
if (mScene->mRootNode) {
|
|
|
|
|
ExportNodeHierarchy(mScene->mRootNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ExportMeshes();
|
|
|
|
|
|
|
|
|
|
ExportScene();
|
|
|
|
|
|
2017-09-01 21:49:04 +00:00
|
|
|
|
ExportAnimations();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
AssetWriter writer(*mAsset);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
if (isBinary) {
|
|
|
|
|
writer.WriteGLBFile(filename);
|
|
|
|
|
} else {
|
|
|
|
|
writer.WriteFile(filename);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copy a 4x4 matrix from struct aiMatrix to typedef mat4.
|
|
|
|
|
* Also converts from row-major to column-major storage.
|
|
|
|
|
*/
|
2017-07-20 17:59:21 +00:00
|
|
|
|
static void CopyValue(const aiMatrix4x4& v, mat4& o)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
o.a1 = v.a1; o.a2 = v.a2; o.a3 = v.a3; o.a4 = v.a4;
|
|
|
|
|
o.b1 = v.b1; o.b2 = v.b2; o.b3 = v.b3; o.b4 = v.b4;
|
|
|
|
|
o.c1 = v.c1; o.c2 = v.c2; o.c3 = v.c3; o.c4 = v.c4;
|
|
|
|
|
o.d1 = v.d1; o.d2 = v.d2; o.d3 = v.d3; o.d4 = v.d4;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
static void IdentityMatrix4(mat4& o)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& buffer,
|
|
|
|
|
unsigned int count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false)
|
|
|
|
|
{
|
|
|
|
|
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 = unsigned(offset);
|
|
|
|
|
bv->byteLength = length; //! The target that the WebGL buffer should be bound to.
|
|
|
|
|
bv->target = isIndices ? BufferViewTarget_ELEMENT_ARRAY_BUFFER : BufferViewTarget_ARRAY_BUFFER;
|
|
|
|
|
|
|
|
|
|
// accessor
|
|
|
|
|
Ref<Accessor> acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor"));
|
|
|
|
|
acc->bufferView = bv;
|
|
|
|
|
acc->byteOffset = 0;
|
|
|
|
|
acc->byteStride = 0;
|
|
|
|
|
acc->componentType = compType;
|
|
|
|
|
acc->count = count;
|
|
|
|
|
acc->type = typeOut;
|
|
|
|
|
|
|
|
|
|
// calculate min and max values
|
|
|
|
|
{
|
|
|
|
|
// Allocate and initialize with large values.
|
|
|
|
|
float float_MAX = 10000000000000.0f;
|
|
|
|
|
for (unsigned int i = 0 ; i < numCompsOut ; i++) {
|
|
|
|
|
acc->min.push_back( float_MAX);
|
|
|
|
|
acc->max.push_back(-float_MAX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Search and set extreme values.
|
|
|
|
|
float valueTmp;
|
|
|
|
|
for (unsigned int i = 0 ; i < count ; i++) {
|
|
|
|
|
for (unsigned int j = 0 ; j < numCompsOut ; j++) {
|
|
|
|
|
if (numCompsOut == 1) {
|
|
|
|
|
valueTmp = static_cast<unsigned short*>(data)[i];
|
|
|
|
|
} else {
|
|
|
|
|
valueTmp = static_cast<aiVector3D*>(data)[i][j];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (valueTmp < acc->min[j]) {
|
|
|
|
|
acc->min[j] = valueTmp;
|
|
|
|
|
}
|
|
|
|
|
if (valueTmp > acc->max[j]) {
|
|
|
|
|
acc->max[j] = valueTmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// copy the data
|
|
|
|
|
acc->WriteData(count, data, numCompsIn*bytesPerComp);
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 02:09:48 +00:00
|
|
|
|
inline void SetSamplerWrap(SamplerWrap& wrap, aiTextureMapMode map)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
2017-09-04 02:09:48 +00:00
|
|
|
|
switch (map) {
|
2017-07-19 20:21:43 +00:00
|
|
|
|
case aiTextureMapMode_Clamp:
|
2017-09-04 02:09:48 +00:00
|
|
|
|
wrap = SamplerWrap::Clamp_To_Edge;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
break;
|
|
|
|
|
case aiTextureMapMode_Mirror:
|
2017-09-04 02:09:48 +00:00
|
|
|
|
wrap = SamplerWrap::Mirrored_Repeat;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
break;
|
|
|
|
|
case aiTextureMapMode_Wrap:
|
|
|
|
|
case aiTextureMapMode_Decal:
|
|
|
|
|
default:
|
2017-09-04 02:09:48 +00:00
|
|
|
|
wrap = SamplerWrap::Repeat;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
break;
|
|
|
|
|
};
|
2017-09-04 02:09:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glTF2Exporter::GetTexSampler(const aiMaterial* mat, Ref<Texture> texture, aiTextureType tt, unsigned int slot)
|
|
|
|
|
{
|
|
|
|
|
aiString aId;
|
|
|
|
|
std::string id;
|
|
|
|
|
if (aiGetMaterialString(mat, (std::string(_AI_MATKEY_MAPPING_BASE) + "id").c_str(), tt, slot, &aId) == AI_SUCCESS) {
|
|
|
|
|
id = aId.C_Str();
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 18:40:27 +00:00
|
|
|
|
if (Ref<Sampler> ref = mAsset->samplers.Get(id.c_str())) {
|
|
|
|
|
texture->sampler = ref;
|
2017-09-04 02:09:48 +00:00
|
|
|
|
} 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);
|
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-09-04 02:09:48 +00:00
|
|
|
|
if (aiGetMaterialInteger(mat, (std::string(_AI_MATKEY_MAPPING_BASE) + "filtermag").c_str(), tt, slot, (int*)&filterMag) == AI_SUCCESS) {
|
|
|
|
|
texture->sampler->magFilter = filterMag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (aiGetMaterialInteger(mat, (std::string(_AI_MATKEY_MAPPING_BASE) + "filtermin").c_str(), tt, slot, (int*)&filterMin) == AI_SUCCESS) {
|
|
|
|
|
texture->sampler->minFilter = filterMin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
aiString name;
|
|
|
|
|
if (aiGetMaterialString(mat, (std::string(_AI_MATKEY_MAPPING_BASE) + "name").c_str(), tt, slot, &name) == AI_SUCCESS) {
|
|
|
|
|
texture->sampler->name = name.C_Str();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
|
void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int slot)
|
|
|
|
|
{
|
|
|
|
|
const char* key = (std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName).c_str();
|
|
|
|
|
|
|
|
|
|
mat->Get(key, tt, slot, prop);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, float& prop, const char* propName, aiTextureType tt, unsigned int slot)
|
|
|
|
|
{
|
|
|
|
|
const char* key = (std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName).c_str();
|
|
|
|
|
|
|
|
|
|
mat->Get(key, tt, slot, prop);
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTextureType tt, unsigned int slot = 0)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
2017-08-31 22:30:43 +00:00
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
|
if (mat->GetTextureCount(tt) > 0) {
|
2017-08-31 22:30:43 +00:00
|
|
|
|
aiString tex;
|
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
if (mat->Get(AI_MATKEY_TEXTURE(tt, slot), tex) == AI_SUCCESS) {
|
2017-07-19 20:21:43 +00:00
|
|
|
|
std::string path = tex.C_Str();
|
|
|
|
|
|
|
|
|
|
if (path.size() > 0) {
|
|
|
|
|
if (path[0] != '*') {
|
|
|
|
|
std::map<std::string, unsigned int>::iterator it = mTexturesByPath.find(path);
|
|
|
|
|
if (it != mTexturesByPath.end()) {
|
2017-07-26 21:42:22 +00:00
|
|
|
|
texture = mAsset->textures.Get(it->second);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 21:42:22 +00:00
|
|
|
|
if (!texture) {
|
2017-07-19 20:21:43 +00:00
|
|
|
|
std::string texId = mAsset->FindUniqueID("", "texture");
|
2017-07-26 21:42:22 +00:00
|
|
|
|
texture = mAsset->textures.Create(texId);
|
|
|
|
|
mTexturesByPath[path] = texture.GetIndex();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
std::string imgId = mAsset->FindUniqueID("", "image");
|
2017-07-26 21:42:22 +00:00
|
|
|
|
texture->source = mAsset->images.Create(imgId);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
if (path[0] == '*') { // embedded
|
|
|
|
|
aiTexture* tex = mScene->mTextures[atoi(&path[1])];
|
|
|
|
|
|
|
|
|
|
uint8_t* data = reinterpret_cast<uint8_t*>(tex->pcData);
|
2017-07-26 21:42:22 +00:00
|
|
|
|
texture->source->SetData(data, tex->mWidth, *mAsset);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
if (tex->achFormatHint[0]) {
|
|
|
|
|
std::string mimeType = "image/";
|
|
|
|
|
mimeType += (memcmp(tex->achFormatHint, "jpg", 3) == 0) ? "jpeg" : tex->achFormatHint;
|
2017-07-26 21:42:22 +00:00
|
|
|
|
texture->source->mimeType = mimeType;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2017-07-26 21:42:22 +00:00
|
|
|
|
texture->source->uri = path;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 02:09:48 +00:00
|
|
|
|
GetTexSampler(mat, texture, tt, slot);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-26 21:42:22 +00:00
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
void glTF2Exporter::GetMatColor(const aiMaterial* mat, vec4& prop, const char* propName, int type, int idx)
|
2017-07-26 21:42:22 +00:00
|
|
|
|
{
|
|
|
|
|
aiColor4D col;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
if (mat->Get(propName, type, idx, col) == AI_SUCCESS) {
|
2017-08-30 21:25:11 +00:00
|
|
|
|
prop[0] = col.r; prop[1] = col.g; prop[2] = col.b; prop[3] = col.a;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
void glTF2Exporter::GetMatColor(const aiMaterial* mat, vec3& prop, const char* propName, int type, int idx)
|
|
|
|
|
{
|
|
|
|
|
aiColor3D col;
|
|
|
|
|
if (mat->Get(propName, type, idx, col) == AI_SUCCESS) {
|
|
|
|
|
prop[0] = col.r; prop[1] = col.g; prop[2] = col.b;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
void glTF2Exporter::ExportMaterials()
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
2017-08-31 22:30:43 +00:00
|
|
|
|
bool& KHR_materials_pbrSpecularGlossiness = mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness;
|
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
|
aiString aiName;
|
|
|
|
|
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
|
|
|
|
|
const aiMaterial* mat = mScene->mMaterials[i];
|
|
|
|
|
|
2017-09-04 02:09:48 +00:00
|
|
|
|
std::string id = "material_" + std::to_string(i);
|
|
|
|
|
|
|
|
|
|
Ref<Material> m = mAsset->materials.Create(id);
|
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
|
std::string name;
|
|
|
|
|
if (mat->Get(AI_MATKEY_NAME, aiName) == AI_SUCCESS) {
|
|
|
|
|
name = aiName.C_Str();
|
|
|
|
|
}
|
|
|
|
|
name = mAsset->FindUniqueID(name, "material");
|
|
|
|
|
|
2017-09-04 02:09:48 +00:00
|
|
|
|
m->name = name;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
|
GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, aiTextureType_DIFFUSE);
|
|
|
|
|
GetMatTex(mat, m->pbrMetallicRoughness.metallicRoughnessTexture, aiTextureType_UNKNOWN, 0);//get unknown slot
|
|
|
|
|
GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_COLOR_DIFFUSE);
|
2017-09-04 02:09:48 +00:00
|
|
|
|
mat->Get("$mat.gltf.pbrMetallicRoughness.metallicFactor", 0, 0, m->pbrMetallicRoughness.metallicFactor);
|
|
|
|
|
mat->Get("$mat.gltf.pbrMetallicRoughness.roughnessFactor", 0, 0, m->pbrMetallicRoughness.roughnessFactor);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
|
GetMatTex(mat, m->normalTexture, aiTextureType_NORMALS);
|
|
|
|
|
GetMatTex(mat, m->occlusionTexture, aiTextureType_LIGHTMAP);
|
|
|
|
|
GetMatTex(mat, m->emissiveTexture, aiTextureType_EMISSIVE);
|
2017-08-30 21:25:11 +00:00
|
|
|
|
GetMatColor(mat, m->emissiveFactor, AI_MATKEY_COLOR_EMISSIVE);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
mat->Get(AI_MATKEY_TWOSIDED, m->doubleSided);
|
|
|
|
|
mat->Get("$mat.gltf.alphaCutoff", 0, 0, m->alphaCutoff);
|
|
|
|
|
mat->Get("$mat.gltf.alphaMode", 0, 0, m->alphaMode);
|
2017-08-31 22:30:43 +00:00
|
|
|
|
|
|
|
|
|
bool hasPbrSpecularGlossiness;
|
|
|
|
|
mat->Get("$mat.gltf.pbrSpecularGlossiness.on", 0, 0, hasPbrSpecularGlossiness);
|
|
|
|
|
|
|
|
|
|
if (hasPbrSpecularGlossiness) {
|
|
|
|
|
|
|
|
|
|
if (!KHR_materials_pbrSpecularGlossiness) {
|
|
|
|
|
KHR_materials_pbrSpecularGlossiness = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 06:42:09 +00:00
|
|
|
|
m->pbrSpecularGlossiness.on = true;
|
2017-08-31 22:30:43 +00:00
|
|
|
|
GetMatColor(mat, m->pbrSpecularGlossiness.diffuseFactor, "$clr.diffuse", 0, 1);
|
|
|
|
|
GetMatColor(mat, m->pbrSpecularGlossiness.specularFactor, "$clr.specular", 0, 1);
|
|
|
|
|
mat->Get("$mat.gltf.glossinessFactor", 0, 0, m->pbrSpecularGlossiness.glossinessFactor);
|
|
|
|
|
GetMatTex(mat, m->pbrSpecularGlossiness.diffuseTexture, aiTextureType_DIFFUSE, 1);
|
|
|
|
|
GetMatTex(mat, m->pbrSpecularGlossiness.specularGlossinessTexture, aiTextureType_UNKNOWN, 1);
|
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 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)
|
|
|
|
|
{
|
2017-09-01 21:56:13 +00:00
|
|
|
|
|
|
|
|
|
if (nodeIn->mesh && meshID.compare(nodeIn->mesh->id) == 0) {
|
|
|
|
|
meshNode = nodeIn;
|
|
|
|
|
return true;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nodeIn->children.size(); ++i) {
|
|
|
|
|
if(FindMeshNode(nodeIn->children[i], meshNode, meshID)) {
|
2017-09-01 21:56:13 +00:00
|
|
|
|
return true;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 21:49:04 +00:00
|
|
|
|
void ExportSkin(Asset& mAsset, const aiMesh* aimesh, Ref<Mesh>& meshRef, Ref<Buffer>& bufferRef, Ref<Skin>& skinRef, std::vector<aiMatrix4x4>& inverseBindMatricesData)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
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());
|
2017-07-26 00:26:18 +00:00
|
|
|
|
nodeRef->jointName = nodeRef->name;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
unsigned int jointNamesIndex;
|
|
|
|
|
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.
|
2017-08-09 15:36:22 +00:00
|
|
|
|
if (jointsPerVertex[vertexId] > 3) {
|
|
|
|
|
continue;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vertexJointData[vertexId][jointsPerVertex[vertexId]] = 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 ) {
|
|
|
|
|
p.attributes.joint.push_back( vertexJointAccessor );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2017-09-01 21:49:04 +00:00
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
void glTF2Exporter::ExportMeshes()
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
// Not for
|
|
|
|
|
// using IndicesType = decltype(aiFace::mNumIndices);
|
|
|
|
|
// But yes for
|
|
|
|
|
// using IndicesType = unsigned short;
|
|
|
|
|
// because "ComponentType_UNSIGNED_SHORT" used for indices. And it's a maximal type according to glTF specification.
|
|
|
|
|
typedef unsigned short 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];
|
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
std::string name = aim->mName.C_Str();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
std::string meshId = mAsset->FindUniqueID(name, "mesh");
|
2017-07-19 20:21:43 +00:00
|
|
|
|
Ref<Mesh> m = mAsset->meshes.Create(meshId);
|
|
|
|
|
m->primitives.resize(1);
|
|
|
|
|
Mesh::Primitive& p = m->primitives.back();
|
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
|
m->name = name;
|
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
|
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);
|
|
|
|
|
if (v) p.attributes.position.push_back(v);
|
|
|
|
|
|
|
|
|
|
/******************** Normals ********************/
|
|
|
|
|
Ref<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
|
|
|
|
|
if (n) p.attributes.normal.push_back(n);
|
|
|
|
|
|
|
|
|
|
/************** Texture coordinates **************/
|
|
|
|
|
for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
|
|
|
|
// 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, false);
|
|
|
|
|
if (tc) p.attributes.texcoord.push_back(tc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*************** 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] = uint16_t(aim->mFaces[i].mIndices[j]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.indices = ExportData(*mAsset, meshId, b, unsigned(indices.size()), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_SHORT, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 03:47:31 +00:00
|
|
|
|
/*************** Skins ****************/
|
2017-09-01 21:49:04 +00:00
|
|
|
|
if(aim->HasBones()) {
|
2017-08-28 03:47:31 +00:00
|
|
|
|
ExportSkin(*mAsset, aim, m, b, skinRef, inverseBindMatricesData);
|
2017-09-01 21:49:04 +00:00
|
|
|
|
}
|
2017-09-01 03:38:10 +00:00
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
|
// 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);
|
|
|
|
|
|
2017-07-26 00:26:18 +00:00
|
|
|
|
// Find nodes that contain a mesh with bones and add "skeletons" and "skin" attributes to those nodes.
|
2017-07-19 20:21:43 +00:00
|
|
|
|
Ref<Node> rootNode = mAsset->nodes.Get(unsigned(0));
|
|
|
|
|
Ref<Node> meshNode;
|
2017-07-26 00:26:18 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Export the root node of the node hierarchy.
|
|
|
|
|
* Calls ExportNode for all children.
|
|
|
|
|
*/
|
2017-07-20 17:59:21 +00:00
|
|
|
|
unsigned int glTF2Exporter::ExportNodeHierarchy(const aiNode* n)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
Ref<Node> node = mAsset->nodes.Create(mAsset->FindUniqueID(n->mName.C_Str(), "node"));
|
|
|
|
|
|
|
|
|
|
if (!n->mTransformation.IsIdentity()) {
|
|
|
|
|
node->matrix.isPresent = true;
|
|
|
|
|
CopyValue(n->mTransformation, node->matrix.value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 21:56:13 +00:00
|
|
|
|
if (n->mNumMeshes > 0) {
|
|
|
|
|
node->mesh = mAsset->meshes.Get(n->mMeshes[0]);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>
|
|
|
|
|
*/
|
2017-07-20 17:59:21 +00:00
|
|
|
|
unsigned int glTF2Exporter::ExportNode(const aiNode* n, Ref<Node>& parent)
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
2017-07-26 00:26:18 +00:00
|
|
|
|
std::string name = mAsset->FindUniqueID(n->mName.C_Str(), "node");
|
|
|
|
|
Ref<Node> node = mAsset->nodes.Create(name);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
node->parent = parent;
|
2017-07-26 00:26:18 +00:00
|
|
|
|
node->name = name;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
if (!n->mTransformation.IsIdentity()) {
|
|
|
|
|
node->matrix.isPresent = true;
|
|
|
|
|
CopyValue(n->mTransformation, node->matrix.value);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 21:56:13 +00:00
|
|
|
|
if (n->mNumMeshes > 0) {
|
|
|
|
|
node->mesh = mAsset->meshes.Get(n->mMeshes[0]);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
void glTF2Exporter::ExportScene()
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
|
void glTF2Exporter::ExportMetadata()
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
2017-07-20 17:59:21 +00:00
|
|
|
|
AssetMetadata& asset = mAsset->asset;
|
2017-08-09 15:36:22 +00:00
|
|
|
|
asset.version = 2;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
|
ai_snprintf(buffer, 256, "Open Asset Import Library (assimp v%d.%d.%d)",
|
|
|
|
|
aiGetVersionMajor(), aiGetVersionMinor(), aiGetVersionRevision());
|
|
|
|
|
|
|
|
|
|
asset.generator = buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void ExtractAnimationData(Asset& mAsset, std::string& animId, Ref<Animation>& animRef, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond)
|
|
|
|
|
{
|
|
|
|
|
// Loop over the data and check to see if it exactly matches an existing buffer.
|
|
|
|
|
// If yes, then reference the existing corresponding accessor.
|
|
|
|
|
// Otherwise, add to the buffer and create a new accessor.
|
|
|
|
|
|
|
|
|
|
size_t counts[3] = {
|
|
|
|
|
nodeChannel->mNumPositionKeys,
|
|
|
|
|
nodeChannel->mNumScalingKeys,
|
|
|
|
|
nodeChannel->mNumRotationKeys,
|
|
|
|
|
};
|
|
|
|
|
size_t numKeyframes = 1;
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
|
if (counts[i] > numKeyframes) {
|
|
|
|
|
numKeyframes = counts[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
// Extract TIME parameter data.
|
|
|
|
|
// Check if the timeStamps are the same for mPositionKeys, mRotationKeys, and mScalingKeys.
|
|
|
|
|
if(nodeChannel->mNumPositionKeys > 0) {
|
|
|
|
|
typedef float TimeType;
|
|
|
|
|
std::vector<TimeType> timeData;
|
|
|
|
|
timeData.resize(numKeyframes);
|
|
|
|
|
for (size_t i = 0; i < numKeyframes; ++i) {
|
|
|
|
|
size_t frameIndex = i * nodeChannel->mNumPositionKeys / numKeyframes;
|
|
|
|
|
// mTime is measured in ticks, but GLTF time is measured in seconds, so convert.
|
|
|
|
|
// Check if we have to cast type here. e.g. uint16_t()
|
|
|
|
|
timeData[i] = nodeChannel->mPositionKeys[frameIndex].mTime / ticksPerSecond;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 14:16:26 +00:00
|
|
|
|
Ref<Accessor> timeAccessor = ExportData(mAsset, animId, buffer, static_cast<unsigned int>(numKeyframes), &timeData[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_FLOAT);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
if (timeAccessor) animRef->Parameters.TIME = timeAccessor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
// Extract translation parameter data
|
|
|
|
|
if(nodeChannel->mNumPositionKeys > 0) {
|
|
|
|
|
C_STRUCT aiVector3D* translationData = new aiVector3D[numKeyframes];
|
|
|
|
|
for (size_t i = 0; i < numKeyframes; ++i) {
|
|
|
|
|
size_t frameIndex = i * nodeChannel->mNumPositionKeys / numKeyframes;
|
|
|
|
|
translationData[i] = nodeChannel->mPositionKeys[frameIndex].mValue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 14:16:26 +00:00
|
|
|
|
Ref<Accessor> tranAccessor = ExportData(mAsset, animId, buffer, static_cast<unsigned int>(numKeyframes), translationData, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
if ( tranAccessor ) {
|
|
|
|
|
animRef->Parameters.translation = tranAccessor;
|
|
|
|
|
}
|
|
|
|
|
delete[] translationData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
// Extract scale parameter data
|
|
|
|
|
if(nodeChannel->mNumScalingKeys > 0) {
|
|
|
|
|
C_STRUCT aiVector3D* scaleData = new aiVector3D[numKeyframes];
|
|
|
|
|
for (size_t i = 0; i < numKeyframes; ++i) {
|
|
|
|
|
size_t frameIndex = i * nodeChannel->mNumScalingKeys / numKeyframes;
|
|
|
|
|
scaleData[i] = nodeChannel->mScalingKeys[frameIndex].mValue;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 14:16:26 +00:00
|
|
|
|
Ref<Accessor> scaleAccessor = ExportData(mAsset, animId, buffer, static_cast<unsigned int>(numKeyframes), scaleData, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
if ( scaleAccessor ) {
|
|
|
|
|
animRef->Parameters.scale = scaleAccessor;
|
|
|
|
|
}
|
|
|
|
|
delete[] scaleData;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-------------------------------------------------------
|
|
|
|
|
// Extract rotation parameter data
|
|
|
|
|
if(nodeChannel->mNumRotationKeys > 0) {
|
|
|
|
|
vec4* rotationData = new vec4[numKeyframes];
|
|
|
|
|
for (size_t i = 0; i < numKeyframes; ++i) {
|
|
|
|
|
size_t frameIndex = i * nodeChannel->mNumRotationKeys / numKeyframes;
|
|
|
|
|
rotationData[i][0] = nodeChannel->mRotationKeys[frameIndex].mValue.x;
|
|
|
|
|
rotationData[i][1] = nodeChannel->mRotationKeys[frameIndex].mValue.y;
|
|
|
|
|
rotationData[i][2] = nodeChannel->mRotationKeys[frameIndex].mValue.z;
|
|
|
|
|
rotationData[i][3] = nodeChannel->mRotationKeys[frameIndex].mValue.w;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-10 14:16:26 +00:00
|
|
|
|
Ref<Accessor> rotAccessor = ExportData(mAsset, animId, buffer, static_cast<unsigned int>(numKeyframes), rotationData, AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
if ( rotAccessor ) {
|
|
|
|
|
animRef->Parameters.rotation = rotAccessor;
|
|
|
|
|
}
|
|
|
|
|
delete[] rotationData;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 21:49:04 +00:00
|
|
|
|
void glTF2Exporter::ExportAnimations()
|
2017-07-19 20:21:43 +00:00
|
|
|
|
{
|
|
|
|
|
Ref<Buffer> bufferRef = mAsset->buffers.Get(unsigned (0));
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mScene->mNumAnimations; ++i) {
|
|
|
|
|
const aiAnimation* anim = mScene->mAnimations[i];
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2017-08-28 03:47:31 +00:00
|
|
|
|
// Parameters
|
2017-07-19 20:21:43 +00:00
|
|
|
|
ExtractAnimationData(*mAsset, name, animRef, bufferRef, nodeChannel, anim->mTicksPerSecond);
|
|
|
|
|
|
|
|
|
|
for (unsigned int j = 0; j < 3; ++j) {
|
|
|
|
|
std::string channelType;
|
|
|
|
|
int channelSize;
|
|
|
|
|
switch (j) {
|
|
|
|
|
case 0:
|
|
|
|
|
channelType = "rotation";
|
|
|
|
|
channelSize = nodeChannel->mNumRotationKeys;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
channelType = "scale";
|
|
|
|
|
channelSize = nodeChannel->mNumScalingKeys;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
channelType = "translation";
|
|
|
|
|
channelSize = nodeChannel->mNumPositionKeys;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (channelSize < 1) { continue; }
|
|
|
|
|
|
|
|
|
|
Animation::AnimChannel tmpAnimChannel;
|
|
|
|
|
Animation::AnimSampler tmpAnimSampler;
|
|
|
|
|
|
2017-09-10 14:16:26 +00:00
|
|
|
|
tmpAnimChannel.sampler = static_cast<int>(animRef->Samplers.size());
|
2017-07-19 20:21:43 +00:00
|
|
|
|
tmpAnimChannel.target.path = channelType;
|
|
|
|
|
tmpAnimSampler.output = channelType;
|
|
|
|
|
tmpAnimSampler.id = name + "_" + channelType;
|
|
|
|
|
|
2017-07-26 00:26:18 +00:00
|
|
|
|
tmpAnimChannel.target.node = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str());
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
tmpAnimSampler.input = "TIME";
|
|
|
|
|
tmpAnimSampler.interpolation = "LINEAR";
|
|
|
|
|
|
|
|
|
|
animRef->Channels.push_back(tmpAnimChannel);
|
|
|
|
|
animRef->Samplers.push_back(tmpAnimSampler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2017-09-01 21:49:04 +00:00
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_GLTF_EXPORTER
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_EXPORT
|