2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2010-2023 Blender Authors
|
2023-06-14 23:30:43 +10:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2010-10-06 09:53:06 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup collada
|
2011-02-27 20:30:35 +00:00
|
|
|
*/
|
|
|
|
|
|
2011-03-22 22:51:02 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "COLLADABUUtils.h"
|
2010-10-06 09:53:06 +00:00
|
|
|
#include "COLLADASWPrimitves.h"
|
|
|
|
|
#include "COLLADASWSource.h"
|
|
|
|
|
#include "COLLADASWVertices.h"
|
|
|
|
|
|
|
|
|
|
#include "GeometryExporter.h"
|
|
|
|
|
|
2025-01-26 20:08:04 +01:00
|
|
|
#include "DNA_key_types.h"
|
2010-10-06 09:53:06 +00:00
|
|
|
#include "DNA_meshdata_types.h"
|
2012-05-07 18:09:59 +00:00
|
|
|
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
#include "BLI_math_vector_types.hh"
|
2025-01-26 20:08:04 +01:00
|
|
|
#include "BLI_string.h"
|
2012-11-01 15:34:38 +00:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
#include "BKE_attribute.hh"
|
2023-11-16 11:41:55 +01:00
|
|
|
#include "BKE_customdata.hh"
|
2025-01-26 20:08:04 +01:00
|
|
|
#include "BKE_key.hh"
|
2024-01-15 12:44:04 -05:00
|
|
|
#include "BKE_lib_id.hh"
|
2025-01-09 17:26:40 +01:00
|
|
|
#include "BKE_material.hh"
|
2023-03-12 22:29:15 +01:00
|
|
|
#include "BKE_mesh.hh"
|
2012-05-07 18:09:59 +00:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
#include "collada_internal.h"
|
2012-06-07 17:55:26 +00:00
|
|
|
#include "collada_utils.h"
|
2010-10-06 09:53:06 +00: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
|
|
|
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;
|
|
|
|
|
|
Partial rewrite of the Collada Module for Blender 2.8
Most important changes are in the Animation exporter and Animation Importer.
There is still some cleaning up to be done. But the Exporter/Importer basically
work within Blender 2.8
Some details:
User Interface:
The interface has been reorganized to look more like the FBX interface.
New options in user interface:
* keep_keyframes:
When sampling the distance between 2 keyframes is defined by
the sampling rate. Furthermore the keyframes defined in the
FCurves are not exported. However when this option is enabled
then also the defined keyframes will be added to the exported fcurves
* keep_smooth_curves:
When sampling we do not use FCurves. So we also have no Curve handles
for smooth exporting. However when this option is enabled, Blender
does its best to recreate the handles for export. This is a very
experimental feature and it is know to break when:
- the exported animated objects have parent inverse matrices
different from the unit matrix
- The exported objects have negative scaling
There may be many other situations when this feature breaks.
This needs to be further tested. It may be removed later or replaced
by something less wonky.
BlenderContext:
is a new class that contains the bridge to Blender. It contains
pointers to the current export/import context plus derived values
of Depsgraph, Scene, Main
Reporting:
I reorganized the output on the Blender Console to become more
informative and more readable
Preservation of Item names:
name attributes are now encoded with XML entities. This makes
sure that i can export/import names exactly defined in the tool.
This affects material names, bone names and object names.
Hierarchy export:
* Object and Bone Hierarchies are now exported correctly
by taking the Blender parent/child hierarchy into account
* Export also not selected intermediate objects
Problem:
When we export an Object Hierarchy, then we must export
all elements of the hierarchy to maintain the transforms. This
is especially important when exporting animated objects, because the
animation curves are exported as relative curves based on the
parent-child hierarchy. If an intermediate animated object is missing
then the exported animation breaks.
Solution:
If the "Selected" Optioon is enabled, then take care
to also export all objects which are not selected and hidden,
but which are parents of selected objects.
Node Based Material Importer (wip):
Added basic support for Materials with diffuse color and
diffuse textures. More properties (opacity, emission) need
changes in the used shader.
Note: Materials are all constructed by using the principled BSDF shader.
Animation Exporter:
* Massive optimization of the Animation Bake tool (Animation Sampler).
Instead of sampling each fcurve separately, i now sample all
exported fcurves simultaneously. So i avoid many (many!)
scene updates during animation export.
* Add support for Continuous Acceleration (Fcurve handles)
This allows us to create smoother FCurves during importing Collada
Animation curves. Possibly this should become an option ionstead of
a fixed import feature.
* Add support for sampling curves (to bake animations)
* The animation sampler now can be used for any animation curve.
Before the sampler only looked at curves which are supported by
Standard Collada 1.4. However the Collada exporter currently
ignores all animation curves which are not covered by the 1.4.1
Collada Standards. There is still some room for improvements
here (work in progres)
Known issues:
* Some exports do currently not work reliably, among those
are the camera animations, material animations and light animations
those animations will be added back next (work in progres)
* Exporting animation curves with keyframes (and tangents)
sometimes results in odd curves (when parent inverse matrix is involved)
This needs to be checked in more depth (probably it can not be solved).
* Export of "all animations in scene" is disabled because the
Collada Importer can not handle this reliably at the
moment (work in progres).
* Support for Animation Clip export
Added one extra level to the exported animations
such that now all scene animations are enclosed:
<Animation name="id_name(ob)_Action">
<Animation>...</Animation>
...
</Animation>
Animation Importer:
* Import of animations for objects with multiple materials
When importing multiple materials for one object,
the imported material animation curves have all been
assigned to the first material in the object.
Error handling (wip):
The Importer was a bit confused as it sometimes ignored fatal
parsing errors and continued to import. I did my best to
unconfuse it, but i believe that this needs to be tested more.
Refactoring:
update : move generation of effect id names into own function
update : adjust importer/exporter for no longer supported HEMI lights
cleanup: Removed no lopnger existing attribute from the exporter presets
cleanup: Removed not needed Context attribute from DocumentExporter
fix : Avoid duplicate deletion of temporary items
cleanup: fixed indentation and white space issues
update : Make BCAnimation class more self contained
cleanup: Renamed classes, updated comments for better reading
cleanup: Moved static class functions to collada_utils
cleanup: Moved typedefs to more intuitive locations
cleanup: indentation and class method declarations
cleanup: Removed no longer needed methods
update : Moved Classes into separate files
cleanup: Added comments
cleanup: take care of name conventions
... : many more small changes, not helpful to list them all
2018-11-23 15:57:45 +01:00
|
|
|
void GeometryExporter::exportGeom()
|
2010-10-06 09:53:06 +00:00
|
|
|
{
|
Partial rewrite of the Collada Module for Blender 2.8
Most important changes are in the Animation exporter and Animation Importer.
There is still some cleaning up to be done. But the Exporter/Importer basically
work within Blender 2.8
Some details:
User Interface:
The interface has been reorganized to look more like the FBX interface.
New options in user interface:
* keep_keyframes:
When sampling the distance between 2 keyframes is defined by
the sampling rate. Furthermore the keyframes defined in the
FCurves are not exported. However when this option is enabled
then also the defined keyframes will be added to the exported fcurves
* keep_smooth_curves:
When sampling we do not use FCurves. So we also have no Curve handles
for smooth exporting. However when this option is enabled, Blender
does its best to recreate the handles for export. This is a very
experimental feature and it is know to break when:
- the exported animated objects have parent inverse matrices
different from the unit matrix
- The exported objects have negative scaling
There may be many other situations when this feature breaks.
This needs to be further tested. It may be removed later or replaced
by something less wonky.
BlenderContext:
is a new class that contains the bridge to Blender. It contains
pointers to the current export/import context plus derived values
of Depsgraph, Scene, Main
Reporting:
I reorganized the output on the Blender Console to become more
informative and more readable
Preservation of Item names:
name attributes are now encoded with XML entities. This makes
sure that i can export/import names exactly defined in the tool.
This affects material names, bone names and object names.
Hierarchy export:
* Object and Bone Hierarchies are now exported correctly
by taking the Blender parent/child hierarchy into account
* Export also not selected intermediate objects
Problem:
When we export an Object Hierarchy, then we must export
all elements of the hierarchy to maintain the transforms. This
is especially important when exporting animated objects, because the
animation curves are exported as relative curves based on the
parent-child hierarchy. If an intermediate animated object is missing
then the exported animation breaks.
Solution:
If the "Selected" Optioon is enabled, then take care
to also export all objects which are not selected and hidden,
but which are parents of selected objects.
Node Based Material Importer (wip):
Added basic support for Materials with diffuse color and
diffuse textures. More properties (opacity, emission) need
changes in the used shader.
Note: Materials are all constructed by using the principled BSDF shader.
Animation Exporter:
* Massive optimization of the Animation Bake tool (Animation Sampler).
Instead of sampling each fcurve separately, i now sample all
exported fcurves simultaneously. So i avoid many (many!)
scene updates during animation export.
* Add support for Continuous Acceleration (Fcurve handles)
This allows us to create smoother FCurves during importing Collada
Animation curves. Possibly this should become an option ionstead of
a fixed import feature.
* Add support for sampling curves (to bake animations)
* The animation sampler now can be used for any animation curve.
Before the sampler only looked at curves which are supported by
Standard Collada 1.4. However the Collada exporter currently
ignores all animation curves which are not covered by the 1.4.1
Collada Standards. There is still some room for improvements
here (work in progres)
Known issues:
* Some exports do currently not work reliably, among those
are the camera animations, material animations and light animations
those animations will be added back next (work in progres)
* Exporting animation curves with keyframes (and tangents)
sometimes results in odd curves (when parent inverse matrix is involved)
This needs to be checked in more depth (probably it can not be solved).
* Export of "all animations in scene" is disabled because the
Collada Importer can not handle this reliably at the
moment (work in progres).
* Support for Animation Clip export
Added one extra level to the exported animations
such that now all scene animations are enclosed:
<Animation name="id_name(ob)_Action">
<Animation>...</Animation>
...
</Animation>
Animation Importer:
* Import of animations for objects with multiple materials
When importing multiple materials for one object,
the imported material animation curves have all been
assigned to the first material in the object.
Error handling (wip):
The Importer was a bit confused as it sometimes ignored fatal
parsing errors and continued to import. I did my best to
unconfuse it, but i believe that this needs to be tested more.
Refactoring:
update : move generation of effect id names into own function
update : adjust importer/exporter for no longer supported HEMI lights
cleanup: Removed no lopnger existing attribute from the exporter presets
cleanup: Removed not needed Context attribute from DocumentExporter
fix : Avoid duplicate deletion of temporary items
cleanup: fixed indentation and white space issues
update : Make BCAnimation class more self contained
cleanup: Renamed classes, updated comments for better reading
cleanup: Moved static class functions to collada_utils
cleanup: Moved typedefs to more intuitive locations
cleanup: indentation and class method declarations
cleanup: Removed no longer needed methods
update : Moved Classes into separate files
cleanup: Added comments
cleanup: take care of name conventions
... : many more small changes, not helpful to list them all
2018-11-23 15:57:45 +01:00
|
|
|
Scene *sce = blender_context.get_scene();
|
2010-10-06 09:53:06 +00:00
|
|
|
openLibrary();
|
|
|
|
|
|
|
|
|
|
GeometryFunctor gf;
|
2019-05-23 12:17:17 +02:00
|
|
|
gf.forEachMeshObjectInExportSet<GeometryExporter>(
|
|
|
|
|
sce, *this, this->export_settings.get_export_set());
|
2010-10-06 09:53:06 +00:00
|
|
|
|
|
|
|
|
closeLibrary();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeometryExporter::operator()(Object *ob)
|
2018-06-08 08:07:48 +02:00
|
|
|
{
|
2019-05-23 12:17:17 +02:00
|
|
|
bool use_instantiation = this->export_settings.get_use_object_instantiation();
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh = bc_get_mesh_copy(blender_context,
|
2023-12-08 17:19:46 -05:00
|
|
|
ob,
|
|
|
|
|
this->export_settings.get_export_mesh_type(),
|
|
|
|
|
this->export_settings.get_apply_modifiers(),
|
|
|
|
|
this->export_settings.get_triangulate());
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-26 22:46:28 +00:00
|
|
|
std::string geom_id = get_geometry_id(ob, use_instantiation);
|
2010-10-06 09:53:06 +00:00
|
|
|
std::vector<Normal> nor;
|
2013-03-03 13:53:32 +00:00
|
|
|
std::vector<BCPolygonNormalsIndices> norind;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* Skip if linked geometry was already exported from another reference */
|
2012-06-12 22:05:33 +00:00
|
|
|
if (use_instantiation && exportedGeometry.find(geom_id) != exportedGeometry.end()) {
|
2010-11-16 22:27:31 +00:00
|
|
|
return;
|
2012-06-12 22:05:33 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-26 22:46:28 +00:00
|
|
|
std::string geom_name = (use_instantiation) ? id_name(ob->data) : id_name(ob);
|
Partial rewrite of the Collada Module for Blender 2.8
Most important changes are in the Animation exporter and Animation Importer.
There is still some cleaning up to be done. But the Exporter/Importer basically
work within Blender 2.8
Some details:
User Interface:
The interface has been reorganized to look more like the FBX interface.
New options in user interface:
* keep_keyframes:
When sampling the distance between 2 keyframes is defined by
the sampling rate. Furthermore the keyframes defined in the
FCurves are not exported. However when this option is enabled
then also the defined keyframes will be added to the exported fcurves
* keep_smooth_curves:
When sampling we do not use FCurves. So we also have no Curve handles
for smooth exporting. However when this option is enabled, Blender
does its best to recreate the handles for export. This is a very
experimental feature and it is know to break when:
- the exported animated objects have parent inverse matrices
different from the unit matrix
- The exported objects have negative scaling
There may be many other situations when this feature breaks.
This needs to be further tested. It may be removed later or replaced
by something less wonky.
BlenderContext:
is a new class that contains the bridge to Blender. It contains
pointers to the current export/import context plus derived values
of Depsgraph, Scene, Main
Reporting:
I reorganized the output on the Blender Console to become more
informative and more readable
Preservation of Item names:
name attributes are now encoded with XML entities. This makes
sure that i can export/import names exactly defined in the tool.
This affects material names, bone names and object names.
Hierarchy export:
* Object and Bone Hierarchies are now exported correctly
by taking the Blender parent/child hierarchy into account
* Export also not selected intermediate objects
Problem:
When we export an Object Hierarchy, then we must export
all elements of the hierarchy to maintain the transforms. This
is especially important when exporting animated objects, because the
animation curves are exported as relative curves based on the
parent-child hierarchy. If an intermediate animated object is missing
then the exported animation breaks.
Solution:
If the "Selected" Optioon is enabled, then take care
to also export all objects which are not selected and hidden,
but which are parents of selected objects.
Node Based Material Importer (wip):
Added basic support for Materials with diffuse color and
diffuse textures. More properties (opacity, emission) need
changes in the used shader.
Note: Materials are all constructed by using the principled BSDF shader.
Animation Exporter:
* Massive optimization of the Animation Bake tool (Animation Sampler).
Instead of sampling each fcurve separately, i now sample all
exported fcurves simultaneously. So i avoid many (many!)
scene updates during animation export.
* Add support for Continuous Acceleration (Fcurve handles)
This allows us to create smoother FCurves during importing Collada
Animation curves. Possibly this should become an option ionstead of
a fixed import feature.
* Add support for sampling curves (to bake animations)
* The animation sampler now can be used for any animation curve.
Before the sampler only looked at curves which are supported by
Standard Collada 1.4. However the Collada exporter currently
ignores all animation curves which are not covered by the 1.4.1
Collada Standards. There is still some room for improvements
here (work in progres)
Known issues:
* Some exports do currently not work reliably, among those
are the camera animations, material animations and light animations
those animations will be added back next (work in progres)
* Exporting animation curves with keyframes (and tangents)
sometimes results in odd curves (when parent inverse matrix is involved)
This needs to be checked in more depth (probably it can not be solved).
* Export of "all animations in scene" is disabled because the
Collada Importer can not handle this reliably at the
moment (work in progres).
* Support for Animation Clip export
Added one extra level to the exported animations
such that now all scene animations are enclosed:
<Animation name="id_name(ob)_Action">
<Animation>...</Animation>
...
</Animation>
Animation Importer:
* Import of animations for objects with multiple materials
When importing multiple materials for one object,
the imported material animation curves have all been
assigned to the first material in the object.
Error handling (wip):
The Importer was a bit confused as it sometimes ignored fatal
parsing errors and continued to import. I did my best to
unconfuse it, but i believe that this needs to be tested more.
Refactoring:
update : move generation of effect id names into own function
update : adjust importer/exporter for no longer supported HEMI lights
cleanup: Removed no lopnger existing attribute from the exporter presets
cleanup: Removed not needed Context attribute from DocumentExporter
fix : Avoid duplicate deletion of temporary items
cleanup: fixed indentation and white space issues
update : Make BCAnimation class more self contained
cleanup: Renamed classes, updated comments for better reading
cleanup: Moved static class functions to collada_utils
cleanup: Moved typedefs to more intuitive locations
cleanup: indentation and class method declarations
cleanup: Removed no longer needed methods
update : Moved Classes into separate files
cleanup: Added comments
cleanup: take care of name conventions
... : many more small changes, not helpful to list them all
2018-11-23 15:57:45 +01:00
|
|
|
geom_name = encode_xml(geom_name);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-11-16 22:27:31 +00:00
|
|
|
exportedGeometry.insert(geom_id);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-26 20:08:04 +01:00
|
|
|
bool has_color = CustomData_has_layer(&mesh->fdata_legacy, CD_MCOL);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
create_normals(nor, norind, mesh);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* openMesh(geoId, geoName, meshId) */
|
2012-02-02 10:34:44 +00:00
|
|
|
openMesh(geom_id, geom_name);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for vertex coords */
|
2023-12-08 16:40:06 -05:00
|
|
|
createVertsSource(geom_id, mesh);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for normal coords */
|
2023-12-08 16:40:06 -05:00
|
|
|
createNormalsSource(geom_id, mesh, nor);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2025-01-26 20:08:04 +01:00
|
|
|
bool has_uvs = CustomData_has_layer(&mesh->corner_data, CD_PROP_FLOAT2);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for uv coords if mesh has uv coords */
|
2013-03-02 15:58:13 +00:00
|
|
|
if (has_uvs) {
|
2023-12-08 16:40:06 -05:00
|
|
|
createTexcoordsSource(geom_id, mesh);
|
2013-03-02 15:58:13 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-02 15:58:13 +00:00
|
|
|
if (has_color) {
|
2023-12-08 16:40:06 -05:00
|
|
|
createVertexColorSource(geom_id, mesh);
|
2013-03-02 15:58:13 +00:00
|
|
|
}
|
2019-04-30 13:41:21 +10:00
|
|
|
/* <vertices> */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
COLLADASW::Vertices verts(mSW);
|
2011-01-27 19:39:06 +00:00
|
|
|
verts.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX));
|
2010-10-06 09:53:06 +00:00
|
|
|
COLLADASW::InputList &input_list = verts.getInputList();
|
2011-01-27 19:39:06 +00:00
|
|
|
COLLADASW::Input input(COLLADASW::InputSemantic::POSITION,
|
|
|
|
|
getUrlBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
|
2010-10-06 09:53:06 +00:00
|
|
|
input_list.push_back(input);
|
|
|
|
|
verts.add();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
createLooseEdgeList(ob, mesh, geom_id);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-12 14:23:16 +11:00
|
|
|
/* Only create poly-lists if number of faces > 0. */
|
2023-12-08 16:40:06 -05:00
|
|
|
if (mesh->totface_legacy > 0) {
|
2019-04-30 13:41:21 +10:00
|
|
|
/* XXX slow */
|
2012-09-12 18:26:01 +00:00
|
|
|
if (ob->totcol) {
|
|
|
|
|
for (int a = 0; a < ob->totcol; a++) {
|
2023-12-08 16:40:06 -05:00
|
|
|
create_mesh_primitive_list(a, has_uvs, has_color, ob, mesh, geom_id, norind);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-09-12 18:26:01 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2023-12-08 16:40:06 -05:00
|
|
|
create_mesh_primitive_list(0, has_uvs, has_color, ob, mesh, geom_id, norind);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
closeMesh();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
closeGeometry();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-05-23 12:17:17 +02:00
|
|
|
if (this->export_settings.get_include_shapekeys()) {
|
2018-10-11 08:49:28 +11:00
|
|
|
Key *key = BKE_key_from_object(ob);
|
2013-02-10 17:06:05 +00:00
|
|
|
if (key) {
|
2023-12-08 16:40:06 -05:00
|
|
|
blender::MutableSpan<float3> positions = mesh->vert_positions_for_write();
|
2018-10-11 08:49:28 +11:00
|
|
|
KeyBlock *kb = (KeyBlock *)key->block.first;
|
2019-04-30 13:41:21 +10:00
|
|
|
/* skip the basis */
|
2013-01-21 13:45:49 +00:00
|
|
|
kb = kb->next;
|
|
|
|
|
for (; kb; kb = kb->next) {
|
2025-04-22 09:39:48 -04:00
|
|
|
BKE_keyblock_convert_to_mesh(kb, positions);
|
2023-12-08 16:40:06 -05:00
|
|
|
export_key_mesh(ob, mesh, kb);
|
2013-01-21 13:45:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
BKE_id_free(nullptr, mesh);
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void GeometryExporter::export_key_mesh(Object *ob, Mesh *mesh, KeyBlock *kb)
|
2013-02-10 17:06:05 +00:00
|
|
|
{
|
2013-01-21 13:45:49 +00:00
|
|
|
std::string geom_id = get_geometry_id(ob, false) + "_morph_" + translate_id(kb->name);
|
|
|
|
|
std::vector<Normal> nor;
|
2013-03-03 13:53:32 +00:00
|
|
|
std::vector<BCPolygonNormalsIndices> norind;
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
if (exportedGeometry.find(geom_id) != exportedGeometry.end()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-12 17:52:18 +00:00
|
|
|
std::string geom_name = kb->name;
|
2013-01-21 13:45:49 +00:00
|
|
|
|
|
|
|
|
exportedGeometry.insert(geom_id);
|
|
|
|
|
|
2025-01-26 20:08:04 +01:00
|
|
|
bool has_color = CustomData_has_layer(&mesh->fdata_legacy, CD_MCOL);
|
2013-01-21 13:45:49 +00:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
create_normals(nor, norind, mesh);
|
2013-01-21 13:45:49 +00:00
|
|
|
|
|
|
|
|
// openMesh(geoId, geoName, meshId)
|
|
|
|
|
openMesh(geom_id, geom_name);
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for vertex coords */
|
2023-12-08 16:40:06 -05:00
|
|
|
createVertsSource(geom_id, mesh);
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for normal coords */
|
2023-12-08 16:40:06 -05:00
|
|
|
createNormalsSource(geom_id, mesh, nor);
|
2013-01-21 13:45:49 +00:00
|
|
|
|
2025-01-26 20:08:04 +01:00
|
|
|
bool has_uvs = CustomData_has_layer(&mesh->corner_data, CD_PROP_FLOAT2);
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for uv coords if mesh has uv coords */
|
2013-03-02 15:58:13 +00:00
|
|
|
if (has_uvs) {
|
2023-12-08 16:40:06 -05:00
|
|
|
createTexcoordsSource(geom_id, mesh);
|
2013-03-02 15:58:13 +00:00
|
|
|
}
|
2013-01-21 13:45:49 +00:00
|
|
|
|
2013-03-02 15:58:13 +00:00
|
|
|
if (has_color) {
|
2023-12-08 16:40:06 -05:00
|
|
|
createVertexColorSource(geom_id, mesh);
|
2013-03-02 15:58:13 +00:00
|
|
|
}
|
2013-01-21 13:45:49 +00:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* <vertices> */
|
2013-01-21 13:45:49 +00:00
|
|
|
|
|
|
|
|
COLLADASW::Vertices verts(mSW);
|
|
|
|
|
verts.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX));
|
|
|
|
|
COLLADASW::InputList &input_list = verts.getInputList();
|
|
|
|
|
COLLADASW::Input input(COLLADASW::InputSemantic::POSITION,
|
|
|
|
|
getUrlBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
|
|
|
|
|
input_list.push_back(input);
|
|
|
|
|
verts.add();
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
// createLooseEdgeList(ob, mesh, geom_id, norind);
|
2013-01-21 13:45:49 +00:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* XXX slow */
|
2013-01-21 13:45:49 +00:00
|
|
|
if (ob->totcol) {
|
|
|
|
|
for (int a = 0; a < ob->totcol; a++) {
|
2023-12-08 16:40:06 -05:00
|
|
|
create_mesh_primitive_list(a, has_uvs, has_color, ob, mesh, geom_id, norind);
|
2013-01-21 13:45:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-12-08 16:40:06 -05:00
|
|
|
create_mesh_primitive_list(0, has_uvs, has_color, ob, mesh, geom_id, norind);
|
2013-01-21 13:45:49 +00:00
|
|
|
}
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
closeMesh();
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2013-01-21 13:45:49 +00:00
|
|
|
closeGeometry();
|
|
|
|
|
}
|
2012-05-16 11:21:03 +00:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void GeometryExporter::createLooseEdgeList(Object *ob, Mesh *mesh, std::string &geom_id)
|
2012-05-16 11:21:03 +00:00
|
|
|
{
|
2022-11-18 16:05:06 -06:00
|
|
|
using namespace blender;
|
2023-12-08 16:40:06 -05:00
|
|
|
const Span<int2> edges = mesh->edges();
|
2012-05-16 11:21:03 +00:00
|
|
|
int edges_in_linelist = 0;
|
2022-09-25 17:04:52 +10:00
|
|
|
std::vector<uint> edge_list;
|
2012-05-16 11:21:03 +00:00
|
|
|
int index;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* Find all loose edges in Mesh
|
|
|
|
|
* and save vertex indices in edge_list */
|
2023-12-08 16:40:06 -05:00
|
|
|
const bke::LooseEdgeCache &loose_edges = mesh->loose_edges();
|
2022-11-18 16:05:06 -06:00
|
|
|
if (loose_edges.count > 0) {
|
|
|
|
|
for (const int64_t i : edges.index_range()) {
|
|
|
|
|
if (loose_edges.is_loose_bits[i]) {
|
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 int2 &edge = edges[i];
|
2022-11-18 16:05:06 -06:00
|
|
|
edges_in_linelist += 1;
|
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
|
|
|
edge_list.push_back(edge[0]);
|
|
|
|
|
edge_list.push_back(edge[1]);
|
2022-11-18 16:05:06 -06:00
|
|
|
}
|
2012-05-16 11:21:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-16 11:21:03 +00:00
|
|
|
if (edges_in_linelist > 0) {
|
2019-04-30 13:41:21 +10:00
|
|
|
/* Create the list of loose edges */
|
2012-05-16 11:21:03 +00:00
|
|
|
COLLADASW::Lines lines(mSW);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-16 11:21:03 +00:00
|
|
|
lines.setCount(edges_in_linelist);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-16 11:21:03 +00:00
|
|
|
COLLADASW::InputList &til = lines.getInputList();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* creates <input> in <lines> for vertices */
|
2012-05-16 11:21:03 +00:00
|
|
|
COLLADASW::Input input1(COLLADASW::InputSemantic::VERTEX,
|
|
|
|
|
getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX),
|
|
|
|
|
0);
|
|
|
|
|
til.push_back(input1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-16 11:21:03 +00:00
|
|
|
lines.prepareToAppendValues();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-06-08 08:07:48 +02:00
|
|
|
for (index = 0; index < edges_in_linelist; index++) {
|
2012-06-12 22:05:33 +00:00
|
|
|
lines.appendValues(edge_list[2 * index + 1]);
|
|
|
|
|
lines.appendValues(edge_list[2 * index]);
|
2012-05-16 11:21:03 +00:00
|
|
|
}
|
|
|
|
|
lines.finish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 17:45:25 +01:00
|
|
|
static void prepareToAppendValues(bool is_triangulated,
|
|
|
|
|
COLLADASW::PrimitivesBase &primitive_list,
|
2022-09-25 17:04:52 +10:00
|
|
|
std::vector<ulong> &vcount_list)
|
2019-02-19 17:45:25 +01:00
|
|
|
{
|
2019-04-30 13:41:21 +10:00
|
|
|
/* performs the actual writing */
|
2019-02-19 17:45:25 +01:00
|
|
|
if (is_triangulated) {
|
|
|
|
|
((COLLADASW::Triangles &)primitive_list).prepareToAppendValues();
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-04-30 13:41:21 +10:00
|
|
|
/* sets <vcount> */
|
2019-02-19 17:45:25 +01:00
|
|
|
primitive_list.setVCountList(vcount_list);
|
|
|
|
|
((COLLADASW::Polylist &)primitive_list).prepareToAppendValues();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void finish_and_delete_primitive_List(bool is_triangulated,
|
|
|
|
|
COLLADASW::PrimitivesBase *primitive_list)
|
|
|
|
|
{
|
|
|
|
|
if (is_triangulated) {
|
|
|
|
|
((COLLADASW::Triangles *)primitive_list)->finish();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
((COLLADASW::Polylist *)primitive_list)->finish();
|
|
|
|
|
}
|
|
|
|
|
delete primitive_list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static COLLADASW::PrimitivesBase *create_primitive_list(bool is_triangulated,
|
|
|
|
|
COLLADASW::StreamWriter *mSW)
|
|
|
|
|
{
|
|
|
|
|
COLLADASW::PrimitivesBase *primitive_list;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-19 17:45:25 +01:00
|
|
|
if (is_triangulated) {
|
|
|
|
|
primitive_list = new COLLADASW::Triangles(mSW);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
primitive_list = new COLLADASW::Polylist(mSW);
|
|
|
|
|
}
|
|
|
|
|
return primitive_list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
static bool collect_vertex_counts_per_poly(Mesh *mesh,
|
2019-02-19 17:45:25 +01:00
|
|
|
int material_index,
|
2022-09-25 17:04:52 +10:00
|
|
|
std::vector<ulong> &vcount_list)
|
2019-02-19 17:45:25 +01:00
|
|
|
{
|
2023-12-20 13:13:16 -05:00
|
|
|
using namespace blender;
|
2023-12-08 16:40:06 -05:00
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
|
|
|
|
const blender::bke::AttributeAccessor attributes = mesh->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const blender::VArray<int> material_indices = *attributes.lookup_or_default<int>(
|
2023-12-20 13:13:16 -05:00
|
|
|
"material_index", bke::AttrDomain::Face, 0);
|
2019-02-19 17:45:25 +01:00
|
|
|
bool is_triangulated = true;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
/* Expecting that the material index is always 0 if the mesh has no materials assigned */
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
2022-08-31 09:09:01 -05:00
|
|
|
if (material_indices[i] == material_index) {
|
2023-07-24 22:06:55 +02:00
|
|
|
const int vertex_count = faces[i].size();
|
2019-02-19 17:45:25 +01:00
|
|
|
vcount_list.push_back(vertex_count);
|
2019-05-31 22:51:19 +10:00
|
|
|
if (vertex_count != 3) {
|
2019-02-19 17:45:25 +01:00
|
|
|
is_triangulated = false;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2019-02-19 17:45:25 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return is_triangulated;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 18:46:06 +01:00
|
|
|
std::string GeometryExporter::makeVertexColorSourceId(std::string &geom_id, const char *layer_name)
|
2014-05-01 14:52:10 +02:00
|
|
|
{
|
|
|
|
|
std::string result = getIdBySemantics(geom_id, COLLADASW::InputSemantic::COLOR) + "-" +
|
|
|
|
|
layer_name;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 17:45:25 +01:00
|
|
|
void GeometryExporter::create_mesh_primitive_list(short material_index,
|
2013-02-27 13:53:43 +00:00
|
|
|
bool has_uvs,
|
|
|
|
|
bool has_color,
|
|
|
|
|
Object *ob,
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh,
|
2013-02-27 13:53:43 +00:00
|
|
|
std::string &geom_id,
|
2013-03-03 13:53:32 +00:00
|
|
|
std::vector<BCPolygonNormalsIndices> &norind)
|
2013-02-27 13:53:43 +00:00
|
|
|
{
|
2023-12-20 13:13:16 -05:00
|
|
|
using namespace blender;
|
2023-12-08 16:40:06 -05:00
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
|
|
|
|
const Span<int> corner_verts = mesh->corner_verts();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-25 17:04:52 +10:00
|
|
|
std::vector<ulong> vcount_list;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
bool is_triangulated = collect_vertex_counts_per_poly(mesh, material_index, vcount_list);
|
2019-02-19 17:45:25 +01:00
|
|
|
int polygon_count = vcount_list.size();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* no faces using this material */
|
2019-02-19 17:45:25 +01:00
|
|
|
if (polygon_count == 0) {
|
2013-02-27 13:53:43 +00:00
|
|
|
fprintf(
|
|
|
|
|
stderr, "%s: material with index %d is not used.\n", id_name(ob).c_str(), material_index);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-06 17:49:09 +01:00
|
|
|
Material *ma = ob->totcol ? BKE_object_material_get(ob, material_index + 1) : nullptr;
|
2019-02-19 17:45:25 +01:00
|
|
|
COLLADASW::PrimitivesBase *primitive_list = create_primitive_list(is_triangulated, mSW);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-24 11:21:18 +10:00
|
|
|
/* sets count attribute in `<polylist>`. */
|
2019-02-19 17:45:25 +01:00
|
|
|
primitive_list->setCount(polygon_count);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* sets material name */
|
2013-02-27 13:53:43 +00:00
|
|
|
if (ma) {
|
|
|
|
|
std::string material_id = get_material_id(ma);
|
|
|
|
|
std::ostringstream ostr;
|
|
|
|
|
ostr << translate_id(material_id);
|
2019-02-19 17:45:25 +01:00
|
|
|
primitive_list->setMaterial(ostr.str());
|
2013-02-27 13:53:43 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-19 17:45:25 +01:00
|
|
|
COLLADASW::Input vertex_input(COLLADASW::InputSemantic::VERTEX,
|
|
|
|
|
getUrlBySemantics(geom_id, COLLADASW::InputSemantic::VERTEX),
|
|
|
|
|
0);
|
|
|
|
|
COLLADASW::Input normals_input(COLLADASW::InputSemantic::NORMAL,
|
|
|
|
|
getUrlBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL),
|
|
|
|
|
1);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-19 17:45:25 +01:00
|
|
|
COLLADASW::InputList &til = primitive_list->getInputList();
|
|
|
|
|
til.push_back(vertex_input);
|
|
|
|
|
til.push_back(normals_input);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* if mesh has uv coords writes <input> for TEXCOORD */
|
2023-12-19 20:38:59 -05:00
|
|
|
int num_layers = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2);
|
|
|
|
|
int active_uv = CustomData_get_active_layer(&mesh->corner_data, CD_PROP_FLOAT2);
|
2019-02-19 17:45:25 +01:00
|
|
|
for (int i = 0; i < num_layers; i++) {
|
2023-06-20 16:24:33 +02:00
|
|
|
if (!this->export_settings.get_active_uv_only() || i == active_uv) {
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
// char *name = CustomData_get_layer_name(&mesh->ldata, CD_PROP_FLOAT2, i);
|
2019-02-19 17:45:25 +01:00
|
|
|
COLLADASW::Input texcoord_input(
|
|
|
|
|
COLLADASW::InputSemantic::TEXCOORD,
|
2019-05-23 12:17:17 +02:00
|
|
|
makeUrl(makeTexcoordSourceId(geom_id, i, this->export_settings.get_active_uv_only())),
|
2013-12-09 23:18:01 +01:00
|
|
|
2, /* this is only until we have optimized UV sets */
|
2023-07-20 11:30:25 +10:00
|
|
|
this->export_settings.get_active_uv_only() ? 0 : i /* set (0,1,2,...) */
|
2013-02-27 13:53:43 +00:00
|
|
|
);
|
2019-02-19 17:45:25 +01:00
|
|
|
til.push_back(texcoord_input);
|
2013-02-27 13:53:43 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-19 20:38:59 -05:00
|
|
|
int totlayer_mcol = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_BYTE_COLOR);
|
2014-05-01 14:52:10 +02:00
|
|
|
if (totlayer_mcol > 0) {
|
|
|
|
|
int map_index = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-05-01 14:52:10 +02:00
|
|
|
for (int a = 0; a < totlayer_mcol; a++) {
|
2023-12-08 17:19:46 -05:00
|
|
|
const char *layer_name = bc_CustomData_get_layer_name(
|
2023-12-19 20:38:59 -05:00
|
|
|
&mesh->corner_data, CD_PROP_BYTE_COLOR, a);
|
2014-05-01 14:52:10 +02:00
|
|
|
COLLADASW::Input input4(COLLADASW::InputSemantic::COLOR,
|
|
|
|
|
makeUrl(makeVertexColorSourceId(geom_id, layer_name)),
|
|
|
|
|
(has_uvs) ? 3 : 2, /* all color layers have same index order */
|
|
|
|
|
map_index /* set number equals color map index */
|
|
|
|
|
);
|
|
|
|
|
til.push_back(input4);
|
|
|
|
|
map_index++;
|
2013-02-27 13:53:43 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* performs the actual writing */
|
2019-02-19 17:45:25 +01:00
|
|
|
prepareToAppendValues(is_triangulated, *primitive_list, vcount_list);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
const blender::bke::AttributeAccessor attributes = mesh->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const blender::VArray<int> material_indices = *attributes.lookup_or_default<int>(
|
2023-12-20 13:13:16 -05:00
|
|
|
"material_index", bke::AttrDomain::Face, 0);
|
2022-08-31 09:09:01 -05:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* <p> */
|
2013-02-27 13:53:43 +00:00
|
|
|
int texindex = 0;
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
const blender::IndexRange poly = faces[i];
|
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
|
|
|
int loop_count = poly.size();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
if (material_indices[i] == material_index) {
|
2013-03-03 13:53:32 +00:00
|
|
|
BCPolygonNormalsIndices normal_indices = norind[i];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-02-27 13:53:43 +00:00
|
|
|
for (int j = 0; j < loop_count; j++) {
|
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 int vert = corner_verts[poly[j]];
|
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
|
|
|
primitive_list->appendValues(vert);
|
2019-02-19 17:45:25 +01:00
|
|
|
primitive_list->appendValues(normal_indices[j]);
|
2019-05-31 22:51:19 +10:00
|
|
|
if (has_uvs) {
|
2019-02-19 17:45:25 +01:00
|
|
|
primitive_list->appendValues(texindex + 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 (has_color) {
|
2019-02-19 17:45:25 +01:00
|
|
|
primitive_list->appendValues(texindex + j);
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2013-02-27 13:53:43 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-02-27 13:53:43 +00:00
|
|
|
texindex += loop_count;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-02-19 17:45:25 +01:00
|
|
|
finish_and_delete_primitive_List(is_triangulated, primitive_list);
|
2013-02-27 13:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void GeometryExporter::createVertsSource(std::string geom_id, Mesh *mesh)
|
2010-10-06 09:53:06 +00:00
|
|
|
{
|
2023-12-08 16:40:06 -05:00
|
|
|
const Span<float3> positions = mesh->vert_positions();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
COLLADASW::FloatSourceF source(mSW);
|
2011-01-27 19:39:06 +00:00
|
|
|
source.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::POSITION));
|
|
|
|
|
source.setArrayId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::POSITION) +
|
2012-06-12 22:05:33 +00:00
|
|
|
ARRAY_ID_SUFFIX);
|
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
|
|
|
source.setAccessorCount(positions.size());
|
2010-10-06 09:53:06 +00:00
|
|
|
source.setAccessorStride(3);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
|
2025-01-26 20:08:04 +01:00
|
|
|
param.emplace_back("X");
|
|
|
|
|
param.emplace_back("Y");
|
|
|
|
|
param.emplace_back("Z");
|
2012-10-04 13:26:15 +00:00
|
|
|
/* main function, it creates <source id = "">, <float_array id = ""
|
|
|
|
|
* count = ""> */
|
2010-10-06 09:53:06 +00:00
|
|
|
source.prepareToAppendValues();
|
2019-04-30 13:41:21 +10:00
|
|
|
/* appends data to <float_array> */
|
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
|
|
|
for (const int i : positions.index_range()) {
|
2019-05-23 12:17:17 +02:00
|
|
|
Vector co;
|
|
|
|
|
if (export_settings.get_apply_global_orientation()) {
|
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
|
|
|
float co_c[3];
|
|
|
|
|
copy_v3_v3(co_c, positions[i]);
|
|
|
|
|
bc_add_global_transform(co, co_c, export_settings.get_global_transform());
|
2019-05-23 12:17:17 +02:00
|
|
|
}
|
|
|
|
|
else {
|
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
|
|
|
copy_v3_v3(co, positions[i]);
|
2019-05-23 12:17:17 +02:00
|
|
|
}
|
|
|
|
|
source.appendValues(co[0], co[1], co[2]);
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
source.finish();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void GeometryExporter::createVertexColorSource(std::string geom_id, Mesh *mesh)
|
2010-10-06 09:53:06 +00:00
|
|
|
{
|
2014-05-01 14:52:10 +02:00
|
|
|
/* Find number of vertex color layers */
|
2023-12-19 20:38:59 -05:00
|
|
|
int totlayer_mcol = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_BYTE_COLOR);
|
2019-05-31 22:51:19 +10:00
|
|
|
if (totlayer_mcol == 0) {
|
2010-10-06 09:53:06 +00:00
|
|
|
return;
|
2019-05-31 22:51:19 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-05-01 14:52:10 +02:00
|
|
|
int map_index = 0;
|
|
|
|
|
for (int a = 0; a < totlayer_mcol; a++) {
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-05-01 14:52:10 +02:00
|
|
|
map_index++;
|
2022-05-14 18:57:52 +02:00
|
|
|
const MLoopCol *mloopcol = (const MLoopCol *)CustomData_get_layer_n(
|
2023-12-19 20:38:59 -05:00
|
|
|
&mesh->corner_data, CD_PROP_BYTE_COLOR, a);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-05-01 14:52:10 +02:00
|
|
|
COLLADASW::FloatSourceF source(mSW);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-19 20:38:59 -05:00
|
|
|
const char *layer_name = bc_CustomData_get_layer_name(
|
|
|
|
|
&mesh->corner_data, CD_PROP_BYTE_COLOR, a);
|
2014-05-01 14:52:10 +02:00
|
|
|
std::string layer_id = makeVertexColorSourceId(geom_id, layer_name);
|
|
|
|
|
source.setId(layer_id);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-05-01 14:52:10 +02:00
|
|
|
source.setNodeName(layer_name);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-05-01 14:52:10 +02:00
|
|
|
source.setArrayId(layer_id + ARRAY_ID_SUFFIX);
|
2023-12-20 02:21:48 +01:00
|
|
|
source.setAccessorCount(mesh->corners_num);
|
2017-09-29 18:12:14 +10:00
|
|
|
source.setAccessorStride(4);
|
2010-10-06 09:53:06 +00:00
|
|
|
|
2012-06-22 16:16:58 +00:00
|
|
|
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
|
2025-01-26 20:08:04 +01:00
|
|
|
param.emplace_back("R");
|
|
|
|
|
param.emplace_back("G");
|
|
|
|
|
param.emplace_back("B");
|
|
|
|
|
param.emplace_back("A");
|
2013-03-02 15:58:13 +00:00
|
|
|
|
2013-12-09 23:18:01 +01:00
|
|
|
source.prepareToAppendValues();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
for (const int corner : faces[i]) {
|
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 MLoopCol *mlc = &mloopcol[corner];
|
2010-10-06 09:53:06 +00:00
|
|
|
source.appendValues(mlc->r / 255.0f, mlc->g / 255.0f, mlc->b / 255.0f, mlc->a / 255.0f);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 23:18:01 +01:00
|
|
|
source.finish();
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-09 23:18:01 +01:00
|
|
|
std::string GeometryExporter::makeTexcoordSourceId(std::string &geom_id,
|
|
|
|
|
int layer_index,
|
|
|
|
|
bool is_single_layer)
|
2010-10-06 09:53:06 +00:00
|
|
|
{
|
|
|
|
|
char suffix[20];
|
2013-12-09 23:18:01 +01:00
|
|
|
if (is_single_layer) {
|
|
|
|
|
suffix[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-05-09 12:50:37 +10:00
|
|
|
SNPRINTF(suffix, "-%d", layer_index);
|
2013-12-09 23:18:01 +01:00
|
|
|
}
|
2011-01-27 19:39:06 +00:00
|
|
|
return getIdBySemantics(geom_id, COLLADASW::InputSemantic::TEXCOORD) + suffix;
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
void GeometryExporter::createTexcoordsSource(std::string geom_id, Mesh *mesh)
|
2010-10-06 09:53:06 +00:00
|
|
|
{
|
2023-12-20 02:21:48 +01:00
|
|
|
int totuv = mesh->corners_num;
|
2023-12-08 16:40:06 -05:00
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-19 20:38:59 -05:00
|
|
|
int num_layers = CustomData_number_of_layers(&mesh->corner_data, CD_PROP_FLOAT2);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* write <source> for each layer
|
|
|
|
|
* each <source> will get id like meshName + "map-channel-1" */
|
2023-12-19 20:38:59 -05:00
|
|
|
int active_uv_index = CustomData_get_active_layer_index(&mesh->corner_data, CD_PROP_FLOAT2);
|
2010-10-06 09:53:06 +00:00
|
|
|
for (int a = 0; a < num_layers; a++) {
|
2023-12-19 20:38:59 -05:00
|
|
|
int layer_index = CustomData_get_layer_index_n(&mesh->corner_data, CD_PROP_FLOAT2, a);
|
2019-05-23 12:17:17 +02:00
|
|
|
if (!this->export_settings.get_active_uv_only() || layer_index == active_uv_index) {
|
Mesh: Move UV layers to generic attributes
Currently the `MLoopUV` struct stores UV coordinates and flags related
to editing UV maps in the UV editor. This patch changes the coordinates
to use the generic 2D vector type, and moves the flags into three
separate boolean attributes. This follows the design in T95965, with
the ultimate intention of simplifying code and improving performance.
Importantly, the change allows exporters and renderers to use UVs
"touched" by geometry nodes, which only creates generic attributes.
It also allows geometry nodes to create "proper" UV maps from scratch,
though only with the Store Named Attribute node for now.
The new design considers any 2D vector attribute on the corner domain
to be a UV map. In the future, they might be distinguished from regular
2D vectors with attribute metadata, which may be helpful because they
are often interpolated differently.
Most of the code changes deal with passing around UV BMesh custom data
offsets and tracking the boolean "sublayers". The boolean layers are
use the following prefixes for attribute names: vert selection: `.vs.`,
edge selection: `.es.`, pinning: `.pn.`. Currently these are short to
avoid using up the maximum length of attribute names. To accommodate
for these 4 extra characters, the name length limit is enlarged to 68
bytes, while the maximum user settable name length is still 64 bytes.
Unfortunately Python/RNA API access to the UV flag data becomes slower.
Accessing the boolean layers directly is be better for performance in
general.
Like the other mesh SoA refactors, backward and forward compatibility
aren't affected, and won't be changed until 4.0. We pay for that by
making mesh reading and writing more expensive with conversions.
Resolves T85962
Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
|
|
|
const blender::float2 *uv_map = static_cast<const blender::float2 *>(
|
2023-12-19 20:38:59 -05:00
|
|
|
CustomData_get_layer_n(&mesh->corner_data, CD_PROP_FLOAT2, a));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-22 16:16:58 +00:00
|
|
|
COLLADASW::FloatSourceF source(mSW);
|
2013-12-09 23:18:01 +01:00
|
|
|
std::string layer_id = makeTexcoordSourceId(
|
2019-05-23 12:17:17 +02:00
|
|
|
geom_id, a, this->export_settings.get_active_uv_only());
|
2012-06-22 16:16:58 +00:00
|
|
|
source.setId(layer_id);
|
|
|
|
|
source.setArrayId(layer_id + ARRAY_ID_SUFFIX);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-22 16:16:58 +00:00
|
|
|
source.setAccessorCount(totuv);
|
|
|
|
|
source.setAccessorStride(2);
|
|
|
|
|
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
|
2025-01-26 20:08:04 +01:00
|
|
|
param.emplace_back("S");
|
|
|
|
|
param.emplace_back("T");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-22 16:16:58 +00:00
|
|
|
source.prepareToAppendValues();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int i : faces.index_range()) {
|
|
|
|
|
for (const int corner : faces[i]) {
|
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
|
|
|
source.appendValues(uv_map[corner][0], uv_map[corner][1]);
|
2012-06-22 16:16:58 +00:00
|
|
|
}
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-06-22 16:16:58 +00:00
|
|
|
source.finish();
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-16 19:02:00 +02:00
|
|
|
bool operator<(const Normal &a, const Normal &b)
|
|
|
|
|
{
|
2021-06-26 21:35:18 +10:00
|
|
|
/* Only needed to sort normal vectors and find() them later in a map. */
|
2015-04-16 19:02:00 +02:00
|
|
|
return a.x < b.x || (a.x == b.x && (a.y < b.y || (a.y == b.y && a.z < b.z)));
|
|
|
|
|
}
|
2010-10-06 09:53:06 +00:00
|
|
|
|
2023-12-08 17:19:46 -05:00
|
|
|
void GeometryExporter::createNormalsSource(std::string geom_id,
|
|
|
|
|
Mesh *mesh,
|
|
|
|
|
std::vector<Normal> &nor)
|
2010-10-06 09:53:06 +00:00
|
|
|
{
|
|
|
|
|
COLLADASW::FloatSourceF source(mSW);
|
2011-01-27 19:39:06 +00:00
|
|
|
source.setId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL));
|
|
|
|
|
source.setArrayId(getIdBySemantics(geom_id, COLLADASW::InputSemantic::NORMAL) + ARRAY_ID_SUFFIX);
|
2022-09-25 18:33:28 +10:00
|
|
|
source.setAccessorCount(ulong(nor.size()));
|
2010-10-06 09:53:06 +00:00
|
|
|
source.setAccessorStride(3);
|
|
|
|
|
COLLADASW::SourceBase::ParameterNameList ¶m = source.getParameterNameList();
|
2025-01-26 20:08:04 +01:00
|
|
|
param.emplace_back("X");
|
|
|
|
|
param.emplace_back("Y");
|
|
|
|
|
param.emplace_back("Z");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
source.prepareToAppendValues();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
std::vector<Normal>::iterator it;
|
|
|
|
|
for (it = nor.begin(); it != nor.end(); it++) {
|
|
|
|
|
Normal &n = *it;
|
2019-05-23 12:17:17 +02:00
|
|
|
|
|
|
|
|
Vector no{n.x, n.y, n.z};
|
|
|
|
|
if (export_settings.get_apply_global_orientation()) {
|
|
|
|
|
bc_add_global_transform(no, export_settings.get_global_transform());
|
|
|
|
|
}
|
|
|
|
|
source.appendValues(no[0], no[1], no[2]);
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
source.finish();
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-03 13:53:32 +00:00
|
|
|
void GeometryExporter::create_normals(std::vector<Normal> &normals,
|
|
|
|
|
std::vector<BCPolygonNormalsIndices> &polygons_normals,
|
2023-12-08 16:40:06 -05:00
|
|
|
Mesh *mesh)
|
2010-10-06 09:53:06 +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;
|
2022-09-25 17:04:52 +10:00
|
|
|
std::map<Normal, uint> shared_normal_indices;
|
2013-03-04 00:17:20 +00:00
|
|
|
int last_normal_index = -1;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
const Span<float3> positions = mesh->vert_positions();
|
|
|
|
|
const Span<float3> vert_normals = mesh->vert_normals();
|
|
|
|
|
const blender::OffsetIndices faces = mesh->faces();
|
|
|
|
|
const Span<int> corner_verts = mesh->corner_verts();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-12-08 16:40:06 -05:00
|
|
|
const bke::AttributeAccessor attributes = mesh->attributes();
|
2023-04-19 11:21:06 +02:00
|
|
|
const VArray<bool> sharp_faces = *attributes.lookup_or_default<bool>(
|
2023-12-20 13:13:16 -05:00
|
|
|
"sharp_face", bke::AttrDomain::Face, false);
|
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
|
|
|
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
blender::Span<blender::float3> corner_normals;
|
2023-12-08 16:40:06 -05:00
|
|
|
if (mesh->normals_domain() == blender::bke::MeshNormalDomain::Corner) {
|
|
|
|
|
corner_normals = mesh->corner_normals();
|
2015-04-16 19:02:00 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int face_index : faces.index_range()) {
|
|
|
|
|
const IndexRange face = faces[face_index];
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
bool use_vert_normals = !corner_normals.is_empty() || !sharp_faces[face_index];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-02-27 15:52:29 -05:00
|
|
|
if (!use_vert_normals) {
|
2019-04-30 13:41:21 +10:00
|
|
|
/* For flat faces use face normal as vertex normal: */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-24 22:06:55 +02:00
|
|
|
const float3 vector = blender::bke::mesh::face_normal_calc(positions,
|
|
|
|
|
corner_verts.slice(face));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-03 13:53:32 +00:00
|
|
|
Normal n = {vector[0], vector[1], vector[2]};
|
|
|
|
|
normals.push_back(n);
|
|
|
|
|
last_normal_index++;
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-03 13:53:32 +00:00
|
|
|
BCPolygonNormalsIndices poly_indices;
|
2023-07-24 22:06:55 +02:00
|
|
|
for (const int corner : face) {
|
2023-02-27 15:52:29 -05:00
|
|
|
if (use_vert_normals) {
|
2015-04-16 19:02:00 +02:00
|
|
|
float normalized[3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Replace auto smooth with node group
Design task: #93551
This PR replaces the auto smooth option with a geometry nodes modifier
that sets the sharp edge attribute. This solves a fair number of long-
standing problems related to auto smooth, simplifies the process of
normal computation, and allows Blender to automatically choose between
face, vertex, and face corner normals based on the sharp edge and face
attributes.
Versioning adds a geometry node group to objects with meshes that had
auto-smooth enabled. The modifier can be applied, which also improves
performance.
Auto smooth is now unnecessary to get a combination of sharp and smooth
edges. In general workflows are changed a bit. Separate procedural and
destructive workflows are available. Custom normals can be used
immediately without turning on the removed auto smooth option.
**Procedural**
The node group asset "Smooth by Angle" is the main way to set sharp
normals based on the edge angle. It can be accessed directly in the add
modifier menu. Of course the modifier can be reordered, muted, or
applied like any other, or changed internally like any geometry nodes
modifier.
**Destructive**
Often the sharp edges don't need to be dynamic. This can give better
performance since edge angles don't need to be recalculated. In edit
mode the two operators "Select Sharp Edges" and "Mark Sharp" can be
used. In other modes, the "Shade Smooth by Angle" controls the edge
sharpness directly.
### Breaking API Changes
- `use_auto_smooth` is removed. Face corner normals are now used
automatically if there are mixed smooth vs. not smooth tags. Meshes
now always use custom normals if they exist.
- In Cycles, the lack of the separate auto smooth state makes normals look
triangulated when all faces are shaded smooth.
- `auto_smooth_angle` is removed. Replaced by a modifier (or operator)
controlling the sharp edge attribute. This means the mesh itself
(without an object) doesn't know anything about automatically smoothing
by angle anymore.
- `create_normals_split`, `calc_normals_split`, and `free_normals_split`
are removed, and are replaced by the simpler `Mesh.corner_normals`
collection property. Since it gives access to the normals cache, it
is automatically updated when relevant data changes.
Addons are updated here: https://projects.blender.org/blender/blender-addons/pulls/104609
### Tests
- `geo_node_curves_test_deform_curves_on_surface` has slightly different
results because face corner normals are used instead of interpolated
vertex normals.
- `bf_wavefront_obj_tests` has different export results for one file
which mixed sharp and smooth faces without turning on auto smooth.
- `cycles_mesh_cpu` has one object which is completely flat shaded.
Previously every edge was split before rendering, now it looks triangulated.
Pull Request: https://projects.blender.org/blender/blender/pulls/108014
2023-10-20 16:54:08 +02:00
|
|
|
if (!corner_normals.is_empty()) {
|
|
|
|
|
normalize_v3_v3(normalized, corner_normals[corner]);
|
2015-05-11 17:22:18 +02:00
|
|
|
}
|
|
|
|
|
else {
|
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
|
|
|
copy_v3_v3(normalized, vert_normals[corner_verts[corner]]);
|
2015-05-11 17:22:18 +02:00
|
|
|
normalize_v3(normalized);
|
|
|
|
|
}
|
2015-04-16 19:02:00 +02:00
|
|
|
Normal n = {normalized[0], normalized[1], normalized[2]};
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-04-16 19:02:00 +02:00
|
|
|
if (shared_normal_indices.find(n) != shared_normal_indices.end()) {
|
|
|
|
|
poly_indices.add_index(shared_normal_indices[n]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2013-03-03 13:53:32 +00:00
|
|
|
last_normal_index++;
|
|
|
|
|
poly_indices.add_index(last_normal_index);
|
2015-04-16 19:02:00 +02:00
|
|
|
shared_normal_indices[n] = last_normal_index;
|
|
|
|
|
normals.push_back(n);
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2013-03-03 13:53:32 +00:00
|
|
|
poly_indices.add_index(last_normal_index);
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-03 13:53:32 +00:00
|
|
|
polygons_normals.push_back(poly_indices);
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-25 16:49:59 +00:00
|
|
|
std::string GeometryExporter::getIdBySemantics(std::string geom_id,
|
|
|
|
|
COLLADASW::InputSemantic::Semantics type,
|
|
|
|
|
std::string other_suffix)
|
|
|
|
|
{
|
2010-10-06 09:53:06 +00:00
|
|
|
return geom_id + getSuffixBySemantic(type) + other_suffix;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-25 16:49:59 +00:00
|
|
|
COLLADASW::URI GeometryExporter::getUrlBySemantics(std::string geom_id,
|
|
|
|
|
COLLADASW::InputSemantic::Semantics type,
|
|
|
|
|
std::string other_suffix)
|
|
|
|
|
{
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
std::string id(getIdBySemantics(geom_id, type, other_suffix));
|
|
|
|
|
return COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
COLLADASW::URI GeometryExporter::makeUrl(std::string id)
|
|
|
|
|
{
|
|
|
|
|
return COLLADASW::URI(COLLADABU::Utils::EMPTY_STRING, id);
|
|
|
|
|
}
|