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