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