1 #ifndef DALI_SCENE3D_LOADER_NODE_DEFINITION_H_
2 #define DALI_SCENE3D_LOADER_NODE_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>
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>
45 * @brief Interface to report (const) resource ids to.
47 class DALI_SCENE3D_API IResourceReceiver
50 virtual ~IResourceReceiver() = default;
52 virtual void Register(ResourceType::Value type, Index id) = 0;
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.
60 class DALI_SCENE3D_API IResourceReflector
63 virtual ~IResourceReflector() = default;
65 virtual void Reflect(ResourceType::Value type, Index& id) = 0;
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.
73 struct DALI_SCENE3D_API ConstraintDefinition
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.
78 bool operator<(const ConstraintDefinition& other) const
80 return mProperty < other.mProperty;
83 bool operator==(const ConstraintDefinition& other) const
85 return mSourceIdx == other.mSourceIdx && mProperty == other.mProperty;
88 bool operator!=(const ConstraintDefinition& other) const
90 return !operator==(other);
94 struct DALI_SCENE3D_API Transforms
96 MatrixStack modelStack;
97 const ViewProjection& viewProjection;
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.
104 struct DALI_SCENE3D_API SkinningShaderConfigurationRequest
108 ModelPrimitive mPrimitive;
110 bool operator<(const SkinningShaderConfigurationRequest& other) const
112 return mShader < other.mShader || (mShader == other.mShader && mPrimitive < other.mPrimitive);
117 * @brief Needed to configure blend shape properties.
119 struct DALI_SCENE3D_API BlendshapeShaderConfigurationRequest
121 std::string mNodeName;
124 ModelPrimitive mPrimitive;
126 bool operator<(const BlendshapeShaderConfigurationRequest& other) const
128 return mShader < other.mShader || (mShader == other.mShader && mPrimitive < other.mPrimitive);
133 * @brief Request for creating a constraint, output from NodeDefinition::OnCreate.
135 struct DALI_SCENE3D_API ConstraintRequest
137 const ConstraintDefinition* const mConstraint; ///< Definition of the constraint to create.
138 Actor mTarget; ///< Target of the constraint.
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.
146 struct DALI_SCENE3D_API NodeDefinition
149 using Vector = std::vector<NodeDefinition>;
154 ResourceBundle& mResources;
156 Dali::Scene3D::Loader::ShaderManagerPtr mShaderManager;
159 std::vector<ConstraintRequest> mConstrainables;
160 std::vector<SkinningShaderConfigurationRequest> mSkinnables;
161 std::vector<BlendshapeShaderConfigurationRequest> mBlendshapeRequests;
164 class DALI_SCENE3D_API Renderable
167 Index mShaderIdx = INVALID_INDEX;
170 virtual ~Renderable() = default;
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;
178 struct CustomizationDefinition
182 Index GetChildId(const Customization::Choices& choices, const NodeDefinition& node)
184 auto choice = choices.Get(mTag);
185 return std::min(choice != Customization::NONE ? choice : 0,
186 static_cast<Index>(node.mChildren.size() - 1));
193 virtual void Start(NodeDefinition& n) = 0;
194 virtual void Finish(NodeDefinition& n) = 0;
197 ~IVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
203 virtual void Start(const NodeDefinition& n) = 0;
204 virtual void Finish(const NodeDefinition& n) = 0;
207 ~IConstVisitor() = default; // deliberately non-virtual these are transient objects and we don't want to pay for the vtable.
213 Property::Value mValue;
215 bool operator<(const Extra& other) const
217 return mKey < other.mKey;
223 * @brief Creates a ModelNode from this definition only.
224 * @note Not recursive.
226 ModelNode CreateModelNode(CreateParams& params);
229 * @brief Gets local space matrix of this node
230 * @return Matrix of local space.
232 Matrix GetLocalSpace() const;
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.
241 bool GetExtents(const ResourceBundle& resources, Vector3& min, Vector3& max) const;
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.
248 static std::string_view GetIblScaleFactorUniformName();
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.
255 static std::string_view GetIblYDirectionUniformName();
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.
262 static std::string_view GetIblMaxLodUniformName();
265 static const char* ORIGINAL_MATRIX_PROPERTY_NAME;
268 uint32_t mNodeId = INVALID_INDEX;
270 Vector3 mPosition = Vector3::ZERO;
271 Quaternion mOrientation = Quaternion::IDENTITY;
272 Vector3 mScale = Vector3::ONE;
273 Vector3 mSize = Vector3::ONE;
275 bool mIsVisible = true;
277 std::vector<std::unique_ptr<Renderable>> mRenderables;
278 std::unique_ptr<CustomizationDefinition> mCustomization;
279 std::vector<Extra> mExtras;
280 std::vector<ConstraintDefinition> mConstraints;
282 std::vector<Index> mChildren;
283 Index mParentIdx = INVALID_INDEX;
286 class DALI_SCENE3D_API ModelRenderable : public NodeDefinition::Renderable
289 Vector4 mColor = Color::WHITE;
290 Index mMeshIdx = INVALID_INDEX;
291 Index mMaterialIdx = INVALID_INDEX;
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;
301 * @brief Parameters for an Arc node.
303 class DALI_SCENE3D_API ArcRenderable : public ModelRenderable
306 bool mAntiAliasing = true;
308 float mStartAngleDegrees = .0f;
309 float mEndAngleDegrees = .0f;
313 static void GetEndVectorWithDiffAngle(float startAngle, float endAngle, Vector2& endVector);
315 void OnCreate(const NodeDefinition& nodeDefinition, NodeDefinition::CreateParams& params, ModelNode& node) const override;
318 } // namespace Loader
319 } // namespace Scene3D
322 #endif // DALI_SCENE3D_LOADER_NODE_DEFINITION_H_