DALi Version 2.2.11
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / gltf2-loader.cpp
index 36c1639..90002b3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2022 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  *
  */
-#include <fstream>
+
+// FILE HEADER
 #include <dali-scene3d/public-api/loader/gltf2-loader.h>
+
+// EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
+#include <dali/public-api/images/image-operations.h>
+#include <dali/public-api/math/quaternion.h>
+#include <dali/devel-api/threading/mutex.h>
+#include <fstream>
+
+// INTERNAL INCLUDES
 #include <dali-scene3d/internal/loader/gltf2-asset.h>
 #include <dali-scene3d/public-api/loader/load-result.h>
 #include <dali-scene3d/public-api/loader/resource-bundle.h>
 #include <dali-scene3d/public-api/loader/scene-definition.h>
 #include <dali-scene3d/public-api/loader/shader-definition-factory.h>
 #include <dali-scene3d/public-api/loader/utils.h>
-#include <dali/public-api/math/quaternion.h>
-#include <dali/integration-api/debug.h>
-
-#define ENUM_STRING_MAPPING(t, x) \
-  {                               \
-#x, t::x                      \
-  }
 
 namespace gt = gltf2;
 namespace js = json;
@@ -41,6 +44,9 @@ namespace Loader
 {
 namespace
 {
+Dali::Mutex gInitializeMutex;
+Dali::Mutex gReadMutex;
+
 const std::string POSITION_PROPERTY("position");
 const std::string ORIENTATION_PROPERTY("orientation");
 const std::string SCALE_PROPERTY("scale");
@@ -74,9 +80,6 @@ struct AttributeMapping
 
 std::vector<gt::Animation> ReadAnimationArray(const json_value_s& j)
 {
-  gt::Animation proxy;
-  SetRefReaderObject(proxy);
-
   auto results = js::Read::Array<gt::Animation, js::ObjectReader<gt::Animation>::Read>(j);
 
   for(auto& animation : results)
@@ -169,6 +172,19 @@ const auto MATERIAL_PBR_READER = std::move(js::Reader<gt::Material::Pbr>()
                                              .Register(*js::MakeProperty("roughnessFactor", js::Read::Number<float>, &gt::Material::Pbr::mRoughnessFactor))
                                              .Register(*js::MakeProperty("metallicRoughnessTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::Material::Pbr::mMetallicRoughnessTexture)));
 
+const auto MATERIAL_SPECULAR_READER = std::move(js::Reader<gt::MaterialSpecular>()
+                                                  .Register(*js::MakeProperty("specularFactor", js::Read::Number<float>, &gt::MaterialSpecular::mSpecularFactor))
+                                                  .Register(*js::MakeProperty("specularTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::MaterialSpecular::mSpecularTexture))
+                                                  .Register(*js::MakeProperty("specularColorFactor", gt::ReadDaliVector<Vector3>, &gt::MaterialSpecular::mSpecularColorFactor))
+                                                  .Register(*js::MakeProperty("specularColorTexture", js::ObjectReader<gt::TextureInfo>::Read, &gt::MaterialSpecular::mSpecularColorTexture)));
+
+const auto MATERIAL_IOR_READER = std::move(js::Reader<gt::MaterialIor>()
+                                             .Register(*js::MakeProperty("ior", js::Read::Number<float>, &gt::MaterialIor::mIor)));
+
+const auto MATERIAL_EXTENSION_READER = std::move(js::Reader<gt::MaterialExtensions>()
+                                                   .Register(*js::MakeProperty("KHR_materials_ior", js::ObjectReader<gt::MaterialIor>::Read, &gt::MaterialExtensions::mMaterialIor))
+                                                   .Register(*js::MakeProperty("KHR_materials_specular", js::ObjectReader<gt::MaterialSpecular>::Read, &gt::MaterialExtensions::mMaterialSpecular)));
+
 const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
                                          .Register(*new js::Property<gt::Material, std::string_view>("name", js::Read::StringView, &gt::Material::mName))
                                          .Register(*js::MakeProperty("pbrMetallicRoughness", js::ObjectReader<gt::Material::Pbr>::Read, &gt::Material::mPbrMetallicRoughness))
@@ -178,7 +194,8 @@ const auto MATERIAL_READER = std::move(js::Reader<gt::Material>()
                                          .Register(*js::MakeProperty("emissiveFactor", gt::ReadDaliVector<Vector3>, &gt::Material::mEmissiveFactor))
                                          .Register(*js::MakeProperty("alphaMode", gt::ReadStringEnum<gt::AlphaMode>, &gt::Material::mAlphaMode))
                                          .Register(*js::MakeProperty("alphaCutoff", js::Read::Number<float>, &gt::Material::mAlphaCutoff))
-                                         .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, &gt::Material::mDoubleSided)));
+                                         .Register(*js::MakeProperty("doubleSided", js::Read::Boolean, &gt::Material::mDoubleSided))
+                                         .Register(*js::MakeProperty("extensions", js::ObjectReader<gt::MaterialExtensions>::Read, &gt::Material::mMaterialExtensions)));
 
 std::map<gt::Attribute::Type, gt::Ref<gt::Accessor>> ReadMeshPrimitiveAttributes(const json_value_s& j)
 {
@@ -409,7 +426,10 @@ SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& s)
 {
   if(s)
   {
-    return (s->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (s->mMinFilter - gt::Filter::NEAREST) : ((s->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2) | ((s->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) | (ConvertWrapMode(s->mWrapS) << SamplerFlags::WRAP_S_SHIFT) | (ConvertWrapMode(s->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
+    return ((s->mMinFilter < gt::Filter::NEAREST_MIPMAP_NEAREST) ? (s->mMinFilter - gt::Filter::NEAREST) : ((s->mMinFilter - gt::Filter::NEAREST_MIPMAP_NEAREST) + 2)) |
+           ((s->mMagFilter - gt::Filter::NEAREST) << SamplerFlags::FILTER_MAG_SHIFT) |
+           (ConvertWrapMode(s->mWrapS) << SamplerFlags::WRAP_S_SHIFT) |
+           (ConvertWrapMode(s->mWrapT) << SamplerFlags::WRAP_T_SHIFT);
   }
   else
   {
@@ -422,23 +442,35 @@ SamplerFlags::Type ConvertSampler(const gt::Ref<gt::Sampler>& s)
   }
 }
 
-TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm)
+TextureDefinition ConvertTextureInfo(const gt::TextureInfo& mm, const ImageMetadata& metaData = ImageMetadata())
 {
-  return TextureDefinition{std::string(mm.mTexture->mSource->mUri), ConvertSampler(mm.mTexture->mSampler)};
+  return TextureDefinition{std::string(mm.mTexture->mSource->mUri), ConvertSampler(mm.mTexture->mSampler), metaData.mMinSize, metaData.mSamplingMode};
 }
 
-void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMaterials)& outMaterials)
+void ConvertMaterial(const gt::Material& material, const std::unordered_map<std::string, ImageMetadata>& imageMetaData, decltype(ResourceBundle::mMaterials)& outMaterials)
 {
+  auto getTextureMetaData = [](const std::unordered_map<std::string, ImageMetadata>& metaData, const gt::TextureInfo& info) {
+    if(auto search = metaData.find(info.mTexture->mSource->mUri.data()); search != metaData.end())
+    {
+      return search->second;
+    }
+    else
+    {
+      return ImageMetadata();
+    }
+  };
+
   MaterialDefinition matDef;
 
   auto& pbr = material.mPbrMetallicRoughness;
-  if(pbr.mBaseColorFactor.a < 1.f)
+  if(material.mAlphaMode == gt::AlphaMode::BLEND)
   {
+    matDef.mIsOpaque = false;
     matDef.mFlags |= MaterialDefinition::TRANSPARENCY;
   }
-
-  if(material.mAlphaMode == gt::AlphaMode::MASK)
+  else if(material.mAlphaMode == gt::AlphaMode::MASK)
   {
+    matDef.mIsMask = true;
     matDef.SetAlphaCutoff(std::min(1.f, std::max(0.f, material.mAlphaCutoff)));
   }
 
@@ -448,7 +480,7 @@ void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMat
   if(pbr.mBaseColorTexture)
   {
     const auto semantic = MaterialDefinition::ALBEDO;
-    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture)});
+    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mBaseColorTexture, getTextureMetaData(imageMetaData, pbr.mBaseColorTexture))});
     // TODO: and there had better be one
     matDef.mFlags |= semantic;
   }
@@ -464,7 +496,7 @@ void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMat
   {
     const auto semantic = MaterialDefinition::METALLIC | MaterialDefinition::ROUGHNESS |
                           MaterialDefinition::GLTF_CHANNELS;
-    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture)});
+    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(pbr.mMetallicRoughnessTexture, getTextureMetaData(imageMetaData, pbr.mMetallicRoughnessTexture))});
     // TODO: and there had better be one
     matDef.mFlags |= semantic;
   }
