1 #ifndef DALI_SCENE3D_MODEL_COMPONENTS_MODEL_PRIMITIVE_IMPL_H
2 #define DALI_SCENE3D_MODEL_COMPONENTS_MODEL_PRIMITIVE_IMPL_H
5 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/common/dali-common.h>
23 #include <dali/public-api/common/intrusive-ptr.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/object/property-value.h>
26 #include <dali/public-api/object/property.h>
30 #include <dali-scene3d/internal/model-components/material-modify-observer.h>
31 #include <dali-scene3d/internal/model-components/model-primitive-modify-observer.h>
32 #include <dali-scene3d/public-api/light/light.h>
33 #include <dali-scene3d/public-api/loader/blend-shape-details.h>
34 #include <dali-scene3d/public-api/loader/mesh-definition.h>
35 #include <dali-scene3d/public-api/loader/shader-manager.h>
36 #include <dali-scene3d/public-api/model-components/material.h>
37 #include <dali-scene3d/public-api/model-components/model-primitive.h>
45 using ModelPrimitivePtr = IntrusivePtr<ModelPrimitive>;
48 * @brief TODO : Explain me.
49 * Use Vector4 Tangent data
50 * Same ModelPrimitive all shares IBL
52 class ModelPrimitive : public BaseObject, public MaterialModifyObserver
55 using ModelPrimitiveModifyObserverContainer = std::set<ModelPrimitiveModifyObserver*>;
58 // Creation & Destruction
60 * @brief Create a new ModelPrimitive object.
61 * @return A smart-pointer to the newly allocated ModelPrimitive.
63 static ModelPrimitivePtr New();
67 * @brief Construct a new ModelPrimitive.
72 * @brief Second-phase constructor.
77 * @brief Virtual destructor.
79 virtual ~ModelPrimitive();
83 * @brief Set Renderer that is created by Scene3D::Loader internally.
85 void SetRenderer(Dali::Renderer renderer);
88 * @copydoc Dali::Scene3D::ModelPrimitive::GetRenderer()
90 Dali::Renderer GetRenderer() const;
93 * @copydoc Dali::Scene3D::ModelPrimitive::SetGeometry()
95 void SetGeometry(Dali::Geometry geometry);
98 * @copydoc Dali::Scene3D::ModelPrimitive::GetGeometry()
100 Dali::Geometry GetGeometry() const;
103 * @copydoc Dali::Scene3D::ModelPrimitive::SetMaterial()
105 void SetMaterial(Dali::Scene3D::Material material, bool updateRenderer = true);
108 * @copydoc Dali::Scene3D::ModelPrimitive::GetMaterial()
110 Dali::Scene3D::Material GetMaterial() const;
113 * @brief Adds a primitive observer to this model primitive.
115 * @param[in] observer The observer to add.
117 void AddPrimitiveObserver(ModelPrimitiveModifyObserver* observer);
120 * @brief Removes a primitive observer from this model primitive.
122 * @param[in] observer The observer to remove.
124 void RemovePrimitiveObserver(ModelPrimitiveModifyObserver* observer);
127 * @brief Sets the image-based lighting texture for this model primitive.
129 * @param[in] diffuseTexture The diffuse texture.
130 * @param[in] specularTexture The specular texture.
131 * @param[in] iblScaleFactor The scale factor to set for image-based lighting.
132 * @param[in] specularMipmapLevels The number of mipmap levels of specular texture.
134 void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels);
137 * @brief Sets the scale factor for image-based lighting for this model primitive.
139 * @param[in] iblScaleFactor The scale factor to set for image-based lighting.
141 void SetImageBasedLightScaleFactor(float iblScaleFactor);
144 * @brief Adds a Light on this medel primitive
146 * @param[in] light Added light.
147 * @param[in] lightIndex Index of added light.
149 void AddLight(Scene3D::Light light, uint32_t lightIndex);
152 * @brief Removes a light at the lightIndex of this model primitive
154 * @param[in] lightIndex Index of added light.
156 void RemoveLight(uint32_t lightIndex);
159 * @brief Updates shaders by using current material
161 * @param[in] shaderManager Shader manager to create shader.
163 void UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager);
166 * @brief Sets the blend shape data for this model primitive.
168 * @param[in] data The blend shape data to set.
170 void SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data);
173 * @brief Sets the blend shape geometry for this model primitive.
175 * @param[in] blendShapeGeometry The blend shape geometry to set.
177 void SetBlendShapeGeometry(Dali::Texture blendShapeGeometry);
180 * @brief Sets the blend shape options for this model primitive.
182 * @param[in] hasPositions Whether or not this model primitive has positions for blend shapes.
183 * @param[in] hasNormals Whether or not this model primitive has normals for blend shapes.
184 * @param[in] hasTangents Whether or not this model primitive has tangents for blend shapes.
185 * @param[in] version blendShape version.
187 void SetBlendShapeOptions(bool hasPositions, bool hasNormals, bool hasTangents, Scene3D::Loader::BlendShapes::Version version);
190 * @brief Sets whether or not this model primitive is skinned.
192 * @param[in] isSkinned Whether or not this model primitive is skinned.
194 void SetSkinned(bool isSkinned);
196 private: // From MaterialModifyObserver
198 * @copydoc Dali::Scene3D::Internal::Material::MaterialModifyObserver::OnMaterialModified()
200 void OnMaterialModified(Dali::Scene3D::Material material, MaterialModifyObserver::ModifyFlag flag) override;
204 * @brief Apply materials data into renderer.
206 void ApplyMaterialToRenderer(MaterialModifyObserver::ModifyFlag flag = MaterialModifyObserver::ModifyFlag::NONE);
209 * @brief Updates the uniform of renderer.
211 void UpdateRendererUniform();
214 * @brief Creates a renderer.
216 void CreateRenderer();
219 * @brief Updates the image-based lighting texture.
221 void UpdateImageBasedLightTexture();
224 // Delete copy & move operator
225 ModelPrimitive(const ModelPrimitive&) = delete;
226 ModelPrimitive(ModelPrimitive&&) = delete;
227 ModelPrimitive& operator=(const ModelPrimitive& rhs) = delete;
228 ModelPrimitive& operator=(ModelPrimitive&& rhs) noexcept = delete;
231 ModelPrimitiveModifyObserverContainer mObservers{};
234 Dali::Renderer mRenderer;
235 Dali::Geometry mGeometry;
236 Dali::Shader mShader;
237 Dali::TextureSet mTextureSet;
238 Dali::Scene3D::Material mMaterial;
240 Scene3D::Loader::ShaderManagerPtr mShaderManager;
243 std::vector<Scene3D::Light> mLights;
244 int32_t mLightCount{0};
247 Dali::Texture mSpecularTexture;
248 Dali::Texture mDiffuseTexture;
249 float mIblScaleFactor{1.0f};
250 uint32_t mSpecularMipmapLevels{1u};
253 Scene3D::Loader::BlendShapes::BlendShapeData mBlendShapeData;
254 Dali::Texture mBlendShapeGeometry;
255 bool mHasSkinning = false;
256 bool mHasPositions = false;
257 bool mHasNormals = false;
258 bool mHasTangents = false;
259 Scene3D::Loader::BlendShapes::Version mBlendShapeVersion = Scene3D::Loader::BlendShapes::Version::INVALID;
261 bool mIsMaterialChanged = false;
262 bool mNeedToSetRendererUniform = false;
265 } // namespace Internal
267 // Helpers for public-api forwarding methods
269 inline Internal::ModelPrimitive& GetImplementation(Dali::Scene3D::ModelPrimitive& modelPrimitive)
271 DALI_ASSERT_ALWAYS(modelPrimitive && "ModelPrimitive handle is empty");
273 BaseObject& handle = modelPrimitive.GetBaseObject();
275 return static_cast<Internal::ModelPrimitive&>(handle);
278 inline const Internal::ModelPrimitive& GetImplementation(const Dali::Scene3D::ModelPrimitive& modelPrimitive)
280 DALI_ASSERT_ALWAYS(modelPrimitive && "ModelPrimitive handle is empty");
282 const BaseObject& handle = modelPrimitive.GetBaseObject();
284 return static_cast<const Internal::ModelPrimitive&>(handle);
287 } // namespace Scene3D
291 #endif // DALI_SCENE3D_MODEL_COMPONENTS_MODEL_PRIMITIVE_IMPL_H