X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=dali-scene3d%2Finternal%2Floader%2Fgltf2-util.cpp;h=330a2c0ecefb6980caa190f28dc0246bdb647d7e;hb=679f02413b55445b39148f4b102c153b1b5fae83;hp=b44ad70636e4bad0a7e33a7f757d28829ad9a5c2;hpb=4fb5c0c0f9f02319b3cff19338a61bfb098fbd8e;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-scene3d/internal/loader/gltf2-util.cpp b/dali-scene3d/internal/loader/gltf2-util.cpp index b44ad70..330a2c0 100644 --- a/dali-scene3d/internal/loader/gltf2-util.cpp +++ b/dali-scene3d/internal/loader/gltf2-util.cpp @@ -19,6 +19,7 @@ #include // EXTERNAL INCLUDES +#include #include using namespace Dali::Scene3D::Loader; @@ -80,102 +81,162 @@ void ApplyAccessorMinMax(const gltf2::Accessor& accessor, float* values) MeshDefinition::Blob::ApplyMinMax(accessor.mMin, accessor.mMax, accessor.mCount, values); } -const auto BUFFER_READER = std::move(json::Reader() - .Register(*json::MakeProperty("byteLength", json::Read::Number, &gltf2::Buffer::mByteLength)) - .Register(*json::MakeProperty("uri", json::Read::StringView, &gltf2::Buffer::mUri))); - -const auto BUFFER_VIEW_READER = std::move(json::Reader() - .Register(*json::MakeProperty("buffer", gltf2::RefReader::Read, &gltf2::BufferView::mBuffer)) - .Register(*json::MakeProperty("byteOffset", json::Read::Number, &gltf2::BufferView::mByteOffset)) - .Register(*json::MakeProperty("byteLength", json::Read::Number, &gltf2::BufferView::mByteLength)) - .Register(*json::MakeProperty("byteStride", json::Read::Number, &gltf2::BufferView::mByteStride)) - .Register(*json::MakeProperty("target", json::Read::Number, &gltf2::BufferView::mTarget))); - -const auto BUFFER_VIEW_CLIENT_READER = std::move(json::Reader() - .Register(*json::MakeProperty("bufferView", gltf2::RefReader::Read, &gltf2::BufferViewClient::mBufferView)) - .Register(*json::MakeProperty("byteOffset", json::Read::Number, &gltf2::BufferViewClient::mByteOffset))); - -const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(json::Reader() - .Register(*new json::Property>("bufferView", gltf2::RefReader::Read, &gltf2::ComponentTypedBufferViewClient::mBufferView)) - .Register(*new json::Property("byteOffset", json::Read::Number, &gltf2::ComponentTypedBufferViewClient::mByteOffset)) - .Register(*json::MakeProperty("componentType", json::Read::Enum, &gltf2::ComponentTypedBufferViewClient::mComponentType))); - -const auto ACCESSOR_SPARSE_READER = std::move(json::Reader() - .Register(*json::MakeProperty("count", json::Read::Number, &gltf2::Accessor::Sparse::mCount)) - .Register(*json::MakeProperty("indices", json::ObjectReader::Read, &gltf2::Accessor::Sparse::mIndices)) - .Register(*json::MakeProperty("values", json::ObjectReader::Read, &gltf2::Accessor::Sparse::mValues))); - -const auto ACCESSOR_READER = std::move(json::Reader() - .Register(*new json::Property>("bufferView", - gltf2::RefReader::Read, - &gltf2::Accessor::mBufferView)) - .Register(*new json::Property("byteOffset", - json::Read::Number, - &gltf2::Accessor::mByteOffset)) - .Register(*new json::Property("componentType", - json::Read::Enum, - &gltf2::Accessor::mComponentType)) - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Accessor::mName)) - .Register(*json::MakeProperty("count", json::Read::Number, &gltf2::Accessor::mCount)) - .Register(*json::MakeProperty("normalized", json::Read::Boolean, &gltf2::Accessor::mNormalized)) - .Register(*json::MakeProperty("type", gltf2::ReadStringEnum, &gltf2::Accessor::mType)) - .Register(*json::MakeProperty("min", json::Read::Array, &gltf2::Accessor::mMin)) - .Register(*json::MakeProperty("max", json::Read::Array, &gltf2::Accessor::mMax)) - .Register(*new json::Property("sparse", json::ObjectReader::Read, &gltf2::Accessor::SetSparse))); - -const auto IMAGE_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Material::mName)) - .Register(*json::MakeProperty("uri", json::Read::StringView, &gltf2::Image::mUri)) - .Register(*json::MakeProperty("mimeType", json::Read::StringView, &gltf2::Image::mMimeType)) - .Register(*json::MakeProperty("bufferView", gltf2::RefReader::Read, &gltf2::Image::mBufferView))); - -const auto SAMPLER_READER = std::move(json::Reader() - .Register(*json::MakeProperty("minFilter", json::Read::Enum, &gltf2::Sampler::mMinFilter)) - .Register(*json::MakeProperty("magFilter", json::Read::Enum, &gltf2::Sampler::mMagFilter)) - .Register(*json::MakeProperty("wrapS", json::Read::Enum, &gltf2::Sampler::mWrapS)) - .Register(*json::MakeProperty("wrapT", json::Read::Enum, &gltf2::Sampler::mWrapT))); - -const auto TEXURE_READER = std::move(json::Reader() - .Register(*json::MakeProperty("source", gltf2::RefReader::Read, &gltf2::Texture::mSource)) - .Register(*json::MakeProperty("sampler", gltf2::RefReader::Read, &gltf2::Texture::mSampler))); - -const auto TEXURE_INFO_READER = std::move(json::Reader() - .Register(*json::MakeProperty("index", gltf2::RefReader::Read, &gltf2::TextureInfo::mTexture)) - .Register(*json::MakeProperty("texCoord", json::Read::Number, &gltf2::TextureInfo::mTexCoord)) - .Register(*json::MakeProperty("scale", json::Read::Number, &gltf2::TextureInfo::mScale)) - .Register(*json::MakeProperty("strength", json::Read::Number, &gltf2::TextureInfo::mStrength))); - -const auto MATERIAL_PBR_READER = std::move(json::Reader() - .Register(*json::MakeProperty("baseColorFactor", gltf2::ReadDaliVector, &gltf2::Material::Pbr::mBaseColorFactor)) - .Register(*json::MakeProperty("baseColorTexture", json::ObjectReader::Read, &gltf2::Material::Pbr::mBaseColorTexture)) - .Register(*json::MakeProperty("metallicFactor", json::Read::Number, &gltf2::Material::Pbr::mMetallicFactor)) - .Register(*json::MakeProperty("roughnessFactor", json::Read::Number, &gltf2::Material::Pbr::mRoughnessFactor)) - .Register(*json::MakeProperty("metallicRoughnessTexture", json::ObjectReader::Read, &gltf2::Material::Pbr::mMetallicRoughnessTexture))); - -const auto MATERIAL_SPECULAR_READER = std::move(json::Reader() - .Register(*json::MakeProperty("specularFactor", json::Read::Number, &gltf2::MaterialSpecular::mSpecularFactor)) - .Register(*json::MakeProperty("specularTexture", json::ObjectReader::Read, &gltf2::MaterialSpecular::mSpecularTexture)) - .Register(*json::MakeProperty("specularColorFactor", gltf2::ReadDaliVector, &gltf2::MaterialSpecular::mSpecularColorFactor)) - .Register(*json::MakeProperty("specularColorTexture", json::ObjectReader::Read, &gltf2::MaterialSpecular::mSpecularColorTexture))); - -const auto MATERIAL_IOR_READER = std::move(json::Reader() - .Register(*json::MakeProperty("ior", json::Read::Number, &gltf2::MaterialIor::mIor))); - -const auto MATERIAL_EXTENSION_READER = std::move(json::Reader() - .Register(*json::MakeProperty("KHR_materials_ior", json::ObjectReader::Read, &gltf2::MaterialExtensions::mMaterialIor)) - .Register(*json::MakeProperty("KHR_materials_specular", json::ObjectReader::Read, &gltf2::MaterialExtensions::mMaterialSpecular))); - -const auto MATERIAL_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Material::mName)) - .Register(*json::MakeProperty("pbrMetallicRoughness", json::ObjectReader::Read, &gltf2::Material::mPbrMetallicRoughness)) - .Register(*json::MakeProperty("normalTexture", json::ObjectReader::Read, &gltf2::Material::mNormalTexture)) - .Register(*json::MakeProperty("occlusionTexture", json::ObjectReader::Read, &gltf2::Material::mOcclusionTexture)) - .Register(*json::MakeProperty("emissiveTexture", json::ObjectReader::Read, &gltf2::Material::mEmissiveTexture)) - .Register(*json::MakeProperty("emissiveFactor", gltf2::ReadDaliVector, &gltf2::Material::mEmissiveFactor)) - .Register(*json::MakeProperty("alphaMode", gltf2::ReadStringEnum, &gltf2::Material::mAlphaMode)) - .Register(*json::MakeProperty("alphaCutoff", json::Read::Number, &gltf2::Material::mAlphaCutoff)) - .Register(*json::MakeProperty("doubleSided", json::Read::Boolean, &gltf2::Material::mDoubleSided)) - .Register(*json::MakeProperty("extensions", json::ObjectReader::Read, &gltf2::Material::mMaterialExtensions))); +const json::Reader& GetBufferReader() +{ + static const auto BUFFER_READER = std::move(json::Reader() + .Register(*json::MakeProperty("byteLength", json::Read::Number, &gltf2::Buffer::mByteLength)) + .Register(*json::MakeProperty("uri", json::Read::StringView, &gltf2::Buffer::mUri))); + return BUFFER_READER; +} + +const json::Reader& GetBufferViewReader() +{ + static const auto BUFFER_VIEW_READER = std::move(json::Reader() + .Register(*json::MakeProperty("buffer", gltf2::RefReader::Read, &gltf2::BufferView::mBuffer)) + .Register(*json::MakeProperty("byteOffset", json::Read::Number, &gltf2::BufferView::mByteOffset)) + .Register(*json::MakeProperty("byteLength", json::Read::Number, &gltf2::BufferView::mByteLength)) + .Register(*json::MakeProperty("byteStride", json::Read::Number, &gltf2::BufferView::mByteStride)) + .Register(*json::MakeProperty("target", json::Read::Number, &gltf2::BufferView::mTarget))); + return BUFFER_VIEW_READER; +} + +const json::Reader& GetBufferViewClientReader() +{ + static const auto BUFFER_VIEW_CLIENT_READER = std::move(json::Reader() + .Register(*json::MakeProperty("bufferView", gltf2::RefReader::Read, &gltf2::BufferViewClient::mBufferView)) + .Register(*json::MakeProperty("byteOffset", json::Read::Number, &gltf2::BufferViewClient::mByteOffset))); + return BUFFER_VIEW_CLIENT_READER; +} + +const json::Reader& GetComponentTypedBufferViewClientReader() +{ + static const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(json::Reader() + .Register(*new json::Property>("bufferView", gltf2::RefReader::Read, &gltf2::ComponentTypedBufferViewClient::mBufferView)) + .Register(*new json::Property("byteOffset", json::Read::Number, &gltf2::ComponentTypedBufferViewClient::mByteOffset)) + .Register(*json::MakeProperty("componentType", json::Read::Enum, &gltf2::ComponentTypedBufferViewClient::mComponentType))); + return COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER; +} + +const json::Reader& GetAccessorSparseReader() +{ + static const auto ACCESSOR_SPARSE_READER = std::move(json::Reader() + .Register(*json::MakeProperty("count", json::Read::Number, &gltf2::Accessor::Sparse::mCount)) + .Register(*json::MakeProperty("indices", json::ObjectReader::Read, &gltf2::Accessor::Sparse::mIndices)) + .Register(*json::MakeProperty("values", json::ObjectReader::Read, &gltf2::Accessor::Sparse::mValues))); + return ACCESSOR_SPARSE_READER; +} + +const json::Reader& GetAccessorReader() +{ + static const auto ACCESSOR_READER = std::move(json::Reader() + .Register(*new json::Property>("bufferView", + gltf2::RefReader::Read, + &gltf2::Accessor::mBufferView)) + .Register(*new json::Property("byteOffset", + json::Read::Number, + &gltf2::Accessor::mByteOffset)) + .Register(*new json::Property("componentType", + json::Read::Enum, + &gltf2::Accessor::mComponentType)) + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Accessor::mName)) + .Register(*json::MakeProperty("count", json::Read::Number, &gltf2::Accessor::mCount)) + .Register(*json::MakeProperty("normalized", json::Read::Boolean, &gltf2::Accessor::mNormalized)) + .Register(*json::MakeProperty("type", gltf2::ReadStringEnum, &gltf2::Accessor::mType)) + .Register(*json::MakeProperty("min", json::Read::Array, &gltf2::Accessor::mMin)) + .Register(*json::MakeProperty("max", json::Read::Array, &gltf2::Accessor::mMax)) + .Register(*new json::Property("sparse", json::ObjectReader::Read, &gltf2::Accessor::SetSparse))); + return ACCESSOR_READER; +} + +const json::Reader& GetImageReader() +{ + static const auto IMAGE_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Material::mName)) + .Register(*json::MakeProperty("uri", json::Read::StringView, &gltf2::Image::mUri)) + .Register(*json::MakeProperty("mimeType", json::Read::StringView, &gltf2::Image::mMimeType)) + .Register(*json::MakeProperty("bufferView", gltf2::RefReader::Read, &gltf2::Image::mBufferView))); + return IMAGE_READER; +} + +const json::Reader& GetSamplerReader() +{ + static const auto SAMPLER_READER = std::move(json::Reader() + .Register(*json::MakeProperty("minFilter", json::Read::Enum, &gltf2::Sampler::mMinFilter)) + .Register(*json::MakeProperty("magFilter", json::Read::Enum, &gltf2::Sampler::mMagFilter)) + .Register(*json::MakeProperty("wrapS", json::Read::Enum, &gltf2::Sampler::mWrapS)) + .Register(*json::MakeProperty("wrapT", json::Read::Enum, &gltf2::Sampler::mWrapT))); + return SAMPLER_READER; +} + +const json::Reader& GetTextureReader() +{ + static const auto TEXURE_READER = std::move(json::Reader() + .Register(*json::MakeProperty("source", gltf2::RefReader::Read, &gltf2::Texture::mSource)) + .Register(*json::MakeProperty("sampler", gltf2::RefReader::Read, &gltf2::Texture::mSampler))); + return TEXURE_READER; +} + +const json::Reader& GetTextureInfoReader() +{ + static const auto TEXURE_INFO_READER = std::move(json::Reader() + .Register(*json::MakeProperty("index", gltf2::RefReader::Read, &gltf2::TextureInfo::mTexture)) + .Register(*json::MakeProperty("texCoord", json::Read::Number, &gltf2::TextureInfo::mTexCoord)) + .Register(*json::MakeProperty("scale", json::Read::Number, &gltf2::TextureInfo::mScale)) + .Register(*json::MakeProperty("strength", json::Read::Number, &gltf2::TextureInfo::mStrength))); + return TEXURE_INFO_READER; +} + +const json::Reader& GetMaterialPbrReader() +{ + static const auto MATERIAL_PBR_READER = std::move(json::Reader() + .Register(*json::MakeProperty("baseColorFactor", gltf2::ReadDaliVector, &gltf2::Material::Pbr::mBaseColorFactor)) + .Register(*json::MakeProperty("baseColorTexture", json::ObjectReader::Read, &gltf2::Material::Pbr::mBaseColorTexture)) + .Register(*json::MakeProperty("metallicFactor", json::Read::Number, &gltf2::Material::Pbr::mMetallicFactor)) + .Register(*json::MakeProperty("roughnessFactor", json::Read::Number, &gltf2::Material::Pbr::mRoughnessFactor)) + .Register(*json::MakeProperty("metallicRoughnessTexture", json::ObjectReader::Read, &gltf2::Material::Pbr::mMetallicRoughnessTexture))); + return MATERIAL_PBR_READER; +} + +const json::Reader& GetMaterialSpecularReader() +{ + static const auto MATERIAL_SPECULAR_READER = std::move(json::Reader() + .Register(*json::MakeProperty("specularFactor", json::Read::Number, &gltf2::MaterialSpecular::mSpecularFactor)) + .Register(*json::MakeProperty("specularTexture", json::ObjectReader::Read, &gltf2::MaterialSpecular::mSpecularTexture)) + .Register(*json::MakeProperty("specularColorFactor", gltf2::ReadDaliVector, &gltf2::MaterialSpecular::mSpecularColorFactor)) + .Register(*json::MakeProperty("specularColorTexture", json::ObjectReader::Read, &gltf2::MaterialSpecular::mSpecularColorTexture))); + return MATERIAL_SPECULAR_READER; +} + +const json::Reader& GetMaterialIorReader() +{ + static const auto MATERIAL_IOR_READER = std::move(json::Reader() + .Register(*json::MakeProperty("ior", json::Read::Number, &gltf2::MaterialIor::mIor))); + return MATERIAL_IOR_READER; +} + +const json::Reader& GetMaterialExtensionsReader() +{ + static const auto MATERIAL_EXTENSION_READER = std::move(json::Reader() + .Register(*json::MakeProperty("KHR_materials_ior", json::ObjectReader::Read, &gltf2::MaterialExtensions::mMaterialIor)) + .Register(*json::MakeProperty("KHR_materials_specular", json::ObjectReader::Read, &gltf2::MaterialExtensions::mMaterialSpecular))); + return MATERIAL_EXTENSION_READER; +} + +const json::Reader& GetMaterialReader() +{ + static const auto MATERIAL_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Material::mName)) + .Register(*json::MakeProperty("pbrMetallicRoughness", json::ObjectReader::Read, &gltf2::Material::mPbrMetallicRoughness)) + .Register(*json::MakeProperty("normalTexture", json::ObjectReader::Read, &gltf2::Material::mNormalTexture)) + .Register(*json::MakeProperty("occlusionTexture", json::ObjectReader::Read, &gltf2::Material::mOcclusionTexture)) + .Register(*json::MakeProperty("emissiveTexture", json::ObjectReader::Read, &gltf2::Material::mEmissiveTexture)) + .Register(*json::MakeProperty("emissiveFactor", gltf2::ReadDaliVector, &gltf2::Material::mEmissiveFactor)) + .Register(*json::MakeProperty("alphaMode", gltf2::ReadStringEnum, &gltf2::Material::mAlphaMode)) + .Register(*json::MakeProperty("alphaCutoff", json::Read::Number, &gltf2::Material::mAlphaCutoff)) + .Register(*json::MakeProperty("doubleSided", json::Read::Boolean, &gltf2::Material::mDoubleSided)) + .Register(*json::MakeProperty("extensions", json::ObjectReader::Read, &gltf2::Material::mMaterialExtensions))); + return MATERIAL_READER; +} std::map> ReadMeshPrimitiveAttributes(const json_value_s& j) { @@ -209,130 +270,182 @@ std::vector>> ReadM return result; } -const auto MESH_PRIMITIVE_READER = std::move(json::Reader() - .Register(*json::MakeProperty("attributes", ReadMeshPrimitiveAttributes, &gltf2::Mesh::Primitive::mAttributes)) - .Register(*json::MakeProperty("indices", gltf2::RefReader::Read, &gltf2::Mesh::Primitive::mIndices)) - .Register(*json::MakeProperty("material", gltf2::RefReader::Read, &gltf2::Mesh::Primitive::mMaterial)) - .Register(*json::MakeProperty("mode", json::Read::Enum, &gltf2::Mesh::Primitive::mMode)) - .Register(*json::MakeProperty("targets", ReadMeshPrimitiveTargets, &gltf2::Mesh::Primitive::mTargets))); - -const auto MESH_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Mesh::mName)) - .Register(*json::MakeProperty("primitives", - json::Read::Array::Read>, - &gltf2::Mesh::mPrimitives)) - .Register(*json::MakeProperty("weights", json::Read::Array, &gltf2::Mesh::mWeights))); - -const auto SKIN_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Skin::mName)) - .Register(*json::MakeProperty("inverseBindMatrices", - gltf2::RefReader::Read, - &gltf2::Skin::mInverseBindMatrices)) - .Register(*json::MakeProperty("skeleton", - gltf2::RefReader::Read, - &gltf2::Skin::mSkeleton)) - .Register(*json::MakeProperty("joints", - json::Read::Array, gltf2::RefReader::Read>, - &gltf2::Skin::mJoints))); - -const auto CAMERA_PERSPECTIVE_READER = std::move(json::Reader() - .Register(*json::MakeProperty("aspectRatio", json::Read::Number, &gltf2::Camera::Perspective::mAspectRatio)) - .Register(*json::MakeProperty("yfov", json::Read::Number, &gltf2::Camera::Perspective::mYFov)) - .Register(*json::MakeProperty("zfar", json::Read::Number, &gltf2::Camera::Perspective::mZFar)) - .Register(*json::MakeProperty("znear", json::Read::Number, &gltf2::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted - -const auto CAMERA_ORTHOGRAPHIC_READER = std::move(json::Reader() - .Register(*json::MakeProperty("xmag", json::Read::Number, &gltf2::Camera::Orthographic::mXMag)) - .Register(*json::MakeProperty("ymag", json::Read::Number, &gltf2::Camera::Orthographic::mYMag)) - .Register(*json::MakeProperty("zfar", json::Read::Number, &gltf2::Camera::Orthographic::mZFar)) - .Register(*json::MakeProperty("znear", json::Read::Number, &gltf2::Camera::Orthographic::mZNear))); - -const auto CAMERA_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Camera::mName)) - .Register(*json::MakeProperty("type", json::Read::StringView, &gltf2::Camera::mType)) - .Register(*json::MakeProperty("perspective", json::ObjectReader::Read, &gltf2::Camera::mPerspective)) - .Register(*json::MakeProperty("orthographic", json::ObjectReader::Read, &gltf2::Camera::mOrthographic))); - -const auto NODE_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Node::mName)) - .Register(*json::MakeProperty("translation", gltf2::ReadDaliVector, &gltf2::Node::mTranslation)) - .Register(*json::MakeProperty("rotation", gltf2::ReadQuaternion, &gltf2::Node::mRotation)) - .Register(*json::MakeProperty("scale", gltf2::ReadDaliVector, &gltf2::Node::mScale)) - .Register(*new json::Property("matrix", gltf2::ReadDaliVector, &gltf2::Node::SetMatrix)) - .Register(*json::MakeProperty("camera", gltf2::RefReader::Read, &gltf2::Node::mCamera)) - .Register(*json::MakeProperty("children", json::Read::Array, gltf2::RefReader::Read>, &gltf2::Node::mChildren)) - .Register(*json::MakeProperty("mesh", gltf2::RefReader::Read, &gltf2::Node::mMesh)) - .Register(*json::MakeProperty("skin", gltf2::RefReader::Read, &gltf2::Node::mSkin))); - -const auto ANIMATION_SAMPLER_READER = std::move(json::Reader() - .Register(*json::MakeProperty("input", gltf2::RefReader::Read, &gltf2::Animation::Sampler::mInput)) - .Register(*json::MakeProperty("output", gltf2::RefReader::Read, &gltf2::Animation::Sampler::mOutput)) - .Register(*json::MakeProperty("interpolation", gltf2::ReadStringEnum, &gltf2::Animation::Sampler::mInterpolation))); - -const auto ANIMATION_TARGET_READER = std::move(json::Reader() - .Register(*json::MakeProperty("node", gltf2::RefReader::Read, &gltf2::Animation::Channel::Target::mNode)) - .Register(*json::MakeProperty("path", gltf2::ReadStringEnum, &gltf2::Animation::Channel::Target::mPath))); - -const auto ANIMATION_CHANNEL_READER = std::move(json::Reader() - .Register(*json::MakeProperty("target", json::ObjectReader::Read, &gltf2::Animation::Channel::mTarget)) - .Register(*json::MakeProperty("sampler", gltf2::RefReader::Read, &gltf2::Animation::Channel::mSampler))); - -const auto ANIMATION_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Animation::mName)) - .Register(*json::MakeProperty("samplers", - json::Read::Array::Read>, - &gltf2::Animation::mSamplers)) - .Register(*json::MakeProperty("channels", - json::Read::Array::Read>, - &gltf2::Animation::mChannels))); - -const auto SCENE_READER = std::move(json::Reader() - .Register(*new json::Property("name", json::Read::StringView, &gltf2::Scene::mName)) - .Register(*json::MakeProperty("nodes", - json::Read::Array, gltf2::RefReader::Read>, - &gltf2::Scene::mNodes))); - -const auto DOCUMENT_READER = std::move(json::Reader() - .Register(*json::MakeProperty("buffers", - json::Read::Array::Read>, - &gltf2::Document::mBuffers)) - .Register(*json::MakeProperty("bufferViews", - json::Read::Array::Read>, - &gltf2::Document::mBufferViews)) - .Register(*json::MakeProperty("accessors", - json::Read::Array::Read>, - &gltf2::Document::mAccessors)) - .Register(*json::MakeProperty("images", - json::Read::Array::Read>, - &gltf2::Document::mImages)) - .Register(*json::MakeProperty("samplers", - json::Read::Array::Read>, - &gltf2::Document::mSamplers)) - .Register(*json::MakeProperty("textures", - json::Read::Array::Read>, - &gltf2::Document::mTextures)) - .Register(*json::MakeProperty("materials", - json::Read::Array::Read>, - &gltf2::Document::mMaterials)) - .Register(*json::MakeProperty("meshes", - json::Read::Array::Read>, - &gltf2::Document::mMeshes)) - .Register(*json::MakeProperty("skins", - json::Read::Array::Read>, - &gltf2::Document::mSkins)) - .Register(*json::MakeProperty("cameras", - json::Read::Array::Read>, - &gltf2::Document::mCameras)) - .Register(*json::MakeProperty("nodes", - json::Read::Array::Read>, - &gltf2::Document::mNodes)) - .Register(*json::MakeProperty("animations", - ReadAnimationArray, - &gltf2::Document::mAnimations)) - .Register(*json::MakeProperty("scenes", - json::Read::Array::Read>, - &gltf2::Document::mScenes)) - .Register(*json::MakeProperty("scene", gltf2::RefReader::Read, &gltf2::Document::mScene))); +const json::Reader& GetMeshPrimitiveReader() +{ + static const auto MESH_PRIMITIVE_READER = std::move(json::Reader() + .Register(*json::MakeProperty("attributes", ReadMeshPrimitiveAttributes, &gltf2::Mesh::Primitive::mAttributes)) + .Register(*json::MakeProperty("indices", gltf2::RefReader::Read, &gltf2::Mesh::Primitive::mIndices)) + .Register(*json::MakeProperty("material", gltf2::RefReader::Read, &gltf2::Mesh::Primitive::mMaterial)) + .Register(*json::MakeProperty("mode", json::Read::Enum, &gltf2::Mesh::Primitive::mMode)) + .Register(*json::MakeProperty("targets", ReadMeshPrimitiveTargets, &gltf2::Mesh::Primitive::mTargets))); + return MESH_PRIMITIVE_READER; +} + +const json::Reader& GetMeshReader() +{ + static const auto MESH_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Mesh::mName)) + .Register(*json::MakeProperty("primitives", + json::Read::Array::Read>, + &gltf2::Mesh::mPrimitives)) + .Register(*json::MakeProperty("weights", json::Read::Array, &gltf2::Mesh::mWeights))); + return MESH_READER; +} + +const json::Reader& GetSkinReader() +{ + static const auto SKIN_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Skin::mName)) + .Register(*json::MakeProperty("inverseBindMatrices", + gltf2::RefReader::Read, + &gltf2::Skin::mInverseBindMatrices)) + .Register(*json::MakeProperty("skeleton", + gltf2::RefReader::Read, + &gltf2::Skin::mSkeleton)) + .Register(*json::MakeProperty("joints", + json::Read::Array, gltf2::RefReader::Read>, + &gltf2::Skin::mJoints))); + return SKIN_READER; +} + +const json::Reader& GetCameraPerspectiveReader() +{ + static const auto CAMERA_PERSPECTIVE_READER = std::move(json::Reader() + .Register(*json::MakeProperty("aspectRatio", json::Read::Number, &gltf2::Camera::Perspective::mAspectRatio)) + .Register(*json::MakeProperty("yfov", json::Read::Number, &gltf2::Camera::Perspective::mYFov)) + .Register(*json::MakeProperty("zfar", json::Read::Number, &gltf2::Camera::Perspective::mZFar)) + .Register(*json::MakeProperty("znear", json::Read::Number, &gltf2::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted + return CAMERA_PERSPECTIVE_READER; +} + +const json::Reader& GetCameraOrthographicReader() +{ + static const auto CAMERA_ORTHOGRAPHIC_READER = std::move(json::Reader() + .Register(*json::MakeProperty("xmag", json::Read::Number, &gltf2::Camera::Orthographic::mXMag)) + .Register(*json::MakeProperty("ymag", json::Read::Number, &gltf2::Camera::Orthographic::mYMag)) + .Register(*json::MakeProperty("zfar", json::Read::Number, &gltf2::Camera::Orthographic::mZFar)) + .Register(*json::MakeProperty("znear", json::Read::Number, &gltf2::Camera::Orthographic::mZNear))); + return CAMERA_ORTHOGRAPHIC_READER; +} + +const json::Reader& GetCameraReader() +{ + static const auto CAMERA_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Camera::mName)) + .Register(*json::MakeProperty("type", json::Read::StringView, &gltf2::Camera::mType)) + .Register(*json::MakeProperty("perspective", json::ObjectReader::Read, &gltf2::Camera::mPerspective)) + .Register(*json::MakeProperty("orthographic", json::ObjectReader::Read, &gltf2::Camera::mOrthographic))); + return CAMERA_READER; +} + +const json::Reader& GetNodeReader() +{ + static const auto NODE_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Node::mName)) + .Register(*json::MakeProperty("translation", gltf2::ReadDaliVector, &gltf2::Node::mTranslation)) + .Register(*json::MakeProperty("rotation", gltf2::ReadQuaternion, &gltf2::Node::mRotation)) + .Register(*json::MakeProperty("scale", gltf2::ReadDaliVector, &gltf2::Node::mScale)) + .Register(*new json::Property("matrix", gltf2::ReadDaliVector, &gltf2::Node::SetMatrix)) + .Register(*json::MakeProperty("camera", gltf2::RefReader::Read, &gltf2::Node::mCamera)) + .Register(*json::MakeProperty("children", json::Read::Array, gltf2::RefReader::Read>, &gltf2::Node::mChildren)) + .Register(*json::MakeProperty("mesh", gltf2::RefReader::Read, &gltf2::Node::mMesh)) + .Register(*json::MakeProperty("skin", gltf2::RefReader::Read, &gltf2::Node::mSkin))); + return NODE_READER; +} + +const json::Reader& GetAnimationSamplerReader() +{ + static const auto ANIMATION_SAMPLER_READER = std::move(json::Reader() + .Register(*json::MakeProperty("input", gltf2::RefReader::Read, &gltf2::Animation::Sampler::mInput)) + .Register(*json::MakeProperty("output", gltf2::RefReader::Read, &gltf2::Animation::Sampler::mOutput)) + .Register(*json::MakeProperty("interpolation", gltf2::ReadStringEnum, &gltf2::Animation::Sampler::mInterpolation))); + return ANIMATION_SAMPLER_READER; +} + +const json::Reader& GetAnimationChannelTargetReader() +{ + static const auto ANIMATION_TARGET_READER = std::move(json::Reader() + .Register(*json::MakeProperty("node", gltf2::RefReader::Read, &gltf2::Animation::Channel::Target::mNode)) + .Register(*json::MakeProperty("path", gltf2::ReadStringEnum, &gltf2::Animation::Channel::Target::mPath))); + return ANIMATION_TARGET_READER; +} + +const json::Reader& GetAnimationChannelReader() +{ + static const auto ANIMATION_CHANNEL_READER = std::move(json::Reader() + .Register(*json::MakeProperty("target", json::ObjectReader::Read, &gltf2::Animation::Channel::mTarget)) + .Register(*json::MakeProperty("sampler", gltf2::RefReader::Read, &gltf2::Animation::Channel::mSampler))); + return ANIMATION_CHANNEL_READER; +} + +const json::Reader& GetAnimationReader() +{ + static const auto ANIMATION_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Animation::mName)) + .Register(*json::MakeProperty("samplers", + json::Read::Array::Read>, + &gltf2::Animation::mSamplers)) + .Register(*json::MakeProperty("channels", + json::Read::Array::Read>, + &gltf2::Animation::mChannels))); + return ANIMATION_READER; +} + +const json::Reader& GetSceneReader() +{ + static const auto SCENE_READER = std::move(json::Reader() + .Register(*new json::Property("name", json::Read::StringView, &gltf2::Scene::mName)) + .Register(*json::MakeProperty("nodes", + json::Read::Array, gltf2::RefReader::Read>, + &gltf2::Scene::mNodes))); + return SCENE_READER; +} + +const json::Reader& GetDocumentReader() +{ + static const auto DOCUMENT_READER = std::move(json::Reader() + .Register(*json::MakeProperty("buffers", + json::Read::Array::Read>, + &gltf2::Document::mBuffers)) + .Register(*json::MakeProperty("bufferViews", + json::Read::Array::Read>, + &gltf2::Document::mBufferViews)) + .Register(*json::MakeProperty("accessors", + json::Read::Array::Read>, + &gltf2::Document::mAccessors)) + .Register(*json::MakeProperty("images", + json::Read::Array::Read>, + &gltf2::Document::mImages)) + .Register(*json::MakeProperty("samplers", + json::Read::Array::Read>, + &gltf2::Document::mSamplers)) + .Register(*json::MakeProperty("textures", + json::Read::Array::Read>, + &gltf2::Document::mTextures)) + .Register(*json::MakeProperty("materials", + json::Read::Array::Read>, + &gltf2::Document::mMaterials)) + .Register(*json::MakeProperty("meshes", + json::Read::Array::Read>, + &gltf2::Document::mMeshes)) + .Register(*json::MakeProperty("skins", + json::Read::Array::Read>, + &gltf2::Document::mSkins)) + .Register(*json::MakeProperty("cameras", + json::Read::Array::Read>, + &gltf2::Document::mCameras)) + .Register(*json::MakeProperty("nodes", + json::Read::Array::Read>, + &gltf2::Document::mNodes)) + .Register(*json::MakeProperty("animations", + ReadAnimationArray, + &gltf2::Document::mAnimations)) + .Register(*json::MakeProperty("scenes", + json::Read::Array::Read>, + &gltf2::Document::mScenes)) + .Register(*json::MakeProperty("scene", gltf2::RefReader::Read, &gltf2::Document::mScene))); + return DOCUMENT_READER; +} void ConvertBuffer(const gltf2::Buffer& buffer, decltype(ResourceBundle::mBuffers)& outBuffers, const std::string& resourcePath) { @@ -428,7 +541,8 @@ void AddTextureStage(uint32_t semantic, MaterialDefinition& materialDefinition, void ConvertMaterial(const gltf2::Material& material, const std::unordered_map& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials, ConversionContext& context) { - auto getTextureMetaData = [](const std::unordered_map& metaData, const gltf2::TextureInfo& info) { + auto getTextureMetaData = [](const std::unordered_map& metaData, const gltf2::TextureInfo& info) + { if(!info.mTexture->mSource->mUri.empty()) { if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end()) @@ -444,12 +558,14 @@ void ConvertMaterial(const gltf2::Material& material, const std::unordered_mapsecond; + auto positionIter = attribs.find(gltf2::Attribute::POSITION); + + if(positionIter == attribs.end()) + { + DALI_LOG_ERROR("Primitive mesh dosn't have POSITION atrributes!"); + continue; + } + + auto& accPositions = *positionIter->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 @@ -770,7 +894,8 @@ void ConvertNode(gltf2::Node const& node, const Index gltfIndex, Index parentInd auto& resources = output.mResources; const auto index = scene.GetNodeCount(); - auto weakNode = scene.AddNode([&]() { + auto weakNode = scene.AddNode([&]() + { std::unique_ptr nodeDefinition{new NodeDefinition()}; nodeDefinition->mParentIdx = parentIndex; @@ -954,7 +1079,7 @@ float LoadKeyFrames(ConversionContext& context, const gltf2::Animation::Channel& return duration; } -float LoadBlendShapeKeyFrames(ConversionContext& context, const gltf2::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, std::vector& properties) +float LoadBlendShapeKeyFrames(ConversionContext& context, const gltf2::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, AnimationDefinition& animationDefinition) { const gltf2::Accessor& input = *channel.mSampler->mInput; const gltf2::Accessor& output = *channel.mSampler->mOutput; @@ -970,7 +1095,7 @@ float LoadBlendShapeKeyFrames(ConversionContext& context, const gltf2::Animation const auto remainingSize = sizeof(weightNameBuffer) - prefixSize; for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex) { - AnimatedProperty& animatedProperty = properties[propertyIndex++]; + AnimatedProperty animatedProperty; animatedProperty.mNodeIndex = nodeIndex; snprintf(pWeightName, remainingSize, "%d]", weightIndex); @@ -990,6 +1115,8 @@ float LoadBlendShapeKeyFrames(ConversionContext& context, const gltf2::Animation } animatedProperty.mTimePeriod = {0.f, duration}; + + animationDefinition.SetProperty(propertyIndex++, std::move(animatedProperty)); } return duration; @@ -998,8 +1125,7 @@ float LoadBlendShapeKeyFrames(ConversionContext& context, const gltf2::Animation template float LoadAnimation(AnimationDefinition& animationDefinition, Index nodeIndex, Index propertyIndex, const std::string& propertyName, const gltf2::Animation::Channel& channel, ConversionContext& context) { - AnimatedProperty& animatedProperty = animationDefinition.mProperties[propertyIndex]; - + AnimatedProperty animatedProperty; animatedProperty.mNodeIndex = nodeIndex; animatedProperty.mPropertyName = propertyName; @@ -1007,6 +1133,7 @@ float LoadAnimation(AnimationDefinition& animationDefinition, Index nodeIndex, I float duration = LoadKeyFrames(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath); animatedProperty.mTimePeriod = {0.f, duration}; + animationDefinition.SetProperty(propertyIndex, std::move(animatedProperty)); return duration; } @@ -1022,7 +1149,7 @@ void ConvertAnimations(const gltf2::Document& document, ConversionContext& conte if(!animation.mName.empty()) { - animationDefinition.mName = animation.mName; + animationDefinition.SetName(animation.mName.data()); } uint32_t numberOfProperties = 0u; @@ -1037,7 +1164,7 @@ void ConvertAnimations(const gltf2::Document& document, ConversionContext& conte numberOfProperties++; } } - animationDefinition.mProperties.resize(numberOfProperties); + animationDefinition.ReserveSize(numberOfProperties); Index propertyIndex = 0u; for(const auto& channel : animation.mChannels) @@ -1064,7 +1191,7 @@ void ConvertAnimations(const gltf2::Document& document, ConversionContext& conte } case gltf2::Animation::Channel::Target::WEIGHTS: { - duration = LoadBlendShapeKeyFrames(context, channel, nodeIndex, propertyIndex, animationDefinition.mProperties); + duration = LoadBlendShapeKeyFrames(context, channel, nodeIndex, propertyIndex, animationDefinition); break; } @@ -1075,7 +1202,7 @@ void ConvertAnimations(const gltf2::Document& document, ConversionContext& conte } } - animationDefinition.mDuration = std::max(duration, animationDefinition.mDuration); + animationDefinition.SetDuration(std::max(duration, animationDefinition.GetDuration())); ++propertyIndex; } @@ -1184,33 +1311,33 @@ void ProduceShaders(ShaderDefinitionFactory& shaderFactory, Dali::Scene3D::Loade void SetObjectReaders() { - json::SetObjectReader(BUFFER_READER); - json::SetObjectReader(BUFFER_VIEW_READER); - json::SetObjectReader(BUFFER_VIEW_CLIENT_READER); - json::SetObjectReader(COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER); - json::SetObjectReader(ACCESSOR_SPARSE_READER); - json::SetObjectReader(ACCESSOR_READER); - json::SetObjectReader(IMAGE_READER); - json::SetObjectReader(SAMPLER_READER); - json::SetObjectReader(TEXURE_READER); - json::SetObjectReader(TEXURE_INFO_READER); - json::SetObjectReader(MATERIAL_PBR_READER); - json::SetObjectReader(MATERIAL_SPECULAR_READER); - json::SetObjectReader(MATERIAL_IOR_READER); - json::SetObjectReader(MATERIAL_EXTENSION_READER); - json::SetObjectReader(MATERIAL_READER); - json::SetObjectReader(MESH_PRIMITIVE_READER); - json::SetObjectReader(MESH_READER); - json::SetObjectReader(SKIN_READER); - json::SetObjectReader(CAMERA_PERSPECTIVE_READER); - json::SetObjectReader(CAMERA_ORTHOGRAPHIC_READER); - json::SetObjectReader(CAMERA_READER); - json::SetObjectReader(NODE_READER); - json::SetObjectReader(ANIMATION_SAMPLER_READER); - json::SetObjectReader(ANIMATION_TARGET_READER); - json::SetObjectReader(ANIMATION_CHANNEL_READER); - json::SetObjectReader(ANIMATION_READER); - json::SetObjectReader(SCENE_READER); + json::SetObjectReader(GetBufferReader()); + json::SetObjectReader(GetBufferViewReader()); + json::SetObjectReader(GetBufferViewClientReader()); + json::SetObjectReader(GetComponentTypedBufferViewClientReader()); + json::SetObjectReader(GetAccessorSparseReader()); + json::SetObjectReader(GetAccessorReader()); + json::SetObjectReader(GetImageReader()); + json::SetObjectReader(GetSamplerReader()); + json::SetObjectReader(GetTextureReader()); + json::SetObjectReader(GetTextureInfoReader()); + json::SetObjectReader(GetMaterialPbrReader()); + json::SetObjectReader(GetMaterialSpecularReader()); + json::SetObjectReader(GetMaterialIorReader()); + json::SetObjectReader(GetMaterialExtensionsReader()); + json::SetObjectReader(GetMaterialReader()); + json::SetObjectReader(GetMeshPrimitiveReader()); + json::SetObjectReader(GetMeshReader()); + json::SetObjectReader(GetSkinReader()); + json::SetObjectReader(GetCameraPerspectiveReader()); + json::SetObjectReader(GetCameraOrthographicReader()); + json::SetObjectReader(GetCameraReader()); + json::SetObjectReader(GetNodeReader()); + json::SetObjectReader(GetAnimationSamplerReader()); + json::SetObjectReader(GetAnimationChannelTargetReader()); + json::SetObjectReader(GetAnimationChannelReader()); + json::SetObjectReader(GetAnimationReader()); + json::SetObjectReader(GetSceneReader()); } void SetDefaultEnvironmentMap(const gltf2::Document& document, ConversionContext& context) @@ -1244,7 +1371,7 @@ const std::string_view GetRendererModelIdentification() void ReadDocument(const json_object_s& jsonObject, gltf2::Document& document) { - DOCUMENT_READER.Read(jsonObject, document); + GetDocumentReader().Read(jsonObject, document); } void ReadDocumentFromParsedData(const json_object_s& jsonObject, gltf2::Document& document)