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