1 #ifndef DALI_SCENE_LOADER_SCENE_DEFINITION_H_
2 #define DALI_SCENE_LOADER_SCENE_DEFINITION_H_
4 * Copyright (c) 2021 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/node-definition.h"
23 #include "dali-scene-loader/public-api/string-callback.h"
24 #include "dali-scene-loader/public-api/utils.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 Intermediate representation of a scene with functionality required to
42 * create DALi objects (Actors, Renderers) from it.
44 class DALI_SCENE_LOADER_API SceneDefinition
47 using NodePredicate = std::function<bool(const NodeDefinition&)>;
48 using NodeConsumer = std::function<void(NodeDefinition&)>;
49 using ConstNodeConsumer = std::function<void(const NodeDefinition&)>;
53 SceneDefinition(SceneDefinition&& other);
57 * @brief Registers a scene root node.
58 * @return The index of the scene root node *registration*.
60 Index AddRootNode(Index iNode);
63 * @return the list of scene root node IDs in the order of their loading.
65 const std::vector<Index>& GetRoots() const;
68 * @brief Removes scene root registration at the given index @a iRoot.
69 * @note @a iRoot is the index of the registration (i.e. into the vector returned by GetRoots()),
72 void RemoveRootNode(Index iRoot);
75 * @return The number of node( definition)s in the scene.
77 uint32_t GetNodeCount() const;
80 * @return Const pointer to the node (definition) at the given index.
82 const NodeDefinition* GetNode(Index iNode) const;
85 * @return Pointer to the node (definition) at the given index.
87 NodeDefinition* GetNode(Index iNode);
90 * @brief Traverses the scene starting from the node at the given index into
91 * nodes, using the given customization @a choices and the visitor @a v.
93 void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IVisitor& v);
96 * @brief Traverses the scene starting from the node at the given index into
97 * nodes, using the given customization @a choices and the visitor @a v.
99 void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IConstVisitor& v) const;
102 * @brief Counts the references to meshes, shaders, materials that nodes in
103 * the scene are holding, writing the results into @a refCounts.
104 * @note @a refCounts' entries must have the correct size. Use ResourceBundle::GetRefCounter().
106 void CountResourceRefs(Index iNode, const Customization::Choices& choices, ResourceRefCounts& refCounts) const;
109 * @brief Given a bundle of @a resources that are loaded, and customization
110 * @a choices, this method traverses the scene, creating the actors and renderers
111 * from node definitions.
112 * @return Handle to the root actor.
114 Actor CreateNodes(Index iNode, const Customization::Choices& choices, NodeDefinition::CreateParams& params) const;
117 * @brief Creates / update a registry of mappings from customization tags to
118 * a lists of names of customizable nodes under each tag, and the number of
119 * options. If @a outMissingChoices was specified, each tag that it encounters
120 * in the scene but not in @a choices, will be registered on it with the default
123 void GetCustomizationOptions(const Customization::Choices& choices,
124 Customization::Map& outCustomizationOptions,
125 Customization::Choices* outMissingChoices) const;
128 * @brief Attempts to add @a nodeDef to the end of nodes, and its index to the end of
129 * its parent's list of children (if iParent != NodeDefinition::INVALID_PARENT).
130 * @return If the operation was successful - which requires nodeDef->name to be unique -
131 * a pointer to the stored node definition; nullptr otherwise.
133 NodeDefinition* AddNode(std::unique_ptr<NodeDefinition>&& nodeDef);
136 * @brief Moves the node to some other parent and / or to a different index.
137 * @return Whether the operation was successful.
138 * @note This is currently breaking an assumption of never having a child of a node at a lower
139 * index as that of the node itself, due to the fact that we're only changing parent ids (and
140 * entries into the vector of children of node definitions), to save the complication of having
141 * to move about, and offset indices to, everything past the reparented node. This should be
142 * sufficient AT LEAST AS LONG AS we recreate the SceneDefinition when loading the scene; if
143 * we ever needed to serialize it, we should ensure correct ordering of nodes.
145 bool ReparentNode(const std::string& name, const std::string& newParentName, Index siblingOrder);
148 * @brief Removes a node with the given name, including all of its children, and updating
149 * the indices on all remaining node definitions.
150 * @return Whether the operation was successful.
152 bool RemoveNode(const std::string& name);
155 * @brief Builds the model matrix stack for the node at the given @a index.
156 * @note It only pushes new matrices; does not require the stack to be empty (or cares if it was not).
158 void GetNodeModelStack(Index index, MatrixStack& model) const;
161 * @brief Attempts to find the definition of a node with the given @a name. Only upon
162 * success, and if @a outIndex is non-null, the index of the node is written to it.
163 * @return Pointer to the node definition; nullptr if not found.
164 * @note No ownership transfer.
166 NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr);
169 * @brief Attempts to find the definition of a node with the given @a name. Only upon
170 * success, and if @a outIndex is non-null, the index of the node is written to it.
171 * @return Pointer to the node definition; nullptr if not found.
172 * @note No ownership transfer.
174 const NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr) const;
177 * @return The index of the given NodeDefinition@ a node, or -1 if the node definition
180 Index FindNodeIndex(const NodeDefinition& node) const;
183 * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true
185 * @note A @a limit value of 0 means no limit.
187 void FindNodes(NodePredicate predicate, NodeConsumer consumer, unsigned int limit = 0);
190 * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true
192 * @note A @a limit value of 0 means no limit.
194 void FindNodes(NodePredicate predicate, ConstNodeConsumer consumer, unsigned int limit = 0) const;
197 * @brief Applies constraints from the given requests.
199 void ApplyConstraints(Actor& root,
200 std::vector<ConstraintRequest>&& constrainables,
201 StringCallback onError = DefaultErrorCallback) const;
204 * @brief Sets up joint matrix properties and constraints on actors that are involved in skeletal
205 * animation (i.e. those that are between (inclusive) the lower and upper bounds of any skeleton),
206 * to ensure the correct update of meshes skinned to these skeletons.
207 * @param iRoot The index of the scene root node. Skeletons that aren't descendants of this node
209 * @param skeletons The list of skeletons that require setting up.
210 * @param rootActor The Actor corresponding to the root node, which will be used to locate
213 void ConfigureSkeletonJoints(uint32_t iRoot, const SkeletonDefinition::Vector& skeletons, Actor rootActor) const;
216 * @brief Ensures that there is no overlap between shaders used by nodes that have
217 * meshes skinned to different skeletons.
219 void EnsureUniqueSkinningShaderInstances(ResourceBundle& resources) const;
222 * @brief Performs the configuration of the given skinning shaders with the given skeleton
223 * This means that the absolute transforms of the joints are calculated and set as one of
224 * the uniforms in the mat4 @b uBone array (in depth first traversal). Further, the following
226 * - a @b jointMatrix property on each joint Actor;<br />
227 * - constraint from the Actor's local position and rotation (and if it has a @e joint
228 * parent, the jointMatrix of the parent) to its @b jointMatrix property;<br />
229 * - a constraint from the the Actor's @b jointMatrix property to the related entry in
230 * the shader's @b uBone property;<br />
231 * This ensures the automatic update of the skeletal animation, should any of the joints'
232 * transform changes, by whatever means.
233 * @return The success of the operations. Error messages will be posted to the optional
234 * @a onError callback.
235 * @note A maximum of SkinningDetails::MAX_JOINTS joints per skeleton are supported at the moment.
236 * @note Even if multiple skinned meshes use the same skinning shader, the correct number
237 * of separate instances need to be declared in the .dli to avoid clashing uniform
238 * definitions and constraints.
240 void ConfigureSkinningShaders(const ResourceBundle& resources,
242 std::vector<SkinningShaderConfigurationRequest>&& requests) const;
245 * @brief Ensures there is no two meshes with blend shapes sharing the same shader.
247 void EnsureUniqueBlendShapeShaderInstances(ResourceBundle& resources) const;
250 * @brief Performs the configuration of the given blend shapes.
252 * For each node with blend shapes it registers into the actor the weights properties for each morph target
253 * and some needed uniforms into the shader.
255 * @param[in] root The root actor.
256 * @param[in] requests The requests to configure blend shapes.
257 * @param[in] resources The resources bundle. Meshes need to be accessed to configure the blend shapes.
258 * @param[in] onError The error callback.
260 bool ConfigureBlendshapeShaders(const ResourceBundle& resources,
262 std::vector<BlendshapeShaderConfigurationRequest>&& requests,
263 StringCallback onError = DefaultErrorCallback) const;
265 SceneDefinition& operator=(SceneDefinition&& other);
268 bool FindNode(const std::string& name, std::unique_ptr<NodeDefinition>** result);
271 std::vector<std::unique_ptr<NodeDefinition>> mNodes; // size unknown up front (may discard nodes).
272 std::vector<Index> mRootNodeIds;
275 } // namespace SceneLoader
278 #endif //DALI_SCENE_LOADER_SCENE_DEFINITION_H_