Skip to content

Commit

Permalink
svg: add lottie resource loader importer using thorvg
Browse files Browse the repository at this point in the history
add lottie importer to import lottie as CompressedTexture2D, and support drop it as AnimatedSprite2D in editor
  • Loading branch information
beicause committed Nov 4, 2024
1 parent e25776e commit afad109
Show file tree
Hide file tree
Showing 126 changed files with 30,048 additions and 1,338 deletions.
20 changes: 20 additions & 0 deletions editor/plugins/canvas_item_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "core/config/project_settings.h"
#include "core/input/input.h"
#include "core/io/resource_importer.h"
#include "core/os/keyboard.h"
#include "editor/debugger/editor_debugger_node.h"
#include "editor/editor_main_screen.h"
Expand All @@ -47,6 +48,7 @@
#include "editor/scene_tree_dock.h"
#include "editor/themes/editor_scale.h"
#include "editor/themes/editor_theme_manager.h"
#include "scene/2d/animated_sprite_2d.h"
#include "scene/2d/audio_stream_player_2d.h"
#include "scene/2d/polygon_2d.h"
#include "scene/2d/skeleton_2d.h"
Expand All @@ -61,6 +63,7 @@
#include "scene/gui/view_panner.h"
#include "scene/main/canvas_layer.h"
#include "scene/main/window.h"
#include "scene/resources/atlas_texture.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/style_box_texture.h"

Expand Down Expand Up @@ -5895,6 +5898,22 @@ void CanvasItemEditorViewport::_create_texture_node(Node *p_parent, Node *p_chil
Vector2(0, texture_size.height)
};
undo_redo->add_do_property(p_child, "polygon", list);
} else if (Object::cast_to<AnimatedSprite2D>(p_child)) {
Dictionary meta = ResourceFormatImporter::get_singleton()->get_resource_metadata(p_path);
Ref<SpriteFrames> frames = memnew(SpriteFrames);
frames->set_animation_speed(SceneStringName(default_), meta.get("fps", 5));
Size2i sprite_size = meta["sprite_size"];
for (int i = 0; i < (int)meta["frame_count"]; i++) {
int x = i % (int)meta["columns"] * sprite_size.width;
int y = i / (int)meta["columns"] * sprite_size.height;
Ref<AtlasTexture> atlas_texture;
atlas_texture.instantiate();
atlas_texture->set_atlas(texture);
atlas_texture->set_region(Rect2(x, y, sprite_size.width, sprite_size.height));
frames->add_frame(SceneStringName(default_), atlas_texture);
}
undo_redo->add_do_property(p_child, "autoplay", SceneStringName(default_));
undo_redo->add_do_property(p_child, "frames", frames);
}

