2016-12-28 17:30:58 +01:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2005 by the Blender Foundation.
|
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup modifiers
|
2016-12-28 17:30:58 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
2019-02-25 11:39:14 +01:00
|
|
|
#include "DNA_material_types.h"
|
|
|
|
#include "DNA_mesh_types.h"
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-05-25 21:44:33 +02:00
|
|
|
#include "BKE_editmesh.h"
|
2018-05-15 13:26:40 +02:00
|
|
|
#include "BKE_mesh.h"
|
|
|
|
#include "BKE_library.h"
|
2016-12-28 17:30:58 +01:00
|
|
|
#include "BKE_modifier.h"
|
|
|
|
#include "BKE_particle.h"
|
|
|
|
|
2018-06-22 15:03:42 +02:00
|
|
|
#include "DEG_depsgraph_query.h"
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-06-22 15:03:42 +02:00
|
|
|
#include "MOD_util.h"
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-06-17 17:04:27 +02:00
|
|
|
static void initData(ModifierData *md)
|
2016-12-28 17:30:58 +01:00
|
|
|
{
|
|
|
|
ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
|
|
|
|
psmd->psys = NULL;
|
2018-05-15 13:26:40 +02:00
|
|
|
psmd->mesh_final = NULL;
|
2018-05-25 21:44:33 +02:00
|
|
|
psmd->mesh_original = NULL;
|
2016-12-28 17:30:58 +01:00
|
|
|
psmd->totdmvert = psmd->totdmedge = psmd->totdmface = 0;
|
|
|
|
}
|
|
|
|
static void freeData(ModifierData *md)
|
|
|
|
{
|
|
|
|
ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (psmd->mesh_final) {
|
|
|
|
BKE_id_free(NULL, psmd->mesh_final);
|
|
|
|
psmd->mesh_final = NULL;
|
2018-05-25 21:44:33 +02:00
|
|
|
if (psmd->mesh_original) {
|
|
|
|
BKE_id_free(NULL, psmd->mesh_original);
|
|
|
|
psmd->mesh_original = NULL;
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-08 15:04:10 +02:00
|
|
|
psmd->totdmvert = psmd->totdmedge = psmd->totdmface = 0;
|
2016-12-28 17:30:58 +01:00
|
|
|
|
|
|
|
/* ED_object_modifier_remove may have freed this first before calling
|
|
|
|
* modifier_free (which calls this function) */
|
|
|
|
if (psmd->psys)
|
|
|
|
psmd->psys->flag |= PSYS_DELETE;
|
|
|
|
}
|
2018-05-08 15:04:10 +02:00
|
|
|
|
2018-07-04 12:45:30 +02:00
|
|
|
static void copyData(const ModifierData *md, ModifierData *target, const int flag)
|
2016-12-28 17:30:58 +01:00
|
|
|
{
|
|
|
|
#if 0
|
2018-05-08 15:04:10 +02:00
|
|
|
const ParticleSystemModifierData *psmd = (const ParticleSystemModifierData *) md;
|
2016-12-28 17:30:58 +01:00
|
|
|
#endif
|
|
|
|
ParticleSystemModifierData *tpsmd = (ParticleSystemModifierData *) target;
|
|
|
|
|
2018-07-04 12:45:30 +02:00
|
|
|
modifier_copyData_generic(md, target, flag);
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
tpsmd->mesh_final = NULL;
|
2018-05-25 21:44:33 +02:00
|
|
|
tpsmd->mesh_original = NULL;
|
2016-12-28 17:30:58 +01:00
|
|
|
tpsmd->totdmvert = tpsmd->totdmedge = tpsmd->totdmface = 0;
|
|
|
|
}
|
|
|
|
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
static void requiredDataMask(Object *UNUSED(ob), ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
|
2016-12-28 17:30:58 +01:00
|
|
|
{
|
|
|
|
ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
|
|
|
|
psys_emitter_customdata_mask(psmd->psys, r_cddata_masks);
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* saves the current emitter state for a particle system and calculates particles */
|
2018-05-12 08:04:56 +02:00
|
|
|
static void deformVerts(
|
2018-05-12 08:21:07 +02:00
|
|
|
ModifierData *md, const ModifierEvalContext *ctx,
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *mesh,
|
2018-05-12 08:04:56 +02:00
|
|
|
float (*vertexCos)[3],
|
2018-11-27 17:21:16 +01:00
|
|
|
int numVerts)
|
2016-12-28 17:30:58 +01:00
|
|
|
{
|
2018-05-15 13:26:40 +02:00
|
|
|
Mesh *mesh_src = mesh;
|
2016-12-28 17:30:58 +01:00
|
|
|
ParticleSystemModifierData *psmd = (ParticleSystemModifierData *) md;
|
|
|
|
ParticleSystem *psys = NULL;
|
|
|
|
/* float cfra = BKE_scene_frame_get(md->scene); */ /* UNUSED */
|
|
|
|
|
2018-05-01 17:33:04 +02:00
|
|
|
if (ctx->object->particlesystem.first)
|
2016-12-28 17:30:58 +01:00
|
|
|
psys = psmd->psys;
|
|
|
|
else
|
|
|
|
return;
|
2018-06-17 17:04:27 +02:00
|
|
|
|
2018-05-01 17:33:04 +02:00
|
|
|
if (!psys_check_enabled(ctx->object, psys, (ctx->flag & MOD_APPLY_RENDER) != 0))
|
2016-12-28 17:30:58 +01:00
|
|
|
return;
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (mesh_src == NULL) {
|
2018-11-27 17:21:16 +01:00
|
|
|
mesh_src = MOD_deform_mesh_eval_get(ctx->object, NULL, NULL, vertexCos, numVerts, false, true);
|
2018-05-15 13:26:40 +02:00
|
|
|
if (mesh_src == NULL) {
|
2016-12-28 17:30:58 +01:00
|
|
|
return;
|
2018-05-15 13:26:40 +02:00
|
|
|
}
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clear old dm */
|
2019-02-08 15:05:29 +01:00
|
|
|
bool had_mesh_final = (psmd->mesh_final != NULL);
|
2018-05-15 13:26:40 +02:00
|
|
|
if (psmd->mesh_final) {
|
|
|
|
BKE_id_free(NULL, psmd->mesh_final);
|
|
|
|
psmd->mesh_final = NULL;
|
2018-05-25 21:44:33 +02:00
|
|
|
if (psmd->mesh_original) {
|
|
|
|
BKE_id_free(NULL, psmd->mesh_original);
|
|
|
|
psmd->mesh_original = NULL;
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (psmd->flag & eParticleSystemFlag_file_loaded) {
|
2018-05-15 13:26:40 +02:00
|
|
|
/* in file read mesh just wasn't saved in file so no need to reset everything */
|
2016-12-28 17:30:58 +01:00
|
|
|
psmd->flag &= ~eParticleSystemFlag_file_loaded;
|
2019-02-08 15:05:29 +01:00
|
|
|
if (psys->particles == NULL) {
|
|
|
|
psys->recalc |= ID_RECALC_PSYS_RESET;
|
|
|
|
}
|
2019-02-12 10:55:15 +01:00
|
|
|
/* TODO(sergey): This is not how particles were working prior to copy on
|
|
|
|
* write, but now evaluation is similar to case when one duplicates the
|
|
|
|
* object. In that case particles were doing reset here. */
|
|
|
|
psys->recalc |= ID_RECALC_PSYS_RESET;
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
/* make new mesh */
|
2018-09-20 12:33:45 +02:00
|
|
|
psmd->mesh_final = BKE_mesh_copy_for_eval(mesh_src, false);
|
2018-05-15 13:26:40 +02:00
|
|
|
BKE_mesh_apply_vert_coords(psmd->mesh_final, vertexCos);
|
|
|
|
BKE_mesh_calc_normals(psmd->mesh_final);
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
BKE_mesh_tessface_ensure(psmd->mesh_final);
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
if (!psmd->mesh_final->runtime.deformed_only) {
|
2018-05-25 21:44:33 +02:00
|
|
|
/* Get the original mesh from the object, this is what the particles
|
|
|
|
* are attached to so in case of non-deform modifiers we need to remap
|
|
|
|
* them to the final mesh (typically subdivision surfaces). */
|
|
|
|
Mesh *mesh_original = NULL;
|
|
|
|
|
|
|
|
if (ctx->object->type == OB_MESH) {
|
2019-02-17 18:05:18 +11:00
|
|
|
BMEditMesh *em = BKE_editmesh_from_object(ctx->object);
|
2018-05-25 21:44:33 +02:00
|
|
|
|
2019-02-17 18:05:18 +11:00
|
|
|
if (em) {
|
2018-05-25 21:44:33 +02:00
|
|
|
/* In edit mode get directly from the edit mesh. */
|
Refactor CDData masks, to have one mask per mesh elem type.
We already have different storages for cddata of verts, edges etc.,
'simply' do the same for the mask flags we use all around Blender code
to request some data, or limit some operation to some layers, etc.
Reason we need this is that some cddata types (like Normals) are
actually shared between verts/polys/loops, and we don’t want to generate
clnors everytime we request vnors!
As a side note, this also does final fix to T59338, which was the
trigger for this patch (need to request computed loop normals for
another mesh than evaluated one).
Reviewers: brecht, campbellbarton, sergey
Differential Revision: https://developer.blender.org/D4407
2019-03-07 11:13:40 +01:00
|
|
|
psmd->mesh_original = BKE_mesh_from_bmesh_for_eval_nomain(em->bm, NULL);
|
2018-05-25 21:44:33 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Otherwise get regular mesh. */
|
|
|
|
mesh_original = ctx->object->data;
|
|
|
|
}
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
2018-05-25 21:44:33 +02:00
|
|
|
else {
|
|
|
|
mesh_original = mesh_src;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh_original) {
|
|
|
|
/* Make a persistent copy of the mesh. We don't actually need
|
|
|
|
* all this data, just some topology for remapping. Could be
|
|
|
|
* optimized once. */
|
2018-09-20 12:33:45 +02:00
|
|
|
psmd->mesh_original = BKE_mesh_copy_for_eval(mesh_original, false);
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
2018-05-25 21:44:33 +02:00
|
|
|
|
|
|
|
BKE_mesh_tessface_ensure(psmd->mesh_original);
|
2018-05-15 13:26:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mesh_src != psmd->mesh_final && mesh_src != mesh) {
|
|
|
|
BKE_id_free(NULL, mesh_src);
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 15:05:29 +01:00
|
|
|
/* Report change in mesh structure.
|
|
|
|
* This is an unreliable check for the topology check, but allows some
|
|
|
|
* handy configuration like emitting particles from inside particle
|
|
|
|
* instance. */
|
|
|
|
if (had_mesh_final &&
|
|
|
|
(psmd->mesh_final->totvert != psmd->totdmvert ||
|
|
|
|
psmd->mesh_final->totedge != psmd->totdmedge ||
|
|
|
|
psmd->mesh_final->totface != psmd->totdmface))
|
|
|
|
{
|
|
|
|
psys->recalc |= ID_RECALC_PSYS_RESET;
|
|
|
|
psmd->totdmvert = psmd->mesh_final->totvert;
|
|
|
|
psmd->totdmedge = psmd->mesh_final->totedge;
|
|
|
|
psmd->totdmface = psmd->mesh_final->totface;
|
|
|
|
}
|
|
|
|
|
2018-05-01 17:33:04 +02:00
|
|
|
if (!(ctx->object->transflag & OB_NO_PSYS_UPDATE)) {
|
2018-06-22 15:03:42 +02:00
|
|
|
struct Scene *scene = DEG_get_evaluated_scene(ctx->depsgraph);
|
2016-12-28 17:30:58 +01:00
|
|
|
psmd->flag &= ~eParticleSystemFlag_psys_updated;
|
2018-06-22 15:03:42 +02:00
|
|
|
particle_system_update(ctx->depsgraph, scene, ctx->object, psys, (ctx->flag & MOD_APPLY_RENDER) != 0);
|
2016-12-28 17:30:58 +01:00
|
|
|
psmd->flag |= eParticleSystemFlag_psys_updated;
|
|
|
|
}
|
2019-02-08 15:05:29 +01:00
|
|
|
|
|
|
|
if (DEG_is_active(ctx->depsgraph)) {
|
|
|
|
Object *object_orig = DEG_get_original_object(ctx->object);
|
|
|
|
ModifierData *md_orig = modifiers_findByName(object_orig, psmd->modifier.name);
|
|
|
|
BLI_assert(md_orig != NULL);
|
|
|
|
ParticleSystemModifierData *psmd_orig = (ParticleSystemModifierData *) md_orig;
|
|
|
|
psmd_orig->flag = psmd->flag;
|
|
|
|
}
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
|
2018-06-29 14:56:38 +02:00
|
|
|
/* disabled particles in editmode for now, until support for proper evaluated mesh
|
2016-12-28 17:30:58 +01:00
|
|
|
* updates is coded */
|
|
|
|
#if 0
|
|
|
|
static void deformVertsEM(
|
2018-06-29 14:56:38 +02:00
|
|
|
ModifierData *md, Object *ob, BMEditMesh *editData,
|
|
|
|
Mesh *mesh, float (*vertexCos)[3], int numVerts)
|
2016-12-28 17:30:58 +01:00
|
|
|
{
|
2018-06-29 14:56:38 +02:00
|
|
|
const bool do_temp_mesh = (mesh == NULL);
|
|
|
|
if (do_temp_mesh) {
|
|
|
|
mesh = BKE_id_new_nomain(ID_ME, ((ID *)ob->data)->name);
|
|
|
|
BM_mesh_bm_to_me(NULL, editData->bm, mesh, &((BMeshToMeshParams){0}));
|
|
|
|
}
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-06-29 14:56:38 +02:00
|
|
|
deformVerts(md, ob, mesh, vertexCos, numVerts);
|
2016-12-28 17:30:58 +01:00
|
|
|
|
2018-06-29 14:56:38 +02:00
|
|
|
if (derivedData) {
|
|
|
|
BKE_id_free(NULL, mesh);
|
|
|
|
}
|
2016-12-28 17:30:58 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
ModifierTypeInfo modifierType_ParticleSystem = {
|
|
|
|
/* name */ "ParticleSystem",
|
|
|
|
/* structName */ "ParticleSystemModifierData",
|
|
|
|
/* structSize */ sizeof(ParticleSystemModifierData),
|
|
|
|
/* type */ eModifierTypeType_OnlyDeform,
|
|
|
|
/* flags */ eModifierTypeFlag_AcceptsMesh |
|
|
|
|
eModifierTypeFlag_SupportsMapping |
|
|
|
|
eModifierTypeFlag_UsesPointCache /* |
|
|
|
|
eModifierTypeFlag_SupportsEditmode |
|
|
|
|
eModifierTypeFlag_EnableInEditmode */,
|
|
|
|
|
|
|
|
/* copyData */ copyData,
|
2018-04-18 15:45:54 +02:00
|
|
|
|
2018-05-15 13:26:40 +02:00
|
|
|
/* deformVerts */ deformVerts,
|
2016-12-28 17:30:58 +01:00
|
|
|
/* deformMatrices */ NULL,
|
2018-04-18 15:45:54 +02:00
|
|
|
/* deformVertsEM */ NULL,
|
2016-12-28 17:30:58 +01:00
|
|
|
/* deformMatricesEM */ NULL,
|
|
|
|
/* applyModifier */ NULL,
|
2018-04-18 15:45:54 +02:00
|
|
|
|
2016-12-28 17:30:58 +01:00
|
|
|
/* initData */ initData,
|
|
|
|
/* requiredDataMask */ requiredDataMask,
|
|
|
|
/* freeData */ freeData,
|
|
|
|
/* isDisabled */ NULL,
|
|
|
|
/* updateDepsgraph */ NULL,
|
|
|
|
/* dependsOnTime */ NULL,
|
|
|
|
/* dependsOnNormals */ NULL,
|
|
|
|
/* foreachObjectLink */ NULL,
|
|
|
|
/* foreachIDLink */ NULL,
|
|
|
|
/* foreachTexLink */ NULL,
|
2019-03-18 15:56:16 +01:00
|
|
|
/* freeRuntimeData */ NULL,
|
2016-12-28 17:30:58 +01:00
|
|
|
};
|