2017-09-01 21:49:04 +00:00
|
|
|
/*
|
2017-07-19 20:21:43 +00:00
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, 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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2020-05-02 13:14:38 +00:00
|
|
|
#include "AssetLib/glTF/glTFCommon.h"
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
#include <assimp/MemoryIOWrapper.h>
|
2020-05-02 13:14:38 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2017-07-19 20:21:43 +00:00
|
|
|
#include <assimp/DefaultLogger.hpp>
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
#ifdef ASSIMP_ENABLE_DRACO
|
2021-01-27 10:42:28 +00:00
|
|
|
|
|
|
|
// Google draco library headers spew many warnings. Bad Google, no cookie
|
|
|
|
#if _MSC_VER
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4018) // Signed/unsigned mismatch
|
|
|
|
#pragma warning(disable : 4804) // Unsafe use of type 'bool'
|
|
|
|
#elif defined(__clang__)
|
|
|
|
#pragma clang diagnostic push
|
|
|
|
#pragma clang diagnostic ignored "-Wsign-compare"
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wbool-compare"
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-compare"
|
|
|
|
#endif
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
#include "draco/compression/decode.h"
|
|
|
|
#include "draco/core/decoder_buffer.h"
|
2021-01-27 10:42:28 +00:00
|
|
|
|
|
|
|
#if _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#elif defined(__clang__)
|
|
|
|
#pragma clang diagnostic pop
|
|
|
|
#elif defined(__GNUC__)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
2021-01-26 16:24:18 +00:00
|
|
|
#ifndef DRACO_MESH_COMPRESSION_SUPPORTED
|
|
|
|
#error glTF: KHR_draco_mesh_compression: draco library must have DRACO_MESH_COMPRESSION_SUPPORTED
|
|
|
|
#endif
|
2021-01-26 15:56:49 +00:00
|
|
|
#endif
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
namespace glTF2 {
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
//
|
|
|
|
// JSON Value reading helpers
|
|
|
|
//
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
struct ReadHelper {
|
|
|
|
static bool Read(Value &val, T &out) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return val.IsInt() ? out = static_cast<T>(val.GetInt()), true : false;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
struct ReadHelper<bool> {
|
|
|
|
static bool Read(Value &val, bool &out) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return val.IsBool() ? out = val.GetBool(), true : false;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
struct ReadHelper<float> {
|
|
|
|
static bool Read(Value &val, float &out) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return val.IsNumber() ? out = static_cast<float>(val.GetDouble()), true : false;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <unsigned int N>
|
|
|
|
struct ReadHelper<float[N]> {
|
|
|
|
static bool Read(Value &val, float (&out)[N]) {
|
2017-07-19 20:21:43 +00:00
|
|
|
if (!val.IsArray() || val.Size() != N) return false;
|
|
|
|
for (unsigned int i = 0; i < N; ++i) {
|
|
|
|
if (val[i].IsNumber())
|
|
|
|
out[i] = static_cast<float>(val[i].GetDouble());
|
|
|
|
}
|
|
|
|
return true;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
struct ReadHelper<const char *> {
|
|
|
|
static bool Read(Value &val, const char *&out) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return val.IsString() ? (out = val.GetString(), true) : false;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
struct ReadHelper<std::string> {
|
|
|
|
static bool Read(Value &val, std::string &out) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return val.IsString() ? (out = std::string(val.GetString(), val.GetStringLength()), true) : false;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
struct ReadHelper<uint64_t> {
|
|
|
|
static bool Read(Value &val, uint64_t &out) {
|
2019-01-23 17:07:44 +00:00
|
|
|
return val.IsUint64() ? out = val.GetUint64(), true : false;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2019-01-23 17:07:44 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <>
|
|
|
|
struct ReadHelper<int64_t> {
|
|
|
|
static bool Read(Value &val, int64_t &out) {
|
2019-01-23 17:07:44 +00:00
|
|
|
return val.IsInt64() ? out = val.GetInt64(), true : false;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2019-01-23 17:07:44 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
struct ReadHelper<Nullable<T>> {
|
|
|
|
static bool Read(Value &val, Nullable<T> &out) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return out.isPresent = ReadHelper<T>::Read(val, out.value);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
};
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
inline static bool ReadValue(Value &val, T &out) {
|
|
|
|
return ReadHelper<T>::Read(val, out);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
inline static bool ReadMember(Value &obj, const char *id, T &out) {
|
|
|
|
Value::MemberIterator it = obj.FindMember(id);
|
|
|
|
if (it != obj.MemberEnd()) {
|
|
|
|
return ReadHelper<T>::Read(it->value, out);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
inline static T MemberOrDefault(Value &obj, const char *id, T defaultValue) {
|
|
|
|
T out;
|
|
|
|
return ReadMember(obj, id, out) ? out : defaultValue;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline Value *FindMember(Value &val, const char *id) {
|
|
|
|
Value::MemberIterator it = val.FindMember(id);
|
2021-01-27 09:42:49 +00:00
|
|
|
return (it != val.MemberEnd()) ? &it->value : nullptr;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
template<int N>
|
|
|
|
inline void throwUnexpectedTypeError(const char (&expectedTypeName)[N], const char* memberId, const char* context, const char* extraContext) {
|
|
|
|
std::string fullContext = context;
|
|
|
|
if (extraContext && (strlen(extraContext) > 0))
|
|
|
|
{
|
2021-03-15 16:46:41 +00:00
|
|
|
fullContext = fullContext + " (" + extraContext + ")";
|
2021-03-15 13:06:11 +00:00
|
|
|
}
|
|
|
|
throw DeadlyImportError("Member \"", memberId, "\" was not of type \"", expectedTypeName, "\" when reading ", fullContext);
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
// Look-up functions with type checks. Context and extra context help the user identify the problem if there's an error.
|
|
|
|
|
|
|
|
inline Value *FindStringInContext(Value &val, const char *memberId, const char* context, const char* extraContext = nullptr) {
|
|
|
|
Value::MemberIterator it = val.FindMember(memberId);
|
|
|
|
if (it == val.MemberEnd()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!it->value.IsString()) {
|
|
|
|
throwUnexpectedTypeError("string", memberId, context, extraContext);
|
|
|
|
}
|
|
|
|
return &it->value;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-08-24 21:31:33 +00:00
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline Value *FindNumberInContext(Value &val, const char *memberId, const char* context, const char* extraContext = nullptr) {
|
|
|
|
Value::MemberIterator it = val.FindMember(memberId);
|
|
|
|
if (it == val.MemberEnd()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!it->value.IsNumber()) {
|
|
|
|
throwUnexpectedTypeError("number", memberId, context, extraContext);
|
|
|
|
}
|
|
|
|
return &it->value;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline Value *FindUIntInContext(Value &val, const char *memberId, const char* context, const char* extraContext = nullptr) {
|
|
|
|
Value::MemberIterator it = val.FindMember(memberId);
|
|
|
|
if (it == val.MemberEnd()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!it->value.IsUint()) {
|
|
|
|
throwUnexpectedTypeError("uint", memberId, context, extraContext);
|
|
|
|
}
|
|
|
|
return &it->value;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline Value *FindArrayInContext(Value &val, const char *memberId, const char* context, const char* extraContext = nullptr) {
|
|
|
|
Value::MemberIterator it = val.FindMember(memberId);
|
|
|
|
if (it == val.MemberEnd()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!it->value.IsArray()) {
|
|
|
|
throwUnexpectedTypeError("array", memberId, context, extraContext);
|
|
|
|
}
|
|
|
|
return &it->value;
|
2021-01-27 09:42:49 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline Value *FindObjectInContext(Value &val, const char *memberId, const char* context, const char* extraContext = nullptr) {
|
|
|
|
Value::MemberIterator it = val.FindMember(memberId);
|
|
|
|
if (it == val.MemberEnd()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!it->value.IsObject()) {
|
|
|
|
throwUnexpectedTypeError("object", memberId, context, extraContext);
|
|
|
|
}
|
|
|
|
return &it->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *FindExtensionInContext(Value &val, const char *extensionId, const char* context, const char* extraContext = nullptr) {
|
|
|
|
if (Value *extensionList = FindObjectInContext(val, "extensions", context, extraContext)) {
|
2021-03-15 16:27:50 +00:00
|
|
|
if (Value *extension = FindObjectInContext(*extensionList, extensionId, context, extraContext)) {
|
2021-01-27 09:55:03 +00:00
|
|
|
return extension;
|
|
|
|
}
|
2021-01-27 09:42:49 +00:00
|
|
|
}
|
2021-01-27 09:55:03 +00:00
|
|
|
return nullptr;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2021-03-15 13:06:11 +00:00
|
|
|
|
|
|
|
// Overloads when the value is the document.
|
|
|
|
|
|
|
|
inline Value *FindString(Document &doc, const char *memberId) {
|
|
|
|
return FindStringInContext(doc, memberId, "the document");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *FindNumber(Document &doc, const char *memberId) {
|
|
|
|
return FindNumberInContext(doc, memberId, "the document");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *FindUInt(Document &doc, const char *memberId) {
|
|
|
|
return FindUIntInContext(doc, memberId, "the document");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *FindArray(Document &val, const char *memberId) {
|
|
|
|
return FindArrayInContext(val, memberId, "the document");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *FindObject(Document &doc, const char *memberId) {
|
|
|
|
return FindObjectInContext(doc, memberId, "the document");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *FindExtension(Value &val, const char *extensionId) {
|
|
|
|
return FindExtensionInContext(val, extensionId, "the document");
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} // namespace
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline Value *Object::FindString(Value &val, const char *memberId) {
|
|
|
|
return FindStringInContext(val, memberId, id.c_str(), name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *Object::FindNumber(Value &val, const char *memberId) {
|
|
|
|
return FindNumberInContext(val, memberId, id.c_str(), name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *Object::FindUInt(Value &val, const char *memberId) {
|
|
|
|
return FindUIntInContext(val, memberId, id.c_str(), name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *Object::FindArray(Value &val, const char *memberId) {
|
|
|
|
return FindArrayInContext(val, memberId, id.c_str(), name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *Object::FindObject(Value &val, const char *memberId) {
|
|
|
|
return FindObjectInContext(val, memberId, id.c_str(), name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline Value *Object::FindExtension(Value &val, const char *extensionId) {
|
|
|
|
return FindExtensionInContext(val, extensionId, id.c_str(), name.c_str());
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
#ifdef ASSIMP_ENABLE_DRACO
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline void CopyFaceIndex_Draco(Buffer &decodedIndexBuffer, const draco::Mesh &draco_mesh) {
|
|
|
|
const size_t faceStride = sizeof(T) * 3;
|
|
|
|
for (draco::FaceIndex f(0); f < draco_mesh.num_faces(); ++f) {
|
|
|
|
const draco::Mesh::Face &face = draco_mesh.face(f);
|
|
|
|
T indices[3] = { static_cast<T>(face[0].value()), static_cast<T>(face[1].value()), static_cast<T>(face[2].value()) };
|
|
|
|
memcpy(decodedIndexBuffer.GetPointer() + (f.value() * faceStride), &indices[0], faceStride);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetDecodedIndexBuffer_Draco(const draco::Mesh &dracoMesh, Mesh::Primitive &prim) {
|
|
|
|
if (!prim.indices || dracoMesh.num_faces() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create a decoded Index buffer (if there is one)
|
|
|
|
size_t componentBytes = prim.indices->GetBytesPerComponent();
|
|
|
|
|
2021-01-26 17:33:10 +00:00
|
|
|
std::unique_ptr<Buffer> decodedIndexBuffer(new Buffer());
|
2021-01-26 15:56:49 +00:00
|
|
|
decodedIndexBuffer->Grow(dracoMesh.num_faces() * 3 * componentBytes);
|
|
|
|
|
|
|
|
// If accessor uses the same size as draco implementation, copy the draco buffer directly
|
|
|
|
|
|
|
|
// Usually uint32_t but shouldn't assume
|
|
|
|
if (sizeof(dracoMesh.face(draco::FaceIndex(0))[0]) == componentBytes) {
|
|
|
|
memcpy(decodedIndexBuffer->GetPointer(), &dracoMesh.face(draco::FaceIndex(0))[0], decodedIndexBuffer->byteLength);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not same size, convert
|
|
|
|
switch (componentBytes) {
|
2021-02-18 08:32:37 +00:00
|
|
|
case sizeof(uint32_t):
|
|
|
|
CopyFaceIndex_Draco<uint32_t>(*decodedIndexBuffer, dracoMesh);
|
|
|
|
break;
|
|
|
|
case sizeof(uint16_t):
|
|
|
|
CopyFaceIndex_Draco<uint16_t>(*decodedIndexBuffer, dracoMesh);
|
|
|
|
break;
|
|
|
|
case sizeof(uint8_t):
|
|
|
|
CopyFaceIndex_Draco<uint8_t>(*decodedIndexBuffer, dracoMesh);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
break;
|
2021-01-26 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign this alternate data buffer to the accessor
|
|
|
|
prim.indices->decodedBuffer.swap(decodedIndexBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2021-01-27 09:42:49 +00:00
|
|
|
static bool GetAttributeForAllPoints_Draco(const draco::Mesh &dracoMesh,
|
2021-01-27 09:55:03 +00:00
|
|
|
const draco::PointAttribute &dracoAttribute,
|
|
|
|
Buffer &outBuffer) {
|
|
|
|
size_t byteOffset = 0;
|
|
|
|
T values[4] = { 0, 0, 0, 0 };
|
|
|
|
for (draco::PointIndex i(0); i < dracoMesh.num_points(); ++i) {
|
|
|
|
const draco::AttributeValueIndex val_index = dracoAttribute.mapped_index(i);
|
2021-02-18 08:32:37 +00:00
|
|
|
if (!dracoAttribute.ConvertValue<T>(val_index, dracoAttribute.num_components(), values)) {
|
2021-01-27 09:55:03 +00:00
|
|
|
return false;
|
2021-02-18 08:32:37 +00:00
|
|
|
}
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
memcpy(outBuffer.GetPointer() + byteOffset, &values[0], sizeof(T) * dracoAttribute.num_components());
|
|
|
|
byteOffset += sizeof(T) * dracoAttribute.num_components();
|
|
|
|
}
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
return true;
|
2021-01-26 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetDecodedAttributeBuffer_Draco(const draco::Mesh &dracoMesh, uint32_t dracoAttribId, Accessor &accessor) {
|
|
|
|
// Create decoded buffer
|
|
|
|
const draco::PointAttribute *pDracoAttribute = dracoMesh.GetAttributeByUniqueId(dracoAttribId);
|
2021-02-18 08:32:37 +00:00
|
|
|
if (pDracoAttribute == nullptr) {
|
2021-01-26 15:56:49 +00:00
|
|
|
throw DeadlyImportError("GLTF: Invalid draco attribute id: ", dracoAttribId);
|
2021-02-18 08:32:37 +00:00
|
|
|
}
|
2021-01-26 15:56:49 +00:00
|
|
|
|
|
|
|
size_t componentBytes = accessor.GetBytesPerComponent();
|
|
|
|
|
2021-01-26 17:33:10 +00:00
|
|
|
std::unique_ptr<Buffer> decodedAttribBuffer(new Buffer());
|
2021-01-26 15:56:49 +00:00
|
|
|
decodedAttribBuffer->Grow(dracoMesh.num_points() * pDracoAttribute->num_components() * componentBytes);
|
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
switch (accessor.componentType) {
|
2021-02-18 08:32:37 +00:00
|
|
|
case ComponentType_BYTE:
|
|
|
|
GetAttributeForAllPoints_Draco<int8_t>(dracoMesh, *pDracoAttribute, *decodedAttribBuffer);
|
|
|
|
break;
|
|
|
|
case ComponentType_UNSIGNED_BYTE:
|
|
|
|
GetAttributeForAllPoints_Draco<uint8_t>(dracoMesh, *pDracoAttribute, *decodedAttribBuffer);
|
|
|
|
break;
|
|
|
|
case ComponentType_SHORT:
|
|
|
|
GetAttributeForAllPoints_Draco<int16_t>(dracoMesh, *pDracoAttribute, *decodedAttribBuffer);
|
|
|
|
break;
|
|
|
|
case ComponentType_UNSIGNED_SHORT:
|
|
|
|
GetAttributeForAllPoints_Draco<uint16_t>(dracoMesh, *pDracoAttribute, *decodedAttribBuffer);
|
|
|
|
break;
|
|
|
|
case ComponentType_UNSIGNED_INT:
|
|
|
|
GetAttributeForAllPoints_Draco<uint32_t>(dracoMesh, *pDracoAttribute, *decodedAttribBuffer);
|
|
|
|
break;
|
|
|
|
case ComponentType_FLOAT:
|
|
|
|
GetAttributeForAllPoints_Draco<float>(dracoMesh, *pDracoAttribute, *decodedAttribBuffer);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ai_assert(false);
|
|
|
|
break;
|
2021-01-26 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assign this alternate data buffer to the accessor
|
|
|
|
accessor.decodedBuffer.swap(decodedAttribBuffer);
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:19:35 +00:00
|
|
|
#endif // ASSIMP_ENABLE_DRACO
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
//
|
|
|
|
// LazyDict methods
|
|
|
|
//
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
inline LazyDict<T>::LazyDict(Asset &asset, const char *dictId, const char *extId) :
|
2020-05-18 19:16:45 +00:00
|
|
|
mDictId(dictId),
|
|
|
|
mExtId(extId),
|
|
|
|
mDict(0),
|
|
|
|
mAsset(asset) {
|
2017-07-19 20:21:43 +00:00
|
|
|
asset.mDicts.push_back(this); // register to the list of dictionaries
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
inline LazyDict<T>::~LazyDict() {
|
2017-07-19 20:21:43 +00:00
|
|
|
for (size_t i = 0; i < mObjs.size(); ++i) {
|
|
|
|
delete mObjs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
inline void LazyDict<T>::AttachToDocument(Document &doc) {
|
2021-01-27 10:56:57 +00:00
|
|
|
Value *container = nullptr;
|
2021-03-15 13:06:11 +00:00
|
|
|
const char* context = nullptr;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (mExtId) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *exts = FindObject(doc, "extensions")) {
|
2021-03-15 13:06:11 +00:00
|
|
|
container = FindObjectInContext(*exts, mExtId, "extensions");
|
|
|
|
context = mExtId;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} else {
|
2017-07-19 20:21:43 +00:00
|
|
|
container = &doc;
|
2021-03-15 13:06:11 +00:00
|
|
|
context = "the document";
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (container) {
|
2021-03-15 13:06:11 +00:00
|
|
|
mDict = FindArrayInContext(*container, mDictId, context);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
inline void LazyDict<T>::DetachFromDocument() {
|
2021-01-27 10:56:57 +00:00
|
|
|
mDict = nullptr;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
unsigned int LazyDict<T>::Remove(const char *id) {
|
2017-11-07 20:13:01 +00:00
|
|
|
id = T::TranslateId(mAsset, id);
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
typename IdDict::iterator objIt = mObjsById.find(id);
|
2017-11-07 20:13:01 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (objIt == mObjsById.end()) {
|
2017-11-07 20:13:01 +00:00
|
|
|
throw DeadlyExportError("GLTF: Object with id \"" + std::string(id) + "\" is not found");
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const unsigned int index = objIt->second;
|
2017-11-07 20:13:01 +00:00
|
|
|
|
|
|
|
mAsset.mUsedIds[id] = false;
|
|
|
|
mObjsById.erase(id);
|
|
|
|
mObjsByOIndex.erase(index);
|
2021-03-17 22:12:54 +00:00
|
|
|
delete mObjs[index];
|
2017-11-07 20:13:01 +00:00
|
|
|
mObjs.erase(mObjs.begin() + index);
|
|
|
|
|
|
|
|
//update index of object in mObjs;
|
2017-11-25 10:38:12 +00:00
|
|
|
for (unsigned int i = index; i < mObjs.size(); ++i) {
|
2017-11-07 20:13:01 +00:00
|
|
|
T *obj = mObjs[i];
|
|
|
|
|
|
|
|
obj->index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (IdDict::iterator it = mObjsById.begin(); it != mObjsById.end(); ++it) {
|
|
|
|
if (it->second <= index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mObjsById[it->first] = it->second - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Dict::iterator it = mObjsByOIndex.begin(); it != mObjsByOIndex.end(); ++it) {
|
|
|
|
if (it->second <= index) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mObjsByOIndex[it->first] = it->second - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
Ref<T> LazyDict<T>::Retrieve(unsigned int i) {
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2017-08-25 20:52:44 +00:00
|
|
|
typename Dict::iterator it = mObjsByOIndex.find(i);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (it != mObjsByOIndex.end()) { // already created?
|
2017-07-19 20:21:43 +00:00
|
|
|
return Ref<T>(mObjs, it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read it from the JSON object
|
|
|
|
if (!mDict) {
|
2020-08-18 17:21:20 +00:00
|
|
|
throw DeadlyImportError("GLTF: Missing section \"", mDictId, "\"");
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 21:31:33 +00:00
|
|
|
if (!mDict->IsArray()) {
|
2020-11-30 15:04:06 +00:00
|
|
|
throw DeadlyImportError("GLTF: Field \"", mDictId, "\" is not an array");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= mDict->Size()) {
|
|
|
|
throw DeadlyImportError("GLTF: Array index ", i, " is out of bounds (", mDict->Size(), ") for \"", mDictId, "\"");
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2017-08-24 21:31:33 +00:00
|
|
|
|
2017-09-05 18:39:52 +00:00
|
|
|
Value &obj = (*mDict)[i];
|
2017-08-24 21:31:33 +00:00
|
|
|
|
|
|
|
if (!obj.IsObject()) {
|
2020-11-30 15:20:51 +00:00
|
|
|
throw DeadlyImportError("GLTF: Object at index ", i, " in array \"", mDictId, "\" is not a JSON object");
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 09:54:24 +00:00
|
|
|
if (mRecursiveReferenceCheck.find(i) != mRecursiveReferenceCheck.end()) {
|
2020-11-30 15:20:51 +00:00
|
|
|
throw DeadlyImportError("GLTF: Object at index ", i, " in array \"", mDictId, "\" has recursive reference to itself");
|
2020-03-11 09:54:24 +00:00
|
|
|
}
|
|
|
|
mRecursiveReferenceCheck.insert(i);
|
|
|
|
|
2019-11-19 17:05:24 +00:00
|
|
|
// Unique ptr prevents memory leak in case of Read throws an exception
|
2019-11-19 17:43:31 +00:00
|
|
|
auto inst = std::unique_ptr<T>(new T());
|
2020-11-05 15:02:41 +00:00
|
|
|
// Try to make this human readable so it can be used in error messages.
|
2021-03-09 20:08:28 +00:00
|
|
|
inst->id = std::string(mDictId) + "[" + ai_to_string(i) + "]";
|
2017-08-25 20:52:44 +00:00
|
|
|
inst->oIndex = i;
|
2017-08-24 21:31:33 +00:00
|
|
|
ReadMember(obj, "name", inst->name);
|
|
|
|
inst->Read(obj, mAsset);
|
|
|
|
|
2020-03-11 09:54:24 +00:00
|
|
|
Ref<T> result = Add(inst.release());
|
|
|
|
mRecursiveReferenceCheck.erase(i);
|
|
|
|
return result;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
Ref<T> LazyDict<T>::Get(unsigned int i) {
|
2017-09-01 21:49:04 +00:00
|
|
|
return Ref<T>(mObjs, i);
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
Ref<T> LazyDict<T>::Get(const char *id) {
|
2017-09-01 21:49:04 +00:00
|
|
|
id = T::TranslateId(mAsset, id);
|
|
|
|
|
|
|
|
typename IdDict::iterator it = mObjsById.find(id);
|
|
|
|
if (it != mObjsById.end()) { // already created?
|
|
|
|
return Ref<T>(mObjs, it->second);
|
|
|
|
}
|
|
|
|
|
2017-09-05 18:40:27 +00:00
|
|
|
return Ref<T>();
|
2017-09-01 21:49:04 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
Ref<T> LazyDict<T>::Add(T *obj) {
|
2017-07-19 20:21:43 +00:00
|
|
|
unsigned int idx = unsigned(mObjs.size());
|
|
|
|
mObjs.push_back(obj);
|
2017-08-25 20:52:44 +00:00
|
|
|
mObjsByOIndex[obj->oIndex] = idx;
|
2017-09-01 21:49:04 +00:00
|
|
|
mObjsById[obj->id] = idx;
|
2017-07-19 20:21:43 +00:00
|
|
|
mAsset.mUsedIds[obj->id] = true;
|
|
|
|
return Ref<T>(mObjs, idx);
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
Ref<T> LazyDict<T>::Create(const char *id) {
|
2017-07-19 20:21:43 +00:00
|
|
|
Asset::IdMap::iterator it = mAsset.mUsedIds.find(id);
|
|
|
|
if (it != mAsset.mUsedIds.end()) {
|
|
|
|
throw DeadlyImportError("GLTF: two objects with the same ID exist");
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
T *inst = new T();
|
2017-09-01 21:49:04 +00:00
|
|
|
unsigned int idx = unsigned(mObjs.size());
|
2017-07-19 20:21:43 +00:00
|
|
|
inst->id = id;
|
2017-09-01 21:49:04 +00:00
|
|
|
inst->index = idx;
|
|
|
|
inst->oIndex = idx;
|
2017-07-19 20:21:43 +00:00
|
|
|
return Add(inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// glTF dictionary objects methods
|
|
|
|
//
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline Buffer::Buffer() :
|
2020-05-18 19:16:45 +00:00
|
|
|
byteLength(0),
|
|
|
|
type(Type_arraybuffer),
|
|
|
|
EncodedRegion_Current(nullptr),
|
|
|
|
mIsSpecial(false) {}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline Buffer::~Buffer() {
|
|
|
|
for (SEncodedRegion *reg : EncodedRegion_List)
|
|
|
|
delete reg;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline const char *Buffer::TranslateId(Asset & /*r*/, const char *id) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Buffer::Read(Value &obj, Asset &r) {
|
2017-07-19 20:21:43 +00:00
|
|
|
size_t statedLength = MemberOrDefault<size_t>(obj, "byteLength", 0);
|
|
|
|
byteLength = statedLength;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *it = FindString(obj, "uri");
|
2017-07-19 20:21:43 +00:00
|
|
|
if (!it) {
|
|
|
|
if (statedLength > 0) {
|
|
|
|
throw DeadlyImportError("GLTF: buffer with non-zero length missing the \"uri\" attribute");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const char *uri = it->GetString();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2019-09-11 15:00:39 +00:00
|
|
|
glTFCommon::Util::DataURI dataURI;
|
2017-07-19 20:21:43 +00:00
|
|
|
if (ParseDataURI(uri, it->GetStringLength(), dataURI)) {
|
|
|
|
if (dataURI.base64) {
|
2021-01-27 10:56:57 +00:00
|
|
|
uint8_t *data = nullptr;
|
2019-09-11 15:00:39 +00:00
|
|
|
this->byteLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, data);
|
2017-10-01 06:51:13 +00:00
|
|
|
this->mData.reset(data, std::default_delete<uint8_t[]>());
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (statedLength > 0 && this->byteLength != statedLength) {
|
2021-03-09 20:08:28 +00:00
|
|
|
throw DeadlyImportError("GLTF: buffer \"", id, "\", expected ", ai_to_string(statedLength),
|
|
|
|
" bytes, but found ", ai_to_string(dataURI.dataLength));
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} else { // assume raw data
|
2017-07-19 20:21:43 +00:00
|
|
|
if (statedLength != dataURI.dataLength) {
|
2021-03-09 20:08:28 +00:00
|
|
|
throw DeadlyImportError("GLTF: buffer \"", id, "\", expected ", ai_to_string(statedLength),
|
|
|
|
" bytes, but found ", ai_to_string(dataURI.dataLength));
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 06:51:13 +00:00
|
|
|
this->mData.reset(new uint8_t[dataURI.dataLength], std::default_delete<uint8_t[]>());
|
2020-03-08 20:24:01 +00:00
|
|
|
memcpy(this->mData.get(), dataURI.data, dataURI.dataLength);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} else { // Local file
|
2017-07-19 20:21:43 +00:00
|
|
|
if (byteLength > 0) {
|
2021-01-27 09:55:03 +00:00
|
|
|
std::string dir = !r.mCurrentAssetDir.empty() ? (r.mCurrentAssetDir.back() == '/' ? r.mCurrentAssetDir : r.mCurrentAssetDir + '/') : "";
|
2017-09-07 18:55:44 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
IOStream *file = r.OpenFile(dir + uri, "rb");
|
2017-07-19 20:21:43 +00:00
|
|
|
if (file) {
|
|
|
|
bool ok = LoadFromStream(*file, byteLength);
|
|
|
|
delete file;
|
|
|
|
|
|
|
|
if (!ok)
|
2020-08-18 17:21:20 +00:00
|
|
|
throw DeadlyImportError("GLTF: error while reading referenced file \"", uri, "\"");
|
2020-03-08 20:24:01 +00:00
|
|
|
} else {
|
2020-08-18 17:21:20 +00:00
|
|
|
throw DeadlyImportError("GLTF: could not open referenced file \"", uri, "\"");
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline bool Buffer::LoadFromStream(IOStream &stream, size_t length, size_t baseOffset) {
|
2017-07-19 20:21:43 +00:00
|
|
|
byteLength = length ? length : stream.FileSize();
|
|
|
|
|
|
|
|
if (baseOffset) {
|
|
|
|
stream.Seek(baseOffset, aiOrigin_SET);
|
|
|
|
}
|
|
|
|
|
2017-09-14 07:34:16 +00:00
|
|
|
mData.reset(new uint8_t[byteLength], std::default_delete<uint8_t[]>());
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (stream.Read(mData.get(), byteLength, 1) != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Buffer::EncodedRegion_Mark(const size_t pOffset, const size_t pEncodedData_Length, uint8_t *pDecodedData, const size_t pDecodedData_Length, const std::string &pID) {
|
|
|
|
// Check pointer to data
|
|
|
|
if (pDecodedData == nullptr) throw DeadlyImportError("GLTF: for marking encoded region pointer to decoded data must be provided.");
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
// Check offset
|
|
|
|
if (pOffset > byteLength) {
|
|
|
|
const uint8_t val_size = 32;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
char val[val_size];
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-07-15 02:19:07 +00:00
|
|
|
ai_snprintf(val, val_size, AI_SIZEFMT, pOffset);
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("GLTF: incorrect offset value (", val, ") for marking encoded region.");
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
// Check length
|
|
|
|
if ((pOffset + pEncodedData_Length) > byteLength) {
|
|
|
|
const uint8_t val_size = 64;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
char val[val_size];
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-07-15 02:34:30 +00:00
|
|
|
ai_snprintf(val, val_size, AI_SIZEFMT "/" AI_SIZEFMT, pOffset, pEncodedData_Length);
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("GLTF: encoded region with offset/length (", val, ") is out of range.");
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
// Add new region
|
|
|
|
EncodedRegion_List.push_back(new SEncodedRegion(pOffset, pEncodedData_Length, pDecodedData, pDecodedData_Length, pID));
|
|
|
|
// And set new value for "byteLength"
|
|
|
|
byteLength += (pDecodedData_Length - pEncodedData_Length);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Buffer::EncodedRegion_SetCurrent(const std::string &pID) {
|
|
|
|
if ((EncodedRegion_Current != nullptr) && (EncodedRegion_Current->ID == pID)) return;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
for (SEncodedRegion *reg : EncodedRegion_List) {
|
|
|
|
if (reg->ID == pID) {
|
|
|
|
EncodedRegion_Current = reg;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("GLTF: EncodedRegion with ID: \"", pID, "\" not found.");
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline bool Buffer::ReplaceData(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t *pReplace_Data, const size_t pReplace_Count) {
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if ((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const size_t new_data_size = byteLength + pReplace_Count - pBufferData_Count;
|
|
|
|
uint8_t *new_data = new uint8_t[new_data_size];
|
|
|
|
// Copy data which place before replacing part.
|
|
|
|
::memcpy(new_data, mData.get(), pBufferData_Offset);
|
|
|
|
// Copy new data.
|
|
|
|
::memcpy(&new_data[pBufferData_Offset], pReplace_Data, pReplace_Count);
|
|
|
|
// Copy data which place after replacing part.
|
|
|
|
::memcpy(&new_data[pBufferData_Offset + pReplace_Count], &mData.get()[pBufferData_Offset + pBufferData_Count], pBufferData_Offset);
|
|
|
|
// Apply new data
|
|
|
|
mData.reset(new_data, std::default_delete<uint8_t[]>());
|
|
|
|
byteLength = new_data_size;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
return true;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2018-10-26 22:36:34 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline bool Buffer::ReplaceData_joint(const size_t pBufferData_Offset, const size_t pBufferData_Count, const uint8_t *pReplace_Data, const size_t pReplace_Count) {
|
|
|
|
if ((pBufferData_Count == 0) || (pReplace_Count == 0) || (pReplace_Data == nullptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-04-26 02:56:56 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const size_t new_data_size = byteLength + pReplace_Count - pBufferData_Count;
|
|
|
|
uint8_t *new_data = new uint8_t[new_data_size];
|
|
|
|
// Copy data which place before replacing part.
|
|
|
|
memcpy(new_data, mData.get(), pBufferData_Offset);
|
|
|
|
// Copy new data.
|
|
|
|
memcpy(&new_data[pBufferData_Offset], pReplace_Data, pReplace_Count);
|
|
|
|
// Copy data which place after replacing part.
|
|
|
|
memcpy(&new_data[pBufferData_Offset + pReplace_Count], &mData.get()[pBufferData_Offset + pBufferData_Count], new_data_size - (pBufferData_Offset + pReplace_Count));
|
|
|
|
// Apply new data
|
|
|
|
mData.reset(new_data, std::default_delete<uint8_t[]>());
|
|
|
|
byteLength = new_data_size;
|
2018-04-26 02:56:56 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
return true;
|
2018-04-26 02:56:56 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline size_t Buffer::AppendData(uint8_t *data, size_t length) {
|
2017-07-19 20:21:43 +00:00
|
|
|
size_t offset = this->byteLength;
|
2018-01-16 08:26:00 +00:00
|
|
|
// Force alignment to 4 bits
|
|
|
|
Grow((length + 3) & ~3);
|
2017-07-19 20:21:43 +00:00
|
|
|
memcpy(mData.get() + offset, data, length);
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Buffer::Grow(size_t amount) {
|
2020-04-01 09:16:39 +00:00
|
|
|
if (amount <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-18 19:16:45 +00:00
|
|
|
|
2020-04-01 09:16:39 +00:00
|
|
|
// Capacity is big enough
|
2020-03-08 20:24:01 +00:00
|
|
|
if (capacity >= byteLength + amount) {
|
2019-01-23 17:07:44 +00:00
|
|
|
byteLength += amount;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-01 09:16:39 +00:00
|
|
|
// Just allocate data which we need
|
|
|
|
capacity = byteLength + amount;
|
2019-01-23 17:07:44 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
uint8_t *b = new uint8_t[capacity];
|
2020-04-01 09:16:39 +00:00
|
|
|
if (nullptr != mData) {
|
|
|
|
memcpy(b, mData.get(), byteLength);
|
|
|
|
}
|
2017-09-14 07:34:16 +00:00
|
|
|
mData.reset(b, std::default_delete<uint8_t[]>());
|
2017-07-19 20:21:43 +00:00
|
|
|
byteLength += amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// struct BufferView
|
|
|
|
//
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void BufferView::Read(Value &obj, Asset &r) {
|
2017-08-24 21:31:33 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *bufferVal = FindUInt(obj, "buffer")) {
|
2017-08-25 20:09:07 +00:00
|
|
|
buffer = r.buffers.Retrieve(bufferVal->GetUint());
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 08:45:47 +00:00
|
|
|
if (!buffer) {
|
|
|
|
throw DeadlyImportError("GLTF: Buffer view without valid buffer.");
|
|
|
|
}
|
|
|
|
|
2019-01-23 17:07:44 +00:00
|
|
|
byteOffset = MemberOrDefault(obj, "byteOffset", size_t(0));
|
|
|
|
byteLength = MemberOrDefault(obj, "byteLength", size_t(0));
|
2017-11-20 18:01:28 +00:00
|
|
|
byteStride = MemberOrDefault(obj, "byteStride", 0u);
|
2020-10-23 11:01:43 +00:00
|
|
|
|
|
|
|
// Check length
|
|
|
|
if ((byteOffset + byteLength) > buffer->byteLength) {
|
2021-01-11 08:45:47 +00:00
|
|
|
throw DeadlyImportError("GLTF: Buffer view with offset/length (", byteOffset, "/", byteLength, ") is out of range.");
|
2020-10-23 11:01:43 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 19:20:31 +00:00
|
|
|
inline uint8_t *BufferView::GetPointer(size_t accOffset) {
|
2021-01-27 10:56:57 +00:00
|
|
|
if (!buffer) return nullptr;
|
2020-05-15 19:20:31 +00:00
|
|
|
uint8_t *basePtr = buffer->GetPointer();
|
2021-01-27 10:56:57 +00:00
|
|
|
if (!basePtr) return nullptr;
|
2020-05-15 19:20:31 +00:00
|
|
|
|
|
|
|
size_t offset = accOffset + byteOffset;
|
|
|
|
if (buffer->EncodedRegion_Current != nullptr) {
|
|
|
|
const size_t begin = buffer->EncodedRegion_Current->Offset;
|
|
|
|
const size_t end = begin + buffer->EncodedRegion_Current->DecodedData_Length;
|
|
|
|
if ((offset >= begin) && (offset < end))
|
|
|
|
return &buffer->EncodedRegion_Current->DecodedData[offset - begin];
|
|
|
|
}
|
|
|
|
|
|
|
|
return basePtr + offset;
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
//
|
|
|
|
// struct Accessor
|
|
|
|
//
|
2020-05-15 19:20:31 +00:00
|
|
|
inline void Accessor::Sparse::PopulateData(size_t numBytes, uint8_t *bytes) {
|
|
|
|
if (bytes) {
|
|
|
|
data.assign(bytes, bytes + numBytes);
|
|
|
|
} else {
|
|
|
|
data.resize(numBytes, 0x00);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Accessor::Sparse::PatchData(unsigned int elementSize) {
|
|
|
|
uint8_t *pIndices = indices->GetPointer(indicesByteOffset);
|
|
|
|
const unsigned int indexSize = int(ComponentTypeSize(indicesType));
|
|
|
|
uint8_t *indicesEnd = pIndices + count * indexSize;
|
|
|
|
|
|
|
|
uint8_t *pValues = values->GetPointer(valuesByteOffset);
|
|
|
|
while (pIndices != indicesEnd) {
|
|
|
|
size_t offset;
|
|
|
|
switch (indicesType) {
|
|
|
|
case ComponentType_UNSIGNED_BYTE:
|
|
|
|
offset = *pIndices;
|
|
|
|
break;
|
|
|
|
case ComponentType_UNSIGNED_SHORT:
|
|
|
|
offset = *reinterpret_cast<uint16_t *>(pIndices);
|
|
|
|
break;
|
|
|
|
case ComponentType_UNSIGNED_INT:
|
|
|
|
offset = *reinterpret_cast<uint32_t *>(pIndices);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// have fun with float and negative values from signed types as indices.
|
|
|
|
throw DeadlyImportError("Unsupported component type in index.");
|
|
|
|
}
|
|
|
|
|
|
|
|
offset *= elementSize;
|
|
|
|
std::memcpy(data.data() + offset, pValues, elementSize);
|
|
|
|
|
|
|
|
pValues += elementSize;
|
|
|
|
pIndices += indexSize;
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 19:32:58 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Accessor::Read(Value &obj, Asset &r) {
|
2017-08-24 21:31:33 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *bufferViewVal = FindUInt(obj, "bufferView")) {
|
2017-08-25 20:09:07 +00:00
|
|
|
bufferView = r.bufferViews.Retrieve(bufferViewVal->GetUint());
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:07:44 +00:00
|
|
|
byteOffset = MemberOrDefault(obj, "byteOffset", size_t(0));
|
2017-07-19 20:21:43 +00:00
|
|
|
componentType = MemberOrDefault(obj, "componentType", ComponentType_BYTE);
|
2021-03-16 11:47:19 +00:00
|
|
|
{
|
|
|
|
const Value* countValue = FindUInt(obj, "count");
|
|
|
|
if (!countValue || countValue->GetInt() < 1)
|
|
|
|
{
|
|
|
|
throw DeadlyImportError("A strictly positive count value is required, when reading ", id.c_str(), name.empty() ? "" : " (" + name + ")");
|
|
|
|
}
|
|
|
|
count = countValue->GetUint();
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const char *typestr;
|
2017-07-19 20:21:43 +00:00
|
|
|
type = ReadMember(obj, "type", typestr) ? AttribType::FromString(typestr) : AttribType::SCALAR;
|
2020-05-15 19:20:31 +00:00
|
|
|
|
2020-11-06 09:57:48 +00:00
|
|
|
if (bufferView) {
|
2020-11-05 15:02:41 +00:00
|
|
|
// Check length
|
|
|
|
unsigned long long byteLength = (unsigned long long)GetBytesPerComponent() * (unsigned long long)count;
|
2020-10-23 11:01:43 +00:00
|
|
|
|
2021-01-11 08:45:47 +00:00
|
|
|
// handle integer overflow
|
|
|
|
if (byteLength < count) {
|
|
|
|
throw DeadlyImportError("GLTF: Accessor with offset/count (", byteOffset, "/", count, ") is out of range.");
|
|
|
|
}
|
2020-10-23 11:01:43 +00:00
|
|
|
|
2021-01-11 08:45:47 +00:00
|
|
|
if ((byteOffset + byteLength) > bufferView->byteLength || (bufferView->byteOffset + byteOffset + byteLength) > bufferView->buffer->byteLength) {
|
|
|
|
throw DeadlyImportError("GLTF: Accessor with offset/length (", byteOffset, "/", byteLength, ") is out of range.");
|
2020-11-05 15:02:41 +00:00
|
|
|
}
|
2020-10-23 11:01:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 19:20:31 +00:00
|
|
|
if (Value *sparseValue = FindObject(obj, "sparse")) {
|
|
|
|
sparse.reset(new Sparse);
|
|
|
|
// count
|
|
|
|
ReadMember(*sparseValue, "count", sparse->count);
|
|
|
|
|
|
|
|
// indices
|
|
|
|
if (Value *indicesValue = FindObject(*sparseValue, "indices")) {
|
|
|
|
//indices bufferView
|
|
|
|
Value *indiceViewID = FindUInt(*indicesValue, "bufferView");
|
|
|
|
sparse->indices = r.bufferViews.Retrieve(indiceViewID->GetUint());
|
|
|
|
//indices byteOffset
|
|
|
|
sparse->indicesByteOffset = MemberOrDefault(*indicesValue, "byteOffset", size_t(0));
|
|
|
|
//indices componentType
|
|
|
|
sparse->indicesType = MemberOrDefault(*indicesValue, "componentType", ComponentType_BYTE);
|
|
|
|
//sparse->indices->Read(*indicesValue, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// value
|
|
|
|
if (Value *valuesValue = FindObject(*sparseValue, "values")) {
|
|
|
|
//value bufferView
|
|
|
|
Value *valueViewID = FindUInt(*valuesValue, "bufferView");
|
|
|
|
sparse->values = r.bufferViews.Retrieve(valueViewID->GetUint());
|
|
|
|
//value byteOffset
|
|
|
|
sparse->valuesByteOffset = MemberOrDefault(*valuesValue, "byteOffset", size_t(0));
|
|
|
|
//sparse->values->Read(*valuesValue, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
// indicesType
|
|
|
|
sparse->indicesType = MemberOrDefault(*sparseValue, "componentType", ComponentType_UNSIGNED_SHORT);
|
|
|
|
|
|
|
|
const unsigned int elementSize = GetElementSize();
|
|
|
|
const size_t dataSize = count * elementSize;
|
|
|
|
sparse->PopulateData(dataSize, bufferView ? bufferView->GetPointer(byteOffset) : 0);
|
|
|
|
sparse->PatchData(elementSize);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline unsigned int Accessor::GetNumComponents() {
|
2017-07-19 20:21:43 +00:00
|
|
|
return AttribType::GetNumComponents(type);
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline unsigned int Accessor::GetBytesPerComponent() {
|
2017-07-19 20:21:43 +00:00
|
|
|
return int(ComponentTypeSize(componentType));
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline unsigned int Accessor::GetElementSize() {
|
2017-07-19 20:21:43 +00:00
|
|
|
return GetNumComponents() * GetBytesPerComponent();
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline uint8_t *Accessor::GetPointer() {
|
2021-01-26 15:56:49 +00:00
|
|
|
if (decodedBuffer)
|
|
|
|
return decodedBuffer->GetPointer();
|
|
|
|
|
2020-05-15 19:20:31 +00:00
|
|
|
if (sparse)
|
|
|
|
return sparse->data.data();
|
|
|
|
|
2021-01-27 10:56:57 +00:00
|
|
|
if (!bufferView || !bufferView->buffer) return nullptr;
|
2020-03-08 20:24:01 +00:00
|
|
|
uint8_t *basePtr = bufferView->buffer->GetPointer();
|
2021-01-27 10:56:57 +00:00
|
|
|
if (!basePtr) return nullptr;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
size_t offset = byteOffset + bufferView->byteOffset;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
// Check if region is encoded.
|
|
|
|
if (bufferView->buffer->EncodedRegion_Current != nullptr) {
|
|
|
|
const size_t begin = bufferView->buffer->EncodedRegion_Current->Offset;
|
|
|
|
const size_t end = begin + bufferView->buffer->EncodedRegion_Current->DecodedData_Length;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if ((offset >= begin) && (offset < end))
|
|
|
|
return &bufferView->buffer->EncodedRegion_Current->DecodedData[offset - begin];
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
return basePtr + offset;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
inline size_t Accessor::GetStride() {
|
|
|
|
// Decoded buffer is always packed
|
|
|
|
if (decodedBuffer)
|
|
|
|
return GetElementSize();
|
|
|
|
|
|
|
|
// Sparse and normal bufferView
|
|
|
|
return (bufferView && bufferView->byteStride ? bufferView->byteStride : GetElementSize());
|
2021-01-26 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
inline size_t Accessor::GetMaxByteSize() {
|
|
|
|
if (decodedBuffer)
|
|
|
|
return decodedBuffer->byteLength;
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
return (bufferView ? bufferView->byteLength : sparse->data.size());
|
2021-01-26 15:56:49 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
namespace {
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void CopyData(size_t count,
|
|
|
|
const uint8_t *src, size_t src_stride,
|
|
|
|
uint8_t *dst, size_t dst_stride) {
|
|
|
|
if (src_stride == dst_stride) {
|
|
|
|
memcpy(dst, src, count * src_stride);
|
|
|
|
} else {
|
|
|
|
size_t sz = std::min(src_stride, dst_stride);
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
memcpy(dst, src, sz);
|
|
|
|
if (sz < dst_stride) {
|
|
|
|
memset(dst + sz, 0, dst_stride - sz);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
src += src_stride;
|
|
|
|
dst += dst_stride;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-05 15:02:41 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
} // namespace
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-05-18 19:16:45 +00:00
|
|
|
template <class T>
|
|
|
|
void Accessor::ExtractData(T *&outData) {
|
|
|
|
uint8_t *data = GetPointer();
|
2020-03-23 08:03:29 +00:00
|
|
|
if (!data) {
|
2020-11-05 15:10:52 +00:00
|
|
|
throw DeadlyImportError("GLTF2: data is null when extracting data from ", getContextForErrorMessages(id, name));
|
2020-03-23 08:03:29 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
const size_t elemSize = GetElementSize();
|
|
|
|
const size_t totalSize = elemSize * count;
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
const size_t stride = GetStride();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
const size_t targetElemSize = sizeof(T);
|
2020-09-10 09:47:58 +00:00
|
|
|
|
|
|
|
if (elemSize > targetElemSize) {
|
2020-11-30 15:04:06 +00:00
|
|
|
throw DeadlyImportError("GLTF: elemSize ", elemSize, " > targetElemSize ", targetElemSize, " in ", getContextForErrorMessages(id, name));
|
2020-09-10 09:47:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
const size_t maxSize = GetMaxByteSize();
|
2021-01-27 09:55:03 +00:00
|
|
|
if (count * stride > maxSize) {
|
2020-11-30 15:20:51 +00:00
|
|
|
throw DeadlyImportError("GLTF: count*stride ", (count * stride), " > maxSize ", maxSize, " in ", getContextForErrorMessages(id, name));
|
2020-09-10 09:47:58 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
outData = new T[count];
|
|
|
|
if (stride == elemSize && targetElemSize == elemSize) {
|
|
|
|
memcpy(outData, data, totalSize);
|
2020-03-08 20:24:01 +00:00
|
|
|
} else {
|
2017-07-19 20:21:43 +00:00
|
|
|
for (size_t i = 0; i < count; ++i) {
|
2020-03-08 20:24:01 +00:00
|
|
|
memcpy(outData + i, data + i * stride, elemSize);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Accessor::WriteData(size_t _count, const void *src_buffer, size_t src_stride) {
|
|
|
|
uint8_t *buffer_ptr = bufferView->buffer->GetPointer();
|
2017-07-19 20:21:43 +00:00
|
|
|
size_t offset = byteOffset + bufferView->byteOffset;
|
|
|
|
|
|
|
|
size_t dst_stride = GetNumComponents() * GetBytesPerComponent();
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const uint8_t *src = reinterpret_cast<const uint8_t *>(src_buffer);
|
|
|
|
uint8_t *dst = reinterpret_cast<uint8_t *>(buffer_ptr + offset);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
ai_assert(dst + _count * dst_stride <= buffer_ptr + bufferView->buffer->byteLength);
|
|
|
|
CopyData(_count, src, src_stride, dst, dst_stride);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 19:20:31 +00:00
|
|
|
inline void Accessor::WriteSparseValues(size_t _count, const void *src_data, size_t src_dataStride) {
|
|
|
|
if (!sparse)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// values
|
|
|
|
uint8_t *value_buffer_ptr = sparse->values->buffer->GetPointer();
|
|
|
|
size_t value_offset = sparse->valuesByteOffset + sparse->values->byteOffset;
|
|
|
|
size_t value_dst_stride = GetNumComponents() * GetBytesPerComponent();
|
|
|
|
const uint8_t *value_src = reinterpret_cast<const uint8_t *>(src_data);
|
|
|
|
uint8_t *value_dst = reinterpret_cast<uint8_t *>(value_buffer_ptr + value_offset);
|
|
|
|
ai_assert(value_dst + _count * value_dst_stride <= value_buffer_ptr + sparse->values->buffer->byteLength);
|
|
|
|
CopyData(_count, value_src, src_dataStride, value_dst, value_dst_stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Accessor::WriteSparseIndices(size_t _count, const void *src_idx, size_t src_idxStride) {
|
|
|
|
if (!sparse)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// indices
|
|
|
|
uint8_t *indices_buffer_ptr = sparse->indices->buffer->GetPointer();
|
|
|
|
size_t indices_offset = sparse->indicesByteOffset + sparse->indices->byteOffset;
|
|
|
|
size_t indices_dst_stride = 1 * sizeof(unsigned short);
|
|
|
|
const uint8_t *indices_src = reinterpret_cast<const uint8_t *>(src_idx);
|
|
|
|
uint8_t *indices_dst = reinterpret_cast<uint8_t *>(indices_buffer_ptr + indices_offset);
|
|
|
|
ai_assert(indices_dst + _count * indices_dst_stride <= indices_buffer_ptr + sparse->indices->buffer->byteLength);
|
|
|
|
CopyData(_count, indices_src, src_idxStride, indices_dst, indices_dst_stride);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
inline Accessor::Indexer::Indexer(Accessor &acc) :
|
2020-05-18 19:16:45 +00:00
|
|
|
accessor(acc),
|
|
|
|
data(acc.GetPointer()),
|
|
|
|
elemSize(acc.GetElementSize()),
|
2021-01-26 15:56:49 +00:00
|
|
|
stride(acc.GetStride()) {
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Accesses the i-th value as defined by the accessor
|
2020-03-08 20:24:01 +00:00
|
|
|
template <class T>
|
|
|
|
T Accessor::Indexer::GetValue(int i) {
|
2017-07-19 20:21:43 +00:00
|
|
|
ai_assert(data);
|
2021-01-26 15:56:49 +00:00
|
|
|
ai_assert(i * stride < accessor.GetMaxByteSize());
|
2020-11-06 13:43:16 +00:00
|
|
|
// Ensure that the memcpy doesn't overwrite the local.
|
|
|
|
const size_t sizeToCopy = std::min(elemSize, sizeof(T));
|
2017-07-19 20:21:43 +00:00
|
|
|
T value = T();
|
2020-11-06 13:43:16 +00:00
|
|
|
// Assume platform endianness matches GLTF binary data (which is little-endian).
|
|
|
|
memcpy(&value, data + i * stride, sizeToCopy);
|
2017-07-19 20:21:43 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline Image::Image() :
|
2020-05-18 19:16:45 +00:00
|
|
|
width(0),
|
|
|
|
height(0),
|
|
|
|
mDataLength(0) {
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Image::Read(Value &obj, Asset &r) {
|
2017-07-19 20:21:43 +00:00
|
|
|
if (!mDataLength) {
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curUri = FindString(obj, "uri");
|
|
|
|
if (nullptr != curUri) {
|
|
|
|
const char *uristr = curUri->GetString();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2019-09-11 15:00:39 +00:00
|
|
|
glTFCommon::Util::DataURI dataURI;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (ParseDataURI(uristr, curUri->GetStringLength(), dataURI)) {
|
2017-07-19 20:21:43 +00:00
|
|
|
mimeType = dataURI.mediaType;
|
|
|
|
if (dataURI.base64) {
|
2018-09-21 19:24:53 +00:00
|
|
|
uint8_t *ptr = nullptr;
|
2019-09-11 15:00:39 +00:00
|
|
|
mDataLength = glTFCommon::Util::DecodeBase64(dataURI.data, dataURI.dataLength, ptr);
|
2018-09-21 19:24:53 +00:00
|
|
|
mData.reset(ptr);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} else {
|
2017-07-19 20:21:43 +00:00
|
|
|
this->uri = uristr;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} else if (Value *bufferViewVal = FindUInt(obj, "bufferView")) {
|
2018-02-24 16:44:40 +00:00
|
|
|
this->bufferView = r.bufferViews.Retrieve(bufferViewVal->GetUint());
|
2020-11-06 13:43:16 +00:00
|
|
|
if (Value *mtype = FindString(obj, "mimeType")) {
|
|
|
|
this->mimeType = mtype->GetString();
|
|
|
|
}
|
2021-01-27 09:55:03 +00:00
|
|
|
if (!this->bufferView || this->mimeType.empty()) {
|
2020-11-06 13:43:16 +00:00
|
|
|
throw DeadlyImportError("GLTF2: ", getContextForErrorMessages(id, name), " does not have a URI, so it must have a valid bufferView and mimetype");
|
|
|
|
}
|
|
|
|
|
2018-02-24 16:44:40 +00:00
|
|
|
Ref<Buffer> buffer = this->bufferView->buffer;
|
|
|
|
|
|
|
|
this->mDataLength = this->bufferView->byteLength;
|
2018-02-24 17:47:43 +00:00
|
|
|
// maybe this memcpy could be avoided if aiTexture does not delete[] pcData at destruction.
|
2018-10-26 22:36:34 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
this->mData.reset(new uint8_t[this->mDataLength]);
|
|
|
|
memcpy(this->mData.get(), buffer->GetPointer() + this->bufferView->byteOffset, this->mDataLength);
|
2021-01-27 09:55:03 +00:00
|
|
|
} else {
|
|
|
|
throw DeadlyImportError("GLTF2: ", getContextForErrorMessages(id, name), " should have either a URI of a bufferView and mimetype");
|
2018-02-24 16:44:40 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline uint8_t *Image::StealData() {
|
|
|
|
mDataLength = 0;
|
|
|
|
return mData.release();
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 22:49:00 +00:00
|
|
|
// Never take over the ownership of data whenever binary or not
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Image::SetData(uint8_t *data, size_t length, Asset &r) {
|
2017-07-19 20:21:43 +00:00
|
|
|
Ref<Buffer> b = r.GetBodyBuffer();
|
|
|
|
if (b) { // binary file: append to body
|
|
|
|
std::string bvId = r.FindUniqueID(this->id, "imgdata");
|
|
|
|
bufferView = r.bufferViews.Create(bvId);
|
|
|
|
|
|
|
|
bufferView->buffer = b;
|
|
|
|
bufferView->byteLength = length;
|
|
|
|
bufferView->byteOffset = b->AppendData(data, length);
|
2020-03-08 20:24:01 +00:00
|
|
|
} else { // text file: will be stored as a data uri
|
2020-01-15 22:49:00 +00:00
|
|
|
uint8_t *temp = new uint8_t[length];
|
|
|
|
memcpy(temp, data, length);
|
|
|
|
this->mData.reset(temp);
|
|
|
|
this->mDataLength = length;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Sampler::Read(Value &obj, Asset & /*r*/) {
|
2017-07-19 20:21:43 +00:00
|
|
|
SetDefaults();
|
|
|
|
|
2017-09-04 02:09:48 +00:00
|
|
|
ReadMember(obj, "name", name);
|
2017-07-19 20:21:43 +00:00
|
|
|
ReadMember(obj, "magFilter", magFilter);
|
|
|
|
ReadMember(obj, "minFilter", minFilter);
|
|
|
|
ReadMember(obj, "wrapS", wrapS);
|
|
|
|
ReadMember(obj, "wrapT", wrapT);
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Sampler::SetDefaults() {
|
2017-09-04 02:09:48 +00:00
|
|
|
//only wrapping modes have defaults
|
|
|
|
wrapS = SamplerWrap::Repeat;
|
|
|
|
wrapT = SamplerWrap::Repeat;
|
|
|
|
magFilter = SamplerMagFilter::UNSET;
|
|
|
|
minFilter = SamplerMinFilter::UNSET;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Texture::Read(Value &obj, Asset &r) {
|
|
|
|
if (Value *sourceVal = FindUInt(obj, "source")) {
|
2017-08-25 20:09:07 +00:00
|
|
|
source = r.images.Retrieve(sourceVal->GetUint());
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *samplerVal = FindUInt(obj, "sampler")) {
|
2017-08-25 20:09:07 +00:00
|
|
|
sampler = r.samplers.Retrieve(samplerVal->GetUint());
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
void Material::SetTextureProperties(Asset &r, Value *prop, TextureInfo &out) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (r.extensionsUsed.KHR_texture_transform) {
|
2021-01-27 09:42:49 +00:00
|
|
|
if (Value *pKHR_texture_transform = FindExtension(*prop, "KHR_texture_transform")) {
|
2020-03-08 20:24:01 +00:00
|
|
|
out.textureTransformSupported = true;
|
2021-01-27 09:42:49 +00:00
|
|
|
if (Value *array = FindArray(*pKHR_texture_transform, "offset")) {
|
|
|
|
out.TextureTransformExt_t.offset[0] = (*array)[0].GetFloat();
|
|
|
|
out.TextureTransformExt_t.offset[1] = (*array)[1].GetFloat();
|
|
|
|
} else {
|
|
|
|
out.TextureTransformExt_t.offset[0] = 0;
|
|
|
|
out.TextureTransformExt_t.offset[1] = 0;
|
|
|
|
}
|
2019-11-14 20:11:53 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
if (!ReadMember(*pKHR_texture_transform, "rotation", out.TextureTransformExt_t.rotation)) {
|
|
|
|
out.TextureTransformExt_t.rotation = 0;
|
|
|
|
}
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
if (Value *array = FindArray(*pKHR_texture_transform, "scale")) {
|
|
|
|
out.TextureTransformExt_t.scale[0] = (*array)[0].GetFloat();
|
|
|
|
out.TextureTransformExt_t.scale[1] = (*array)[1].GetFloat();
|
|
|
|
} else {
|
|
|
|
out.TextureTransformExt_t.scale[0] = 1;
|
|
|
|
out.TextureTransformExt_t.scale[1] = 1;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
if (Value *indexProp = FindUInt(*prop, "index")) {
|
|
|
|
out.texture = r.textures.Retrieve(indexProp->GetUint());
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *texcoord = FindUInt(*prop, "texCoord")) {
|
|
|
|
out.texCoord = texcoord->GetUint();
|
|
|
|
}
|
|
|
|
}
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline void Material::ReadTextureProperty(Asset &r, Value &vals, const char *propName, TextureInfo &out) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *prop = FindMember(vals, propName)) {
|
|
|
|
SetTextureProperties(r, prop, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline void Material::ReadTextureProperty(Asset &r, Value &vals, const char *propName, NormalTextureInfo &out) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *prop = FindMember(vals, propName)) {
|
|
|
|
SetTextureProperties(r, prop, out);
|
|
|
|
|
|
|
|
if (Value *scale = FindNumber(*prop, "scale")) {
|
|
|
|
out.scale = static_cast<float>(scale->GetDouble());
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
inline void Material::ReadTextureProperty(Asset &r, Value &vals, const char *propName, OcclusionTextureInfo &out) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *prop = FindMember(vals, propName)) {
|
|
|
|
SetTextureProperties(r, prop, out);
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *strength = FindNumber(*prop, "strength")) {
|
|
|
|
out.strength = static_cast<float>(strength->GetDouble());
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Material::Read(Value &material, Asset &r) {
|
2017-07-19 20:21:43 +00:00
|
|
|
SetDefaults();
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *curPbrMetallicRoughness = FindObject(material, "pbrMetallicRoughness")) {
|
|
|
|
ReadMember(*curPbrMetallicRoughness, "baseColorFactor", this->pbrMetallicRoughness.baseColorFactor);
|
|
|
|
ReadTextureProperty(r, *curPbrMetallicRoughness, "baseColorTexture", this->pbrMetallicRoughness.baseColorTexture);
|
|
|
|
ReadTextureProperty(r, *curPbrMetallicRoughness, "metallicRoughnessTexture", this->pbrMetallicRoughness.metallicRoughnessTexture);
|
|
|
|
ReadMember(*curPbrMetallicRoughness, "metallicFactor", this->pbrMetallicRoughness.metallicFactor);
|
|
|
|
ReadMember(*curPbrMetallicRoughness, "roughnessFactor", this->pbrMetallicRoughness.roughnessFactor);
|
2017-08-25 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
ReadTextureProperty(r, material, "normalTexture", this->normalTexture);
|
|
|
|
ReadTextureProperty(r, material, "occlusionTexture", this->occlusionTexture);
|
|
|
|
ReadTextureProperty(r, material, "emissiveTexture", this->emissiveTexture);
|
|
|
|
ReadMember(material, "emissiveFactor", this->emissiveFactor);
|
2017-08-28 03:47:54 +00:00
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
ReadMember(material, "doubleSided", this->doubleSided);
|
|
|
|
ReadMember(material, "alphaMode", this->alphaMode);
|
|
|
|
ReadMember(material, "alphaCutoff", this->alphaCutoff);
|
2017-08-25 21:58:41 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *extensions = FindObject(material, "extensions")) {
|
2017-08-31 05:35:10 +00:00
|
|
|
if (r.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *curPbrSpecularGlossiness = FindObject(*extensions, "KHR_materials_pbrSpecularGlossiness")) {
|
2017-09-05 20:29:00 +00:00
|
|
|
PbrSpecularGlossiness pbrSG;
|
2017-08-31 22:30:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
ReadMember(*curPbrSpecularGlossiness, "diffuseFactor", pbrSG.diffuseFactor);
|
|
|
|
ReadTextureProperty(r, *curPbrSpecularGlossiness, "diffuseTexture", pbrSG.diffuseTexture);
|
|
|
|
ReadTextureProperty(r, *curPbrSpecularGlossiness, "specularGlossinessTexture", pbrSG.specularGlossinessTexture);
|
|
|
|
ReadMember(*curPbrSpecularGlossiness, "specularFactor", pbrSG.specularFactor);
|
|
|
|
ReadMember(*curPbrSpecularGlossiness, "glossinessFactor", pbrSG.glossinessFactor);
|
2017-09-05 20:29:00 +00:00
|
|
|
|
|
|
|
this->pbrSpecularGlossiness = Nullable<PbrSpecularGlossiness>(pbrSG);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-18 21:01:25 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
// Extension KHR_texture_transform is handled in ReadTextureProperty
|
2019-11-14 20:11:53 +00:00
|
|
|
|
2020-12-20 06:59:12 +00:00
|
|
|
if (r.extensionsUsed.KHR_materials_sheen) {
|
|
|
|
if (Value *curMaterialSheen = FindObject(*extensions, "KHR_materials_sheen")) {
|
|
|
|
MaterialSheen sheen;
|
|
|
|
|
|
|
|
ReadMember(*curMaterialSheen, "sheenColorFactor", sheen.sheenColorFactor);
|
|
|
|
ReadTextureProperty(r, *curMaterialSheen, "sheenColorTexture", sheen.sheenColorTexture);
|
|
|
|
ReadMember(*curMaterialSheen, "sheenRoughnessFactor", sheen.sheenRoughnessFactor);
|
|
|
|
ReadTextureProperty(r, *curMaterialSheen, "sheenRoughnessTexture", sheen.sheenRoughnessTexture);
|
|
|
|
|
|
|
|
this->materialSheen = Nullable<MaterialSheen>(sheen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 07:12:09 +00:00
|
|
|
if (r.extensionsUsed.KHR_materials_clearcoat) {
|
|
|
|
if (Value *curMaterialClearcoat = FindObject(*extensions, "KHR_materials_clearcoat")) {
|
|
|
|
MaterialClearcoat clearcoat;
|
|
|
|
|
|
|
|
ReadMember(*curMaterialClearcoat, "clearcoatFactor", clearcoat.clearcoatFactor);
|
|
|
|
ReadTextureProperty(r, *curMaterialClearcoat, "clearcoatTexture", clearcoat.clearcoatTexture);
|
|
|
|
ReadMember(*curMaterialClearcoat, "clearcoatRoughnessFactor", clearcoat.clearcoatRoughnessFactor);
|
|
|
|
ReadTextureProperty(r, *curMaterialClearcoat, "clearcoatRoughnessTexture", clearcoat.clearcoatRoughnessTexture);
|
|
|
|
ReadTextureProperty(r, *curMaterialClearcoat, "clearcoatNormalTexture", clearcoat.clearcoatNormalTexture);
|
|
|
|
|
|
|
|
this->materialClearcoat = Nullable<MaterialClearcoat>(clearcoat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:43:01 +00:00
|
|
|
if (r.extensionsUsed.KHR_materials_transmission) {
|
|
|
|
if (Value *curMaterialTransmission = FindObject(*extensions, "KHR_materials_transmission")) {
|
|
|
|
MaterialTransmission transmission;
|
|
|
|
|
|
|
|
ReadMember(*curMaterialTransmission, "transmissionFactor", transmission.transmissionFactor);
|
|
|
|
ReadTextureProperty(r, *curMaterialTransmission, "transmissionTexture", transmission.transmissionTexture);
|
|
|
|
|
|
|
|
this->materialTransmission = Nullable<MaterialTransmission>(transmission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 21:01:25 +00:00
|
|
|
unlit = nullptr != FindObject(*extensions, "KHR_materials_unlit");
|
2017-08-31 05:35:10 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2020-03-08 20:24:01 +00:00
|
|
|
void SetVector(vec4 &v, const float (&in)[4]) {
|
|
|
|
v[0] = in[0];
|
|
|
|
v[1] = in[1];
|
|
|
|
v[2] = in[2];
|
|
|
|
v[3] = in[3];
|
|
|
|
}
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void SetVector(vec3 &v, const float (&in)[3]) {
|
|
|
|
v[0] = in[0];
|
|
|
|
v[1] = in[1];
|
|
|
|
v[2] = in[2];
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} // namespace
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Material::SetDefaults() {
|
2017-08-30 21:25:11 +00:00
|
|
|
//pbr materials
|
2017-09-05 19:45:32 +00:00
|
|
|
SetVector(pbrMetallicRoughness.baseColorFactor, defaultBaseColor);
|
2021-01-27 10:56:57 +00:00
|
|
|
pbrMetallicRoughness.metallicFactor = 1.0f;
|
|
|
|
pbrMetallicRoughness.roughnessFactor = 1.0f;
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2017-09-05 19:45:32 +00:00
|
|
|
SetVector(emissiveFactor, defaultEmissiveFactor);
|
2017-08-30 21:25:11 +00:00
|
|
|
alphaMode = "OPAQUE";
|
2021-01-27 10:56:57 +00:00
|
|
|
alphaCutoff = 0.5f;
|
2017-07-19 20:21:43 +00:00
|
|
|
doubleSided = false;
|
2018-05-18 21:01:25 +00:00
|
|
|
unlit = false;
|
2017-09-05 20:29:00 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void PbrSpecularGlossiness::SetDefaults() {
|
2017-08-31 05:35:10 +00:00
|
|
|
//pbrSpecularGlossiness properties
|
2017-09-05 20:29:00 +00:00
|
|
|
SetVector(diffuseFactor, defaultDiffuseFactor);
|
|
|
|
SetVector(specularFactor, defaultSpecularFactor);
|
2021-01-27 10:56:57 +00:00
|
|
|
glossinessFactor = 1.0f;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 06:59:12 +00:00
|
|
|
inline void MaterialSheen::SetDefaults() {
|
|
|
|
//KHR_materials_sheen properties
|
|
|
|
SetVector(sheenColorFactor, defaultSheenFactor);
|
|
|
|
sheenRoughnessFactor = 0.f;
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
template <int N>
|
|
|
|
inline int Compare(const char *attr, const char (&str)[N]) {
|
|
|
|
return (strncmp(attr, str, N - 1) == 0) ? N - 1 : 0;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-07-15 02:19:07 +00:00
|
|
|
#if _MSC_VER
|
2020-05-18 19:16:45 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4706)
|
2020-07-15 02:19:07 +00:00
|
|
|
#endif // _MSC_VER
|
2020-03-15 10:47:05 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline bool GetAttribVector(Mesh::Primitive &p, const char *attr, Mesh::AccessorList *&v, int &pos) {
|
|
|
|
if ((pos = Compare(attr, "POSITION"))) {
|
|
|
|
v = &(p.attributes.position);
|
|
|
|
} else if ((pos = Compare(attr, "NORMAL"))) {
|
|
|
|
v = &(p.attributes.normal);
|
|
|
|
} else if ((pos = Compare(attr, "TANGENT"))) {
|
|
|
|
v = &(p.attributes.tangent);
|
|
|
|
} else if ((pos = Compare(attr, "TEXCOORD"))) {
|
|
|
|
v = &(p.attributes.texcoord);
|
|
|
|
} else if ((pos = Compare(attr, "COLOR"))) {
|
|
|
|
v = &(p.attributes.color);
|
|
|
|
} else if ((pos = Compare(attr, "JOINT"))) {
|
|
|
|
v = &(p.attributes.joint);
|
|
|
|
} else if ((pos = Compare(attr, "JOINTMATRIX"))) {
|
|
|
|
v = &(p.attributes.jointmatrix);
|
|
|
|
} else if ((pos = Compare(attr, "WEIGHT"))) {
|
|
|
|
v = &(p.attributes.weight);
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-05 13:28:29 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline bool GetAttribTargetVector(Mesh::Primitive &p, const int targetIndex, const char *attr, Mesh::AccessorList *&v, int &pos) {
|
|
|
|
if ((pos = Compare(attr, "POSITION"))) {
|
|
|
|
v = &(p.targets[targetIndex].position);
|
|
|
|
} else if ((pos = Compare(attr, "NORMAL"))) {
|
|
|
|
v = &(p.targets[targetIndex].normal);
|
|
|
|
} else if ((pos = Compare(attr, "TANGENT"))) {
|
|
|
|
v = &(p.targets[targetIndex].tangent);
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
return true;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} // namespace
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Mesh::Read(Value &pJSON_Object, Asset &pAsset_Root) {
|
|
|
|
Value *curName = FindMember(pJSON_Object, "name");
|
|
|
|
if (nullptr != curName) {
|
2020-03-12 16:38:42 +00:00
|
|
|
name = curName->GetString();
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
/****************** Mesh primitives ******************/
|
|
|
|
Value *curPrimitives = FindArray(pJSON_Object, "primitives");
|
|
|
|
if (nullptr != curPrimitives) {
|
|
|
|
this->primitives.resize(curPrimitives->Size());
|
|
|
|
for (unsigned int i = 0; i < curPrimitives->Size(); ++i) {
|
|
|
|
Value &primitive = (*curPrimitives)[i];
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Primitive &prim = this->primitives[i];
|
2017-07-19 20:21:43 +00:00
|
|
|
prim.mode = MemberOrDefault(primitive, "mode", PrimitiveMode_TRIANGLES);
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
if (Value *indices = FindUInt(primitive, "indices")) {
|
|
|
|
prim.indices = pAsset_Root.accessors.Retrieve(indices->GetUint());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Value *material = FindUInt(primitive, "material")) {
|
|
|
|
prim.material = pAsset_Root.materials.Retrieve(material->GetUint());
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *attrs = FindObject(primitive, "attributes")) {
|
2017-07-19 20:21:43 +00:00
|
|
|
for (Value::MemberIterator it = attrs->MemberBegin(); it != attrs->MemberEnd(); ++it) {
|
2017-08-25 20:09:07 +00:00
|
|
|
if (!it->value.IsUint()) continue;
|
2020-03-08 20:24:01 +00:00
|
|
|
const char *attr = it->name.GetString();
|
2017-11-27 18:45:15 +00:00
|
|
|
// Valid attribute semantics include POSITION, NORMAL, TANGENT, TEXCOORD, COLOR, JOINT, JOINTMATRIX,
|
2017-07-19 20:21:43 +00:00
|
|
|
// and WEIGHT.Attribute semantics can be of the form[semantic]_[set_index], e.g., TEXCOORD_0, TEXCOORD_1, etc.
|
|
|
|
|
|
|
|
int undPos = 0;
|
2021-01-26 15:56:49 +00:00
|
|
|
Mesh::AccessorList *vec = nullptr;
|
2017-07-19 20:21:43 +00:00
|
|
|
if (GetAttribVector(prim, attr, vec, undPos)) {
|
|
|
|
size_t idx = (attr[undPos] == '_') ? atoi(attr + undPos + 1) : 0;
|
2020-10-23 11:01:43 +00:00
|
|
|
if ((*vec).size() != idx) {
|
2021-01-27 09:55:03 +00:00
|
|
|
throw DeadlyImportError("GLTF: Invalid attribute in mesh: ", name, " primitive: ", i, "attrib: ", attr,
|
2021-01-27 09:42:49 +00:00
|
|
|
". All indices for indexed attribute semantics must start with 0 and be continuous positive integers: TEXCOORD_0, TEXCOORD_1, etc.");
|
2020-10-23 11:01:43 +00:00
|
|
|
}
|
|
|
|
(*vec).resize(idx + 1);
|
2020-03-08 20:24:01 +00:00
|
|
|
(*vec)[idx] = pAsset_Root.accessors.Retrieve(it->value.GetUint());
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
#ifdef ASSIMP_ENABLE_DRACO
|
|
|
|
// KHR_draco_mesh_compression spec: Draco can only be used for glTF Triangles or Triangle Strips
|
|
|
|
if (pAsset_Root.extensionsUsed.KHR_draco_mesh_compression && (prim.mode == PrimitiveMode_TRIANGLES || prim.mode == PrimitiveMode_TRIANGLE_STRIP)) {
|
|
|
|
// Look for draco mesh compression extension and bufferView
|
|
|
|
// Skip if any missing
|
2021-01-27 09:42:49 +00:00
|
|
|
if (Value *dracoExt = FindExtension(primitive, "KHR_draco_mesh_compression")) {
|
|
|
|
if (Value *bufView = FindUInt(*dracoExt, "bufferView")) {
|
|
|
|
// Attempt to load indices and attributes using draco compression
|
|
|
|
auto bufferView = pAsset_Root.bufferViews.Retrieve(bufView->GetUint());
|
|
|
|
// Attempt to perform the draco decode on the buffer data
|
|
|
|
const char *bufferViewData = reinterpret_cast<const char *>(bufferView->buffer->GetPointer() + bufferView->byteOffset);
|
|
|
|
draco::DecoderBuffer decoderBuffer;
|
|
|
|
decoderBuffer.Init(bufferViewData, bufferView->byteLength);
|
|
|
|
draco::Decoder decoder;
|
|
|
|
auto decodeResult = decoder.DecodeMeshFromBuffer(&decoderBuffer);
|
|
|
|
if (!decodeResult.ok()) {
|
|
|
|
// A corrupt Draco isn't actually fatal if the primitive data is also provided in a standard buffer, but does anyone do that?
|
|
|
|
throw DeadlyImportError("GLTF: Invalid Draco mesh compression in mesh: ", name, " primitive: ", i, ": ", decodeResult.status().error_msg_string());
|
|
|
|
}
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
// Now we have a draco mesh
|
|
|
|
const std::unique_ptr<draco::Mesh> &pDracoMesh = decodeResult.value();
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
// Redirect the accessors to the decoded data
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
// Indices
|
|
|
|
SetDecodedIndexBuffer_Draco(*pDracoMesh, prim);
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
// Vertex attributes
|
|
|
|
if (Value *attrs = FindObject(*dracoExt, "attributes")) {
|
|
|
|
for (Value::MemberIterator it = attrs->MemberBegin(); it != attrs->MemberEnd(); ++it) {
|
|
|
|
if (!it->value.IsUint()) continue;
|
|
|
|
const char *attr = it->name.GetString();
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
int undPos = 0;
|
|
|
|
Mesh::AccessorList *vec = nullptr;
|
|
|
|
if (GetAttribVector(prim, attr, vec, undPos)) {
|
|
|
|
size_t idx = (attr[undPos] == '_') ? atoi(attr + undPos + 1) : 0;
|
|
|
|
if (idx >= (*vec).size()) {
|
|
|
|
throw DeadlyImportError("GLTF: Invalid draco attribute in mesh: ", name, " primitive: ", i, " attrib: ", attr,
|
|
|
|
". All indices for indexed attribute semantics must start with 0 and be continuous positive integers: TEXCOORD_0, TEXCOORD_1, etc.");
|
|
|
|
}
|
2021-01-26 15:56:49 +00:00
|
|
|
|
2021-01-27 09:42:49 +00:00
|
|
|
if (!(*vec)[idx]) {
|
|
|
|
throw DeadlyImportError("GLTF: Invalid draco attribute in mesh: ", name, " primitive: ", i, " attrib: ", attr,
|
|
|
|
". All draco-encoded attributes must also define an accessor.");
|
2021-01-26 15:56:49 +00:00
|
|
|
}
|
2021-01-27 09:42:49 +00:00
|
|
|
|
|
|
|
Accessor &attribAccessor = *(*vec)[idx];
|
|
|
|
if (attribAccessor.count == 0)
|
|
|
|
throw DeadlyImportError("GLTF: Invalid draco attribute in mesh: ", name, " primitive: ", i, " attrib: ", attr);
|
|
|
|
|
|
|
|
// Redirect this accessor to the appropriate Draco vertex attribute data
|
|
|
|
const uint32_t dracoAttribId = it->value.GetUint();
|
|
|
|
SetDecodedAttributeBuffer_Draco(*pDracoMesh, dracoAttribId, attribAccessor);
|
2021-01-26 15:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *targetsArray = FindArray(primitive, "targets");
|
|
|
|
if (nullptr != targetsArray) {
|
2018-07-05 13:28:29 +00:00
|
|
|
prim.targets.resize(targetsArray->Size());
|
2020-03-08 20:24:01 +00:00
|
|
|
for (unsigned int j = 0; j < targetsArray->Size(); ++j) {
|
|
|
|
Value &target = (*targetsArray)[j];
|
|
|
|
if (!target.IsObject()) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-05 13:28:29 +00:00
|
|
|
for (Value::MemberIterator it = target.MemberBegin(); it != target.MemberEnd(); ++it) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!it->value.IsUint()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const char *attr = it->name.GetString();
|
2018-07-05 13:28:29 +00:00
|
|
|
// Valid attribute semantics include POSITION, NORMAL, TANGENT
|
|
|
|
int undPos = 0;
|
2021-01-27 10:56:57 +00:00
|
|
|
Mesh::AccessorList *vec = nullptr;
|
2020-05-05 19:51:40 +00:00
|
|
|
if (GetAttribTargetVector(prim, j, attr, vec, undPos)) {
|
2018-07-05 13:28:29 +00:00
|
|
|
size_t idx = (attr[undPos] == '_') ? atoi(attr + undPos + 1) : 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
if ((*vec).size() <= idx) {
|
|
|
|
(*vec).resize(idx + 1);
|
|
|
|
}
|
2018-07-05 13:28:29 +00:00
|
|
|
(*vec)[idx] = pAsset_Root.accessors.Retrieve(it->value.GetUint());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 13:28:29 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curWeights = FindArray(pJSON_Object, "weights");
|
|
|
|
if (nullptr != curWeights) {
|
|
|
|
this->weights.resize(curWeights->Size());
|
|
|
|
for (unsigned int i = 0; i < curWeights->Size(); ++i) {
|
|
|
|
Value &weightValue = (*curWeights)[i];
|
|
|
|
if (weightValue.IsNumber()) {
|
|
|
|
this->weights[i] = weightValue.GetFloat();
|
|
|
|
}
|
2018-07-05 13:28:29 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-14 23:31:27 +00:00
|
|
|
|
2020-04-19 19:14:47 +00:00
|
|
|
Value *extras = FindObject(pJSON_Object, "extras");
|
2020-05-18 19:16:45 +00:00
|
|
|
if (nullptr != extras) {
|
|
|
|
if (Value *curTargetNames = FindArray(*extras, "targetNames")) {
|
2020-04-14 23:31:27 +00:00
|
|
|
this->targetNames.resize(curTargetNames->Size());
|
|
|
|
for (unsigned int i = 0; i < curTargetNames->Size(); ++i) {
|
2020-05-18 19:16:45 +00:00
|
|
|
Value &targetNameValue = (*curTargetNames)[i];
|
2020-04-14 23:31:27 +00:00
|
|
|
if (targetNameValue.IsString()) {
|
|
|
|
this->targetNames[i] = targetNameValue.GetString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Camera::Read(Value &obj, Asset & /*r*/) {
|
2018-09-13 06:04:04 +00:00
|
|
|
std::string type_string = std::string(MemberOrDefault(obj, "type", "perspective"));
|
|
|
|
if (type_string == "orthographic") {
|
|
|
|
type = Camera::Orthographic;
|
|
|
|
} else {
|
|
|
|
type = Camera::Perspective;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
const char *subobjId = (type == Camera::Orthographic) ? "orthographic" : "perspective";
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *it = FindObject(obj, subobjId);
|
2017-07-19 20:21:43 +00:00
|
|
|
if (!it) throw DeadlyImportError("GLTF: Camera missing its parameters");
|
|
|
|
|
|
|
|
if (type == Camera::Perspective) {
|
2017-08-24 21:31:33 +00:00
|
|
|
cameraProperties.perspective.aspectRatio = MemberOrDefault(*it, "aspectRatio", 0.f);
|
2020-03-08 20:24:01 +00:00
|
|
|
cameraProperties.perspective.yfov = MemberOrDefault(*it, "yfov", 3.1415f / 2.f);
|
|
|
|
cameraProperties.perspective.zfar = MemberOrDefault(*it, "zfar", 100.f);
|
|
|
|
cameraProperties.perspective.znear = MemberOrDefault(*it, "znear", 0.01f);
|
|
|
|
} else {
|
2020-04-28 00:07:56 +00:00
|
|
|
cameraProperties.ortographic.xmag = MemberOrDefault(*it, "xmag", 1.f);
|
|
|
|
cameraProperties.ortographic.ymag = MemberOrDefault(*it, "ymag", 1.f);
|
|
|
|
cameraProperties.ortographic.zfar = MemberOrDefault(*it, "zfar", 100.f);
|
|
|
|
cameraProperties.ortographic.znear = MemberOrDefault(*it, "znear", 0.01f);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Light::Read(Value &obj, Asset & /*r*/) {
|
2019-08-03 18:51:00 +00:00
|
|
|
#ifndef M_PI
|
|
|
|
const float M_PI = 3.14159265358979323846f;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::string type_string;
|
|
|
|
ReadMember(obj, "type", type_string);
|
|
|
|
if (type_string == "directional")
|
|
|
|
type = Light::Directional;
|
|
|
|
else if (type_string == "point")
|
|
|
|
type = Light::Point;
|
|
|
|
else
|
|
|
|
type = Light::Spot;
|
|
|
|
|
|
|
|
name = MemberOrDefault(obj, "name", "");
|
|
|
|
|
|
|
|
SetVector(color, vec3{ 1.0f, 1.0f, 1.0f });
|
|
|
|
ReadMember(obj, "color", color);
|
|
|
|
|
|
|
|
intensity = MemberOrDefault(obj, "intensity", 1.0f);
|
|
|
|
|
|
|
|
ReadMember(obj, "range", range);
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (type == Light::Spot) {
|
|
|
|
Value *spot = FindObject(obj, "spot");
|
2019-08-03 18:51:00 +00:00
|
|
|
if (!spot) throw DeadlyImportError("GLTF: Light missing its spot parameters");
|
|
|
|
innerConeAngle = MemberOrDefault(*spot, "innerConeAngle", 0.0f);
|
2020-11-16 08:19:13 +00:00
|
|
|
outerConeAngle = MemberOrDefault(*spot, "outerConeAngle", static_cast<float>(M_PI / 4.0f));
|
2019-08-03 18:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
inline CustomExtension ReadExtensions(const char *name, Value &obj) {
|
2020-06-26 04:26:45 +00:00
|
|
|
CustomExtension ret;
|
|
|
|
ret.name = name;
|
|
|
|
if (obj.IsObject()) {
|
|
|
|
ret.mValues.isPresent = true;
|
|
|
|
for (auto it = obj.MemberBegin(); it != obj.MemberEnd(); ++it) {
|
2021-01-27 09:55:03 +00:00
|
|
|
auto &val = it->value;
|
2020-06-26 04:26:45 +00:00
|
|
|
ret.mValues.value.push_back(ReadExtensions(it->name.GetString(), val));
|
|
|
|
}
|
2021-01-27 09:55:03 +00:00
|
|
|
} else if (obj.IsArray()) {
|
2020-06-26 04:26:45 +00:00
|
|
|
ret.mValues.value.reserve(obj.Size());
|
|
|
|
ret.mValues.isPresent = true;
|
2021-01-27 09:55:03 +00:00
|
|
|
for (unsigned int i = 0; i < obj.Size(); ++i) {
|
2020-06-26 04:26:45 +00:00
|
|
|
ret.mValues.value.push_back(ReadExtensions(name, obj[i]));
|
|
|
|
}
|
2021-01-27 09:55:03 +00:00
|
|
|
} else if (obj.IsNumber()) {
|
2020-06-26 04:26:45 +00:00
|
|
|
if (obj.IsUint64()) {
|
|
|
|
ret.mUint64Value.value = obj.GetUint64();
|
|
|
|
ret.mUint64Value.isPresent = true;
|
|
|
|
} else if (obj.IsInt64()) {
|
|
|
|
ret.mInt64Value.value = obj.GetInt64();
|
|
|
|
ret.mInt64Value.isPresent = true;
|
|
|
|
} else if (obj.IsDouble()) {
|
|
|
|
ret.mDoubleValue.value = obj.GetDouble();
|
|
|
|
ret.mDoubleValue.isPresent = true;
|
|
|
|
}
|
2021-01-27 09:55:03 +00:00
|
|
|
} else if (obj.IsString()) {
|
2020-06-26 04:26:45 +00:00
|
|
|
ReadValue(obj, ret.mStringValue);
|
|
|
|
ret.mStringValue.isPresent = true;
|
2021-01-27 09:55:03 +00:00
|
|
|
} else if (obj.IsBool()) {
|
2020-06-27 02:53:26 +00:00
|
|
|
ret.mBoolValue.value = obj.GetBool();
|
|
|
|
ret.mBoolValue.isPresent = true;
|
|
|
|
}
|
2020-06-26 04:26:45 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Node::Read(Value &obj, Asset &r) {
|
2019-09-14 07:12:08 +00:00
|
|
|
if (name.empty()) {
|
|
|
|
name = id;
|
|
|
|
}
|
2017-08-24 21:31:33 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curChildren = FindArray(obj, "children");
|
|
|
|
if (nullptr != curChildren) {
|
|
|
|
this->children.reserve(curChildren->Size());
|
|
|
|
for (unsigned int i = 0; i < curChildren->Size(); ++i) {
|
|
|
|
Value &child = (*curChildren)[i];
|
2017-08-25 20:09:07 +00:00
|
|
|
if (child.IsUint()) {
|
2017-07-19 20:21:43 +00:00
|
|
|
// get/create the child node
|
2017-08-25 20:09:07 +00:00
|
|
|
Ref<Node> chn = r.nodes.Retrieve(child.GetUint());
|
2020-03-08 20:24:01 +00:00
|
|
|
if (chn) {
|
|
|
|
this->children.push_back(chn);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curMatrix = FindArray(obj, "matrix");
|
|
|
|
if (nullptr != curMatrix) {
|
|
|
|
ReadValue(*curMatrix, this->matrix);
|
|
|
|
} else {
|
2017-07-19 20:21:43 +00:00
|
|
|
ReadMember(obj, "translation", translation);
|
|
|
|
ReadMember(obj, "scale", scale);
|
|
|
|
ReadMember(obj, "rotation", rotation);
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curMesh = FindUInt(obj, "mesh");
|
2020-03-15 09:17:54 +00:00
|
|
|
if (nullptr != curMesh) {
|
2020-03-08 20:24:01 +00:00
|
|
|
unsigned int numMeshes = 1;
|
2017-09-17 19:11:01 +00:00
|
|
|
this->meshes.reserve(numMeshes);
|
2020-03-08 20:24:01 +00:00
|
|
|
Ref<Mesh> meshRef = r.meshes.Retrieve((*curMesh).GetUint());
|
|
|
|
if (meshRef) {
|
|
|
|
this->meshes.push_back(meshRef);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 09:55:03 +00:00
|
|
|
// Do not retrieve a skin here, just take a reference, to avoid infinite recursion
|
2020-05-15 13:48:41 +00:00
|
|
|
// Skins will be properly loaded later
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curSkin = FindUInt(obj, "skin");
|
2020-03-15 09:17:54 +00:00
|
|
|
if (nullptr != curSkin) {
|
2020-05-15 13:48:41 +00:00
|
|
|
this->skin = r.skins.Get(curSkin->GetUint());
|
2018-10-26 22:36:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curCamera = FindUInt(obj, "camera");
|
2020-03-15 09:17:54 +00:00
|
|
|
if (nullptr != curCamera) {
|
2020-03-08 20:24:01 +00:00
|
|
|
this->camera = r.cameras.Retrieve(curCamera->GetUint());
|
|
|
|
if (this->camera) {
|
2017-07-19 20:21:43 +00:00
|
|
|
this->camera->id = this->id;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2019-08-03 18:51:00 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curExtensions = FindObject(obj, "extensions");
|
|
|
|
if (nullptr != curExtensions) {
|
2020-06-26 04:26:45 +00:00
|
|
|
this->extensions = ReadExtensions("extensions", *curExtensions);
|
|
|
|
|
2019-08-03 18:51:00 +00:00
|
|
|
if (r.extensionsUsed.KHR_lights_punctual) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *ext = FindObject(*curExtensions, "KHR_lights_punctual")) {
|
|
|
|
Value *curLight = FindUInt(*ext, "light");
|
|
|
|
if (nullptr != curLight) {
|
|
|
|
this->light = r.lights.Retrieve(curLight->GetUint());
|
|
|
|
if (this->light) {
|
2019-08-03 18:51:00 +00:00
|
|
|
this->light->id = this->id;
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
2019-08-03 18:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Scene::Read(Value &obj, Asset &r) {
|
2020-11-22 05:36:08 +00:00
|
|
|
if (Value *scene_name = FindString(obj, "name")) {
|
|
|
|
if (scene_name->IsString()) {
|
|
|
|
this->name = scene_name->GetString();
|
2020-11-22 05:10:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *array = FindArray(obj, "nodes")) {
|
2017-07-19 20:21:43 +00:00
|
|
|
for (unsigned int i = 0; i < array->Size(); ++i) {
|
2017-08-25 20:09:07 +00:00
|
|
|
if (!(*array)[i].IsUint()) continue;
|
|
|
|
Ref<Node> node = r.nodes.Retrieve((*array)[i].GetUint());
|
2017-07-19 20:21:43 +00:00
|
|
|
if (node)
|
|
|
|
this->nodes.push_back(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Skin::Read(Value &obj, Asset &r) {
|
|
|
|
if (Value *matrices = FindUInt(obj, "inverseBindMatrices")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
inverseBindMatrices = r.accessors.Retrieve(matrices->GetUint());
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *joints = FindArray(obj, "joints")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
for (unsigned i = 0; i < joints->Size(); ++i) {
|
|
|
|
if (!(*joints)[i].IsUint()) continue;
|
|
|
|
Ref<Node> node = r.nodes.Retrieve((*joints)[i].GetUint());
|
|
|
|
if (node) {
|
|
|
|
this->jointNames.push_back(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Animation::Read(Value &obj, Asset &r) {
|
|
|
|
Value *curSamplers = FindArray(obj, "samplers");
|
|
|
|
if (nullptr != curSamplers) {
|
|
|
|
for (unsigned i = 0; i < curSamplers->Size(); ++i) {
|
|
|
|
Value &sampler = (*curSamplers)[i];
|
2018-10-26 22:36:34 +00:00
|
|
|
|
|
|
|
Sampler s;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *input = FindUInt(sampler, "input")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
s.input = r.accessors.Retrieve(input->GetUint());
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *output = FindUInt(sampler, "output")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
s.output = r.accessors.Retrieve(output->GetUint());
|
|
|
|
}
|
|
|
|
s.interpolation = Interpolation_LINEAR;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *interpolation = FindString(sampler, "interpolation")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
const std::string interp = interpolation->GetString();
|
|
|
|
if (interp == "LINEAR") {
|
2020-03-08 20:24:01 +00:00
|
|
|
s.interpolation = Interpolation_LINEAR;
|
2018-10-26 22:36:34 +00:00
|
|
|
} else if (interp == "STEP") {
|
2020-03-08 20:24:01 +00:00
|
|
|
s.interpolation = Interpolation_STEP;
|
2018-10-26 22:36:34 +00:00
|
|
|
} else if (interp == "CUBICSPLINE") {
|
2020-03-08 20:24:01 +00:00
|
|
|
s.interpolation = Interpolation_CUBICSPLINE;
|
2018-10-26 22:36:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this->samplers.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curChannels = FindArray(obj, "channels");
|
|
|
|
if (nullptr != curChannels) {
|
|
|
|
for (unsigned i = 0; i < curChannels->Size(); ++i) {
|
|
|
|
Value &channel = (*curChannels)[i];
|
2018-10-26 22:36:34 +00:00
|
|
|
|
|
|
|
Channel c;
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curSampler = FindUInt(channel, "sampler");
|
|
|
|
if (nullptr != curSampler) {
|
|
|
|
c.sampler = curSampler->GetUint();
|
2018-10-26 22:36:34 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *target = FindObject(channel, "target")) {
|
|
|
|
if (Value *node = FindUInt(*target, "node")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
c.target.node = r.nodes.Retrieve(node->GetUint());
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *path = FindString(*target, "path")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
const std::string p = path->GetString();
|
|
|
|
if (p == "translation") {
|
|
|
|
c.target.path = AnimationPath_TRANSLATION;
|
|
|
|
} else if (p == "rotation") {
|
|
|
|
c.target.path = AnimationPath_ROTATION;
|
|
|
|
} else if (p == "scale") {
|
|
|
|
c.target.path = AnimationPath_SCALE;
|
|
|
|
} else if (p == "weights") {
|
|
|
|
c.target.path = AnimationPath_WEIGHTS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this->channels.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void AssetMetadata::Read(Document &doc) {
|
|
|
|
if (Value *obj = FindObject(doc, "asset")) {
|
2017-07-19 20:21:43 +00:00
|
|
|
ReadMember(*obj, "copyright", copyright);
|
|
|
|
ReadMember(*obj, "generator", generator);
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
if (Value *versionString = FindStringInContext(*obj, "version", "\"asset\"")) {
|
2017-09-06 19:32:44 +00:00
|
|
|
version = versionString->GetString();
|
|
|
|
}
|
2021-03-15 13:06:11 +00:00
|
|
|
Value *curProfile = FindObjectInContext(*obj, "profile", "\"asset\"");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (nullptr != curProfile) {
|
|
|
|
ReadMember(*curProfile, "api", this->profile.api);
|
|
|
|
ReadMember(*curProfile, "version", this->profile.version);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 19:32:44 +00:00
|
|
|
if (version.empty() || version[0] != '2') {
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("GLTF: Unsupported glTF version: ", version);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Asset methods implementation
|
|
|
|
//
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Asset::ReadBinaryHeader(IOStream &stream, std::vector<char> &sceneData) {
|
2020-05-15 16:46:50 +00:00
|
|
|
ASSIMP_LOG_DEBUG("Reading GLTF2 binary");
|
2017-11-20 13:59:05 +00:00
|
|
|
GLB_Header header;
|
|
|
|
if (stream.Read(&header, sizeof(header), 1) != 1) {
|
|
|
|
throw DeadlyImportError("GLTF: Unable to read the file header");
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (strncmp((char *)header.magic, AI_GLB_MAGIC_NUMBER, sizeof(header.magic)) != 0) {
|
2017-11-20 13:59:05 +00:00
|
|
|
throw DeadlyImportError("GLTF: Invalid binary glTF file");
|
|
|
|
}
|
|
|
|
|
|
|
|
AI_SWAP4(header.version);
|
2021-03-09 20:08:28 +00:00
|
|
|
asset.version = ai_to_string(header.version);
|
2017-11-20 13:59:05 +00:00
|
|
|
if (header.version != 2) {
|
|
|
|
throw DeadlyImportError("GLTF: Unsupported binary glTF version");
|
|
|
|
}
|
|
|
|
|
|
|
|
GLB_Chunk chunk;
|
|
|
|
if (stream.Read(&chunk, sizeof(chunk), 1) != 1) {
|
|
|
|
throw DeadlyImportError("GLTF: Unable to read JSON chunk");
|
|
|
|
}
|
|
|
|
|
|
|
|
AI_SWAP4(chunk.chunkLength);
|
|
|
|
AI_SWAP4(chunk.chunkType);
|
|
|
|
|
|
|
|
if (chunk.chunkType != ChunkType_JSON) {
|
|
|
|
throw DeadlyImportError("GLTF: JSON chunk missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the scene data
|
|
|
|
|
|
|
|
mSceneLength = chunk.chunkLength;
|
|
|
|
sceneData.resize(mSceneLength + 1);
|
|
|
|
sceneData[mSceneLength] = '\0';
|
|
|
|
|
|
|
|
if (stream.Read(&sceneData[0], 1, mSceneLength) != mSceneLength) {
|
|
|
|
throw DeadlyImportError("GLTF: Could not read the file contents");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t padding = ((chunk.chunkLength + 3) & ~3) - chunk.chunkLength;
|
|
|
|
if (padding > 0) {
|
|
|
|
stream.Seek(padding, aiOrigin_CUR);
|
|
|
|
}
|
|
|
|
|
|
|
|
AI_SWAP4(header.length);
|
|
|
|
mBodyOffset = 12 + 8 + chunk.chunkLength + padding + 8;
|
|
|
|
if (header.length >= mBodyOffset) {
|
|
|
|
if (stream.Read(&chunk, sizeof(chunk), 1) != 1) {
|
|
|
|
throw DeadlyImportError("GLTF: Unable to read BIN chunk");
|
|
|
|
}
|
|
|
|
|
|
|
|
AI_SWAP4(chunk.chunkLength);
|
|
|
|
AI_SWAP4(chunk.chunkType);
|
|
|
|
|
|
|
|
if (chunk.chunkType != ChunkType_BIN) {
|
|
|
|
throw DeadlyImportError("GLTF: BIN chunk missing");
|
|
|
|
}
|
|
|
|
|
|
|
|
mBodyLength = chunk.chunkLength;
|
2020-03-08 20:24:01 +00:00
|
|
|
} else {
|
2017-11-20 13:59:05 +00:00
|
|
|
mBodyOffset = mBodyLength = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Asset::Load(const std::string &pFile, bool isBinary) {
|
2020-05-15 16:46:50 +00:00
|
|
|
ASSIMP_LOG_DEBUG("Loading GLTF2 asset");
|
2017-07-19 20:21:43 +00:00
|
|
|
mCurrentAssetDir.clear();
|
2020-03-15 09:17:54 +00:00
|
|
|
/*int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\')));
|
|
|
|
if (pos != int(std::string::npos)) */
|
|
|
|
|
2020-12-28 07:18:50 +00:00
|
|
|
if (0 != strncmp(pFile.c_str(), AI_MEMORYIO_MAGIC_FILENAME, AI_MEMORYIO_MAGIC_FILENAME_LENGTH)) {
|
|
|
|
mCurrentAssetDir = glTFCommon::getCurrentAssetDir(pFile);
|
|
|
|
}
|
2021-01-27 09:55:03 +00:00
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
shared_ptr<IOStream> stream(OpenFile(pFile.c_str(), "rb", true));
|
|
|
|
if (!stream) {
|
|
|
|
throw DeadlyImportError("GLTF: Could not open file for reading");
|
|
|
|
}
|
|
|
|
|
2017-11-20 13:59:05 +00:00
|
|
|
// is binary? then read the header
|
|
|
|
std::vector<char> sceneData;
|
|
|
|
if (isBinary) {
|
|
|
|
SetAsBinary(); // also creates the body buffer
|
|
|
|
ReadBinaryHeader(*stream, sceneData);
|
2020-03-08 20:24:01 +00:00
|
|
|
} else {
|
2017-11-20 13:59:05 +00:00
|
|
|
mSceneLength = stream->FileSize();
|
|
|
|
mBodyLength = 0;
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2017-11-20 13:59:05 +00:00
|
|
|
// read the scene data
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2017-11-20 13:59:05 +00:00
|
|
|
sceneData.resize(mSceneLength + 1);
|
|
|
|
sceneData[mSceneLength] = '\0';
|
|
|
|
|
|
|
|
if (stream->Read(&sceneData[0], 1, mSceneLength) != mSceneLength) {
|
|
|
|
throw DeadlyImportError("GLTF: Could not read the file contents");
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse the JSON document
|
2020-05-15 16:46:50 +00:00
|
|
|
ASSIMP_LOG_DEBUG("Parsing GLTF2 JSON");
|
2017-07-19 20:21:43 +00:00
|
|
|
Document doc;
|
|
|
|
doc.ParseInsitu(&sceneData[0]);
|
|
|
|
|
|
|
|
if (doc.HasParseError()) {
|
|
|
|
char buffer[32];
|
|
|
|
ai_snprintf(buffer, 32, "%d", static_cast<int>(doc.GetErrorOffset()));
|
2020-08-18 16:35:08 +00:00
|
|
|
throw DeadlyImportError("GLTF: JSON parse error, offset ", buffer, ": ", GetParseError_En(doc.GetParseError()));
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!doc.IsObject()) {
|
|
|
|
throw DeadlyImportError("GLTF: JSON document root must be a JSON object");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the buffer instance for the current file embedded contents
|
|
|
|
if (mBodyLength > 0) {
|
|
|
|
if (!mBodyBuffer->LoadFromStream(*stream, mBodyLength, mBodyOffset)) {
|
|
|
|
throw DeadlyImportError("GLTF: Unable to read gltf file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the metadata
|
|
|
|
asset.Read(doc);
|
|
|
|
ReadExtensionsUsed(doc);
|
2019-11-27 23:01:01 +00:00
|
|
|
ReadExtensionsRequired(doc);
|
|
|
|
|
2021-01-26 15:56:49 +00:00
|
|
|
#ifndef ASSIMP_ENABLE_DRACO
|
2021-01-27 09:42:49 +00:00
|
|
|
// Is Draco required?
|
2019-11-27 23:01:01 +00:00
|
|
|
if (extensionsRequired.KHR_draco_mesh_compression) {
|
2021-01-26 15:56:49 +00:00
|
|
|
throw DeadlyImportError("GLTF: Draco mesh compression not supported.");
|
2019-11-27 23:01:01 +00:00
|
|
|
}
|
2021-01-26 15:56:49 +00:00
|
|
|
#endif
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
// Prepare the dictionaries
|
|
|
|
for (size_t i = 0; i < mDicts.size(); ++i) {
|
|
|
|
mDicts[i]->AttachToDocument(doc);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the "scene" property, which specifies which scene to load
|
|
|
|
// and recursively load everything referenced by it
|
2018-05-23 00:32:12 +00:00
|
|
|
unsigned int sceneIndex = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
Value *curScene = FindUInt(doc, "scene");
|
|
|
|
if (nullptr != curScene) {
|
|
|
|
sceneIndex = curScene->GetUint();
|
2018-05-23 00:32:12 +00:00
|
|
|
}
|
2017-08-24 21:31:33 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *scenesArray = FindArray(doc, "scenes")) {
|
2018-05-23 00:32:12 +00:00
|
|
|
if (sceneIndex < scenesArray->Size()) {
|
|
|
|
this->scene = scenes.Retrieve(sceneIndex);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *skinsArray = FindArray(doc, "skins")) {
|
2018-11-27 22:22:09 +00:00
|
|
|
for (unsigned int i = 0; i < skinsArray->Size(); ++i) {
|
|
|
|
skins.Retrieve(i);
|
|
|
|
}
|
2018-11-27 11:45:53 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (Value *animsArray = FindArray(doc, "animations")) {
|
2018-10-26 22:36:34 +00:00
|
|
|
for (unsigned int i = 0; i < animsArray->Size(); ++i) {
|
|
|
|
animations.Retrieve(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
// Clean up
|
|
|
|
for (size_t i = 0; i < mDicts.size(); ++i) {
|
|
|
|
mDicts[i]->DetachFromDocument();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Asset::SetAsBinary() {
|
2017-11-20 13:59:05 +00:00
|
|
|
if (!mBodyBuffer) {
|
|
|
|
mBodyBuffer = buffers.Create("binary_glTF");
|
|
|
|
mBodyBuffer->MarkAsSpecial();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 23:01:01 +00:00
|
|
|
// As required extensions are only a concept in glTF 2.0, this is here
|
|
|
|
// instead of glTFCommon.h
|
|
|
|
#define CHECK_REQUIRED_EXT(EXT) \
|
2020-03-08 20:24:01 +00:00
|
|
|
if (exts.find(#EXT) != exts.end()) extensionsRequired.EXT = true;
|
2019-11-27 23:01:01 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Asset::ReadExtensionsRequired(Document &doc) {
|
|
|
|
Value *extsRequired = FindArray(doc, "extensionsRequired");
|
2019-12-01 11:34:45 +00:00
|
|
|
if (nullptr == extsRequired) {
|
2020-03-08 20:24:01 +00:00
|
|
|
return;
|
2019-12-01 11:34:45 +00:00
|
|
|
}
|
2019-11-27 23:01:01 +00:00
|
|
|
|
|
|
|
std::gltf_unordered_map<std::string, bool> exts;
|
|
|
|
for (unsigned int i = 0; i < extsRequired->Size(); ++i) {
|
|
|
|
if ((*extsRequired)[i].IsString()) {
|
|
|
|
exts[(*extsRequired)[i].GetString()] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_REQUIRED_EXT(KHR_draco_mesh_compression);
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#undef CHECK_REQUIRED_EXT
|
2019-11-27 23:01:01 +00:00
|
|
|
}
|
2017-11-20 13:59:05 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline void Asset::ReadExtensionsUsed(Document &doc) {
|
|
|
|
Value *extsUsed = FindArray(doc, "extensionsUsed");
|
2017-07-19 20:21:43 +00:00
|
|
|
if (!extsUsed) return;
|
|
|
|
|
|
|
|
std::gltf_unordered_map<std::string, bool> exts;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < extsUsed->Size(); ++i) {
|
|
|
|
if ((*extsUsed)[i].IsString()) {
|
|
|
|
exts[(*extsUsed)[i].GetString()] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 05:35:10 +00:00
|
|
|
CHECK_EXT(KHR_materials_pbrSpecularGlossiness);
|
2018-05-18 21:01:25 +00:00
|
|
|
CHECK_EXT(KHR_materials_unlit);
|
2019-08-03 18:51:00 +00:00
|
|
|
CHECK_EXT(KHR_lights_punctual);
|
2020-03-08 20:24:01 +00:00
|
|
|
CHECK_EXT(KHR_texture_transform);
|
2020-12-20 06:59:12 +00:00
|
|
|
CHECK_EXT(KHR_materials_sheen);
|
2020-12-23 07:12:09 +00:00
|
|
|
CHECK_EXT(KHR_materials_clearcoat);
|
2020-12-23 09:43:01 +00:00
|
|
|
CHECK_EXT(KHR_materials_transmission);
|
2021-01-26 15:56:49 +00:00
|
|
|
CHECK_EXT(KHR_draco_mesh_compression);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#undef CHECK_EXT
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline IOStream *Asset::OpenFile(std::string path, const char *mode, bool /*absolute*/) {
|
|
|
|
#ifdef ASSIMP_API
|
|
|
|
return mIOSystem->Open(path, mode);
|
|
|
|
#else
|
2021-01-27 10:56:57 +00:00
|
|
|
if (path.size() < 2) return nullptr;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!absolute && path[1] != ':' && path[0] != '/') { // relative?
|
|
|
|
path = mCurrentAssetDir + path;
|
|
|
|
}
|
|
|
|
FILE *f = fopen(path.c_str(), mode);
|
2021-01-27 10:56:57 +00:00
|
|
|
return f ? new IOStream(f) : nullptr;
|
2020-03-08 20:24:01 +00:00
|
|
|
#endif
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
inline std::string Asset::FindUniqueID(const std::string &str, const char *suffix) {
|
2017-07-19 20:21:43 +00:00
|
|
|
std::string id = str;
|
|
|
|
|
|
|
|
if (!id.empty()) {
|
|
|
|
if (mUsedIds.find(id) == mUsedIds.end())
|
|
|
|
return id;
|
|
|
|
|
|
|
|
id += "_";
|
|
|
|
}
|
|
|
|
|
|
|
|
id += suffix;
|
|
|
|
|
|
|
|
Asset::IdMap::iterator it = mUsedIds.find(id);
|
2020-03-15 10:47:05 +00:00
|
|
|
if (it == mUsedIds.end()) {
|
2017-07-19 20:21:43 +00:00
|
|
|
return id;
|
2020-03-15 10:47:05 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2017-09-28 01:41:35 +00:00
|
|
|
std::vector<char> buffer;
|
|
|
|
buffer.resize(id.size() + 16);
|
|
|
|
int offset = ai_snprintf(buffer.data(), buffer.size(), "%s_", id.c_str());
|
2017-07-19 20:21:43 +00:00
|
|
|
for (int i = 0; it != mUsedIds.end(); ++i) {
|
2017-09-28 01:41:35 +00:00
|
|
|
ai_snprintf(buffer.data() + offset, buffer.size() - offset, "%d", i);
|
|
|
|
id = buffer.data();
|
2017-07-19 20:21:43 +00:00
|
|
|
it = mUsedIds.find(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2020-07-15 02:19:07 +00:00
|
|
|
#if _MSC_VER
|
2021-01-27 09:55:03 +00:00
|
|
|
#pragma warning(pop)
|
2020-07-15 02:19:07 +00:00
|
|
|
#endif // _MSC_VER
|
2020-03-08 20:24:01 +00:00
|
|
|
|
|
|
|
} // namespace glTF2
|