@@ -477,7 +509,7 @@ void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMat
   if(material.mNormalTexture)
   {
     const auto semantic = MaterialDefinition::NORMAL;
-    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture)});
+    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mNormalTexture, getTextureMetaData(imageMetaData, material.mNormalTexture))});
     // TODO: and there had better be one
     matDef.mFlags |= semantic;
   }
@@ -489,7 +521,7 @@ void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMat
   if(material.mOcclusionTexture)
   {
     const auto semantic = MaterialDefinition::OCCLUSION;
-    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture)});
+    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mOcclusionTexture, getTextureMetaData(imageMetaData, material.mOcclusionTexture))});
     // TODO: and there had better be one
     matDef.mFlags |= semantic;
     matDef.mOcclusionStrength = material.mOcclusionTexture.mStrength;
@@ -498,12 +530,34 @@ void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMat
   if(material.mEmissiveTexture)
   {
     const auto semantic = MaterialDefinition::EMISSIVE;
-    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture)});
+    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mEmissiveTexture, getTextureMetaData(imageMetaData, material.mEmissiveTexture))});
     // TODO: and there had better be one
     matDef.mFlags |= semantic;
     matDef.mEmissiveFactor = material.mEmissiveFactor;
   }
 
+  if(material.mMaterialExtensions.mMaterialIor.mIor < MAXFLOAT)
+  {
+    float ior                  = material.mMaterialExtensions.mMaterialIor.mIor;
+    matDef.mDielectricSpecular = powf((ior - 1.0f) / (ior + 1.0f), 2.0f);
+  }
+  matDef.mSpecularFactor      = material.mMaterialExtensions.mMaterialSpecular.mSpecularFactor;
+  matDef.mSpecularColorFactor = material.mMaterialExtensions.mMaterialSpecular.mSpecularColorFactor;
+
+  if(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture)
+  {
+    const auto semantic = MaterialDefinition::SPECULAR;
+    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularTexture))});
+    matDef.mFlags |= semantic;
+  }
+
+  if(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture)
+  {
+    const auto semantic = MaterialDefinition::SPECULAR_COLOR;
+    matDef.mTextureStages.push_back({semantic, ConvertTextureInfo(material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture, getTextureMetaData(imageMetaData, material.mMaterialExtensions.mMaterialSpecular.mSpecularColorTexture))});
+    matDef.mFlags |= semantic;
+  }
+
   matDef.mDoubleSided = material.mDoubleSided;
 
   outMaterials.emplace_back(std::move(matDef), TextureSet());
