[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-asset.cpp
1 /*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 *
16 */
17
18 // EXTERNAL INCLUDES
19 #include <dali/public-api/math/matrix.h>
20 #include <algorithm>
21 #include <map>
22
23 // INTERNAL INCLUDES
24 #include <dali-scene3d/internal/loader/gltf2-asset.h>
25
26 using namespace Dali;
27
28 namespace gltf2
29 {
30 namespace
31 {
32 constexpr uint32_t ACCESSOR_TYPE_ELEMENT_COUNT[]{
33   1,
34   2,
35   3,
36   4,
37   4,
38   9,
39   16,
40   static_cast<uint32_t>(-1)};
41
42 const std::map<std::string_view, AccessorType::Type>& GetAccessorTypes()
43 {
44   static const std::map<std::string_view, AccessorType::Type> ACCESSOR_TYPES{
45     ENUM_STRING_MAPPING(AccessorType, SCALAR),
46     ENUM_STRING_MAPPING(AccessorType, VEC2),
47     ENUM_STRING_MAPPING(AccessorType, VEC3),
48     ENUM_STRING_MAPPING(AccessorType, VEC4),
49     ENUM_STRING_MAPPING(AccessorType, MAT2),
50     ENUM_STRING_MAPPING(AccessorType, MAT3),
51     ENUM_STRING_MAPPING(AccessorType, MAT4),
52   };
53   return ACCESSOR_TYPES;
54 }
55
56 const std::map<std::string_view, AlphaMode::Type>& GetAlphaModeTypes()
57 {
58   static const std::map<std::string_view, AlphaMode::Type> ALPHA_MODE_TYPES{
59     ENUM_STRING_MAPPING(AlphaMode::Type, OPAQUE),
60     ENUM_STRING_MAPPING(AlphaMode::Type, MASK),
61     ENUM_STRING_MAPPING(AlphaMode::Type, BLEND),
62   };
63   return ALPHA_MODE_TYPES;
64 }
65
66 const std::map<std::string_view, Animation::Sampler::Interpolation::Type>& GetAnimationSamplerInterpolation()
67 {
68   static const std::map<std::string_view, Animation::Sampler::Interpolation::Type> ANIMATION_SAMPLER_INTERPOLATION{
69     ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, STEP),
70     ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, LINEAR),
71     ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, CUBICSPLINE),
72   };
73   return ANIMATION_SAMPLER_INTERPOLATION;
74 }
75
76 const std::map<std::string_view, Animation::Channel::Target::Type>& GetAnimationChannelTargetPathTypes()
77 {
78   static const std::map<std::string_view, Animation::Channel::Target::Type> ANIMATION_CHANNEL_TARGET_PATH_TYPES{
79     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, TRANSLATION),
80     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, ROTATION),
81     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, SCALE),
82     ENUM_STRING_MAPPING(Animation::Channel::Target::Type, WEIGHTS),
83   };
84   return ANIMATION_CHANNEL_TARGET_PATH_TYPES;
85 }
86
87 } // namespace
88
89 ENUM_TYPE_FROM_STRING(AccessorType, GetAccessorTypes())
90 ENUM_TYPE_FROM_STRING(AlphaMode, GetAlphaModeTypes())
91 ENUM_TYPE_FROM_STRING(Animation::Sampler::Interpolation, GetAnimationSamplerInterpolation())
92 ENUM_TYPE_FROM_STRING(Animation::Channel::Target, GetAnimationChannelTargetPathTypes())
93
94 const std::map<std::string_view, Attribute::Type>& GetTargetTypes()
95 {
96   static const std::map<std::string_view, Attribute::Type> TARGET_TYPES{
97     ENUM_STRING_MAPPING(Attribute::Type, POSITION),
98     ENUM_STRING_MAPPING(Attribute::Type, TANGENT),
99     ENUM_STRING_MAPPING(Attribute::Type, NORMAL),
100   };
101   return TARGET_TYPES;
102 }
103
104 const std::map<Attribute::Type, const char*>& GetAttributeSetTypes()
105 {
106   static const std::map<Attribute::Type, const char*> ATTRIBUTE_SET_TYPES{
107     {Attribute::Type::TEXCOORD_N, "TEXCOORD_%u"},
108     {Attribute::Type::COLOR_N, "COLOR_%u"},
109     {Attribute::Type::JOINTS_N, "JOINTS_%u"},
110     {Attribute::Type::WEIGHTS_N, "WEIGHTS_%u"},
111   };
112   return ATTRIBUTE_SET_TYPES;
113 }
114
115 uint32_t Attribute::HashFromString(const char* token, size_t length)
116 {
117   auto& table1 = GetTargetTypes();
118   auto& table2 = GetAttributeSetTypes();
119
120   std::string target(token, length);
121   std::transform(target.begin(), target.end(), target.begin(), ::toupper);
122
123   auto iFind = table1.find(std::string_view(target.c_str(), length));
124   if(iFind != table1.end())
125   {
126     return Attribute::ToHash(iFind->second, false, 0);
127   }
128
129   uint32_t hash = Attribute::ToHash(Attribute::INVALID, false, 0);
130   for(const auto& [key, match] : table2)
131   {
132     int setIndex;
133     if(sscanf(target.c_str(), match, &setIndex) > 0)
134     {
135       hash = Attribute::ToHash(key, true, setIndex);
136       break;
137     }
138   }
139   return hash;
140 }
141
142 Attribute::Type Attribute::TargetFromString(const char* token, size_t length)
143 {
144   std::string target(token, length);
145   std::transform(target.begin(), target.end(), target.begin(), ::toupper);
146
147   auto iFind = GetTargetTypes().find(std::string_view(target.c_str(), length));
148   if(iFind != GetTargetTypes().end())
149   {
150     return iFind->second;
151   }
152   return Attribute::INVALID;
153 }
154
155 bool Component::IsUnsigned(Type t)
156 {
157   return t == UNSIGNED_BYTE || t == UNSIGNED_SHORT || t == UNSIGNED_INT;
158 }
159
160 uint32_t Component::Size(Type t)
161 {
162   switch(t)
163   {
164     case BYTE:
165     case UNSIGNED_BYTE:
166       return 1;
167     case SHORT:
168     case UNSIGNED_SHORT:
169       return 2;
170     case UNSIGNED_INT:
171     case FLOAT:
172       return 4;
173     default:
174       return -1;
175   }
176 }
177
178 uint32_t AccessorType::ElementCount(Type t)
179 {
180   return ACCESSOR_TYPE_ELEMENT_COUNT[t];
181 }
182
183 uint32_t ComponentTypedBufferViewClient::GetBytesPerComponent() const
184 {
185   return Component::Size(mComponentType);
186 }
187
188 void Node::SetMatrix(const Matrix& m)
189 {
190   m.GetTransformComponents(mTranslation, mRotation, mScale);
191 }
192
193 Quaternion ReadQuaternion(const json_value_s& j)
194 {
195   return Quaternion(ReadDaliVector<Vector4>(j));
196 }
197
198 } // namespace gltf2