1 #ifndef DALI_SCENE3D_LOADER_SCENE_DEFINITION_H_
2 #define DALI_SCENE3D_LOADER_SCENE_DEFINITION_H_
4 * Copyright (c) 2023 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/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>
29 #include <dali-scene3d/public-api/loader/customization.h>
30 #include <dali-scene3d/public-api/loader/node-definition.h>
31 #include <dali-scene3d/public-api/loader/string-callback.h>
32 #include <dali-scene3d/public-api/loader/utils.h>
34 namespace Dali::Scene3D::Loader
39 * @brief Intermediate representation of a scene with functionality required to
40 * create DALi objects (Actors, Renderers) from it.
43 class DALI_SCENE3D_API SceneDefinition
46 using NodePredicate = std::function<bool(const NodeDefinition&)>;
47 using NodeConsumer = std::function<void(NodeDefinition&)>;
48 using ConstNodeConsumer = std::function<void(const NodeDefinition&)>;
52 SceneDefinition(SceneDefinition&& other);
56 * @brief Registers a scene root node.
58 * @return The index of the scene root node *registration*.
60 Index AddRootNode(Index iNode);
63 * @brief Retrieves a list of scene root node IDs in the order of loading.
65 * @return List of scene root node IDs in the order of loading
67 const std::vector<Index>& GetRoots() const;
70 * @brief Removes scene root registration at the given index @a iRoot.
72 * @note @a iRoot is the index of the registration (i.e. into the vector returned by GetRoots()), not of the node.
74 void RemoveRootNode(Index iRoot);
77 * @brief Retrieve the number of node(definition)s in the scene.
79 * @return The number of node(definition)s in the scene
81 uint32_t GetNodeCount() const;
84 * @brief Retrieve a const pointer to the node (definition) at the given index.
86 * @return Const pointer to the node (definition).
88 const NodeDefinition* GetNode(Index iNode) const;
91 * @brief Retrive a pointer to the node (definition) at the given index.
93 * @return Pointer to the node (definition).
95 NodeDefinition* GetNode(Index iNode);
98 * @brief Traverses the scene starting from the node at the given index into nodes,
99 * using the given customization @a choices and the visitor @a v.
102 void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IVisitor& v);
105 * @brief Traverses the scene starting from the node at the given index into nodes,
106 * using the given customization @a choices and the visitor @a v.
109 void Visit(Index iNode, const Customization::Choices& choices, NodeDefinition::IConstVisitor& v) const;
112 * @brief Counts the references to meshes, shaders, materials that nodes in the scene are holding,
113 * writing the results into @a refCounts.
115 * @note @a refCounts' entries must have the correct size. Use ResourceBundle::GetRefCounter().
117 void CountResourceRefs(Index iNode, const Customization::Choices& choices, ResourceRefCounts& refCounts) const;
120 * @brief Given a bundle of @a resources that are loaded, and customization @a choices,
121 * this method traverses the scene, creating the ModelNodes and renderers from node definitions.
123 * @return Handle to the root node.
125 ModelNode CreateNodes(Index iNode, const Customization::Choices& choices, NodeDefinition::CreateParams& params);
128 * @brief Creates / update a registry of mappings from customization tags to a lists of names of
129 * customizable nodes under each tag, and the number of options.
131 * If @a outMissingChoices was specified, each tag that it encounters in the scene but not in @a choices,
132 * will be registered on it with the default choice of 0.
135 void GetCustomizationOptions(const Customization::Choices& choices,
136 Customization::Map& outCustomizationOptions,
137 Customization::Choices* outMissingChoices) const;
140 * @brief Attempts to add @a nodeDef to the end of nodes, and its index to the end of its parent's
141 * list of children (if iParent != NodeDefinition::INVALID_PARENT).
143 * @return If the operation was successful - which requires nodeDef->name to be unique -
144 * a pointer to the stored node definition; nullptr otherwise.
146 NodeDefinition* AddNode(std::unique_ptr<NodeDefinition>&& nodeDef);
149 * @brief Moves the node to some other parent and / or to a different index.
151 * @return Whether the operation was successful.
152 * @note This is currently breaking an assumption of never having a child of a node at a lower
153 * index as that of the node itself, due to the fact that we're only changing parent ids (and
154 * entries into the vector of children of node definitions), to save the complication of having
155 * to move about, and offset indices to, everything past the reparented node. This should be
156 * sufficient AT LEAST AS LONG AS we recreate the SceneDefinition when loading the scene; if
157 * we ever needed to serialize it, we should ensure correct ordering of nodes.
159 bool ReparentNode(const std::string& name, const std::string& newParentName, Index siblingOrder);
162 * @brief Removes a node with the given name, including all of its children, and updating
163 * the indices on all remaining node definitions.
165 * @return Whether the operation was successful.
167 bool RemoveNode(const std::string& name);
170 * @brief Builds the model matrix stack for the node at the given @a index.
172 * @note It only pushes new matrices; does not require the stack to be empty (or cares if it was not).
174 void GetNodeModelStack(Index index, MatrixStack& model) const;
177 * @brief Attempts to find the definition of a node with the given @a name. Only upon
178 * success, and if @a outIndex is non-null, the index of the node is written to it.
180 * @return Pointer to the node definition; nullptr if not found.
181 * @note No ownership transfer.
183 NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr);
186 * @brief Attempts to find the definition of a node with the given @a name. Only upon
187 * success, and if @a outIndex is non-null, the index of the node is written to it.
189 * @return Pointer to the node definition; nullptr if not found.
190 * @note No ownership transfer.
192 const NodeDefinition* FindNode(const std::string& name, Index* outIndex = nullptr) const;
195 * @brief The index of the given NodeDefinition @a node, or -1 if the node definition was not found.
197 * @return The index of the given NodeDefinition @a node, or -1 if the node definition was not found
199 Index FindNodeIndex(const NodeDefinition& node) const;
202 * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true
205 * @note A @a limit value of 0 means no limit.
207 void FindNodes(NodePredicate predicate, NodeConsumer consumer, unsigned int limit = 0);
210 * @brief Calls @a consumer with up to @a limit NodeDefinitions that evaluate to true with @a predicate.
212 * @note A @a limit value of 0 means no limit.
214 void FindNodes(NodePredicate predicate, ConstNodeConsumer consumer, unsigned int limit = 0) const;
217 * @brief Applies constraints from the given requests.
220 void ApplyConstraints(Actor& root,
221 std::vector<ConstraintRequest>&& constrainables,
222 StringCallback onError = DefaultErrorCallback) const;
225 * @brief Ensures that there is no overlap between shaders used by nodes that have meshes skinned to different skeletons.
228 void EnsureUniqueSkinningShaderInstances(ResourceBundle& resources) const;
231 * @brief Performs the configuration of the given skinning shaders with the given skeleton.
233 * This means that the absolute transforms of the joints are calculated and set as one of
234 * the uniforms in the mat4 @b uBone array (in depth first traversal). Further, the following
236 * - a @b jointMatrix property on each joint Actor;<br />
237 * - constraint from the Actor's local position and rotation (and if it has a @e joint
238 * parent, the jointMatrix of the parent) to its @b jointMatrix property;<br />
239 * - a constraint from the the Actor's @b jointMatrix property to the related entry in
240 * the shader's @b uBone property;<br />
241 * This ensures the automatic update of the skeletal animation, should any of the joints' transform changes,
244 * @note Error messages will be posted to the optional @a onError callback.
245 * @note A maximum of SkinningDetails::MAX_JOINTS joints per skeleton are supported at the moment.
246 * @note Even if multiple skinned meshes use the same skinning shader, the correct number
247 * of separate instances need to be declared in the .dli to avoid clashing uniform
248 * definitions and constraints.
250 void ConfigureSkinningShaders(const ResourceBundle& resources,
252 std::vector<SkinningShaderConfigurationRequest>&& requests) const;
255 * @brief Ensures there is no two meshes with blend shapes sharing the same shader.
258 void EnsureUniqueBlendShapeShaderInstances(ResourceBundle& resources) const;
261 * @brief Performs the configuration of the given blend shapes.
263 * For each node with blend shapes it registers into the actor the weights properties for each morph target
264 * and some needed uniforms into the shader.
267 * @param[in] root The root actor.
268 * @param[in] requests The requests to configure blend shapes.
269 * @param[in] resources The resources bundle. Meshes need to be accessed to configure the blend shapes.
270 * @param[in] onError The error callback.
272 bool ConfigureBlendshapeShaders(const ResourceBundle& resources,
274 std::vector<BlendshapeShaderConfigurationRequest>&& requests,
275 StringCallback onError = DefaultErrorCallback) const;
277 SceneDefinition& operator=(SceneDefinition&& other);
280 bool FindNode(const std::string& name, std::unique_ptr<NodeDefinition>** result);
283 std::vector<std::unique_ptr<NodeDefinition>> mNodes; // size unknown up front (may discard nodes).
284 std::vector<Index> mRootNodeIds;
287 } // namespace Dali::Scene3D::Loader
289 #endif //DALI_SCENE3D_LOADER_SCENE_DEFINITION_H_