Skip to content

Commit a31f0ef

Browse files
committed
Give up on gradient blending in trajectories for now
1 parent 7d1aca7 commit a31f0ef

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

modules/carto/src/layers/edged-layers.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ function patchShaders(shaders: any, outline: string): void {
2323
// Interpolate colors between vertices
2424
shaders.inject = {
2525
...shaders.inject,
26-
'vs:#decl': `${shaders.inject['vs:#decl']}in vec4 instanceEndColors;`
26+
'vs:#decl': `${shaders.inject['vs:#decl']}\nin vec4 instanceEndColors;\nout vec4 vEndColor;`,
27+
'fs:#decl': `${shaders.inject['fs:#decl']}\nin vec4 vEndColor;`,
28+
'vs:#main-end': `${shaders.inject['vs:#main-end']}\nvEndColor = vec4(instanceEndColors.rgb, instanceEndColors.a * layer.opacity);`
2729
};
28-
shaders.vs = shaders.vs.replace(
30+
31+
shaders.fs = shaders.fs.replace(
2932
/* glsl */ `
30-
'vColor = vec4(instanceColors.rgb, instanceColors.a * layer.opacity);
33+
fragColor = vColor;
3134
`,
3235
/* glsl */ `
33-
vec4 color = mix(instanceColors, instanceEndColors, positions.x);
34-
vColor = vec4(color.rgb, color.a * layer.opacity);
36+
float f = clamp(vPathPosition.y, 0.0, 1.0);
37+
fragColor = mix(vColor, vEndColor, f);
3538
`
3639
);
3740

@@ -49,9 +52,11 @@ function patchInstanceColors(attributeManager: AttributeManager): void {
4952
attributeManager.addInstanced({
5053
instanceColors: {
5154
...instanceColors.settings,
55+
// Sort of works, but end caps introduce aritfacts
56+
vertexOffset: 1,
5257
shaderAttributes: {
53-
instanceColors: {vertexOffset: 0},
54-
instanceEndColors: {vertexOffset: 1}
58+
instanceColors: {vertexOffset: 1},
59+
instanceEndColors: {vertexOffset: 2}
5560
}
5661
}
5762
});

modules/carto/src/layers/trajectory-tile-layer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ export default class TrajectoryTileLayer<
169169
_subLayerProps: {
170170
...props._subLayerProps,
171171
linestrings: {
172-
type: EdgedTripsLayer,
173-
getTimestamps: (d: any, info: any) => info.index,
172+
//type: EdgedTripsLayer,
173+
type: TripsLayer,
174174
currentTime: showTrips ? normalizedCurrentTime : totalTimeSpan,
175175
trailLength: showTrips ? props.trailLength : totalTimeSpan,
176176
parameters: {depthTest: false},

test/apps/carto-trajectories/app.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {MapboxOverlay} from '@deck.gl/mapbox';
66

77
import {DeckProps } from '@deck.gl/core';
88
import {trajectoryQuerySource, trajectoryTableSource} from '@carto/api-client';
9-
import {TrajectoryTileLayer, colorContinuous} from '@deck.gl/carto';
9+
import {TrajectoryTileLayer, colorCategories, colorContinuous} from '@deck.gl/carto';
1010
import {normalizeTimestamp} from '@deck.gl/carto/layers/trajectory-utils';
1111

1212
import RangeInput from './range-input';
@@ -296,16 +296,38 @@ export default function App() {
296296
data: dataSource,
297297
renderMode: showTrips ? 'trips' : 'paths',
298298
uniqueIdProperty: 'trajectoryId', // TODO internalize this
299-
// Example: Use speed-based coloring with CARTO palette
299+
300+
// Color entire line by trajectoryId
301+
//_getLineColor: colorContinuous({
302+
// attr: 'trajectoryId',
303+
// domain: [0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000],
304+
// colors: palette
305+
//}),
306+
getLineColor: (d: any, info: any) => {
307+
//return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
308+
const { data, index } = info;
309+
const nVertices = data.startIndices[index + 1] - data.startIndices[index];
310+
const colors = new Array(nVertices).fill(0).map((_, i) => {
311+
return [255 * Math.random(), 255 * Math.random(), 255 * Math.random(), 255];
312+
});
313+
314+
// Return an array to color each vertex
315+
return colors;
316+
},
317+
318+
319+
// Alternative API, dedicated accessor: Color each vertex by speed
300320
getVertexColor: colorContinuous({
301321
attr: (d: any) => d.speed,
302322
domain: [5, 7, 9, 11, 13, 15],
303323
colors: palette
304324
}),
305325

306326
// Alternative examples:
307-
// getVertexColor: ({speed}) => speed > 10 ? [255, 0, 0, 255] : [0, 255, 0, 255], // Red/Green based on speed
308-
// getVertexColor: ({speed}, {index}) => [Math.min(255, speed * 20), 100, 200, 255], // Speed-based red intensity
327+
// getVertexColor: (d: any) => {
328+
329+
// return d.speed > 10 ? [255, 0, 0, 255] : [0, 255, 0, 255]; // Red/Green based on speed
330+
//},
309331
currentTime,
310332
trailLength,
311333
getWidth: lineWidth,

0 commit comments

Comments
 (0)