Blender V5.3
abc_writer_hair.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_writer_hair.h"
11
12#include "DNA_mesh_types.h"
13#include "DNA_meshdata_types.h"
14#include "DNA_object_types.h"
15
16#include "BLI_math_matrix_c.hh"
17#include "BLI_math_vector_c.hh"
18
19#include "BKE_customdata.hh"
21#include "BKE_mesh_runtime.hh"
22#include "BKE_object.hh"
23#include "BKE_particle.h"
24
25#include "CLG_log.h"
26
27namespace blender {
28
29static CLG_LogRef LOG = {"io.alembic"};
30
31using Alembic::Abc::P3fArraySamplePtr;
32using Alembic::AbcGeom::OCurves;
33using Alembic::AbcGeom::OCurvesSchema;
34using Alembic::AbcGeom::ON3fGeomParam;
35using Alembic::AbcGeom::OV2fGeomParam;
36
37namespace io::alembic {
38
40 : ABCAbstractWriter(args), uv_warning_shown_(false)
41{
42}
43
45{
46 CLOG_DEBUG(&LOG, "exporting %s", args_.abc_path.c_str());
47 abc_curves_ = OCurves(args_.abc_parent, args_.abc_name, timesample_index_);
48 abc_curves_schema_ = abc_curves_.getSchema();
49}
50
51Alembic::Abc::OObject ABCHairWriter::get_alembic_object() const
52{
53 return abc_curves_;
54}
55
56Alembic::Abc::OCompoundProperty ABCHairWriter::abc_prop_for_custom_props()
57{
58 return abc_schema_prop_for_custom_props(abc_curves_schema_);
59}
60
62{
63 /* We assume that hair particles are always animated. */
64 return true;
65}
66
68{
69 const Mesh *mesh = BKE_object_get_evaluated_mesh(context.object);
70 if (!mesh) {
71 return;
72 }
73 BKE_mesh_tessface_ensure(const_cast<Mesh *>(mesh));
74
75 std::vector<Imath::V3f> verts;
76 std::vector<int32_t> hvertices;
77 std::vector<Imath::V2f> uv_values;
78 std::vector<Imath::V3f> norm_values;
79
80 ParticleSystem *psys = context.particle_system;
81 if (psys->pathcache) {
82 ParticleSettings *part = psys->part;
83 bool export_children = psys->childcache && part->childtype != 0;
84
85 if (!export_children || part->draw & PART_DRAW_PARENT) {
86 write_hair_sample(
87 context, const_cast<Mesh *>(mesh), verts, norm_values, uv_values, hvertices);
88 }
89
90 if (export_children) {
91 write_hair_child_sample(
92 context, const_cast<Mesh *>(mesh), verts, norm_values, uv_values, hvertices);
93 }
94 }
95
96 Alembic::Abc::P3fArraySample iPos(verts);
97 OCurvesSchema::Sample sample(iPos, hvertices);
98 sample.setBasis(Alembic::AbcGeom::kNoBasis);
99 sample.setType(Alembic::AbcGeom::kLinear);
100 sample.setWrap(Alembic::AbcGeom::kNonPeriodic);
101
102 if (!uv_values.empty()) {
103 OV2fGeomParam::Sample uv_smp;
104 uv_smp.setVals(uv_values);
105 sample.setUVs(uv_smp);
106 }
107
108 if (!norm_values.empty()) {
109 ON3fGeomParam::Sample norm_smp;
110 norm_smp.setVals(norm_values);
111 sample.setNormals(norm_smp);
112 }
113
114 update_bounding_box(context.object);
115 sample.setSelfBounds(bounding_box_);
116 abc_curves_schema_.set(sample);
117}
118
119void ABCHairWriter::write_hair_sample(const HierarchyContext &context,
120 Mesh *mesh,
121 std::vector<Imath::V3f> &verts,
122 std::vector<Imath::V3f> &norm_values,
123 std::vector<Imath::V2f> &uv_values,
124 std::vector<int32_t> &hvertices)
125{
126 /* Get untransformed vertices, there's a xform under the hair. */
127 float inv_mat[4][4];
128 invert_m4_m4_safe(inv_mat, context.object->object_to_world().ptr());
129
130 MTFace *mtface = static_cast<MTFace *>(
131 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MTFACE, mesh->totface_legacy));
132 const MFace *mface = static_cast<const MFace *>(
133 CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE));
134 const Span<float3> positions = mesh->vert_positions();
135 const Span<float3> vert_normals = mesh->vert_normals();
136
137 if ((!mtface || !mface) && !uv_warning_shown_) {
138 CLOG_WARN(&LOG, "No UV set found for underlying geometry of %s", context.object->id.name + 2);
139 uv_warning_shown_ = true;
140 }
141
142 ParticleSystem *psys = context.particle_system;
143 ParticleSettings *part = psys->part;
144 ParticleData *pa = psys->particles;
145 int k;
146
147 ParticleCacheKey **cache = psys->pathcache;
148 ParticleCacheKey *path;
149 float normal[3];
150 Imath::V3f tmp_nor;
151
152 for (int p = 0; p < psys->totpart; p++, pa++) {
153 /* underlying info for faces-only emission */
154 path = cache[p];
155
156 /* Write UV and normal vectors */
157 if (part->from == PART_FROM_FACE && mtface) {
158 const int num = pa->num_dmcache >= 0 ? pa->num_dmcache : pa->num;
159
160 if (num < mesh->totface_legacy) {
161 /* TODO(Sybren): check whether the null check here and if(mface) are actually required
162 */
163 const MFace *face = mface == nullptr ? nullptr : &mface[num];
164 MTFace *tface = mtface + num;
165
166 if (mface) {
167 float uv[2], mapfw[4], vec[3];
168
169 psys_interpolate_uvs(tface, face->v4, pa->fuv, uv);
170 uv_values.emplace_back(uv[0], uv[1]);
171
173 reinterpret_cast<const float (*)[3]>(positions.data()),
174 reinterpret_cast<const float (*)[3]>(vert_normals.data()),
175 face,
176 tface,
177 nullptr,
178 mapfw,
179 vec,
180 normal,
181 nullptr,
182 nullptr,
183 nullptr);
184
185 copy_yup_from_zup(tmp_nor.getValue(), normal);
186 norm_values.push_back(tmp_nor);
187 }
188 }
189 else {
190 CLOG_WARN(&LOG, "Particle to faces overflow (%d/%d)", num, mesh->totface_legacy);
191 }
192 }
193 else if (part->from == PART_FROM_VERT && mtface) {
194 /* vertex id */
195 const int num = (pa->num_dmcache >= 0) ? pa->num_dmcache : pa->num;
196
197 /* iterate over all faces to find a corresponding underlying UV */
198 for (int n = 0; n < mesh->totface_legacy; n++) {
199 const MFace *face = &mface[n];
200 const MTFace *tface = mtface + n;
201 uint vtx[4];
202 vtx[0] = face->v1;
203 vtx[1] = face->v2;
204 vtx[2] = face->v3;
205 vtx[3] = face->v4;
206 bool found = false;
207
208 for (int o = 0; o < 4; o++) {
209 if (o > 2 && vtx[o] == 0) {
210 break;
211 }
212
213 if (vtx[o] == num) {
214 uv_values.emplace_back(tface->uv[o][0], tface->uv[o][1]);
215 copy_v3_v3(normal, vert_normals[vtx[o]]);
216 copy_yup_from_zup(tmp_nor.getValue(), normal);
217 norm_values.push_back(tmp_nor);
218 found = true;
219 break;
220 }
221 }
222
223 if (found) {
224 break;
225 }
226 }
227 }
228
229 int steps = path->segments + 1;
230 hvertices.push_back(steps);
231
232 for (k = 0; k < steps; k++, path++) {
233 float vert[3];
234 copy_v3_v3(vert, path->co);
235 mul_m4_v3(inv_mat, vert);
236
237 /* Convert Z-up to Y-up. */
238 verts.emplace_back(vert[0], vert[2], -vert[1]);
239 }
240 }
241}
242
243void ABCHairWriter::write_hair_child_sample(const HierarchyContext &context,
244 Mesh *mesh,
245 std::vector<Imath::V3f> &verts,
246 std::vector<Imath::V3f> &norm_values,
247 std::vector<Imath::V2f> &uv_values,
248 std::vector<int32_t> &hvertices)
249{
250 /* Get untransformed vertices, there's a xform under the hair. */
251 float inv_mat[4][4];
252 invert_m4_m4_safe(inv_mat, context.object->object_to_world().ptr());
253
254 const MFace *mface = static_cast<const MFace *>(
255 CustomData_get_layer(&mesh->fdata_legacy, CD_MFACE));
256 MTFace *mtface = static_cast<MTFace *>(
257 CustomData_get_layer_for_write(&mesh->fdata_legacy, CD_MTFACE, mesh->totface_legacy));
258 const Span<float3> positions = mesh->vert_positions();
259 const Span<float3> vert_normals = mesh->vert_normals();
260
261 ParticleSystem *psys = context.particle_system;
262 ParticleSettings *part = psys->part;
263 ParticleCacheKey **cache = psys->childcache;
264 ParticleCacheKey *path;
265
266 ChildParticle *pc = psys->child;
267
268 for (int p = 0; p < psys->totchild; p++, pc++) {
269 path = cache[p];
270
271 if (part->from == PART_FROM_FACE && part->childtype != PART_CHILD_PARTICLES && mtface) {
272 const int num = pc->num;
273 if (num < 0) {
274 CLOG_WARN(
275 &LOG,
276 "Child particle of hair system %s has unknown face index of geometry of %s, skipping "
277 "child hair.",
278 psys->name,
279 context.object->id.name + 2);
280 continue;
281 }
282
283 const MFace *face = &mface[num];
284 MTFace *tface = mtface + num;
285
286 float uv[2], tmpnor[3], mapfw[4], vec[3];
287
288 psys_interpolate_uvs(tface, face->v4, pc->fuv, uv);
289 uv_values.emplace_back(uv[0], uv[1]);
290
292 reinterpret_cast<const float (*)[3]>(positions.data()),
293 reinterpret_cast<const float (*)[3]>(vert_normals.data()),
294 face,
295 tface,
296 nullptr,
297 mapfw,
298 vec,
299 tmpnor,
300 nullptr,
301 nullptr,
302 nullptr);
303
304 /* Convert Z-up to Y-up. */
305 norm_values.emplace_back(tmpnor[0], tmpnor[2], -tmpnor[1]);
306 }
307 else {
308 if (!uv_values.empty()) {
309 uv_values.push_back(uv_values[pc->parent]);
310 }
311 if (!norm_values.empty()) {
312 norm_values.push_back(norm_values[pc->parent]);
313 }
314 }
315
316 int steps = path->segments + 1;
317 hvertices.push_back(steps);
318
319 for (int k = 0; k < steps; k++) {
320 float vert[3];
321 copy_v3_v3(vert, path->co);
322 mul_m4_v3(inv_mat, vert);
323
324 /* Convert Z-up to Y-up. */
325 verts.emplace_back(vert[0], vert[2], -vert[1]);
326
327 path++;
328 }
329 }
330}
331
332} // namespace io::alembic
333} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
General operations, lookup, etc. for blender objects.
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
constexpr const T * data() const
Definition BLI_span.hh:219
constexpr const T * data() const
Definition BLI_span.hh:219
ABCAbstractWriter(const ABCWriterConstructorArgs &args)
Alembic::Abc::OCompoundProperty abc_schema_prop_for_custom_props(T abc_schema)
const ABCWriterConstructorArgs args_
virtual void update_bounding_box(Object *object)
Alembic::Abc::OObject get_alembic_object() const override
ABCHairWriter(const ABCWriterConstructorArgs &args)
void do_write(HierarchyContext &context) override
bool check_is_animated(const HierarchyContext &context) const override
Alembic::Abc::OCompoundProperty abc_prop_for_custom_props() override
void create_alembic_objects(const HierarchyContext *context) override
#define LOG(level)
Definition log.h:97
int context(const bContext *C, const char *member, bContextDataResult *result)
BLI_INLINE void copy_yup_from_zup(float yup[3], const float zup[3])
BMesh Iterators.
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
void psys_interpolate_face(struct Mesh *mesh, const float(*vert_positions)[3], const float(*vert_normals)[3], const struct MFace *mface, struct MTFace *tface, const float(*orcodata)[3], float w[4], float vec[3], float nor[3], float utan[3], float vtan[3], float orco[3])
void mul_m4_v3(const float M[4][4], float r[3])
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
ATTR_WARN_UNUSED_RESULT const size_t num
void psys_interpolate_uvs(const struct MTFace *tface, int quad, const float w[4], float r_uv[2])
void invert_m4_m4_safe(float inverse[4][4], const float mat[4][4])
void BKE_mesh_tessface_ensure(Mesh *mesh)
static CLG_LogRef LOG
Definition logging.cpp:12
void * CustomData_get_layer_for_write(CustomData *data, eCustomDataType type, int totelem)
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
Definition object.cc:4436
void vert(const int vert_id, float4 &position)
ustring name
Definition graph/node.h:178
struct ParticleCacheKey ** childcache
struct ParticleCacheKey ** pathcache