Skip to content

Commit 241934b

Browse files
endankegithub-actions[bot]
authored andcommitted
Use matrices to pass variable curve points
GitOrigin-RevId: 92df0a5dfe92657576dd19cbf9c293539efb1e81
1 parent 1e23d86 commit 241934b

1 file changed

Lines changed: 39 additions & 26 deletions

File tree

src/shaders/line.vertex.glsl

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ uniform float u_width_scale;
3838
uniform highp float u_floor_width_scale;
3939

4040
#ifdef RENDER_LINE_CURVE
41-
uniform vec3 u_curve_point_a;
42-
uniform vec3 u_curve_point_b;
43-
uniform vec3 u_curve_point_c;
44-
uniform vec3 u_curve_point_d;
45-
uniform vec3 u_curve_point_e;
41+
// Encodes curve control points in 3x3 matrices for x, y, z
42+
// Note: Could be replaced with an uniform array once Metal support is implemented
43+
uniform mat3 u_curve_points_x;
44+
uniform mat3 u_curve_points_y;
45+
uniform mat3 u_curve_points_z;
46+
uniform float u_curve_point_count;
4647
#endif
4748

4849
#ifdef ELEVATED
@@ -107,13 +108,15 @@ out highp float v_depth;
107108

108109
#ifdef RENDER_LINE_CURVE
109110

110-
vec4 curveWeights(float curve_seg) {
111-
return vec4(
112-
1.0 - step(0.5, abs(curve_seg)),
113-
1.0 - step(0.5, abs(curve_seg - 1.0)),
114-
1.0 - step(0.5, abs(curve_seg - 2.0)),
115-
1.0 - step(0.5, abs(curve_seg - 3.0))
116-
);
111+
vec3 getCurvePoint(int index) {
112+
int row = index / 3;
113+
int col = index - row * 3;
114+
115+
float x = u_curve_points_x[row][col];
116+
float y = u_curve_points_y[row][col];
117+
float z = u_curve_points_z[row][col];
118+
119+
return vec3(x, y, z);
117120
}
118121

119122
vec3 catmullRom(vec3 p0, vec3 p1, vec3 p2, vec3 p3, float t) {
@@ -141,15 +144,29 @@ struct CurveResult {
141144
vec2 tangent; // tangent direction
142145
};
143146

144-
CurveResult calculateCurve(float tl, vec4 w) {
145-
vec3 pA0 = u_curve_point_a - (u_curve_point_b - u_curve_point_a);
146-
vec3 pE3 = u_curve_point_e + (u_curve_point_e - u_curve_point_d);
147-
vec3 p0 = w.x * pA0 + w.y * u_curve_point_a + w.z * u_curve_point_b + w.w * u_curve_point_c;
148-
vec3 p1 = w.x * u_curve_point_a + w.y * u_curve_point_b + w.z * u_curve_point_c + w.w * u_curve_point_d;
149-
vec3 p2 = w.x * u_curve_point_b + w.y * u_curve_point_c + w.z * u_curve_point_d + w.w * u_curve_point_e;
150-
vec3 p3 = w.x * u_curve_point_c + w.y * u_curve_point_d + w.z * u_curve_point_e + w.w * pE3;
151-
vec3 point = catmullRom(p0, p1, p2, p3, tl);
152-
vec2 tangent = catmullRomTangent(p0.xy, p1.xy, p2.xy, p3.xy, tl) * 4.0;
147+
CurveResult calculateCurve(float line_progress) {
148+
float curve_progress = line_progress * (u_curve_point_count - 1.0);
149+
float curve_progress_local = fract(curve_progress);
150+
float curve_segment = floor(curve_progress);
151+
int seg = int(curve_segment);
152+
153+
vec3 p1 = getCurvePoint(seg);
154+
vec3 p2 = getCurvePoint(seg + 1);
155+
156+
float is_first_seg = step(curve_segment, 0.5);
157+
vec3 p0_extrapolated = p1 - (p2 - p1);
158+
vec3 p0_fetched = getCurvePoint(max(seg - 1, 0));
159+
vec3 p0 = mix(p0_fetched, p0_extrapolated, is_first_seg);
160+
161+
int last_seg = int(u_curve_point_count) - 2;
162+
float is_last_seg = step(float(last_seg) - 0.5, curve_segment);
163+
vec3 p3_extrapolated = p2 + (p2 - p1);
164+
vec3 p3_fetched = getCurvePoint(min(seg + 2, int(u_curve_point_count) - 1));
165+
vec3 p3 = mix(p3_fetched, p3_extrapolated, is_last_seg);
166+
167+
vec3 point = catmullRom(p0, p1, p2, p3, curve_progress_local);
168+
vec2 tangent = catmullRomTangent(p0.xy, p1.xy, p2.xy, p3.xy, curve_progress_local) * (u_curve_point_count - 1.0);
169+
153170
CurveResult result;
154171
result.point = point;
155172
result.tangent = tangent;
@@ -202,11 +219,7 @@ void main() {
202219
bool left = normal.y == 1.0;
203220

204221
#ifdef RENDER_LINE_CURVE
205-
float curve_progress = clamp(line_progress, 0.0, 0.999999) * 4.0;
206-
float curve_progress_local = fract(curve_progress);
207-
float curve_segment = floor(curve_progress);
208-
vec4 curve_w = curveWeights(curve_segment);
209-
CurveResult curve = calculateCurve(curve_progress_local, curve_w);
222+
CurveResult curve = calculateCurve(line_progress);
210223
pos = curve.point.xy * 8192.0;
211224
a_extrude = length(a_extrude) * normalize(curve.tangent);
212225
a_extrude = left ? vec2(-a_extrude.y, a_extrude.x) : vec2(a_extrude.y, -a_extrude.x);

0 commit comments

Comments
 (0)