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