// Compute the global position
Expand Down Expand Up @@ -6300,6 +6319,7 @@ CanvasItemEditorViewport::CanvasItemEditorViewport(CanvasItemEditor *p_canvas_it
texture_node_types.push_back("GPUParticles2D");
texture_node_types.push_back("Polygon2D");
texture_node_types.push_back("TouchScreenButton");
texture_node_types.push_back("AnimatedSprite2D");
// Control
texture_node_types.push_back("TextureRect");
texture_node_types.push_back("TextureButton");
Expand Down
27 changes: 25 additions & 2 deletions modules/svg/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ thirdparty_dir = "#thirdparty/thorvg/"
thirdparty_sources = [
# common
"src/common/tvgCompressor.cpp",
"src/common/tvgLines.cpp",
"src/common/tvgMath.cpp",
"src/common/tvgStr.cpp",
# SVG parser
Expand Down Expand Up @@ -52,13 +51,28 @@ thirdparty_sources = [
"src/renderer/sw_engine/tvgSwImage.cpp",
"src/renderer/sw_engine/tvgSwMath.cpp",
"src/renderer/sw_engine/tvgSwMemPool.cpp",
"src/renderer/sw_engine/tvgSwPostEffect.cpp",
"src/renderer/sw_engine/tvgSwRaster.cpp",
"src/renderer/sw_engine/tvgSwRenderer.cpp",
"src/renderer/sw_engine/tvgSwRle.cpp",
"src/renderer/sw_engine/tvgSwShape.cpp",
"src/renderer/sw_engine/tvgSwStroke.cpp",
]

if env.editor_build:
thirdparty_sources += [
"src/renderer/tvgAnimation.cpp",
# Lottie loader
"src/loaders/lottie/tvgLottieAnimation.cpp",
"src/loaders/lottie/tvgLottieBuilder.cpp",
"src/loaders/lottie/tvgLottieInterpolator.cpp",
"src/loaders/lottie/tvgLottieLoader.cpp",
"src/loaders/lottie/tvgLottieModel.cpp",
"src/loaders/lottie/tvgLottieModifier.cpp",
"src/loaders/lottie/tvgLottieParser.cpp",
"src/loaders/lottie/tvgLottieParserHandler.cpp",
]

if env["module_webp_enabled"]:
thirdparty_sources += ["src/loaders/external_webp/tvgWebpLoader.cpp"]
env_svg.Append(CPPDEFINES=["THORVG_WEBP_LOADER_SUPPORT"])
Expand All @@ -70,6 +84,9 @@ env_svg.Prepend(CPPPATH=[thirdparty_dir + "inc"])
# Enable ThorVG static object linking.
env_svg.Append(CPPDEFINES=["TVG_STATIC"])

if env.editor_build:
env_svg.Append(CPPDEFINES=["THORVG_LOTTIE_LOADER_SUPPORT", "LOTTIE_ENABLED"])

env_thirdparty = env_svg.Clone()
env_thirdparty.disable_warnings()
env_thirdparty.Prepend(
Expand All @@ -83,6 +100,9 @@ env_thirdparty.Prepend(
thirdparty_dir + "src/loaders/jpg",
]
)

if env.editor_build:
env_thirdparty.Prepend(CPPPATH=[thirdparty_dir + "src/loaders/lottie"])
if env["builtin_libpng"]:
env_thirdparty.Prepend(CPPPATH=["#thirdparty/libpng"])
if env["module_webp_enabled"]:
Expand All @@ -97,7 +117,10 @@ env.modules_sources += thirdparty_obj

module_obj = []

env_svg.add_source_files(module_obj, "*.cpp")
env_svg.add_source_files(module_obj, ["register_types.cpp", "image_loader_svg.cpp"])
if env.editor_build:
env_svg.add_source_files(module_obj, "editor/*.cpp")

env.modules_sources += module_obj

# Needed to force rebuilding the module files when the thirdparty library is updated.
Expand Down
10 changes: 10 additions & 0 deletions modules/svg/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,13 @@ def can_build(env, platform):

def configure(env):
pass


def get_doc_classes():
return [
"ResourceImporterLottie",
]


def get_doc_path():
return "doc_classes"
66 changes: 66 additions & 0 deletions modules/svg/doc_classes/ResourceImporterLottie.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="ResourceImporterLottie" inherits="ResourceImporter" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
Import a Lottie animation as a sprite sheet.
</brief_description>
<description>
This imports a Lottie animation to a [CompressedTexture2D]. See also [ResourceImporterTexture].
[b]Note:[/b] Both JSON and dotLottie (extra resources are ignored) are supported. Only files with the [code].lottie[/code] extension are recognized. You will have to rename the JSON file extension to [code].lottie[/code] to import it.
</description>
<tutorials>
</tutorials>
<members>
<member name="compress/channel_pack" type="int" setter="" getter="" default="0">
</member>
<member name="compress/hdr_compression" type="int" setter="" getter="" default="1">
</member>
<member name="compress/high_quality" type="bool" setter="" getter="" default="false">
</member>
<member name="compress/lossy_quality" type="float" setter="" getter="" default="0.7">
</member>
<member name="compress/mode" type="int" setter="" getter="" default="0">
</member>
<member name="compress/normal_map" type="int" setter="" getter="" default="0">
</member>
<member name="detect_3d/compress_to" type="int" setter="" getter="" default="1">
</member>
<member name="editor/convert_colors_with_editor_theme" type="bool" setter="" getter="" default="false">
</member>
<member name="editor/scale_with_editor_scale" type="bool" setter="" getter="" default="false">
</member>
<member name="lottie/begin" type="float" setter="" getter="" default="0">
</member>
<member name="lottie/columns" type="int" setter="" getter="" default="0">
</member>
<member name="lottie/end" type="float" setter="" getter="" default="1">
</member>
<member name="lottie/fps" type="float" setter="" getter="" default="30">
</member>
<member name="lottie/scale" type="float" setter="" getter="" default="1">
</member>
<member name="lottie/size_limit" type="int" setter="" getter="" default="2048">
</member>
<member name="mipmaps/generate" type="bool" setter="" getter="" default="false">
</member>
<member name="mipmaps/limit" type="int" setter="" getter="" default="-1">
</member>
<member name="process/fix_alpha_border" type="bool" setter="" getter="" default="true">
</member>
<member name="process/hdr_as_srgb" type="bool" setter="" getter="" default="false">
</member>
<member name="process/hdr_clamp_exposure" type="bool" setter="" getter="" default="false">
</member>
<member name="process/normal_map_invert_y" type="bool" setter="" getter="" default="false">
</member>
<member name="process/premult_alpha" type="bool" setter="" getter="" default="false">
</member>
<member name="process/size_limit" type="int" setter="" getter="" default="0">
</member>
<member name="roughness/mode" type="int" setter="" getter="" default="0">
</member>
<member name="roughness/src_normal" type="String" setter="" getter="" default="&quot;&quot;">
</member>
<member name="svg/scale" type="float" setter="" getter="" default="1.0">
</member>
</members>
</class>
Loading

0 comments on commit afad109

Please sign in to comment.