2020-06-12 00:37:06 +00:00
|
|
|
/*
|
2017-07-19 20:21:43 +00:00
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, 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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2022-01-02 18:47:43 +00:00
|
|
|
#include <assimp/Base64.hpp>
|
2017-07-19 20:21:43 +00:00
|
|
|
#include <rapidjson/stringbuffer.h>
|
|
|
|
#include <rapidjson/writer.h>
|
|
|
|
#include <rapidjson/prettywriter.h>
|
|
|
|
|
2017-07-20 17:59:21 +00:00
|
|
|
namespace glTF2 {
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
using rapidjson::StringBuffer;
|
|
|
|
using rapidjson::PrettyWriter;
|
|
|
|
using rapidjson::Writer;
|
|
|
|
using rapidjson::StringRef;
|
|
|
|
using rapidjson::StringRef;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2019-11-22 05:46:14 +00:00
|
|
|
template<typename T, size_t N>
|
|
|
|
inline Value& MakeValue(Value& val, T(&r)[N], MemoryPoolAllocator<>& al) {
|
2017-07-19 20:21:43 +00:00
|
|
|
val.SetArray();
|
|
|
|
val.Reserve(N, al);
|
|
|
|
for (decltype(N) i = 0; i < N; ++i) {
|
|
|
|
val.PushBack(r[i], al);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-11-22 05:46:14 +00:00
|
|
|
template<typename T>
|
|
|
|
inline Value& MakeValue(Value& val, const std::vector<T> & r, MemoryPoolAllocator<>& al) {
|
2017-07-19 20:21:43 +00:00
|
|
|
val.SetArray();
|
|
|
|
val.Reserve(static_cast<rapidjson::SizeType>(r.size()), al);
|
|
|
|
for (unsigned int i = 0; i < r.size(); ++i) {
|
|
|
|
val.PushBack(r[i], al);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-11-22 05:46:14 +00:00
|
|
|
template<typename C, typename T>
|
|
|
|
inline Value& MakeValueCast(Value& val, const std::vector<T> & r, MemoryPoolAllocator<>& al) {
|
|
|
|
val.SetArray();
|
|
|
|
val.Reserve(static_cast<rapidjson::SizeType>(r.size()), al);
|
|
|
|
for (unsigned int i = 0; i < r.size(); ++i) {
|
|
|
|
val.PushBack(static_cast<C>(r[i]), al);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline Value& MakeValue(Value& val, T r, MemoryPoolAllocator<>& /*al*/) {
|
|
|
|
val.Set(r);
|
2017-08-30 21:25:11 +00:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
template<class T>
|
|
|
|
inline void AddRefsVector(Value& obj, const char* fieldId, std::vector< Ref<T> >& v, MemoryPoolAllocator<>& al) {
|
|
|
|
if (v.empty()) return;
|
|
|
|
Value lst;
|
|
|
|
lst.SetArray();
|
|
|
|
lst.Reserve(unsigned(v.size()), al);
|
|
|
|
for (size_t i = 0; i < v.size(); ++i) {
|
2017-07-26 00:26:18 +00:00
|
|
|
lst.PushBack(v[i]->index, al);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
obj.AddMember(StringRef(fieldId), lst, al);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Accessor& a, AssetWriter& w)
|
|
|
|
{
|
2020-05-15 19:20:31 +00:00
|
|
|
if (a.bufferView) {
|
|
|
|
obj.AddMember("bufferView", a.bufferView->index, w.mAl);
|
|
|
|
obj.AddMember("byteOffset", (unsigned int)a.byteOffset, w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
obj.AddMember("componentType", int(a.componentType), w.mAl);
|
2019-02-06 19:14:22 +00:00
|
|
|
obj.AddMember("count", (unsigned int)a.count, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
obj.AddMember("type", StringRef(AttribType::ToString(a.type)), w.mAl);
|
2021-02-03 23:05:58 +00:00
|
|
|
Value vTmpMax, vTmpMin;
|
|
|
|
if (a.componentType == ComponentType_FLOAT) {
|
|
|
|
obj.AddMember("max", MakeValue(vTmpMax, a.max, w.mAl), w.mAl);
|
|
|
|
obj.AddMember("min", MakeValue(vTmpMin, a.min, w.mAl), w.mAl);
|
|
|
|
} else {
|
|
|
|
obj.AddMember("max", MakeValueCast<int64_t>(vTmpMax, a.max, w.mAl), w.mAl);
|
|
|
|
obj.AddMember("min", MakeValueCast<int64_t>(vTmpMin, a.min, w.mAl), w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2020-05-15 19:20:31 +00:00
|
|
|
if (a.sparse) {
|
|
|
|
Value sparseValue;
|
|
|
|
sparseValue.SetObject();
|
|
|
|
|
|
|
|
//count
|
|
|
|
sparseValue.AddMember("count", (unsigned int)a.sparse->count, w.mAl);
|
|
|
|
|
|
|
|
//indices
|
|
|
|
Value indices;
|
|
|
|
indices.SetObject();
|
|
|
|
indices.AddMember("bufferView", a.sparse->indices->index, w.mAl);
|
|
|
|
indices.AddMember("byteOffset", (unsigned int)a.sparse->indicesByteOffset, w.mAl);
|
|
|
|
indices.AddMember("componentType", int(a.sparse->indicesType), w.mAl);
|
|
|
|
sparseValue.AddMember("indices", indices, w.mAl);
|
|
|
|
|
|
|
|
//values
|
|
|
|
Value values;
|
|
|
|
values.SetObject();
|
|
|
|
values.AddMember("bufferView", a.sparse->values->index, w.mAl);
|
|
|
|
values.AddMember("byteOffset", (unsigned int)a.sparse->valuesByteOffset, w.mAl);
|
|
|
|
sparseValue.AddMember("values", values, w.mAl);
|
|
|
|
|
|
|
|
obj.AddMember("sparse", sparseValue, w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Animation& a, AssetWriter& w)
|
|
|
|
{
|
|
|
|
/****************** Channels *******************/
|
|
|
|
Value channels;
|
|
|
|
channels.SetArray();
|
2018-10-26 22:36:34 +00:00
|
|
|
channels.Reserve(unsigned(a.channels.size()), w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2018-10-26 22:36:34 +00:00
|
|
|
for (size_t i = 0; i < unsigned(a.channels.size()); ++i) {
|
|
|
|
Animation::Channel& c = a.channels[i];
|
2017-07-19 20:21:43 +00:00
|
|
|
Value valChannel;
|
|
|
|
valChannel.SetObject();
|
|
|
|
{
|
2017-07-26 21:20:53 +00:00
|
|
|
valChannel.AddMember("sampler", c.sampler, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
Value valTarget;
|
|
|
|
valTarget.SetObject();
|
|
|
|
{
|
2017-07-26 21:20:53 +00:00
|
|
|
valTarget.AddMember("node", c.target.node->index, w.mAl);
|
2018-10-26 22:36:34 +00:00
|
|
|
switch (c.target.path) {
|
|
|
|
case AnimationPath_TRANSLATION:
|
|
|
|
valTarget.AddMember("path", "translation", w.mAl);
|
|
|
|
break;
|
|
|
|
case AnimationPath_ROTATION:
|
|
|
|
valTarget.AddMember("path", "rotation", w.mAl);
|
|
|
|
break;
|
|
|
|
case AnimationPath_SCALE:
|
|
|
|
valTarget.AddMember("path", "scale", w.mAl);
|
|
|
|
break;
|
|
|
|
case AnimationPath_WEIGHTS:
|
|
|
|
valTarget.AddMember("path", "weights", w.mAl);
|
|
|
|
break;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
valChannel.AddMember("target", valTarget, w.mAl);
|
|
|
|
}
|
|
|
|
channels.PushBack(valChannel, w.mAl);
|
|
|
|
}
|
|
|
|
obj.AddMember("channels", channels, w.mAl);
|
|
|
|
|
|
|
|
/****************** Samplers *******************/
|
|
|
|
Value valSamplers;
|
2017-07-26 00:26:18 +00:00
|
|
|
valSamplers.SetArray();
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2018-10-26 22:36:34 +00:00
|
|
|
for (size_t i = 0; i < unsigned(a.samplers.size()); ++i) {
|
|
|
|
Animation::Sampler& s = a.samplers[i];
|
2017-07-19 20:21:43 +00:00
|
|
|
Value valSampler;
|
|
|
|
valSampler.SetObject();
|
|
|
|
{
|
2018-10-26 22:36:34 +00:00
|
|
|
valSampler.AddMember("input", s.input->index, w.mAl);
|
|
|
|
switch (s.interpolation) {
|
|
|
|
case Interpolation_LINEAR:
|
2020-02-10 02:03:26 +00:00
|
|
|
valSampler.AddMember("interpolation", "LINEAR", w.mAl);
|
2018-10-26 22:36:34 +00:00
|
|
|
break;
|
|
|
|
case Interpolation_STEP:
|
2020-02-10 02:03:26 +00:00
|
|
|
valSampler.AddMember("interpolation", "STEP", w.mAl);
|
2018-10-26 22:36:34 +00:00
|
|
|
break;
|
|
|
|
case Interpolation_CUBICSPLINE:
|
2020-02-10 02:03:26 +00:00
|
|
|
valSampler.AddMember("interpolation", "CUBICSPLINE", w.mAl);
|
2018-10-26 22:36:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
valSampler.AddMember("output", s.output->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2017-07-26 00:26:18 +00:00
|
|
|
valSamplers.PushBack(valSampler, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
obj.AddMember("samplers", valSamplers, w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Buffer& b, AssetWriter& w)
|
|
|
|
{
|
|
|
|
obj.AddMember("byteLength", static_cast<uint64_t>(b.byteLength), w.mAl);
|
2018-01-05 09:08:17 +00:00
|
|
|
|
|
|
|
const auto uri = b.GetURI();
|
|
|
|
const auto relativeUri = uri.substr(uri.find_last_of("/\\") + 1u);
|
|
|
|
obj.AddMember("uri", Value(relativeUri, w.mAl).Move(), w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, BufferView& bv, AssetWriter& w)
|
|
|
|
{
|
2017-07-26 00:26:18 +00:00
|
|
|
obj.AddMember("buffer", bv.buffer->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
obj.AddMember("byteOffset", static_cast<uint64_t>(bv.byteOffset), w.mAl);
|
|
|
|
obj.AddMember("byteLength", static_cast<uint64_t>(bv.byteLength), w.mAl);
|
2017-11-20 18:01:28 +00:00
|
|
|
if (bv.byteStride != 0) {
|
|
|
|
obj.AddMember("byteStride", bv.byteStride, w.mAl);
|
|
|
|
}
|
2020-02-10 02:03:26 +00:00
|
|
|
if (bv.target != BufferViewTarget_NONE) {
|
2018-01-05 09:28:12 +00:00
|
|
|
obj.AddMember("target", int(bv.target), w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 10:13:52 +00:00
|
|
|
inline void Write(Value& /*obj*/, Camera& /*c*/, AssetWriter& /*w*/)
|
2017-07-19 20:21:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-03 18:51:00 +00:00
|
|
|
inline void Write(Value& /*obj*/, Light& /*c*/, AssetWriter& /*w*/)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
inline void Write(Value& obj, Image& img, AssetWriter& w)
|
|
|
|
{
|
2021-05-06 22:10:06 +00:00
|
|
|
//basisu: no need to handle .ktx2, .basis, write as is
|
2018-01-05 09:28:12 +00:00
|
|
|
if (img.bufferView) {
|
|
|
|
obj.AddMember("bufferView", img.bufferView->index, w.mAl);
|
|
|
|
obj.AddMember("mimeType", Value(img.mimeType, w.mAl).Move(), w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-01-05 09:28:12 +00:00
|
|
|
std::string uri;
|
|
|
|
if (img.HasData()) {
|
|
|
|
uri = "data:" + (img.mimeType.empty() ? "application/octet-stream" : img.mimeType);
|
|
|
|
uri += ";base64,";
|
2022-01-02 18:47:43 +00:00
|
|
|
Base64::Encode(img.GetData(), img.GetDataLength(), uri);
|
2018-01-05 09:28:12 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
uri = img.uri;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
2018-01-05 09:28:12 +00:00
|
|
|
obj.AddMember("uri", Value(uri, w.mAl).Move(), w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2017-08-30 21:25:11 +00:00
|
|
|
inline void SetTexBasic(TextureInfo t, Value& tex, MemoryPoolAllocator<>& al)
|
|
|
|
{
|
|
|
|
tex.SetObject();
|
|
|
|
tex.AddMember("index", t.texture->index, al);
|
|
|
|
|
|
|
|
if (t.texCoord != 0) {
|
|
|
|
tex.AddMember("texCoord", t.texCoord, al);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void WriteTex(Value& obj, TextureInfo t, const char* propName, MemoryPoolAllocator<>& al)
|
2017-07-19 20:21:43 +00:00
|
|
|
{
|
2017-08-30 21:25:11 +00:00
|
|
|
|
|
|
|
if (t.texture) {
|
2017-07-26 00:26:18 +00:00
|
|
|
Value tex;
|
2017-08-30 21:25:11 +00:00
|
|
|
|
|
|
|
SetTexBasic(t, tex, al);
|
|
|
|
|
2017-07-26 00:26:18 +00:00
|
|
|
obj.AddMember(StringRef(propName), tex, al);
|
2017-07-26 21:42:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-30 21:25:11 +00:00
|
|
|
inline void WriteTex(Value& obj, NormalTextureInfo t, const char* propName, MemoryPoolAllocator<>& al)
|
2017-07-26 21:42:22 +00:00
|
|
|
{
|
2017-08-30 21:25:11 +00:00
|
|
|
|
|
|
|
if (t.texture) {
|
|
|
|
Value tex;
|
|
|
|
|
|
|
|
SetTexBasic(t, tex, al);
|
|
|
|
|
|
|
|
if (t.scale != 1) {
|
|
|
|
tex.AddMember("scale", t.scale, al);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.AddMember(StringRef(propName), tex, al);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-30 21:25:11 +00:00
|
|
|
|
|
|
|
inline void WriteTex(Value& obj, OcclusionTextureInfo t, const char* propName, MemoryPoolAllocator<>& al)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (t.texture) {
|
|
|
|
Value tex;
|
|
|
|
|
|
|
|
SetTexBasic(t, tex, al);
|
|
|
|
|
|
|
|
if (t.strength != 1) {
|
|
|
|
tex.AddMember("strength", t.strength, al);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.AddMember(StringRef(propName), tex, al);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
inline void WriteVec(Value& obj, float(&prop)[N], const char* propName, MemoryPoolAllocator<>& al)
|
|
|
|
{
|
|
|
|
Value arr;
|
|
|
|
obj.AddMember(StringRef(propName), MakeValue(arr, prop, al), al);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N>
|
2017-09-05 19:45:32 +00:00
|
|
|
inline void WriteVec(Value& obj, float(&prop)[N], const char* propName, const float(&defaultVal)[N], MemoryPoolAllocator<>& al)
|
2017-08-30 21:25:11 +00:00
|
|
|
{
|
|
|
|
if (!std::equal(std::begin(prop), std::end(prop), std::begin(defaultVal))) {
|
|
|
|
WriteVec(obj, prop, propName, al);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void WriteFloat(Value& obj, float prop, const char* propName, MemoryPoolAllocator<>& al)
|
|
|
|
{
|
|
|
|
Value num;
|
|
|
|
obj.AddMember(StringRef(propName), MakeValue(num, prop, al), al);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Material& m, AssetWriter& w)
|
|
|
|
{
|
2017-08-30 21:25:11 +00:00
|
|
|
Value pbrMetallicRoughness;
|
|
|
|
pbrMetallicRoughness.SetObject();
|
|
|
|
{
|
2017-08-31 22:30:43 +00:00
|
|
|
WriteTex(pbrMetallicRoughness, m.pbrMetallicRoughness.baseColorTexture, "baseColorTexture", w.mAl);
|
|
|
|
WriteTex(pbrMetallicRoughness, m.pbrMetallicRoughness.metallicRoughnessTexture, "metallicRoughnessTexture", w.mAl);
|
2017-09-05 19:45:32 +00:00
|
|
|
WriteVec(pbrMetallicRoughness, m.pbrMetallicRoughness.baseColorFactor, "baseColorFactor", defaultBaseColor, w.mAl);
|
2017-08-30 21:25:11 +00:00
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
if (m.pbrMetallicRoughness.metallicFactor != 1) {
|
|
|
|
WriteFloat(pbrMetallicRoughness, m.pbrMetallicRoughness.metallicFactor, "metallicFactor", w.mAl);
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
if (m.pbrMetallicRoughness.roughnessFactor != 1) {
|
|
|
|
WriteFloat(pbrMetallicRoughness, m.pbrMetallicRoughness.roughnessFactor, "roughnessFactor", w.mAl);
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
2017-07-26 21:39:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 01:50:06 +00:00
|
|
|
if (!pbrMetallicRoughness.ObjectEmpty()) {
|
2017-08-30 21:25:11 +00:00
|
|
|
obj.AddMember("pbrMetallicRoughness", pbrMetallicRoughness, w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteTex(obj, m.normalTexture, "normalTexture", w.mAl);
|
|
|
|
WriteTex(obj, m.emissiveTexture, "emissiveTexture", w.mAl);
|
|
|
|
WriteTex(obj, m.occlusionTexture, "occlusionTexture", w.mAl);
|
|
|
|
WriteVec(obj, m.emissiveFactor, "emissiveFactor", defaultEmissiveFactor, w.mAl);
|
|
|
|
|
|
|
|
if (m.alphaCutoff != 0.5) {
|
|
|
|
WriteFloat(obj, m.alphaCutoff, "alphaCutoff", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m.alphaMode != "OPAQUE") {
|
2017-09-12 15:55:22 +00:00
|
|
|
obj.AddMember("alphaMode", Value(m.alphaMode, w.mAl).Move(), w.mAl);
|
2017-08-30 21:25:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m.doubleSided) {
|
|
|
|
obj.AddMember("doubleSided", m.doubleSided, w.mAl);
|
|
|
|
}
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
Value exts;
|
|
|
|
exts.SetObject();
|
2017-08-31 05:35:10 +00:00
|
|
|
|
2017-09-05 20:29:00 +00:00
|
|
|
if (m.pbrSpecularGlossiness.isPresent) {
|
2017-08-31 22:30:43 +00:00
|
|
|
Value pbrSpecularGlossiness;
|
|
|
|
pbrSpecularGlossiness.SetObject();
|
2017-08-31 05:35:10 +00:00
|
|
|
|
2017-09-05 20:29:00 +00:00
|
|
|
PbrSpecularGlossiness &pbrSG = m.pbrSpecularGlossiness.value;
|
2017-08-31 05:35:10 +00:00
|
|
|
|
2017-09-05 20:29:00 +00:00
|
|
|
//pbrSpecularGlossiness
|
|
|
|
WriteVec(pbrSpecularGlossiness, pbrSG.diffuseFactor, "diffuseFactor", defaultDiffuseFactor, w.mAl);
|
|
|
|
WriteVec(pbrSpecularGlossiness, pbrSG.specularFactor, "specularFactor", defaultSpecularFactor, w.mAl);
|
|
|
|
|
|
|
|
if (pbrSG.glossinessFactor != 1) {
|
2019-10-25 01:19:28 +00:00
|
|
|
WriteFloat(pbrSpecularGlossiness, pbrSG.glossinessFactor, "glossinessFactor", w.mAl);
|
2017-08-31 22:30:43 +00:00
|
|
|
}
|
2017-08-31 05:35:10 +00:00
|
|
|
|
2017-09-05 20:29:00 +00:00
|
|
|
WriteTex(pbrSpecularGlossiness, pbrSG.diffuseTexture, "diffuseTexture", w.mAl);
|
|
|
|
WriteTex(pbrSpecularGlossiness, pbrSG.specularGlossinessTexture, "specularGlossinessTexture", w.mAl);
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
if (!pbrSpecularGlossiness.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_pbrSpecularGlossiness", pbrSpecularGlossiness, w.mAl);
|
|
|
|
}
|
2017-08-31 05:35:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 21:01:25 +00:00
|
|
|
if (m.unlit) {
|
|
|
|
Value unlit;
|
|
|
|
unlit.SetObject();
|
|
|
|
exts.AddMember("KHR_materials_unlit", unlit, w.mAl);
|
|
|
|
}
|
|
|
|
|
2022-11-08 21:09:50 +00:00
|
|
|
if (m.materialSpecular.isPresent) {
|
|
|
|
Value materialSpecular(rapidjson::Type::kObjectType);
|
|
|
|
materialSpecular.SetObject();
|
|
|
|
|
|
|
|
MaterialSpecular &specular = m.materialSpecular.value;
|
|
|
|
|
2023-03-26 15:13:16 +00:00
|
|
|
if (specular.specularFactor != 0.0f) {
|
2022-11-08 21:09:50 +00:00
|
|
|
WriteFloat(materialSpecular, specular.specularFactor, "specularFactor", w.mAl);
|
|
|
|
WriteTex(materialSpecular, specular.specularTexture, "specularTexture", w.mAl);
|
|
|
|
}
|
|
|
|
if (specular.specularColorFactor[0] != defaultSpecularColorFactor[0] && specular.specularColorFactor[1] != defaultSpecularColorFactor[1] && specular.specularColorFactor[2] != defaultSpecularColorFactor[2]) {
|
|
|
|
WriteVec(materialSpecular, specular.specularColorFactor, "specularColorFactor", w.mAl);
|
|
|
|
WriteTex(materialSpecular, specular.specularColorTexture, "specularColorTexture", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!materialSpecular.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_specular", materialSpecular, w.mAl);
|
2022-11-18 15:24:37 +00:00
|
|
|
}
|
2022-11-08 21:09:50 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 13:34:16 +00:00
|
|
|
if (m.materialSheen.isPresent) {
|
|
|
|
Value materialSheen(rapidjson::Type::kObjectType);
|
|
|
|
|
|
|
|
MaterialSheen &sheen = m.materialSheen.value;
|
|
|
|
|
|
|
|
WriteVec(materialSheen, sheen.sheenColorFactor, "sheenColorFactor", defaultSheenFactor, w.mAl);
|
|
|
|
|
|
|
|
if (sheen.sheenRoughnessFactor != 0.f) {
|
|
|
|
WriteFloat(materialSheen, sheen.sheenRoughnessFactor, "sheenRoughnessFactor", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteTex(materialSheen, sheen.sheenColorTexture, "sheenColorTexture", w.mAl);
|
|
|
|
WriteTex(materialSheen, sheen.sheenRoughnessTexture, "sheenRoughnessTexture", w.mAl);
|
|
|
|
|
|
|
|
if (!materialSheen.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_sheen", materialSheen, w.mAl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 08:56:15 +00:00
|
|
|
if (m.materialClearcoat.isPresent) {
|
|
|
|
Value materialClearcoat(rapidjson::Type::kObjectType);
|
|
|
|
|
|
|
|
MaterialClearcoat &clearcoat = m.materialClearcoat.value;
|
|
|
|
|
|
|
|
if (clearcoat.clearcoatFactor != 0.f) {
|
|
|
|
WriteFloat(materialClearcoat, clearcoat.clearcoatFactor, "clearcoatFactor", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clearcoat.clearcoatRoughnessFactor != 0.f) {
|
|
|
|
WriteFloat(materialClearcoat, clearcoat.clearcoatRoughnessFactor, "clearcoatRoughnessFactor", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteTex(materialClearcoat, clearcoat.clearcoatTexture, "clearcoatTexture", w.mAl);
|
|
|
|
WriteTex(materialClearcoat, clearcoat.clearcoatRoughnessTexture, "clearcoatRoughnessTexture", w.mAl);
|
|
|
|
WriteTex(materialClearcoat, clearcoat.clearcoatNormalTexture, "clearcoatNormalTexture", w.mAl);
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2020-12-23 08:56:15 +00:00
|
|
|
if (!materialClearcoat.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_clearcoat", materialClearcoat, w.mAl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-23 09:43:21 +00:00
|
|
|
if (m.materialTransmission.isPresent) {
|
|
|
|
Value materialTransmission(rapidjson::Type::kObjectType);
|
|
|
|
|
|
|
|
MaterialTransmission &transmission = m.materialTransmission.value;
|
|
|
|
|
|
|
|
if (transmission.transmissionFactor != 0.f) {
|
|
|
|
WriteFloat(materialTransmission, transmission.transmissionFactor, "transmissionFactor", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteTex(materialTransmission, transmission.transmissionTexture, "transmissionTexture", w.mAl);
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2020-12-23 09:43:21 +00:00
|
|
|
if (!materialTransmission.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_transmission", materialTransmission, w.mAl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 07:10:02 +00:00
|
|
|
if (m.materialVolume.isPresent) {
|
|
|
|
Value materialVolume(rapidjson::Type::kObjectType);
|
|
|
|
|
|
|
|
MaterialVolume &volume = m.materialVolume.value;
|
|
|
|
|
|
|
|
if (volume.thicknessFactor != 0.f) {
|
|
|
|
WriteFloat(materialVolume, volume.thicknessFactor, "thicknessFactor", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteTex(materialVolume, volume.thicknessTexture, "thicknessTexture", w.mAl);
|
|
|
|
|
|
|
|
if (volume.attenuationDistance != INFINITY) {
|
|
|
|
WriteFloat(materialVolume, volume.attenuationDistance, "attenuationDistance", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
WriteVec(materialVolume, volume.attenuationColor, "attenuationColor", defaultAttenuationColor, w.mAl);
|
|
|
|
|
|
|
|
if (!materialVolume.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_volume", materialVolume, w.mAl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 08:36:53 +00:00
|
|
|
if (m.materialIOR.isPresent) {
|
|
|
|
Value materialIOR(rapidjson::Type::kObjectType);
|
|
|
|
|
|
|
|
MaterialIOR &ior = m.materialIOR.value;
|
|
|
|
|
|
|
|
if (ior.ior != 1.5f) {
|
|
|
|
WriteFloat(materialIOR, ior.ior, "ior", w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!materialIOR.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_ior", materialIOR, w.mAl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-04 16:37:28 +00:00
|
|
|
if (m.materialEmissiveStrength.isPresent) {
|
2022-11-03 21:11:21 +00:00
|
|
|
Value materialEmissiveStrength(rapidjson::Type::kObjectType);
|
|
|
|
|
|
|
|
MaterialEmissiveStrength &emissiveStrength = m.materialEmissiveStrength.value;
|
|
|
|
|
|
|
|
if (emissiveStrength.emissiveStrength != 0.f) {
|
|
|
|
WriteFloat(materialEmissiveStrength, emissiveStrength.emissiveStrength, "emissiveStrength", w.mAl);
|
|
|
|
}
|
|
|
|
|
2022-11-04 16:37:28 +00:00
|
|
|
if (!materialEmissiveStrength.ObjectEmpty()) {
|
|
|
|
exts.AddMember("KHR_materials_emissive_strength", materialEmissiveStrength, w.mAl);
|
2022-11-03 21:11:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 22:30:43 +00:00
|
|
|
if (!exts.ObjectEmpty()) {
|
|
|
|
obj.AddMember("extensions", exts, w.mAl);
|
2017-08-31 05:35:10 +00:00
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
inline void WriteAttrs(AssetWriter& w, Value& attrs, Mesh::AccessorList& lst,
|
|
|
|
const char* semantic, bool forceNumber = false)
|
|
|
|
{
|
|
|
|
if (lst.empty()) return;
|
|
|
|
if (lst.size() == 1 && !forceNumber) {
|
2017-07-26 00:26:18 +00:00
|
|
|
attrs.AddMember(StringRef(semantic), lst[0]->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (size_t i = 0; i < lst.size(); ++i) {
|
|
|
|
char buffer[32];
|
|
|
|
ai_snprintf(buffer, 32, "%s_%d", semantic, int(i));
|
2017-07-26 00:26:18 +00:00
|
|
|
attrs.AddMember(Value(buffer, w.mAl).Move(), lst[i]->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Mesh& m, AssetWriter& w)
|
|
|
|
{
|
2022-11-08 21:09:50 +00:00
|
|
|
/****************** Primitives *******************/
|
2017-07-19 20:21:43 +00:00
|
|
|
Value primitives;
|
|
|
|
primitives.SetArray();
|
|
|
|
primitives.Reserve(unsigned(m.primitives.size()), w.mAl);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m.primitives.size(); ++i) {
|
|
|
|
Mesh::Primitive& p = m.primitives[i];
|
|
|
|
Value prim;
|
|
|
|
prim.SetObject();
|
2021-03-10 08:48:12 +00:00
|
|
|
|
|
|
|
// Extensions
|
2021-03-29 17:03:01 +00:00
|
|
|
if (p.ngonEncoded)
|
2021-03-10 08:48:12 +00:00
|
|
|
{
|
|
|
|
Value exts;
|
|
|
|
exts.SetObject();
|
|
|
|
|
|
|
|
Value FB_ngon_encoding;
|
|
|
|
FB_ngon_encoding.SetObject();
|
|
|
|
|
|
|
|
exts.AddMember(StringRef("FB_ngon_encoding"), FB_ngon_encoding, w.mAl);
|
|
|
|
prim.AddMember("extensions", exts, w.mAl);
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
{
|
|
|
|
prim.AddMember("mode", Value(int(p.mode)).Move(), w.mAl);
|
|
|
|
|
|
|
|
if (p.material)
|
2017-07-26 00:26:18 +00:00
|
|
|
prim.AddMember("material", p.material->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (p.indices)
|
2017-07-26 00:26:18 +00:00
|
|
|
prim.AddMember("indices", p.indices->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
Value attrs;
|
|
|
|
attrs.SetObject();
|
|
|
|
{
|
|
|
|
WriteAttrs(w, attrs, p.attributes.position, "POSITION");
|
|
|
|
WriteAttrs(w, attrs, p.attributes.normal, "NORMAL");
|
2017-09-08 19:58:09 +00:00
|
|
|
WriteAttrs(w, attrs, p.attributes.texcoord, "TEXCOORD", true);
|
|
|
|
WriteAttrs(w, attrs, p.attributes.color, "COLOR", true);
|
|
|
|
WriteAttrs(w, attrs, p.attributes.joint, "JOINTS", true);
|
|
|
|
WriteAttrs(w, attrs, p.attributes.weight, "WEIGHTS", true);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
prim.AddMember("attributes", attrs, w.mAl);
|
2020-03-12 19:14:17 +00:00
|
|
|
|
|
|
|
// targets for blendshapes
|
|
|
|
if (p.targets.size() > 0) {
|
|
|
|
Value tjs;
|
|
|
|
tjs.SetArray();
|
|
|
|
tjs.Reserve(unsigned(p.targets.size()), w.mAl);
|
|
|
|
for (unsigned int t = 0; t < p.targets.size(); ++t) {
|
|
|
|
Value tj;
|
|
|
|
tj.SetObject();
|
|
|
|
{
|
|
|
|
WriteAttrs(w, tj, p.targets[t].position, "POSITION");
|
|
|
|
WriteAttrs(w, tj, p.targets[t].normal, "NORMAL");
|
|
|
|
WriteAttrs(w, tj, p.targets[t].tangent, "TANGENT");
|
|
|
|
}
|
|
|
|
tjs.PushBack(tj, w.mAl);
|
|
|
|
}
|
|
|
|
prim.AddMember("targets", tjs, w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
primitives.PushBack(prim, w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.AddMember("primitives", primitives, w.mAl);
|
2020-06-05 19:17:27 +00:00
|
|
|
// targetNames
|
|
|
|
if (m.targetNames.size() > 0) {
|
|
|
|
Value extras;
|
|
|
|
extras.SetObject();
|
|
|
|
Value targetNames;
|
|
|
|
targetNames.SetArray();
|
|
|
|
targetNames.Reserve(unsigned(m.targetNames.size()), w.mAl);
|
|
|
|
for (unsigned int n = 0; n < m.targetNames.size(); ++n) {
|
|
|
|
std::string name = m.targetNames[n];
|
|
|
|
Value tname;
|
|
|
|
tname.SetString(name.c_str(), w.mAl);
|
|
|
|
targetNames.PushBack(tname, w.mAl);
|
|
|
|
}
|
|
|
|
extras.AddMember("targetNames", targetNames, w.mAl);
|
|
|
|
obj.AddMember("extras", extras, w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 23:33:05 +00:00
|
|
|
inline void WriteExtrasValue(Value &parent, const CustomExtension &value, AssetWriter &w) {
|
2022-07-21 22:44:51 +00:00
|
|
|
Value valueNode;
|
|
|
|
|
|
|
|
if (value.mStringValue.isPresent) {
|
|
|
|
MakeValue(valueNode, value.mStringValue.value.c_str(), w.mAl);
|
|
|
|
} else if (value.mDoubleValue.isPresent) {
|
|
|
|
MakeValue(valueNode, value.mDoubleValue.value, w.mAl);
|
|
|
|
} else if (value.mUint64Value.isPresent) {
|
|
|
|
MakeValue(valueNode, value.mUint64Value.value, w.mAl);
|
2022-07-21 23:33:05 +00:00
|
|
|
} else if (value.mInt64Value.isPresent) {
|
|
|
|
MakeValue(valueNode, value.mInt64Value.value, w.mAl);
|
2022-07-21 22:44:51 +00:00
|
|
|
} else if (value.mBoolValue.isPresent) {
|
|
|
|
MakeValue(valueNode, value.mBoolValue.value, w.mAl);
|
2022-07-21 23:33:05 +00:00
|
|
|
} else if (value.mValues.isPresent) {
|
2022-07-21 22:44:51 +00:00
|
|
|
valueNode.SetObject();
|
2022-07-21 23:33:05 +00:00
|
|
|
for (auto const &subvalue : value.mValues.value) {
|
2022-07-21 22:44:51 +00:00
|
|
|
WriteExtrasValue(valueNode, subvalue, w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent.AddMember(StringRef(value.name), valueNode, w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void WriteExtras(Value &obj, const Extras &extras, AssetWriter &w) {
|
|
|
|
if (!extras.HasExtras()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value extrasNode;
|
|
|
|
extrasNode.SetObject();
|
|
|
|
|
|
|
|
for (auto const &value : extras.mValues) {
|
|
|
|
WriteExtrasValue(extrasNode, value, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.AddMember("extras", extrasNode, w.mAl);
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
inline void Write(Value& obj, Node& n, AssetWriter& w)
|
|
|
|
{
|
|
|
|
if (n.matrix.isPresent) {
|
|
|
|
Value val;
|
|
|
|
obj.AddMember("matrix", MakeValue(val, n.matrix.value, w.mAl).Move(), w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.translation.isPresent) {
|
|
|
|
Value val;
|
|
|
|
obj.AddMember("translation", MakeValue(val, n.translation.value, w.mAl).Move(), w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.scale.isPresent) {
|
|
|
|
Value val;
|
|
|
|
obj.AddMember("scale", MakeValue(val, n.scale.value, w.mAl).Move(), w.mAl);
|
|
|
|
}
|
|
|
|
if (n.rotation.isPresent) {
|
|
|
|
Value val;
|
|
|
|
obj.AddMember("rotation", MakeValue(val, n.rotation.value, w.mAl).Move(), w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
AddRefsVector(obj, "children", n.children, w.mAl);
|
|
|
|
|
2017-09-17 21:00:57 +00:00
|
|
|
if (!n.meshes.empty()) {
|
|
|
|
obj.AddMember("mesh", n.meshes[0]->index, w.mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (n.skin) {
|
2017-07-26 00:26:18 +00:00
|
|
|
obj.AddMember("skin", n.skin->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2021-02-02 21:19:57 +00:00
|
|
|
//gltf2 spec does not support "skeletons" under node
|
|
|
|
if(n.skeletons.size()) {
|
|
|
|
AddRefsVector(obj, "skeletons", n.skeletons, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2022-07-21 22:44:51 +00:00
|
|
|
|
|
|
|
WriteExtras(obj, n.extras, w);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 10:13:52 +00:00
|
|
|
inline void Write(Value& /*obj*/, Program& /*b*/, AssetWriter& /*w*/)
|
2017-07-19 20:21:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Sampler& b, AssetWriter& w)
|
|
|
|
{
|
2017-09-04 02:09:48 +00:00
|
|
|
if (!b.name.empty()) {
|
|
|
|
obj.AddMember("name", b.name, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2017-09-04 02:09:48 +00:00
|
|
|
|
|
|
|
if (b.wrapS != SamplerWrap::UNSET && b.wrapS != SamplerWrap::Repeat) {
|
|
|
|
obj.AddMember("wrapS", static_cast<unsigned int>(b.wrapS), w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2017-09-04 02:09:48 +00:00
|
|
|
|
|
|
|
if (b.wrapT != SamplerWrap::UNSET && b.wrapT != SamplerWrap::Repeat) {
|
|
|
|
obj.AddMember("wrapT", static_cast<unsigned int>(b.wrapT), w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2017-09-04 02:09:48 +00:00
|
|
|
|
|
|
|
if (b.magFilter != SamplerMagFilter::UNSET) {
|
|
|
|
obj.AddMember("magFilter", static_cast<unsigned int>(b.magFilter), w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b.minFilter != SamplerMinFilter::UNSET) {
|
|
|
|
obj.AddMember("minFilter", static_cast<unsigned int>(b.minFilter), w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& scene, Scene& s, AssetWriter& w)
|
|
|
|
{
|
|
|
|
AddRefsVector(scene, "nodes", s.nodes, w.mAl);
|
|
|
|
}
|
|
|
|
|
2017-11-02 10:13:52 +00:00
|
|
|
inline void Write(Value& /*obj*/, Shader& /*b*/, AssetWriter& /*w*/)
|
2017-07-19 20:21:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Skin& b, AssetWriter& w)
|
|
|
|
{
|
|
|
|
/****************** jointNames *******************/
|
|
|
|
Value vJointNames;
|
|
|
|
vJointNames.SetArray();
|
|
|
|
vJointNames.Reserve(unsigned(b.jointNames.size()), w.mAl);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < unsigned(b.jointNames.size()); ++i) {
|
2017-07-26 00:26:18 +00:00
|
|
|
vJointNames.PushBack(b.jointNames[i]->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2017-07-26 00:26:18 +00:00
|
|
|
obj.AddMember("joints", vJointNames, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (b.bindShapeMatrix.isPresent) {
|
|
|
|
Value val;
|
|
|
|
obj.AddMember("bindShapeMatrix", MakeValue(val, b.bindShapeMatrix.value, w.mAl).Move(), w.mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b.inverseBindMatrices) {
|
2017-07-26 00:26:18 +00:00
|
|
|
obj.AddMember("inverseBindMatrices", b.inverseBindMatrices->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Write(Value& obj, Texture& tex, AssetWriter& w)
|
|
|
|
{
|
|
|
|
if (tex.source) {
|
2017-07-26 00:26:18 +00:00
|
|
|
obj.AddMember("source", tex.source->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
if (tex.sampler) {
|
2017-07-26 00:26:18 +00:00
|
|
|
obj.AddMember("sampler", tex.sampler->index, w.mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline AssetWriter::AssetWriter(Asset& a)
|
|
|
|
: mDoc()
|
|
|
|
, mAsset(a)
|
|
|
|
, mAl(mDoc.GetAllocator())
|
|
|
|
{
|
|
|
|
mDoc.SetObject();
|
|
|
|
|
|
|
|
WriteMetadata();
|
|
|
|
WriteExtensionsUsed();
|
|
|
|
|
|
|
|
// Dump the contents of the dictionaries
|
|
|
|
for (size_t i = 0; i < a.mDicts.size(); ++i) {
|
|
|
|
a.mDicts[i]->WriteObjects(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the target scene field
|
|
|
|
if (mAsset.scene) {
|
2017-07-26 00:26:18 +00:00
|
|
|
mDoc.AddMember("scene", mAsset.scene->index, mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2020-06-12 00:37:06 +00:00
|
|
|
if(mAsset.extras) {
|
|
|
|
mDoc.AddMember("extras", *mAsset.extras, mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void AssetWriter::WriteFile(const char* path)
|
|
|
|
{
|
|
|
|
std::unique_ptr<IOStream> jsonOutFile(mAsset.OpenFile(path, "wt", true));
|
|
|
|
|
2022-11-08 16:03:55 +00:00
|
|
|
if (jsonOutFile == nullptr) {
|
2017-07-19 20:21:43 +00:00
|
|
|
throw DeadlyExportError("Could not open output file: " + std::string(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuffer docBuffer;
|
|
|
|
|
|
|
|
PrettyWriter<StringBuffer> writer(docBuffer);
|
2020-05-07 06:41:05 +00:00
|
|
|
if (!mDoc.Accept(writer)) {
|
|
|
|
throw DeadlyExportError("Failed to write scene data!");
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (jsonOutFile->Write(docBuffer.GetString(), docBuffer.GetSize(), 1) != 1) {
|
|
|
|
throw DeadlyExportError("Failed to write scene data!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write buffer data to separate .bin files
|
|
|
|
for (unsigned int i = 0; i < mAsset.buffers.Size(); ++i) {
|
|
|
|
Ref<Buffer> b = mAsset.buffers.Get(i);
|
|
|
|
|
|
|
|
std::string binPath = b->GetURI();
|
|
|
|
|
|
|
|
std::unique_ptr<IOStream> binOutFile(mAsset.OpenFile(binPath, "wb", true));
|
|
|
|
|
2022-11-08 16:03:55 +00:00
|
|
|
if (binOutFile == nullptr) {
|
2017-07-19 20:21:43 +00:00
|
|
|
throw DeadlyExportError("Could not open output file: " + binPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b->byteLength > 0) {
|
|
|
|
if (binOutFile->Write(b->GetPointer(), b->byteLength, 1) != 1) {
|
|
|
|
throw DeadlyExportError("Failed to write binary file: " + binPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 15:11:12 +00:00
|
|
|
inline void AssetWriter::WriteGLBFile(const char* path)
|
|
|
|
{
|
|
|
|
std::unique_ptr<IOStream> outfile(mAsset.OpenFile(path, "wb", true));
|
|
|
|
|
2022-11-08 16:03:55 +00:00
|
|
|
if (outfile == nullptr) {
|
2017-12-14 15:11:12 +00:00
|
|
|
throw DeadlyExportError("Could not open output file: " + std::string(path));
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:28:12 +00:00
|
|
|
Ref<Buffer> bodyBuffer = mAsset.GetBodyBuffer();
|
|
|
|
if (bodyBuffer->byteLength > 0) {
|
|
|
|
rapidjson::Value glbBodyBuffer;
|
|
|
|
glbBodyBuffer.SetObject();
|
2018-01-11 18:15:05 +00:00
|
|
|
glbBodyBuffer.AddMember("byteLength", static_cast<uint64_t>(bodyBuffer->byteLength), mAl);
|
2018-01-05 09:28:12 +00:00
|
|
|
mDoc["buffers"].PushBack(glbBodyBuffer, mAl);
|
|
|
|
}
|
|
|
|
|
2017-12-14 16:32:18 +00:00
|
|
|
// Padding with spaces as required by the spec
|
|
|
|
uint32_t padding = 0x20202020;
|
2017-12-14 15:11:12 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// JSON chunk
|
|
|
|
//
|
|
|
|
|
|
|
|
StringBuffer docBuffer;
|
|
|
|
Writer<StringBuffer> writer(docBuffer);
|
2020-05-07 06:41:05 +00:00
|
|
|
if (!mDoc.Accept(writer)) {
|
|
|
|
throw DeadlyExportError("Failed to write scene data!");
|
|
|
|
}
|
2017-12-14 15:11:12 +00:00
|
|
|
|
2023-02-17 19:20:00 +00:00
|
|
|
uint32_t jsonChunkLength = static_cast<uint32_t>((docBuffer.GetSize() + 3) & ~3); // Round up to next multiple of 4
|
2017-12-14 15:11:12 +00:00
|
|
|
auto paddingLength = jsonChunkLength - docBuffer.GetSize();
|
|
|
|
|
|
|
|
GLB_Chunk jsonChunk;
|
|
|
|
jsonChunk.chunkLength = jsonChunkLength;
|
|
|
|
jsonChunk.chunkType = ChunkType_JSON;
|
|
|
|
AI_SWAP4(jsonChunk.chunkLength);
|
|
|
|
|
|
|
|
outfile->Seek(sizeof(GLB_Header), aiOrigin_SET);
|
|
|
|
if (outfile->Write(&jsonChunk, 1, sizeof(GLB_Chunk)) != sizeof(GLB_Chunk)) {
|
|
|
|
throw DeadlyExportError("Failed to write scene data header!");
|
|
|
|
}
|
|
|
|
if (outfile->Write(docBuffer.GetString(), 1, docBuffer.GetSize()) != docBuffer.GetSize()) {
|
|
|
|
throw DeadlyExportError("Failed to write scene data!");
|
|
|
|
}
|
2017-12-14 16:32:18 +00:00
|
|
|
if (paddingLength && outfile->Write(&padding, 1, paddingLength) != paddingLength) {
|
2017-12-14 15:11:12 +00:00
|
|
|
throw DeadlyExportError("Failed to write scene data padding!");
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Binary chunk
|
|
|
|
//
|
|
|
|
|
2020-05-28 09:05:38 +00:00
|
|
|
int GLB_Chunk_count = 1;
|
2017-12-14 15:11:12 +00:00
|
|
|
uint32_t binaryChunkLength = 0;
|
2018-01-05 09:28:12 +00:00
|
|
|
if (bodyBuffer->byteLength > 0) {
|
2023-02-17 19:20:00 +00:00
|
|
|
binaryChunkLength = static_cast<uint32_t>((bodyBuffer->byteLength + 3) & ~3); // Round up to next multiple of 4
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2020-05-28 09:05:38 +00:00
|
|
|
auto curPaddingLength = binaryChunkLength - bodyBuffer->byteLength;
|
|
|
|
++GLB_Chunk_count;
|
2018-01-05 09:28:12 +00:00
|
|
|
|
|
|
|
GLB_Chunk binaryChunk;
|
|
|
|
binaryChunk.chunkLength = binaryChunkLength;
|
|
|
|
binaryChunk.chunkType = ChunkType_BIN;
|
|
|
|
AI_SWAP4(binaryChunk.chunkLength);
|
|
|
|
|
|
|
|
size_t bodyOffset = sizeof(GLB_Header) + sizeof(GLB_Chunk) + jsonChunk.chunkLength;
|
|
|
|
outfile->Seek(bodyOffset, aiOrigin_SET);
|
|
|
|
if (outfile->Write(&binaryChunk, 1, sizeof(GLB_Chunk)) != sizeof(GLB_Chunk)) {
|
|
|
|
throw DeadlyExportError("Failed to write body data header!");
|
|
|
|
}
|
|
|
|
if (outfile->Write(bodyBuffer->GetPointer(), 1, bodyBuffer->byteLength) != bodyBuffer->byteLength) {
|
|
|
|
throw DeadlyExportError("Failed to write body data!");
|
|
|
|
}
|
2020-05-28 09:05:38 +00:00
|
|
|
if (curPaddingLength && outfile->Write(&padding, 1, paddingLength) != paddingLength) {
|
2018-01-05 09:28:12 +00:00
|
|
|
throw DeadlyExportError("Failed to write body data padding!");
|
2017-12-14 15:11:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Header
|
|
|
|
//
|
|
|
|
|
|
|
|
GLB_Header header;
|
|
|
|
memcpy(header.magic, AI_GLB_MAGIC_NUMBER, sizeof(header.magic));
|
|
|
|
|
|
|
|
header.version = 2;
|
|
|
|
AI_SWAP4(header.version);
|
|
|
|
|
2020-05-28 09:05:38 +00:00
|
|
|
header.length = uint32_t(sizeof(GLB_Header) + GLB_Chunk_count * sizeof(GLB_Chunk) + jsonChunkLength + binaryChunkLength);
|
2017-12-14 15:11:12 +00:00
|
|
|
AI_SWAP4(header.length);
|
|
|
|
|
|
|
|
outfile->Seek(0, aiOrigin_SET);
|
|
|
|
if (outfile->Write(&header, 1, sizeof(GLB_Header)) != sizeof(GLB_Header)) {
|
|
|
|
throw DeadlyExportError("Failed to write the header!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 20:21:43 +00:00
|
|
|
inline void AssetWriter::WriteMetadata()
|
|
|
|
{
|
|
|
|
Value asset;
|
|
|
|
asset.SetObject();
|
2017-09-06 19:32:44 +00:00
|
|
|
asset.AddMember("version", Value(mAsset.asset.version, mAl).Move(), mAl);
|
|
|
|
asset.AddMember("generator", Value(mAsset.asset.generator, mAl).Move(), mAl);
|
2019-12-09 14:30:12 +00:00
|
|
|
if (!mAsset.asset.copyright.empty())
|
|
|
|
asset.AddMember("copyright", Value(mAsset.asset.copyright, mAl).Move(), mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
mDoc.AddMember("asset", asset, mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void AssetWriter::WriteExtensionsUsed()
|
|
|
|
{
|
|
|
|
Value exts;
|
|
|
|
exts.SetArray();
|
|
|
|
{
|
2017-08-31 22:30:43 +00:00
|
|
|
// This is used to export pbrSpecularGlossiness materials with GLTF 2.
|
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_pbrSpecularGlossiness"), mAl);
|
|
|
|
}
|
2018-05-18 21:01:25 +00:00
|
|
|
|
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_unlit) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_unlit"), mAl);
|
|
|
|
}
|
2020-12-21 13:34:16 +00:00
|
|
|
|
2022-11-08 21:09:50 +00:00
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_specular) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_specular"), mAl);
|
|
|
|
}
|
|
|
|
|
2020-12-21 13:34:16 +00:00
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_sheen) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_sheen"), mAl);
|
|
|
|
}
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2020-12-23 08:56:15 +00:00
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_clearcoat) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_clearcoat"), mAl);
|
|
|
|
}
|
2020-12-23 09:43:21 +00:00
|
|
|
|
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_transmission) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_transmission"), mAl);
|
|
|
|
}
|
2021-03-10 08:48:12 +00:00
|
|
|
|
2021-09-30 07:10:02 +00:00
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_volume) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_volume"), mAl);
|
|
|
|
}
|
|
|
|
|
2021-10-07 08:36:53 +00:00
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_ior) {
|
|
|
|
exts.PushBack(StringRef("KHR_materials_ior"), mAl);
|
|
|
|
}
|
|
|
|
|
2022-11-08 18:55:20 +00:00
|
|
|
if (this->mAsset.extensionsUsed.KHR_materials_emissive_strength) {
|
2022-11-03 21:11:21 +00:00
|
|
|
exts.PushBack(StringRef("KHR_materials_emissive_strength"), mAl);
|
|
|
|
}
|
|
|
|
|
2021-03-10 08:48:12 +00:00
|
|
|
if (this->mAsset.extensionsUsed.FB_ngon_encoding) {
|
|
|
|
exts.PushBack(StringRef("FB_ngon_encoding"), mAl);
|
|
|
|
}
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2021-05-06 22:10:06 +00:00
|
|
|
if (this->mAsset.extensionsUsed.KHR_texture_basisu) {
|
|
|
|
exts.PushBack(StringRef("KHR_texture_basisu"), mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!exts.Empty())
|
|
|
|
mDoc.AddMember("extensionsUsed", exts, mAl);
|
2021-07-29 11:28:51 +00:00
|
|
|
|
2021-05-06 22:10:06 +00:00
|
|
|
//basisu extensionRequired
|
|
|
|
Value extsReq;
|
|
|
|
extsReq.SetArray();
|
|
|
|
if (this->mAsset.extensionsUsed.KHR_texture_basisu) {
|
|
|
|
extsReq.PushBack(StringRef("KHR_texture_basisu"), mAl);
|
|
|
|
mDoc.AddMember("extensionsRequired", extsReq, mAl);
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void AssetWriter::WriteObjects(LazyDict<T>& d)
|
|
|
|
{
|
|
|
|
if (d.mObjs.empty()) return;
|
|
|
|
|
|
|
|
Value* container = &mDoc;
|
2022-11-08 21:09:50 +00:00
|
|
|
const char* context = "Document";
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
if (d.mExtId) {
|
|
|
|
Value* exts = FindObject(mDoc, "extensions");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (nullptr != exts) {
|
2017-07-19 20:21:43 +00:00
|
|
|
mDoc.AddMember("extensions", Value().SetObject().Move(), mDoc.GetAllocator());
|
|
|
|
exts = FindObject(mDoc, "extensions");
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
container = FindObjectInContext(*exts, d.mExtId, "extensions");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (nullptr != container) {
|
2017-07-19 20:21:43 +00:00
|
|
|
exts->AddMember(StringRef(d.mExtId), Value().SetObject().Move(), mDoc.GetAllocator());
|
2021-03-15 13:06:11 +00:00
|
|
|
container = FindObjectInContext(*exts, d.mExtId, "extensions");
|
|
|
|
context = d.mExtId;
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 13:06:11 +00:00
|
|
|
Value *dict = FindArrayInContext(*container, d.mDictId, context);
|
2020-03-12 16:38:42 +00:00
|
|
|
if (nullptr == dict) {
|
2017-07-26 00:26:18 +00:00
|
|
|
container->AddMember(StringRef(d.mDictId), Value().SetArray().Move(), mDoc.GetAllocator());
|
2021-03-15 13:06:11 +00:00
|
|
|
dict = FindArrayInContext(*container, d.mDictId, context);
|
2018-12-27 21:28:23 +00:00
|
|
|
if (nullptr == dict) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < d.mObjs.size(); ++i) {
|
2020-03-08 20:24:01 +00:00
|
|
|
if (d.mObjs[i]->IsSpecial()) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-19 20:21:43 +00:00
|
|
|
|
|
|
|
Value obj;
|
|
|
|
obj.SetObject();
|
|
|
|
|
|
|
|
if (!d.mObjs[i]->name.empty()) {
|
|
|
|
obj.AddMember("name", StringRef(d.mObjs[i]->name.c_str()), mAl);
|
|
|
|
}
|
|
|
|
|
|
|
|
Write(obj, *d.mObjs[i], *this);
|
|
|
|
|
2017-07-26 00:26:18 +00:00
|
|
|
dict->PushBack(obj, mAl);
|
2017-07-19 20:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void WriteLazyDict(LazyDict<T>& d, AssetWriter& w)
|
|
|
|
{
|
|
|
|
w.WriteObjects(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|