pull/35357/head
deanlee 3 weeks ago
parent f0d872bb95
commit 756c4617b2
  1. 45
      system/ui/lib/shader_polygon.py
  2. 13
      system/ui/onroad/model_renderer.py

@ -15,7 +15,7 @@ uniform int pointCount;
uniform vec4 fillColor; uniform vec4 fillColor;
uniform vec2 resolution; uniform vec2 resolution;
uniform int useGradient; uniform bool useGradient;
uniform vec2 gradientStart; uniform vec2 gradientStart;
uniform vec2 gradientEnd; uniform vec2 gradientEnd;
uniform vec4 gradientColors[8]; uniform vec4 gradientColors[8];
@ -23,30 +23,24 @@ uniform float gradientStops[8];
uniform int gradientColorCount; uniform int gradientColorCount;
vec4 getGradientColor(vec2 pos) { vec4 getGradientColor(vec2 pos) {
vec2 gradientDir = gradientEnd - gradientStart; vec2 gradientDir = gradientEnd - gradientStart;
float gradientLength = length(gradientDir); float gradientLength = length(gradientDir);
if (gradientLength < 0.001) return gradientColors[0]; if (gradientLength < 0.001) return gradientColors[0];
float t = clamp(dot(pos - gradientStart, gradientDir) / (gradientLength * gradientLength), 0.0, 1.0); vec2 normalizedDir = gradientDir / gradientLength;
vec2 pointVec = pos - gradientStart;
float projection = dot(pointVec, normalizedDir);
float t = clamp(projection / gradientLength, 0.0, 1.0);
// Binary search for better performance with many stops for (int i = 0; i < gradientColorCount - 1; i++) {
int left = 0; if (t >= gradientStops[i] && t <= gradientStops[i+1]) {
int right = gradientColorCount - 1; float segmentT = (t - gradientStops[i]) / (gradientStops[i+1] - gradientStops[i]);
return mix(gradientColors[i], gradientColors[i+1], segmentT);
while (left < right - 1) {
int mid = (left + right) / 2;
if (t <= gradientStops[mid]) {
right = mid;
} else {
left = mid;
}
} }
}
if (left == right) return gradientColors[left]; return gradientColors[gradientColorCount-1];
float segmentT = (t - gradientStops[left]) / (gradientStops[right] - gradientStops[left]);
return mix(gradientColors[left], gradientColors[right], segmentT);
} }
float distanceToEdge(vec2 p) { float distanceToEdge(vec2 p) {
@ -69,9 +63,7 @@ float distanceToEdge(vec2 p) {
} }
float t = max(0.0, min(1.0, dot(v1, v2) / l2)); float t = max(0.0, min(1.0, dot(v1, v2) / l2));
vec2 projection = edge0 + t * v2; vec2 projection = edge0 + t * v2;
float dist = length(p - projection); float dist = length(p - projection);
minDist = min(minDist, dist); minDist = min(minDist, dist);
} }
@ -116,13 +108,9 @@ bool isPointInsidePolygon(vec2 p) {
void main() { void main() {
vec2 pixel = fragTexCoord * resolution; vec2 pixel = fragTexCoord * resolution;
bool inside = isPointInsidePolygon(pixel); bool inside = isPointInsidePolygon(pixel);
float dist = distanceToEdge(pixel); float dist = distanceToEdge(pixel);
float aaWidth = 1.0; float aaWidth = 1.0;
float alpha = inside ? float alpha = inside ?
min(1.0, dist / aaWidth) : min(1.0, dist / aaWidth) :
max(0.0, 1.0 - dist / aaWidth); max(0.0, 1.0 - dist / aaWidth);
@ -134,7 +122,6 @@ void main() {
} else { } else {
color = fillColor; color = fillColor;
} }
finalColor = vec4(color.rgb, color.a * alpha); finalColor = vec4(color.rgb, color.a * alpha);
} else { } else {
finalColor = vec4(0.0, 0.0, 0.0, 0.0); finalColor = vec4(0.0, 0.0, 0.0, 0.0);
@ -151,8 +138,8 @@ out vec2 fragTexCoord;
uniform mat4 mvp; uniform mat4 mvp;
void main() { void main() {
fragTexCoord = vertexTexCoord; fragTexCoord = vertexTexCoord;
gl_Position = mvp * vec4(vertexPosition, 1.0); gl_Position = mvp * vec4(vertexPosition, 1.0);
} }
""" """

@ -226,8 +226,6 @@ class ModelRenderer:
'colors': segment_colors, 'colors': segment_colors,
'stops': gradient_stops, 'stops': gradient_stops,
} }
# Draw the entire path with a single gradient fill
draw_polygon(self._track_vertices, gradient=gradient) draw_polygon(self._track_vertices, gradient=gradient)
else: else:
# Draw with throttle/no throttle gradient # Draw with throttle/no throttle gradient
@ -253,12 +251,11 @@ class ModelRenderer:
] ]
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': colors,
'stops': [0.0, 1.0] 'stops': [0.0, 1.0],
} }
# Draw path with gradient
draw_polygon(self._track_vertices, gradient=gradient) draw_polygon(self._track_vertices, gradient=gradient)
def _draw_lead(self, lead_data, vd, rect): def _draw_lead(self, lead_data, vd, rect):

Loading…
Cancel
Save