1 #ifndef DALI_SCENE3D_LOADERER_SCENE_DEFINITION_H_
2 #define DALI_SCENE3D_LOADERER_SCENE_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-scene3d/public-api/loader/customization.h"
22 #include "dali-scene3d/public-api/loader/node-definition.h"
23 #include "dali-scene3d/public-api/loader/string-callback.h"
24 #include "dali-scene3d/public-api/loader/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"
43 * @brief Intermediate representation of a scene with functionality required to
44 * create DALi objects (Actors, Renderers) from it.
46 class DALI_SCENE3D_API SceneDefinition
49 using NodePredicate = std::function<bool(const NodeDefinition&)>;
50 using NodeConsumer = std::function<void(NodeDefinition&)>;
51 using ConstNodeConsumer = std::function<void(const NodeDefinition&)>;
55 SceneDefinition(SceneDefinition&& other);
59 * @brief Registers a scene root node.
60 * @return The index of the scene root node *registration*.
62 Index AddRootNode(Index iNode);
65 * @return the list of scene root node IDs in the order of their loading.
67 const std::vector<Index>& GetRoots() const;
70 * @brief Removes scene root registration at the given index @a iRoot.
71 * @note @a iRoot is the index of the registration (i.e. into the vector returned by GetRoots()),
74 void RemoveRootNode(Index iRoot);
77 * @return The number of node( definition)s in the scene.
79 uint32_t GetNodeCount() const;
82 * @return Const pointer to the node (definition) at the given index.
84 const NodeDefinition* GetNode(Index iNode) const;
87 * @return Pointer to the node (definition) at the given index.
89 NodeDefinition* GetNode(Index iNode);
92 * @brief Traverses the scene starting from the node at the given index into
93 * nodes, using the given customization @a choices and the visitor @a v.
95 void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IVisitor& v);
98 * @brief Traverses the scene starting from the node at the given index into
99 * nodes, using the given customization @a choices and the visitor @a v.
101 void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IConstVisitor& v) const;
104 * @brief Counts the references to meshes, shaders, materials that nodes in
105 * the scene are holding, writing the results into @a refCounts.
106 * @note @a refCounts' entries must have the correct size. Use ResourceBundle::GetRefCounter().
108 void CountResourceRefs(Index iNode, const Customization::Choices& choices, ResourceRefCounts& refCounts) const;
111 * @brief Given a bundle of @a resources that are loaded, and customization
112 * @a choices, this method traverses the scene, creating the actors and renderers
113 * from node definitions.
114 * @return Handle to the root actor.
116 Actor CreateNodes(Index iNode, const Customization::Choices& choices, NodeDefinition::CreateParams& params) const;
119 * @brief Creates / update a registry of mappings from customization tags to
120 * a lists of names of customizable nodes under each tag, and the number of
121 * options. If @a outMissingChoices was specified, each tag that it encounters
122 * in the scene but not in @a choices, will be registered on it with the default
125 void GetCustomizationOptions(const Customization::Choices& choices,
126 Customization::Map& outCustomizationOptions,
127 Customization::Choices* outMissingChoices) const;
130 * @brief Attempts to add @a nodeDef to the end of nodes, and its index to the end of
131 * its parent's list of children (if iParent != NodeDefinition::INVALID_PARENT).
132 * @return If the operation was successful - which requires nodeDef->name to be unique -
133 * a pointer to the stored node definition; nullptr otherwise.
135 NodeDefinition* AddNode(std::unique_ptr<NodeDefinition>&& nodeDef);
138 * @brief Moves the node to some other parent and / or to a different index.
139 * @return Whether the operation was successful.
140 * @note This is currently breaking an assumption of never having a child of a node at a lower
141 * index as that of the node itself, due to the fact that we're only changing parent ids (and
142 * entries into the vector of children of node definitions), to save the complication of having
143 * to move about, and offset indices to, everything past the reparented node. This should be
144 * sufficient AT LEAST AS LONG AS we recreate the SceneDefinition when loading the scene; if
145 * we ever needed to serialize it, we should ensure correct ordering of nodes.
147 bool ReparentNode(const std::string& name, const std::string& newParentName, Index siblingOrder);
150 * @brief Removes a node with the given name, including all of its children, and updating
151 * the indices on all remaining node definitions.
152 * @return Whether the operation was successful.
154 bool RemoveNode(const std::string& name);
157 * @brief Builds the model matrix stack for the node at the given @a index.
158 * @note It only pushes new matrices; does not require the stack to be empty (or cares if it was not).
160 void GetNodeModelStack(Index index, MatrixStack& model) const;
163 * @brief Attempts to find the definition of a node with the given @a name. Only upon
164 * success, and if @a outIndex is non-null, the index of the node is written to it.
165 * @return Pointer to the node definition; nullptr if not found.
166 * @note No ownership transfer.
168 NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr);
171 * @brief Attempts to find the definition of a node with the given @a name. Only upon
172 * success, and if @a outIndex is non-null, the index of the node is written to it.
173 * @return Pointer to the node definition; nullptr if not found.
174 * @note No ownership transfer.
176 const NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr) const;
179 * @return The index of the given NodeDefinition@ a node, or -1 if the node definition
182 Index FindNodeIndex(const NodeDefinition& node) const;
185 * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true
187 * @note A @a limit value of 0 means no limit.
189 void FindNodes(NodePredicate predicate, NodeConsumer consumer, unsigned int limit = 0);
192 * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true
194 * @note A @a limit value of 0 means no limit.
196 void FindNodes(NodePredicate predicate, ConstNodeConsumer consumer, unsigned int limit = 0) const;
199 * @brief Applies constraints from the given requests.
201 void ApplyConstraints(Actor& root,
202 std::vector<ConstraintRequest>&& constrainables,
203 StringCallback onError = DefaultErrorCallback) const;
206 * @brief Sets up joint matrix properties and constraints on actors that are involved in skeletal
207 * animation (i.e. those that are between (inclusive) the lower and upper bounds of any skeleton),
208 * to ensure the correct update of meshes skinned to these skeletons.
209 * @param iRoot The index of the scene root node. Skeletons that aren't descendants of this node
211 * @param skeletons The list of skeletons that require setting up.
212 * @param rootActor The Actor corresponding to the root node, which will be used to locate
215 void ConfigureSkeletonJoints(uint32_t iRoot, const SkeletonDefinition::Vector& skeletons, Actor rootActor) const;
218 * @brief Ensures that there is no overlap between shaders used by nodes that have
219 * meshes skinned to different skeletons.
221 void EnsureUniqueSkinningShaderInstances(ResourceBundle& resources) const;
224 * @brief Performs the configuration of the given skinning shaders with the given skeleton
225 * This means that the absolute transforms of the joints are calculated and set as one of
226 * the uniforms in the mat4 @b uBone array (in depth first traversal). Further, the following
228 * - a @b jointMatrix property on each joint Actor;<br />
229 * - constraint from the Actor's local position and rotation (and if it has a @e joint
230 * parent, the jointMatrix of the parent) to its @b jointMatrix property;<br />
231 * - a constraint from the the Actor's @b jointMatrix property to the related entry in
232 * the shader's @b uBone property;<br />
233 * This ensures the automatic update of the skeletal animation, should any of the joints'
234 * transform changes, by whatever means.
235 * @return The success of the operations. Error messages will be posted to the optional
236 * @a onError callback.
237 * @note A maximum of SkinningDetails::MAX_JOINTS joints per skeleton are supported at the moment.
238 * @note Even if multiple skinned meshes use the same skinning shader, the correct number
239 * of separate instances need to be declared in the .dli to avoid clashing uniform
240 * definitions and constraints.
242 void ConfigureSkinningShaders(const ResourceBundle& resources,
244 std::vector<SkinningShaderConfigurationRequest>&& requests) const;
247 * @brief Ensures there is no two meshes with blend shapes sharing the same shader.
249 void EnsureUniqueBlendShapeShaderInstances(ResourceBundle& resources) const;
252 * @brief Performs the configuration of the given blend shapes.
254 * For each node with blend shapes it registers into the actor the weights properties for each morph target
255 * and some needed uniforms into the shader.
257 * @param[in] root The root actor.
258 * @param[in] requests The requests to configure blend shapes.
259 * @param[in] resources The resources bundle. Meshes need to be accessed to configure the blend shapes.
260 * @param[in] onError The error callback.
262 bool ConfigureBlendshapeShaders(const ResourceBundle& resources,
264 std::vector<BlendshapeShaderConfigurationRequest>&& requests,
265 StringCallback onError = DefaultErrorCallback) const;
267 SceneDefinition& operator=(SceneDefinition&& other);
270 bool FindNode(const std::string& name, std::unique_ptr<NodeDefinition>** result);
273 std::vector<std::unique_ptr<NodeDefinition>> mNodes; // size unknown up front (may discard nodes).
274 std::vector<Index> mRootNodeIds;
277 } // namespace Loader
278 } // namespace Scene3D
281 #endif //DALI_SCENE3D_LOADERER_SCENE_DEFINITION_H_