Gpencil Noise - Add noise offset parameter

This patch adds a noise offset option to the grease pencil noise modifier.
It allows the user to animate the noise along the length of the stroke to create movement that is currently not possible.

It works by adding an offset to the noise table and adding the remaining floating point value to the noise table sampling.

Reviewed By: #grease_pencil

Differential Revision: https://developer.blender.org/D10021
This commit is contained in:
Cody Winchester 2021-01-14 22:25:17 +01:00 committed by Antonio Vazquez
parent 0f2ae614a1
commit 7a4bdc3a4f
4 changed files with 33 additions and 11 deletions

View File

@ -110,11 +110,11 @@ static bool dependsOnTime(GpencilModifierData *md)
return (mmd->flag & GP_NOISE_USE_RANDOM) != 0;
}
static float *noise_table(int len, int seed)
static float *noise_table(int len, int offset, int seed)
{
float *table = MEM_callocN(sizeof(float) * len, __func__);
for (int i = 0; i < len; i++) {
table[i] = BLI_hash_int_01(BLI_hash_int_2d(seed, i + 1));
table[i] = BLI_hash_int_01(BLI_hash_int_2d(seed, i + offset + 1));
}
return table;
}
@ -172,11 +172,19 @@ static void deformStroke(GpencilModifierData *md,
/* Sanitize as it can create out of bound reads. */
float noise_scale = clamp_f(mmd->noise_scale, 0.0f, 1.0f);
int len = ceilf(gps->totpoints * noise_scale) + 1;
float *noise_table_position = (mmd->factor > 0.0f) ? noise_table(len, seed + 2) : NULL;
float *noise_table_strength = (mmd->factor_strength > 0.0f) ? noise_table(len, seed + 3) : NULL;
float *noise_table_thickness = (mmd->factor_thickness > 0.0f) ? noise_table(len, seed) : NULL;
float *noise_table_uvs = (mmd->factor_uvs > 0.0f) ? noise_table(len, seed + 4) : NULL;
int len = ceilf(gps->totpoints * noise_scale) + 2;
float *noise_table_position = (mmd->factor > 0.0f) ?
noise_table(len, (int)floor(mmd->noise_offset), seed + 2) :
NULL;
float *noise_table_strength = (mmd->factor_strength > 0.0f) ?
noise_table(len, (int)floor(mmd->noise_offset), seed + 3) :
NULL;
float *noise_table_thickness = (mmd->factor_thickness > 0.0f) ?
noise_table(len, (int)floor(mmd->noise_offset), seed) :
NULL;
float *noise_table_uvs = (mmd->factor_uvs > 0.0f) ?
noise_table(len, (int)floor(mmd->noise_offset), seed + 4) :
NULL;
/* Calculate stroke normal. */
if (gps->totpoints > 2) {
@ -225,24 +233,27 @@ static void deformStroke(GpencilModifierData *md,
cross_v3_v3v3(vec2, vec1, normal);
normalize_v3(vec2);
float noise = table_sample(noise_table_position, i * noise_scale);
float noise = table_sample(noise_table_position,
i * noise_scale + fractf(mmd->noise_offset));
madd_v3_v3fl(&pt->x, vec2, (noise * 2.0f - 1.0f) * weight * mmd->factor * 0.1f);
}
if (mmd->factor_thickness > 0.0f) {
float noise = table_sample(noise_table_thickness, i * noise_scale);
float noise = table_sample(noise_table_thickness,
i * noise_scale + fractf(mmd->noise_offset));
pt->pressure *= max_ff(1.0f + (noise * 2.0f - 1.0f) * weight * mmd->factor_thickness, 0.0f);
CLAMP_MIN(pt->pressure, GPENCIL_STRENGTH_MIN);
}
if (mmd->factor_strength > 0.0f) {
float noise = table_sample(noise_table_strength, i * noise_scale);
float noise = table_sample(noise_table_strength,
i * noise_scale + fractf(mmd->noise_offset));
pt->strength *= max_ff(1.0f - noise * weight * mmd->factor_strength, 0.0f);
CLAMP(pt->strength, GPENCIL_STRENGTH_MIN, 1.0f);
}
if (mmd->factor_uvs > 0.0f) {
float noise = table_sample(noise_table_uvs, i * noise_scale);
float noise = table_sample(noise_table_uvs, i * noise_scale + fractf(mmd->noise_offset));
pt->uv_rot += (noise * 2.0f - 1.0f) * weight * mmd->factor_uvs * M_PI_2;
CLAMP(pt->uv_rot, -M_PI_2, M_PI_2);
}
@ -292,6 +303,7 @@ static void panel_draw(const bContext *UNUSED(C), Panel *panel)
uiItemR(col, ptr, "factor_thickness", 0, IFACE_("Thickness"), ICON_NONE);
uiItemR(col, ptr, "factor_uvs", 0, IFACE_("UV"), ICON_NONE);
uiItemR(col, ptr, "noise_scale", 0, NULL, ICON_NONE);
uiItemR(col, ptr, "noise_offset", 0, NULL, ICON_NONE);
uiItemR(col, ptr, "seed", 0, NULL, ICON_NONE);
gpencil_modifier_panel_end(layout, ptr);

View File

@ -152,6 +152,7 @@
.factor_thickness = 0.0f, \
.factor_uvs = 0.0f, \
.noise_scale = 0.0f, \
.noise_offset = 0.0f, \
.step = 4, \
.layer_pass = 0, \
.seed = 1, \

View File

@ -107,6 +107,8 @@ typedef struct NoiseGpencilModifierData {
float factor_uvs;
/** Noise Frequency scaling */
float noise_scale;
float noise_offset;
char _pad[4];
/** How many frames before recalculate randoms. */
int step;
/** Custom index for passes. */

View File

@ -498,6 +498,13 @@ static void rna_def_modifier_gpencilnoise(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Noise Scale", "Scale the noise frequency");
RNA_def_property_update(prop, 0, "rna_GpencilModifier_update");
prop = RNA_def_property(srna, "noise_offset", PROP_FLOAT, PROP_FACTOR);
RNA_def_property_float_sdna(prop, NULL, "noise_offset");
RNA_def_property_range(prop, 0.0, FLT_MAX);
RNA_def_property_ui_range(prop, 0.0, 100.0, 0.1, 3);
RNA_def_property_ui_text(prop, "Noise Offset", "Offset the noise along the strokes");
RNA_def_property_update(prop, 0, "rna_GpencilModifier_update");
prop = RNA_def_property(srna, "use_custom_curve", PROP_BOOLEAN, PROP_NONE);
RNA_def_property_boolean_sdna(prop, NULL, "flag", GP_NOISE_CUSTOM_CURVE);
RNA_def_property_ui_text(