2020-06-10 20:23:29 +00:00
|
|
|
/*
|
2017-07-19 20:21:43 +00:00
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2024-02-23 21:30:05 +00:00
|
|
|
Copyright (c) 2006-2024, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
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
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
#include "AssetLib/glTF2/glTF2Exporter.h"
|
|
|
|
#include "AssetLib/glTF2/glTF2AssetWriter.h"
|
2019-06-10 21:26:00 +00:00
|
|
|
#include "PostProcessing/SplitLargeMeshes.h"
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/ByteSwapper.h>
|
2021-09-13 20:38:20 +00:00
|
|
|
#include <assimp/Exceptional.h>
|
2017-07-19 20:21:43 +00:00
|
|
|
#include <assimp/SceneCombiner.h>
|
2021-09-13 20:38:20 +00:00
|
|
|
#include <assimp/StringComparison.h>
|
|
|
|
#include <assimp/commonMetaData.h>
|
2017-07-19 20:21:43 +00:00
|
|
|
#include <assimp/material.h>
|
|
|
|
#include <assimp/scene.h>
|
2021-09-13 20:38:20 +00:00
|
|
|
#include <assimp/version.h>
|
|
|
|
#include <assimp/Exporter.hpp>
|
|
|
|
#include <assimp/IOSystem.hpp>
|
2024-02-06 20:24:41 +00:00
|
|
|
#include <assimp/config.h>
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2017-11-09 22:19:26 +00:00
|
|
|
// Header files, standard library.
|
2021-09-14 18:45:36 +00:00
|
|
|
#include <cinttypes>
|
2019-11-22 16:56:31 +00:00
|
|
|
#include <limits>
|
2021-09-13 20:38:20 +00:00
|
|
|
#include <memory>
|
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 {
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// 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);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// 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);
|
|
|
|
}
|
2017-12-14 15:11:12 +00:00
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
} // end of namespace Assimp
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
glTF2Exporter::glTF2Exporter(const char *filename, IOSystem *pIOSystem, const aiScene *pScene,
|
|
|
|
const ExportProperties *pProperties, bool isBinary) :
|
|
|
|
mFilename(filename), mIOSystem(pIOSystem), mScene(pScene), mProperties(pProperties), mAsset(new Asset(pIOSystem)) {
|
2021-03-10 08:48:12 +00:00
|
|
|
// Always on as our triangulation process is aware of this type of encoding
|
|
|
|
mAsset->extensionsUsed.FB_ngon_encoding = true;
|
|
|
|
|
2024-02-06 20:24:41 +00:00
|
|
|
configEpsilon = mProperties->GetPropertyFloat(
|
|
|
|
AI_CONFIG_CHECK_IDENTITY_MATRIX_EPSILON,
|
|
|
|
(ai_real)AI_CONFIG_CHECK_IDENTITY_MATRIX_EPSILON_DEFAULT);
|
|
|
|
|
2018-01-05 09:28:12 +00:00
|
|
|
if (isBinary) {
|
|
|
|
mAsset->SetAsBinary();
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
ExportMetadata();
|
|
|
|
|
|
|
|
ExportMaterials();
|
|
|
|
|
|
|
|
if (mScene->mRootNode) {
|
|
|
|
ExportNodeHierarchy(mScene->mRootNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExportMeshes();
|
2017-09-17 21:00:57 +00:00
|
|
|
MergeMeshes();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
ExportScene();
|
|
|
|
|
2017-09-01 21:49:04 +00:00
|
|
|
ExportAnimations();
|
2021-09-07 13:04:08 +00:00
|
|
|
|
2020-06-12 00:37:06 +00:00
|
|
|
// export extras
|
2021-09-13 20:38:20 +00:00
|
|
|
if (mProperties->HasPropertyCallback("extras")) {
|
|
|
|
std::function<void *(void *)> ExportExtras = mProperties->GetPropertyCallback("extras");
|
|
|
|
mAsset->extras = (rapidjson::Value *)ExportExtras(0);
|
2020-06-10 20:23:29 +00:00
|
|
|
}
|
2021-09-07 13:04:08 +00:00
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
AssetWriter writer(*mAsset);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2017-12-14 15:11:12 +00:00
|
|
|
if (isBinary) {
|
|
|
|
writer.WriteGLBFile(filename);
|
|
|
|
} else {
|
|
|
|
writer.WriteFile(filename);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-08-25 16:20:13 +00:00
|
|
|
glTF2Exporter::~glTF2Exporter() = default;
|
2018-05-12 06:39:22 +00:00
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
/*
|
|
|
|
* Copy a 4x4 matrix from struct aiMatrix to typedef mat4.
|
|
|
|
* Also converts from row-major to column-major storage.
|
|
|
|
*/
|
2021-09-13 20:38:20 +00:00
|
|
|
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;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
static void CopyValue(const aiMatrix4x4 &v, aiMatrix4x4 &o) {
|
2020-02-10 02:03:26 +00:00
|
|
|
memcpy(&o, &v, sizeof(aiMatrix4x4));
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
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;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-22 05:46:14 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-11-22 05:46:14 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 19:20:31 +00:00
|
|
|
// compute the (data-dataBase), store the non-zero data items
|
|
|
|
template <typename T>
|
|
|
|
size_t NZDiff(void *data, void *dataBase, size_t count, unsigned int numCompsIn, unsigned int numCompsOut, void *&outputNZDiff, void *&outputNZIdx) {
|
|
|
|
std::vector<T> vNZDiff;
|
|
|
|
std::vector<unsigned short> vNZIdx;
|
|
|
|
size_t totalComps = count * numCompsIn;
|
|
|
|
T *bufferData_ptr = static_cast<T *>(data);
|
|
|
|
T *bufferData_end = bufferData_ptr + totalComps;
|
|
|
|
T *bufferBase_ptr = static_cast<T *>(dataBase);
|
|
|
|
|
|
|
|
// Search and set extreme values.
|
|
|
|
for (short idx = 0; bufferData_ptr < bufferData_end; idx += 1, bufferData_ptr += numCompsIn) {
|
|
|
|
bool bNonZero = false;
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// for the data, check any component Non Zero
|
2020-05-15 19:20:31 +00:00
|
|
|
for (unsigned int j = 0; j < numCompsOut; j++) {
|
|
|
|
double valueData = bufferData_ptr[j];
|
|
|
|
double valueBase = bufferBase_ptr ? bufferBase_ptr[j] : 0;
|
|
|
|
if ((valueData - valueBase) != 0) {
|
|
|
|
bNonZero = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// all zeros, continue
|
2020-05-15 19:20:31 +00:00
|
|
|
if (!bNonZero)
|
|
|
|
continue;
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// non zero, store the data
|
2020-05-15 19:20:31 +00:00
|
|
|
for (unsigned int j = 0; j < numCompsOut; j++) {
|
|
|
|
T valueData = bufferData_ptr[j];
|
|
|
|
T valueBase = bufferBase_ptr ? bufferBase_ptr[j] : 0;
|
|
|
|
vNZDiff.push_back(valueData - valueBase);
|
|
|
|
}
|
|
|
|
vNZIdx.push_back(idx);
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// avoid all-0, put 1 item
|
2020-06-08 23:18:11 +00:00
|
|
|
if (vNZDiff.size() == 0) {
|
|
|
|
for (unsigned int j = 0; j < numCompsOut; j++)
|
|
|
|
vNZDiff.push_back(0);
|
|
|
|
vNZIdx.push_back(0);
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// process data
|
2020-05-15 19:20:31 +00:00
|
|
|
outputNZDiff = new T[vNZDiff.size()];
|
|
|
|
memcpy(outputNZDiff, vNZDiff.data(), vNZDiff.size() * sizeof(T));
|
|
|
|
|
|
|
|
outputNZIdx = new unsigned short[vNZIdx.size()];
|
|
|
|
memcpy(outputNZIdx, vNZIdx.data(), vNZIdx.size() * sizeof(unsigned short));
|
|
|
|
return vNZIdx.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t NZDiff(ComponentType compType, void *data, void *dataBase, size_t count, unsigned int numCompsIn, unsigned int numCompsOut, void *&nzDiff, void *&nzIdx) {
|
|
|
|
switch (compType) {
|
|
|
|
case ComponentType_SHORT:
|
|
|
|
return NZDiff<short>(data, dataBase, count, numCompsIn, numCompsOut, nzDiff, nzIdx);
|
|
|
|
case ComponentType_UNSIGNED_SHORT:
|
|
|
|
return NZDiff<unsigned short>(data, dataBase, count, numCompsIn, numCompsOut, nzDiff, nzIdx);
|
|
|
|
case ComponentType_UNSIGNED_INT:
|
|
|
|
return NZDiff<unsigned int>(data, dataBase, count, numCompsIn, numCompsOut, nzDiff, nzIdx);
|
|
|
|
case ComponentType_FLOAT:
|
|
|
|
return NZDiff<float>(data, dataBase, count, numCompsIn, numCompsOut, nzDiff, nzIdx);
|
|
|
|
case ComponentType_BYTE:
|
|
|
|
return NZDiff<int8_t>(data, dataBase, count, numCompsIn, numCompsOut, nzDiff, nzIdx);
|
|
|
|
case ComponentType_UNSIGNED_BYTE:
|
|
|
|
return NZDiff<uint8_t>(data, dataBase, count, numCompsIn, numCompsOut, nzDiff, nzIdx);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-15 19:32:58 +00:00
|
|
|
|
2020-05-15 19:20:31 +00:00
|
|
|
inline Ref<Accessor> ExportDataSparse(Asset &a, std::string &meshName, Ref<Buffer> &buffer,
|
2022-11-08 16:03:55 +00:00
|
|
|
size_t count, void *data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, BufferViewTarget target = BufferViewTarget_NONE, void *dataBase = nullptr) {
|
2020-05-15 19:20:31 +00:00
|
|
|
if (!count || !data) {
|
|
|
|
return Ref<Accessor>();
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int numCompsIn = AttribType::GetNumComponents(typeIn);
|
|
|
|
unsigned int numCompsOut = AttribType::GetNumComponents(typeOut);
|
|
|
|
unsigned int bytesPerComp = ComponentTypeSize(compType);
|
|
|
|
|
|
|
|
// accessor
|
|
|
|
Ref<Accessor> acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor"));
|
|
|
|
|
|
|
|
// if there is a basic data vector
|
|
|
|
if (dataBase) {
|
|
|
|
size_t base_offset = buffer->byteLength;
|
|
|
|
size_t base_padding = base_offset % bytesPerComp;
|
|
|
|
base_offset += base_padding;
|
|
|
|
size_t base_length = count * numCompsOut * bytesPerComp;
|
|
|
|
buffer->Grow(base_length + base_padding);
|
|
|
|
|
|
|
|
Ref<BufferView> bv = a.bufferViews.Create(a.FindUniqueID(meshName, "view"));
|
|
|
|
bv->buffer = buffer;
|
|
|
|
bv->byteOffset = base_offset;
|
|
|
|
bv->byteLength = base_length; //! The target that the WebGL buffer should be bound to.
|
|
|
|
bv->byteStride = 0;
|
|
|
|
bv->target = target;
|
|
|
|
acc->bufferView = bv;
|
|
|
|
acc->WriteData(count, dataBase, numCompsIn * bytesPerComp);
|
|
|
|
}
|
|
|
|
acc->byteOffset = 0;
|
|
|
|
acc->componentType = compType;
|
|
|
|
acc->count = count;
|
|
|
|
acc->type = typeOut;
|
|
|
|
|
|
|
|
if (data) {
|
2022-11-08 16:03:55 +00:00
|
|
|
void *nzDiff = nullptr, *nzIdx = nullptr;
|
2020-05-15 19:20:31 +00:00
|
|
|
size_t nzCount = NZDiff(compType, data, dataBase, count, numCompsIn, numCompsOut, nzDiff, nzIdx);
|
|
|
|
acc->sparse.reset(new Accessor::Sparse);
|
|
|
|
acc->sparse->count = nzCount;
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// indices
|
2020-05-15 19:20:31 +00:00
|
|
|
unsigned int bytesPerIdx = sizeof(unsigned short);
|
|
|
|
size_t indices_offset = buffer->byteLength;
|
|
|
|
size_t indices_padding = indices_offset % bytesPerIdx;
|
|
|
|
indices_offset += indices_padding;
|
|
|
|
size_t indices_length = nzCount * 1 * bytesPerIdx;
|
|
|
|
buffer->Grow(indices_length + indices_padding);
|
|
|
|
|
|
|
|
Ref<BufferView> indicesBV = a.bufferViews.Create(a.FindUniqueID(meshName, "view"));
|
|
|
|
indicesBV->buffer = buffer;
|
|
|
|
indicesBV->byteOffset = indices_offset;
|
|
|
|
indicesBV->byteLength = indices_length;
|
|
|
|
indicesBV->byteStride = 0;
|
|
|
|
acc->sparse->indices = indicesBV;
|
|
|
|
acc->sparse->indicesType = ComponentType_UNSIGNED_SHORT;
|
|
|
|
acc->sparse->indicesByteOffset = 0;
|
|
|
|
acc->WriteSparseIndices(nzCount, nzIdx, 1 * bytesPerIdx);
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// values
|
2020-05-15 19:20:31 +00:00
|
|
|
size_t values_offset = buffer->byteLength;
|
|
|
|
size_t values_padding = values_offset % bytesPerComp;
|
|
|
|
values_offset += values_padding;
|
|
|
|
size_t values_length = nzCount * numCompsOut * bytesPerComp;
|
|
|
|
buffer->Grow(values_length + values_padding);
|
|
|
|
|
|
|
|
Ref<BufferView> valuesBV = a.bufferViews.Create(a.FindUniqueID(meshName, "view"));
|
|
|
|
valuesBV->buffer = buffer;
|
|
|
|
valuesBV->byteOffset = values_offset;
|
|
|
|
valuesBV->byteLength = values_length;
|
|
|
|
valuesBV->byteStride = 0;
|
|
|
|
acc->sparse->values = valuesBV;
|
|
|
|
acc->sparse->valuesByteOffset = 0;
|
|
|
|
acc->WriteSparseValues(nzCount, nzDiff, numCompsIn * bytesPerComp);
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// clear
|
|
|
|
delete[] (char *)nzDiff;
|
|
|
|
delete[] (char *)nzIdx;
|
2020-05-15 19:20:31 +00:00
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}
|
2021-09-13 20:38:20 +00:00
|
|
|
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) {
|
2018-05-12 06:39:22 +00:00
|
|
|
if (!count || !data) {
|
|
|
|
return Ref<Accessor>();
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
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;
|
2019-01-23 17:07:44 +00:00
|
|
|
bv->byteOffset = offset;
|
2017-07-19 20:21:43 +00:00
|
|
|
bv->byteLength = length; //! The target that the WebGL buffer should be bound to.
|
2017-11-20 18:01:28 +00:00
|
|
|
bv->byteStride = 0;
|
2020-02-10 02:03:26 +00:00
|
|
|
bv->target = target;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
// 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
|
2021-09-13 20:38:20 +00:00
|
|
|
SetAccessorRange(compType, acc, data, count, numCompsIn, numCompsOut);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
// copy the data
|
2021-09-13 20:38:20 +00:00
|
|
|
acc->WriteData(count, data, numCompsIn * bytesPerComp);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
2022-07-21 23:33:05 +00:00
|
|
|
inline void ExportNodeExtras(const aiMetadataEntry &metadataEntry, aiString name, CustomExtension &value) {
|
2022-07-21 22:44:51 +00:00
|
|
|
|
|
|
|
value.name = name.C_Str();
|
|
|
|
switch (metadataEntry.mType) {
|
|
|
|
case AI_BOOL:
|
|
|
|
value.mBoolValue.value = *static_cast<bool *>(metadataEntry.mData);
|
|
|
|
value.mBoolValue.isPresent = true;
|
|
|
|
break;
|
|
|
|
case AI_INT32:
|
2022-07-21 23:33:05 +00:00
|
|
|
value.mInt64Value.value = *static_cast<int32_t *>(metadataEntry.mData);
|
|
|
|
value.mInt64Value.isPresent = true;
|
2022-07-21 22:44:51 +00:00
|
|
|
break;
|
|
|
|
case AI_UINT64:
|
|
|
|
value.mUint64Value.value = *static_cast<uint64_t *>(metadataEntry.mData);
|
|
|
|
value.mUint64Value.isPresent = true;
|
|
|
|
break;
|
2022-08-01 12:39:10 +00:00
|
|
|
case AI_FLOAT:
|
|
|
|
value.mDoubleValue.value = *static_cast<float *>(metadataEntry.mData);
|
|
|
|
value.mDoubleValue.isPresent = true;
|
|
|
|
break;
|
2022-07-21 22:44:51 +00:00
|
|
|
case AI_DOUBLE:
|
|
|
|
value.mDoubleValue.value = *static_cast<double *>(metadataEntry.mData);
|
|
|
|
value.mDoubleValue.isPresent = true;
|
|
|
|
break;
|
|
|
|
case AI_AISTRING:
|
|
|
|
value.mStringValue.value = static_cast<aiString *>(metadataEntry.mData)->C_Str();
|
|
|
|
value.mStringValue.isPresent = true;
|
|
|
|
break;
|
2022-08-01 12:39:10 +00:00
|
|
|
case AI_AIMETADATA: {
|
2022-07-21 22:44:51 +00:00
|
|
|
const aiMetadata *subMetadata = static_cast<aiMetadata *>(metadataEntry.mData);
|
2022-07-21 23:33:05 +00:00
|
|
|
value.mValues.value.resize(subMetadata->mNumProperties);
|
|
|
|
value.mValues.isPresent = true;
|
2022-07-21 22:44:51 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < subMetadata->mNumProperties; ++i) {
|
2022-07-21 23:33:05 +00:00
|
|
|
ExportNodeExtras(subMetadata->mValues[i], subMetadata->mKeys[i], value.mValues.value.at(i));
|
2022-07-21 22:44:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-08-01 12:39:10 +00:00
|
|
|
default:
|
|
|
|
// AI_AIVECTOR3D not handled
|
|
|
|
break;
|
|
|
|
}
|
2022-07-21 22:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void ExportNodeExtras(const aiMetadata *metadata, Extras &extras) {
|
2022-07-21 23:04:05 +00:00
|
|
|
if (metadata == nullptr || metadata->mNumProperties == 0) {
|
2022-07-21 22:44:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extras.mValues.resize(metadata->mNumProperties);
|
|
|
|
for (unsigned int i = 0; i < metadata->mNumProperties; ++i) {
|
|
|
|
ExportNodeExtras(metadata->mValues[i], metadata->mKeys[i], extras.mValues.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
inline void SetSamplerWrap(SamplerWrap &wrap, aiTextureMapMode map) {
|
2017-09-04 02:09:48 +00:00
|
|
|
switch (map) {
|
2021-09-13 20:38:20 +00:00
|
|
|
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;
|
2017-07-19 20:21:43 +00:00
|
|
|
};
|
2017-09-04 02:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::GetTexSampler(const aiMaterial &mat, Ref<Texture> texture, aiTextureType tt, unsigned int slot) {
|
2017-09-04 02:09:48 +00:00
|
|
|
aiString aId;
|
|
|
|
std::string id;
|
2021-09-07 13:04:08 +00:00
|
|
|
if (aiGetMaterialString(&mat, AI_MATKEY_GLTF_MAPPINGID(tt, slot), &aId) == AI_SUCCESS) {
|
2017-09-04 02:09:48 +00:00
|
|
|
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;
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
if (aiGetMaterialInteger(&mat, AI_MATKEY_MAPPINGMODE_U(tt, slot), (int *)&mapU) == AI_SUCCESS) {
|
2017-09-04 02:09:48 +00:00
|
|
|
SetSamplerWrap(texture->sampler->wrapS, mapU);
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
if (aiGetMaterialInteger(&mat, AI_MATKEY_MAPPINGMODE_V(tt, slot), (int *)&mapV) == AI_SUCCESS) {
|
2017-09-04 02:09:48 +00:00
|
|
|
SetSamplerWrap(texture->sampler->wrapT, mapV);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
if (aiGetMaterialInteger(&mat, AI_MATKEY_GLTF_MAPPINGFILTER_MAG(tt, slot), (int *)&filterMag) == AI_SUCCESS) {
|
2017-09-04 02:09:48 +00:00
|
|
|
texture->sampler->magFilter = filterMag;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
if (aiGetMaterialInteger(&mat, AI_MATKEY_GLTF_MAPPINGFILTER_MIN(tt, slot), (int *)&filterMin) == AI_SUCCESS) {
|
2017-09-04 02:09:48 +00:00
|
|
|
texture->sampler->minFilter = filterMin;
|
|
|
|
}
|
|
|
|
|
|
|
|
aiString name;
|
2021-09-07 13:04:08 +00:00
|
|
|
if (aiGetMaterialString(&mat, AI_MATKEY_GLTF_MAPPINGNAME(tt, slot), &name) == AI_SUCCESS) {
|
2017-09-04 02:09:48 +00:00
|
|
|
texture->sampler->name = name.C_Str();
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::GetMatTexProp(const aiMaterial &mat, unsigned int &prop, const char *propName, aiTextureType tt, unsigned int slot) {
|
2017-09-12 14:07:15 +00:00
|
|
|
std::string textureKey = std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName;
|
2017-08-31 22:30:43 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
mat.Get(textureKey.c_str(), tt, slot, prop);
|
2017-08-31 22:30:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::GetMatTexProp(const aiMaterial &mat, float &prop, const char *propName, aiTextureType tt, unsigned int slot) {
|
2017-09-12 14:07:15 +00:00
|
|
|
std::string textureKey = std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName;
|
2017-08-31 22:30:43 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
mat.Get(textureKey.c_str(), tt, slot, prop);
|
2017-08-31 22:30:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::GetMatTex(const aiMaterial &mat, Ref<Texture> &texture, unsigned int &texCoord, aiTextureType tt, unsigned int slot = 0) {
|
2022-02-22 20:07:42 +00:00
|
|
|
if (mat.GetTextureCount(tt) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-16 08:12:35 +00:00
|
|
|
|
2022-02-22 20:07:42 +00:00
|
|
|
aiString tex;
|
2021-09-07 13:04:08 +00:00
|
|
|
|
2022-02-22 20:07:42 +00:00
|
|
|
// Read texcoord (UV map index)
|
2023-10-17 05:37:35 +00:00
|
|
|
// Note: must be an int to be successful.
|
|
|
|
int tmp = 0;
|
|
|
|
const auto ok = mat.Get(AI_MATKEY_UVWSRC(tt, slot), tmp);
|
|
|
|
if (ok == aiReturn_SUCCESS) texCoord = tmp;
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2022-02-22 20:07:42 +00:00
|
|
|
if (mat.Get(AI_MATKEY_TEXTURE(tt, slot), tex) == AI_SUCCESS) {
|
|
|
|
std::string path = tex.C_Str();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2022-02-22 20:07:42 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-09-07 13:04:08 +00:00
|
|
|
|
2022-02-22 20:07:42 +00:00
|
|
|
bool useBasisUniversal = false;
|
|
|
|
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);
|
|
|
|
|
|
|
|
const aiTexture *curTex = mScene->GetEmbeddedTexture(path.c_str());
|
|
|
|
if (curTex != nullptr) { // embedded
|
|
|
|
texture->source->name = curTex->mFilename.C_Str();
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// basisu: embedded ktx2, bu
|
2022-02-22 20:07:42 +00:00
|
|
|
if (curTex->achFormatHint[0]) {
|
|
|
|
std::string mimeType = "image/";
|
|
|
|
if (memcmp(curTex->achFormatHint, "jpg", 3) == 0)
|
|
|
|
mimeType += "jpeg";
|
|
|
|
else if (memcmp(curTex->achFormatHint, "ktx", 3) == 0) {
|
2021-05-06 22:10:06 +00:00
|
|
|
useBasisUniversal = true;
|
2022-02-22 20:07:42 +00:00
|
|
|
mimeType += "ktx";
|
|
|
|
} else if (memcmp(curTex->achFormatHint, "kx2", 3) == 0) {
|
|
|
|
useBasisUniversal = true;
|
|
|
|
mimeType += "ktx2";
|
|
|
|
} else if (memcmp(curTex->achFormatHint, "bu", 2) == 0) {
|
|
|
|
useBasisUniversal = true;
|
|
|
|
mimeType += "basis";
|
|
|
|
} else
|
|
|
|
mimeType += curTex->achFormatHint;
|
|
|
|
texture->source->mimeType = mimeType;
|
2021-05-06 22:10:06 +00:00
|
|
|
}
|
2021-09-07 13:04:08 +00:00
|
|
|
|
2022-02-22 20:07:42 +00:00
|
|
|
// The asset has its own buffer, see Image::SetData
|
2023-07-27 15:43:26 +00:00
|
|
|
// basisu: "image/ktx2", "image/basis" as is
|
2022-02-22 20:07:42 +00:00
|
|
|
texture->source->SetData(reinterpret_cast<uint8_t *>(curTex->pcData), curTex->mWidth, *mAsset);
|
|
|
|
} else {
|
|
|
|
texture->source->uri = path;
|
|
|
|
if (texture->source->uri.find(".ktx") != std::string::npos ||
|
|
|
|
texture->source->uri.find(".basis") != std::string::npos) {
|
|
|
|
useBasisUniversal = true;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2022-02-22 20:07:42 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// basisu
|
2022-02-22 20:07:42 +00:00
|
|
|
if (useBasisUniversal) {
|
|
|
|
mAsset->extensionsUsed.KHR_texture_basisu = true;
|
|
|
|
mAsset->extensionsRequired.KHR_texture_basisu = true;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2022-02-22 20:07:42 +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
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::GetMatTex(const aiMaterial &mat, TextureInfo &prop, aiTextureType tt, unsigned int slot = 0) {
|
|
|
|
Ref<Texture> &texture = prop.texture;
|
2021-09-07 13:04:08 +00:00
|
|
|
GetMatTex(mat, texture, prop.texCoord, tt, slot);
|
2017-08-31 22:30:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::GetMatTex(const aiMaterial &mat, NormalTextureInfo &prop, aiTextureType tt, unsigned int slot = 0) {
|
|
|
|
Ref<Texture> &texture = prop.texture;
|
2017-08-31 22:30:43 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
GetMatTex(mat, texture, prop.texCoord, tt, slot);
|
2017-08-31 22:30:43 +00:00
|
|
|
|
|
|
|
if (texture) {
|
2023-07-27 15:43:26 +00:00
|
|
|
// GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
2017-08-31 22:30:43 +00:00
|
|
|
GetMatTexProp(mat, prop.scale, "scale", tt, slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::GetMatTex(const aiMaterial &mat, OcclusionTextureInfo &prop, aiTextureType tt, unsigned int slot = 0) {
|
|
|
|
Ref<Texture> &texture = prop.texture;
|
2017-08-31 22:30:43 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
GetMatTex(mat, texture, prop.texCoord, tt, slot);
|
2017-08-31 22:30:43 +00:00
|
|
|
|
|
|
|
if (texture) {
|
2023-07-27 15:43:26 +00:00
|
|
|
// GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
2017-08-31 22:30:43 +00:00
|
|
|
GetMatTexProp(mat, prop.strength, "strength", tt, slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
aiReturn glTF2Exporter::GetMatColor(const aiMaterial &mat, vec4 &prop, const char *propName, int type, int idx) const {
|
2017-07-26 21:42:22 +00:00
|
|
|
aiColor4D col;
|
2021-09-07 13:04:08 +00:00
|
|
|
aiReturn result = mat.Get(propName, type, idx, col);
|
2017-10-19 16:01:40 +00:00
|
|
|
|
|
|
|
if (result == AI_SUCCESS) {
|
2021-09-13 20:38:20 +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-10-19 16:01:40 +00:00
|
|
|
|
|
|
|
return result;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
aiReturn glTF2Exporter::GetMatColor(const aiMaterial &mat, vec3 &prop, const char *propName, int type, int idx) const {
|
2017-08-30 21:25:11 +00:00
|
|
|
aiColor3D col;
|
2021-09-07 13:04:08 +00:00
|
|
|
aiReturn result = mat.Get(propName, type, idx, col);
|
2017-10-19 16:01:40 +00:00
|
|
|
|
|
|
|
if (result == AI_SUCCESS) {
|
2021-09-07 13:04:08 +00:00
|
|
|
prop[0] = col.r;
|
|
|
|
prop[1] = col.g;
|
|
|
|
prop[2] = col.b;
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
2017-10-19 16:01:40 +00:00
|
|
|
|
|
|
|
return result;
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2023-03-26 12:52:44 +00:00
|
|
|
// This extension has been deprecated, only export with the specific flag enabled, defaults to false. Uses KHR_material_specular default.
|
2021-09-07 13:04:08 +00:00
|
|
|
bool glTF2Exporter::GetMatSpecGloss(const aiMaterial &mat, glTF2::PbrSpecularGlossiness &pbrSG) {
|
2023-03-26 12:52:44 +00:00
|
|
|
bool result = false;
|
|
|
|
// If has Glossiness, a Specular Color or Specular Texture, use the KHR_materials_pbrSpecularGlossiness extension
|
2021-09-07 13:04:08 +00:00
|
|
|
if (mat.Get(AI_MATKEY_GLOSSINESS_FACTOR, pbrSG.glossinessFactor) == AI_SUCCESS) {
|
|
|
|
result = true;
|
|
|
|
} else {
|
|
|
|
// Don't have explicit glossiness, convert from pbr roughness or legacy shininess
|
|
|
|
float shininess;
|
|
|
|
if (mat.Get(AI_MATKEY_ROUGHNESS_FACTOR, shininess) == AI_SUCCESS) {
|
|
|
|
pbrSG.glossinessFactor = 1.0f - shininess; // Extension defines this way
|
|
|
|
} else if (mat.Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) {
|
|
|
|
pbrSG.glossinessFactor = shininess / 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetMatColor(mat, pbrSG.specularFactor, AI_MATKEY_COLOR_SPECULAR) == AI_SUCCESS) {
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
// Add any appropriate textures
|
|
|
|
GetMatTex(mat, pbrSG.specularGlossinessTexture, aiTextureType_SPECULAR);
|
|
|
|
|
|
|
|
result = result || pbrSG.specularGlossinessTexture.texture;
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
// Likely to always have diffuse
|
|
|
|
GetMatTex(mat, pbrSG.diffuseTexture, aiTextureType_DIFFUSE);
|
|
|
|
GetMatColor(mat, pbrSG.diffuseFactor, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:50 +00:00
|
|
|
bool glTF2Exporter::GetMatSpecular(const aiMaterial &mat, glTF2::MaterialSpecular &specular) {
|
2023-03-26 12:52:44 +00:00
|
|
|
// Specular requires either/or, default factors of zero disables specular, so do not export
|
2023-07-13 09:51:56 +00:00
|
|
|
if (GetMatColor(mat, specular.specularColorFactor, AI_MATKEY_COLOR_SPECULAR) != AI_SUCCESS && mat.Get(AI_MATKEY_SPECULAR_FACTOR, specular.specularFactor) != AI_SUCCESS) {
|
2023-03-26 14:55:38 +00:00
|
|
|
return false;
|
2022-11-08 21:09:50 +00:00
|
|
|
}
|
2023-03-26 14:55:38 +00:00
|
|
|
// The spec states that the default is 1.0 and [1.0, 1.0, 1.0]. We if both are 0, which should disable specular. Otherwise, if one is 0, set to 1.0
|
|
|
|
const bool colorFactorIsZero = specular.specularColorFactor[0] == defaultSpecularColorFactor[0] && specular.specularColorFactor[1] == defaultSpecularColorFactor[1] && specular.specularColorFactor[2] == defaultSpecularColorFactor[2];
|
|
|
|
if (specular.specularFactor == 0.0f && colorFactorIsZero) {
|
|
|
|
return false;
|
|
|
|
} else if (specular.specularFactor == 0.0f) {
|
|
|
|
specular.specularFactor = 1.0f;
|
|
|
|
} else if (colorFactorIsZero) {
|
|
|
|
specular.specularColorFactor[0] = specular.specularColorFactor[1] = specular.specularColorFactor[2] = 1.0f;
|
|
|
|
}
|
2023-11-26 00:13:42 +00:00
|
|
|
GetMatTex(mat, specular.specularTexture, aiTextureType_SPECULAR, 0);
|
|
|
|
GetMatTex(mat, specular.specularColorTexture, aiTextureType_SPECULAR, 1);
|
2023-03-26 14:55:38 +00:00
|
|
|
return true;
|
2022-11-08 21:09:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
bool glTF2Exporter::GetMatSheen(const aiMaterial &mat, glTF2::MaterialSheen &sheen) {
|
|
|
|
// Return true if got any valid Sheen properties or textures
|
2022-02-22 20:07:42 +00:00
|
|
|
if (GetMatColor(mat, sheen.sheenColorFactor, AI_MATKEY_SHEEN_COLOR_FACTOR) != aiReturn_SUCCESS) {
|
2021-09-07 13:04:08 +00:00
|
|
|
return false;
|
2022-02-22 20:07:42 +00:00
|
|
|
}
|
2021-09-07 13:04:08 +00:00
|
|
|
|
|
|
|
// Default Sheen color factor {0,0,0} disables Sheen, so do not export
|
2022-06-16 23:23:55 +00:00
|
|
|
if (sheen.sheenColorFactor[0] == defaultSheenFactor[0] && sheen.sheenColorFactor[1] == defaultSheenFactor[1] && sheen.sheenColorFactor[2] == defaultSheenFactor[2]) {
|
2021-09-07 13:04:08 +00:00
|
|
|
return false;
|
2022-02-22 20:07:42 +00:00
|
|
|
}
|
2021-09-07 13:04:08 +00:00
|
|
|
|
|
|
|
mat.Get(AI_MATKEY_SHEEN_ROUGHNESS_FACTOR, sheen.sheenRoughnessFactor);
|
|
|
|
|
|
|
|
GetMatTex(mat, sheen.sheenColorTexture, AI_MATKEY_SHEEN_COLOR_TEXTURE);
|
|
|
|
GetMatTex(mat, sheen.sheenRoughnessTexture, AI_MATKEY_SHEEN_ROUGHNESS_TEXTURE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool glTF2Exporter::GetMatClearcoat(const aiMaterial &mat, glTF2::MaterialClearcoat &clearcoat) {
|
|
|
|
if (mat.Get(AI_MATKEY_CLEARCOAT_FACTOR, clearcoat.clearcoatFactor) != aiReturn_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clearcoat factor of zero disables Clearcoat, so do not export
|
|
|
|
if (clearcoat.clearcoatFactor == 0.0f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mat.Get(AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR, clearcoat.clearcoatRoughnessFactor);
|
|
|
|
|
|
|
|
GetMatTex(mat, clearcoat.clearcoatTexture, AI_MATKEY_CLEARCOAT_TEXTURE);
|
|
|
|
GetMatTex(mat, clearcoat.clearcoatRoughnessTexture, AI_MATKEY_CLEARCOAT_ROUGHNESS_TEXTURE);
|
|
|
|
GetMatTex(mat, clearcoat.clearcoatNormalTexture, AI_MATKEY_CLEARCOAT_NORMAL_TEXTURE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool glTF2Exporter::GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission) {
|
|
|
|
bool result = mat.Get(AI_MATKEY_TRANSMISSION_FACTOR, transmission.transmissionFactor) == aiReturn_SUCCESS;
|
|
|
|
GetMatTex(mat, transmission.transmissionTexture, AI_MATKEY_TRANSMISSION_TEXTURE);
|
|
|
|
return result || transmission.transmissionTexture.texture;
|
|
|
|
}
|
|
|
|
|
2021-09-30 07:47:53 +00:00
|
|
|
bool glTF2Exporter::GetMatVolume(const aiMaterial &mat, glTF2::MaterialVolume &volume) {
|
|
|
|
bool result = mat.Get(AI_MATKEY_VOLUME_THICKNESS_FACTOR, volume.thicknessFactor) != aiReturn_SUCCESS;
|
|
|
|
|
|
|
|
GetMatTex(mat, volume.thicknessTexture, AI_MATKEY_VOLUME_THICKNESS_TEXTURE);
|
|
|
|
|
|
|
|
result = result || mat.Get(AI_MATKEY_VOLUME_ATTENUATION_DISTANCE, volume.attenuationDistance);
|
|
|
|
result = result || GetMatColor(mat, volume.attenuationColor, AI_MATKEY_VOLUME_ATTENUATION_COLOR) != aiReturn_SUCCESS;
|
|
|
|
|
|
|
|
// Valid if any of these properties are available
|
|
|
|
return result || volume.thicknessTexture.texture;
|
|
|
|
}
|
|
|
|
|
2021-10-07 08:36:53 +00:00
|
|
|
bool glTF2Exporter::GetMatIOR(const aiMaterial &mat, glTF2::MaterialIOR &ior) {
|
2021-10-07 08:39:08 +00:00
|
|
|
return mat.Get(AI_MATKEY_REFRACTI, ior.ior) == aiReturn_SUCCESS;
|
2021-10-07 08:36:53 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 21:11:21 +00:00
|
|
|
bool glTF2Exporter::GetMatEmissiveStrength(const aiMaterial &mat, glTF2::MaterialEmissiveStrength &emissiveStrength) {
|
|
|
|
return mat.Get(AI_MATKEY_EMISSIVE_INTENSITY, emissiveStrength.emissiveStrength) == aiReturn_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2023-03-26 15:03:46 +00:00
|
|
|
void glTF2Exporter::ExportMaterials() {
|
2017-07-19 20:21:43 +00:00
|
|
|
aiString aiName;
|
|
|
|
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
|
2021-09-07 13:04:08 +00:00
|
|
|
ai_assert(mScene->mMaterials[i] != nullptr);
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiMaterial &mat = *(mScene->mMaterials[i]);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
std::string id = "material_" + ai_to_string(i);
|
2017-09-04 02:09:48 +00:00
|
|
|
|
|
|
|
Ref<Material> m = mAsset->materials.Create(id);
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
std::string name;
|
2021-09-07 13:04:08 +00:00
|
|
|
if (mat.Get(AI_MATKEY_NAME, aiName) == AI_SUCCESS) {
|
2017-07-19 20:21:43 +00:00
|
|
|
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
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, aiTextureType_BASE_COLOR);
|
2017-10-19 16:01:40 +00:00
|
|
|
|
|
|
|
if (!m->pbrMetallicRoughness.baseColorTexture.texture) {
|
2023-07-27 15:43:26 +00:00
|
|
|
// if there wasn't a baseColorTexture defined in the source, fallback to any diffuse texture
|
2017-10-19 16:01:40 +00:00
|
|
|
GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, aiTextureType_DIFFUSE);
|
|
|
|
}
|
|
|
|
|
2023-07-24 02:28:01 +00:00
|
|
|
GetMatTex(mat, m->pbrMetallicRoughness.metallicRoughnessTexture, aiTextureType_DIFFUSE_ROUGHNESS);
|
2017-10-19 16:01:40 +00:00
|
|
|
|
2023-07-27 15:42:39 +00:00
|
|
|
if (!m->pbrMetallicRoughness.metallicRoughnessTexture.texture) {
|
2023-07-27 15:43:26 +00:00
|
|
|
// if there wasn't a aiTextureType_DIFFUSE_ROUGHNESS defined in the source, fallback to aiTextureType_METALNESS
|
2023-07-27 15:42:39 +00:00
|
|
|
GetMatTex(mat, m->pbrMetallicRoughness.metallicRoughnessTexture, aiTextureType_METALNESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m->pbrMetallicRoughness.metallicRoughnessTexture.texture) {
|
2023-07-31 15:19:11 +00:00
|
|
|
// if there still wasn't a aiTextureType_METALNESS defined in the source, fallback to AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE
|
2023-07-27 15:42:39 +00:00
|
|
|
GetMatTex(mat, m->pbrMetallicRoughness.metallicRoughnessTexture, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE);
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
if (GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_BASE_COLOR) != AI_SUCCESS) {
|
2017-10-19 16:01:40 +00:00
|
|
|
// if baseColorFactor wasn't defined, then the source is likely not a metallic roughness material.
|
2023-07-27 15:43:26 +00:00
|
|
|
// a fallback to any diffuse color should be used instead
|
2017-10-19 16:01:40 +00:00
|
|
|
GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
}
|
2017-09-06 18:16:52 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
if (mat.Get(AI_MATKEY_METALLIC_FACTOR, m->pbrMetallicRoughness.metallicFactor) != AI_SUCCESS) {
|
2023-07-27 15:43:26 +00:00
|
|
|
// if metallicFactor wasn't defined, then the source is likely not a PBR file, and the metallicFactor should be 0
|
2017-09-06 18:16:52 +00:00
|
|
|
m->pbrMetallicRoughness.metallicFactor = 0;
|
|
|
|
}
|
|
|
|
|
2017-10-19 16:30:32 +00:00
|
|
|
// get roughness if source is gltf2 file
|
2021-09-07 13:04:08 +00:00
|
|
|
if (mat.Get(AI_MATKEY_ROUGHNESS_FACTOR, m->pbrMetallicRoughness.roughnessFactor) != AI_SUCCESS) {
|
2017-10-19 16:30:32 +00:00
|
|
|
// otherwise, try to derive and convert from specular + shininess values
|
|
|
|
aiColor4D specularColor;
|
|
|
|
ai_real shininess;
|
|
|
|
|
2022-02-22 20:07:42 +00:00
|
|
|
if (mat.Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS && mat.Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) {
|
2017-10-19 16:30:32 +00:00
|
|
|
// convert specular color to luminance
|
2017-11-25 10:38:12 +00:00
|
|
|
float specularIntensity = specularColor[0] * 0.2125f + specularColor[1] * 0.7154f + specularColor[2] * 0.0721f;
|
2023-07-27 15:43:26 +00:00
|
|
|
// normalize shininess (assuming max is 1000) with an inverse exponentional curve
|
2017-10-26 15:33:33 +00:00
|
|
|
float normalizedShininess = std::sqrt(shininess / 1000);
|
2017-10-19 16:30:32 +00:00
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// clamp the shininess value between 0 and 1
|
2017-10-26 15:33:33 +00:00
|
|
|
normalizedShininess = std::min(std::max(normalizedShininess, 0.0f), 1.0f);
|
2017-10-19 16:30:32 +00:00
|
|
|
// low specular intensity values should produce a rough material even if shininess is high.
|
2017-10-26 15:33:33 +00:00
|
|
|
normalizedShininess = normalizedShininess * specularIntensity;
|
2017-10-19 16:30:32 +00:00
|
|
|
|
2017-10-26 15:33:33 +00:00
|
|
|
m->pbrMetallicRoughness.roughnessFactor = 1 - normalizedShininess;
|
2017-10-19 16:30:32 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
mat.Get(AI_MATKEY_TWOSIDED, m->doubleSided);
|
|
|
|
mat.Get(AI_MATKEY_GLTF_ALPHACUTOFF, m->alphaCutoff);
|
2017-09-06 18:17:24 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
float opacity;
|
2017-09-18 16:19:55 +00:00
|
|
|
aiString alphaMode;
|
2017-09-12 15:55:52 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
if (mat.Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) {
|
|
|
|
if (opacity < 1) {
|
|
|
|
m->alphaMode = "BLEND";
|
|
|
|
m->pbrMetallicRoughness.baseColorFactor[3] *= opacity;
|
2017-09-06 18:17:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-07 13:04:08 +00:00
|
|
|
if (mat.Get(AI_MATKEY_GLTF_ALPHAMODE, alphaMode) == AI_SUCCESS) {
|
|
|
|
m->alphaMode = alphaMode.C_Str();
|
|
|
|
}
|
2017-08-31 22:30:43 +00:00
|
|
|
|
2023-03-26 14:55:38 +00:00
|
|
|
// This extension has been deprecated, only export with the specific flag enabled, defaults to false. Uses KHR_material_specular default.
|
2023-03-26 15:03:46 +00:00
|
|
|
if (mProperties->GetPropertyBool(AI_CONFIG_USE_GLTF_PBR_SPECULAR_GLOSSINESS)) {
|
2021-09-07 13:04:08 +00:00
|
|
|
// KHR_materials_pbrSpecularGlossiness extension
|
2017-09-05 20:29:00 +00:00
|
|
|
PbrSpecularGlossiness pbrSG;
|
2021-09-07 13:04:08 +00:00
|
|
|
if (GetMatSpecGloss(mat, pbrSG)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness = true;
|
|
|
|
m->pbrSpecularGlossiness = Nullable<PbrSpecularGlossiness>(pbrSG);
|
2017-10-19 16:30:32 +00:00
|
|
|
}
|
2017-08-31 22:30:43 +00:00
|
|
|
}
|
2018-05-18 21:01:25 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
// glTFv2 is either PBR or Unlit
|
|
|
|
aiShadingMode shadingMode = aiShadingMode_PBR_BRDF;
|
|
|
|
mat.Get(AI_MATKEY_SHADING_MODEL, shadingMode);
|
|
|
|
if (shadingMode == aiShadingMode_Unlit) {
|
2018-05-18 21:01:25 +00:00
|
|
|
mAsset->extensionsUsed.KHR_materials_unlit = true;
|
|
|
|
m->unlit = true;
|
2021-09-07 13:04:08 +00:00
|
|
|
} else {
|
|
|
|
// These extensions are not compatible with KHR_materials_unlit or KHR_materials_pbrSpecularGlossiness
|
|
|
|
if (!m->pbrSpecularGlossiness.isPresent) {
|
2022-11-08 21:09:50 +00:00
|
|
|
MaterialSpecular specular;
|
|
|
|
if (GetMatSpecular(mat, specular)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_specular = true;
|
|
|
|
m->materialSpecular = Nullable<MaterialSpecular>(specular);
|
2024-01-05 02:02:26 +00:00
|
|
|
GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_COLOR_DIFFUSE);
|
2022-11-08 21:09:50 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
MaterialSheen sheen;
|
|
|
|
if (GetMatSheen(mat, sheen)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_sheen = true;
|
|
|
|
m->materialSheen = Nullable<MaterialSheen>(sheen);
|
|
|
|
}
|
2020-12-23 09:43:21 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
MaterialClearcoat clearcoat;
|
|
|
|
if (GetMatClearcoat(mat, clearcoat)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_clearcoat = true;
|
|
|
|
m->materialClearcoat = Nullable<MaterialClearcoat>(clearcoat);
|
|
|
|
}
|
2020-12-23 09:43:21 +00:00
|
|
|
|
2021-09-07 13:04:08 +00:00
|
|
|
MaterialTransmission transmission;
|
|
|
|
if (GetMatTransmission(mat, transmission)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_transmission = true;
|
|
|
|
m->materialTransmission = Nullable<MaterialTransmission>(transmission);
|
|
|
|
}
|
2023-01-16 08:12:35 +00:00
|
|
|
|
2021-09-30 07:47:53 +00:00
|
|
|
MaterialVolume volume;
|
|
|
|
if (GetMatVolume(mat, volume)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_volume = true;
|
|
|
|
m->materialVolume = Nullable<MaterialVolume>(volume);
|
|
|
|
}
|
2023-01-16 08:12:35 +00:00
|
|
|
|
2021-10-07 08:36:53 +00:00
|
|
|
MaterialIOR ior;
|
|
|
|
if (GetMatIOR(mat, ior)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_ior = true;
|
|
|
|
m->materialIOR = Nullable<MaterialIOR>(ior);
|
|
|
|
}
|
2022-11-04 16:37:28 +00:00
|
|
|
|
|
|
|
MaterialEmissiveStrength emissiveStrength;
|
2022-11-03 21:11:21 +00:00
|
|
|
if (GetMatEmissiveStrength(mat, emissiveStrength)) {
|
|
|
|
mAsset->extensionsUsed.KHR_materials_emissive_strength = true;
|
|
|
|
m->materialEmissiveStrength = Nullable<MaterialEmissiveStrength>(emissiveStrength);
|
2021-10-07 08:36:53 +00:00
|
|
|
}
|
2021-09-07 13:04:08 +00:00
|
|
|
}
|
2020-12-23 09:43:21 +00:00
|
|
|
}
|
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.
|
|
|
|
*/
|
2021-09-07 13:04:08 +00:00
|
|
|
bool FindMeshNode(Ref<Node> &nodeIn, Ref<Node> &meshNode, const std::string &meshID) {
|
2017-09-17 19:11:01 +00:00
|
|
|
for (unsigned int i = 0; i < nodeIn->meshes.size(); ++i) {
|
|
|
|
if (meshID.compare(nodeIn->meshes[i]->id) == 0) {
|
2017-09-18 18:48:07 +00:00
|
|
|
meshNode = nodeIn;
|
|
|
|
return true;
|
2017-09-17 19:11:01 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < nodeIn->children.size(); ++i) {
|
2021-09-13 20:38:20 +00:00
|
|
|
if (FindMeshNode(nodeIn->children[i], meshNode, meshID)) {
|
2017-09-18 18:48:07 +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.
|
|
|
|
*/
|
2021-09-13 20:38:20 +00:00
|
|
|
Ref<Node> FindSkeletonRootJoint(Ref<Skin> &skinRef) {
|
2017-07-19 20:21:43 +00:00
|
|
|
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;
|
2022-06-07 22:10:13 +00:00
|
|
|
} while (parentNodeRef && !parentNodeRef->jointName.empty());
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
return parentNodeRef;
|
|
|
|
}
|
|
|
|
|
2023-09-22 21:10:39 +00:00
|
|
|
struct boneIndexWeightPair {
|
|
|
|
unsigned int indexJoint;
|
|
|
|
float weight;
|
|
|
|
bool operator()(boneIndexWeightPair &a, boneIndexWeightPair &b) {
|
|
|
|
return a.weight > b.weight;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-01-16 08:12:35 +00:00
|
|
|
void ExportSkin(Asset &mAsset, const aiMesh *aimesh, Ref<Mesh> &meshRef, Ref<Buffer> &bufferRef, Ref<Skin> &skinRef,
|
2023-09-22 21:10:39 +00:00
|
|
|
std::vector<aiMatrix4x4> &inverseBindMatricesData, bool unlimitedBonesPerVertex) {
|
2017-07-19 20:21:43 +00:00
|
|
|
if (aimesh->mNumBones < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the vertex joint and weight data.
|
2021-09-13 20:38:20 +00:00
|
|
|
const size_t NumVerts(aimesh->mNumVertices);
|
|
|
|
int *jointsPerVertex = new int[NumVerts];
|
2023-09-22 21:10:39 +00:00
|
|
|
std::vector<std::vector<boneIndexWeightPair>> allVerticesPairs;
|
|
|
|
int maxJointsPerVertex = 0;
|
2017-07-19 20:21:43 +00:00
|
|
|
for (size_t i = 0; i < NumVerts; ++i) {
|
|
|
|
jointsPerVertex[i] = 0;
|
2023-09-22 21:10:39 +00:00
|
|
|
std::vector<boneIndexWeightPair> vertexPair;
|
|
|
|
allVerticesPairs.push_back(vertexPair);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int idx_bone = 0; idx_bone < aimesh->mNumBones; ++idx_bone) {
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiBone *aib = aimesh->mBones[idx_bone];
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-09-18 12:18:45 +00:00
|
|
|
unsigned int jointNamesIndex = 0;
|
2017-07-19 20:21:43 +00:00
|
|
|
bool addJointToJointNames = true;
|
2021-09-13 20:38:20 +00:00
|
|
|
for (unsigned int idx_joint = 0; idx_joint < skinRef->jointNames.size(); ++idx_joint) {
|
2017-07-19 20:21:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-09-22 21:10:39 +00:00
|
|
|
// aib->mWeights =====> temp pairs data
|
|
|
|
for (unsigned int idx_weights = 0; idx_weights < aib->mNumWeights;
|
|
|
|
++idx_weights) {
|
2017-07-19 20:21:43 +00:00
|
|
|
unsigned int vertexId = aib->mWeights[idx_weights].mVertexId;
|
2021-09-13 20:38:20 +00:00
|
|
|
float vertWeight = aib->mWeights[idx_weights].mWeight;
|
2023-09-22 21:10:39 +00:00
|
|
|
allVerticesPairs[vertexId].push_back({jointNamesIndex, vertWeight});
|
|
|
|
jointsPerVertex[vertexId] += 1;
|
|
|
|
maxJointsPerVertex =
|
|
|
|
std::max(maxJointsPerVertex, jointsPerVertex[vertexId]);
|
|
|
|
}
|
|
|
|
} // End: for-loop mNumMeshes
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2023-09-22 21:10:39 +00:00
|
|
|
if (!unlimitedBonesPerVertex){
|
|
|
|
// skinning limited only for 4 bones per vertex, default
|
|
|
|
maxJointsPerVertex = 4;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2023-09-22 21:10:39 +00:00
|
|
|
// temp pairs data =====> vertexWeightData
|
|
|
|
size_t numGroups = (maxJointsPerVertex - 1) / 4 + 1;
|
|
|
|
vec4 *vertexJointData = new vec4[NumVerts * numGroups];
|
|
|
|
vec4 *vertexWeightData = new vec4[NumVerts * numGroups];
|
|
|
|
for (size_t indexVertex = 0; indexVertex < NumVerts; ++indexVertex) {
|
|
|
|
// order pairs by weight for each vertex
|
|
|
|
std::sort(allVerticesPairs[indexVertex].begin(),
|
|
|
|
allVerticesPairs[indexVertex].end(),
|
|
|
|
boneIndexWeightPair());
|
|
|
|
for (size_t indexGroup = 0; indexGroup < numGroups; ++indexGroup) {
|
|
|
|
for (size_t indexJoint = 0; indexJoint < 4; ++indexJoint) {
|
|
|
|
size_t indexBone = indexGroup * 4 + indexJoint;
|
|
|
|
size_t indexData = indexVertex + NumVerts * indexGroup;
|
|
|
|
if (indexBone >= allVerticesPairs[indexVertex].size()) {
|
|
|
|
vertexJointData[indexData][indexJoint] = 0.f;
|
|
|
|
vertexWeightData[indexData][indexJoint] = 0.f;
|
|
|
|
} else {
|
|
|
|
vertexJointData[indexData][indexJoint] =
|
|
|
|
static_cast<float>(
|
|
|
|
allVerticesPairs[indexVertex][indexBone].indexJoint);
|
|
|
|
vertexWeightData[indexData][indexJoint] =
|
|
|
|
allVerticesPairs[indexVertex][indexBone].weight;
|
|
|
|
}
|
2021-11-13 04:07:29 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2023-09-22 21:10:39 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2023-09-22 21:10:39 +00:00
|
|
|
for (size_t idx_group = 0; idx_group < numGroups; ++idx_group) {
|
|
|
|
Mesh::Primitive &p = meshRef->primitives.back();
|
|
|
|
Ref<Accessor> vertexJointAccessor = ExportData(
|
|
|
|
mAsset, skinRef->id, bufferRef, aimesh->mNumVertices,
|
|
|
|
vertexJointData + idx_group * NumVerts,
|
|
|
|
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;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2023-09-22 21:10:39 +00:00
|
|
|
p.attributes.joint.push_back(vertexJointAccessor);
|
|
|
|
delete[] arrys;
|
|
|
|
}
|
|
|
|
Ref<Accessor> vertexWeightAccessor = ExportData(
|
|
|
|
mAsset, skinRef->id, bufferRef, aimesh->mNumVertices,
|
|
|
|
vertexWeightData + idx_group * NumVerts,
|
|
|
|
AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT);
|
|
|
|
if (vertexWeightAccessor) {
|
|
|
|
p.attributes.weight.push_back(vertexWeightAccessor);
|
2018-04-26 02:56:56 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
delete[] jointsPerVertex;
|
|
|
|
delete[] vertexWeightData;
|
|
|
|
delete[] vertexJointData;
|
2017-09-01 21:49:04 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::ExportMeshes() {
|
2018-07-19 09:29:04 +00:00
|
|
|
typedef decltype(aiFace::mNumIndices) IndicesType;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
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) {
|
2021-09-13 20:38:20 +00:00
|
|
|
b = mAsset->buffers.Create(bufferId);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------
|
|
|
|
// Initialize variables for the skin
|
|
|
|
bool createSkin = false;
|
|
|
|
for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) {
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiMesh *aim = mScene->mMeshes[idx_mesh];
|
|
|
|
if (aim->HasBones()) {
|
2017-07-19 20:21:43 +00:00
|
|
|
createSkin = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Skin> skinRef;
|
|
|
|
std::string skinName = mAsset->FindUniqueID("skin", "skin");
|
|
|
|
std::vector<aiMatrix4x4> inverseBindMatricesData;
|
2021-09-13 20:38:20 +00:00
|
|
|
if (createSkin) {
|
2017-07-19 20:21:43 +00:00
|
|
|
skinRef = mAsset->skins.Create(skinName);
|
|
|
|
skinRef->name = skinName;
|
|
|
|
}
|
|
|
|
//----------------------------------------
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) {
|
|
|
|
const aiMesh *aim = mScene->mMeshes[idx_mesh];
|
2017-07-19 20:21:43 +00:00
|
|
|
|
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);
|
2021-09-13 20:38:20 +00:00
|
|
|
Mesh::Primitive &p = m->primitives.back();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
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);
|
2021-03-29 17:03:01 +00:00
|
|
|
p.ngonEncoded = (aim->mPrimitiveTypes & aiPrimitiveType_NGONEncodingFlag) != 0;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
/******************* Vertices ********************/
|
2022-02-22 20:07:42 +00:00
|
|
|
Ref<Accessor> v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3,
|
2023-07-27 15:43:26 +00:00
|
|
|
AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
|
2022-02-22 20:07:42 +00:00
|
|
|
if (v) {
|
|
|
|
p.attributes.position.push_back(v);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
/******************** Normals ********************/
|
2018-01-16 08:56:44 +00:00
|
|
|
// Normalize all normals as the validator can emit a warning otherwise
|
2021-09-13 20:38:20 +00:00
|
|
|
if (nullptr != aim->mNormals) {
|
|
|
|
for (auto i = 0u; i < aim->mNumVertices; ++i) {
|
|
|
|
aim->mNormals[i].NormalizeSafe();
|
2018-05-12 06:10:26 +00:00
|
|
|
}
|
2018-01-16 08:56:44 +00:00
|
|
|
}
|
|
|
|
|
2023-01-16 08:12:35 +00:00
|
|
|
Ref<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3,
|
2023-07-27 15:43:26 +00:00
|
|
|
AttribType::VEC3, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
|
2022-02-22 20:07:42 +00:00
|
|
|
if (n) {
|
|
|
|
p.attributes.normal.push_back(n);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
/************** Texture coordinates **************/
|
2017-07-19 20:21:43 +00:00
|
|
|
for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
|
2022-02-22 20:07:42 +00:00
|
|
|
if (!aim->HasTextureCoords(i)) {
|
2021-09-13 20:38:20 +00:00
|
|
|
continue;
|
2022-02-22 20:07:42 +00:00
|
|
|
}
|
2020-05-07 06:59:48 +00:00
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
// Flip UV y coords
|
2021-09-13 20:38:20 +00:00
|
|
|
if (aim->mNumUVComponents[i] > 1) {
|
2017-07-19 20:21:43 +00:00
|
|
|
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;
|
|
|
|
|
2023-01-16 08:12:35 +00:00
|
|
|
Ref<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i],
|
2023-07-27 15:43:26 +00:00
|
|
|
AttribType::VEC3, type, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
|
2022-02-22 20:07:42 +00:00
|
|
|
if (tc) {
|
|
|
|
p.attributes.texcoord.push_back(tc);
|
|
|
|
}
|
2021-09-13 20:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************** Vertex colors ****************/
|
|
|
|
for (unsigned int indexColorChannel = 0; indexColorChannel < aim->GetNumColorChannels(); ++indexColorChannel) {
|
2022-02-22 20:07:42 +00:00
|
|
|
Ref<Accessor> c = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mColors[indexColorChannel],
|
2023-07-27 15:43:26 +00:00
|
|
|
AttribType::VEC4, AttribType::VEC4, ComponentType_FLOAT, BufferViewTarget_ARRAY_BUFFER);
|
2022-02-22 20:07:42 +00:00
|
|
|
if (c) {
|
2021-09-13 20:38:20 +00:00
|
|
|
p.attributes.color.push_back(c);
|
2022-02-22 20:07:42 +00:00
|
|
|
}
|
2021-09-13 20:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************** Vertices indices ****************/
|
|
|
|
if (aim->mNumFaces > 0) {
|
|
|
|
std::vector<IndicesType> indices;
|
|
|
|
unsigned int nIndicesPerFace = aim->mFaces[0].mNumIndices;
|
2017-07-19 20:21:43 +00:00
|
|
|
indices.resize(aim->mNumFaces * nIndicesPerFace);
|
|
|
|
for (size_t i = 0; i < aim->mNumFaces; ++i) {
|
|
|
|
for (size_t j = 0; j < nIndicesPerFace; ++j) {
|
2021-09-13 20:38:20 +00:00
|
|
|
indices[i * nIndicesPerFace + j] = IndicesType(aim->mFaces[i].mIndices[j]);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 08:12:35 +00:00
|
|
|
p.indices = ExportData(*mAsset, meshId, b, indices.size(), &indices[0], AttribType::SCALAR, AttribType::SCALAR,
|
2023-07-27 15:43:26 +00:00
|
|
|
ComponentType_UNSIGNED_INT, BufferViewTarget_ELEMENT_ARRAY_BUFFER);
|
2021-09-13 20:38:20 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
switch (aim->mPrimitiveTypes) {
|
2021-09-13 20:38:20 +00:00
|
|
|
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;
|
2022-02-22 20:07:42 +00:00
|
|
|
break;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2023-09-22 21:10:39 +00:00
|
|
|
// /*************** Skins ****************/
|
|
|
|
// if (aim->HasBones()) {
|
|
|
|
// ExportSkin(*mAsset, aim, m, b, skinRef, inverseBindMatricesData);
|
|
|
|
// }
|
2017-08-28 03:47:31 +00:00
|
|
|
/*************** Skins ****************/
|
2021-09-13 20:38:20 +00:00
|
|
|
if (aim->HasBones()) {
|
2023-09-22 21:10:39 +00:00
|
|
|
bool unlimitedBonesPerVertex =
|
|
|
|
this->mProperties->HasPropertyBool(
|
|
|
|
AI_CONFIG_EXPORT_GLTF_UNLIMITED_SKINNING_BONES_PER_VERTEX) &&
|
|
|
|
this->mProperties->GetPropertyBool(
|
|
|
|
AI_CONFIG_EXPORT_GLTF_UNLIMITED_SKINNING_BONES_PER_VERTEX);
|
|
|
|
ExportSkin(*mAsset, aim, m, b, skinRef, inverseBindMatricesData,
|
|
|
|
unlimitedBonesPerVertex);
|
2017-09-01 21:49:04 +00:00
|
|
|
}
|
2020-03-12 19:14:17 +00:00
|
|
|
|
|
|
|
/*************** Targets for blendshapes ****************/
|
|
|
|
if (aim->mNumAnimMeshes > 0) {
|
2020-05-19 23:10:41 +00:00
|
|
|
bool bUseSparse = this->mProperties->HasPropertyBool("GLTF2_SPARSE_ACCESSOR_EXP") &&
|
|
|
|
this->mProperties->GetPropertyBool("GLTF2_SPARSE_ACCESSOR_EXP");
|
|
|
|
bool bIncludeNormal = this->mProperties->HasPropertyBool("GLTF2_TARGET_NORMAL_EXP") &&
|
|
|
|
this->mProperties->GetPropertyBool("GLTF2_TARGET_NORMAL_EXP");
|
2020-06-05 19:17:27 +00:00
|
|
|
bool bExportTargetNames = this->mProperties->HasPropertyBool("GLTF2_TARGETNAMES_EXP") &&
|
2021-09-13 20:38:20 +00:00
|
|
|
this->mProperties->GetPropertyBool("GLTF2_TARGETNAMES_EXP");
|
2020-05-19 23:10:41 +00:00
|
|
|
|
2020-03-12 19:14:17 +00:00
|
|
|
p.targets.resize(aim->mNumAnimMeshes);
|
|
|
|
for (unsigned int am = 0; am < aim->mNumAnimMeshes; ++am) {
|
|
|
|
aiAnimMesh *pAnimMesh = aim->mAnimMeshes[am];
|
2022-02-22 20:07:42 +00:00
|
|
|
if (bExportTargetNames) {
|
|
|
|
m->targetNames.emplace_back(pAnimMesh->mName.data);
|
|
|
|
}
|
2020-03-12 19:14:17 +00:00
|
|
|
// 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];
|
|
|
|
}
|
2020-05-19 23:10:41 +00:00
|
|
|
Ref<Accessor> vec;
|
|
|
|
if (bUseSparse) {
|
|
|
|
vec = ExportDataSparse(*mAsset, meshId, b,
|
|
|
|
pAnimMesh->mNumVertices, pPositionDiff,
|
|
|
|
AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
|
|
|
|
} else {
|
|
|
|
vec = ExportData(*mAsset, meshId, b,
|
|
|
|
pAnimMesh->mNumVertices, pPositionDiff,
|
|
|
|
AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
|
|
|
|
}
|
2020-03-19 11:58:41 +00:00
|
|
|
if (vec) {
|
|
|
|
p.targets[am].position.push_back(vec);
|
2020-03-12 19:14:17 +00:00
|
|
|
}
|
|
|
|
delete[] pPositionDiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal
|
2020-05-19 23:10:41 +00:00
|
|
|
if (pAnimMesh->HasNormals() && bIncludeNormal) {
|
2020-03-12 19:14:17 +00:00
|
|
|
aiVector3D *pNormalDiff = new aiVector3D[pAnimMesh->mNumVertices];
|
|
|
|
for (unsigned int vt = 0; vt < pAnimMesh->mNumVertices; ++vt) {
|
|
|
|
pNormalDiff[vt] = pAnimMesh->mNormals[vt] - aim->mNormals[vt];
|
|
|
|
}
|
2020-05-19 23:10:41 +00:00
|
|
|
Ref<Accessor> vec;
|
|
|
|
if (bUseSparse) {
|
|
|
|
vec = ExportDataSparse(*mAsset, meshId, b,
|
|
|
|
pAnimMesh->mNumVertices, pNormalDiff,
|
|
|
|
AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
|
|
|
|
} else {
|
|
|
|
vec = ExportData(*mAsset, meshId, b,
|
|
|
|
pAnimMesh->mNumVertices, pNormalDiff,
|
|
|
|
AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
|
|
|
|
}
|
2020-03-19 11:58:41 +00:00
|
|
|
if (vec) {
|
|
|
|
p.targets[am].normal.push_back(vec);
|
2020-03-12 19:14:17 +00:00
|
|
|
}
|
|
|
|
delete[] pNormalDiff;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tangent?
|
|
|
|
}
|
|
|
|
}
|
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
|
2021-02-02 21:19:57 +00:00
|
|
|
bool bAddCustomizedProperty = this->mProperties->HasPropertyBool("GLTF2_CUSTOMIZE_PROPERTY");
|
2017-07-19 20:21:43 +00:00
|
|
|
if (createSkin) {
|
2021-09-13 20:38:20 +00:00
|
|
|
mat4 *invBindMatrixData = new mat4[inverseBindMatricesData.size()];
|
|
|
|
for (unsigned int idx_joint = 0; idx_joint < inverseBindMatricesData.size(); ++idx_joint) {
|
2017-07-19 20:21:43 +00:00
|
|
|
CopyValue(inverseBindMatricesData[idx_joint], invBindMatrixData[idx_joint]);
|
|
|
|
}
|
|
|
|
|
2018-12-30 09:33:21 +00:00
|
|
|
Ref<Accessor> invBindMatrixAccessor = ExportData(*mAsset, skinName, b,
|
|
|
|
static_cast<unsigned int>(inverseBindMatricesData.size()),
|
2021-09-13 20:38:20 +00:00
|
|
|
invBindMatrixData, AttribType::MAT4, AttribType::MAT4, ComponentType_FLOAT);
|
2018-12-30 09:33:21 +00:00
|
|
|
if (invBindMatrixAccessor) {
|
|
|
|
skinRef->inverseBindMatrices = invBindMatrixAccessor;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
// Identity Matrix =====> skinRef->bindShapeMatrix
|
|
|
|
// Temporary. Hard-coded identity matrix here
|
2021-02-02 21:19:57 +00:00
|
|
|
skinRef->bindShapeMatrix.isPresent = bAddCustomizedProperty;
|
2017-07-19 20:21:43 +00:00
|
|
|
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);
|
2021-09-13 20:38:20 +00:00
|
|
|
if (bAddCustomizedProperty)
|
2021-02-02 21:19:57 +00:00
|
|
|
meshNode->skeletons.push_back(rootJoint);
|
2017-07-26 00:26:18 +00:00
|
|
|
meshNode->skin = skinRef;
|
|
|
|
}
|
2018-12-30 09:33:21 +00:00
|
|
|
delete[] invBindMatrixData;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 09:33:21 +00:00
|
|
|
// Merges a node's multiple meshes (with one primitive each) into one mesh with multiple primitives
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::MergeMeshes() {
|
2017-09-17 21:00:57 +00:00
|
|
|
for (unsigned int n = 0; n < mAsset->nodes.Size(); ++n) {
|
|
|
|
Ref<Node> node = mAsset->nodes.Get(n);
|
|
|
|
|
2017-09-24 09:29:43 +00:00
|
|
|
unsigned int nMeshes = static_cast<unsigned int>(node->meshes.size());
|
2017-09-17 21:00:57 +00:00
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// skip if it's 1 or less meshes per node
|
2017-09-18 14:43:05 +00:00
|
|
|
if (nMeshes > 1) {
|
2017-09-17 21:00:57 +00:00
|
|
|
Ref<Mesh> firstMesh = node->meshes.at(0);
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// loop backwards to allow easy removal of a mesh from a node once it's merged
|
2017-09-18 14:43:05 +00:00
|
|
|
for (unsigned int m = nMeshes - 1; m >= 1; --m) {
|
2017-09-17 21:00:57 +00:00
|
|
|
Ref<Mesh> mesh = node->meshes.at(m);
|
2017-09-18 14:43:05 +00:00
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// append this mesh's primitives to the first mesh's primitives
|
2017-11-07 20:13:01 +00:00
|
|
|
firstMesh->primitives.insert(
|
2021-09-13 20:38:20 +00:00
|
|
|
firstMesh->primitives.end(),
|
|
|
|
mesh->primitives.begin(),
|
|
|
|
mesh->primitives.end());
|
2017-11-07 20:13:01 +00:00
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// remove the mesh from the list of meshes
|
2017-11-07 20:13:01 +00:00
|
|
|
unsigned int removedIndex = mAsset->meshes.Remove(mesh->id.c_str());
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// find the presence of the removed mesh in other nodes
|
2017-11-07 20:13:01 +00:00
|
|
|
for (unsigned int nn = 0; nn < mAsset->nodes.Size(); ++nn) {
|
2020-03-08 20:24:01 +00:00
|
|
|
Ref<Node> curNode = mAsset->nodes.Get(nn);
|
2017-11-07 20:13:01 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
for (unsigned int mm = 0; mm < curNode->meshes.size(); ++mm) {
|
|
|
|
Ref<Mesh> &meshRef = curNode->meshes.at(mm);
|
2017-11-07 20:13:01 +00:00
|
|
|
unsigned int meshIndex = meshRef.GetIndex();
|
|
|
|
|
|
|
|
if (meshIndex == removedIndex) {
|
2020-11-30 19:11:41 +00:00
|
|
|
curNode->meshes.erase(curNode->meshes.begin() + mm);
|
2017-11-07 20:13:01 +00:00
|
|
|
} else if (meshIndex > removedIndex) {
|
|
|
|
Ref<Mesh> newMeshRef = mAsset->meshes.Get(meshIndex - 1);
|
|
|
|
|
|
|
|
meshRef = newMeshRef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 21:00:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
// since we were looping backwards, reverse the order of merged primitives to their original order
|
2017-09-18 14:43:05 +00:00
|
|
|
std::reverse(firstMesh->primitives.begin() + 1, firstMesh->primitives.end());
|
2017-09-17 21:00:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
/*
|
|
|
|
* Export the root node of the node hierarchy.
|
|
|
|
* Calls ExportNode for all children.
|
|
|
|
*/
|
2021-09-13 20:38:20 +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"));
|
|
|
|
|
2018-05-03 09:07:39 +00:00
|
|
|
node->name = n->mName.C_Str();
|
|
|
|
|
2024-02-06 20:24:41 +00:00
|
|
|
if (!n->mTransformation.IsIdentity(configEpsilon)) {
|
2017-07-19 20:21:43 +00:00
|
|
|
node->matrix.isPresent = true;
|
|
|
|
CopyValue(n->mTransformation, node->matrix.value);
|
|
|
|
}
|
|
|
|
|
2017-09-17 19:11:01 +00:00
|
|
|
for (unsigned int i = 0; i < n->mNumMeshes; ++i) {
|
2022-02-22 20:07:42 +00:00
|
|
|
node->meshes.emplace_back(mAsset->meshes.Get(n->mMeshes[i]));
|
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);
|
2022-02-22 20:07:42 +00:00
|
|
|
node->children.emplace_back(mAsset->nodes.Get(idx));
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return node.GetIndex();
|
|
|
|
}
|
|
|
|
|
2023-07-27 15:43:26 +00:00
|
|
|
/*
|
2017-07-19 20:21:43 +00:00
|
|
|
* Export node and recursively calls ExportNode for all children.
|
|
|
|
* Since these nodes are not the root node, we also export the parent Ref<Node>
|
|
|
|
*/
|
2021-09-13 20:38:20 +00:00
|
|
|
unsigned int glTF2Exporter::ExportNode(const aiNode *n, Ref<Node> &parent) {
|
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
|
|
|
|
2022-07-21 23:04:05 +00:00
|
|
|
ExportNodeExtras(n->mMetaData, node->extras);
|
2022-07-21 22:44:51 +00:00
|
|
|
|
2024-02-06 20:24:41 +00:00
|
|
|
if (!n->mTransformation.IsIdentity(configEpsilon)) {
|
2021-09-13 20:38:20 +00:00
|
|
|
if (mScene->mNumAnimations > 0 || (mProperties && mProperties->HasPropertyBool("GLTF2_NODE_IN_TRS"))) {
|
|
|
|
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);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-09-17 19:11:01 +00:00
|
|
|
for (unsigned int i = 0; i < n->mNumMeshes; ++i) {
|
2022-02-22 20:07:42 +00:00
|
|
|
node->meshes.emplace_back(mAsset->meshes.Get(n->mMeshes[i]));
|
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);
|
2022-02-22 20:07:42 +00:00
|
|
|
node->children.emplace_back(mAsset->nodes.Get(idx));
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return node.GetIndex();
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::ExportScene() {
|
2021-09-07 13:04:08 +00:00
|
|
|
// Use the name of the scene if specified
|
|
|
|
const std::string sceneName = (mScene->mName.length > 0) ? mScene->mName.C_Str() : "defaultScene";
|
|
|
|
|
|
|
|
// Ensure unique
|
|
|
|
Ref<Scene> scene = mAsset->scenes.Create(mAsset->FindUniqueID(sceneName, ""));
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
// root node will be the first one exported (idx 0)
|
|
|
|
if (mAsset->nodes.Size() > 0) {
|
2022-02-22 20:07:42 +00:00
|
|
|
scene->nodes.emplace_back(mAsset->nodes.Get(0u));
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set as the default scene
|
|
|
|
mAsset->scene = scene;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::ExportMetadata() {
|
|
|
|
AssetMetadata &asset = mAsset->asset;
|
2017-09-06 19:32:44 +00:00
|
|
|
asset.version = "2.0";
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
char buffer[256];
|
2020-01-22 11:40:57 +00:00
|
|
|
ai_snprintf(buffer, 256, "Open Asset Import Library (assimp v%d.%d.%x)",
|
2021-09-13 20:38:20 +00:00
|
|
|
aiGetVersionMajor(), aiGetVersionMinor(), aiGetVersionRevision());
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
asset.generator = buffer;
|
2019-12-09 14:30:12 +00:00
|
|
|
|
|
|
|
// Copyright
|
2021-09-13 20:38:20 +00:00
|
|
|
aiString copyright_str;
|
|
|
|
if (mScene->mMetaData != nullptr && mScene->mMetaData->Get(AI_METADATA_SOURCE_COPYRIGHT, copyright_str)) {
|
2019-12-09 14:30:12 +00:00
|
|
|
asset.copyright = copyright_str.C_Str();
|
2021-09-13 20:38:20 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 17:06:23 +00:00
|
|
|
inline Ref<Accessor> GetSamplerInputRef(Asset &asset, std::string &animId, Ref<Buffer> &buffer, std::vector<ai_real> ×) {
|
2019-01-28 15:02:09 +00:00
|
|
|
return ExportData(asset, animId, buffer, (unsigned int)times.size(), ×[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_FLOAT);
|
2018-10-26 22:36:34 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
inline void ExtractTranslationSampler(Asset &asset, std::string &animId, Ref<Buffer> &buffer, const aiNodeAnim *nodeChannel, float ticksPerSecond, Animation::Sampler &sampler) {
|
2018-10-26 22:36:34 +00:00
|
|
|
const unsigned int numKeyframes = nodeChannel->mNumPositionKeys;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-05-06 19:07:38 +00:00
|
|
|
std::vector<ai_real> times(numKeyframes);
|
|
|
|
std::vector<ai_real> values(numKeyframes * 3);
|
2018-10-26 22:36:34 +00:00
|
|
|
for (unsigned int i = 0; i < numKeyframes; ++i) {
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiVectorKey &key = nodeChannel->mPositionKeys[i];
|
2018-10-26 22:36:34 +00:00
|
|
|
// mTime is measured in ticks, but GLTF time is measured in seconds, so convert.
|
|
|
|
times[i] = static_cast<float>(key.mTime / ticksPerSecond);
|
2023-07-27 15:43:26 +00:00
|
|
|
values[(i * 3) + 0] = (ai_real)key.mValue.x;
|
|
|
|
values[(i * 3) + 1] = (ai_real)key.mValue.y;
|
|
|
|
values[(i * 3) + 2] = (ai_real)key.mValue.z;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 22:36:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
inline void ExtractScaleSampler(Asset &asset, std::string &animId, Ref<Buffer> &buffer, const aiNodeAnim *nodeChannel, float ticksPerSecond, Animation::Sampler &sampler) {
|
2018-10-26 22:36:34 +00:00
|
|
|
const unsigned int numKeyframes = nodeChannel->mNumScalingKeys;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-05-06 19:07:38 +00:00
|
|
|
std::vector<ai_real> times(numKeyframes);
|
|
|
|
std::vector<ai_real> values(numKeyframes * 3);
|
2018-10-26 22:36:34 +00:00
|
|
|
for (unsigned int i = 0; i < numKeyframes; ++i) {
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiVectorKey &key = nodeChannel->mScalingKeys[i];
|
2018-10-26 22:36:34 +00:00
|
|
|
// mTime is measured in ticks, but GLTF time is measured in seconds, so convert.
|
|
|
|
times[i] = static_cast<float>(key.mTime / ticksPerSecond);
|
2023-07-27 15:43:26 +00:00
|
|
|
values[(i * 3) + 0] = (ai_real)key.mValue.x;
|
|
|
|
values[(i * 3) + 1] = (ai_real)key.mValue.y;
|
|
|
|
values[(i * 3) + 2] = (ai_real)key.mValue.z;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2018-10-26 22:36:34 +00:00
|
|
|
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;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
inline void ExtractRotationSampler(Asset &asset, std::string &animId, Ref<Buffer> &buffer, const aiNodeAnim *nodeChannel, float ticksPerSecond, Animation::Sampler &sampler) {
|
2018-10-26 22:36:34 +00:00
|
|
|
const unsigned int numKeyframes = nodeChannel->mNumRotationKeys;
|
|
|
|
|
2021-05-06 19:07:38 +00:00
|
|
|
std::vector<ai_real> times(numKeyframes);
|
|
|
|
std::vector<ai_real> values(numKeyframes * 4);
|
2018-10-26 22:36:34 +00:00
|
|
|
for (unsigned int i = 0; i < numKeyframes; ++i) {
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiQuatKey &key = nodeChannel->mRotationKeys[i];
|
2018-10-26 22:36:34 +00:00
|
|
|
// mTime is measured in ticks, but GLTF time is measured in seconds, so convert.
|
|
|
|
times[i] = static_cast<float>(key.mTime / ticksPerSecond);
|
2023-07-27 15:43:26 +00:00
|
|
|
values[(i * 4) + 0] = (ai_real)key.mValue.x;
|
|
|
|
values[(i * 4) + 1] = (ai_real)key.mValue.y;
|
|
|
|
values[(i * 4) + 2] = (ai_real)key.mValue.z;
|
|
|
|
values[(i * 4) + 3] = (ai_real)key.mValue.w;
|
2018-10-26 22:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
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);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
void glTF2Exporter::ExportAnimations() {
|
|
|
|
Ref<Buffer> bufferRef = mAsset->buffers.Get(unsigned(0));
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mScene->mNumAnimations; ++i) {
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiAnimation *anim = mScene->mAnimations[i];
|
2018-10-26 22:36:34 +00:00
|
|
|
const float ticksPerSecond = static_cast<float>(anim->mTicksPerSecond);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
std::string nameAnim = "anim";
|
|
|
|
if (anim->mName.length > 0) {
|
|
|
|
nameAnim = anim->mName.C_Str();
|
|
|
|
}
|
2020-12-13 16:02:50 +00:00
|
|
|
Ref<Animation> animRef = mAsset->animations.Create(nameAnim);
|
2021-02-17 04:08:22 +00:00
|
|
|
animRef->name = nameAnim;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
for (unsigned int channelIndex = 0; channelIndex < anim->mNumChannels; ++channelIndex) {
|
2021-09-13 20:38:20 +00:00
|
|
|
const aiNodeAnim *nodeChannel = anim->mChannels[channelIndex];
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
std::string name = nameAnim + "_" + ai_to_string(channelIndex);
|
2017-07-19 20:21:43 +00:00
|
|
|
name = mAsset->FindUniqueID(name, "animation");
|
|
|
|
|
2018-10-26 22:36:34 +00:00
|
|
|
Ref<Node> animNode = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str());
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
if (nodeChannel->mNumPositionKeys > 0) {
|
2020-12-13 16:02:50 +00:00
|
|
|
Animation::Sampler translationSampler;
|
|
|
|
ExtractTranslationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, translationSampler);
|
|
|
|
AddSampler(animRef, animNode, translationSampler, AnimationPath_TRANSLATION);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
if (nodeChannel->mNumRotationKeys > 0) {
|
2020-12-13 16:02:50 +00:00
|
|
|
Animation::Sampler rotationSampler;
|
|
|
|
ExtractRotationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, rotationSampler);
|
|
|
|
AddSampler(animRef, animNode, rotationSampler, AnimationPath_ROTATION);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-09-13 20:38:20 +00:00
|
|
|
if (nodeChannel->mNumScalingKeys > 0) {
|
2020-12-13 16:02:50 +00:00
|
|
|
Animation::Sampler scaleSampler;
|
|
|
|
ExtractScaleSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, scaleSampler);
|
|
|
|
AddSampler(animRef, animNode, scaleSampler, AnimationPath_SCALE);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
} // 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
|