[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / public-api / loader / blend-shape-details.cpp
1
2 /*
3 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 // FILE HEADER
20 #include <dali-scene3d/public-api/loader/blend-shape-details.h>
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/animation/constraints.h>
24 #include <dali/public-api/object/property.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-scene3d/public-api/loader/resource-bundle.h>
28
29 namespace Dali::Scene3D::Loader
30 {
31 const char* BlendShapes::NUMBER_OF_BLEND_SHAPES("uNumberOfBlendShapes");
32 const char* BlendShapes::UNNORMALIZE_FACTOR("uBlendShapeUnnormalizeFactor");
33 const char* BlendShapes::COMPONENT_SIZE("uBlendShapeComponentSize");
34
35 const char* BlendShapes::COMPONENTS("blendShapeComponents");
36
37 const char* BlendShapes::WEIGHTS_UNIFORM("uBlendShapeWeight");
38
39 void BlendShapes::ConfigureProperties(const BlendShapeData& data, Renderer renderer)
40 {
41   uint32_t index = 0u;
42
43   char        weightNameBuffer[32];
44   char        unnormalizeFactorNameBuffer[64];
45   char* const pWeightName = weightNameBuffer + snprintf(weightNameBuffer, sizeof(weightNameBuffer), "%s", WEIGHTS_UNIFORM);
46   char* const pFactorName = unnormalizeFactorNameBuffer + snprintf(unnormalizeFactorNameBuffer, sizeof(unnormalizeFactorNameBuffer), "%s", UNNORMALIZE_FACTOR);
47   for(const auto& weight : data.weights)
48   {
49     snprintf(pWeightName, sizeof(weightNameBuffer) - (pWeightName - weightNameBuffer), "[%d]", index);
50     std::string weightName{weightNameBuffer};
51     Dali::Actor actor = data.mActor.GetHandle();
52     if(actor)
53     {
54       actor.RegisterProperty(weightName, weight);
55     }
56
57     if(renderer && data.version == Version::VERSION_1_0)
58     {
59       snprintf(pFactorName, sizeof(unnormalizeFactorNameBuffer) - (pFactorName - unnormalizeFactorNameBuffer), "[%d]", index);
60       std::string factorName{unnormalizeFactorNameBuffer};
61       renderer.RegisterProperty(factorName, data.unnormalizeFactors[index]);
62     }
63
64     ++index;
65   }
66
67   if(renderer)
68   {
69     if(Version::VERSION_2_0 == data.version)
70     {
71       renderer.RegisterProperty(UNNORMALIZE_FACTOR, data.unnormalizeFactors[0u]);
72     }
73
74     renderer.RegisterProperty(NUMBER_OF_BLEND_SHAPES, Property::Value(static_cast<int32_t>(index)));
75     renderer.RegisterProperty(COMPONENT_SIZE, Property::Value(static_cast<int32_t>(data.bufferOffset)));
76
77     // Create a read only property to preserve the components of the blend shape.
78     renderer.RegisterProperty(COMPONENTS, data.components, Property::AccessMode::READ_ONLY);
79   }
80 }
81
82 } // namespace Dali::Scene3D::Loader