Files
armorpaint/paint/plugins/plugins.c
T

183 lines
5.4 KiB
C
Raw Normal View History

2024-08-22 23:07:22 +02:00
2026-03-23 23:20:02 +01:00
#ifdef WITH_PLUGINS
2026-04-16 20:42:30 +02:00
#include "engine.h"
2025-09-02 12:05:25 +02:00
#include "iron_array.h"
#include "iron_map.h"
2026-07-18 12:09:44 +02:00
#include "iron_obj.h"
2025-09-02 12:05:25 +02:00
#include "iron_ui.h"
2026-07-18 12:09:44 +02:00
#include <stdlib.h>
2025-07-20 00:14:24 +02:00
2024-08-24 11:17:02 +02:00
void *io_svg_parse(char *buf);
2025-09-25 13:58:12 +02:00
void *io_exr_parse(char *buf, size_t len);
2026-04-16 20:42:30 +02:00
void *io_psd_parse(uint8_t *buf, size_t len, const char *filename);
2026-03-23 23:20:02 +01:00
void *io_tiff_parse(uint8_t *buf, size_t len);
void *io_gltf_parse(char *buf, size_t size, const char *path);
void *io_gltf_parse_skinned(char *buf, size_t size, const char *path, int frame);
2024-08-24 11:17:02 +02:00
void *io_fbx_parse(char *buf, size_t size);
void *io_fbx_parse_skinned(char *buf, size_t size, int frame);
2026-04-16 20:42:30 +02:00
void proc_uv_unwrap(void *mesh);
typedef struct asset {
i32 id;
char *name;
char *file;
} asset_t;
2025-09-02 12:05:25 +02:00
2026-03-16 14:28:30 +01:00
extern any_map_t *import_mesh_importers;
2026-03-15 10:47:53 +01:00
extern string_array_t *_path_mesh_formats;
string_array_t *path_mesh_formats(void);
2026-03-16 14:28:30 +01:00
extern any_map_t *import_texture_importers;
2026-03-15 10:47:53 +01:00
extern string_array_t *_path_texture_formats;
string_array_t *path_texture_formats(void);
2026-04-16 20:42:30 +02:00
extern any_map_t *util_mesh_unwrappers;
2026-07-05 11:30:04 +02:00
extern any_map_t *data_cached_textures;
2026-04-16 20:42:30 +02:00
void import_texture_run(char *path, bool hdr_as_envmap);
2026-06-05 20:34:10 +02:00
any_array_t *project_get_assets(void);
2026-04-16 20:42:30 +02:00
void tab_textures_delete_texture(asset_t *asset);
2025-09-02 12:05:25 +02:00
int plugins_skinning_frame = -1;
int plugins_split_by = 0;
2026-04-16 20:42:30 +02:00
void io_psd_import_layer(char *file_name, char *layer_name, void *tex) {
char *path = string("%s.%s.png", file_name, layer_name);
2026-07-05 11:30:04 +02:00
any_map_set(data_cached_textures, path, tex);
2026-04-16 20:42:30 +02:00
import_texture_run(path, false);
}
2026-03-23 23:20:02 +01:00
static void *import_exr(char *path) {
buffer_t *b = data_get_blob(path);
data_delete_blob(path);
return io_exr_parse((char *)b->buffer, b->length);
2025-09-02 12:05:25 +02:00
}
2026-04-16 20:42:30 +02:00
static void *import_psd(char *path) {
char *filename = substring(path, string_last_index_of(path, PATH_SEP) + 1, string_length(path));
// Delete existing layers so they can be re-imported
char *prefix = string("%s.", filename);
2026-06-05 20:34:10 +02:00
any_array_t *project_assets = project_get_assets();
2026-04-16 20:42:30 +02:00
for (int i = project_assets->length - 1; i >= 0; --i) {
asset_t *a = project_assets->buffer[i];
if (starts_with(a->name, prefix)) {
tab_textures_delete_texture(a);
}
}
buffer_t *b = data_get_blob(path);
data_delete_blob(path);
return io_psd_parse((uint8_t *)b->buffer, b->length, filename);
}
2026-03-23 23:20:02 +01:00
static void *import_tiff(char *path) {
buffer_t *b = data_get_blob(path);
data_delete_blob(path);
return io_tiff_parse((uint8_t *)b->buffer, b->length);
}
2024-08-22 23:07:22 +02:00
2026-03-23 23:20:02 +01:00
static void *import_svg(char *path) {
buffer_t *b = data_get_blob(path);
data_delete_blob(path);
return io_svg_parse((char *)b->buffer);
}
2024-08-22 23:07:22 +02:00
2026-07-18 12:09:44 +02:00
static buffer_t *plugins_skin_blob = NULL;
void plugins_skin_data_clear() {
gc_unroot(plugins_skin_blob);
plugins_skin_blob = NULL;
}
static void plugins_skin_data_set(buffer_t *b) {
plugins_skin_data_clear();
plugins_skin_blob = b;
gc_root(plugins_skin_blob);
}
bool plugins_skin_data_exists() {
return plugins_skin_blob != NULL;
}
static void plugins_free_raw_mesh(raw_mesh_t *raw) {
i16_array_t *arrays[] = {raw->posa, raw->nora, raw->texa, raw->texa1, raw->cola};
for (int i = 0; i < 5; ++i) {
if (arrays[i] != NULL) {
free(arrays[i]->buffer);
free(arrays[i]);
}
}
if (raw->inda != NULL) {
free(raw->inda->buffer);
free(raw->inda);
}
free(raw->name);
free(raw);
}
bool plugins_skin_data_apply(int frame, i16_array_t *posa, i16_array_t *nora, float *scale_pos) {
if (plugins_skin_blob == NULL) {
return false;
}
raw_mesh_t *raw = io_gltf_parse_skinned((char *)plugins_skin_blob->buffer, plugins_skin_blob->length, NULL, frame);
if (raw == NULL) {
return false;
}
memcpy(posa->buffer, raw->posa->buffer, posa->length * sizeof(int16_t));
memcpy(nora->buffer, raw->nora->buffer, nora->length * sizeof(int16_t));
*scale_pos = raw->scale_pos;
plugins_free_raw_mesh(raw);
return true;
}
2026-03-23 23:20:02 +01:00
static void *import_gltf_glb(char *path) {
buffer_t *b = data_get_blob(path);
data_delete_blob(path);
if (plugins_skinning_frame == -1) {
return io_gltf_parse((char *)b->buffer, b->length, path);
}
else {
2026-07-18 12:09:44 +02:00
plugins_skin_data_set(b);
return io_gltf_parse_skinned((char *)b->buffer, b->length, path, plugins_skinning_frame);
}
2026-03-23 23:20:02 +01:00
}
2025-09-02 12:05:25 +02:00
2026-03-23 23:20:02 +01:00
static void *import_fbx(char *path) {
buffer_t *b = data_get_blob(path);
data_delete_blob(path);
if (plugins_skinning_frame == -1) {
return io_fbx_parse((char *)b->buffer, b->length);
}
else {
return io_fbx_parse_skinned((char *)b->buffer, b->length, plugins_skinning_frame);
}
2026-03-23 23:20:02 +01:00
}
2025-09-02 12:05:25 +02:00
2026-03-23 23:20:02 +01:00
void plugins_init() {
path_texture_formats(); // Init array
any_map_set(import_texture_importers, "exr", import_exr);
any_array_push(_path_texture_formats, "exr");
2026-04-16 20:42:30 +02:00
any_map_set(import_texture_importers, "psd", import_psd);
any_array_push(_path_texture_formats, "psd");
2026-03-23 23:20:02 +01:00
any_map_set(import_texture_importers, "svg", import_svg);
any_array_push(_path_texture_formats, "svg");
any_map_set(import_texture_importers, "tiff", import_tiff);
any_array_push(_path_texture_formats, "tiff");
any_map_set(import_texture_importers, "tif", import_tiff);
any_array_push(_path_texture_formats, "tif");
2025-09-02 12:05:25 +02:00
2026-03-23 23:20:02 +01:00
path_mesh_formats(); // Init array
any_map_set(import_mesh_importers, "gltf", import_gltf_glb);
any_array_push(_path_mesh_formats, "gltf");
any_map_set(import_mesh_importers, "glb", import_gltf_glb);
any_array_push(_path_mesh_formats, "glb");
any_map_set(import_mesh_importers, "fbx", import_fbx);
any_array_push(_path_mesh_formats, "fbx");
2025-09-02 12:05:25 +02:00
2026-03-23 23:20:02 +01:00
any_map_set(util_mesh_unwrappers, "uv_unwrap", proc_uv_unwrap);
2024-08-22 23:07:22 +02:00
}
2026-03-23 23:20:02 +01:00
#endif