2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/images/image-operations.h>
24 #include <dali/public-api/math/quaternion.h>
25 #include <dali/devel-api/threading/mutex.h>
29 #include <dali-scene3d/internal/loader/gltf2-asset.h>
30 #include <dali-scene3d/public-api/loader/load-result.h>
31 #include <dali-scene3d/public-api/loader/resource-bundle.h>
32 #include <dali-scene3d/public-api/loader/scene-definition.h>
33 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
34 #include <dali-scene3d/public-api/loader/utils.h>
47 Dali::Mutex gInitializeMutex;
48 Dali::Mutex gReadMutex;
50 const std::string POSITION_PROPERTY("position");
51 const std::string ORIENTATION_PROPERTY("orientation");
52 const std::string SCALE_PROPERTY("scale");
53 const std::string BLEND_SHAPE_WEIGHTS_UNIFORM("uBlendShapeWeight");
54 const std::string MRENDERER_MODEL_IDENTIFICATION("M-Renderer");
55 const std::string ROOT_NODE_NAME("RootNode");
56 const Vector3 SCALE_TO_ADJUST(100.0f, 100.0f, 100.0f);
58 const Geometry::Type GLTF2_TO_DALI_PRIMITIVES[]{
64 Geometry::TRIANGLE_STRIP,
65 Geometry::TRIANGLE_FAN}; //...because Dali swaps the last two.
67 struct AttributeMapping
69 gt::Attribute::Type mType;
70 MeshDefinition::Accessor MeshDefinition::*mAccessor;
71 uint16_t mElementSizeRequired;
72 } ATTRIBUTE_MAPPINGS[]{
73 {gt::Attribute::NORMAL, &MeshDefinition::mNormals, sizeof(Vector3)},
74 {gt::Attribute::TANGENT, &MeshDefinition::mTangents, sizeof(Vector3)},
75 {gt::Attribute::TEXCOORD_0, &MeshDefinition::mTexCoords, sizeof(Vector2)},
76 {gt::Attribute::COLOR_0, &MeshDefinition::mColors, sizeof(Vector4)},
77 {gt::Attribute::JOINTS_0, &MeshDefinition::mJoints0, sizeof(Vector4)},
78 {gt::Attribute::WEIGHTS_0, &MeshDefinition::mWeights0, sizeof(Vector4)},
81 std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
83 auto results = js::Read::Array<gt::Animation, js::ObjectReader<gt::Animation>::Read>(j);
85 for(auto& animation : results)
87 for(auto& channel : animation.mChannels)
89 channel.mSampler.UpdateVector(animation.mSamplers);
96 void ApplyAccessorMinMax(const gt::Accessor& acc, float* values)
98 DALI_ASSERT_ALWAYS(acc.mMax.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMax.size());
99 DALI_ASSERT_ALWAYS(acc.mMin.empty() || gt::AccessorType::ElementCount(acc.mType) == acc.mMin.size());
100 MeshDefinition::Blob::ApplyMinMax(acc.mMin, acc.mMax, acc.mCount, values);
103 const auto BUFFER_READER = std::move(js::Reader<gt::Buffer>()
104 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::Buffer::mByteLength))
105 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Buffer::mUri)));
107 const auto BUFFER_VIEW_READER = std::move(js::Reader<gt::BufferView>()
108 .Register(*js::MakeProperty("buffer", gt::RefReader<gt::Document>::Read<gt::Buffer, >::Document::mBuffers>, >::BufferView::mBuffer))
109 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferView::mByteOffset))
110 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::BufferView::mByteLength))
111 .Register(*js::MakeProperty("byteStride", js::Read::Number<uint32_t>, >::BufferView::mByteStride))
112 .Register(*js::MakeProperty("target", js::Read::Number<uint32_t>, >::BufferView::mTarget)));
114 const auto BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::BufferViewClient>()
115 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::BufferViewClient::mBufferView))
116 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferViewClient::mByteOffset)));
118 const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::ComponentTypedBufferViewClient>()
119 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, gt::Ref<gt::BufferView>>("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::ComponentTypedBufferViewClient::mBufferView))
120 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, uint32_t>("byteOffset", js::Read::Number<uint32_t>, >::ComponentTypedBufferViewClient::mByteOffset))
121 .Register(*js::MakeProperty("componentType", js::Read::Enum<gt::Component::Type>, >::ComponentTypedBufferViewClient::mComponentType)));
123 const auto ACCESSOR_SPARSE_READER = std::move(js::Reader<gt::Accessor::Sparse>()
124 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::Sparse::mCount))
125 .Register(*js::MakeProperty("indices", js::ObjectReader<gt::ComponentTypedBufferViewClient>::Read, >::Accessor::Sparse::mIndices))
126 .Register(*js::MakeProperty("values", js::ObjectReader<gt::BufferViewClient>::Read, >::Accessor::Sparse::mValues)));
128 const auto ACCESSOR_READER = std::move(js::Reader<gt::Accessor>()
129 .Register(*new js::Property<gt::Accessor, gt::Ref<gt::BufferView>>("bufferView",
130 gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>,
131 >::Accessor::mBufferView))
132 .Register(*new js::Property<gt::Accessor, uint32_t>("byteOffset",
133 js::Read::Number<uint32_t>,
134 >::Accessor::mByteOffset))
135 .Register(*new js::Property<gt::Accessor, gt::Component::Type>("componentType",
136 js::Read::Enum<gt::Component::Type>,
137 >::Accessor::mComponentType))
138 .Register(*new js::Property<gt::Accessor, std::string_view>("name", js::Read::StringView, >::Accessor::mName))
139 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::mCount))
140 .Register(*js::MakeProperty("normalized", js::Read::Boolean, >::Accessor::mNormalized))
141 .Register(*js::MakeProperty("type", gt::ReadStringEnum<gt::AccessorType>, >::Accessor::mType))
142 .Register(*js::MakeProperty("min", js::Read::Array<float, js::Read::Number>, >::Accessor::mMin))
143 .Register(*js::MakeProperty("max", js::Read::Array<float, js::Read::Number>, >::Accessor::mMax))
144 .Register(*new js::Property<gt::Accessor, gt::Accessor::Sparse>("sparse", js::ObjectReader<gt::Accessor::Sparse>::Read, >::Accessor::SetSparse)));
146 const auto IMAGE_READER = std::move(js::Reader<gt::Image>()
147 .Register(*new js::Property<gt::Image, std::string_view>("name", js::Read::StringView, >::Material::mName))
148 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Image::mUri))
149 .Register(*js::MakeProperty("mimeType", js::Read::StringView, >::Image::mMimeType))
150 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::Image::mBufferView)));
152 const auto SAMPLER_READER = std::move(js::Reader<gt::Sampler>()
153 .Register(*js::MakeProperty("minFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMinFilter))
154 .Register(*js::MakeProperty("magFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMagFilter))
155 .Register(*js::MakeProperty("wrapS", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapS))
156 .Register(*js::MakeProperty("wrapT", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapT)));
158 const auto TEXURE_READER = std::move(js::Reader<gt::Texture>()
159 .Register(*js::MakeProperty("source", gt::RefReader<gt::Document>::Read<gt::Image, >::Document::mImages>, >::Texture::mSource))
160 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Document>::Read<gt::Sampler, >::Document::mSamplers>, >::Texture::mSampler)));
162 const auto TEXURE_INFO_READER = std::move(js::Reader<gt::TextureInfo>()
163 .Register(*js::MakeProperty("index", gt::RefReader<gt::Document>::Read<gt::Texture, >::Document::mTextures>, >::TextureInfo::mTexture))
164 .Register(*js::MakeProperty("texCoord", js::Read::Number<uint32_t>, >::TextureInfo::mTexCoord))
165 .Register(*js::MakeProperty("scale", js::Read::Number<float>, >::TextureInfo::mScale))
166 .Register(*js::MakeProperty("strength", js::Read::Number<float>, >::TextureInfo::mStrength)));
168 const auto MATERIAL_PBR_READER = std::move(js::Reader<gt::Material::Pbr>()
169 .Register(*js::MakeProperty("baseColorFactor", gt::ReadDaliVector<Vector4>, >::Material::Pbr::mBaseColorFactor))
170 .Register(*js::MakeProperty("baseColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mBaseColorTexture))
171 .Register(*js::MakeProperty("metallicFactor", js::Read::Number<float>, >::Material::Pbr::mMetallicFactor))
172 .Register(*js::MakeProperty("roughnessFactor", js::Read::Number<float>, >::Material::Pbr::mRoughnessFactor))
173 .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mMetallicRoughnessTexture)));
175 const auto MATERIAL_SPECULAR_READER = std::move(js::Reader<gt::MaterialSpecular>()
176 .Register(*js::MakeProperty("specularFactor", js::Read::Number<float>, >::MaterialSpecular::mSpecularFactor))
177 .Register(*js::MakeProperty("specularTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularTexture))
178 .Register(*js::MakeProperty("specularColorFactor", gt::ReadDaliVector<Vector3>, >::MaterialSpecular::mSpecularColorFactor))
179 .Register(*js::MakeProperty("specularColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularColorTexture)));
181 const auto MATERIAL_IOR_READER = std::move(js::Reader<gt::MaterialIor>()
182 .Register(*js::MakeProperty("ior", js::Read::Number<float>, >::MaterialIor::mIor)));
184 const auto MATERIAL_EXTENSION_READER = std::move(js::Reader<gt::MaterialExtensions>()
185 .Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader<gt::MaterialIor>::Read, >::MaterialExtensions::mMaterialIor))
186 .Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader<gt::MaterialSpecular>::Read, >::MaterialExtensions::mMaterialSpecular)));
188 const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
189 .Register(*new js::Property<gt::Material, std::string_view>("name", js::Read::StringView, >::Material::mName))
190 .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader<gt::Material::Pbr>::Read, >::Material::mPbrMetallicRoughness))
191 .Register(*js::MakeProperty("normalTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mNormalTexture))
192 .Register(*js::MakeProperty("occlusionTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mOcclusionTexture))
193 .Register(*js::MakeProperty("emissiveTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mEmissiveTexture))
194 .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector<Vector3>, >::Material::mEmissiveFactor))
195 .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum<gt::AlphaMode>, >::Material::mAlphaMode))
196 .Register(*js::MakeProperty("alphaCutoff", js::Read::Number<float>, >::Material::mAlphaCutoff))
197 .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, >::Material::mDoubleSided))
198 .Register(*js::MakeProperty("extensions", js::ObjectReader<gt::MaterialExtensions>::Read, >::Material::mMaterialExtensions)));
200 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
202 auto& jo = js::Cast<json_object_s>(j);
203 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> result;
208 auto jstr = *i->name;
209 result[gt::Attribute::FromString(jstr.string, jstr.string_size)] = gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>(*i->value);
215 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> ReadMeshPrimitiveTargets(const json_value_s& j)
217 auto& jo = js::Cast<json_array_s>(j);
218 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> result;
220 result.reserve(jo.length);
225 result.push_back(std::move(ReadMeshPrimitiveAttributes(*i->value)));
232 const auto MESH_PRIMITIVE_READER = std::move(js::Reader<gt::Mesh::Primitive>()
233 .Register(*js::MakeProperty("attributes", ReadMeshPrimitiveAttributes, >::Mesh::Primitive::mAttributes))
234 .Register(*js::MakeProperty("indices", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Mesh::Primitive::mIndices))
235 .Register(*js::MakeProperty("material", gt::RefReader<gt::Document>::Read<gt::Material, >::Document::mMaterials>, >::Mesh::Primitive::mMaterial))
236 .Register(*js::MakeProperty("mode", js::Read::Enum<gt::Mesh::Primitive::Mode>, >::Mesh::Primitive::mMode))
237 .Register(*js::MakeProperty("targets", ReadMeshPrimitiveTargets, >::Mesh::Primitive::mTargets)));
239 const auto MESH_READER = std::move(js::Reader<gt::Mesh>()
240 .Register(*new js::Property<gt::Mesh, std::string_view>("name", js::Read::StringView, >::Mesh::mName))
241 .Register(*js::MakeProperty("primitives",
242 js::Read::Array<gt::Mesh::Primitive, js::ObjectReader<gt::Mesh::Primitive>::Read>,
243 >::Mesh::mPrimitives))
244 .Register(*js::MakeProperty("weights", js::Read::Array<float, js::Read::Number>, >::Mesh::mWeights)));
246 const auto SKIN_READER = std::move(js::Reader<gt::Skin>()
247 .Register(*new js::Property<gt::Skin, std::string_view>("name", js::Read::StringView, >::Skin::mName))
248 .Register(*js::MakeProperty("inverseBindMatrices",
249 gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>,
250 >::Skin::mInverseBindMatrices))
251 .Register(*js::MakeProperty("skeleton",
252 gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>,
253 >::Skin::mSkeleton))
254 .Register(*js::MakeProperty("joints",
255 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
256 >::Skin::mJoints)));
258 const auto CAMERA_PERSPECTIVE_READER = std::move(js::Reader<gt::Camera::Perspective>()
259 .Register(*js::MakeProperty("aspectRatio", js::Read::Number<float>, >::Camera::Perspective::mAspectRatio))
260 .Register(*js::MakeProperty("yfov", js::Read::Number<float>, >::Camera::Perspective::mYFov))
261 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Perspective::mZFar))
262 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted
264 const auto CAMERA_ORTHOGRAPHIC_READER = std::move(js::Reader<gt::Camera::Orthographic>()
265 .Register(*js::MakeProperty("xmag", js::Read::Number<float>, >::Camera::Orthographic::mXMag))
266 .Register(*js::MakeProperty("ymag", js::Read::Number<float>, >::Camera::Orthographic::mXMag))
267 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Orthographic::mZFar))
268 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Orthographic::mZNear)));
270 const auto CAMERA_READER = std::move(js::Reader<gt::Camera>()
271 .Register(*new js::Property<gt::Camera, std::string_view>("name", js::Read::StringView, >::Camera::mName))
272 .Register(*js::MakeProperty("type", js::Read::StringView, >::Camera::mType))
273 .Register(*js::MakeProperty("perspective", js::ObjectReader<gt::Camera::Perspective>::Read, >::Camera::mPerspective))
274 .Register(*js::MakeProperty("orthographic", js::ObjectReader<gt::Camera::Orthographic>::Read, >::Camera::mOrthographic)));
276 const auto NODE_READER = std::move(js::Reader<gt::Node>()
277 .Register(*new js::Property<gt::Node, std::string_view>("name", js::Read::StringView, >::Node::mName))
278 .Register(*js::MakeProperty("translation", gt::ReadDaliVector<Vector3>, >::Node::mTranslation))
279 .Register(*js::MakeProperty("rotation", gt::ReadQuaternion, >::Node::mRotation))
280 .Register(*js::MakeProperty("scale", gt::ReadDaliVector<Vector3>, >::Node::mScale))
281 .Register(*new js::Property<gt::Node, Matrix>("matrix", gt::ReadDaliVector<Matrix>, >::Node::SetMatrix))
282 .Register(*js::MakeProperty("camera", gt::RefReader<gt::Document>::Read<gt::Camera, >::Document::mCameras>, >::Node::mCamera))
283 .Register(*js::MakeProperty("children", js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>, >::Node::mChildren))
284 .Register(*js::MakeProperty("mesh", gt::RefReader<gt::Document>::Read<gt::Mesh, >::Document::mMeshes>, >::Node::mMesh))
285 .Register(*js::MakeProperty("skin", gt::RefReader<gt::Document>::Read<gt::Skin, >::Document::mSkins>, >::Node::mSkin)));
287 const auto ANIMATION_SAMPLER_READER = std::move(js::Reader<gt::Animation::Sampler>()
288 .Register(*js::MakeProperty("input", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mInput))
289 .Register(*js::MakeProperty("output", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mOutput))
290 .Register(*js::MakeProperty("interpolation", gt::ReadStringEnum<gt::Animation::Sampler::Interpolation>, >::Animation::Sampler::mInterpolation)));
292 const auto ANIMATION_TARGET_READER = std::move(js::Reader<gt::Animation::Channel::Target>()
293 .Register(*js::MakeProperty("node", gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>, >::Animation::Channel::Target::mNode))
294 .Register(*js::MakeProperty("path", gt::ReadStringEnum<gt::Animation::Channel::Target>, >::Animation::Channel::Target::mPath)));
296 const auto ANIMATION_CHANNEL_READER = std::move(js::Reader<gt::Animation::Channel>()
297 .Register(*js::MakeProperty("target", js::ObjectReader<gt::Animation::Channel::Target>::Read, >::Animation::Channel::mTarget))
298 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Animation>::Read<gt::Animation::Sampler, >::Animation::mSamplers>, >::Animation::Channel::mSampler)));
300 const auto ANIMATION_READER = std::move(js::Reader<gt::Animation>()
301 .Register(*new js::Property<gt::Animation, std::string_view>("name", js::Read::StringView, >::Animation::mName))
302 .Register(*js::MakeProperty("samplers",
303 js::Read::Array<gt::Animation::Sampler, js::ObjectReader<gt::Animation::Sampler>::Read>,
304 >::Animation::mSamplers))
305 .Register(*js::MakeProperty("channels",
306 js::Read::Array<gt::Animation::Channel, js::ObjectReader<gt::Animation::Channel>::Read>,
307 >::Animation::mChannels)));
309 const auto SCENE_READER = std::move(js::Reader<gt::Scene>()
310 .Register(*new js::Property<gt::Scene, std::string_view>("name", js::Read::StringView, >::Scene::mName))
311 .Register(*js::MakeProperty("nodes",
312 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
313 >::Scene::mNodes)));
315 const auto DOCUMENT_READER = std::move(js::Reader<gt::Document>()
316 .Register(*js::MakeProperty("buffers",
317 js::Read::Array<gt::Buffer, js::ObjectReader<gt::Buffer>::Read>,
318 >::Document::mBuffers))
319 .Register(*js::MakeProperty("bufferViews",
320 js::Read::Array<gt::BufferView, js::ObjectReader<gt::BufferView>::Read>,
321 >::Document::mBufferViews))
322 .Register(*js::MakeProperty("accessors",
323 js::Read::Array<gt::Accessor, js::ObjectReader<gt::Accessor>::Read>,
324 >::Document::mAccessors))
325 .Register(*js::MakeProperty("images",
326 js::Read::Array<gt::Image, js::ObjectReader<gt::Image>::Read>,
327 >::Document::mImages))
328 .Register(*js::MakeProperty("samplers",
329 js::Read::Array<gt::Sampler, js::ObjectReader<gt::Sampler>::Read>,
330 >::Document::mSamplers))
331 .Register(*js::MakeProperty("textures",
332 js::Read::Array<gt::Texture, js::ObjectReader<gt::Texture>::Read>,
333 >::Document::mTextures))
334 .Register(*js::MakeProperty("materials",
335 js::Read::Array<gt::Material, js::ObjectReader<gt::Material>::Read>,
336 >::Document::mMaterials))
337 .Register(*js::MakeProperty("meshes",
338 js::Read::Array<gt::Mesh, js::ObjectReader<gt::Mesh>::Read>,
339 >::Document::mMeshes))
340 .Register(*js::MakeProperty("skins",
341 js::Read::Array<gt::Skin, js::ObjectReader<gt::Skin>::Read>,
342 >::Document::mSkins))
343 .Register(*js::MakeProperty("cameras",
344 js::Read::Array<gt::Camera, js::ObjectReader<gt::Camera>::Read>,
345 >::Document::mCameras))
346 .Register(*js::MakeProperty("nodes",
347 js::Read::Array<gt::Node, js::ObjectReader<gt::Node>::Read>,
348 >::Document::mNodes))
349 .Register(*js::MakeProperty("animations",
351 >::Document::mAnimations))
352 .Register(*js::MakeProperty("scenes",
353 js::Read::Array<gt::Scene, js::ObjectReader<gt::Scene>::Read>,
354 >::Document::mScenes))
355 .Register(*js::MakeProperty("scene", gt::RefReader<gt::Document>::Read<gt::Scene, >::Document::mScenes>, >::Document::mScene)));
363 bool operator<(const NodeMapping& mapping, Index gltfIdx)
365 return mapping.gltfIdx < gltfIdx;
368 class NodeIndexMapper
371 NodeIndexMapper() = default;
372 NodeIndexMapper(const NodeIndexMapper&) = delete;
373 NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
375 ///@brief Registers a mapping of the @a gltfIdx of a node to its @a runtimeIdx .
376 ///@note If the indices are the same, the registration is omitted, in order to
377 /// save growing a vector.
378 void RegisterMapping(Index gltfIdx, Index runtimeIdx)
380 if(gltfIdx != runtimeIdx)
382 auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx);
383 DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIdx != gltfIdx);
384 mNodes.insert(iInsert, NodeMapping{gltfIdx, runtimeIdx});
388 ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIdx.
389 Index GetRuntimeId(Index gltfIdx) const
391 auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx); // using custom operator<
392 return (iFind != mNodes.end() && iFind->gltfIdx == gltfIdx) ? iFind->runtimeIdx : gltfIdx;
396 std::vector<NodeMapping> mNodes;
399 struct ConversionContext
404 Index mDefaultMaterial;
406 std::vector<Index> mMeshIds;
407 NodeIndexMapper mNodeIndices;
410 void ConvertBuffer(const gt::Buffer& buffer, decltype(ResourceBundle::mBuffers)& outBuffers, const std::string& resourcePath)
412 BufferDefinition bufferDefinition;
414 bufferDefinition.mResourcePath = resourcePath;
415 bufferDefinition.mUri = buffer.mUri;
416 bufferDefinition.mByteLength = buffer.mByteLength;
418 outBuffers.emplace_back(std::move(bufferDefinition));
421 void ConvertBuffers(const gt::Document& doc, ConversionContext& context)
423 auto& outBuffers = context.mOutput.mResources.mBuffers;
424 outBuffers.reserve(doc.mBuffers.size());
426 for(auto& buffer : doc.mBuffers)
428 ConvertBuffer(buffer, outBuffers, context.mPath);
432 SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type wrapMode)
436 case gt::Wrap::REPEAT:
437 return SamplerFlags::WRAP_REPEAT;
438 case gt::Wrap::CLAMP_TO_EDGE:
439 return SamplerFlags::WRAP_CLAMP;
440 case gt::Wrap::MIRRORED_REPEAT:
441 return SamplerFlags::WRAP_MIRROR;
443 throw std::runtime_error("Invalid wrap type.");
447 SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& sampler)
451 return ((sampler->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (sampler->mMinFilter - gt::Filter::NEAREST) : ((sampler->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
452 ((sampler->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
453 (ConvertWrapMode(sampler->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
454 (ConvertWrapMode(sampler->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
458 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texturesampler
459 // "The index of the sampler used by this texture. When undefined, a sampler with repeat wrapping and auto filtering should be used."
460 // "What is an auto filtering", I hear you ask. Since there's nothing else to determine mipmapping from - including glTF image
461 // properties, if not in some extension -, we will simply assume linear filtering.
462 return SamplerFlags::FILTER_LINEAR | (SamplerFlags::FILTER_LINEAR << SamplerFlags::FILTER_MAG_SHIFT) |
463 (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_S_SHIFT) | (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_T_SHIFT);
467 TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm, ConversionContext& context, const ImageMetadata& metaData = ImageMetadata())
469 TextureDefinition textureDefinition;
470 std::string uri = std::string(mm.mTexture->mSource->mUri);
473 uint32_t bufferIndex = mm.mTexture->mSource->mBufferView->mBuffer.GetIndex();
474 if(bufferIndex != INVALID_INDEX && context.mOutput.mResources.mBuffers[bufferIndex].IsAvailable())
476 auto& stream = context.mOutput.mResources.mBuffers[bufferIndex].GetBufferStream();
478 stream.seekg(mm.mTexture->mSource->mBufferView->mByteOffset, stream.beg);
479 std::vector<uint8_t> dataBuffer;
480 dataBuffer.resize(mm.mTexture->mSource->mBufferView->mByteLength);
481 stream.read(reinterpret_cast<char*>(dataBuffer.data()), mm.mTexture->mSource->mBufferView->mByteLength);
482 return TextureDefinition{std::move(dataBuffer), ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
484 return TextureDefinition();
488 return TextureDefinition{uri, ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
492 void ConvertMaterial(const gt::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials, ConversionContext& context)
494 auto getTextureMetaData = [](const std::unordered_map<std::string, ImageMetadata>& metaData, const gt::TextureInfo& info) {
495 if(!info.mTexture->mSource->mUri.empty())
497 if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end())
499 return search->second;
502 return ImageMetadata();
505 MaterialDefinition matDef;
507 auto& pbr = material.mPbrMetallicRoughness;
508 if(material.mAlphaMode == gt::AlphaMode::BLEND)
510 matDef.mIsOpaque = false;
511 matDef.mFlags |= MaterialDefinition::TRANSPARENCY;
513 else if(material.mAlphaMode == gt::AlphaMode::MASK)
515 matDef.mIsMask = true;
516 matDef.SetAlphaCutoff(std::min(1.f, std::max(0.f, material.mAlphaCutoff)));
519 matDef.mBaseColorFactor = pbr.mBaseColorFactor;
521 matDef.mTextureStages.reserve(!!pbr.mBaseColorTexture + !!pbr.mMetallicRoughnessTexture + !!material.mNormalTexture + !!material.mOcclusionTexture + !!material.mEmissiveTexture);
522 if(pbr.mBaseColorTexture)
524 const auto semantic = MaterialDefinition::ALBEDO;
525 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture, context, getTextureMetaData(imageMetaData, pbr.mBaseColorTexture))});
526 // TODO: and there had better be one
527 matDef.mFlags |= semantic;
531 matDef.mNeedAlbedoTexture = false;
534 matDef.mMetallic = pbr.mMetallicFactor;
535 matDef.mRoughness = pbr.mRoughnessFactor;
537 if(pbr.mMetallicRoughnessTexture)
539 const auto semantic = MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
540 MaterialDefinition::GLTF_CHANNELS;
541 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture, context, getTextureMetaData(imageMetaData, pbr.mMetallicRoughnessTexture))});
542 // TODO: and there had better be one
543 matDef.mFlags |= semantic;
547 matDef.mNeedMetallicRoughnessTexture = false;
550 matDef.mNormalScale = material.mNormalTexture.mScale;
551 if(material.mNormalTexture)
553 const auto semantic = MaterialDefinition::NORMAL;
554 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture, context, getTextureMetaData(imageMetaData, material.mNormalTexture))});
555 // TODO: and there had better be one
556 matDef.mFlags |= semantic;
560 matDef.mNeedNormalTexture = false;
563 if(material.mOcclusionTexture)
565 const auto semantic = MaterialDefinition::OCCLUSION;
566 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture, context, getTextureMetaData(imageMetaData, material.mOcclusionTexture))});
567 // TODO: and there had better be one
568 matDef.mFlags |= semantic;
569 matDef.mOcclusionStrength = material.mOcclusionTexture.mStrength;
572 if(material.mEmissiveTexture)
574 const auto semantic = MaterialDefinition::EMISSIVE;
575 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture, context, getTextureMetaData(imageMetaData, material.mEmissiveTexture))});
576 // TODO: and there had better be one
577 matDef.mFlags |= semantic;
578 matDef.mEmissiveFactor = material.mEmissiveFactor;
581 if(material.mMaterialExtensions.mMaterialIor.mIor < MAXFLOAT)
583 float ior = material.mMaterialExtensions.mMaterialIor.mIor;
584 matDef.mDielectricSpecular = powf((ior - 1.0f) / (ior + 1.0f), 2.0f);
586 matDef.mSpecularFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularFactor;
587 matDef.mSpecularColorFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor;
589 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)
591 const auto semantic = MaterialDefinition::SPECULAR;
592 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture))});
593 matDef.mFlags |= semantic;
596 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)
598 const auto semantic = MaterialDefinition::SPECULAR_COLOR;
599 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture, context, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture))});
600 matDef.mFlags |= semantic;
603 matDef.mDoubleSided = material.mDoubleSided;
605 outMaterials.emplace_back(std::move(matDef), TextureSet());
608 void ConvertMaterials(const gt::Document& doc, ConversionContext& context)
610 auto& imageMetaData = context.mOutput.mSceneMetadata.mImageMetadata;
612 auto& outMaterials = context.mOutput.mResources.mMaterials;
613 outMaterials.reserve(doc.mMaterials.size());
615 for(auto& m : doc.mMaterials)
617 ConvertMaterial(m, imageMetaData, outMaterials, context);
621 MeshDefinition::Accessor ConvertMeshPrimitiveAccessor(const gt::Accessor& acc)
623 DALI_ASSERT_ALWAYS((acc.mBufferView &&
624 (acc.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) ||
625 (acc.mSparse && !acc.mBufferView));
627 DALI_ASSERT_ALWAYS(!acc.mSparse ||
628 ((acc.mSparse->mIndices.mBufferView && (acc.mSparse->mIndices.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) &&
629 (acc.mSparse->mValues.mBufferView && (acc.mSparse->mValues.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max()))));
631 MeshDefinition::SparseBlob sparseBlob;
634 const gt::Accessor::Sparse& sparse = *acc.mSparse;
635 const gt::ComponentTypedBufferViewClient& indices = sparse.mIndices;
636 const gt::BufferViewClient& values = sparse.mValues;
638 MeshDefinition::Blob indicesBlob(
639 indices.mBufferView->mByteOffset + indices.mByteOffset,
640 sparse.mCount * indices.GetBytesPerComponent(),
641 static_cast<uint16_t>(indices.mBufferView->mByteStride),
642 static_cast<uint16_t>(indices.GetBytesPerComponent()),
645 MeshDefinition::Blob valuesBlob(
646 values.mBufferView->mByteOffset + values.mByteOffset,
647 sparse.mCount * acc.GetElementSizeBytes(),
648 static_cast<uint16_t>(values.mBufferView->mByteStride),
649 static_cast<uint16_t>(acc.GetElementSizeBytes()),
653 sparseBlob = std::move(MeshDefinition::SparseBlob(std::move(indicesBlob), std::move(valuesBlob), acc.mSparse->mCount));
656 uint32_t bufferViewOffset = 0u;
657 uint32_t bufferViewStride = 0u;
660 bufferViewOffset = acc.mBufferView->mByteOffset;
661 bufferViewStride = acc.mBufferView->mByteStride;
664 return MeshDefinition::Accessor{
665 std::move(MeshDefinition::Blob{bufferViewOffset + acc.mByteOffset,
666 acc.GetBytesLength(),
667 static_cast<uint16_t>(bufferViewStride),
668 static_cast<uint16_t>(acc.GetElementSizeBytes()),
671 std::move(sparseBlob),
672 acc.mBufferView ? acc.mBufferView->mBuffer.GetIndex() : 0};
675 void ConvertMeshes(const gt::Document& doc, ConversionContext& context)
677 uint32_t meshCount = 0;
678 context.mMeshIds.reserve(doc.mMeshes.size());
679 for(auto& mesh : doc.mMeshes)
681 context.mMeshIds.push_back(meshCount);
682 meshCount += mesh.mPrimitives.size();
685 auto& outMeshes = context.mOutput.mResources.mMeshes;
686 outMeshes.reserve(meshCount);
687 for(auto& mesh : doc.mMeshes)
689 for(auto& primitive : mesh.mPrimitives)
691 MeshDefinition meshDefinition;
693 auto& attribs = primitive.mAttributes;
694 meshDefinition.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[primitive.mMode];
696 auto& accPositions = *attribs.find(gt::Attribute::POSITION)->second;
697 meshDefinition.mPositions = ConvertMeshPrimitiveAccessor(accPositions);
698 // glTF2 support vector4 tangent for mesh.
699 // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#meshes-overview
700 meshDefinition.mTangentType = Property::VECTOR4;
702 const bool needNormalsTangents = accPositions.mType == gt::AccessorType::VEC3;
703 for(auto& am : ATTRIBUTE_MAPPINGS)
705 auto iFind = attribs.find(am.mType);
706 if(iFind != attribs.end())
708 auto& accessor = meshDefinition.*(am.mAccessor);
709 accessor = ConvertMeshPrimitiveAccessor(*iFind->second);
711 if(iFind->first == gt::Attribute::JOINTS_0)
713 meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
714 meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_JOINT_IDS;
715 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
718 else if(needNormalsTangents)
722 case gt::Attribute::NORMAL:
723 meshDefinition.RequestNormals();
726 case gt::Attribute::TANGENT:
727 meshDefinition.RequestTangents();
736 if(primitive.mIndices)
738 meshDefinition.mIndices = ConvertMeshPrimitiveAccessor(*primitive.mIndices);
739 meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
740 meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_INDICES;
741 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_INDICES) || primitive.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
744 if(!primitive.mTargets.empty())
746 meshDefinition.mBlendShapes.reserve(primitive.mTargets.size());
747 meshDefinition.mBlendShapeVersion = BlendShapes::Version::VERSION_2_0;
748 for(const auto& target : primitive.mTargets)
750 MeshDefinition::BlendShape blendShape;
752 auto endIt = target.end();
753 auto it = target.find(gt::Attribute::POSITION);
756 blendShape.deltas = ConvertMeshPrimitiveAccessor(*it->second);
758 it = target.find(gt::Attribute::NORMAL);
761 blendShape.normals = ConvertMeshPrimitiveAccessor(*it->second);
763 it = target.find(gt::Attribute::TANGENT);
766 blendShape.tangents = ConvertMeshPrimitiveAccessor(*it->second);
769 if(!mesh.mWeights.empty())
771 blendShape.weight = mesh.mWeights[meshDefinition.mBlendShapes.size()];
774 meshDefinition.mBlendShapes.push_back(std::move(blendShape));
778 outMeshes.push_back({std::move(meshDefinition), MeshGeometry{}});
783 ModelRenderable* MakeModelRenderable(const gt::Mesh::Primitive& prim, ConversionContext& context)
785 auto modelRenderable = new ModelRenderable();
787 modelRenderable->mShaderIdx = 0; // TODO: further thought
789 auto materialIdx = prim.mMaterial.GetIndex();
790 if(INVALID_INDEX == materialIdx)
792 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
793 if(INVALID_INDEX == context.mDefaultMaterial)
795 auto& outMaterials = context.mOutput.mResources.mMaterials;
796 context.mDefaultMaterial = outMaterials.size();
798 ConvertMaterial(gt::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials, context);
801 materialIdx = context.mDefaultMaterial;
804 modelRenderable->mMaterialIdx = materialIdx;
806 return modelRenderable;
809 void ConvertCamera(const gt::Camera& camera, CameraParameters& camParams)
811 camParams.isPerspective = camera.mType.compare("perspective") == 0;
812 if(camParams.isPerspective)
814 auto& perspective = camera.mPerspective;
815 camParams.yFov = Degree(Radian(perspective.mYFov)).degree;
816 camParams.zNear = perspective.mZNear;
817 camParams.zFar = perspective.mZFar;
818 // TODO: yes, we seem to ignore aspectRatio in CameraParameters.
822 auto& ortho = camera.mOrthographic;
823 camParams.orthographicSize = ortho.mYMag * .5f;
824 camParams.aspectRatio = ortho.mXMag / ortho.mYMag;
825 camParams.zNear = ortho.mZNear;
826 camParams.zFar = ortho.mZFar;
830 void ConvertNode(gt::Node const& node, const Index gltfIdx, Index parentIdx, ConversionContext& context, bool isMRendererModel)
832 auto& output = context.mOutput;
833 auto& scene = output.mScene;
834 auto& resources = output.mResources;
836 const auto idx = scene.GetNodeCount();
837 auto weakNode = scene.AddNode([&]() {
838 std::unique_ptr<NodeDefinition> nodeDef{new NodeDefinition()};
840 nodeDef->mParentIdx = parentIdx;
841 nodeDef->mName = node.mName;
842 if(nodeDef->mName.empty())
844 // TODO: Production quality generation of unique names.
845 nodeDef->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDef.get()));
848 if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
850 nodeDef->mPosition = node.mTranslation;
851 nodeDef->mOrientation = node.mRotation;
852 nodeDef->mScale = node.mScale;
854 if(isMRendererModel && node.mName == ROOT_NODE_NAME && node.mScale == SCALE_TO_ADJUST)
856 nodeDef->mScale *= 0.01f;
864 ExceptionFlinger(ASSERT_LOCATION) << "Node name '" << node.mName << "' is not unique; scene is invalid.";
867 context.mNodeIndices.RegisterMapping(gltfIdx, idx);
869 Index skeletonIdx = node.mSkin ? node.mSkin.GetIndex() : INVALID_INDEX;
872 auto& mesh = *node.mMesh;
873 uint32_t primitiveCount = mesh.mPrimitives.size();
874 auto meshIdx = context.mMeshIds[node.mMesh.GetIndex()];
875 weakNode->mRenderables.reserve(primitiveCount);
876 for(uint32_t i = 0; i < primitiveCount; ++i)
878 std::unique_ptr<NodeDefinition::Renderable> renderable;
879 auto modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
880 modelRenderable->mMeshIdx = meshIdx + i;
882 DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
883 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
884 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx = skeletonIdx;
886 renderable.reset(modelRenderable);
887 weakNode->mRenderables.push_back(std::move(renderable));
893 CameraParameters camParams;
894 ConvertCamera(*node.mCamera, camParams);
896 camParams.matrix.SetTransformComponents(node.mScale, node.mRotation, node.mTranslation);
897 output.mCameraParameters.push_back(camParams);
900 for(auto& n : node.mChildren)
902 ConvertNode(*n, n.GetIndex(), idx, context, isMRendererModel);
906 void ConvertSceneNodes(const gt::Scene& scene, ConversionContext& context, bool isMRendererModel)
908 auto& outScene = context.mOutput.mScene;
909 Index rootIdx = outScene.GetNodeCount();
910 switch(scene.mNodes.size())
916 ConvertNode(*scene.mNodes[0], scene.mNodes[0].GetIndex(), INVALID_INDEX, context, isMRendererModel);
917 outScene.AddRootNode(rootIdx);
922 std::unique_ptr<NodeDefinition> sceneRoot{new NodeDefinition()};
923 sceneRoot->mName = "GLTF_LOADER_SCENE_ROOT_" + std::to_string(outScene.GetRoots().size());
925 outScene.AddNode(std::move(sceneRoot));
926 outScene.AddRootNode(rootIdx);
928 for(auto& n : scene.mNodes)
930 ConvertNode(*n, n.GetIndex(), rootIdx, context, isMRendererModel);
937 void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel)
939 if(!doc.mScenes.empty())
941 uint32_t rootSceneIndex = 0u;
944 rootSceneIndex = doc.mScene.GetIndex();
946 ConvertSceneNodes(doc.mScenes[rootSceneIndex], context, isMRendererModel);
948 for(uint32_t i = 0, i1 = rootSceneIndex; i < i1; ++i)
950 ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
953 for(uint32_t i = rootSceneIndex + 1; i < doc.mScenes.size(); ++i)
955 ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
961 void LoadDataFromAccessor(ConversionContext& context, uint32_t bufferIndex, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
963 if(bufferIndex >= context.mOutput.mResources.mBuffers.size())
965 DALI_LOG_ERROR("Invailid buffer index\n");
969 auto& buffer = context.mOutput.mResources.mBuffers[bufferIndex];
970 if(!buffer.IsAvailable())
972 DALI_LOG_ERROR("Failed to load from buffer stream.\n");
974 auto& stream = buffer.GetBufferStream();
976 stream.seekg(offset, stream.beg);
977 stream.read(reinterpret_cast<char*>(dataBuffer.Begin()), size);
981 float LoadDataFromAccessors(ConversionContext& context, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
983 inputDataBuffer.Resize(input.mCount);
984 outputDataBuffer.Resize(output.mCount);
986 const uint32_t inputDataBufferSize = input.GetBytesLength();
987 const uint32_t outputDataBufferSize = output.GetBytesLength();
989 LoadDataFromAccessor<float>(context, output.mBufferView->mBuffer.GetIndex(), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
990 LoadDataFromAccessor<T>(context, output.mBufferView->mBuffer.GetIndex(), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
991 ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
993 return inputDataBuffer[input.mCount - 1u];
997 float LoadKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
999 const gltf2::Accessor& input = *channel.mSampler->mInput;
1000 const gltf2::Accessor& output = *channel.mSampler->mOutput;
1002 Vector<float> inputDataBuffer;
1003 Vector<T> outputDataBuffer;
1005 const float duration = std::max(LoadDataFromAccessors<T>(context, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
1007 for(uint32_t i = 0; i < input.mCount; ++i)
1009 keyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i]);
1015 float LoadBlendShapeKeyFrames(ConversionContext& context, const gt::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
1017 const gltf2::Accessor& input = *channel.mSampler->mInput;
1018 const gltf2::Accessor& output = *channel.mSampler->mOutput;
1020 Vector<float> inputDataBuffer;
1021 Vector<float> outputDataBuffer;
1023 const float duration = LoadDataFromAccessors<float>(context, input, output, inputDataBuffer, outputDataBuffer);
1025 char weightNameBuffer[32];
1026 auto prefixSize = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM.c_str());
1027 char* const pWeightName = weightNameBuffer + prefixSize;
1028 const auto remainingSize = sizeof(weightNameBuffer) - prefixSize;
1029 for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex)
1031 AnimatedProperty& animatedProperty = properties[propertyIndex++];
1033 animatedProperty.mNodeIndex = nodeIndex;
1034 snprintf(pWeightName, remainingSize, "%d]", weightIndex);
1035 animatedProperty.mPropertyName = std::string(weightNameBuffer);
1037 animatedProperty.mKeyFrames = KeyFrames::New();
1038 for(uint32_t i = 0; i < input.mCount; ++i)
1040 animatedProperty.mKeyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i * endWeightIndex + weightIndex]);
1043 animatedProperty.mTimePeriod = {0.f, duration};
1049 void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
1051 auto& output = context.mOutput;
1053 output.mAnimationDefinitions.reserve(output.mAnimationDefinitions.size() + doc.mAnimations.size());
1055 for(const auto& animation : doc.mAnimations)
1057 AnimationDefinition animationDef;
1059 if(!animation.mName.empty())
1061 animationDef.mName = animation.mName;
1064 uint32_t numberOfProperties = 0u;
1065 for(const auto& channel : animation.mChannels)
1067 if(channel.mTarget.mPath == gt::Animation::Channel::Target::WEIGHTS)
1069 numberOfProperties += channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount;
1073 numberOfProperties++;
1076 animationDef.mProperties.resize(numberOfProperties);
1078 Index propertyIndex = 0u;
1079 for(const auto& channel : animation.mChannels)
1081 Index nodeIndex = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
1082 float duration = 0.f;
1084 switch(channel.mTarget.mPath)
1086 case gt::Animation::Channel::Target::TRANSLATION:
1088 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1090 animatedProperty.mNodeIndex = nodeIndex;
1091 animatedProperty.mPropertyName = POSITION_PROPERTY;
1093 animatedProperty.mKeyFrames = KeyFrames::New();
1094 duration = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1096 animatedProperty.mTimePeriod = {0.f, duration};
1099 case gt::Animation::Channel::Target::ROTATION:
1101 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1103 animatedProperty.mNodeIndex = nodeIndex;
1104 animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
1106 animatedProperty.mKeyFrames = KeyFrames::New();
1107 duration = LoadKeyFrames<Quaternion>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1109 animatedProperty.mTimePeriod = {0.f, duration};
1112 case gt::Animation::Channel::Target::SCALE:
1114 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1116 animatedProperty.mNodeIndex = nodeIndex;
1117 animatedProperty.mPropertyName = SCALE_PROPERTY;
1119 animatedProperty.mKeyFrames = KeyFrames::New();
1120 duration = LoadKeyFrames<Vector3>(context, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1122 animatedProperty.mTimePeriod = {0.f, duration};
1125 case gt::Animation::Channel::Target::WEIGHTS:
1127 duration = LoadBlendShapeKeyFrames(context, channel, nodeIndex, propertyIndex, animationDef.mProperties);
1133 // nothing to animate.
1138 animationDef.mDuration = std::max(duration, animationDef.mDuration);
1143 output.mAnimationDefinitions.push_back(std::move(animationDef));
1147 void ProcessSkins(const gt::Document& doc, ConversionContext& context)
1149 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skininversebindmatrices
1150 // If an inverseBindMatrices accessor was provided, we'll load the joint data from the buffer,
1151 // otherwise we'll set identity matrices for inverse bind pose.
1152 struct IInverseBindMatrixProvider
1154 virtual ~IInverseBindMatrixProvider()
1157 virtual void Provide(Matrix& ibm) = 0;
1160 struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
1162 std::istream& mStream;
1163 const uint32_t mElementSizeBytes;
1165 InverseBindMatrixAccessor(const gt::Accessor& accessor, ConversionContext& context)
1166 : mStream(context.mOutput.mResources.mBuffers[accessor.mBufferView->mBuffer.GetIndex()].GetBufferStream()),
1167 mElementSizeBytes(accessor.GetElementSizeBytes())
1169 DALI_ASSERT_DEBUG(accessor.mType == gt::AccessorType::MAT4 && accessor.mComponentType == gt::Component::FLOAT);
1171 if(!mStream.rdbuf()->in_avail())
1173 DALI_LOG_ERROR("Failed to load from stream\n");
1176 mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset, mStream.beg);
1179 virtual void Provide(Matrix& ibm) override
1181 DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), mElementSizeBytes));
1185 struct DefaultInverseBindMatrixProvider : public IInverseBindMatrixProvider
1187 virtual void Provide(Matrix& ibm) override
1189 ibm = Matrix::IDENTITY;
1193 auto& resources = context.mOutput.mResources;
1194 resources.mSkeletons.reserve(doc.mSkins.size());
1196 for(auto& skin : doc.mSkins)
1198 std::unique_ptr<IInverseBindMatrixProvider> ibmProvider;
1199 if(skin.mInverseBindMatrices)
1201 ibmProvider.reset(new InverseBindMatrixAccessor(*skin.mInverseBindMatrices, context));
1205 ibmProvider.reset(new DefaultInverseBindMatrixProvider());
1208 SkeletonDefinition skeleton;
1209 if(skin.mSkeleton.GetIndex() != INVALID_INDEX)
1211 skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(skin.mSkeleton.GetIndex());
1214 skeleton.mJoints.resize(skin.mJoints.size());
1215 auto iJoint = skeleton.mJoints.begin();
1216 for(auto& joint : skin.mJoints)
1218 iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(joint.GetIndex());
1220 ibmProvider->Provide(iJoint->mInverseBindMatrix);
1225 resources.mSkeletons.push_back(std::move(skeleton));
1229 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene)
1231 uint32_t nodeCount = scene.GetNodeCount();
1232 for(uint32_t i = 0; i < nodeCount; ++i)
1234 auto nodeDef = scene.GetNode(i);
1235 for(auto& renderable : nodeDef->mRenderables)
1237 if(shaderFactory.ProduceShader(*renderable) == INVALID_INDEX)
1239 DALI_LOG_ERROR("Fail to produce shader\n");
1245 void SetObjectReaders()
1247 js::SetObjectReader(BUFFER_READER);
1248 js::SetObjectReader(BUFFER_VIEW_READER);
1249 js::SetObjectReader(BUFFER_VIEW_CLIENT_READER);
1250 js::SetObjectReader(COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER);
1251 js::SetObjectReader(ACCESSOR_SPARSE_READER);
1252 js::SetObjectReader(ACCESSOR_READER);
1253 js::SetObjectReader(IMAGE_READER);
1254 js::SetObjectReader(SAMPLER_READER);
1255 js::SetObjectReader(TEXURE_READER);
1256 js::SetObjectReader(TEXURE_INFO_READER);
1257 js::SetObjectReader(MATERIAL_PBR_READER);
1258 js::SetObjectReader(MATERIAL_SPECULAR_READER);
1259 js::SetObjectReader(MATERIAL_IOR_READER);
1260 js::SetObjectReader(MATERIAL_EXTENSION_READER);
1261 js::SetObjectReader(MATERIAL_READER);
1262 js::SetObjectReader(MESH_PRIMITIVE_READER);
1263 js::SetObjectReader(MESH_READER);
1264 js::SetObjectReader(SKIN_READER);
1265 js::SetObjectReader(CAMERA_PERSPECTIVE_READER);
1266 js::SetObjectReader(CAMERA_ORTHOGRAPHIC_READER);
1267 js::SetObjectReader(CAMERA_READER);
1268 js::SetObjectReader(NODE_READER);
1269 js::SetObjectReader(ANIMATION_SAMPLER_READER);
1270 js::SetObjectReader(ANIMATION_TARGET_READER);
1271 js::SetObjectReader(ANIMATION_CHANNEL_READER);
1272 js::SetObjectReader(ANIMATION_READER);
1273 js::SetObjectReader(SCENE_READER);
1276 void SetDefaultEnvironmentMap(const gt::Document& doc, ConversionContext& context)
1278 EnvironmentDefinition envDef;
1279 envDef.mUseBrdfTexture = true;
1280 envDef.mIblIntensity = Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
1281 context.mOutput.mResources.mEnvironmentMaps.push_back({std::move(envDef), EnvironmentDefinition::Textures()});
1286 void InitializeGltfLoader()
1288 // Set ObjectReader only once (for all gltf loading).
1289 static bool setObjectReadersRequired = true;
1291 Mutex::ScopedLock lock(gInitializeMutex);
1292 if(setObjectReadersRequired)
1294 // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1296 setObjectReadersRequired = false;
1301 void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
1303 bool failed = false;
1304 auto js = LoadTextFile(url.c_str(), &failed);
1307 throw std::runtime_error("Failed to load " + url);
1310 json::unique_ptr root(json_parse(js.c_str(), js.size()));
1313 throw std::runtime_error("Failed to parse " + url);
1318 auto& rootObj = js::Cast<json_object_s>(*root);
1319 auto jsAsset = js::FindObjectChild("asset", rootObj);
1321 auto jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsAsset));
1324 doc.mAsset.mVersion = js::Read::StringView(*jsAssetVersion);
1327 bool isMRendererModel(false);
1328 auto jsAssetGenerator = js::FindObjectChild("generator", js::Cast<json_object_s>(*jsAsset));
1329 if(jsAssetGenerator)
1331 doc.mAsset.mGenerator = js::Read::StringView(*jsAssetGenerator);
1332 isMRendererModel = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
1336 Mutex::ScopedLock lock(gReadMutex);
1337 gt::SetRefReaderObject(doc);
1338 DOCUMENT_READER.Read(rootObj, doc);
1341 auto path = url.substr(0, url.rfind('/') + 1);
1342 ConversionContext context{params, path, INVALID_INDEX};
1344 ConvertBuffers(doc, context);
1345 ConvertMaterials(doc, context);
1346 ConvertMeshes(doc, context);
1347 ConvertNodes(doc, context, isMRendererModel);
1348 ConvertAnimations(doc, context);
1349 ProcessSkins(doc, context);
1350 ProduceShaders(shaderFactory, params.mScene);
1351 params.mScene.EnsureUniqueSkinningShaderInstances(params.mResources);
1353 // Set Default Environment map
1354 SetDefaultEnvironmentMap(doc, context);
1357 } // namespace Loader
1358 } // namespace Scene3D