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-loader-impl.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/images/image-operations.h>
24 #include <dali/public-api/math/quaternion.h>
28 #include <dali-scene3d/internal/loader/gltf2-asset.h>
29 #include <dali-scene3d/public-api/loader/load-result.h>
30 #include <dali-scene3d/public-api/loader/resource-bundle.h>
31 #include <dali-scene3d/public-api/loader/scene-definition.h>
32 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
33 #include <dali-scene3d/public-api/loader/utils.h>
49 const char* POSITION_PROPERTY("position");
50 const char* ORIENTATION_PROPERTY("orientation");
51 const char* SCALE_PROPERTY("scale");
52 const char* BLEND_SHAPE_WEIGHTS_UNIFORM("uBlendShapeWeight");
53 const char* MRENDERER_MODEL_IDENTIFICATION("M-Renderer");
54 const char* ROOT_NODE_NAME("RootNode");
55 const Vector3 SCALE_TO_ADJUST(100.0f, 100.0f, 100.0f);
57 const Geometry::Type GLTF2_TO_DALI_PRIMITIVES[]{
63 Geometry::TRIANGLE_STRIP,
64 Geometry::TRIANGLE_FAN}; //...because Dali swaps the last two.
66 struct AttributeMapping
68 gt::Attribute::Type mType;
69 MeshDefinition::Accessor MeshDefinition::*mAccessor;
70 uint16_t mElementSizeRequired;
71 } ATTRIBUTE_MAPPINGS[]{
72 {gt::Attribute::NORMAL, &MeshDefinition::mNormals, sizeof(Vector3)},
73 {gt::Attribute::TANGENT, &MeshDefinition::mTangents, sizeof(Vector3)},
74 {gt::Attribute::TEXCOORD_0, &MeshDefinition::mTexCoords, sizeof(Vector2)},
75 {gt::Attribute::COLOR_0, &MeshDefinition::mColors, sizeof(Vector4)},
76 {gt::Attribute::JOINTS_0, &MeshDefinition::mJoints0, sizeof(Vector4)},
77 {gt::Attribute::WEIGHTS_0, &MeshDefinition::mWeights0, sizeof(Vector4)},
80 std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
82 auto results = js::Read::Array<gt::Animation, js::ObjectReader<gt::Animation>::Read>(j);
84 for(auto& animation : results)
86 for(auto& channel : animation.mChannels)
88 channel.mSampler.UpdateVector(animation.mSamplers);
95 void ApplyAccessorMinMax(const gt::Accessor& acc, float* values)
97 DALI_ASSERT_ALWAYS(acc.mMax.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMax.size());
98 DALI_ASSERT_ALWAYS(acc.mMin.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMin.size());
99 MeshDefinition::Blob::ApplyMinMax(acc.mMin, acc.mMax, acc.mCount, values);
102 const js::Reader<gt::Buffer>& GetBufferReader()
104 static const auto BUFFER_READER = std::move(js::Reader<gt::Buffer>()
105 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::Buffer::mByteLength))
106 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Buffer::mUri)));
107 return BUFFER_READER;
110 const js::Reader<gt::BufferView>& GetBufferViewReader()
112 static const auto BUFFER_VIEW_READER = std::move(js::Reader<gt::BufferView>()
113 .Register(*js::MakeProperty("buffer", gt::RefReader<gt::Document>::Read<gt::Buffer, >::Document::mBuffers>, >::BufferView::mBuffer))
114 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferView::mByteOffset))
115 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::BufferView::mByteLength))
116 .Register(*js::MakeProperty("byteStride", js::Read::Number<uint32_t>, >::BufferView::mByteStride))
117 .Register(*js::MakeProperty("target", js::Read::Number<uint32_t>, >::BufferView::mTarget)));
118 return BUFFER_VIEW_READER;
121 const js::Reader<gt::BufferViewClient>& GetBufferViewClientReader()
123 static const auto BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::BufferViewClient>()
124 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::BufferViewClient::mBufferView))
125 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferViewClient::mByteOffset)));
126 return BUFFER_VIEW_CLIENT_READER;
129 const js::Reader<gt::ComponentTypedBufferViewClient>& GetComponentTypedBufferViewClientReader()
131 static const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::ComponentTypedBufferViewClient>()
132 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, gt::Ref<gt::BufferView>>("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::ComponentTypedBufferViewClient::mBufferView))
133 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, uint32_t>("byteOffset", js::Read::Number<uint32_t>, >::ComponentTypedBufferViewClient::mByteOffset))
134 .Register(*js::MakeProperty("componentType", js::Read::Enum<gt::Component::Type>, >::ComponentTypedBufferViewClient::mComponentType)));
135 return COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER;
138 const js::Reader<gt::Accessor::Sparse>& GetAccessorSparseReader()
140 static const auto ACCESSOR_SPARSE_READER = std::move(js::Reader<gt::Accessor::Sparse>()
141 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::Sparse::mCount))
142 .Register(*js::MakeProperty("indices", js::ObjectReader<gt::ComponentTypedBufferViewClient>::Read, >::Accessor::Sparse::mIndices))
143 .Register(*js::MakeProperty("values", js::ObjectReader<gt::BufferViewClient>::Read, >::Accessor::Sparse::mValues)));
144 return ACCESSOR_SPARSE_READER;
147 const js::Reader<gt::Accessor>& GetAccessorReader()
149 static const auto ACCESSOR_READER = std::move(js::Reader<gt::Accessor>()
150 .Register(*new js::Property<gt::Accessor, gt::Ref<gt::BufferView>>("bufferView",
151 gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>,
152 >::Accessor::mBufferView))
153 .Register(*new js::Property<gt::Accessor, uint32_t>("byteOffset",
154 js::Read::Number<uint32_t>,
155 >::Accessor::mByteOffset))
156 .Register(*new js::Property<gt::Accessor, gt::Component::Type>("componentType",
157 js::Read::Enum<gt::Component::Type>,
158 >::Accessor::mComponentType))
159 .Register(*new js::Property<gt::Accessor, std::string_view>("name", js::Read::StringView, >::Accessor::mName))
160 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::mCount))
161 .Register(*js::MakeProperty("normalized", js::Read::Boolean, >::Accessor::mNormalized))
162 .Register(*js::MakeProperty("type", gt::ReadStringEnum<gt::AccessorType>, >::Accessor::mType))
163 .Register(*js::MakeProperty("min", js::Read::Array<float, js::Read::Number>, >::Accessor::mMin))
164 .Register(*js::MakeProperty("max", js::Read::Array<float, js::Read::Number>, >::Accessor::mMax))
165 .Register(*new js::Property<gt::Accessor, gt::Accessor::Sparse>("sparse", js::ObjectReader<gt::Accessor::Sparse>::Read, >::Accessor::SetSparse)));
166 return ACCESSOR_READER;
169 const js::Reader<gt::Image>& GetImageReader()
171 static const auto IMAGE_READER = std::move(js::Reader<gt::Image>()
172 .Register(*new js::Property<gt::Image, std::string_view>("name", js::Read::StringView, >::Material::mName))
173 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Image::mUri))
174 .Register(*js::MakeProperty("mimeType", js::Read::StringView, >::Image::mMimeType))
175 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::Image::mBufferView)));
179 const js::Reader<gt::Sampler>& GetSamplerReader()
181 static const auto SAMPLER_READER = std::move(js::Reader<gt::Sampler>()
182 .Register(*js::MakeProperty("minFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMinFilter))
183 .Register(*js::MakeProperty("magFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMagFilter))
184 .Register(*js::MakeProperty("wrapS", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapS))
185 .Register(*js::MakeProperty("wrapT", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapT)));
186 return SAMPLER_READER;
189 const js::Reader<gt::Texture>& GetTextureReader()
191 static const auto TEXURE_READER = std::move(js::Reader<gt::Texture>()
192 .Register(*js::MakeProperty("source", gt::RefReader<gt::Document>::Read<gt::Image, >::Document::mImages>, >::Texture::mSource))
193 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Document>::Read<gt::Sampler, >::Document::mSamplers>, >::Texture::mSampler)));
194 return TEXURE_READER;
197 const js::Reader<gt::TextureInfo>& GetTextureInfoReader()
199 static const auto TEXURE_INFO_READER = std::move(js::Reader<gt::TextureInfo>()
200 .Register(*js::MakeProperty("index", gt::RefReader<gt::Document>::Read<gt::Texture, >::Document::mTextures>, >::TextureInfo::mTexture))
201 .Register(*js::MakeProperty("texCoord", js::Read::Number<uint32_t>, >::TextureInfo::mTexCoord))
202 .Register(*js::MakeProperty("scale", js::Read::Number<float>, >::TextureInfo::mScale))
203 .Register(*js::MakeProperty("strength", js::Read::Number<float>, >::TextureInfo::mStrength)));
204 return TEXURE_INFO_READER;
207 const js::Reader<gt::Material::Pbr>& GetMaterialPbrReader()
209 static const auto MATERIAL_PBR_READER = std::move(js::Reader<gt::Material::Pbr>()
210 .Register(*js::MakeProperty("baseColorFactor", gt::ReadDaliVector<Vector4>, >::Material::Pbr::mBaseColorFactor))
211 .Register(*js::MakeProperty("baseColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mBaseColorTexture))
212 .Register(*js::MakeProperty("metallicFactor", js::Read::Number<float>, >::Material::Pbr::mMetallicFactor))
213 .Register(*js::MakeProperty("roughnessFactor", js::Read::Number<float>, >::Material::Pbr::mRoughnessFactor))
214 .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mMetallicRoughnessTexture)));
215 return MATERIAL_PBR_READER;
218 const js::Reader<gt::MaterialSpecular>& GetMaterialSpecularReader()
220 static const auto MATERIAL_SPECULAR_READER = std::move(js::Reader<gt::MaterialSpecular>()
221 .Register(*js::MakeProperty("specularFactor", js::Read::Number<float>, >::MaterialSpecular::mSpecularFactor))
222 .Register(*js::MakeProperty("specularTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularTexture))
223 .Register(*js::MakeProperty("specularColorFactor", gt::ReadDaliVector<Vector3>, >::MaterialSpecular::mSpecularColorFactor))
224 .Register(*js::MakeProperty("specularColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularColorTexture)));
225 return MATERIAL_SPECULAR_READER;
228 const js::Reader<gt::MaterialIor>& GetMaterialIorReader()
230 static const auto MATERIAL_IOR_READER = std::move(js::Reader<gt::MaterialIor>()
231 .Register(*js::MakeProperty("ior", js::Read::Number<float>, >::MaterialIor::mIor)));
232 return MATERIAL_IOR_READER;
235 const js::Reader<gt::MaterialExtensions>& GetMaterialExtensionsReader()
237 static const auto MATERIAL_EXTENSION_READER = std::move(js::Reader<gt::MaterialExtensions>()
238 .Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader<gt::MaterialIor>::Read, >::MaterialExtensions::mMaterialIor))
239 .Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader<gt::MaterialSpecular>::Read, >::MaterialExtensions::mMaterialSpecular)));
240 return MATERIAL_EXTENSION_READER;
243 const js::Reader<gt::Material>& GetMaterialReader()
245 static const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
246 .Register(*new js::Property<gt::Material, std::string_view>("name", js::Read::StringView, >::Material::mName))
247 .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader<gt::Material::Pbr>::Read, >::Material::mPbrMetallicRoughness))
248 .Register(*js::MakeProperty("normalTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mNormalTexture))
249 .Register(*js::MakeProperty("occlusionTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mOcclusionTexture))
250 .Register(*js::MakeProperty("emissiveTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mEmissiveTexture))
251 .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector<Vector3>, >::Material::mEmissiveFactor))
252 .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum<gt::AlphaMode>, >::Material::mAlphaMode))
253 .Register(*js::MakeProperty("alphaCutoff", js::Read::Number<float>, >::Material::mAlphaCutoff))
254 .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, >::Material::mDoubleSided))
255 .Register(*js::MakeProperty("extensions", js::ObjectReader<gt::MaterialExtensions>::Read, >::Material::mMaterialExtensions)));
256 return MATERIAL_READER;
259 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
261 auto& jo = js::Cast<json_object_s>(j);
262 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> result;
267 auto jstr = *i->name;
268 result[gt::Attribute::FromString(jstr.string, jstr.string_size)] = gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>(*i->value);
274 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> ReadMeshPrimitiveTargets(const json_value_s& j)
276 auto& jo = js::Cast<json_array_s>(j);
277 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> result;
279 result.reserve(jo.length);
284 result.push_back(std::move(ReadMeshPrimitiveAttributes(*i->value)));
291 const js::Reader<gt::Mesh::Primitive>& GetMeshPrimitiveReader()
293 static const auto MESH_PRIMITIVE_READER = std::move(js::Reader<gt::Mesh::Primitive>()
294 .Register(*js::MakeProperty("attributes", ReadMeshPrimitiveAttributes, >::Mesh::Primitive::mAttributes))
295 .Register(*js::MakeProperty("indices", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Mesh::Primitive::mIndices))
296 .Register(*js::MakeProperty("material", gt::RefReader<gt::Document>::Read<gt::Material, >::Document::mMaterials>, >::Mesh::Primitive::mMaterial))
297 .Register(*js::MakeProperty("mode", js::Read::Enum<gt::Mesh::Primitive::Mode>, >::Mesh::Primitive::mMode))
298 .Register(*js::MakeProperty("targets", ReadMeshPrimitiveTargets, >::Mesh::Primitive::mTargets)));
299 return MESH_PRIMITIVE_READER;
302 const js::Reader<gt::Mesh>& GetMeshReader()
304 static const auto MESH_READER = std::move(js::Reader<gt::Mesh>()
305 .Register(*new js::Property<gt::Mesh, std::string_view>("name", js::Read::StringView, >::Mesh::mName))
306 .Register(*js::MakeProperty("primitives",
307 js::Read::Array<gt::Mesh::Primitive, js::ObjectReader<gt::Mesh::Primitive>::Read>,
308 >::Mesh::mPrimitives))
309 .Register(*js::MakeProperty("weights", js::Read::Array<float, js::Read::Number>, >::Mesh::mWeights)));
313 const js::Reader<gt::Skin>& GetSkinReader()
315 static const auto SKIN_READER = std::move(js::Reader<gt::Skin>()
316 .Register(*new js::Property<gt::Skin, std::string_view>("name", js::Read::StringView, >::Skin::mName))
317 .Register(*js::MakeProperty("inverseBindMatrices",
318 gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>,
319 >::Skin::mInverseBindMatrices))
320 .Register(*js::MakeProperty("skeleton",
321 gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>,
322 >::Skin::mSkeleton))
323 .Register(*js::MakeProperty("joints",
324 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
325 >::Skin::mJoints)));
329 const js::Reader<gt::Camera::Perspective>& GetCameraPerspectiveReader()
331 static const auto CAMERA_PERSPECTIVE_READER = std::move(js::Reader<gt::Camera::Perspective>()
332 .Register(*js::MakeProperty("aspectRatio", js::Read::Number<float>, >::Camera::Perspective::mAspectRatio))
333 .Register(*js::MakeProperty("yfov", js::Read::Number<float>, >::Camera::Perspective::mYFov))
334 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Perspective::mZFar))
335 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted
336 return CAMERA_PERSPECTIVE_READER;
339 const js::Reader<gt::Camera::Orthographic>& GetCameraOrthographicReader()
341 static const auto CAMERA_ORTHOGRAPHIC_READER = std::move(js::Reader<gt::Camera::Orthographic>()
342 .Register(*js::MakeProperty("xmag", js::Read::Number<float>, >::Camera::Orthographic::mXMag))
343 .Register(*js::MakeProperty("ymag", js::Read::Number<float>, >::Camera::Orthographic::mYMag))
344 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Orthographic::mZFar))
345 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Orthographic::mZNear)));
346 return CAMERA_ORTHOGRAPHIC_READER;
349 const js::Reader<gt::Camera>& GetCameraReader()
351 static const auto CAMERA_READER = std::move(js::Reader<gt::Camera>()
352 .Register(*new js::Property<gt::Camera, std::string_view>("name", js::Read::StringView, >::Camera::mName))
353 .Register(*js::MakeProperty("type", js::Read::StringView, >::Camera::mType))
354 .Register(*js::MakeProperty("perspective", js::ObjectReader<gt::Camera::Perspective>::Read, >::Camera::mPerspective))
355 .Register(*js::MakeProperty("orthographic", js::ObjectReader<gt::Camera::Orthographic>::Read, >::Camera::mOrthographic)));
356 return CAMERA_READER;
359 const js::Reader<gt::Node>& GetNodeReader()
361 static const auto NODE_READER = std::move(js::Reader<gt::Node>()
362 .Register(*new js::Property<gt::Node, std::string_view>("name", js::Read::StringView, >::Node::mName))
363 .Register(*js::MakeProperty("translation", gt::ReadDaliVector<Vector3>, >::Node::mTranslation))
364 .Register(*js::MakeProperty("rotation", gt::ReadQuaternion, >::Node::mRotation))
365 .Register(*js::MakeProperty("scale", gt::ReadDaliVector<Vector3>, >::Node::mScale))
366 .Register(*new js::Property<gt::Node, Matrix>("matrix", gt::ReadDaliVector<Matrix>, >::Node::SetMatrix))
367 .Register(*js::MakeProperty("camera", gt::RefReader<gt::Document>::Read<gt::Camera, >::Document::mCameras>, >::Node::mCamera))
368 .Register(*js::MakeProperty("children", js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>, >::Node::mChildren))
369 .Register(*js::MakeProperty("mesh", gt::RefReader<gt::Document>::Read<gt::Mesh, >::Document::mMeshes>, >::Node::mMesh))
370 .Register(*js::MakeProperty("skin", gt::RefReader<gt::Document>::Read<gt::Skin, >::Document::mSkins>, >::Node::mSkin)));
374 const js::Reader<gt::Animation::Sampler>& GetAnimationSamplerReader()
376 static const auto ANIMATION_SAMPLER_READER = std::move(js::Reader<gt::Animation::Sampler>()
377 .Register(*js::MakeProperty("input", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mInput))
378 .Register(*js::MakeProperty("output", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mOutput))
379 .Register(*js::MakeProperty("interpolation", gt::ReadStringEnum<gt::Animation::Sampler::Interpolation>, >::Animation::Sampler::mInterpolation)));
380 return ANIMATION_SAMPLER_READER;
383 const js::Reader<gt::Animation::Channel::Target>& GetAnimationChannelTargetReader()
385 static const auto ANIMATION_TARGET_READER = std::move(js::Reader<gt::Animation::Channel::Target>()
386 .Register(*js::MakeProperty("node", gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>, >::Animation::Channel::Target::mNode))
387 .Register(*js::MakeProperty("path", gt::ReadStringEnum<gt::Animation::Channel::Target>, >::Animation::Channel::Target::mPath)));
388 return ANIMATION_TARGET_READER;
391 const js::Reader<gt::Animation::Channel>& GetAnimationChannelReader()
393 static const auto ANIMATION_CHANNEL_READER = std::move(js::Reader<gt::Animation::Channel>()
394 .Register(*js::MakeProperty("target", js::ObjectReader<gt::Animation::Channel::Target>::Read, >::Animation::Channel::mTarget))
395 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Animation>::Read<gt::Animation::Sampler, >::Animation::mSamplers>, >::Animation::Channel::mSampler)));
396 return ANIMATION_CHANNEL_READER;
399 const js::Reader<gt::Animation>& GetAnimationReader()
401 static const auto ANIMATION_READER = std::move(js::Reader<gt::Animation>()
402 .Register(*new js::Property<gt::Animation, std::string_view>("name", js::Read::StringView, >::Animation::mName))
403 .Register(*js::MakeProperty("samplers",
404 js::Read::Array<gt::Animation::Sampler, js::ObjectReader<gt::Animation::Sampler>::Read>,
405 >::Animation::mSamplers))
406 .Register(*js::MakeProperty("channels",
407 js::Read::Array<gt::Animation::Channel, js::ObjectReader<gt::Animation::Channel>::Read>,
408 >::Animation::mChannels)));
409 return ANIMATION_READER;
412 const js::Reader<gt::Scene>& GetSceneReader()
414 static const auto SCENE_READER = std::move(js::Reader<gt::Scene>()
415 .Register(*new js::Property<gt::Scene, std::string_view>("name", js::Read::StringView, >::Scene::mName))
416 .Register(*js::MakeProperty("nodes",
417 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
418 >::Scene::mNodes)));
422 const js::Reader<gt::Document>& GetDocumentReader()
424 static const auto DOCUMENT_READER = std::move(js::Reader<gt::Document>()
425 .Register(*js::MakeProperty("buffers",
426 js::Read::Array<gt::Buffer, js::ObjectReader<gt::Buffer>::Read>,
427 >::Document::mBuffers))
428 .Register(*js::MakeProperty("bufferViews",
429 js::Read::Array<gt::BufferView, js::ObjectReader<gt::BufferView>::Read>,
430 >::Document::mBufferViews))
431 .Register(*js::MakeProperty("accessors",
432 js::Read::Array<gt::Accessor, js::ObjectReader<gt::Accessor>::Read>,
433 >::Document::mAccessors))
434 .Register(*js::MakeProperty("images",
435 js::Read::Array<gt::Image, js::ObjectReader<gt::Image>::Read>,
436 >::Document::mImages))
437 .Register(*js::MakeProperty("samplers",
438 js::Read::Array<gt::Sampler, js::ObjectReader<gt::Sampler>::Read>,
439 >::Document::mSamplers))
440 .Register(*js::MakeProperty("textures",
441 js::Read::Array<gt::Texture, js::ObjectReader<gt::Texture>::Read>,
442 >::Document::mTextures))
443 .Register(*js::MakeProperty("materials",
444 js::Read::Array<gt::Material, js::ObjectReader<gt::Material>::Read>,
445 >::Document::mMaterials))
446 .Register(*js::MakeProperty("meshes",
447 js::Read::Array<gt::Mesh, js::ObjectReader<gt::Mesh>::Read>,
448 >::Document::mMeshes))
449 .Register(*js::MakeProperty("skins",
450 js::Read::Array<gt::Skin, js::ObjectReader<gt::Skin>::Read>,
451 >::Document::mSkins))
452 .Register(*js::MakeProperty("cameras",
453 js::Read::Array<gt::Camera, js::ObjectReader<gt::Camera>::Read>,
454 >::Document::mCameras))
455 .Register(*js::MakeProperty("nodes",
456 js::Read::Array<gt::Node, js::ObjectReader<gt::Node>::Read>,
457 >::Document::mNodes))
458 .Register(*js::MakeProperty("animations",
460 >::Document::mAnimations))
461 .Register(*js::MakeProperty("scenes",
462 js::Read::Array<gt::Scene, js::ObjectReader<gt::Scene>::Read>,
463 >::Document::mScenes))
464 .Register(*js::MakeProperty("scene", gt::RefReader<gt::Document>::Read<gt::Scene, >::Document::mScenes>, >::Document::mScene)));
465 return DOCUMENT_READER;
474 bool operator<(const NodeMapping& mapping, Index gltfIdx)
476 return mapping.gltfIdx < gltfIdx;
479 class NodeIndexMapper
482 NodeIndexMapper() = default;
483 NodeIndexMapper(const NodeIndexMapper&) = delete;
484 NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
486 ///@brief Registers a mapping of the @a gltfIdx of a node to its @a runtimeIdx .
487 ///@note If the indices are the same, the registration is omitted, in order to
488 /// save growing a vector.
489 void RegisterMapping(Index gltfIdx, Index runtimeIdx)
491 if(gltfIdx != runtimeIdx)
493 auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx);
494 DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIdx != gltfIdx);
495 mNodes.insert(iInsert, NodeMapping{gltfIdx, runtimeIdx});
499 ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIdx.
500 Index GetRuntimeId(Index gltfIdx) const
502 auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx); // using custom operator<
503 return (iFind != mNodes.end() && iFind->gltfIdx == gltfIdx) ? iFind->runtimeIdx : gltfIdx;
507 std::vector<NodeMapping> mNodes;
510 struct ConversionContext
515 Index mDefaultMaterial;
517 std::vector<Index> mMeshIds;
518 NodeIndexMapper mNodeIndices;
521 void ConvertBuffer(const gt::Buffer& buffer, decltype(ResourceBundle::mBuffers)& outBuffers, const std::string& resourcePath)
523 BufferDefinition bufferDefinition;
525 bufferDefinition.mResourcePath = resourcePath;
526 bufferDefinition.mUri = buffer.mUri;
527 bufferDefinition.mByteLength = buffer.mByteLength;
529 outBuffers.emplace_back(std::move(bufferDefinition));
532 void ConvertBuffers(const gt::Document& doc, ConversionContext& context)
534 auto& outBuffers = context.mOutput.mResources.mBuffers;
535 outBuffers.reserve(doc.mBuffers.size());
537 for(auto& buffer : doc.mBuffers)
539 ConvertBuffer(buffer, outBuffers, context.mPath);
543 SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type wrapMode)
547 case gt::Wrap::REPEAT:
548 return SamplerFlags::WRAP_REPEAT;
549 case gt::Wrap::CLAMP_TO_EDGE:
550 return SamplerFlags::WRAP_CLAMP;
551 case gt::Wrap::MIRRORED_REPEAT:
552 return SamplerFlags::WRAP_MIRROR;
554 throw std::runtime_error("Invalid wrap type.");
558 SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& sampler)
562 return ((sampler->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (sampler->mMinFilter - gt::Filter::NEAREST) : ((sampler->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
563 ((sampler->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
564 (ConvertWrapMode(sampler->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
565 (ConvertWrapMode(sampler->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
569 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texturesampler
570 // "The index of the sampler used by this texture. When undefined, a sampler with repeat wrapping and auto filtering should be used."
571 // "What is an auto filtering", I hear you ask. Since there's nothing else to determine mipmapping from - including glTF image
572 // properties, if not in some extension -, we will simply assume linear filtering.
573 return SamplerFlags::FILTER_LINEAR | (SamplerFlags::FILTER_LINEAR << SamplerFlags::FILTER_MAG_SHIFT) |
574 (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_S_SHIFT) | (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_T_SHIFT);
578 TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm, ConversionContext& context, const ImageMetadata& metaData = ImageMetadata())
580 TextureDefinition textureDefinition;
581 std::string uri = std::string(mm.mTexture->mSource->mUri);
584 uint32_t bufferIndex = mm.mTexture->mSource->mBufferView->mBuffer.GetIndex();
585 if(bufferIndex != INVALID_INDEX && context.mOutput.mResources.mBuffers[bufferIndex].IsAvailable())
587 auto& stream = context.mOutput.mResources.mBuffers[bufferIndex].GetBufferStream();
589 stream.seekg(mm.mTexture->mSource->mBufferView->mByteOffset, stream.beg);
590 std::vector<uint8_t> dataBuffer;
591 dataBuffer.resize(mm.mTexture->mSource->mBufferView->mByteLength);
592 stream.read(reinterpret_cast<char*>(dataBuffer.data()), static_cast<std::streamsize>(static_cast<size_t>(mm.mTexture->mSource->mBufferView->mByteLength)));
593 return TextureDefinition{std::move(dataBuffer), ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
595 return TextureDefinition();
599 return TextureDefinition{uri, ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
603 void ConvertMaterial(const gt::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials, ConversionContext& context)
605 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([&]()
966 std::unique_ptr<NodeDefinition> nodeDef{new NodeDefinition()};
968 nodeDef->mParentIdx = parentIdx;
969 nodeDef->mName = node.mName;
970 if(nodeDef->mName.empty())
972 // TODO: Production quality generation of unique names.
973 nodeDef->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDef.get()));
976 if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
978 nodeDef->mPosition = node.mTranslation;
979 nodeDef->mOrientation = node.mRotation;
980 nodeDef->mScale = node.mScale;
982 if(isMRendererModel && node.mName == ROOT_NODE_NAME && node.mScale == SCALE_TO_ADJUST)
984 nodeDef->mScale *= 0.01f;
988 return nodeDef; }());
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 Gltf2LoaderImpl::InitializeGltfLoader()
1429 static Dali::Mutex mInitializeMutex;
1430 // Set ObjectReader only once (for all gltf loading).
1431 static bool setObjectReadersRequired = true;
1433 Mutex::ScopedLock lock(mInitializeMutex);
1434 if(setObjectReadersRequired)
1436 // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1438 setObjectReadersRequired = false;
1443 bool Gltf2LoaderImpl::LoadModel(const std::string& url, Dali::Scene3D::Loader::LoadResult& result)
1445 bool failed = false;
1446 auto js = LoadTextFile(url.c_str(), &failed);
1449 DALI_LOG_ERROR("Failed to load %s\n", url.c_str());
1453 json::unique_ptr root(json_parse(js.c_str(), js.size()));
1456 DALI_LOG_ERROR("Failed to parse %s\n", url.c_str());
1462 Dali::Scene3D::Loader::ShaderDefinitionFactory shaderFactory;
1463 shaderFactory.SetResources(result.mResources);
1465 auto& rootObj = js::Cast<json_object_s>(*root);
1466 auto jsAsset = js::FindObjectChild("asset", rootObj);
1468 auto jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsAsset));
1471 doc.mAsset.mVersion = js::Read::StringView(*jsAssetVersion);
1474 bool isMRendererModel(false);
1475 auto jsAssetGenerator = js::FindObjectChild("generator", js::Cast<json_object_s>(*jsAsset));
1476 if(jsAssetGenerator)
1478 doc.mAsset.mGenerator = js::Read::StringView(*jsAssetGenerator);
1479 isMRendererModel = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
1482 InitializeGltfLoader();
1484 static Dali::Mutex mReadMutex;
1485 Mutex::ScopedLock lock(mReadMutex);
1486 gt::SetRefReaderObject(doc);
1487 GetDocumentReader().Read(rootObj, doc);
1490 auto path = url.substr(0, url.rfind('/') + 1);
1491 ConversionContext context{result, path, INVALID_INDEX};
1493 ConvertBuffers(doc, context);
1494 ConvertMaterials(doc, context);
1495 ConvertMeshes(doc, context);
1496 ConvertNodes(doc, context, isMRendererModel);
1497 ConvertAnimations(doc, context);
1498 ProcessSkins(doc, context);
1499 ProduceShaders(shaderFactory, result.mScene);
1500 result.mScene.EnsureUniqueSkinningShaderInstances(result.mResources);
1502 // Set Default Environment map
1503 SetDefaultEnvironmentMap(doc, context);
1508 } // namespace Internal
1509 } // namespace Loader
1510 } // namespace Scene3D