Skip to content

Commit

Permalink
Brush stroke smoothing using cubic bezier curves
Browse files Browse the repository at this point in the history
It feels so much better!
  • Loading branch information
mbrlabs committed Aug 29, 2024
1 parent bb0ece2 commit e5e7c26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lorien/BrushStroke/BrushStrokeOptimizer.gd
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class_name BrushStrokeOptimizer

# -------------------------------------------------------------------------------------------------
const ANGLE_THRESHOLD := 1.0
const DISTANCE_THRESHOLD := 2.0
const ANGLE_THRESHOLD := 0.5
const DISTANCE_THRESHOLD := 1.0

# -------------------------------------------------------------------------------------------------
var points_removed := 0
Expand Down
13 changes: 11 additions & 2 deletions lorien/InfiniteCanvas/Tools/BrushTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,24 @@ func _process(delta: float) -> void:
if performing_stroke:
var pos := _cursor.global_position

# Basic smoothing
# Smoothing
var diff := pos.distance_squared_to(_last_accepted_position)
if diff <= MOVEMENT_THRESHOLD || _current_pressure <= MIN_PRESSURE:
return


var points := get_current_brush_stroke().points
if points.size() > 3:
var p3 := points[-3]
var p2 := points[-2]
var p1 := points[-1]
# TODO: expose the smoothing factor in the settings
pos = Utils.cubic_bezier(p3, p2, p1, pos, 0.75)

var sensitivity: float = Settings.get_general_value(
Settings.GENERAL_PRESSURE_SENSITIVITY, Config.DEFAULT_PRESSURE_SENSITIVITY
)

# Pressure
var point_pressure := pressure_curve.sample(_current_pressure) * sensitivity
if _first_point:
point_pressure *= 1.4
Expand Down
15 changes: 15 additions & 0 deletions lorien/Misc/Utils.gd
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,18 @@ func translate_action(action_name: String) -> String:
# Does an _exact_ match for the given key stroke.
func event_pressed_bug_workaround(action_name: String, event: InputEvent) -> bool:
return InputMap.action_has_event(action_name, event) && event.is_pressed() && !event.is_echo()

# -------------------------------------------------------------------------------------------------
func cubic_bezier(p0: Vector2, p1: Vector2, p2: Vector2, p3: Vector2, t: float) -> Vector2:
var q0 := p0.lerp(p1, t)
var q1 := p1.lerp(p2, t)
var q2 := p2.lerp(p3, t)
var r0 := q0.lerp(q1, t)
var r1 := q1.lerp(q2, t)
return r0.lerp(r1, t)

# -------------------------------------------------------------------------------------------------
func quadratic_bezier(p0: Vector2, p1: Vector2, p2: Vector2, t: float) -> Vector2:
var q0 := p0.lerp(p1, t)
var q1 := p1.lerp(p2, t)
return q0.lerp(q1, t)

0 comments on commit e5e7c26

Please sign in to comment.