Merge "Change trace tag for text dorelayout" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / model-components / model-node-impl.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-scene3d/internal/model-components/model-node-impl.h>
20
21 // EXTERNAL INCLUDES
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>
25
26 // INTERNAL INCLUDES
27 #include <dali-scene3d/internal/model-components/model-primitive-impl.h>
28 #include <dali-scene3d/internal/light/light-impl.h>
29
30 namespace Dali
31 {
32 namespace Scene3D
33 {
34 namespace Internal
35 {
36 namespace
37 {
38 /**
39  * Creates control through type registry
40  */
41 BaseHandle Create()
42 {
43   return Scene3D::ModelNode::New();
44 }
45
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
50
51 Dali::Scene3D::ModelNode ModelNode::New()
52 {
53   // Create the implementation, temporarily owned on stack
54   IntrusivePtr<ModelNode> nodeImpl = new ModelNode();
55
56   // Pass ownership to handle
57   Scene3D::ModelNode handle(*nodeImpl);
58
59   // Second-phase init of the implementation
60   // This can only be done after the CustomActor connection has been made...
61   nodeImpl->Initialize();
62
63   return handle;
64 }
65
66 ModelNode::ModelNode()
67 : CustomActorImpl(ActorFlags::DISABLE_SIZE_NEGOTIATION)
68 {
69 }
70
71 ModelNode::~ModelNode()
72 {
73 }
74
75 void ModelNode::Initialize()
76 {
77   OnInitialize();
78   mLights.resize(Scene3D::Internal::Light::GetMaximumEnabledLightCount());
79 }
80
81 void ModelNode::OnInitialize()
82 {
83 }
84
85 void ModelNode::OnSceneConnection(int depth)
86 {
87 }
88
89 void ModelNode::OnSceneDisconnection()
90 {
91 }
92
93 void ModelNode::OnChildAdd(Actor& child)
94 {
95 }
96
97 void ModelNode::OnChildRemove(Actor& child)
98 {
99 }
100
101 void ModelNode::OnPropertySet(Property::Index index, const Property::Value& propertyValue)
102 {
103 }
104
105 void ModelNode::OnSizeSet(const Vector3& targetSize)
106 {
107 }
108
109 void ModelNode::OnSizeAnimation(Animation& animation, const Vector3& targetSize)
110 {
111   // @todo size negotiate background to new size, animate as well?
112 }
113
114 void ModelNode::OnRelayout(const Vector2& size, RelayoutContainer& container)
115 {
116 }
117
118 void ModelNode::OnSetResizePolicy(ResizePolicy::Type policy, Dimension::Type dimension)
119 {
120 }
121
122 Vector3 ModelNode::GetNaturalSize()
123 {
124   return Vector3::ZERO;
125 }
126
127 float ModelNode::CalculateChildSize(const Dali::Actor& child, Dimension::Type dimension)
128 {
129   return 0.0f;
130 }
131
132 float ModelNode::GetHeightForWidth(float width)
133 {
134   return 0.0f;
135 }
136
137 float ModelNode::GetWidthForHeight(float height)
138 {
139   return 0.0f;
140 }
141
142 bool ModelNode::RelayoutDependentOnChildren(Dimension::Type dimension)
143 {
144   return false;
145 }
146
147 void ModelNode::OnCalculateRelayoutSize(Dimension::Type dimension)
148 {
149 }
150
151 void ModelNode::OnLayoutNegotiated(float size, Dimension::Type dimension)
152 {
153 }
154
155 ModelNode& GetImplementation(Dali::Scene3D::ModelNode& handle)
156 {
157   CustomActorImpl& customInterface = handle.GetImplementation();
158   ModelNode&       impl            = dynamic_cast<Internal::ModelNode&>(customInterface);
159   return impl;
160 }
161
162 const ModelNode& GetImplementation(const Dali::Scene3D::ModelNode& handle)
163 {
164   const CustomActorImpl& customInterface = handle.GetImplementation();
165   // downcast to control
166   const ModelNode& impl = dynamic_cast<const Internal::ModelNode&>(customInterface);
167   return impl;
168 }
169
170 // Public Method
171
172 uint32_t ModelNode::GetModelPrimitiveCount() const
173 {
174   return static_cast<uint32_t>(mModelPrimitiveContainer.size());
175 }
176
177 void ModelNode::AddModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
178 {
179   for(auto&& primitive : mModelPrimitiveContainer)
180   {
181     if(primitive == modelPrimitive)
182     {
183       return;
184     }
185   }
186
187   mModelPrimitiveContainer.push_back(modelPrimitive);
188
189   Actor self = Self();
190   GetImplementation(modelPrimitive).AddPrimitiveObserver(this);
191   if(mDiffuseTexture && mSpecularTexture)
192   {
193     GetImplementation(modelPrimitive).SetImageBasedLightTexture(mDiffuseTexture, mSpecularTexture, mIblScaleFactor, mSpecularMipmapLevels);
194   }
195
196   uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
197   for(uint32_t i = 0; i < maxLightCount; ++i)
198   {
199     if(mLights[i])
200     {
201       GetImplementation(modelPrimitive).AddLight(mLights[i], i);
202     }
203   }
204
205   Dali::Renderer renderer = GetImplementation(modelPrimitive).GetRenderer();
206   if(renderer)
207   {
208     uint32_t rendererCount = self.GetRendererCount();
209     bool     exist         = false;
210     for(uint32_t i = 0; i < rendererCount; ++i)
211     {
212       if(renderer == self.GetRendererAt(i))
213       {
214         exist = true;
215         break;
216       }
217     }
218     if(!exist)
219     {
220       self.AddRenderer(renderer);
221     }
222   }
223 }
224
225 void ModelNode::RemoveModelPrimitive(Dali::Scene3D::ModelPrimitive modelPrimitive)
226 {
227   uint32_t primitiveCount = GetModelPrimitiveCount();
228   for(uint32_t i = 0; i < primitiveCount; ++i)
229   {
230     if(mModelPrimitiveContainer[i] != modelPrimitive)
231     {
232       continue;
233     }
234
235     RemoveModelPrimitive(i);
236     break;
237   }
238 }
239
240 void ModelNode::RemoveModelPrimitive(uint32_t index)
241 {
242   if(index >= mModelPrimitiveContainer.size())
243   {
244     return;
245   }
246
247   uint32_t maxLightCount = Scene3D::Internal::Light::GetMaximumEnabledLightCount();
248   for(uint32_t i = 0; i < maxLightCount; ++i)
249   {
250     if(mLights[i])
251     {
252       GetImplementation(mModelPrimitiveContainer[index]).RemoveLight(i);
253     }
254   }
255
256   Actor self = Self();
257   GetImplementation(mModelPrimitiveContainer[index]).RemovePrimitiveObserver(this);
258
259   Dali::Renderer renderer = GetImplementation(mModelPrimitiveContainer[index]).GetRenderer();
260   if(renderer)
261   {
262     self.RemoveRenderer(renderer);
263   }
264
265   mModelPrimitiveContainer.erase(mModelPrimitiveContainer.begin() + index);
266 }
267
268 Dali::Scene3D::ModelPrimitive ModelNode::GetModelPrimitive(uint32_t index) const
269 {
270   if(index < mModelPrimitiveContainer.size())
271   {
272     return mModelPrimitiveContainer[index];
273   }
274   return Scene3D::ModelPrimitive();
275 }
276
277 Scene3D::ModelNode ModelNode::FindChildModelNodeByName(std::string_view nodeName)
278 {
279   Actor childActor = Self().FindChildByName(nodeName);
280   return Scene3D::ModelNode::DownCast(childActor);
281 }
282
283 void ModelNode::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
284 {
285   mDiffuseTexture       = diffuseTexture;
286   mSpecularTexture      = specularTexture;
287   mIblScaleFactor       = iblScaleFactor;
288   mSpecularMipmapLevels = specularMipmapLevels;
289   for(auto&& primitive : mModelPrimitiveContainer)
290   {
291     GetImplementation(primitive).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
292   }
293 }
294
295 void ModelNode::SetImageBasedLightScaleFactor(float iblScaleFactor)
296 {
297   mIblScaleFactor = iblScaleFactor;
298   for(auto&& primitive : mModelPrimitiveContainer)
299   {
300     GetImplementation(primitive).SetImageBasedLightScaleFactor(iblScaleFactor);
301   }
302 }
303
304 void ModelNode::AddLight(Scene3D::Light light, uint32_t lightIndex)
305 {
306   mLights[lightIndex] = light;
307   for(auto&& primitive : mModelPrimitiveContainer)
308   {
309     GetImplementation(primitive).AddLight(light, lightIndex);
310   }
311 }
312
313 void ModelNode::RemoveLight(uint32_t lightIndex)
314 {
315   for(auto&& primitive : mModelPrimitiveContainer)
316   {
317     GetImplementation(primitive).RemoveLight(lightIndex);
318   }
319   mLights[lightIndex].Reset();
320 }
321
322 void ModelNode::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data, Scene3D::ModelPrimitive primitive)
323 {
324   GetImplementation(primitive).SetBlendShapeData(data);
325 }
326
327 void ModelNode::SetBoneMatrix(const Matrix& inverseMatrix, Scene3D::ModelPrimitive primitive, Scene3D::Loader::Index& boneIndex)
328 {
329   Dali::Scene3D::Loader::Skinning::BoneData boneData;
330   boneData.primitive = primitive;
331   boneData.boneIndex = boneIndex;
332   char propertyNameBuffer[32];
333   snprintf(propertyNameBuffer, sizeof(propertyNameBuffer), "%s[%d]", Dali::Scene3D::Loader::Skinning::BONE_UNIFORM_NAME, boneIndex);
334   boneData.propertyName  = propertyNameBuffer;
335   boneData.inverseMatrix = inverseMatrix;
336   mBoneDataContainer.push_back(std::move(boneData));
337
338   UpdateBoneMatrix(primitive);
339 }
340
341 void ModelNode::OnRendererCreated(Renderer renderer)
342 {
343   Self().AddRenderer(renderer);
344 }
345
346 void ModelNode::UpdateBoneMatrix(Scene3D::ModelPrimitive primitive)
347 {
348   for(auto&& boneData : mBoneDataContainer)
349   {
350     if(boneData.primitive != primitive)
351     {
352       continue;
353     }
354
355     Dali::Renderer renderer = GetImplementation(primitive).GetRenderer();
356     if(!renderer)
357     {
358       continue;
359     }
360
361     if(boneData.constraint)
362     {
363       boneData.constraint.Remove();
364       boneData.constraint.Reset();
365     }
366
367     auto propBoneXform = renderer.GetPropertyIndex(boneData.propertyName);
368     if(propBoneXform == Property::INVALID_INDEX)
369     {
370       propBoneXform = renderer.RegisterProperty(boneData.propertyName, Matrix{false});
371     }
372
373     Matrix inverseMatrix = boneData.inverseMatrix;
374     // Constrain bone matrix to joint transform.
375     boneData.constraint = Constraint::New<Matrix>(renderer, propBoneXform, [inverseMatrix](Matrix& output, const PropertyInputContainer& inputs)
376                                                   { Matrix::Multiply(output, inverseMatrix, inputs[0]->GetMatrix()); });
377
378     Actor joint = Self();
379     boneData.constraint.AddSource(Source{joint, Actor::Property::WORLD_MATRIX});
380     boneData.constraint.ApplyPost();
381     break;
382   }
383 }
384
385 } // namespace Internal
386
387 } // namespace Scene3D
388
389 } // namespace Dali