Skip to content

Commit

Permalink
Velocity based line thickness
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrlabs committed Sep 29, 2024
1 parent 1e09e6a commit 3dd930b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 27 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added
- Brush stroke stabilizer/smoothing
- Adaptive frame rate for reduced GPU/CPU load when the user is idle
- Velocity based line thickness (in addition to pen pressure)
- File tabs are now responsive and can be scrolled when not enough screen width is availble
- File tabs can be moved via drag and drop
- Native file dialogs
Expand Down
1 change: 0 additions & 1 deletion lorien/BrushStroke/BrushStroke.gd
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func refresh() -> void:
bottom_right.x = max(bottom_right.x, point.x)
bottom_right.y = max(bottom_right.y, point.y)

_line2d.width_curve.bake()
top_left_pos = top_left
bottom_right_pos = bottom_right
_visibility_notifier.rect = Utils.calculate_rect(top_left, bottom_right)
Expand Down
6 changes: 3 additions & 3 deletions lorien/Config.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const VERSION_MINOR := 7
const VERSION_PATCH := 0
const VERSION_STATUS := "-dev"
static var VERSION_STRING := "%d.%d.%d%s" % [VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_STATUS]
const CONFIG_PATH := "user://settings_v3.cfg"
const CONFIG_PATH := "user://settings_v4.cfg"
const PALETTES_PATH := "user://palettes.cfg"
const STATE_PATH := "user://state.cfg"
const MAX_PALETTE_SIZE := 40
Expand All @@ -14,8 +14,8 @@ const BACKGROUND_IDLE_TIME_THRESHOLD := 250 # in ms
const MIN_WINDOW_SIZE := Vector2(320, 256)
const DEFAULT_CANVAS_COLOR := Color("202124")
const DEFAULT_BRUSH_COLOR := Color.WHITE
const DEFAULT_BRUSH_SIZE := 12
const DEFAULT_PRESSURE_SENSITIVITY := 1.5
const DEFAULT_BRUSH_SIZE := 10
const DEFAULT_PRESSURE_SENSITIVITY := 1.0
const DEFAULT_CONSTANT_PRESSURE := false
const DEFAULT_SELECTION_COLOR := Color("#2a967c")
const DEFAULT_FOREGROUND_FPS := 144
Expand Down
22 changes: 13 additions & 9 deletions lorien/InfiniteCanvas/InfiniteCanvas.gd
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,6 @@ func disable() -> void:
func take_screenshot() -> Image:
return _viewport.get_texture().get_data()

# -------------------------------------------------------------------------------------------------
func start_stroke() -> void:
_current_stroke = BRUSH_STROKE.instantiate()
_current_stroke.size = _brush_size
_current_stroke.color = _brush_color

_strokes_parent.add_child(_current_stroke)
_optimizer.reset()

# -------------------------------------------------------------------------------------------------
func add_stroke(stroke: BrushStroke) -> void:
if _current_project != null:
Expand All @@ -235,6 +226,19 @@ func remove_last_stroke_point() -> void:
func remove_all_stroke_points() -> void:
_current_stroke.remove_all_points()

# -------------------------------------------------------------------------------------------------
func is_drawing() -> bool:
return _current_stroke != null

# -------------------------------------------------------------------------------------------------
func start_stroke() -> void:
_current_stroke = BRUSH_STROKE.instantiate()
_current_stroke.size = _brush_size
_current_stroke.color = _brush_color

_strokes_parent.add_child(_current_stroke)
_optimizer.reset()

# -------------------------------------------------------------------------------------------------
func end_stroke() -> void:
if _current_stroke != null:
Expand Down
2 changes: 1 addition & 1 deletion lorien/InfiniteCanvas/InfiniteCanvas.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[ext_resource type="Script" path="res://InfiniteCanvas/InfiniteCanvas.gd" id="1"]
[ext_resource type="Script" path="res://InfiniteCanvas/PanZoomCamera.gd" id="2"]
[ext_resource type="Curve" path="res://InfiniteCanvas/default_pressure_curve.tres" id="3"]
[ext_resource type="Curve" uid="uid://bgd7v60kyywsk" path="res://InfiniteCanvas/default_pressure_curve.tres" id="3"]
[ext_resource type="PackedScene" uid="uid://cf3j2vavqos04" path="res://InfiniteCanvas/Cursor/BrushCursor/BrushCursor.tscn" id="4"]
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/BrushTool.gd" id="5"]
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/LineTool.gd" id="6"]
Expand Down
34 changes: 24 additions & 10 deletions lorien/InfiniteCanvas/Tools/BrushTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const DOT_MAX_DISTANCE_THRESHOLD := 6.0

