2 * Copyright (c) 2023 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/light/light-impl.h>
28 #include <dali-scene3d/internal/model-components/model-primitive-impl.h>
39 * Creates control through type registry
43 return Scene3D::ModelNode::New();
46 // Setup properties, signals and actions using the type-registry.
47 DALI_TYPE_REGISTRATION_BEGIN(Scene3D::ModelNode, Dali::CustomActor, Create);
48 DALI_TYPE_REGISTRATION_END()
49 } // unnamed namespace
51 Dali::Scene3D::ModelNode ModelNode::New()
53 // Create the implementation, temporarily owned on stack
54 IntrusivePtr<ModelNode> nodeImpl = new ModelNode();
56 // Pass ownership to handle
57 Scene3D::ModelNode handle(*nodeImpl);
59 // Second-phase init of the implementation
60 // This can only be done after the CustomActor connection has been made...
61 nodeImpl->Initialize();
66 ModelNode::ModelNode()
67 : CustomActorImpl(ActorFlags::DISABLE_SIZE_NEGOTIATION)
71 ModelNode::~ModelNode()
75 void ModelNode::Initialize()
78 mLights.resize(Scene3D::Internal::Light::GetMaximumEnabledLightCount());
81 void ModelNode::OnInitialize()
85 void ModelNode::OnSceneConnection(int depth)
89 void ModelNode::OnSceneDisconnection()
93 void ModelNode::OnChildAdd(Actor& child)
97 void ModelNode::OnChildRemove(Actor& child)
101 void ModelNode::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
105 void ModelNode::OnSizeSet(const Vector3& targetSize)
109 void ModelNode::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
111 // @todo size negotiate background to new size, animate as well?
114 void ModelNode::OnRelayout(const Vector2& size, RelayoutContainer& container)
118 void ModelNode::OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension)
122 Vector3 ModelNode::GetNaturalSize()
124 return Vector3::ZERO;
127 float ModelNode::CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension)
132 float ModelNode::GetHeightForWidth(float width)
137 float ModelNode::GetWidthForHeight(float height)
142 bool ModelNode::RelayoutDependentOnChildren(Dimension::Type dimension)
147 void ModelNode::OnCalculateRelayoutSize(Dimension::Type dimension)
151 void ModelNode::OnLayoutNegotiated(float size, Dimension::Type dimension)
155 ModelNode& GetImplementation(Dali::Scene3D::ModelNode& handle)
157 CustomActorImpl& customInterface = handle.GetImplementation();
158 ModelNode& impl = dynamic_cast<Internal::ModelNode&>(customInterface);
162 const ModelNode& GetImplementation(const Dali::Scene3D::ModelNode& handle)
164 const CustomActorImpl& customInterface = handle.GetImplementation();
165 // downcast to control
166 const ModelNode& impl = dynamic_cast<const Internal::ModelNode&>(customInterface);
172 uint32_t ModelNode::GetModelPrimitiveCount() const
174 return static_cast<uint32_t>(mModelPrimitiveContainer.size());
177 void ModelNode::AddModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
179 for(auto&& primitive : mModelPrimitiveContainer)
181 if(primitive == modelPrimitive)
187 mModelPrimitiveContainer.push_back(modelPrimitive);
190 GetImplementation(modelPrimitive).AddPrimitiveObserver(this);
191 if(mDiffuseTexture && mSpecularTexture)
193 GetImplementation(modelPrimitive).SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
196 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
197 for(uint32_t i = 0; i < maxLightCount; ++i)
201 GetImplementation(modelPrimitive).AddLight(mLights[i], i);
205 Dali::Renderer renderer = GetImplementation(modelPrimitive).GetRenderer();
208 uint32_t rendererCount = self.GetRendererCount();
210 for(uint32_t i = 0; i < rendererCount; ++i)
212 if(renderer == self.GetRendererAt(i))
220 self.AddRenderer(renderer);
225 void ModelNode::RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
227 uint32_t primitiveCount = GetModelPrimitiveCount();
228 for(uint32_t i = 0; i < primitiveCount; ++i)
230 if(mModelPrimitiveContainer[i] != modelPrimitive)
235 RemoveModelPrimitive(i);
240 void ModelNode::RemoveModelPrimitive(uint32_t index)
242 if(index >= mModelPrimitiveContainer.size())
247 uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
248 for(uint32_t i = 0; i < maxLightCount; ++i)
252 GetImplementation(mModelPrimitiveContainer[index]).RemoveLight(i);
257 GetImplementation(mModelPrimitiveContainer[index]).RemovePrimitiveObserver(this);
259 Dali::Renderer renderer = GetImplementation(mModelPrimitiveContainer[index]).GetRenderer();
262 self.RemoveRenderer(renderer);
265 mModelPrimitiveContainer.erase(mModelPrimitiveContainer.begin() + index);
268 Dali::Scene3D::ModelPrimitive ModelNode::GetModelPrimitive(uint32_t index) const
270 if(index < mModelPrimitiveContainer.size())
272 return mModelPrimitiveContainer[index];
274 return Scene3D::ModelPrimitive();
277 Scene3D::ModelNode ModelNode::FindChildModelNodeByName(std::string_view nodeName)
279 Actor childActor = Self().FindChildByName(nodeName);
280 return Scene3D::ModelNode::DownCast(childActor);
283 void ModelNode::RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const
285 blendShapeNames.reserve(blendShapeNames.size() + mBlendShapeIndexMap.size());
286 for(const auto& iter : mBlendShapeIndexMap)
288 blendShapeNames.push_back(iter.first);
292 Loader::BlendShapes::Index ModelNode::GetBlendShapeIndexByName(std::string_view blendShapeName) const
294 auto iter = mBlendShapeIndexMap.find(std::string(blendShapeName));
295 if(iter != mBlendShapeIndexMap.end())
299 return Loader::BlendShapes::INVALID_INDEX;
302 void ModelNode::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
304 mDiffuseTexture = diffuseTexture;
305 mSpecularTexture = specularTexture;
306 mIblScaleFactor = iblScaleFactor;
307 mSpecularMipmapLevels = specularMipmapLevels;
308 for(auto&& primitive : mModelPrimitiveContainer)
310 GetImplementation(primitive).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
314 void ModelNode::SetImageBasedLightScaleFactor(float iblScaleFactor)
316 mIblScaleFactor = iblScaleFactor;
317 for(auto&& primitive : mModelPrimitiveContainer)
319 GetImplementation(primitive).SetImageBasedLightScaleFactor(iblScaleFactor);
323 void ModelNode::AddLight(Scene3D::Light light, uint32_t lightIndex)
325 mLights[lightIndex] = light;
326 for(auto&& primitive : mModelPrimitiveContainer)
328 GetImplementation(primitive).AddLight(light, lightIndex);
332 void ModelNode::RemoveLight(uint32_t lightIndex)
334 for(auto&& primitive : mModelPrimitiveContainer)
336 GetImplementation(primitive).RemoveLight(lightIndex);
338 mLights[lightIndex].Reset();
341 void ModelNode::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data, Scene3D::ModelPrimitive primitive)
343 // Update mBlendShapeIndexMap
344 mBlendShapeIndexMap.clear();
345 const auto blendShapeCount = data.names.size();
346 for(Loader::BlendShapes::Index index = 0u; index < blendShapeCount; ++index)
348 auto& name = data.names[index];
351 mBlendShapeIndexMap[name] = index;
355 GetImplementation(primitive).SetBlendShapeData(data);
358 void ModelNode::SetBoneMatrix(const Matrix& inverseMatrix, Scene3D::ModelPrimitive primitive, Scene3D::Loader::Index& boneIndex)
360 Dali::Scene3D::Loader::Skinning::BoneData boneData;
361 boneData.primitive = primitive;
362 boneData.boneIndex = boneIndex;
363 char propertyNameBuffer[32];
364 snprintf(propertyNameBuffer, sizeof(propertyNameBuffer), "%s[%d]", Dali::Scene3D::Loader::Skinning::BONE_UNIFORM_NAME, boneIndex);
365 boneData.propertyName = propertyNameBuffer;
366 boneData.inverseMatrix = inverseMatrix;
367 mBoneDataContainer.push_back(std::move(boneData));
369 UpdateBoneMatrix(primitive);
372 void ModelNode::OnRendererCreated(Renderer renderer)
374 Self().AddRenderer(renderer);
377 void ModelNode::UpdateBoneMatrix(Scene3D::ModelPrimitive primitive)
379 for(auto&& boneData : mBoneDataContainer)
381 if(boneData.primitive != primitive)
386 Dali::Renderer renderer = GetImplementation(primitive).GetRenderer();
392 if(boneData.constraint)
394 boneData.constraint.Remove();
395 boneData.constraint.Reset();
398 auto propBoneXform = renderer.GetPropertyIndex(boneData.propertyName);
399 if(propBoneXform == Property::INVALID_INDEX)
401 propBoneXform = renderer.RegisterProperty(boneData.propertyName, Matrix{false});
404 Matrix inverseMatrix = boneData.inverseMatrix;
405 // Constrain bone matrix to joint transform.
406 boneData.constraint = Constraint::New<Matrix>(renderer, propBoneXform, [inverseMatrix](Matrix& output, const PropertyInputContainer& inputs) { Matrix::Multiply(output, inverseMatrix, inputs[0]->GetMatrix()); });
408 Actor joint = Self();
409 boneData.constraint.AddSource(Source{joint, Actor::Property::WORLD_MATRIX});
410 boneData.constraint.ApplyPost();
415 } // namespace Internal
417 } // namespace Scene3D