2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Contributor(s): Esteban Tovagliari, Cedric Paille, Kevin Dietrich
20 * ***** END GPL LICENSE BLOCK *****
27 #include "abc_transform.h"
31 #include "MEM_guardedalloc.h"
33 #include "DNA_modifier_types.h"
35 #include "BLI_listbase.h"
36 #include "BLI_math_geom.h"
38 #include "BKE_DerivedMesh.h"
39 #include "BKE_object.h"
40 #include "BKE_particle.h"
43 using Alembic::Abc::P3fArraySamplePtr;
45 using Alembic::AbcGeom::OCurves;
46 using Alembic::AbcGeom::OCurvesSchema;
47 using Alembic::AbcGeom::ON3fGeomParam;
48 using Alembic::AbcGeom::OV2fGeomParam;
50 /* ************************************************************************** */
52 AbcHairWriter::AbcHairWriter(Scene *scene,
54 AbcTransformWriter *parent,
55 uint32_t time_sampling,
56 ExportSettings &settings,
58 : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
62 OCurves curves(parent->alembicXform(), psys->name, m_time_sampling);
63 m_schema = curves.getSchema();
66 void AbcHairWriter::do_write()
72 ParticleSystemModifierData *psmd = psys_get_modifier(m_object, m_psys);
74 if (!psmd->dm_final) {
78 DerivedMesh *dm = mesh_create_derived_view(m_scene, m_object, CD_MASK_MESH);
79 DM_ensure_tessface(dm);
81 std::vector<Imath::V3f> verts;
82 std::vector<int32_t> hvertices;
83 std::vector<Imath::V2f> uv_values;
84 std::vector<Imath::V3f> norm_values;
86 if (m_psys->pathcache) {
87 ParticleSettings *part = m_psys->part;
89 write_hair_sample(dm, part, verts, norm_values, uv_values, hvertices);
91 if (m_settings.export_child_hairs && m_psys->childcache) {
92 write_hair_child_sample(dm, part, verts, norm_values, uv_values, hvertices);
98 Alembic::Abc::P3fArraySample iPos(verts);
99 m_sample = OCurvesSchema::Sample(iPos, hvertices);
100 m_sample.setBasis(Alembic::AbcGeom::kNoBasis);
101 m_sample.setType(Alembic::AbcGeom::kLinear);
102 m_sample.setWrap(Alembic::AbcGeom::kNonPeriodic);
104 if (!uv_values.empty()) {
105 OV2fGeomParam::Sample uv_smp;
106 uv_smp.setVals(uv_values);
107 m_sample.setUVs(uv_smp);
110 if (!norm_values.empty()) {
111 ON3fGeomParam::Sample norm_smp;
112 norm_smp.setVals(norm_values);
113 m_sample.setNormals(norm_smp);
116 m_sample.setSelfBounds(bounds());
117 m_schema.set(m_sample);
120 void AbcHairWriter::write_hair_sample(DerivedMesh *dm,
121 ParticleSettings *part,
122 std::vector<Imath::V3f> &verts,
123 std::vector<Imath::V3f> &norm_values,
124 std::vector<Imath::V2f> &uv_values,
125 std::vector<int32_t> &hvertices)
127 /* Get untransformed vertices, there's a xform under the hair. */
129 invert_m4_m4_safe(inv_mat, m_object->obmat);
131 MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
132 MFace *mface = dm->getTessFaceArray(dm);
133 MVert *mverts = dm->getVertArray(dm);
135 if (!mtface || !mface) {
136 std::fprintf(stderr, "Warning, no UV set found for underlying geometry.\n");
139 ParticleData * pa = m_psys->particles;
142 ParticleCacheKey **cache = m_psys->pathcache;
143 ParticleCacheKey *path;
147 for (int p = 0; p < m_psys->totpart; ++p, ++pa) {
148 /* underlying info for faces-only emission */
151 if (part->from == PART_FROM_FACE && mtface) {
152 const int num = pa->num_dmcache >= 0 ? pa->num_dmcache : pa->num;
154 if (num < dm->getNumTessFaces(dm)) {
155 MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
156 MTFace *tface = mtface + num;
159 float r_uv[2], mapfw[4], vec[3];
161 psys_interpolate_uvs(tface, face->v4, pa->fuv, r_uv);
162 uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
164 psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, normal, NULL, NULL, NULL, NULL);
166 copy_yup_from_zup(tmp_nor.getValue(), normal);
167 norm_values.push_back(tmp_nor);
171 std::fprintf(stderr, "Particle to faces overflow (%d/%d)\n", num, dm->getNumTessFaces(dm));
174 else if (part->from == PART_FROM_VERT && mtface) {
176 const int num = (pa->num_dmcache >= 0) ? pa->num_dmcache : pa->num;
178 /* iterate over all faces to find a corresponding underlying UV */
179 for (int n = 0; n < dm->getNumTessFaces(dm); ++n) {
180 MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, n, CD_MFACE));
181 MTFace *tface = mtface + n;
189 for (int o = 0; o < 4; ++o) {
190 if (o > 2 && vtx[o] == 0) {
195 uv_values.push_back(Imath::V2f(tface->uv[o][0], tface->uv[o][1]));
197 MVert *mv = mverts + vtx[o];
199 normal_short_to_float_v3(normal, mv->no);
200 copy_yup_from_zup(tmp_nor.getValue(), normal);
201 norm_values.push_back(tmp_nor);
213 int steps = path->segments + 1;
214 hvertices.push_back(steps);
216 for (k = 0; k < steps; ++k) {
218 copy_v3_v3(vert, path->co);
219 mul_m4_v3(inv_mat, vert);
221 /* Convert Z-up to Y-up. */
222 verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));
229 void AbcHairWriter::write_hair_child_sample(DerivedMesh *dm,
230 ParticleSettings *part,
231 std::vector<Imath::V3f> &verts,
232 std::vector<Imath::V3f> &norm_values,
233 std::vector<Imath::V2f> &uv_values,
234 std::vector<int32_t> &hvertices)
236 /* Get untransformed vertices, there's a xform under the hair. */
238 invert_m4_m4_safe(inv_mat, m_object->obmat);
240 MTFace *mtface = static_cast<MTFace *>(CustomData_get_layer(&dm->faceData, CD_MTFACE));
241 MFace *mface = dm->getTessFaceArray(dm);
242 MVert *mverts = dm->getVertArray(dm);
244 if (!mtface || !mface) {
245 std::fprintf(stderr, "Warning, no UV set found for underlying geometry.\n");
248 ParticleCacheKey **cache = m_psys->childcache;
249 ParticleCacheKey *path;
251 ChildParticle *pc = m_psys->child;
253 for (int p = 0; p < m_psys->totchild; ++p, ++pc) {
256 if (part->from == PART_FROM_FACE) {
257 const int num = pc->num;
259 MFace *face = static_cast<MFace *>(dm->getTessFaceData(dm, num, CD_MFACE));
260 MTFace *tface = mtface + num;
262 if (mface && mtface) {
263 float r_uv[2], tmpnor[3], mapfw[4], vec[3];
265 psys_interpolate_uvs(tface, face->v4, pc->fuv, r_uv);
266 uv_values.push_back(Imath::V2f(r_uv[0], r_uv[1]));
268 psys_interpolate_face(mverts, face, tface, NULL, mapfw, vec, tmpnor, NULL, NULL, NULL, NULL);
270 /* Convert Z-up to Y-up. */
271 norm_values.push_back(Imath::V3f(tmpnor[0], tmpnor[2], -tmpnor[1]));
275 int steps = path->segments + 1;
276 hvertices.push_back(steps);
278 for (int k = 0; k < steps; ++k) {
280 copy_v3_v3(vert, path->co);
281 mul_m4_v3(inv_mat, vert);
283 /* Convert Z-up to Y-up. */
284 verts.push_back(Imath::V3f(vert[0], vert[2], -vert[1]));