# -------------------------------------------------------------------------------------------------
var _current_pressure: float
var _current_velocity: float
var _moved := false
var _last_accepted_position: Vector2

Expand All @@ -20,6 +21,11 @@ func tool_event(event: InputEvent) -> void:

if event is InputEventMouseMotion:
_current_pressure = event.pressure

var screen := DisplayServer.screen_get_size()
var velocity: Vector2 = event.screen_velocity / Vector2(screen.x, screen.y)
_current_velocity = velocity.length()

if performing_stroke:
_moved = true
_cursor.set_pressure(event.pressure)
Expand Down Expand Up @@ -54,20 +60,28 @@ func _process(delta: float) -> void:
Settings.GENERAL_STABILIZER_STRENGTH, Config.DEFAULT_STABILIZER_STRENGTH
)

var points := get_current_brush_stroke().points
if points.size() > 3:
var p3 := points[-3]
var p2 := points[-2]
var p1 := points[-1]
# t is in [0.5, 1.0] interval depending on stabilizer settings
var t := 0.5 + (1.0 - stabilizer_strength) * 0.5
pos = Utils.cubic_bezier(p3, p2, p1, pos, t)

# Pressure
if stabilizer_strength >= 0.01:
var points := get_current_brush_stroke().points
if points.size() > 3:
var p3 := points[-3]
var p2 := points[-2]
var p1 := points[-1]
# t is in [0.5, 1.0] interval depending on stabilizer settings
var t := 0.5 + (1.0 - stabilizer_strength) * 0.5
pos = Utils.cubic_bezier(p3, p2, p1, pos, t)
#pos = Utils.quadratic_bezier(p2, p1, pos, t)


# Pressure & velocity
var sensitivity: float = Settings.get_value(
Settings.GENERAL_PRESSURE_SENSITIVITY, Config.DEFAULT_PRESSURE_SENSITIVITY
)

if _current_pressure >= 0.99:
_current_pressure -= min(_current_velocity * 1.5, 0.33)
else:
_current_pressure -= min(_current_velocity, 0.33)

var point_pressure := pressure_curve.sample(_current_pressure) * sensitivity

add_stroke_point(pos, point_pressure)
Expand Down
5 changes: 3 additions & 2 deletions lorien/InfiniteCanvas/default_pressure_curve.tres
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[gd_resource type="Curve" format=2]
[gd_resource type="Curve" format=3 uid="uid://bgd7v60kyywsk"]

[resource]
_data = [ Vector2( 0, 0.105263 ), 0.0, 0.210526, 0, 0, Vector2( 0.789286, 0.45614 ), 1.07456, 1.07456, 0, 0, Vector2( 1, 1 ), 3.50877, 0.0, 0, 0 ]
_data = [Vector2(0, 0.252555), 0.0, 0.747445, 0, 1, Vector2(1, 1), 0.747445, 0.0, 1, 0]
point_count = 2
2 changes: 1 addition & 1 deletion lorien/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func _exit_tree() -> void:
# -------------------------------------------------------------------------------------------------
func _process(delta: float) -> void:
# Lower fps if user is idle
if (Time.get_ticks_msec() - _last_input_time) > Config.BACKGROUND_IDLE_TIME_THRESHOLD:
if !_canvas.is_drawing() && (Time.get_ticks_msec() - _last_input_time) > Config.BACKGROUND_IDLE_TIME_THRESHOLD:
Engine.max_fps = Settings.get_value(Settings.RENDERING_BACKGROUND_FPS, Config.DEFAULT_BACKGROUND_FPS)

# Upate statusbar
Expand Down

0 comments on commit 3dd930b

Please sign in to comment.