2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2019 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2019-08-14 18:37:46 +02:00
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* \ingroup edobj
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
#include <cctype>
|
|
|
|
|
#include <cfloat>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cstring>
|
2019-08-14 18:37:46 +02:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.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"
|
2021-07-30 22:28:25 -04:00
|
|
|
#include "BLI_string.h"
|
|
|
|
|
#include "BLI_string_utf8.h"
|
2019-08-14 18:37:46 +02:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
|
|
|
|
#include "DNA_mesh_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
#include "DNA_object_types.h"
|
2020-03-25 17:39:48 +01:00
|
|
|
#include "DNA_userdef_types.h"
|
|
|
|
|
|
|
|
|
|
#include "BLT_translation.h"
|
2019-08-14 18:37:46 +02:00
|
|
|
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
#include "BKE_attribute.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_context.hh"
|
|
|
|
|
#include "BKE_customdata.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
#include "BKE_global.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2023-12-01 19:43:16 +01:00
|
|
|
#include "BKE_main.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_mesh_mirror.hh"
|
|
|
|
|
#include "BKE_mesh_remesh_voxel.hh"
|
|
|
|
|
#include "BKE_mesh_runtime.hh"
|
2023-11-14 09:30:40 +01:00
|
|
|
#include "BKE_modifier.hh"
|
2023-10-09 23:41:53 +02:00
|
|
|
#include "BKE_object.hh"
|
2023-08-02 22:14:18 +02:00
|
|
|
#include "BKE_paint.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
#include "BKE_report.h"
|
|
|
|
|
#include "BKE_scene.h"
|
2023-11-28 16:05:12 -05:00
|
|
|
#include "BKE_shrinkwrap.hh"
|
|
|
|
|
#include "BKE_unit.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
|
2023-09-22 03:18:17 +02:00
|
|
|
#include "DEG_depsgraph.hh"
|
|
|
|
|
#include "DEG_depsgraph_build.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "ED_mesh.hh"
|
|
|
|
|
#include "ED_object.hh"
|
2023-08-04 23:11:22 +02:00
|
|
|
#include "ED_screen.hh"
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "ED_sculpt.hh"
|
|
|
|
|
#include "ED_space_api.hh"
|
|
|
|
|
#include "ED_undo.hh"
|
|
|
|
|
#include "ED_view3d.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
|
2023-08-10 22:40:27 +02:00
|
|
|
#include "RNA_access.hh"
|
|
|
|
|
#include "RNA_define.hh"
|
|
|
|
|
#include "RNA_enum_types.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
|
2020-03-25 17:39:48 +01:00
|
|
|
#include "GPU_immediate.h"
|
|
|
|
|
#include "GPU_immediate_util.h"
|
|
|
|
|
#include "GPU_matrix.h"
|
|
|
|
|
#include "GPU_state.h"
|
|
|
|
|
|
2023-08-04 23:11:22 +02:00
|
|
|
#include "WM_api.hh"
|
|
|
|
|
#include "WM_message.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
#include "WM_toolsystem.h"
|
2023-08-04 23:11:22 +02:00
|
|
|
#include "WM_types.hh"
|
2019-08-14 18:37:46 +02:00
|
|
|
|
2023-08-05 02:57:52 +02:00
|
|
|
#include "UI_interface.hh"
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
#include "BLF_api.h"
|
|
|
|
|
|
2019-08-14 18:37:46 +02:00
|
|
|
#include "object_intern.h" /* own include */
|
|
|
|
|
|
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
|
|
|
using blender::float3;
|
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 blender::IndexRange;
|
|
|
|
|
using blender::Span;
|
|
|
|
|
|
2020-03-04 10:56:56 +11:00
|
|
|
/* TODO(sebpa): unstable, can lead to unrecoverable errors. */
|
|
|
|
|
// #define USE_MESH_CURVATURE
|
|
|
|
|
|
2020-04-01 10:53:46 +11:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Voxel Remesh Operator
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2019-08-14 18:37:46 +02:00
|
|
|
static bool object_remesh_poll(bContext *C)
|
|
|
|
|
{
|
|
|
|
|
Object *ob = CTX_data_active_object(C);
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
if (ob == nullptr || ob->data == nullptr) {
|
2020-06-30 17:51:41 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ID_IS_LINKED(ob) || ID_IS_LINKED(ob->data) || ID_IS_OVERRIDE_LIBRARY(ob->data)) {
|
2022-12-09 15:41:05 -06:00
|
|
|
CTX_wm_operator_poll_msg_set(C, "The remesher cannot work on linked or override data");
|
2019-09-17 17:08:51 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 18:37:46 +02:00
|
|
|
if (BKE_object_is_in_editmode(ob)) {
|
2020-02-17 13:00:01 +01:00
|
|
|
CTX_wm_operator_poll_msg_set(C, "The remesher cannot run from edit mode");
|
2019-08-14 18:37:46 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ob->mode == OB_MODE_SCULPT && ob->sculpt->bm) {
|
2020-02-17 13:00:01 +01:00
|
|
|
CTX_wm_operator_poll_msg_set(C, "The remesher cannot run with dyntopo activated");
|
2019-09-18 16:55:08 +02:00
|
|
|
return false;
|
2019-08-14 18:37:46 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-08 10:14:02 +02:00
|
|
|
if (BKE_modifiers_uses_multires(ob)) {
|
2019-10-01 22:30:14 +02:00
|
|
|
CTX_wm_operator_poll_msg_set(
|
2020-02-17 13:00:01 +01:00
|
|
|
C, "The remesher cannot run with a Multires modifier in the modifier stack");
|
2019-10-01 22:30:14 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 18:37:46 +02:00
|
|
|
return ED_operator_object_active_editable_mesh(C);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int voxel_remesh_exec(bContext *C, wmOperator *op)
|
|
|
|
|
{
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
using namespace blender;
|
2019-08-14 18:37:46 +02:00
|
|
|
Object *ob = CTX_data_active_object(C);
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2019-08-14 18:37:46 +02:00
|
|
|
|
|
|
|
|
if (mesh->remesh_voxel_size <= 0.0f) {
|
2020-02-17 13:00:01 +01:00
|
|
|
BKE_report(op->reports, RPT_ERROR, "Voxel remesher cannot run with a voxel size of 0.0");
|
2019-08-14 18:37:46 +02:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
if (mesh->faces_num == 0) {
|
2021-06-23 18:42:20 +02:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-07 18:33:28 +02:00
|
|
|
float isovalue = 0.0f;
|
|
|
|
|
if (mesh->flag & ME_REMESH_REPROJECT_VOLUME) {
|
|
|
|
|
isovalue = mesh->remesh_voxel_size * 0.3f;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 23:26:35 -04:00
|
|
|
Mesh *new_mesh = BKE_mesh_remesh_voxel(
|
2019-10-07 18:33:28 +02:00
|
|
|
mesh, mesh->remesh_voxel_size, mesh->remesh_voxel_adaptivity, isovalue);
|
2019-08-14 18:37:46 +02:00
|
|
|
|
|
|
|
|
if (!new_mesh) {
|
2020-02-17 13:00:01 +01:00
|
|
|
BKE_report(op->reports, RPT_ERROR, "Voxel remesher failed to create mesh");
|
2019-08-14 18:37:46 +02:00
|
|
|
return OPERATOR_CANCELLED;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 14:50:15 +11:00
|
|
|
if (ob->mode == OB_MODE_SCULPT) {
|
2022-08-15 14:52:02 -07:00
|
|
|
ED_sculpt_undo_geometry_begin(ob, op);
|
2019-11-20 14:50:15 +11:00
|
|
|
}
|
|
|
|
|
|
2019-09-27 17:41:05 +02:00
|
|
|
if (mesh->flag & ME_REMESH_FIX_POLES && mesh->remesh_voxel_adaptivity <= 0.0f) {
|
2021-07-30 23:26:35 -04:00
|
|
|
Mesh *mesh_fixed_poles = BKE_mesh_remesh_voxel_fix_poles(new_mesh);
|
|
|
|
|
BKE_id_free(nullptr, new_mesh);
|
|
|
|
|
new_mesh = mesh_fixed_poles;
|
2019-08-14 18:37:46 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-05 14:53:23 +01:00
|
|
|
if (mesh->flag & ME_REMESH_REPROJECT_VOLUME) {
|
2019-09-26 16:28:56 +02:00
|
|
|
BKE_shrinkwrap_remesh_target_project(new_mesh, mesh, ob);
|
|
|
|
|
}
|
2019-08-14 18:37:46 +02:00
|
|
|
|
2023-11-22 15:21:58 +01:00
|
|
|
if (mesh->flag & ME_REMESH_REPROJECT_ATTRIBUTES) {
|
|
|
|
|
bke::mesh_remesh_reproject_attributes(*mesh, *new_mesh);
|
Sculpt Vertex Colors: Initial implementation
Sculpt Vertex Colors is a painting system that runs inside sculpt mode, reusing all its tools and optimizations. This provides much better performance, easier to maintain code and more advanced features (new brush engine, filters, symmetry options, masks and face sets compatibility...). This is also the initial step for future features like vertex painting in Multires and brushes that can sculpt and paint at the same time.
This commit includes:
- SCULPT_UNDO_COLOR for undo support in sculpt mode
- SCULPT_UPDATE_COLOR and PBVH flags and rendering
- Sculpt Color API functions
- Sculpt capability for sculpt tools (only enabled in the Paint Brush for now)
- Rendering support in workbench (default to Sculpt Vertex Colors except in Vertex Paint)
- Conversion operator between MPropCol (Sculpt Vertex Colors) and MLoopCol (Vertex Paint)
- Remesher reprojection in the Voxel Remehser
- Paint Brush and Smear Brush with color smoothing in alt-smooth mode
- Parameters for the new brush engine (density, opacity, flow, wet paint mixing, tip scale) implemented in Sculpt Vertex Colors
- Color Filter
- Color picker (uses S shortcut, replaces smooth)
- Color selector in the top bar
Reviewed By: brecht
Maniphest Tasks: T72866
Differential Revision: https://developer.blender.org/D5975
2020-06-22 20:05:28 +02:00
|
|
|
}
|
2023-11-28 12:45:14 -05:00
|
|
|
else {
|
|
|
|
|
const VArray<bool> sharp_face = *mesh->attributes().lookup_or_default<bool>(
|
|
|
|
|
"sharp_face", ATTR_DOMAIN_FACE, false);
|
|
|
|
|
BKE_mesh_smooth_flag_set(new_mesh, !sharp_face[0]);
|
|
|
|
|
}
|
Sculpt Vertex Colors: Initial implementation
Sculpt Vertex Colors is a painting system that runs inside sculpt mode, reusing all its tools and optimizations. This provides much better performance, easier to maintain code and more advanced features (new brush engine, filters, symmetry options, masks and face sets compatibility...). This is also the initial step for future features like vertex painting in Multires and brushes that can sculpt and paint at the same time.
This commit includes:
- SCULPT_UNDO_COLOR for undo support in sculpt mode
- SCULPT_UPDATE_COLOR and PBVH flags and rendering
- Sculpt Color API functions
- Sculpt capability for sculpt tools (only enabled in the Paint Brush for now)
- Rendering support in workbench (default to Sculpt Vertex Colors except in Vertex Paint)
- Conversion operator between MPropCol (Sculpt Vertex Colors) and MLoopCol (Vertex Paint)
- Remesher reprojection in the Voxel Remehser
- Paint Brush and Smear Brush with color smoothing in alt-smooth mode
- Parameters for the new brush engine (density, opacity, flow, wet paint mixing, tip scale) implemented in Sculpt Vertex Colors
- Color Filter
- Color picker (uses S shortcut, replaces smooth)
- Color selector in the top bar
Reviewed By: brecht
Maniphest Tasks: T72866
Differential Revision: https://developer.blender.org/D5975
2020-06-22 20:05:28 +02:00
|
|
|
|
2022-09-09 08:24:31 -05:00
|
|
|
BKE_mesh_nomain_to_mesh(new_mesh, mesh, ob);
|
2019-09-26 16:28:56 +02:00
|
|
|
|
2019-08-14 18:37:46 +02:00
|
|
|
if (ob->mode == OB_MODE_SCULPT) {
|
|
|
|
|
ED_sculpt_undo_geometry_end(ob);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
BKE_mesh_batch_cache_dirty_tag(static_cast<Mesh *>(ob->data), BKE_MESH_BATCH_DIRTY_ALL);
|
2019-08-14 18:37:46 +02:00
|
|
|
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
|
|
|
|
|
WM_event_add_notifier(C, NC_GEOM | ND_DATA, ob->data);
|
|
|
|
|
|
|
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OBJECT_OT_voxel_remesh(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
|
|
|
|
ot->name = "Voxel Remesh";
|
|
|
|
|
ot->description =
|
|
|
|
|
"Calculates a new manifold mesh based on the volume of the current mesh. All data layers "
|
|
|
|
|
"will be lost";
|
|
|
|
|
ot->idname = "OBJECT_OT_voxel_remesh";
|
|
|
|
|
|
|
|
|
|
/* api callbacks */
|
|
|
|
|
ot->poll = object_remesh_poll;
|
|
|
|
|
ot->exec = voxel_remesh_exec;
|
|
|
|
|
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
}
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2020-04-01 10:53:46 +11:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Voxel Size Operator
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2020-03-25 17:39:48 +01:00
|
|
|
#define VOXEL_SIZE_EDIT_MAX_GRIDS_LINES 500
|
|
|
|
|
#define VOXEL_SIZE_EDIT_MAX_STR_LEN 20
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
struct VoxelSizeEditCustomData {
|
2020-03-25 17:39:48 +01:00
|
|
|
void *draw_handle;
|
|
|
|
|
Object *active_object;
|
|
|
|
|
|
|
|
|
|
float init_mval[2];
|
|
|
|
|
float slow_mval[2];
|
|
|
|
|
|
|
|
|
|
bool slow_mode;
|
|
|
|
|
|
|
|
|
|
float init_voxel_size;
|
|
|
|
|
float slow_voxel_size;
|
|
|
|
|
float voxel_size;
|
|
|
|
|
|
|
|
|
|
float preview_plane[4][3];
|
|
|
|
|
|
|
|
|
|
float text_mat[4][4];
|
2021-07-30 13:15:01 -04:00
|
|
|
};
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
static void voxel_size_parallel_lines_draw(uint pos3d,
|
|
|
|
|
const float initial_co[3],
|
|
|
|
|
const float end_co[3],
|
|
|
|
|
const float length_co[3],
|
|
|
|
|
const float spacing)
|
|
|
|
|
{
|
|
|
|
|
const float total_len = len_v3v3(initial_co, end_co);
|
2022-09-25 18:33:28 +10:00
|
|
|
const int tot_lines = int(total_len / spacing);
|
2020-03-25 17:39:48 +01:00
|
|
|
const int tot_lines_half = (tot_lines / 2) + 1;
|
|
|
|
|
float spacing_dir[3], lines_start[3];
|
|
|
|
|
float line_dir[3];
|
|
|
|
|
sub_v3_v3v3(spacing_dir, end_co, initial_co);
|
|
|
|
|
normalize_v3(spacing_dir);
|
|
|
|
|
|
|
|
|
|
sub_v3_v3v3(line_dir, length_co, initial_co);
|
|
|
|
|
|
|
|
|
|
if (tot_lines > VOXEL_SIZE_EDIT_MAX_GRIDS_LINES || tot_lines <= 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mid_v3_v3v3(lines_start, initial_co, end_co);
|
|
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
immBegin(GPU_PRIM_LINES, uint(tot_lines_half) * 2);
|
2020-03-25 17:39:48 +01:00
|
|
|
for (int i = 0; i < tot_lines_half; i++) {
|
|
|
|
|
float line_start[3];
|
|
|
|
|
float line_end[3];
|
|
|
|
|
madd_v3_v3v3fl(line_start, lines_start, spacing_dir, spacing * i);
|
|
|
|
|
add_v3_v3v3(line_end, line_start, line_dir);
|
|
|
|
|
immVertex3fv(pos3d, line_start);
|
|
|
|
|
immVertex3fv(pos3d, line_end);
|
|
|
|
|
}
|
|
|
|
|
immEnd();
|
|
|
|
|
|
|
|
|
|
mul_v3_fl(spacing_dir, -1.0f);
|
|
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
immBegin(GPU_PRIM_LINES, uint(tot_lines_half - 1) * 2);
|
2020-03-25 17:39:48 +01:00
|
|
|
for (int i = 1; i < tot_lines_half; i++) {
|
|
|
|
|
float line_start[3];
|
|
|
|
|
float line_end[3];
|
|
|
|
|
madd_v3_v3v3fl(line_start, lines_start, spacing_dir, spacing * i);
|
|
|
|
|
add_v3_v3v3(line_end, line_start, line_dir);
|
|
|
|
|
immVertex3fv(pos3d, line_start);
|
|
|
|
|
immVertex3fv(pos3d, line_end);
|
|
|
|
|
}
|
|
|
|
|
immEnd();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-08 16:58:37 +11:00
|
|
|
static void voxel_size_edit_draw(const bContext *C, ARegion * /*region*/, void *arg)
|
2020-03-25 17:39:48 +01:00
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
VoxelSizeEditCustomData *cd = static_cast<VoxelSizeEditCustomData *>(arg);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
2020-08-16 15:38:34 +02:00
|
|
|
GPU_blend(GPU_BLEND_ALPHA);
|
2020-03-25 17:39:48 +01:00
|
|
|
GPU_line_smooth(true);
|
|
|
|
|
|
|
|
|
|
uint pos3d = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
|
|
|
|
|
immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
|
|
|
|
|
GPU_matrix_push();
|
2022-10-24 14:16:37 +02:00
|
|
|
GPU_matrix_mul(cd->active_object->object_to_world);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
/* Draw Rect */
|
|
|
|
|
immUniformColor4f(0.9f, 0.9f, 0.9f, 0.8f);
|
|
|
|
|
GPU_line_width(3.0f);
|
|
|
|
|
|
|
|
|
|
immBegin(GPU_PRIM_LINES, 8);
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[0]);
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[1]);
|
|
|
|
|
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[1]);
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[2]);
|
|
|
|
|
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[2]);
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[3]);
|
|
|
|
|
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[3]);
|
|
|
|
|
immVertex3fv(pos3d, cd->preview_plane[0]);
|
|
|
|
|
immEnd();
|
|
|
|
|
|
|
|
|
|
/* Draw Grid */
|
|
|
|
|
GPU_line_width(1.0f);
|
|
|
|
|
|
|
|
|
|
const float total_len = len_v3v3(cd->preview_plane[0], cd->preview_plane[1]);
|
2022-09-25 18:33:28 +10:00
|
|
|
const int tot_lines = int(total_len / cd->voxel_size);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
2021-02-05 16:23:34 +11:00
|
|
|
/* Smooth-step to reduce the alpha of the grid as the line number increases. */
|
2020-03-25 17:39:48 +01:00
|
|
|
const float a = VOXEL_SIZE_EDIT_MAX_GRIDS_LINES * 0.1f;
|
|
|
|
|
const float b = VOXEL_SIZE_EDIT_MAX_GRIDS_LINES;
|
|
|
|
|
const float x = clamp_f((tot_lines - a) / (b - a), 0.0f, 1.0);
|
|
|
|
|
const float alpha_factor = 1.0f - (x * x * (3.0f - 2.0f * x));
|
|
|
|
|
|
|
|
|
|
immUniformColor4f(0.9f, 0.9f, 0.9f, 0.75f * alpha_factor);
|
|
|
|
|
voxel_size_parallel_lines_draw(
|
|
|
|
|
pos3d, cd->preview_plane[0], cd->preview_plane[1], cd->preview_plane[3], cd->voxel_size);
|
|
|
|
|
voxel_size_parallel_lines_draw(
|
|
|
|
|
pos3d, cd->preview_plane[1], cd->preview_plane[2], cd->preview_plane[0], cd->voxel_size);
|
|
|
|
|
|
|
|
|
|
/* Draw text */
|
2020-04-01 10:41:34 +11:00
|
|
|
const uiStyle *style = UI_style_get();
|
2020-03-25 17:39:48 +01:00
|
|
|
const uiFontStyle *fstyle = &style->widget;
|
|
|
|
|
const int fontid = fstyle->uifont_id;
|
|
|
|
|
float strwidth, strheight;
|
|
|
|
|
short fstyle_points = fstyle->points;
|
|
|
|
|
char str[VOXEL_SIZE_EDIT_MAX_STR_LEN];
|
|
|
|
|
short strdrawlen = 0;
|
2022-04-22 08:08:15 +02:00
|
|
|
Scene *scene = CTX_data_scene(C);
|
|
|
|
|
UnitSettings *unit = &scene->unit;
|
2022-04-24 13:37:01 +10:00
|
|
|
BKE_unit_value_as_string(str,
|
|
|
|
|
VOXEL_SIZE_EDIT_MAX_STR_LEN,
|
2022-09-25 18:33:28 +10:00
|
|
|
double(cd->voxel_size * unit->scale_length),
|
2022-06-08 11:51:29 -07:00
|
|
|
-3,
|
2022-04-24 13:37:01 +10:00
|
|
|
B_UNIT_LENGTH,
|
|
|
|
|
unit,
|
|
|
|
|
true);
|
2020-03-25 17:39:48 +01:00
|
|
|
strdrawlen = BLI_strlen_utf8(str);
|
|
|
|
|
|
|
|
|
|
immUnbindProgram();
|
|
|
|
|
|
|
|
|
|
GPU_matrix_push();
|
|
|
|
|
GPU_matrix_mul(cd->text_mat);
|
2023-03-17 04:19:05 +01:00
|
|
|
BLF_size(fontid, 10.0f * fstyle_points * UI_SCALE_FAC);
|
2020-03-25 17:39:48 +01:00
|
|
|
BLF_color3f(fontid, 1.0f, 1.0f, 1.0f);
|
|
|
|
|
BLF_width_and_height(fontid, str, strdrawlen, &strwidth, &strheight);
|
|
|
|
|
BLF_position(fontid, -0.5f * strwidth, -0.5f * strheight, 0.0f);
|
|
|
|
|
BLF_draw(fontid, str, strdrawlen);
|
|
|
|
|
GPU_matrix_pop();
|
|
|
|
|
|
|
|
|
|
GPU_matrix_pop();
|
|
|
|
|
|
2020-08-16 15:38:34 +02:00
|
|
|
GPU_blend(GPU_BLEND_NONE);
|
2020-03-25 17:39:48 +01:00
|
|
|
GPU_line_smooth(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void voxel_size_edit_cancel(bContext *C, wmOperator *op)
|
|
|
|
|
{
|
2020-11-29 18:10:47 -05:00
|
|
|
ARegion *region = CTX_wm_region(C);
|
2021-07-30 13:15:01 -04:00
|
|
|
VoxelSizeEditCustomData *cd = static_cast<VoxelSizeEditCustomData *>(op->customdata);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
2020-11-29 18:10:47 -05:00
|
|
|
ED_region_draw_cb_exit(region->type, cd->draw_handle);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
MEM_freeN(op->customdata);
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
ED_workspace_status_text(C, nullptr);
|
2020-03-25 17:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int voxel_size_edit_modal(bContext *C, wmOperator *op, const wmEvent *event)
|
|
|
|
|
{
|
2020-11-29 18:10:47 -05:00
|
|
|
ARegion *region = CTX_wm_region(C);
|
2021-07-30 13:15:01 -04:00
|
|
|
VoxelSizeEditCustomData *cd = static_cast<VoxelSizeEditCustomData *>(op->customdata);
|
2020-03-25 17:39:48 +01:00
|
|
|
Object *active_object = cd->active_object;
|
|
|
|
|
Mesh *mesh = (Mesh *)active_object->data;
|
|
|
|
|
|
|
|
|
|
/* Cancel modal operator */
|
|
|
|
|
if ((event->type == EVT_ESCKEY && event->val == KM_PRESS) ||
|
|
|
|
|
(event->type == RIGHTMOUSE && event->val == KM_PRESS))
|
|
|
|
|
{
|
|
|
|
|
voxel_size_edit_cancel(C, op);
|
2020-11-29 18:10:47 -05:00
|
|
|
ED_region_tag_redraw(region);
|
2020-03-25 17:39:48 +01:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Finish modal operator */
|
|
|
|
|
if ((event->type == LEFTMOUSE && event->val == KM_RELEASE) ||
|
|
|
|
|
(event->type == EVT_RETKEY && event->val == KM_PRESS) ||
|
|
|
|
|
(event->type == EVT_PADENTER && event->val == KM_PRESS))
|
|
|
|
|
{
|
2020-11-29 18:10:47 -05:00
|
|
|
ED_region_draw_cb_exit(region->type, cd->draw_handle);
|
2020-03-25 17:39:48 +01:00
|
|
|
mesh->remesh_voxel_size = cd->voxel_size;
|
|
|
|
|
MEM_freeN(op->customdata);
|
2020-11-29 18:10:47 -05:00
|
|
|
ED_region_tag_redraw(region);
|
2021-07-30 13:15:01 -04:00
|
|
|
ED_workspace_status_text(C, nullptr);
|
2020-03-25 17:39:48 +01:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
const float mval[2] = {float(event->mval[0]), float(event->mval[1])};
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
float d = cd->init_mval[0] - mval[0];
|
|
|
|
|
|
|
|
|
|
if (cd->slow_mode) {
|
|
|
|
|
d = cd->slow_mval[0] - mval[0];
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 22:48:34 +11:00
|
|
|
if (event->modifier & KM_CTRL) {
|
2020-04-03 12:38:04 +11:00
|
|
|
/* Multiply d by the initial voxel size to prevent uncontrollable speeds when using low voxel
|
2020-03-25 17:39:48 +01:00
|
|
|
* sizes. */
|
|
|
|
|
/* When the voxel size is slower, it needs more precision. */
|
|
|
|
|
d = d * min_ff(pow2f(cd->init_voxel_size), 0.1f) * 0.05f;
|
|
|
|
|
}
|
2022-06-22 01:36:13 -07:00
|
|
|
else {
|
|
|
|
|
/* Linear mode, enables jumping to any voxel size. */
|
|
|
|
|
d = d * 0.0005f;
|
|
|
|
|
}
|
2020-03-25 17:39:48 +01:00
|
|
|
if (cd->slow_mode) {
|
|
|
|
|
cd->voxel_size = cd->slow_voxel_size + d * 0.05f;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
cd->voxel_size = cd->init_voxel_size + d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event->type == EVT_LEFTSHIFTKEY && event->val == KM_PRESS) {
|
|
|
|
|
cd->slow_mode = true;
|
|
|
|
|
copy_v2_v2(cd->slow_mval, mval);
|
|
|
|
|
cd->slow_voxel_size = cd->voxel_size;
|
|
|
|
|
}
|
|
|
|
|
if (event->type == EVT_LEFTSHIFTKEY && event->val == KM_RELEASE) {
|
|
|
|
|
cd->slow_mode = false;
|
|
|
|
|
cd->slow_voxel_size = 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cd->voxel_size = clamp_f(cd->voxel_size, 0.0001f, 1.0f);
|
|
|
|
|
|
2020-11-29 18:10:47 -05:00
|
|
|
ED_region_tag_redraw(region);
|
2020-03-25 17:39:48 +01:00
|
|
|
return OPERATOR_RUNNING_MODAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int voxel_size_edit_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
|
|
|
|
{
|
2020-11-29 18:10:47 -05:00
|
|
|
ARegion *region = CTX_wm_region(C);
|
2020-03-25 17:39:48 +01:00
|
|
|
Object *active_object = CTX_data_active_object(C);
|
|
|
|
|
Mesh *mesh = (Mesh *)active_object->data;
|
|
|
|
|
|
2021-12-24 22:17:49 -05:00
|
|
|
VoxelSizeEditCustomData *cd = MEM_cnew<VoxelSizeEditCustomData>(
|
|
|
|
|
"Voxel Size Edit OP Custom Data");
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
/* Initial operator Custom Data setup. */
|
|
|
|
|
cd->draw_handle = ED_region_draw_cb_activate(
|
2020-11-29 18:10:47 -05:00
|
|
|
region->type, voxel_size_edit_draw, cd, REGION_DRAW_POST_VIEW);
|
2020-03-25 17:39:48 +01:00
|
|
|
cd->active_object = active_object;
|
|
|
|
|
cd->init_mval[0] = event->mval[0];
|
|
|
|
|
cd->init_mval[1] = event->mval[1];
|
|
|
|
|
cd->init_voxel_size = mesh->remesh_voxel_size;
|
|
|
|
|
cd->voxel_size = mesh->remesh_voxel_size;
|
|
|
|
|
op->customdata = cd;
|
|
|
|
|
|
2021-02-14 20:58:04 +11:00
|
|
|
/* Select the front facing face of the mesh bounding box. */
|
2023-10-10 09:31:20 +02:00
|
|
|
const blender::Bounds<float3> bounds = *mesh->bounds_min_max();
|
|
|
|
|
BoundBox bb;
|
|
|
|
|
BKE_boundbox_init_from_minmax(&bb, bounds.min, bounds.max);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
/* Indices of the Bounding Box faces. */
|
2020-08-07 22:36:11 +10:00
|
|
|
const int BB_faces[6][4] = {
|
2020-03-25 17:39:48 +01:00
|
|
|
{3, 0, 4, 7},
|
|
|
|
|
{1, 2, 6, 5},
|
|
|
|
|
{3, 2, 1, 0},
|
|
|
|
|
{4, 5, 6, 7},
|
|
|
|
|
{0, 1, 5, 4},
|
|
|
|
|
{2, 3, 7, 6},
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-10 09:31:20 +02:00
|
|
|
copy_v3_v3(cd->preview_plane[0], bb.vec[BB_faces[0][0]]);
|
|
|
|
|
copy_v3_v3(cd->preview_plane[1], bb.vec[BB_faces[0][1]]);
|
|
|
|
|
copy_v3_v3(cd->preview_plane[2], bb.vec[BB_faces[0][2]]);
|
|
|
|
|
copy_v3_v3(cd->preview_plane[3], bb.vec[BB_faces[0][3]]);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
RegionView3D *rv3d = CTX_wm_region_view3d(C);
|
|
|
|
|
|
|
|
|
|
float mat[3][3];
|
|
|
|
|
float current_normal[3];
|
|
|
|
|
float view_normal[3] = {0.0f, 0.0f, 1.0f};
|
|
|
|
|
|
|
|
|
|
/* Calculate the view normal. */
|
2022-11-02 14:41:49 +01:00
|
|
|
invert_m4_m4(active_object->world_to_object, active_object->object_to_world);
|
2020-03-25 17:39:48 +01:00
|
|
|
copy_m3_m4(mat, rv3d->viewinv);
|
|
|
|
|
mul_m3_v3(mat, view_normal);
|
2022-11-02 14:41:49 +01:00
|
|
|
copy_m3_m4(mat, active_object->world_to_object);
|
2020-03-25 17:39:48 +01:00
|
|
|
mul_m3_v3(mat, view_normal);
|
|
|
|
|
normalize_v3(view_normal);
|
|
|
|
|
|
|
|
|
|
normal_tri_v3(current_normal, cd->preview_plane[0], cd->preview_plane[1], cd->preview_plane[2]);
|
|
|
|
|
|
|
|
|
|
float min_dot = dot_v3v3(current_normal, view_normal);
|
|
|
|
|
float current_dot = 1;
|
|
|
|
|
|
|
|
|
|
/* Check if there is a face that is more aligned towards the view. */
|
|
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
|
normal_tri_v3(
|
2023-10-10 09:31:20 +02:00
|
|
|
current_normal, bb.vec[BB_faces[i][0]], bb.vec[BB_faces[i][1]], bb.vec[BB_faces[i][2]]);
|
2020-03-25 17:39:48 +01:00
|
|
|
current_dot = dot_v3v3(current_normal, view_normal);
|
|
|
|
|
|
|
|
|
|
if (current_dot < min_dot) {
|
|
|
|
|
min_dot = current_dot;
|
2023-10-10 09:31:20 +02:00
|
|
|
copy_v3_v3(cd->preview_plane[0], bb.vec[BB_faces[i][0]]);
|
|
|
|
|
copy_v3_v3(cd->preview_plane[1], bb.vec[BB_faces[i][1]]);
|
|
|
|
|
copy_v3_v3(cd->preview_plane[2], bb.vec[BB_faces[i][2]]);
|
|
|
|
|
copy_v3_v3(cd->preview_plane[3], bb.vec[BB_faces[i][3]]);
|
2020-03-25 17:39:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Matrix calculation to position the text in 3D space. */
|
|
|
|
|
float text_pos[3];
|
|
|
|
|
float scale_mat[4][4];
|
|
|
|
|
|
|
|
|
|
float d_a[3], d_b[3];
|
|
|
|
|
float d_a_proj[2], d_b_proj[2];
|
2021-06-01 12:49:20 +10:00
|
|
|
float preview_plane_proj[4][2];
|
2020-08-07 22:36:11 +10:00
|
|
|
const float y_axis_proj[2] = {0.0f, 1.0f};
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
mid_v3_v3v3(text_pos, cd->preview_plane[0], cd->preview_plane[2]);
|
|
|
|
|
|
|
|
|
|
/* Project the selected face in the previous step of the Bounding Box. */
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-06-18 18:23:09 +02:00
|
|
|
float preview_plane_world_space[3];
|
2022-10-24 14:16:37 +02:00
|
|
|
mul_v3_m4v3(preview_plane_world_space, active_object->object_to_world, cd->preview_plane[i]);
|
2021-06-01 12:49:20 +10:00
|
|
|
ED_view3d_project_v2(region, preview_plane_world_space, preview_plane_proj[i]);
|
2020-03-25 17:39:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Get the initial X and Y axis of the basis from the edges of the Bounding Box face. */
|
|
|
|
|
sub_v3_v3v3(d_a, cd->preview_plane[1], cd->preview_plane[0]);
|
|
|
|
|
sub_v3_v3v3(d_b, cd->preview_plane[3], cd->preview_plane[0]);
|
|
|
|
|
normalize_v3(d_a);
|
|
|
|
|
normalize_v3(d_b);
|
|
|
|
|
|
|
|
|
|
/* Project the X and Y axis. */
|
|
|
|
|
sub_v2_v2v2(d_a_proj, preview_plane_proj[1], preview_plane_proj[0]);
|
|
|
|
|
sub_v2_v2v2(d_b_proj, preview_plane_proj[3], preview_plane_proj[0]);
|
|
|
|
|
normalize_v2(d_a_proj);
|
|
|
|
|
normalize_v2(d_b_proj);
|
|
|
|
|
|
|
|
|
|
unit_m4(cd->text_mat);
|
|
|
|
|
|
|
|
|
|
/* Select the axis that is aligned with the view Y axis to use it as the basis Y. */
|
|
|
|
|
if (fabsf(dot_v2v2(d_a_proj, y_axis_proj)) > fabsf(dot_v2v2(d_b_proj, y_axis_proj))) {
|
|
|
|
|
copy_v3_v3(cd->text_mat[0], d_b);
|
|
|
|
|
copy_v3_v3(cd->text_mat[1], d_a);
|
|
|
|
|
|
|
|
|
|
/* Flip the X and Y basis vectors to make sure they always point upwards and to the right. */
|
|
|
|
|
if (d_b_proj[0] < 0.0f) {
|
|
|
|
|
mul_v3_fl(cd->text_mat[0], -1.0f);
|
|
|
|
|
}
|
|
|
|
|
if (d_a_proj[1] < 0.0f) {
|
|
|
|
|
mul_v3_fl(cd->text_mat[1], -1.0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_v3_v3(cd->text_mat[0], d_a);
|
|
|
|
|
copy_v3_v3(cd->text_mat[1], d_b);
|
|
|
|
|
if (d_a_proj[0] < 0.0f) {
|
|
|
|
|
mul_v3_fl(cd->text_mat[0], -1.0f);
|
|
|
|
|
}
|
|
|
|
|
if (d_b_proj[1] < 0.0f) {
|
|
|
|
|
mul_v3_fl(cd->text_mat[1], -1.0f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Use the Bounding Box face normal as the basis Z. */
|
|
|
|
|
normal_tri_v3(cd->text_mat[2], cd->preview_plane[0], cd->preview_plane[1], cd->preview_plane[2]);
|
|
|
|
|
|
2022-08-03 15:05:07 -07:00
|
|
|
/* Invert object scale. */
|
|
|
|
|
float scale[3];
|
2022-10-24 14:16:37 +02:00
|
|
|
mat4_to_size(scale, active_object->object_to_world);
|
2022-08-03 15:05:07 -07:00
|
|
|
invert_v3(scale);
|
|
|
|
|
size_to_mat4(scale_mat, scale);
|
2022-08-05 13:34:10 +10:00
|
|
|
|
2022-08-03 15:05:07 -07:00
|
|
|
mul_m4_m4_pre(cd->text_mat, scale_mat);
|
|
|
|
|
|
2020-03-25 17:39:48 +01:00
|
|
|
/* Write the text position into the matrix. */
|
|
|
|
|
copy_v3_v3(cd->text_mat[3], text_pos);
|
|
|
|
|
|
2022-08-03 15:05:07 -07:00
|
|
|
/* Scale the text to constant viewport size. */
|
2020-06-18 18:23:09 +02:00
|
|
|
float text_pos_word_space[3];
|
2022-10-24 14:16:37 +02:00
|
|
|
mul_v3_m4v3(text_pos_word_space, active_object->object_to_world, text_pos);
|
2020-06-18 18:23:09 +02:00
|
|
|
const float pixelsize = ED_view3d_pixel_size(rv3d, text_pos_word_space);
|
2020-06-08 22:22:55 +02:00
|
|
|
scale_m4_fl(scale_mat, pixelsize * 0.5f);
|
2020-03-25 17:39:48 +01:00
|
|
|
mul_m4_m4_post(cd->text_mat, scale_mat);
|
|
|
|
|
|
|
|
|
|
WM_event_add_modal_handler(C, op);
|
|
|
|
|
|
2020-11-29 18:10:47 -05:00
|
|
|
ED_region_tag_redraw(region);
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
const char *status_str = TIP_(
|
2022-06-22 01:36:13 -07:00
|
|
|
"Move the mouse to change the voxel size. CTRL: Relative Scale, SHIFT: Precision Mode, "
|
|
|
|
|
"ENTER/LMB: Confirm Size, ESC/RMB: Cancel");
|
2020-03-25 17:39:48 +01:00
|
|
|
ED_workspace_status_text(C, status_str);
|
|
|
|
|
|
|
|
|
|
return OPERATOR_RUNNING_MODAL;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 15:42:50 +02:00
|
|
|
static bool voxel_size_edit_poll(bContext *C)
|
|
|
|
|
{
|
|
|
|
|
return CTX_wm_region_view3d(C) && object_remesh_poll(C);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 17:39:48 +01:00
|
|
|
void OBJECT_OT_voxel_size_edit(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
|
|
|
|
ot->name = "Edit Voxel Size";
|
|
|
|
|
ot->description = "Modify the mesh voxel size interactively used in the voxel remesher";
|
|
|
|
|
ot->idname = "OBJECT_OT_voxel_size_edit";
|
|
|
|
|
|
|
|
|
|
/* api callbacks */
|
2020-06-08 15:42:50 +02:00
|
|
|
ot->poll = voxel_size_edit_poll;
|
2020-03-25 17:39:48 +01:00
|
|
|
ot->invoke = voxel_size_edit_invoke;
|
|
|
|
|
ot->modal = voxel_size_edit_modal;
|
|
|
|
|
ot->cancel = voxel_size_edit_cancel;
|
|
|
|
|
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 10:53:46 +11:00
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Quadriflow Remesh Operator
|
|
|
|
|
* \{ */
|
2020-03-25 17:39:48 +01:00
|
|
|
|
|
|
|
|
#define QUADRIFLOW_MIRROR_BISECT_TOLERANCE 0.005f
|
|
|
|
|
|
2019-08-26 18:34:11 +02:00
|
|
|
enum {
|
|
|
|
|
QUADRIFLOW_REMESH_RATIO = 1,
|
|
|
|
|
QUADRIFLOW_REMESH_EDGE_LENGTH,
|
|
|
|
|
QUADRIFLOW_REMESH_FACES,
|
|
|
|
|
};
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
enum eSymmetryAxes {
|
2019-09-25 14:54:34 +02:00
|
|
|
SYMMETRY_AXES_X = (1 << 0),
|
|
|
|
|
SYMMETRY_AXES_Y = (1 << 1),
|
|
|
|
|
SYMMETRY_AXES_Z = (1 << 2),
|
2021-07-30 13:15:01 -04:00
|
|
|
};
|
2019-09-25 14:54:34 +02:00
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
struct QuadriFlowJob {
|
2019-08-26 18:34:11 +02:00
|
|
|
/* from wmJob */
|
2023-06-03 08:36:28 +10:00
|
|
|
Object *owner;
|
2022-11-04 18:37:25 +11:00
|
|
|
bool *stop, *do_update;
|
2019-08-26 18:34:11 +02:00
|
|
|
float *progress;
|
|
|
|
|
|
2023-06-03 08:36:28 +10:00
|
|
|
const wmOperator *op;
|
2020-12-29 14:43:12 +01:00
|
|
|
Scene *scene;
|
2019-08-26 18:34:11 +02:00
|
|
|
int target_faces;
|
|
|
|
|
int seed;
|
2020-11-21 15:52:12 +01:00
|
|
|
bool use_mesh_symmetry;
|
2019-09-25 14:54:34 +02:00
|
|
|
eSymmetryAxes symmetry_axes;
|
|
|
|
|
|
2019-08-26 18:34:11 +02:00
|
|
|
bool use_preserve_sharp;
|
|
|
|
|
bool use_preserve_boundary;
|
|
|
|
|
bool use_mesh_curvature;
|
|
|
|
|
|
2023-11-22 15:21:58 +01:00
|
|
|
bool preserve_attributes;
|
2019-08-26 18:34:11 +02:00
|
|
|
bool smooth_normals;
|
|
|
|
|
|
|
|
|
|
int success;
|
2020-03-03 15:28:36 +01:00
|
|
|
bool is_nonblocking_job;
|
2021-07-30 13:15:01 -04:00
|
|
|
};
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2019-10-04 14:25:34 +02:00
|
|
|
static bool mesh_is_manifold_consistent(Mesh *mesh)
|
|
|
|
|
{
|
|
|
|
|
/* In this check we count boundary edges as manifold. Additionally, we also
|
|
|
|
|
* check that the direction of the faces are consistent and doesn't suddenly
|
|
|
|
|
* flip
|
|
|
|
|
*/
|
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 = mesh->vert_positions();
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
const Span<blender::int2> edges = mesh->edges();
|
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 = mesh->corner_verts();
|
|
|
|
|
const Span<int> corner_edges = mesh->corner_edges();
|
2019-10-04 14:25:34 +02:00
|
|
|
|
|
|
|
|
bool is_manifold_consistent = true;
|
|
|
|
|
char *edge_faces = (char *)MEM_callocN(mesh->totedge * sizeof(char), "remesh_manifold_check");
|
|
|
|
|
int *edge_vert = (int *)MEM_malloc_arrayN(
|
2020-04-03 16:21:24 +11:00
|
|
|
mesh->totedge, sizeof(uint), "remesh_consistent_check");
|
2019-10-04 14:25:34 +02:00
|
|
|
|
2020-04-03 16:21:24 +11:00
|
|
|
for (uint i = 0; i < mesh->totedge; i++) {
|
2019-10-04 14:25:34 +02:00
|
|
|
edge_vert[i] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
for (const int corner_i : corner_verts.index_range()) {
|
|
|
|
|
const int vert = corner_verts[corner_i];
|
|
|
|
|
const int edge = corner_edges[corner_i];
|
|
|
|
|
edge_faces[edge] += 1;
|
|
|
|
|
if (edge_faces[edge] > 2) {
|
2019-10-07 18:09:38 +02:00
|
|
|
is_manifold_consistent = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-04 14:25:34 +02:00
|
|
|
|
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
|
|
|
if (edge_vert[edge] == -1) {
|
|
|
|
|
edge_vert[edge] = vert;
|
2019-10-07 18:09:38 +02:00
|
|
|
}
|
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
|
|
|
else if (edge_vert[edge] == vert) {
|
2019-10-07 18:09:38 +02:00
|
|
|
/* Mesh has flips in the surface so it is non consistent */
|
|
|
|
|
is_manifold_consistent = false;
|
|
|
|
|
break;
|
2019-10-04 14:25:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_manifold_consistent) {
|
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
|
|
|
for (const int i : edges.index_range()) {
|
2021-10-21 15:13:55 +02:00
|
|
|
/* Check for wire edges. */
|
2019-10-04 14:25:34 +02:00
|
|
|
if (edge_faces[i] == 0) {
|
|
|
|
|
is_manifold_consistent = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-10-21 15:13:55 +02:00
|
|
|
/* Check for zero length edges */
|
Mesh: Move edges to a generic attribute
Implements #95966, as the final step of #95965.
This commit changes the storage of mesh edge vertex indices from the
`MEdge` type to the generic `int2` attribute type. This follows the
general design for geometry and the attribute system, where the data
storage type and the usage semantics are separated.
The main benefit of the change is reduced memory usage-- the
requirements of storing mesh edges is reduced by 1/3. For example,
this saves 8MB on a 1 million vertex grid. This also gives performance
benefits to any memory-bound mesh processing algorithm that uses edges.
Another benefit is that all of the edge's vertex indices are
contiguous. In a few cases, it's helpful to process all of them as
`Span<int>` rather than `Span<int2>`. Similarly, the type is more
likely to match a generic format used by a library, or code that
shouldn't know about specific Blender `Mesh` types.
Various Notes:
- The `.edge_verts` name is used to reflect a mapping between domains,
similar to `.corner_verts`, etc. The period means that it the data
shouldn't change arbitrarily by the user or procedural operations.
- `edge[0]` is now used instead of `edge.v1`
- Signed integers are used instead of unsigned to reduce the mixing
of signed-ness, which can be error prone.
- All of the previously used core mesh data types (`MVert`, `MEdge`,
`MLoop`, `MPoly` are now deprecated. Only generic types are used).
- The `vec2i` DNA type is used in the few C files where necessary.
Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
|
|
|
if (compare_v3v3(positions[edges[i][0]], positions[edges[i][1]], 1e-4f)) {
|
2021-10-21 15:13:55 +02:00
|
|
|
is_manifold_consistent = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-10-04 14:25:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MEM_freeN(edge_faces);
|
|
|
|
|
MEM_freeN(edge_vert);
|
|
|
|
|
|
|
|
|
|
return is_manifold_consistent;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 18:34:11 +02:00
|
|
|
static void quadriflow_free_job(void *customdata)
|
|
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
QuadriFlowJob *qj = static_cast<QuadriFlowJob *>(customdata);
|
2019-08-26 18:34:11 +02:00
|
|
|
MEM_freeN(qj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* called by quadriflowjob, only to check job 'stop' value */
|
|
|
|
|
static int quadriflow_break_job(void *customdata)
|
|
|
|
|
{
|
|
|
|
|
QuadriFlowJob *qj = (QuadriFlowJob *)customdata;
|
|
|
|
|
// return *(qj->stop);
|
|
|
|
|
|
|
|
|
|
/* this is not nice yet, need to make the jobs list template better
|
|
|
|
|
* for identifying/acting upon various different jobs */
|
|
|
|
|
/* but for now we'll reuse the render break... */
|
|
|
|
|
bool should_break = (G.is_break);
|
|
|
|
|
|
|
|
|
|
if (should_break) {
|
|
|
|
|
qj->success = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return should_break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 16:23:34 +11:00
|
|
|
/** Called by ocean-bake, #wmJob sends notifier. */
|
2019-08-26 18:34:11 +02:00
|
|
|
static void quadriflow_update_job(void *customdata, float progress, int *cancel)
|
|
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
QuadriFlowJob *qj = static_cast<QuadriFlowJob *>(customdata);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
if (quadriflow_break_job(qj)) {
|
|
|
|
|
*cancel = 1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
*cancel = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*(qj->do_update) = true;
|
|
|
|
|
*(qj->progress) = progress;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 16:17:36 +01:00
|
|
|
static Mesh *remesh_symmetry_bisect(Mesh *mesh, eSymmetryAxes symmetry_axes)
|
2019-09-25 14:54:34 +02:00
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
MirrorModifierData mmd = {{nullptr}};
|
2019-09-25 14:54:34 +02:00
|
|
|
mmd.tolerance = QUADRIFLOW_MIRROR_BISECT_TOLERANCE;
|
|
|
|
|
|
|
|
|
|
Mesh *mesh_bisect, *mesh_bisect_temp;
|
2023-04-19 15:49:13 -04:00
|
|
|
mesh_bisect = BKE_mesh_copy_for_eval(mesh);
|
2019-09-25 14:54:34 +02:00
|
|
|
|
|
|
|
|
int axis;
|
|
|
|
|
float plane_co[3], plane_no[3];
|
|
|
|
|
zero_v3(plane_co);
|
|
|
|
|
|
|
|
|
|
for (char i = 0; i < 3; i++) {
|
|
|
|
|
eSymmetryAxes symm_it = (eSymmetryAxes)(1 << i);
|
|
|
|
|
if (symmetry_axes & symm_it) {
|
|
|
|
|
axis = i;
|
|
|
|
|
mmd.flag = 0;
|
|
|
|
|
mmd.flag &= MOD_MIR_BISECT_AXIS_X << i;
|
|
|
|
|
zero_v3(plane_no);
|
|
|
|
|
plane_no[axis] = -1.0f;
|
|
|
|
|
mesh_bisect_temp = mesh_bisect;
|
2021-01-05 22:27:49 +11:00
|
|
|
mesh_bisect = BKE_mesh_mirror_bisect_on_mirror_plane_for_modifier(
|
2019-11-22 13:03:56 +11:00
|
|
|
&mmd, mesh_bisect, axis, plane_co, plane_no);
|
2019-09-25 14:54:34 +02:00
|
|
|
if (mesh_bisect_temp != mesh_bisect) {
|
2021-07-30 13:15:01 -04:00
|
|
|
BKE_id_free(nullptr, mesh_bisect_temp);
|
2019-09-25 14:54:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
BKE_id_free(nullptr, mesh);
|
2019-09-25 14:54:34 +02:00
|
|
|
|
|
|
|
|
return mesh_bisect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Mesh *remesh_symmetry_mirror(Object *ob, Mesh *mesh, eSymmetryAxes symmetry_axes)
|
|
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
MirrorModifierData mmd = {{nullptr}};
|
2019-09-25 14:54:34 +02:00
|
|
|
mmd.tolerance = QUADRIFLOW_MIRROR_BISECT_TOLERANCE;
|
|
|
|
|
Mesh *mesh_mirror, *mesh_mirror_temp;
|
|
|
|
|
|
|
|
|
|
mesh_mirror = mesh;
|
|
|
|
|
|
|
|
|
|
int axis;
|
|
|
|
|
|
|
|
|
|
for (char i = 0; i < 3; i++) {
|
|
|
|
|
eSymmetryAxes symm_it = (eSymmetryAxes)(1 << i);
|
|
|
|
|
if (symmetry_axes & symm_it) {
|
|
|
|
|
axis = i;
|
|
|
|
|
mmd.flag = 0;
|
|
|
|
|
mmd.flag &= MOD_MIR_AXIS_X << i;
|
|
|
|
|
mesh_mirror_temp = mesh_mirror;
|
2021-11-12 18:07:07 +11:00
|
|
|
mesh_mirror = BKE_mesh_mirror_apply_mirror_on_axis_for_modifier(
|
2023-02-23 19:10:01 +01:00
|
|
|
&mmd, ob, mesh_mirror, axis, true, nullptr, nullptr);
|
2019-09-25 14:54:34 +02:00
|
|
|
if (mesh_mirror_temp != mesh_mirror) {
|
2021-07-30 13:15:01 -04:00
|
|
|
BKE_id_free(nullptr, mesh_mirror_temp);
|
2019-09-25 14:54:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mesh_mirror;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 13:06:02 +02:00
|
|
|
static void quadriflow_start_job(void *customdata, wmJobWorkerStatus *worker_status)
|
2019-08-26 18:34:11 +02:00
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
QuadriFlowJob *qj = static_cast<QuadriFlowJob *>(customdata);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2023-10-06 13:06:02 +02:00
|
|
|
qj->stop = &worker_status->stop;
|
|
|
|
|
qj->do_update = &worker_status->do_update;
|
|
|
|
|
qj->progress = &worker_status->progress;
|
2019-08-26 18:34:11 +02:00
|
|
|
qj->success = 1;
|
|
|
|
|
|
2020-03-03 15:28:36 +01:00
|
|
|
if (qj->is_nonblocking_job) {
|
|
|
|
|
G.is_break = false; /* XXX shared with render - replace with job 'stop' switch */
|
|
|
|
|
}
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
Object *ob = qj->owner;
|
2021-07-30 13:15:01 -04:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2019-08-26 18:34:11 +02:00
|
|
|
Mesh *new_mesh;
|
2019-09-25 14:54:34 +02:00
|
|
|
Mesh *bisect_mesh;
|
|
|
|
|
|
2019-10-04 14:25:34 +02:00
|
|
|
/* Check if the mesh is manifold. Quadriflow requires manifold meshes */
|
|
|
|
|
if (!mesh_is_manifold_consistent(mesh)) {
|
|
|
|
|
qj->success = -2;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 14:54:34 +02:00
|
|
|
/* Run Quadriflow bisect operations on a copy of the mesh to keep the code readable without
|
|
|
|
|
* freeing the original ID */
|
2023-04-19 15:49:13 -04:00
|
|
|
bisect_mesh = BKE_mesh_copy_for_eval(mesh);
|
2019-09-25 14:54:34 +02:00
|
|
|
|
|
|
|
|
/* Bisect the input mesh using the paint symmetry settings */
|
2020-03-13 16:17:36 +01:00
|
|
|
bisect_mesh = remesh_symmetry_bisect(bisect_mesh, qj->symmetry_axes);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2021-07-30 22:18:44 -04:00
|
|
|
new_mesh = BKE_mesh_remesh_quadriflow(bisect_mesh,
|
|
|
|
|
qj->target_faces,
|
|
|
|
|
qj->seed,
|
|
|
|
|
qj->use_preserve_sharp,
|
|
|
|
|
(qj->use_preserve_boundary || qj->use_mesh_symmetry),
|
2020-03-04 10:56:56 +11:00
|
|
|
#ifdef USE_MESH_CURVATURE
|
2021-07-30 22:18:44 -04:00
|
|
|
qj->use_mesh_curvature,
|
2020-03-04 10:56:56 +11:00
|
|
|
#else
|
2021-07-30 22:18:44 -04:00
|
|
|
false,
|
2020-03-04 10:56:56 +11:00
|
|
|
#endif
|
2021-07-30 22:18:44 -04:00
|
|
|
quadriflow_update_job,
|
|
|
|
|
(void *)qj);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
BKE_id_free(nullptr, bisect_mesh);
|
2019-09-25 14:54:34 +02:00
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
if (new_mesh == nullptr) {
|
2023-10-06 13:06:02 +02:00
|
|
|
worker_status->do_update = true;
|
|
|
|
|
worker_status->stop = false;
|
2019-08-26 18:34:11 +02:00
|
|
|
if (qj->success == 1) {
|
2020-03-04 10:56:56 +11:00
|
|
|
/* This is not a user cancellation event. */
|
2019-08-26 18:34:11 +02:00
|
|
|
qj->success = 0;
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-25 14:54:34 +02:00
|
|
|
/* Mirror the Quadriflow result to build the final mesh */
|
2019-10-28 00:36:23 +11:00
|
|
|
new_mesh = remesh_symmetry_mirror(qj->owner, new_mesh, qj->symmetry_axes);
|
2019-09-25 14:54:34 +02:00
|
|
|
|
2019-08-26 18:34:11 +02:00
|
|
|
if (ob->mode == OB_MODE_SCULPT) {
|
2022-08-15 14:52:02 -07:00
|
|
|
ED_sculpt_undo_geometry_begin(ob, qj->op);
|
2019-08-26 18:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-22 15:21:58 +01:00
|
|
|
if (qj->preserve_attributes) {
|
|
|
|
|
blender::bke::mesh_remesh_reproject_attributes(*mesh, *new_mesh);
|
2019-08-26 18:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-09 08:24:31 -05:00
|
|
|
BKE_mesh_nomain_to_mesh(new_mesh, mesh, ob);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
Mesh: Move face shade smooth flag to a generic attribute
Currently the shade smooth status for mesh faces is stored as part of
`MPoly::flag`. As described in #95967, this moves that information
to a separate boolean attribute. It also flips its status, so the
attribute is now called `sharp_face`, which mirrors the existing
`sharp_edge` attribute. The attribute doesn't need to be allocated
when all faces are smooth. Forward compatibility is kept until
4.0 like the other mesh refactors.
This will reduce memory bandwidth requirements for some operations,
since the array of booleans uses 12 times less memory than `MPoly`.
It also allows faces to be stored more efficiently in the future, since
the flag is now unused. It's also possible to use generic functions to
process the values. For example, finding whether there is a sharp face
is just `sharp_faces.contains(true)`.
The `shade_smooth` attribute is no longer accessible with geometry nodes.
Since there were dedicated accessor nodes for that data, that shouldn't
be a problem. That's difficult to version automatically since the named
attribute nodes could be used in arbitrary combinations.
**Implementation notes:**
- The attribute and array variables in the code use the `sharp_faces`
term, to be consistent with the user-facing "sharp faces" wording,
and to avoid requiring many renames when #101689 is implemented.
- Cycles now accesses smooth face status with the generic attribute,
to avoid overhead.
- Changing the zero-value from "smooth" to "flat" takes some care to
make sure defaults are the same.
- Versioning for the edge mode extrude node is particularly complex.
New nodes are added by versioning to propagate the attribute in its
old inverted state.
- A lot of access is still done through the `CustomData` API rather
than the attribute API because of a few functions. That can be
cleaned up easily in the future.
- In the future we would benefit from a way to store attributes as a
single value for when all faces are sharp.
Pull Request: https://projects.blender.org/blender/blender/pulls/104422
2023-03-08 15:36:18 +01:00
|
|
|
BKE_mesh_smooth_flag_set(static_cast<Mesh *>(ob->data), qj->smooth_normals);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
if (ob->mode == OB_MODE_SCULPT) {
|
|
|
|
|
ED_sculpt_undo_geometry_end(ob);
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 13:15:01 -04:00
|
|
|
BKE_mesh_batch_cache_dirty_tag(static_cast<Mesh *>(ob->data), BKE_MESH_BATCH_DIRTY_ALL);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2023-10-06 13:06:02 +02:00
|
|
|
worker_status->do_update = true;
|
|
|
|
|
worker_status->stop = false;
|
2019-08-26 18:34:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void quadriflow_end_job(void *customdata)
|
|
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
QuadriFlowJob *qj = (QuadriFlowJob *)customdata;
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
Object *ob = qj->owner;
|
|
|
|
|
|
2020-03-03 15:28:36 +01:00
|
|
|
if (qj->is_nonblocking_job) {
|
2021-07-30 13:15:01 -04:00
|
|
|
WM_set_locked_interface(static_cast<wmWindowManager *>(G_MAIN->wm.first), false);
|
2020-03-03 15:28:36 +01:00
|
|
|
}
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2019-10-04 14:25:34 +02:00
|
|
|
switch (qj->success) {
|
|
|
|
|
case 1:
|
|
|
|
|
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
|
2020-02-17 13:00:01 +01:00
|
|
|
WM_reportf(RPT_INFO, "QuadriFlow: Remeshing completed");
|
2019-10-04 14:25:34 +02:00
|
|
|
break;
|
|
|
|
|
case 0:
|
2020-02-17 13:00:01 +01:00
|
|
|
WM_reportf(RPT_ERROR, "QuadriFlow: Remeshing failed");
|
2019-10-04 14:25:34 +02:00
|
|
|
break;
|
|
|
|
|
case -1:
|
2023-04-17 11:40:14 +02:00
|
|
|
WM_report(RPT_WARNING, "QuadriFlow: Remeshing canceled");
|
2019-10-04 14:25:34 +02:00
|
|
|
break;
|
|
|
|
|
case -2:
|
|
|
|
|
WM_report(RPT_WARNING,
|
|
|
|
|
"QuadriFlow: The mesh needs to be manifold and have face normals that point in a "
|
2020-02-17 13:00:01 +01:00
|
|
|
"consistent direction");
|
2019-10-04 14:25:34 +02:00
|
|
|
break;
|
2019-08-26 18:34:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int quadriflow_remesh_exec(bContext *C, wmOperator *op)
|
|
|
|
|
{
|
2021-07-30 13:15:01 -04:00
|
|
|
QuadriFlowJob *job = (QuadriFlowJob *)MEM_mallocN(sizeof(QuadriFlowJob), "QuadriFlowJob");
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2022-08-15 14:52:02 -07:00
|
|
|
job->op = op;
|
2019-08-26 18:34:11 +02:00
|
|
|
job->owner = CTX_data_active_object(C);
|
2020-12-29 14:43:12 +01:00
|
|
|
job->scene = CTX_data_scene(C);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
job->target_faces = RNA_int_get(op->ptr, "target_faces");
|
|
|
|
|
job->seed = RNA_int_get(op->ptr, "seed");
|
|
|
|
|
|
2020-11-21 15:52:12 +01:00
|
|
|
job->use_mesh_symmetry = RNA_boolean_get(op->ptr, "use_mesh_symmetry");
|
2019-09-25 14:54:34 +02:00
|
|
|
|
2019-08-26 18:34:11 +02:00
|
|
|
job->use_preserve_sharp = RNA_boolean_get(op->ptr, "use_preserve_sharp");
|
|
|
|
|
job->use_preserve_boundary = RNA_boolean_get(op->ptr, "use_preserve_boundary");
|
|
|
|
|
|
2020-03-04 10:56:56 +11:00
|
|
|
#ifdef USE_MESH_CURVATURE
|
2019-08-26 18:34:11 +02:00
|
|
|
job->use_mesh_curvature = RNA_boolean_get(op->ptr, "use_mesh_curvature");
|
2020-03-04 10:56:56 +11:00
|
|
|
#endif
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2023-11-22 15:21:58 +01:00
|
|
|
job->preserve_attributes = RNA_boolean_get(op->ptr, "preserve_attributes");
|
2019-08-26 18:34:11 +02:00
|
|
|
job->smooth_normals = RNA_boolean_get(op->ptr, "smooth_normals");
|
|
|
|
|
|
2019-09-25 14:54:34 +02:00
|
|
|
/* Update the target face count if symmetry is enabled */
|
2020-09-18 19:58:48 +02:00
|
|
|
Object *ob = CTX_data_active_object(C);
|
2020-11-21 15:52:12 +01:00
|
|
|
if (ob && job->use_mesh_symmetry) {
|
2020-09-18 19:58:48 +02:00
|
|
|
Mesh *mesh = BKE_mesh_from_object(ob);
|
|
|
|
|
job->symmetry_axes = (eSymmetryAxes)mesh->symmetry;
|
2019-09-25 14:54:34 +02:00
|
|
|
for (char i = 0; i < 3; i++) {
|
|
|
|
|
eSymmetryAxes symm_it = (eSymmetryAxes)(1 << i);
|
|
|
|
|
if (job->symmetry_axes & symm_it) {
|
|
|
|
|
job->target_faces = job->target_faces / 2;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2020-11-21 15:52:12 +01:00
|
|
|
job->use_mesh_symmetry = false;
|
2021-07-30 13:15:01 -04:00
|
|
|
job->symmetry_axes = (eSymmetryAxes)0;
|
2019-09-25 14:54:34 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:28:36 +01:00
|
|
|
if (op->flag == 0) {
|
|
|
|
|
/* This is called directly from the exec operator, this operation is now blocking */
|
|
|
|
|
job->is_nonblocking_job = false;
|
2023-10-06 13:06:02 +02:00
|
|
|
wmJobWorkerStatus worker_status = {};
|
|
|
|
|
quadriflow_start_job(job, &worker_status);
|
2020-03-03 15:28:36 +01:00
|
|
|
quadriflow_end_job(job);
|
|
|
|
|
quadriflow_free_job(job);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2021-02-05 16:23:34 +11:00
|
|
|
/* Non blocking call. For when the operator has been called from the GUI. */
|
2020-03-03 15:28:36 +01:00
|
|
|
job->is_nonblocking_job = true;
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2020-03-03 15:28:36 +01:00
|
|
|
wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
|
|
|
|
|
CTX_wm_window(C),
|
|
|
|
|
CTX_data_scene(C),
|
|
|
|
|
"QuadriFlow Remesh",
|
|
|
|
|
WM_JOB_PROGRESS,
|
|
|
|
|
WM_JOB_TYPE_QUADRIFLOW_REMESH);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2020-03-03 15:28:36 +01:00
|
|
|
WM_jobs_customdata_set(wm_job, job, quadriflow_free_job);
|
|
|
|
|
WM_jobs_timer(wm_job, 0.1, NC_GEOM | ND_DATA, NC_GEOM | ND_DATA);
|
2021-07-30 13:15:01 -04:00
|
|
|
WM_jobs_callbacks(wm_job, quadriflow_start_job, nullptr, nullptr, quadriflow_end_job);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2020-03-03 15:28:36 +01:00
|
|
|
WM_set_locked_interface(CTX_wm_manager(C), true);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
2020-03-03 15:28:36 +01:00
|
|
|
WM_jobs_start(CTX_wm_manager(C), wm_job);
|
|
|
|
|
}
|
2019-08-26 18:34:11 +02:00
|
|
|
return OPERATOR_FINISHED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool quadriflow_check(bContext *C, wmOperator *op)
|
|
|
|
|
{
|
|
|
|
|
int mode = RNA_enum_get(op->ptr, "mode");
|
|
|
|
|
|
|
|
|
|
if (mode == QUADRIFLOW_REMESH_EDGE_LENGTH) {
|
|
|
|
|
float area = RNA_float_get(op->ptr, "mesh_area");
|
|
|
|
|
if (area < 0.0f) {
|
|
|
|
|
Object *ob = CTX_data_active_object(C);
|
2021-07-30 13:15:01 -04:00
|
|
|
area = BKE_mesh_calc_area(static_cast<const Mesh *>(ob->data));
|
2019-08-26 18:34:11 +02:00
|
|
|
RNA_float_set(op->ptr, "mesh_area", area);
|
|
|
|
|
}
|
|
|
|
|
int num_faces;
|
|
|
|
|
float edge_len = RNA_float_get(op->ptr, "target_edge_length");
|
|
|
|
|
|
|
|
|
|
num_faces = area / (edge_len * edge_len);
|
|
|
|
|
RNA_int_set(op->ptr, "target_faces", num_faces);
|
|
|
|
|
}
|
|
|
|
|
else if (mode == QUADRIFLOW_REMESH_RATIO) {
|
|
|
|
|
Object *ob = CTX_data_active_object(C);
|
2021-07-30 13:15:01 -04:00
|
|
|
Mesh *mesh = static_cast<Mesh *>(ob->data);
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
int num_faces;
|
|
|
|
|
float ratio = RNA_float_get(op->ptr, "target_ratio");
|
|
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
num_faces = mesh->faces_num * ratio;
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
RNA_int_set(op->ptr, "target_faces", num_faces);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Hide the target variables if they are not active */
|
|
|
|
|
static bool quadriflow_poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop)
|
|
|
|
|
{
|
|
|
|
|
const char *prop_id = RNA_property_identifier(prop);
|
|
|
|
|
|
|
|
|
|
if (STRPREFIX(prop_id, "target")) {
|
|
|
|
|
int mode = RNA_enum_get(op->ptr, "mode");
|
|
|
|
|
|
|
|
|
|
if (STREQ(prop_id, "target_edge_length") && mode != QUADRIFLOW_REMESH_EDGE_LENGTH) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-07-03 15:42:22 +02:00
|
|
|
if (STREQ(prop_id, "target_faces")) {
|
2019-08-26 18:34:11 +02:00
|
|
|
if (mode != QUADRIFLOW_REMESH_FACES) {
|
|
|
|
|
/* Make sure we can edit the target_faces value even if it doesn't start as EDITABLE */
|
|
|
|
|
float area = RNA_float_get(op->ptr, "mesh_area");
|
|
|
|
|
if (area < -0.8f) {
|
|
|
|
|
area += 0.2f;
|
|
|
|
|
/* Make sure we have up to date values from the start */
|
|
|
|
|
RNA_def_property_flag((PropertyRNA *)prop, PROP_EDITABLE);
|
|
|
|
|
quadriflow_check((bContext *)C, op);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Only disable input */
|
|
|
|
|
RNA_def_property_clear_flag((PropertyRNA *)prop, PROP_EDITABLE);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
RNA_def_property_flag((PropertyRNA *)prop, PROP_EDITABLE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (STREQ(prop_id, "target_ratio") && mode != QUADRIFLOW_REMESH_RATIO) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const EnumPropertyItem mode_type_items[] = {
|
|
|
|
|
{QUADRIFLOW_REMESH_RATIO,
|
|
|
|
|
"RATIO",
|
|
|
|
|
0,
|
|
|
|
|
"Ratio",
|
|
|
|
|
"Specify target number of faces relative to the current mesh"},
|
|
|
|
|
{QUADRIFLOW_REMESH_EDGE_LENGTH,
|
|
|
|
|
"EDGE",
|
|
|
|
|
0,
|
|
|
|
|
"Edge Length",
|
|
|
|
|
"Input target edge length in the new mesh"},
|
|
|
|
|
{QUADRIFLOW_REMESH_FACES, "FACES", 0, "Faces", "Input target number of faces in the new mesh"},
|
2021-07-30 13:15:01 -04:00
|
|
|
{0, nullptr, 0, nullptr, nullptr},
|
2019-08-26 18:34:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void OBJECT_OT_quadriflow_remesh(wmOperatorType *ot)
|
|
|
|
|
{
|
|
|
|
|
/* identifiers */
|
|
|
|
|
ot->name = "QuadriFlow Remesh";
|
|
|
|
|
ot->description =
|
|
|
|
|
"Create a new quad based mesh using the surface data of the current mesh. All data "
|
|
|
|
|
"layers will be lost";
|
|
|
|
|
ot->idname = "OBJECT_OT_quadriflow_remesh";
|
|
|
|
|
|
|
|
|
|
/* api callbacks */
|
|
|
|
|
ot->poll = object_remesh_poll;
|
|
|
|
|
ot->poll_property = quadriflow_poll_property;
|
|
|
|
|
ot->check = quadriflow_check;
|
|
|
|
|
ot->invoke = WM_operator_props_popup_confirm;
|
|
|
|
|
ot->exec = quadriflow_remesh_exec;
|
|
|
|
|
|
|
|
|
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
|
|
|
|
|
|
|
|
|
PropertyRNA *prop;
|
|
|
|
|
|
|
|
|
|
/* properties */
|
2019-09-25 14:54:34 +02:00
|
|
|
RNA_def_boolean(ot->srna,
|
2020-11-21 15:52:12 +01:00
|
|
|
"use_mesh_symmetry",
|
2019-09-25 14:54:34 +02:00
|
|
|
true,
|
2020-11-21 15:52:12 +01:00
|
|
|
"Use Mesh Symmetry",
|
|
|
|
|
"Generates a symmetrical mesh using the mesh symmetry configuration");
|
2019-09-25 14:54:34 +02:00
|
|
|
|
2019-08-26 18:34:11 +02:00
|
|
|
RNA_def_boolean(ot->srna,
|
|
|
|
|
"use_preserve_sharp",
|
|
|
|
|
false,
|
|
|
|
|
"Preserve Sharp",
|
|
|
|
|
"Try to preserve sharp features on the mesh");
|
|
|
|
|
|
|
|
|
|
RNA_def_boolean(ot->srna,
|
|
|
|
|
"use_preserve_boundary",
|
|
|
|
|
false,
|
|
|
|
|
"Preserve Mesh Boundary",
|
|
|
|
|
"Try to preserve mesh boundary on the mesh");
|
2020-03-04 10:56:56 +11:00
|
|
|
#ifdef USE_MESH_CURVATURE
|
2019-08-26 18:34:11 +02:00
|
|
|
RNA_def_boolean(ot->srna,
|
|
|
|
|
"use_mesh_curvature",
|
|
|
|
|
false,
|
|
|
|
|
"Use Mesh Curvature",
|
|
|
|
|
"Take the mesh curvature into account when remeshing");
|
2020-03-04 10:56:56 +11:00
|
|
|
#endif
|
2019-08-26 18:34:11 +02:00
|
|
|
RNA_def_boolean(ot->srna,
|
2023-11-22 15:21:58 +01:00
|
|
|
"preserve_attributes",
|
2019-08-26 18:34:11 +02:00
|
|
|
false,
|
2023-11-22 15:21:58 +01:00
|
|
|
"Preserve Attributes",
|
|
|
|
|
"Reproject attributes onto the new mesh");
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
RNA_def_boolean(ot->srna,
|
|
|
|
|
"smooth_normals",
|
|
|
|
|
false,
|
|
|
|
|
"Smooth Normals",
|
|
|
|
|
"Set the output mesh normals to smooth");
|
|
|
|
|
|
|
|
|
|
RNA_def_enum(ot->srna,
|
|
|
|
|
"mode",
|
|
|
|
|
mode_type_items,
|
2019-10-01 17:37:35 +02:00
|
|
|
QUADRIFLOW_REMESH_FACES,
|
2019-08-26 18:34:11 +02:00
|
|
|
"Mode",
|
|
|
|
|
"How to specify the amount of detail for the new mesh");
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_float(ot->srna,
|
|
|
|
|
"target_ratio",
|
|
|
|
|
1,
|
|
|
|
|
0,
|
|
|
|
|
FLT_MAX,
|
|
|
|
|
"Ratio",
|
|
|
|
|
"Relative number of faces compared to the current mesh",
|
|
|
|
|
0.0f,
|
|
|
|
|
1.0f);
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_float(ot->srna,
|
|
|
|
|
"target_edge_length",
|
|
|
|
|
0.1f,
|
|
|
|
|
0.0000001f,
|
|
|
|
|
FLT_MAX,
|
|
|
|
|
"Edge Length",
|
|
|
|
|
"Target edge length in the new mesh",
|
|
|
|
|
0.00001f,
|
|
|
|
|
1.0f);
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_int(ot->srna,
|
|
|
|
|
"target_faces",
|
2019-10-01 17:37:35 +02:00
|
|
|
4000,
|
2019-08-26 18:34:11 +02:00
|
|
|
1,
|
|
|
|
|
INT_MAX,
|
|
|
|
|
"Number of Faces",
|
|
|
|
|
"Approximate number of faces (quads) in the new mesh",
|
|
|
|
|
1,
|
|
|
|
|
INT_MAX);
|
|
|
|
|
|
|
|
|
|
prop = RNA_def_float(
|
|
|
|
|
ot->srna,
|
|
|
|
|
"mesh_area",
|
|
|
|
|
-1.0f,
|
|
|
|
|
-FLT_MAX,
|
|
|
|
|
FLT_MAX,
|
|
|
|
|
"Old Object Face Area",
|
|
|
|
|
"This property is only used to cache the object area for later calculations",
|
|
|
|
|
0.0f,
|
|
|
|
|
FLT_MAX);
|
2021-07-30 13:15:01 -04:00
|
|
|
RNA_def_property_flag(prop, static_cast<PropertyFlag>(PROP_HIDDEN | PROP_SKIP_SAVE));
|
2019-08-26 18:34:11 +02:00
|
|
|
|
|
|
|
|
RNA_def_int(ot->srna,
|
|
|
|
|
"seed",
|
|
|
|
|
0,
|
|
|
|
|
0,
|
|
|
|
|
INT_MAX,
|
|
|
|
|
"Seed",
|
|
|
|
|
"Random seed to use with the solver. Different seeds will cause the remesher to "
|
|
|
|
|
"come up with different quad layouts on the mesh",
|
|
|
|
|
0,
|
|
|
|
|
255);
|
|
|
|
|
}
|
2020-04-01 10:53:46 +11:00
|
|
|
|
|
|
|
|
/** \} */
|