@@ -511,12 +565,14 @@ void ConvertMaterial(const gt::Material& material, decltype(ResourceBundle::mMat
 
 void ConvertMaterials(const gt::Document& doc, ConversionContext& context)
 {
+  auto& imageMetaData = context.mOutput.mSceneMetadata.mImageMetadata;
+
   auto& outMaterials = context.mOutput.mResources.mMaterials;
   outMaterials.reserve(doc.mMaterials.size());
 
   for(auto& m : doc.mMaterials)
   {
-    ConvertMaterial(m, outMaterials);
+    ConvertMaterial(m, imageMetaData, outMaterials);
   }
 }
 
@@ -591,11 +647,11 @@ void ConvertMeshes(const gt::Document& doc, ConversionContext& context)
     {
       MeshDefinition meshDefinition;
 
-      auto& attribs          = primitive.mAttributes;
+      auto& attribs                 = primitive.mAttributes;
       meshDefinition.mUri           = attribs.begin()->second->mBufferView->mBuffer->mUri;
       meshDefinition.mPrimitiveType = GLTF2_TO_DALI_PRIMITIVES[primitive.mMode];
 
-      auto& accPositions = *attribs.find(gt::Attribute::POSITION)->second;
+      auto& accPositions        = *attribs.find(gt::Attribute::POSITION)->second;
       meshDefinition.mPositions = ConvertMeshPrimitiveAccessor(accPositions);
       // glTF2 support vector4 tangent for mesh.
       // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#meshes-overview
@@ -614,7 +670,8 @@ void ConvertMeshes(const gt::Document& doc, ConversionContext& context)
           if(iFind->first == gt::Attribute::JOINTS_0)
           {
             meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_SHORT) * MeshDefinition::U16_JOINT_IDS;
-            DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
+            meshDefinition.mFlags |= (iFind->second->mComponentType == gt::Component::UNSIGNED_BYTE) * MeshDefinition::U8_JOINT_IDS;
+            DALI_ASSERT_DEBUG(MaskMatch(meshDefinition.mFlags, MeshDefinition::U16_JOINT_IDS) || MaskMatch(meshDefinition.mFlags, MeshDefinition::U8_JOINT_IDS) || iFind->second->mComponentType == gt::Component::FLOAT);
           }
         }
         else if(needNormalsTangents)
@@ -693,10 +750,10 @@ ModelRenderable* MakeModelRenderable(const gt::Mesh::Primitive& prim, Conversion
     // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#default-material
     if(INVALID_INDEX == context.mDefaultMaterial)
     {
-      auto& outMaterials    = context.mOutput.mResources.mMaterials;
+      auto& outMaterials       = context.mOutput.mResources.mMaterials;
       context.mDefaultMaterial = outMaterials.size();
 
-      ConvertMaterial(gt::Material{}, outMaterials);
+      ConvertMaterial(gt::Material{}, context.mOutput.mSceneMetadata.mImageMetadata, outMaterials);
     }
 
     materialIdx = context.mDefaultMaterial;
@@ -777,8 +834,8 @@ void ConvertNode(gt::Node const& node, const Index gltfIdx, Index parentIdx, Con
     for(uint32_t i = 0; i < primitiveCount; ++i)
     {
       std::unique_ptr<NodeDefinition::Renderable> renderable;
-      auto modelRenderable      = MakeModelRenderable(mesh.mPrimitives[i], context);
-      modelRenderable->mMeshIdx = meshIdx + i;
+      auto                                        modelRenderable = MakeModelRenderable(mesh.mPrimitives[i], context);
+      modelRenderable->mMeshIdx                                   = meshIdx + i;
 
       DALI_ASSERT_DEBUG(resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == INVALID_INDEX ||
                         resources.mMeshes[modelRenderable->mMeshIdx].first.mSkeletonIdx == skeletonIdx);
@@ -837,16 +894,24 @@ void ConvertSceneNodes(const gt::Scene& scene, ConversionContext& context, bool
 
 void ConvertNodes(const gt::Document& doc, ConversionContext& context, bool isMRendererModel)
 {
-  ConvertSceneNodes(*doc.mScene, context, isMRendererModel);
-
-  for(uint32_t i = 0, i1 = doc.mScene.GetIndex(); i < i1; ++i)
+  if(!doc.mScenes.empty())
   {
-    ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
-  }
+    uint32_t rootSceneIndex = 0u;
+    if(doc.mScene)
+    {
+      rootSceneIndex = doc.mScene.GetIndex();
+    }
+    ConvertSceneNodes(doc.mScenes[rootSceneIndex], context, isMRendererModel);
 
-  for(uint32_t i = doc.mScene.GetIndex() + 1; i < doc.mScenes.size(); ++i)
-  {
-    ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
+    for(uint32_t i = 0, i1 = rootSceneIndex; i < i1; ++i)
+    {
+      ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
+    }
+
+    for(uint32_t i = rootSceneIndex + 1; i < doc.mScenes.size(); ++i)
+    {
+      ConvertSceneNodes(doc.mScenes[i], context, isMRendererModel);
+    }
   }
 }
 
@@ -890,7 +955,7 @@ float LoadKeyFrames(const std::string& path, const gt::Animation::Channel& chann
   Vector<float> inputDataBuffer;
   Vector<T>     outputDataBuffer;
 
-  const float duration = LoadDataFromAccessors<T>(path, input, output, inputDataBuffer, outputDataBuffer);
+  const float duration = std::max(LoadDataFromAccessors<T>(path, input, output, inputDataBuffer, outputDataBuffer), AnimationDefinition::MIN_DURATION_SECONDS);
 
   for(uint32_t i = 0; i < input.mCount; ++i)
   {
@@ -900,7 +965,7 @@ float LoadKeyFrames(const std::string& path, const gt::Animation::Channel& chann
   return duration;
 }
 
-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)
+float LoadBlendShapeKeyFrames(const std::string& path, const gt::Animation::Channel& channel, Index nodeIndex, uint32_t& propertyIndex, std::vector<Dali::Scene3D::Loader::AnimatedProperty>& properties)
 {
   const gltf2::Accessor& input  = *channel.mSampler->mInput;
   const gltf2::Accessor& output = *channel.mSampler->mOutput;
@@ -918,7 +983,7 @@ float LoadBlendShapeKeyFrames(const std::string& path, const gt::Animation::Chan
   {
     AnimatedProperty& animatedProperty = properties[propertyIndex++];
 
-    animatedProperty.mNodeName = nodeName;
+    animatedProperty.mNodeIndex = nodeIndex;
     snprintf(pWeightName, remainingSize, "%d]", weightIndex);
     animatedProperty.mPropertyName = std::string(weightNameBuffer);
 
@@ -950,28 +1015,24 @@ void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
     }
 
     uint32_t numberOfProperties = 0u;
-
-    for(const auto& channel : animation.mChannels)
-    {
-      numberOfProperties += channel.mSampler->mOutput->mCount;
-    }
-    animationDef.mProperties.resize(numberOfProperties);
-
-    Index propertyIndex = 0u;
     for(const auto& channel : animation.mChannels)
     {
-      std::string nodeName;
-      if(!channel.mTarget.mNode->mName.empty())
+      if(channel.mTarget.mPath == gt::Animation::Channel::Target::WEIGHTS)
       {
-        nodeName = channel.mTarget.mNode->mName;
+        numberOfProperties += channel.mSampler->mOutput->mCount / channel.mSampler->mInput->mCount;
       }
       else
       {
-        Index index = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
-        nodeName    = context.mOutput.mScene.GetNode(index)->mName;
+        numberOfProperties++;
       }
+    }
+    animationDef.mProperties.resize(numberOfProperties);
 
-      float duration = 0.f;
+    Index propertyIndex = 0u;
+    for(const auto& channel : animation.mChannels)
+    {
+      Index nodeIndex = context.mNodeIndices.GetRuntimeId(channel.mTarget.mNode.GetIndex());
+      float duration  = 0.f;
 
       switch(channel.mTarget.mPath)
       {
@@ -979,7 +1040,7 @@ void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
         {
           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
 
-          animatedProperty.mNodeName     = nodeName;
+          animatedProperty.mNodeIndex    = nodeIndex;
           animatedProperty.mPropertyName = POSITION_PROPERTY;
 
           animatedProperty.mKeyFrames = KeyFrames::New();
@@ -992,7 +1053,7 @@ void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
         {
           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
 
-          animatedProperty.mNodeName     = nodeName;
+          animatedProperty.mNodeIndex    = nodeIndex;
           animatedProperty.mPropertyName = ORIENTATION_PROPERTY;
 
           animatedProperty.mKeyFrames = KeyFrames::New();
@@ -1005,7 +1066,7 @@ void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
         {
           AnimatedProperty& animatedProperty = animationDef.mProperties[propertyIndex];
 
-          animatedProperty.mNodeName     = nodeName;
+          animatedProperty.mNodeIndex    = nodeIndex;
           animatedProperty.mPropertyName = SCALE_PROPERTY;
 
           animatedProperty.mKeyFrames = KeyFrames::New();
@@ -1016,7 +1077,7 @@ void ConvertAnimations(const gt::Document& doc, ConversionContext& context)
         }
         case gt::Animation::Channel::Target::WEIGHTS:
         {
-          duration = LoadBlendShapeKeyFrames(context.mPath, channel, nodeName, propertyIndex, animationDef.mProperties);
+          duration = LoadBlendShapeKeyFrames(context.mPath, channel, nodeIndex, propertyIndex, animationDef.mProperties);
 
           break;
         }
@@ -1143,6 +1204,9 @@ void SetObjectReaders()
   js::SetObjectReader(TEXURE_READER);
   js::SetObjectReader(TEXURE_INFO_READER);
   js::SetObjectReader(MATERIAL_PBR_READER);
+  js::SetObjectReader(MATERIAL_SPECULAR_READER);
+  js::SetObjectReader(MATERIAL_IOR_READER);
+  js::SetObjectReader(MATERIAL_EXTENSION_READER);
   js::SetObjectReader(MATERIAL_READER);
   js::SetObjectReader(MESH_PRIMITIVE_READER);
   js::SetObjectReader(MESH_READER);
@@ -1168,6 +1232,21 @@ void SetDefaultEnvironmentMap(const gt::Document& doc, ConversionContext& contex
 
 } // namespace
 
+void InitializeGltfLoader()
+{
+  // Set ObjectReader only once (for all gltf loading).
+  static bool setObjectReadersRequired = true;
+  {
+    Mutex::ScopedLock lock(gInitializeMutex);
+    if(setObjectReadersRequired)
+    {
+      // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
+      SetObjectReaders();
+      setObjectReadersRequired = false;
+    }
+  }
+}
+
 void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactory, LoadResult& params)
 {
   bool failed = false;
@@ -1183,14 +1262,6 @@ void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactor
     throw std::runtime_error("Failed to parse " + url);
   }
 
-  static bool setObjectReaders = true;
-  if(setObjectReaders)
-  {
-    // NOTE: only referencing own, anonymous namespace, const objects; the pointers will never need to change.
-    SetObjectReaders();
-    setObjectReaders = false;
-  }
-
   gt::Document doc;
 
   auto& rootObj = js::Cast<json_object_s>(*root);
@@ -1210,8 +1281,11 @@ void LoadGltfScene(const std::string& url, ShaderDefinitionFactory& shaderFactor
     isMRendererModel      = (doc.mAsset.mGenerator.find(MRENDERER_MODEL_IDENTIFICATION) != std::string_view::npos);
   }
 
-  gt::SetRefReaderObject(doc);
-  DOCUMENT_READER.Read(rootObj, doc);
+  {
+    Mutex::ScopedLock lock(gReadMutex);
+    gt::SetRefReaderObject(doc);
+    DOCUMENT_READER.Read(rootObj, doc);
+  }
 
   auto              path = url.substr(0, url.rfind('/') + 1);
   ConversionContext context{params, path, INVALID_INDEX};