[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / node-definition.h
1 #ifndef DALI_SCENE3D_LOADER_NODE_DEFINITION_H_
2 #define DALI_SCENE3D_LOADER_NODE_DEFINITION_H_
3 /*
4  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/actors/actor.h>
22 #include <dali/public-api/math/matrix.h>
23 #include <dali/public-api/math/quaternion.h>
24 #include <dali/public-api/math/vector4.h>
25 #include <functional>
26 #include <memory>
27 #include <string>
28
29 // INTERNAL INCLUDES
30 #include <dali-scene3d/public-api/loader/customization.h>
31 #include <dali-scene3d/public-api/loader/matrix-stack.h>
32 #include <dali-scene3d/public-api/loader/resource-bundle.h>
33 #include <dali-scene3d/public-api/loader/shader-manager.h>
34 #include <dali-scene3d/public-api/model-components/model-node.h>
35
36 namespace Dali
37 {
38 namespace Scene3D
39 {
40 namespace Loader
41 {
42 class ViewProjection;
43
44 /**
45  * @brief Interface to report (const) resource ids to.
46  * @SINCE_2_0.7
47  */
48 class DALI_SCENE3D_API IResourceReceiver
49 {
50 public:
51   virtual ~IResourceReceiver() = default;
52
53   virtual void Register(ResourceType::Value type, Index id) = 0;
54 };
55
56 /**
57  * @brief Interface to report modifiable resource ids to.
58  * @SINCE_2_0.7
59  * @note These are supposed to be transient. Obviously, the references collected
60  *  this way must not outlive the objects that they came from.
61  */
62 class DALI_SCENE3D_API IResourceReflector
63 {
64 public:
65   virtual ~IResourceReflector() = default;
66
67   virtual void Reflect(ResourceType::Value type, Index& id) = 0;
68 };
69
70 /**
71  * @brief Intermediate representation for a constraint that shall be
72  *  set up after the Actors were created. The target of the constraint
73  *  is the node definition that carries it.
74  * @SINCE_2_0.7
75  */
76 struct DALI_SCENE3D_API ConstraintDefinition
77 {
78   std::string mProperty;  ///< name of the property to constrain.
79   Index       mSourceIdx; ///< index of the node to serve as the source of the constraint.
80
81   bool operator<(const ConstraintDefinition& other) const
82   {
83     return mProperty < other.mProperty;
84   }
85
86   bool operator==(const ConstraintDefinition& other) const
87   {
88     return mSourceIdx == other.mSourceIdx && mProperty == other.mProperty;
89   }
90
91   bool operator!=(const ConstraintDefinition& other) const
92   {
93     return !operator==(other);
94   }
95 };
96
97 struct DALI_SCENE3D_API Transforms
98 {
99   MatrixStack           modelStack;
100   const ViewProjection& viewProjection;
101 };
102
103 /**
104  * @brief Information about a skeleton and the shader that needs to be configured with it.
105  * @SINCE_2_0.7
106  * @note Multiple skeletons shalt not share the same shader.
107  */
108 struct DALI_SCENE3D_API SkinningShaderConfigurationRequest
109 {
110   Index          mSkeletonIdx;
111   Shader         mShader;
112   ModelPrimitive mPrimitive;
113
114   bool operator<(const SkinningShaderConfigurationRequest& other) const
115   {
116     return mShader < other.mShader || (mShader == other.mShader && mPrimitive < other.mPrimitive);
117   }
118 };
119
120 /**
121  * @brief Needed to configure blend shape properties.
122  * @SINCE_2_0.7
123  */
124 struct DALI_SCENE3D_API BlendshapeShaderConfigurationRequest
125 {
126   std::string    mNodeName;
127   Index          mMeshIdx;
128   Shader         mShader;
129   ModelPrimitive mPrimitive;
130
131   bool operator<(const BlendshapeShaderConfigurationRequest& other) const
132   {
133     return mShader < other.mShader || (mShader == other.mShader && mPrimitive < other.mPrimitive);
134   }
135 };
136
137 /**
138  * @brief Request for creating a constraint, output from NodeDefinition::OnCreate.
139  * @SINCE_2_0.7
140  */
141 struct DALI_SCENE3D_API ConstraintRequest
142 {
143   const ConstraintDefinition* const mConstraint; ///< Definition of the constraint to create.
144   Actor                             mTarget;     ///< Target of the constraint.
145 };
146
147 /**
148  * @brief Defines a node, consisting of a name, a transform, a size, a list of child nodes,
149  *  and slots for customization and rendering logic, which are mutually exclusive in the
150  *  current implementation.
151  * @SINCE_2_0.7
152  */
153 struct DALI_SCENE3D_API NodeDefinition
154 {
155 public: // TYPES
156   using Vector = std::vector<NodeDefinition>;
157
158   struct CreateParams
159   {
160   public: // input
161     ResourceBundle&                         mResources;
162     Transforms&                             mXforms;
163     Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
164
165   public: // output
166     std::vector<ConstraintRequest>                    mConstrainables;
167     std::vector<SkinningShaderConfigurationRequest>   mSkinnables;
168     std::vector<BlendshapeShaderConfigurationRequest> mBlendshapeRequests;
169   };
170
171   class DALI_SCENE3D_API Renderable
172   {
173   public: // DATA
174     Index mShaderIdx = INVALID_INDEX;
175
176   public: // METHODS
177     virtual ~Renderable() = default;
178
179     virtual bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const;
180     virtual void RegisterResources(IResourceReceiver& receiver) const;
181     virtual void ReflectResources(IResourceReflector& reflector);
182     virtual void OnCreate(const NodeDefinition& nodeDefinition, CreateParams& params, ModelNode& node) const;
183   };
184
185   struct CustomizationDefinition
186   {
187     std::string mTag;
188
189     Index GetChildId(const Customization::Choices& choices, const NodeDefinition& node)
190     {
191       auto choice = choices.Get(mTag);
192       return std::min(choice != Customization::NONE ? choice : 0,
193                       static_cast<Index>(node.mChildren.size() - 1));
194     }
195   };
196
197   class IVisitor
198   {
199   public:
200     virtual void Start(NodeDefinition& n)  = 0;
201     virtual void Finish(NodeDefinition& n) = 0;
202
203   protected:
204     ~IVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
205   };
206
207   class IConstVisitor
208   {
209   public:
210     virtual void Start(const NodeDefinition& n)  = 0;
211     virtual void Finish(const NodeDefinition& n) = 0;
212
213   protected:
214     ~IConstVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
215   };
216
217   struct Extra
218   {
219     std::string     mKey;
220     Property::Value mValue;
221
222     bool operator<(const Extra& other) const
223     {
224       return mKey < other.mKey;
225     }
226   };
227
228 public: // METHODS
229   /**
230    * @brief Creates a ModelNode from this definition only.
231    * @SINCE_2_0.7
232    * @note Not recursive.
233    */
234   ModelNode CreateModelNode(CreateParams& params);
235
236   /**
237    * @brief Gets local space matrix of this node
238    * @SINCE_2_1.32
239    * @return Matrix of local space.
240    */
241   Matrix GetLocalSpace() const;
242
243   /**
244    * @brief Retrieves minimum and maximum position of this node in local space.
245    * @SINCE_2_1.32
246    * @param[in] resources ResourceBundle that contains mesh information of this node.
247    * @param[out] min Minimum position of the mesh of this node.
248    * @param[out] max Maximum position of the mesh of this node.
249    * @return true If the node has mesh.
250    */
251   bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const;
252
253   /**
254    * @brief Retrieves Scale Factor uniform name.
255    *
256    * This uniform name can be used to change scale factor for ibl.
257    * @SINCE_2_1.32
258    * @return std::string_view of the scale factor uniform name.
259    */
260   static std::string_view GetIblScaleFactorUniformName();
261
262   /**
263    * @brief Retrieves ibl Ydirection uniform name.
264    *
265    * This uniform name can be used to flip y direction of ibl in shader.
266    * @SINCE_2_1.32
267    * @return std::string_view of the YDirection uniform name.
268    */
269   static std::string_view GetIblYDirectionUniformName();
270
271   /**
272    * @brief Retrieves ibl MaxLod uniform name.
273    *
274    * This uniform name can be used to set max lod of ibl in shader.
275    * @SINCE_2_2.19
276    * @return std::string_view of the Max Lod uniform name.
277    */
278   static std::string_view GetIblMaxLodUniformName();
279
280 public: // DATA
281   static const char* ORIGINAL_MATRIX_PROPERTY_NAME;
282
283   std::string mName;
284   uint32_t    mNodeId = INVALID_INDEX;
285
286   Vector3    mPosition    = Vector3::ZERO;
287   Quaternion mOrientation = Quaternion::IDENTITY;
288   Vector3    mScale       = Vector3::ONE;
289   Vector3    mSize        = Vector3::ONE;
290
291   bool mIsVisible = true;
292
293   std::vector<std::unique_ptr<Renderable>> mRenderables;
294   std::unique_ptr<CustomizationDefinition> mCustomization;
295   std::vector<Extra>                       mExtras;
296   std::vector<ConstraintDefinition>        mConstraints;
297
298   std::vector<Index> mChildren;
299   Index              mParentIdx = INVALID_INDEX;
300 };
301
302 class DALI_SCENE3D_API ModelRenderable : public NodeDefinition::Renderable
303 {
304 public: // DATA
305   Vector4 mColor       = Color::WHITE;
306   Index   mMeshIdx     = INVALID_INDEX;
307   Index   mMaterialIdx = INVALID_INDEX;
308
309 public: // METHODS
310   bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const override;
311   void RegisterResources(IResourceReceiver& receiver) const override;
312   void ReflectResources(IResourceReflector& reflector) override;
313   void OnCreate(const NodeDefinition& nodeDefinition, NodeDefinition::CreateParams& params, ModelNode& node) const override;
314 };
315
316 /**
317  * @brief Parameters for an Arc node.
318  * @SINCE_2_0.7
319  */
320 class DALI_SCENE3D_API ArcRenderable : public ModelRenderable
321 {
322 public: // DATA
323   bool  mAntiAliasing      = true;
324   int   mArcCaps           = 0;
325   float mStartAngleDegrees = .0f;
326   float mEndAngleDegrees   = .0f;
327   float mRadius            = .0f;
328
329 public: // METHODS
330   static void GetEndVectorWithDiffAngle(float startAngle, float endAngle, Vector2& endVector);
331
332   void OnCreate(const NodeDefinition& nodeDefinition, NodeDefinition::CreateParams& params, ModelNode& node) const override;
333 };
334
335 } // namespace Loader
336 } // namespace Scene3D
337 } // namespace Dali
338
339 #endif // DALI_SCENE3D_LOADER_NODE_DEFINITION_H_