2018-07-31 10:22:19 +02: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
|
2020-05-09 17:14:35 +10:00
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2018-07-31 10:22:19 +02:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2017, Blender Foundation
|
|
|
|
|
* This is a new part of Blender
|
|
|
|
|
*/
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup modifiers
|
2018-07-31 10:22:19 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
2020-03-09 16:27:24 +01:00
|
|
|
#include "BLI_listbase.h"
|
2019-02-25 11:39:14 +01:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_gpencil_modifier_types.h"
|
|
|
|
|
#include "DNA_gpencil_types.h"
|
2018-07-31 10:22:19 +02:00
|
|
|
#include "DNA_meshdata_types.h"
|
|
|
|
|
#include "DNA_object_types.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "DNA_scene_types.h"
|
2018-07-31 10:22:19 +02:00
|
|
|
|
|
|
|
|
#include "BKE_deform.h"
|
2020-03-19 11:35:17 +01:00
|
|
|
#include "BKE_gpencil_geom.h"
|
2018-07-31 10:22:19 +02:00
|
|
|
#include "BKE_gpencil_modifier.h"
|
|
|
|
|
#include "BKE_lattice.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_layer.h"
|
2020-02-10 12:58:59 +01:00
|
|
|
#include "BKE_lib_query.h"
|
2018-07-31 10:22:19 +02:00
|
|
|
#include "BKE_main.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BKE_modifier.h"
|
|
|
|
|
#include "BKE_scene.h"
|
2018-07-31 10:22:19 +02:00
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
|
|
#include "MOD_gpencil_modifiertypes.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "MOD_gpencil_util.h"
|
2018-07-31 10:22:19 +02:00
|
|
|
|
|
|
|
|
#include "DEG_depsgraph.h"
|
|
|
|
|
#include "DEG_depsgraph_build.h"
|
|
|
|
|
#include "DEG_depsgraph_query.h"
|
|
|
|
|
|
|
|
|
|
static void initData(GpencilModifierData *md)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
LatticeGpencilModifierData *gpmd = (LatticeGpencilModifierData *)md;
|
|
|
|
|
gpmd->pass_index = 0;
|
|
|
|
|
gpmd->layername[0] = '\0';
|
2019-08-21 08:30:45 +02:00
|
|
|
gpmd->materialname[0] = '\0';
|
2019-04-17 06:17:24 +02:00
|
|
|
gpmd->vgname[0] = '\0';
|
|
|
|
|
gpmd->object = NULL;
|
|
|
|
|
gpmd->cache_data = NULL;
|
|
|
|
|
gpmd->strength = 1.0f;
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
|
|
|
|
|
{
|
2020-05-08 10:32:30 +02:00
|
|
|
BKE_gpencil_modifier_copydata_generic(md, target);
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
static void deformStroke(GpencilModifierData *md,
|
|
|
|
|
Depsgraph *UNUSED(depsgraph),
|
|
|
|
|
Object *ob,
|
|
|
|
|
bGPDlayer *gpl,
|
2019-08-02 09:50:33 +02:00
|
|
|
bGPDframe *UNUSED(gpf),
|
2019-04-17 06:17:24 +02:00
|
|
|
bGPDstroke *gps)
|
2018-07-31 10:22:19 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
LatticeGpencilModifierData *mmd = (LatticeGpencilModifierData *)md;
|
2020-03-06 12:50:56 +11:00
|
|
|
const int def_nr = BKE_object_defgroup_name_index(ob, mmd->vgname);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
|
if (!is_stroke_affected_by_modifier(ob,
|
|
|
|
|
mmd->layername,
|
2019-08-21 08:30:45 +02:00
|
|
|
mmd->materialname,
|
2019-04-17 06:17:24 +02:00
|
|
|
mmd->pass_index,
|
|
|
|
|
mmd->layer_pass,
|
|
|
|
|
1,
|
|
|
|
|
gpl,
|
|
|
|
|
gps,
|
|
|
|
|
mmd->flag & GP_LATTICE_INVERT_LAYER,
|
|
|
|
|
mmd->flag & GP_LATTICE_INVERT_PASS,
|
2019-08-21 08:30:45 +02:00
|
|
|
mmd->flag & GP_LATTICE_INVERT_LAYERPASS,
|
|
|
|
|
mmd->flag & GP_LATTICE_INVERT_MATERIAL)) {
|
2019-04-17 06:17:24 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mmd->cache_data == NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < gps->totpoints; i++) {
|
|
|
|
|
bGPDspoint *pt = &gps->points[i];
|
|
|
|
|
MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
|
|
|
|
|
|
|
|
|
|
/* verify vertex group */
|
|
|
|
|
const float weight = get_modifier_point_weight(
|
|
|
|
|
dvert, (mmd->flag & GP_LATTICE_INVERT_VGROUP) != 0, def_nr);
|
|
|
|
|
if (weight < 0.0f) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
calc_latt_deform((struct LatticeDeformData *)mmd->cache_data, &pt->x, mmd->strength * weight);
|
|
|
|
|
}
|
2020-03-09 16:27:24 +01:00
|
|
|
/* Calc geometry data. */
|
|
|
|
|
BKE_gpencil_stroke_geometry_update(gps);
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* FIXME: Ideally we be doing this on a copy of the main depsgraph
|
|
|
|
|
* (i.e. one where we don't have to worry about restoring state)
|
|
|
|
|
*/
|
2019-04-17 06:17:24 +02:00
|
|
|
static void bakeModifier(Main *bmain, Depsgraph *depsgraph, GpencilModifierData *md, Object *ob)
|
2018-07-31 10:22:19 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
LatticeGpencilModifierData *mmd = (LatticeGpencilModifierData *)md;
|
|
|
|
|
Scene *scene = DEG_get_evaluated_scene(depsgraph);
|
|
|
|
|
struct LatticeDeformData *ldata = NULL;
|
|
|
|
|
bGPdata *gpd = ob->data;
|
|
|
|
|
int oldframe = (int)DEG_get_ctime(depsgraph);
|
|
|
|
|
|
2019-04-22 09:16:44 +10:00
|
|
|
if (mmd->object == NULL) {
|
2019-04-17 06:17:24 +02:00
|
|
|
return;
|
2019-04-22 09:16:44 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-03-09 16:27:24 +01:00
|
|
|
LISTBASE_FOREACH (bGPDlayer *, gpl, &gpd->layers) {
|
|
|
|
|
LISTBASE_FOREACH (bGPDframe *, gpf, &gpl->frames) {
|
2019-04-17 06:17:24 +02:00
|
|
|
/* apply lattice effects on this frame
|
|
|
|
|
* NOTE: this assumes that we don't want lattice animation on non-keyframed frames
|
|
|
|
|
*/
|
|
|
|
|
CFRA = gpf->framenum;
|
|
|
|
|
BKE_scene_graph_update_for_newframe(depsgraph, bmain);
|
|
|
|
|
|
|
|
|
|
/* recalculate lattice data */
|
|
|
|
|
BKE_gpencil_lattice_init(ob);
|
|
|
|
|
|
|
|
|
|
/* compute lattice effects on this frame */
|
2020-03-09 16:27:24 +01:00
|
|
|
LISTBASE_FOREACH (bGPDstroke *, gps, &gpf->strokes) {
|
2019-08-02 09:50:33 +02:00
|
|
|
deformStroke(md, depsgraph, ob, gpl, gpf, gps);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* free lingering data */
|
|
|
|
|
ldata = (struct LatticeDeformData *)mmd->cache_data;
|
|
|
|
|
if (ldata) {
|
|
|
|
|
end_latt_deform(ldata);
|
|
|
|
|
mmd->cache_data = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* return frame state and DB to original state */
|
|
|
|
|
CFRA = oldframe;
|
|
|
|
|
BKE_scene_graph_update_for_newframe(depsgraph, bmain);
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void freeData(GpencilModifierData *md)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
LatticeGpencilModifierData *mmd = (LatticeGpencilModifierData *)md;
|
|
|
|
|
struct LatticeDeformData *ldata = (struct LatticeDeformData *)mmd->cache_data;
|
|
|
|
|
/* free deform data */
|
|
|
|
|
if (ldata) {
|
|
|
|
|
end_latt_deform(ldata);
|
|
|
|
|
}
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isDisabled(GpencilModifierData *md, int UNUSED(userRenderParams))
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
LatticeGpencilModifierData *mmd = (LatticeGpencilModifierData *)md;
|
2018-07-31 10:22:19 +02:00
|
|
|
|
2019-08-23 15:50:53 +02:00
|
|
|
/* The object type check is only needed here in case we have a placeholder
|
|
|
|
|
* object assigned (because the library containing the lattice is missing).
|
|
|
|
|
*
|
2019-08-30 10:52:39 +10:00
|
|
|
* In other cases it should be impossible to have a type mismatch.
|
2019-08-23 15:50:53 +02:00
|
|
|
*/
|
|
|
|
|
return !mmd->object || mmd->object->type != OB_LATTICE;
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void updateDepsgraph(GpencilModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
|
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
LatticeGpencilModifierData *lmd = (LatticeGpencilModifierData *)md;
|
|
|
|
|
if (lmd->object != NULL) {
|
|
|
|
|
DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_GEOMETRY, "Lattice Modifier");
|
|
|
|
|
DEG_add_object_relation(ctx->node, lmd->object, DEG_OB_COMP_TRANSFORM, "Lattice Modifier");
|
|
|
|
|
}
|
|
|
|
|
DEG_add_object_relation(ctx->node, ctx->object, DEG_OB_COMP_TRANSFORM, "Lattice Modifier");
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
static void foreachObjectLink(GpencilModifierData *md,
|
|
|
|
|
Object *ob,
|
|
|
|
|
ObjectWalkFunc walk,
|
|
|
|
|
void *userData)
|
2018-07-31 10:22:19 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
LatticeGpencilModifierData *mmd = (LatticeGpencilModifierData *)md;
|
2018-07-31 10:22:19 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
walk(userData, ob, &mmd->object, IDWALK_CB_NOP);
|
2018-07-31 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GpencilModifierTypeInfo modifierType_Gpencil_Lattice = {
|
2019-04-17 06:17:24 +02:00
|
|
|
/* name */ "Lattice",
|
|
|
|
|
/* structName */ "LatticeGpencilModifierData",
|
|
|
|
|
/* structSize */ sizeof(LatticeGpencilModifierData),
|
|
|
|
|
/* type */ eGpencilModifierTypeType_Gpencil,
|
|
|
|
|
/* flags */ eGpencilModifierTypeFlag_Single | eGpencilModifierTypeFlag_SupportsEditmode,
|
|
|
|
|
|
|
|
|
|
/* copyData */ copyData,
|
|
|
|
|
|
|
|
|
|
/* deformStroke */ deformStroke,
|
|
|
|
|
/* generateStrokes */ NULL,
|
|
|
|
|
/* bakeModifier */ bakeModifier,
|
|
|
|
|
/* remapTime */ NULL,
|
|
|
|
|
|
|
|
|
|
/* initData */ initData,
|
|
|
|
|
/* freeData */ freeData,
|
|
|
|
|
/* isDisabled */ isDisabled,
|
|
|
|
|
/* updateDepsgraph */ updateDepsgraph,
|
|
|
|
|
/* dependsOnTime */ NULL,
|
|
|
|
|
/* foreachObjectLink */ foreachObjectLink,
|
|
|
|
|
/* foreachIDLink */ NULL,
|
|
|
|
|
/* foreachTexLink */ NULL,
|
2018-07-31 10:22:19 +02:00
|
|
|
};
|