2022-02-11 09:07:11 +11:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2012-12-18 00:51:25 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup freestyle
|
2012-12-18 00:51:25 +00:00
|
|
|
*/
|
2008-08-07 15:04:25 +00:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
#include "BlenderFileLoader.h"
|
|
|
|
|
|
2013-07-09 23:25:02 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
#include "BKE_attribute.hh"
|
2013-01-03 23:27:20 +00:00
|
|
|
#include "BKE_global.h"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2019-05-16 13:49:21 +02:00
|
|
|
#include "BKE_object.h"
|
2013-01-03 23:27:20 +00:00
|
|
|
|
2014-06-24 17:12:38 +09:00
|
|
|
#include <sstream>
|
|
|
|
|
|
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::Span;
|
|
|
|
|
|
2013-04-09 00:46:49 +00:00
|
|
|
namespace Freestyle {
|
|
|
|
|
|
2019-01-24 07:08:19 +01:00
|
|
|
BlenderFileLoader::BlenderFileLoader(Render *re, ViewLayer *view_layer, Depsgraph *depsgraph)
|
2008-08-07 15:04:25 +00:00
|
|
|
{
|
|
|
|
|
_re = re;
|
2019-01-24 07:08:19 +01:00
|
|
|
_depsgraph = depsgraph;
|
2020-11-06 17:49:09 +01:00
|
|
|
_Scene = nullptr;
|
2010-01-24 23:12:57 +00:00
|
|
|
_numFacesRead = 0;
|
2015-08-09 23:36:53 +09:00
|
|
|
#if 0
|
2010-01-24 23:12:57 +00:00
|
|
|
_minEdgeSize = DBL_MAX;
|
2015-08-09 23:36:53 +09:00
|
|
|
#endif
|
2017-11-22 10:52:39 -02:00
|
|
|
_smooth = (view_layer->freestyle_config.flags & FREESTYLE_FACE_SMOOTHNESS_FLAG) != 0;
|
2020-11-06 17:49:09 +01:00
|
|
|
_pRenderMonitor = nullptr;
|
2008-08-07 15:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BlenderFileLoader::~BlenderFileLoader()
|
|
|
|
|
{
|
2020-11-06 17:49:09 +01:00
|
|
|
_Scene = nullptr;
|
2008-08-07 15:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-07 23:17:23 +00:00
|
|
|
NodeGroup *BlenderFileLoader::Load()
|
2008-08-07 15:04:25 +00:00
|
|
|
{
|
2013-01-03 23:27:20 +00:00
|
|
|
if (G.debug & G_DEBUG_FREESTYLE) {
|
|
|
|
|
cout << "\n=== Importing triangular meshes into Blender ===" << endl;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-10 14:08:59 +00:00
|
|
|
// creation of the scene root node
|
|
|
|
|
_Scene = new NodeGroup;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-06 11:25:27 +11:00
|
|
|
if (_re->clip_start < 0.0f) {
|
2013-07-09 23:25:02 +00:00
|
|
|
// Adjust clipping start/end and set up a Z offset when the viewport preview
|
2019-02-16 12:21:44 +11:00
|
|
|
// is used with the orthographic view. In this case, _re->clip_start is negative,
|
2013-07-09 23:25:02 +00:00
|
|
|
// while Freestyle assumes that imported mesh data are in the camera coordinate
|
2023-02-12 14:37:16 +11:00
|
|
|
// system with the view point located at origin [bug #36009].
|
2013-07-09 23:25:02 +00:00
|
|
|
_z_near = -0.001f;
|
2019-02-16 12:21:44 +11:00
|
|
|
_z_offset = _re->clip_start + _z_near;
|
|
|
|
|
_z_far = -_re->clip_end + _z_offset;
|
2013-07-09 23:25:02 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2019-02-16 12:21:44 +11:00
|
|
|
_z_near = -_re->clip_start;
|
|
|
|
|
_z_far = -_re->clip_end;
|
2020-11-06 11:25:27 +11:00
|
|
|
_z_offset = 0.0f;
|
2013-07-09 23:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
2008-08-07 15:04:25 +00:00
|
|
|
int id = 0;
|
2020-05-14 01:30:09 +02:00
|
|
|
const eEvaluationMode eval_mode = DEG_get_mode(_depsgraph);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-23 12:39:56 +02:00
|
|
|
DEGObjectIterSettings deg_iter_settings{};
|
|
|
|
|
deg_iter_settings.depsgraph = _depsgraph;
|
|
|
|
|
deg_iter_settings.flags = DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY |
|
|
|
|
|
DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET | DEG_ITER_OBJECT_FLAG_VISIBLE |
|
|
|
|
|
DEG_ITER_OBJECT_FLAG_DUPLI;
|
|
|
|
|
DEG_OBJECT_ITER_BEGIN (°_iter_settings, ob) {
|
2018-04-04 15:51:03 +02:00
|
|
|
if (_pRenderMonitor && _pRenderMonitor->testBreak()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-10-15 22:51:21 +02:00
|
|
|
if ((ob->base_flag & (BASE_HOLDOUT | BASE_INDIRECT_ONLY)) ||
|
|
|
|
|
(ob->visibility_flag & OB_HOLDOUT)) {
|
2019-01-24 07:14:28 +01:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-05-14 01:30:09 +02:00
|
|
|
if (!(BKE_object_visibility(ob, eval_mode) & OB_VISIBLE_SELF)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-17 10:20:25 -04:00
|
|
|
/* Evaluated metaballs will appear as mesh objects in the iterator. */
|
|
|
|
|
if (ob->type == OB_MBALL) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 17:49:09 +01:00
|
|
|
Mesh *mesh = BKE_object_to_mesh(nullptr, ob, false);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
if (mesh) {
|
|
|
|
|
insertShapeNode(ob, mesh, ++id);
|
2019-05-16 13:49:21 +02:00
|
|
|
BKE_object_to_mesh_clear(ob);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-01-03 23:27:20 +00:00
|
|
|
}
|
2018-04-04 15:51:03 +02:00
|
|
|
DEG_OBJECT_ITER_END;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
// Return the built scene.
|
2010-01-10 14:08:59 +00:00
|
|
|
return _Scene;
|
2008-08-07 15:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
#define CLIPPED_BY_NEAR -1
|
|
|
|
|
#define NOT_CLIPPED 0
|
|
|
|
|
#define CLIPPED_BY_FAR 1
|
|
|
|
|
|
|
|
|
|
// check if each vertex of a triangle (V1, V2, V3) is clipped by the near/far plane
|
|
|
|
|
// and calculate the number of triangles to be generated by clipping
|
2010-02-19 01:10:04 +00:00
|
|
|
int BlenderFileLoader::countClippedFaces(float v1[3], float v2[3], float v3[3], int clip[3])
|
2010-01-24 23:12:57 +00:00
|
|
|
{
|
2010-02-19 01:10:04 +00:00
|
|
|
float *v[3];
|
2013-01-18 02:13:36 +00:00
|
|
|
int numClipped, sum, numTris = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
v[0] = v1;
|
|
|
|
|
v[1] = v2;
|
|
|
|
|
v[2] = v3;
|
|
|
|
|
numClipped = sum = 0;
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2010-02-19 01:10:04 +00:00
|
|
|
if (v[i][2] > _z_near) {
|
2010-01-24 23:12:57 +00:00
|
|
|
clip[i] = CLIPPED_BY_NEAR;
|
|
|
|
|
numClipped++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else if (v[i][2] < _z_far) {
|
2010-01-24 23:12:57 +00:00
|
|
|
clip[i] = CLIPPED_BY_FAR;
|
|
|
|
|
numClipped++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2010-01-24 23:12:57 +00:00
|
|
|
clip[i] = NOT_CLIPPED;
|
|
|
|
|
}
|
2013-01-03 23:27:20 +00:00
|
|
|
#if 0
|
|
|
|
|
if (G.debug & G_DEBUG_FREESTYLE) {
|
2019-04-17 08:24:14 +02:00
|
|
|
printf("%d %s\n",
|
|
|
|
|
i,
|
|
|
|
|
(clip[i] == NOT_CLIPPED) ? "not" : (clip[i] == CLIPPED_BY_NEAR) ? "near" : "far");
|
2013-01-03 23:27:20 +00:00
|
|
|
}
|
|
|
|
|
#endif
|
2010-01-24 23:12:57 +00:00
|
|
|
sum += clip[i];
|
|
|
|
|
}
|
|
|
|
|
switch (numClipped) {
|
2018-08-30 01:31:20 +10:00
|
|
|
case 0:
|
2010-01-24 23:12:57 +00:00
|
|
|
numTris = 1; // triangle
|
2018-08-30 01:31:20 +10:00
|
|
|
break;
|
|
|
|
|
case 1:
|
2010-01-24 23:12:57 +00:00
|
|
|
numTris = 2; // tetragon
|
2018-08-30 01:31:20 +10:00
|
|
|
break;
|
|
|
|
|
case 2:
|
2019-05-31 22:51:19 +10:00
|
|
|
if (sum == 0) {
|
2018-08-30 01:31:20 +10:00
|
|
|
numTris = 3; // pentagon
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2018-08-30 01:31:20 +10:00
|
|
|
numTris = 1; // triangle
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2018-08-30 01:31:20 +10:00
|
|
|
break;
|
|
|
|
|
case 3:
|
2020-11-06 12:30:59 +11:00
|
|
|
if (ELEM(sum, 3, -3)) {
|
2018-08-30 01:31:20 +10:00
|
|
|
numTris = 0;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2018-08-30 01:31:20 +10:00
|
|
|
numTris = 2; // tetragon
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2018-08-30 01:31:20 +10:00
|
|
|
break;
|
2010-01-24 23:12:57 +00:00
|
|
|
}
|
|
|
|
|
return numTris;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the intersection point C between the line segment from V1 to V2 and
|
|
|
|
|
// a clipping plane at depth Z (i.e., the Z component of C is known, while
|
|
|
|
|
// the X and Y components are unknown).
|
2010-02-19 01:10:04 +00:00
|
|
|
void BlenderFileLoader::clipLine(float v1[3], float v2[3], float c[3], float z)
|
2010-01-24 23:12:57 +00:00
|
|
|
{
|
2011-11-20 19:03:52 +00:00
|
|
|
// Order v1 and v2 by Z values to make sure that clipLine(P, Q, c, z)
|
|
|
|
|
// and clipLine(Q, P, c, z) gives exactly the same numerical result.
|
|
|
|
|
float *p, *q;
|
|
|
|
|
if (v1[2] < v2[2]) {
|
|
|
|
|
p = v1;
|
|
|
|
|
q = v2;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2011-11-20 19:03:52 +00:00
|
|
|
p = v2;
|
|
|
|
|
q = v1;
|
|
|
|
|
}
|
2010-01-24 23:12:57 +00:00
|
|
|
double d[3];
|
2019-05-31 22:51:19 +10:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2011-11-20 19:03:52 +00:00
|
|
|
d[i] = q[i] - p[i];
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2011-11-20 19:03:52 +00:00
|
|
|
double t = (z - p[2]) / d[2];
|
|
|
|
|
c[0] = p[0] + t * d[0];
|
|
|
|
|
c[1] = p[1] + t * d[1];
|
2010-01-24 23:12:57 +00:00
|
|
|
c[2] = z;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clip the triangle (V1, V2, V3) by the near and far clipping plane and
|
|
|
|
|
// obtain a set of vertices after the clipping. The number of vertices
|
|
|
|
|
// is at most 5.
|
2010-11-27 11:23:11 +00:00
|
|
|
void BlenderFileLoader::clipTriangle(int numTris,
|
|
|
|
|
float triCoords[][3],
|
|
|
|
|
float v1[3],
|
|
|
|
|
float v2[3],
|
|
|
|
|
float v3[3],
|
2012-12-18 00:51:25 +00:00
|
|
|
float triNormals[][3],
|
|
|
|
|
float n1[3],
|
|
|
|
|
float n2[3],
|
|
|
|
|
float n3[3],
|
2023-01-23 17:31:44 +11:00
|
|
|
bool edgeMarks[5],
|
2012-12-18 00:51:25 +00:00
|
|
|
bool em1,
|
|
|
|
|
bool em2,
|
|
|
|
|
bool em3,
|
2020-07-13 11:27:09 +02:00
|
|
|
const int clip[3])
|
2010-01-24 23:12:57 +00:00
|
|
|
{
|
2010-11-27 11:23:11 +00:00
|
|
|
float *v[3], *n[3];
|
2011-10-06 02:04:43 +00:00
|
|
|
bool em[3];
|
2010-01-24 23:12:57 +00:00
|
|
|
int i, j, k;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-11-27 11:23:11 +00:00
|
|
|
v[0] = v1;
|
|
|
|
|
n[0] = n1;
|
|
|
|
|
v[1] = v2;
|
|
|
|
|
n[1] = n2;
|
|
|
|
|
v[2] = v3;
|
|
|
|
|
n[2] = n3;
|
2011-10-06 02:04:43 +00:00
|
|
|
em[0] = em1; /* edge mark of the edge between v1 and v2 */
|
|
|
|
|
em[1] = em2; /* edge mark of the edge between v2 and v3 */
|
|
|
|
|
em[2] = em3; /* edge mark of the edge between v3 and v1 */
|
2010-01-24 23:12:57 +00:00
|
|
|
k = 0;
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
|
j = (i + 1) % 3;
|
|
|
|
|
if (clip[i] == NOT_CLIPPED) {
|
2010-11-27 11:23:11 +00:00
|
|
|
copy_v3_v3(triCoords[k], v[i]);
|
|
|
|
|
copy_v3_v3(triNormals[k], n[i]);
|
2011-10-06 02:04:43 +00:00
|
|
|
edgeMarks[k] = em[i];
|
2010-11-27 11:23:11 +00:00
|
|
|
k++;
|
2010-01-24 23:12:57 +00:00
|
|
|
if (clip[j] != NOT_CLIPPED) {
|
2010-11-27 11:23:11 +00:00
|
|
|
clipLine(v[i], v[j], triCoords[k], (clip[j] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
|
|
|
|
|
copy_v3_v3(triNormals[k], n[j]);
|
2011-10-06 02:04:43 +00:00
|
|
|
edgeMarks[k] = false;
|
2010-11-27 11:23:11 +00:00
|
|
|
k++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
else if (clip[i] != clip[j]) {
|
2010-01-24 23:12:57 +00:00
|
|
|
if (clip[j] == NOT_CLIPPED) {
|
2010-11-27 11:23:11 +00:00
|
|
|
clipLine(v[i], v[j], triCoords[k], (clip[i] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
|
|
|
|
|
copy_v3_v3(triNormals[k], n[i]);
|
2011-10-06 02:04:43 +00:00
|
|
|
edgeMarks[k] = em[i];
|
2010-11-27 11:23:11 +00:00
|
|
|
k++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2010-11-27 11:23:11 +00:00
|
|
|
clipLine(v[i], v[j], triCoords[k], (clip[i] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
|
|
|
|
|
copy_v3_v3(triNormals[k], n[i]);
|
2011-10-06 02:04:43 +00:00
|
|
|
edgeMarks[k] = em[i];
|
2010-11-27 11:23:11 +00:00
|
|
|
k++;
|
|
|
|
|
clipLine(v[i], v[j], triCoords[k], (clip[j] == CLIPPED_BY_NEAR) ? _z_near : _z_far);
|
|
|
|
|
copy_v3_v3(triNormals[k], n[j]);
|
2011-10-06 02:04:43 +00:00
|
|
|
edgeMarks[k] = false;
|
2010-11-27 11:23:11 +00:00
|
|
|
k++;
|
2010-01-24 23:12:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-07-09 23:25:02 +00:00
|
|
|
BLI_assert(k == 2 + numTris);
|
2015-03-28 00:34:16 +05:00
|
|
|
(void)numTris; /* Ignored in release builds. */
|
2010-01-24 23:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
2010-11-27 11:23:11 +00:00
|
|
|
void BlenderFileLoader::addTriangle(struct LoaderState *ls,
|
|
|
|
|
float v1[3],
|
|
|
|
|
float v2[3],
|
|
|
|
|
float v3[3],
|
2012-12-18 00:51:25 +00:00
|
|
|
float n1[3],
|
|
|
|
|
float n2[3],
|
|
|
|
|
float n3[3],
|
|
|
|
|
bool fm,
|
|
|
|
|
bool em1,
|
|
|
|
|
bool em2,
|
|
|
|
|
bool em3)
|
2010-01-24 23:12:57 +00:00
|
|
|
{
|
2015-08-09 23:36:53 +09:00
|
|
|
float *fv[3], *fn[3];
|
|
|
|
|
#if 0
|
|
|
|
|
float len;
|
|
|
|
|
#endif
|
2022-09-25 17:04:52 +10:00
|
|
|
uint i, j;
|
2011-10-06 02:04:43 +00:00
|
|
|
IndexedFaceSet::FaceEdgeMark marks = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
// initialize the bounding box by the first vertex
|
|
|
|
|
if (ls->currentIndex == 0) {
|
|
|
|
|
copy_v3_v3(ls->minBBox, v1);
|
|
|
|
|
copy_v3_v3(ls->maxBBox, v1);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-11-27 11:23:11 +00:00
|
|
|
fv[0] = v1;
|
|
|
|
|
fn[0] = n1;
|
|
|
|
|
fv[1] = v2;
|
|
|
|
|
fn[1] = n2;
|
|
|
|
|
fv[2] = v3;
|
|
|
|
|
fn[2] = n3;
|
2010-01-24 23:12:57 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
copy_v3_v3(ls->pv, fv[i]);
|
2010-11-27 11:23:11 +00:00
|
|
|
copy_v3_v3(ls->pn, fn[i]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
// update the bounding box
|
2012-12-18 00:51:25 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
2019-05-31 22:51:19 +10:00
|
|
|
if (ls->minBBox[j] > ls->pv[j]) {
|
2012-12-18 00:51:25 +00:00
|
|
|
ls->minBBox[j] = ls->pv[j];
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-31 22:51:19 +10:00
|
|
|
if (ls->maxBBox[j] < ls->pv[j]) {
|
2012-12-18 00:51:25 +00:00
|
|
|
ls->maxBBox[j] = ls->pv[j];
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2010-01-24 23:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
2015-08-09 23:36:53 +09:00
|
|
|
#if 0
|
2010-01-24 23:12:57 +00:00
|
|
|
len = len_v3v3(fv[i], fv[(i + 1) % 3]);
|
2019-05-31 23:21:16 +10:00
|
|
|
if (_minEdgeSize > len) {
|
2012-12-18 00:51:25 +00:00
|
|
|
_minEdgeSize = len;
|
2019-05-31 23:21:16 +10:00
|
|
|
}
|
2015-08-09 23:36:53 +09:00
|
|
|
#endif
|
2012-12-18 00:51:25 +00:00
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
*ls->pvi = ls->currentIndex;
|
|
|
|
|
*ls->pni = ls->currentIndex;
|
|
|
|
|
*ls->pmi = ls->currentMIndex;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
ls->currentIndex += 3;
|
2010-01-24 23:12:57 +00:00
|
|
|
ls->pv += 3;
|
|
|
|
|
ls->pn += 3;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
ls->pvi++;
|
|
|
|
|
ls->pni++;
|
|
|
|
|
ls->pmi++;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-31 22:51:19 +10:00
|
|
|
if (fm) {
|
2012-12-18 00:51:25 +00:00
|
|
|
marks |= IndexedFaceSet::FACE_MARK;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
|
|
|
|
if (em1) {
|
2012-12-18 00:51:25 +00:00
|
|
|
marks |= IndexedFaceSet::EDGE_MARK_V1V2;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
|
|
|
|
if (em2) {
|
2012-12-18 00:51:25 +00:00
|
|
|
marks |= IndexedFaceSet::EDGE_MARK_V2V3;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
|
|
|
|
if (em3) {
|
2012-12-18 00:51:25 +00:00
|
|
|
marks |= IndexedFaceSet::EDGE_MARK_V3V1;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
*(ls->pm++) = marks;
|
2010-01-24 23:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-05 19:01:12 +00:00
|
|
|
// With A, B and P indicating the three vertices of a given triangle, returns:
|
|
|
|
|
// 1 if points A and B are in the same position in the 3D space;
|
|
|
|
|
// 2 if the distance between point P and line segment AB is zero; and
|
|
|
|
|
// zero otherwise.
|
|
|
|
|
int BlenderFileLoader::testDegenerateTriangle(float v1[3], float v2[3], float v3[3])
|
|
|
|
|
{
|
2013-07-12 00:18:27 +00:00
|
|
|
const float eps = 1.0e-6;
|
|
|
|
|
const float eps_sq = eps * eps;
|
|
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
#if 0
|
|
|
|
|
float area = area_tri_v3(v1, v2, v3);
|
|
|
|
|
bool verbose = (area < 1.0e-6);
|
|
|
|
|
#endif
|
2012-03-05 19:01:12 +00:00
|
|
|
|
|
|
|
|
if (equals_v3v3(v1, v2) || equals_v3v3(v2, v3) || equals_v3v3(v1, v3)) {
|
2012-12-18 00:51:25 +00:00
|
|
|
#if 0
|
2013-01-03 23:27:20 +00:00
|
|
|
if (verbose && G.debug & G_DEBUG_FREESTYLE) {
|
2012-12-18 00:51:25 +00:00
|
|
|
printf("BlenderFileLoader::testDegenerateTriangle = 1\n");
|
2013-01-03 23:27:20 +00:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
#endif
|
2012-03-05 19:01:12 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2013-07-12 00:18:27 +00:00
|
|
|
if (dist_squared_to_line_segment_v3(v1, v2, v3) < eps_sq ||
|
|
|
|
|
dist_squared_to_line_segment_v3(v2, v1, v3) < eps_sq ||
|
|
|
|
|
dist_squared_to_line_segment_v3(v3, v1, v2) < eps_sq)
|
|
|
|
|
{
|
2012-12-18 00:51:25 +00:00
|
|
|
#if 0
|
2013-01-03 23:27:20 +00:00
|
|
|
if (verbose && G.debug & G_DEBUG_FREESTYLE) {
|
2012-12-18 00:51:25 +00:00
|
|
|
printf("BlenderFileLoader::testDegenerateTriangle = 2\n");
|
2013-01-03 23:27:20 +00:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
#endif
|
2012-03-05 19:01:12 +00:00
|
|
|
return 2;
|
|
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
#if 0
|
2013-01-03 23:27:20 +00:00
|
|
|
if (verbose && G.debug & G_DEBUG_FREESTYLE) {
|
2012-12-18 00:51:25 +00:00
|
|
|
printf("BlenderFileLoader::testDegenerateTriangle = 0\n");
|
2013-01-03 23:27:20 +00:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
#endif
|
2012-03-05 19:01:12 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-13 18:31:29 +02:00
|
|
|
static bool testEdgeMark(Mesh *me, const FreestyleEdge *fed, const MLoopTri *lt, int i)
|
2012-03-05 19:01:12 +00:00
|
|
|
{
|
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 = me->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 = me->corner_verts();
|
|
|
|
|
const Span<int> corner_edges = me->corner_edges();
|
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
|
|
|
|
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 int corner = lt->tri[i];
|
|
|
|
|
const int corner_next = lt->tri[(i + 1) % 3];
|
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 blender::int2 &edge = edges[corner_edges[corner]];
|
2018-04-04 15:51:03 +02:00
|
|
|
|
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 (!ELEM(corner_verts[corner_next], edge[0], edge[1])) {
|
2018-04-04 15:51:03 +02:00
|
|
|
/* Not an edge in the original mesh before triangulation. */
|
|
|
|
|
return false;
|
2012-03-05 19:01:12 +00:00
|
|
|
}
|
2018-04-04 15:51:03 +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
|
|
|
return (fed[corner_edges[corner]].flag & FREESTYLE_EDGE_MARK) != 0;
|
2012-03-05 19:01:12 +00:00
|
|
|
}
|
2012-03-03 12:01:14 +00:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
void BlenderFileLoader::insertShapeNode(Object *ob, Mesh *me, int id)
|
2008-08-07 15:04:25 +00: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
|
|
|
using namespace blender;
|
2018-04-04 15:51:03 +02:00
|
|
|
char *name = ob->id.name + 2;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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> vert_positions = me->vert_positions();
|
Mesh: Replace MPoly struct with offset indices
Implements #95967.
Currently the `MPoly` struct is 12 bytes, and stores the index of a
face's first corner and the number of corners/verts/edges. Polygons
and corners are always created in order by Blender, meaning each
face's corners will be after the previous face's corners. We can take
advantage of this fact and eliminate the redundancy in mesh face
storage by only storing a single integer corner offset for each face.
The size of the face is then encoded by the offset of the next face.
The size of a single integer is 4 bytes, so this reduces memory
usage by 3 times.
The same method is used for `CurvesGeometry`, so Blender already has
an abstraction to simplify using these offsets called `OffsetIndices`.
This class is used to easily retrieve a range of corner indices for
each face. This also gives the opportunity for sharing some logic with
curves.
Another benefit of the change is that the offsets and sizes stored in
`MPoly` can no longer disagree with each other. Storing faces in the
order of their corners can simplify some code too.
Face/polygon variables now use the `IndexRange` type, which comes with
quite a few utilities that can simplify code.
Some:
- The offset integer array has to be one longer than the face count to
avoid a branch for every face, which means the data is no longer part
of the mesh's `CustomData`.
- We lose the ability to "reference" an original mesh's offset array
until more reusable CoW from #104478 is committed. That will be added
in a separate commit.
- Since they aren't part of `CustomData`, poly offsets often have to be
copied manually.
- To simplify using `OffsetIndices` in many places, some functions and
structs in headers were moved to only compile in C++.
- All meshes created by Blender use the same order for faces and face
corners, but just in case, meshes with mismatched order are fixed by
versioning code.
- `MeshPolygon.totloop` is no longer editable in RNA. This API break is
necessary here unfortunately. It should be worth it in 3.6, since
that's the best way to allow loading meshes from 4.0, which is
important for an LTS version.
Pull Request: https://projects.blender.org/blender/blender/pulls/105938
2023-04-04 20:39:28 +02:00
|
|
|
const OffsetIndices mesh_polys = me->polys();
|
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();
|
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
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
// Compute loop triangles
|
|
|
|
|
int tottri = poly_to_tri_count(me->totpoly, me->totloop);
|
|
|
|
|
MLoopTri *mlooptri = (MLoopTri *)MEM_malloc_arrayN(tottri, sizeof(*mlooptri), __func__);
|
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
|
|
|
blender::bke::mesh::looptris_calc(vert_positions, mesh_polys, corner_verts, {mlooptri, tottri});
|
2023-05-04 15:39:10 +02:00
|
|
|
const blender::Span<int> looptri_polys = me->looptri_polys();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
// Compute loop normals
|
|
|
|
|
BKE_mesh_calc_normals_split(me);
|
2022-05-13 18:31:29 +02:00
|
|
|
const float(*lnors)[3] = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
if (CustomData_has_layer(&me->ldata, CD_NORMAL)) {
|
2023-01-13 17:21:20 -06:00
|
|
|
lnors = (const float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL);
|
2018-04-04 15:51:03 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
// Get other mesh data
|
2023-01-13 17:21:20 -06:00
|
|
|
const FreestyleEdge *fed = (const FreestyleEdge *)CustomData_get_layer(&me->edata,
|
|
|
|
|
CD_FREESTYLE_EDGE);
|
|
|
|
|
const FreestyleFace *ffa = (const FreestyleFace *)CustomData_get_layer(&me->pdata,
|
|
|
|
|
CD_FREESTYLE_FACE);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-01-24 16:34:26 +01:00
|
|
|
// Compute view matrix
|
|
|
|
|
Object *ob_camera_eval = DEG_get_evaluated_object(_depsgraph, RE_GetCamera(_re));
|
|
|
|
|
float viewinv[4][4], viewmat[4][4];
|
|
|
|
|
RE_GetCameraModelMatrix(_re, ob_camera_eval, viewinv);
|
|
|
|
|
invert_m4_m4(viewmat, viewinv);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
// Compute matrix including camera transform
|
|
|
|
|
float obmat[4][4], nmat[4][4];
|
2022-10-24 14:16:37 +02:00
|
|
|
mul_m4_m4m4(obmat, viewmat, ob->object_to_world);
|
2018-04-04 15:51:03 +02:00
|
|
|
invert_m4_m4(nmat, obmat);
|
|
|
|
|
transpose_m4(nmat);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
// We count the number of triangles after the clipping by the near and far view
|
2021-07-03 23:08:40 +10:00
|
|
|
// planes is applied (NOTE: mesh vertices are in the camera coordinate system).
|
2022-09-26 10:04:44 +10:00
|
|
|
uint numFaces = 0;
|
2018-04-04 15:51:03 +02:00
|
|
|
float v1[3], v2[3], v3[3];
|
|
|
|
|
float n1[3], n2[3], n3[3], facenormal[3];
|
|
|
|
|
int clip[3];
|
|
|
|
|
for (int a = 0; a < tottri; a++) {
|
|
|
|
|
const MLoopTri *lt = &mlooptri[a];
|
2019-04-17 06:17:24 +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
|
|
|
copy_v3_v3(v1, vert_positions[corner_verts[lt->tri[0]]]);
|
|
|
|
|
copy_v3_v3(v2, vert_positions[corner_verts[lt->tri[1]]]);
|
|
|
|
|
copy_v3_v3(v3, vert_positions[corner_verts[lt->tri[2]]]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
mul_m4_v3(obmat, v1);
|
|
|
|
|
mul_m4_v3(obmat, v2);
|
|
|
|
|
mul_m4_v3(obmat, v3);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-07-09 23:25:02 +00:00
|
|
|
v1[2] += _z_offset;
|
|
|
|
|
v2[2] += _z_offset;
|
|
|
|
|
v3[2] += _z_offset;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
numFaces += countClippedFaces(v1, v2, v3, clip);
|
2008-08-07 15:04:25 +00:00
|
|
|
}
|
2013-01-03 23:27:20 +00:00
|
|
|
#if 0
|
|
|
|
|
if (G.debug & G_DEBUG_FREESTYLE) {
|
|
|
|
|
cout << "numFaces " << numFaces << endl;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2018-04-04 15:51:03 +02:00
|
|
|
if (numFaces == 0) {
|
|
|
|
|
MEM_freeN(mlooptri);
|
2010-01-24 23:12:57 +00:00
|
|
|
return;
|
2018-04-04 15:51:03 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-30 01:36:12 +00:00
|
|
|
// We allocate memory for the meshes to be imported
|
2014-05-19 11:10:11 +09:00
|
|
|
NodeGroup *currentMesh = new NodeGroup;
|
2012-12-18 00:51:25 +00:00
|
|
|
NodeShape *shape = new NodeShape;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-26 10:04:44 +10:00
|
|
|
uint vSize = 3 * 3 * numFaces;
|
2008-08-07 15:04:25 +00:00
|
|
|
float *vertices = new float[vSize];
|
2022-09-26 10:04:44 +10:00
|
|
|
uint nSize = vSize;
|
2008-08-07 15:04:25 +00:00
|
|
|
float *normals = new float[nSize];
|
2022-09-26 10:04:44 +10:00
|
|
|
uint *numVertexPerFaces = new uint[numFaces];
|
2014-10-23 20:26:39 +09:00
|
|
|
vector<Material *> meshMaterials;
|
2008-08-07 15:04:25 +00:00
|
|
|
vector<FrsMaterial> meshFrsMaterials;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-08-07 15:04:25 +00:00
|
|
|
IndexedFaceSet::TRIANGLES_STYLE *faceStyle = new IndexedFaceSet::TRIANGLES_STYLE[numFaces];
|
2022-09-26 10:04:44 +10:00
|
|
|
uint i;
|
2008-08-07 15:04:25 +00:00
|
|
|
for (i = 0; i < numFaces; i++) {
|
2012-12-18 00:51:25 +00:00
|
|
|
faceStyle[i] = IndexedFaceSet::TRIANGLES;
|
|
|
|
|
numVertexPerFaces[i] = 3;
|
2008-08-07 15:04:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-10-06 02:04:43 +00:00
|
|
|
IndexedFaceSet::FaceEdgeMark *faceEdgeMarks = new IndexedFaceSet::FaceEdgeMark[numFaces];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-26 10:04:44 +10:00
|
|
|
uint viSize = 3 * numFaces;
|
|
|
|
|
uint *VIndices = new uint[viSize];
|
|
|
|
|
uint niSize = viSize;
|
|
|
|
|
uint *NIndices = new uint[niSize];
|
|
|
|
|
uint *MIndices = new uint[viSize]; // Material Indices
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-01-24 23:12:57 +00:00
|
|
|
struct LoaderState ls;
|
|
|
|
|
ls.pv = vertices;
|
|
|
|
|
ls.pn = normals;
|
2011-10-06 02:04:43 +00:00
|
|
|
ls.pm = faceEdgeMarks;
|
2010-01-24 23:12:57 +00:00
|
|
|
ls.pvi = VIndices;
|
|
|
|
|
ls.pni = NIndices;
|
|
|
|
|
ls.pmi = MIndices;
|
|
|
|
|
ls.currentIndex = 0;
|
|
|
|
|
ls.currentMIndex = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-08-07 15:04:25 +00:00
|
|
|
FrsMaterial tmpMat;
|
2019-04-17 06:17:24 +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
|
|
|
const bke::AttributeAccessor attributes = me->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<int> material_indices = *attributes.lookup_or_default<int>(
|
2022-09-07 21:41:39 -05:00
|
|
|
"material_index", ATTR_DOMAIN_FACE, 0);
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<bool> sharp_faces = *attributes.lookup_or_default<bool>(
|
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
|
|
|
"sharp_face", ATTR_DOMAIN_FACE, false);
|
2022-08-31 09:09:01 -05:00
|
|
|
|
2010-01-30 01:36:12 +00:00
|
|
|
// We parse the vlak nodes again and import meshes while applying the clipping
|
|
|
|
|
// by the near and far view planes.
|
2018-04-04 15:51:03 +02:00
|
|
|
for (int a = 0; a < tottri; a++) {
|
|
|
|
|
const MLoopTri *lt = &mlooptri[a];
|
2023-05-04 15:39:10 +02:00
|
|
|
const int poly_i = looptri_polys[a];
|
|
|
|
|
Material *mat = BKE_object_material_get(ob, material_indices[poly_i] + 1);
|
2019-04-17 06:17:24 +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
|
|
|
copy_v3_v3(v1, vert_positions[corner_verts[lt->tri[0]]]);
|
|
|
|
|
copy_v3_v3(v2, vert_positions[corner_verts[lt->tri[1]]]);
|
|
|
|
|
copy_v3_v3(v3, vert_positions[corner_verts[lt->tri[2]]]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
mul_m4_v3(obmat, v1);
|
|
|
|
|
mul_m4_v3(obmat, v2);
|
|
|
|
|
mul_m4_v3(obmat, v3);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-07-09 23:25:02 +00:00
|
|
|
v1[2] += _z_offset;
|
|
|
|
|
v2[2] += _z_offset;
|
|
|
|
|
v3[2] += _z_offset;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-04 15:39:10 +02:00
|
|
|
if (_smooth && (!sharp_faces[poly_i]) && lnors) {
|
2018-04-04 15:51:03 +02:00
|
|
|
copy_v3_v3(n1, lnors[lt->tri[0]]);
|
|
|
|
|
copy_v3_v3(n2, lnors[lt->tri[1]]);
|
|
|
|
|
copy_v3_v3(n3, lnors[lt->tri[2]]);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
mul_mat3_m4_v3(nmat, n1);
|
|
|
|
|
mul_mat3_m4_v3(nmat, n2);
|
|
|
|
|
mul_mat3_m4_v3(nmat, n3);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
normalize_v3(n1);
|
|
|
|
|
normalize_v3(n2);
|
|
|
|
|
normalize_v3(n3);
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2018-04-04 15:51:03 +02:00
|
|
|
normal_tri_v3(facenormal, v3, v2, v1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
copy_v3_v3(n1, facenormal);
|
|
|
|
|
copy_v3_v3(n2, facenormal);
|
|
|
|
|
copy_v3_v3(n3, facenormal);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-25 17:04:52 +10:00
|
|
|
uint numTris = countClippedFaces(v1, v2, v3, clip);
|
2019-05-31 22:51:19 +10:00
|
|
|
if (numTris == 0) {
|
2012-12-18 00:51:25 +00:00
|
|
|
continue;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-04 15:39:10 +02:00
|
|
|
bool fm = (ffa) ? (ffa[poly_i].flag & FREESTYLE_FACE_MARK) != 0 : false;
|
2018-04-04 15:51:03 +02:00
|
|
|
bool em1 = false, em2 = false, em3 = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
if (fed) {
|
|
|
|
|
em1 = testEdgeMark(me, fed, lt, 0);
|
|
|
|
|
em2 = testEdgeMark(me, fed, lt, 1);
|
|
|
|
|
em3 = testEdgeMark(me, fed, lt, 2);
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
if (mat) {
|
2014-07-07 15:54:46 +09:00
|
|
|
tmpMat.setLine(mat->line_col[0], mat->line_col[1], mat->line_col[2], mat->line_col[3]);
|
2018-05-31 13:32:53 +02:00
|
|
|
tmpMat.setDiffuse(mat->r, mat->g, mat->b, 1.0f);
|
Remove Blender Internal and legacy viewport from Blender 2.8.
Brecht authored this commit, but he gave me the honours to actually
do it. Here it goes; Blender Internal. Bye bye, you did great!
* Point density, voxel data, ocean, environment map textures were removed,
as these only worked within BI rendering. Note that the ocean modifier
and the Cycles point density shader node continue to work.
* Dynamic paint using material shading was removed, as this only worked
with BI. If we ever wanted to support this again probably it should go
through the baking API.
* GPU shader export through the Python API was removed. This only worked
for the old BI GLSL shaders, which no longer exists. Doing something
similar for Eevee would be significantly more complicated because it
uses a lot of multiplass rendering and logic outside the shader, it's
probably impractical.
* Collada material import / export code is mostly gone, as it only worked
for BI materials. We need to add Cycles / Eevee material support at some
point.
* The mesh noise operator was removed since it only worked with BI
material texture slots. A displacement modifier can be used instead.
* The delete texture paint slot operator was removed since it only worked
for BI material texture slots. Could be added back with node support.
* Not all legacy viewport features are supported in the new viewport, but
their code was removed. If we need to bring anything back we can look at
older git revisions.
* There is some legacy viewport code that I could not remove yet, and some
that I probably missed.
* Shader node execution code was left mostly intact, even though it is not
used anywhere now. We may eventually use this to replace the texture
nodes with Cycles / Eevee shader nodes.
* The Cycles Bake panel now includes settings for baking multires normal
and displacement maps. The underlying code needs to be merged properly,
and we plan to add back support for multires AO baking and add support
to Cycles baking for features like vertex color, displacement, and other
missing baking features.
* This commit removes DNA and the Python API for BI material, lamp, world
and scene settings. This breaks a lot of addons.
* There is more DNA that can be removed or renamed, where Cycles or Eevee
are reusing some old BI properties but the names are not really correct
anymore.
* Texture slots for materials, lamps and world were removed. They remain
for brushes, particles and freestyle linestyles.
* 'BLENDER_RENDER' remains in the COMPAT_ENGINES of UI panels. Cycles and
other renderers use this to find all panels to show, minus a few panels
that they have their own replacement for.
2018-04-19 17:34:44 +02:00
|
|
|
tmpMat.setSpecular(mat->specr, mat->specg, mat->specb, 1.0f);
|
2020-11-06 11:25:27 +11:00
|
|
|
tmpMat.setShininess(128.0f);
|
2014-07-07 15:54:46 +09:00
|
|
|
tmpMat.setPriority(mat->line_priority);
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-10-23 20:26:39 +09:00
|
|
|
if (meshMaterials.empty()) {
|
|
|
|
|
meshMaterials.push_back(mat);
|
2012-12-18 00:51:25 +00:00
|
|
|
meshFrsMaterials.push_back(tmpMat);
|
|
|
|
|
shape->setFrsMaterial(tmpMat);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2014-10-23 20:26:39 +09:00
|
|
|
// find if the Blender material is already in the list
|
2022-09-25 17:04:52 +10:00
|
|
|
uint i = 0;
|
2012-12-18 00:51:25 +00:00
|
|
|
bool found = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-10-23 20:26:39 +09:00
|
|
|
for (vector<Material *>::iterator it = meshMaterials.begin(), itend = meshMaterials.end();
|
|
|
|
|
it != itend;
|
|
|
|
|
it++, i++)
|
|
|
|
|
{
|
|
|
|
|
if (*it == mat) {
|
|
|
|
|
ls.currentMIndex = i;
|
|
|
|
|
found = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
if (!found) {
|
2014-10-23 20:26:39 +09:00
|
|
|
meshMaterials.push_back(mat);
|
|
|
|
|
meshFrsMaterials.push_back(tmpMat);
|
2012-12-18 00:51:25 +00:00
|
|
|
ls.currentMIndex = meshFrsMaterials.size() - 1;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
float triCoords[5][3], triNormals[5][3];
|
|
|
|
|
bool edgeMarks[5]; // edgeMarks[i] is for the edge between i-th and (i+1)-th vertices
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
clipTriangle(
|
|
|
|
|
numTris, triCoords, v1, v2, v3, triNormals, n1, n2, n3, edgeMarks, em1, em2, em3, clip);
|
|
|
|
|
for (i = 0; i < numTris; i++) {
|
|
|
|
|
addTriangle(&ls,
|
2018-04-04 15:51:03 +02:00
|
|
|
triCoords[0],
|
|
|
|
|
triCoords[i + 1],
|
|
|
|
|
triCoords[i + 2],
|
|
|
|
|
triNormals[0],
|
|
|
|
|
triNormals[i + 1],
|
|
|
|
|
triNormals[i + 2],
|
|
|
|
|
fm,
|
|
|
|
|
(i == 0) ? edgeMarks[0] : false,
|
|
|
|
|
edgeMarks[i + 1],
|
|
|
|
|
(i == numTris - 1) ? edgeMarks[i + 2] : false);
|
|
|
|
|
_numFacesRead++;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-04-04 15:51:03 +02:00
|
|
|
MEM_freeN(mlooptri);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-17 17:05:14 +02:00
|
|
|
// We might have several times the same vertex. We want a clean
|
2012-12-18 00:51:25 +00:00
|
|
|
// shape with no real-vertex. Here, we are making a cleaning pass.
|
2020-11-06 17:49:09 +01:00
|
|
|
float *cleanVertices = nullptr;
|
2022-09-25 17:04:52 +10:00
|
|
|
uint cvSize;
|
|
|
|
|
uint *cleanVIndices = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
GeomCleaner::CleanIndexedVertexArray(
|
|
|
|
|
vertices, vSize, VIndices, viSize, &cleanVertices, &cvSize, &cleanVIndices);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-06 17:49:09 +01:00
|
|
|
float *cleanNormals = nullptr;
|
2022-09-25 17:04:52 +10:00
|
|
|
uint cnSize;
|
|
|
|
|
uint *cleanNIndices = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
GeomCleaner::CleanIndexedVertexArray(
|
|
|
|
|
normals, nSize, NIndices, niSize, &cleanNormals, &cnSize, &cleanNIndices);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-08-07 15:04:25 +00:00
|
|
|
// format materials array
|
2013-03-07 23:17:23 +00:00
|
|
|
FrsMaterial **marray = new FrsMaterial *[meshFrsMaterials.size()];
|
2022-09-25 17:04:52 +10:00
|
|
|
uint mindex = 0;
|
2013-03-07 23:17:23 +00:00
|
|
|
for (vector<FrsMaterial>::iterator m = meshFrsMaterials.begin(), mend = meshFrsMaterials.end();
|
|
|
|
|
m != mend;
|
|
|
|
|
++m)
|
|
|
|
|
{
|
2012-12-18 00:51:25 +00:00
|
|
|
marray[mindex] = new FrsMaterial(*m);
|
|
|
|
|
++mindex;
|
2008-08-07 15:04:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-08-07 15:04:25 +00:00
|
|
|
// deallocates memory:
|
|
|
|
|
delete[] vertices;
|
|
|
|
|
delete[] normals;
|
|
|
|
|
delete[] VIndices;
|
|
|
|
|
delete[] NIndices;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-03 01:03:20 +00:00
|
|
|
// Fix for degenerated triangles
|
2012-02-29 01:11:37 +00:00
|
|
|
// A degenerate triangle is a triangle such that
|
|
|
|
|
// 1) A and B are in the same position in the 3D space; or
|
|
|
|
|
// 2) the distance between point P and line segment AB is zero.
|
2012-03-03 01:03:20 +00:00
|
|
|
// Only those degenerate triangles in the second form are resolved here
|
|
|
|
|
// by adding a small offset to P, whereas those in the first form are
|
|
|
|
|
// addressed later in WShape::MakeFace().
|
|
|
|
|
vector<detri_t> detriList;
|
|
|
|
|
Vec3r zero(0.0, 0.0, 0.0);
|
2022-09-26 10:04:44 +10:00
|
|
|
uint vi0, vi1, vi2;
|
2012-02-28 20:56:31 +00:00
|
|
|
for (i = 0; i < viSize; i += 3) {
|
2012-03-03 01:03:20 +00:00
|
|
|
detri_t detri;
|
2012-02-28 20:56:31 +00:00
|
|
|
vi0 = cleanVIndices[i];
|
2013-03-07 23:17:23 +00:00
|
|
|
vi1 = cleanVIndices[i + 1];
|
|
|
|
|
vi2 = cleanVIndices[i + 2];
|
|
|
|
|
Vec3r v0(cleanVertices[vi0], cleanVertices[vi0 + 1], cleanVertices[vi0 + 2]);
|
|
|
|
|
Vec3r v1(cleanVertices[vi1], cleanVertices[vi1 + 1], cleanVertices[vi1 + 2]);
|
|
|
|
|
Vec3r v2(cleanVertices[vi2], cleanVertices[vi2 + 1], cleanVertices[vi2 + 2]);
|
2012-02-29 01:11:37 +00:00
|
|
|
if (v0 == v1 || v0 == v2 || v1 == v2) {
|
2012-05-01 18:46:15 +00:00
|
|
|
continue; // do nothing for now
|
2012-02-29 01:11:37 +00:00
|
|
|
}
|
2020-08-07 12:39:05 +02:00
|
|
|
if (GeomUtils::distPointSegment<Vec3r>(v0, v1, v2) < 1.0e-6) {
|
2012-12-18 00:51:25 +00:00
|
|
|
detri.viP = vi0;
|
|
|
|
|
detri.viA = vi1;
|
|
|
|
|
detri.viB = vi2;
|
2012-02-28 20:56:31 +00:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
else if (GeomUtils::distPointSegment<Vec3r>(v1, v0, v2) < 1.0e-6) {
|
|
|
|
|
detri.viP = vi1;
|
|
|
|
|
detri.viA = vi0;
|
|
|
|
|
detri.viB = vi2;
|
2012-02-28 20:56:31 +00:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
else if (GeomUtils::distPointSegment<Vec3r>(v2, v0, v1) < 1.0e-6) {
|
|
|
|
|
detri.viP = vi2;
|
|
|
|
|
detri.viA = vi0;
|
|
|
|
|
detri.viB = vi1;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2012-03-03 01:03:20 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-03 01:03:20 +00:00
|
|
|
detri.v = zero;
|
|
|
|
|
detri.n = 0;
|
2022-09-25 17:04:52 +10:00
|
|
|
for (uint j = 0; j < viSize; j += 3) {
|
2019-05-31 22:51:19 +10:00
|
|
|
if (i == j) {
|
2012-12-18 00:51:25 +00:00
|
|
|
continue;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2012-12-18 00:51:25 +00:00
|
|
|
vi0 = cleanVIndices[j];
|
2012-03-03 01:03:20 +00:00
|
|
|
vi1 = cleanVIndices[j + 1];
|
2013-03-07 23:17:23 +00:00
|
|
|
vi2 = cleanVIndices[j + 2];
|
|
|
|
|
Vec3r v0(cleanVertices[vi0], cleanVertices[vi0 + 1], cleanVertices[vi0 + 2]);
|
|
|
|
|
Vec3r v1(cleanVertices[vi1], cleanVertices[vi1 + 1], cleanVertices[vi1 + 2]);
|
|
|
|
|
Vec3r v2(cleanVertices[vi2], cleanVertices[vi2 + 1], cleanVertices[vi2 + 2]);
|
|
|
|
|
if (detri.viP == vi0 && (detri.viA == vi1 || detri.viB == vi1)) {
|
|
|
|
|
detri.v += (v2 - v0);
|
2012-03-03 01:03:20 +00:00
|
|
|
detri.n++;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-03-03 01:03:20 +00:00
|
|
|
else if (detri.viP == vi0 && (detri.viA == vi2 || detri.viB == vi2)) {
|
|
|
|
|
detri.v += (v1 - v0);
|
|
|
|
|
detri.n++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else if (detri.viP == vi1 && (detri.viA == vi0 || detri.viB == vi0)) {
|
2012-03-03 01:03:20 +00:00
|
|
|
detri.v += (v2 - v1);
|
|
|
|
|
detri.n++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else if (detri.viP == vi1 && (detri.viA == vi2 || detri.viB == vi2)) {
|
2012-03-03 01:03:20 +00:00
|
|
|
detri.v += (v0 - v1);
|
|
|
|
|
detri.n++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else if (detri.viP == vi2 && (detri.viA == vi0 || detri.viB == vi0)) {
|
2012-03-03 01:03:20 +00:00
|
|
|
detri.v += (v1 - v2);
|
|
|
|
|
detri.n++;
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
|
|
|
|
else if (detri.viP == vi2 && (detri.viA == vi1 || detri.viB == vi1)) {
|
2012-03-03 01:03:20 +00:00
|
|
|
detri.v += (v0 - v2);
|
|
|
|
|
detri.n++;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-03-03 01:03:20 +00:00
|
|
|
if (detri.n > 0) {
|
|
|
|
|
detri.v.normalizeSafe();
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-03-03 01:03:20 +00:00
|
|
|
detriList.push_back(detri);
|
2012-12-18 00:51:25 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-07-03 14:59:27 +02:00
|
|
|
if (!detriList.empty()) {
|
2012-12-18 00:51:25 +00:00
|
|
|
vector<detri_t>::iterator v;
|
|
|
|
|
for (v = detriList.begin(); v != detriList.end(); v++) {
|
2012-03-03 01:03:20 +00:00
|
|
|
detri_t detri = (*v);
|
2012-12-18 00:51:25 +00:00
|
|
|
if (detri.n == 0) {
|
|
|
|
|
cleanVertices[detri.viP] = cleanVertices[detri.viA];
|
|
|
|
|
cleanVertices[detri.viP + 1] = cleanVertices[detri.viA + 1];
|
2012-03-03 01:03:20 +00:00
|
|
|
cleanVertices[detri.viP + 2] = cleanVertices[detri.viA + 2];
|
2012-02-28 20:56:31 +00:00
|
|
|
}
|
2012-03-03 01:03:20 +00:00
|
|
|
else if (detri.v.norm() > 0.0) {
|
|
|
|
|
cleanVertices[detri.viP] += 1.0e-5 * detri.v.x();
|
2014-07-27 22:17:19 +09:00
|
|
|
cleanVertices[detri.viP + 1] += 1.0e-5 * detri.v.y();
|
2013-03-07 23:17:23 +00:00
|
|
|
cleanVertices[detri.viP + 2] += 1.0e-5 * detri.v.z();
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-03-07 23:17:23 +00:00
|
|
|
if (G.debug & G_DEBUG_FREESTYLE) {
|
|
|
|
|
printf("Warning: Object %s contains %lu degenerated triangle%s (strokes may be incorrect)\n",
|
2019-04-17 06:17:24 +02:00
|
|
|
name,
|
2022-09-25 18:33:28 +10:00
|
|
|
ulong(detriList.size()),
|
2012-12-18 00:51:25 +00:00
|
|
|
(detriList.size() > 1) ? "s" : "");
|
2012-03-03 01:03:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-01-03 23:27:20 +00:00
|
|
|
// Create the IndexedFaceSet with the retrieved attributes
|
|
|
|
|
IndexedFaceSet *rep;
|
|
|
|
|
rep = new IndexedFaceSet(cleanVertices,
|
2019-04-17 06:17:24 +02:00
|
|
|
cvSize,
|
2013-01-03 23:27:20 +00:00
|
|
|
cleanNormals,
|
|
|
|
|
cnSize,
|
|
|
|
|
marray,
|
2012-12-18 00:51:25 +00:00
|
|
|
meshFrsMaterials.size(),
|
2020-11-06 17:49:09 +01:00
|
|
|
nullptr,
|
2012-12-18 00:51:25 +00:00
|
|
|
0,
|
|
|
|
|
numFaces,
|
|
|
|
|
numVertexPerFaces,
|
|
|
|
|
faceStyle,
|
|
|
|
|
faceEdgeMarks,
|
|
|
|
|
cleanVIndices,
|
|
|
|
|
viSize,
|
|
|
|
|
cleanNIndices,
|
|
|
|
|
niSize,
|
|
|
|
|
MIndices,
|
|
|
|
|
viSize,
|
2020-11-06 17:49:09 +01:00
|
|
|
nullptr,
|
2012-12-18 00:51:25 +00:00
|
|
|
0,
|
|
|
|
|
0);
|
2008-08-07 15:04:25 +00:00
|
|
|
// sets the id of the rep
|
|
|
|
|
rep->setId(Id(id, 0));
|
2018-04-04 15:51:03 +02:00
|
|
|
rep->setName(ob->id.name + 2);
|
2020-06-23 09:54:14 +10:00
|
|
|
rep->setLibraryPath(ob->id.lib ? ob->id.lib->filepath : "");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-12-18 00:51:25 +00:00
|
|
|
const BBox<Vec3r> bbox = BBox<Vec3r>(Vec3r(ls.minBBox[0], ls.minBBox[1], ls.minBBox[2]),
|
2010-01-24 23:12:57 +00:00
|
|
|
Vec3r(ls.maxBBox[0], ls.maxBBox[1], ls.maxBBox[2]));
|
2008-08-07 15:04:25 +00:00
|
|
|
rep->setBBox(bbox);
|
|
|
|
|
shape->AddRep(rep);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-08-07 15:04:25 +00:00
|
|
|
currentMesh->AddChild(shape);
|
|
|
|
|
_Scene->AddChild(currentMesh);
|
|
|
|
|
}
|
2013-04-09 00:46:49 +00:00
|
|
|
|
|
|
|
|
} /* namespace Freestyle */
|