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