DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / gltf2-loader.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // FILE HEADER
19 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/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             meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_JOINT_IDS;
674             DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
675           }
676         }
677         else if(needNormalsTangents)
678         {
679           switch(am.mType)
680           {
681             case gt::Attribute::NORMAL:
682               meshDefinition.RequestNormals();
683               break;
684
685             case gt::Attribute::TANGENT:
686               meshDefinition.RequestTangents();
687               break;
688
689             default:
690               break;
691           }
692         }
693       }
694
695       if(primitive.mIndices)
696       {
697         meshDefinition.mIndices = ConvertMeshPrimitiveAccessor(*primitive.mIndices);
698         meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
699         DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || primitive.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
700       }
701
702       if(!primitive.mTargets.empty())
703       {
704         meshDefinition.mBlendShapes.reserve(primitive.mTargets.size());
705         meshDefinition.mBlendShapeVersion = BlendShapes::Version::VERSION_2_0;
706         for(const auto& target : primitive.mTargets)
707         {
708           MeshDefinition::BlendShape blendShape;
709
710           auto endIt = target.end();
711           auto it    = target.find(gt::Attribute::POSITION);
712           if(it != endIt)
713           {
714             blendShape.deltas = ConvertMeshPrimitiveAccessor(*it->second);
715           }
716           it = target.find(gt::Attribute::NORMAL);
717           if(it != endIt)
718           {
719             blendShape.normals = ConvertMeshPrimitiveAccessor(*it->second);
720           }
721           it = target.find(gt::Attribute::TANGENT);
722           if(it != endIt)
723           {
724             blendShape.tangents = ConvertMeshPrimitiveAccessor(*it->second);
725           }
726
727           if(!mesh.mWeights.empty())
728           {
729             blendShape.weight = mesh.mWeights[meshDefinition.mBlendShapes.size()];
730           }
731
732           meshDefinition.mBlendShapes.push_back(std::move(blendShape));
733         }
734       }
735
736       outMeshes.push_back({std::move(meshDefinition), MeshGeometry{}});
737     }
738   }
739 }
740
741 ModelRenderable* MakeModelRenderable(const gt::Mesh::Primitive& prim, ConversionContext& context)
742 {
743   auto modelRenderable = new ModelRenderable();
744
745   modelRenderable->mShaderIdx = 0; // TODO: further thought
746
747   auto materialIdx = prim.mMaterial.GetIndex();
748   if(INVALID_INDEX == materialIdx)
749   {
750     // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
751     if(INVALID_INDEX == context.mDefaultMaterial)
752     {
753       auto& outMaterials       = context.mOutput.mResources.mMaterials;
754       context.mDefaultMaterial = outMaterials.size();
755
756       ConvertMaterial(gt::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials);
757     }
758
759     materialIdx = context.mDefaultMaterial;
760   }
761
762   modelRenderable->mMaterialIdx = materialIdx;
763
764   return modelRenderable;
765 }
766
767 void ConvertCamera(const gt::Camera& camera, CameraParameters& camParams)
768 {
769   camParams.isPerspective = camera.mType.compare("perspective") == 0;
770   if(camParams.isPerspective)
771   {
772     auto& perspective = camera.mPerspective;
773     camParams.yFov    = Degree(Radian(perspective.mYFov)).degree;
774     camParams.zNear   = perspective.mZNear;
775     camParams.zFar    = perspective.mZFar;
776     // TODO: yes, we seem to ignore aspectRatio in CameraParameters.
777   }
778   else
779   {
780     auto& ortho                = camera.mOrthographic;
781     camParams.orthographicSize = ortho.mYMag * .5f;
782     camParams.aspectRatio      = ortho.mXMag / ortho.mYMag;
783     camParams.zNear            = ortho.mZNear;
784     camParams.zFar             = ortho.mZFar;
785   }
786 }
787
788 void ConvertNode(gt::Node const& node, const Index gltfIdx, Index parentIdx, ConversionContext& context, bool isMRendererModel)
789 {
790   auto& output    = context.mOutput;
791   auto& scene     = output.mScene;
792   auto& resources = output.mResources;
793
794   const auto idx      = scene.GetNodeCount();
795   auto       weakNode = scene.AddNode([&]() {
796     std::unique_ptr<NodeDefinition> nodeDef{new NodeDefinition()};
797
798     nodeDef->mParentIdx = parentIdx;
799     nodeDef->mName      = node.mName;
800     if(nodeDef->mName.empty())
801     {
802       // TODO: Production quality generation of unique names.
803       nodeDef->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDef.get()));
804     }
805
806     if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
807     {
808       nodeDef->mPosition    = node.mTranslation;
809       nodeDef->mOrientation = node.mRotation;
810       nodeDef->mScale       = node.mScale;
811
812       if(isMRendererModel && node.mName == ROOT_NODE_NAME && node.mScale == SCALE_TO_ADJUST)
813       {
814         nodeDef->mScale *= 0.01f;
815       }
816     }
817
818     return nodeDef;
819   }());
820   if(!weakNode)
821   {
822     ExceptionFlinger(ASSERT_LOCATION) << "Node name '" << node.mName << "' is not unique; scene is invalid.";
823   }
824
825   context.mNodeIndices.RegisterMapping(gltfIdx, idx);
826
827   Index skeletonIdx = node.mSkin ? node.mSkin.GetIndex() : INVALID_INDEX;
828   if(node.mMesh)
829   {
830     auto&    mesh           = *node.mMesh;
831     uint32_t primitiveCount = mesh.mPrimitives.size();
832     auto     meshIdx        = context.mMeshIds[node.mMesh.GetIndex()];
833     weakNode->mRenderables.reserve(primitiveCount);
834     for(uint32_t i = 0; i < primitiveCount; ++i)
835     {
836       std::unique_ptr<NodeDefinition::Renderable> renderable;
837       auto                                        modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
838       modelRenderable->mMeshIdx                                   = meshIdx + i;
839
840       DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
841                         resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
842       resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx = skeletonIdx;
843
844       renderable.reset(modelRenderable);
845       weakNode->mRenderables.push_back(std::move(renderable));
846     }
847   }
848
849   if(node.mCamera)
850   {
851     CameraParameters camParams;
852     ConvertCamera(*node.mCamera, camParams);
853
854     camParams.matrix.SetTransformComponents(node.mScale, node.mRotation, node.mTranslation);
855     output.mCameraParameters.push_back(camParams);
856   }
857
858   for(auto& n : node.mChildren)
859   {
860     ConvertNode(*n, n.GetIndex(), idx, context, isMRendererModel);
861   }
862 }
863
864 void ConvertSceneNodes(const gt::Scene& scene, ConversionContext& context, bool isMRendererModel)
865 {
866   auto& outScene = context.mOutput.mScene;
867   Index rootIdx  = outScene.GetNodeCount();
868   switch(scene.mNodes.size())
869   {
870     case 0:
871       break;
872
873     case 1:
874       ConvertNode(*scene.mNodes[0], scene.mNodes[0].GetIndex(), INVALID_INDEX, context, isMRendererModel);
875       outScene.AddRootNode(rootIdx);
876       break;
877
878     default:
879     {
880       std::unique_ptr<NodeDefinition> sceneRoot{new NodeDefinition()};
881       sceneRoot->mName = "GLTF_LOADER_SCENE_ROOT_" + std::to_string(outScene.GetRoots().size());
882
883       outScene.AddNode(std::move(sceneRoot));
884       outScene.AddRootNode(rootIdx);
885
886       for(auto& n : scene.mNodes)
887       {
888         ConvertNode(*n, n.GetIndex(), rootIdx, context, isMRendererModel);
889       }
890       break;
891     }
892   }
893 }
894
895 void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel)
896 {
897   if(!doc.mScenes.empty())
898   {
899     uint32_t rootSceneIndex = 0u;
900     if(doc.mScene)
901     {
902       rootSceneIndex = doc.mScene.GetIndex();
903     }
904     ConvertSceneNodes(doc.mScenes[rootSceneIndex], context, isMRendererModel);
905
906     for(uint32_t i = 0, i1 = rootSceneIndex; i < i1; ++i)
907     {
908       ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
909     }
910
911     for(uint32_t i = rootSceneIndex + 1; i < doc.mScenes.size(); ++i)
912     {
913       ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
914     }
915   }
916 }
917
918 template<typename T>
919 void LoadDataFromAccessor(const std::string& path, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
920 {
921   std::ifstream animationBinaryFile(path, std::ifstream::binary);
922
923   if(!animationBinaryFile.is_open())
924   {
925     throw std::runtime_error("Failed to load " + path);
926   }
927
928   animationBinaryFile.seekg(offset);
929   animationBinaryFile.read(reinterpret_cast<char*>(dataBuffer.Begin()), size);
930   animationBinaryFile.close();
931 }
932
933 template<typename T>
934 float LoadDataFromAccessors(const std::string& path, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
935 {
936   inputDataBuffer.Resize(input.mCount);
937   outputDataBuffer.Resize(output.mCount);
938
939   const uint32_t inputDataBufferSize  = input.GetBytesLength();
940   const uint32_t outputDataBufferSize = output.GetBytesLength();
941
942   LoadDataFromAccessor<float>(path + std::string(input.mBufferView->mBuffer->mUri), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
943   LoadDataFromAccessor<T>(path + std::string(output.mBufferView->mBuffer->mUri), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
944   ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
945
946   return inputDataBuffer[input.mCount - 1u];
947 }
948
949 template<typename T>
950 float LoadKeyFrames(const std::string& path, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
951 {
952   const gltf2::Accessor& input  = *channel.mSampler->mInput;
953   const gltf2::Accessor& output = *channel.mSampler->mOutput;
954
955   Vector<float> inputDataBuffer;
956   Vector<T>     outputDataBuffer;
957
958   const float duration = std::max(LoadDataFromAccessors<T>(path, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
959
960   for(uint32_t i = 0; i < input.mCount; ++i)
961   {
962     keyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i]);
963   }
964
965   return duration;
966 }
967
968 float LoadBlendShapeKeyFrames(const std::string& path, const gt::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
969 {
970   const gltf2::Accessor& input  = *channel.mSampler->mInput;
971   const gltf2::Accessor& output = *channel.mSampler->mOutput;
972
973   Vector<float> inputDataBuffer;
974   Vector<float> outputDataBuffer;
975
976   const float duration = LoadDataFromAccessors<float>(path, input, output, inputDataBuffer, outputDataBuffer);
977
978   char        weightNameBuffer[32];
979   auto        prefixSize    = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM.c_str());
980   char* const pWeightName   = weightNameBuffer + prefixSize;
981   const auto  remainingSize = sizeof(weightNameBuffer) - prefixSize;
982   for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex)
983   {
984     AnimatedProperty& animatedProperty = properties[propertyIndex++];
985
986     animatedProperty.mNodeIndex = nodeIndex;
987     snprintf(pWeightName, remainingSize, "%d]", weightIndex);
988     animatedProperty.mPropertyName = std::string(weightNameBuffer);
989
990     animatedProperty.mKeyFrames = KeyFrames::New();
991     for(uint32_t i = 0; i < input.mCount; ++i)
992     {
993       animatedProperty.mKeyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i * endWeightIndex + weightIndex]);
994     }
995
996     animatedProperty.mTimePeriod = {0.f, duration};
997   }
998
999   return duration;
1000 }
1001
1002 void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
1003 {
1004   auto& output = context.mOutput;
1005
1006   output.mAnimationDefinitions.reserve(output.mAnimationDefinitions.size() + doc.mAnimations.size());
1007
1008   for(const auto& animation : doc.mAnimations)
1009   {
1010     AnimationDefinition animationDef;
1011
1012     if(!animation.mName.empty())
1013     {
1014       animationDef.mName = animation.mName;
1015     }
1016
1017     uint32_t numberOfProperties = 0u;
1018     for(const auto& channel : animation.mChannels)
1019     {
1020       if(channel.mTarget.mPath == gt::Animation::Channel::Target::WEIGHTS)
1021       {
1022         numberOfProperties += channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount;
1023       }
1024       else
1025       {
1026         numberOfProperties++;
1027       }
1028     }
1029     animationDef.mProperties.resize(numberOfProperties);
1030
1031     Index propertyIndex = 0u;
1032     for(const auto& channel : animation.mChannels)
1033     {
1034       Index nodeIndex = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
1035       float duration  = 0.f;
1036
1037       switch(channel.mTarget.mPath)
1038       {
1039         case gt::Animation::Channel::Target::TRANSLATION:
1040         {
1041           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1042
1043           animatedProperty.mNodeIndex    = nodeIndex;
1044           animatedProperty.mPropertyName = POSITION_PROPERTY;
1045
1046           animatedProperty.mKeyFrames = KeyFrames::New();
1047           duration                    = LoadKeyFrames<Vector3>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1048
1049           animatedProperty.mTimePeriod = {0.f, duration};
1050           break;
1051         }
1052         case gt::Animation::Channel::Target::ROTATION:
1053         {
1054           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1055
1056           animatedProperty.mNodeIndex    = nodeIndex;
1057           animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
1058
1059           animatedProperty.mKeyFrames = KeyFrames::New();
1060           duration                    = LoadKeyFrames<Quaternion>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1061
1062           animatedProperty.mTimePeriod = {0.f, duration};
1063           break;
1064         }
1065         case gt::Animation::Channel::Target::SCALE:
1066         {
1067           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1068
1069           animatedProperty.mNodeIndex    = nodeIndex;
1070           animatedProperty.mPropertyName = SCALE_PROPERTY;
1071
1072           animatedProperty.mKeyFrames = KeyFrames::New();
1073           duration                    = LoadKeyFrames<Vector3>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1074
1075           animatedProperty.mTimePeriod = {0.f, duration};
1076           break;
1077         }
1078         case gt::Animation::Channel::Target::WEIGHTS:
1079         {
1080           duration = LoadBlendShapeKeyFrames(context.mPath, channel, nodeIndex, propertyIndex, animationDef.mProperties);
1081
1082           break;
1083         }
1084         default:
1085         {
1086           // nothing to animate.
1087           break;
1088         }
1089       }
1090
1091       animationDef.mDuration = std::max(duration, animationDef.mDuration);
1092
1093       ++propertyIndex;
1094     }
1095
1096     output.mAnimationDefinitions.push_back(std::move(animationDef));
1097   }
1098 }
1099
1100 void ProcessSkins(const gt::Document& doc, ConversionContext& context)
1101 {
1102   // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skininversebindmatrices
1103   // If an inverseBindMatrices accessor was provided, we'll load the joint data from the buffer,
1104   // otherwise we'll set identity matrices for inverse bind pose.
1105   struct IInverseBindMatrixProvider
1106   {
1107     virtual ~IInverseBindMatrixProvider()
1108     {
1109     }
1110     virtual void Provide(Matrix& ibm) = 0;
1111   };
1112
1113   struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
1114   {
1115     std::ifstream  mStream;
1116     const uint32_t mElementSizeBytes;
1117
1118     InverseBindMatrixAccessor(const gt::Accessor& accessor, const std::string& path)
1119     : mStream(path + std::string(accessor.mBufferView->mBuffer->mUri), std::ios::binary),
1120       mElementSizeBytes(accessor.GetElementSizeBytes())
1121     {
1122       DALI_ASSERT_ALWAYS(mStream);
1123       DALI_ASSERT_DEBUG(accessor.mType == gt::AccessorType::MAT4 && accessor.mComponentType == gt::Component::FLOAT);
1124
1125       mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset);
1126     }
1127
1128     virtual void Provide(Matrix& ibm) override
1129     {
1130       DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), mElementSizeBytes));
1131     }
1132   };
1133
1134   struct DefaultInverseBindMatrixProvider : public IInverseBindMatrixProvider
1135   {
1136     virtual void Provide(Matrix& ibm) override
1137     {
1138       ibm = Matrix::IDENTITY;
1139     }
1140   };
1141
1142   auto& resources = context.mOutput.mResources;
1143   resources.mSkeletons.reserve(doc.mSkins.size());
1144
1145   for(auto& s : doc.mSkins)
1146   {
1147     std::unique_ptr<IInverseBindMatrixProvider> ibmProvider;
1148     if(s.mInverseBindMatrices)
1149     {
1150       ibmProvider.reset(new InverseBindMatrixAccessor(*s.mInverseBindMatrices, context.mPath));
1151     }
1152     else
1153     {
1154       ibmProvider.reset(new DefaultInverseBindMatrixProvider());
1155     }
1156
1157     SkeletonDefinition skeleton;
1158     if(s.mSkeleton.GetIndex() != INVALID_INDEX)
1159     {
1160       skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(s.mSkeleton.GetIndex());
1161     }
1162
1163     skeleton.mJoints.resize(s.mJoints.size());
1164     auto iJoint = skeleton.mJoints.begin();
1165     for(auto& j : s.mJoints)
1166     {
1167       iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(j.GetIndex());
1168
1169       ibmProvider->Provide(iJoint->mInverseBindMatrix);
1170
1171       ++iJoint;
1172     }
1173
1174     resources.mSkeletons.push_back(std::move(skeleton));
1175   }
1176 }
1177
1178 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene)
1179 {
1180   uint32_t nodeCount = scene.GetNodeCount();
1181   for(uint32_t i = 0; i < nodeCount; ++i)
1182   {
1183     auto nodeDef = scene.GetNode(i);
1184     for(auto& renderable : nodeDef->mRenderables)
1185     {
1186       if(shaderFactory.ProduceShader(*renderable) == INVALID_INDEX)
1187       {
1188         DALI_LOG_ERROR("Fail to produce shader\n");
1189       }
1190     }
1191   }
1192 }
1193
1194 void SetObjectReaders()
1195 {
1196   js::SetObjectReader(BUFFER_READER);
1197   js::SetObjectReader(BUFFER_VIEW_READER);
1198   js::SetObjectReader(BUFFER_VIEW_CLIENT_READER);
1199   js::SetObjectReader(COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER);
1200   js::SetObjectReader(ACCESSOR_SPARSE_READER);
1201   js::SetObjectReader(ACCESSOR_READER);
1202   js::SetObjectReader(IMAGE_READER);
1203   js::SetObjectReader(SAMPLER_READER);
1204   js::SetObjectReader(TEXURE_READER);
1205   js::SetObjectReader(TEXURE_INFO_READER);
1206   js::SetObjectReader(MATERIAL_PBR_READER);
1207   js::SetObjectReader(MATERIAL_SPECULAR_READER);
1208   js::SetObjectReader(MATERIAL_IOR_READER);
1209   js::SetObjectReader(MATERIAL_EXTENSION_READER);
1210   js::SetObjectReader(MATERIAL_READER);
1211   js::SetObjectReader(MESH_PRIMITIVE_READER);
1212   js::SetObjectReader(MESH_READER);
1213   js::SetObjectReader(SKIN_READER);
1214   js::SetObjectReader(CAMERA_PERSPECTIVE_READER);
1215   js::SetObjectReader(CAMERA_ORTHOGRAPHIC_READER);
1216   js::SetObjectReader(CAMERA_READER);
1217   js::SetObjectReader(NODE_READER);
1218   js::SetObjectReader(ANIMATION_SAMPLER_READER);
1219   js::SetObjectReader(ANIMATION_TARGET_READER);
1220   js::SetObjectReader(ANIMATION_CHANNEL_READER);
1221   js::SetObjectReader(ANIMATION_READER);
1222   js::SetObjectReader(SCENE_READER);
1223 }
1224
1225 void SetDefaultEnvironmentMap(const gt::Document& doc, ConversionContext& context)
1226 {
1227   EnvironmentDefinition envDef;
1228   envDef.mUseBrdfTexture = true;
1229   envDef.mIblIntensity   = Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
1230   context.mOutput.mResources.mEnvironmentMaps.push_back({std::move(envDef), EnvironmentDefinition::Textures()});
1231 }
1232
1233 } // namespace
1234
1235 void InitializeGltfLoader()
1236 {
1237   // Set ObjectReader only once (for all gltf loading).
1238   static bool setObjectReadersRequired = true;
1239   {
1240     Mutex::ScopedLock lock(gInitializeMutex);
1241     if(setObjectReadersRequired)
1242     {
1243       // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1244       SetObjectReaders();
1245       setObjectReadersRequired = false;
1246     }
1247   }
1248 }
1249
1250 void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
1251 {
1252   bool failed = false;
1253   auto js     = LoadTextFile(url.c_str(), &failed);
1254   if(failed)
1255   {
1256     throw std::runtime_error("Failed to load " + url);
1257   }
1258
1259   json::unique_ptr root(json_parse(js.c_str(), js.size()));
1260   if(!root)
1261   {
1262     throw std::runtime_error("Failed to parse " + url);
1263   }
1264
1265   gt::Document doc;
1266
1267   auto& rootObj = js::Cast<json_object_s>(*root);
1268   auto  jsAsset = js::FindObjectChild("asset", rootObj);
1269
1270   auto jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsAsset));
1271   if(jsAssetVersion)
1272   {
1273     doc.mAsset.mVersion = js::Read::StringView(*jsAssetVersion);
1274   }
1275
1276   bool isMRendererModel(false);
1277   auto jsAssetGenerator = js::FindObjectChild("generator", js::Cast<json_object_s>(*jsAsset));
1278   if(jsAssetGenerator)
1279   {
1280     doc.mAsset.mGenerator = js::Read::StringView(*jsAssetGenerator);
1281     isMRendererModel      = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
1282   }
1283
1284   {
1285     Mutex::ScopedLock lock(gReadMutex);
1286     gt::SetRefReaderObject(doc);
1287     DOCUMENT_READER.Read(rootObj, doc);
1288   }
1289
1290   auto              path = url.substr(0, url.rfind('/') + 1);
1291   ConversionContext context{params, path, INVALID_INDEX};
1292
1293   ConvertMaterials(doc, context);
1294   ConvertMeshes(doc, context);
1295   ConvertNodes(doc, context, isMRendererModel);
1296   ConvertAnimations(doc, context);
1297   ProcessSkins(doc, context);
1298   ProduceShaders(shaderFactory, params.mScene);
1299   params.mScene.EnsureUniqueSkinningShaderInstances(params.mResources);
1300
1301   // Set Default Environment map
1302   SetDefaultEnvironmentMap(doc, context);
1303 }
1304
1305 } // namespace Loader
1306 } // namespace Scene3D
1307 } // namespace Dali