Skip to content

Commit 4a9e721

Browse files
committed
Fix aligned projections
1 parent c039675 commit 4a9e721

File tree

5 files changed

+39
-24
lines changed

5 files changed

+39
-24
lines changed

fill-vert.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const float MAX_LINES = 256.;
1414
void main() {
1515
float depth = (MAX_LINES - 4. - id) / (MAX_LINES);
1616

17-
vec2 position = (position + translate) * scale
18-
+ (positionFract + translateFract) * scale
19-
+ (position + translate) * scaleFract
20-
+ (positionFract + translateFract) * scaleFract;
17+
vec2 position = position * scale
18+
+ positionFract * scale
19+
+ position * scaleFract + translate
20+
+ positionFract * scaleFract + translateFract;
2121

2222
gl_Position = vec4(position * 2.0 - 1.0, depth, 1);
2323

index.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@ function createLine (regl, options) {
426426
options = extend({}, defaults, options)
427427
}
428428

429+
429430
//calculate state values
430431
updateDiff(state, options, [{
431432
thickness: parseFloat,
@@ -442,6 +443,7 @@ function createLine (regl, options) {
442443
let count = Math.floor(positions.length / 2)
443444
let bounds = getBounds(positions, 2)
444445

446+
// FIXME: make it dynamic
445447
if (!state.range && !options.range) {
446448
options.range = bounds
447449
}
@@ -579,10 +581,20 @@ function createLine (regl, options) {
579581
let bounds = state.bounds
580582
if (!range) range = bounds
581583

582-
let nrange = normalize(range.slice(), 2, bounds)
584+
let boundsW = bounds[2] - bounds[0],
585+
boundsH = bounds[3] - bounds[1]
586+
587+
let rangeW = range[2] - range[0],
588+
rangeH = range[3] - range[1]
583589

584-
state.scale = [Math.min(1 / (nrange[2] - nrange[0]), MAX_SCALE), Math.min(1 / (nrange[3] - nrange[1]), MAX_SCALE)]
585-
state.translate = [-nrange[0], -nrange[1]]
590+
state.scale = [
591+
boundsW / rangeW,
592+
boundsH / rangeH
593+
]
594+
state.translate = [
595+
-range[0] / rangeW + bounds[0] / rangeW || 0,
596+
-range[1] / rangeH + bounds[1] / rangeH || 0
597+
]
586598

587599
state.scaleFract = fract32(state.scale)
588600
state.translateFract = fract32(state.translate)

miter-vert.glsl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,17 @@ void main() {
137137
aBotCoord += normalize(startBotJoin * normalWidth) * abClipping;
138138
}
139139

140-
vec2 aTopPosition = (aTopCoord + translate) * scale;
141-
vec2 aBotPosition = (aBotCoord + translate) * scale;
140+
vec2 aTopPosition = (aTopCoord) * scale + translate;
141+
vec2 aBotPosition = (aBotCoord) * scale + translate;
142142

143-
vec2 bTopPosition = (bTopCoord + translate) * scale;
144-
vec2 bBotPosition = (bBotCoord + translate) * scale;
143+
vec2 bTopPosition = (bTopCoord) * scale + translate;
144+
vec2 bBotPosition = (bBotCoord) * scale + translate;
145145

146146
//position is normalized 0..1 coord on the screen
147147
vec2 position = (aTopPosition * lineTop + aBotPosition * lineBot) * lineStart + (bTopPosition * lineTop + bBotPosition * lineBot) * lineEnd;
148148

149-
startCoord = (aCoord + translate) * scaleRatio + viewport.xy;
150-
endCoord = (bCoord + translate) * scaleRatio + viewport.xy;
149+
startCoord = aCoord * scaleRatio + translate * viewport.zw + viewport.xy;
150+
endCoord = bCoord * scaleRatio + translate * viewport.zw + viewport.xy;
151151

152152
gl_Position = vec4(position * 2.0 - 1.0, depth, 1);
153153

@@ -160,7 +160,7 @@ void main() {
160160
vec2 startMiterWidth = vec2(startJoinDirection) * thickness * miterLimit * .5;
161161
startCutoff = vec4(aCoord, aCoord);
162162
startCutoff.zw += vec2(-startJoinDirection.y, startJoinDirection.x) / scaleRatio;
163-
startCutoff = (startCutoff + translate.xyxy) * scaleRatio.xyxy;
163+
startCutoff = startCutoff * scaleRatio.xyxy + translate.xyxy * viewport.zwzw;
164164
startCutoff += viewport.xyxy;
165165
startCutoff += startMiterWidth.xyxy;
166166
}
@@ -169,7 +169,7 @@ void main() {
169169
vec2 endMiterWidth = vec2(endJoinDirection) * thickness * miterLimit * .5;
170170
endCutoff = vec4(bCoord, bCoord);
171171
endCutoff.zw += vec2(-endJoinDirection.y, endJoinDirection.x) / scaleRatio;
172-
endCutoff = (endCutoff + translate.xyxy) * scaleRatio.xyxy;
172+
endCutoff = endCutoff * scaleRatio.xyxy + translate.xyxy * viewport.zwzw;
173173
endCutoff += viewport.xyxy;
174174
endCutoff += endMiterWidth.xyxy;
175175
}
@@ -181,7 +181,7 @@ void main() {
181181
vec2 startMiterWidth = vec2(startJoinDirection) * thickness * abs(dot(startJoinDirection, currNormal)) * .5;
182182
startCutoff = vec4(aCoord, aCoord);
183183
startCutoff.zw += vec2(-startJoinDirection.y, startJoinDirection.x) / scaleRatio;
184-
startCutoff = (startCutoff + translate.xyxy) * scaleRatio.xyxy;
184+
startCutoff = startCutoff * scaleRatio.xyxy + translate.xyxy * viewport.zwzw;
185185
startCutoff += viewport.xyxy;
186186
startCutoff += startMiterWidth.xyxy;
187187
}
@@ -190,7 +190,7 @@ void main() {
190190
vec2 endMiterWidth = vec2(endJoinDirection) * thickness * abs(dot(endJoinDirection, currNormal)) * .5;
191191
endCutoff = vec4(bCoord, bCoord);
192192
endCutoff.zw += vec2(-endJoinDirection.y, endJoinDirection.x) / scaleRatio;
193-
endCutoff = (endCutoff + translate.xyxy) * scaleRatio.xyxy;
193+
endCutoff = endCutoff * scaleRatio.xyxy + translate.xyxy * viewport.zwzw;
194194
endCutoff += viewport.xyxy;
195195
endCutoff += endMiterWidth.xyxy;
196196
}

rect-vert.glsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ varying vec2 tangent;
1414
const float MAX_LINES = 256.;
1515

1616
vec2 project(vec2 position, vec2 positionFract, vec2 scale, vec2 scaleFract, vec2 translate, vec2 translateFract) {
17-
return (position + translate) * scale
18-
+ (positionFract + translateFract) * scale
19-
+ (position + translate) * scaleFract
20-
+ (positionFract + translateFract) * scaleFract;
17+
return position * scale
18+
+ positionFract * scale
19+
+ position * scaleFract + translate
20+
+ positionFract * scaleFract + translateFract;
2121
}
2222

2323
void main() {

test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,13 @@ panZoom(cnv, e => {
101101

102102

103103
/** Test cases */
104-
t.only('reverse direction', t => {
104+
t('aligned line', t => {
105105
batch.push(extend({}, options, {
106-
positions: [ 0, 0, 0, 1 ]
106+
positions: [ 0, 0, 1, 0 ],
107+
type: 'rect'
107108
}))
109+
110+
t.end()
108111
})
109112

110113
t('multiple points', t => {
@@ -196,7 +199,7 @@ t('closed path', t => {
196199
t.end()
197200
})
198201

199-
t('time case', t => {
202+
t.only('time case', t => {
200203
batch.push({
201204
type: 'rect',
202205
positions: [25741380000,1293840000000,25741380001,1293926400000,25741380002,1294012800000,25741380003,1294099200000,25741380004,1294185600000,1477434180000,1294272000000,1477434180001,1294358400000,1477434180002,1294444800000,1477434180003,1294531200000,1477434180004,1294617600000],

0 commit comments

Comments
 (0)