2022-02-11 09:07:11 +11: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"
|
|
|
|
|
|
|
|
|
|
#include "DNA_meshdata_types.h"
|
2012-05-07 18:09:59 +00:00
|
|
|
|
2012-11-01 15:34:38 +00:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
#include "BKE_attribute.hh"
|
2018-11-07 18:00:24 +01:00
|
|
|
#include "BKE_customdata.h"
|
2012-05-07 18:09:59 +00:00
|
|
|
#include "BKE_global.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_id.h"
|
2012-08-12 17:13:07 +00:00
|
|
|
#include "BKE_material.h"
|
|
|
|
|
#include "BKE_mesh.h"
|
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: 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();
|
2018-06-14 15:15:51 +02:00
|
|
|
Mesh *me = bc_get_mesh_copy(blender_context,
|
2013-03-02 15:58:13 +00:00
|
|
|
ob,
|
2019-05-23 12:17:17 +02:00
|
|
|
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
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
bool has_color = bool(CustomData_has_layer(&me->fdata, CD_MCOL));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
create_normals(nor, norind, me);
|
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 */
|
2010-10-06 09:53:06 +00:00
|
|
|
createVertsSource(geom_id, me);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for normal coords */
|
2010-10-06 09:53:06 +00:00
|
|
|
createNormalsSource(geom_id, me, nor);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
bool has_uvs = bool(CustomData_has_layer(&me->ldata, CD_MLOOPUV));
|
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) {
|
2010-10-06 09:53:06 +00:00
|
|
|
createTexcoordsSource(geom_id, me);
|
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) {
|
2010-10-06 09:53:06 +00:00
|
|
|
createVertexColorSource(geom_id, me);
|
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
|
|
|
|
2013-03-03 13:53:32 +00:00
|
|
|
createLooseEdgeList(ob, me, geom_id);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* Only create Polylists if number of faces > 0 */
|
2012-09-12 18:26:01 +00:00
|
|
|
if (me->totface > 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++) {
|
2019-02-19 17:45:25 +01:00
|
|
|
create_mesh_primitive_list(a, has_uvs, has_color, ob, me, geom_id, norind);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-09-12 18:26:01 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2019-02-19 17:45:25 +01:00
|
|
|
create_mesh_primitive_list(0, has_uvs, has_color, ob, me, 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) {
|
2022-09-07 00:06:31 -05:00
|
|
|
blender::MutableSpan<MVert> verts = me->verts_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) {
|
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
|
|
|
BKE_keyblock_convert_to_mesh(kb, verts.data(), me->totvert);
|
2013-03-02 15:58:13 +00:00
|
|
|
export_key_mesh(ob, me, kb);
|
2013-01-21 13:45:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-06 17:49:09 +01:00
|
|
|
BKE_id_free(nullptr, me);
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-02 15:58:13 +00:00
|
|
|
void GeometryExporter::export_key_mesh(Object *ob, Mesh *me, 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);
|
|
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
bool has_color = bool(CustomData_has_layer(&me->fdata, CD_MCOL));
|
2013-01-21 13:45:49 +00:00
|
|
|
|
|
|
|
|
create_normals(nor, norind, me);
|
|
|
|
|
|
|
|
|
|
// 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 */
|
2013-01-21 13:45:49 +00:00
|
|
|
createVertsSource(geom_id, me);
|
2018-06-08 08:07:48 +02:00
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* writes <source> for normal coords */
|
2013-01-21 13:45:49 +00:00
|
|
|
createNormalsSource(geom_id, me, nor);
|
|
|
|
|
|
2022-09-25 18:33:28 +10:00
|
|
|
bool has_uvs = bool(CustomData_has_layer(&me->ldata, CD_MLOOPUV));
|
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) {
|
2013-01-21 13:45:49 +00:00
|
|
|
createTexcoordsSource(geom_id, me);
|
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) {
|
2013-01-21 13:45:49 +00:00
|
|
|
createVertexColorSource(geom_id, me);
|
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();
|
|
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
// createLooseEdgeList(ob, me, 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++) {
|
2019-02-19 17:45:25 +01:00
|
|
|
create_mesh_primitive_list(a, has_uvs, has_color, ob, me, geom_id, norind);
|
2013-01-21 13:45:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2019-02-19 17:45:25 +01:00
|
|
|
create_mesh_primitive_list(0, has_uvs, has_color, ob, me, 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
|
|
|
|
|
|
|
|
void GeometryExporter::createLooseEdgeList(Object *ob, Mesh *me, std::string &geom_id)
|
|
|
|
|
{
|
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
|
|
|
const Span<MEdge> edges = me->edges();
|
2012-05-16 11:21:03 +00:00
|
|
|
int totedges = me->totedge;
|
|
|
|
|
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 */
|
2018-06-08 08:07:48 +02:00
|
|
|
for (index = 0; index < totedges; index++) {
|
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
|
|
|
const MEdge *edge = &edges[index];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-16 11:21:03 +00:00
|
|
|
if (edge->flag & ME_LOOSEEDGE) {
|
|
|
|
|
edges_in_linelist += 1;
|
|
|
|
|
edge_list.push_back(edge->v1);
|
|
|
|
|
edge_list.push_back(edge->v2);
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool collect_vertex_counts_per_poly(Mesh *me,
|
|
|
|
|
int material_index,
|
2022-09-25 17:04:52 +10:00
|
|
|
std::vector<ulong> &vcount_list)
|
2019-02-19 17:45:25 +01:00
|
|
|
{
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
2022-09-07 21:41:39 -05:00
|
|
|
const blender::bke::AttributeAccessor attributes = me->attributes();
|
2022-08-31 09:09:01 -05:00
|
|
|
const blender::VArray<int> material_indices = attributes.lookup_or_default<int>(
|
|
|
|
|
"material_index", ATTR_DOMAIN_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 */
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (const int i : polys.index_range()) {
|
2022-08-31 09:09:01 -05:00
|
|
|
if (material_indices[i] == material_index) {
|
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
|
|
|
const MPoly &poly = polys[i];
|
|
|
|
|
const int vertex_count = poly.totloop;
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-01 14:52:10 +02:00
|
|
|
std::string GeometryExporter::makeVertexColorSourceId(std::string &geom_id, char *layer_name)
|
|
|
|
|
{
|
|
|
|
|
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,
|
|
|
|
|
Mesh *me,
|
|
|
|
|
std::string &geom_id,
|
2013-03-03 13:53:32 +00:00
|
|
|
std::vector<BCPolygonNormalsIndices> &norind)
|
2013-02-27 13:53:43 +00:00
|
|
|
{
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
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
|
|
|
const Span<MLoop> loops = me->loops();
|
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
|
|
|
|
2019-02-19 17:45:25 +01:00
|
|
|
bool is_triangulated = collect_vertex_counts_per_poly(me, material_index, vcount_list);
|
|
|
|
|
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
|
|
|
|
2019-04-30 13:41:21 +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 */
|
2018-11-28 18:13:58 +01:00
|
|
|
int num_layers = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
|
|
|
|
|
int active_uv_index = CustomData_get_active_layer_index(&me->ldata, CD_MLOOPUV);
|
2019-02-19 17:45:25 +01:00
|
|
|
for (int i = 0; i < num_layers; i++) {
|
2018-12-01 14:18:11 +01:00
|
|
|
int layer_index = CustomData_get_layer_index_n(&me->ldata, CD_MLOOPUV, i);
|
2019-05-23 12:17:17 +02:00
|
|
|
if (!this->export_settings.get_active_uv_only() || layer_index == active_uv_index) {
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-11-28 18:13:58 +01:00
|
|
|
// char *name = CustomData_get_layer_name(&me->ldata, CD_MLOOPUV, 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 */
|
2019-05-23 12:17:17 +02:00
|
|
|
(this->export_settings.get_active_uv_only()) ? 0 : layer_index - 1 /* 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
|
|
|
}
|
|
|
|
|
|
2022-04-20 09:10:10 -05:00
|
|
|
int totlayer_mcol = CustomData_number_of_layers(&me->ldata, 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++) {
|
2022-04-20 09:10:10 -05:00
|
|
|
char *layer_name = bc_CustomData_get_layer_name(&me->ldata, 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
|
|
|
|
2022-09-07 21:41:39 -05:00
|
|
|
const blender::bke::AttributeAccessor attributes = me->attributes();
|
2022-08-31 09:09:01 -05:00
|
|
|
const blender::VArray<int> material_indices = attributes.lookup_or_default<int>(
|
|
|
|
|
"material_index", ATTR_DOMAIN_FACE, 0);
|
|
|
|
|
|
2019-04-30 13:41:21 +10:00
|
|
|
/* <p> */
|
2013-02-27 13:53:43 +00:00
|
|
|
int texindex = 0;
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (const int i : polys.index_range()) {
|
|
|
|
|
const MPoly *p = &polys[i];
|
2013-02-27 13:53:43 +00:00
|
|
|
int loop_count = p->totloop;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-08-31 09:09:01 -05:00
|
|
|
if (material_indices[i] == material_index) {
|
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
|
|
|
const MLoop *l = &loops[p->loopstart];
|
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++) {
|
2019-02-19 17:45:25 +01:00
|
|
|
primitive_list->appendValues(l[j].v);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2010-10-06 09:53:06 +00:00
|
|
|
void GeometryExporter::createVertsSource(std::string geom_id, Mesh *me)
|
|
|
|
|
{
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MVert> verts = me->verts();
|
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: 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
|
|
|
source.setAccessorCount(verts.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();
|
|
|
|
|
param.push_back("X");
|
|
|
|
|
param.push_back("Y");
|
|
|
|
|
param.push_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: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (const int i : verts.index_range()) {
|
2019-05-23 12:17:17 +02:00
|
|
|
Vector co;
|
|
|
|
|
if (export_settings.get_apply_global_orientation()) {
|
|
|
|
|
bc_add_global_transform(co, verts[i].co, export_settings.get_global_transform());
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
copy_v3_v3(co, verts[i].co);
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeometryExporter::createVertexColorSource(std::string geom_id, Mesh *me)
|
|
|
|
|
{
|
2014-05-01 14:52:10 +02:00
|
|
|
/* Find number of vertex color layers */
|
2022-04-20 09:10:10 -05:00
|
|
|
int totlayer_mcol = CustomData_number_of_layers(&me->ldata, 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(
|
|
|
|
|
&me->ldata, 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
|
|
|
|
2022-04-20 09:10:10 -05:00
|
|
|
char *layer_name = bc_CustomData_get_layer_name(&me->ldata, 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);
|
2012-06-22 16:16:58 +00:00
|
|
|
source.setAccessorCount(me->totloop);
|
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();
|
2014-05-01 14:52:10 +02:00
|
|
|
param.push_back("R");
|
|
|
|
|
param.push_back("G");
|
|
|
|
|
param.push_back("B");
|
2017-09-29 18:12:14 +10:00
|
|
|
param.push_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
|
|
|
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (const int i : polys.index_range()) {
|
|
|
|
|
const MPoly &poly = polys[i];
|
|
|
|
|
const MLoopCol *mlc = mloopcol + poly.loopstart;
|
|
|
|
|
for (int j = 0; j < poly.totloop; j++, mlc++) {
|
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 {
|
|
|
|
|
sprintf(suffix, "-%d", layer_index);
|
|
|
|
|
}
|
2011-01-27 19:39:06 +00:00
|
|
|
return getIdBySemantics(geom_id, COLLADASW::InputSemantic::TEXCOORD) + suffix;
|
2010-10-06 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GeometryExporter::createTexcoordsSource(std::string geom_id, Mesh *me)
|
|
|
|
|
{
|
2013-03-02 15:58:13 +00:00
|
|
|
int totuv = me->totloop;
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-03-02 15:58:13 +00:00
|
|
|
int num_layers = CustomData_number_of_layers(&me->ldata, CD_MLOOPUV);
|
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" */
|
2015-05-30 14:18:35 +02:00
|
|
|
int active_uv_index = CustomData_get_active_layer_index(&me->ldata, CD_MLOOPUV);
|
2010-10-06 09:53:06 +00:00
|
|
|
for (int a = 0; a < num_layers; a++) {
|
2015-05-30 14:18:35 +02:00
|
|
|
int layer_index = CustomData_get_layer_index_n(&me->ldata, CD_MLOOPUV, a);
|
2019-05-23 12:17:17 +02:00
|
|
|
if (!this->export_settings.get_active_uv_only() || layer_index == active_uv_index) {
|
2013-03-02 15:58:13 +00:00
|
|
|
MLoopUV *mloops = (MLoopUV *)CustomData_get_layer_n(&me->ldata, CD_MLOOPUV, 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();
|
|
|
|
|
param.push_back("S");
|
|
|
|
|
param.push_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
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (const int i : polys.index_range()) {
|
|
|
|
|
const MPoly *mpoly = &polys[i];
|
2013-03-02 15:58:13 +00:00
|
|
|
MLoopUV *mloop = mloops + mpoly->loopstart;
|
|
|
|
|
for (int j = 0; j < mpoly->totloop; j++) {
|
|
|
|
|
source.appendValues(mloop[j].uv[0], mloop[j].uv[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
|
|
|
|
|
|
|
|
void GeometryExporter::createNormalsSource(std::string geom_id, Mesh *me, std::vector<Normal> &nor)
|
|
|
|
|
{
|
|
|
|
|
#if 0
|
|
|
|
|
int totverts = dm->getNumVerts(dm);
|
|
|
|
|
MVert *verts = dm->getVertArray(dm);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
param.push_back("X");
|
|
|
|
|
param.push_back("Y");
|
|
|
|
|
param.push_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,
|
|
|
|
|
Mesh *me)
|
2010-10-06 09:53:06 +00:00
|
|
|
{
|
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
|
|
|
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MVert> verts = me->verts();
|
Refactor: Move normals out of MVert, lazy calculation
As described in T91186, this commit moves mesh vertex normals into a
contiguous array of float vectors in a custom data layer, how face
normals are currently stored.
The main interface is documented in `BKE_mesh.h`. Vertex and face
normals are now calculated on-demand and cached, retrieved with an
"ensure" function. Since the logical state of a mesh is now "has
normals when necessary", they can be retrieved from a `const` mesh.
The goal is to use on-demand calculation for all derived data, but
leave room for eager calculation for performance purposes (modifier
evaluation is threaded, but viewport data generation is not).
**Benefits**
This moves us closer to a SoA approach rather than the current AoS
paradigm. Accessing a contiguous `float3` is much more efficient than
retrieving data from a larger struct. The memory requirements for
accessing only normals or vertex locations are smaller, and at the
cost of more memory usage for just normals, they now don't have to
be converted between float and short, which also simplifies code
In the future, the remaining items can be removed from `MVert`,
leaving only `float3`, which has similar benefits (see T93602).
Removing the combination of derived and original data makes it
conceptually simpler to only calculate normals when necessary.
This is especially important now that we have more opportunities
for temporary meshes in geometry nodes.
**Performance**
In addition to the theoretical future performance improvements by
making `MVert == float3`, I've done some basic performance testing
on this patch directly. The data is fairly rough, but it gives an idea
about where things stand generally.
- Mesh line primitive 4m Verts: 1.16x faster (36 -> 31 ms),
showing that accessing just `MVert` is now more efficient.
- Spring Splash Screen: 1.03-1.06 -> 1.06-1.11 FPS, a very slight
change that at least shows there is no regression.
- Sprite Fright Snail Smoosh: 3.30-3.40 -> 3.42-3.50 FPS, a small
but observable speedup.
- Set Position Node with Scaled Normal: 1.36x faster (53 -> 39 ms),
shows that using normals in geometry nodes is faster.
- Normal Calculation 1.6m Vert Cube: 1.19x faster (25 -> 21 ms),
shows that calculating normals is slightly faster now.
- File Size of 1.6m Vert Cube: 1.03x smaller (214.7 -> 208.4 MB),
Normals are not saved in files, which can help with large meshes.
As for memory usage, it may be slightly more in some cases, but
I didn't observe any difference in the production files I tested.
**Tests**
Some modifiers and cycles test results need to be updated with this
commit, for two reasons:
- The subdivision surface modifier is not responsible for calculating
normals anymore. In master, the modifier creates different normals
than the result of the `Mesh` normal calculation, so this is a bug
fix.
- There are small differences in the results of some modifiers that
use normals because they are not converted to and from `short`
anymore.
**Future improvements**
- Remove `ModifierTypeInfo::dependsOnNormals`. Code in each modifier
already retrieves normals if they are needed anyway.
- Copy normals as part of a better CoW system for attributes.
- Make more areas use lazy instead of eager normal calculation.
- Remove `BKE_mesh_normals_tag_dirty` in more places since that is
now the default state of a new mesh.
- Possibly apply a similar change to derived face corner normals.
Differential Revision: https://developer.blender.org/D12770
2022-01-13 14:37:58 -06:00
|
|
|
const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(me);
|
2022-09-07 00:06:31 -05:00
|
|
|
const Span<MPoly> polys = me->polys();
|
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
|
|
|
const Span<MLoop> loops = me->loops();
|
2022-05-13 18:31:29 +02:00
|
|
|
const float(*lnors)[3] = nullptr;
|
2015-05-06 17:03:24 +02:00
|
|
|
bool use_custom_normals = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-04-16 19:02:00 +02:00
|
|
|
BKE_mesh_calc_normals_split(me);
|
|
|
|
|
if (CustomData_has_layer(&me->ldata, CD_NORMAL)) {
|
|
|
|
|
lnors = (float(*)[3])CustomData_get_layer(&me->ldata, CD_NORMAL);
|
2015-05-06 17:03:24 +02:00
|
|
|
use_custom_normals = true;
|
2015-04-16 19:02:00 +02:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
|
|
|
for (const int poly_index : polys.index_range()) {
|
|
|
|
|
const MPoly *mpoly = &polys[poly_index];
|
2015-05-06 17:03:24 +02:00
|
|
|
bool use_vertex_normals = use_custom_normals || mpoly->flag & ME_SMOOTH;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-05-06 17:03:24 +02:00
|
|
|
if (!use_vertex_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
|
|
|
|
2013-03-03 13:53:32 +00:00
|
|
|
float vector[3];
|
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
|
|
|
BKE_mesh_calc_poly_normal(mpoly, &loops[mpoly->loopstart], verts.data(), vector);
|
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;
|
|
|
|
|
for (int loop_index = 0; loop_index < mpoly->totloop; loop_index++) {
|
2022-09-25 17:04:52 +10:00
|
|
|
uint loop_idx = mpoly->loopstart + loop_index;
|
2015-05-06 17:03:24 +02:00
|
|
|
if (use_vertex_normals) {
|
2015-04-16 19:02:00 +02:00
|
|
|
float normalized[3];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-05-11 17:22:18 +02:00
|
|
|
if (use_custom_normals) {
|
|
|
|
|
normalize_v3_v3(normalized, lnors[loop_idx]);
|
|
|
|
|
}
|
|
|
|
|
else {
|
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
|
|
|
copy_v3_v3(normalized, vert_normals[loops[loop_index].v]);
|
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);
|
|
|
|
|
}
|