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