diff options
Diffstat (limited to 'source/blender/blenkernel/intern/pointcloud.c')
-rw-r--r-- | source/blender/blenkernel/intern/pointcloud.c | 99 |
1 files changed, 90 insertions, 9 deletions
diff --git a/source/blender/blenkernel/intern/pointcloud.c b/source/blender/blenkernel/intern/pointcloud.c index fb10c9f03e3..6ec305a971c 100644 --- a/source/blender/blenkernel/intern/pointcloud.c +++ b/source/blender/blenkernel/intern/pointcloud.c @@ -47,10 +47,15 @@ #include "DEG_depsgraph_query.h" +#include "BLO_read_write.h" + /* PointCloud datablock */ static void pointcloud_random(PointCloud *pointcloud); +static const char *POINTCLOUD_ATTR_POSITION = "Position"; +static const char *POINTCLOUD_ATTR_RADIUS = "Radius"; + static void pointcloud_init_data(ID *id) { PointCloud *pointcloud = (PointCloud *)id; @@ -59,8 +64,18 @@ static void pointcloud_init_data(ID *id) MEMCPY_STRUCT_AFTER(pointcloud, DNA_struct_default_get(PointCloud), id); CustomData_reset(&pointcloud->pdata); - CustomData_add_layer(&pointcloud->pdata, CD_LOCATION, CD_CALLOC, NULL, pointcloud->totpoint); - CustomData_add_layer(&pointcloud->pdata, CD_RADIUS, CD_CALLOC, NULL, pointcloud->totpoint); + CustomData_add_layer_named(&pointcloud->pdata, + CD_PROP_FLOAT3, + CD_CALLOC, + NULL, + pointcloud->totpoint, + POINTCLOUD_ATTR_POSITION); + CustomData_add_layer_named(&pointcloud->pdata, + CD_PROP_FLOAT, + CD_CALLOC, + NULL, + pointcloud->totpoint, + POINTCLOUD_ATTR_RADIUS); BKE_pointcloud_update_customdata_pointers(pointcloud); pointcloud_random(pointcloud); @@ -98,6 +113,64 @@ static void pointcloud_foreach_id(ID *id, LibraryForeachIDData *data) } } +static void pointcloud_blend_write(BlendWriter *writer, ID *id, const void *id_address) +{ + PointCloud *pointcloud = (PointCloud *)id; + if (pointcloud->id.us > 0 || BLO_write_is_undo(writer)) { + CustomDataLayer *players = NULL, players_buff[CD_TEMP_CHUNK_SIZE]; + CustomData_blend_write_prepare( + &pointcloud->pdata, &players, players_buff, ARRAY_SIZE(players_buff)); + + /* Write LibData */ + BLO_write_id_struct(writer, PointCloud, id_address, &pointcloud->id); + BKE_id_blend_write(writer, &pointcloud->id); + + /* Direct data */ + CustomData_blend_write( + writer, &pointcloud->pdata, players, pointcloud->totpoint, CD_MASK_ALL, &pointcloud->id); + + BLO_write_pointer_array(writer, pointcloud->totcol, pointcloud->mat); + if (pointcloud->adt) { + BKE_animdata_blend_write(writer, pointcloud->adt); + } + + /* Remove temporary data. */ + if (players && players != players_buff) { + MEM_freeN(players); + } + } +} + +static void pointcloud_blend_read_data(BlendDataReader *reader, ID *id) +{ + PointCloud *pointcloud = (PointCloud *)id; + BLO_read_data_address(reader, &pointcloud->adt); + BKE_animdata_blend_read_data(reader, pointcloud->adt); + + /* Geometry */ + CustomData_blend_read(reader, &pointcloud->pdata, pointcloud->totpoint); + BKE_pointcloud_update_customdata_pointers(pointcloud); + + /* Materials */ + BLO_read_pointer_array(reader, (void **)&pointcloud->mat); +} + +static void pointcloud_blend_read_lib(BlendLibReader *reader, ID *id) +{ + PointCloud *pointcloud = (PointCloud *)id; + for (int a = 0; a < pointcloud->totcol; a++) { + BLO_read_id_address(reader, pointcloud->id.lib, &pointcloud->mat[a]); + } +} + +static void pointcloud_blend_read_expand(BlendExpander *expander, ID *id) +{ + PointCloud *pointcloud = (PointCloud *)id; + for (int a = 0; a < pointcloud->totcol; a++) { + BLO_expand(expander, pointcloud->mat[a]); + } +} + IDTypeInfo IDType_ID_PT = { .id_code = ID_PT, .id_filter = FILTER_ID_PT, @@ -115,10 +188,10 @@ IDTypeInfo IDType_ID_PT = { .foreach_id = pointcloud_foreach_id, .foreach_cache = NULL, - .blend_write = NULL, - .blend_read_data = NULL, - .blend_read_lib = NULL, - .blend_read_expand = NULL, + .blend_write = pointcloud_blend_write, + .blend_read_data = pointcloud_blend_read_data, + .blend_read_lib = pointcloud_blend_read_lib, + .blend_read_expand = pointcloud_blend_read_expand, }; static void pointcloud_random(PointCloud *pointcloud) @@ -189,8 +262,15 @@ BoundBox *BKE_pointcloud_boundbox_get(Object *ob) void BKE_pointcloud_update_customdata_pointers(PointCloud *pointcloud) { - pointcloud->co = CustomData_get_layer(&pointcloud->pdata, CD_LOCATION); - pointcloud->radius = CustomData_get_layer(&pointcloud->pdata, CD_RADIUS); + pointcloud->co = CustomData_get_layer_named( + &pointcloud->pdata, CD_PROP_FLOAT3, POINTCLOUD_ATTR_POSITION); + pointcloud->radius = CustomData_get_layer_named( + &pointcloud->pdata, CD_PROP_FLOAT, POINTCLOUD_ATTR_RADIUS); +} + +bool BKE_pointcloud_customdata_required(PointCloud *UNUSED(pointcloud), CustomDataLayer *layer) +{ + return layer->type == CD_PROP_FLOAT3 && STREQ(layer->name, POINTCLOUD_ATTR_POSITION); } /* Dependency Graph */ @@ -259,7 +339,8 @@ static PointCloud *pointcloud_evaluate_modifiers(struct Depsgraph *depsgraph, } /* Ensure we are not overwriting referenced data. */ - CustomData_duplicate_referenced_layer(&pointcloud->pdata, CD_LOCATION, pointcloud->totpoint); + CustomData_duplicate_referenced_layer_named( + &pointcloud->pdata, CD_PROP_FLOAT3, POINTCLOUD_ATTR_POSITION, pointcloud->totpoint); BKE_pointcloud_update_customdata_pointers(pointcloud); /* Created deformed coordinates array on demand. */ |