[dali_2.2.16] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / gltf2-loader.cpp
1 /*
2  * Copyright (c) 2023 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
18 // FILE HEADER
19 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/threading/mutex.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/images/image-operations.h>
25 #include <dali/public-api/math/quaternion.h>
26 #include <memory>
27
28 // INTERNAL INCLUDES
29 #include <dali-scene3d/internal/loader/gltf2-asset.h>
30 #include <dali-scene3d/public-api/loader/load-result.h>
31 #include <dali-scene3d/public-api/loader/resource-bundle.h>
32 #include <dali-scene3d/public-api/loader/scene-definition.h>
33 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
34 #include <dali-scene3d/public-api/loader/utils.h>
35
36 namespace gt = gltf2;
37 namespace js = json;
38
39 namespace Dali
40 {
41 namespace Scene3D
42 {
43 namespace Loader
44 {
45 namespace
46 {
47 Dali::Mutex gInitializeMutex;
48 Dali::Mutex gReadMutex;
49
50 const char*   POSITION_PROPERTY("position");
51 const char*   ORIENTATION_PROPERTY("orientation");
52 const char*   SCALE_PROPERTY("scale");
53 const char*   BLEND_SHAPE_WEIGHTS_UNIFORM("uBlendShapeWeight");
54 const char*   MRENDERER_MODEL_IDENTIFICATION("M-Renderer");
55 const char*   ROOT_NODE_NAME("RootNode");
56 const Vector3 SCALE_TO_ADJUST(100.0f, 100.0f, 100.0f);
57
58 const Geometry::Type GLTF2_TO_DALI_PRIMITIVES[]{
59   Geometry::POINTS,
60   Geometry::LINES,
61   Geometry::LINE_LOOP,
62   Geometry::LINE_STRIP,
63   Geometry::TRIANGLES,
64   Geometry::TRIANGLE_STRIP,
65   Geometry::TRIANGLE_FAN}; //...because Dali swaps the last two.
66
67 struct AttributeMapping
68 {
69   gt::Attribute::Type      mType;
70   MeshDefinition::Accessor MeshDefinition::*mAccessor;
71   uint16_t                                  mElementSizeRequired;
72 } ATTRIBUTE_MAPPINGS[]{
73   {gt::Attribute::NORMAL, &MeshDefinition::mNormals, sizeof(Vector3)},
74   {gt::Attribute::TANGENT, &MeshDefinition::mTangents, sizeof(Vector3)},
75   {gt::Attribute::TEXCOORD_0, &MeshDefinition::mTexCoords, sizeof(Vector2)},
76   {gt::Attribute::COLOR_0, &MeshDefinition::mColors, sizeof(Vector4)},
77   {gt::Attribute::JOINTS_0, &MeshDefinition::mJoints0, sizeof(Vector4)},
78   {gt::Attribute::WEIGHTS_0, &MeshDefinition::mWeights0, sizeof(Vector4)},
79 };
80
81 std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
82 {
83   auto results = js::Read::Array<gt::Animation, js::ObjectReader<gt::Animation>::Read>(j);
84
85   for(auto& animation : results)
86   {
87     for(auto& channel : animation.mChannels)
88     {
89       channel.mSampler.UpdateVector(animation.mSamplers);
90     }
91   }
92
93   return results;
94 }
95
96 void ApplyAccessorMinMax(const gt::Accessor& acc, float* values)
97 {
98   DALI_ASSERT_ALWAYS(acc.mMax.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMax.size());
99   DALI_ASSERT_ALWAYS(acc.mMin.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMin.size());
100   MeshDefinition::Blob::ApplyMinMax(acc.mMin, acc.mMax, acc.mCount, values);
101 }
102
103 const js::Reader<gt::Buffer>& GetBufferReader()
104 {
105   static const auto BUFFER_READER = std::move(js::Reader<gt::Buffer>()
106                                                  .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, &gt::Buffer::mByteLength))
107                                                  .Register(*js::MakeProperty("uri", js::Read::StringView, &gt::Buffer::mUri)));
108   return BUFFER_READER;
109 }
110
111 const js::Reader<gt::BufferView>& GetBufferViewReader()
112 {
113   static const auto BUFFER_VIEW_READER = std::move(js::Reader<gt::BufferView>()
114                                                       .Register(*js::MakeProperty("buffer", gt::RefReader<gt::Document>::Read<gt::Buffer, &gt::Document::mBuffers>, &gt::BufferView::mBuffer))
115                                                       .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, &gt::BufferView::mByteOffset))
116                                                       .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, &gt::BufferView::mByteLength))
117                                                       .Register(*js::MakeProperty("byteStride", js::Read::Number<uint32_t>, &gt::BufferView::mByteStride))
118                                                       .Register(*js::MakeProperty("target", js::Read::Number<uint32_t>, &gt::BufferView::mTarget)));
119   return BUFFER_VIEW_READER;
120 }
121
122 const js::Reader<gt::BufferViewClient>& GetBufferViewClientReader()
123 {
124   static const auto BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::BufferViewClient>()
125                                                    .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>, &gt::BufferViewClient::mBufferView))
126                                                    .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, &gt::BufferViewClient::mByteOffset)));
127   return BUFFER_VIEW_CLIENT_READER;
128 }
129
130 const js::Reader<gt::ComponentTypedBufferViewClient>& GetComponentTypedBufferViewClientReader()
131 {
132   static const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::ComponentTypedBufferViewClient>()
133                                                                    .Register(*new js::Property<gt::ComponentTypedBufferViewClient, gt::Ref<gt::BufferView>>("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>, &gt::ComponentTypedBufferViewClient::mBufferView))
134                                                                    .Register(*new js::Property<gt::ComponentTypedBufferViewClient, uint32_t>("byteOffset", js::Read::Number<uint32_t>, &gt::ComponentTypedBufferViewClient::mByteOffset))
135                                                                    .Register(*js::MakeProperty("componentType", js::Read::Enum<gt::Component::Type>, &gt::ComponentTypedBufferViewClient::mComponentType)));
136   return COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER;
137 }
138
139 const js::Reader<gt::Accessor::Sparse>& GetAccessorSparseReader()
140 {
141   static const auto ACCESSOR_SPARSE_READER = std::move(js::Reader<gt::Accessor::Sparse>()
142                                                 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, &gt::Accessor::Sparse::mCount))
143                                                 .Register(*js::MakeProperty("indices", js::ObjectReader<gt::ComponentTypedBufferViewClient>::Read, &gt::Accessor::Sparse::mIndices))
144                                                 .Register(*js::MakeProperty("values", js::ObjectReader<gt::BufferViewClient>::Read, &gt::Accessor::Sparse::mValues)));
145   return ACCESSOR_SPARSE_READER;
146 }
147
148 const js::Reader<gt::Accessor>& GetAccessorReader()
149 {
150   static const auto ACCESSOR_READER = std::move(js::Reader<gt::Accessor>()
151                                          .Register(*new js::Property<gt::Accessor, gt::Ref<gt::BufferView>>("bufferView",
152                                                                                                             gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>,
153                                                                                                             &gt::Accessor::mBufferView))
154                                          .Register(*new js::Property<gt::Accessor, uint32_t>("byteOffset",
155                                                                                              js::Read::Number<uint32_t>,
156                                                                                              &gt::Accessor::mByteOffset))
157                                          .Register(*new js::Property<gt::Accessor, gt::Component::Type>("componentType",
158                                                                                                         js::Read::Enum<gt::Component::Type>,
159                                                                                                         &gt::Accessor::mComponentType))
160                                          .Register(*new js::Property<gt::Accessor, std::string_view>("name", js::Read::StringView, &gt::Accessor::mName))
161                                          .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, &gt::Accessor::mCount))
162                                          .Register(*js::MakeProperty("normalized", js::Read::Boolean, &gt::Accessor::mNormalized))
163                                          .Register(*js::MakeProperty("type", gt::ReadStringEnum<gt::AccessorType>, &gt::Accessor::mType))
164                                          .Register(*js::MakeProperty("min", js::Read::Array<float, js::Read::Number>, &gt::Accessor::mMin))
165                                          .Register(*js::MakeProperty("max", js::Read::Array<float, js::Read::Number>, &gt::Accessor::mMax))
166                                          .Register(*new js::Property<gt::Accessor, gt::Accessor::Sparse>("sparse", js::ObjectReader<gt::Accessor::Sparse>::Read, &gt::Accessor::SetSparse)));
167   return ACCESSOR_READER;
168 }
169
170 const js::Reader<gt::Image>& GetImageReader()
171 {
172   static const auto IMAGE_READER = std::move(js::Reader<gt::Image>()
173                                       .Register(*new js::Property<gt::Image, std::string_view>("name", js::Read::StringView, &gt::Material::mName))
174                                       .Register(*js::MakeProperty("uri", js::Read::StringView, &gt::Image::mUri))
175                                       .Register(*js::MakeProperty("mimeType", js::Read::StringView, &gt::Image::mMimeType))
176                                       .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, &gt::Document::mBufferViews>, &gt::Image::mBufferView)));
177   return IMAGE_READER;
178 }
179
180 const js::Reader<gt::Sampler>& GetSamplerReader()
181 {
182   static const auto SAMPLER_READER = std::move(js::Reader<gt::Sampler>()
183                                         .Register(*js::MakeProperty("minFilter", js::Read::Enum<gt::Filter::Type>, &gt::Sampler::mMinFilter))
184                                         .Register(*js::MakeProperty("magFilter", js::Read::Enum<gt::Filter::Type>, &gt::Sampler::mMagFilter))
185                                         .Register(*js::MakeProperty("wrapS", js::Read::Enum<gt::Wrap::Type>, &gt::Sampler::mWrapS))
186                                         .Register(*js::MakeProperty("wrapT", js::Read::Enum<gt::Wrap::Type>, &gt::Sampler::mWrapT)));
187   return SAMPLER_READER;
188 }
189
190 const js::Reader<gt::Texture>& GetTextureReader()
191 {
192   static const auto TEXURE_READER = std::move(js::Reader<gt::Texture>()
193                                        .Register(*js::MakeProperty("source", gt::RefReader<gt::Document>::Read<gt::Image, &gt::Document::mImages>, &gt::Texture::mSource))
194                                        .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Document>::Read<gt::Sampler, &gt::Document::mSamplers>, &gt::Texture::mSampler)));
195   return TEXURE_READER;
196 }
197
198 const js::Reader<gt::TextureInfo>& GetTextureInfoReader()
199 {
200   static const auto TEXURE_INFO_READER = std::move(js::Reader<gt::TextureInfo>()
201                                             .Register(*js::MakeProperty("index", gt::RefReader<gt::Document>::Read<gt::Texture, &gt::Document::mTextures>, &gt::TextureInfo::mTexture))
202                                             .Register(*js::MakeProperty("texCoord", js::Read::Number<uint32_t>, &gt::TextureInfo::mTexCoord))
203                                             .Register(*js::MakeProperty("scale", js::Read::Number<float>, &gt::TextureInfo::mScale))
204                                             .Register(*js::MakeProperty("strength", js::Read::Number<float>, &gt::TextureInfo::mStrength)));
205   return TEXURE_INFO_READER;
206 }
207
208 const js::Reader<gt::Material::Pbr>& GetMaterialPbrReader()
209 {
210   static const auto MATERIAL_PBR_READER = std::move(js::Reader<gt::Material::Pbr>()
211                                              .Register(*js::MakeProperty("baseColorFactor", gt::ReadDaliVector<Vector4>, &gt::Material::Pbr::mBaseColorFactor))
212                                              .Register(*js::MakeProperty("baseColorTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::Pbr::mBaseColorTexture))
213                                              .Register(*js::MakeProperty("metallicFactor", js::Read::Number<float>, &gt::Material::Pbr::mMetallicFactor))
214                                              .Register(*js::MakeProperty("roughnessFactor", js::Read::Number<float>, &gt::Material::Pbr::mRoughnessFactor))
215                                              .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::Pbr::mMetallicRoughnessTexture)));
216   return MATERIAL_PBR_READER;
217 }
218
219 const js::Reader<gt::MaterialSpecular>& GetMaterialSpecularReader()
220 {
221   static const auto MATERIAL_SPECULAR_READER = std::move(js::Reader<gt::MaterialSpecular>()
222                                                   .Register(*js::MakeProperty("specularFactor", js::Read::Number<float>, &gt::MaterialSpecular::mSpecularFactor))
223                                                   .Register(*js::MakeProperty("specularTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::MaterialSpecular::mSpecularTexture))
224                                                   .Register(*js::MakeProperty("specularColorFactor", gt::ReadDaliVector<Vector3>, &gt::MaterialSpecular::mSpecularColorFactor))
225                                                   .Register(*js::MakeProperty("specularColorTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::MaterialSpecular::mSpecularColorTexture)));
226   return MATERIAL_SPECULAR_READER;
227 }
228
229 const js::Reader<gt::MaterialIor>& GetMaterialIorReader()
230 {
231   static const auto MATERIAL_IOR_READER = std::move(js::Reader<gt::MaterialIor>()
232                                              .Register(*js::MakeProperty("ior", js::Read::Number<float>, &gt::MaterialIor::mIor)));
233   return MATERIAL_IOR_READER;
234 }
235
236 const js::Reader<gt::MaterialExtensions>& GetMaterialExtensionsReader()
237 {
238   static const auto MATERIAL_EXTENSION_READER = std::move(js::Reader<gt::MaterialExtensions>()
239                                                    .Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader<gt::MaterialIor>::Read, &gt::MaterialExtensions::mMaterialIor))
240                                                    .Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader<gt::MaterialSpecular>::Read, &gt::MaterialExtensions::mMaterialSpecular)));
241   return MATERIAL_EXTENSION_READER;
242 }
243
244 const js::Reader<gt::Material>& GetMaterialReader()
245 {
246   static const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
247                                          .Register(*new js::Property<gt::Material, std::string_view>("name", js::Read::StringView, &gt::Material::mName))
248                                          .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader<gt::Material::Pbr>::Read, &gt::Material::mPbrMetallicRoughness))
249                                          .Register(*js::MakeProperty("normalTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::mNormalTexture))
250                                          .Register(*js::MakeProperty("occlusionTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::mOcclusionTexture))
251                                          .Register(*js::MakeProperty("emissiveTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::mEmissiveTexture))
252                                          .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector<Vector3>, &gt::Material::mEmissiveFactor))
253                                          .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum<gt::AlphaMode>, &gt::Material::mAlphaMode))
254                                          .Register(*js::MakeProperty("alphaCutoff", js::Read::Number<float>, &gt::Material::mAlphaCutoff))
255                                          .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, &gt::Material::mDoubleSided))
256                                          .Register(*js::MakeProperty("extensions", js::ObjectReader<gt::MaterialExtensions>::Read, &gt::Material::mMaterialExtensions)));
257   return MATERIAL_READER;
258 }
259
260 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
261 {
262   auto&                                                jo = js::Cast<json_object_s>(j);
263   std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> result;
264
265   auto i = jo.start;
266   while(i)
267   {
268     auto jstr                                                        = *i->name;
269     result[gt::Attribute::FromString(jstr.string, jstr.string_size)] = gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>(*i->value);
270     i                                                                = i->next;
271   }
272   return result;
273 }
274
275 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> ReadMeshPrimitiveTargets(const json_value_s& j)
276 {
277   auto&                                                             jo = js::Cast<json_array_s>(j);
278   std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> result;
279
280   result.reserve(jo.length);
281
282   auto i = jo.start;
283   while(i)
284   {
285     result.push_back(std::move(ReadMeshPrimitiveAttributes(*i->value)));
286     i = i->next;
287   }
288
289   return result;
290 }
291
292 const js::Reader<gt::Mesh::Primitive>& GetMeshPrimitiveReader()
293 {
294   static const auto MESH_PRIMITIVE_READER = std::move(js::Reader<gt::Mesh::Primitive>()
295                                                .Register(*js::MakeProperty("attributes", ReadMeshPrimitiveAttributes, &gt::Mesh::Primitive::mAttributes))
296                                                .Register(*js::MakeProperty("indices", gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>, &gt::Mesh::Primitive::mIndices))
297                                                .Register(*js::MakeProperty("material", gt::RefReader<gt::Document>::Read<gt::Material, &gt::Document::mMaterials>, &gt::Mesh::Primitive::mMaterial))
298                                                .Register(*js::MakeProperty("mode", js::Read::Enum<gt::Mesh::Primitive::Mode>, &gt::Mesh::Primitive::mMode))
299                                                .Register(*js::MakeProperty("targets", ReadMeshPrimitiveTargets, &gt::Mesh::Primitive::mTargets)));
300   return MESH_PRIMITIVE_READER;
301 }
302
303 const js::Reader<gt::Mesh>& GetMeshReader()
304 {
305   static const auto MESH_READER = std::move(js::Reader<gt::Mesh>()
306                                      .Register(*new js::Property<gt::Mesh, std::string_view>("name", js::Read::StringView, &gt::Mesh::mName))
307                                      .Register(*js::MakeProperty("primitives",
308                                                                  js::Read::Array<gt::Mesh::Primitive, js::ObjectReader<gt::Mesh::Primitive>::Read>,
309                                                                  &gt::Mesh::mPrimitives))
310                                      .Register(*js::MakeProperty("weights", js::Read::Array<float, js::Read::Number>, &gt::Mesh::mWeights)));
311   return MESH_READER;
312 }
313
314 const js::Reader<gt::Skin>& GetSkinReader()
315 {
316   static const auto SKIN_READER = std::move(js::Reader<gt::Skin>()
317                                      .Register(*new js::Property<gt::Skin, std::string_view>("name", js::Read::StringView, &gt::Skin::mName))
318                                      .Register(*js::MakeProperty("inverseBindMatrices",
319                                                                  gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>,
320                                                                  &gt::Skin::mInverseBindMatrices))
321                                      .Register(*js::MakeProperty("skeleton",
322                                                                  gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>,
323                                                                  &gt::Skin::mSkeleton))
324                                      .Register(*js::MakeProperty("joints",
325                                                                  js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>>,
326                                                                  &gt::Skin::mJoints)));
327   return SKIN_READER;
328 }
329
330 const js::Reader<gt::Camera::Perspective>& GetCameraPerspectiveReader()
331 {
332   static const auto CAMERA_PERSPECTIVE_READER = std::move(js::Reader<gt::Camera::Perspective>()
333                                                    .Register(*js::MakeProperty("aspectRatio", js::Read::Number<float>, &gt::Camera::Perspective::mAspectRatio))
334                                                    .Register(*js::MakeProperty("yfov", js::Read::Number<float>, &gt::Camera::Perspective::mYFov))
335                                                    .Register(*js::MakeProperty("zfar", js::Read::Number<float>, &gt::Camera::Perspective::mZFar))
336                                                    .Register(*js::MakeProperty("znear", js::Read::Number<float>, &gt::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted
337   return CAMERA_PERSPECTIVE_READER;
338 }
339
340 const js::Reader<gt::Camera::Orthographic>& GetCameraOrthographicReader()
341 {
342   static const auto CAMERA_ORTHOGRAPHIC_READER = std::move(js::Reader<gt::Camera::Orthographic>()
343                                                     .Register(*js::MakeProperty("xmag", js::Read::Number<float>, &gt::Camera::Orthographic::mXMag))
344                                                     .Register(*js::MakeProperty("ymag", js::Read::Number<float>, &gt::Camera::Orthographic::mYMag))
345                                                     .Register(*js::MakeProperty("zfar", js::Read::Number<float>, &gt::Camera::Orthographic::mZFar))
346                                                     .Register(*js::MakeProperty("znear", js::Read::Number<float>, &gt::Camera::Orthographic::mZNear)));
347   return CAMERA_ORTHOGRAPHIC_READER;
348 }
349
350 const js::Reader<gt::Camera>& GetCameraReader()
351 {
352   static const auto CAMERA_READER = std::move(js::Reader<gt::Camera>()
353                                        .Register(*new js::Property<gt::Camera, std::string_view>("name", js::Read::StringView, &gt::Camera::mName))
354                                        .Register(*js::MakeProperty("type", js::Read::StringView, &gt::Camera::mType))
355                                        .Register(*js::MakeProperty("perspective", js::ObjectReader<gt::Camera::Perspective>::Read, &gt::Camera::mPerspective))
356                                        .Register(*js::MakeProperty("orthographic", js::ObjectReader<gt::Camera::Orthographic>::Read, &gt::Camera::mOrthographic)));
357   return CAMERA_READER;
358 }
359
360 const js::Reader<gt::Node>& GetNodeReader()
361 {
362   static const auto NODE_READER = std::move(js::Reader<gt::Node>()
363                                      .Register(*new js::Property<gt::Node, std::string_view>("name", js::Read::StringView, &gt::Node::mName))
364                                      .Register(*js::MakeProperty("translation", gt::ReadDaliVector<Vector3>, &gt::Node::mTranslation))
365                                      .Register(*js::MakeProperty("rotation", gt::ReadQuaternion, &gt::Node::mRotation))
366                                      .Register(*js::MakeProperty("scale", gt::ReadDaliVector<Vector3>, &gt::Node::mScale))
367                                      .Register(*new js::Property<gt::Node, Matrix>("matrix", gt::ReadDaliVector<Matrix>, &gt::Node::SetMatrix))
368                                      .Register(*js::MakeProperty("camera", gt::RefReader<gt::Document>::Read<gt::Camera, &gt::Document::mCameras>, &gt::Node::mCamera))
369                                      .Register(*js::MakeProperty("children", js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>>, &gt::Node::mChildren))
370                                      .Register(*js::MakeProperty("mesh", gt::RefReader<gt::Document>::Read<gt::Mesh, &gt::Document::mMeshes>, &gt::Node::mMesh))
371                                      .Register(*js::MakeProperty("skin", gt::RefReader<gt::Document>::Read<gt::Skin, &gt::Document::mSkins>, &gt::Node::mSkin)));
372   return NODE_READER;
373 }
374
375 const js::Reader<gt::Animation::Sampler>& GetAnimationSamplerReader()
376 {
377   static const auto ANIMATION_SAMPLER_READER = std::move(js::Reader<gt::Animation::Sampler>()
378                                                   .Register(*js::MakeProperty("input", gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>, &gt::Animation::Sampler::mInput))
379                                                   .Register(*js::MakeProperty("output", gt::RefReader<gt::Document>::Read<gt::Accessor, &gt::Document::mAccessors>, &gt::Animation::Sampler::mOutput))
380                                                   .Register(*js::MakeProperty("interpolation", gt::ReadStringEnum<gt::Animation::Sampler::Interpolation>, &gt::Animation::Sampler::mInterpolation)));
381   return ANIMATION_SAMPLER_READER;
382 }
383
384 const js::Reader<gt::Animation::Channel::Target>& GetAnimationChannelTargetReader()
385 {
386   static const auto ANIMATION_TARGET_READER = std::move(js::Reader<gt::Animation::Channel::Target>()
387                                                  .Register(*js::MakeProperty("node", gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>, &gt::Animation::Channel::Target::mNode))
388                                                  .Register(*js::MakeProperty("path", gt::ReadStringEnum<gt::Animation::Channel::Target>, &gt::Animation::Channel::Target::mPath)));
389   return ANIMATION_TARGET_READER;
390 }
391
392 const js::Reader<gt::Animation::Channel>& GetAnimationChannelReader()
393 {
394   static const auto ANIMATION_CHANNEL_READER = std::move(js::Reader<gt::Animation::Channel>()
395                                                   .Register(*js::MakeProperty("target", js::ObjectReader<gt::Animation::Channel::Target>::Read, &gt::Animation::Channel::mTarget))
396                                                   .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Animation>::Read<gt::Animation::Sampler, &gt::Animation::mSamplers>, &gt::Animation::Channel::mSampler)));
397   return ANIMATION_CHANNEL_READER;
398 }
399
400 const js::Reader<gt::Animation>& GetAnimationReader()
401 {
402   static const auto ANIMATION_READER = std::move(js::Reader<gt::Animation>()
403                                           .Register(*new js::Property<gt::Animation, std::string_view>("name", js::Read::StringView, &gt::Animation::mName))
404                                           .Register(*js::MakeProperty("samplers",
405                                                                       js::Read::Array<gt::Animation::Sampler, js::ObjectReader<gt::Animation::Sampler>::Read>,
406                                                                       &gt::Animation::mSamplers))
407                                           .Register(*js::MakeProperty("channels",
408                                                                       js::Read::Array<gt::Animation::Channel, js::ObjectReader<gt::Animation::Channel>::Read>,
409                                                                       &gt::Animation::mChannels)));
410   return ANIMATION_READER;
411 }
412
413 const js::Reader<gt::Scene>& GetSceneReader()
414 {
415   static const auto SCENE_READER = std::move(js::Reader<gt::Scene>()
416                                       .Register(*new js::Property<gt::Scene, std::string_view>("name", js::Read::StringView, &gt::Scene::mName))
417                                       .Register(*js::MakeProperty("nodes",
418                                                                   js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, &gt::Document::mNodes>>,
419                                                                   &gt::Scene::mNodes)));
420   return SCENE_READER;
421 }
422
423 const js::Reader<gt::Document>& GetDocumentReader()
424 {
425   static const auto DOCUMENT_READER = std::move(js::Reader<gt::Document>()
426                                          .Register(*js::MakeProperty("buffers",
427                                                                      js::Read::Array<gt::Buffer, js::ObjectReader<gt::Buffer>::Read>,
428                                                                      &gt::Document::mBuffers))
429                                          .Register(*js::MakeProperty("bufferViews",
430                                                                      js::Read::Array<gt::BufferView, js::ObjectReader<gt::BufferView>::Read>,
431                                                                      &gt::Document::mBufferViews))
432                                          .Register(*js::MakeProperty("accessors",
433                                                                      js::Read::Array<gt::Accessor, js::ObjectReader<gt::Accessor>::Read>,
434                                                                      &gt::Document::mAccessors))
435                                          .Register(*js::MakeProperty("images",
436                                                                      js::Read::Array<gt::Image, js::ObjectReader<gt::Image>::Read>,
437                                                                      &gt::Document::mImages))
438                                          .Register(*js::MakeProperty("samplers",
439                                                                      js::Read::Array<gt::Sampler, js::ObjectReader<gt::Sampler>::Read>,
440                                                                      &gt::Document::mSamplers))
441                                          .Register(*js::MakeProperty("textures",
442                                                                      js::Read::Array<gt::Texture, js::ObjectReader<gt::Texture>::Read>,
443                                                                      &gt::Document::mTextures))
444                                          .Register(*js::MakeProperty("materials",
445                                                                      js::Read::Array<gt::Material, js::ObjectReader<gt::Material>::Read>,
446                                                                      &gt::Document::mMaterials))
447                                          .Register(*js::MakeProperty("meshes",
448                                                                      js::Read::Array<gt::Mesh, js::ObjectReader<gt::Mesh>::Read>,
449                                                                      &gt::Document::mMeshes))
450                                          .Register(*js::MakeProperty("skins",
451                                                                      js::Read::Array<gt::Skin, js::ObjectReader<gt::Skin>::Read>,
452                                                                      &gt::Document::mSkins))
453                                          .Register(*js::MakeProperty("cameras",
454                                                                      js::Read::Array<gt::Camera, js::ObjectReader<gt::Camera>::Read>,
455                                                                      &gt::Document::mCameras))
456                                          .Register(*js::MakeProperty("nodes",
457                                                                      js::Read::Array<gt::Node, js::ObjectReader<gt::Node>::Read>,
458                                                                      &gt::Document::mNodes))
459                                          .Register(*js::MakeProperty("animations",
460                                                                      ReadAnimationArray,
461                                                                      &gt::Document::mAnimations))
462                                          .Register(*js::MakeProperty("scenes",
463                                                                      js::Read::Array<gt::Scene, js::ObjectReader<gt::Scene>::Read>,
464                                                                      &gt::Document::mScenes))
465                                          .Register(*js::MakeProperty("scene", gt::RefReader<gt::Document>::Read<gt::Scene, &gt::Document::mScenes>, &gt::Document::mScene)));
466   return DOCUMENT_READER;
467 }
468
469 struct NodeMapping
470 {
471   Index gltfIdx;
472   Index runtimeIdx;
473 };
474
475 bool operator<(const NodeMapping& mapping, Index gltfIdx)
476 {
477   return mapping.gltfIdx < gltfIdx;
478 }
479
480 class NodeIndexMapper
481 {
482 public:
483   NodeIndexMapper()                       = default;
484   NodeIndexMapper(const NodeIndexMapper&) = delete;
485   NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
486
487   ///@brief Registers a mapping of the @a gltfIdx of a node to its @a runtimeIdx .
488   ///@note If the indices are the same, the registration is omitted, in order to
489   /// save growing a vector.
490   void RegisterMapping(Index gltfIdx, Index runtimeIdx)
491   {
492     if(gltfIdx != runtimeIdx)
493     {
494       auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx);
495       DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIdx != gltfIdx);
496       mNodes.insert(iInsert, NodeMapping{gltfIdx, runtimeIdx});
497     }
498   }
499
500   ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIdx.
501   Index GetRuntimeId(Index gltfIdx) const
502   {
503     auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx); // using custom operator<
504     return (iFind != mNodes.end() && iFind->gltfIdx == gltfIdx) ? iFind->runtimeIdx : gltfIdx;
505   }
506
507 private:
508   std::vector<NodeMapping> mNodes;
509 };
510
511 struct ConversionContext
512 {
513   LoadResult& mOutput;
514
515   std::string mPath;
516   Index       mDefaultMaterial;
517
518   std::vector<Index> mMeshIds;
519   NodeIndexMapper    mNodeIndices;
520 };
521
522 void ConvertBuffer(const gt::Buffer& buffer, decltype(ResourceBundle::mBuffers)& outBuffers, const std::string& resourcePath)
523 {
524   BufferDefinition bufferDefinition;
525
526   bufferDefinition.mResourcePath = resourcePath;
527   bufferDefinition.mUri          = buffer.mUri;
528   bufferDefinition.mByteLength   = buffer.mByteLength;
529
530   outBuffers.emplace_back(std::move(bufferDefinition));
531 }
532
533 void ConvertBuffers(const gt::Document& doc, ConversionContext& context)
534 {
535   auto& outBuffers = context.mOutput.mResources.mBuffers;
536   outBuffers.reserve(doc.mBuffers.size());
537
538   for(auto& buffer : doc.mBuffers)
539   {
540     ConvertBuffer(buffer, outBuffers, context.mPath);
541   }
542 }
543
544 SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type wrapMode)
545 {
546   switch(wrapMode)
547   {
548     case gt::Wrap::REPEAT:
549       return SamplerFlags::WRAP_REPEAT;
550     case gt::Wrap::CLAMP_TO_EDGE:
551       return SamplerFlags::WRAP_CLAMP;
552     case gt::Wrap::MIRRORED_REPEAT:
553       return SamplerFlags::WRAP_MIRROR;
554     default:
555       throw std::runtime_error("Invalid wrap type.");
556   }
557 }
558
559 SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& sampler)
560 {
561   if(sampler)
562   {
563     return ((sampler->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (sampler->mMinFilter - gt::Filter::NEAREST) : ((sampler->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
564            ((sampler->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
565            (ConvertWrapMode(sampler->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
566            (ConvertWrapMode(sampler->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
567   }
568   else
569   {
570     // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texturesampler
571     // "The index of the sampler used by this texture. When undefined, a sampler with repeat wrapping and auto filtering should be used."
572     // "What is an auto filtering", I hear you ask. Since there's nothing else to determine mipmapping from - including glTF image
573     // properties, if not in some extension -, we will simply assume linear filtering.
574     return SamplerFlags::FILTER_LINEAR | (SamplerFlags::FILTER_LINEAR << SamplerFlags::FILTER_MAG_SHIFT) |
575            (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_S_SHIFT) | (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_T_SHIFT);
576   }
577 }
578
579 TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm, ConversionContext& context, const ImageMetadata& metaData = ImageMetadata())
580 {
581   TextureDefinition textureDefinition;
582   std::string       uri = std::string(mm.mTexture->mSource->mUri);
583   if(uri.empty())
584   {
585     uint32_t bufferIndex = mm.mTexture->mSource->mBufferView->mBuffer.GetIndex();
586     if(bufferIndex != INVALID_INDEX && context.mOutput.mResources.mBuffers[bufferIndex].IsAvailable())
587     {
588       auto& stream = context.mOutput.mResources.mBuffers[bufferIndex].GetBufferStream();
589       stream.clear();
590       stream.seekg(mm.mTexture->mSource->mBufferView->mByteOffset, stream.beg);
591       std::vector<uint8_t> dataBuffer;
592       dataBuffer.resize(mm.mTexture->mSource->mBufferView->mByteLength);
593       stream.read(reinterpret_cast<char*>(dataBuffer.data()), static_cast<std::streamsize>(static_cast<size_t>(mm.mTexture->mSource->mBufferView->mByteLength)));
594       return TextureDefinition{std::move(dataBuffer), ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
595     }
596     return TextureDefinition();
597   }
598   else
599   {
600     return TextureDefinition{uri, ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
601   }
602 }
603
604 void ConvertMaterial(const gt::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials, ConversionContext& context)
605 {
606   auto getTextureMetaData = [](const std::unordered_map<std::string, ImageMetadata>& metaData, const gt::TextureInfo& info) {
607     if(!info.mTexture->mSource->mUri.empty())
608     {
609       if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end())
610       {
611         return search->second;
612       }
613     }
614     return ImageMetadata();
615   };
616
617   MaterialDefinition matDef;
618
619   auto& pbr = material.mPbrMetallicRoughness;
620   if(material.mAlphaMode == gt::AlphaMode::BLEND)
621   {
622     matDef.mIsOpaque = false;
623     matDef.mFlags |= MaterialDefinition::TRANSPARENCY;
624   }
625   else if(material.mAlphaMode == gt::AlphaMode::MASK)
626   {
627     matDef.mIsMask = true;
628     matDef.SetAlphaCutoff(std::min(1.f, std::max(0.f, material.mAlphaCutoff)));
629   }
630
631   matDef.mBaseColorFactor = pbr.mBaseColorFactor;
632
633   matDef.mTextureStages.reserve(!!pbr.mBaseColorTexture + !!pbr.mMetallicRoughnessTexture + !!material.mNormalTexture + !!material.mOcclusionTexture + !!material.mEmissiveTexture);
634   if(pbr.mBaseColorTexture)
635   {
636     const auto semantic = MaterialDefinition::ALBEDO;
637     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture, context, getTextureMetaData(imageMetaData, pbr.mBaseColorTexture))});
638     // TODO: and there had better be one
639     matDef.mFlags |= semantic;
640   }
641   else
642   {
643     matDef.mNeedAlbedoTexture = false;
644   }
645
646   matDef.mMetallic  = pbr.mMetallicFactor;
647   matDef.mRoughness = pbr.mRoughnessFactor;
648
649   if(pbr.mMetallicRoughnessTexture)
650   {
651     const auto semantic = MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
652                           MaterialDefinition::GLTF_CHANNELS;
653     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture, context, getTextureMetaData(imageMetaData, pbr.mMetallicRoughnessTexture))});
654     // TODO: and there had better be one
655     matDef.mFlags |= semantic;
656   }
657   else
658   {
659     matDef.mNeedMetallicRoughnessTexture = false;
660   }
661
662   matDef.mNormalScale = material.mNormalTexture.mScale;
663   if(material.mNormalTexture)
664   {
665     const auto semantic = MaterialDefinition::NORMAL;
666     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture, context, getTextureMetaData(imageMetaData, material.mNormalTexture))});
667     // TODO: and there had better be one
668     matDef.mFlags |= semantic;
669   }
670   else
671   {
672     matDef.mNeedNormalTexture = false;
673   }
674
675   if(material.mOcclusionTexture)
676   {
677     const auto semantic = MaterialDefinition::OCCLUSION;
678     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture, context, getTextureMetaData(imageMetaData, material.mOcclusionTexture))});
679     // TODO: and there had better be one
680     matDef.mFlags |= semantic;
681     matDef.mOcclusionStrength = material.mOcclusionTexture.mStrength;
682   }
683
684   if(material.mEmissiveTexture)
685   {
686     const auto semantic = MaterialDefinition::EMISSIVE;
687     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture, context, getTextureMetaData(imageMetaData, material.mEmissiveTexture))});
688     // TODO: and there had better be one
689     matDef.mFlags |= semantic;
690     matDef.mEmissiveFactor = material.mEmissiveFactor;
691   }
692
693   if(!Dali::Equals(material.mMaterialExtensions.mMaterialIor.mIor, gltf2::UNDEFINED_FLOAT_VALUE))
694   {
695     float ior                  = material.mMaterialExtensions.mMaterialIor.mIor;
696     matDef.mDielectricSpecular = powf((ior - 1.0f) / (ior + 1.0f), 2.0f);
697   }
698   matDef.mSpecularFactor      = material.mMaterialExtensions.mMaterialSpecular.mSpecularFactor;
699   matDef.mSpecularColorFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor;
700
701   if(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)
702   {
703     const auto semantic = MaterialDefinition::SPECULAR;
704     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture))});
705     matDef.mFlags |= semantic;
706   }
707
708   if(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)
709   {
710     const auto semantic = MaterialDefinition::SPECULAR_COLOR;
711     matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture))});
712     matDef.mFlags |= semantic;
713   }
714
715   matDef.mDoubleSided = material.mDoubleSided;
716
717   outMaterials.emplace_back(std::move(matDef), TextureSet());
718 }
719
720 void ConvertMaterials(const gt::Document& doc, ConversionContext& context)
721 {
722   auto& imageMetaData = context.mOutput.mSceneMetadata.mImageMetadata;
723
724   auto& outMaterials = context.mOutput.mResources.mMaterials;
725   outMaterials.reserve(doc.mMaterials.size());
726
727   for(auto& m : doc.mMaterials)
728   {
729     ConvertMaterial(m, imageMetaData, outMaterials, context);
730   }
731 }
732
733 MeshDefinition::Accessor ConvertMeshPrimitiveAccessor(const gt::Accessor& acc)
734 {
735   DALI_ASSERT_ALWAYS((acc.mBufferView &&
736                       (acc.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) ||
737                      (acc.mSparse && !acc.mBufferView));
738
739   DALI_ASSERT_ALWAYS(!acc.mSparse ||
740                      ((acc.mSparse->mIndices.mBufferView && (acc.mSparse->mIndices.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) &&
741                       (acc.mSparse->mValues.mBufferView && (acc.mSparse->mValues.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max()))));
742
743   MeshDefinition::SparseBlob sparseBlob;
744   if(acc.mSparse)
745   {
746     const gt::Accessor::Sparse&               sparse  = *acc.mSparse;
747     const gt::ComponentTypedBufferViewClient& indices = sparse.mIndices;
748     const gt::BufferViewClient&               values  = sparse.mValues;
749
750     MeshDefinition::Blob indicesBlob(
751       indices.mBufferView->mByteOffset + indices.mByteOffset,
752       sparse.mCount * indices.GetBytesPerComponent(),
753       static_cast<uint16_t>(indices.mBufferView->mByteStride),
754       static_cast<uint16_t>(indices.GetBytesPerComponent()),
755       {},
756       {});
757     MeshDefinition::Blob valuesBlob(
758       values.mBufferView->mByteOffset + values.mByteOffset,
759       sparse.mCount * acc.GetElementSizeBytes(),
760       static_cast<uint16_t>(values.mBufferView->mByteStride),
761       static_cast<uint16_t>(acc.GetElementSizeBytes()),
762       {},
763       {});
764
765     sparseBlob = std::move(MeshDefinition::SparseBlob(std::move(indicesBlob), std::move(valuesBlob), acc.mSparse->mCount));
766   }
767
768   uint32_t bufferViewOffset = 0u;
769   uint32_t bufferViewStride = 0u;
770   if(acc.mBufferView)
771   {
772     bufferViewOffset = acc.mBufferView->mByteOffset;
773     bufferViewStride = acc.mBufferView->mByteStride;
774   }
775
776   return MeshDefinition::Accessor{
777     std::move(MeshDefinition::Blob{bufferViewOffset + acc.mByteOffset,
778                                    acc.GetBytesLength(),
779                                    static_cast<uint16_t>(bufferViewStride),
780                                    static_cast<uint16_t>(acc.GetElementSizeBytes()),
781                                    acc.mMin,
782                                    acc.mMax}),
783     std::move(sparseBlob),
784     acc.mBufferView ? acc.mBufferView->mBuffer.GetIndex() : 0};
785 }
786
787 void ConvertMeshes(const gt::Document& doc, ConversionContext& context)
788 {
789   uint32_t meshCount = 0;
790   context.mMeshIds.reserve(doc.mMeshes.size());
791   for(auto& mesh : doc.mMeshes)
792   {
793     context.mMeshIds.push_back(meshCount);
794     meshCount += mesh.mPrimitives.size();
795   }
796
797   auto& outMeshes = context.mOutput.mResources.mMeshes;
798   outMeshes.reserve(meshCount);
799   for(auto& mesh : doc.mMeshes)
800   {
801     for(auto& primitive : mesh.mPrimitives)
802     {
803       MeshDefinition meshDefinition;
804
805       auto& attribs                 = primitive.mAttributes;
806       meshDefinition.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[primitive.mMode];
807
808       auto& accPositions        = *attribs.find(gt::Attribute::POSITION)->second;
809       meshDefinition.mPositions = ConvertMeshPrimitiveAccessor(accPositions);
810       // glTF2 support vector4 tangent for mesh.
811       // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#meshes-overview
812       meshDefinition.mTangentType = Property::VECTOR4;
813
814       const bool needNormalsTangents = accPositions.mType == gt::AccessorType::VEC3;
815       for(auto& am : ATTRIBUTE_MAPPINGS)
816       {
817         auto iFind = attribs.find(am.mType);
818         if(iFind != attribs.end())
819         {
820           auto& accessor = meshDefinition.*(am.mAccessor);
821           accessor       = ConvertMeshPrimitiveAccessor(*iFind->second);
822
823           if(iFind->first == gt::Attribute::JOINTS_0)
824           {
825             meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
826             meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_JOINT_IDS;
827             DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
828           }
829         }
830         else if(needNormalsTangents)
831         {
832           switch(am.mType)
833           {
834             case gt::Attribute::NORMAL:
835               meshDefinition.RequestNormals();
836               break;
837
838             case gt::Attribute::TANGENT:
839               meshDefinition.RequestTangents();
840               break;
841
842             default:
843               break;
844           }
845         }
846       }
847
848       if(primitive.mIndices)
849       {
850         meshDefinition.mIndices = ConvertMeshPrimitiveAccessor(*primitive.mIndices);
851         meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
852         meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_INDICES;
853         DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_INDICES) || primitive.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
854       }
855
856       if(!primitive.mTargets.empty())
857       {
858         meshDefinition.mBlendShapes.reserve(primitive.mTargets.size());
859         meshDefinition.mBlendShapeVersion = BlendShapes::Version::VERSION_2_0;
860         for(const auto& target : primitive.mTargets)
861         {
862           MeshDefinition::BlendShape blendShape;
863
864           auto endIt = target.end();
865           auto it    = target.find(gt::Attribute::POSITION);
866           if(it != endIt)
867           {
868             blendShape.deltas = ConvertMeshPrimitiveAccessor(*it->second);
869           }
870           it = target.find(gt::Attribute::NORMAL);
871           if(it != endIt)
872           {
873             blendShape.normals = ConvertMeshPrimitiveAccessor(*it->second);
874           }
875           it = target.find(gt::Attribute::TANGENT);
876           if(it != endIt)
877           {
878             blendShape.tangents = ConvertMeshPrimitiveAccessor(*it->second);
879           }
880
881           if(!mesh.mWeights.empty())
882           {
883             blendShape.weight = mesh.mWeights[meshDefinition.mBlendShapes.size()];
884           }
885
886           meshDefinition.mBlendShapes.push_back(std::move(blendShape));
887         }
888       }
889
890       outMeshes.push_back({std::move(meshDefinition), MeshGeometry{}});
891     }
892   }
893 }
894
895 ModelRenderable* MakeModelRenderable(const gt::Mesh::Primitive& prim, ConversionContext& context)
896 {
897   auto modelRenderable = new ModelRenderable();
898
899   modelRenderable->mShaderIdx = 0; // TODO: further thought
900
901   auto materialIdx = prim.mMaterial.GetIndex();
902   if(INVALID_INDEX == materialIdx)
903   {
904     // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
905     if(INVALID_INDEX == context.mDefaultMaterial)
906     {
907       auto& outMaterials       = context.mOutput.mResources.mMaterials;
908       context.mDefaultMaterial = outMaterials.size();
909
910       ConvertMaterial(gt::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials, context);
911     }
912
913     materialIdx = context.mDefaultMaterial;
914   }
915
916   modelRenderable->mMaterialIdx = materialIdx;
917
918   return modelRenderable;
919 }
920
921 void ConvertCamera(const gt::Camera& camera, CameraParameters& camParams)
922 {
923   camParams.isPerspective = camera.mType.compare("perspective") == 0;
924   if(camParams.isPerspective)
925   {
926     auto& perspective = camera.mPerspective;
927     if(!Dali::Equals(perspective.mYFov, gltf2::UNDEFINED_FLOAT_VALUE))
928     {
929       camParams.yFovDegree = Degree(Radian(perspective.mYFov));
930     }
931     else
932     {
933       camParams.yFovDegree = Degree(gltf2::UNDEFINED_FLOAT_VALUE);
934     }
935     camParams.zNear = perspective.mZNear;
936     camParams.zFar  = perspective.mZFar;
937     // TODO: yes, we seem to ignore aspectRatio in CameraParameters.
938   }
939   else
940   {
941     auto& ortho = camera.mOrthographic;
942     if(!Dali::Equals(ortho.mYMag, gltf2::UNDEFINED_FLOAT_VALUE) && !Dali::Equals(ortho.mXMag, gltf2::UNDEFINED_FLOAT_VALUE))
943     {
944       camParams.orthographicSize = ortho.mYMag * .5f;
945       camParams.aspectRatio      = ortho.mXMag / ortho.mYMag;
946     }
947     else
948     {
949       camParams.orthographicSize = gltf2::UNDEFINED_FLOAT_VALUE;
950       camParams.aspectRatio      = gltf2::UNDEFINED_FLOAT_VALUE;
951     }
952     camParams.zNear = ortho.mZNear;
953     camParams.zFar  = ortho.mZFar;
954   }
955 }
956
957 void ConvertNode(gt::Node const& node, const Index gltfIdx, Index parentIdx, ConversionContext& context, bool isMRendererModel)
958 {
959   auto& output    = context.mOutput;
960   auto& scene     = output.mScene;
961   auto& resources = output.mResources;
962
963   const auto idx      = scene.GetNodeCount();
964   auto       weakNode = scene.AddNode([&]() {
965     std::unique_ptr<NodeDefinition> nodeDef{new NodeDefinition()};
966
967     nodeDef->mParentIdx = parentIdx;
968     nodeDef->mName      = node.mName;
969     if(nodeDef->mName.empty())
970     {
971       // TODO: Production quality generation of unique names.
972       nodeDef->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDef.get()));
973     }
974
975     if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
976     {
977       nodeDef->mPosition    = node.mTranslation;
978       nodeDef->mOrientation = node.mRotation;
979       nodeDef->mScale       = node.mScale;
980
981       if(isMRendererModel && node.mName == ROOT_NODE_NAME && node.mScale == SCALE_TO_ADJUST)
982       {
983         nodeDef->mScale *= 0.01f;
984       }
985     }
986
987     return nodeDef;
988   }());
989   if(!weakNode)
990   {
991     ExceptionFlinger(ASSERT_LOCATION) << "Node name '" << node.mName << "' is not unique; scene is invalid.";
992   }
993
994   context.mNodeIndices.RegisterMapping(gltfIdx, idx);
995
996   Index skeletonIdx = node.mSkin ? node.mSkin.GetIndex() : INVALID_INDEX;
997   if(node.mMesh)
998   {
999     auto&    mesh           = *node.mMesh;
1000     uint32_t primitiveCount = mesh.mPrimitives.size();
1001     auto     meshIdx        = context.mMeshIds[node.mMesh.GetIndex()];
1002     weakNode->mRenderables.reserve(primitiveCount);
1003     for(uint32_t i = 0; i < primitiveCount; ++i)
1004     {
1005       std::unique_ptr<NodeDefinition::Renderable> renderable;
1006       auto                                        modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
1007       modelRenderable->mMeshIdx                                   = meshIdx + i;
1008
1009       DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
1010                         resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
1011       resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx = skeletonIdx;
1012
1013       renderable.reset(modelRenderable);
1014       weakNode->mRenderables.push_back(std::move(renderable));
1015     }
1016   }
1017
1018   if(node.mCamera)
1019   {
1020     CameraParameters camParams;
1021     ConvertCamera(*node.mCamera, camParams);
1022
1023     camParams.matrix.SetTransformComponents(node.mScale, node.mRotation, node.mTranslation);
1024     output.mCameraParameters.push_back(camParams);
1025   }
1026
1027   for(auto& n : node.mChildren)
1028   {
1029     ConvertNode(*n, n.GetIndex(), idx, context, isMRendererModel);
1030   }
1031 }
1032
1033 void ConvertSceneNodes(const gt::Scene& scene, ConversionContext& context, bool isMRendererModel)
1034 {
1035   auto& outScene = context.mOutput.mScene;
1036   Index rootIdx  = outScene.GetNodeCount();
1037   switch(scene.mNodes.size())
1038   {
1039     case 0:
1040       break;
1041
1042     case 1:
1043       ConvertNode(*scene.mNodes[0], scene.mNodes[0].GetIndex(), INVALID_INDEX, context, isMRendererModel);
1044       outScene.AddRootNode(rootIdx);
1045       break;
1046
1047     default:
1048     {
1049       std::unique_ptr<NodeDefinition> sceneRoot{new NodeDefinition()};
1050       sceneRoot->mName = "GLTF_LOADER_SCENE_ROOT_" + std::to_string(outScene.GetRoots().size());
1051
1052       outScene.AddNode(std::move(sceneRoot));
1053       outScene.AddRootNode(rootIdx);
1054
1055       for(auto& n : scene.mNodes)
1056       {
1057         ConvertNode(*n, n.GetIndex(), rootIdx, context, isMRendererModel);
1058       }
1059       break;
1060     }
1061   }
1062 }
1063
1064 void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel)
1065 {
1066   if(!doc.mScenes.empty())
1067   {
1068     uint32_t rootSceneIndex = 0u;
1069     if(doc.mScene)
1070     {
1071       rootSceneIndex = doc.mScene.GetIndex();
1072     }
1073     ConvertSceneNodes(doc.mScenes[rootSceneIndex], context, isMRendererModel);
1074
1075     for(uint32_t i = 0, i1 = rootSceneIndex; i < i1; ++i)
1076     {
1077       ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
1078     }
1079
1080     for(uint32_t i = rootSceneIndex + 1; i < doc.mScenes.size(); ++i)
1081     {
1082       ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
1083     }
1084   }
1085 }
1086
1087 template<typename T>
1088 void LoadDataFromAccessor(ConversionContext& context, uint32_t bufferIndex, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
1089 {
1090   if(bufferIndex >= context.mOutput.mResources.mBuffers.size())
1091   {
1092     DALI_LOG_ERROR("Invailid buffer index\n");
1093     return;
1094   }
1095
1096   auto& buffer = context.mOutput.mResources.mBuffers[bufferIndex];
1097   if(!buffer.IsAvailable())
1098   {
1099     DALI_LOG_ERROR("Failed to load from buffer stream.\n");
1100   }
1101   auto& stream = buffer.GetBufferStream();
1102   stream.clear();
1103   stream.seekg(offset, stream.beg);
1104   stream.read(reinterpret_cast<char*>(dataBuffer.Begin()), static_cast<std::streamsize>(static_cast<size_t>(size)));
1105 }
1106
1107 template<typename T>
1108 float LoadDataFromAccessors(ConversionContext& context, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
1109 {
1110   inputDataBuffer.Resize(input.mCount);
1111   outputDataBuffer.Resize(output.mCount);
1112
1113   const uint32_t inputDataBufferSize  = input.GetBytesLength();
1114   const uint32_t outputDataBufferSize = output.GetBytesLength();
1115
1116   LoadDataFromAccessor<float>(context, output.mBufferView->mBuffer.GetIndex(), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
1117   LoadDataFromAccessor<T>(context, output.mBufferView->mBuffer.GetIndex(), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
1118   ApplyAccessorMinMax(input, reinterpret_cast<float*>(inputDataBuffer.begin()));
1119   ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
1120
1121   return inputDataBuffer[input.mCount - 1u];
1122 }
1123
1124 template<typename T>
1125 float LoadKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
1126 {
1127   const gltf2::Accessor& input  = *channel.mSampler->mInput;
1128   const gltf2::Accessor& output = *channel.mSampler->mOutput;
1129
1130   Vector<float> inputDataBuffer;
1131   Vector<T>     outputDataBuffer;
1132
1133   const float duration = std::max(LoadDataFromAccessors<T>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
1134
1135   // Set first frame value as first keyframe (gltf animation spec)
1136   if(input.mCount > 0 && !Dali::EqualsZero(inputDataBuffer[0]))
1137   {
1138     keyFrames.Add(0.0f, outputDataBuffer[0]);
1139   }
1140
1141   for(uint32_t i = 0; i < input.mCount; ++i)
1142   {
1143     keyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i]);
1144   }
1145
1146   return duration;
1147 }
1148
1149 float LoadBlendShapeKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
1150 {
1151   const gltf2::Accessor& input  = *channel.mSampler->mInput;
1152   const gltf2::Accessor& output = *channel.mSampler->mOutput;
1153
1154   Vector<float> inputDataBuffer;
1155   Vector<float> outputDataBuffer;
1156
1157   const float duration = std::max(LoadDataFromAccessors<float>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
1158
1159   char        weightNameBuffer[32];
1160   auto        prefixSize    = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM);
1161   char* const pWeightName   = weightNameBuffer + prefixSize;
1162   const auto  remainingSize = sizeof(weightNameBuffer) - prefixSize;
1163   for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex)
1164   {
1165     AnimatedProperty& animatedProperty = properties[propertyIndex++];
1166
1167     animatedProperty.mNodeIndex = nodeIndex;
1168     snprintf(pWeightName, remainingSize, "%d]", weightIndex);
1169     animatedProperty.mPropertyName = std::string(weightNameBuffer);
1170
1171     animatedProperty.mKeyFrames = KeyFrames::New();
1172
1173     // Set first frame value as first keyframe (gltf animation spec)
1174     if(input.mCount > 0 && !Dali::EqualsZero(inputDataBuffer[0]))
1175     {
1176       animatedProperty.mKeyFrames.Add(0.0f, outputDataBuffer[weightIndex]);
1177     }
1178
1179     for(uint32_t i = 0; i < input.mCount; ++i)
1180     {
1181       animatedProperty.mKeyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i * endWeightIndex + weightIndex]);
1182     }
1183
1184     animatedProperty.mTimePeriod = {0.f, duration};
1185   }
1186
1187   return duration;
1188 }
1189
1190 void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
1191 {
1192   auto& output = context.mOutput;
1193
1194   output.mAnimationDefinitions.reserve(output.mAnimationDefinitions.size() + doc.mAnimations.size());
1195
1196   for(const auto& animation : doc.mAnimations)
1197   {
1198     AnimationDefinition animationDef;
1199
1200     if(!animation.mName.empty())
1201     {
1202       animationDef.mName = animation.mName;
1203     }
1204
1205     uint32_t numberOfProperties = 0u;
1206     for(const auto& channel : animation.mChannels)
1207     {
1208       if(channel.mTarget.mPath == gt::Animation::Channel::Target::WEIGHTS)
1209       {
1210         numberOfProperties += channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount;
1211       }
1212       else
1213       {
1214         numberOfProperties++;
1215       }
1216     }
1217     animationDef.mProperties.resize(numberOfProperties);
1218
1219     Index propertyIndex = 0u;
1220     for(const auto& channel : animation.mChannels)
1221     {
1222       Index nodeIndex = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
1223       float duration  = 0.f;
1224
1225       switch(channel.mTarget.mPath)
1226       {
1227         case gt::Animation::Channel::Target::TRANSLATION:
1228         {
1229           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1230
1231           animatedProperty.mNodeIndex    = nodeIndex;
1232           animatedProperty.mPropertyName = POSITION_PROPERTY;
1233
1234           animatedProperty.mKeyFrames = KeyFrames::New();
1235           duration                    = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1236
1237           animatedProperty.mTimePeriod = {0.f, duration};
1238           break;
1239         }
1240         case gt::Animation::Channel::Target::ROTATION:
1241         {
1242           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1243
1244           animatedProperty.mNodeIndex    = nodeIndex;
1245           animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
1246
1247           animatedProperty.mKeyFrames = KeyFrames::New();
1248           duration                    = LoadKeyFrames<Quaternion>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1249
1250           animatedProperty.mTimePeriod = {0.f, duration};
1251           break;
1252         }
1253         case gt::Animation::Channel::Target::SCALE:
1254         {
1255           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1256
1257           animatedProperty.mNodeIndex    = nodeIndex;
1258           animatedProperty.mPropertyName = SCALE_PROPERTY;
1259
1260           animatedProperty.mKeyFrames = KeyFrames::New();
1261           duration                    = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1262
1263           animatedProperty.mTimePeriod = {0.f, duration};
1264           break;
1265         }
1266         case gt::Animation::Channel::Target::WEIGHTS:
1267         {
1268           duration = LoadBlendShapeKeyFrames(context, channel, nodeIndex, propertyIndex, animationDef.mProperties);
1269
1270           break;
1271         }
1272         default:
1273         {
1274           // nothing to animate.
1275           break;
1276         }
1277       }
1278
1279       animationDef.mDuration = std::max(duration, animationDef.mDuration);
1280
1281       ++propertyIndex;
1282     }
1283
1284     output.mAnimationDefinitions.push_back(std::move(animationDef));
1285   }
1286 }
1287
1288 void ProcessSkins(const gt::Document& doc, ConversionContext& context)
1289 {
1290   // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skininversebindmatrices
1291   // If an inverseBindMatrices accessor was provided, we'll load the joint data from the buffer,
1292   // otherwise we'll set identity matrices for inverse bind pose.
1293   struct IInverseBindMatrixProvider
1294   {
1295     virtual ~IInverseBindMatrixProvider()
1296     {
1297     }
1298     virtual void Provide(Matrix& ibm) = 0;
1299   };
1300
1301   struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
1302   {
1303     std::istream&  mStream;
1304     const uint32_t mElementSizeBytes;
1305
1306     InverseBindMatrixAccessor(const gt::Accessor& accessor, ConversionContext& context)
1307     : mStream(context.mOutput.mResources.mBuffers[accessor.mBufferView->mBuffer.GetIndex()].GetBufferStream()),
1308       mElementSizeBytes(accessor.GetElementSizeBytes())
1309     {
1310       DALI_ASSERT_DEBUG(accessor.mType == gt::AccessorType::MAT4 && accessor.mComponentType == gt::Component::FLOAT);
1311
1312       if(!mStream.rdbuf()->in_avail())
1313       {
1314         DALI_LOG_ERROR("Failed to load from stream\n");
1315       }
1316       mStream.clear();
1317       mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset, mStream.beg);
1318     }
1319
1320     virtual void Provide(Matrix& ibm) override
1321     {
1322       DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), static_cast<std::streamsize>(static_cast<size_t>(mElementSizeBytes))));
1323     }
1324   };
1325
1326   struct DefaultInverseBindMatrixProvider : public IInverseBindMatrixProvider
1327   {
1328     virtual void Provide(Matrix& ibm) override
1329     {
1330       ibm = Matrix::IDENTITY;
1331     }
1332   };
1333
1334   auto& resources = context.mOutput.mResources;
1335   resources.mSkeletons.reserve(doc.mSkins.size());
1336
1337   for(auto& skin : doc.mSkins)
1338   {
1339     std::unique_ptr<IInverseBindMatrixProvider> ibmProvider;
1340     if(skin.mInverseBindMatrices)
1341     {
1342       ibmProvider.reset(new InverseBindMatrixAccessor(*skin.mInverseBindMatrices, context));
1343     }
1344     else
1345     {
1346       ibmProvider.reset(new DefaultInverseBindMatrixProvider());
1347     }
1348
1349     SkeletonDefinition skeleton;
1350     if(skin.mSkeleton.GetIndex() != INVALID_INDEX)
1351     {
1352       skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(skin.mSkeleton.GetIndex());
1353     }
1354
1355     skeleton.mJoints.resize(skin.mJoints.size());
1356     auto iJoint = skeleton.mJoints.begin();
1357     for(auto& joint : skin.mJoints)
1358     {
1359       iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(joint.GetIndex());
1360
1361       ibmProvider->Provide(iJoint->mInverseBindMatrix);
1362
1363       ++iJoint;
1364     }
1365
1366     resources.mSkeletons.push_back(std::move(skeleton));
1367   }
1368 }
1369
1370 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene)
1371 {
1372   uint32_t nodeCount = scene.GetNodeCount();
1373   for(uint32_t i = 0; i < nodeCount; ++i)
1374   {
1375     auto nodeDef = scene.GetNode(i);
1376     for(auto& renderable : nodeDef->mRenderables)
1377     {
1378       if(shaderFactory.ProduceShader(*renderable) == INVALID_INDEX)
1379       {
1380         DALI_LOG_ERROR("Fail to produce shader\n");
1381       }
1382     }
1383   }
1384 }
1385
1386 void SetObjectReaders()
1387 {
1388   js::SetObjectReader(GetBufferReader());
1389   js::SetObjectReader(GetBufferViewReader());
1390   js::SetObjectReader(GetBufferViewClientReader());
1391   js::SetObjectReader(GetComponentTypedBufferViewClientReader());
1392   js::SetObjectReader(GetAccessorSparseReader());
1393   js::SetObjectReader(GetAccessorReader());
1394   js::SetObjectReader(GetImageReader());
1395   js::SetObjectReader(GetSamplerReader());
1396   js::SetObjectReader(GetTextureReader());
1397   js::SetObjectReader(GetTextureInfoReader());
1398   js::SetObjectReader(GetMaterialPbrReader());
1399   js::SetObjectReader(GetMaterialSpecularReader());
1400   js::SetObjectReader(GetMaterialIorReader());
1401   js::SetObjectReader(GetMaterialExtensionsReader());
1402   js::SetObjectReader(GetMaterialReader());
1403   js::SetObjectReader(GetMeshPrimitiveReader());
1404   js::SetObjectReader(GetMeshReader());
1405   js::SetObjectReader(GetSkinReader());
1406   js::SetObjectReader(GetCameraPerspectiveReader());
1407   js::SetObjectReader(GetCameraOrthographicReader());
1408   js::SetObjectReader(GetCameraReader());
1409   js::SetObjectReader(GetNodeReader());
1410   js::SetObjectReader(GetAnimationSamplerReader());
1411   js::SetObjectReader(GetAnimationChannelTargetReader());
1412   js::SetObjectReader(GetAnimationChannelReader());
1413   js::SetObjectReader(GetAnimationReader());
1414   js::SetObjectReader(GetSceneReader());
1415 }
1416
1417 void SetDefaultEnvironmentMap(const gt::Document& doc, ConversionContext& context)
1418 {
1419   EnvironmentDefinition envDef;
1420   envDef.mUseBrdfTexture = true;
1421   envDef.mIblIntensity   = Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
1422   context.mOutput.mResources.mEnvironmentMaps.push_back({std::move(envDef), EnvironmentDefinition::Textures()});
1423 }
1424
1425 } // namespace
1426
1427 void InitializeGltfLoader()
1428 {
1429   // Set ObjectReader only once (for all gltf loading).
1430   static bool setObjectReadersRequired = true;
1431   {
1432     Mutex::ScopedLock lock(gInitializeMutex);
1433     if(setObjectReadersRequired)
1434     {
1435       // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1436       SetObjectReaders();
1437       setObjectReadersRequired = false;
1438     }
1439   }
1440 }
1441
1442 void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
1443 {
1444   bool failed = false;
1445   auto js     = LoadTextFile(url.c_str(), &failed);
1446   if(failed)
1447   {
1448     throw std::runtime_error("Failed to load " + url);
1449   }
1450
1451   json::unique_ptr root(json_parse(js.c_str(), js.size()));
1452   if(!root)
1453   {
1454     throw std::runtime_error("Failed to parse " + url);
1455   }
1456
1457   gt::Document doc;
1458
1459   auto& rootObj = js::Cast<json_object_s>(*root);
1460   auto  jsAsset = js::FindObjectChild("asset", rootObj);
1461
1462   auto jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsAsset));
1463   if(jsAssetVersion)
1464   {
1465     doc.mAsset.mVersion = js::Read::StringView(*jsAssetVersion);
1466   }
1467
1468   bool isMRendererModel(false);
1469   auto jsAssetGenerator = js::FindObjectChild("generator", js::Cast<json_object_s>(*jsAsset));
1470   if(jsAssetGenerator)
1471   {
1472     doc.mAsset.mGenerator = js::Read::StringView(*jsAssetGenerator);
1473     isMRendererModel      = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
1474   }
1475
1476   {
1477     Mutex::ScopedLock lock(gReadMutex);
1478     gt::SetRefReaderObject(doc);
1479     GetDocumentReader().Read(rootObj, doc);
1480   }
1481
1482   auto              path = url.substr(0, url.rfind('/') + 1);
1483   ConversionContext context{params, path, INVALID_INDEX};
1484
1485   ConvertBuffers(doc, context);
1486   ConvertMaterials(doc, context);
1487   ConvertMeshes(doc, context);
1488   ConvertNodes(doc, context, isMRendererModel);
1489   ConvertAnimations(doc, context);
1490   ProcessSkins(doc, context);
1491   ProduceShaders(shaderFactory, params.mScene);
1492   params.mScene.EnsureUniqueSkinningShaderInstances(params.mResources);
1493
1494   // Set Default Environment map
1495   SetDefaultEnvironmentMap(doc, context);
1496 }
1497
1498 } // namespace Loader
1499 } // namespace Scene3D
1500 } // namespace Dali