2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
22 #include <dali/devel-api/threading/mutex.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/public-api/images/image-operations.h>
25 #include <dali/public-api/math/quaternion.h>
29 #include <dali-scene3d/internal/loader/gltf2-asset.h>
30 #include <dali-scene3d/public-api/loader/load-result.h>
31 #include <dali-scene3d/public-api/loader/resource-bundle.h>
32 #include <dali-scene3d/public-api/loader/scene-definition.h>
33 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
34 #include <dali-scene3d/public-api/loader/utils.h>
47 Dali::Mutex gInitializeMutex;
48 Dali::Mutex gReadMutex;
50 const char* POSITION_PROPERTY("position");
51 const char* ORIENTATION_PROPERTY("orientation");
52 const char* SCALE_PROPERTY("scale");
53 const char* BLEND_SHAPE_WEIGHTS_UNIFORM("uBlendShapeWeight");
54 const char* MRENDERER_MODEL_IDENTIFICATION("M-Renderer");
55 const char* ROOT_NODE_NAME("RootNode");
56 const Vector3 SCALE_TO_ADJUST(100.0f, 100.0f, 100.0f);
58 const Geometry::Type GLTF2_TO_DALI_PRIMITIVES[]{
64 Geometry::TRIANGLE_STRIP,
65 Geometry::TRIANGLE_FAN}; //...because Dali swaps the last two.
67 struct AttributeMapping
69 gt::Attribute::Type mType;
70 MeshDefinition::Accessor MeshDefinition::*mAccessor;
71 uint16_t mElementSizeRequired;
72 } ATTRIBUTE_MAPPINGS[]{
73 {gt::Attribute::NORMAL, &MeshDefinition::mNormals, sizeof(Vector3)},
74 {gt::Attribute::TANGENT, &MeshDefinition::mTangents, sizeof(Vector3)},
75 {gt::Attribute::TEXCOORD_0, &MeshDefinition::mTexCoords, sizeof(Vector2)},
76 {gt::Attribute::COLOR_0, &MeshDefinition::mColors, sizeof(Vector4)},
77 {gt::Attribute::JOINTS_0, &MeshDefinition::mJoints0, sizeof(Vector4)},
78 {gt::Attribute::WEIGHTS_0, &MeshDefinition::mWeights0, sizeof(Vector4)},
81 std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
83 auto results = js::Read::Array<gt::Animation, js::ObjectReader<gt::Animation>::Read>(j);
85 for(auto& animation : results)
87 for(auto& channel : animation.mChannels)
89 channel.mSampler.UpdateVector(animation.mSamplers);
96 void ApplyAccessorMinMax(const gt::Accessor& acc, float* values)
98 DALI_ASSERT_ALWAYS(acc.mMax.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMax.size());
99 DALI_ASSERT_ALWAYS(acc.mMin.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMin.size());
100 MeshDefinition::Blob::ApplyMinMax(acc.mMin, acc.mMax, acc.mCount, values);
103 const js::Reader<gt::Buffer>& GetBufferReader()
105 static const auto BUFFER_READER = std::move(js::Reader<gt::Buffer>()
106 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::Buffer::mByteLength))
107 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Buffer::mUri)));
108 return BUFFER_READER;
111 const js::Reader<gt::BufferView>& GetBufferViewReader()
113 static const auto BUFFER_VIEW_READER = std::move(js::Reader<gt::BufferView>()
114 .Register(*js::MakeProperty("buffer", gt::RefReader<gt::Document>::Read<gt::Buffer, >::Document::mBuffers>, >::BufferView::mBuffer))
115 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferView::mByteOffset))
116 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::BufferView::mByteLength))
117 .Register(*js::MakeProperty("byteStride", js::Read::Number<uint32_t>, >::BufferView::mByteStride))
118 .Register(*js::MakeProperty("target", js::Read::Number<uint32_t>, >::BufferView::mTarget)));
119 return BUFFER_VIEW_READER;
122 const js::Reader<gt::BufferViewClient>& GetBufferViewClientReader()
124 static const auto BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::BufferViewClient>()
125 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::BufferViewClient::mBufferView))
126 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferViewClient::mByteOffset)));
127 return BUFFER_VIEW_CLIENT_READER;
130 const js::Reader<gt::ComponentTypedBufferViewClient>& GetComponentTypedBufferViewClientReader()
132 static const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::ComponentTypedBufferViewClient>()
133 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, gt::Ref<gt::BufferView>>("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::ComponentTypedBufferViewClient::mBufferView))
134 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, uint32_t>("byteOffset", js::Read::Number<uint32_t>, >::ComponentTypedBufferViewClient::mByteOffset))
135 .Register(*js::MakeProperty("componentType", js::Read::Enum<gt::Component::Type>, >::ComponentTypedBufferViewClient::mComponentType)));
136 return COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER;
139 const js::Reader<gt::Accessor::Sparse>& GetAccessorSparseReader()
141 static const auto ACCESSOR_SPARSE_READER = std::move(js::Reader<gt::Accessor::Sparse>()
142 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::Sparse::mCount))
143 .Register(*js::MakeProperty("indices", js::ObjectReader<gt::ComponentTypedBufferViewClient>::Read, >::Accessor::Sparse::mIndices))
144 .Register(*js::MakeProperty("values", js::ObjectReader<gt::BufferViewClient>::Read, >::Accessor::Sparse::mValues)));
145 return ACCESSOR_SPARSE_READER;
148 const js::Reader<gt::Accessor>& GetAccessorReader()
150 static const auto ACCESSOR_READER = std::move(js::Reader<gt::Accessor>()
151 .Register(*new js::Property<gt::Accessor, gt::Ref<gt::BufferView>>("bufferView",
152 gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>,
153 >::Accessor::mBufferView))
154 .Register(*new js::Property<gt::Accessor, uint32_t>("byteOffset",
155 js::Read::Number<uint32_t>,
156 >::Accessor::mByteOffset))
157 .Register(*new js::Property<gt::Accessor, gt::Component::Type>("componentType",
158 js::Read::Enum<gt::Component::Type>,
159 >::Accessor::mComponentType))
160 .Register(*new js::Property<gt::Accessor, std::string_view>("name", js::Read::StringView, >::Accessor::mName))
161 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::mCount))
162 .Register(*js::MakeProperty("normalized", js::Read::Boolean, >::Accessor::mNormalized))
163 .Register(*js::MakeProperty("type", gt::ReadStringEnum<gt::AccessorType>, >::Accessor::mType))
164 .Register(*js::MakeProperty("min", js::Read::Array<float, js::Read::Number>, >::Accessor::mMin))
165 .Register(*js::MakeProperty("max", js::Read::Array<float, js::Read::Number>, >::Accessor::mMax))
166 .Register(*new js::Property<gt::Accessor, gt::Accessor::Sparse>("sparse", js::ObjectReader<gt::Accessor::Sparse>::Read, >::Accessor::SetSparse)));
167 return ACCESSOR_READER;
170 const js::Reader<gt::Image>& GetImageReader()
172 static const auto IMAGE_READER = std::move(js::Reader<gt::Image>()
173 .Register(*new js::Property<gt::Image, std::string_view>("name", js::Read::StringView, >::Material::mName))
174 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Image::mUri))
175 .Register(*js::MakeProperty("mimeType", js::Read::StringView, >::Image::mMimeType))
176 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::Image::mBufferView)));
180 const js::Reader<gt::Sampler>& GetSamplerReader()
182 static const auto SAMPLER_READER = std::move(js::Reader<gt::Sampler>()
183 .Register(*js::MakeProperty("minFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMinFilter))
184 .Register(*js::MakeProperty("magFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMagFilter))
185 .Register(*js::MakeProperty("wrapS", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapS))
186 .Register(*js::MakeProperty("wrapT", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapT)));
187 return SAMPLER_READER;
190 const js::Reader<gt::Texture>& GetTextureReader()
192 static const auto TEXURE_READER = std::move(js::Reader<gt::Texture>()
193 .Register(*js::MakeProperty("source", gt::RefReader<gt::Document>::Read<gt::Image, >::Document::mImages>, >::Texture::mSource))
194 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Document>::Read<gt::Sampler, >::Document::mSamplers>, >::Texture::mSampler)));
195 return TEXURE_READER;
198 const js::Reader<gt::TextureInfo>& GetTextureInfoReader()
200 static const auto TEXURE_INFO_READER = std::move(js::Reader<gt::TextureInfo>()
201 .Register(*js::MakeProperty("index", gt::RefReader<gt::Document>::Read<gt::Texture, >::Document::mTextures>, >::TextureInfo::mTexture))
202 .Register(*js::MakeProperty("texCoord", js::Read::Number<uint32_t>, >::TextureInfo::mTexCoord))
203 .Register(*js::MakeProperty("scale", js::Read::Number<float>, >::TextureInfo::mScale))
204 .Register(*js::MakeProperty("strength", js::Read::Number<float>, >::TextureInfo::mStrength)));
205 return TEXURE_INFO_READER;
208 const js::Reader<gt::Material::Pbr>& GetMaterialPbrReader()
210 static const auto MATERIAL_PBR_READER = std::move(js::Reader<gt::Material::Pbr>()
211 .Register(*js::MakeProperty("baseColorFactor", gt::ReadDaliVector<Vector4>, >::Material::Pbr::mBaseColorFactor))
212 .Register(*js::MakeProperty("baseColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mBaseColorTexture))
213 .Register(*js::MakeProperty("metallicFactor", js::Read::Number<float>, >::Material::Pbr::mMetallicFactor))
214 .Register(*js::MakeProperty("roughnessFactor", js::Read::Number<float>, >::Material::Pbr::mRoughnessFactor))
215 .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mMetallicRoughnessTexture)));
216 return MATERIAL_PBR_READER;
219 const js::Reader<gt::MaterialSpecular>& GetMaterialSpecularReader()
221 static const auto MATERIAL_SPECULAR_READER = std::move(js::Reader<gt::MaterialSpecular>()
222 .Register(*js::MakeProperty("specularFactor", js::Read::Number<float>, >::MaterialSpecular::mSpecularFactor))
223 .Register(*js::MakeProperty("specularTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularTexture))
224 .Register(*js::MakeProperty("specularColorFactor", gt::ReadDaliVector<Vector3>, >::MaterialSpecular::mSpecularColorFactor))
225 .Register(*js::MakeProperty("specularColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularColorTexture)));
226 return MATERIAL_SPECULAR_READER;
229 const js::Reader<gt::MaterialIor>& GetMaterialIorReader()
231 static const auto MATERIAL_IOR_READER = std::move(js::Reader<gt::MaterialIor>()
232 .Register(*js::MakeProperty("ior", js::Read::Number<float>, >::MaterialIor::mIor)));
233 return MATERIAL_IOR_READER;
236 const js::Reader<gt::MaterialExtensions>& GetMaterialExtensionsReader()
238 static const auto MATERIAL_EXTENSION_READER = std::move(js::Reader<gt::MaterialExtensions>()
239 .Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader<gt::MaterialIor>::Read, >::MaterialExtensions::mMaterialIor))
240 .Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader<gt::MaterialSpecular>::Read, >::MaterialExtensions::mMaterialSpecular)));
241 return MATERIAL_EXTENSION_READER;
244 const js::Reader<gt::Material>& GetMaterialReader()
246 static const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
247 .Register(*new js::Property<gt::Material, std::string_view>("name", js::Read::StringView, >::Material::mName))
248 .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader<gt::Material::Pbr>::Read, >::Material::mPbrMetallicRoughness))
249 .Register(*js::MakeProperty("normalTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mNormalTexture))
250 .Register(*js::MakeProperty("occlusionTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mOcclusionTexture))
251 .Register(*js::MakeProperty("emissiveTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mEmissiveTexture))
252 .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector<Vector3>, >::Material::mEmissiveFactor))
253 .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum<gt::AlphaMode>, >::Material::mAlphaMode))
254 .Register(*js::MakeProperty("alphaCutoff", js::Read::Number<float>, >::Material::mAlphaCutoff))
255 .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, >::Material::mDoubleSided))
256 .Register(*js::MakeProperty("extensions", js::ObjectReader<gt::MaterialExtensions>::Read, >::Material::mMaterialExtensions)));
257 return MATERIAL_READER;
260 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
262 auto& jo = js::Cast<json_object_s>(j);
263 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> result;
268 auto jstr = *i->name;
269 result[gt::Attribute::FromString(jstr.string, jstr.string_size)] = gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>(*i->value);
275 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> ReadMeshPrimitiveTargets(const json_value_s& j)
277 auto& jo = js::Cast<json_array_s>(j);
278 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> result;
280 result.reserve(jo.length);
285 result.push_back(std::move(ReadMeshPrimitiveAttributes(*i->value)));
292 const js::Reader<gt::Mesh::Primitive>& GetMeshPrimitiveReader()
294 static const auto MESH_PRIMITIVE_READER = std::move(js::Reader<gt::Mesh::Primitive>()
295 .Register(*js::MakeProperty("attributes", ReadMeshPrimitiveAttributes, >::Mesh::Primitive::mAttributes))
296 .Register(*js::MakeProperty("indices", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Mesh::Primitive::mIndices))
297 .Register(*js::MakeProperty("material", gt::RefReader<gt::Document>::Read<gt::Material, >::Document::mMaterials>, >::Mesh::Primitive::mMaterial))
298 .Register(*js::MakeProperty("mode", js::Read::Enum<gt::Mesh::Primitive::Mode>, >::Mesh::Primitive::mMode))
299 .Register(*js::MakeProperty("targets", ReadMeshPrimitiveTargets, >::Mesh::Primitive::mTargets)));
300 return MESH_PRIMITIVE_READER;
303 const js::Reader<gt::Mesh>& GetMeshReader()
305 static const auto MESH_READER = std::move(js::Reader<gt::Mesh>()
306 .Register(*new js::Property<gt::Mesh, std::string_view>("name", js::Read::StringView, >::Mesh::mName))
307 .Register(*js::MakeProperty("primitives",
308 js::Read::Array<gt::Mesh::Primitive, js::ObjectReader<gt::Mesh::Primitive>::Read>,
309 >::Mesh::mPrimitives))
310 .Register(*js::MakeProperty("weights", js::Read::Array<float, js::Read::Number>, >::Mesh::mWeights)));
314 const js::Reader<gt::Skin>& GetSkinReader()
316 static const auto SKIN_READER = std::move(js::Reader<gt::Skin>()
317 .Register(*new js::Property<gt::Skin, std::string_view>("name", js::Read::StringView, >::Skin::mName))
318 .Register(*js::MakeProperty("inverseBindMatrices",
319 gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>,
320 >::Skin::mInverseBindMatrices))
321 .Register(*js::MakeProperty("skeleton",
322 gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>,
323 >::Skin::mSkeleton))
324 .Register(*js::MakeProperty("joints",
325 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
326 >::Skin::mJoints)));
330 const js::Reader<gt::Camera::Perspective>& GetCameraPerspectiveReader()
332 static const auto CAMERA_PERSPECTIVE_READER = std::move(js::Reader<gt::Camera::Perspective>()
333 .Register(*js::MakeProperty("aspectRatio", js::Read::Number<float>, >::Camera::Perspective::mAspectRatio))
334 .Register(*js::MakeProperty("yfov", js::Read::Number<float>, >::Camera::Perspective::mYFov))
335 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Perspective::mZFar))
336 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted
337 return CAMERA_PERSPECTIVE_READER;
340 const js::Reader<gt::Camera::Orthographic>& GetCameraOrthographicReader()
342 static const auto CAMERA_ORTHOGRAPHIC_READER = std::move(js::Reader<gt::Camera::Orthographic>()
343 .Register(*js::MakeProperty("xmag", js::Read::Number<float>, >::Camera::Orthographic::mXMag))
344 .Register(*js::MakeProperty("ymag", js::Read::Number<float>, >::Camera::Orthographic::mYMag))
345 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Orthographic::mZFar))
346 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Orthographic::mZNear)));
347 return CAMERA_ORTHOGRAPHIC_READER;
350 const js::Reader<gt::Camera>& GetCameraReader()
352 static const auto CAMERA_READER = std::move(js::Reader<gt::Camera>()
353 .Register(*new js::Property<gt::Camera, std::string_view>("name", js::Read::StringView, >::Camera::mName))
354 .Register(*js::MakeProperty("type", js::Read::StringView, >::Camera::mType))
355 .Register(*js::MakeProperty("perspective", js::ObjectReader<gt::Camera::Perspective>::Read, >::Camera::mPerspective))
356 .Register(*js::MakeProperty("orthographic", js::ObjectReader<gt::Camera::Orthographic>::Read, >::Camera::mOrthographic)));
357 return CAMERA_READER;
360 const js::Reader<gt::Node>& GetNodeReader()
362 static const auto NODE_READER = std::move(js::Reader<gt::Node>()
363 .Register(*new js::Property<gt::Node, std::string_view>("name", js::Read::StringView, >::Node::mName))
364 .Register(*js::MakeProperty("translation", gt::ReadDaliVector<Vector3>, >::Node::mTranslation))
365 .Register(*js::MakeProperty("rotation", gt::ReadQuaternion, >::Node::mRotation))
366 .Register(*js::MakeProperty("scale", gt::ReadDaliVector<Vector3>, >::Node::mScale))
367 .Register(*new js::Property<gt::Node, Matrix>("matrix", gt::ReadDaliVector<Matrix>, >::Node::SetMatrix))
368 .Register(*js::MakeProperty("camera", gt::RefReader<gt::Document>::Read<gt::Camera, >::Document::mCameras>, >::Node::mCamera))
369 .Register(*js::MakeProperty("children", js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>, >::Node::mChildren))
370 .Register(*js::MakeProperty("mesh", gt::RefReader<gt::Document>::Read<gt::Mesh, >::Document::mMeshes>, >::Node::mMesh))
371 .Register(*js::MakeProperty("skin", gt::RefReader<gt::Document>::Read<gt::Skin, >::Document::mSkins>, >::Node::mSkin)));
375 const js::Reader<gt::Animation::Sampler>& GetAnimationSamplerReader()
377 static const auto ANIMATION_SAMPLER_READER = std::move(js::Reader<gt::Animation::Sampler>()
378 .Register(*js::MakeProperty("input", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mInput))
379 .Register(*js::MakeProperty("output", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mOutput))
380 .Register(*js::MakeProperty("interpolation", gt::ReadStringEnum<gt::Animation::Sampler::Interpolation>, >::Animation::Sampler::mInterpolation)));
381 return ANIMATION_SAMPLER_READER;
384 const js::Reader<gt::Animation::Channel::Target>& GetAnimationChannelTargetReader()
386 static const auto ANIMATION_TARGET_READER = std::move(js::Reader<gt::Animation::Channel::Target>()
387 .Register(*js::MakeProperty("node", gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>, >::Animation::Channel::Target::mNode))
388 .Register(*js::MakeProperty("path", gt::ReadStringEnum<gt::Animation::Channel::Target>, >::Animation::Channel::Target::mPath)));
389 return ANIMATION_TARGET_READER;
392 const js::Reader<gt::Animation::Channel>& GetAnimationChannelReader()
394 static const auto ANIMATION_CHANNEL_READER = std::move(js::Reader<gt::Animation::Channel>()
395 .Register(*js::MakeProperty("target", js::ObjectReader<gt::Animation::Channel::Target>::Read, >::Animation::Channel::mTarget))
396 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Animation>::Read<gt::Animation::Sampler, >::Animation::mSamplers>, >::Animation::Channel::mSampler)));
397 return ANIMATION_CHANNEL_READER;
400 const js::Reader<gt::Animation>& GetAnimationReader()
402 static const auto ANIMATION_READER = std::move(js::Reader<gt::Animation>()
403 .Register(*new js::Property<gt::Animation, std::string_view>("name", js::Read::StringView, >::Animation::mName))
404 .Register(*js::MakeProperty("samplers",
405 js::Read::Array<gt::Animation::Sampler, js::ObjectReader<gt::Animation::Sampler>::Read>,
406 >::Animation::mSamplers))
407 .Register(*js::MakeProperty("channels",
408 js::Read::Array<gt::Animation::Channel, js::ObjectReader<gt::Animation::Channel>::Read>,
409 >::Animation::mChannels)));
410 return ANIMATION_READER;
413 const js::Reader<gt::Scene>& GetSceneReader()
415 static const auto SCENE_READER = std::move(js::Reader<gt::Scene>()
416 .Register(*new js::Property<gt::Scene, std::string_view>("name", js::Read::StringView, >::Scene::mName))
417 .Register(*js::MakeProperty("nodes",
418 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
419 >::Scene::mNodes)));
423 const js::Reader<gt::Document>& GetDocumentReader()
425 static const auto DOCUMENT_READER = std::move(js::Reader<gt::Document>()
426 .Register(*js::MakeProperty("buffers",
427 js::Read::Array<gt::Buffer, js::ObjectReader<gt::Buffer>::Read>,
428 >::Document::mBuffers))
429 .Register(*js::MakeProperty("bufferViews",
430 js::Read::Array<gt::BufferView, js::ObjectReader<gt::BufferView>::Read>,
431 >::Document::mBufferViews))
432 .Register(*js::MakeProperty("accessors",
433 js::Read::Array<gt::Accessor, js::ObjectReader<gt::Accessor>::Read>,
434 >::Document::mAccessors))
435 .Register(*js::MakeProperty("images",
436 js::Read::Array<gt::Image, js::ObjectReader<gt::Image>::Read>,
437 >::Document::mImages))
438 .Register(*js::MakeProperty("samplers",
439 js::Read::Array<gt::Sampler, js::ObjectReader<gt::Sampler>::Read>,
440 >::Document::mSamplers))
441 .Register(*js::MakeProperty("textures",
442 js::Read::Array<gt::Texture, js::ObjectReader<gt::Texture>::Read>,
443 >::Document::mTextures))
444 .Register(*js::MakeProperty("materials",
445 js::Read::Array<gt::Material, js::ObjectReader<gt::Material>::Read>,
446 >::Document::mMaterials))
447 .Register(*js::MakeProperty("meshes",
448 js::Read::Array<gt::Mesh, js::ObjectReader<gt::Mesh>::Read>,
449 >::Document::mMeshes))
450 .Register(*js::MakeProperty("skins",
451 js::Read::Array<gt::Skin, js::ObjectReader<gt::Skin>::Read>,
452 >::Document::mSkins))
453 .Register(*js::MakeProperty("cameras",
454 js::Read::Array<gt::Camera, js::ObjectReader<gt::Camera>::Read>,
455 >::Document::mCameras))
456 .Register(*js::MakeProperty("nodes",
457 js::Read::Array<gt::Node, js::ObjectReader<gt::Node>::Read>,
458 >::Document::mNodes))
459 .Register(*js::MakeProperty("animations",
461 >::Document::mAnimations))
462 .Register(*js::MakeProperty("scenes",
463 js::Read::Array<gt::Scene, js::ObjectReader<gt::Scene>::Read>,
464 >::Document::mScenes))
465 .Register(*js::MakeProperty("scene", gt::RefReader<gt::Document>::Read<gt::Scene, >::Document::mScenes>, >::Document::mScene)));
466 return DOCUMENT_READER;
475 bool operator<(const NodeMapping& mapping, Index gltfIdx)
477 return mapping.gltfIdx < gltfIdx;
480 class NodeIndexMapper
483 NodeIndexMapper() = default;
484 NodeIndexMapper(const NodeIndexMapper&) = delete;
485 NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
487 ///@brief Registers a mapping of the @a gltfIdx of a node to its @a runtimeIdx .
488 ///@note If the indices are the same, the registration is omitted, in order to
489 /// save growing a vector.
490 void RegisterMapping(Index gltfIdx, Index runtimeIdx)
492 if(gltfIdx != runtimeIdx)
494 auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx);
495 DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIdx != gltfIdx);
496 mNodes.insert(iInsert, NodeMapping{gltfIdx, runtimeIdx});
500 ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIdx.
501 Index GetRuntimeId(Index gltfIdx) const
503 auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx); // using custom operator<
504 return (iFind != mNodes.end() && iFind->gltfIdx == gltfIdx) ? iFind->runtimeIdx : gltfIdx;
508 std::vector<NodeMapping> mNodes;
511 struct ConversionContext
516 Index mDefaultMaterial;
518 std::vector<Index> mMeshIds;
519 NodeIndexMapper mNodeIndices;
522 void ConvertBuffer(const gt::Buffer& buffer, decltype(ResourceBundle::mBuffers)& outBuffers, const std::string& resourcePath)
524 BufferDefinition bufferDefinition;
526 bufferDefinition.mResourcePath = resourcePath;
527 bufferDefinition.mUri = buffer.mUri;
528 bufferDefinition.mByteLength = buffer.mByteLength;
530 outBuffers.emplace_back(std::move(bufferDefinition));
533 void ConvertBuffers(const gt::Document& doc, ConversionContext& context)
535 auto& outBuffers = context.mOutput.mResources.mBuffers;
536 outBuffers.reserve(doc.mBuffers.size());
538 for(auto& buffer : doc.mBuffers)
540 ConvertBuffer(buffer, outBuffers, context.mPath);
544 SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type wrapMode)
548 case gt::Wrap::REPEAT:
549 return SamplerFlags::WRAP_REPEAT;
550 case gt::Wrap::CLAMP_TO_EDGE:
551 return SamplerFlags::WRAP_CLAMP;
552 case gt::Wrap::MIRRORED_REPEAT:
553 return SamplerFlags::WRAP_MIRROR;
555 throw std::runtime_error("Invalid wrap type.");
559 SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& sampler)
563 return ((sampler->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (sampler->mMinFilter - gt::Filter::NEAREST) : ((sampler->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
564 ((sampler->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
565 (ConvertWrapMode(sampler->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
566 (ConvertWrapMode(sampler->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
570 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texturesampler
571 // "The index of the sampler used by this texture. When undefined, a sampler with repeat wrapping and auto filtering should be used."
572 // "What is an auto filtering", I hear you ask. Since there's nothing else to determine mipmapping from - including glTF image
573 // properties, if not in some extension -, we will simply assume linear filtering.
574 return SamplerFlags::FILTER_LINEAR | (SamplerFlags::FILTER_LINEAR << SamplerFlags::FILTER_MAG_SHIFT) |
575 (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_S_SHIFT) | (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_T_SHIFT);
579 TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm, ConversionContext& context, const ImageMetadata& metaData = ImageMetadata())
581 TextureDefinition textureDefinition;
582 std::string uri = std::string(mm.mTexture->mSource->mUri);
585 uint32_t bufferIndex = mm.mTexture->mSource->mBufferView->mBuffer.GetIndex();
586 if(bufferIndex != INVALID_INDEX && context.mOutput.mResources.mBuffers[bufferIndex].IsAvailable())
588 auto& stream = context.mOutput.mResources.mBuffers[bufferIndex].GetBufferStream();
590 stream.seekg(mm.mTexture->mSource->mBufferView->mByteOffset, stream.beg);
591 std::vector<uint8_t> dataBuffer;
592 dataBuffer.resize(mm.mTexture->mSource->mBufferView->mByteLength);
593 stream.read(reinterpret_cast<char*>(dataBuffer.data()), static_cast<std::streamsize>(static_cast<size_t>(mm.mTexture->mSource->mBufferView->mByteLength)));
594 return TextureDefinition{std::move(dataBuffer), ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
596 return TextureDefinition();
600 return TextureDefinition{uri, ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
604 void ConvertMaterial(const gt::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials, ConversionContext& context)
606 auto getTextureMetaData = [](const std::unordered_map<std::string, ImageMetadata>& metaData, const gt::TextureInfo& info) {
607 if(!info.mTexture->mSource->mUri.empty())
609 if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end())
611 return search->second;
614 return ImageMetadata();
617 MaterialDefinition matDef;
619 auto& pbr = material.mPbrMetallicRoughness;
620 if(material.mAlphaMode == gt::AlphaMode::BLEND)
622 matDef.mIsOpaque = false;
623 matDef.mFlags |= MaterialDefinition::TRANSPARENCY;
625 else if(material.mAlphaMode == gt::AlphaMode::MASK)
627 matDef.mIsMask = true;
628 matDef.SetAlphaCutoff(std::min(1.f, std::max(0.f, material.mAlphaCutoff)));
631 matDef.mBaseColorFactor = pbr.mBaseColorFactor;
633 matDef.mTextureStages.reserve(!!pbr.mBaseColorTexture + !!pbr.mMetallicRoughnessTexture + !!material.mNormalTexture + !!material.mOcclusionTexture + !!material.mEmissiveTexture);
634 if(pbr.mBaseColorTexture)
636 const auto semantic = MaterialDefinition::ALBEDO;
637 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture, context, getTextureMetaData(imageMetaData, pbr.mBaseColorTexture))});
638 // TODO: and there had better be one
639 matDef.mFlags |= semantic;
643 matDef.mNeedAlbedoTexture = false;
646 matDef.mMetallic = pbr.mMetallicFactor;
647 matDef.mRoughness = pbr.mRoughnessFactor;
649 if(pbr.mMetallicRoughnessTexture)
651 const auto semantic = MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
652 MaterialDefinition::GLTF_CHANNELS;
653 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture, context, getTextureMetaData(imageMetaData, pbr.mMetallicRoughnessTexture))});
654 // TODO: and there had better be one
655 matDef.mFlags |= semantic;
659 matDef.mNeedMetallicRoughnessTexture = false;
662 matDef.mNormalScale = material.mNormalTexture.mScale;
663 if(material.mNormalTexture)
665 const auto semantic = MaterialDefinition::NORMAL;
666 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture, context, getTextureMetaData(imageMetaData, material.mNormalTexture))});
667 // TODO: and there had better be one
668 matDef.mFlags |= semantic;
672 matDef.mNeedNormalTexture = false;
675 if(material.mOcclusionTexture)
677 const auto semantic = MaterialDefinition::OCCLUSION;
678 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture, context, getTextureMetaData(imageMetaData, material.mOcclusionTexture))});
679 // TODO: and there had better be one
680 matDef.mFlags |= semantic;
681 matDef.mOcclusionStrength = material.mOcclusionTexture.mStrength;
684 if(material.mEmissiveTexture)
686 const auto semantic = MaterialDefinition::EMISSIVE;
687 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture, context, getTextureMetaData(imageMetaData, material.mEmissiveTexture))});
688 // TODO: and there had better be one
689 matDef.mFlags |= semantic;
690 matDef.mEmissiveFactor = material.mEmissiveFactor;
693 if(!Dali::Equals(material.mMaterialExtensions.mMaterialIor.mIor, gltf2::UNDEFINED_FLOAT_VALUE))
695 float ior = material.mMaterialExtensions.mMaterialIor.mIor;
696 matDef.mDielectricSpecular = powf((ior - 1.0f) / (ior + 1.0f), 2.0f);
698 matDef.mSpecularFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularFactor;
699 matDef.mSpecularColorFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor;
701 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)
703 const auto semantic = MaterialDefinition::SPECULAR;
704 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture))});
705 matDef.mFlags |= semantic;
708 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)
710 const auto semantic = MaterialDefinition::SPECULAR_COLOR;
711 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture))});
712 matDef.mFlags |= semantic;
715 matDef.mDoubleSided = material.mDoubleSided;
717 outMaterials.emplace_back(std::move(matDef), TextureSet());
720 void ConvertMaterials(const gt::Document& doc, ConversionContext& context)
722 auto& imageMetaData = context.mOutput.mSceneMetadata.mImageMetadata;
724 auto& outMaterials = context.mOutput.mResources.mMaterials;
725 outMaterials.reserve(doc.mMaterials.size());
727 for(auto& m : doc.mMaterials)
729 ConvertMaterial(m, imageMetaData, outMaterials, context);
733 MeshDefinition::Accessor ConvertMeshPrimitiveAccessor(const gt::Accessor& acc)
735 DALI_ASSERT_ALWAYS((acc.mBufferView &&
736 (acc.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) ||
737 (acc.mSparse && !acc.mBufferView));
739 DALI_ASSERT_ALWAYS(!acc.mSparse ||
740 ((acc.mSparse->mIndices.mBufferView && (acc.mSparse->mIndices.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) &&
741 (acc.mSparse->mValues.mBufferView && (acc.mSparse->mValues.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max()))));
743 MeshDefinition::SparseBlob sparseBlob;
746 const gt::Accessor::Sparse& sparse = *acc.mSparse;
747 const gt::ComponentTypedBufferViewClient& indices = sparse.mIndices;
748 const gt::BufferViewClient& values = sparse.mValues;
750 MeshDefinition::Blob indicesBlob(
751 indices.mBufferView->mByteOffset + indices.mByteOffset,
752 sparse.mCount * indices.GetBytesPerComponent(),
753 static_cast<uint16_t>(indices.mBufferView->mByteStride),
754 static_cast<uint16_t>(indices.GetBytesPerComponent()),
757 MeshDefinition::Blob valuesBlob(
758 values.mBufferView->mByteOffset + values.mByteOffset,
759 sparse.mCount * acc.GetElementSizeBytes(),
760 static_cast<uint16_t>(values.mBufferView->mByteStride),
761 static_cast<uint16_t>(acc.GetElementSizeBytes()),
765 sparseBlob = std::move(MeshDefinition::SparseBlob(std::move(indicesBlob), std::move(valuesBlob), acc.mSparse->mCount));
768 uint32_t bufferViewOffset = 0u;
769 uint32_t bufferViewStride = 0u;
772 bufferViewOffset = acc.mBufferView->mByteOffset;
773 bufferViewStride = acc.mBufferView->mByteStride;
776 return MeshDefinition::Accessor{
777 std::move(MeshDefinition::Blob{bufferViewOffset + acc.mByteOffset,
778 acc.GetBytesLength(),
779 static_cast<uint16_t>(bufferViewStride),
780 static_cast<uint16_t>(acc.GetElementSizeBytes()),
783 std::move(sparseBlob),
784 acc.mBufferView ? acc.mBufferView->mBuffer.GetIndex() : 0};
787 void ConvertMeshes(const gt::Document& doc, ConversionContext& context)
789 uint32_t meshCount = 0;
790 context.mMeshIds.reserve(doc.mMeshes.size());
791 for(auto& mesh : doc.mMeshes)
793 context.mMeshIds.push_back(meshCount);
794 meshCount += mesh.mPrimitives.size();
797 auto& outMeshes = context.mOutput.mResources.mMeshes;
798 outMeshes.reserve(meshCount);
799 for(auto& mesh : doc.mMeshes)
801 for(auto& primitive : mesh.mPrimitives)
803 MeshDefinition meshDefinition;
805 auto& attribs = primitive.mAttributes;
806 meshDefinition.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[primitive.mMode];
808 auto& accPositions = *attribs.find(gt::Attribute::POSITION)->second;
809 meshDefinition.mPositions = ConvertMeshPrimitiveAccessor(accPositions);
810 // glTF2 support vector4 tangent for mesh.
811 // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#meshes-overview
812 meshDefinition.mTangentType = Property::VECTOR4;
814 const bool needNormalsTangents = accPositions.mType == gt::AccessorType::VEC3;
815 for(auto& am : ATTRIBUTE_MAPPINGS)
817 auto iFind = attribs.find(am.mType);
818 if(iFind != attribs.end())
820 auto& accessor = meshDefinition.*(am.mAccessor);
821 accessor = ConvertMeshPrimitiveAccessor(*iFind->second);
823 if(iFind->first == gt::Attribute::JOINTS_0)
825 meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
826 meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_JOINT_IDS;
827 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
830 else if(needNormalsTangents)
834 case gt::Attribute::NORMAL:
835 meshDefinition.RequestNormals();
838 case gt::Attribute::TANGENT:
839 meshDefinition.RequestTangents();
848 if(primitive.mIndices)
850 meshDefinition.mIndices = ConvertMeshPrimitiveAccessor(*primitive.mIndices);
851 meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
852 meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_INDICES;
853 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_INDICES) || primitive.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
856 if(!primitive.mTargets.empty())
858 meshDefinition.mBlendShapes.reserve(primitive.mTargets.size());
859 meshDefinition.mBlendShapeVersion = BlendShapes::Version::VERSION_2_0;
860 for(const auto& target : primitive.mTargets)
862 MeshDefinition::BlendShape blendShape;
864 auto endIt = target.end();
865 auto it = target.find(gt::Attribute::POSITION);
868 blendShape.deltas = ConvertMeshPrimitiveAccessor(*it->second);
870 it = target.find(gt::Attribute::NORMAL);
873 blendShape.normals = ConvertMeshPrimitiveAccessor(*it->second);
875 it = target.find(gt::Attribute::TANGENT);
878 blendShape.tangents = ConvertMeshPrimitiveAccessor(*it->second);
881 if(!mesh.mWeights.empty())
883 blendShape.weight = mesh.mWeights[meshDefinition.mBlendShapes.size()];
886 meshDefinition.mBlendShapes.push_back(std::move(blendShape));
890 outMeshes.push_back({std::move(meshDefinition), MeshGeometry{}});
895 ModelRenderable* MakeModelRenderable(const gt::Mesh::Primitive& prim, ConversionContext& context)
897 auto modelRenderable = new ModelRenderable();
899 modelRenderable->mShaderIdx = 0; // TODO: further thought
901 auto materialIdx = prim.mMaterial.GetIndex();
902 if(INVALID_INDEX == materialIdx)
904 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
905 if(INVALID_INDEX == context.mDefaultMaterial)
907 auto& outMaterials = context.mOutput.mResources.mMaterials;
908 context.mDefaultMaterial = outMaterials.size();
910 ConvertMaterial(gt::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials, context);
913 materialIdx = context.mDefaultMaterial;
916 modelRenderable->mMaterialIdx = materialIdx;
918 return modelRenderable;
921 void ConvertCamera(const gt::Camera& camera, CameraParameters& camParams)
923 camParams.isPerspective = camera.mType.compare("perspective") == 0;
924 if(camParams.isPerspective)
926 auto& perspective = camera.mPerspective;
927 if(!Dali::Equals(perspective.mYFov, gltf2::UNDEFINED_FLOAT_VALUE))
929 camParams.yFovDegree = Degree(Radian(perspective.mYFov));
933 camParams.yFovDegree = Degree(gltf2::UNDEFINED_FLOAT_VALUE);
935 camParams.zNear = perspective.mZNear;
936 camParams.zFar = perspective.mZFar;
937 // TODO: yes, we seem to ignore aspectRatio in CameraParameters.
941 auto& ortho = camera.mOrthographic;
942 if(!Dali::Equals(ortho.mYMag, gltf2::UNDEFINED_FLOAT_VALUE) && !Dali::Equals(ortho.mXMag, gltf2::UNDEFINED_FLOAT_VALUE))
944 camParams.orthographicSize = ortho.mYMag * .5f;
945 camParams.aspectRatio = ortho.mXMag / ortho.mYMag;
949 camParams.orthographicSize = gltf2::UNDEFINED_FLOAT_VALUE;
950 camParams.aspectRatio = gltf2::UNDEFINED_FLOAT_VALUE;
952 camParams.zNear = ortho.mZNear;
953 camParams.zFar = ortho.mZFar;
957 void ConvertNode(gt::Node const& node, const Index gltfIdx, Index parentIdx, ConversionContext& context, bool isMRendererModel)
959 auto& output = context.mOutput;
960 auto& scene = output.mScene;
961 auto& resources = output.mResources;
963 const auto idx = scene.GetNodeCount();
964 auto weakNode = scene.AddNode([&]() {
965 std::unique_ptr<NodeDefinition> nodeDef{new NodeDefinition()};
967 nodeDef->mParentIdx = parentIdx;
968 nodeDef->mName = node.mName;
969 if(nodeDef->mName.empty())
971 // TODO: Production quality generation of unique names.
972 nodeDef->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDef.get()));
975 if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
977 nodeDef->mPosition = node.mTranslation;
978 nodeDef->mOrientation = node.mRotation;
979 nodeDef->mScale = node.mScale;
981 if(isMRendererModel && node.mName == ROOT_NODE_NAME && node.mScale == SCALE_TO_ADJUST)
983 nodeDef->mScale *= 0.01f;
991 ExceptionFlinger(ASSERT_LOCATION) << "Node name '" << node.mName << "' is not unique; scene is invalid.";
994 context.mNodeIndices.RegisterMapping(gltfIdx, idx);
996 Index skeletonIdx = node.mSkin ? node.mSkin.GetIndex() : INVALID_INDEX;
999 auto& mesh = *node.mMesh;
1000 uint32_t primitiveCount = mesh.mPrimitives.size();
1001 auto meshIdx = context.mMeshIds[node.mMesh.GetIndex()];
1002 weakNode->mRenderables.reserve(primitiveCount);
1003 for(uint32_t i = 0; i < primitiveCount; ++i)
1005 std::unique_ptr<NodeDefinition::Renderable> renderable;
1006 auto modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
1007 modelRenderable->mMeshIdx = meshIdx + i;
1009 DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
1010 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
1011 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx = skeletonIdx;
1013 renderable.reset(modelRenderable);
1014 weakNode->mRenderables.push_back(std::move(renderable));
1020 CameraParameters camParams;
1021 ConvertCamera(*node.mCamera, camParams);
1023 camParams.matrix.SetTransformComponents(node.mScale, node.mRotation, node.mTranslation);
1024 output.mCameraParameters.push_back(camParams);
1027 for(auto& n : node.mChildren)
1029 ConvertNode(*n, n.GetIndex(), idx, context, isMRendererModel);
1033 void ConvertSceneNodes(const gt::Scene& scene, ConversionContext& context, bool isMRendererModel)
1035 auto& outScene = context.mOutput.mScene;
1036 Index rootIdx = outScene.GetNodeCount();
1037 switch(scene.mNodes.size())
1043 ConvertNode(*scene.mNodes[0], scene.mNodes[0].GetIndex(), INVALID_INDEX, context, isMRendererModel);
1044 outScene.AddRootNode(rootIdx);
1049 std::unique_ptr<NodeDefinition> sceneRoot{new NodeDefinition()};
1050 sceneRoot->mName = "GLTF_LOADER_SCENE_ROOT_" + std::to_string(outScene.GetRoots().size());
1052 outScene.AddNode(std::move(sceneRoot));
1053 outScene.AddRootNode(rootIdx);
1055 for(auto& n : scene.mNodes)
1057 ConvertNode(*n, n.GetIndex(), rootIdx, context, isMRendererModel);
1064 void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel)
1066 if(!doc.mScenes.empty())
1068 uint32_t rootSceneIndex = 0u;
1071 rootSceneIndex = doc.mScene.GetIndex();
1073 ConvertSceneNodes(doc.mScenes[rootSceneIndex], context, isMRendererModel);
1075 for(uint32_t i = 0, i1 = rootSceneIndex; i < i1; ++i)
1077 ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
1080 for(uint32_t i = rootSceneIndex + 1; i < doc.mScenes.size(); ++i)
1082 ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
1087 template<typename T>
1088 void LoadDataFromAccessor(ConversionContext& context, uint32_t bufferIndex, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
1090 if(bufferIndex >= context.mOutput.mResources.mBuffers.size())
1092 DALI_LOG_ERROR("Invailid buffer index\n");
1096 auto& buffer = context.mOutput.mResources.mBuffers[bufferIndex];
1097 if(!buffer.IsAvailable())
1099 DALI_LOG_ERROR("Failed to load from buffer stream.\n");
1101 auto& stream = buffer.GetBufferStream();
1103 stream.seekg(offset, stream.beg);
1104 stream.read(reinterpret_cast<char*>(dataBuffer.Begin()), static_cast<std::streamsize>(static_cast<size_t>(size)));
1107 template<typename T>
1108 float LoadDataFromAccessors(ConversionContext& context, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
1110 inputDataBuffer.Resize(input.mCount);
1111 outputDataBuffer.Resize(output.mCount);
1113 const uint32_t inputDataBufferSize = input.GetBytesLength();
1114 const uint32_t outputDataBufferSize = output.GetBytesLength();
1116 LoadDataFromAccessor<float>(context, output.mBufferView->mBuffer.GetIndex(), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
1117 LoadDataFromAccessor<T>(context, output.mBufferView->mBuffer.GetIndex(), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
1118 ApplyAccessorMinMax(input, reinterpret_cast<float*>(inputDataBuffer.begin()));
1119 ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
1121 return inputDataBuffer[input.mCount - 1u];
1124 template<typename T>
1125 float LoadKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
1127 const gltf2::Accessor& input = *channel.mSampler->mInput;
1128 const gltf2::Accessor& output = *channel.mSampler->mOutput;
1130 Vector<float> inputDataBuffer;
1131 Vector<T> outputDataBuffer;
1133 const float duration = std::max(LoadDataFromAccessors<T>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
1135 // Set first frame value as first keyframe (gltf animation spec)
1136 if(input.mCount > 0 && !Dali::EqualsZero(inputDataBuffer[0]))
1138 keyFrames.Add(0.0f, outputDataBuffer[0]);
1141 for(uint32_t i = 0; i < input.mCount; ++i)
1143 keyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i]);
1149 float LoadBlendShapeKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
1151 const gltf2::Accessor& input = *channel.mSampler->mInput;
1152 const gltf2::Accessor& output = *channel.mSampler->mOutput;
1154 Vector<float> inputDataBuffer;
1155 Vector<float> outputDataBuffer;
1157 const float duration = std::max(LoadDataFromAccessors<float>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
1159 char weightNameBuffer[32];
1160 auto prefixSize = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM);
1161 char* const pWeightName = weightNameBuffer + prefixSize;
1162 const auto remainingSize = sizeof(weightNameBuffer) - prefixSize;
1163 for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex)
1165 AnimatedProperty& animatedProperty = properties[propertyIndex++];
1167 animatedProperty.mNodeIndex = nodeIndex;
1168 snprintf(pWeightName, remainingSize, "%d]", weightIndex);
1169 animatedProperty.mPropertyName = std::string(weightNameBuffer);
1171 animatedProperty.mKeyFrames = KeyFrames::New();
1173 // Set first frame value as first keyframe (gltf animation spec)
1174 if(input.mCount > 0 && !Dali::EqualsZero(inputDataBuffer[0]))
1176 animatedProperty.mKeyFrames.Add(0.0f, outputDataBuffer[weightIndex]);
1179 for(uint32_t i = 0; i < input.mCount; ++i)
1181 animatedProperty.mKeyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i * endWeightIndex + weightIndex]);
1184 animatedProperty.mTimePeriod = {0.f, duration};
1190 void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
1192 auto& output = context.mOutput;
1194 output.mAnimationDefinitions.reserve(output.mAnimationDefinitions.size() + doc.mAnimations.size());
1196 for(const auto& animation : doc.mAnimations)
1198 AnimationDefinition animationDef;
1200 if(!animation.mName.empty())
1202 animationDef.mName = animation.mName;
1205 uint32_t numberOfProperties = 0u;
1206 for(const auto& channel : animation.mChannels)
1208 if(channel.mTarget.mPath == gt::Animation::Channel::Target::WEIGHTS)
1210 numberOfProperties += channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount;
1214 numberOfProperties++;
1217 animationDef.mProperties.resize(numberOfProperties);
1219 Index propertyIndex = 0u;
1220 for(const auto& channel : animation.mChannels)
1222 Index nodeIndex = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
1223 float duration = 0.f;
1225 switch(channel.mTarget.mPath)
1227 case gt::Animation::Channel::Target::TRANSLATION:
1229 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1231 animatedProperty.mNodeIndex = nodeIndex;
1232 animatedProperty.mPropertyName = POSITION_PROPERTY;
1234 animatedProperty.mKeyFrames = KeyFrames::New();
1235 duration = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1237 animatedProperty.mTimePeriod = {0.f, duration};
1240 case gt::Animation::Channel::Target::ROTATION:
1242 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1244 animatedProperty.mNodeIndex = nodeIndex;
1245 animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
1247 animatedProperty.mKeyFrames = KeyFrames::New();
1248 duration = LoadKeyFrames<Quaternion>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1250 animatedProperty.mTimePeriod = {0.f, duration};
1253 case gt::Animation::Channel::Target::SCALE:
1255 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1257 animatedProperty.mNodeIndex = nodeIndex;
1258 animatedProperty.mPropertyName = SCALE_PROPERTY;
1260 animatedProperty.mKeyFrames = KeyFrames::New();
1261 duration = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1263 animatedProperty.mTimePeriod = {0.f, duration};
1266 case gt::Animation::Channel::Target::WEIGHTS:
1268 duration = LoadBlendShapeKeyFrames(context, channel, nodeIndex, propertyIndex, animationDef.mProperties);
1274 // nothing to animate.
1279 animationDef.mDuration = std::max(duration, animationDef.mDuration);
1284 output.mAnimationDefinitions.push_back(std::move(animationDef));
1288 void ProcessSkins(const gt::Document& doc, ConversionContext& context)
1290 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skininversebindmatrices
1291 // If an inverseBindMatrices accessor was provided, we'll load the joint data from the buffer,
1292 // otherwise we'll set identity matrices for inverse bind pose.
1293 struct IInverseBindMatrixProvider
1295 virtual ~IInverseBindMatrixProvider()
1298 virtual void Provide(Matrix& ibm) = 0;
1301 struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
1303 std::istream& mStream;
1304 const uint32_t mElementSizeBytes;
1306 InverseBindMatrixAccessor(const gt::Accessor& accessor, ConversionContext& context)
1307 : mStream(context.mOutput.mResources.mBuffers[accessor.mBufferView->mBuffer.GetIndex()].GetBufferStream()),
1308 mElementSizeBytes(accessor.GetElementSizeBytes())
1310 DALI_ASSERT_DEBUG(accessor.mType == gt::AccessorType::MAT4 && accessor.mComponentType == gt::Component::FLOAT);
1312 if(!mStream.rdbuf()->in_avail())
1314 DALI_LOG_ERROR("Failed to load from stream\n");
1317 mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset, mStream.beg);
1320 virtual void Provide(Matrix& ibm) override
1322 DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), static_cast<std::streamsize>(static_cast<size_t>(mElementSizeBytes))));
1326 struct DefaultInverseBindMatrixProvider : public IInverseBindMatrixProvider
1328 virtual void Provide(Matrix& ibm) override
1330 ibm = Matrix::IDENTITY;
1334 auto& resources = context.mOutput.mResources;
1335 resources.mSkeletons.reserve(doc.mSkins.size());
1337 for(auto& skin : doc.mSkins)
1339 std::unique_ptr<IInverseBindMatrixProvider> ibmProvider;
1340 if(skin.mInverseBindMatrices)
1342 ibmProvider.reset(new InverseBindMatrixAccessor(*skin.mInverseBindMatrices, context));
1346 ibmProvider.reset(new DefaultInverseBindMatrixProvider());
1349 SkeletonDefinition skeleton;
1350 if(skin.mSkeleton.GetIndex() != INVALID_INDEX)
1352 skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(skin.mSkeleton.GetIndex());
1355 skeleton.mJoints.resize(skin.mJoints.size());
1356 auto iJoint = skeleton.mJoints.begin();
1357 for(auto& joint : skin.mJoints)
1359 iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(joint.GetIndex());
1361 ibmProvider->Provide(iJoint->mInverseBindMatrix);
1366 resources.mSkeletons.push_back(std::move(skeleton));
1370 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene)
1372 uint32_t nodeCount = scene.GetNodeCount();
1373 for(uint32_t i = 0; i < nodeCount; ++i)
1375 auto nodeDef = scene.GetNode(i);
1376 for(auto& renderable : nodeDef->mRenderables)
1378 if(shaderFactory.ProduceShader(*renderable) == INVALID_INDEX)
1380 DALI_LOG_ERROR("Fail to produce shader\n");
1386 void SetObjectReaders()
1388 js::SetObjectReader(GetBufferReader());
1389 js::SetObjectReader(GetBufferViewReader());
1390 js::SetObjectReader(GetBufferViewClientReader());
1391 js::SetObjectReader(GetComponentTypedBufferViewClientReader());
1392 js::SetObjectReader(GetAccessorSparseReader());
1393 js::SetObjectReader(GetAccessorReader());
1394 js::SetObjectReader(GetImageReader());
1395 js::SetObjectReader(GetSamplerReader());
1396 js::SetObjectReader(GetTextureReader());
1397 js::SetObjectReader(GetTextureInfoReader());
1398 js::SetObjectReader(GetMaterialPbrReader());
1399 js::SetObjectReader(GetMaterialSpecularReader());
1400 js::SetObjectReader(GetMaterialIorReader());
1401 js::SetObjectReader(GetMaterialExtensionsReader());
1402 js::SetObjectReader(GetMaterialReader());
1403 js::SetObjectReader(GetMeshPrimitiveReader());
1404 js::SetObjectReader(GetMeshReader());
1405 js::SetObjectReader(GetSkinReader());
1406 js::SetObjectReader(GetCameraPerspectiveReader());
1407 js::SetObjectReader(GetCameraOrthographicReader());
1408 js::SetObjectReader(GetCameraReader());
1409 js::SetObjectReader(GetNodeReader());
1410 js::SetObjectReader(GetAnimationSamplerReader());
1411 js::SetObjectReader(GetAnimationChannelTargetReader());
1412 js::SetObjectReader(GetAnimationChannelReader());
1413 js::SetObjectReader(GetAnimationReader());
1414 js::SetObjectReader(GetSceneReader());
1417 void SetDefaultEnvironmentMap(const gt::Document& doc, ConversionContext& context)
1419 EnvironmentDefinition envDef;
1420 envDef.mUseBrdfTexture = true;
1421 envDef.mIblIntensity = Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
1422 context.mOutput.mResources.mEnvironmentMaps.push_back({std::move(envDef), EnvironmentDefinition::Textures()});
1427 void InitializeGltfLoader()
1429 // Set ObjectReader only once (for all gltf loading).
1430 static bool setObjectReadersRequired = true;
1432 Mutex::ScopedLock lock(gInitializeMutex);
1433 if(setObjectReadersRequired)
1435 // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1437 setObjectReadersRequired = false;
1442 void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
1444 bool failed = false;
1445 auto js = LoadTextFile(url.c_str(), &failed);
1448 throw std::runtime_error("Failed to load " + url);
1451 json::unique_ptr root(json_parse(js.c_str(), js.size()));
1454 throw std::runtime_error("Failed to parse " + url);
1459 auto& rootObj = js::Cast<json_object_s>(*root);
1460 auto jsAsset = js::FindObjectChild("asset", rootObj);
1462 auto jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsAsset));
1465 doc.mAsset.mVersion = js::Read::StringView(*jsAssetVersion);
1468 bool isMRendererModel(false);
1469 auto jsAssetGenerator = js::FindObjectChild("generator", js::Cast<json_object_s>(*jsAsset));
1470 if(jsAssetGenerator)
1472 doc.mAsset.mGenerator = js::Read::StringView(*jsAssetGenerator);
1473 isMRendererModel = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
1477 Mutex::ScopedLock lock(gReadMutex);
1478 gt::SetRefReaderObject(doc);
1479 GetDocumentReader().Read(rootObj, doc);
1482 auto path = url.substr(0, url.rfind('/') + 1);
1483 ConversionContext context{params, path, INVALID_INDEX};
1485 ConvertBuffers(doc, context);
1486 ConvertMaterials(doc, context);
1487 ConvertMeshes(doc, context);
1488 ConvertNodes(doc, context, isMRendererModel);
1489 ConvertAnimations(doc, context);
1490 ProcessSkins(doc, context);
1491 ProduceShaders(shaderFactory, params.mScene);
1492 params.mScene.EnsureUniqueSkinningShaderInstances(params.mResources);
1494 // Set Default Environment map
1495 SetDefaultEnvironmentMap(doc, context);
1498 } // namespace Loader
1499 } // namespace Scene3D