2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali-scene3d/internal/model-components/model-node-impl.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/public-api/object/type-registry.h>
27 #include <dali-scene3d/internal/controls/model/model-impl.h>
28 #include <dali-scene3d/internal/light/light-impl.h>
29 #include <dali-scene3d/internal/model-components/model-primitive-impl.h>
40 * Creates control through type registry
44 return Scene3D::ModelNode::New();
47 // Setup properties, signals and actions using the type-registry.
48 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::ModelNode, Dali::CustomActor, Create);
49 DALI_TYPE_REGISTRATION_END()
50 } // unnamed namespace
52 Dali::Scene3D::ModelNode ModelNode::New()
54 // Create the implementation, temporarily owned on stack
55 IntrusivePtr<ModelNode> nodeImpl = new ModelNode();
57 // Pass ownership to handle
58 Scene3D::ModelNode handle(*nodeImpl);
60 // Second-phase init of the implementation
61 // This can only be done after the CustomActor connection has been made...
62 nodeImpl->Initialize();
67 ModelNode::ModelNode()
68 : Control(static_cast<ControlBehaviour>(ControlBehaviour::DISABLE_STYLE_CHANGE_SIGNALS | ActorFlags::DISABLE_SIZE_NEGOTIATION))
72 ModelNode::~ModelNode()
76 void ModelNode::Initialize()
81 void ModelNode::OnInitialize()
84 self.RegisterProperty("uIsShadowCasting", static_cast<int>(mIsShadowCasting));
85 self.RegisterProperty("uIsShadowReceiving", static_cast<int>(mIsShadowReceiving));
88 void ModelNode::OnSceneConnection(int depth)
92 void ModelNode::OnSceneDisconnection()
96 void ModelNode::OnChildAdd(Actor& child)
100 void ModelNode::OnChildRemove(Actor& child)
104 void ModelNode::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
108 void ModelNode::OnSizeSet(const Vector3& targetSize)
112 void ModelNode::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
114 // @todo size negotiate background to new size, animate as well?
117 void ModelNode::OnRelayout(const Vector2& size, RelayoutContainer& container)
121 void ModelNode::OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension)
125 Vector3 ModelNode::GetNaturalSize()
127 return Vector3::ZERO;
130 float ModelNode::CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension)
135 float ModelNode::GetHeightForWidth(float width)
140 float ModelNode::GetWidthForHeight(float height)
145 bool ModelNode::RelayoutDependentOnChildren(Dimension::Type dimension)
150 void ModelNode::OnCalculateRelayoutSize(Dimension::Type dimension)
154 void ModelNode::OnLayoutNegotiated(float size, Dimension::Type dimension)
158 ModelNode& GetImplementation(Dali::Scene3D::ModelNode& handle)
160 CustomActorImpl& customInterface = handle.GetImplementation();
161 ModelNode& impl = dynamic_cast<Internal::ModelNode&>(customInterface);
165 const ModelNode& GetImplementation(const Dali::Scene3D::ModelNode& handle)
167 const CustomActorImpl& customInterface = handle.GetImplementation();
168 // downcast to control
169 const ModelNode& impl = dynamic_cast<const Internal::ModelNode&>(customInterface);
175 uint32_t ModelNode::GetModelPrimitiveCount() const
177 return static_cast<uint32_t>(mModelPrimitiveContainer.size());
180 void ModelNode::AddModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive, Loader::ShaderOption::HashType hash)
182 for(auto&& primitive : mModelPrimitiveContainer)
184 if(primitive == modelPrimitive)
190 mModelPrimitiveContainer.push_back(modelPrimitive);
193 GetImplementation(modelPrimitive).AddPrimitiveObserver(this);
194 if(mShadowMapTexture)
196 GetImplementation(modelPrimitive).SetShadowMapTexture(mShadowMapTexture);
199 if(mDiffuseTexture && mSpecularTexture)
201 GetImplementation(modelPrimitive).SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
204 GetImplementation(modelPrimitive).UpdateShader(mShaderManager, hash);
206 Dali::Renderer renderer = GetImplementation(modelPrimitive).GetRenderer();
209 uint32_t rendererCount = self.GetRendererCount();
211 for(uint32_t i = 0; i < rendererCount; ++i)
213 if(renderer == self.GetRendererAt(i))
221 self.AddRenderer(renderer);
226 void ModelNode::RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
228 uint32_t primitiveCount = GetModelPrimitiveCount();
229 for(uint32_t i = 0; i < primitiveCount; ++i)
231 if(mModelPrimitiveContainer[i] != modelPrimitive)
236 RemoveModelPrimitive(i);
241 void ModelNode::RemoveModelPrimitive(uint32_t index)
243 if(index >= mModelPrimitiveContainer.size())
248 GetImplementation(mModelPrimitiveContainer[index]).UpdateShader(nullptr, 0u);
251 GetImplementation(mModelPrimitiveContainer[index]).RemovePrimitiveObserver(this);
253 Dali::Renderer renderer = GetImplementation(mModelPrimitiveContainer[index]).GetRenderer();
256 self.RemoveRenderer(renderer);
259 mModelPrimitiveContainer.erase(mModelPrimitiveContainer.begin() + index);
262 Dali::Scene3D::ModelPrimitive ModelNode::GetModelPrimitive(uint32_t index) const
264 if(index < mModelPrimitiveContainer.size())
266 return mModelPrimitiveContainer[index];
268 return Scene3D::ModelPrimitive();
271 Scene3D::ModelNode ModelNode::FindChildModelNodeByName(std::string_view nodeName)
273 Actor childActor = Self().FindChildByName(nodeName);
274 return Scene3D::ModelNode::DownCast(childActor);
277 void ModelNode::RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const
279 blendShapeNames.reserve(blendShapeNames.size() + mBlendShapeIndexMap.size());
280 for(const auto& iter : mBlendShapeIndexMap)
282 blendShapeNames.push_back(iter.first);
286 Loader::BlendShapes::Index ModelNode::GetBlendShapeIndexByName(std::string_view blendShapeName) const
288 auto iter = mBlendShapeIndexMap.find(std::string(blendShapeName));
289 if(iter != mBlendShapeIndexMap.end())
293 return Loader::BlendShapes::INVALID_INDEX;
296 void ModelNode::SetShadowMapTexture(Dali::Texture shadowMapTexture)
298 mShadowMapTexture = shadowMapTexture;
299 for(auto&& primitive : mModelPrimitiveContainer)
301 GetImplementation(primitive).SetShadowMapTexture(mShadowMapTexture);
305 void ModelNode::CastShadow(bool castShadow)
307 if(mIsShadowCasting == castShadow)
312 mIsShadowCasting = castShadow;
315 self.RegisterProperty("uIsShadowCasting", static_cast<int>(mIsShadowCasting));
318 bool ModelNode::IsShadowCasting() const
320 return mIsShadowCasting;
323 void ModelNode::ReceiveShadow(bool receiveShadow)
325 if(mIsShadowReceiving == receiveShadow)
330 mIsShadowReceiving = receiveShadow;
333 self.RegisterProperty("uIsShadowReceiving", static_cast<int>(mIsShadowReceiving));
336 bool ModelNode::IsShadowReceiving() const
338 return mIsShadowReceiving;
341 void ModelNode::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
343 mDiffuseTexture = diffuseTexture;
344 mSpecularTexture = specularTexture;
345 mIblScaleFactor = iblScaleFactor;
346 mSpecularMipmapLevels = specularMipmapLevels;
347 for(auto&& primitive : mModelPrimitiveContainer)
349 GetImplementation(primitive).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
353 void ModelNode::SetImageBasedLightScaleFactor(float iblScaleFactor)
355 mIblScaleFactor = iblScaleFactor;
356 for(auto&& primitive : mModelPrimitiveContainer)
358 GetImplementation(primitive).SetImageBasedLightScaleFactor(iblScaleFactor);
362 void ModelNode::UpdateShader(Scene3D::Loader::ShaderManagerPtr shaderManager)
364 if(mShaderManager != shaderManager)
366 mShaderManager = shaderManager;
367 for(auto&& primitive : mModelPrimitiveContainer)
369 GetImplementation(primitive).UpdateShader(mShaderManager, 0u);
374 void ModelNode::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data, Scene3D::ModelPrimitive primitive)
376 // Update mBlendShapeIndexMap
377 mBlendShapeIndexMap.clear();
378 const auto blendShapeCount = data.names.size();
379 for(Loader::BlendShapes::Index index = 0u; index < blendShapeCount; ++index)
381 auto& name = data.names[index];
384 mBlendShapeIndexMap[name] = index;
388 GetImplementation(primitive).SetBlendShapeData(data);
391 void ModelNode::SetBoneMatrix(const Matrix& inverseMatrix, Scene3D::ModelPrimitive primitive, Scene3D::Loader::Index& boneIndex)
393 Dali::Scene3D::Loader::Skinning::BoneData boneData;
394 boneData.primitive = primitive;
395 boneData.boneIndex = boneIndex;
396 char propertyNameBuffer[32];
397 snprintf(propertyNameBuffer, sizeof(propertyNameBuffer), "%s[%d]", Dali::Scene3D::Loader::Skinning::BONE_UNIFORM_NAME, boneIndex);
398 boneData.propertyName = propertyNameBuffer;
399 boneData.inverseMatrix = inverseMatrix;
400 mBoneDataContainer.push_back(std::move(boneData));
402 UpdateBoneMatrix(primitive);
405 void ModelNode::OnRendererCreated(Renderer renderer)
407 Self().AddRenderer(renderer);
410 void ModelNode::UpdateBoneMatrix(Scene3D::ModelPrimitive primitive)
412 for(auto&& boneData : mBoneDataContainer)
414 if(boneData.primitive != primitive)
419 Dali::Renderer renderer = GetImplementation(primitive).GetRenderer();
425 if(boneData.constraint)
427 boneData.constraint.Remove();
428 boneData.constraint.Reset();
431 auto propBoneXform = renderer.GetPropertyIndex(boneData.propertyName);
432 if(propBoneXform == Property::INVALID_INDEX)
434 propBoneXform = renderer.RegisterProperty(boneData.propertyName, Matrix{false});
437 Matrix inverseMatrix = boneData.inverseMatrix;
438 // Constrain bone matrix to joint transform.
439 boneData.constraint = Constraint::New<Matrix>(renderer, propBoneXform, [inverseMatrix](Matrix& output, const PropertyInputContainer& inputs) { Matrix::Multiply(output, inverseMatrix, inputs[0]->GetMatrix()); });
441 Actor joint = Self();
442 boneData.constraint.AddSource(Source{joint, Actor::Property::WORLD_MATRIX});
443 boneData.constraint.ApplyPost();
448 void ModelNode::SetColliderMesh(ColliderMeshUniquePtr&& colliderMesh)
450 if(!colliderMesh && !mColliderMesh)
455 if(!mParentModel) // find parent model if not set
457 auto parent = Self().GetParent();
460 auto modelHandle = Scene3D::Model::DownCast(parent);
463 mParentModel = &GetImpl(modelHandle);
466 parent = parent.GetParent();
470 // Resetting collider mesh if argument is nullptr
471 auto handle = Scene3D::ModelNode::DownCast(Self());
476 mParentModel->RemoveColliderMesh(handle);
479 // If collider mesh is to be set then register it with the parent model.
480 // If nullptr, ignore.
483 mParentModel->RegisterColliderMesh(handle);
487 mColliderMesh = std::move(colliderMesh);
490 bool ModelNode::HasColliderMesh() const
492 return mColliderMesh != nullptr;
495 const Scene3D::Algorithm::ColliderMesh& ModelNode::GetColliderMesh() const
497 return *mColliderMesh;
500 } // namespace Internal
502 } // namespace Scene3D