[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / scene3d-view / gltf-loader.h
1 #ifndef DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H\r
2 #define DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H\r
3 \r
4 /*\r
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  */\r
20 \r
21 // EXTERNAL INCLUDES\r
22 #include <dali/public-api/actors/layer.h>\r
23 #include <dali/public-api/animation/animation.h>\r
24 #include <dali/public-api/common/vector-wrapper.h>\r
25 #include <dali/public-api/rendering/renderer.h>\r
26 #include <dali/public-api/rendering/shader.h>\r
27 \r
28 // INTERNAL INCLUDES\r
29 #include <dali-toolkit/devel-api/builder/json-parser.h>\r
30 \r
31 using namespace Dali;\r
32 \r
33 namespace Dali\r
34 {\r
35 namespace Toolkit\r
36 {\r
37 namespace Internal\r
38 {\r
39 class Scene3dView;\r
40 \r
41 namespace Gltf\r
42 {\r
43 enum ShaderType\r
44 {\r
45   NO_TEXTURE_SHADER,\r
46   BASECOLOR_SHADER,\r
47   METALLICROUGHNESS_SHADER,\r
48   BASECOLOR_METALLICROUGHNESS_SHADER,\r
49   NORMAL_SHADER,\r
50   BASECOLOR_NORMAL_SHADER,\r
51   METALLICROUGHNESS_NORMAL_SHADER,\r
52   BASECOLOR_METALLICROUGHNESS_NORMAL_SHADER,\r
53   OCCLUSION_SHADER,\r
54   BASECOLOR_OCCLUSION_SHADER,\r
55   METALLICROUGHNESS_OCCLUSION_SHADER,\r
56   BASECOLOR_METALLICROUGHNESS_OCCLUSION_SHADER,\r
57   NORMAL_OCCLUSION_SHADER,\r
58   BASECOLOR_NORMAL_OCCLUSION_SHADER,\r
59   METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
60   BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
61   EMIT_SHADER,\r
62   BASECOLOR_EMIT_SHADER,\r
63   METALLICROUGHNESS_EMIT_SHADER,\r
64   BASECOLOR_METALLICROUGHNESS_EMIT_SHADER,\r
65   NORMAL_EMIT_SHADER,\r
66   BASECOLOR_NORMAL_EMIT_SHADER,\r
67   METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
68   BASECOLOR_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
69   OCCLUSION_EMIT_SHADER,\r
70   BASECOLOR_OCCLUSION_EMIT_SHADER,\r
71   METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
72   BASECOLOR_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
73   NORMAL_OCCLUSION_EMIT_SHADER,\r
74   BASECOLOR_NORMAL_OCCLUSION_EMIT_SHADER,\r
75   METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
76   BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
77   IBL_SHADER,\r
78   IBL_BASECOLOR_SHADER,\r
79   IBL_METALLICROUGHNESS_SHADER,\r
80   IBL_BASECOLOR_METALLICROUGHNESS_SHADER,\r
81   IBL_NORMAL_SHADER,\r
82   IBL_BASECOLOR_NORMAL_SHADER,\r
83   IBL_METALLICROUGHNESS_NORMAL_SHADER,\r
84   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_SHADER,\r
85   IBL_OCCLUSION_SHADER,\r
86   IBL_BASECOLOR_OCCLUSION_SHADER,\r
87   IBL_METALLICROUGHNESS_OCCLUSION_SHADER,\r
88   IBL_BASECOLOR_METALLICROUGHNESS_OCCLUSION_SHADER,\r
89   IBL_NORMAL_OCCLUSION_SHADER,\r
90   IBL_BASECOLOR_NORMAL_OCCLUSION_SHADER,\r
91   IBL_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
92   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_SHADER,\r
93   IBL_EMIT_SHADER,\r
94   IBL_BASECOLOR_EMIT_SHADER,\r
95   IBL_METALLICROUGHNESS_EMIT_SHADER,\r
96   IBL_BASECOLOR_METALLICROUGHNESS_EMIT_SHADER,\r
97   IBL_NORMAL_EMIT_SHADER,\r
98   IBL_BASECOLOR_NORMAL_EMIT_SHADER,\r
99   IBL_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
100   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_EMIT_SHADER,\r
101   IBL_OCCLUSION_EMIT_SHADER,\r
102   IBL_BASECOLOR_OCCLUSION_EMIT_SHADER,\r
103   IBL_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
104   IBL_BASECOLOR_METALLICROUGHNESS_OCCLUSION_EMIT_SHADER,\r
105   IBL_NORMAL_OCCLUSION_EMIT_SHADER,\r
106   IBL_BASECOLOR_NORMAL_OCCLUSION_EMIT_SHADER,\r
107   IBL_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
108   IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER,\r
109   SHADER_TYPE_MAX = IBL_BASECOLOR_METALLICROUGHNESS_NORMAL_OCCLUSION_EMIT_SHADER\r
110 };\r
111 \r
112 struct BufferInfo\r
113 {\r
114   BufferInfo()\r
115   : byteLength(-1),\r
116     uri(""),\r
117     name("")\r
118   {\r
119   }\r
120 \r
121   ~BufferInfo()\r
122   {\r
123   }\r
124 \r
125   int32_t     byteLength;\r
126   std::string uri;\r
127   std::string name;\r
128 };\r
129 \r
130 struct BufferViewInfo\r
131 {\r
132   BufferViewInfo()\r
133   : buffer(-1),\r
134     byteOffset(0),\r
135     byteLength(0),\r
136     byteStride(0),\r
137     target(0),\r
138     name("")\r
139   {\r
140   }\r
141 \r
142   ~BufferViewInfo()\r
143   {\r
144   }\r
145 \r
146   int32_t     buffer;\r
147   int32_t     byteOffset;\r
148   int32_t     byteLength;\r
149   int32_t     byteStride;\r
150   int32_t     target;\r
151   std::string name;\r
152 };\r
153 \r
154 struct TextureInfo\r
155 {\r
156   TextureInfo()\r
157   : sourceIdx(-1),\r
158     samplerIdx(-1)\r
159   {\r
160   }\r
161 \r
162   ~TextureInfo()\r
163   {\r
164   }\r
165   int32_t sourceIdx;\r
166   int32_t samplerIdx;\r
167 };\r
168 \r
169 struct PbrTextureInfo\r
170 {\r
171   PbrTextureInfo()\r
172   : index(-1),\r
173     texCoord(0),\r
174     value(0.0)\r
175   {\r
176   }\r
177   ~PbrTextureInfo()\r
178   {\r
179   }\r
180 \r
181   int32_t index;\r
182   int32_t texCoord;\r
183   float   value;\r
184 };\r
185 \r
186 struct MaterialInfo\r
187 {\r
188   MaterialInfo()\r
189   : baseColorFactor(1, 1, 1, 1),\r
190     metallicFactor(1.0),\r
191     roughnessFactor(1.0),\r
192     emissiveFactor(0.0, 0.0, 0.0),\r
193     alphaMode("OPAQUE"),\r
194     alphaCutoff(0.5),\r
195     doubleSided(false),\r
196     name("")\r
197   {\r
198   }\r
199 \r
200   ~MaterialInfo()\r
201   {\r
202   }\r
203 \r
204   Vector4     baseColorFactor;\r
205   float       metallicFactor;\r
206   float       roughnessFactor;\r
207   Vector3     emissiveFactor;\r
208   std::string alphaMode;\r
209   float       alphaCutoff;\r
210   bool        doubleSided;\r
211 \r
212   PbrTextureInfo baseColorTexture;\r
213   PbrTextureInfo metallicRoughnessTexture;\r
214   PbrTextureInfo normalTexture;\r
215   PbrTextureInfo occlusionTexture;\r
216   PbrTextureInfo emissiveTexture;\r
217 \r
218   std::string name;\r
219   //need to add max, min\r
220 };\r
221 \r
222 struct AccessorInfo\r
223 {\r
224   AccessorInfo()\r
225   : bufferView(-1),\r
226     byteOffset(0),\r
227     componentType(-1),\r
228     normalized(false),\r
229     count(0),\r
230     type(""),\r
231     max(0),\r
232     min(0),\r
233     name("")\r
234   {\r
235   }\r
236 \r
237   ~AccessorInfo()\r
238   {\r
239   }\r
240 \r
241   int32_t     bufferView;\r
242   int32_t     byteOffset;\r
243   int32_t     componentType;\r
244   bool        normalized;\r
245   int32_t     count;\r
246   std::string type;\r
247   int32_t     max;\r
248   int32_t     min;\r
249   std::string name;\r
250   //need to add max, min\r
251 };\r
252 \r
253 struct Attribute\r
254 {\r
255   Attribute()\r
256   : POSITION(-1),\r
257     NORMAL(-1),\r
258     TANGENT(-1)\r
259   {\r
260   }\r
261 \r
262   ~Attribute()\r
263   {\r
264   }\r
265 \r
266   int32_t POSITION;\r
267   int32_t NORMAL;\r
268   int32_t TANGENT;\r
269 \r
270   std::vector<int32_t> TEXCOORD;\r
271   std::vector<int32_t> COLOR;\r
272 };\r
273 \r
274 struct MeshInfo\r
275 {\r
276   MeshInfo()\r
277   : indicesIdx(-1),\r
278     materialsIdx(-1),\r
279     mode(4)\r
280   {\r
281   }\r
282 \r
283   ~MeshInfo()\r
284   {\r
285   }\r
286   Geometry    geometry;\r
287   std::string name;\r
288 \r
289   int32_t indicesIdx;\r
290   int32_t materialsIdx;\r
291   int32_t mode;\r
292 \r
293   Vector3 size;\r
294   Vector3 pivot;\r
295 \r
296   Attribute attribute;\r
297   //need to add max, min\r
298 };\r
299 \r
300 struct AnimationChannelInfo\r
301 {\r
302   AnimationChannelInfo()\r
303   : sampler(-1),\r
304     targetNode(-1),\r
305     path("")\r
306   {\r
307   }\r
308 \r
309   ~AnimationChannelInfo()\r
310   {\r
311   }\r
312 \r
313   int32_t     sampler;\r
314   int32_t     targetNode;\r
315   std::string path;\r
316 };\r
317 \r
318 struct AnimationSamplerInfo\r
319 {\r
320   AnimationSamplerInfo()\r
321   : input(-1),\r
322     output(-1),\r
323     interpolation("")\r
324   {\r
325   }\r
326 \r
327   ~AnimationSamplerInfo()\r
328   {\r
329   }\r
330 \r
331   int32_t     input;\r
332   int32_t     output;\r
333   std::string interpolation;\r
334 };\r
335 \r
336 struct AnimationInfo\r
337 {\r
338   AnimationInfo()\r
339   : name("")\r
340   {\r
341   }\r
342 \r
343   ~AnimationInfo()\r
344   {\r
345   }\r
346 \r
347   std::string                       name;\r
348   std::vector<AnimationChannelInfo> channelArray;\r
349   std::vector<AnimationSamplerInfo> samplerArray;\r
350 };\r
351 \r
352 struct OrthographicInfo\r
353 {\r
354   OrthographicInfo()\r
355   : xmag(0.0f),\r
356     ymag(0.0f),\r
357     zfar(0.0f),\r
358     znear(0.0f)\r
359   {\r
360   }\r
361 \r
362   ~OrthographicInfo()\r
363   {\r
364   }\r
365 \r
366   float xmag;\r
367   float ymag;\r
368   float zfar;\r
369   float znear;\r
370 };\r
371 \r
372 struct PerspectiveInfo\r
373 {\r
374   PerspectiveInfo()\r
375   : aspectRatio(0.0f),\r
376     yfov(0.0f),\r
377     zfar(0.0f),\r
378     znear(0.0f)\r
379   {\r
380   }\r
381 \r
382   ~PerspectiveInfo()\r
383   {\r
384   }\r
385 \r
386   float aspectRatio;\r
387   float yfov;\r
388   float zfar;\r
389   float znear;\r
390 };\r
391 \r
392 struct CameraInfo\r
393 {\r
394   CameraInfo()\r
395   : name(""),\r
396     type("")\r
397   {\r
398   }\r
399 \r
400   ~CameraInfo()\r
401   {\r
402   }\r
403 \r
404   std::string      name;\r
405   std::string      type;\r
406   OrthographicInfo orthographic;\r
407   PerspectiveInfo  perspective;\r
408 };\r
409 \r
410 /**\r
411  *\r
412  * Loader is a class to parse glTf, to load data from file, and to generate Scene.\r
413  * This glTF loader supports glTF 2.0 features.\r
414  *\r
415  * @remarks glTF loader didn't support such features.\r
416  *  - Sparse accessor\r
417  *  - Morphing\r
418  *  - Skeletal Animation\r
419  * These features will be supported soon.\r
420  *\r
421  */\r
422 class Loader\r
423 {\r
424 public:\r
425   /**\r
426    * @brief Create an uninitialized Loader.\r
427    */\r
428   Loader();\r
429 \r
430   /**\r
431    * @brief Destructor\r
432    */\r
433   ~Loader();\r
434 \r
435   /**\r
436    * @brief Load Scene3dView from scene format file(e.g., glTF).\r
437    * @param[in] filePath Path of scene format file.\r
438    * @param[in] scene3dView Scene3dView data loaded from file.\r
439    * @return true if scene is successfully loaded\r
440    */\r
441   bool LoadScene(const std::string& filePath, Internal::Scene3dView& scene3dView);\r
442 \r
443 private:\r
444   bool ParseGltf(const std::string& filePath);\r
445   bool LoadAssets();\r
446 \r
447   bool CreateScene(Internal::Scene3dView& scene3dView);\r
448 \r
449   void LoadCamera(Scene3dView& scene3dView);\r
450   bool LoadOrthoGraphic(const TreeNode& camera, CameraInfo& cameraInfo);\r
451   bool LoadPerspective(const TreeNode& camera, CameraInfo& cameraInfo);\r
452 \r
453   bool  LoadSceneNodes(Scene3dView& scene3dView);\r
454   Actor AddNode(Scene3dView& scene3dView, uint32_t index);\r
455   void  SetActorCache(Actor& actor, uint32_t index);\r
456   bool  SetTextureAndSampler(TextureSet& textureSet, int32_t textureIdx, std::string& toShader, std::string shader, int32_t& addIdx);\r
457 \r
458   bool LoadAnimation(Scene3dView& scene3dView);\r
459   bool LoadAnimationChannels(const TreeNode& animation, AnimationInfo& animationInfo);\r
460   bool LoadAnimationSamplers(const TreeNode& animation, AnimationInfo& animationInfo);\r
461 \r
462 private:\r
463   Dali::Toolkit::JsonParser mParser;\r
464   const TreeNode*           mNodes;\r
465   const TreeNode*           mRoot;\r
466 \r
467   std::string mPath;\r
468 \r
469   std::vector<Actor> mActorCache;\r
470   Shader             mShaderCache[ShaderType::SHADER_TYPE_MAX + 1];\r
471 \r
472   std::vector<BufferInfo>     mBufferArray;\r
473   std::vector<BufferViewInfo> mBufferViewArray;\r
474   std::vector<AccessorInfo>   mAccessorArray;\r
475 \r
476   std::vector<MeshInfo>     mMeshArray;\r
477   std::vector<MaterialInfo> mMaterialArray;\r
478   std::vector<TextureInfo>  mTextureArray;\r
479 \r
480   std::vector<Texture> mSourceArray;\r
481   std::vector<Sampler> mSamplerArray;\r
482 };\r
483 \r
484 } //namespace Gltf\r
485 \r
486 } //namespace Internal\r
487 \r
488 } //namespace Toolkit\r
489 \r
490 } //namespace Dali\r
491 \r
492 #endif // DALI_TOOLKIT_INTERNAL_GLTF_LOADER_H\r