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