lottie: support drop shadow effect

issue: https://github.com/thorvg/thorvg/issues/2153
issue: https://github.com/thorvg/thorvg/issues/2718
This commit is contained in:
Hermet Park 2024-10-30 12:33:10 +09:00
parent 2324f6e75d
commit 13aa26109f
5 changed files with 60 additions and 2 deletions

View file

@ -1297,14 +1297,23 @@ bool LottieBuilder::updateMatte(LottieComposition* comp, float frameNo, Scene* s
void LottieBuilder::updateEffect(LottieLayer* layer, float frameNo)
{
constexpr int QUALITY = 25;
constexpr float BLUR_TO_SIGMA = 0.3f;
if (layer->effects.count == 0) return;
for (auto ef = layer->effects.begin(); ef < layer->effects.end(); ++ef) {
if (!(*ef)->enable) continue;
switch ((*ef)->type) {
case LottieEffect::DropShadow: {
auto effect = static_cast<LottieDropShadow*>(*ef);
auto color = effect->color(frameNo);
layer->scene->push(SceneEffect::DropShadow, color.rgb[0], color.rgb[1], color.rgb[2], effect->opacity(frameNo), effect->angle(frameNo), effect->distance(frameNo), effect->blurness(frameNo), QUALITY);
break;
}
case LottieEffect::GaussianBlur: {
auto effect = static_cast<LottieGaussianBlur*>(*ef);
layer->scene->push(SceneEffect::GaussianBlur, sqrt(effect->blurness(frameNo)), effect->direction(frameNo) - 1, effect->wrap(frameNo), 25);
layer->scene->push(SceneEffect::GaussianBlur, effect->blurness(frameNo) * BLUR_TO_SIGMA, effect->direction(frameNo) - 1, effect->wrap(frameNo), QUALITY);
break;
}
default: break;

View file

@ -81,7 +81,8 @@ struct LottieEffect
{
enum Type : uint8_t
{
GaussianBlur = 0,
DropShadow = 0,
GaussianBlur = 1,
};
virtual ~LottieEffect() {}
@ -91,6 +92,21 @@ struct LottieEffect
};
struct LottieDropShadow : LottieEffect
{
LottieColor color;
LottieOpacity opacity = 0;
LottieAngle angle = 0.0f;
LottieSlider distance = 0.0f;
LottieSlider blurness = 0.0f;
LottieDropShadow()
{
type = DropShadow;
}
};
struct LottieGaussianBlur : LottieEffect
{
LottieSlider blurness = 0.0f;

View file

@ -60,6 +60,7 @@ static unsigned long _int2str(int num)
LottieEffect* LottieParser::getEffect(int type)
{
switch (type) {
case 25: return new LottieDropShadow;
case 29: return new LottieGaussianBlur;
default: return nullptr;
}
@ -1296,6 +1297,32 @@ void LottieParser::parseGaussianBlur(LottieGaussianBlur* effect)
}
void LottieParser::parseDropShadow(LottieDropShadow* effect)
{
int idx = 0; //color -> opacity -> angle -> distance -> blur
enterArray();
while (nextArrayValue()) {
enterObject();
while (auto key = nextObjectKey()) {
if (KEY_AS("v")) {
enterObject();
while (auto key = nextObjectKey()) {
if (KEY_AS("k")) {
if (idx == 0) parsePropertyInternal(effect->color);
else if (idx == 1) parsePropertyInternal(effect->opacity);
else if (idx == 2) parsePropertyInternal(effect->angle);
else if (idx == 3) parsePropertyInternal(effect->distance);
else if (idx == 4) parsePropertyInternal(effect->blurness);
else skip(key);
++idx;
} else skip(key);
}
} else skip(key);
}
}
}
void LottieParser::parseEffect(LottieEffect* effect)
{
switch (effect->type) {
@ -1303,6 +1330,10 @@ void LottieParser::parseEffect(LottieEffect* effect)
parseGaussianBlur(static_cast<LottieGaussianBlur*>(effect));
break;
}
case LottieEffect::DropShadow: {
parseDropShadow(static_cast<LottieDropShadow*>(effect));
break;
}
default: break;
}
}

View file

@ -99,6 +99,7 @@ private:
LottieMarker* parseMarker();
void parseGaussianBlur(LottieGaussianBlur* effect);
void parseDropShadow(LottieDropShadow* effect);
bool parseDirection(LottieShape* shape, const char* key);
bool parseCommon(LottieObject* obj, const char* key);

View file

@ -905,6 +905,7 @@ using LottieFloat = LottieGenericProperty<float>;
using LottieOpacity = LottieGenericProperty<uint8_t>;
using LottieColor = LottieGenericProperty<RGB24>;
using LottieSlider = LottieFloat;
using LottieAngle = LottieFloat;
using LottieCheckbox = LottieGenericProperty<int8_t>;
#endif //_TVG_LOTTIE_PROPERTY_H_