[dali_2.1.1] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene-loader / public-api / gltf2-loader.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include "dali-scene-loader/public-api/gltf2-loader.h"
18 #include <fstream>
19 #include "dali-scene-loader/internal/gltf2-asset.h"
20 #include "dali-scene-loader/public-api/load-result.h"
21 #include "dali-scene-loader/public-api/resource-bundle.h"
22 #include "dali-scene-loader/public-api/scene-definition.h"
23 #include "dali-scene-loader/public-api/shader-definition-factory.h"
24 #include "dali-scene-loader/public-api/utils.h"
25 #include "dali/public-api/math/quaternion.h"
26
27 #define ENUM_STRING_MAPPING(t, x) \
28   {                               \
29 #x, t::x                      \
30   }
31
32 namespace gt = gltf2;
33 namespace js = json;
34
35 namespace Dali
36 {
37 namespace SceneLoader
38 {
39 namespace
40 {
41 const std::string POSITION_PROPERTY("position");
42 const std::string ORIENTATION_PROPERTY("orientation");
43 const std::string SCALE_PROPERTY("scale");
44 const std::string BLEND_SHAPE_WEIGHTS_UNIFORM("uBlendShapeWeight");
45
46 const Geometry::Type GLTF2_TO_DALI_PRIMITIVES[]{
47   Geometry::POINTS,
48   Geometry::LINES,
49   Geometry::LINE_LOOP,
50   Geometry::LINE_STRIP,
51   Geometry::TRIANGLES,
52   Geometry::TRIANGLE_STRIP,
53   Geometry::TRIANGLE_FAN}; //...because Dali swaps the last two.
54
55 struct AttributeMapping
56 {
57   gt::Attribute::Type      mType;
58   MeshDefinition::Accessor MeshDefinition::*mAccessor;
59   uint16_t                                  mElementSizeRequired;
60 } ATTRIBUTE_MAPPINGS[]{
61   {gt::Attribute::NORMAL, &MeshDefinition::mNormals, sizeof(Vector3)},
62   {gt::Attribute::TANGENT, &MeshDefinition::mTangents, sizeof(Vector3)},
63   {gt::Attribute::TEXCOORD_0, &MeshDefinition::mTexCoords, sizeof(Vector2)},
64   {gt::Attribute::JOINTS_0, &MeshDefinition::mJoints0, sizeof(Vector4)},
65   {gt::Attribute::WEIGHTS_0, &MeshDefinition::mWeights0, sizeof(Vector4)},
66 };
67
68 std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
69 {
70   gt::Animation proxy;
71   SetRefReaderObject(proxy);
72
73   auto results = js::Read::Array<gt::Animation, js::ObjectReader<gt::Animation>::Read>(j);
74
75   for(auto& animation : results)
76   {
77     for(auto& channel : animation.mChannels)
78     {
79       channel.mSampler.UpdateVector(animation.mSamplers);
80     }
81   }
82
83   return results;
84 }
85
86 void ApplyAccessorMinMax(const gt::Accessor& acc, float* values)
87 {
88   DALI_ASSERT_ALWAYS(acc.mMax.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMax.size());
89   DALI_ASSERT_ALWAYS(acc.mMin.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMin.size());
90   MeshDefinition::Blob::ApplyMinMax(acc.mMin, acc.mMax, acc.mCount, values);
91 }
92
93 const auto BUFFER_READER = std::move(js::Reader<gt::Buffer>()
94                                        .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, &gt::Buffer::mByteLength))
95                                        .Register(*js::MakeProperty("uri", js::Read::StringView, &gt::Buffer::mUri)));
96
97 const auto BUFFER_VIEW_READER = std::move(js::Reader<gt::BufferView>()
98                                             .Register(*js::MakeProperty("buffer", gt::RefReader<gt::Document>::Read<gt::Buffer, &gt::Document::mBuffers>, &gt::BufferView::mBuffer))
99                                             .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, &gt::BufferView::mByteOffset))
100                                             .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, &gt::BufferView::mByteLength))
101                                             .Register(*js::MakeProperty("byteStride", js::Read::Number<uint32_t>, &gt::BufferView::mByteStride))
102                                             .Register(*js::MakeProperty("target", js::Read::Number<uint32_t>, &gt::BufferView::mTarget)));
103
104 const auto BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::BufferViewClient>()
105                                                    .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>, &gt::BufferViewClient::mBufferView))
106                                                    .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, &gt::BufferViewClient::mByteOffset)));
107
108 const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::ComponentTypedBufferViewClient>()
109                                                                    .Register(*new js::Property<gt::ComponentTypedBufferViewClient, gt::Ref<gt::BufferView>>("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>, &gt::ComponentTypedBufferViewClient::mBufferView))
110                                                                    .Register(*new js::Property<gt::ComponentTypedBufferViewClient, uint32_t>("byteOffset", js::Read::Number<uint32_t>, &gt::ComponentTypedBufferViewClient::mByteOffset))
111                                                                    .Register(*js::MakeProperty("componentType", js::Read::Enum<gt::Component::Type>, &gt::ComponentTypedBufferViewClient::mComponentType)));
112
113 const auto ACCESSOR_SPARSE_READER = std::move(js::Reader<gt::Accessor::Sparse>()
114                                                 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, &gt::Accessor::Sparse::mCount))
115                                                 .Register(*js::MakeProperty("indices", js::ObjectReader<gt::ComponentTypedBufferViewClient>::Read, &gt::Accessor::Sparse::mIndices))
116                                                 .Register(*js::MakeProperty("values", js::ObjectReader<gt::BufferViewClient>::Read, &gt::Accessor::Sparse::mValues)));
117
118 const auto ACCESSOR_READER = std::move(js::Reader<gt::Accessor>()
119                                          .Register(*new js::Property<gt::Accessor, gt::Ref<gt::BufferView>>("bufferView",
120                                                                                                             gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>,
121                                                                                                             &gt::Accessor::mBufferView))
122                                          .Register(*new js::Property<gt::Accessor, uint32_t>("byteOffset",
123                                                                                              js::Read::Number<uint32_t>,
124                                                                                              &gt::Accessor::mByteOffset))
125                                          .Register(*new js::Property<gt::Accessor, gt::Component::Type>("componentType",
126                                                                                                         js::Read::Enum<gt::Component::Type>,
127                                                                                                         &gt::Accessor::mComponentType))
128                                          .Register(*new js::Property<gt::Accessor, std::string_view>("name", js::Read::StringView, &gt::Accessor::mName))
129                                          .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, &gt::Accessor::mCount))
130                                          .Register(*js::MakeProperty("normalized", js::Read::Boolean, &gt::Accessor::mNormalized))
131                                          .Register(*js::MakeProperty("type", gt::ReadStringEnum<gt::AccessorType>, &gt::Accessor::mType))
132                                          .Register(*js::MakeProperty("min", js::Read::Array<float, js::Read::Number>, &gt::Accessor::mMin))
133                                          .Register(*js::MakeProperty("max", js::Read::Array<float, js::Read::Number>, &gt::Accessor::mMax))
134                                          .Register(*new js::Property<gt::Accessor, gt::Accessor::Sparse>("sparse", js::ObjectReader<gt::Accessor::Sparse>::Read, &gt::Accessor::SetSparse)));
135
136 const auto IMAGE_READER = std::move(js::Reader<gt::Image>()
137                                       .Register(*new js::Property<gt::Image, std::string_view>("name", js::Read::StringView, &gt::Material::mName))
138                                       .Register(*js::MakeProperty("uri", js::Read::StringView, &gt::Image::mUri))
139                                       .Register(*js::MakeProperty("mimeType", js::Read::StringView, &gt::Image::mMimeType))
140                                       .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>, &gt::Image::mBufferView)));
141
142 const auto SAMPLER_READER = std::move(js::Reader<gt::Sampler>()
143                                         .Register(*js::MakeProperty("minFilter", js::Read::Enum<gt::Filter::Type>, &gt::Sampler::mMinFilter))
144                                         .Register(*js::MakeProperty("magFilter", js::Read::Enum<gt::Filter::Type>, &gt::Sampler::mMagFilter))
145                                         .Register(*js::MakeProperty("wrapS", js::Read::Enum<gt::Wrap::Type>, &gt::Sampler::mWrapS))
146                                         .Register(*js::MakeProperty("wrapT", js::Read::Enum<gt::Wrap::Type>, &gt::Sampler::mWrapT)));
147
148 const auto TEXURE_READER = std::move(js::Reader<gt::Texture>()
149                                        .Register(*js::MakeProperty("source", gt::RefReader<gt::Document>::Read<gt::Image, &gt::Document::mImages>, &gt::Texture::mSource))
150                                        .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Document>::Read<gt::Sampler, &gt::Document::mSamplers>, &gt::Texture::mSampler)));
151
152 const auto TEXURE_INFO_READER = std::move(js::Reader<gt::TextureInfo>()
153                                             .Register(*js::MakeProperty("index", gt::RefReader<gt::Document>::Read<gt::Texture, &gt::Document::mTextures>, &gt::TextureInfo::mTexture))
154                                             .Register(*js::MakeProperty("texCoord", js::Read::Number<uint32_t>, &gt::TextureInfo::mTexCoord))
155                                             .Register(*js::MakeProperty("scale", js::Read::Number<float>, &gt::TextureInfo::mScale)));
156
157 const auto MATERIAL_PBR_READER = std::move(js::Reader<gt::Material::Pbr>()
158                                              .Register(*js::MakeProperty("baseColorFactor", gt::ReadDaliVector<Vector4>, &gt::Material::Pbr::mBaseColorFactor))
159                                              .Register(*js::MakeProperty("baseColorTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::Pbr::mBaseColorTexture))
160                                              .Register(*js::MakeProperty("metallicFactor", js::Read::Number<float>, &gt::Material::Pbr::mMetallicFactor))
161                                              .Register(*js::MakeProperty("roughnessFactor", js::Read::Number<float>, &gt::Material::Pbr::mRoughnessFactor))
162                                              .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::Pbr::mMetallicRoughnessTexture)));
163
164 const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
165                                          .Register(*new js::Property<gt::Material, std::string_view>("name", js::Read::StringView, &gt::Material::mName))
166                                          .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader<gt::Material::Pbr>::Read, &gt::Material::mPbrMetallicRoughness))
167                                          .Register(*js::MakeProperty("normalTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::mNormalTexture))
168                                          .Register(*js::MakeProperty("occlusionTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::mOcclusionTexture))
169                                          .Register(*js::MakeProperty("emissiveTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::mEmissiveTexture))
170                                          .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector<Vector3>, &gt::Material::mEmissiveFactor))
171                                          .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum<gt::AlphaMode>, &gt::Material::mAlphaMode))
172                                          .Register(*js::MakeProperty("alphaCutoff", js::Read::Number<float>, &gt::Material::mAlphaCutoff)));
173
174 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
175 {
176   auto&                                                jo = js::Cast<json_object_s>(j);
177   std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> result;
178
179   auto i = jo.start;
180   while(i)
181   {
182     auto jstr                                                        = *i->name;
183     result[gt::Attribute::FromString(jstr.string, jstr.string_size)] = gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>(*i->value);
184     i                                                                = i->next;
185   }
186   return result;
187 }
188
189 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> ReadMeshPrimitiveTargets(const json_value_s& j)
190 {
191   auto&                                                             jo = js::Cast<json_array_s>(j);
192   std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> result;
193
194   result.reserve(jo.length);
195
196   auto i = jo.start;
197   while(i)
198   {
199     result.push_back(std::move(ReadMeshPrimitiveAttributes(*i->value)));
200     i = i->next;
201   }
202
203   return result;
204 }
205
206 const auto MESH_PRIMITIVE_READER = std::move(js::Reader<gt::Mesh::Primitive>()
207                                                .Register(*js::MakeProperty("attributes", ReadMeshPrimitiveAttributes, &gt::Mesh::Primitive::mAttributes))
208                                                .Register(*js::MakeProperty("indices", gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>, &gt::Mesh::Primitive::mIndices))
209                                                .Register(*js::MakeProperty("material", gt::RefReader<gt::Document>::Read<gt::Material, &gt::Document::mMaterials>, &gt::Mesh::Primitive::mMaterial))
210                                                .Register(*js::MakeProperty("mode", js::Read::Enum<gt::Mesh::Primitive::Mode>, &gt::Mesh::Primitive::mMode))
211                                                .Register(*js::MakeProperty("targets", ReadMeshPrimitiveTargets, &gt::Mesh::Primitive::mTargets)));
212
213 const auto MESH_READER = std::move(js::Reader<gt::Mesh>()
214                                      .Register(*new js::Property<gt::Mesh, std::string_view>("name", js::Read::StringView, &gt::Mesh::mName))
215                                      .Register(*js::MakeProperty("primitives",
216                                                                  js::Read::Array<gt::Mesh::Primitive, js::ObjectReader<gt::Mesh::Primitive>::Read>,
217                                                                  &gt::Mesh::mPrimitives))
218                                      .Register(*js::MakeProperty("weights", js::Read::Array<float, js::Read::Number>, &gt::Mesh::mWeights)));
219
220 const auto SKIN_READER = std::move(js::Reader<gt::Skin>()
221                                      .Register(*new js::Property<gt::Skin, std::string_view>("name", js::Read::StringView, &gt::Skin::mName))
222                                      .Register(*js::MakeProperty("inverseBindMatrices",
223                                                                  gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>,
224                                                                  &gt::Skin::mInverseBindMatrices))
225                                      .Register(*js::MakeProperty("skeleton",
226                                                                  gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>,
227                                                                  &gt::Skin::mSkeleton))
228                                      .Register(*js::MakeProperty("joints",
229                                                                  js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>>,
230                                                                  &gt::Skin::mJoints)));
231
232 const auto CAMERA_PERSPECTIVE_READER = std::move(js::Reader<gt::Camera::Perspective>()
233                                                    .Register(*js::MakeProperty("aspectRatio", js::Read::Number<float>, &gt::Camera::Perspective::mAspectRatio))
234                                                    .Register(*js::MakeProperty("yfov", js::Read::Number<float>, &gt::Camera::Perspective::mYFov))
235                                                    .Register(*js::MakeProperty("zfar", js::Read::Number<float>, &gt::Camera::Perspective::mZFar))
236                                                    .Register(*js::MakeProperty("znear", js::Read::Number<float>, &gt::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted
237
238 const auto CAMERA_ORTHOGRAPHIC_READER = std::move(js::Reader<gt::Camera::Orthographic>()
239                                                     .Register(*js::MakeProperty("xmag", js::Read::Number<float>, &gt::Camera::Orthographic::mXMag))
240                                                     .Register(*js::MakeProperty("ymag", js::Read::Number<float>, &gt::Camera::Orthographic::mXMag))
241                                                     .Register(*js::MakeProperty("zfar", js::Read::Number<float>, &gt::Camera::Orthographic::mZFar))
242                                                     .Register(*js::MakeProperty("znear", js::Read::Number<float>, &gt::Camera::Orthographic::mZNear)));
243
244 const auto CAMERA_READER = std::move(js::Reader<gt::Camera>()
245                                        .Register(*new js::Property<gt::Camera, std::string_view>("name", js::Read::StringView, &gt::Camera::mName))
246                                        .Register(*js::MakeProperty("type", js::Read::StringView, &gt::Camera::mType))
247                                        .Register(*js::MakeProperty("perspective", js::ObjectReader<gt::Camera::Perspective>::Read, &gt::Camera::mPerspective))
248                                        .Register(*js::MakeProperty("orthographic", js::ObjectReader<gt::Camera::Orthographic>::Read, &gt::Camera::mOrthographic)));
249
250 const auto NODE_READER = std::move(js::Reader<gt::Node>()
251                                      .Register(*new js::Property<gt::Node, std::string_view>("name", js::Read::StringView, &gt::Node::mName))
252                                      .Register(*js::MakeProperty("translation", gt::ReadDaliVector<Vector3>, &gt::Node::mTranslation))
253                                      .Register(*js::MakeProperty("rotation", gt::ReadQuaternion, &gt::Node::mRotation))
254                                      .Register(*js::MakeProperty("scale", gt::ReadDaliVector<Vector3>, &gt::Node::mScale))
255                                      .Register(*new js::Property<gt::Node, Matrix>("matrix", gt::ReadDaliVector<Matrix>, &gt::Node::SetMatrix))
256                                      .Register(*js::MakeProperty("camera", gt::RefReader<gt::Document>::Read<gt::Camera, &gt::Document::mCameras>, &gt::Node::mCamera))
257                                      .Register(*js::MakeProperty("children", js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>>, &gt::Node::mChildren))
258                                      .Register(*js::MakeProperty("mesh", gt::RefReader<gt::Document>::Read<gt::Mesh, &gt::Document::mMeshes>, &gt::Node::mMesh))
259                                      .Register(*js::MakeProperty("skin", gt::RefReader<gt::Document>::Read<gt::Skin, &gt::Document::mSkins>, &gt::Node::mSkin)));
260
261 const auto ANIMATION_SAMPLER_READER = std::move(js::Reader<gt::Animation::Sampler>()
262                                                   .Register(*js::MakeProperty("input", gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>, &gt::Animation::Sampler::mInput))
263                                                   .Register(*js::MakeProperty("output", gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>, &gt::Animation::Sampler::mOutput))
264                                                   .Register(*js::MakeProperty("interpolation", gt::ReadStringEnum<gt::Animation::Sampler::Interpolation>, &gt::Animation::Sampler::mInterpolation)));
265
266 const auto ANIMATION_TARGET_READER = std::move(js::Reader<gt::Animation::Channel::Target>()
267                                                  .Register(*js::MakeProperty("node", gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>, &gt::Animation::Channel::Target::mNode))
268                                                  .Register(*js::MakeProperty("path", gt::ReadStringEnum<gt::Animation::Channel::Target>, &gt::Animation::Channel::Target::mPath)));
269
270 const auto ANIMATION_CHANNEL_READER = std::move(js::Reader<gt::Animation::Channel>()
271                                                   .Register(*js::MakeProperty("target", js::ObjectReader<gt::Animation::Channel::Target>::Read, &gt::Animation::Channel::mTarget))
272                                                   .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Animation>::Read<gt::Animation::Sampler, &gt::Animation::mSamplers>, &gt::Animation::Channel::mSampler)));
273
274 const auto ANIMATION_READER = std::move(js::Reader<gt::Animation>()
275                                           .Register(*new js::Property<gt::Animation, std::string_view>("name", js::Read::StringView, &gt::Animation::mName))
276                                           .Register(*js::MakeProperty("samplers",
277                                                                       js::Read::Array<gt::Animation::Sampler, js::ObjectReader<gt::Animation::Sampler>::Read>,
278                                                                       &gt::Animation::mSamplers))
279                                           .Register(*js::MakeProperty("channels",
280                                                                       js::Read::Array<gt::Animation::Channel, js::ObjectReader<gt::Animation::Channel>::Read>,
281                                                                       &gt::Animation::mChannels)));
282
283 const auto SCENE_READER = std::move(js::Reader<gt::Scene>()
284                                       .Register(*new js::Property<gt::Scene, std::string_view>("name", js::Read::StringView, &gt::Scene::mName))
285                                       .Register(*js::MakeProperty("nodes",
286                                                                   js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>>,
287                                                                   &gt::Scene::mNodes)));
288
289 const auto DOCUMENT_READER = std::move(js::Reader<gt::Document>()
290                                          .Register(*js::MakeProperty("buffers",
291                                                                      js::Read::Array<gt::Buffer, js::ObjectReader<gt::Buffer>::Read>,
292                                                                      &gt::Document::mBuffers))
293                                          .Register(*js::MakeProperty("bufferViews",
294                                                                      js::Read::Array<gt::BufferView, js::ObjectReader<gt::BufferView>::Read>,
295                                                                      &gt::Document::mBufferViews))
296                                          .Register(*js::MakeProperty("accessors",
297                                                                      js::Read::Array<gt::Accessor, js::ObjectReader<gt::Accessor>::Read>,
298                                                                      &gt::Document::mAccessors))
299                                          .Register(*js::MakeProperty("images",
300                                                                      js::Read::Array<gt::Image, js::ObjectReader<gt::Image>::Read>,
301                                                                      &gt::Document::mImages))
302                                          .Register(*js::MakeProperty("samplers",
303                                                                      js::Read::Array<gt::Sampler, js::ObjectReader<gt::Sampler>::Read>,
304                                                                      &gt::Document::mSamplers))
305                                          .Register(*js::MakeProperty("textures",
306                                                                      js::Read::Array<gt::Texture, js::ObjectReader<gt::Texture>::Read>,
307                                                                      &gt::Document::mTextures))
308                                          .Register(*js::MakeProperty("materials",
309                                                                      js::Read::Array<gt::Material, js::ObjectReader<gt::Material>::Read>,
310                                                                      &gt::Document::mMaterials))
311                                          .Register(*js::MakeProperty("meshes",
312                                                                      js::Read::Array<gt::Mesh, js::ObjectReader<gt::Mesh>::Read>,
313                                                                      &gt::Document::mMeshes))
314                                          .Register(*js::MakeProperty("skins",
315                                                                      js::Read::Array<gt::Skin, js::ObjectReader<gt::Skin>::Read>,
316                                                                      &gt::Document::mSkins))
317                                          .Register(*js::MakeProperty("cameras",
318                                                                      js::Read::Array<gt::Camera, js::ObjectReader<gt::Camera>::Read>,
319                                                                      &gt::Document::mCameras))
320                                          .Register(*js::MakeProperty("nodes",
321                                                                      js::Read::Array<gt::Node, js::ObjectReader<gt::Node>::Read>,
322                                                                      &gt::Document::mNodes))
323                                          .Register(*js::MakeProperty("animations",
324                                                                      ReadAnimationArray,
325                                                                      &gt::Document::mAnimations))
326                                          .Register(*js::MakeProperty("scenes",
327                                                                      js::Read::Array<gt::Scene, js::ObjectReader<gt::Scene>::Read>,
328                                                                      &gt::Document::mScenes))
329                                          .Register(*js::MakeProperty("scene", gt::RefReader<gt::Document>::Read<gt::Scene, &gt::Document::mScenes>, &gt::Document::mScene)));
330
331 struct NodeMapping
332 {
333   Index gltfIdx;
334   Index runtimeIdx;
335 };
336
337 bool operator<(const NodeMapping& mapping, Index gltfIdx)
338 {
339   return mapping.gltfIdx < gltfIdx;
340 }
341
342 class NodeIndexMapper
343 {
344 public:
345   NodeIndexMapper()                       = default;
346   NodeIndexMapper(const NodeIndexMapper&) = delete;
347   NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
348
349   ///@brief Registers a mapping of the @a gltfIdx of a node to its @a runtimeIdx .
350   ///@note If the indices are the same, the registration is omitted, in order to
351   /// save growing a vector.
352   void RegisterMapping(Index gltfIdx, Index runtimeIdx)
353   {
354     if(gltfIdx != runtimeIdx)
355     {
356       auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx);
357       DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIdx != gltfIdx);
358       mNodes.insert(iInsert, NodeMapping{gltfIdx, runtimeIdx});
359     }
360   }
361
362   ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIdx.
363   Index GetRuntimeId(Index gltfIdx) const
364   {
365     auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx); // using custom operator<
366     return (iFind != mNodes.end() && iFind->gltfIdx == gltfIdx) ? iFind->runtimeIdx : gltfIdx;
367   }
368
369 private:
370   std::vector<NodeMapping> mNodes;
371 };
372
373 struct ConversionContext
374 {
375   LoadResult& mOutput;
376
377   std::string mPath;
378   Index       mDefaultMaterial;
379
380   std::vector<Index> mMeshIds;
381   NodeIndexMapper    mNodeIndices;
382 };
383
384 SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type w)
385 {
386   switch(w)
387   {
388     case gt::Wrap::REPEAT:
389       return SamplerFlags::WRAP_REPEAT;
390     case gt::Wrap::CLAMP_TO_EDGE:
391       return SamplerFlags::WRAP_CLAMP;
392     case gt::Wrap::MIRRORED_REPEAT:
393       return SamplerFlags::WRAP_MIRROR;
394     default:
395       throw std::runtime_error("Invalid wrap type.");
396   }
397 }
398
399 SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& s)
400 {
401   if(s)
402   {
403     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);
404   }
405   else
406   {
407     // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texturesampler
408     // "The index of the sampler used by this texture. When undefined, a sampler with repeat wrapping and auto filtering should be used."
409     // "What is an auto filtering", I hear you ask. Since there's nothing else to determine mipmapping from - including glTF image
410     // properties, if not in some extension -, we will simply assume linear filtering.
411     return SamplerFlags::FILTER_LINEAR | (SamplerFlags::FILTER_LINEAR << SamplerFlags::FILTER_MAG_SHIFT) |
412            (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_S_SHIFT) | (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_T_SHIFT);
413   }
414 }
415
416 TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm)
417 {
418   return TextureDefinition{std::string(mm.mTexture->mSource->mUri), ConvertSampler(mm.mTexture->mSampler)};
419 }
420
421 void ConvertMaterial(const gt::Material& m, decltype(ResourceBundle::mMaterials)& outMaterials)
422 {
423   MaterialDefinition matDef;
424
425   auto& pbr = m.mPbrMetallicRoughness;
426   if(m.mAlphaMode != gt::AlphaMode::OPAQUE || pbr.mBaseColorFactor.a < 1.f)
427   {
428     matDef.mFlags |= MaterialDefinition::TRANSPARENCY;
429   }
430
431   if(m.mAlphaMode == gt::AlphaMode::MASK)
432   {
433     matDef.SetAlphaCutoff(std::min(1.f, std::max(0.f, m.mAlphaCutoff)));
434   }
435
436   matDef.mColor = pbr.mBaseColorFactor;
437
438   matDef.mTextureStages.reserve(!!pbr.mBaseColorTexture + !!pbr.mMetallicRoughnessTexture + !!m.mNormalTexture);
439   if(pbr.mBaseColorTexture)
440   {
441     const auto semantic = MaterialDefinition::ALBEDO;
442     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture)});
443     // TODO: and there had better be one
444     matDef.mFlags |= semantic;
445   }
446
447   matDef.mMetallic  = pbr.mMetallicFactor;
448   matDef.mRoughness = pbr.mRoughnessFactor;
449
450   if(pbr.mMetallicRoughnessTexture)
451   {
452     const auto semantic = MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
453                           MaterialDefinition::GLTF_CHANNELS;
454     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture)});
455     // TODO: and there had better be one
456     matDef.mFlags |= semantic;
457   }
458
459   if(m.mNormalTexture)
460   {
461     const auto semantic = MaterialDefinition::NORMAL;
462     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(m.mNormalTexture)});
463     // TODO: and there had better be one
464     matDef.mFlags |= semantic;
465   }
466
467   // TODO: handle doubleSided
468
469   outMaterials.emplace_back(std::move(matDef), TextureSet());
470 }
471
472 void ConvertMaterials(const gt::Document& doc, ConversionContext& cctx)
473 {
474   auto& outMaterials = cctx.mOutput.mResources.mMaterials;
475   outMaterials.reserve(doc.mMaterials.size());
476
477   for(auto& m : doc.mMaterials)
478   {
479     ConvertMaterial(m, outMaterials);
480   }
481 }
482
483 MeshDefinition::Accessor ConvertMeshPrimitiveAccessor(const gt::Accessor& acc)
484 {
485   DALI_ASSERT_ALWAYS((acc.mBufferView &&
486                       (acc.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) ||
487                      (acc.mSparse && !acc.mBufferView));
488
489   DALI_ASSERT_ALWAYS(!acc.mSparse ||
490                      ((acc.mSparse->mIndices.mBufferView && (acc.mSparse->mIndices.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) &&
491                       (acc.mSparse->mValues.mBufferView && (acc.mSparse->mValues.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max()))));
492
493   MeshDefinition::SparseBlob sparseBlob;
494   if(acc.mSparse)
495   {
496     const gt::Accessor::Sparse&               sparse  = *acc.mSparse;
497     const gt::ComponentTypedBufferViewClient& indices = sparse.mIndices;
498     const gt::BufferViewClient&               values  = sparse.mValues;
499
500     MeshDefinition::Blob indicesBlob(
501       indices.mBufferView->mByteOffset + indices.mByteOffset,
502       sparse.mCount * indices.GetBytesPerComponent(),
503       static_cast<uint16_t>(indices.mBufferView->mByteStride),
504       static_cast<uint16_t>(indices.GetBytesPerComponent()),
505       {},
506       {});
507     MeshDefinition::Blob valuesBlob(
508       values.mBufferView->mByteOffset + values.mByteOffset,
509       sparse.mCount * acc.GetElementSizeBytes(),
510       static_cast<uint16_t>(values.mBufferView->mByteStride),
511       static_cast<uint16_t>(acc.GetElementSizeBytes()),
512       {},
513       {});
514
515     sparseBlob = std::move(MeshDefinition::SparseBlob(std::move(indicesBlob), std::move(valuesBlob), acc.mSparse->mCount));
516   }
517
518   uint32_t bufferViewOffset = 0u;
519   uint32_t bufferViewStride = 0u;
520   if(acc.mBufferView)
521   {
522     bufferViewOffset = acc.mBufferView->mByteOffset;
523     bufferViewStride = acc.mBufferView->mByteStride;
524   }
525
526   return MeshDefinition::Accessor{
527     std::move(MeshDefinition::Blob{bufferViewOffset + acc.mByteOffset,
528                                    acc.GetBytesLength(),
529                                    static_cast<uint16_t>(bufferViewStride),
530                                    static_cast<uint16_t>(acc.GetElementSizeBytes()),
531                                    acc.mMin,
532                                    acc.mMax}),
533     std::move(sparseBlob)};
534 }
535
536 void ConvertMeshes(const gt::Document& doc, ConversionContext& cctx)
537 {
538   uint32_t meshCount = 0;
539   cctx.mMeshIds.reserve(doc.mMeshes.size());
540   for(auto& m : doc.mMeshes)
541   {
542     cctx.mMeshIds.push_back(meshCount);
543     meshCount += m.mPrimitives.size();
544   }
545
546   auto& outMeshes = cctx.mOutput.mResources.mMeshes;
547   outMeshes.reserve(meshCount);
548   for(auto& m : doc.mMeshes)
549   {
550     for(auto& p : m.mPrimitives)
551     {
552       MeshDefinition meshDef;
553
554       auto& attribs          = p.mAttributes;
555       meshDef.mUri           = attribs.begin()->second->mBufferView->mBuffer->mUri;
556       meshDef.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[p.mMode];
557
558       auto& accPositions = *attribs.find(gt::Attribute::POSITION)->second;
559       meshDef.mPositions = ConvertMeshPrimitiveAccessor(accPositions);
560
561       const bool needNormalsTangents = accPositions.mType == gt::AccessorType::VEC3;
562       for(auto& am : ATTRIBUTE_MAPPINGS)
563       {
564         auto iFind = attribs.find(am.mType);
565         if(iFind != attribs.end())
566         {
567           DALI_ASSERT_DEBUG(iFind->second->mBufferView->mBuffer->mUri.compare(meshDef.mUri) == 0);
568           auto& accessor = meshDef.*(am.mAccessor);
569           accessor       = ConvertMeshPrimitiveAccessor(*iFind->second);
570
571           // Fixing up -- a few of glTF2 sample models have VEC4 tangents; we need VEC3s.
572           if(iFind->first == gt::Attribute::TANGENT && (accessor.mBlob.mElementSizeHint > am.mElementSizeRequired))
573           {
574             accessor.mBlob.mStride          = std::max(static_cast<uint16_t>(accessor.mBlob.mStride + accessor.mBlob.mElementSizeHint - am.mElementSizeRequired),
575                                               accessor.mBlob.mElementSizeHint);
576             accessor.mBlob.mElementSizeHint = am.mElementSizeRequired;
577           }
578
579           if(iFind->first == gt::Attribute::JOINTS_0)
580           {
581             meshDef.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
582             DALI_ASSERT_DEBUG(MaskMatch(meshDef.mFlags, MeshDefinition::U16_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
583           }
584         }
585         else if(needNormalsTangents)
586         {
587           switch(am.mType)
588           {
589             case gt::Attribute::NORMAL:
590               meshDef.RequestNormals();
591               break;
592
593             case gt::Attribute::TANGENT:
594               meshDef.RequestTangents();
595               break;
596
597             default:
598               break;
599           }
600         }
601       }
602
603       if(p.mIndices)
604       {
605         meshDef.mIndices = ConvertMeshPrimitiveAccessor(*p.mIndices);
606         meshDef.mFlags |= (p.mIndices->mComponentType == gt::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
607         DALI_ASSERT_DEBUG(MaskMatch(meshDef.mFlags, MeshDefinition::U32_INDICES) || p.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
608       }
609
610       if(!p.mTargets.empty())
611       {
612         meshDef.mBlendShapes.reserve(p.mTargets.size());
613         meshDef.mBlendShapeVersion = BlendShapes::Version::VERSION_2_0;
614         for(const auto& target : p.mTargets)
615         {
616           MeshDefinition::BlendShape blendShape;
617
618           auto endIt = target.end();
619           auto it    = target.find(gt::Attribute::POSITION);
620           if(it != endIt)
621           {
622             blendShape.deltas = ConvertMeshPrimitiveAccessor(*it->second);
623           }
624           it = target.find(gt::Attribute::NORMAL);
625           if(it != endIt)
626           {
627             blendShape.normals = ConvertMeshPrimitiveAccessor(*it->second);
628           }
629           it = target.find(gt::Attribute::TANGENT);
630           if(it != endIt)
631           {
632             blendShape.tangents = ConvertMeshPrimitiveAccessor(*it->second);
633           }
634
635           if(!m.mWeights.empty())
636           {
637             blendShape.weight = m.mWeights[meshDef.mBlendShapes.size()];
638           }
639
640           meshDef.mBlendShapes.push_back(std::move(blendShape));
641         }
642       }
643
644       outMeshes.push_back({std::move(meshDef), MeshGeometry{}});
645     }
646   }
647 }
648
649 ModelNode* MakeModelNode(const gt::Mesh::Primitive& prim, ConversionContext& cctx)
650 {
651   auto modelNode = new ModelNode();
652
653   modelNode->mShaderIdx = 0; // TODO: further thought
654
655   auto materialIdx = prim.mMaterial.GetIndex();
656   if(INVALID_INDEX == materialIdx)
657   {
658     // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
659     if(INVALID_INDEX == cctx.mDefaultMaterial)
660     {
661       auto& outMaterials    = cctx.mOutput.mResources.mMaterials;
662       cctx.mDefaultMaterial = outMaterials.size();
663
664       ConvertMaterial(gt::Material{}, outMaterials);
665     }
666
667     materialIdx = cctx.mDefaultMaterial;
668   }
669
670   modelNode->mMaterialIdx = materialIdx;
671
672   return modelNode;
673 }
674
675 void ConvertCamera(const gt::Camera& camera, CameraParameters& camParams)
676 {
677   camParams.isPerspective = camera.mType.compare("perspective") == 0;
678   if(camParams.isPerspective)
679   {
680     auto& perspective = camera.mPerspective;
681     camParams.yFov    = Degree(Radian(perspective.mYFov)).degree;
682     camParams.zNear   = perspective.mZNear;
683     camParams.zFar    = perspective.mZFar;
684     // TODO: yes, we seem to ignore aspectRatio in CameraParameters.
685   }
686   else
687   {
688     auto& ortho                = camera.mOrthographic;
689     camParams.orthographicSize = Vector4(-ortho.mXMag, ortho.mXMag, ortho.mYMag, -ortho.mYMag) * .5f;
690     camParams.zNear            = ortho.mZNear;
691     camParams.zFar             = ortho.mZFar;
692   }
693 }
694
695 void ConvertNode(gt::Node const& node, const Index gltfIdx, Index parentIdx, ConversionContext& cctx)
696 {
697   auto& output    = cctx.mOutput;
698   auto& scene     = output.mScene;
699   auto& resources = output.mResources;
700
701   const auto idx      = scene.GetNodeCount();
702   auto       weakNode = scene.AddNode([&]() {
703     std::unique_ptr<NodeDefinition> nodeDef{new NodeDefinition()};
704
705     nodeDef->mParentIdx = parentIdx;
706     nodeDef->mName      = node.mName;
707     if(nodeDef->mName.empty())
708     {
709       // TODO: Production quality generation of unique names.
710       nodeDef->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDef.get()));
711     }
712
713     if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
714     {
715       nodeDef->mPosition    = node.mTranslation;
716       nodeDef->mOrientation = node.mRotation;
717       nodeDef->mScale       = node.mScale;
718     }
719
720     return nodeDef;
721   }());
722   if(!weakNode)
723   {
724     ExceptionFlinger(ASSERT_LOCATION) << "Node name '" << node.mName << "' is not unique; scene is invalid.";
725   }
726
727   cctx.mNodeIndices.RegisterMapping(gltfIdx, idx);
728
729   Index skeletonIdx = node.mSkin ? node.mSkin.GetIndex() : INVALID_INDEX;
730   if(node.mMesh && !node.mMesh->mPrimitives.empty())
731   {
732     auto& mesh = *node.mMesh;
733
734     auto iPrim          = mesh.mPrimitives.begin();
735     auto modelNode      = MakeModelNode(*iPrim, cctx);
736     auto meshIdx        = cctx.mMeshIds[node.mMesh.GetIndex()];
737     modelNode->mMeshIdx = meshIdx;
738
739     weakNode->mRenderable.reset(modelNode);
740
741     DALI_ASSERT_DEBUG(resources.mMeshes[meshIdx].first.mSkeletonIdx == INVALID_INDEX ||
742                       resources.mMeshes[meshIdx].first.mSkeletonIdx == skeletonIdx);
743     resources.mMeshes[meshIdx].first.mSkeletonIdx = skeletonIdx;
744
745     // As does model-exporter, we'll create anonymous child nodes for additional mesh( primitiv)es.
746     while(++iPrim != mesh.mPrimitives.end())
747     {
748       std::unique_ptr<NodeDefinition> child{new NodeDefinition};
749       child->mParentIdx = idx;
750
751       auto childModel = MakeModelNode(*iPrim, cctx);
752
753       ++meshIdx;
754       childModel->mMeshIdx = meshIdx;
755
756       child->mRenderable.reset(childModel);
757
758       scene.AddNode(std::move(child));
759
760       DALI_ASSERT_DEBUG(resources.mMeshes[meshIdx].first.mSkeletonIdx == INVALID_INDEX ||
761                         resources.mMeshes[meshIdx].first.mSkeletonIdx == skeletonIdx);
762       resources.mMeshes[meshIdx].first.mSkeletonIdx = skeletonIdx;
763     }
764   }
765
766   if(node.mCamera)
767   {
768     CameraParameters camParams;
769     ConvertCamera(*node.mCamera, camParams);
770
771     camParams.matrix.SetTransformComponents(node.mScale, node.mRotation, node.mTranslation);
772     output.mCameraParameters.push_back(camParams);
773   }
774
775   for(auto& n : node.mChildren)
776   {
777     ConvertNode(*n, n.GetIndex(), idx, cctx);
778   }
779 }
780
781 void ConvertSceneNodes(const gt::Scene& scene, ConversionContext& cctx)
782 {
783   auto& outScene = cctx.mOutput.mScene;
784   Index rootIdx  = outScene.GetNodeCount();
785   switch(scene.mNodes.size())
786   {
787     case 0:
788       break;
789
790     case 1:
791       ConvertNode(*scene.mNodes[0], scene.mNodes[0].GetIndex(), INVALID_INDEX, cctx);
792       outScene.AddRootNode(rootIdx);
793       break;
794
795     default:
796     {
797       std::unique_ptr<NodeDefinition> sceneRoot{new NodeDefinition()};
798       sceneRoot->mName = "GLTF_LOADER_SCENE_ROOT_" + std::to_string(outScene.GetRoots().size());
799
800       outScene.AddNode(std::move(sceneRoot));
801       outScene.AddRootNode(rootIdx);
802
803       for(auto& n : scene.mNodes)
804       {
805         ConvertNode(*n, n.GetIndex(), rootIdx, cctx);
806       }
807       break;
808     }
809   }
810 }
811
812 void ConvertNodes(const gt::Document& doc, ConversionContext& cctx)
813 {
814   ConvertSceneNodes(*doc.mScene, cctx);
815
816   for(uint32_t i = 0, i1 = doc.mScene.GetIndex(); i < i1; ++i)
817   {
818     ConvertSceneNodes(doc.mScenes[i], cctx);
819   }
820
821   for(uint32_t i = doc.mScene.GetIndex() + 1; i < doc.mScenes.size(); ++i)
822   {
823     ConvertSceneNodes(doc.mScenes[i], cctx);
824   }
825 }
826
827 template<typename T>
828 void LoadDataFromAccessor(const std::string& path, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
829 {
830   std::ifstream animationBinaryFile(path, std::ifstream::binary);
831
832   if(!animationBinaryFile.is_open())
833   {
834     throw std::runtime_error("Failed to load " + path);
835   }
836
837   animationBinaryFile.seekg(offset);
838   animationBinaryFile.read(reinterpret_cast<char*>(dataBuffer.Begin()), size);
839   animationBinaryFile.close();
840 }
841
842 template<typename T>
843 float LoadDataFromAccessors(const std::string& path, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
844 {
845   inputDataBuffer.Resize(input.mCount);
846   outputDataBuffer.Resize(output.mCount);
847
848   const uint32_t inputDataBufferSize  = input.GetBytesLength();
849   const uint32_t outputDataBufferSize = output.GetBytesLength();
850
851   LoadDataFromAccessor<float>(path + std::string(input.mBufferView->mBuffer->mUri), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
852   LoadDataFromAccessor<T>(path + std::string(output.mBufferView->mBuffer->mUri), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
853   ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
854
855   return inputDataBuffer[input.mCount - 1u];
856 }
857
858 template<typename T>
859 float LoadKeyFrames(const std::string& path, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
860 {
861   const gltf2::Accessor& input  = *channel.mSampler->mInput;
862   const gltf2::Accessor& output = *channel.mSampler->mOutput;
863
864   Vector<float> inputDataBuffer;
865   Vector<T>     outputDataBuffer;
866
867   const float duration = LoadDataFromAccessors<T>(path, input, output, inputDataBuffer, outputDataBuffer);
868
869   for(uint32_t i = 0; i < input.mCount; ++i)
870   {
871     keyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i]);
872   }
873
874   return duration;
875 }
876
877 float LoadBlendShapeKeyFrames(const std::string& path, const gt::Animation::Channel& channel, const std::string& nodeName, uint32_t& propertyIndex, std::vector<SceneLoader::AnimatedProperty>& properties)
878 {
879   const gltf2::Accessor& input  = *channel.mSampler->mInput;
880   const gltf2::Accessor& output = *channel.mSampler->mOutput;
881
882   Vector<float> inputDataBuffer;
883   Vector<float> outputDataBuffer;
884
885   const float duration = LoadDataFromAccessors<float>(path, input, output, inputDataBuffer, outputDataBuffer);
886
887   char        weightNameBuffer[32];
888   auto        prefixSize    = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM.c_str());
889   char* const pWeightName   = weightNameBuffer + prefixSize;
890   const auto  remainingSize = sizeof(weightNameBuffer) - prefixSize;
891   for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex)
892   {
893     AnimatedProperty& animatedProperty = properties[propertyIndex++];
894
895     animatedProperty.mNodeName = nodeName;
896     snprintf(pWeightName, remainingSize, "%d]", weightIndex);
897     animatedProperty.mPropertyName = std::string(weightNameBuffer);
898
899     animatedProperty.mKeyFrames = KeyFrames::New();
900     for(uint32_t i = 0; i < input.mCount; ++i)
901     {
902       animatedProperty.mKeyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i * endWeightIndex + weightIndex]);
903     }
904
905     animatedProperty.mTimePeriod = {0.f, duration};
906   }
907
908   return duration;
909 }
910
911 void ConvertAnimations(const gt::Document& doc, ConversionContext& cctx)
912 {
913   auto& output = cctx.mOutput;
914
915   output.mAnimationDefinitions.reserve(output.mAnimationDefinitions.size() + doc.mAnimations.size());
916
917   for(const auto& animation : doc.mAnimations)
918   {
919     AnimationDefinition animationDef;
920
921     if(!animation.mName.empty())
922     {
923       animationDef.mName = animation.mName;
924     }
925
926     uint32_t numberOfProperties = 0u;
927
928     for(const auto& channel : animation.mChannels)
929     {
930       numberOfProperties += channel.mSampler->mOutput->mCount;
931     }
932     animationDef.mProperties.resize(numberOfProperties);
933
934     Index propertyIndex = 0u;
935     for(const auto& channel : animation.mChannels)
936     {
937       std::string nodeName;
938       if(!channel.mTarget.mNode->mName.empty())
939       {
940         nodeName = channel.mTarget.mNode->mName;
941       }
942       else
943       {
944         Index index = cctx.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
945         nodeName    = cctx.mOutput.mScene.GetNode(index)->mName;
946       }
947
948       float duration = 0.f;
949
950       switch(channel.mTarget.mPath)
951       {
952         case gt::Animation::Channel::Target::TRANSLATION:
953         {
954           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
955
956           animatedProperty.mNodeName     = nodeName;
957           animatedProperty.mPropertyName = POSITION_PROPERTY;
958
959           animatedProperty.mKeyFrames = KeyFrames::New();
960           duration                    = LoadKeyFrames<Vector3>(cctx.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
961
962           animatedProperty.mTimePeriod = {0.f, duration};
963           break;
964         }
965         case gt::Animation::Channel::Target::ROTATION:
966         {
967           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
968
969           animatedProperty.mNodeName     = nodeName;
970           animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
971
972           animatedProperty.mKeyFrames = KeyFrames::New();
973           duration                    = LoadKeyFrames<Quaternion>(cctx.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
974
975           animatedProperty.mTimePeriod = {0.f, duration};
976           break;
977         }
978         case gt::Animation::Channel::Target::SCALE:
979         {
980           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
981
982           animatedProperty.mNodeName     = nodeName;
983           animatedProperty.mPropertyName = SCALE_PROPERTY;
984
985           animatedProperty.mKeyFrames = KeyFrames::New();
986           duration                    = LoadKeyFrames<Vector3>(cctx.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
987
988           animatedProperty.mTimePeriod = {0.f, duration};
989           break;
990         }
991         case gt::Animation::Channel::Target::WEIGHTS:
992         {
993           duration = LoadBlendShapeKeyFrames(cctx.mPath, channel, nodeName, propertyIndex, animationDef.mProperties);
994
995           break;
996         }
997         default:
998         {
999           // nothing to animate.
1000           break;
1001         }
1002       }
1003
1004       animationDef.mDuration = std::max(duration, animationDef.mDuration);
1005
1006       ++propertyIndex;
1007     }
1008
1009     output.mAnimationDefinitions.push_back(std::move(animationDef));
1010   }
1011 }
1012
1013 void ProcessSkins(const gt::Document& doc, ConversionContext& cctx)
1014 {
1015   // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skininversebindmatrices
1016   // If an inverseBindMatrices accessor was provided, we'll load the joint data from the buffer,
1017   // otherwise we'll set identity matrices for inverse bind pose.
1018   struct IInverseBindMatrixProvider
1019   {
1020     virtual ~IInverseBindMatrixProvider()
1021     {
1022     }
1023     virtual void Provide(Matrix& ibm) = 0;
1024   };
1025
1026   struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
1027   {
1028     std::ifstream  mStream;
1029     const uint32_t mElementSizeBytes;
1030
1031     InverseBindMatrixAccessor(const gt::Accessor& accessor, const std::string& path)
1032     : mStream(path + std::string(accessor.mBufferView->mBuffer->mUri), std::ios::binary),
1033       mElementSizeBytes(accessor.GetElementSizeBytes())
1034     {
1035       DALI_ASSERT_ALWAYS(mStream);
1036       DALI_ASSERT_DEBUG(accessor.mType == gt::AccessorType::MAT4 && accessor.mComponentType == gt::Component::FLOAT);
1037
1038       mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset);
1039     }
1040
1041     virtual void Provide(Matrix& ibm) override
1042     {
1043       DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), mElementSizeBytes));
1044     }
1045   };
1046
1047   struct DefaultInverseBindMatrixProvider : public IInverseBindMatrixProvider
1048   {
1049     virtual void Provide(Matrix& ibm) override
1050     {
1051       ibm = Matrix::IDENTITY;
1052     }
1053   };
1054
1055   auto& resources = cctx.mOutput.mResources;
1056   resources.mSkeletons.reserve(doc.mSkins.size());
1057
1058   for(auto& s : doc.mSkins)
1059   {
1060     std::unique_ptr<IInverseBindMatrixProvider> ibmProvider;
1061     if(s.mInverseBindMatrices)
1062     {
1063       ibmProvider.reset(new InverseBindMatrixAccessor(*s.mInverseBindMatrices, cctx.mPath));
1064     }
1065     else
1066     {
1067       ibmProvider.reset(new DefaultInverseBindMatrixProvider());
1068     }
1069
1070     SkeletonDefinition skeleton;
1071     if(s.mSkeleton.GetIndex() != INVALID_INDEX)
1072     {
1073       skeleton.mRootNodeIdx = cctx.mNodeIndices.GetRuntimeId(s.mSkeleton.GetIndex());
1074     }
1075
1076     skeleton.mJoints.resize(s.mJoints.size());
1077     auto iJoint = skeleton.mJoints.begin();
1078     for(auto& j : s.mJoints)
1079     {
1080       iJoint->mNodeIdx = cctx.mNodeIndices.GetRuntimeId(j.GetIndex());
1081
1082       ibmProvider->Provide(iJoint->mInverseBindMatrix);
1083
1084       ++iJoint;
1085     }
1086
1087     resources.mSkeletons.push_back(std::move(skeleton));
1088   }
1089 }
1090
1091 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene)
1092 {
1093   for(size_t i0 = 0, i1 = scene.GetNodeCount(); i0 != i1; ++i0)
1094   {
1095     auto nodeDef = scene.GetNode(i0);
1096     if(auto renderable = nodeDef->mRenderable.get())
1097     {
1098       renderable->mShaderIdx = shaderFactory.ProduceShader(*nodeDef);
1099     }
1100   }
1101 }
1102
1103 void SetObjectReaders()
1104 {
1105   js::SetObjectReader(BUFFER_READER);
1106   js::SetObjectReader(BUFFER_VIEW_READER);
1107   js::SetObjectReader(BUFFER_VIEW_CLIENT_READER);
1108   js::SetObjectReader(COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER);
1109   js::SetObjectReader(ACCESSOR_SPARSE_READER);
1110   js::SetObjectReader(ACCESSOR_READER);
1111   js::SetObjectReader(IMAGE_READER);
1112   js::SetObjectReader(SAMPLER_READER);
1113   js::SetObjectReader(TEXURE_READER);
1114   js::SetObjectReader(TEXURE_INFO_READER);
1115   js::SetObjectReader(MATERIAL_PBR_READER);
1116   js::SetObjectReader(MATERIAL_READER);
1117   js::SetObjectReader(MESH_PRIMITIVE_READER);
1118   js::SetObjectReader(MESH_READER);
1119   js::SetObjectReader(SKIN_READER);
1120   js::SetObjectReader(CAMERA_PERSPECTIVE_READER);
1121   js::SetObjectReader(CAMERA_ORTHOGRAPHIC_READER);
1122   js::SetObjectReader(CAMERA_READER);
1123   js::SetObjectReader(NODE_READER);
1124   js::SetObjectReader(ANIMATION_SAMPLER_READER);
1125   js::SetObjectReader(ANIMATION_TARGET_READER);
1126   js::SetObjectReader(ANIMATION_CHANNEL_READER);
1127   js::SetObjectReader(ANIMATION_READER);
1128   js::SetObjectReader(SCENE_READER);
1129 }
1130
1131 } // namespace
1132
1133 void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
1134 {
1135   bool failed = false;
1136   auto js     = LoadTextFile(url.c_str(), &failed);
1137   if(failed)
1138   {
1139     throw std::runtime_error("Failed to load " + url);
1140   }
1141
1142   json::unique_ptr root(json_parse(js.c_str(), js.size()));
1143   if(!root)
1144   {
1145     throw std::runtime_error("Failed to parse " + url);
1146   }
1147
1148   static bool setObjectReaders = true;
1149   if(setObjectReaders)
1150   {
1151     // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1152     SetObjectReaders();
1153     setObjectReaders = false;
1154   }
1155
1156   gt::Document doc;
1157
1158   auto& rootObj        = js::Cast<json_object_s>(*root);
1159   auto  jsAsset        = js::FindObjectChild("asset", rootObj);
1160   auto  jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsAsset));
1161   doc.mAsset.mVersion  = js::Read::StringView(*jsAssetVersion);
1162
1163   gt::SetRefReaderObject(doc);
1164   DOCUMENT_READER.Read(rootObj, doc);
1165
1166   auto              path = url.substr(0, url.rfind('/') + 1);
1167   ConversionContext cctx{params, path, INVALID_INDEX};
1168
1169   ConvertMaterials(doc, cctx);
1170   ConvertMeshes(doc, cctx);
1171   ConvertNodes(doc, cctx);
1172   ConvertAnimations(doc, cctx);
1173
1174   ProcessSkins(doc, cctx);
1175
1176   ProduceShaders(shaderFactory, params.mScene);
1177   params.mScene.EnsureUniqueSkinningShaderInstances(params.mResources);
1178 }
1179
1180 } // namespace SceneLoader
1181 } // namespace Dali