/*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*
*/
-#include <fstream>
+
+// FILE HEADER
#include <dali-scene3d/public-api/loader/gltf2-loader.h>
+
+// EXTERNAL INCLUDES
+#include <dali/devel-api/threading/mutex.h>
+#include <dali/integration-api/debug.h>
+#include <dali/public-api/images/image-operations.h>
+#include <dali/public-api/math/quaternion.h>
+#include <memory>
+
+// INTERNAL INCLUDES
#include <dali-scene3d/internal/loader/gltf2-asset.h>
#include <dali-scene3d/public-api/loader/load-result.h>
#include <dali-scene3d/public-api/loader/resource-bundle.h>
#include <dali-scene3d/public-api/loader/scene-definition.h>
#include <dali-scene3d/public-api/loader/shader-definition-factory.h>
#include <dali-scene3d/public-api/loader/utils.h>
-#include <dali/public-api/math/quaternion.h>
-#include <dali/integration-api/debug.h>
-
-#define ENUM_STRING_MAPPING(t, x) \
- { \
-#x, t::x \
- }
namespace gt = gltf2;
namespace js = json;
{
namespace
{
+Dali::Mutex gInitializeMutex;
+Dali::Mutex gReadMutex;
+
const std::string POSITION_PROPERTY("position");
const std::string ORIENTATION_PROPERTY("orientation");
const std::string SCALE_PROPERTY("scale");
std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
{
- gt::Animation proxy;
- SetRefReaderObject(proxy);
-
auto results = js::Read::Array<gt::Animation, js::ObjectReader<gt::Animation>::Read>(j);
for(auto& animation : results)
const auto MATERIAL_IOR_READER = std::move(js::Reader<gt::MaterialIor>()
.Register(*js::MakeProperty("ior", js::Read::Number<float>, >::MaterialIor::mIor)));
-
const auto MATERIAL_EXTENSION_READER = std::move(js::Reader<gt::MaterialExtensions>()
.Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader<gt::MaterialIor>::Read, >::MaterialExtensions::mMaterialIor))
.Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader<gt::MaterialSpecular>::Read, >::MaterialExtensions::mMaterialSpecular)));
NodeIndexMapper mNodeIndices;
};
-SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type w)
+void ConvertBuffer(const gt::Buffer& buffer, decltype(ResourceBundle::mBuffers)& outBuffers, const std::string& resourcePath)
+{
+ BufferDefinition bufferDefinition;
+
+ bufferDefinition.mResourcePath = resourcePath;
+ bufferDefinition.mUri = buffer.mUri;
+ bufferDefinition.mByteLength = buffer.mByteLength;
+
+ outBuffers.emplace_back(std::move(bufferDefinition));
+}
+
+void ConvertBuffers(const gt::Document& doc, ConversionContext& context)
{
- switch(w)
+ auto& outBuffers = context.mOutput.mResources.mBuffers;
+ outBuffers.reserve(doc.mBuffers.size());
+
+ for(auto& buffer : doc.mBuffers)
+ {
+ ConvertBuffer(buffer, outBuffers, context.mPath);
+ }
+}
+
+SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type wrapMode)
+{
+ switch(wrapMode)
{
case gt::Wrap::REPEAT:
return SamplerFlags::WRAP_REPEAT;
}
}
-SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& s)
+SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& sampler)
{
- if(s)
+ if(sampler)
{
- return (s->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (s->mMinFilter - gt::Filter::NEAREST) : ((s->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2) | ((s->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) | (ConvertWrapMode(s->mWrapS) << SamplerFlags::WRAP_S_SHIFT) | (ConvertWrapMode(s->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
+ return ((sampler->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (sampler->mMinFilter - gt::Filter::NEAREST) : ((sampler->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
+ ((sampler->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
+ (ConvertWrapMode(sampler->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
+ (ConvertWrapMode(sampler->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
}
else
{
}
}
-TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm)
+TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm, ConversionContext& context, const ImageMetadata& metaData = ImageMetadata())
{
- return TextureDefinition{std::string(mm.mTexture->mSource->mUri), ConvertSampler(mm.mTexture->mSampler)};
+ TextureDefinition textureDefinition;
+ std::string uri = std::string(mm.mTexture->mSource->mUri);
+ if(uri.empty())
+ {
+ uint32_t bufferIndex = mm.mTexture->mSource->mBufferView->mBuffer.GetIndex();
+ if(bufferIndex != INVALID_INDEX && context.mOutput.mResources.mBuffers[bufferIndex].IsAvailable())
+ {
+ auto& stream = context.mOutput.mResources.mBuffers[bufferIndex].GetBufferStream();
+ stream.clear();
+ stream.seekg(mm.mTexture->mSource->mBufferView->mByteOffset, stream.beg);
+ std::vector<uint8_t> dataBuffer;
+ dataBuffer.resize(mm.mTexture->mSource->mBufferView->mByteLength);
+ stream.read(reinterpret_cast<char*>(dataBuffer.data()), static_cast<std::streamsize>(static_cast<size_t>(mm.mTexture->mSource->mBufferView->mByteLength)));
+ return TextureDefinition{std::move(dataBuffer), ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
+ }
+ return TextureDefinition();
+ }
+ else
+ {
+ return TextureDefinition{uri, ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
+ }
}
-void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMaterials)& outMaterials)
+void ConvertMaterial(const gt::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials, ConversionContext& context)
{
+ auto getTextureMetaData = [](const std::unordered_map<std::string, ImageMetadata>& metaData, const gt::TextureInfo& info) {
+ if(!info.mTexture->mSource->mUri.empty())
+ {
+ if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end())
+ {
+ return search->second;
+ }
+ }
+ return ImageMetadata();
+ };
+
MaterialDefinition matDef;
auto& pbr = material.mPbrMetallicRoughness;
- if(pbr.mBaseColorFactor.a < 1.f)
+ if(material.mAlphaMode == gt::AlphaMode::BLEND)
{
+ matDef.mIsOpaque = false;
matDef.mFlags |= MaterialDefinition::TRANSPARENCY;
}
-
- if(material.mAlphaMode == gt::AlphaMode::MASK)
+ else if(material.mAlphaMode == gt::AlphaMode::MASK)
{
+ matDef.mIsMask = true;
matDef.SetAlphaCutoff(std::min(1.f, std::max(0.f, material.mAlphaCutoff)));
}
if(pbr.mBaseColorTexture)
{
const auto semantic = MaterialDefinition::ALBEDO;
- matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture)});
+ matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture, context, getTextureMetaData(imageMetaData, pbr.mBaseColorTexture))});
// TODO: and there had better be one
matDef.mFlags |= semantic;
}
{
const auto semantic = MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
MaterialDefinition::GLTF_CHANNELS;
- matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture)});
+ matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture, context, getTextureMetaData(imageMetaData, pbr.mMetallicRoughnessTexture))});
// TODO: and there had better be one
matDef.mFlags |= semantic;
}
if(material.mNormalTexture)
{
const auto semantic = MaterialDefinition::NORMAL;
- matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture)});
+ matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture, context, getTextureMetaData(imageMetaData, material.mNormalTexture))});
// TODO: and there had better be one
matDef.mFlags |= semantic;
}
if(material.mOcclusionTexture)
{
const auto semantic = MaterialDefinition::OCCLUSION;
- matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture)});
+ matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture, context, getTextureMetaData(imageMetaData, material.mOcclusionTexture))});
// TODO: and there had better be one
matDef.mFlags |= semantic;
matDef.mOcclusionStrength = material.mOcclusionTexture.mStrength;
if(material.mEmissiveTexture)
{
const auto semantic = MaterialDefinition::EMISSIVE;
- matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture)});
+ matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture, context, getTextureMetaData(imageMetaData, material.mEmissiveTexture))});
// TODO: and there had better be one
matDef.mFlags |= semantic;
matDef.mEmissiveFactor = material.mEmissiveFactor;
if(material.mMaterialExtensions.mMaterialIor.mIor < MAXFLOAT)
{
- float ior = material.mMaterialExtensions.mMaterialIor.mIor;
- matDef.mDielectricSpecular = powf((ior-1.0f)/(ior+1.0f), 2.0f);
+ float ior = material.mMaterialExtensions.mMaterialIor.mIor;
+ matDef.mDielectricSpecular = powf((ior - 1.0f) / (ior + 1.0f), 2.0f);
}
matDef.mSpecularFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularFactor;
matDef.mSpecularColorFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor;
if(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)
{
const auto semantic = MaterialDefinition::SPECULAR;
- matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)});
+ matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture))});
matDef.mFlags |= semantic;
}
if(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)
{
const auto semantic = MaterialDefinition::SPECULAR_COLOR;
- matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)});
+ matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture))});
matDef.mFlags |= semantic;
}
void ConvertMaterials(const gt::Document& doc, ConversionContext& context)
{
+ auto& imageMetaData = context.mOutput.mSceneMetadata.mImageMetadata;
+
auto& outMaterials = context.mOutput.mResources.mMaterials;
outMaterials.reserve(doc.mMaterials.size());
for(auto& m : doc.mMaterials)
{
- ConvertMaterial(m, outMaterials);
+ ConvertMaterial(m, imageMetaData, outMaterials, context);
}
}
static_cast<uint16_t>(acc.GetElementSizeBytes()),
acc.mMin,
acc.mMax}),
- std::move(sparseBlob)};
+ std::move(sparseBlob),
+ acc.mBufferView ? acc.mBufferView->mBuffer.GetIndex() : 0};
}
void ConvertMeshes(const gt::Document& doc, ConversionContext& context)
{
MeshDefinition meshDefinition;
- auto& attribs = primitive.mAttributes;
- meshDefinition.mUri = attribs.begin()->second->mBufferView->mBuffer->mUri;
+ auto& attribs = primitive.mAttributes;
meshDefinition.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[primitive.mMode];
- auto& accPositions = *attribs.find(gt::Attribute::POSITION)->second;
+ auto& accPositions = *attribs.find(gt::Attribute::POSITION)->second;
meshDefinition.mPositions = ConvertMeshPrimitiveAccessor(accPositions);
// glTF2 support vector4 tangent for mesh.
// https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#meshes-overview
auto iFind = attribs.find(am.mType);
if(iFind != attribs.end())
{
- DALI_ASSERT_DEBUG(iFind->second->mBufferView->mBuffer->mUri.compare(meshDefinition.mUri) == 0);
auto& accessor = meshDefinition.*(am.mAccessor);
accessor = ConvertMeshPrimitiveAccessor(*iFind->second);
if(iFind->first == gt::Attribute::JOINTS_0)
{
meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
- DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
+ meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_JOINT_IDS;
+ DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
}
}
else if(needNormalsTangents)
{
meshDefinition.mIndices = ConvertMeshPrimitiveAccessor(*primitive.mIndices);
meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
- DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || primitive.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
+ meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_INDICES;
+ DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_INDICES) || primitive.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
}
if(!primitive.mTargets.empty())
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
if(INVALID_INDEX == context.mDefaultMaterial)
{
- auto& outMaterials = context.mOutput.mResources.mMaterials;
+ auto& outMaterials = context.mOutput.mResources.mMaterials;
context.mDefaultMaterial = outMaterials.size();
- ConvertMaterial(gt::Material{}, outMaterials);
+ ConvertMaterial(gt::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials, context);
}
materialIdx = context.mDefaultMaterial;
for(uint32_t i = 0; i < primitiveCount; ++i)
{
std::unique_ptr<NodeDefinition::Renderable> renderable;
- auto modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
- modelRenderable->mMeshIdx = meshIdx + i;
+ auto modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
+ modelRenderable->mMeshIdx = meshIdx + i;
DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel)
{
- ConvertSceneNodes(*doc.mScene, context, isMRendererModel);
-
- for(uint32_t i = 0, i1 = doc.mScene.GetIndex(); i < i1; ++i)
+ if(!doc.mScenes.empty())
{
- ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
- }
+ uint32_t rootSceneIndex = 0u;
+ if(doc.mScene)
+ {
+ rootSceneIndex = doc.mScene.GetIndex();
+ }
+ ConvertSceneNodes(doc.mScenes[rootSceneIndex], context, isMRendererModel);
- for(uint32_t i = doc.mScene.GetIndex() + 1; i < doc.mScenes.size(); ++i)
- {
- ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
+ for(uint32_t i = 0, i1 = rootSceneIndex; i < i1; ++i)
+ {
+ ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
+ }
+
+ for(uint32_t i = rootSceneIndex + 1; i < doc.mScenes.size(); ++i)
+ {
+ ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
+ }
}
}
template<typename T>
-void LoadDataFromAccessor(const std::string& path, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
+void LoadDataFromAccessor(ConversionContext& context, uint32_t bufferIndex, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
{
- std::ifstream animationBinaryFile(path, std::ifstream::binary);
-
- if(!animationBinaryFile.is_open())
+ if(bufferIndex >= context.mOutput.mResources.mBuffers.size())
{
- throw std::runtime_error("Failed to load " + path);
+ DALI_LOG_ERROR("Invailid buffer index\n");
+ return;
}
- animationBinaryFile.seekg(offset);
- animationBinaryFile.read(reinterpret_cast<char*>(dataBuffer.Begin()), size);
- animationBinaryFile.close();
+ auto& buffer = context.mOutput.mResources.mBuffers[bufferIndex];
+ if(!buffer.IsAvailable())
+ {
+ DALI_LOG_ERROR("Failed to load from buffer stream.\n");
+ }
+ auto& stream = buffer.GetBufferStream();
+ stream.clear();
+ stream.seekg(offset, stream.beg);
+ stream.read(reinterpret_cast<char*>(dataBuffer.Begin()), static_cast<std::streamsize>(static_cast<size_t>(size)));
}
template<typename T>
-float LoadDataFromAccessors(const std::string& path, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
+float LoadDataFromAccessors(ConversionContext& context, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
{
inputDataBuffer.Resize(input.mCount);
outputDataBuffer.Resize(output.mCount);
const uint32_t inputDataBufferSize = input.GetBytesLength();
const uint32_t outputDataBufferSize = output.GetBytesLength();
- LoadDataFromAccessor<float>(path + std::string(input.mBufferView->mBuffer->mUri), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
- LoadDataFromAccessor<T>(path + std::string(output.mBufferView->mBuffer->mUri), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
+ LoadDataFromAccessor<float>(context, output.mBufferView->mBuffer.GetIndex(), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
+ LoadDataFromAccessor<T>(context, output.mBufferView->mBuffer.GetIndex(), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
return inputDataBuffer[input.mCount - 1u];
}
template<typename T>
-float LoadKeyFrames(const std::string& path, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
+float LoadKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
{
const gltf2::Accessor& input = *channel.mSampler->mInput;
const gltf2::Accessor& output = *channel.mSampler->mOutput;
Vector<float> inputDataBuffer;
Vector<T> outputDataBuffer;
- const float duration = LoadDataFromAccessors<T>(path, input, output, inputDataBuffer, outputDataBuffer);
+ const float duration = std::max(LoadDataFromAccessors<T>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
for(uint32_t i = 0; i < input.mCount; ++i)
{
return duration;
}
-float LoadBlendShapeKeyFrames(const std::string& path, const gt::Animation::Channel& channel, const std::string& nodeName, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
+float LoadBlendShapeKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
{
const gltf2::Accessor& input = *channel.mSampler->mInput;
const gltf2::Accessor& output = *channel.mSampler->mOutput;
Vector<float> inputDataBuffer;
Vector<float> outputDataBuffer;
- const float duration = LoadDataFromAccessors<float>(path, input, output, inputDataBuffer, outputDataBuffer);
+ const float duration = LoadDataFromAccessors<float>(context, input, output, inputDataBuffer, outputDataBuffer);
char weightNameBuffer[32];
auto prefixSize = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM.c_str());
{
AnimatedProperty& animatedProperty = properties[propertyIndex++];
- animatedProperty.mNodeName = nodeName;
+ animatedProperty.mNodeIndex = nodeIndex;
snprintf(pWeightName, remainingSize, "%d]", weightIndex);
animatedProperty.mPropertyName = std::string(weightNameBuffer);
}
uint32_t numberOfProperties = 0u;
-
- for(const auto& channel : animation.mChannels)
- {
- numberOfProperties += channel.mSampler->mOutput->mCount;
- }
- animationDef.mProperties.resize(numberOfProperties);
-
- Index propertyIndex = 0u;
for(const auto& channel : animation.mChannels)
{
- std::string nodeName;
- if(!channel.mTarget.mNode->mName.empty())
+ if(channel.mTarget.mPath == gt::Animation::Channel::Target::WEIGHTS)
{
- nodeName = channel.mTarget.mNode->mName;
+ numberOfProperties += channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount;
}
else
{
- Index index = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
- nodeName = context.mOutput.mScene.GetNode(index)->mName;
+ numberOfProperties++;
}
+ }
+ animationDef.mProperties.resize(numberOfProperties);
- float duration = 0.f;
+ Index propertyIndex = 0u;
+ for(const auto& channel : animation.mChannels)
+ {
+ Index nodeIndex = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
+ float duration = 0.f;
switch(channel.mTarget.mPath)
{
{
AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
- animatedProperty.mNodeName = nodeName;
+ animatedProperty.mNodeIndex = nodeIndex;
animatedProperty.mPropertyName = POSITION_PROPERTY;
animatedProperty.mKeyFrames = KeyFrames::New();
- duration = LoadKeyFrames<Vector3>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
+ duration = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
animatedProperty.mTimePeriod = {0.f, duration};
break;
{
AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
- animatedProperty.mNodeName = nodeName;
+ animatedProperty.mNodeIndex = nodeIndex;
animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
animatedProperty.mKeyFrames = KeyFrames::New();
- duration = LoadKeyFrames<Quaternion>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
+ duration = LoadKeyFrames<Quaternion>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
animatedProperty.mTimePeriod = {0.f, duration};
break;
{
AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
- animatedProperty.mNodeName = nodeName;
+ animatedProperty.mNodeIndex = nodeIndex;
animatedProperty.mPropertyName = SCALE_PROPERTY;
animatedProperty.mKeyFrames = KeyFrames::New();
- duration = LoadKeyFrames<Vector3>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
+ duration = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
animatedProperty.mTimePeriod = {0.f, duration};
break;
}
case gt::Animation::Channel::Target::WEIGHTS:
{
- duration = LoadBlendShapeKeyFrames(context.mPath, channel, nodeName, propertyIndex, animationDef.mProperties);
+ duration = LoadBlendShapeKeyFrames(context, channel, nodeIndex, propertyIndex, animationDef.mProperties);
break;
}
struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
{
- std::ifstream mStream;
+ std::istream& mStream;
const uint32_t mElementSizeBytes;
- InverseBindMatrixAccessor(const gt::Accessor& accessor, const std::string& path)
- : mStream(path + std::string(accessor.mBufferView->mBuffer->mUri), std::ios::binary),
+ InverseBindMatrixAccessor(const gt::Accessor& accessor, ConversionContext& context)
+ : mStream(context.mOutput.mResources.mBuffers[accessor.mBufferView->mBuffer.GetIndex()].GetBufferStream()),
mElementSizeBytes(accessor.GetElementSizeBytes())
{
- DALI_ASSERT_ALWAYS(mStream);
DALI_ASSERT_DEBUG(accessor.mType == gt::AccessorType::MAT4 && accessor.mComponentType == gt::Component::FLOAT);
- mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset);
+ if(!mStream.rdbuf()->in_avail())
+ {
+ DALI_LOG_ERROR("Failed to load from stream\n");
+ }
+ mStream.clear();
+ mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset, mStream.beg);
}
virtual void Provide(Matrix& ibm) override
{
- DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), mElementSizeBytes));
+ DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), static_cast<std::streamsize>(static_cast<size_t>(mElementSizeBytes))));
}
};
auto& resources = context.mOutput.mResources;
resources.mSkeletons.reserve(doc.mSkins.size());
- for(auto& s : doc.mSkins)
+ for(auto& skin : doc.mSkins)
{
std::unique_ptr<IInverseBindMatrixProvider> ibmProvider;
- if(s.mInverseBindMatrices)
+ if(skin.mInverseBindMatrices)
{
- ibmProvider.reset(new InverseBindMatrixAccessor(*s.mInverseBindMatrices, context.mPath));
+ ibmProvider.reset(new InverseBindMatrixAccessor(*skin.mInverseBindMatrices, context));
}
else
{
}
SkeletonDefinition skeleton;
- if(s.mSkeleton.GetIndex() != INVALID_INDEX)
+ if(skin.mSkeleton.GetIndex() != INVALID_INDEX)
{
- skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(s.mSkeleton.GetIndex());
+ skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(skin.mSkeleton.GetIndex());
}
- skeleton.mJoints.resize(s.mJoints.size());
+ skeleton.mJoints.resize(skin.mJoints.size());
auto iJoint = skeleton.mJoints.begin();
- for(auto& j : s.mJoints)
+ for(auto& joint : skin.mJoints)
{
- iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(j.GetIndex());
+ iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(joint.GetIndex());
ibmProvider->Provide(iJoint->mInverseBindMatrix);
} // namespace
+void InitializeGltfLoader()
+{
+ // Set ObjectReader only once (for all gltf loading).
+ static bool setObjectReadersRequired = true;
+ {
+ Mutex::ScopedLock lock(gInitializeMutex);
+ if(setObjectReadersRequired)
+ {
+ // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
+ SetObjectReaders();
+ setObjectReadersRequired = false;
+ }
+ }
+}
+
void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
{
bool failed = false;
throw std::runtime_error("Failed to parse " + url);
}
- static bool setObjectReaders = true;
- if(setObjectReaders)
- {
- // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
- SetObjectReaders();
- setObjectReaders = false;
- }
-
gt::Document doc;
auto& rootObj = js::Cast<json_object_s>(*root);
isMRendererModel = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
}
- gt::SetRefReaderObject(doc);
- DOCUMENT_READER.Read(rootObj, doc);
+ {
+ Mutex::ScopedLock lock(gReadMutex);
+ gt::SetRefReaderObject(doc);
+ DOCUMENT_READER.Read(rootObj, doc);
+ }
auto path = url.substr(0, url.rfind('/') + 1);
ConversionContext context{params, path, INVALID_INDEX};
+ ConvertBuffers(doc, context);
ConvertMaterials(doc, context);
ConvertMeshes(doc, context);
ConvertNodes(doc, context, isMRendererModel);