[dali_2.1.31] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / loader / gltf2-asset.cpp
1 /*
2 * Copyright (c) 2022 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 #include "dali-scene3d/internal/loader/gltf2-asset.h"
18 #include <algorithm>
19 #include <map>
20 #include "dali/public-api/math/matrix.h"
21
22 #define ENUM_STRING_MAPPING(t, x) \
23   {                               \
24 #x, t::x                      \
25   }
26
27 using namespace Dali;
28
29 namespace gltf2
30 {
31 namespace
32 {
33 constexpr uint32_t ACCESSOR_TYPE_ELEMENT_COUNT[]{
34   1,
35   2,
36   3,
37   4,
38   4,
39   9,
40   16,
41   static_cast<uint32_t>(-1)};
42
43 const std::map<std::string_view, AccessorType::Type> ACCESSOR_TYPES{
44   ENUM_STRING_MAPPING(AccessorType, SCALAR),
45   ENUM_STRING_MAPPING(AccessorType, VEC2),
46   ENUM_STRING_MAPPING(AccessorType, VEC3),
47   ENUM_STRING_MAPPING(AccessorType, VEC4),
48   ENUM_STRING_MAPPING(AccessorType, MAT2),
49   ENUM_STRING_MAPPING(AccessorType, MAT3),
50   ENUM_STRING_MAPPING(AccessorType, MAT4),
51 };
52
53 const std::map<std::string_view, AlphaMode::Type> ALPHA_MODE_TYPES{
54   ENUM_STRING_MAPPING(AlphaMode::Type, OPAQUE),
55   ENUM_STRING_MAPPING(AlphaMode::Type, MASK),
56   ENUM_STRING_MAPPING(AlphaMode::Type, BLEND),
57 };
58
59 const std::map<std::string_view, Attribute::Type> ATTRIBUTE_TYPES{
60   ENUM_STRING_MAPPING(Attribute::Type, POSITION),
61   ENUM_STRING_MAPPING(Attribute::Type, NORMAL),
62   ENUM_STRING_MAPPING(Attribute::Type, TANGENT),
63   ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_0),
64   ENUM_STRING_MAPPING(Attribute::Type, TEXCOORD_1),
65   ENUM_STRING_MAPPING(Attribute::Type, COLOR_0),
66   ENUM_STRING_MAPPING(Attribute::Type, JOINTS_0),
67   ENUM_STRING_MAPPING(Attribute::Type, WEIGHTS_0),
68 };
69
70 const std::map<std::string_view, Animation::Sampler::Interpolation::Type> ANIMATION_SAMPLER_INTERPOLATION{
71   ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, STEP),
72   ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, LINEAR),
73   ENUM_STRING_MAPPING(Animation::Sampler::Interpolation::Type, CUBICSPLINE),
74 };
75
76 const std::map<std::string_view, Animation::Channel::Target::Type> ANIMATION_CHANNEL_TARGET_PATH_TYPES{
77   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, TRANSLATION),
78   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, ROTATION),
79   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, SCALE),
80   ENUM_STRING_MAPPING(Animation::Channel::Target::Type, WEIGHTS),
81 };
82
83 } // namespace
84
85 bool Component::IsUnsigned(Type t)
86 {
87   return t == UNSIGNED_BYTE || t == UNSIGNED_SHORT || t == UNSIGNED_INT;
88 }
89
90 uint32_t Component::Size(Type t)
91 {
92   switch(t)
93   {
94     case BYTE:
95     case UNSIGNED_BYTE:
96       return 1;
97     case SHORT:
98     case UNSIGNED_SHORT:
99       return 2;
100     case UNSIGNED_INT:
101     case FLOAT:
102       return 4;
103     default:
104       return -1;
105   }
106 }
107
108 uint32_t AccessorType::ElementCount(Type t)
109 {
110   return ACCESSOR_TYPE_ELEMENT_COUNT[t];
111 }
112
113 AccessorType::Type AccessorType::FromString(const char* s, size_t len)
114 {
115   auto iFind = ACCESSOR_TYPES.find(std::string_view(s, len));
116   if(iFind != ACCESSOR_TYPES.end())
117   {
118     return iFind->second;
119   }
120   return AccessorType::INVALID;
121 }
122
123 AlphaMode::Type AlphaMode::FromString(const char* s, size_t len)
124 {
125   auto iFind = ALPHA_MODE_TYPES.find(std::string_view(s, len));
126   if(iFind != ALPHA_MODE_TYPES.end())
127   {
128     return iFind->second;
129   }
130   return AlphaMode::INVALID;
131 }
132
133 Attribute::Type Attribute::FromString(const char* s, size_t len)
134 {
135   auto iFind = ATTRIBUTE_TYPES.find(std::string_view(s, len));
136   if(iFind != ATTRIBUTE_TYPES.end())
137   {
138     return iFind->second;
139   }
140   return Attribute::INVALID;
141 }
142
143 Animation::Sampler::Interpolation::Type Animation::Sampler::Interpolation::FromString(const char* s, size_t len)
144 {
145   auto iFind = ANIMATION_SAMPLER_INTERPOLATION.find(std::string_view(s, len));
146   if(iFind != ANIMATION_SAMPLER_INTERPOLATION.end())
147   {
148     return iFind->second;
149   }
150   return Animation::Sampler::Interpolation::Type::INVALID;
151 }
152
153 uint32_t ComponentTypedBufferViewClient::GetBytesPerComponent() const
154 {
155   return Component::Size(mComponentType);
156 }
157
158 Animation::Channel::Target::Type Animation::Channel::Target::FromString(const char* s, size_t len)
159 {
160   std::string target(s, len);
161   std::transform(target.begin(), target.end(), target.begin(), ::toupper);
162
163   auto iFind = ANIMATION_CHANNEL_TARGET_PATH_TYPES.find(std::string_view(target.c_str(), len));
164   if(iFind != ANIMATION_CHANNEL_TARGET_PATH_TYPES.end())
165   {
166     return iFind->second;
167   }
168   return Animation::Channel::Target::INVALID;
169 };
170
171 void Node::SetMatrix(const Matrix& m)
172 {
173   m.GetTransformComponents(mTranslation, mRotation, mScale);
174 }
175
176 Quaternion ReadQuaternion(const json_value_s& j)
177 {
178   return Quaternion(ReadDaliVector<Vector4>(j));
179 }
180
181 } // namespace gltf2