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"
|
2011-02-12 17:51:02 +00:00
|
|
|
#include "DNA_meshdata_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_modifier_types.h"
|
|
|
|
|
#include "DNA_object_types.h"
|
|
|
|
|
#include "DNA_scene_types.h"
|
2011-02-12 17:51:02 +00:00
|
|
|
|
2022-05-15 20:27:28 +02:00
|
|
|
#include "BLI_bitmap.h"
|
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
|
|
|
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_DerivedMesh.hh"
|
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"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.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.hh"
|
|
|
|
|
#include "BKE_object_types.hh"
|
2022-01-05 09:53:48 +01:00
|
|
|
#include "BKE_report.h"
|
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. */
|
2022-01-11 15:42:07 +01:00
|
|
|
makeDerivedMesh(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);
|
|
|
|
|
Mesh *mesh_eval_cage = editbmesh_get_eval_cage(
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
{
|
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 */
|
|
|
|
|
const float *vd_prev, *vd_curr, *vd_next; /* deform */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-09-15 11:38:48 +00:00
|
|
|
const int i_prev = BM_elem_index_get(l_iter->prev->v);
|
|
|
|
|
const int i_curr = BM_elem_index_get(l_iter->v);
|
|
|
|
|
const int i_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. */
|
2013-09-15 11:38:48 +00:00
|
|
|
vd_prev = mappedcos[i_prev];
|
|
|
|
|
vd_curr = mappedcos[i_curr];
|
|
|
|
|
vd_next = mappedcos[i_next];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
if (has_origcos) {
|
2013-09-15 11:38:48 +00:00
|
|
|
co_prev = origcos[i_prev];
|
|
|
|
|
co_curr = origcos[i_curr];
|
|
|
|
|
co_next = origcos[i_next];
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-09-15 11:38:48 +00:00
|
|
|
set_crazy_vertex_quat(quats[i_curr], 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
|
|
|
}
|
|
|
|
|
|
2014-05-07 02:59:23 +03:00
|
|
|
void BKE_crazyspace_set_quats_mesh(Mesh *me,
|
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-11-19 18:31:37 +01:00
|
|
|
BitVector<> vert_tag(me->totvert);
|
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 */
|
Mesh: Move positions to a generic attribute
**Changes**
As described in T93602, this patch removes all use of the `MVert`
struct, replacing it with a generic named attribute with the name
`"position"`, consistent with other geometry types.
Variable names have been changed from `verts` to `positions`, to align
with the attribute name and the more generic design (positions are not
vertices, they are just an attribute stored on the point domain).
This change is made possible by previous commits that moved all other
data out of `MVert` to runtime data or other generic attributes. What
remains is mostly a simple type change. Though, the type still shows up
859 times, so the patch is quite large.
One compromise is that now `CD_MASK_BAREMESH` now contains
`CD_PROP_FLOAT3`. With the general move towards generic attributes
over custom data types, we are removing use of these type masks anyway.
**Benefits**
The most obvious benefit is reduced memory usage and the benefits
that brings in memory-bound situations. `float3` is only 3 bytes, in
comparison to `MVert` which was 4. When there are millions of vertices
this starts to matter more.
The other benefits come from using a more generic type. Instead of
writing algorithms specifically for `MVert`, code can just use arrays
of vectors. This will allow eliminating many temporary arrays or
wrappers used to extract positions.
Many possible improvements aren't implemented in this patch, though
I did switch simplify or remove the process of creating temporary
position arrays in a few places.
The design clarity that "positions are just another attribute" brings
allows removing explicit copying of vertices in some procedural
operations-- they are just processed like most other attributes.
**Performance**
This touches so many areas that it's hard to benchmark exhaustively,
but I observed some areas as examples.
* The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster.
* The Spring splash screen went from ~4.3 to ~4.5 fps.
* The subdivision surface modifier/node was slightly faster
RNA access through Python may be slightly slower, since now we need
a name lookup instead of just a custom data type lookup for each index.
**Future Improvements**
* Remove uses of "vert_coords" functions:
* `BKE_mesh_vert_coords_alloc`
* `BKE_mesh_vert_coords_get`
* `BKE_mesh_vert_coords_apply{_with_mat4}`
* Remove more hidden copying of positions
* General simplification now possible in many areas
* Convert more code to C++ to use `float3` instead of `float[3]`
* Currently `reinterpret_cast` is used for those C-API functions
Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
|
|
|
const Span<float3> positions = me->vert_positions();
|
2023-11-19 18:31:37 +01:00
|
|
|
const OffsetIndices<int> faces = me->faces();
|
Mesh: Replace MLoop struct with generic attributes
Implements #102359.
Split the `MLoop` struct into two separate integer arrays called
`corner_verts` and `corner_edges`, referring to the vertex each corner
is attached to and the next edge around the face at each corner. These
arrays can be sliced to give access to the edges or vertices in a face.
Then they are often referred to as "poly_verts" or "poly_edges".
The main benefits are halving the necessary memory bandwidth when only
one array is used and simplifications from using regular integer indices
instead of a special-purpose struct.
The commit also starts a renaming from "loop" to "corner" in mesh code.
Like the other mesh struct of array refactors, forward compatibility is
kept by writing files with the older format. This will be done until 4.0
to ease the transition process.
Looking at a small portion of the patch should give a good impression
for the rest of the changes. I tried to make the changes as small as
possible so it's easy to tell the correctness from the diff. Though I
found Blender developers have been very inventive over the last decade
when finding different ways to loop over the corners in a face.
For performance, nearly every piece of code that deals with `Mesh` is
slightly impacted. Any algorithm that is memory bottle-necked should
see an improvement. For example, here is a comparison of interpolating
a vertex float attribute to face corners (Ryzen 3700x):
**Before** (Average: 3.7 ms, Min: 3.4 ms)
```
threading::parallel_for(loops.index_range(), 4096, [&](IndexRange range) {
for (const int64_t i : range) {
dst[i] = src[loops[i].v];
}
});
```
**After** (Average: 2.9 ms, Min: 2.6 ms)
```
array_utils::gather(src, corner_verts, dst);
```
That's an improvement of 28% to the average timings, and it's also a
simplification, since an index-based routine can be used instead.
For more examples using the new arrays, see the design task.
Pull Request: https://projects.blender.org/blender/blender/pulls/104424
2023-03-20 15:55:13 +01:00
|
|
|
const Span<int> corner_verts = me->corner_verts();
|
2023-12-04 12:28:44 +11:00
|
|
|
const bool has_origcos = !origcos.is_empty();
|
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) {
|
2023-12-04 12:28:44 +11:00
|
|
|
const int i_curr = corner_verts[corner];
|
|
|
|
|
if (vert_tag[i_curr]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
const int i_prev = corner_verts[mesh::face_corner_prev(face, corner)];
|
|
|
|
|
const int i_next = corner_verts[mesh::face_corner_next(face, corner)];
|
2023-11-19 18:31:37 +01:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
const float *co_prev, *co_curr, *co_next; /* orig */
|
|
|
|
|
const float *vd_prev, *vd_curr, *vd_next; /* deform */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
/* Retrieve mapped coordinates. */
|
|
|
|
|
vd_prev = mappedcos[i_prev];
|
|
|
|
|
vd_curr = mappedcos[i_curr];
|
|
|
|
|
vd_next = mappedcos[i_next];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
if (has_origcos) {
|
|
|
|
|
co_prev = origcos[i_prev];
|
|
|
|
|
co_curr = origcos[i_curr];
|
|
|
|
|
co_next = origcos[i_next];
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
co_prev = positions[i_prev];
|
|
|
|
|
co_curr = positions[i_curr];
|
|
|
|
|
co_next = positions[i_next];
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
set_crazy_vertex_quat(quats[i_curr], co_curr, co_next, co_prev, vd_curr, vd_next, vd_prev);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-04 12:28:44 +11:00
|
|
|
vert_tag[i_curr].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);
|
|
|
|
|
Mesh *me = 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
|
|
|
|
2022-07-22 12:33:08 +02:00
|
|
|
if (!editbmesh_modifier_is_enabled(scene, ob, md, me != 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
|
|
|
|
2023-07-07 13:07:15 +02:00
|
|
|
me = BKE_mesh_wrapper_from_editmesh(em, &cd_mask_extra, me_input);
|
2023-11-16 18:29:52 +01:00
|
|
|
deformcos.reinitialize(verts_num);
|
|
|
|
|
BKE_mesh_wrapper_vert_coords_copy(me, deformcos);
|
|
|
|
|
deformmats.reinitialize(verts_num);
|
|
|
|
|
deformmats.fill(blender::float3x3::identity());
|
2011-02-12 17:51:02 +00:00
|
|
|
}
|
2023-11-16 18:29:52 +01:00
|
|
|
mti->deform_matrices_EM(md, &mectx, em, me, 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++) {
|
2022-07-22 12:33:08 +02:00
|
|
|
if (editbmesh_modifier_is_enabled(scene, ob, md, me != 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
|
|
|
|
2018-10-09 15:37:10 +11:00
|
|
|
if (me) {
|
2022-07-22 12:33:08 +02:00
|
|
|
BKE_id_free(nullptr, me);
|
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;
|
2022-07-22 12:33:08 +02:00
|
|
|
Mesh *me_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. */
|
2022-07-22 12:33:08 +02:00
|
|
|
Mesh *me = static_cast<Mesh *>(object_eval.data);
|
2023-04-19 15:49:13 -04:00
|
|
|
me_eval = BKE_mesh_copy_for_eval(me);
|
2023-11-16 18:29:52 +01:00
|
|
|
deformcos = me->vert_positions();
|
|
|
|
|
deformmats.reinitialize(me->totvert);
|
|
|
|
|
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) {
|
2023-11-16 18:29:52 +01:00
|
|
|
mti->deform_matrices(md, &mectx, me_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
|
|
|
|
2022-07-22 12:33:08 +02:00
|
|
|
if (me_eval != nullptr) {
|
|
|
|
|
BKE_id_free(nullptr, me_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();
|
|
|
|
|
deformmats.reinitialize(mesh->totvert);
|
|
|
|
|
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);
|
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) {
|
2023-04-19 15:49:13 -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
|
|
|
}
|
|
|
|
|
|
2022-07-22 12:33:08 +02:00
|
|
|
quats = static_cast<float(*)[4]>(MEM_mallocN(mesh->totvert * 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
|
|
|
|
2018-12-05 16:53:45 +01:00
|
|
|
for (i = 0; i < mesh->totvert; 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();
|
|
|
|
|
deformmats.reinitialize(mesh->totvert);
|
|
|
|
|
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) {
|
|
|
|
|
if (edit_hints->positions.has_value()) {
|
|
|
|
|
BLI_assert(edit_hints->positions->size() == points_num);
|
|
|
|
|
deformation.positions = *edit_hints->positions;
|
|
|
|
|
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();
|
|
|
|
|
BLI_assert(layer_index >= 0 && layer_index < layers_orig.size());
|
2023-11-02 17:10:59 +01:00
|
|
|
const int drawing_index = layers_orig[layer_index]->drawing_index_at(frame);
|
2023-10-12 15:42:04 +02:00
|
|
|
if (drawing_index == -1) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-09-20 08:39:30 -04:00
|
|
|
const GreasePencilDrawingBase *drawing_base = grease_pencil_orig.drawing(drawing_index);
|
2023-10-12 15:42:04 +02:00
|
|
|
if (drawing_base->type != GP_DRAWING) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
const bke::greasepencil::Drawing &drawing_orig =
|
|
|
|
|
reinterpret_cast<const GreasePencilDrawing *>(drawing_base)->wrap();
|
2023-06-08 10:20:18 +02:00
|
|
|
|
|
|
|
|
GeometryDeformation deformation;
|
2023-10-12 15:42:04 +02:00
|
|
|
/* Use the undeformed positions by default. */
|
|
|
|
|
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;
|
2023-10-12 15:42:04 +02:00
|
|
|
if (geometry_eval == nullptr || !geometry_eval->has<GeometryComponentEditData>()) {
|
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. */
|
|
|
|
|
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())
|
|
|
|
|
{
|
2023-10-25 10:19:19 +02:00
|
|
|
BLI_assert(edit_hints->drawing_hints->size() == layers_orig.size());
|
2023-10-12 15:42:04 +02:00
|
|
|
const GreasePencilDrawingEditHints &drawing_hints =
|
|
|
|
|
edit_hints->drawing_hints.value()[layer_index];
|
|
|
|
|
if (drawing_hints.positions.has_value()) {
|
|
|
|
|
deformation.positions = *drawing_hints.positions;
|
2023-10-25 10:19:19 +02:00
|
|
|
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();
|
2023-10-25 10:19:19 +02:00
|
|
|
if (layers_eval.size() != layers_orig.size()) {
|
|
|
|
|
const bke::greasepencil::Layer *layer_eval = layers_eval[layer_index];
|
2023-11-02 17:10:59 +01:00
|
|
|
const int drawing_index_eval = layer_eval->drawing_index_at(frame);
|
2023-10-25 10:19:19 +02:00
|
|
|
if (drawing_index_eval != -1) {
|
|
|
|
|
const GreasePencilDrawingBase *drawing_base_eval = grease_pencil_eval->drawing(
|
|
|
|
|
drawing_index_eval);
|
|
|
|
|
if (drawing_base_eval->type != GP_DRAWING) {
|
|
|
|
|
return deformation;
|
|
|
|
|
}
|
|
|
|
|
const bke::greasepencil::Drawing &drawing_eval =
|
|
|
|
|
reinterpret_cast<const GreasePencilDrawing *>(drawing_base_eval)->wrap();
|
|
|
|
|
if (drawing_eval.strokes().points_num() == drawing_orig.strokes().points_num()) {
|
|
|
|
|
deformation.positions = drawing_eval.strokes().positions();
|
|
|
|
|
return deformation;
|
|
|
|
|
}
|
2023-10-12 15:42:04 +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
|