2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2005 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup bke
|
2011-02-27 20:29:51 +00:00
|
|
|
*/
|
|
|
|
|
|
2011-02-12 17:51:02 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2012-02-19 22:17:30 +00:00
|
|
|
#include "DNA_mesh_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
|
#include "DNA_object_types.h"
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2016-06-22 21:20:09 +10:00
|
|
|
#include "BLI_linklist.h"
|
Cleanup: reduce amount of math-related includes
Using ClangBuildAnalyzer on the whole Blender build, it was pointing
out that BLI_math.h is the heaviest "header hub" (i.e. non tiny file
that is included a lot).
However, there's very little (actually zero) source files in Blender
that need "all the math" (base, colors, vectors, matrices,
quaternions, intersection, interpolation, statistics, solvers and
time). A common use case is source files needing just vectors, or
just vectors & matrices, or just colors etc. Actually, 181 files
were including the whole math thing without needing it at all.
This change removes BLI_math.h completely, and instead in all the
places that need it, includes BLI_math_vector.h or BLI_math_color.h
and so on.
Change from that:
- BLI_math_color.h was included 1399 times -> now 408 (took 114.0sec
to parse -> now 36.3sec)
- BLI_simd.h 1403 -> 418 (109.7sec -> 34.9sec).
Full rebuild of Blender (Apple M1, Xcode, RelWithDebInfo) is not
affected much (342sec -> 334sec). Most of benefit would be when
someone's changing BLI_simd.h or BLI_math_color.h or similar files,
that now there's 3x fewer files result in a recompile.
Pull Request #110944
2023-08-09 11:39:20 +03:00
|
|
|
#include "BLI_math_matrix.h"
|
|
|
|
|
#include "BLI_math_rotation.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2012-03-24 01:24:58 +00:00
|
|
|
|
2022-07-22 15:39:41 +02:00
|
|
|
#include "BKE_crazyspace.hh"
|
|
|
|
|
#include "BKE_curves.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_editmesh.hh"
|
2022-07-22 15:39:41 +02:00
|
|
|
#include "BKE_geometry_set.hh"
|
2023-06-08 10:20:18 +02:00
|
|
|
#include "BKE_grease_pencil.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2024-05-20 12:26:07 -04:00
|
|
|
#include "BKE_mesh_runtime.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_wrapper.hh"
|
2023-11-14 09:30:40 +01:00
|
|
|
#include "BKE_modifier.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_multires.hh"
|
2023-11-15 18:46:07 +01:00
|
|
|
#include "BKE_object_types.hh"
|
2024-02-10 18:34:29 +01:00
|
|
|
#include "BKE_report.hh"
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph_query.hh"
|
2018-12-05 16:53:45 +01:00
|
|
|
|
2014-02-27 12:28:40 +11:00
|
|
|
BLI_INLINE void tan_calc_quat_v3(float r_quat[4],
|
|
|
|
|
const float co_1[3],
|
|
|
|
|
const float co_2[3],
|
|
|
|
|
const float co_3[3])
|
|
|
|
|
{
|
|
|
|
|
float vec_u[3], vec_v[3];
|
2014-02-28 21:23:06 +11:00
|
|
|
float nor[3];
|
|
|
|
|
|
|
|
|
|
sub_v3_v3v3(vec_u, co_1, co_2);
|
|
|
|
|
sub_v3_v3v3(vec_v, co_1, co_3);
|
|
|
|
|
|
|
|
|
|
cross_v3_v3v3(nor, vec_u, vec_v);
|
|
|
|
|
|
|
|
|
|
if (normalize_v3(nor) > FLT_EPSILON) {
|
|
|
|
|
const float zero_vec[3] = {0.0f};
|
|
|
|
|
tri_to_quat_ex(r_quat, zero_vec, vec_u, vec_v, nor);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2014-02-27 12:28:40 +11:00
|
|
|
unit_qt(r_quat);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-15 08:05:27 +00:00
|
|
|
static void set_crazy_vertex_quat(float r_quat[4],
|
|
|
|
|
const float co_1[3],
|
|
|
|
|
const float co_2[3],
|
|
|
|
|
const float co_3[3],
|
|
|
|
|
const float vd_1[3],
|
|
|
|
|
const float vd_2[3],
|
|
|
|
|
const float vd_3[3])
|
|
|
|
|
{
|
2011-02-12 17:51:02 +00:00
|
|
|
float q1[4], q2[4];
|
|
|
|
|
|
2014-02-27 12:28:40 +11:00
|
|
|
tan_calc_quat_v3(q1, co_1, co_2, co_3);
|
|
|
|
|
tan_calc_quat_v3(q2, vd_1, vd_2, vd_3);
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2013-09-15 08:05:27 +00:00
|
|
|
sub_qt_qtqt(r_quat, q2, q1);
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-24 14:51:07 -03:00
|
|
|
static bool modifiers_disable_subsurf_temporary(Object *ob, const int cageIndex)
|
2011-02-12 17:51:02 +00:00
|
|
|
{
|
2022-10-24 14:51:07 -03:00
|
|
|
bool changed = false;
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2022-07-22 12:33:08 +02:00
|
|
|
ModifierData *md = static_cast<ModifierData *>(ob->modifiers.first);
|
2020-01-31 23:58:32 -03:00
|
|
|
for (int i = 0; md && i <= cageIndex; i++, md = md->next) {
|
2019-04-22 09:39:35 +10:00
|
|
|
if (md->type == eModifierType_Subsurf) {
|
2020-01-31 23:58:32 -03:00
|
|
|
md->mode ^= eModifierMode_DisableTemporary;
|
2022-10-24 14:51:07 -03:00
|
|
|
changed = true;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2022-10-24 14:51:07 -03:00
|
|
|
return changed;
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 18:29:52 +01:00
|
|
|
blender::Array<blender::float3> BKE_crazyspace_get_mapped_editverts(Depsgraph *depsgraph,
|
|
|
|
|
Object *obedit)
|
2011-02-12 17:51:02 +00:00
|
|
|
{
|
2019-06-05 15:17:53 +02:00
|
|
|
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
|
|
|
|
|
Object *obedit_eval = DEG_get_evaluated_object(depsgraph, obedit);
|
2022-10-24 14:51:07 -03:00
|
|
|
const int cageIndex = BKE_modifiers_get_cage_index(scene_eval, obedit_eval, nullptr, true);
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2022-10-24 14:51:07 -03:00
|
|
|
/* Disable subsurf temporal, get mapped cos, and enable it. */
|
|
|
|
|
if (modifiers_disable_subsurf_temporary(obedit_eval, cageIndex)) {
|
|
|
|
|
/* Need to make new cage.
|
|
|
|
|
* TODO: Avoid losing original evaluated geometry. */
|
2024-05-20 13:11:12 -04:00
|
|
|
blender::bke::mesh_data_update(*depsgraph, *scene_eval, *obedit_eval, CD_MASK_BAREMESH);
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-24 14:51:07 -03:00
|
|
|
/* Now get the cage. */
|
|
|
|
|
BMEditMesh *em_eval = BKE_editmesh_from_object(obedit_eval);
|
2024-05-20 12:26:07 -04:00
|
|
|
Mesh *mesh_eval_cage = blender::bke::editbmesh_get_eval_cage(
|
2022-10-24 14:51:07 -03:00
|
|
|
depsgraph, scene_eval, obedit_eval, em_eval, &CD_MASK_BAREMESH);
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2022-10-24 14:51:07 -03:00
|
|
|
const int nverts = em_eval->bm->totvert;
|
2023-11-16 18:29:52 +01:00
|
|
|
blender::Array<blender::float3> vertexcos(nverts);
|
2024-05-20 12:26:07 -04:00
|
|
|
blender::bke::mesh_get_mapped_verts_coords(mesh_eval_cage, vertexcos);
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2022-10-24 14:51:07 -03:00
|
|
|
/* Set back the flag, and ensure new cage needs to be built. */
|
|
|
|
|
if (modifiers_disable_subsurf_temporary(obedit_eval, cageIndex)) {
|
|
|
|
|
DEG_id_tag_update(&obedit->id, ID_RECALC_GEOMETRY);
|
|
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
|
|
|
|
|
return vertexcos;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 02:59:23 +03:00
|
|
|
void BKE_crazyspace_set_quats_editmesh(BMEditMesh *em,
|
2023-11-16 18:29:52 +01:00
|
|
|
const blender::Span<blender::float3> origcos,
|
|
|
|
|
const blender::Span<blender::float3> mappedcos,
|
2014-05-07 02:59:23 +03:00
|
|
|
float (*quats)[4],
|
2014-02-28 21:10:32 +11:00
|
|
|
const bool use_select)
|
2011-02-12 17:51:02 +00:00
|
|
|
{
|
2024-05-25 10:41:07 -04:00
|
|
|
using namespace blender;
|
2013-09-15 11:38:48 +00:00
|
|
|
BMFace *f;
|
|
|
|
|
BMIter iter;
|
|
|
|
|
int index;
|
2023-12-04 12:28:44 +11:00
|
|
|
const bool has_origcos = !origcos.is_empty();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-15 11:38:48 +00:00
|
|
|
{
|
|
|
|
|
BMVert *v;
|
|
|
|
|
BM_ITER_MESH_INDEX (v, &iter, em->bm, BM_VERTS_OF_MESH, index) {
|
|
|
|
|
BM_elem_flag_disable(v, BM_ELEM_TAG);
|
|
|
|
|
BM_elem_index_set(v, index); /* set_inline */
|
2011-04-15 01:19:13 +00:00
|
|
|
}
|
2013-09-15 11:38:48 +00:00
|
|
|
em->bm->elem_index_dirty &= ~BM_VERT;
|
2011-04-15 01:19:13 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-15 11:38:48 +00:00
|
|
|
BM_ITER_MESH (f, &iter, em->bm, BM_FACES_OF_MESH) {
|
|
|
|
|
BMLoop *l_iter, *l_first;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-15 11:38:48 +00:00
|
|
|
l_iter = l_first = BM_FACE_FIRST_LOOP(f);
|
|
|
|
|
do {
|
2014-02-28 21:10:32 +11:00
|
|
|
if (BM_elem_flag_test(l_iter->v, BM_ELEM_HIDDEN) ||
|
|
|
|
|
BM_elem_flag_test(l_iter->v, BM_ELEM_TAG) ||
|
|
|
|
|
(use_select && !BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)))
|
|
|
|
|
{
|
2013-09-15 11:38:48 +00:00
|
|
|
continue;
|
2014-02-28 21:10:32 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-15 11:38:48 +00:00
|
|
|
if (!BM_elem_flag_test(l_iter->v, BM_ELEM_TAG)) {
|
|
|
|
|
const float *co_prev, *co_curr, *co_next; /* orig */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-05-25 10:41:07 -04:00
|
|
|
const int vert_prev = BM_elem_index_get(l_iter->prev->v);
|
|
|
|
|
const int vert = BM_elem_index_get(l_iter->v);
|
|
|
|
|
const int vert_next = BM_elem_index_get(l_iter->next->v);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
/* Retrieve mapped coordinates. */
|
2024-05-25 10:41:07 -04:00
|
|
|
const float3 &vd_prev = mappedcos[vert_prev];
|
|
|
|
|
const float3 &vd_curr = mappedcos[vert];
|
|
|
|
|
const float3 &vd_next = mappedcos[vert_next];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
if (has_origcos) {
|
2024-05-25 10:41:07 -04:00
|
|
|
co_prev = origcos[vert_prev];
|
|
|
|
|
co_curr = origcos[vert];
|
|
|
|
|
co_next = origcos[vert_next];
|
2013-09-15 11:38:48 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
co_prev = l_iter->prev->v->co;
|
|
|
|
|
co_curr = l_iter->v->co;
|
|
|
|
|
co_next = l_iter->next->v->co;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-05-25 10:41:07 -04:00
|
|
|
set_crazy_vertex_quat(quats[vert], co_curr, co_next, co_prev, vd_curr, vd_next, vd_prev);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-15 11:38:48 +00:00
|
|
|
BM_elem_flag_enable(l_iter->v, BM_ELEM_TAG);
|
|
|
|
|
}
|
|
|
|
|
} while ((l_iter = l_iter->next) != l_first);
|
2011-04-15 01:19:13 +00:00
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void BKE_crazyspace_set_quats_mesh(Mesh *mesh,
|
2023-11-16 18:29:52 +01:00
|
|
|
const blender::Span<blender::float3> origcos,
|
|
|
|
|
const blender::Span<blender::float3> mappedcos,
|
2014-05-07 02:59:23 +03:00
|
|
|
float (*quats)[4])
|
2011-02-12 17:51:02 +00:00
|
|
|
{
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
using namespace blender;
|
|
|
|
|
using namespace blender::bke;
|
2023-12-20 02:21:48 +01:00
|
|
|
BitVector<> vert_tag(mesh->verts_num);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-12 17:51:02 +00:00
|
|
|
/* first store two sets of tangent vectors in vertices, we derive it just from the face-edges */
|
2024-05-25 10:41:07 -04:00
|
|
|
const Span<float3> positions = origcos.is_empty() ? mesh->vert_positions() : origcos;
|
2023-12-08 16:40:06 -05:00
|
|
|
const OffsetIndices<int> faces = mesh->faces();
|
|
|
|
|
const Span<int> corner_verts = mesh->corner_verts();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-19 18:31:37 +01:00
|
|
|
for (const int i : faces.index_range()) {
|
2023-07-24 22:06:55 +02:00
|
|
|
const IndexRange face = faces[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-19 18:31:37 +01:00
|
|
|
for (const int corner : face) {
|
2024-05-25 10:41:07 -04:00
|
|
|
const int vert = corner_verts[corner];
|
|
|
|
|
if (vert_tag[vert]) {
|
2023-12-04 12:28:44 +11:00
|
|
|
continue;
|
|
|
|
|
}
|
2024-05-25 10:41:07 -04:00
|
|
|
const int vert_prev = corner_verts[mesh::face_corner_prev(face, corner)];
|
|
|
|
|
const int vert_next = corner_verts[mesh::face_corner_next(face, corner)];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-05-25 10:41:07 -04:00
|
|
|
const float3 &vd_prev = mappedcos[vert_prev];
|
|
|
|
|
const float3 &vd_curr = mappedcos[vert];
|
|
|
|
|
const float3 &vd_next = mappedcos[vert_next];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-05-25 10:41:07 -04:00
|
|
|
const float3 &co_prev = positions[vert_prev];
|
|
|
|
|
const float3 &co_curr = positions[vert];
|
|
|
|
|
const float3 &co_next = positions[vert_next];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-05-25 10:41:07 -04:00
|
|
|
set_crazy_vertex_quat(quats[vert], co_curr, co_next, co_prev, vd_curr, vd_next, vd_prev);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-05-25 10:41:07 -04:00
|
|
|
vert_tag[vert].set();
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 18:29:52 +01:00
|
|
|
int BKE_crazyspace_get_first_deform_matrices_editbmesh(
|
|
|
|
|
Depsgraph *depsgraph,
|
|
|
|
|
Scene *scene,
|
|
|
|
|
Object *ob,
|
|
|
|
|
BMEditMesh *em,
|
|
|
|
|
blender::Array<blender::float3x3, 0> &deformmats,
|
|
|
|
|
blender::Array<blender::float3, 0> &deformcos)
|
2011-02-12 17:51:02 +00:00
|
|
|
{
|
|
|
|
|
ModifierData *md;
|
2022-07-22 12:33:08 +02:00
|
|
|
Mesh *me_input = static_cast<Mesh *>(ob->data);
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = nullptr;
|
2023-11-16 18:29:52 +01:00
|
|
|
int i, modifiers_left_num = 0;
|
2023-07-07 13:07:15 +02:00
|
|
|
const int verts_num = em->bm->totvert;
|
2022-07-29 23:23:50 -05:00
|
|
|
int cageIndex = BKE_modifiers_get_cage_index(scene, ob, nullptr, true);
|
2023-07-27 12:04:18 +10:00
|
|
|
VirtualModifierData virtual_modifier_data;
|
2022-07-22 12:33:08 +02:00
|
|
|
ModifierEvalContext mectx = {depsgraph, ob, ModifierApplyFlag(0)};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-05-08 10:14:02 +02:00
|
|
|
BKE_modifiers_clear_errors(ob);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
md = BKE_modifiers_get_virtual_modifierlist(ob, &virtual_modifier_data);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-12 17:51:02 +00:00
|
|
|
/* compute the deformation matrices and coordinates for the first
|
2012-03-03 16:31:46 +00:00
|
|
|
* modifiers with on cage editing that are enabled and support computing
|
|
|
|
|
* deform matrices */
|
2012-03-24 06:38:07 +00:00
|
|
|
for (i = 0; md && i <= cageIndex; i++, md = md->next) {
|
2022-07-22 12:33:08 +02:00
|
|
|
const ModifierTypeInfo *mti = BKE_modifier_get_info(static_cast<ModifierType>(md->type));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-05-20 12:26:07 -04:00
|
|
|
if (!blender::bke::editbmesh_modifier_is_enabled(scene, ob, md, mesh != nullptr)) {
|
2011-02-12 17:51:02 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-14 10:03:56 +01:00
|
|
|
if (mti->type == ModifierTypeType::OnlyDeform && mti->deform_matrices_EM) {
|
2023-11-16 18:29:52 +01:00
|
|
|
if (deformmats.is_empty()) {
|
2016-06-22 21:20:09 +10:00
|
|
|
const int required_mode = eModifierMode_Realtime | eModifierMode_Editmode;
|
2020-03-17 11:04:50 +11:00
|
|
|
CustomData_MeshMasks cd_mask_extra = CD_MASK_BAREMESH;
|
2020-05-08 10:14:02 +02:00
|
|
|
CDMaskLink *datamasks = BKE_modifier_calc_data_masks(
|
2023-12-05 09:30:13 -05:00
|
|
|
scene, md, &cd_mask_extra, required_mode);
|
2020-03-17 11:04:50 +11:00
|
|
|
cd_mask_extra = datamasks->mask;
|
2022-07-22 12:33:08 +02:00
|
|
|
BLI_linklist_free((LinkNode *)datamasks, nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-04-18 13:52:20 +02:00
|
|
|
mesh = BKE_mesh_wrapper_from_editmesh(
|
|
|
|
|
std::make_shared<BMEditMesh>(*em), &cd_mask_extra, me_input);
|
2023-11-16 18:29:52 +01:00
|
|
|
deformcos.reinitialize(verts_num);
|
2023-12-08 16:40:06 -05:00
|
|
|
BKE_mesh_wrapper_vert_coords_copy(mesh, deformcos);
|
2023-11-16 18:29:52 +01:00
|
|
|
deformmats.reinitialize(verts_num);
|
|
|
|
|
deformmats.fill(blender::float3x3::identity());
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2023-12-08 16:40:06 -05:00
|
|
|
mti->deform_matrices_EM(md, &mectx, em, mesh, deformcos, deformmats);
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-22 09:39:35 +10:00
|
|
|
else {
|
2011-02-12 17:51:02 +00:00
|
|
|
break;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-22 09:39:35 +10:00
|
|
|
for (; md && i <= cageIndex; md = md->next, i++) {
|
2024-05-20 12:26:07 -04:00
|
|
|
if (blender::bke::editbmesh_modifier_is_enabled(scene, ob, md, mesh != nullptr) &&
|
2020-05-08 10:14:02 +02:00
|
|
|
BKE_modifier_is_correctable_deformed(md))
|
|
|
|
|
{
|
2022-03-28 12:29:47 +11:00
|
|
|
modifiers_left_num++;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
if (mesh) {
|
|
|
|
|
BKE_id_free(nullptr, mesh);
|
2018-10-09 15:37:10 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
return modifiers_left_num;
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2019-04-14 10:48:42 +02:00
|
|
|
/**
|
2020-07-01 13:12:24 +10:00
|
|
|
* Crazy-space evaluation needs to have an object which has all the fields
|
2019-03-11 16:00:04 +01:00
|
|
|
* evaluated, but the mesh data being at undeformed state. This way it can
|
|
|
|
|
* re-apply modifiers and also have proper pointers to key data blocks.
|
|
|
|
|
*
|
2019-04-14 10:48:42 +02:00
|
|
|
* Similar to #BKE_object_eval_reset(), but does not modify the actual evaluated object.
|
|
|
|
|
*/
|
2023-06-03 08:36:28 +10:00
|
|
|
static void crazyspace_init_object_for_eval(Depsgraph *depsgraph,
|
2019-03-11 16:00:04 +01:00
|
|
|
Object *object,
|
|
|
|
|
Object *object_crazy)
|
|
|
|
|
{
|
|
|
|
|
Object *object_eval = DEG_get_evaluated_object(depsgraph, object);
|
2022-07-22 12:33:08 +02:00
|
|
|
*object_crazy = blender::dna::shallow_copy(*object_eval);
|
2023-11-15 18:46:07 +01:00
|
|
|
object_crazy->runtime = MEM_new<blender::bke::ObjectRuntime>(__func__, *object_eval->runtime);
|
|
|
|
|
if (object_crazy->runtime->data_orig != nullptr) {
|
|
|
|
|
object_crazy->data = object_crazy->runtime->data_orig;
|
2019-03-11 16:00:04 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 16:56:42 +02:00
|
|
|
static bool crazyspace_modifier_supports_deform_matrices(ModifierData *md)
|
2019-09-11 11:14:06 +02:00
|
|
|
{
|
|
|
|
|
if (ELEM(md->type, eModifierType_Subsurf, eModifierType_Multires)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-07-22 12:33:08 +02:00
|
|
|
const ModifierTypeInfo *mti = BKE_modifier_get_info(static_cast<ModifierType>(md->type));
|
2023-11-14 10:03:56 +01:00
|
|
|
return (mti->type == ModifierTypeType::OnlyDeform);
|
2019-09-11 11:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-04 16:56:42 +02:00
|
|
|
static bool crazyspace_modifier_supports_deform(ModifierData *md)
|
|
|
|
|
{
|
2022-07-22 12:33:08 +02:00
|
|
|
const ModifierTypeInfo *mti = BKE_modifier_get_info(static_cast<ModifierType>(md->type));
|
2023-11-14 10:03:56 +01:00
|
|
|
return (mti->type == ModifierTypeType::OnlyDeform);
|
2019-10-04 16:56:42 +02:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
int BKE_sculpt_get_first_deform_matrices(Depsgraph *depsgraph,
|
2018-04-06 12:07:27 +02:00
|
|
|
Scene *scene,
|
2018-12-05 16:53:45 +01:00
|
|
|
Object *object,
|
2023-11-16 18:29:52 +01:00
|
|
|
blender::Array<blender::float3x3, 0> &deformmats,
|
|
|
|
|
blender::Array<blender::float3, 0> &deformcos)
|
2011-02-12 17:51:02 +00:00
|
|
|
{
|
|
|
|
|
ModifierData *md;
|
2024-03-11 11:21:12 -04:00
|
|
|
Mesh *mesh_eval = nullptr;
|
2022-03-28 12:29:47 +11:00
|
|
|
int modifiers_left_num = 0;
|
2023-07-27 12:04:18 +10:00
|
|
|
VirtualModifierData virtual_modifier_data;
|
2019-03-11 16:00:04 +01:00
|
|
|
Object object_eval;
|
|
|
|
|
crazyspace_init_object_for_eval(depsgraph, object, &object_eval);
|
2023-11-15 18:46:07 +01:00
|
|
|
BLI_SCOPED_DEFER([&]() { MEM_delete(object_eval.runtime); });
|
2022-07-29 23:23:50 -05:00
|
|
|
MultiresModifierData *mmd = get_multires_modifier(scene, &object_eval, false);
|
2019-09-11 11:14:06 +02:00
|
|
|
const bool is_sculpt_mode = (object->mode & OB_MODE_SCULPT) != 0;
|
2022-07-22 12:33:08 +02:00
|
|
|
const bool has_multires = mmd != nullptr && mmd->sculptlvl > 0;
|
|
|
|
|
const ModifierEvalContext mectx = {depsgraph, &object_eval, ModifierApplyFlag(0)};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-11 11:14:06 +02:00
|
|
|
if (is_sculpt_mode && has_multires) {
|
2023-11-16 18:29:52 +01:00
|
|
|
deformcos = {};
|
|
|
|
|
deformmats = {};
|
2022-03-28 12:29:47 +11:00
|
|
|
return modifiers_left_num;
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
md = BKE_modifiers_get_virtual_modifierlist(&object_eval, &virtual_modifier_data);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-30 10:00:20 +00:00
|
|
|
for (; md; md = md->next) {
|
2020-05-08 10:14:02 +02:00
|
|
|
if (!BKE_modifier_is_enabled(scene, md, eModifierMode_Realtime)) {
|
2012-03-24 06:38:07 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-10-04 16:56:42 +02:00
|
|
|
if (crazyspace_modifier_supports_deform_matrices(md)) {
|
2022-07-22 12:33:08 +02:00
|
|
|
const ModifierTypeInfo *mti = BKE_modifier_get_info(static_cast<ModifierType>(md->type));
|
2023-11-16 18:29:52 +01:00
|
|
|
if (deformmats.is_empty()) {
|
2021-07-07 12:55:19 +10:00
|
|
|
/* NOTE: Evaluated object is re-set to its original un-deformed state. */
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(object_eval.data);
|
2024-05-20 13:18:24 -04:00
|
|
|
mesh_eval = BKE_mesh_copy_for_eval(*mesh);
|
2023-12-08 16:40:06 -05:00
|
|
|
deformcos = mesh->vert_positions();
|
2023-12-20 02:21:48 +01:00
|
|
|
deformmats.reinitialize(mesh->verts_num);
|
2023-11-16 18:29:52 +01:00
|
|
|
deformmats.fill(blender::float3x3::identity());
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-27 12:04:18 +10:00
|
|
|
if (mti->deform_matrices) {
|
2024-03-11 11:21:12 -04:00
|
|
|
mti->deform_matrices(md, &mectx, mesh_eval, deformcos, deformmats);
|
2018-04-18 15:45:54 +02:00
|
|
|
}
|
2019-04-22 09:39:35 +10:00
|
|
|
else {
|
2019-11-30 19:07:44 +03:00
|
|
|
/* More complex handling will continue in BKE_crazyspace_build_sculpt.
|
2023-02-12 14:37:16 +11:00
|
|
|
* Exiting the loop on a non-deform modifier causes issues - #71213. */
|
2019-11-30 19:07:44 +03:00
|
|
|
BLI_assert(crazyspace_modifier_supports_deform(md));
|
2011-02-12 17:51:02 +00:00
|
|
|
break;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-30 10:00:20 +00:00
|
|
|
for (; md; md = md->next) {
|
2020-05-08 10:14:02 +02:00
|
|
|
if (!BKE_modifier_is_enabled(scene, md, eModifierMode_Realtime)) {
|
2012-03-24 06:38:07 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-11 11:14:06 +02:00
|
|
|
if (crazyspace_modifier_supports_deform(md)) {
|
2022-03-28 12:29:47 +11:00
|
|
|
modifiers_left_num++;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2024-03-11 11:21:12 -04:00
|
|
|
if (mesh_eval != nullptr) {
|
|
|
|
|
BKE_id_free(nullptr, mesh_eval);
|
2018-10-10 12:01:05 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-03-28 12:29:47 +11:00
|
|
|
return modifiers_left_num;
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_crazyspace_build_sculpt(Depsgraph *depsgraph,
|
2018-12-05 16:53:45 +01:00
|
|
|
Scene *scene,
|
|
|
|
|
Object *object,
|
2023-11-16 18:29:52 +01:00
|
|
|
blender::Array<blender::float3x3, 0> &deformmats,
|
|
|
|
|
blender::Array<blender::float3, 0> &deformcos)
|
2011-02-12 17:51:02 +00:00
|
|
|
{
|
2018-12-05 16:53:45 +01:00
|
|
|
int totleft = BKE_sculpt_get_first_deform_matrices(
|
|
|
|
|
depsgraph, scene, object, deformmats, deformcos);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (totleft) {
|
2020-07-01 13:12:24 +10:00
|
|
|
/* There are deformation modifier which doesn't support deformation matrices calculation.
|
|
|
|
|
* Need additional crazy-space correction. */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-11 11:14:06 +02:00
|
|
|
Mesh *mesh = (Mesh *)object->data;
|
2022-07-22 12:33:08 +02:00
|
|
|
Mesh *mesh_eval = nullptr;
|
2019-09-11 11:14:06 +02:00
|
|
|
|
2023-11-16 18:29:52 +01:00
|
|
|
if (deformcos.is_empty()) {
|
|
|
|
|
deformcos = mesh->vert_positions();
|
2023-12-20 02:21:48 +01:00
|
|
|
deformmats.reinitialize(mesh->verts_num);
|
2023-11-16 18:29:52 +01:00
|
|
|
deformmats.fill(blender::float3x3::identity());
|
2019-09-11 11:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 18:29:52 +01:00
|
|
|
blender::Array<blender::float3, 0> deformedVerts = deformcos;
|
|
|
|
|
blender::Array<blender::float3, 0> origVerts = deformedVerts;
|
2013-09-15 08:05:27 +00:00
|
|
|
float(*quats)[4];
|
2012-03-30 10:00:20 +00:00
|
|
|
int i, deformed = 0;
|
2023-07-27 12:04:18 +10:00
|
|
|
VirtualModifierData virtual_modifier_data;
|
2019-03-11 16:00:04 +01:00
|
|
|
Object object_eval;
|
|
|
|
|
crazyspace_init_object_for_eval(depsgraph, object, &object_eval);
|
2024-04-10 18:35:45 -04:00
|
|
|
BLI_SCOPED_DEFER([&]() { MEM_delete(object_eval.runtime); });
|
2023-07-27 12:04:18 +10:00
|
|
|
ModifierData *md = BKE_modifiers_get_virtual_modifierlist(&object_eval,
|
|
|
|
|
&virtual_modifier_data);
|
2022-07-22 12:33:08 +02:00
|
|
|
const ModifierEvalContext mectx = {depsgraph, &object_eval, ModifierApplyFlag(0)};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-30 10:00:20 +00:00
|
|
|
for (; md; md = md->next) {
|
2020-05-08 10:14:02 +02:00
|
|
|
if (!BKE_modifier_is_enabled(scene, md, eModifierMode_Realtime)) {
|
2012-03-24 06:38:07 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-11 11:14:06 +02:00
|
|
|
if (crazyspace_modifier_supports_deform(md)) {
|
2022-07-22 12:33:08 +02:00
|
|
|
const ModifierTypeInfo *mti = BKE_modifier_get_info(static_cast<ModifierType>(md->type));
|
2019-09-11 11:14:06 +02:00
|
|
|
|
2011-04-21 05:49:47 +00:00
|
|
|
/* skip leading modifiers which have been already
|
2012-03-03 16:31:46 +00:00
|
|
|
* handled in sculpt_get_first_deform_matrices */
|
2023-07-27 12:04:18 +10:00
|
|
|
if (mti->deform_matrices && !deformed) {
|
2011-02-14 16:06:15 +00:00
|
|
|
continue;
|
2019-04-22 09:39:35 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-07-22 12:33:08 +02:00
|
|
|
if (mesh_eval == nullptr) {
|
2024-05-20 13:18:24 -04:00
|
|
|
mesh_eval = BKE_mesh_copy_for_eval(*mesh);
|
2019-09-11 11:14:06 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-16 18:29:52 +01:00
|
|
|
mti->deform_verts(md, &mectx, mesh_eval, deformedVerts);
|
2012-03-30 10:00:20 +00:00
|
|
|
deformed = 1;
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
quats = static_cast<float(*)[4]>(MEM_mallocN(mesh->verts_num * sizeof(*quats), "crazy quats"));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-12-05 16:53:45 +01:00
|
|
|
BKE_crazyspace_set_quats_mesh(mesh, origVerts, deformedVerts, quats);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-20 02:21:48 +01:00
|
|
|
for (i = 0; i < mesh->verts_num; i++) {
|
2011-02-12 17:51:02 +00:00
|
|
|
float qmat[3][3], tmat[3][3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-15 08:05:27 +00:00
|
|
|
quat_to_mat3(qmat, quats[i]);
|
2023-11-16 18:29:52 +01:00
|
|
|
mul_m3_m3m3(tmat, qmat, deformmats[i].ptr());
|
|
|
|
|
copy_m3_m3(deformmats[i].ptr(), tmat);
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-02-12 17:51:02 +00:00
|
|
|
MEM_freeN(quats);
|
2019-09-11 11:14:06 +02:00
|
|
|
|
2022-07-22 12:33:08 +02:00
|
|
|
if (mesh_eval != nullptr) {
|
|
|
|
|
BKE_id_free(nullptr, mesh_eval);
|
2019-09-11 11:14:06 +02:00
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-16 18:29:52 +01:00
|
|
|
if (deformmats.is_empty()) {
|
2018-12-05 16:53:45 +01:00
|
|
|
Mesh *mesh = (Mesh *)object->data;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-11-16 18:29:52 +01:00
|
|
|
deformcos = mesh->vert_positions();
|
2023-12-20 02:21:48 +01:00
|
|
|
deformmats.reinitialize(mesh->verts_num);
|
2023-11-16 18:29:52 +01:00
|
|
|
deformmats.fill(blender::float3x3::identity());
|
2011-05-04 13:15:42 +00:00
|
|
|
}
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2022-01-05 09:53:48 +01:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Crazyspace API
|
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
|
|
void BKE_crazyspace_api_eval(Depsgraph *depsgraph,
|
|
|
|
|
Scene *scene,
|
|
|
|
|
Object *object,
|
2023-06-03 08:36:28 +10:00
|
|
|
ReportList *reports)
|
2022-01-05 09:53:48 +01:00
|
|
|
{
|
2023-11-16 18:29:52 +01:00
|
|
|
if (!object->runtime->crazyspace_deform_imats.is_empty() ||
|
|
|
|
|
!object->runtime->crazyspace_deform_cos.is_empty())
|
2022-07-22 12:33:08 +02:00
|
|
|
{
|
2022-01-05 09:53:48 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (object->type != OB_MESH) {
|
|
|
|
|
BKE_report(reports,
|
|
|
|
|
RPT_ERROR,
|
|
|
|
|
"Crazyspace transformation is only available for Mesh type of objects");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BKE_crazyspace_build_sculpt(depsgraph,
|
|
|
|
|
scene,
|
|
|
|
|
object,
|
2023-11-16 18:29:52 +01:00
|
|
|
object->runtime->crazyspace_deform_imats,
|
|
|
|
|
object->runtime->crazyspace_deform_cos);
|
2022-01-05 09:53:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_crazyspace_api_displacement_to_deformed(Object *object,
|
|
|
|
|
ReportList *reports,
|
2022-01-05 09:53:48 +01:00
|
|
|
int vertex_index,
|
2023-08-01 15:46:26 +10:00
|
|
|
const float displacement[3],
|
2022-01-05 09:53:48 +01:00
|
|
|
float r_displacement_deformed[3])
|
|
|
|
|
{
|
2023-11-16 18:29:52 +01:00
|
|
|
if (vertex_index < 0 || vertex_index >= object->runtime->crazyspace_deform_imats.size()) {
|
2022-01-05 09:53:48 +01:00
|
|
|
BKE_reportf(reports,
|
|
|
|
|
RPT_ERROR,
|
|
|
|
|
"Invalid vertex index %d (expected to be within 0 to %d range)",
|
|
|
|
|
vertex_index,
|
2023-11-16 18:29:52 +01:00
|
|
|
int(object->runtime->crazyspace_deform_imats.size()));
|
2022-01-05 09:53:48 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mul_v3_m3v3(r_displacement_deformed,
|
2023-11-16 18:29:52 +01:00
|
|
|
object->runtime->crazyspace_deform_imats[vertex_index].ptr(),
|
2022-01-05 09:53:48 +01:00
|
|
|
displacement);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
void BKE_crazyspace_api_displacement_to_original(Object *object,
|
|
|
|
|
ReportList *reports,
|
2022-01-05 09:53:48 +01:00
|
|
|
int vertex_index,
|
2023-08-01 15:46:26 +10:00
|
|
|
const float displacement_deformed[3],
|
2022-01-05 09:53:48 +01:00
|
|
|
float r_displacement[3])
|
|
|
|
|
{
|
2023-11-16 18:29:52 +01:00
|
|
|
if (vertex_index < 0 || vertex_index >= object->runtime->crazyspace_deform_imats.size()) {
|
2022-01-05 09:53:48 +01:00
|
|
|
BKE_reportf(reports,
|
|
|
|
|
RPT_ERROR,
|
2022-12-09 15:41:05 -06:00
|
|
|
"Invalid vertex index %d (expected to be within 0 to %d range)",
|
2022-01-05 09:53:48 +01:00
|
|
|
vertex_index,
|
2023-11-16 18:29:52 +01:00
|
|
|
int(object->runtime->crazyspace_deform_imats.size()));
|
2022-01-05 09:53:48 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float mat[3][3];
|
2023-11-16 18:29:52 +01:00
|
|
|
if (!invert_m3_m3(mat, object->runtime->crazyspace_deform_imats[vertex_index].ptr())) {
|
2022-01-05 09:53:48 +01:00
|
|
|
copy_v3_v3(r_displacement, displacement_deformed);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mul_v3_m3v3(r_displacement, mat, displacement_deformed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BKE_crazyspace_api_eval_clear(Object *object)
|
|
|
|
|
{
|
2023-11-16 18:29:52 +01:00
|
|
|
object->runtime->crazyspace_deform_imats = {};
|
|
|
|
|
object->runtime->crazyspace_deform_cos = {};
|
2022-01-05 09:53:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
2022-07-22 15:39:41 +02:00
|
|
|
|
|
|
|
|
namespace blender::bke::crazyspace {
|
|
|
|
|
|
2023-02-15 17:39:53 -05:00
|
|
|
GeometryDeformation get_evaluated_curves_deformation(const Object *ob_eval, const Object &ob_orig)
|
2022-07-22 15:39:41 +02:00
|
|
|
{
|
|
|
|
|
BLI_assert(ob_orig.type == OB_CURVES);
|
|
|
|
|
const Curves &curves_id_orig = *static_cast<const Curves *>(ob_orig.data);
|
2023-01-31 18:45:34 +01:00
|
|
|
const CurvesGeometry &curves_orig = curves_id_orig.geometry.wrap();
|
2022-07-22 15:39:41 +02:00
|
|
|
const int points_num = curves_orig.points_num();
|
|
|
|
|
|
|
|
|
|
GeometryDeformation deformation;
|
|
|
|
|
/* Use the undeformed positions by default. */
|
|
|
|
|
deformation.positions = curves_orig.positions();
|
|
|
|
|
|
|
|
|
|
if (ob_eval == nullptr) {
|
|
|
|
|
return deformation;
|
|
|
|
|
}
|
2023-11-15 18:46:07 +01:00
|
|
|
const GeometrySet *geometry_eval = ob_eval->runtime->geometry_set_eval;
|
2022-07-22 15:39:41 +02:00
|
|
|
if (geometry_eval == nullptr) {
|
|
|
|
|
return deformation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If available, use deformation information generated during evaluation. */
|
|
|
|
|
const GeometryComponentEditData *edit_component_eval =
|
2023-08-03 17:09:18 +02:00
|
|
|
geometry_eval->get_component<GeometryComponentEditData>();
|
2022-07-22 15:39:41 +02:00
|
|
|
bool uses_extra_positions = false;
|
|
|
|
|
if (edit_component_eval != nullptr) {
|
|
|
|
|
const CurvesEditHints *edit_hints = edit_component_eval->curves_edit_hints_.get();
|
|
|
|
|
if (edit_hints != nullptr && &edit_hints->curves_id_orig == &curves_id_orig) {
|
2024-04-03 14:14:34 +02:00
|
|
|
if (const std::optional<Span<float3>> positions = edit_hints->positions()) {
|
|
|
|
|
BLI_assert(positions->size() == points_num);
|
|
|
|
|
deformation.positions = *positions;
|
2022-07-22 15:39:41 +02:00
|
|
|
uses_extra_positions = true;
|
|
|
|
|
}
|
|
|
|
|
if (edit_hints->deform_mats.has_value()) {
|
|
|
|
|
BLI_assert(edit_hints->deform_mats->size() == points_num);
|
|
|
|
|
deformation.deform_mats = *edit_hints->deform_mats;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Use the positions of the evaluated curves directly, if the number of points matches. */
|
|
|
|
|
if (!uses_extra_positions) {
|
2023-08-03 17:09:18 +02:00
|
|
|
const CurveComponent *curves_component_eval = geometry_eval->get_component<CurveComponent>();
|
2022-07-22 15:39:41 +02:00
|
|
|
if (curves_component_eval != nullptr) {
|
2023-08-03 17:09:18 +02:00
|
|
|
const Curves *curves_id_eval = curves_component_eval->get();
|
2022-07-22 15:39:41 +02:00
|
|
|
if (curves_id_eval != nullptr) {
|
2023-01-31 18:45:34 +01:00
|
|
|
const CurvesGeometry &curves_eval = curves_id_eval->geometry.wrap();
|
2022-07-22 15:39:41 +02:00
|
|
|
if (curves_eval.points_num() == points_num) {
|
|
|
|
|
deformation.positions = curves_eval.positions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return deformation;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 16:33:38 +01:00
|
|
|
GeometryDeformation get_evaluated_curves_deformation(const Depsgraph &depsgraph,
|
|
|
|
|
const Object &ob_orig)
|
|
|
|
|
{
|
|
|
|
|
const Object *ob_eval = DEG_get_evaluated_object(&depsgraph, const_cast<Object *>(&ob_orig));
|
|
|
|
|
return get_evaluated_curves_deformation(ob_eval, ob_orig);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 10:20:18 +02:00
|
|
|
GeometryDeformation get_evaluated_grease_pencil_drawing_deformation(const Object *ob_eval,
|
|
|
|
|
const Object &ob_orig,
|
2023-11-02 17:10:59 +01:00
|
|
|
const int layer_index,
|
|
|
|
|
const int frame)
|
2023-06-08 10:20:18 +02:00
|
|
|
{
|
|
|
|
|
BLI_assert(ob_orig.type == OB_GREASE_PENCIL);
|
|
|
|
|
const GreasePencil &grease_pencil_orig = *static_cast<const GreasePencil *>(ob_orig.data);
|
|
|
|
|
|
2023-10-25 10:19:19 +02:00
|
|
|
const Span<const bke::greasepencil::Layer *> layers_orig = grease_pencil_orig.layers();
|
2024-09-23 13:54:02 +02:00
|
|
|
const bke::greasepencil::Layer &layer_orig = grease_pencil_orig.layer(layer_index);
|
2024-05-13 14:53:22 +02:00
|
|
|
const bke::greasepencil::Drawing *drawing_orig = grease_pencil_orig.get_drawing_at(layer_orig,
|
|
|
|
|
frame);
|
|
|
|
|
if (drawing_orig == nullptr) {
|
2023-10-12 15:42:04 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2023-06-08 10:20:18 +02:00
|
|
|
|
|
|
|
|
GeometryDeformation deformation;
|
2023-10-12 15:42:04 +02:00
|
|
|
/* Use the undeformed positions by default. */
|
2024-05-13 14:53:22 +02:00
|
|
|
deformation.positions = drawing_orig->strokes().positions();
|
2023-06-08 10:20:18 +02:00
|
|
|
|
|
|
|
|
if (ob_eval == nullptr) {
|
|
|
|
|
return deformation;
|
|
|
|
|
}
|
2023-11-15 18:46:07 +01:00
|
|
|
const GeometrySet *geometry_eval = ob_eval->runtime->geometry_set_eval;
|
2024-04-22 11:41:31 +02:00
|
|
|
if (geometry_eval == nullptr) {
|
2023-06-08 10:20:18 +02:00
|
|
|
return deformation;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 15:42:04 +02:00
|
|
|
/* If there are edit hints, use the positions of those. */
|
2024-04-22 11:41:31 +02:00
|
|
|
if (geometry_eval->has<GeometryComponentEditData>()) {
|
|
|
|
|
const GeometryComponentEditData &edit_component_eval =
|
|
|
|
|
*geometry_eval->get_component<GeometryComponentEditData>();
|
|
|
|
|
const GreasePencilEditHints *edit_hints = edit_component_eval.grease_pencil_edit_hints_.get();
|
|
|
|
|
if (edit_hints != nullptr && &edit_hints->grease_pencil_id_orig == &grease_pencil_orig &&
|
|
|
|
|
edit_hints->drawing_hints.has_value())
|
|
|
|
|
{
|
|
|
|
|
BLI_assert(edit_hints->drawing_hints->size() == layers_orig.size());
|
|
|
|
|
const GreasePencilDrawingEditHints &drawing_hints =
|
|
|
|
|
edit_hints->drawing_hints.value()[layer_index];
|
|
|
|
|
if (const std::optional<Span<float3>> positions = drawing_hints.positions()) {
|
|
|
|
|
deformation.positions = *positions;
|
|
|
|
|
return deformation;
|
|
|
|
|
}
|
2023-10-12 15:42:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
2023-06-08 10:20:18 +02:00
|
|
|
|
2023-10-12 15:42:04 +02:00
|
|
|
/* Otherwise use the positions of the evaluated drawing if the number of points match. */
|
|
|
|
|
if (const GreasePencilComponent *grease_pencil_component_eval =
|
|
|
|
|
geometry_eval->get_component<GreasePencilComponent>())
|
|
|
|
|
{
|
|
|
|
|
if (const GreasePencil *grease_pencil_eval = grease_pencil_component_eval->get()) {
|
|
|
|
|
Span<const bke::greasepencil::Layer *> layers_eval = grease_pencil_eval->layers();
|
2024-01-16 10:54:58 +01:00
|
|
|
if (layers_eval.size() == layers_orig.size()) {
|
2024-05-13 14:53:22 +02:00
|
|
|
const bke::greasepencil::Layer &layer_eval = *layers_eval[layer_index];
|
|
|
|
|
if (const bke::greasepencil::Drawing *drawing_eval = grease_pencil_eval->get_drawing_at(
|
|
|
|
|
layer_eval, frame))
|
|
|
|
|
if (drawing_eval->strokes().points_num() == drawing_orig->strokes().points_num()) {
|
|
|
|
|
deformation.positions = drawing_eval->strokes().positions();
|
2023-10-25 10:19:19 +02:00
|
|
|
return deformation;
|
|
|
|
|
}
|
2023-10-12 15:42:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-13 14:53:22 +02:00
|
|
|
|
2023-06-08 10:20:18 +02:00
|
|
|
return deformation;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-21 15:12:47 +02:00
|
|
|
GeometryDeformation get_evaluated_grease_pencil_drawing_deformation(const Depsgraph &depsgraph,
|
|
|
|
|
const Object &ob_orig,
|
2023-11-02 17:10:59 +01:00
|
|
|
const int layer_index,
|
|
|
|
|
const int frame)
|
2023-10-21 15:12:47 +02:00
|
|
|
{
|
|
|
|
|
const Object *ob_eval = DEG_get_evaluated_object(&depsgraph, const_cast<Object *>(&ob_orig));
|
2023-11-02 17:10:59 +01:00
|
|
|
return get_evaluated_grease_pencil_drawing_deformation(ob_eval, ob_orig, layer_index, frame);
|
2023-10-21 15:12:47 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-22 15:39:41 +02:00
|
|
|
} // namespace blender::bke::crazyspace
|