system/ui: fix rapid path color transition by correcting hardcoded increment (#35368)

refactor color blending with configurable transition duration
pull/35370/head
Dean Lee 2 weeks ago committed by GitHub
parent 3682fac7b6
commit db8ecf183f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 37
      system/ui/onroad/model_renderer.py

@ -4,13 +4,15 @@ import numpy as np
import pyray as rl import pyray as rl
from cereal import messaging, car from cereal import messaging, car
from openpilot.common.params import Params from openpilot.common.params import Params
from openpilot.system.ui.lib.application import DEFAULT_FPS
from openpilot.system.ui.lib.shader_polygon import draw_polygon from openpilot.system.ui.lib.shader_polygon import draw_polygon
CLIP_MARGIN = 500 CLIP_MARGIN = 500
MIN_DRAW_DISTANCE = 10.0 MIN_DRAW_DISTANCE = 10.0
MAX_DRAW_DISTANCE = 100.0 MAX_DRAW_DISTANCE = 100.0
PATH_COLOR_TRANSITION_DURATION = 0.5 # Seconds for color transition animation
PATH_BLEND_INCREMENT = 1.0 / (PATH_COLOR_TRANSITION_DURATION * DEFAULT_FPS)
THROTTLE_COLORS = [ THROTTLE_COLORS = [
rl.Color(25, 235, 99, 102), # HSLF(148/360, 0.94, 0.51, 0.4) rl.Color(25, 235, 99, 102), # HSLF(148/360, 0.94, 0.51, 0.4)
@ -238,22 +240,17 @@ class ModelRenderer:
# Update blend factor # Update blend factor
if self._blend_factor < 1.0: if self._blend_factor < 1.0:
self._blend_factor = min(self._blend_factor + 0.1, 1.0) self._blend_factor = min(self._blend_factor + PATH_BLEND_INCREMENT, 1.0)
begin_colors = NO_THROTTLE_COLORS if allow_throttle else THROTTLE_COLORS begin_colors = NO_THROTTLE_COLORS if allow_throttle else THROTTLE_COLORS
end_colors = THROTTLE_COLORS if allow_throttle else NO_THROTTLE_COLORS end_colors = THROTTLE_COLORS if allow_throttle else NO_THROTTLE_COLORS
# Blend colors based on transition # Blend colors based on transition
colors = [ blended_colors = self._blend_colors(begin_colors, end_colors, self._blend_factor)
self._blend_colors(begin_colors[0], end_colors[0], self._blend_factor),
self._blend_colors(begin_colors[1], end_colors[1], self._blend_factor),
self._blend_colors(begin_colors[2], end_colors[2], self._blend_factor),
]
gradient = { gradient = {
'start': (0.0, 1.0), # Bottom of path 'start': (0.0, 1.0), # Bottom of path
'end': (0.0, 0.0), # Top of path 'end': (0.0, 0.0), # Top of path
'colors': colors, 'colors': blended_colors,
'stops': [0.0, 0.5, 1.0], 'stops': [0.0, 0.5, 1.0],
} }
draw_polygon(self._track_vertices, gradient=gradient) draw_polygon(self._track_vertices, gradient=gradient)
@ -364,14 +361,16 @@ class ModelRenderer:
return rl.Color(r_val, g_val, b_val, a_val) return rl.Color(r_val, g_val, b_val, a_val)
@staticmethod @staticmethod
def _blend_colors(start: rl.Color, end: rl.Color, t: float) -> rl.Color: def _blend_colors(begin_colors, end_colors, t):
"""Blend between two colors with factor t"""
if t >= 1.0: if t >= 1.0:
return end return end_colors
if t <= 0.0:
return rl.Color( return begin_colors
int((1.0 - t) * start.r + t * end.r),
int((1.0 - t) * start.g + t * end.g), inv_t = 1.0 - t
int((1.0 - t) * start.b + t * end.b), return [rl.Color(
int((1.0 - t) * start.a + t * end.a), int(inv_t * start.r + t * end.r),
) int(inv_t * start.g + t * end.g),
int(inv_t * start.b + t * end.b),
int(inv_t * start.a + t * end.a)
) for start, end in zip(begin_colors, end_colors, strict=True)]

Loading…
Cancel
Save