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/internal/loader/gltf2-util.h>
22 #include <dali-scene3d/public-api/loader/utils.h>
23 #include <dali/devel-api/scripting/scripting.h>
24 #include <dali/devel-api/threading/mutex.h>
25 #include <dali/integration-api/debug.h>
26 #include <limits> ///< for std::numeric_limits
28 using namespace Dali::Scene3D::Loader;
30 namespace Dali::Scene3D::Loader::Internal
34 static constexpr std::string_view MRENDERER_MODEL_IDENTIFICATION = "M-Renderer";
35 static constexpr std::string_view POSITION_PROPERTY = "position";
36 static constexpr std::string_view ORIENTATION_PROPERTY = "orientation";
37 static constexpr std::string_view SCALE_PROPERTY = "scale";
38 static constexpr std::string_view BLEND_SHAPE_WEIGHTS_UNIFORM = "uBlendShapeWeight";
39 static constexpr std::string_view ROOT_NODE_NAME = "RootNode";
40 static const Vector3 SCALE_TO_ADJUST(100.0f, 100.0f, 100.0f);
42 static const Geometry::Type GLTF2_TO_DALI_PRIMITIVES[]{
48 Geometry::TRIANGLE_STRIP,
49 Geometry::TRIANGLE_FAN}; //...because Dali swaps the last two.
51 static const Dali::Scripting::StringEnum EXTENSION_STRING_TABLE[] =
53 {"NONE", gltf2::ExtensionFlags::NONE},
54 {"KHR_mesh_quantization", gltf2::ExtensionFlags::KHR_MESH_QUANTIZATION},
55 {"KHR_texture_transform", gltf2::ExtensionFlags::KHR_TEXTURE_TRANSFORM},
56 {"KHR_materials_ior", gltf2::ExtensionFlags::KHR_MATERIALS_IOR},
57 {"KHR_materials_specular", gltf2::ExtensionFlags::KHR_MATERIALS_SPECULAR},
59 static const unsigned int EXTENSION_STRING_TABLE_COUNT = sizeof(EXTENSION_STRING_TABLE) / sizeof(EXTENSION_STRING_TABLE[0]);
61 std::vector<gltf2::Animation> ReadAnimationArray(const json_value_s& j)
63 auto results = json::Read::Array<gltf2::Animation, json::ObjectReader<gltf2::Animation>::Read>(j);
65 for(auto& animation : results)
67 for(auto& channel : animation.mChannels)
69 channel.mSampler.UpdateVector(animation.mSamplers);
76 void ApplyAccessorMinMax(const gltf2::Accessor& accessor, float* values)
78 DALI_ASSERT_ALWAYS(accessor.mMax.empty() || gltf2::AccessorType::ElementCount(accessor.mType) == accessor.mMax.size());
79 DALI_ASSERT_ALWAYS(accessor.mMin.empty() || gltf2::AccessorType::ElementCount(accessor.mType) == accessor.mMin.size());
80 MeshDefinition::Blob::ApplyMinMax(accessor.mMin, accessor.mMax, accessor.mCount, values);
83 const json::Reader<gltf2::Buffer>& GetBufferReader()
85 static const auto BUFFER_READER = std::move(json::Reader<gltf2::Buffer>()
86 .Register(*json::MakeProperty("byteLength", json::Read::Number<uint32_t>, &gltf2::Buffer::mByteLength))
87 .Register(*json::MakeProperty("uri", json::Read::StringView, &gltf2::Buffer::mUri)));
91 const json::Reader<gltf2::BufferView>& GetBufferViewReader()
93 static const auto BUFFER_VIEW_READER = std::move(json::Reader<gltf2::BufferView>()
94 .Register(*json::MakeProperty("buffer", gltf2::RefReader<gltf2::Document>::Read<gltf2::Buffer, &gltf2::Document::mBuffers>, &gltf2::BufferView::mBuffer))
95 .Register(*json::MakeProperty("byteOffset", json::Read::Number<uint32_t>, &gltf2::BufferView::mByteOffset))
96 .Register(*json::MakeProperty("byteLength", json::Read::Number<uint32_t>, &gltf2::BufferView::mByteLength))
97 .Register(*json::MakeProperty("byteStride", json::Read::Number<uint32_t>, &gltf2::BufferView::mByteStride))
98 .Register(*json::MakeProperty("target", json::Read::Number<uint32_t>, &gltf2::BufferView::mTarget)));
99 return BUFFER_VIEW_READER;
102 const json::Reader<gltf2::BufferViewClient>& GetBufferViewClientReader()
104 static const auto BUFFER_VIEW_CLIENT_READER = std::move(json::Reader<gltf2::BufferViewClient>()
105 .Register(*json::MakeProperty("bufferView", gltf2::RefReader<gltf2::Document>::Read<gltf2::BufferView, &gltf2::Document::mBufferViews>, &gltf2::BufferViewClient::mBufferView))
106 .Register(*json::MakeProperty("byteOffset", json::Read::Number<uint32_t>, &gltf2::BufferViewClient::mByteOffset)));
107 return BUFFER_VIEW_CLIENT_READER;
110 const json::Reader<gltf2::ComponentTypedBufferViewClient>& GetComponentTypedBufferViewClientReader()
112 static const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(json::Reader<gltf2::ComponentTypedBufferViewClient>()
113 .Register(*new json::Property<gltf2::ComponentTypedBufferViewClient, gltf2::Ref<gltf2::BufferView>>("bufferView", gltf2::RefReader<gltf2::Document>::Read<gltf2::BufferView, &gltf2::Document::mBufferViews>, &gltf2::ComponentTypedBufferViewClient::mBufferView))
114 .Register(*new json::Property<gltf2::ComponentTypedBufferViewClient, uint32_t>("byteOffset", json::Read::Number<uint32_t>, &gltf2::ComponentTypedBufferViewClient::mByteOffset))
115 .Register(*json::MakeProperty("componentType", json::Read::Enum<gltf2::Component::Type>, &gltf2::ComponentTypedBufferViewClient::mComponentType)));
116 return COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER;
119 const json::Reader<gltf2::Accessor::Sparse>& GetAccessorSparseReader()
121 static const auto ACCESSOR_SPARSE_READER = std::move(json::Reader<gltf2::Accessor::Sparse>()
122 .Register(*json::MakeProperty("count", json::Read::Number<uint32_t>, &gltf2::Accessor::Sparse::mCount))
123 .Register(*json::MakeProperty("indices", json::ObjectReader<gltf2::ComponentTypedBufferViewClient>::Read, &gltf2::Accessor::Sparse::mIndices))
124 .Register(*json::MakeProperty("values", json::ObjectReader<gltf2::BufferViewClient>::Read, &gltf2::Accessor::Sparse::mValues)));
125 return ACCESSOR_SPARSE_READER;
128 const json::Reader<gltf2::Accessor>& GetAccessorReader()
130 static const auto ACCESSOR_READER = std::move(json::Reader<gltf2::Accessor>()
131 .Register(*new json::Property<gltf2::Accessor, gltf2::Ref<gltf2::BufferView>>("bufferView",
132 gltf2::RefReader<gltf2::Document>::Read<gltf2::BufferView, &gltf2::Document::mBufferViews>,
133 &gltf2::Accessor::mBufferView))
134 .Register(*new json::Property<gltf2::Accessor, uint32_t>("byteOffset", json::Read::Number<uint32_t>, &gltf2::Accessor::mByteOffset))
135 .Register(*new json::Property<gltf2::Accessor, gltf2::Component::Type>("componentType",
136 json::Read::Enum<gltf2::Component::Type>,
137 &gltf2::Accessor::mComponentType))
138 .Register(*new json::Property<gltf2::Accessor, std::string_view>("name", json::Read::StringView, &gltf2::Accessor::mName))
139 .Register(*json::MakeProperty("count", json::Read::Number<uint32_t>, &gltf2::Accessor::mCount))
140 .Register(*json::MakeProperty("normalized", json::Read::Boolean, &gltf2::Accessor::mNormalized))
141 .Register(*json::MakeProperty("type", gltf2::ReadStringEnum<gltf2::AccessorType>, &gltf2::Accessor::mType))
142 .Register(*json::MakeProperty("min", json::Read::Array<float, json::Read::Number>, &gltf2::Accessor::mMin))
143 .Register(*json::MakeProperty("max", json::Read::Array<float, json::Read::Number>, &gltf2::Accessor::mMax))
144 .Register(*new json::Property<gltf2::Accessor, gltf2::Accessor::Sparse>("sparse", json::ObjectReader<gltf2::Accessor::Sparse>::Read, &gltf2::Accessor::SetSparse)));
145 return ACCESSOR_READER;
148 const json::Reader<gltf2::Image>& GetImageReader()
150 static const auto IMAGE_READER = std::move(json::Reader<gltf2::Image>()
151 .Register(*new json::Property<gltf2::Image, std::string_view>("name", json::Read::StringView, &gltf2::Material::mName))
152 .Register(*json::MakeProperty("uri", json::Read::StringView, &gltf2::Image::mUri))
153 .Register(*json::MakeProperty("mimeType", json::Read::StringView, &gltf2::Image::mMimeType))
154 .Register(*json::MakeProperty("bufferView", gltf2::RefReader<gltf2::Document>::Read<gltf2::BufferView, &gltf2::Document::mBufferViews>, &gltf2::Image::mBufferView)));
158 const json::Reader<gltf2::Sampler>& GetSamplerReader()
160 static const auto SAMPLER_READER = std::move(json::Reader<gltf2::Sampler>()
161 .Register(*json::MakeProperty("minFilter", json::Read::Enum<gltf2::Filter::Type>, &gltf2::Sampler::mMinFilter))
162 .Register(*json::MakeProperty("magFilter", json::Read::Enum<gltf2::Filter::Type>, &gltf2::Sampler::mMagFilter))
163 .Register(*json::MakeProperty("wrapS", json::Read::Enum<gltf2::Wrap::Type>, &gltf2::Sampler::mWrapS))
164 .Register(*json::MakeProperty("wrapT", json::Read::Enum<gltf2::Wrap::Type>, &gltf2::Sampler::mWrapT)));
165 return SAMPLER_READER;
168 const json::Reader<gltf2::Texture>& GetTextureReader()
170 static const auto TEXURE_READER = std::move(json::Reader<gltf2::Texture>()
171 .Register(*json::MakeProperty("source", gltf2::RefReader<gltf2::Document>::Read<gltf2::Image, &gltf2::Document::mImages>, &gltf2::Texture::mSource))
172 .Register(*json::MakeProperty("sampler", gltf2::RefReader<gltf2::Document>::Read<gltf2::Sampler, &gltf2::Document::mSamplers>, &gltf2::Texture::mSampler)));
173 return TEXURE_READER;
176 const json::Reader<gltf2::TextureInfo>& GetTextureInfoReader()
178 static const auto TEXURE_INFO_READER = std::move(json::Reader<gltf2::TextureInfo>()
179 .Register(*json::MakeProperty("index", gltf2::RefReader<gltf2::Document>::Read<gltf2::Texture, &gltf2::Document::mTextures>, &gltf2::TextureInfo::mTexture))
180 .Register(*json::MakeProperty("texCoord", json::Read::Number<uint32_t>, &gltf2::TextureInfo::mTexCoord))
181 .Register(*json::MakeProperty("scale", json::Read::Number<float>, &gltf2::TextureInfo::mScale))
182 .Register(*json::MakeProperty("strength", json::Read::Number<float>, &gltf2::TextureInfo::mStrength)));
183 return TEXURE_INFO_READER;
186 const json::Reader<gltf2::Material::Pbr>& GetMaterialPbrReader()
188 static const auto MATERIAL_PBR_READER = std::move(json::Reader<gltf2::Material::Pbr>()
189 .Register(*json::MakeProperty("baseColorFactor", gltf2::ReadDaliVector<Vector4>, &gltf2::Material::Pbr::mBaseColorFactor))
190 .Register(*json::MakeProperty("baseColorTexture", json::ObjectReader<gltf2::TextureInfo>::Read, &gltf2::Material::Pbr::mBaseColorTexture))
191 .Register(*json::MakeProperty("metallicFactor", json::Read::Number<float>, &gltf2::Material::Pbr::mMetallicFactor))
192 .Register(*json::MakeProperty("roughnessFactor", json::Read::Number<float>, &gltf2::Material::Pbr::mRoughnessFactor))
193 .Register(*json::MakeProperty("metallicRoughnessTexture", json::ObjectReader<gltf2::TextureInfo>::Read, &gltf2::Material::Pbr::mMetallicRoughnessTexture)));
194 return MATERIAL_PBR_READER;
197 const json::Reader<gltf2::MaterialSpecular>& GetMaterialSpecularReader()
199 static const auto MATERIAL_SPECULAR_READER = std::move(json::Reader<gltf2::MaterialSpecular>()
200 .Register(*json::MakeProperty("specularFactor", json::Read::Number<float>, &gltf2::MaterialSpecular::mSpecularFactor))
201 .Register(*json::MakeProperty("specularTexture", json::ObjectReader<gltf2::TextureInfo>::Read, &gltf2::MaterialSpecular::mSpecularTexture))
202 .Register(*json::MakeProperty("specularColorFactor", gltf2::ReadDaliVector<Vector3>, &gltf2::MaterialSpecular::mSpecularColorFactor))
203 .Register(*json::MakeProperty("specularColorTexture", json::ObjectReader<gltf2::TextureInfo>::Read, &gltf2::MaterialSpecular::mSpecularColorTexture)));
204 return MATERIAL_SPECULAR_READER;
207 const json::Reader<gltf2::MaterialIor>& GetMaterialIorReader()
209 static const auto MATERIAL_IOR_READER = std::move(json::Reader<gltf2::MaterialIor>()
210 .Register(*json::MakeProperty("ior", json::Read::Number<float>, &gltf2::MaterialIor::mIor)));
211 return MATERIAL_IOR_READER;
214 const json::Reader<gltf2::MaterialExtensions>& GetMaterialExtensionsReader()
216 static const auto MATERIAL_EXTENSION_READER = std::move(json::Reader<gltf2::MaterialExtensions>()
217 .Register(*json::MakeProperty("KHR_materials_ior", json::ObjectReader<gltf2::MaterialIor>::Read, &gltf2::MaterialExtensions::mMaterialIor))
218 .Register(*json::MakeProperty("KHR_materials_specular", json::ObjectReader<gltf2::MaterialSpecular>::Read, &gltf2::MaterialExtensions::mMaterialSpecular)));
219 return MATERIAL_EXTENSION_READER;
222 const json::Reader<gltf2::Material>& GetMaterialReader()
224 static const auto MATERIAL_READER = std::move(json::Reader<gltf2::Material>()
225 .Register(*new json::Property<gltf2::Material, std::string_view>("name", json::Read::StringView, &gltf2::Material::mName))
226 .Register(*json::MakeProperty("pbrMetallicRoughness", json::ObjectReader<gltf2::Material::Pbr>::Read, &gltf2::Material::mPbrMetallicRoughness))
227 .Register(*json::MakeProperty("normalTexture", json::ObjectReader<gltf2::TextureInfo>::Read, &gltf2::Material::mNormalTexture))
228 .Register(*json::MakeProperty("occlusionTexture", json::ObjectReader<gltf2::TextureInfo>::Read, &gltf2::Material::mOcclusionTexture))
229 .Register(*json::MakeProperty("emissiveTexture", json::ObjectReader<gltf2::TextureInfo>::Read, &gltf2::Material::mEmissiveTexture))
230 .Register(*json::MakeProperty("emissiveFactor", gltf2::ReadDaliVector<Vector3>, &gltf2::Material::mEmissiveFactor))
231 .Register(*json::MakeProperty("alphaMode", gltf2::ReadStringEnum<gltf2::AlphaMode>, &gltf2::Material::mAlphaMode))
232 .Register(*json::MakeProperty("alphaCutoff", json::Read::Number<float>, &gltf2::Material::mAlphaCutoff))
233 .Register(*json::MakeProperty("doubleSided", json::Read::Boolean, &gltf2::Material::mDoubleSided))
234 .Register(*json::MakeProperty("extensions", json::ObjectReader<gltf2::MaterialExtensions>::Read, &gltf2::Material::mMaterialExtensions)));
235 return MATERIAL_READER;
238 std::map<gltf2::Attribute::HashType, gltf2::Ref<gltf2::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
240 auto& jsonObject = json::Cast<json_object_s>(j);
241 std::map<gltf2::Attribute::HashType, gltf2::Ref<gltf2::Accessor>> result;
243 auto element = jsonObject.start;
246 auto jsonString = *element->name;
248 gltf2::Attribute::HashType hash = gltf2::Attribute::HashFromString(jsonString.string, jsonString.string_size);
251 gltf2::RefReader<gltf2::Document>::Read<gltf2::Accessor, &gltf2::Document::mAccessors>(*element->value);
252 element = element->next;
257 std::map<gltf2::Attribute::Type, gltf2::Ref<gltf2::Accessor>> ReadMeshPrimitiveAttributes2(const json_value_s& j)
259 auto& jsonObject = json::Cast<json_object_s>(j);
260 std::map<gltf2::Attribute::Type, gltf2::Ref<gltf2::Accessor>> result;
262 auto element = jsonObject.start;
265 auto jsonString = *element->name;
267 result[gltf2::Attribute::TargetFromString(jsonString.string, jsonString.string_size)] = gltf2::RefReader<gltf2::Document>::Read<gltf2::Accessor, &gltf2::Document::mAccessors>(*element->value);
269 element = element->next;
274 std::vector<std::map<gltf2::Attribute::Type, gltf2::Ref<gltf2::Accessor>>> ReadMeshPrimitiveTargets(const json_value_s& j)
276 auto& jsonObject = json::Cast<json_array_s>(j);
277 std::vector<std::map<gltf2::Attribute::Type, gltf2::Ref<gltf2::Accessor>>> result;
279 result.reserve(jsonObject.length);
281 auto element = jsonObject.start;
284 result.push_back(std::move(ReadMeshPrimitiveAttributes2(*element->value)));
285 element = element->next;
291 const json::Reader<gltf2::Mesh::Primitive>& GetMeshPrimitiveReader()
293 static const auto MESH_PRIMITIVE_READER = std::move(json::Reader<gltf2::Mesh::Primitive>()
294 .Register(*json::MakeProperty("attributes", ReadMeshPrimitiveAttributes, &gltf2::Mesh::Primitive::mAttributes))
295 .Register(*json::MakeProperty("indices", gltf2::RefReader<gltf2::Document>::Read<gltf2::Accessor, &gltf2::Document::mAccessors>, &gltf2::Mesh::Primitive::mIndices))
296 .Register(*json::MakeProperty("material", gltf2::RefReader<gltf2::Document>::Read<gltf2::Material, &gltf2::Document::mMaterials>, &gltf2::Mesh::Primitive::mMaterial))
297 .Register(*json::MakeProperty("mode", json::Read::Enum<gltf2::Mesh::Primitive::Mode>, &gltf2::Mesh::Primitive::mMode))
298 .Register(*json::MakeProperty("targets", ReadMeshPrimitiveTargets, &gltf2::Mesh::Primitive::mTargets)));
299 return MESH_PRIMITIVE_READER;
302 const json::Reader<gltf2::Mesh::Extras>& GetMeshExtrasReader()
304 static const auto MESH_EXTRAS_READER = std::move(json::Reader<gltf2::Mesh::Extras>()
305 .Register(*json::MakeProperty("targetNames", json::Read::Array<std::string_view, json::Read::StringView>, &gltf2::Mesh::Extras::mTargetNames)));
306 return MESH_EXTRAS_READER;
309 std::vector<std::string_view> ReadMeshExtensionsTargetsName(const json_value_s& j)
311 auto& jsonObject = json::Cast<json_object_s>(j);
312 std::vector<std::string_view> result;
314 auto element = jsonObject.start;
317 auto jsonString = *element->name;
318 uint32_t index = json::Read::Number<uint32_t>(*element->value);
320 if(result.size() <= index)
322 DALI_ASSERT_ALWAYS(index < std::numeric_limits<uint32_t>::max());
323 result.resize(index + 1u);
326 result[index] = json::Read::StringView(jsonString);
328 element = element->next;
333 const json::Reader<gltf2::Mesh::Extensions>& GetMeshExtensionsReader()
335 static const auto MESH_EXTENSIONS_READER = std::move(json::Reader<gltf2::Mesh::Extensions>()
336 .Register(*json::MakeProperty("SXR_targets_names", ReadMeshExtensionsTargetsName, &gltf2::Mesh::Extensions::mSXRTargetsNames))
337 .Register(*json::MakeProperty("avatar_shape_names", ReadMeshExtensionsTargetsName, &gltf2::Mesh::Extensions::mAvatarShapeNames)));
338 return MESH_EXTENSIONS_READER;
341 const json::Reader<gltf2::Mesh>& GetMeshReader()
343 static const auto MESH_READER = std::move(json::Reader<gltf2::Mesh>()
344 .Register(*new json::Property<gltf2::Mesh, std::string_view>("name", json::Read::StringView, &gltf2::Mesh::mName))
345 .Register(*json::MakeProperty("primitives",
346 json::Read::Array<gltf2::Mesh::Primitive, json::ObjectReader<gltf2::Mesh::Primitive>::Read>,
347 &gltf2::Mesh::mPrimitives))
348 .Register(*json::MakeProperty("weights", json::Read::Array<float, json::Read::Number>, &gltf2::Mesh::mWeights))
349 .Register(*json::MakeProperty("extras", json::ObjectReader<gltf2::Mesh::Extras>::Read, &gltf2::Mesh::mExtras))
350 .Register(*json::MakeProperty("extensions", json::ObjectReader<gltf2::Mesh::Extensions>::Read, &gltf2::Mesh::mExtensions)));
354 const json::Reader<gltf2::Skin>& GetSkinReader()
356 static const auto SKIN_READER = std::move(json::Reader<gltf2::Skin>()
357 .Register(*new json::Property<gltf2::Skin, std::string_view>("name", json::Read::StringView, &gltf2::Skin::mName))
358 .Register(*json::MakeProperty("inverseBindMatrices",
359 gltf2::RefReader<gltf2::Document>::Read<gltf2::Accessor, &gltf2::Document::mAccessors>,
360 &gltf2::Skin::mInverseBindMatrices))
361 .Register(*json::MakeProperty("skeleton",
362 gltf2::RefReader<gltf2::Document>::Read<gltf2::Node, &gltf2::Document::mNodes>,
363 &gltf2::Skin::mSkeleton))
364 .Register(*json::MakeProperty("joints",
365 json::Read::Array<gltf2::Ref<gltf2::Node>, gltf2::RefReader<gltf2::Document>::Read<gltf2::Node, &gltf2::Document::mNodes>>,
366 &gltf2::Skin::mJoints)));
370 const json::Reader<gltf2::Camera::Perspective>& GetCameraPerspectiveReader()
372 static const auto CAMERA_PERSPECTIVE_READER = std::move(json::Reader<gltf2::Camera::Perspective>()
373 .Register(*json::MakeProperty("aspectRatio", json::Read::Number<float>, &gltf2::Camera::Perspective::mAspectRatio))
374 .Register(*json::MakeProperty("yfov", json::Read::Number<float>, &gltf2::Camera::Perspective::mYFov))
375 .Register(*json::MakeProperty("zfar", json::Read::Number<float>, &gltf2::Camera::Perspective::mZFar))
376 .Register(*json::MakeProperty("znear", json::Read::Number<float>, &gltf2::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted
377 return CAMERA_PERSPECTIVE_READER;
380 const json::Reader<gltf2::Camera::Orthographic>& GetCameraOrthographicReader()
382 static const auto CAMERA_ORTHOGRAPHIC_READER = std::move(json::Reader<gltf2::Camera::Orthographic>()
383 .Register(*json::MakeProperty("xmag", json::Read::Number<float>, &gltf2::Camera::Orthographic::mXMag))
384 .Register(*json::MakeProperty("ymag", json::Read::Number<float>, &gltf2::Camera::Orthographic::mYMag))
385 .Register(*json::MakeProperty("zfar", json::Read::Number<float>, &gltf2::Camera::Orthographic::mZFar))
386 .Register(*json::MakeProperty("znear", json::Read::Number<float>, &gltf2::Camera::Orthographic::mZNear)));
387 return CAMERA_ORTHOGRAPHIC_READER;
390 const json::Reader<gltf2::Camera>& GetCameraReader()
392 static const auto CAMERA_READER = std::move(json::Reader<gltf2::Camera>()
393 .Register(*new json::Property<gltf2::Camera, std::string_view>("name", json::Read::StringView, &gltf2::Camera::mName))
394 .Register(*json::MakeProperty("type", json::Read::StringView, &gltf2::Camera::mType))
395 .Register(*json::MakeProperty("perspective", json::ObjectReader<gltf2::Camera::Perspective>::Read, &gltf2::Camera::mPerspective))
396 .Register(*json::MakeProperty("orthographic", json::ObjectReader<gltf2::Camera::Orthographic>::Read, &gltf2::Camera::mOrthographic)));
397 return CAMERA_READER;
400 const json::Reader<gltf2::Node>& GetNodeReader()
402 static const auto NODE_READER = std::move(json::Reader<gltf2::Node>()
403 .Register(*new json::Property<gltf2::Node, std::string_view>("name", json::Read::StringView, &gltf2::Node::mName))
404 .Register(*json::MakeProperty("translation", gltf2::ReadDaliVector<Vector3>, &gltf2::Node::mTranslation))
405 .Register(*json::MakeProperty("rotation", gltf2::ReadQuaternion, &gltf2::Node::mRotation))
406 .Register(*json::MakeProperty("scale", gltf2::ReadDaliVector<Vector3>, &gltf2::Node::mScale))
407 .Register(*new json::Property<gltf2::Node, Matrix>("matrix", gltf2::ReadDaliVector<Matrix>, &gltf2::Node::SetMatrix))
408 .Register(*json::MakeProperty("camera", gltf2::RefReader<gltf2::Document>::Read<gltf2::Camera, &gltf2::Document::mCameras>, &gltf2::Node::mCamera))
409 .Register(*json::MakeProperty("children", json::Read::Array<gltf2::Ref<gltf2::Node>, gltf2::RefReader<gltf2::Document>::Read<gltf2::Node, &gltf2::Document::mNodes>>, &gltf2::Node::mChildren))
410 .Register(*json::MakeProperty("mesh", gltf2::RefReader<gltf2::Document>::Read<gltf2::Mesh, &gltf2::Document::mMeshes>, &gltf2::Node::mMesh))
411 .Register(*json::MakeProperty("skin", gltf2::RefReader<gltf2::Document>::Read<gltf2::Skin, &gltf2::Document::mSkins>, &gltf2::Node::mSkin)));
415 const json::Reader<gltf2::Animation::Sampler>& GetAnimationSamplerReader()
417 static const auto ANIMATION_SAMPLER_READER = std::move(json::Reader<gltf2::Animation::Sampler>()
418 .Register(*json::MakeProperty("input", gltf2::RefReader<gltf2::Document>::Read<gltf2::Accessor, &gltf2::Document::mAccessors>, &gltf2::Animation::Sampler::mInput))
419 .Register(*json::MakeProperty("output", gltf2::RefReader<gltf2::Document>::Read<gltf2::Accessor, &gltf2::Document::mAccessors>, &gltf2::Animation::Sampler::mOutput))
420 .Register(*json::MakeProperty("interpolation", gltf2::ReadStringEnum<gltf2::Animation::Sampler::Interpolation>, &gltf2::Animation::Sampler::mInterpolation)));
421 return ANIMATION_SAMPLER_READER;
424 const json::Reader<gltf2::Animation::Channel::Target>& GetAnimationChannelTargetReader()
426 static const auto ANIMATION_TARGET_READER = std::move(json::Reader<gltf2::Animation::Channel::Target>()
427 .Register(*json::MakeProperty("node", gltf2::RefReader<gltf2::Document>::Read<gltf2::Node, &gltf2::Document::mNodes>, &gltf2::Animation::Channel::Target::mNode))
428 .Register(*json::MakeProperty("path", gltf2::ReadStringEnum<gltf2::Animation::Channel::Target>, &gltf2::Animation::Channel::Target::mPath)));
429 return ANIMATION_TARGET_READER;
432 const json::Reader<gltf2::Animation::Channel>& GetAnimationChannelReader()
434 static const auto ANIMATION_CHANNEL_READER = std::move(json::Reader<gltf2::Animation::Channel>()
435 .Register(*json::MakeProperty("target", json::ObjectReader<gltf2::Animation::Channel::Target>::Read, &gltf2::Animation::Channel::mTarget))
436 .Register(*json::MakeProperty("sampler", gltf2::RefReader<gltf2::Animation>::Read<gltf2::Animation::Sampler, &gltf2::Animation::mSamplers>, &gltf2::Animation::Channel::mSampler)));
437 return ANIMATION_CHANNEL_READER;
440 const json::Reader<gltf2::Animation>& GetAnimationReader()
442 static const auto ANIMATION_READER = std::move(json::Reader<gltf2::Animation>()
443 .Register(*new json::Property<gltf2::Animation, std::string_view>("name", json::Read::StringView, &gltf2::Animation::mName))
444 .Register(*json::MakeProperty("samplers",
445 json::Read::Array<gltf2::Animation::Sampler, json::ObjectReader<gltf2::Animation::Sampler>::Read>,
446 &gltf2::Animation::mSamplers))
447 .Register(*json::MakeProperty("channels",
448 json::Read::Array<gltf2::Animation::Channel, json::ObjectReader<gltf2::Animation::Channel>::Read>,
449 &gltf2::Animation::mChannels)));
450 return ANIMATION_READER;
453 const json::Reader<gltf2::Scene>& GetSceneReader()
455 static const auto SCENE_READER = std::move(json::Reader<gltf2::Scene>()
456 .Register(*new json::Property<gltf2::Scene, std::string_view>("name", json::Read::StringView, &gltf2::Scene::mName))
457 .Register(*json::MakeProperty("nodes",
458 json::Read::Array<gltf2::Ref<gltf2::Node>, gltf2::RefReader<gltf2::Document>::Read<gltf2::Node, &gltf2::Document::mNodes>>,
459 &gltf2::Scene::mNodes)));
463 const json::Reader<gltf2::Document>& GetDocumentReader()
465 static const auto DOCUMENT_READER = std::move(json::Reader<gltf2::Document>()
466 .Register(*json::MakeProperty("buffers",
467 json::Read::Array<gltf2::Buffer, json::ObjectReader<gltf2::Buffer>::Read>,
468 &gltf2::Document::mBuffers))
469 .Register(*json::MakeProperty("bufferViews",
470 json::Read::Array<gltf2::BufferView, json::ObjectReader<gltf2::BufferView>::Read>,
471 &gltf2::Document::mBufferViews))
472 .Register(*json::MakeProperty("accessors",
473 json::Read::Array<gltf2::Accessor, json::ObjectReader<gltf2::Accessor>::Read>,
474 &gltf2::Document::mAccessors))
475 .Register(*json::MakeProperty("images",
476 json::Read::Array<gltf2::Image, json::ObjectReader<gltf2::Image>::Read>,
477 &gltf2::Document::mImages))
478 .Register(*json::MakeProperty("samplers",
479 json::Read::Array<gltf2::Sampler, json::ObjectReader<gltf2::Sampler>::Read>,
480 &gltf2::Document::mSamplers))
481 .Register(*json::MakeProperty("textures",
482 json::Read::Array<gltf2::Texture, json::ObjectReader<gltf2::Texture>::Read>,
483 &gltf2::Document::mTextures))
484 .Register(*json::MakeProperty("materials",
485 json::Read::Array<gltf2::Material, json::ObjectReader<gltf2::Material>::Read>,
486 &gltf2::Document::mMaterials))
487 .Register(*json::MakeProperty("meshes",
488 json::Read::Array<gltf2::Mesh, json::ObjectReader<gltf2::Mesh>::Read>,
489 &gltf2::Document::mMeshes))
490 .Register(*json::MakeProperty("skins",
491 json::Read::Array<gltf2::Skin, json::ObjectReader<gltf2::Skin>::Read>,
492 &gltf2::Document::mSkins))
493 .Register(*json::MakeProperty("cameras",
494 json::Read::Array<gltf2::Camera, json::ObjectReader<gltf2::Camera>::Read>,
495 &gltf2::Document::mCameras))
496 .Register(*json::MakeProperty("nodes",
497 json::Read::Array<gltf2::Node, json::ObjectReader<gltf2::Node>::Read>,
498 &gltf2::Document::mNodes))
499 .Register(*json::MakeProperty("animations",
501 &gltf2::Document::mAnimations))
502 .Register(*json::MakeProperty("extensionsRequired",
503 json::Read::Array<std::string_view, json::Read::StringView>,
504 &gltf2::Document::mExtensionsRequired))
505 .Register(*json::MakeProperty("extensionsUsed",
506 json::Read::Array<std::string_view, json::Read::StringView>,
507 &gltf2::Document::mExtensionsUsed))
508 .Register(*json::MakeProperty("scenes",
509 json::Read::Array<gltf2::Scene, json::ObjectReader<gltf2::Scene>::Read>,
510 &gltf2::Document::mScenes))
511 .Register(*json::MakeProperty("scene", gltf2::RefReader<gltf2::Document>::Read<gltf2::Scene, &gltf2::Document::mScenes>, &gltf2::Document::mScene)));
512 return DOCUMENT_READER;
515 void ConvertBuffer(const gltf2::Buffer& buffer, decltype(ResourceBundle::mBuffers)& outBuffers, const std::string& resourcePath)
517 BufferDefinition bufferDefinition;
519 bufferDefinition.mResourcePath = resourcePath;
520 bufferDefinition.mUri = buffer.mUri;
521 bufferDefinition.mByteLength = buffer.mByteLength;
523 outBuffers.emplace_back(std::move(bufferDefinition));
526 void ConvertBuffers(const gltf2::Document& document, ConversionContext& context)
528 auto& outBuffers = context.mOutput.mResources.mBuffers;
529 outBuffers.reserve(document.mBuffers.size());
531 for(auto& buffer : document.mBuffers)
533 if(buffer.mUri.empty())
537 ConvertBuffer(buffer, outBuffers, context.mPath);
541 SamplerFlags::Type ConvertWrapMode(gltf2::Wrap::Type wrapMode)
545 case gltf2::Wrap::REPEAT:
546 return SamplerFlags::WRAP_REPEAT;
547 case gltf2::Wrap::CLAMP_TO_EDGE:
548 return SamplerFlags::WRAP_CLAMP;
549 case gltf2::Wrap::MIRRORED_REPEAT:
550 return SamplerFlags::WRAP_MIRROR;
552 throw std::runtime_error("Invalid wrap type.");
556 SamplerFlags::Type ConvertSampler(const gltf2::Ref<gltf2::Sampler>& sampler)
560 return ((sampler->mMinFilter < gltf2::Filter::NEAREST_MIPMAP_NEAREST) ? (sampler->mMinFilter - gltf2::Filter::NEAREST) : ((sampler->mMinFilter - gltf2::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
561 ((sampler->mMagFilter - gltf2::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
562 (ConvertWrapMode(sampler->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
563 (ConvertWrapMode(sampler->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
567 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texturesampler
568 // "The index of the sampler used by this texture. When undefined, a sampler with repeat wrapping and auto filtering should be used."
569 // "What is an auto filtering", I hear you ask. Since there's nothing else to determine mipmapping from - including glTF image
570 // properties, if not in some extension -, we will simply assume linear filtering.
571 return SamplerFlags::FILTER_LINEAR | (SamplerFlags::FILTER_LINEAR << SamplerFlags::FILTER_MAG_SHIFT) |
572 (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_S_SHIFT) | (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_T_SHIFT);
576 TextureDefinition ConvertTextureInfo(const gltf2::TextureInfo& textureInfo, ConversionContext& context, const ImageMetadata& metaData = ImageMetadata())
578 TextureDefinition textureDefinition;
579 std::string uri = std::string(textureInfo.mTexture->mSource->mUri);
582 uint32_t bufferIndex = textureInfo.mTexture->mSource->mBufferView->mBuffer.GetIndex();
583 if(bufferIndex != INVALID_INDEX && context.mOutput.mResources.mBuffers[bufferIndex].IsAvailable())
585 auto& stream = context.mOutput.mResources.mBuffers[bufferIndex].GetBufferStream();
587 stream.seekg(textureInfo.mTexture->mSource->mBufferView->mByteOffset, stream.beg);
588 std::vector<uint8_t> dataBuffer;
589 dataBuffer.resize(textureInfo.mTexture->mSource->mBufferView->mByteLength);
590 stream.read(reinterpret_cast<char*>(dataBuffer.data()), static_cast<std::streamsize>(static_cast<size_t>(textureInfo.mTexture->mSource->mBufferView->mByteLength)));
591 return TextureDefinition{std::move(dataBuffer), ConvertSampler(textureInfo.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
593 return TextureDefinition();
597 return TextureDefinition{uri, ConvertSampler(textureInfo.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
601 void AddTextureStage(uint32_t semantic, MaterialDefinition& materialDefinition, gltf2::TextureInfo textureInfo, const Dali::Scene3D::Loader::ImageMetadata& metaData, ConversionContext& context)
603 materialDefinition.mTextureStages.push_back({semantic, ConvertTextureInfo(textureInfo, context, metaData)});
604 materialDefinition.mFlags |= semantic;
607 void ConvertMaterial(const gltf2::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials, ConversionContext& context)
609 auto getTextureMetaData = [](const std::unordered_map<std::string, ImageMetadata>& metaData, const gltf2::TextureInfo& info) {
610 if(!info.mTexture->mSource->mUri.empty())
612 if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end())
614 return search->second;
617 return ImageMetadata();
620 MaterialDefinition materialDefinition;
622 materialDefinition.mFlags |= MaterialDefinition::GLTF_CHANNELS;
623 materialDefinition.mShadowAvailable = true;
625 auto& pbr = material.mPbrMetallicRoughness;
626 if(material.mAlphaMode == gltf2::AlphaMode::BLEND)
628 materialDefinition.mAlphaModeType = Scene3D::Material::AlphaModeType::BLEND;
629 materialDefinition.mIsOpaque = false;
630 materialDefinition.mFlags |= MaterialDefinition::TRANSPARENCY;
632 else if(material.mAlphaMode == gltf2::AlphaMode::MASK)
634 materialDefinition.mAlphaModeType = Scene3D::Material::AlphaModeType::MASK;
635 materialDefinition.mIsMask = true;
636 materialDefinition.SetAlphaCutoff(std::min(1.f, std::max(0.f, material.mAlphaCutoff)));
639 materialDefinition.mBaseColorFactor = pbr.mBaseColorFactor;
641 materialDefinition.mTextureStages.reserve(!!pbr.mBaseColorTexture + !!pbr.mMetallicRoughnessTexture + !!material.mNormalTexture + !!material.mOcclusionTexture + !!material.mEmissiveTexture);
642 if(pbr.mBaseColorTexture)
644 AddTextureStage(MaterialDefinition::ALBEDO, materialDefinition, pbr.mBaseColorTexture, getTextureMetaData(imageMetaData, pbr.mBaseColorTexture), context);
648 materialDefinition.mNeedAlbedoTexture = false;
651 materialDefinition.mMetallic = pbr.mMetallicFactor;
652 materialDefinition.mRoughness = pbr.mRoughnessFactor;
654 if(pbr.mMetallicRoughnessTexture)
656 AddTextureStage(MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS,
658 pbr.mMetallicRoughnessTexture,
659 getTextureMetaData(imageMetaData, pbr.mMetallicRoughnessTexture),
664 materialDefinition.mNeedMetallicRoughnessTexture = false;
667 materialDefinition.mNormalScale = material.mNormalTexture.mScale;
668 if(material.mNormalTexture)
670 AddTextureStage(MaterialDefinition::NORMAL, materialDefinition, material.mNormalTexture, getTextureMetaData(imageMetaData, material.mNormalTexture), context);
674 materialDefinition.mNeedNormalTexture = false;
677 if(material.mOcclusionTexture)
679 AddTextureStage(MaterialDefinition::OCCLUSION, materialDefinition, material.mOcclusionTexture, getTextureMetaData(imageMetaData, material.mOcclusionTexture), context);
680 materialDefinition.mOcclusionStrength = material.mOcclusionTexture.mStrength;
683 materialDefinition.mEmissiveFactor = material.mEmissiveFactor;
684 if(material.mEmissiveTexture)
686 AddTextureStage(MaterialDefinition::EMISSIVE, materialDefinition, material.mEmissiveTexture, getTextureMetaData(imageMetaData, material.mEmissiveTexture), context);
689 if(!Dali::Equals(material.mMaterialExtensions.mMaterialIor.mIor, gltf2::UNDEFINED_FLOAT_VALUE))
691 materialDefinition.mIor = material.mMaterialExtensions.mMaterialIor.mIor;
692 materialDefinition.mDielectricSpecular = powf((materialDefinition.mIor - 1.0f) / (materialDefinition.mIor + 1.0f), 2.0f);
694 materialDefinition.mSpecularFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularFactor;
695 materialDefinition.mSpecularColorFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor;
697 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)
699 AddTextureStage(MaterialDefinition::SPECULAR, materialDefinition, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture), context);
702 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)
704 AddTextureStage(MaterialDefinition::SPECULAR_COLOR, materialDefinition, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture), context);
707 materialDefinition.mDoubleSided = material.mDoubleSided;
709 outMaterials.emplace_back(std::move(materialDefinition), TextureSet());
712 void ConvertMaterials(const gltf2::Document& document, ConversionContext& context)
714 auto& imageMetaData = context.mOutput.mSceneMetadata.mImageMetadata;
716 auto& outMaterials = context.mOutput.mResources.mMaterials;
717 outMaterials.reserve(document.mMaterials.size());
719 for(auto& material : document.mMaterials)
721 ConvertMaterial(material, imageMetaData, outMaterials, context);
725 MeshDefinition::Accessor ConvertMeshPrimitiveAccessor(const gltf2::Accessor& accessor)
727 DALI_ASSERT_ALWAYS((accessor.mBufferView &&
728 (accessor.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) ||
729 (accessor.mSparse && !accessor.mBufferView));
731 DALI_ASSERT_ALWAYS(!accessor.mSparse ||
732 ((accessor.mSparse->mIndices.mBufferView && (accessor.mSparse->mIndices.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) &&
733 (accessor.mSparse->mValues.mBufferView && (accessor.mSparse->mValues.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max()))));
735 MeshDefinition::SparseBlob sparseBlob;
738 const gltf2::Accessor::Sparse& sparse = *accessor.mSparse;
739 const gltf2::ComponentTypedBufferViewClient& indices = sparse.mIndices;
740 const gltf2::BufferViewClient& values = sparse.mValues;
742 MeshDefinition::Blob indicesBlob(
743 indices.mBufferView->mByteOffset + indices.mByteOffset,
744 sparse.mCount * indices.GetBytesPerComponent(),
745 static_cast<uint16_t>(indices.mBufferView->mByteStride),
746 static_cast<uint16_t>(indices.GetBytesPerComponent()),
749 MeshDefinition::Blob valuesBlob(
750 values.mBufferView->mByteOffset + values.mByteOffset,
751 sparse.mCount * accessor.GetElementSizeBytes(),
752 static_cast<uint16_t>(values.mBufferView->mByteStride),
753 static_cast<uint16_t>(accessor.GetElementSizeBytes()),
757 sparseBlob = std::move(MeshDefinition::SparseBlob(std::move(indicesBlob), std::move(valuesBlob), accessor.mSparse->mCount));
760 uint32_t bufferViewOffset = 0u;
761 uint32_t bufferViewStride = 0u;
762 if(accessor.mBufferView)
764 bufferViewOffset = accessor.mBufferView->mByteOffset;
765 bufferViewStride = accessor.mBufferView->mByteStride;
768 return MeshDefinition::Accessor{
769 std::move(MeshDefinition::Blob{bufferViewOffset + accessor.mByteOffset,
770 accessor.GetBytesLength(),
771 static_cast<uint16_t>(bufferViewStride),
772 static_cast<uint16_t>(accessor.GetElementSizeBytes()),
775 std::move(sparseBlob),
776 accessor.mBufferView ? accessor.mBufferView->mBuffer.GetIndex() : 0,
777 accessor.mNormalized};
780 MeshDefinition::Accessor* GetAccessorFromAttribute(gltf2::Attribute::HashType attributeHash,
781 MeshDefinition& meshDefinition,
782 bool& needNormals, bool& needTangents)
784 MeshDefinition::Accessor* accessorDest{nullptr};
786 switch(gltf2::Attribute::TypeFromHash(attributeHash))
788 case gltf2::Attribute::POSITION:
790 accessorDest = &meshDefinition.mPositions;
793 case gltf2::Attribute::NORMAL:
795 accessorDest = &meshDefinition.mNormals;
799 case gltf2::Attribute::TANGENT:
801 accessorDest = &meshDefinition.mTangents;
802 needTangents = false;
805 case gltf2::Attribute::TEXCOORD_N:
807 meshDefinition.mTexCoords.emplace_back(MeshDefinition::Accessor{});
808 accessorDest = &meshDefinition.mTexCoords.back();
811 case gltf2::Attribute::COLOR_N:
813 meshDefinition.mColors.emplace_back(MeshDefinition::Accessor{});
814 accessorDest = &meshDefinition.mColors.back();
817 case gltf2::Attribute::JOINTS_N:
819 if(meshDefinition.mJoints.size() < MeshDefinition::MAX_NUMBER_OF_JOINT_SETS)
821 meshDefinition.mJoints.emplace_back(MeshDefinition::Accessor{});
822 accessorDest = &meshDefinition.mJoints.back();
826 case gltf2::Attribute::WEIGHTS_N:
828 if(meshDefinition.mWeights.size() < MeshDefinition::MAX_NUMBER_OF_JOINT_SETS)
830 meshDefinition.mWeights.emplace_back(MeshDefinition::Accessor{});
831 accessorDest = &meshDefinition.mWeights.back();
835 case gltf2::Attribute::INVALID:
837 accessorDest = nullptr;
844 void SetFlagsFromComponentType(const gltf2::Accessor& accessor,
845 gltf2::Attribute::HashType attributeHash,
846 MeshDefinition& meshDefinition,
849 switch(gltf2::Attribute::TypeFromHash(attributeHash))
851 case gltf2::Attribute::POSITION:
855 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::BYTE) * MeshDefinition::S8_POSITION;
856 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_BYTE) * MeshDefinition::U8_POSITION;
857 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::SHORT) * MeshDefinition::S16_POSITION;
858 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_SHORT) * MeshDefinition::U16_POSITION;
860 DALI_ASSERT_DEBUG((isQuantized && (MaskMatch(meshDefinition.mFlags, MeshDefinition::S8_POSITION) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_POSITION) || MaskMatch(meshDefinition.mFlags, MeshDefinition::S16_POSITION) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_POSITION))) || accessor.mComponentType == gltf2::Component::FLOAT);
863 case gltf2::Attribute::NORMAL:
867 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::BYTE) * MeshDefinition::S8_NORMAL;
868 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::SHORT) * MeshDefinition::S16_NORMAL;
871 DALI_ASSERT_DEBUG((isQuantized && (MaskMatch(meshDefinition.mFlags, MeshDefinition::S8_NORMAL) || MaskMatch(meshDefinition.mFlags, MeshDefinition::S16_NORMAL))) || accessor.mComponentType == gltf2::Component::FLOAT);
874 case gltf2::Attribute::TANGENT:
878 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::BYTE) * MeshDefinition::S8_TANGENT;
879 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::SHORT) * MeshDefinition::S16_TANGENT;
882 DALI_ASSERT_DEBUG((isQuantized && (MaskMatch(meshDefinition.mFlags, MeshDefinition::S8_TANGENT) || MaskMatch(meshDefinition.mFlags, MeshDefinition::S16_TANGENT))) || accessor.mComponentType == gltf2::Component::FLOAT);
885 case gltf2::Attribute::TEXCOORD_N:
889 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::BYTE) * MeshDefinition::S8_TEXCOORD;
890 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_BYTE) * MeshDefinition::U8_TEXCOORD;
891 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::SHORT) * MeshDefinition::S16_TEXCOORD;
892 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_SHORT) * MeshDefinition::U16_TEXCOORD;
894 DALI_ASSERT_DEBUG((isQuantized && (MaskMatch(meshDefinition.mFlags, MeshDefinition::S8_TEXCOORD) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_TEXCOORD) || MaskMatch(meshDefinition.mFlags, MeshDefinition::S16_TEXCOORD) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_TEXCOORD))) || accessor.mComponentType == gltf2::Component::FLOAT);
897 case gltf2::Attribute::COLOR_N:
901 case gltf2::Attribute::JOINTS_N:
903 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
904 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_BYTE) * MeshDefinition::U8_JOINT_IDS;
905 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) ||
906 MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_JOINT_IDS) ||
907 accessor.mComponentType == gltf2::Component::FLOAT);
910 case gltf2::Attribute::WEIGHTS_N:
912 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_SHORT) * MeshDefinition::U16_WEIGHT;
913 meshDefinition.mFlags |= (accessor.mComponentType == gltf2::Component::UNSIGNED_BYTE) * MeshDefinition::U8_WEIGHT;
914 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_WEIGHT) ||
915 MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_WEIGHT) ||
916 accessor.mComponentType == gltf2::Component::FLOAT);
920 case gltf2::Attribute::INVALID:
927 void ConvertMeshes(const gltf2::Document& document, ConversionContext& context)
929 bool isQuantized = MaskMatch(document.mExtensionFlags, gltf2::ExtensionFlags::KHR_MESH_QUANTIZATION);
931 uint32_t meshCount = 0;
932 context.mMeshIds.reserve(document.mMeshes.size());
933 for(auto& mesh : document.mMeshes)
935 context.mMeshIds.push_back(meshCount);
936 meshCount += mesh.mPrimitives.size();
939 auto& outMeshes = context.mOutput.mResources.mMeshes;
940 outMeshes.reserve(meshCount);
941 for(auto& mesh : document.mMeshes)
943 for(auto& primitive : mesh.mPrimitives)
945 MeshDefinition meshDefinition;
947 auto& attribs = primitive.mAttributes;
948 meshDefinition.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[primitive.mMode];
950 auto positionIter = attribs.find(gltf2::Attribute::ToHash(gltf2::Attribute::POSITION, false, 0));
951 if(positionIter == attribs.end())
953 DALI_LOG_ERROR("Primitive mesh dosn't have POSITION atrributes!");
957 auto& positionsAccessor = *positionIter->second;
959 // glTF2 support vector4 tangent for mesh.
960 // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#meshes-overview
961 meshDefinition.mTangentType = Property::VECTOR4;
963 bool needNormals = (positionsAccessor.mType == gltf2::AccessorType::VEC3);
964 bool needTangents = (positionsAccessor.mType == gltf2::AccessorType::VEC3);
966 for(const auto& [attributeHash, accessor] : attribs)
968 MeshDefinition::Accessor* accessorDest = GetAccessorFromAttribute(attributeHash, meshDefinition, needNormals, needTangents);
969 if(accessorDest == nullptr)
973 *accessorDest = ConvertMeshPrimitiveAccessor(*accessor);
974 SetFlagsFromComponentType(*accessor, attributeHash, meshDefinition, isQuantized);
979 meshDefinition.RequestNormals();
983 meshDefinition.RequestTangents();
986 if(primitive.mIndices)
988 meshDefinition.mIndices = ConvertMeshPrimitiveAccessor(*primitive.mIndices);
989 meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gltf2::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
990 meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gltf2::Component::UNSIGNED_BYTE) * MeshDefinition::U8_INDICES;
991 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_INDICES) || primitive.mIndices->mComponentType == gltf2::Component::UNSIGNED_SHORT);
994 if(!primitive.mTargets.empty())
996 meshDefinition.mBlendShapes.reserve(primitive.mTargets.size());
997 meshDefinition.mBlendShapeVersion = BlendShapes::Version::VERSION_2_0;
998 uint32_t blendShapeIndex = 0u;
999 for(const auto& target : primitive.mTargets)
1001 MeshDefinition::BlendShape blendShape;
1003 auto endIt = target.end();
1004 auto it = target.find(gltf2::Attribute::POSITION);
1007 blendShape.deltas = ConvertMeshPrimitiveAccessor(*it->second);
1008 blendShape.deltas.mNormalized = it->second->mNormalized;
1012 blendShape.mFlags |= (it->second->mComponentType == gltf2::Component::BYTE) * MeshDefinition::S8_POSITION;
1013 blendShape.mFlags |= (it->second->mComponentType == gltf2::Component::SHORT) * MeshDefinition::S16_POSITION;
1016 DALI_ASSERT_DEBUG((isQuantized && (MaskMatch(blendShape.mFlags, MeshDefinition::S8_POSITION) || MaskMatch(blendShape.mFlags, MeshDefinition::U8_POSITION) || MaskMatch(blendShape.mFlags, MeshDefinition::S16_POSITION) || MaskMatch(blendShape.mFlags, MeshDefinition::U16_POSITION))) || it->second->mComponentType == gltf2::Component::FLOAT);
1018 it = target.find(gltf2::Attribute::NORMAL);
1021 blendShape.normals = ConvertMeshPrimitiveAccessor(*it->second);
1022 blendShape.normals.mNormalized = it->second->mNormalized;
1026 blendShape.mFlags |= (it->second->mComponentType == gltf2::Component::BYTE) * MeshDefinition::S8_NORMAL;
1027 blendShape.mFlags |= (it->second->mComponentType == gltf2::Component::SHORT) * MeshDefinition::S16_NORMAL;
1030 DALI_ASSERT_DEBUG((isQuantized && (MaskMatch(blendShape.mFlags, MeshDefinition::S8_NORMAL) || MaskMatch(blendShape.mFlags, MeshDefinition::S16_NORMAL))) || it->second->mComponentType == gltf2::Component::FLOAT);
1032 it = target.find(gltf2::Attribute::TANGENT);
1035 blendShape.tangents = ConvertMeshPrimitiveAccessor(*it->second);
1036 blendShape.tangents.mNormalized = it->second->mNormalized;
1040 blendShape.mFlags |= (it->second->mComponentType == gltf2::Component::BYTE) * MeshDefinition::S8_TANGENT;
1041 blendShape.mFlags |= (it->second->mComponentType == gltf2::Component::SHORT) * MeshDefinition::S16_TANGENT;
1044 DALI_ASSERT_DEBUG((isQuantized && (MaskMatch(blendShape.mFlags, MeshDefinition::S8_TANGENT) || MaskMatch(blendShape.mFlags, MeshDefinition::S16_TANGENT))) || it->second->mComponentType == gltf2::Component::FLOAT);
1047 if(!mesh.mWeights.empty())
1049 blendShape.weight = mesh.mWeights[meshDefinition.mBlendShapes.size()];
1052 // Get blendshape name from extras / SXR_targets_names / avatar_shape_names.
1053 if(blendShapeIndex < mesh.mExtras.mTargetNames.size())
1055 blendShape.name = mesh.mExtras.mTargetNames[blendShapeIndex];
1057 else if(blendShapeIndex < mesh.mExtensions.mSXRTargetsNames.size())
1059 blendShape.name = mesh.mExtensions.mSXRTargetsNames[blendShapeIndex];
1061 else if(blendShapeIndex < mesh.mExtensions.mAvatarShapeNames.size())
1063 blendShape.name = mesh.mExtensions.mAvatarShapeNames[blendShapeIndex];
1066 meshDefinition.mBlendShapes.push_back(std::move(blendShape));
1071 outMeshes.push_back({std::move(meshDefinition), MeshGeometry{}});
1076 ModelRenderable* MakeModelRenderable(const gltf2::Mesh::Primitive& primitive, ConversionContext& context)
1078 auto modelRenderable = new ModelRenderable();
1080 auto materialIdx = primitive.mMaterial.GetIndex();
1081 if(INVALID_INDEX == materialIdx)
1083 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
1084 if(INVALID_INDEX == context.mDefaultMaterial)
1086 auto& outMaterials = context.mOutput.mResources.mMaterials;
1087 context.mDefaultMaterial = outMaterials.size();
1089 ConvertMaterial(gltf2::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials, context);
1092 materialIdx = context.mDefaultMaterial;
1095 modelRenderable->mMaterialIdx = materialIdx;
1097 return modelRenderable;
1100 void ConvertCamera(const gltf2::Camera& camera, CameraParameters& cameraParameters)
1102 cameraParameters.isPerspective = camera.mType.compare("perspective") == 0;
1103 if(cameraParameters.isPerspective)
1105 auto& perspective = camera.mPerspective;
1106 if(!Dali::Equals(perspective.mYFov, gltf2::UNDEFINED_FLOAT_VALUE))
1108 cameraParameters.yFovDegree = Degree(Radian(perspective.mYFov));
1112 cameraParameters.yFovDegree = Degree(gltf2::UNDEFINED_FLOAT_VALUE);
1114 cameraParameters.zNear = perspective.mZNear;
1115 cameraParameters.zFar = perspective.mZFar;
1116 // TODO: yes, we seem to ignore aspectRatio in CameraParameters.
1120 auto& ortho = camera.mOrthographic;
1121 if(!Dali::Equals(ortho.mYMag, gltf2::UNDEFINED_FLOAT_VALUE) && !Dali::Equals(ortho.mXMag, gltf2::UNDEFINED_FLOAT_VALUE))
1123 cameraParameters.orthographicSize = ortho.mYMag * .5f;
1124 cameraParameters.aspectRatio = ortho.mXMag / ortho.mYMag;
1128 cameraParameters.orthographicSize = gltf2::UNDEFINED_FLOAT_VALUE;
1129 cameraParameters.aspectRatio = gltf2::UNDEFINED_FLOAT_VALUE;
1131 cameraParameters.zNear = ortho.mZNear;
1132 cameraParameters.zFar = ortho.mZFar;
1135 cameraParameters.name = std::string(camera.mName);
1138 void ConvertNode(gltf2::Node const& node, const Index gltfIndex, Index parentIndex, ConversionContext& context, bool isMRendererModel)
1140 auto& output = context.mOutput;
1141 auto& scene = output.mScene;
1142 auto& resources = output.mResources;
1144 const auto index = scene.GetNodeCount();
1145 auto weakNode = scene.AddNode([&]() {
1146 std::unique_ptr<NodeDefinition> nodeDefinition{new NodeDefinition()};
1148 nodeDefinition->mParentIdx = parentIndex;
1149 nodeDefinition->mName = node.mName;
1150 if(nodeDefinition->mName.empty())
1152 // TODO: Production quality generation of unique names.
1153 nodeDefinition->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDefinition.get()));
1156 if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
1158 nodeDefinition->mPosition = node.mTranslation;
1159 nodeDefinition->mOrientation = node.mRotation;
1160 nodeDefinition->mScale = node.mScale;
1162 if(isMRendererModel && node.mName == ROOT_NODE_NAME && node.mScale == SCALE_TO_ADJUST)
1164 nodeDefinition->mScale *= 0.01f;
1168 return nodeDefinition; }());
1171 ExceptionFlinger(ASSERT_LOCATION) << "Node name '" << node.mName << "' is not unique; scene is invalid.";
1174 context.mNodeIndices.RegisterMapping(gltfIndex, index);
1176 Index skeletonIdx = node.mSkin ? node.mSkin.GetIndex() : INVALID_INDEX;
1179 auto& mesh = *node.mMesh;
1180 uint32_t primitiveCount = mesh.mPrimitives.size();
1181 auto meshIndex = context.mMeshIds[node.mMesh.GetIndex()];
1182 weakNode->mRenderables.reserve(primitiveCount);
1183 for(uint32_t i = 0; i < primitiveCount; ++i)
1185 std::unique_ptr<NodeDefinition::Renderable> renderable;
1186 auto modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
1187 modelRenderable->mMeshIdx = meshIndex + i;
1189 DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
1190 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
1191 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx = skeletonIdx;
1193 renderable.reset(modelRenderable);
1194 weakNode->mRenderables.push_back(std::move(renderable));
1200 CameraParameters cameraParameters;
1201 ConvertCamera(*node.mCamera, cameraParameters);
1203 cameraParameters.matrix.SetTransformComponents(node.mScale, node.mRotation, node.mTranslation);
1204 output.mCameraParameters.push_back(cameraParameters);
1207 for(auto& child : node.mChildren)
1209 ConvertNode(*child, child.GetIndex(), index, context, isMRendererModel);
1213 void ConvertSceneNodes(const gltf2::Scene& scene, ConversionContext& context, bool isMRendererModel)
1215 auto& outScene = context.mOutput.mScene;
1216 Index rootIndex = outScene.GetNodeCount();
1217 switch(scene.mNodes.size())
1223 ConvertNode(*scene.mNodes[0], scene.mNodes[0].GetIndex(), INVALID_INDEX, context, isMRendererModel);
1224 outScene.AddRootNode(rootIndex);
1229 std::unique_ptr<NodeDefinition> sceneRoot{new NodeDefinition()};
1230 sceneRoot->mName = "GLTF_LOADER_SCENE_ROOT_" + std::to_string(outScene.GetRoots().size());
1232 outScene.AddNode(std::move(sceneRoot));
1233 outScene.AddRootNode(rootIndex);
1235 for(auto& node : scene.mNodes)
1237 ConvertNode(*node, node.GetIndex(), rootIndex, context, isMRendererModel);
1244 void ConvertNodes(const gltf2::Document& document, ConversionContext& context, bool isMRendererModel)
1246 if(!document.mScenes.empty())
1248 uint32_t rootSceneIndex = 0u;
1251 rootSceneIndex = document.mScene.GetIndex();
1253 ConvertSceneNodes(document.mScenes[rootSceneIndex], context, isMRendererModel);
1255 for(uint32_t i = 0; i < rootSceneIndex; ++i)
1257 ConvertSceneNodes(document.mScenes[i], context, isMRendererModel);
1260 for(uint32_t i = rootSceneIndex + 1; i < document.mScenes.size(); ++i)
1262 ConvertSceneNodes(document.mScenes[i], context, isMRendererModel);
1267 template<typename T>
1268 void LoadDataFromAccessor(ConversionContext& context, uint32_t bufferIndex, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
1270 if(bufferIndex >= context.mOutput.mResources.mBuffers.size())
1272 DALI_LOG_ERROR("Invailid buffer index\n");
1276 auto& buffer = context.mOutput.mResources.mBuffers[bufferIndex];
1277 if(!buffer.IsAvailable())
1279 DALI_LOG_ERROR("Failed to load from buffer stream.\n");
1281 auto& stream = buffer.GetBufferStream();
1283 stream.seekg(offset, stream.beg);
1284 stream.read(reinterpret_cast<char*>(dataBuffer.Begin()), static_cast<std::streamsize>(static_cast<size_t>(size)));
1287 template<typename T>
1288 float LoadDataFromAccessors(ConversionContext& context, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
1290 inputDataBuffer.Resize(input.mCount);
1291 outputDataBuffer.Resize(output.mCount);
1293 const uint32_t inputDataBufferSize = input.GetBytesLength();
1294 const uint32_t outputDataBufferSize = output.GetBytesLength();
1296 LoadDataFromAccessor<float>(context, output.mBufferView->mBuffer.GetIndex(), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
1297 LoadDataFromAccessor<T>(context, output.mBufferView->mBuffer.GetIndex(), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
1298 ApplyAccessorMinMax(input, reinterpret_cast<float*>(inputDataBuffer.begin()));
1299 ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
1301 return inputDataBuffer[input.mCount - 1u];
1304 bool IsFirstFrameValueEmpty(const uint32_t inputCount, const Vector<float>& inputBuffer)
1306 return (inputCount > 0 && !Dali::EqualsZero(inputBuffer[0]));
1309 template<typename T>
1310 float LoadKeyFrames(ConversionContext& context, const gltf2::Animation::Channel& channel, KeyFrames& keyFrames, gltf2::Animation::Channel::Target::Type type)
1312 const gltf2::Accessor& input = *channel.mSampler->mInput;
1313 const gltf2::Accessor& output = *channel.mSampler->mOutput;
1315 Vector<float> inputDataBuffer;
1316 Vector<T> outputDataBuffer;
1318 const float duration = std::max(LoadDataFromAccessors<T>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
1320 if(IsFirstFrameValueEmpty(input.mCount, inputDataBuffer))
1322 keyFrames.Add(0.0f, outputDataBuffer[0]);
1325 for(uint32_t i = 0; i < input.mCount; ++i)
1327 keyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i]);
1333 float LoadBlendShapeKeyFrames(ConversionContext& context, const gltf2::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, AnimationDefinition& animationDefinition)
1335 const gltf2::Accessor& input = *channel.mSampler->mInput;
1336 const gltf2::Accessor& output = *channel.mSampler->mOutput;
1338 Vector<float> inputDataBuffer;
1339 Vector<float> outputDataBuffer;
1341 const float duration = std::max(LoadDataFromAccessors<float>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
1343 char weightNameBuffer[32];
1344 auto prefixSize = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM.data());
1345 char* const pWeightName = weightNameBuffer + prefixSize;
1346 const auto remainingSize = sizeof(weightNameBuffer) - prefixSize;
1347 for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex)
1349 AnimatedProperty animatedProperty;
1351 animatedProperty.mNodeIndex = nodeIndex;
1352 snprintf(pWeightName, remainingSize, "%d]", weightIndex);
1353 animatedProperty.mPropertyName = std::string(weightNameBuffer);
1355 animatedProperty.mKeyFrames = KeyFrames::New();
1357 if(IsFirstFrameValueEmpty(input.mCount, inputDataBuffer))
1359 animatedProperty.mKeyFrames.Add(0.0f, outputDataBuffer[weightIndex]);
1362 for(uint32_t i = 0; i < input.mCount; ++i)
1364 animatedProperty.mKeyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i * endWeightIndex + weightIndex]);
1367 animatedProperty.mTimePeriod = {0.f, duration};
1369 animationDefinition.SetProperty(propertyIndex++, std::move(animatedProperty));
1375 template<typename T>
1376 float LoadAnimation(AnimationDefinition& animationDefinition, Index nodeIndex, Index propertyIndex, const std::string& propertyName, const gltf2::Animation::Channel& channel, ConversionContext& context)
1378 AnimatedProperty animatedProperty;
1379 animatedProperty.mNodeIndex = nodeIndex;
1380 animatedProperty.mPropertyName = propertyName;
1382 animatedProperty.mKeyFrames = KeyFrames::New();
1383 float duration = LoadKeyFrames<T>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1384 animatedProperty.mTimePeriod = {0.f, duration};
1386 animationDefinition.SetProperty(propertyIndex, std::move(animatedProperty));
1390 void ConvertAnimations(const gltf2::Document& document, ConversionContext& context)
1392 auto& output = context.mOutput;
1394 output.mAnimationDefinitions.reserve(output.mAnimationDefinitions.size() + document.mAnimations.size());
1396 for(const auto& animation : document.mAnimations)
1398 AnimationDefinition animationDefinition;
1400 if(!animation.mName.empty())
1402 animationDefinition.SetName(animation.mName.data());
1405 uint32_t numberOfProperties = 0u;
1406 for(const auto& channel : animation.mChannels)
1408 if(channel.mTarget.mPath == gltf2::Animation::Channel::Target::WEIGHTS)
1410 numberOfProperties += channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount;
1414 numberOfProperties++;
1417 animationDefinition.ReserveSize(numberOfProperties);
1419 Index propertyIndex = 0u;
1420 for(const auto& channel : animation.mChannels)
1422 Index nodeIndex = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
1423 float duration = 0.f;
1425 switch(channel.mTarget.mPath)
1427 case gltf2::Animation::Channel::Target::TRANSLATION:
1429 duration = LoadAnimation<Vector3>(animationDefinition, nodeIndex, propertyIndex, POSITION_PROPERTY.data(), channel, context);
1432 case gltf2::Animation::Channel::Target::ROTATION:
1434 duration = LoadAnimation<Quaternion>(animationDefinition, nodeIndex, propertyIndex, ORIENTATION_PROPERTY.data(), channel, context);
1437 case gltf2::Animation::Channel::Target::SCALE:
1439 duration = LoadAnimation<Vector3>(animationDefinition, nodeIndex, propertyIndex, SCALE_PROPERTY.data(), channel, context);
1442 case gltf2::Animation::Channel::Target::WEIGHTS:
1444 duration = LoadBlendShapeKeyFrames(context, channel, nodeIndex, propertyIndex, animationDefinition);
1450 // nothing to animate.
1455 animationDefinition.SetDuration(std::max(duration, animationDefinition.GetDuration()));
1460 output.mAnimationDefinitions.push_back(std::move(animationDefinition));
1464 void ProcessSkins(const gltf2::Document& document, ConversionContext& context)
1466 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skininversebindmatrices
1467 // If an inverseBindMatrices accessor was provided, we'll load the joint data from the buffer,
1468 // otherwise we'll set identity matrices for inverse bind pose.
1469 struct IInverseBindMatrixProvider
1471 virtual ~IInverseBindMatrixProvider()
1474 virtual void Provide(Matrix& inverseBindMatrix) = 0;
1477 struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
1479 std::istream& mStream;
1480 const uint32_t mElementSizeBytes;
1482 InverseBindMatrixAccessor(const gltf2::Accessor& accessor, ConversionContext& context)
1483 : mStream(context.mOutput.mResources.mBuffers[accessor.mBufferView->mBuffer.GetIndex()].GetBufferStream()),
1484 mElementSizeBytes(accessor.GetElementSizeBytes())
1486 DALI_ASSERT_DEBUG(accessor.mType == gltf2::AccessorType::MAT4 && accessor.mComponentType == gltf2::Component::FLOAT);
1488 if(!mStream.rdbuf()->in_avail())
1490 DALI_LOG_ERROR("Failed to load from stream\n");
1493 mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset, mStream.beg);
1496 virtual void Provide(Matrix& inverseBindMatrix) override
1498 DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(inverseBindMatrix.AsFloat()), static_cast<std::streamsize>(static_cast<size_t>(mElementSizeBytes))));
1502 struct DefaultInverseBindMatrixProvider : public IInverseBindMatrixProvider
1504 virtual void Provide(Matrix& inverseBindMatrix) override
1506 inverseBindMatrix = Matrix::IDENTITY;
1510 auto& resources = context.mOutput.mResources;
1511 resources.mSkeletons.reserve(document.mSkins.size());
1513 for(auto& skin : document.mSkins)
1515 std::unique_ptr<IInverseBindMatrixProvider> inverseBindMatrixProvider;
1516 if(skin.mInverseBindMatrices)
1518 inverseBindMatrixProvider.reset(new InverseBindMatrixAccessor(*skin.mInverseBindMatrices, context));
1522 inverseBindMatrixProvider.reset(new DefaultInverseBindMatrixProvider());
1525 SkeletonDefinition skeleton;
1526 if(skin.mSkeleton.GetIndex() != INVALID_INDEX)
1528 skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(skin.mSkeleton.GetIndex());
1531 skeleton.mJoints.resize(skin.mJoints.size());
1532 auto iJoint = skeleton.mJoints.begin();
1533 for(auto& joint : skin.mJoints)
1535 iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(joint.GetIndex());
1537 inverseBindMatrixProvider->Provide(iJoint->mInverseBindMatrix);
1542 resources.mSkeletons.push_back(std::move(skeleton));
1546 void SetObjectReaders()
1548 json::SetObjectReader(GetBufferReader());
1549 json::SetObjectReader(GetBufferViewReader());
1550 json::SetObjectReader(GetBufferViewClientReader());
1551 json::SetObjectReader(GetComponentTypedBufferViewClientReader());
1552 json::SetObjectReader(GetAccessorSparseReader());
1553 json::SetObjectReader(GetAccessorReader());
1554 json::SetObjectReader(GetImageReader());
1555 json::SetObjectReader(GetSamplerReader());
1556 json::SetObjectReader(GetTextureReader());
1557 json::SetObjectReader(GetTextureInfoReader());
1558 json::SetObjectReader(GetMaterialPbrReader());
1559 json::SetObjectReader(GetMaterialSpecularReader());
1560 json::SetObjectReader(GetMaterialIorReader());
1561 json::SetObjectReader(GetMaterialExtensionsReader());
1562 json::SetObjectReader(GetMaterialReader());
1563 json::SetObjectReader(GetMeshPrimitiveReader());
1564 json::SetObjectReader(GetMeshExtrasReader());
1565 json::SetObjectReader(GetMeshExtensionsReader());
1566 json::SetObjectReader(GetMeshReader());
1567 json::SetObjectReader(GetSkinReader());
1568 json::SetObjectReader(GetCameraPerspectiveReader());
1569 json::SetObjectReader(GetCameraOrthographicReader());
1570 json::SetObjectReader(GetCameraReader());
1571 json::SetObjectReader(GetNodeReader());
1572 json::SetObjectReader(GetAnimationSamplerReader());
1573 json::SetObjectReader(GetAnimationChannelTargetReader());
1574 json::SetObjectReader(GetAnimationChannelReader());
1575 json::SetObjectReader(GetAnimationReader());
1576 json::SetObjectReader(GetSceneReader());
1579 void SetDefaultEnvironmentMap(const gltf2::Document& document, ConversionContext& context)
1581 EnvironmentDefinition environmentDefinition;
1582 environmentDefinition.mUseBrdfTexture = true;
1583 environmentDefinition.mIblIntensity = Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
1584 context.mOutput.mResources.mEnvironmentMaps.push_back({std::move(environmentDefinition), EnvironmentDefinition::Textures()});
1587 void InitializeGltfLoader()
1589 static Dali::Mutex initializeMutex;
1590 // Set ObjectReader only once (for all gltf loading).
1591 static bool setObjectReadersRequired = true;
1593 Mutex::ScopedLock lock(initializeMutex);
1594 if(setObjectReadersRequired)
1596 // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1598 setObjectReadersRequired = false;
1603 const std::string_view GetRendererModelIdentification()
1605 return MRENDERER_MODEL_IDENTIFICATION;
1608 void ReadDocument(const json_object_s& jsonObject, gltf2::Document& document)
1610 GetDocumentReader().Read(jsonObject, document);
1613 void ReadDocumentFromParsedData(const json_object_s& jsonObject, gltf2::Document& document)
1615 static Dali::Mutex readMutex;
1616 Mutex::ScopedLock lock(readMutex);
1617 gt::SetRefReaderObject(document);
1618 Gltf2Util::ReadDocument(jsonObject, document);
1621 bool GenerateDocument(json::unique_ptr& root, gt::Document& document, bool& isMRendererModel)
1623 auto& rootObject = js::Cast<json_object_s>(*root);
1624 auto jsonAsset = js::FindObjectChild("asset", rootObject);
1626 auto jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsonAsset));
1629 document.mAsset.mVersion = js::Read::StringView(*jsAssetVersion);
1632 auto jsAssetGenerator = js::FindObjectChild("generator", js::Cast<json_object_s>(*jsonAsset));
1633 if(jsAssetGenerator)
1635 document.mAsset.mGenerator = js::Read::StringView(*jsAssetGenerator);
1636 isMRendererModel = (document.mAsset.mGenerator.find(Gltf2Util::GetRendererModelIdentification().data()) != std::string_view::npos);
1639 Gltf2Util::InitializeGltfLoader();
1640 Gltf2Util::ReadDocumentFromParsedData(rootObject, document);
1645 void ConvertGltfToContext(gt::Document& document, Gltf2Util::ConversionContext& context, bool isMRendererModel)
1647 for(auto& extension : document.mExtensionsRequired)
1649 gltf2::ExtensionFlags flag;
1650 if(Dali::Scripting::GetEnumeration<gltf2::ExtensionFlags>(extension.data(), EXTENSION_STRING_TABLE, EXTENSION_STRING_TABLE_COUNT, flag))
1652 document.mExtensionFlags |= flag;
1656 DALI_LOG_ERROR("Unsupported glTF extension required: %s\n", extension.data());
1657 DALI_ASSERT_DEBUG(false && "Unsupported glTF extension required");
1661 Gltf2Util::ConvertBuffers(document, context);
1662 Gltf2Util::ConvertMaterials(document, context);
1663 Gltf2Util::ConvertMeshes(document, context);
1664 Gltf2Util::ConvertNodes(document, context, isMRendererModel);
1665 Gltf2Util::ConvertAnimations(document, context);
1666 Gltf2Util::ProcessSkins(document, context);
1668 // Set Default Environment map
1669 Gltf2Util::SetDefaultEnvironmentMap(document, context);
1672 } // namespace Gltf2Util
1674 } // namespace Dali::Scene3D::Loader::Internal