Parse gltf mesh extra and extensions + Get BlendShape index by name
[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/light/light-impl.h>
28 #include <dali-scene3d/internal/model-components/model-primitive-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::RetrieveBlendShapeNames(std::vector<std::string>& blendShapeNames) const
284 {
285   blendShapeNames.reserve(blendShapeNames.size() + mBlendShapeIndexMap.size());
286   for(const auto& iter : mBlendShapeIndexMap)
287   {
288     blendShapeNames.push_back(iter.first);
289   }
290 }
291
292 Loader::BlendShapes::Index ModelNode::GetBlendShapeIndexByName(std::string_view blendShapeName) const
293 {
294   auto iter = mBlendShapeIndexMap.find(std::string(blendShapeName));
295   if(iter != mBlendShapeIndexMap.end())
296   {
297     return iter->second;
298   }
299   return Loader::BlendShapes::INVALID_INDEX;
300 }
301
302 void ModelNode::SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float iblScaleFactor, uint32_t specularMipmapLevels)
303 {
304   mDiffuseTexture       = diffuseTexture;
305   mSpecularTexture      = specularTexture;
306   mIblScaleFactor       = iblScaleFactor;
307   mSpecularMipmapLevels = specularMipmapLevels;
308   for(auto&& primitive : mModelPrimitiveContainer)
309   {
310     GetImplementation(primitive).SetImageBasedLightTexture(diffuseTexture, specularTexture, iblScaleFactor, specularMipmapLevels);
311   }
312 }
313
314 void ModelNode::SetImageBasedLightScaleFactor(float iblScaleFactor)
315 {
316   mIblScaleFactor = iblScaleFactor;
317   for(auto&& primitive : mModelPrimitiveContainer)
318   {
319     GetImplementation(primitive).SetImageBasedLightScaleFactor(iblScaleFactor);
320   }
321 }
322
323 void ModelNode::AddLight(Scene3D::Light light, uint32_t lightIndex)
324 {
325   mLights[lightIndex] = light;
326   for(auto&& primitive : mModelPrimitiveContainer)
327   {
328     GetImplementation(primitive).AddLight(light, lightIndex);
329   }
330 }
331
332 void ModelNode::RemoveLight(uint32_t lightIndex)
333 {
334   for(auto&& primitive : mModelPrimitiveContainer)
335   {
336     GetImplementation(primitive).RemoveLight(lightIndex);
337   }
338   mLights[lightIndex].Reset();
339 }
340
341 void ModelNode::SetBlendShapeData(Scene3D::Loader::BlendShapes::BlendShapeData& data, Scene3D::ModelPrimitive primitive)
342 {
343   // Update mBlendShapeIndexMap
344   mBlendShapeIndexMap.clear();
345   const auto blendShapeCount = data.names.size();
346   for(Loader::BlendShapes::Index index = 0u; index < blendShapeCount; ++index)
347   {
348     auto& name = data.names[index];
349     if(!name.empty())
350     {
351       mBlendShapeIndexMap[name] = index;
352     }
353   }
354
355   GetImplementation(primitive).SetBlendShapeData(data);
356 }
357
358 void ModelNode::SetBoneMatrix(const Matrix& inverseMatrix, Scene3D::ModelPrimitive primitive, Scene3D::Loader::Index& boneIndex)
359 {
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));
368
369   UpdateBoneMatrix(primitive);
370 }
371
372 void ModelNode::OnRendererCreated(Renderer renderer)
373 {
374   Self().AddRenderer(renderer);
375 }
376
377 void ModelNode::UpdateBoneMatrix(Scene3D::ModelPrimitive primitive)
378 {
379   for(auto&& boneData : mBoneDataContainer)
380   {
381     if(boneData.primitive != primitive)
382     {
383       continue;
384     }
385
386     Dali::Renderer renderer = GetImplementation(primitive).GetRenderer();
387     if(!renderer)
388     {
389       continue;
390     }
391
392     if(boneData.constraint)
393     {
394       boneData.constraint.Remove();
395       boneData.constraint.Reset();
396     }
397
398     auto propBoneXform = renderer.GetPropertyIndex(boneData.propertyName);
399     if(propBoneXform == Property::INVALID_INDEX)
400     {
401       propBoneXform = renderer.RegisterProperty(boneData.propertyName, Matrix{false});
402     }
403
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()); });
407
408     Actor joint = Self();
409     boneData.constraint.AddSource(Source{joint, Actor::Property::WORLD_MATRIX});
410     boneData.constraint.ApplyPost();
411     break;
412   }
413 }
414
415 } // namespace Internal
416
417 } // namespace Scene3D
418
419 } // namespace Dali