2 * Copyright (c) 2022 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>
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>
46 const std::string POSITION_PROPERTY("position");
47 const std::string ORIENTATION_PROPERTY("orientation");
48 const std::string SCALE_PROPERTY("scale");
49 const std::string BLEND_SHAPE_WEIGHTS_UNIFORM("uBlendShapeWeight");
50 const std::string MRENDERER_MODEL_IDENTIFICATION("M-Renderer");
51 const std::string ROOT_NODE_NAME("RootNode");
52 const Vector3 SCALE_TO_ADJUST(100.0f, 100.0f, 100.0f);
54 const Geometry::Type GLTF2_TO_DALI_PRIMITIVES[]{
60 Geometry::TRIANGLE_STRIP,
61 Geometry::TRIANGLE_FAN}; //...because Dali swaps the last two.
63 struct AttributeMapping
65 gt::Attribute::Type mType;
66 MeshDefinition::Accessor MeshDefinition::*mAccessor;
67 uint16_t mElementSizeRequired;
68 } ATTRIBUTE_MAPPINGS[]{
69 {gt::Attribute::NORMAL, &MeshDefinition::mNormals, sizeof(Vector3)},
70 {gt::Attribute::TANGENT, &MeshDefinition::mTangents, sizeof(Vector3)},
71 {gt::Attribute::TEXCOORD_0, &MeshDefinition::mTexCoords, sizeof(Vector2)},
72 {gt::Attribute::COLOR_0, &MeshDefinition::mColors, sizeof(Vector4)},
73 {gt::Attribute::JOINTS_0, &MeshDefinition::mJoints0, sizeof(Vector4)},
74 {gt::Attribute::WEIGHTS_0, &MeshDefinition::mWeights0, sizeof(Vector4)},
77 std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
80 SetRefReaderObject(proxy);
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 auto BUFFER_READER = std::move(js::Reader<gt::Buffer>()
103 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::Buffer::mByteLength))
104 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Buffer::mUri)));
106 const auto BUFFER_VIEW_READER = std::move(js::Reader<gt::BufferView>()
107 .Register(*js::MakeProperty("buffer", gt::RefReader<gt::Document>::Read<gt::Buffer, >::Document::mBuffers>, >::BufferView::mBuffer))
108 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferView::mByteOffset))
109 .Register(*js::MakeProperty("byteLength", js::Read::Number<uint32_t>, >::BufferView::mByteLength))
110 .Register(*js::MakeProperty("byteStride", js::Read::Number<uint32_t>, >::BufferView::mByteStride))
111 .Register(*js::MakeProperty("target", js::Read::Number<uint32_t>, >::BufferView::mTarget)));
113 const auto BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::BufferViewClient>()
114 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::BufferViewClient::mBufferView))
115 .Register(*js::MakeProperty("byteOffset", js::Read::Number<uint32_t>, >::BufferViewClient::mByteOffset)));
117 const auto COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER = std::move(js::Reader<gt::ComponentTypedBufferViewClient>()
118 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, gt::Ref<gt::BufferView>>("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::ComponentTypedBufferViewClient::mBufferView))
119 .Register(*new js::Property<gt::ComponentTypedBufferViewClient, uint32_t>("byteOffset", js::Read::Number<uint32_t>, >::ComponentTypedBufferViewClient::mByteOffset))
120 .Register(*js::MakeProperty("componentType", js::Read::Enum<gt::Component::Type>, >::ComponentTypedBufferViewClient::mComponentType)));
122 const auto ACCESSOR_SPARSE_READER = std::move(js::Reader<gt::Accessor::Sparse>()
123 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::Sparse::mCount))
124 .Register(*js::MakeProperty("indices", js::ObjectReader<gt::ComponentTypedBufferViewClient>::Read, >::Accessor::Sparse::mIndices))
125 .Register(*js::MakeProperty("values", js::ObjectReader<gt::BufferViewClient>::Read, >::Accessor::Sparse::mValues)));
127 const auto ACCESSOR_READER = std::move(js::Reader<gt::Accessor>()
128 .Register(*new js::Property<gt::Accessor, gt::Ref<gt::BufferView>>("bufferView",
129 gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>,
130 >::Accessor::mBufferView))
131 .Register(*new js::Property<gt::Accessor, uint32_t>("byteOffset",
132 js::Read::Number<uint32_t>,
133 >::Accessor::mByteOffset))
134 .Register(*new js::Property<gt::Accessor, gt::Component::Type>("componentType",
135 js::Read::Enum<gt::Component::Type>,
136 >::Accessor::mComponentType))
137 .Register(*new js::Property<gt::Accessor, std::string_view>("name", js::Read::StringView, >::Accessor::mName))
138 .Register(*js::MakeProperty("count", js::Read::Number<uint32_t>, >::Accessor::mCount))
139 .Register(*js::MakeProperty("normalized", js::Read::Boolean, >::Accessor::mNormalized))
140 .Register(*js::MakeProperty("type", gt::ReadStringEnum<gt::AccessorType>, >::Accessor::mType))
141 .Register(*js::MakeProperty("min", js::Read::Array<float, js::Read::Number>, >::Accessor::mMin))
142 .Register(*js::MakeProperty("max", js::Read::Array<float, js::Read::Number>, >::Accessor::mMax))
143 .Register(*new js::Property<gt::Accessor, gt::Accessor::Sparse>("sparse", js::ObjectReader<gt::Accessor::Sparse>::Read, >::Accessor::SetSparse)));
145 const auto IMAGE_READER = std::move(js::Reader<gt::Image>()
146 .Register(*new js::Property<gt::Image, std::string_view>("name", js::Read::StringView, >::Material::mName))
147 .Register(*js::MakeProperty("uri", js::Read::StringView, >::Image::mUri))
148 .Register(*js::MakeProperty("mimeType", js::Read::StringView, >::Image::mMimeType))
149 .Register(*js::MakeProperty("bufferView", gt::RefReader<gt::Document>::Read<gt::BufferView, >::Document::mBufferViews>, >::Image::mBufferView)));
151 const auto SAMPLER_READER = std::move(js::Reader<gt::Sampler>()
152 .Register(*js::MakeProperty("minFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMinFilter))
153 .Register(*js::MakeProperty("magFilter", js::Read::Enum<gt::Filter::Type>, >::Sampler::mMagFilter))
154 .Register(*js::MakeProperty("wrapS", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapS))
155 .Register(*js::MakeProperty("wrapT", js::Read::Enum<gt::Wrap::Type>, >::Sampler::mWrapT)));
157 const auto TEXURE_READER = std::move(js::Reader<gt::Texture>()
158 .Register(*js::MakeProperty("source", gt::RefReader<gt::Document>::Read<gt::Image, >::Document::mImages>, >::Texture::mSource))
159 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Document>::Read<gt::Sampler, >::Document::mSamplers>, >::Texture::mSampler)));
161 const auto TEXURE_INFO_READER = std::move(js::Reader<gt::TextureInfo>()
162 .Register(*js::MakeProperty("index", gt::RefReader<gt::Document>::Read<gt::Texture, >::Document::mTextures>, >::TextureInfo::mTexture))
163 .Register(*js::MakeProperty("texCoord", js::Read::Number<uint32_t>, >::TextureInfo::mTexCoord))
164 .Register(*js::MakeProperty("scale", js::Read::Number<float>, >::TextureInfo::mScale))
165 .Register(*js::MakeProperty("strength", js::Read::Number<float>, >::TextureInfo::mStrength)));
167 const auto MATERIAL_PBR_READER = std::move(js::Reader<gt::Material::Pbr>()
168 .Register(*js::MakeProperty("baseColorFactor", gt::ReadDaliVector<Vector4>, >::Material::Pbr::mBaseColorFactor))
169 .Register(*js::MakeProperty("baseColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mBaseColorTexture))
170 .Register(*js::MakeProperty("metallicFactor", js::Read::Number<float>, >::Material::Pbr::mMetallicFactor))
171 .Register(*js::MakeProperty("roughnessFactor", js::Read::Number<float>, >::Material::Pbr::mRoughnessFactor))
172 .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::Pbr::mMetallicRoughnessTexture)));
174 const auto MATERIAL_SPECULAR_READER = std::move(js::Reader<gt::MaterialSpecular>()
175 .Register(*js::MakeProperty("specularFactor", js::Read::Number<float>, >::MaterialSpecular::mSpecularFactor))
176 .Register(*js::MakeProperty("specularTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularTexture))
177 .Register(*js::MakeProperty("specularColorFactor", gt::ReadDaliVector<Vector3>, >::MaterialSpecular::mSpecularColorFactor))
178 .Register(*js::MakeProperty("specularColorTexture", js::ObjectReader<gt::TextureInfo>::Read, >::MaterialSpecular::mSpecularColorTexture)));
180 const auto MATERIAL_IOR_READER = std::move(js::Reader<gt::MaterialIor>()
181 .Register(*js::MakeProperty("ior", js::Read::Number<float>, >::MaterialIor::mIor)));
183 const auto MATERIAL_EXTENSION_READER = std::move(js::Reader<gt::MaterialExtensions>()
184 .Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader<gt::MaterialIor>::Read, >::MaterialExtensions::mMaterialIor))
185 .Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader<gt::MaterialSpecular>::Read, >::MaterialExtensions::mMaterialSpecular)));
187 const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
188 .Register(*new js::Property<gt::Material, std::string_view>("name", js::Read::StringView, >::Material::mName))
189 .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader<gt::Material::Pbr>::Read, >::Material::mPbrMetallicRoughness))
190 .Register(*js::MakeProperty("normalTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mNormalTexture))
191 .Register(*js::MakeProperty("occlusionTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mOcclusionTexture))
192 .Register(*js::MakeProperty("emissiveTexture", js::ObjectReader<gt::TextureInfo>::Read, >::Material::mEmissiveTexture))
193 .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector<Vector3>, >::Material::mEmissiveFactor))
194 .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum<gt::AlphaMode>, >::Material::mAlphaMode))
195 .Register(*js::MakeProperty("alphaCutoff", js::Read::Number<float>, >::Material::mAlphaCutoff))
196 .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, >::Material::mDoubleSided))
197 .Register(*js::MakeProperty("extensions", js::ObjectReader<gt::MaterialExtensions>::Read, >::Material::mMaterialExtensions)));
199 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
201 auto& jo = js::Cast<json_object_s>(j);
202 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> result;
207 auto jstr = *i->name;
208 result[gt::Attribute::FromString(jstr.string, jstr.string_size)] = gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>(*i->value);
214 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> ReadMeshPrimitiveTargets(const json_value_s& j)
216 auto& jo = js::Cast<json_array_s>(j);
217 std::vector<std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>>> result;
219 result.reserve(jo.length);
224 result.push_back(std::move(ReadMeshPrimitiveAttributes(*i->value)));
231 const auto MESH_PRIMITIVE_READER = std::move(js::Reader<gt::Mesh::Primitive>()
232 .Register(*js::MakeProperty("attributes", ReadMeshPrimitiveAttributes, >::Mesh::Primitive::mAttributes))
233 .Register(*js::MakeProperty("indices", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Mesh::Primitive::mIndices))
234 .Register(*js::MakeProperty("material", gt::RefReader<gt::Document>::Read<gt::Material, >::Document::mMaterials>, >::Mesh::Primitive::mMaterial))
235 .Register(*js::MakeProperty("mode", js::Read::Enum<gt::Mesh::Primitive::Mode>, >::Mesh::Primitive::mMode))
236 .Register(*js::MakeProperty("targets", ReadMeshPrimitiveTargets, >::Mesh::Primitive::mTargets)));
238 const auto MESH_READER = std::move(js::Reader<gt::Mesh>()
239 .Register(*new js::Property<gt::Mesh, std::string_view>("name", js::Read::StringView, >::Mesh::mName))
240 .Register(*js::MakeProperty("primitives",
241 js::Read::Array<gt::Mesh::Primitive, js::ObjectReader<gt::Mesh::Primitive>::Read>,
242 >::Mesh::mPrimitives))
243 .Register(*js::MakeProperty("weights", js::Read::Array<float, js::Read::Number>, >::Mesh::mWeights)));
245 const auto SKIN_READER = std::move(js::Reader<gt::Skin>()
246 .Register(*new js::Property<gt::Skin, std::string_view>("name", js::Read::StringView, >::Skin::mName))
247 .Register(*js::MakeProperty("inverseBindMatrices",
248 gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>,
249 >::Skin::mInverseBindMatrices))
250 .Register(*js::MakeProperty("skeleton",
251 gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>,
252 >::Skin::mSkeleton))
253 .Register(*js::MakeProperty("joints",
254 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
255 >::Skin::mJoints)));
257 const auto CAMERA_PERSPECTIVE_READER = std::move(js::Reader<gt::Camera::Perspective>()
258 .Register(*js::MakeProperty("aspectRatio", js::Read::Number<float>, >::Camera::Perspective::mAspectRatio))
259 .Register(*js::MakeProperty("yfov", js::Read::Number<float>, >::Camera::Perspective::mYFov))
260 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Perspective::mZFar))
261 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Perspective::mZNear))); // TODO: infinite perspective projection, where znear is omitted
263 const auto CAMERA_ORTHOGRAPHIC_READER = std::move(js::Reader<gt::Camera::Orthographic>()
264 .Register(*js::MakeProperty("xmag", js::Read::Number<float>, >::Camera::Orthographic::mXMag))
265 .Register(*js::MakeProperty("ymag", js::Read::Number<float>, >::Camera::Orthographic::mXMag))
266 .Register(*js::MakeProperty("zfar", js::Read::Number<float>, >::Camera::Orthographic::mZFar))
267 .Register(*js::MakeProperty("znear", js::Read::Number<float>, >::Camera::Orthographic::mZNear)));
269 const auto CAMERA_READER = std::move(js::Reader<gt::Camera>()
270 .Register(*new js::Property<gt::Camera, std::string_view>("name", js::Read::StringView, >::Camera::mName))
271 .Register(*js::MakeProperty("type", js::Read::StringView, >::Camera::mType))
272 .Register(*js::MakeProperty("perspective", js::ObjectReader<gt::Camera::Perspective>::Read, >::Camera::mPerspective))
273 .Register(*js::MakeProperty("orthographic", js::ObjectReader<gt::Camera::Orthographic>::Read, >::Camera::mOrthographic)));
275 const auto NODE_READER = std::move(js::Reader<gt::Node>()
276 .Register(*new js::Property<gt::Node, std::string_view>("name", js::Read::StringView, >::Node::mName))
277 .Register(*js::MakeProperty("translation", gt::ReadDaliVector<Vector3>, >::Node::mTranslation))
278 .Register(*js::MakeProperty("rotation", gt::ReadQuaternion, >::Node::mRotation))
279 .Register(*js::MakeProperty("scale", gt::ReadDaliVector<Vector3>, >::Node::mScale))
280 .Register(*new js::Property<gt::Node, Matrix>("matrix", gt::ReadDaliVector<Matrix>, >::Node::SetMatrix))
281 .Register(*js::MakeProperty("camera", gt::RefReader<gt::Document>::Read<gt::Camera, >::Document::mCameras>, >::Node::mCamera))
282 .Register(*js::MakeProperty("children", js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>, >::Node::mChildren))
283 .Register(*js::MakeProperty("mesh", gt::RefReader<gt::Document>::Read<gt::Mesh, >::Document::mMeshes>, >::Node::mMesh))
284 .Register(*js::MakeProperty("skin", gt::RefReader<gt::Document>::Read<gt::Skin, >::Document::mSkins>, >::Node::mSkin)));
286 const auto ANIMATION_SAMPLER_READER = std::move(js::Reader<gt::Animation::Sampler>()
287 .Register(*js::MakeProperty("input", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mInput))
288 .Register(*js::MakeProperty("output", gt::RefReader<gt::Document>::Read<gt::Accessor, >::Document::mAccessors>, >::Animation::Sampler::mOutput))
289 .Register(*js::MakeProperty("interpolation", gt::ReadStringEnum<gt::Animation::Sampler::Interpolation>, >::Animation::Sampler::mInterpolation)));
291 const auto ANIMATION_TARGET_READER = std::move(js::Reader<gt::Animation::Channel::Target>()
292 .Register(*js::MakeProperty("node", gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>, >::Animation::Channel::Target::mNode))
293 .Register(*js::MakeProperty("path", gt::ReadStringEnum<gt::Animation::Channel::Target>, >::Animation::Channel::Target::mPath)));
295 const auto ANIMATION_CHANNEL_READER = std::move(js::Reader<gt::Animation::Channel>()
296 .Register(*js::MakeProperty("target", js::ObjectReader<gt::Animation::Channel::Target>::Read, >::Animation::Channel::mTarget))
297 .Register(*js::MakeProperty("sampler", gt::RefReader<gt::Animation>::Read<gt::Animation::Sampler, >::Animation::mSamplers>, >::Animation::Channel::mSampler)));
299 const auto ANIMATION_READER = std::move(js::Reader<gt::Animation>()
300 .Register(*new js::Property<gt::Animation, std::string_view>("name", js::Read::StringView, >::Animation::mName))
301 .Register(*js::MakeProperty("samplers",
302 js::Read::Array<gt::Animation::Sampler, js::ObjectReader<gt::Animation::Sampler>::Read>,
303 >::Animation::mSamplers))
304 .Register(*js::MakeProperty("channels",
305 js::Read::Array<gt::Animation::Channel, js::ObjectReader<gt::Animation::Channel>::Read>,
306 >::Animation::mChannels)));
308 const auto SCENE_READER = std::move(js::Reader<gt::Scene>()
309 .Register(*new js::Property<gt::Scene, std::string_view>("name", js::Read::StringView, >::Scene::mName))
310 .Register(*js::MakeProperty("nodes",
311 js::Read::Array<gt::Ref<gt::Node>, gt::RefReader<gt::Document>::Read<gt::Node, >::Document::mNodes>>,
312 >::Scene::mNodes)));
314 const auto DOCUMENT_READER = std::move(js::Reader<gt::Document>()
315 .Register(*js::MakeProperty("buffers",
316 js::Read::Array<gt::Buffer, js::ObjectReader<gt::Buffer>::Read>,
317 >::Document::mBuffers))
318 .Register(*js::MakeProperty("bufferViews",
319 js::Read::Array<gt::BufferView, js::ObjectReader<gt::BufferView>::Read>,
320 >::Document::mBufferViews))
321 .Register(*js::MakeProperty("accessors",
322 js::Read::Array<gt::Accessor, js::ObjectReader<gt::Accessor>::Read>,
323 >::Document::mAccessors))
324 .Register(*js::MakeProperty("images",
325 js::Read::Array<gt::Image, js::ObjectReader<gt::Image>::Read>,
326 >::Document::mImages))
327 .Register(*js::MakeProperty("samplers",
328 js::Read::Array<gt::Sampler, js::ObjectReader<gt::Sampler>::Read>,
329 >::Document::mSamplers))
330 .Register(*js::MakeProperty("textures",
331 js::Read::Array<gt::Texture, js::ObjectReader<gt::Texture>::Read>,
332 >::Document::mTextures))
333 .Register(*js::MakeProperty("materials",
334 js::Read::Array<gt::Material, js::ObjectReader<gt::Material>::Read>,
335 >::Document::mMaterials))
336 .Register(*js::MakeProperty("meshes",
337 js::Read::Array<gt::Mesh, js::ObjectReader<gt::Mesh>::Read>,
338 >::Document::mMeshes))
339 .Register(*js::MakeProperty("skins",
340 js::Read::Array<gt::Skin, js::ObjectReader<gt::Skin>::Read>,
341 >::Document::mSkins))
342 .Register(*js::MakeProperty("cameras",
343 js::Read::Array<gt::Camera, js::ObjectReader<gt::Camera>::Read>,
344 >::Document::mCameras))
345 .Register(*js::MakeProperty("nodes",
346 js::Read::Array<gt::Node, js::ObjectReader<gt::Node>::Read>,
347 >::Document::mNodes))
348 .Register(*js::MakeProperty("animations",
350 >::Document::mAnimations))
351 .Register(*js::MakeProperty("scenes",
352 js::Read::Array<gt::Scene, js::ObjectReader<gt::Scene>::Read>,
353 >::Document::mScenes))
354 .Register(*js::MakeProperty("scene", gt::RefReader<gt::Document>::Read<gt::Scene, >::Document::mScenes>, >::Document::mScene)));
362 bool operator<(const NodeMapping& mapping, Index gltfIdx)
364 return mapping.gltfIdx < gltfIdx;
367 class NodeIndexMapper
370 NodeIndexMapper() = default;
371 NodeIndexMapper(const NodeIndexMapper&) = delete;
372 NodeIndexMapper& operator=(const NodeIndexMapper&) = delete;
374 ///@brief Registers a mapping of the @a gltfIdx of a node to its @a runtimeIdx .
375 ///@note If the indices are the same, the registration is omitted, in order to
376 /// save growing a vector.
377 void RegisterMapping(Index gltfIdx, Index runtimeIdx)
379 if(gltfIdx != runtimeIdx)
381 auto iInsert = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx);
382 DALI_ASSERT_DEBUG(iInsert == mNodes.end() || iInsert->gltfIdx != gltfIdx);
383 mNodes.insert(iInsert, NodeMapping{gltfIdx, runtimeIdx});
387 ///@brief Retrieves the runtime index of a Node, mapped to the given @a gltfIdx.
388 Index GetRuntimeId(Index gltfIdx) const
390 auto iFind = std::lower_bound(mNodes.begin(), mNodes.end(), gltfIdx); // using custom operator<
391 return (iFind != mNodes.end() && iFind->gltfIdx == gltfIdx) ? iFind->runtimeIdx : gltfIdx;
395 std::vector<NodeMapping> mNodes;
398 struct ConversionContext
403 Index mDefaultMaterial;
405 std::vector<Index> mMeshIds;
406 NodeIndexMapper mNodeIndices;
409 SamplerFlags::Type ConvertWrapMode(gt::Wrap::Type w)
413 case gt::Wrap::REPEAT:
414 return SamplerFlags::WRAP_REPEAT;
415 case gt::Wrap::CLAMP_TO_EDGE:
416 return SamplerFlags::WRAP_CLAMP;
417 case gt::Wrap::MIRRORED_REPEAT:
418 return SamplerFlags::WRAP_MIRROR;
420 throw std::runtime_error("Invalid wrap type.");
424 SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& s)
428 return ((s->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (s->mMinFilter - gt::Filter::NEAREST) : ((s->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
429 ((s->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
430 (ConvertWrapMode(s->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
431 (ConvertWrapMode(s->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
435 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texturesampler
436 // "The index of the sampler used by this texture. When undefined, a sampler with repeat wrapping and auto filtering should be used."
437 // "What is an auto filtering", I hear you ask. Since there's nothing else to determine mipmapping from - including glTF image
438 // properties, if not in some extension -, we will simply assume linear filtering.
439 return SamplerFlags::FILTER_LINEAR | (SamplerFlags::FILTER_LINEAR << SamplerFlags::FILTER_MAG_SHIFT) |
440 (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_S_SHIFT) | (SamplerFlags::WRAP_REPEAT << SamplerFlags::WRAP_T_SHIFT);
444 TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm, const ImageMetadata& metaData = ImageMetadata())
446 return TextureDefinition{std::string(mm.mTexture->mSource->mUri), ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
449 void ConvertMaterial(const gt::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials)
451 auto getTextureMetaData = [](const std::unordered_map<std::string, ImageMetadata>& metaData, const gt::TextureInfo& info) {
452 if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end())
454 return search->second;
458 return ImageMetadata();
462 MaterialDefinition matDef;
464 auto& pbr = material.mPbrMetallicRoughness;
465 if(material.mAlphaMode == gt::AlphaMode::BLEND)
467 matDef.mIsOpaque = false;
468 matDef.mFlags |= MaterialDefinition::TRANSPARENCY;
470 else if(material.mAlphaMode == gt::AlphaMode::MASK)
472 matDef.mIsMask = true;
473 matDef.SetAlphaCutoff(std::min(1.f, std::max(0.f, material.mAlphaCutoff)));
476 matDef.mBaseColorFactor = pbr.mBaseColorFactor;
478 matDef.mTextureStages.reserve(!!pbr.mBaseColorTexture + !!pbr.mMetallicRoughnessTexture + !!material.mNormalTexture + !!material.mOcclusionTexture + !!material.mEmissiveTexture);
479 if(pbr.mBaseColorTexture)
481 const auto semantic = MaterialDefinition::ALBEDO;
482 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture, getTextureMetaData(imageMetaData, pbr.mBaseColorTexture))});
483 // TODO: and there had better be one
484 matDef.mFlags |= semantic;
488 matDef.mNeedAlbedoTexture = false;
491 matDef.mMetallic = pbr.mMetallicFactor;
492 matDef.mRoughness = pbr.mRoughnessFactor;
494 if(pbr.mMetallicRoughnessTexture)
496 const auto semantic = MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
497 MaterialDefinition::GLTF_CHANNELS;
498 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture, getTextureMetaData(imageMetaData, pbr.mMetallicRoughnessTexture))});
499 // TODO: and there had better be one
500 matDef.mFlags |= semantic;
504 matDef.mNeedMetallicRoughnessTexture = false;
507 matDef.mNormalScale = material.mNormalTexture.mScale;
508 if(material.mNormalTexture)
510 const auto semantic = MaterialDefinition::NORMAL;
511 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture, getTextureMetaData(imageMetaData, material.mNormalTexture))});
512 // TODO: and there had better be one
513 matDef.mFlags |= semantic;
517 matDef.mNeedNormalTexture = false;
520 if(material.mOcclusionTexture)
522 const auto semantic = MaterialDefinition::OCCLUSION;
523 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture, getTextureMetaData(imageMetaData, material.mOcclusionTexture))});
524 // TODO: and there had better be one
525 matDef.mFlags |= semantic;
526 matDef.mOcclusionStrength = material.mOcclusionTexture.mStrength;
529 if(material.mEmissiveTexture)
531 const auto semantic = MaterialDefinition::EMISSIVE;
532 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture, getTextureMetaData(imageMetaData, material.mEmissiveTexture))});
533 // TODO: and there had better be one
534 matDef.mFlags |= semantic;
535 matDef.mEmissiveFactor = material.mEmissiveFactor;
538 if(material.mMaterialExtensions.mMaterialIor.mIor < MAXFLOAT)
540 float ior = material.mMaterialExtensions.mMaterialIor.mIor;
541 matDef.mDielectricSpecular = powf((ior - 1.0f) / (ior + 1.0f), 2.0f);
543 matDef.mSpecularFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularFactor;
544 matDef.mSpecularColorFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor;
546 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)
548 const auto semantic = MaterialDefinition::SPECULAR;
549 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture))});
550 matDef.mFlags |= semantic;
553 if(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)
555 const auto semantic = MaterialDefinition::SPECULAR_COLOR;
556 matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture))});
557 matDef.mFlags |= semantic;
560 matDef.mDoubleSided = material.mDoubleSided;
562 outMaterials.emplace_back(std::move(matDef), TextureSet());
565 void ConvertMaterials(const gt::Document& doc, ConversionContext& context)
567 auto& imageMetaData = context.mOutput.mSceneMetadata.mImageMetadata;
569 auto& outMaterials = context.mOutput.mResources.mMaterials;
570 outMaterials.reserve(doc.mMaterials.size());
572 for(auto& m : doc.mMaterials)
574 ConvertMaterial(m, imageMetaData, outMaterials);
578 MeshDefinition::Accessor ConvertMeshPrimitiveAccessor(const gt::Accessor& acc)
580 DALI_ASSERT_ALWAYS((acc.mBufferView &&
581 (acc.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) ||
582 (acc.mSparse && !acc.mBufferView));
584 DALI_ASSERT_ALWAYS(!acc.mSparse ||
585 ((acc.mSparse->mIndices.mBufferView && (acc.mSparse->mIndices.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max())) &&
586 (acc.mSparse->mValues.mBufferView && (acc.mSparse->mValues.mBufferView->mByteStride < std::numeric_limits<uint16_t>::max()))));
588 MeshDefinition::SparseBlob sparseBlob;
591 const gt::Accessor::Sparse& sparse = *acc.mSparse;
592 const gt::ComponentTypedBufferViewClient& indices = sparse.mIndices;
593 const gt::BufferViewClient& values = sparse.mValues;
595 MeshDefinition::Blob indicesBlob(
596 indices.mBufferView->mByteOffset + indices.mByteOffset,
597 sparse.mCount * indices.GetBytesPerComponent(),
598 static_cast<uint16_t>(indices.mBufferView->mByteStride),
599 static_cast<uint16_t>(indices.GetBytesPerComponent()),
602 MeshDefinition::Blob valuesBlob(
603 values.mBufferView->mByteOffset + values.mByteOffset,
604 sparse.mCount * acc.GetElementSizeBytes(),
605 static_cast<uint16_t>(values.mBufferView->mByteStride),
606 static_cast<uint16_t>(acc.GetElementSizeBytes()),
610 sparseBlob = std::move(MeshDefinition::SparseBlob(std::move(indicesBlob), std::move(valuesBlob), acc.mSparse->mCount));
613 uint32_t bufferViewOffset = 0u;
614 uint32_t bufferViewStride = 0u;
617 bufferViewOffset = acc.mBufferView->mByteOffset;
618 bufferViewStride = acc.mBufferView->mByteStride;
621 return MeshDefinition::Accessor{
622 std::move(MeshDefinition::Blob{bufferViewOffset + acc.mByteOffset,
623 acc.GetBytesLength(),
624 static_cast<uint16_t>(bufferViewStride),
625 static_cast<uint16_t>(acc.GetElementSizeBytes()),
628 std::move(sparseBlob)};
631 void ConvertMeshes(const gt::Document& doc, ConversionContext& context)
633 uint32_t meshCount = 0;
634 context.mMeshIds.reserve(doc.mMeshes.size());
635 for(auto& mesh : doc.mMeshes)
637 context.mMeshIds.push_back(meshCount);
638 meshCount += mesh.mPrimitives.size();
641 auto& outMeshes = context.mOutput.mResources.mMeshes;
642 outMeshes.reserve(meshCount);
643 for(auto& mesh : doc.mMeshes)
645 for(auto& primitive : mesh.mPrimitives)
647 MeshDefinition meshDefinition;
649 auto& attribs = primitive.mAttributes;
650 meshDefinition.mUri = attribs.begin()->second->mBufferView->mBuffer->mUri;
651 meshDefinition.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[primitive.mMode];
653 auto& accPositions = *attribs.find(gt::Attribute::POSITION)->second;
654 meshDefinition.mPositions = ConvertMeshPrimitiveAccessor(accPositions);
655 // glTF2 support vector4 tangent for mesh.
656 // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#meshes-overview
657 meshDefinition.mTangentType = Property::VECTOR4;
659 const bool needNormalsTangents = accPositions.mType == gt::AccessorType::VEC3;
660 for(auto& am : ATTRIBUTE_MAPPINGS)
662 auto iFind = attribs.find(am.mType);
663 if(iFind != attribs.end())
665 DALI_ASSERT_DEBUG(iFind->second->mBufferView->mBuffer->mUri.compare(meshDefinition.mUri) == 0);
666 auto& accessor = meshDefinition.*(am.mAccessor);
667 accessor = ConvertMeshPrimitiveAccessor(*iFind->second);
669 if(iFind->first == gt::Attribute::JOINTS_0)
671 meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
672 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
675 else if(needNormalsTangents)
679 case gt::Attribute::NORMAL:
680 meshDefinition.RequestNormals();
683 case gt::Attribute::TANGENT:
684 meshDefinition.RequestTangents();
693 if(primitive.mIndices)
695 meshDefinition.mIndices = ConvertMeshPrimitiveAccessor(*primitive.mIndices);
696 meshDefinition.mFlags |= (primitive.mIndices->mComponentType == gt::Component::UNSIGNED_INT) * MeshDefinition::U32_INDICES;
697 DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U32_INDICES) || primitive.mIndices->mComponentType == gt::Component::UNSIGNED_SHORT);
700 if(!primitive.mTargets.empty())
702 meshDefinition.mBlendShapes.reserve(primitive.mTargets.size());
703 meshDefinition.mBlendShapeVersion = BlendShapes::Version::VERSION_2_0;
704 for(const auto& target : primitive.mTargets)
706 MeshDefinition::BlendShape blendShape;
708 auto endIt = target.end();
709 auto it = target.find(gt::Attribute::POSITION);
712 blendShape.deltas = ConvertMeshPrimitiveAccessor(*it->second);
714 it = target.find(gt::Attribute::NORMAL);
717 blendShape.normals = ConvertMeshPrimitiveAccessor(*it->second);
719 it = target.find(gt::Attribute::TANGENT);
722 blendShape.tangents = ConvertMeshPrimitiveAccessor(*it->second);
725 if(!mesh.mWeights.empty())
727 blendShape.weight = mesh.mWeights[meshDefinition.mBlendShapes.size()];
730 meshDefinition.mBlendShapes.push_back(std::move(blendShape));
734 outMeshes.push_back({std::move(meshDefinition), MeshGeometry{}});
739 ModelRenderable* MakeModelRenderable(const gt::Mesh::Primitive& prim, ConversionContext& context)
741 auto modelRenderable = new ModelRenderable();
743 modelRenderable->mShaderIdx = 0; // TODO: further thought
745 auto materialIdx = prim.mMaterial.GetIndex();
746 if(INVALID_INDEX == materialIdx)
748 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
749 if(INVALID_INDEX == context.mDefaultMaterial)
751 auto& outMaterials = context.mOutput.mResources.mMaterials;
752 context.mDefaultMaterial = outMaterials.size();
754 ConvertMaterial(gt::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials);
757 materialIdx = context.mDefaultMaterial;
760 modelRenderable->mMaterialIdx = materialIdx;
762 return modelRenderable;
765 void ConvertCamera(const gt::Camera& camera, CameraParameters& camParams)
767 camParams.isPerspective = camera.mType.compare("perspective") == 0;
768 if(camParams.isPerspective)
770 auto& perspective = camera.mPerspective;
771 camParams.yFov = Degree(Radian(perspective.mYFov)).degree;
772 camParams.zNear = perspective.mZNear;
773 camParams.zFar = perspective.mZFar;
774 // TODO: yes, we seem to ignore aspectRatio in CameraParameters.
778 auto& ortho = camera.mOrthographic;
779 camParams.orthographicSize = ortho.mYMag * .5f;
780 camParams.aspectRatio = ortho.mXMag / ortho.mYMag;
781 camParams.zNear = ortho.mZNear;
782 camParams.zFar = ortho.mZFar;
786 void ConvertNode(gt::Node const& node, const Index gltfIdx, Index parentIdx, ConversionContext& context, bool isMRendererModel)
788 auto& output = context.mOutput;
789 auto& scene = output.mScene;
790 auto& resources = output.mResources;
792 const auto idx = scene.GetNodeCount();
793 auto weakNode = scene.AddNode([&]() {
794 std::unique_ptr<NodeDefinition> nodeDef{new NodeDefinition()};
796 nodeDef->mParentIdx = parentIdx;
797 nodeDef->mName = node.mName;
798 if(nodeDef->mName.empty())
800 // TODO: Production quality generation of unique names.
801 nodeDef->mName = std::to_string(reinterpret_cast<uintptr_t>(nodeDef.get()));
804 if(!node.mSkin) // Nodes with skinned meshes are not supposed to have local transforms.
806 nodeDef->mPosition = node.mTranslation;
807 nodeDef->mOrientation = node.mRotation;
808 nodeDef->mScale = node.mScale;
810 if(isMRendererModel && node.mName == ROOT_NODE_NAME && node.mScale == SCALE_TO_ADJUST)
812 nodeDef->mScale *= 0.01f;
820 ExceptionFlinger(ASSERT_LOCATION) << "Node name '" << node.mName << "' is not unique; scene is invalid.";
823 context.mNodeIndices.RegisterMapping(gltfIdx, idx);
825 Index skeletonIdx = node.mSkin ? node.mSkin.GetIndex() : INVALID_INDEX;
828 auto& mesh = *node.mMesh;
829 uint32_t primitiveCount = mesh.mPrimitives.size();
830 auto meshIdx = context.mMeshIds[node.mMesh.GetIndex()];
831 weakNode->mRenderables.reserve(primitiveCount);
832 for(uint32_t i = 0; i < primitiveCount; ++i)
834 std::unique_ptr<NodeDefinition::Renderable> renderable;
835 auto modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
836 modelRenderable->mMeshIdx = meshIdx + i;
838 DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
839 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
840 resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx = skeletonIdx;
842 renderable.reset(modelRenderable);
843 weakNode->mRenderables.push_back(std::move(renderable));
849 CameraParameters camParams;
850 ConvertCamera(*node.mCamera, camParams);
852 camParams.matrix.SetTransformComponents(node.mScale, node.mRotation, node.mTranslation);
853 output.mCameraParameters.push_back(camParams);
856 for(auto& n : node.mChildren)
858 ConvertNode(*n, n.GetIndex(), idx, context, isMRendererModel);
862 void ConvertSceneNodes(const gt::Scene& scene, ConversionContext& context, bool isMRendererModel)
864 auto& outScene = context.mOutput.mScene;
865 Index rootIdx = outScene.GetNodeCount();
866 switch(scene.mNodes.size())
872 ConvertNode(*scene.mNodes[0], scene.mNodes[0].GetIndex(), INVALID_INDEX, context, isMRendererModel);
873 outScene.AddRootNode(rootIdx);
878 std::unique_ptr<NodeDefinition> sceneRoot{new NodeDefinition()};
879 sceneRoot->mName = "GLTF_LOADER_SCENE_ROOT_" + std::to_string(outScene.GetRoots().size());
881 outScene.AddNode(std::move(sceneRoot));
882 outScene.AddRootNode(rootIdx);
884 for(auto& n : scene.mNodes)
886 ConvertNode(*n, n.GetIndex(), rootIdx, context, isMRendererModel);
893 void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel)
895 if(!doc.mScenes.empty())
897 uint32_t rootSceneIndex = 0u;
900 rootSceneIndex = doc.mScene.GetIndex();
902 ConvertSceneNodes(doc.mScenes[rootSceneIndex], context, isMRendererModel);
904 for(uint32_t i = 0, i1 = rootSceneIndex; i < i1; ++i)
906 ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
909 for(uint32_t i = rootSceneIndex + 1; i < doc.mScenes.size(); ++i)
911 ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
917 void LoadDataFromAccessor(const std::string& path, Vector<T>& dataBuffer, uint32_t offset, uint32_t size)
919 std::ifstream animationBinaryFile(path, std::ifstream::binary);
921 if(!animationBinaryFile.is_open())
923 throw std::runtime_error("Failed to load " + path);
926 animationBinaryFile.seekg(offset);
927 animationBinaryFile.read(reinterpret_cast<char*>(dataBuffer.Begin()), size);
928 animationBinaryFile.close();
932 float LoadDataFromAccessors(const std::string& path, const gltf2::Accessor& input, const gltf2::Accessor& output, Vector<float>& inputDataBuffer, Vector<T>& outputDataBuffer)
934 inputDataBuffer.Resize(input.mCount);
935 outputDataBuffer.Resize(output.mCount);
937 const uint32_t inputDataBufferSize = input.GetBytesLength();
938 const uint32_t outputDataBufferSize = output.GetBytesLength();
940 LoadDataFromAccessor<float>(path + std::string(input.mBufferView->mBuffer->mUri), inputDataBuffer, input.mBufferView->mByteOffset + input.mByteOffset, inputDataBufferSize);
941 LoadDataFromAccessor<T>(path + std::string(output.mBufferView->mBuffer->mUri), outputDataBuffer, output.mBufferView->mByteOffset + output.mByteOffset, outputDataBufferSize);
942 ApplyAccessorMinMax(output, reinterpret_cast<float*>(outputDataBuffer.begin()));
944 return inputDataBuffer[input.mCount - 1u];
948 float LoadKeyFrames(const std::string& path, const gt::Animation::Channel& channel, KeyFrames& keyFrames, gt::Animation::Channel::Target::Type type)
950 const gltf2::Accessor& input = *channel.mSampler->mInput;
951 const gltf2::Accessor& output = *channel.mSampler->mOutput;
953 Vector<float> inputDataBuffer;
954 Vector<T> outputDataBuffer;
956 const float duration = LoadDataFromAccessors<T>(path, input, output, inputDataBuffer, outputDataBuffer);
958 for(uint32_t i = 0; i < input.mCount; ++i)
960 keyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i]);
966 float LoadBlendShapeKeyFrames(const std::string& path, const gt::Animation::Channel& channel, const std::string& nodeName, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
968 const gltf2::Accessor& input = *channel.mSampler->mInput;
969 const gltf2::Accessor& output = *channel.mSampler->mOutput;
971 Vector<float> inputDataBuffer;
972 Vector<float> outputDataBuffer;
974 const float duration = LoadDataFromAccessors<float>(path, input, output, inputDataBuffer, outputDataBuffer);
976 char weightNameBuffer[32];
977 auto prefixSize = snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s[", BLEND_SHAPE_WEIGHTS_UNIFORM.c_str());
978 char* const pWeightName = weightNameBuffer + prefixSize;
979 const auto remainingSize = sizeof(weightNameBuffer) - prefixSize;
980 for(uint32_t weightIndex = 0u, endWeightIndex = channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount; weightIndex < endWeightIndex; ++weightIndex)
982 AnimatedProperty& animatedProperty = properties[propertyIndex++];
984 animatedProperty.mNodeName = nodeName;
985 snprintf(pWeightName, remainingSize, "%d]", weightIndex);
986 animatedProperty.mPropertyName = std::string(weightNameBuffer);
988 animatedProperty.mKeyFrames = KeyFrames::New();
989 for(uint32_t i = 0; i < input.mCount; ++i)
991 animatedProperty.mKeyFrames.Add(inputDataBuffer[i] / duration, outputDataBuffer[i * endWeightIndex + weightIndex]);
994 animatedProperty.mTimePeriod = {0.f, duration};
1000 void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
1002 auto& output = context.mOutput;
1004 output.mAnimationDefinitions.reserve(output.mAnimationDefinitions.size() + doc.mAnimations.size());
1006 for(const auto& animation : doc.mAnimations)
1008 AnimationDefinition animationDef;
1010 if(!animation.mName.empty())
1012 animationDef.mName = animation.mName;
1015 uint32_t numberOfProperties = 0u;
1017 for(const auto& channel : animation.mChannels)
1019 numberOfProperties += channel.mSampler->mOutput->mCount;
1021 animationDef.mProperties.resize(numberOfProperties);
1023 Index propertyIndex = 0u;
1024 for(const auto& channel : animation.mChannels)
1026 std::string nodeName;
1027 if(!channel.mTarget.mNode->mName.empty())
1029 nodeName = channel.mTarget.mNode->mName;
1033 Index index = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
1034 nodeName = context.mOutput.mScene.GetNode(index)->mName;
1037 float duration = 0.f;
1039 switch(channel.mTarget.mPath)
1041 case gt::Animation::Channel::Target::TRANSLATION:
1043 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1045 animatedProperty.mNodeName = nodeName;
1046 animatedProperty.mPropertyName = POSITION_PROPERTY;
1048 animatedProperty.mKeyFrames = KeyFrames::New();
1049 duration = LoadKeyFrames<Vector3>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1051 animatedProperty.mTimePeriod = {0.f, duration};
1054 case gt::Animation::Channel::Target::ROTATION:
1056 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1058 animatedProperty.mNodeName = nodeName;
1059 animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
1061 animatedProperty.mKeyFrames = KeyFrames::New();
1062 duration = LoadKeyFrames<Quaternion>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1064 animatedProperty.mTimePeriod = {0.f, duration};
1067 case gt::Animation::Channel::Target::SCALE:
1069 AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
1071 animatedProperty.mNodeName = nodeName;
1072 animatedProperty.mPropertyName = SCALE_PROPERTY;
1074 animatedProperty.mKeyFrames = KeyFrames::New();
1075 duration = LoadKeyFrames<Vector3>(context.mPath, channel, animatedProperty.mKeyFrames, channel.mTarget.mPath);
1077 animatedProperty.mTimePeriod = {0.f, duration};
1080 case gt::Animation::Channel::Target::WEIGHTS:
1082 duration = LoadBlendShapeKeyFrames(context.mPath, channel, nodeName, propertyIndex, animationDef.mProperties);
1088 // nothing to animate.
1093 animationDef.mDuration = std::max(duration, animationDef.mDuration);
1098 output.mAnimationDefinitions.push_back(std::move(animationDef));
1102 void ProcessSkins(const gt::Document& doc, ConversionContext& context)
1104 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skininversebindmatrices
1105 // If an inverseBindMatrices accessor was provided, we'll load the joint data from the buffer,
1106 // otherwise we'll set identity matrices for inverse bind pose.
1107 struct IInverseBindMatrixProvider
1109 virtual ~IInverseBindMatrixProvider()
1112 virtual void Provide(Matrix& ibm) = 0;
1115 struct InverseBindMatrixAccessor : public IInverseBindMatrixProvider
1117 std::ifstream mStream;
1118 const uint32_t mElementSizeBytes;
1120 InverseBindMatrixAccessor(const gt::Accessor& accessor, const std::string& path)
1121 : mStream(path + std::string(accessor.mBufferView->mBuffer->mUri), std::ios::binary),
1122 mElementSizeBytes(accessor.GetElementSizeBytes())
1124 DALI_ASSERT_ALWAYS(mStream);
1125 DALI_ASSERT_DEBUG(accessor.mType == gt::AccessorType::MAT4 && accessor.mComponentType == gt::Component::FLOAT);
1127 mStream.seekg(accessor.mBufferView->mByteOffset + accessor.mByteOffset);
1130 virtual void Provide(Matrix& ibm) override
1132 DALI_ASSERT_ALWAYS(mStream.read(reinterpret_cast<char*>(ibm.AsFloat()), mElementSizeBytes));
1136 struct DefaultInverseBindMatrixProvider : public IInverseBindMatrixProvider
1138 virtual void Provide(Matrix& ibm) override
1140 ibm = Matrix::IDENTITY;
1144 auto& resources = context.mOutput.mResources;
1145 resources.mSkeletons.reserve(doc.mSkins.size());
1147 for(auto& s : doc.mSkins)
1149 std::unique_ptr<IInverseBindMatrixProvider> ibmProvider;
1150 if(s.mInverseBindMatrices)
1152 ibmProvider.reset(new InverseBindMatrixAccessor(*s.mInverseBindMatrices, context.mPath));
1156 ibmProvider.reset(new DefaultInverseBindMatrixProvider());
1159 SkeletonDefinition skeleton;
1160 if(s.mSkeleton.GetIndex() != INVALID_INDEX)
1162 skeleton.mRootNodeIdx = context.mNodeIndices.GetRuntimeId(s.mSkeleton.GetIndex());
1165 skeleton.mJoints.resize(s.mJoints.size());
1166 auto iJoint = skeleton.mJoints.begin();
1167 for(auto& j : s.mJoints)
1169 iJoint->mNodeIdx = context.mNodeIndices.GetRuntimeId(j.GetIndex());
1171 ibmProvider->Provide(iJoint->mInverseBindMatrix);
1176 resources.mSkeletons.push_back(std::move(skeleton));
1180 void ProduceShaders(ShaderDefinitionFactory& shaderFactory, SceneDefinition& scene)
1182 uint32_t nodeCount = scene.GetNodeCount();
1183 for(uint32_t i = 0; i < nodeCount; ++i)
1185 auto nodeDef = scene.GetNode(i);
1186 for(auto& renderable : nodeDef->mRenderables)
1188 if(shaderFactory.ProduceShader(*renderable) == INVALID_INDEX)
1190 DALI_LOG_ERROR("Fail to produce shader\n");
1196 void SetObjectReaders()
1198 js::SetObjectReader(BUFFER_READER);
1199 js::SetObjectReader(BUFFER_VIEW_READER);
1200 js::SetObjectReader(BUFFER_VIEW_CLIENT_READER);
1201 js::SetObjectReader(COMPONENT_TYPED_BUFFER_VIEW_CLIENT_READER);
1202 js::SetObjectReader(ACCESSOR_SPARSE_READER);
1203 js::SetObjectReader(ACCESSOR_READER);
1204 js::SetObjectReader(IMAGE_READER);
1205 js::SetObjectReader(SAMPLER_READER);
1206 js::SetObjectReader(TEXURE_READER);
1207 js::SetObjectReader(TEXURE_INFO_READER);
1208 js::SetObjectReader(MATERIAL_PBR_READER);
1209 js::SetObjectReader(MATERIAL_SPECULAR_READER);
1210 js::SetObjectReader(MATERIAL_IOR_READER);
1211 js::SetObjectReader(MATERIAL_EXTENSION_READER);
1212 js::SetObjectReader(MATERIAL_READER);
1213 js::SetObjectReader(MESH_PRIMITIVE_READER);
1214 js::SetObjectReader(MESH_READER);
1215 js::SetObjectReader(SKIN_READER);
1216 js::SetObjectReader(CAMERA_PERSPECTIVE_READER);
1217 js::SetObjectReader(CAMERA_ORTHOGRAPHIC_READER);
1218 js::SetObjectReader(CAMERA_READER);
1219 js::SetObjectReader(NODE_READER);
1220 js::SetObjectReader(ANIMATION_SAMPLER_READER);
1221 js::SetObjectReader(ANIMATION_TARGET_READER);
1222 js::SetObjectReader(ANIMATION_CHANNEL_READER);
1223 js::SetObjectReader(ANIMATION_READER);
1224 js::SetObjectReader(SCENE_READER);
1227 void SetDefaultEnvironmentMap(const gt::Document& doc, ConversionContext& context)
1229 EnvironmentDefinition envDef;
1230 envDef.mUseBrdfTexture = true;
1231 envDef.mIblIntensity = Scene3D::Loader::EnvironmentDefinition::GetDefaultIntensity();
1232 context.mOutput.mResources.mEnvironmentMaps.push_back({std::move(envDef), EnvironmentDefinition::Textures()});
1237 void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
1239 bool failed = false;
1240 auto js = LoadTextFile(url.c_str(), &failed);
1243 throw std::runtime_error("Failed to load " + url);
1246 json::unique_ptr root(json_parse(js.c_str(), js.size()));
1249 throw std::runtime_error("Failed to parse " + url);
1252 static bool setObjectReaders = true;
1253 if(setObjectReaders)
1255 // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
1257 setObjectReaders = false;
1262 auto& rootObj = js::Cast<json_object_s>(*root);
1263 auto jsAsset = js::FindObjectChild("asset", rootObj);
1265 auto jsAssetVersion = js::FindObjectChild("version", js::Cast<json_object_s>(*jsAsset));
1268 doc.mAsset.mVersion = js::Read::StringView(*jsAssetVersion);
1271 bool isMRendererModel(false);
1272 auto jsAssetGenerator = js::FindObjectChild("generator", js::Cast<json_object_s>(*jsAsset));
1273 if(jsAssetGenerator)
1275 doc.mAsset.mGenerator = js::Read::StringView(*jsAssetGenerator);
1276 isMRendererModel = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
1279 gt::SetRefReaderObject(doc);
1280 DOCUMENT_READER.Read(rootObj, doc);
1282 auto path = url.substr(0, url.rfind('/') + 1);
1283 ConversionContext context{params, path, INVALID_INDEX};
1285 ConvertMaterials(doc, context);
1286 ConvertMeshes(doc, context);
1287 ConvertNodes(doc, context, isMRendererModel);
1288 ConvertAnimations(doc, context);
1289 ProcessSkins(doc, context);
1290 ProduceShaders(shaderFactory, params.mScene);
1291 params.mScene.EnsureUniqueSkinningShaderInstances(params.mResources);
1293 // Set Default Environment map
1294 SetDefaultEnvironmentMap(doc, context);
1297 } // namespace Loader
1298 } // namespace Scene3D