mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
[godot] Fix Skeleton/Texture loader for runtime loading (#2630)
This commit is contained in:
parent
4f06406e96
commit
15ecabc4a8
@ -37,7 +37,7 @@
|
|||||||
#include "scene/resources/animation.h"
|
#include "scene/resources/animation.h"
|
||||||
|
|
||||||
#ifdef TOOLS_ENABLED
|
#ifdef TOOLS_ENABLED
|
||||||
#include "godot/editor/editor_node.h"
|
#include "editor/editor_node.h"
|
||||||
#include "editor/plugins/animation_player_editor_plugin.h"
|
#include "editor/plugins/animation_player_editor_plugin.h"
|
||||||
#include "editor/plugins/animation_tree_editor_plugin.h"
|
#include "editor/plugins/animation_tree_editor_plugin.h"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -30,6 +30,8 @@
|
|||||||
#include "SpineAtlasResource.h"
|
#include "SpineAtlasResource.h"
|
||||||
#include "SpineRendererObject.h"
|
#include "SpineRendererObject.h"
|
||||||
#include "core/io/json.h"
|
#include "core/io/json.h"
|
||||||
|
#include "core/io/image.h"
|
||||||
|
#include "scene/resources/image_texture.h"
|
||||||
#include "scene/resources/texture.h"
|
#include "scene/resources/texture.h"
|
||||||
#include <spine/TextureLoader.h>
|
#include <spine/TextureLoader.h>
|
||||||
|
|
||||||
@ -49,27 +51,64 @@ public:
|
|||||||
GodotSpineTextureLoader(Array *_textures, Array *_normal_maps, const String &normal_map_prefix, bool is_importing) : textures(_textures), normal_maps(_normal_maps), normal_map_prefix(normal_map_prefix), is_importing(is_importing) {
|
GodotSpineTextureLoader(Array *_textures, Array *_normal_maps, const String &normal_map_prefix, bool is_importing) : textures(_textures), normal_maps(_normal_maps), normal_map_prefix(normal_map_prefix), is_importing(is_importing) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static String fix_path(const String &path) {
|
static bool fix_path(String &path) {
|
||||||
if (path.size() > 5 && path[4] == '/' && path[5] == '/') return path;
|
|
||||||
const String prefix = "res:/";
|
const String prefix = "res:/";
|
||||||
auto i = path.find(prefix);
|
auto i = path.find(prefix);
|
||||||
auto sub_str_pos = i + prefix.size() - 1;
|
if (i == std::string::npos) {
|
||||||
if (sub_str_pos < 0) return path;
|
return false;
|
||||||
auto res = path.substr(sub_str_pos);
|
}
|
||||||
|
|
||||||
|
auto sub_str_pos = i + prefix.size() - 1;
|
||||||
|
auto res = path.substr(sub_str_pos);
|
||||||
if (!EMPTY(res)) {
|
if (!EMPTY(res)) {
|
||||||
if (res[0] != '/') {
|
if (res[0] != '/') {
|
||||||
return prefix + "/" + res;
|
path = prefix + "/" + res;
|
||||||
} else {
|
} else {
|
||||||
return prefix + res;
|
path = prefix + res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return path;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if VERSION_MAJOR > 3
|
||||||
|
Ref<Texture2D> get_texture_from_image(const String &path, bool is_resource) {
|
||||||
|
Error error = OK;
|
||||||
|
if (is_resource) {
|
||||||
|
return ResourceLoader::load(path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &error);
|
||||||
|
} else {
|
||||||
|
Ref<Image> img;
|
||||||
|
img.instantiate();
|
||||||
|
img = img->load_from_file(path);
|
||||||
|
return ImageTexture::create_from_image(img);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Ref<Texture> get_texture_from_image(const String &path, bool is_resource) {
|
||||||
|
Error error = OK;
|
||||||
|
if (is_resource) {
|
||||||
|
return ResourceLoader::load(path, "", false, &error);
|
||||||
|
} else {
|
||||||
|
Vector<uint8_t> buf = FileAccess::get_file_as_array(path, &error);
|
||||||
|
if (error == OK) {
|
||||||
|
Ref<Image> img;
|
||||||
|
img.instantiate();
|
||||||
|
String filename = path.get_filename().to_lower();
|
||||||
|
if (filename.ends_with(".png")) {
|
||||||
|
img->load_png_from_buffer(buf);
|
||||||
|
} else if (filename_lower.ends_with(".jpg")) {
|
||||||
|
img->load_jpg_from_buffer(buf);
|
||||||
|
}
|
||||||
|
return ImageTexture::create_from_image(img);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ref<Texture>();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void load(spine::AtlasPage &page, const spine::String &path) override {
|
void load(spine::AtlasPage &page, const spine::String &path) override {
|
||||||
Error error = OK;
|
Error error = OK;
|
||||||
auto fixed_path = fix_path(String(path.buffer()));
|
String fixed_path = String(path.buffer());
|
||||||
|
bool is_resource = fix_path(fixed_path);
|
||||||
|
|
||||||
|
|
||||||
#ifdef VERSION_MAJOR > 4
|
#ifdef VERSION_MAJOR > 4
|
||||||
@ -86,9 +125,9 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if VERSION_MAJOR > 3
|
#if VERSION_MAJOR > 3
|
||||||
Ref<Texture2D> texture = ResourceLoader::load(fixed_path, "", ResourceFormatLoader::CACHE_MODE_REUSE, &error);
|
Ref<Texture2D> texture = get_texture_from_image(fixed_path, is_resource);
|
||||||
#else
|
#else
|
||||||
Ref<Texture> texture = ResourceLoader::load(fixed_path, "", false, &error);
|
Ref<Texture> texture = get_texture_from_image(fixed_path, is_resource);
|
||||||
#endif
|
#endif
|
||||||
if (error != OK || !texture.is_valid()) {
|
if (error != OK || !texture.is_valid()) {
|
||||||
ERR_PRINT(vformat("Can't load texture: \"%s\"", String(path.buffer())));
|
ERR_PRINT(vformat("Can't load texture: \"%s\"", String(path.buffer())));
|
||||||
@ -106,7 +145,7 @@ public:
|
|||||||
|
|
||||||
String new_path = vformat("%s/%s_%s", fixed_path.get_base_dir(), normal_map_prefix, fixed_path.get_file());
|
String new_path = vformat("%s/%s_%s", fixed_path.get_base_dir(), normal_map_prefix, fixed_path.get_file());
|
||||||
if (ResourceLoader::exists(new_path)) {
|
if (ResourceLoader::exists(new_path)) {
|
||||||
Ref<Texture> normal_map = ResourceLoader::load(new_path);
|
Ref<Texture> normal_map = get_texture_from_image(new_path, is_resource);
|
||||||
normal_maps->append(normal_map);
|
normal_maps->append(normal_map);
|
||||||
renderer_object->normal_map = normal_map;
|
renderer_object->normal_map = normal_map;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -89,6 +89,7 @@ static char *readString(BinaryInput *input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SpineSkeletonFileResource::_bind_methods() {
|
void SpineSkeletonFileResource::_bind_methods() {
|
||||||
|
ClassDB::bind_method(D_METHOD("load_from_file", "path"), &SpineSkeletonFileResource::load_from_file);
|
||||||
ADD_SIGNAL(MethodInfo("skeleton_file_changed"));
|
ADD_SIGNAL(MethodInfo("skeleton_file_changed"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,12 @@
|
|||||||
<tutorials>
|
<tutorials>
|
||||||
</tutorials>
|
</tutorials>
|
||||||
<methods>
|
<methods>
|
||||||
|
<method name="load_from_file">
|
||||||
|
<return type="int" enum="Error" />
|
||||||
|
<argument index="0" name="path" type="String" />
|
||||||
|
<description>
|
||||||
|
</description>
|
||||||
|
</method>
|
||||||
</methods>
|
</methods>
|
||||||
<constants>
|
<constants>
|
||||||
</constants>
|
</constants>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user