Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenkernel/intern/gpencil.c
| Show First 20 Lines • Show All 1,778 Lines • ▼ Show 20 Lines | |||||
| /** | /** | ||||
| * Apply smooth for strength to stroke point */ | * Apply smooth for strength to stroke point */ | ||||
| bool BKE_gpencil_smooth_stroke_strength(bGPDstroke *gps, int point_index, float influence) | bool BKE_gpencil_smooth_stroke_strength(bGPDstroke *gps, int point_index, float influence) | ||||
| { | { | ||||
| bGPDspoint *ptb = &gps->points[point_index]; | bGPDspoint *ptb = &gps->points[point_index]; | ||||
| /* Do nothing if not enough points */ | /* Do nothing if not enough points */ | ||||
| if (gps->totpoints <= 2) { | if ((gps->totpoints <= 2) || (point_index < 1)) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| /* Only affect endpoints by a fraction of the normal influence */ | |||||
| float inf = influence; | |||||
| if ((point_index == 0) || (point_index == gps->totpoints - 1)) { | |||||
| inf *= 0.01f; | |||||
| } | |||||
| CLAMP_MAX(inf, 1.0f); | |||||
| float total = 0.0f; | |||||
| float max_strength = 0.0f; | |||||
| const int steps = 4; | |||||
| const float average_fac = 1.0f / (float)(steps * 2 + 1); | |||||
| int step; | |||||
| /* Compute theoretical optimal value using distances */ | /* add the point itself */ | ||||
| bGPDspoint *pta, *ptc; | total += ptb->strength * average_fac; | ||||
| int before = point_index - 1; | max_strength = ptb->strength; | ||||
| int after = point_index + 1; | |||||
| /* n-steps before/after current point */ | |||||
| for (step = 1; step <= steps; step++) { | |||||
| bGPDspoint *pt1, *pt2; | |||||
| int before = point_index - step; | |||||
| int after = point_index + step; | |||||
| CLAMP_MIN(before, 0); | CLAMP_MIN(before, 0); | ||||
| CLAMP_MAX(after, gps->totpoints - 1); | CLAMP_MAX(after, gps->totpoints - 1); | ||||
| pta = &gps->points[before]; | pt1 = &gps->points[before]; | ||||
| ptc = &gps->points[after]; | pt2 = &gps->points[after]; | ||||
| /* the optimal value is the corresponding to the interpolation of the strength | /* add both these points to the average-sum (s += p[i]/n) */ | ||||
| * at the distance of point b | total += pt1->strength * average_fac; | ||||
| */ | total += pt2->strength * average_fac; | ||||
| float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x); | /* Save max value. */ | ||||
| /* sometimes the factor can be wrong due stroke geometry, so use middle point */ | if (max_strength < pt1->strength) { | ||||
| if ((fac < 0.0f) || (fac > 1.0f)) { | max_strength = pt1->strength; | ||||
| fac = 0.5f; | } | ||||
| if (max_strength < pt2->strength) { | |||||
| max_strength = pt2->strength; | |||||
| } | |||||
| } | } | ||||
| const float optimal = (1.0f - fac) * pta->strength + fac * ptc->strength; | |||||
| /* Based on influence factor, blend between original and optimal */ | /* Based on influence factor, blend between original and optimal smoothed value. */ | ||||
| ptb->strength = (1.0f - influence) * ptb->strength + influence * optimal; | ptb->strength = interpf(ptb->strength, total, inf); | ||||
| /* Clamp to maximum stroke strength to avoid weird results. */ | |||||
| CLAMP_MAX(ptb->strength, max_strength); | |||||
| return true; | return true; | ||||
| } | } | ||||
| /** | /** | ||||
| * Apply smooth for thickness to stroke point (use pressure) */ | * Apply smooth for thickness to stroke point (use pressure) */ | ||||
| bool BKE_gpencil_smooth_stroke_thickness(bGPDstroke *gps, int point_index, float influence) | bool BKE_gpencil_smooth_stroke_thickness(bGPDstroke *gps, int point_index, float influence) | ||||
| { | { | ||||
| bGPDspoint *ptb = &gps->points[point_index]; | bGPDspoint *ptb = &gps->points[point_index]; | ||||
| /* Do nothing if not enough points */ | /* Do nothing if not enough points */ | ||||
| if ((gps->totpoints <= 2) || (point_index < 1)) { | if ((gps->totpoints <= 2) || (point_index < 1)) { | ||||
| return false; | return false; | ||||
| } | } | ||||
| /* Only affect endpoints by a fraction of the normal influence */ | |||||
| float inf = influence; | |||||
| if ((point_index == 0) || (point_index == gps->totpoints - 1)) { | |||||
| inf *= 0.01f; | |||||
| } | |||||
| CLAMP_MAX(inf, 1.0f); | |||||
| float total = 0.0f; | |||||
| float max_pressure = 0.0f; | |||||
| const int steps = 4; | |||||
| const float average_fac = 1.0f / (float)(steps * 2 + 1); | |||||
| int step; | |||||
| /* Compute theoretical optimal value using distances */ | /* add the point itself */ | ||||
| bGPDspoint *pta, *ptc; | total += ptb->pressure * average_fac; | ||||
| int before = point_index - 1; | max_pressure = ptb->pressure; | ||||
| int after = point_index + 1; | |||||
| /* n-steps before/after current point */ | |||||
| for (step = 1; step <= steps; step++) { | |||||
| bGPDspoint *pt1, *pt2; | |||||
| int before = point_index - step; | |||||
| int after = point_index + step; | |||||
| CLAMP_MIN(before, 0); | CLAMP_MIN(before, 0); | ||||
| CLAMP_MAX(after, gps->totpoints - 1); | CLAMP_MAX(after, gps->totpoints - 1); | ||||
| pta = &gps->points[before]; | pt1 = &gps->points[before]; | ||||
| ptc = &gps->points[after]; | pt2 = &gps->points[after]; | ||||
| /* the optimal value is the corresponding to the interpolation of the pressure | /* add both these points to the average-sum (s += p[i]/n) */ | ||||
| * at the distance of point b | total += pt1->pressure * average_fac; | ||||
| */ | total += pt2->pressure * average_fac; | ||||
| float fac = line_point_factor_v3(&ptb->x, &pta->x, &ptc->x); | /* Save max value. */ | ||||
| /* sometimes the factor can be wrong due stroke geometry, so use middle point */ | if (max_pressure < pt1->pressure) { | ||||
| if ((fac < 0.0f) || (fac > 1.0f)) { | max_pressure = pt1->pressure; | ||||
| fac = 0.5f; | } | ||||
| if (max_pressure < pt2->pressure) { | |||||
| max_pressure = pt2->pressure; | |||||
| } | |||||
| } | } | ||||
| float optimal = interpf(ptc->pressure, pta->pressure, fac); | |||||
| /* Based on influence factor, blend between original and optimal */ | |||||
| ptb->pressure = interpf(optimal, ptb->pressure, influence); | |||||
| /* Based on influence factor, blend between original and optimal smoothed value. */ | |||||
| ptb->pressure = interpf(ptb->pressure, total, inf); | |||||
| /* Clamp to maximum stroke thickness to avoid weird results. */ | |||||
| CLAMP_MAX(ptb->pressure, max_pressure); | |||||
| return true; | return true; | ||||
| } | } | ||||
| /** | /** | ||||
| * Apply smooth for UV rotation to stroke point (use pressure). | * Apply smooth for UV rotation to stroke point (use pressure). | ||||
| */ | */ | ||||
| bool BKE_gpencil_smooth_stroke_uv(bGPDstroke *gps, int point_index, float influence) | bool BKE_gpencil_smooth_stroke_uv(bGPDstroke *gps, int point_index, float influence) | ||||
| { | { | ||||
| ▲ Show 20 Lines • Show All 674 Lines • Show Last 20 Lines | |||||