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