1 #ifndef DALI_SCENE_LOADER_NODE_DEFINITION_H_
2 #define DALI_SCENE_LOADER_NODE_DEFINITION_H_
4 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "dali-scene-loader/public-api/customization.h"
22 #include "dali-scene-loader/public-api/matrix-stack.h"
23 #include "dali-scene-loader/public-api/resource-bundle.h"
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"
41 * @brief Interface to report (const) resource ids to.
43 class DALI_SCENE_LOADER_API IResourceReceiver
46 virtual ~IResourceReceiver() = default;
48 virtual void Register(ResourceType::Value type, Index id) = 0;
52 * @brief Interface to report modifiable resource ids to.
53 * @note These are supposed to be transient. Obviously, the references collected
54 * this way must not outlive the objects that they came from.
56 class DALI_SCENE_LOADER_API IResourceReflector
59 virtual ~IResourceReflector() = default;
61 virtual void Reflect(ResourceType::Value type, Index& id) = 0;
65 * @brief Intermediate representation for a constraint that shall be
66 * set up after the Actors were created. The target of the constraint
67 * is the node definition that carries it.
69 struct DALI_SCENE_LOADER_API ConstraintDefinition
71 std::string mProperty; ///< name of the property to constrain.
72 Index mSourceIdx; ///< index of the node to serve as the source of the constraint.
74 bool operator<(const ConstraintDefinition& other) const
76 return mProperty < other.mProperty;
79 bool operator==(const ConstraintDefinition& other) const
81 return mSourceIdx == other.mSourceIdx && mProperty == other.mProperty;
84 bool operator!=(const ConstraintDefinition& other) const
86 return !operator==(other);
90 struct DALI_SCENE_LOADER_API Transforms
92 MatrixStack modelStack;
93 const ViewProjection& viewProjection;
97 * @brief Information about a skeleton and the shader that needs to be configured with it.
98 * @note Multiple skeletons shalt not share the same shader.
100 struct DALI_SCENE_LOADER_API SkinningShaderConfigurationRequest
105 bool operator<(const SkinningShaderConfigurationRequest& other) const
107 return mShader < other.mShader;
112 * @brief Needed to configure blend shape properties.
114 struct DALI_SCENE_LOADER_API BlendshapeShaderConfigurationRequest
116 std::string mNodeName;
120 bool operator<(const BlendshapeShaderConfigurationRequest& other) const
122 return mShader < other.mShader;
127 * @brief Request for creating a constraint, output from NodeDefinition::OnCreate.
129 struct DALI_SCENE_LOADER_API ConstraintRequest
131 const ConstraintDefinition* const mConstraint; ///< Definition of the constraint to create.
132 Actor mTarget; ///< Target of the constraint.
136 * @brief Defines a node, consisting of a name, a transform, a size, a list of child nodes,
137 * and slots for customization and rendering logic, which are mutually exclusive in the
138 * current implementation.
140 struct DALI_SCENE_LOADER_API NodeDefinition
143 using Vector = std::vector<NodeDefinition>;
148 const ResourceBundle& mResources;
152 std::vector<ConstraintRequest> mConstrainables;
153 std::vector<SkinningShaderConfigurationRequest> mSkinnables;
154 std::vector<BlendshapeShaderConfigurationRequest> mBlendshapeRequests;
157 class DALI_SCENE_LOADER_API Renderable
160 Index mShaderIdx = INVALID_INDEX;
163 virtual ~Renderable() = default;
165 virtual bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const;
166 virtual void RegisterResources(IResourceReceiver& receiver) const;
167 virtual void ReflectResources(IResourceReflector& reflector);
168 virtual void OnCreate(const NodeDefinition& node, CreateParams& params, Actor& actor) const;
171 struct CustomizationDefinition
175 Index GetChildId(const Customization::Choices& choices, const NodeDefinition& node)
177 auto choice = choices.Get(mTag);
178 return std::min(choice != Customization::NONE ? choice : 0,
179 static_cast<Index>(node.mChildren.size() - 1));
186 virtual void Start(NodeDefinition& n) = 0;
187 virtual void Finish(NodeDefinition& n) = 0;
190 ~IVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
196 virtual void Start(const NodeDefinition& n) = 0;
197 virtual void Finish(const NodeDefinition& n) = 0;
200 ~IConstVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
206 Property::Value mValue;
208 bool operator<(const Extra& other) const
210 return mKey < other.mKey;
216 * @brief Creates a DALi Actor from this definition only.
217 * @note Not recursive.
219 Actor CreateActor(CreateParams& params) const;
222 * @brief Gets local space matrix of this node
223 * @return Matrix of local space.
225 Matrix GetLocalSpace() const;
228 * @brief Retrieves minimum and maximum position of this node in local space.
229 * @param[in] resources ResourceBundle that contains mesh information of this node.
230 * @param[out] min Minimum position of the mesh of this node.
231 * @param[out] max Maximum position of the mesh of this node.
232 * @return true If the node has mesh.
234 bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const;
237 * @brief Retrieves Scale Factor uniform name.
238 * This uniform name can be used to change scale factor for ibl.
239 * @return std::string_view of the scale factor uniform name.
241 static std::string_view GetIblScaleFactorUniformName();
244 * @brief Retrieves ibl Ydirection uniform name.
245 * This uniform name can be used to flip y direction of ibl in shader.
246 * @return std::string_view of the YDirection uniform name.
248 static std::string_view GetIblYDirectionUniformName();
251 static const std::string ORIGINAL_MATRIX_PROPERTY_NAME;
255 Vector3 mPosition = Vector3::ZERO;
256 Quaternion mOrientation = Quaternion::IDENTITY;
257 Vector3 mScale = Vector3::ONE;
258 Vector3 mSize = Vector3::ONE;
260 bool mIsVisible = true;
262 std::unique_ptr<Renderable> mRenderable;
263 std::unique_ptr<CustomizationDefinition> mCustomization;
264 std::vector<Extra> mExtras;
265 std::vector<ConstraintDefinition> mConstraints;
267 std::vector<Index> mChildren;
268 Index mParentIdx = INVALID_INDEX;
271 class DALI_SCENE_LOADER_API ModelNode : public NodeDefinition::Renderable
274 Vector4 mColor = Color::WHITE;
275 Index mMeshIdx = INVALID_INDEX;
276 Index mMaterialIdx = INVALID_INDEX;
279 bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const override;
280 void RegisterResources(IResourceReceiver& receiver) const override;
281 void ReflectResources(IResourceReflector& reflector) override;
282 void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
286 * @brief Parameters for an Arc node.
288 class DALI_SCENE_LOADER_API ArcNode : public ModelNode
291 bool mAntiAliasing = true;
293 float mStartAngleDegrees = .0f;
294 float mEndAngleDegrees = .0f;
298 static void GetEndVectorWithDiffAngle(float startAngle, float endAngle, Vector2& endVector);
300 void OnCreate(const NodeDefinition& node, NodeDefinition::CreateParams& params, Actor& actor) const override;
303 } // namespace SceneLoader
306 #endif //DALI_SCENE_LOADER_NODE_DEFINITION_H_