With this patch, materials are kept intact in simulation zones and bake nodes without any additional user action. This implements the design proposed in #108410 to support referencing data-blocks (only materials for now) in the baked data. The task also describes why this is not a trivial issue. A previous attempt was implemented in #109703 but it didn't work well-enough. The solution is to have an explicit `name (+ library name) -> data-block` mapping that is stored in the modifier for each bake node and simulation zone. The `library name` is necessary for it to be unique within a .blend file. Note that this refers to the name of the `Library` data-block and not a file path. The baked data only contains the names of the used data-blocks. When the baked data is loaded, the correct material data-block is looked up from the mapping. ### Automatic Mapping Generation The most tricky aspect of this approach is to make it feel mostly automatic. From the user point-of-view, it should just work. Therefore, we don't want the user to have to create the mapping manually in the majority of cases. Creating the mapping automatically is difficult because the data-blocks that should become part of the mapping are only known during depsgraph evaluation. So we somehow have to gather the missing data blocks during evaluation and then write the new mappings back to the original data. While writing back to original data is something we do in some cases already, the situation here is different, because we are actually creating new relations between data-blocks. This also means that we'll have to do user-counting. Since user counts in data-blocks are *not* atomic, we can't do that from multiple threads at the same time. Also, under some circumstances, it may be necessary to trigger depsgraph evaluation again after the write-back because it actually affects the result. To solve this, a small new API is added in `DEG_depsgraph_writeback_sync.hh`. It allows gathering tasks which write back to original data in a synchronous way which may also require a reevaluation. ### Accessing the Mapping A new `BakeDataBlockMap` is passed to geometry nodes evaluation by the modifier. This map allows getting the `ID` pointer that should be used for a specific data-block name that is stored in baked data. It's also used to gather all the missing data mappings during evaluation. ### Weak ID References The baked/cached geometries may have references to other data-blocks (currently only materials, but in the future also e.g. instanced objects/collections). However, the pointers of these data-blocks are not stable over time. That is especially true when storing/loading the data from disk, but also just when playing back the animation. Therefore, the used data-blocks have to referenced in a different way at run-time. This is solved by adding `std::unique_ptr<bake::BakeMaterialsList>` to the run-time data of various geometry data-blocks. If the data-block is cached over a longer period of time (such that material pointers can't be used directly), it stores the material name (+ library name) used by each material slot. When the geometry is used again, the material pointers are restored using these weak name references and the `BakeDataBlockMap`. ### Manual Mapping Management There is a new `Data-Blocks` panel in the bake settings in the node editor sidebar that allows inspecting and modifying the data-blocks that are used when baking. The user can change what data-block a specific name is mapped to. Pull Request: https://projects.blender.org/blender/blender/pulls/117043
75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
#pragma once
|
|
|
|
/** \file
|
|
* \ingroup bke
|
|
* \brief General operations for point clouds.
|
|
*/
|
|
|
|
#include <mutex>
|
|
|
|
#include "BLI_bounds_types.hh"
|
|
#include "BLI_math_vector_types.hh"
|
|
#include "BLI_shared_cache.hh"
|
|
|
|
#include "DNA_pointcloud_types.h"
|
|
|
|
struct Depsgraph;
|
|
struct Main;
|
|
struct Object;
|
|
struct PointCloud;
|
|
struct Scene;
|
|
namespace blender::bke::bake {
|
|
struct BakeMaterialsList;
|
|
}
|
|
|
|
/* PointCloud datablock */
|
|
extern const char *POINTCLOUD_ATTR_POSITION;
|
|
extern const char *POINTCLOUD_ATTR_RADIUS;
|
|
|
|
namespace blender::bke {
|
|
|
|
struct PointCloudRuntime {
|
|
/**
|
|
* A cache of bounds shared between data-blocks with unchanged positions and radii.
|
|
* When data changes affect the bounds, the cache is "un-shared" with other geometries.
|
|
* See #SharedCache comments.
|
|
*/
|
|
mutable SharedCache<Bounds<float3>> bounds_cache;
|
|
|
|
/** Stores weak references to material data blocks. */
|
|
std::unique_ptr<bake::BakeMaterialsList> bake_materials;
|
|
|
|
MEM_CXX_CLASS_ALLOC_FUNCS("PointCloudRuntime");
|
|
};
|
|
|
|
} // namespace blender::bke
|
|
|
|
void *BKE_pointcloud_add(Main *bmain, const char *name);
|
|
void *BKE_pointcloud_add_default(Main *bmain, const char *name);
|
|
PointCloud *BKE_pointcloud_new_nomain(int totpoint);
|
|
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst);
|
|
|
|
bool BKE_pointcloud_attribute_required(const PointCloud *pointcloud, const char *name);
|
|
|
|
/* Dependency Graph */
|
|
|
|
PointCloud *BKE_pointcloud_copy_for_eval(const PointCloud *pointcloud_src);
|
|
|
|
void BKE_pointcloud_data_update(Depsgraph *depsgraph, Scene *scene, Object *object);
|
|
|
|
/* Draw Cache */
|
|
|
|
enum {
|
|
BKE_POINTCLOUD_BATCH_DIRTY_ALL = 0,
|
|
};
|
|
|
|
void BKE_pointcloud_batch_cache_dirty_tag(PointCloud *pointcloud, int mode);
|
|
void BKE_pointcloud_batch_cache_free(PointCloud *pointcloud);
|
|
|
|
extern void (*BKE_pointcloud_batch_cache_dirty_tag_cb)(PointCloud *pointcloud, int mode);
|
|
extern void (*BKE_pointcloud_batch_cache_free_cb)(PointCloud *pointcloud);
|